3 # $0 [server] ([selector]) ([port]) ([mode])
4 # server: the gopher server to dig to
5 # selector: the gopher selector (default is empty)
6 # port: the port to use (default is 70)
7 # mode: the filetype mode (default depends upon selector)
11 # download: fake mode to download the result without changes
14 # When asked for a number (non-dialog uses only), you can also
15 # enter 'n' or 'p' to get the next or previous item (+1 or -1)
18 # USE_DIALOG: force the usage of the command 'dialog'
19 # : true if dialog is found
21 # 1 : force the use of dialog
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'
46 [ "$PORT" = "" ] && PORT
=70
47 if [ "$MODE" = "" ]; then
48 # "" or dir-like selector? -> 1 ; if not -> 0
49 echo "$SELECTOR" |
grep "/$" >/dev
/null
&& MODE
=1 || MODE
=0
50 [ "$SELECTOR" = "" ] && MODE
=1
52 # check explicit modes:
53 if echo "$SELECTOR" |
grep "^/\($PREFIX\|download\)/" >/dev
/null
; then
54 MODE
="`echo "$SELECTOR" | cut -f2 -d/`"
55 SELECTOR
="`echo "$SELECTOR" | sed 's:^/[^/]*/::'`"
59 if [ "$SERVER" = "" ]; then
60 echo "Syntax error: $0 [SERVER] ([SELECTOR]) ([PORT]) ([MODE])" >&2
64 # can be "-" for no escape sequences
65 [ "$LINK_COLOR" = "" ] && LINK_COLOR
=2
67 # Start and end link tags
70 if [ "$LINK_COLOR" != "-" ]; then
71 SL
="`tput setf $LINK_COLOR``tput setaf $LINK_COLOR`"
73 export LESS
="${LESS}-R"
77 if [ "$USE_DIALOG" = "" ]; then
78 if dialog
--help >/dev
/null
2>&1; then
86 if [ "$INVERT" = 1 ]; then
93 # Display a gopher menu for the given resource
96 cat "$1" |
grep "^i\|^$PREFIX" |
while read ln; do
97 if echo "$ln" |
grep "^i" >/dev
/null
2>&1; then
98 if [ "$2" != dialog
]; then
99 echo "$ln" |
sed "s:^.\([^\t]*\).*$: \1:g"
101 echo "$ln" |
sed 's:":'"''"':g;s:^.\([^\t]*\).*$:" " "\1":g'
103 elif echo "$ln" |
grep "^$PREFIX" >/dev
/null
2>&1; then
105 [ $i -le 9 ] && i
=0$i
106 field
="`echo "$ln" | cut -c1`"
109 1) typ
='DIR';; # menu, actually
110 7) typ
='(?)';; # query
111 8) typ
='TEL';; # TELnet (not TELephone)
112 h
) typ
='WEB';; # HTML
115 +) typ
='SVR';; # redundant server
118 if [ "$2" != dialog
]; then
119 echo "$ln" |
sed "s:^.\\([^\t]*\\).*:$typ $i $SL\\1$EL:g"
121 echo "$ln" |
sed "s:"'"'":'':g;s:^.\\([^\t]*\\).*:"'"'"$typ $i"'"'" "'"\1"'":g"
127 # $0 [FILE] [INDEX] [FIELD]
128 # Get a field from the given-by-index link in FILE
136 cat "$1" |
grep "^$PREFIX" |
tail -n+"$2" |
head -n 1 | cut
-f"$3"
139 # Save page content to 'tmp' file
140 tmp
="`mktemp -t gofetch.current_page.XXXXXX`"
142 rm -rf "$tmp" "$tmp.jpg" "$tmp.menu" "$tmp.choice"
146 echo "$SELECTOR" | nc
"$SERVER" "$PORT" > "$tmp"
148 echo Cannot contact gopher server
"[$SERVER]" >&2
152 if [ $MODE = 1 ]; then
153 sed --in-place 's:\r::g' "$tmp"
156 # Process page content
159 # Special, fake mode, only from top-level
167 while [ "$CHOICE" != "" ]; do
168 if [ "$USE_DIALOG" = 1 ]; then
170 cat_menu
"$tmp" dialog |
while read ln; do
171 echo -n " $ln" >> "$tmp.menu"
173 [ "$LINES" = "" ] && LINES
=`tput lines`
174 [ "$COLUMNS" = "" ] && COLUMNS
=`tput cols`
175 title
="$SERVER: $SELECTOR"
176 dialog
--extra-button --extra-label Back \
177 --cancel-label Exit \
180 "$LINES" "$COLUMNS" "$LINES" \
181 --file "$tmp.menu" 2>"$tmp.choice"
185 if [ $val = 3 ]; then
187 elif [ $val = 1 ]; then
190 CHOICE
="`cat "$tmp.choice
" | cut -c5-`"
196 cat_menu
"$tmp" |
less
197 read -p "[$SELECTOR]: " NEW_CHOICE
198 if [ "$NEW_CHOICE" = p
]; then
199 CHOICE
=`expr "$CHOICE" - 1 2>/dev/null`
200 if [ "$CHOICE" -lt 0 ]; then
201 CHOICE
=`cat "$tmp" | grep "^$PREFIX" | wc --lines`
203 elif [ "$NEW_CHOICE" = n
]; then
204 CHOICE
=`expr "$CHOICE" + 1 2>/dev/null`
210 [ "$CHOICE" = q
] && exit 255 # force-quit
211 index
="`expr 1 \* "$CHOICE" 2>/dev/null`"
212 if [ "$index" != "" ]; then
213 goto_server
="`getsel "$tmp" $index 3`"
214 goto_sel
="`getsel "$tmp" $index 2`"
215 goto_port
="`getsel "$tmp" $index 4`"
216 goto_mode
="`getsel "$tmp" $index 1 | cut -c1`"
217 echo "Digging to [$goto_server:$goto_port] $index [$goto_sel]..."
218 sh
"$0" "$goto_server" "$goto_sel" "$goto_port" "$goto_mode"
219 [ $?
= 255 ] && exit 255 # force-quit
224 read -p "Query: " SEARCH
225 selQuery
="$SELECTOR $SEARCH"
227 exec sh
"$0" "$SERVER" "$selQuery" "$PORT" 1
230 telnet -l "$SELECTOR" "$SERVER" "$PORT"
234 echo "<BINARY FILE>" |
less
237 if img2aa
--help >/dev
/null
2>&1; then
238 img2aa
--mode=DITHERING \
240 --width=74 "$tmp" |
less
241 elif jp2a
-h >/dev
/null
2>&1; then
242 if convert
-h >/dev
/null
2>&1; then
243 convert
"$tmp" "$tmp.jpg"
244 jp2a
--border --chars=" .-+=o8#"\
246 --width=74 "$tmp.jpg" |
less
248 echo "required program not found to view images: convert" \
252 echo "required program not found to view images:" \
253 jp2a or img2aa |
less
257 echo "unknwon selector mode: <$MODE>" |
less