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, you can also enter 'n' or 'p' to get the
19 # next or previous item (+1 or -1)
22 # LINK_COLOR: escape sequences colour (def: 2)
23 # - : means no escape sequence
27 # INVERT : invert the output for image viewing (for white backgrounds)
28 # 0 : do not invert (default)
34 # 2: cannot contact server
35 # 3: unknown selector mode
36 # 255: special exit more 'q'
42 PROTOCOL
="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\1|g'`"
43 SERVER
="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\2|g'`"
44 PORT
="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\3|g'`"
45 SELECTOR
="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\4|g'`"
48 PORT
="`echo "$PORT" | sed 's/^://'`"
51 if [ "$PORT" = "" ];then
59 if [ "$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
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:'`"
71 if [ "$SERVER" = "" ]; then
72 echo "Syntax error: $0 [gopher uri]" >&2
76 # can be "-" for no escape sequences
77 [ "$LINK_COLOR" = "" ] && LINK_COLOR
=2
79 # Start and end link tags
82 if [ "$LINK_COLOR" != "-" ]; then
83 SL
="`tput setf $LINK_COLOR``tput setaf $LINK_COLOR`"
85 export LESS
="${LESS} -R"
89 if [ "$INVERT" = 1 ]; then
96 # Display a gopher menu for the given resource
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
104 [ $i -le 9 ] && i
=0$i
105 field
="`echo "$ln" | cut -c1`"
108 1) typ
='DIR';; # menu, actually
109 7) typ
='(?)';; # query
110 8) typ
='TEL';; # TELnet (not TELephone)
111 h
) typ
='WEB';; # HTML
114 +) typ
='SVR';; # redundant server
117 echo "$ln" |
sed "s:^.\\([^\t]*\\).*:$typ $i $SL\\1$EL:g"
122 # $0 [FILE] [INDEX] [FIELD]
123 # Get a field from the given-by-index link in FILE
131 cat "$1" |
grep "^$PREFIX" |
tail -n+"$2" |
head -n 1 | cut
-f"$3"
134 # Save page content to 'tmp' file
135 tmp
="`mktemp -t gofetch.current_page.XXXXXX`"
137 rm -rf "$tmp" "$tmp.jpg" "$tmp.menu" "$tmp.choice"
142 if [ "$PROTOCOL" = gopher
:// -o "$PROTOCOL" = "" ]; then
143 echo "$SELECTOR" | nc
"$SERVER" "$PORT" > "$tmp" || ok
=false
145 if wget
-h >/dev
/null
2>&1; then
146 wget
"${PROTOCOL}$SERVER:$PORT/$SELECTOR" -O "$tmp" >/dev
/null
2>&1
148 curl
"${PROTOCOL}$SERVER:$PORT/$SELECTOR" > "$tmp" 2>/dev
/null
152 if [ $ok = false
]; then
153 echo Cannot contact gopher uri
"[$URI]" >&2
157 if [ $MODE = 1 ]; then
158 sed --in-place 's:\r\n:\n:g;s:\r::g' "$tmp"
161 # Process page content
164 # Special, fake mode, only from top-level
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`
180 elif [ "$NEW_CHOICE" = n
]; then
181 CHOICE
=`expr "$CHOICE" + 1 2>/dev/null`
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
200 read -p "Query: " SEARCH
201 selQuery
="$SELECTOR $SEARCH"
203 exec sh
"$0" "$SERVER" "$selQuery" "$PORT" 1
206 telnet -l "$SELECTOR" "$SERVER" "$PORT"
210 echo "<BINARY FILE>" |
less
213 if img2aa
--help >/dev
/null
2>&1; then
214 img2aa
--mode=DITHERING \
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#"\
222 --width=74 "$tmp.jpg" |
less
224 echo "required program not found to view images: convert" \
228 echo "required program not found to view images:" \
229 jp2a or img2aa |
less
233 echo "unknwon selector mode: <$MODE>" |
less