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 # USE_DIALOG: force the usage of the command 'dialog'
15 # : true if dialog is found
17 # 1 : force the use of dialog
18 # LINK_COLOR: escape sequences colour (def: 2)
19 # - : means no escape sequence
23 # INVERT : invert the output for image viewing (for white backgrounds)
24 # 0 : do not invert (default)
30 # 2: cannot contact server
31 # 3: unknown selector mode
32 # 255: special exit more 'q'
42 [ "$PORT" = "" ] && PORT
=70
43 if [ "$MODE" = "" ]; then
44 # "" or dir-like selector? -> 1 ; if not -> 0
45 echo "$SELECTOR" |
grep "/$" >/dev
/null
&& MODE
=1 || MODE
=0
46 [ "$SELECTOR" = "" ] && MODE
=1
48 # check explicit modes:
49 if echo "$SELECTOR" |
grep "^/\($PREFIX\|download\)/" >/dev
/null
; then
50 MODE
="`echo "$SELECTOR" | cut -f2 -d/`"
51 SELECTOR
="`echo "$SELECTOR" | sed 's:^/[^/]*/::'`"
55 if [ "$SERVER" = "" ]; then
56 echo "Syntax error: $0 [SERVER] ([SELECTOR]) ([PORT]) ([MODE])" >&2
60 # can be "-" for no escape sequences
61 [ "$LINK_COLOR" = "" ] && LINK_COLOR
=2
63 # Start and end link tags
66 if [ "$LINK_COLOR" != "-" ]; then
67 SL
="`tput setf $LINK_COLOR``tput setaf $LINK_COLOR`"
69 export LESS
="${LESS}-R"
73 if [ "$USE_DIALOG" = "" ]; then
74 if dialog
--help >/dev
/null
2>&1; then
82 if [ "$INVERT" = 1 ]; then
89 # Display a gopher menu for the given resource
92 cat "$1" |
grep "^i\|^$PREFIX" |
while read ln; do
93 if echo "$ln" |
grep "^i" >/dev
/null
2>&1; then
94 if [ "$2" != dialog
]; then
95 echo "$ln" |
sed "s:^.\([^\t]*\).*$: \1:g"
97 echo "$ln" |
sed 's:":'"''"':g;s:^.\([^\t]*\).*$:" " "\1":g'
99 elif echo "$ln" |
grep "^$PREFIX" >/dev
/null
2>&1; then
101 [ $i -le 9 ] && i
=0$i
102 field
="`echo "$ln" | cut -c1`"
105 1) typ
='DIR';; # menu, actually
106 7) typ
='(?)';; # query
107 8) typ
='TEL';; # TELnet (not TELephone)
108 h
) typ
='WEB';; # HTML
111 +) typ
='SVR';; # redundant server
114 if [ "$2" != dialog
]; then
115 echo "$ln" |
sed "s:^.\\([^\t]*\\).*:$typ $i $SL\\1$EL:g"
117 echo "$ln" |
sed "s:"'"'":'':g;s:^.\\([^\t]*\\).*:"'"'"$typ $i"'"'" "'"\1"'":g"
123 # $0 [FILE] [INDEX] [FIELD]
124 # Get a field from the given-by-index link in FILE
132 cat "$1" |
grep "^$PREFIX" |
tail -n+"$2" |
head -n 1 | cut
-f"$3"
135 # Save page content to 'tmp' file
136 tmp
="`mktemp -t gofetch.current_page.XXXXXX`"
138 rm -rf "$tmp" "$tmp.jpg" "$tmp.menu" "$tmp.choice"
142 echo "$SELECTOR" | nc
"$SERVER" "$PORT" > "$tmp"
144 echo Cannot contact gopher server
"[$SERVER]" >&2
148 if [ $MODE = 1 ]; then
149 sed --in-place 's:\r::g' "$tmp"
152 # Process page content
155 # Special, fake mode, only from top-level
163 while [ "$CHOICE" != "" ]; do
164 if [ "$USE_DIALOG" = 1 ]; then
166 cat_menu
"$tmp" dialog |
while read ln; do
167 echo -n " $ln" >> "$tmp.menu"
169 [ "$LINES" = "" ] && LINES
=`tput lines`
170 [ "$COLUMNS" = "" ] && COLUMNS
=`tput cols`
171 title
="$SERVER: $SELECTOR"
172 dialog
--extra-button --extra-label Back \
173 --cancel-label Exit \
176 "$LINES" "$COLUMNS" "$LINES" \
177 --file "$tmp.menu" 2>"$tmp.choice"
181 if [ $val = 3 ]; then
183 elif [ $val = 1 ]; then
186 CHOICE
="`cat "$tmp.choice
" | cut -c5-`"
192 cat_menu
"$tmp" |
less
193 read -p "[$SELECTOR]: " CHOICE
196 [ "$CHOICE" = q
] && exit 255 # force-quit
197 index
="`expr 1 \* "$CHOICE" 2>/dev/null`"
198 if [ "$index" != "" ]; then
199 goto_server
="`getsel "$tmp" $index 3`"
200 goto_sel
="`getsel "$tmp" $index 2`"
201 goto_port
="`getsel "$tmp" $index 4`"
202 goto_mode
="`getsel "$tmp" $index 1 | cut -c1`"
203 echo "Digging to [$goto_server:$goto_port] [$goto_sel]..."
204 sh
"$0" "$goto_server" "$goto_sel" "$goto_port" "$goto_mode"
205 [ $?
= 255 ] && exit 255 # force-quit
210 read -p "Query: " SEARCH
211 selQuery
="$SELECTOR $SEARCH"
213 exec sh
"$0" "$SERVER" "$selQuery" "$PORT" 1
216 telnet -l "$SELECTOR" "$SERVER" "$PORT"
220 echo "<BINARY FILE>" |
less
223 if img2aa
--help >/dev
/null
2>&1; then
224 img2aa
--mode=DITHERING \
226 --width=74 "$tmp" |
less
227 elif jp2a
-h >/dev
/null
2>&1; then
228 if convert
-h >/dev
/null
2>&1; then
229 convert
"$tmp" "$tmp.jpg"
230 jp2a
--border --chars=" .-+=o8#"\
232 --width=74 "$tmp.jpg" |
less
234 echo "required program not found to view images: convert" \
238 echo "required program not found to view images:" \
239 jp2a or img2aa |
less
243 echo "unknwon selector mode: <$MODE>" |
less