3 # $0 [gopher uri] ([mode])
4 # uri: the gopher URI to dig to, e.g.:
5 # - gopher://sdf.org:70/1/faq
8 # - http://gopher.nikiroo.be:80/news
10 # default port is 70 for gopher, 80 for http, 443 for https
11 # default filetype depends upon selector:
13 # - 1: menu (dir-like)
15 # - download: fake mode to download the result without changes
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)
22 # USE_DIALOG: force the usage of the command 'dialog'
23 # : true if dialog is found
25 # 1 : force the use of dialog
26 # LINK_COLOR: escape sequences colour (def: 2)
27 # - : means no escape sequence
31 # INVERT : invert the output for image viewing (for white backgrounds)
32 # 0 : do not invert (default)
38 # 2: cannot contact server
39 # 3: unknown selector mode
40 # 255: special exit more 'q'
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'`"
52 PORT
="`echo "$PORT" | sed 's/^://'`"
55 if [ "$PORT" = "" ];then
56 if [ "$PROTOCOL" = http
]; then
58 elif [ "$PROTOCOL" = https
]; then
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
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:'`"
77 if [ "$SERVER" = "" ]; then
78 echo "Syntax error: $0 [gopher uri]" >&2
82 # can be "-" for no escape sequences
83 [ "$LINK_COLOR" = "" ] && LINK_COLOR
=2
85 # Start and end link tags
88 if [ "$LINK_COLOR" != "-" ]; then
89 SL
="`tput setf $LINK_COLOR``tput setaf $LINK_COLOR`"
91 export LESS
="${LESS}-R"
95 if [ "$USE_DIALOG" = "" ]; then
96 if dialog
--help >/dev
/null
2>&1; then
103 # Invert image viewer
104 if [ "$INVERT" = 1 ]; then
111 # Display a gopher menu for the given resource
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"
119 echo "$ln" |
sed 's:":'"''"':g;s:^.\([^\t]*\).*$:" " "\1":g'
121 elif echo "$ln" |
grep "^$PREFIX" >/dev
/null
2>&1; then
123 [ $i -le 9 ] && i
=0$i
124 field
="`echo "$ln" | cut -c1`"
127 1) typ
='DIR';; # menu, actually
128 7) typ
='(?)';; # query
129 8) typ
='TEL';; # TELnet (not TELephone)
130 h
) typ
='WEB';; # HTML
133 +) typ
='SVR';; # redundant server
136 if [ "$2" != dialog
]; then
137 echo "$ln" |
sed "s:^.\\([^\t]*\\).*:$typ $i $SL\\1$EL:g"
139 echo "$ln" |
sed "s:"'"'":'':g;s:^.\\([^\t]*\\).*:"'"'"$typ $i"'"'" "'"\1"'":g"
145 # $0 [FILE] [INDEX] [FIELD]
146 # Get a field from the given-by-index link in FILE
154 cat "$1" |
grep "^$PREFIX" |
tail -n+"$2" |
head -n 1 | cut
-f"$3"
157 # Save page content to 'tmp' file
158 tmp
="`mktemp -t gofetch.current_page.XXXXXX`"
160 rm -rf "$tmp" "$tmp.jpg" "$tmp.menu" "$tmp.choice"
165 if [ "$PROTOCOL" = gopher
:// -o "$PROTOCOL" = "" ]; then
166 echo "$SELECTOR" | nc
"$SERVER" "$PORT" > "$tmp" || ok
=false
168 if wget
-h >/dev
/null
2>&1; then
169 wget
"${PROTOCOL}$SERVER:$PORT/$SELECTOR" -O "$tmp" >/dev
/null
2>&1
171 curl
"${PROTOCOL}$SERVER:$PORT/$SELECTOR" > "$tmp" 2>/dev
/null
175 if [ $ok = false
]; then
176 echo Cannot contact gopher uri
"[$URI]" >&2
180 if [ $MODE = 1 ]; then
181 sed --in-place 's:\r\n:\n:g;s:\r::g' "$tmp"
184 # Process page content
187 # Special, fake mode, only from top-level
195 while [ "$CHOICE" != "" ]; do
196 if [ "$USE_DIALOG" = 1 ]; then
198 cat_menu
"$tmp" dialog |
while read ln; do
199 echo -n " $ln" >> "$tmp.menu"
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 \
208 "$LINES" "$COLUMNS" "$LINES" \
209 --file "$tmp.menu" 2>"$tmp.choice"
213 if [ $val = 3 ]; then
215 elif [ $val = 1 ]; then
218 CHOICE
="`cat "$tmp.choice
" | cut -c5-`"
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`
231 elif [ "$NEW_CHOICE" = n
]; then
232 CHOICE
=`expr "$CHOICE" + 1 2>/dev/null`
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
252 read -p "Query: " SEARCH
253 selQuery
="$SELECTOR $SEARCH"
255 exec sh
"$0" "$SERVER" "$selQuery" "$PORT" 1
258 telnet -l "$SELECTOR" "$SERVER" "$PORT"
262 echo "<BINARY FILE>" |
less
265 if img2aa
--help >/dev
/null
2>&1; then
266 img2aa
--mode=DITHERING \
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#"\
274 --width=74 "$tmp.jpg" |
less
276 echo "required program not found to view images: convert" \
280 echo "required program not found to view images:" \
281 jp2a or img2aa |
less
285 echo "unknwon selector mode: <$MODE>" |
less