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