Commit | Line | Data |
---|---|---|
0eb21b21 NR |
1 | #!/bin/sh |
2 | ||
7f7487a6 NR |
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 | ||
c6852b70 | 13 | # ENV variables: |
370b6826 NR |
14 | # USE_DIALOG: force the usage of the command 'dialog' |
15 | # : true if dialog is found | |
16 | # 0 : do not | |
17 | # 1 : force the use of dialog | |
c6852b70 NR |
18 | # LINK_COLOR: escape sequences colour (def: 2) |
19 | # - : means no escape sequence | |
20 | # 1 : means colour 1 | |
21 | # 2 : means colour 2 | |
22 | # [...] | |
3c2abf21 NR |
23 | # INVERT : invert the output for image viewing (for white backgrounds) |
24 | # 0 : do not invert (default) | |
25 | # 1 : invert | |
c6852b70 | 26 | |
0eb21b21 NR |
27 | SERVER="$1" |
28 | SELECTOR="$2" | |
29 | PORT="$3" | |
30 | MODE="$4" | |
31 | ||
61fcec09 NR |
32 | PREFIX="[0-9hIg+]" |
33 | ||
0eb21b21 NR |
34 | # Defaults: |
35 | [ "$PORT" = "" ] && PORT=70 | |
36 | if [ "$MODE" = "" ]; then | |
37 | # "" or dir-like selector? -> 1 ; if not -> 0 | |
38 | echo "$SELECTOR" | grep "/$" >/dev/null && MODE=1 || MODE=0 | |
39 | [ "$SELECTOR" = "" ] && MODE=1 | |
61fcec09 NR |
40 | |
41 | # check explicit modes: | |
42 | if echo "$SELECTOR" | grep "^/\($PREFIX\|download\)/" >/dev/null; then | |
43 | MODE="`echo "$SELECTOR" | cut -f2 -d/`" | |
44 | SELECTOR="`echo "$SELECTOR" | sed 's:^/[^/]*/::'`" | |
45 | fi | |
0eb21b21 NR |
46 | fi |
47 | ||
48 | if [ "$SERVER" = "" ]; then | |
49 | echo "Syntax error: $0 [SERVER] ([SELECTOR]) ([PORT]) ([MODE])" >&2 | |
50 | exit 2 | |
51 | fi | |
52 | ||
c6852b70 NR |
53 | # can be "-" for no escape sequences |
54 | [ "$LINK_COLOR" = "" ] && LINK_COLOR=2 | |
050494fa NR |
55 | |
56 | # Start and end link tags | |
57 | SL= | |
58 | EL= | |
c6852b70 | 59 | if [ "$LINK_COLOR" != "-" ]; then |
050494fa | 60 | SL="`tput setf $LINK_COLOR``tput setaf $LINK_COLOR`" |
1ad44e82 | 61 | EL="`tput init`" |
8210ebed | 62 | export LESS="${LESS}-R" |
050494fa NR |
63 | fi |
64 | ||
370b6826 NR |
65 | # 'dialog' or text |
66 | if [ "$USE_DIALOG" = "" ]; then | |
67 | if dialog --help >/dev/null 2>&1; then | |
68 | USE_DIALOG=1 | |
69 | else | |
70 | USE_DIALOG=0 | |
71 | fi | |
72 | fi | |
73 | ||
3c2abf21 NR |
74 | # Invert image viewer |
75 | if [ "$INVERT" = 1 ]; then | |
76 | INVERT="--invert" | |
77 | else | |
78 | INVERT= | |
79 | fi | |
80 | ||
370b6826 | 81 | # $0 [FILE] (dialog) |
050494fa | 82 | # Display a gopher menu for the given resource |
0eb21b21 NR |
83 | cat_menu() { |
84 | i=0 | |
7f7487a6 | 85 | cat "$1" | grep "^i\|^$PREFIX" | while read ln; do |
0eb21b21 | 86 | if echo "$ln" | grep "^i" >/dev/null 2>&1; then |
370b6826 NR |
87 | if [ "$2" != dialog ]; then |
88 | echo "$ln" | sed "s:^.\([^\t]*\).*$: \1:g" | |
89 | else | |
90 | echo "$ln" | sed 's:":'"''"':g;s:^.\([^\t]*\).*$:" " "\1":g' | |
91 | fi | |
7f7487a6 | 92 | elif echo "$ln" | grep "^$PREFIX" >/dev/null 2>&1; then |
0eb21b21 | 93 | i=`expr $i + 1` |
1ad44e82 | 94 | [ $i -le 9 ] && i=0$i |
0eb21b21 NR |
95 | field="`echo "$ln" | cut -c1`" |
96 | case "$field" in | |
97 | 0) typ='TXT';; | |
7f7487a6 NR |
98 | 1) typ='DIR';; # menu, actually |
99 | 7) typ='(?)';; # query | |
100 | 8) typ='TEL';; # TELnet (not TELephone) | |
101 | h) typ='WEB';; # HTML | |
7f7487a6 | 102 | I) typ='IMG';; |
1ad44e82 | 103 | g) typ='GIF';; |
7f7487a6 | 104 | +) typ='SVR';; # redundant server |
050494fa | 105 | *) typ='!!!';; |
0eb21b21 | 106 | esac |
370b6826 NR |
107 | if [ "$2" != dialog ]; then |
108 | echo "$ln" | sed "s:^.\\([^\t]*\\).*:$typ $i $SL\\1$EL:g" | |
109 | else | |
110 | echo "$ln" | sed "s:"'"'":'':g;s:^.\\([^\t]*\\).*:"'"'"$typ $i"'"'" "'"\1"'":g" | |
111 | fi | |
0eb21b21 NR |
112 | fi |
113 | done | |
114 | } | |
115 | ||
0eb21b21 | 116 | # $0 [FILE] [INDEX] [FIELD] |
050494fa NR |
117 | # Get a field from the given-by-index link in FILE |
118 | # | |
0eb21b21 NR |
119 | # Fields: |
120 | # 1 = type/name | |
121 | # 2 = selector | |
122 | # 3 = server | |
123 | # 4 = port | |
124 | getsel() { | |
7f7487a6 | 125 | cat "$1" | grep "^$PREFIX" | tail -n+"$2" | head -n 1 | cut -f"$3" |
0eb21b21 NR |
126 | } |
127 | ||
da184edf | 128 | # Save page content to 'tmp' file |
bb019694 | 129 | tmp="`mktemp -t gofetch.current_page.XXXXXX`" |
0eb21b21 | 130 | finish() { |
370b6826 | 131 | rm -rf "$tmp" "$tmp.jpg" "$tmp.menu" "$tmp.choice" |
0eb21b21 NR |
132 | } |
133 | trap finish EXIT | |
134 | ||
135 | if [ $MODE = 1 ]; then | |
136 | echo "$SELECTOR" | nc "$SERVER" "$PORT" | sed 's:\r::g' > "$tmp" | |
137 | else | |
138 | echo "$SELECTOR" | nc "$SERVER" "$PORT" > "$tmp" | |
139 | fi | |
140 | ||
da184edf | 141 | # Process page content |
0eb21b21 | 142 | case "$MODE" in |
7f7487a6 NR |
143 | download) |
144 | # Special, fake mode, only from top-level | |
145 | cat "$tmp" | |
146 | ;; | |
0eb21b21 NR |
147 | 0) |
148 | cat "$tmp" | less | |
149 | ;; | |
1ad44e82 | 150 | 1|+) |
0eb21b21 NR |
151 | CHOICE=start |
152 | while [ "$CHOICE" != "" ]; do | |
370b6826 NR |
153 | if [ "$USE_DIALOG" = 1 ]; then |
154 | > "$tmp.menu" | |
155 | cat_menu "$tmp" dialog | while read ln; do | |
156 | echo -n " $ln" >> "$tmp.menu" | |
157 | done | |
158 | [ "$LINES" = "" ] && LINES=`tput lines` | |
159 | [ "$COLUMNS" = "" ] && COLUMNS=`tput cols` | |
160 | title="$SERVER: $SELECTOR" | |
161 | dialog --extra-button --extra-label Back \ | |
162 | --cancel-label Exit \ | |
163 | --no-shadow \ | |
164 | --menu "$title" \ | |
165 | "$LINES" "$COLUMNS" "$LINES" \ | |
166 | --file "$tmp.menu" 2>"$tmp.choice" | |
370b6826 | 167 | val=$? |
3c2abf21 NR |
168 | clear |
169 | ||
370b6826 NR |
170 | if [ $val = 3 ]; then |
171 | CHOICE="" | |
172 | elif [ $val = 1 ]; then | |
173 | CHOICE="q" | |
174 | else | |
175 | CHOICE="`cat "$tmp.choice" | cut -c5-`" | |
176 | fi | |
177 | ||
178 | rm "$tmp.menu" | |
179 | rm "$tmp.choice" | |
180 | else | |
181 | cat_menu "$tmp" | less | |
182 | read -p "[$SELECTOR]: " CHOICE | |
183 | fi | |
184 | ||
1ad44e82 | 185 | [ "$CHOICE" = q ] && exit 255 # force-quit |
0eb21b21 NR |
186 | index="`expr 1 \* "$CHOICE" 2>/dev/null`" |
187 | if [ "$index" != "" ]; then | |
188 | goto_server="`getsel "$tmp" $index 3`" | |
189 | goto_sel="`getsel "$tmp" $index 2`" | |
190 | goto_port="`getsel "$tmp" $index 4`" | |
191 | goto_mode="`getsel "$tmp" $index 1 | cut -c1`" | |
a1fd1565 | 192 | echo "Digging to [$goto_server:$goto_port] [$goto_sel]..." |
0eb21b21 | 193 | sh "$0" "$goto_server" "$goto_sel" "$goto_port" "$goto_mode" |
1ad44e82 | 194 | [ $? = 255 ] && exit 255 # force-quit |
0eb21b21 NR |
195 | fi |
196 | done | |
197 | ;; | |
198 | 7) | |
199 | read -p "Query: " SEARCH | |
200 | selQuery="$SELECTOR $SEARCH" | |
201 | ||
202 | exec sh "$0" "$SERVER" "$selQuery" "$PORT" 1 | |
203 | ;; | |
204 | 8) | |
205 | telnet -l "$SELECTOR" "$SERVER" "$PORT" | |
206 | read DUMMY | |
207 | ;; | |
208 | 9) | |
209 | echo "<BINARY FILE>" | less | |
210 | ;; | |
da184edf | 211 | g|I) |
5afd6400 NR |
212 | if img2aa --help >/dev/null 2>&1; then |
213 | img2aa --mode=DITHERING \ | |
3c2abf21 | 214 | $INVERT \ |
5afd6400 NR |
215 | --width=74 "$tmp" | less |
216 | elif jp2a -h >/dev/null 2>&1; then | |
217 | if convert -h >/dev/null 2>&1; then | |
da184edf | 218 | convert "$tmp" "$tmp.jpg" |
538b88d6 | 219 | jp2a --border --chars=" .-+=o8#"\ |
3c2abf21 | 220 | $INVERT \ |
da184edf NR |
221 | --width=74 "$tmp.jpg" | less |
222 | else | |
5afd6400 NR |
223 | echo "required program not found to view images: convert" \ |
224 | | less | |
da184edf NR |
225 | fi |
226 | else | |
5afd6400 NR |
227 | echo "required program not found to view images:" \ |
228 | jp2a or img2aa | less | |
da184edf NR |
229 | fi |
230 | ;; | |
0eb21b21 NR |
231 | *) |
232 | echo "unknwon selector mode: <$MODE>" | less | |
233 | exit 3 | |
234 | ;; | |
235 | esac | |
1ad44e82 | 236 |