| 1 | #!/bin/sh |
| 2 | |
| 3 | # $0 [server] ([selector]) ([port]) ([mode]) |
| 4 | # server: the gopher server to dig to |
| 5 | # selector: the gopher selector (default is empty) |
| 6 | # port: the port to use (default is 70) |
| 7 | # mode: the filetype mode (default depends upon selector) |
| 8 | # 0: plain text |
| 9 | # 1: menu (dir-like) |
| 10 | # ... |
| 11 | # download: fake mode to download the result without changes |
| 12 | |
| 13 | # ENV variables: |
| 14 | # LESS: will be used with 'less' (think: "export LESS=-r") |
| 15 | # LINK_COLOR: escape sequences colour (def: 2) |
| 16 | # - : means no escape sequence |
| 17 | # 1 : means colour 1 |
| 18 | # 2 : means colour 2 |
| 19 | # [...] |
| 20 | |
| 21 | SERVER="$1" |
| 22 | SELECTOR="$2" |
| 23 | PORT="$3" |
| 24 | MODE="$4" |
| 25 | |
| 26 | # Defaults: |
| 27 | [ "$PORT" = "" ] && PORT=70 |
| 28 | if [ "$MODE" = "" ]; then |
| 29 | # "" or dir-like selector? -> 1 ; if not -> 0 |
| 30 | echo "$SELECTOR" | grep "/$" >/dev/null && MODE=1 || MODE=0 |
| 31 | [ "$SELECTOR" = "" ] && MODE=1 |
| 32 | fi |
| 33 | |
| 34 | if [ "$SERVER" = "" ]; then |
| 35 | echo "Syntax error: $0 [SERVER] ([SELECTOR]) ([PORT]) ([MODE])" >&2 |
| 36 | exit 2 |
| 37 | fi |
| 38 | |
| 39 | # can be "-" for no escape sequences |
| 40 | [ "$LINK_COLOR" = "" ] && LINK_COLOR=2 |
| 41 | |
| 42 | # Start and end link tags |
| 43 | SL= |
| 44 | EL= |
| 45 | if [ "$LINK_COLOR" != "-" ]; then |
| 46 | SL="`tput setf $LINK_COLOR``tput setaf $LINK_COLOR`" |
| 47 | EL="`tput init`"; |
| 48 | fi |
| 49 | |
| 50 | PREFIX="[0-9hIg]" |
| 51 | |
| 52 | # $0 [FILE] |
| 53 | # Display a gopher menu for the given resource |
| 54 | cat_menu() { |
| 55 | i=0 |
| 56 | cat "$1" | grep "^i\|^$PREFIX" | while read ln; do |
| 57 | ln="`echo "$ln" | cut -f1`" |
| 58 | if echo "$ln" | grep "^i" >/dev/null 2>&1; then |
| 59 | echo "$ln" | sed "s:^.: :g" |
| 60 | elif echo "$ln" | grep "^$PREFIX" >/dev/null 2>&1; then |
| 61 | i=`expr $i + 1` |
| 62 | i=`printf %2.f $i` |
| 63 | field="`echo "$ln" | cut -c1`" |
| 64 | case "$field" in |
| 65 | 0) typ='TXT';; |
| 66 | 1) typ='DIR';; # menu, actually |
| 67 | 7) typ='(?)';; # query |
| 68 | 8) typ='TEL';; # TELnet (not TELephone) |
| 69 | h) typ='WEB';; # HTML |
| 70 | g) typ='GIF';; |
| 71 | I) typ='IMG';; |
| 72 | +) typ='SVR';; # redundant server |
| 73 | *) typ='!!!';; |
| 74 | esac |
| 75 | echo "$ln" | sed "s:^.\\(.*\\):$typ $i $SL\\1$EL:g" |
| 76 | #else |
| 77 | # Bad line |
| 78 | fi |
| 79 | done |
| 80 | } |
| 81 | |
| 82 | # $0 [FILE] [INDEX] [FIELD] |
| 83 | # Get a field from the given-by-index link in FILE |
| 84 | # |
| 85 | # Fields: |
| 86 | # 1 = type/name |
| 87 | # 2 = selector |
| 88 | # 3 = server |
| 89 | # 4 = port |
| 90 | getsel() { |
| 91 | cat "$1" | grep "^$PREFIX" | tail -n+"$2" | head -n 1 | cut -f"$3" |
| 92 | } |
| 93 | |
| 94 | # Save page content to 'tmp' file |
| 95 | tmp="`mktemp -t gofetch.current_page.XXXXXX`" |
| 96 | finish() { |
| 97 | rm -rf "$tmp" "$tmp.jpg" |
| 98 | } |
| 99 | trap finish EXIT |
| 100 | |
| 101 | if [ $MODE = 1 ]; then |
| 102 | echo "$SELECTOR" | nc "$SERVER" "$PORT" | sed 's:\r::g' > "$tmp" |
| 103 | else |
| 104 | echo "$SELECTOR" | nc "$SERVER" "$PORT" > "$tmp" |
| 105 | fi |
| 106 | |
| 107 | # Process page content |
| 108 | case "$MODE" in |
| 109 | download) |
| 110 | # Special, fake mode, only from top-level |
| 111 | cat "$tmp" |
| 112 | ;; |
| 113 | 0) |
| 114 | cat "$tmp" | less |
| 115 | ;; |
| 116 | 1) |
| 117 | CHOICE=start |
| 118 | while [ "$CHOICE" != "" ]; do |
| 119 | cat_menu "$tmp" | less |
| 120 | read -p "[$SELECTOR]: " CHOICE |
| 121 | index="`expr 1 \* "$CHOICE" 2>/dev/null`" |
| 122 | if [ "$index" != "" ]; then |
| 123 | goto_server="`getsel "$tmp" $index 3`" |
| 124 | goto_sel="`getsel "$tmp" $index 2`" |
| 125 | goto_port="`getsel "$tmp" $index 4`" |
| 126 | goto_mode="`getsel "$tmp" $index 1 | cut -c1`" |
| 127 | sh "$0" "$goto_server" "$goto_sel" "$goto_port" "$goto_mode" |
| 128 | fi |
| 129 | done |
| 130 | ;; |
| 131 | 7) |
| 132 | read -p "Query: " SEARCH |
| 133 | selQuery="$SELECTOR $SEARCH" |
| 134 | |
| 135 | exec sh "$0" "$SERVER" "$selQuery" "$PORT" 1 |
| 136 | ;; |
| 137 | 8) |
| 138 | telnet -l "$SELECTOR" "$SERVER" "$PORT" |
| 139 | read DUMMY |
| 140 | ;; |
| 141 | 9) |
| 142 | echo "<BINARY FILE>" | less |
| 143 | ;; |
| 144 | g|I) |
| 145 | if img2aa --help >/dev/null 2>&1; then |
| 146 | img2aa --mode=DITHERING \ |
| 147 | --width=74 "$tmp" | less |
| 148 | elif jp2a -h >/dev/null 2>&1; then |
| 149 | if convert -h >/dev/null 2>&1; then |
| 150 | convert "$tmp" "$tmp.jpg" |
| 151 | jp2a --border --chars=" .-+=o8#"\ |
| 152 | --width=74 "$tmp.jpg" | less |
| 153 | else |
| 154 | echo "required program not found to view images: convert" \ |
| 155 | | less |
| 156 | fi |
| 157 | else |
| 158 | echo "required program not found to view images:" \ |
| 159 | jp2a or img2aa | less |
| 160 | fi |
| 161 | ;; |
| 162 | *) |
| 163 | echo "unknwon selector mode: <$MODE>" | less |
| 164 | exit 3 |
| 165 | ;; |
| 166 | esac |