generated is better than vendor in this case
[gofetch.git] / gopher.sh
index 0042756538be4be9758e701ce79fe04c462b2b0a..5d7439f82955a160c904379315aebfc34884bed1 100755 (executable)
--- a/gopher.sh
+++ b/gopher.sh
@@ -1,28 +1,76 @@
 #!/bin/sh
 
+# $0 [gopher uri] ([mode])
+#      uri: the gopher URI to dig to, e.g.:
+#              - gopher://sdf.org:70/1/faq
+#              - sdf.org/faq
+#              - sdf.org/1/faq
+#              - http://gopher.nikiroo.be:80/news
+#              - ...
+#              default port is 70 for gopher, 80 for http, 443 for https
+#              default filetype depends upon selector:
+#              - 0: plain text
+#              - 1: menu (dir-like)
+#              - ...
+#              - download: fake mode to download the result without changes
+
+# Manual:
+#      When asked for a number, you can also enter 'n' or 'p' to get the 
+#      next or previous item (+1 or -1)
+
 # ENV variables:
 #      LINK_COLOR: escape sequences colour (def: 2)
 #              - : means no escape sequence
 #              1 : means colour 1
 #              2 : means colour 2
 #              [...]
+#      INVERT    : invert the output for image viewing (for white backgrounds)
+#              0 : do not invert (default)
+#              1 : invert 
+
+# EXIT Codes:
+#        0: ok
+#        1: syntax error
+#        2: cannot contact server
+#        3: unknown selector mode
+#      255: special exit more 'q'
+
+URI="$1"
 
-SERVER="$1"
-SELECTOR="$2"
-PORT="$3"
-MODE="$4"
+PREFIX="[0-9hIg+]"
+
+PROTOCOL="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\1|g'`"
+SERVER="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\2|g'`"
+PORT="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\3|g'`"
+SELECTOR="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\4|g'`"
+MODE=
+
+PORT="`echo "$PORT" | sed 's/^://'`"
 
 # Defaults:
-[ "$PORT" = "" ] && PORT=70
+if [ "$PORT" = "" ];then
+       case "$PROTOCOL" in
+       http://)  PORT=80 ;;
+       https://) PORT=443;;
+       *)     PORT=70 ;;
+       esac
+fi
+
 if [ "$MODE" = "" ]; then
        # "" or dir-like selector? -> 1 ; if not -> 0 
        echo "$SELECTOR" | grep "/$" >/dev/null && MODE=1 || MODE=0
        [ "$SELECTOR" = "" ] && MODE=1
+       
+       # check explicit modes:
+       if echo "$SELECTOR" | grep "^/\?\($PREFIX\|download\)/" >/dev/null; then
+               MODE="`echo "$SELECTOR" | sed 's:^/\?\([^/]*\)/\(.*\):\1:'`"
+               SELECTOR="`echo "$SELECTOR" | sed 's:^/\?\([^/]*\)/\(.*\):\2:'`"
+       fi
 fi
 
 if [ "$SERVER" = "" ]; then
-       echo "Syntax error: $0 [SERVER] ([SELECTOR]) ([PORT]) ([MODE])" >&2
-       exit 2
+       echo "Syntax error: $0 [gopher uri]" >&2
+       exit 1
 fi
 
 # can be "-" for no escape sequences
@@ -33,31 +81,40 @@ SL=
 EL=
 if [ "$LINK_COLOR" != "-" ]; then
        SL="`tput setf $LINK_COLOR``tput setaf $LINK_COLOR`"
-       EL="`tput init`";
+       EL="`tput init`"
+       export LESS="${LESS} -R"
+fi
+
+# Invert image viewer
+if [ "$INVERT" = 1 ]; then
+       INVERT="--invert"
+else
+       INVERT=
 fi
 
 # $0 [FILE]
 # Display a gopher menu for the given resource
 cat_menu() {
        i=0
-       cat "$1" | grep '^[i0-9]' | while read ln; do
-               ln="`echo "$ln" | cut -f1`"
+       cat "$1" | grep "^i\|^$PREFIX" | sed 's:\\:\\\\\\\\:g' | while read ln; do
                if echo "$ln" | grep "^i" >/dev/null 2>&1; then
-                       echo "$ln" | sed "s:^.:g"
-               elif echo "$ln" | grep "^[0-9]" >/dev/null 2>&1; then
+                       echo "$ln" | sed "s:^.\([^\t]*\).*$:    \1:g"
+               elif echo "$ln" | grep "^$PREFIX" >/dev/null 2>&1; then
                        i=`expr $i + 1`
-                       i=`printf %2.f $i`
+                       [ $i -le 9 ] && i=0$i 
                        field="`echo "$ln" | cut -c1`"
                        case "$field" in
                                0) typ='TXT';;
