#!/usr/bin/env bash # SPDX-License-Identifier: MIT # finger.bash # A simple finger client written in pure Bash # Copyright © 2025 Giuseppe Masino # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the “Software”), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. set -o errexit set -o nounset set -o pipefail shopt -s lastpipe log() { builtin echo "finger.bash: $*" >&2 ;} info() { log "INFO: $*" ;} warn() { log "WARN: $*" ;} error() { log "ERROR: $*" ; } finger() { if ! [[ "$*" =~ (^[^@ ]+)?(@([^\/: ]+)(:([0-9]+))?)? ]] ; then error "'$1'" is not a valid finger URI exit 1 fi declare -g subject="${BASH_REMATCH[1]:-}" declare -g host="${BASH_REMATCH[3]:-${host:-localhost}}" declare -g port="${BASH_REMATCH[5]:-${port:-79}}" npCharWarned=false info "querying $host:$port" exec 3<>"/dev/tcp/${host}/${port}" if ! [ -e /proc/$$/fd/3 ] ; then error "failed establishing connection" exit 1 fi printf "%s\r\n" "$subject" >&3 while read -r ; do if [[ "$REPLY" =~ "[^ -~]+" ]] && ! $npCharWarned ; then warn "server returned non printable data. Stripping it." npCharWarned=true fi echo "${REPLY//[^ -~]/}" done <&3 npCharWarned=false } if ! [ "$1" = "-i" ] ; then finger "$@" && exit 0 else shift finger "$@" while read -p 'finger> ' -r query ; do case "$query" in ('') ;; (\?) finger ;; ('\?') finger ? ;; (*) finger "$query" ;; esac ; done fi