gopher.sh: remove dialog support (ugly to use)
[gofetch.git] / gopher.sh
CommitLineData
0eb21b21
NR
1#!/bin/sh
2
a8e347c7
NR
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
7f7487a6 16
05ea999a 17# Manual:
4b2acf06
NR
18# When asked for a number, you can also enter 'n' or 'p' to get the
19# next or previous item (+1 or -1)
05ea999a 20
c6852b70
NR
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# [...]
3c2abf21
NR
27# INVERT : invert the output for image viewing (for white backgrounds)
28# 0 : do not invert (default)
29# 1 : invert
c6852b70 30
528c6011
NR
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
a8e347c7 38URI="$1"
0eb21b21 39
61fcec09
NR
40PREFIX="[0-9hIg+]"
41
a8e347c7
NR
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
0eb21b21 50# Defaults:
a8e347c7 51if [ "$PORT" = "" ];then
4e5b0448
NR
52 case "$PROTOCOL" in
53 http) PORT=80 ;;
54 https) PORT=443;;
55 *) PORT=70 ;;
56 esac
a8e347c7
NR
57fi
58
0eb21b21
NR
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
61fcec09
NR
63
64 # check explicit modes:
a8e347c7
NR
65 if echo "$SELECTOR" | grep "^/\?\($PREFIX\|download\)/" >/dev/null; then
66 MODE="`echo "$SELECTOR" | sed 's:^/\?\([^/]*\)/\(.*\):\1:'`"
67 SELECTOR="`echo "$SELECTOR" | sed 's:^/\?\([^/]*\)/\(.*\):\2:'`"
61fcec09 68 fi
0eb21b21
NR
69fi
70
71if [ "$SERVER" = "" ]; then
a8e347c7 72 echo "Syntax error: $0 [gopher uri]" >&2
528c6011 73 exit 1
0eb21b21
NR
74fi
75
c6852b70
NR
76# can be "-" for no escape sequences
77[ "$LINK_COLOR" = "" ] && LINK_COLOR=2
050494fa
NR
78
79# Start and end link tags
80SL=
81EL=
c6852b70 82if [ "$LINK_COLOR" != "-" ]; then
050494fa 83 SL="`tput setf $LINK_COLOR``tput setaf $LINK_COLOR`"
1ad44e82 84 EL="`tput init`"
4e5b0448 85 export LESS="${LESS} -R"
050494fa
NR
86fi
87
3c2abf21
NR
88# Invert image viewer
89if [ "$INVERT" = 1 ]; then
90 INVERT="--invert"
91else
92 INVERT=
93fi
94
4b2acf06 95# $0 [FILE]
050494fa 96# Display a gopher menu for the given resource
0eb21b21
NR
97cat_menu() {
98 i=0
a8e347c7 99 cat "$1" | grep "^i\|^$PREFIX" | sed 's:\\:\\\\\\\\:g' | while read ln; do
0eb21b21 100 if echo "$ln" | grep "^i" >/dev/null 2>&1; then
4b2acf06 101 echo "$ln" | sed "s:^.\([^\t]*\).*$: \1:g"
7f7487a6 102 elif echo "$ln" | grep "^$PREFIX" >/dev/null 2>&1; then
0eb21b21 103 i=`expr $i + 1`
1ad44e82 104 [ $i -le 9 ] && i=0$i
0eb21b21
NR
105 field="`echo "$ln" | cut -c1`"
106 case "$field" in
107 0) typ='TXT';;
7f7487a6
NR
108 1) typ='DIR';; # menu, actually
109 7) typ='(?)';; # query
110 8) typ='TEL';; # TELnet (not TELephone)
111 h) typ='WEB';; # HTML
7f7487a6 112 I) typ='IMG';;
1ad44e82 113 g) typ='GIF';;
7f7487a6 114 +) typ='SVR';; # redundant server
050494fa 115 *) typ='!!!';;
0eb21b21 116 esac
4b2acf06 117 echo "$ln" | sed "s:^.\\([^\t]*\\).*:$typ $i $SL\\1$EL:g"
0eb21b21
NR
118 fi
119 done
120}
121
0eb21b21 122# $0 [FILE] [INDEX] [FIELD]
050494fa
NR
123# Get a field from the given-by-index link in FILE
124#
0eb21b21
NR
125# Fields:
126# 1 = type/name
127# 2 = selector
128# 3 = server
129# 4 = port
130getsel() {
7f7487a6 131 cat "$1" | grep "^$PREFIX" | tail -n+"$2" | head -n 1 | cut -f"$3"
0eb21b21
NR
132}
133
da184edf 134# Save page content to 'tmp' file
bb019694 135tmp="`mktemp -t gofetch.current_page.XXXXXX`"
0eb21b21 136finish() {
370b6826 137 rm -rf "$tmp" "$tmp.jpg" "$tmp.menu" "$tmp.choice"
0eb21b21
NR
138}
139trap finish EXIT
140
a8e347c7
NR
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
528c6011
NR
154 exit 2
155fi
156
0eb21b21 157if [ $MODE = 1 ]; then
a8e347c7 158 sed --in-place 's:\r\n:\n:g;s:\r::g' "$tmp"
0eb21b21
NR
159fi
160
da184edf 161# Process page content
0eb21b21 162case "$MODE" in
7f7487a6
NR
163download)
164 # Special, fake mode, only from top-level
165 cat "$tmp"
166;;
0eb21b21
NR
1670)
168 cat "$tmp" | less
169;;
1ad44e82 1701|+)
05ea999a 171 CHOICE=0
0eb21b21 172 while [ "$CHOICE" != "" ]; do
4b2acf06
NR
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`
370b6826 179 fi
4b2acf06
NR
180 elif [ "$NEW_CHOICE" = n ]; then
181 CHOICE=`expr "$CHOICE" + 1 2>/dev/null`
370b6826 182 else
4b2acf06 183 CHOICE="$NEW_CHOICE"
370b6826
NR
184 fi
185
1ad44e82 186 [ "$CHOICE" = q ] && exit 255 # force-quit
0eb21b21
NR
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`"
fcda6ae3 193 echo "Digging to [$goto_server:$goto_port] $index [$goto_sel]..."
a8e347c7 194 sh "$0" "$goto_server:$goto_port/$goto_mode/$goto_sel"
1ad44e82 195 [ $? = 255 ] && exit 255 # force-quit
0eb21b21
NR
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;;
da184edf 212g|I)
5afd6400
NR
213 if img2aa --help >/dev/null 2>&1; then
214 img2aa --mode=DITHERING \
3c2abf21 215 $INVERT \
5afd6400
NR
216 --width=74 "$tmp" | less
217 elif jp2a -h >/dev/null 2>&1; then
218 if convert -h >/dev/null 2>&1; then
da184edf 219 convert "$tmp" "$tmp.jpg"
538b88d6 220 jp2a --border --chars=" .-+=o8#"\
3c2abf21 221 $INVERT \
da184edf
NR
222 --width=74 "$tmp.jpg" | less
223 else
5afd6400
NR
224 echo "required program not found to view images: convert" \
225 | less
da184edf
NR
226 fi
227 else
5afd6400
NR
228 echo "required program not found to view images:" \
229 jp2a or img2aa | less
da184edf
NR
230 fi
231;;
0eb21b21
NR
232*)
233 echo "unknwon selector mode: <$MODE>" | less
234 exit 3
235;;
236esac
1ad44e82 237