-                               1) typ='DIR';;
-                               7) typ='(?)';;
-                               8) typ='TEL';;
+                               1) typ='DIR';; # menu, actually
+                               7) typ='(?)';; # query
+                               8) typ='TEL';; # TELnet (not TELephone)
+                               h) typ='WEB';; # HTML
+                               I) typ='IMG';;
+                               g) typ='GIF';;
+                               +) typ='SVR';; # redundant server
                                *) typ='!!!';;
                        esac
-                       echo "$ln" | sed "s:^.\\(.*\\):$typ $i  $SL\\1$EL:g"
-               #else
-                       # Bad line
+                       echo "$ln" | sed "s:^.\\([^\t]*\\).*:$typ $i    $SL\\1$EL:g"
                fi
        done
 }
@@ -71,37 +128,71 @@ cat_menu() {
 #      3 = server
 #      4 = port
 getsel() {
-       cat "$1" | grep '^[0-9]' | tail -n+"$2" | head -n 1 | cut -f"$3"
+       cat "$1" | grep "^$PREFIX" | tail -n+"$2" | head -n 1 | cut -f"$3"
 }
 
+# Save page content to 'tmp' file
 tmp="`mktemp -t gofetch.current_page.XXXXXX`"
 finish() {
-  rm -rf "$tmp"
+  rm -rf "$tmp" "$tmp.jpg" "$tmp.menu" "$tmp.choice"
 }
 trap finish EXIT
 
-if [ $MODE = 1 ]; then
-       echo "$SELECTOR" | nc "$SERVER" "$PORT" | sed 's:\r::g' > "$tmp"
+ok=true
+if [ "$PROTOCOL" = gopher:// -o "$PROTOCOL" = "" ]; then
+       echo "$SELECTOR" | nc "$SERVER" "$PORT" > "$tmp" || ok=false
 else
-       echo "$SELECTOR" | nc "$SERVER" "$PORT" > "$tmp"
+       if wget -h >/dev/null 2>&1; then
+               wget "${PROTOCOL}$SERVER:$PORT/$SELECTOR" -O "$tmp" >/dev/null 2>&1
+       else
+               curl "${PROTOCOL}$SERVER:$PORT/$SELECTOR" > "$tmp" 2>/dev/null
+       fi
+fi
+
+if [ $ok = false ]; then
+       echo Cannot contact gopher uri "[$URI]" >&2
+       exit 2
+fi
+
+if [ $MODE = 1 ]; then
+       sed --in-place 's:\r\n:\n:g;s:\r::g' "$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
+1|+)
+       CHOICE=0
        while [ "$CHOICE" != "" ]; do
                cat_menu "$tmp" | less
-               read -p "[$SELECTOR]: " CHOICE
+               read -p "[$SELECTOR]: " NEW_CHOICE
+               if [ "$NEW_CHOICE" = p ]; then
+                       CHOICE=`expr "$CHOICE" - 1 2>/dev/null`
+                       if [ "$CHOICE" -lt 0 ]; then
+                               CHOICE=`cat "$tmp" | grep "^$PREFIX" | wc --lines`
+                       fi
+               elif [ "$NEW_CHOICE" = n ]; then
+                       CHOICE=`expr "$CHOICE" + 1 2>/dev/null`
+               else
+                       CHOICE="$NEW_CHOICE"
+               fi
+               
+               [ "$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"
+                       echo "Digging to [$goto_server:$goto_port] $index [$goto_sel]..."
+                       sh "$0" "$goto_server:$goto_port/$goto_mode/$goto_sel"
+                       [ $? = 255 ] && exit 255 # force-quit
                fi
        done
 ;;
@@ -118,8 +209,29 @@ case "$MODE" in
 9)
        echo "<BINARY FILE>" | less
 ;;
+g|I)
+       if img2aa --help >/dev/null 2>&1; then
+               img2aa --mode=DITHERING \
+                       $INVERT \
+                       --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#"\
+                               $INVERT \
+                               --width=74 "$tmp.jpg" | less
+               else
+                       echo "required program not found to view images: convert" \
+                               | less
+               fi
+       else
+               echo "required program not found to view images:" \
+                       jp2a or img2aa | less
+       fi
+;;
 *)
        echo "unknwon selector mode: <$MODE>" | less
        exit 3
 ;;
 esac
+