b1bda9bca700b5c573eb97715f6b6f5e28210fa3
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 # LINK_COLOR: escape sequences colour (def: 2)
15 # - : means no escape sequence
26 [ "$PORT" = "" ] && PORT
=70
27 if [ "$MODE" = "" ]; then
28 # "" or dir-like selector? -> 1 ; if not -> 0
29 echo "$SELECTOR" |
grep "/$" >/dev
/null
&& MODE
=1 || MODE
=0
30 [ "$SELECTOR" = "" ] && MODE
=1
33 if [ "$SERVER" = "" ]; then
34 echo "Syntax error: $0 [SERVER] ([SELECTOR]) ([PORT]) ([MODE])" >&2
38 # can be "-" for no escape sequences
39 [ "$LINK_COLOR" = "" ] && LINK_COLOR
=2
41 # Start and end link tags
44 if [ "$LINK_COLOR" != "-" ]; then
45 SL
="`tput setf $LINK_COLOR``tput setaf $LINK_COLOR`"
53 # Display a gopher menu for the given resource
56 cat "$1" |
grep "^i\|^$PREFIX" |
while read ln; do
57 if echo "$ln" |
grep "^i" >/dev
/null
2>&1; then
58 echo "$ln" | cut
-f1 |
sed "s:^.: :g"
59 elif echo "$ln" |
grep "^$PREFIX" >/dev
/null
2>&1; then
62 field
="`echo "$ln" | cut -c1`"
65 1) typ
='DIR';; # menu, actually
66 7) typ
='(?)';; # query
67 8) typ
='TEL';; # TELnet (not TELephone)
71 +) typ
='SVR';; # redundant server
74 echo "$ln" |
sed "s:^.\\([^\t]*\\).*:$typ $i $SL\\1$EL:g"
79 # $0 [FILE] [INDEX] [FIELD]
80 # Get a field from the given-by-index link in FILE
88 cat "$1" |
grep "^$PREFIX" |
tail -n+"$2" |
head -n 1 | cut
-f"$3"
91 # Save page content to 'tmp' file
92 tmp
="`mktemp -t gofetch.current_page.XXXXXX`"
94 rm -rf "$tmp" "$tmp.jpg"
98 if [ $MODE = 1 ]; then
99 echo "$SELECTOR" | nc
"$SERVER" "$PORT" |
sed 's:\r::g' > "$tmp"
101 echo "$SELECTOR" | nc
"$SERVER" "$PORT" > "$tmp"
104 # Process page content
107 # Special, fake mode, only from top-level
115 while [ "$CHOICE" != "" ]; do
116 cat_menu
"$tmp" |
less
117 read -p "[$SELECTOR]: " CHOICE
118 [ "$CHOICE" = q
] && exit 255 # force-quit
119 index
="`expr 1 \* "$CHOICE" 2>/dev/null`"
120 if [ "$index" != "" ]; then
121 goto_server
="`getsel "$tmp" $index 3`"
122 goto_sel
="`getsel "$tmp" $index 2`"
123 goto_port
="`getsel "$tmp" $index 4`"
124 goto_mode
="`getsel "$tmp" $index 1 | cut -c1`"
125 sh
"$0" "$goto_server" "$goto_sel" "$goto_port" "$goto_mode"
126 [ $?
= 255 ] && exit 255 # force-quit
131 read -p "Query: " SEARCH
132 selQuery
="$SELECTOR $SEARCH"
134 exec sh
"$0" "$SERVER" "$selQuery" "$PORT" 1
137 telnet -l "$SELECTOR" "$SERVER" "$PORT"
141 echo "<BINARY FILE>" |
less
144 if img2aa
--help >/dev
/null
2>&1; then
145 img2aa
--mode=DITHERING \
146 --width=74 "$tmp" |
less
147 elif jp2a
-h >/dev
/null
2>&1; then
148 if convert
-h >/dev
/null
2>&1; then
149 convert
"$tmp" "$tmp.jpg"
150 jp2a
--border --chars=" .-+=o8#"\
151 --width=74 "$tmp.jpg" |
less
153 echo "required program not found to view images: convert" \
157 echo "required program not found to view images:" \
158 jp2a or img2aa |
less
162 echo "unknwon selector mode: <$MODE>" |
less