| 1 | #!/bin/sh |
| 2 | |
| 3 | # $0 [gopher uri] ([mode]) |
| 4 | # uri: the gopher URI to dig to, e.g.: |
| 5 | # - gopher://sdf.org:70/1/faq |
| 6 | # - sdf.org/faq |
| 7 | # - sdf.org/1/faq |
| 8 | # - http://gopher.nikiroo.be:80/news |
| 9 | # - ... |
| 10 | # default port is 70 for gopher, 80 for http, 443 for https |
| 11 | # default filetype depends upon selector: |
| 12 | # - 0: plain text |
| 13 | # - 1: menu (dir-like) |
| 14 | # - ... |
| 15 | # - download: fake mode to download the result without changes |
| 16 | |
| 17 | # Manual: |
| 18 | # When asked for a number (non-dialog uses only), you can also |
| 19 | # enter 'n' or 'p' to get the next or previous item (+1 or -1) |
| 20 | |
| 21 | # ENV variables: |
| 22 | # USE_DIALOG: force the usage of the command 'dialog' |
| 23 | # : true if dialog is found |
| 24 | # 0 : do not |
| 25 | # 1 : force the use of dialog |
| 26 | # LINK_COLOR: escape sequences colour (def: 2) |
| 27 | # - : means no escape sequence |
| 28 | # 1 : means colour 1 |
| 29 | # 2 : means colour 2 |
| 30 | # [...] |
| 31 | # INVERT : invert the output for image viewing (for white backgrounds) |
| 32 | # 0 : do not invert (default) |
| 33 | # 1 : invert |
| 34 | |
| 35 | # EXIT Codes: |
| 36 | # 0: ok |
| 37 | # 1: syntax error |
| 38 | # 2: cannot contact server |
| 39 | # 3: unknown selector mode |
| 40 | # 255: special exit more 'q' |
| 41 | |
| 42 | URI="$1" |
| 43 | |
| 44 | PREFIX="[0-9hIg+]" |
| 45 | |
| 46 | PROTOCOL="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\1|g'`" |
| 47 | SERVER="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\2|g'`" |
| 48 | PORT="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\3|g'`" |
| 49 | SELECTOR="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\4|g'`" |
| 50 | MODE= |
| 51 | |
| 52 | PORT="`echo "$PORT" | sed 's/^://'`" |
| 53 | |
| 54 | # Defaults: |
| 55 | if [ "$PORT" = "" ];then |
| 56 | case "$PROTOCOL" in |
| 57 | http://) PORT=80 ;; |
| 58 | https://) PORT=443;; |
| 59 | *) PORT=70 ;; |
| 60 | esac |
| 61 | fi |
| 62 | |
| 63 | if [ "$MODE" = "" ]; then |
| 64 | # "" or dir-like selector? -> 1 ; if not -> 0 |
| 65 | echo "$SELECTOR" | grep "/$" >/dev/null && MODE=1 || MODE=0 |
| 66 | [ "$SELECTOR" = "" ] && MODE=1 |
| 67 | |
| 68 | # check explicit modes: |
| 69 | if echo "$SELECTOR" | grep "^/\?\($PREFIX\|download\)/" >/dev/null; then |
| 70 | MODE="`echo "$SELECTOR" | sed 's:^/\?\([^/]*\)/\(.*\):\1:'`" |
| 71 | SELECTOR="`echo "$SELECTOR" | sed 's:^/\?\([^/]*\)/\(.*\):\2:'`" |
| 72 | fi |
| 73 | fi |
| 74 | |
| 75 | if [ "$SERVER" = "" ]; then |
| 76 | echo "Syntax error: $0 [gopher uri]" >&2 |
| 77 | exit 1 |
| 78 | fi |
| 79 | |
| 80 | # can be "-" for no escape sequences |
| 81 | [ "$LINK_COLOR" = "" ] && LINK_COLOR=2 |
| 82 | |
| 83 | # Start and end link tags |
| 84 | SL= |
| 85 | EL= |
| 86 | if [ "$LINK_COLOR" != "-" ]; then |
| 87 | SL="`tput setf $LINK_COLOR``tput setaf $LINK_COLOR`" |
| 88 | EL="`tput init`" |
| 89 | export LESS="${LESS} -R" |
| 90 | fi |
| 91 | |
| 92 | # 'dialog' or text |
| 93 | if [ "$USE_DIALOG" = "" ]; then |
| 94 | if dialog --help >/dev/null 2>&1; then |
| 95 | USE_DIALOG=1 |
| 96 | else |
| 97 | USE_DIALOG=0 |
| 98 | fi |
| 99 | fi |
| 100 | |
| 101 | # Invert image viewer |
| 102 | if [ "$INVERT" = 1 ]; then |
| 103 | INVERT="--invert" |
| 104 | else |
| 105 | INVERT= |
| 106 | fi |
| 107 | |
| 108 | # $0 [FILE] (dialog) |
| 109 | # Display a gopher menu for the given resource |
| 110 | cat_menu() { |
| 111 | i=0 |
| 112 | cat "$1" | grep "^i\|^$PREFIX" | sed 's:\\:\\\\\\\\:g' | while read ln; do |
| 113 | if echo "$ln" | grep "^i" >/dev/null 2>&1; then |
| 114 | if [ "$2" != dialog ]; then |
| 115 | echo "$ln" | sed "s:^.\([^\t]*\).*$: \1:g" |
| 116 | else |
| 117 | echo "$ln" | sed 's:":'"''"':g;s:^.\([^\t]*\).*$:" " "\1":g' |
| 118 | fi |
| 119 | elif echo "$ln" | grep "^$PREFIX" >/dev/null 2>&1; then |
| 120 | i=`expr $i + 1` |
| 121 | [ $i -le 9 ] && i=0$i |
| 122 | field="`echo "$ln" | cut -c1`" |
| 123 | case "$field" in |
| 124 | 0) typ='TXT';; |
| 125 | 1) typ='DIR';; # menu, actually |
| 126 | 7) typ='(?)';; # query |
| 127 | 8) typ='TEL';; # TELnet (not TELephone) |
| 128 | h) typ='WEB';; # HTML |
| 129 | I) typ='IMG';; |
| 130 | g) typ='GIF';; |
| 131 | +) typ='SVR';; # redundant server |
| 132 | *) typ='!!!';; |
| 133 | esac |
| 134 | if [ "$2" != dialog ]; then |
| 135 | echo "$ln" | sed "s:^.\\([^\t]*\\).*:$typ $i $SL\\1$EL:g" |
| 136 | else |
| 137 | echo "$ln" | sed "s:"'"'":'':g;s:^.\\([^\t]*\\).*:"'"'"$typ $i"'"'" "'"\1"'":g" |
| 138 | fi |
| 139 | fi |
| 140 | done |
| 141 | } |
| 142 | |
| 143 | # $0 [FILE] [INDEX] [FIELD] |
| 144 | # Get a field from the given-by-index link in FILE |
| 145 | # |
| 146 | # Fields: |
| 147 | # 1 = type/name |
| 148 | # 2 = selector |
| 149 | # 3 = server |
| 150 | # 4 = port |
| 151 | getsel() { |
| 152 | cat "$1" | grep "^$PREFIX" | tail -n+"$2" | head -n 1 | cut -f"$3" |
| 153 | } |
| 154 | |
| 155 | # Save page content to 'tmp' file |
| 156 | tmp="`mktemp -t gofetch.current_page.XXXXXX`" |
| 157 | finish() { |
| 158 | rm -rf "$tmp" "$tmp.jpg" "$tmp.menu" "$tmp.choice" |
| 159 | } |
| 160 | trap finish EXIT |
| 161 | |
| 162 | ok=true |
| 163 | if [ "$PROTOCOL" = gopher:// -o "$PROTOCOL" = "" ]; then |
| 164 | echo "$SELECTOR" | nc "$SERVER" "$PORT" > "$tmp" || ok=false |
| 165 | else |
| 166 | if wget -h >/dev/null 2>&1; then |
| 167 | wget "${PROTOCOL}$SERVER:$PORT/$SELECTOR" -O "$tmp" >/dev/null 2>&1 |
| 168 | else |
| 169 | curl "${PROTOCOL}$SERVER:$PORT/$SELECTOR" > "$tmp" 2>/dev/null |
| 170 | fi |
| 171 | fi |
| 172 | |
| 173 | if [ $ok = false ]; then |
| 174 | echo Cannot contact gopher uri "[$URI]" >&2 |
| 175 | exit 2 |
| 176 | fi |
| 177 | |
| 178 | if [ $MODE = 1 ]; then |
| 179 | sed --in-place 's:\r\n:\n:g;s:\r::g' "$tmp" |
| 180 | fi |
| 181 | |
| 182 | # Process page content |
| 183 | case "$MODE" in |
| 184 | download) |
| 185 | # Special, fake mode, only from top-level |
| 186 | cat "$tmp" |
| 187 | ;; |
| 188 | 0) |
| 189 | cat "$tmp" | less |
| 190 | ;; |
| 191 | 1|+) |
| 192 | CHOICE=0 |
| 193 | while [ "$CHOICE" != "" ]; do |
| 194 | if [ "$USE_DIALOG" = 1 ]; then |
| 195 | > "$tmp.menu" |
| 196 | cat_menu "$tmp" dialog | while read ln; do |
| 197 | echo -n " $ln" >> "$tmp.menu" |
| 198 | done |
| 199 | [ "$LINES" = "" ] && LINES=`tput lines` |
| 200 | [ "$COLUMNS" = "" ] && COLUMNS=`tput cols` |
| 201 | title="$SERVER: $SELECTOR" |
| 202 | dialog --extra-button --extra-label Back \ |
| 203 | --cancel-label Exit \ |
| 204 | --no-shadow \ |
| 205 | --menu "$title" \ |
| 206 | "$LINES" "$COLUMNS" "$LINES" \ |
| 207 | --file "$tmp.menu" 2>"$tmp.choice" |
| 208 | val=$? |
| 209 | clear |
| 210 | |
| 211 | if [ $val = 3 ]; then |
| 212 | CHOICE="" |
| 213 | elif [ $val = 1 ]; then |
| 214 | CHOICE="q" |
| 215 | else |
| 216 | CHOICE="`cat "$tmp.choice" | cut -c5-`" |
| 217 | fi |
| 218 | |
| 219 | rm "$tmp.menu" |
| 220 | rm "$tmp.choice" |
| 221 | else |
| 222 | cat_menu "$tmp" | less |
| 223 | read -p "[$SELECTOR]: " NEW_CHOICE |
| 224 | if [ "$NEW_CHOICE" = p ]; then |
| 225 | CHOICE=`expr "$CHOICE" - 1 2>/dev/null` |
| 226 | if [ "$CHOICE" -lt 0 ]; then |
| 227 | CHOICE=`cat "$tmp" | grep "^$PREFIX" | wc --lines` |
| 228 | fi |
| 229 | elif [ "$NEW_CHOICE" = n ]; then |
| 230 | CHOICE=`expr "$CHOICE" + 1 2>/dev/null` |
| 231 | else |
| 232 | CHOICE="$NEW_CHOICE" |
| 233 | fi |
| 234 | fi |
| 235 | |
| 236 | [ "$CHOICE" = q ] && exit 255 # force-quit |
| 237 | index="`expr 1 \* "$CHOICE" 2>/dev/null`" |
| 238 | if [ "$index" != "" ]; then |
| 239 | goto_server="`getsel "$tmp" $index 3`" |
| 240 | goto_sel="`getsel "$tmp" $index 2`" |
| 241 | goto_port="`getsel "$tmp" $index 4`" |
| 242 | goto_mode="`getsel "$tmp" $index 1 | cut -c1`" |
| 243 | echo "Digging to [$goto_server:$goto_port] $index [$goto_sel]..." |
| 244 | sh "$0" "$goto_server:$goto_port/$goto_mode/$goto_sel" |
| 245 | [ $? = 255 ] && exit 255 # force-quit |
| 246 | fi |
| 247 | done |
| 248 | ;; |
| 249 | 7) |
| 250 | read -p "Query: " SEARCH |
| 251 | selQuery="$SELECTOR $SEARCH" |
| 252 | |
| 253 | exec sh "$0" "$SERVER" "$selQuery" "$PORT" 1 |
| 254 | ;; |
| 255 | 8) |
| 256 | telnet -l "$SELECTOR" "$SERVER" "$PORT" |
| 257 | read DUMMY |
| 258 | ;; |
| 259 | 9) |
| 260 | echo "<BINARY FILE>" | less |
| 261 | ;; |
| 262 | g|I) |
| 263 | if img2aa --help >/dev/null 2>&1; then |
| 264 | img2aa --mode=DITHERING \ |
| 265 | $INVERT \ |
| 266 | --width=74 "$tmp" | less |
| 267 | elif jp2a -h >/dev/null 2>&1; then |
| 268 | if convert -h >/dev/null 2>&1; then |
| 269 | convert "$tmp" "$tmp.jpg" |
| 270 | jp2a --border --chars=" .-+=o8#"\ |
| 271 | $INVERT \ |
| 272 | --width=74 "$tmp.jpg" | less |
| 273 | else |
| 274 | echo "required program not found to view images: convert" \ |
| 275 | | less |
| 276 | fi |
| 277 | else |
| 278 | echo "required program not found to view images:" \ |
| 279 | jp2a or img2aa | less |
| 280 | fi |
| 281 | ;; |
| 282 | *) |
| 283 | echo "unknwon selector mode: <$MODE>" | less |
| 284 | exit 3 |
| 285 | ;; |
| 286 | esac |
| 287 | |