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
63 if [ "$MODE" = "" ]; then
64 # "" or dir-like selector? -> 1 ; if not -> 0
65 echo "$SELECTOR" |
grep "/$" >/dev
/null
&& MODE
=1 || MODE
=0
66 [ "$SELECTOR" = "" ] && MODE
=1
68 # check explicit modes:
69 if echo "$SELECTOR" |
grep "^/\?\($PREFIX\|download\)/" >/dev
/null
; then
70 MODE
="`echo "$SELECTOR" | sed 's:^/\?\([^/]*\)/\(.*\):\1:'`"
71 SELECTOR
="`echo "$SELECTOR" | sed 's:^/\?\([^/]*\)/\(.*\):\2:'`"
75 if [ "$SERVER" = "" ]; then
76 echo "Syntax error: $0 [gopher uri]" >&2
80 # can be "-" for no escape sequences
81 [ "$LINK_COLOR" = "" ] && LINK_COLOR
=2
83 # Start and end link tags
86 if [ "$LINK_COLOR" != "-" ]; then
87 SL
="`tput setf $LINK_COLOR``tput setaf $LINK_COLOR`"
89 export LESS
="${LESS} -R"
93 if [ "$USE_DIALOG" = "" ]; then
94 if dialog
--help >/dev
/null
2>&1; then
101 # Invert image viewer
102 if [ "$INVERT" = 1 ]; then
109 # Display a gopher menu for the given resource
112 cat "$1" |
grep "^i\|^$PREFIX" |
sed 's:\\:\\\\\\\\:g' |
while read ln; do
113 if echo "$ln" |
grep "^i" >/dev
/null
2>&1; then
114 if [ "$2" != dialog
]; then
115 echo "$ln" |
sed "s:^.\([^\t]*\).*$: \1:g"
117 echo "$ln" |
sed 's:":'"''"':g;s:^.\([^\t]*\).*$:" " "\1":g'
119 elif echo "$ln" |
grep "^$PREFIX" >/dev
/null
2>&1; then
121 [ $i -le 9 ] && i
=0$i
122 field
="`echo "$ln" | cut -c1`"
125 1) typ
='DIR';; # menu, actually
126 7) typ
='(?)';; # query
127 8) typ
='TEL';; # TELnet (not TELephone)
128 h
) typ
='WEB';; # HTML
131 +) typ
='SVR';; # redundant server
134 if [ "$2" != dialog
]; then
135 echo "$ln" |
sed "s:^.\\([^\t]*\\).*:$typ $i $SL\\1$EL:g"
137 echo "$ln" |
sed "s:"'"'":'':g;s:^.\\([^\t]*\\).*:"'"'"$typ $i"'"'" "'"\1"'":g"
143 # $0 [FILE] [INDEX] [FIELD]
144 # Get a field from the given-by-index link in FILE
152 cat "$1" |
grep "^$PREFIX" |
tail -n+"$2" |
head -n 1 | cut
-f"$3"
155 # Save page content to 'tmp' file
156 tmp
="`mktemp -t gofetch.current_page.XXXXXX`"
158 rm -rf "$tmp" "$tmp.jpg" "$tmp.menu" "$tmp.choice"
163 if [ "$PROTOCOL" = gopher
:// -o "$PROTOCOL" = "" ]; then
164 echo "$SELECTOR" | nc
"$SERVER" "$PORT" > "$tmp" || ok
=false
166 if wget
-h >/dev
/null
2>&1; then
167 wget
"${PROTOCOL}$SERVER:$PORT/$SELECTOR" -O "$tmp" >/dev
/null
2>&1
169 curl
"${PROTOCOL}$SERVER:$PORT/$SELECTOR" > "$tmp" 2>/dev
/null
173 if [ $ok = false
]; then
174 echo Cannot contact gopher uri
"[$URI]" >&2
178 if [ $MODE = 1 ]; then
179 sed --in-place 's:\r\n:\n:g;s:\r::g' "$tmp"
182 # Process page content
185 # Special, fake mode, only from top-level
193 while [ "$CHOICE" != "" ]; do
194 if [ "$USE_DIALOG" = 1 ]; then
196 cat_menu
"$tmp" dialog |
while read ln; do
197 echo -n " $ln" >> "$tmp.menu"
199 [ "$LINES" = "" ] && LINES
=`tput lines`
200 [ "$COLUMNS" = "" ] && COLUMNS
=`tput cols`
201 title
="$SERVER: $SELECTOR"
202 dialog
--extra-button --extra-label Back \
203 --cancel-label Exit \
206 "$LINES" "$COLUMNS" "$LINES" \
207 --file "$tmp.menu" 2>"$tmp.choice"
211 if [ $val = 3 ]; then
213 elif [ $val = 1 ]; then
216 CHOICE
="`cat "$tmp.choice
" | cut -c5-`"
222 cat_menu
"$tmp" |
less
223 read -p "[$SELECTOR]: " NEW_CHOICE
224 if [ "$NEW_CHOICE" = p
]; then
225 CHOICE
=`expr "$CHOICE" - 1 2>/dev/null`
226 if [ "$CHOICE" -lt 0 ]; then
227 CHOICE
=`cat "$tmp" | grep "^$PREFIX" | wc --lines`
229 elif [ "$NEW_CHOICE" = n
]; then
230 CHOICE
=`expr "$CHOICE" + 1 2>/dev/null`
236 [ "$CHOICE" = q
] && exit 255 # force-quit
237 index
="`expr 1 \* "$CHOICE" 2>/dev/null`"
238 if [ "$index" != "" ]; then
239 goto_server
="`getsel "$tmp" $index 3`"
240 goto_sel
="`getsel "$tmp" $index 2`"
241 goto_port
="`getsel "$tmp" $index 4`"
242 goto_mode
="`getsel "$tmp" $index 1 | cut -c1`"
243 echo "Digging to [$goto_server:$goto_port] $index [$goto_sel]..."
244 sh
"$0" "$goto_server:$goto_port/$goto_mode/$goto_sel"
245 [ $?
= 255 ] && exit 255 # force-quit
250 read -p "Query: " SEARCH
251 selQuery
="$SELECTOR $SEARCH"
253 exec sh
"$0" "$SERVER" "$selQuery" "$PORT" 1
256 telnet -l "$SELECTOR" "$SERVER" "$PORT"
260 echo "<BINARY FILE>" |
less
263 if img2aa
--help >/dev
/null
2>&1; then
264 img2aa
--mode=DITHERING \
266 --width=74 "$tmp" |
less
267 elif jp2a
-h >/dev
/null
2>&1; then
268 if convert
-h >/dev
/null
2>&1; then
269 convert
"$tmp" "$tmp.jpg"
270 jp2a
--border --chars=" .-+=o8#"\
272 --width=74 "$tmp.jpg" |
less
274 echo "required program not found to view images: convert" \
278 echo "required program not found to view images:" \
279 jp2a or img2aa |
less
283 echo "unknwon selector mode: <$MODE>" |
less