gopher.sh: use single URI parameter
[gofetch.git] / gopher.sh
1 #!/bin/sh
2
3 # $0 [gopher uri] ([mode])
4 # uri: the gopher URI to dig to, e.g.:
5 # - gopher://sdf.org:70/1/faq
6 # - sdf.org/faq
7 # - sdf.org/1/faq
8 # - http://gopher.nikiroo.be:80/news
9 # - ...
10 # default port is 70 for gopher, 80 for http, 443 for https
11 # default filetype depends upon selector:
12 # - 0: plain text
13 # - 1: menu (dir-like)
14 # - ...
15 # - download: fake mode to download the result without changes
16
17 # Manual:
18 # When asked for a number (non-dialog uses only), you can also
19 # enter 'n' or 'p' to get the next or previous item (+1 or -1)
20
21 # ENV variables:
22 # USE_DIALOG: force the usage of the command 'dialog'
23 # : true if dialog is found
24 # 0 : do not
25 # 1 : force the use of dialog
26 # LINK_COLOR: escape sequences colour (def: 2)
27 # - : means no escape sequence
28 # 1 : means colour 1
29 # 2 : means colour 2
30 # [...]
31 # INVERT : invert the output for image viewing (for white backgrounds)
32 # 0 : do not invert (default)
33 # 1 : invert
34
35 # EXIT Codes:
36 # 0: ok
37 # 1: syntax error
38 # 2: cannot contact server
39 # 3: unknown selector mode
40 # 255: special exit more 'q'
41
42 URI="$1"
43
44 PREFIX="[0-9hIg+]"
45
46 PROTOCOL="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\1|g'`"
47 SERVER="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\2|g'`"
48 PORT="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\3|g'`"
49 SELECTOR="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\4|g'`"
50 MODE=
51
52 PORT="`echo "$PORT" | sed 's/^://'`"
53
54 # Defaults:
55 if [ "$PORT" = "" ];then
56 if [ "$PROTOCOL" = http ]; then
57 PORT=80
58 elif [ "$PROTOCOL" = https ]; then
59 PORT=443
60 else
61 PORT=70
62 fi
63 fi
64
65 if [ "$MODE" = "" ]; then
66 # "" or dir-like selector? -> 1 ; if not -> 0
67 echo "$SELECTOR" | grep "/$" >/dev/null && MODE=1 || MODE=0
68 [ "$SELECTOR" = "" ] && MODE=1
69
70 # check explicit modes:
71 if echo "$SELECTOR" | grep "^/\?\($PREFIX\|download\)/" >/dev/null; then
72 MODE="`echo "$SELECTOR" | sed 's:^/\?\([^/]*\)/\(.*\):\1:'`"
73 SELECTOR="`echo "$SELECTOR" | sed 's:^/\?\([^/]*\)/\(.*\):\2:'`"
74 fi
75 fi
76
77 if [ "$SERVER" = "" ]; then
78 echo "Syntax error: $0 [gopher uri]" >&2
79 exit 1
80 fi
81
82 # can be "-" for no escape sequences
83 [ "$LINK_COLOR" = "" ] && LINK_COLOR=2
84
85 # Start and end link tags
86 SL=
87 EL=
88 if [ "$LINK_COLOR" != "-" ]; then
89 SL="`tput setf $LINK_COLOR``tput setaf $LINK_COLOR`"
90 EL="`tput init`"
91 export LESS="${LESS}-R"
92 fi
93
94 # 'dialog' or text
95 if [ "$USE_DIALOG" = "" ]; then
96 if dialog --help >/dev/null 2>&1; then
97 USE_DIALOG=1
98 else
99 USE_DIALOG=0
100 fi
101 fi
102
103 # Invert image viewer
104 if [ "$INVERT" = 1 ]; then
105 INVERT="--invert"
106 else
107 INVERT=
108 fi
109
110 # $0 [FILE] (dialog)
111 # Display a gopher menu for the given resource
112 cat_menu() {
113 i=0
114 cat "$1" | grep "^i\|^$PREFIX" | sed 's:\\:\\\\\\\\:g' | while read ln; do
115 if echo "$ln" | grep "^i" >/dev/null 2>&1; then
116 if [ "$2" != dialog ]; then
117 echo "$ln" | sed "s:^.\([^\t]*\).*$: \1:g"
118 else
119 echo "$ln" | sed 's:":'"''"':g;s:^.\([^\t]*\).*$:" " "\1":g'
120 fi
121 elif echo "$ln" | grep "^$PREFIX" >/dev/null 2>&1; then
122 i=`expr $i + 1`
123 [ $i -le 9 ] && i=0$i
124 field="`echo "$ln" | cut -c1`"
125 case "$field" in
126 0) typ='TXT';;
127 1) typ='DIR';; # menu, actually
128 7) typ='(?)';; # query
129 8) typ='TEL';; # TELnet (not TELephone)
130 h) typ='WEB';; # HTML
131 I) typ='IMG';;
132 g) typ='GIF';;
133 +) typ='SVR';; # redundant server
134 *) typ='!!!';;
135 esac
136 if [ "$2" != dialog ]; then
137 echo "$ln" | sed "s:^.\\([^\t]*\\).*:$typ $i $SL\\1$EL:g"
138 else
139 echo "$ln" | sed "s:"'"'":'':g;s:^.\\([^\t]*\\).*:"'"'"$typ $i"'"'" "'"\1"'":g"
140 fi
141 fi
142 done
143 }
144
145 # $0 [FILE] [INDEX] [FIELD]
146 # Get a field from the given-by-index link in FILE
147 #
148 # Fields:
149 # 1 = type/name
150 # 2 = selector
151 # 3 = server
152 # 4 = port
153 getsel() {
154 cat "$1" | grep "^$PREFIX" | tail -n+"$2" | head -n 1 | cut -f"$3"
155 }
156
157 # Save page content to 'tmp' file
158 tmp="`mktemp -t gofetch.current_page.XXXXXX`"
159 finish() {
160 rm -rf "$tmp" "$tmp.jpg" "$tmp.menu" "$tmp.choice"
161 }
162 trap finish EXIT
163
164 ok=true
165 if [ "$PROTOCOL" = gopher:// -o "$PROTOCOL" = "" ]; then
166 echo "$SELECTOR" | nc "$SERVER" "$PORT" > "$tmp" || ok=false
167 else
168 if wget -h >/dev/null 2>&1; then
169 wget "${PROTOCOL}$SERVER:$PORT/$SELECTOR" -O "$tmp" >/dev/null 2>&1
170 else
171 curl "${PROTOCOL}$SERVER:$PORT/$SELECTOR" > "$tmp" 2>/dev/null
172 fi
173 fi
174
175 if [ $ok = false ]; then
176 echo Cannot contact gopher uri "[$URI]" >&2
177 exit 2
178 fi
179
180 if [ $MODE = 1 ]; then
181 sed --in-place 's:\r\n:\n:g;s:\r::g' "$tmp"
182 fi
183
184 # Process page content
185 case "$MODE" in
186 download)
187 # Special, fake mode, only from top-level
188 cat "$tmp"
189 ;;
190 0)
191 cat "$tmp" | less
192 ;;
193 1|+)
194 CHOICE=0
195 while [ "$CHOICE" != "" ]; do
196 if [ "$USE_DIALOG" = 1 ]; then
197 > "$tmp.menu"
198 cat_menu "$tmp" dialog | while read ln; do
199 echo -n " $ln" >> "$tmp.menu"
200 done
201 [ "$LINES" = "" ] && LINES=`tput lines`
202 [ "$COLUMNS" = "" ] && COLUMNS=`tput cols`
203 title="$SERVER: $SELECTOR"
204 dialog --extra-button --extra-label Back \
205 --cancel-label Exit \
206 --no-shadow \
207 --menu "$title" \
208 "$LINES" "$COLUMNS" "$LINES" \
209 --file "$tmp.menu" 2>"$tmp.choice"
210 val=$?
211 clear
212
213 if [ $val = 3 ]; then
214 CHOICE=""
215 elif [ $val = 1 ]; then
216 CHOICE="q"
217 else
218 CHOICE="`cat "$tmp.choice" | cut -c5-`"
219 fi
220
221 rm "$tmp.menu"
222 rm "$tmp.choice"
223 else
224 cat_menu "$tmp" | less
225 read -p "[$SELECTOR]: " NEW_CHOICE
226 if [ "$NEW_CHOICE" = p ]; then
227 CHOICE=`expr "$CHOICE" - 1 2>/dev/null`
228 if [ "$CHOICE" -lt 0 ]; then
229 CHOICE=`cat "$tmp" | grep "^$PREFIX" | wc --lines`
230 fi
231 elif [ "$NEW_CHOICE" = n ]; then
232 CHOICE=`expr "$CHOICE" + 1 2>/dev/null`
233 else
234 CHOICE="$NEW_CHOICE"
235 fi
236 fi
237
238 [ "$CHOICE" = q ] && exit 255 # force-quit
239 index="`expr 1 \* "$CHOICE" 2>/dev/null`"
240 if [ "$index" != "" ]; then
241 goto_server="`getsel "$tmp" $index 3`"
242 goto_sel="`getsel "$tmp" $index 2`"
243 goto_port="`getsel "$tmp" $index 4`"
244 goto_mode="`getsel "$tmp" $index 1 | cut -c1`"
245 echo "Digging to [$goto_server:$goto_port] $index [$goto_sel]..."
246 sh "$0" "$goto_server:$goto_port/$goto_mode/$goto_sel"
247 [ $? = 255 ] && exit 255 # force-quit
248 fi
249 done
250 ;;
251 7)
252 read -p "Query: " SEARCH
253 selQuery="$SELECTOR $SEARCH"
254
255 exec sh "$0" "$SERVER" "$selQuery" "$PORT" 1
256 ;;
257 8)
258 telnet -l "$SELECTOR" "$SERVER" "$PORT"
259 read DUMMY
260 ;;
261 9)
262 echo "<BINARY FILE>" | less
263 ;;
264 g|I)
265 if img2aa --help >/dev/null 2>&1; then
266 img2aa --mode=DITHERING \
267 $INVERT \
268 --width=74 "$tmp" | less
269 elif jp2a -h >/dev/null 2>&1; then
270 if convert -h >/dev/null 2>&1; then
271 convert "$tmp" "$tmp.jpg"
272 jp2a --border --chars=" .-+=o8#"\
273 $INVERT \
274 --width=74 "$tmp.jpg" | less
275 else
276 echo "required program not found to view images: convert" \
277 | less
278 fi
279 else
280 echo "required program not found to view images:" \
281 jp2a or img2aa | less
282 fi
283 ;;
284 *)
285 echo "unknwon selector mode: <$MODE>" | less
286 exit 3
287 ;;
288 esac
289