-i=0
-while [ -e "$PIDF" ]; do
- i=`expr $i + 1`
- [ $i -gt "$MAXCON" ] && i=1
- fifo="$tmpd/fifo.$i"
- rm -f "$fifo" 2>/dev/null
- mkfifo "$fifo"
- < "$fifo" nc -l -q0 -w"$TIMEOUT" "$ADDR" -p "$port" | (
- if [ -e "$PIDF" ]; then
- read -r query
- selector="`echo "$query" | cut -f1 -d' ' | sed 's:[\r\n]::g'`"
- query="`echo "$query" | cut -f2 -d' ' | sed 's:[\r\n]::g'`"
- if [ "$query" = "" ]; then
- if [ "$defn" = "" ]; then
- echo "7Search $selector $ADDR $port"
- else
- echo "$defn"
- fi
- else
- QUERY_STRING="$query" "$prog" 2>> "$LOG" &
- fi
+# Save page content to 'tmp' file
+tmp="`mktemp -t gofetch.current_page.XXXXXX`"
+finish() {
+ rm -rf "$tmp" "$tmp.jpg"
+}
+trap finish EXIT
+
+if [ $MODE = 1 ]; then
+ echo "$SELECTOR" | nc "$SERVER" "$PORT" | sed 's:\r::g' > "$tmp"
+else
+ echo "$SELECTOR" | nc "$SERVER" "$PORT" > "$tmp"
+fi
+
+# Process page content
+case "$MODE" in
+download)
+ # Special, fake mode, only from top-level
+ cat "$tmp"
+;;
+0)
+ cat "$tmp" | less
+;;
+1|+)
+ CHOICE=start
+ while [ "$CHOICE" != "" ]; do
+ cat_menu "$tmp" | less
+ read -p "[$SELECTOR]: " CHOICE
+ [ "$CHOICE" = q ] && exit 255 # force-quit
+ index="`expr 1 \* "$CHOICE" 2>/dev/null`"
+ if [ "$index" != "" ]; then
+ goto_server="`getsel "$tmp" $index 3`"
+ goto_sel="`getsel "$tmp" $index 2`"
+ goto_port="`getsel "$tmp" $index 4`"
+ goto_mode="`getsel "$tmp" $index 1 | cut -c1`"
+ sh "$0" "$goto_server" "$goto_sel" "$goto_port" "$goto_mode"
+ [ $? = 255 ] && exit 255 # force-quit
+ fi
+ done
+;;
+7)
+ read -p "Query: " SEARCH
+ selQuery="$SELECTOR $SEARCH"
+
+ exec sh "$0" "$SERVER" "$selQuery" "$PORT" 1
+;;
+8)
+ telnet -l "$SELECTOR" "$SERVER" "$PORT"
+ read DUMMY
+;;
+9)
+ echo "<BINARY FILE>" | less
+;;
+g|I)
+ if img2aa --help >/dev/null 2>&1; then
+ img2aa --mode=DITHERING \
+ --width=74 "$tmp" | less
+ elif jp2a -h >/dev/null 2>&1; then
+ if convert -h >/dev/null 2>&1; then
+ convert "$tmp" "$tmp.jpg"
+ jp2a --border --chars=" .-+=o8#"\
+ --width=74 "$tmp.jpg" | less
+ else
+ echo "required program not found to view images: convert" \
+ | less