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