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)
35 [ "$PORT" = "" ] && PORT
=70
36 if [ "$MODE" = "" ]; then
37 # "" or dir-like selector? -> 1 ; if not -> 0
38 echo "$SELECTOR" |
grep "/$" >/dev
/null
&& MODE
=1 || MODE
=0
39 [ "$SELECTOR" = "" ] && MODE
=1
41 # check explicit modes:
42 if echo "$SELECTOR" |
grep "^/\($PREFIX\|download\)/" >/dev
/null
; then
43 MODE
="`echo "$SELECTOR" | cut -f2 -d/`"
44 SELECTOR
="`echo "$SELECTOR" | sed 's:^/[^/]*/::'`"
48 if [ "$SERVER" = "" ]; then
49 echo "Syntax error: $0 [SERVER] ([SELECTOR]) ([PORT]) ([MODE])" >&2
53 # can be "-" for no escape sequences
54 [ "$LINK_COLOR" = "" ] && LINK_COLOR
=2
56 # Start and end link tags
59 if [ "$LINK_COLOR" != "-" ]; then
60 SL
="`tput setf $LINK_COLOR``tput setaf $LINK_COLOR`"
62 export LESS
="${LESS}-R"
66 if [ "$USE_DIALOG" = "" ]; then
67 if dialog
--help >/dev
/null
2>&1; then
75 if [ "$INVERT" = 1 ]; then
82 # Display a gopher menu for the given resource
85 cat "$1" |
grep "^i\|^$PREFIX" |
while read ln; do
86 if echo "$ln" |
grep "^i" >/dev
/null
2>&1; then
87 if [ "$2" != dialog
]; then
88 echo "$ln" |
sed "s:^.\([^\t]*\).*$: \1:g"
90 echo "$ln" |
sed 's:":'"''"':g;s:^.\([^\t]*\).*$:" " "\1":g'
92 elif echo "$ln" |
grep "^$PREFIX" >/dev
/null
2>&1; then
95 field
="`echo "$ln" | cut -c1`"
98 1) typ
='DIR';; # menu, actually
99 7) typ
='(?)';; # query
100 8) typ
='TEL';; # TELnet (not TELephone)
101 h
) typ
='WEB';; # HTML
104 +) typ
='SVR';; # redundant server
107 if [ "$2" != dialog
]; then
108 echo "$ln" |
sed "s:^.\\([^\t]*\\).*:$typ $i $SL\\1$EL:g"
110 echo "$ln" |
sed "s:"'"'":'':g;s:^.\\([^\t]*\\).*:"'"'"$typ $i"'"'" "'"\1"'":g"
116 # $0 [FILE] [INDEX] [FIELD]
117 # Get a field from the given-by-index link in FILE
125 cat "$1" |
grep "^$PREFIX" |
tail -n+"$2" |
head -n 1 | cut
-f"$3"
128 # Save page content to 'tmp' file
129 tmp
="`mktemp -t gofetch.current_page.XXXXXX`"
131 rm -rf "$tmp" "$tmp.jpg" "$tmp.menu" "$tmp.choice"
135 if [ $MODE = 1 ]; then
136 echo "$SELECTOR" | nc
"$SERVER" "$PORT" |
sed 's:\r::g' > "$tmp"
138 echo "$SELECTOR" | nc
"$SERVER" "$PORT" > "$tmp"
141 # Process page content
144 # Special, fake mode, only from top-level
152 while [ "$CHOICE" != "" ]; do
153 if [ "$USE_DIALOG" = 1 ]; then
155 cat_menu
"$tmp" dialog |
while read ln; do
156 echo -n " $ln" >> "$tmp.menu"
158 [ "$LINES" = "" ] && LINES
=`tput lines`
159 [ "$COLUMNS" = "" ] && COLUMNS
=`tput cols`
160 title
="$SERVER: $SELECTOR"
161 dialog
--extra-button --extra-label Back \
162 --cancel-label Exit \
165 "$LINES" "$COLUMNS" "$LINES" \
166 --file "$tmp.menu" 2>"$tmp.choice"
170 if [ $val = 3 ]; then
172 elif [ $val = 1 ]; then
175 CHOICE
="`cat "$tmp.choice
" | cut -c5-`"
181 cat_menu
"$tmp" |
less
182 read -p "[$SELECTOR]: " CHOICE
185 [ "$CHOICE" = q
] && exit 255 # force-quit
186 index
="`expr 1 \* "$CHOICE" 2>/dev/null`"
187 if [ "$index" != "" ]; then
188 goto_server
="`getsel "$tmp" $index 3`"
189 goto_sel
="`getsel "$tmp" $index 2`"
190 goto_port
="`getsel "$tmp" $index 4`"
191 goto_mode
="`getsel "$tmp" $index 1 | cut -c1`"
192 echo "Digging to [$goto_server:$goto_port] [$goto_sel]..."
193 sh
"$0" "$goto_server" "$goto_sel" "$goto_port" "$goto_mode"
194 [ $?
= 255 ] && exit 255 # force-quit
199 read -p "Query: " SEARCH
200 selQuery
="$SELECTOR $SEARCH"
202 exec sh
"$0" "$SERVER" "$selQuery" "$PORT" 1
205 telnet -l "$SELECTOR" "$SERVER" "$PORT"
209 echo "<BINARY FILE>" |
less
212 if img2aa
--help >/dev
/null
2>&1; then
213 img2aa
--mode=DITHERING \
215 --width=74 "$tmp" |
less
216 elif jp2a
-h >/dev
/null
2>&1; then
217 if convert
-h >/dev
/null
2>&1; then
218 convert
"$tmp" "$tmp.jpg"
219 jp2a
--border --chars=" .-+=o8#"\
221 --width=74 "$tmp.jpg" |
less
223 echo "required program not found to view images: convert" \
227 echo "required program not found to view images:" \
228 jp2a or img2aa |
less
232 echo "unknwon selector mode: <$MODE>" |
less