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