3e9a26411cf6ec72bc0038c7eb96f830dc668bf1
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
30 [ "$PORT" = "" ] && PORT
=70
31 if [ "$MODE" = "" ]; then
32 # "" or dir-like selector? -> 1 ; if not -> 0
33 echo "$SELECTOR" |
grep "/$" >/dev
/null
&& MODE
=1 || MODE
=0
34 [ "$SELECTOR" = "" ] && MODE
=1
37 if [ "$SERVER" = "" ]; then
38 echo "Syntax error: $0 [SERVER] ([SELECTOR]) ([PORT]) ([MODE])" >&2
42 # can be "-" for no escape sequences
43 [ "$LINK_COLOR" = "" ] && LINK_COLOR
=2
45 # Start and end link tags
48 if [ "$LINK_COLOR" != "-" ]; then
49 SL
="`tput setf $LINK_COLOR``tput setaf $LINK_COLOR`"
51 export LESS
="${LESS}-R"
55 if [ "$USE_DIALOG" = "" ]; then
56 if dialog
--help >/dev
/null
2>&1; then
66 # Display a gopher menu for the given resource
69 cat "$1" |
grep "^i\|^$PREFIX" |
while read ln; do
70 if echo "$ln" |
grep "^i" >/dev
/null
2>&1; then
71 if [ "$2" != dialog
]; then
72 echo "$ln" |
sed "s:^.\([^\t]*\).*$: \1:g"
74 echo "$ln" |
sed 's:":'"''"':g;s:^.\([^\t]*\).*$:" " "\1":g'
76 elif echo "$ln" |
grep "^$PREFIX" >/dev
/null
2>&1; then
79 field
="`echo "$ln" | cut -c1`"
82 1) typ
='DIR';; # menu, actually
83 7) typ
='(?)';; # query
84 8) typ
='TEL';; # TELnet (not TELephone)
88 +) typ
='SVR';; # redundant server
91 if [ "$2" != dialog
]; then
92 echo "$ln" |
sed "s:^.\\([^\t]*\\).*:$typ $i $SL\\1$EL:g"
94 echo "$ln" |
sed "s:"'"'":'':g;s:^.\\([^\t]*\\).*:"'"'"$typ $i"'"'" "'"\1"'":g"
100 # $0 [FILE] [INDEX] [FIELD]
101 # Get a field from the given-by-index link in FILE
109 cat "$1" |
grep "^$PREFIX" |
tail -n+"$2" |
head -n 1 | cut
-f"$3"
112 # Save page content to 'tmp' file
113 tmp
="`mktemp -t gofetch.current_page.XXXXXX`"
115 rm -rf "$tmp" "$tmp.jpg" "$tmp.menu" "$tmp.choice"
119 if [ $MODE = 1 ]; then
120 echo "$SELECTOR" | nc
"$SERVER" "$PORT" |
sed 's:\r::g' > "$tmp"
122 echo "$SELECTOR" | nc
"$SERVER" "$PORT" > "$tmp"
125 # Process page content
128 # Special, fake mode, only from top-level
136 while [ "$CHOICE" != "" ]; do
137 if [ "$USE_DIALOG" = 1 ]; then
139 cat_menu
"$tmp" dialog |
while read ln; do
140 echo -n " $ln" >> "$tmp.menu"
142 [ "$LINES" = "" ] && LINES
=`tput lines`
143 [ "$COLUMNS" = "" ] && COLUMNS
=`tput cols`
144 title
="$SERVER: $SELECTOR"
145 dialog
--extra-button --extra-label Back \
146 --cancel-label Exit \
149 "$LINES" "$COLUMNS" "$LINES" \
150 --file "$tmp.menu" 2>"$tmp.choice"
153 if [ $val = 3 ]; then
155 elif [ $val = 1 ]; then
158 CHOICE
="`cat "$tmp.choice
" | cut -c5-`"
164 cat_menu
"$tmp" |
less
165 read -p "[$SELECTOR]: " CHOICE
168 [ "$CHOICE" = q
] && exit 255 # force-quit
169 index
="`expr 1 \* "$CHOICE" 2>/dev/null`"
170 if [ "$index" != "" ]; then
171 goto_server
="`getsel "$tmp" $index 3`"
172 goto_sel
="`getsel "$tmp" $index 2`"
173 goto_port
="`getsel "$tmp" $index 4`"
174 goto_mode
="`getsel "$tmp" $index 1 | cut -c1`"
175 sh
"$0" "$goto_server" "$goto_sel" "$goto_port" "$goto_mode"
176 [ $?
= 255 ] && exit 255 # force-quit
181 read -p "Query: " SEARCH
182 selQuery
="$SELECTOR $SEARCH"
184 exec sh
"$0" "$SERVER" "$selQuery" "$PORT" 1
187 telnet -l "$SELECTOR" "$SERVER" "$PORT"
191 echo "<BINARY FILE>" |
less
194 if img2aa
--help >/dev
/null
2>&1; then
195 img2aa
--mode=DITHERING \
196 --width=74 "$tmp" |
less
197 elif jp2a
-h >/dev
/null
2>&1; then
198 if convert
-h >/dev
/null
2>&1; then
199 convert
"$tmp" "$tmp.jpg"
200 jp2a
--border --chars=" .-+=o8#"\
201 --width=74 "$tmp.jpg" |
less
203 echo "required program not found to view images: convert" \
207 echo "required program not found to view images:" \
208 jp2a or img2aa |
less
212 echo "unknwon selector mode: <$MODE>" |
less