[home]__________________________________________________________________[light]

.-----------------------------------------------------------------------------.
| Description                                                                 |
'-----------------------------------------------------------------------------'
.-----------------------------------------------------------------------------.
| bfinger (bash.finger): A 100% LLM-generated command-line Finger client.     |
|                                                                             |
| The core was built by DeepSeek, reviewed by ChatGPT and refined through     |
| testing and feature requests.                                               |
|                                                                             |
| Pure Bash TCP Socket (/dev/tcp) - no raw sockets or external dependencies.  |
|                                                                             |
| Supports long files, preserves whitespace, and handles 8-bit characters,    |
| including Code Page 437 (e.g., bfinger codepage437@happynetbox.com).        |
|                                                                             |
| Examples:                                                                   |
|   bfinger @plan.cat | head -n 20                                            |
|   bfinger fingerverse@happynetbox.com | less                                |
'-----------------------------------------------------------------------------'
.-----------------------------------------------------------------------------.
| System-Wide Installation                                                    |
'-----------------------------------------------------------------------------'
.-----------------------------------------------------------------------------.
| sudo install bfinger /usr/local/bin/             ("chmod +x bfinger" first) |
| sudo cp -i bfinger /usr/local/bin/               (prompts before overwrite) |
'-----------------------------------------------------------------------------'
.-----------------------------------------------------------------------------.
| Changelog                                                                   |
'-----------------------------------------------------------------------------'
.-----------------------------------------------------------------------------.
| Version 1.0.1 : 05 May 2025 : Minor enhancements                            |
| - Removed the "=== Finger response from $HOST ===" header line              |
| - Added flags for accessing built-in help from the command line             |
| - Updated the built-in help section                                         |
|                                                                             |
| Version 1.0.0 : 01 May 2025 : Initial release                               |
| - A Bash-based Finger client                                                |
| - Queries Finger servers (RFC 1288) for user or system information          |
| - Uses built-in Bash TCP socket access: /dev/tcp/host/port                  |
| - Supports long files, 8-bit characters, and command-line parameters        |
| - Streams output byte-by-byte for precise rendering (though slower)         |
| - No dependencies beyond Bash and basic Unix utilities                      |
'-----------------------------------------------------------------------------'

#!/bin/bash

# Released into the Public Domain via Unlicense (2025).
# See: https://unlicense.org/

# Public Domain Finger Client: A Bash-based utility for querying Finger servers
# (RFC 1288) with full support for long files, 8-bit characters, and ASCII art.

set -eo pipefail  # Removed -u to avoid unbound variable errors
[ -n "${DEBUG:-}" ] && set -xu  # Optional: Only enable -u in debug mode

VERSION="1.0 (May 1, 2025)"

# Optional: enforce consistent locale for byte handling
export LC_ALL=C

# Cleanup on interrupt
cleanup() {
  exec 3<&- 3>&- 2>/dev/null || true
  exit 130
}
trap cleanup INT

# Show usage if no args or help flag
if [ $# -eq 0 ] || [[ "$1" =~ ^(/|\-){1,2}(\?|h|help)$ ]]; then
  echo ""
  echo "bfinger $VERSION - Public Domain Finger Client"
  echo ""
  echo "Usage:"
  echo "  bfinger @domain.com         # Default user"
  echo "  bfinger user@domain.com     # Specific user"
  echo "  bfinger user domain.com     # Alternative"
  echo ""
  echo "Options:"
  echo "  -v, --version               Show version info"
  echo "  -h, --help, -?, /?, -help   Show this help message"
  echo ""
  echo "Examples:"
  echo "  bfinger fingerverse@happynetbox.com | less"
  echo "  bfinger @plan.cat | head -n 20"
  echo ""
  echo "install:   chmod +x bfinger && sudo install bfinger /usr/local/bin"
  echo "uninstall: sudo rm /usr/local/bin/bfinger"
  echo ""
  exit 0
fi

# Show version if requested (now safely checking $1 after arg count check)
if [[ "${1:-}" == "--version" || "${1:-}" == "-v" ]]; then
  echo "bfinger $VERSION - Public Domain finger client"
  exit 0
fi

# Parse input
if [[ "$1" == @* ]]; then
  USER=""
  HOST="${1#@}"
elif [[ "$1" =~ @ ]]; then
  USER="${1%%@*}"
  HOST="${1##*@}"
else
  USER="${1:-}"
  HOST="${2:-}"
fi

PORT=79

# Validate host
if [ -z "$HOST" ]; then
  echo "Error: No host specified" >&2
  exit 1
fi

# Optional: basic hostname validation
if [[ ! "$HOST" =~ ^[a-zA-Z0-9._-]+$ ]]; then
  echo "Error: Hostname contains invalid characters" >&2
  exit 1
fi

# Optional: check DNS resolution
if ! getent hosts "$HOST" >/dev/null; then
  echo "Error: Host '$HOST' could not be resolved" >&2
  exit 1
fi

# Connect and stream response
if ! timeout 5 bash -c "exec 3<>/dev/tcp/$HOST/$PORT"; then
  echo "Error: Connection to $HOST:$PORT timed out or failed" >&2
  echo "Tip: Try 'telnet $HOST $PORT' to debug" >&2
  exit 1
fi
exec 3<>/dev/tcp/"$HOST"/"$PORT"

# Send request
printf "%s\r\n" "$USER" >&3

# Read response with 8-bit and whitespace preservation
while IFS= read -r -t 5 -N 1 char <&3; do
  printf "%s" "$char"
done

# Cleanup
exec 3<&- 3>&-
.------------------------------.  .--------------.  .--------------------.
|finger bfinger@happynetbox.com|--| May 05, 2025 |--| 640kb.neocities.org|
'------------------------------'  '--------------'  '--------------------'