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