X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=gopher.sh;h=4fd277bc2cc3b551ddab79945463cff32f28af00;hb=4e5b044863a8371aa4f67d1ea9152292a15cc0ad;hp=a6cc44d7058aa659fe0fbeec1eb73a259c933333;hpb=528c6011e9364f138761e2658ec5941e0638dbf9;p=gofetch.git diff --git a/gopher.sh b/gopher.sh index a6cc44d..4fd277b 100755 --- a/gopher.sh +++ b/gopher.sh @@ -1,14 +1,22 @@ #!/bin/sh -# $0 [server] ([selector]) ([port]) ([mode]) -# server: the gopher server to dig to -# selector: the gopher selector (default is empty) -# port: the port to use (default is 70) -# mode: the filetype mode (default depends upon selector) -# 0: plain text -# 1: menu (dir-like) -# ... -# download: fake mode to download the result without changes +# $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 (non-dialog uses only), you can also +# enter 'n' or 'p' to get the next or previous item (+1 or -1) # ENV variables: # USE_DIALOG: force the usage of the command 'dialog' @@ -31,29 +39,41 @@ # 3: unknown selector mode # 255: special exit more 'q' -SERVER="$1" -SELECTOR="$2" -PORT="$3" -MODE="$4" +URI="$1" 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" | cut -f2 -d/`" - SELECTOR="`echo "$SELECTOR" | sed 's:^/[^/]*/::'`" + 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 + echo "Syntax error: $0 [gopher uri]" >&2 exit 1 fi @@ -66,7 +86,7 @@ EL= if [ "$LINK_COLOR" != "-" ]; then SL="`tput setf $LINK_COLOR``tput setaf $LINK_COLOR`" EL="`tput init`" - export LESS="${LESS}-R" + export LESS="${LESS} -R" fi # 'dialog' or text @@ -89,7 +109,7 @@ fi # Display a gopher menu for the given resource cat_menu() { i=0 - cat "$1" | grep "^i\|^$PREFIX" | while read ln; do + cat "$1" | grep "^i\|^$PREFIX" | sed 's:\\:\\\\\\\\:g' | while read ln; do if echo "$ln" | grep "^i" >/dev/null 2>&1; then if [ "$2" != dialog ]; then echo "$ln" | sed "s:^.\([^\t]*\).*$: \1:g" @@ -139,14 +159,24 @@ finish() { } trap finish EXIT -echo "$SELECTOR" | nc "$SERVER" "$PORT" > "$tmp" -if [ $? != 0 ]; then - echo Cannot contact gopher server "[$SERVER]" >&2 +ok=true +if [ "$PROTOCOL" = gopher:// -o "$PROTOCOL" = "" ]; then + echo "$SELECTOR" | nc "$SERVER" "$PORT" > "$tmp" || ok=false +else + 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::g' "$tmp" + sed --in-place 's:\r\n:\n:g;s:\r::g' "$tmp" fi # Process page content @@ -159,7 +189,7 @@ download) cat "$tmp" | less ;; 1|+) - CHOICE=start + CHOICE=0 while [ "$CHOICE" != "" ]; do if [ "$USE_DIALOG" = 1 ]; then > "$tmp.menu" @@ -190,7 +220,17 @@ download) rm "$tmp.choice" else 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 fi [ "$CHOICE" = q ] && exit 255 # force-quit @@ -200,8 +240,8 @@ download) goto_sel="`getsel "$tmp" $index 2`" goto_port="`getsel "$tmp" $index 4`" goto_mode="`getsel "$tmp" $index 1 | cut -c1`" - echo "Digging to [$goto_server:$goto_port] [$goto_sel]..." - 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