6b16f264bed62a596d1feefe2f826639f0e0cab7
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 # LESS: will be used with 'less' (think: "export LESS=-r")
15 # LINK_COLOR: escape sequences colour (def: 2)
16 # - : means no escape sequence
27 [ "$PORT" = "" ] && PORT
=70
28 if [ "$MODE" = "" ]; then
29 # "" or dir-like selector? -> 1 ; if not -> 0
30 echo "$SELECTOR" |
grep "/$" >/dev
/null
&& MODE
=1 || MODE
=0
31 [ "$SELECTOR" = "" ] && MODE
=1
34 if [ "$SERVER" = "" ]; then
35 echo "Syntax error: $0 [SERVER] ([SELECTOR]) ([PORT]) ([MODE])" >&2
39 # can be "-" for no escape sequences
40 [ "$LINK_COLOR" = "" ] && LINK_COLOR
=2
42 # Start and end link tags
45 if [ "$LINK_COLOR" != "-" ]; then
46 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 ln="`echo "$ln" | cut -f1`"
58 if echo "$ln" |
grep "^i" >/dev
/null
2>&1; then
59 echo "$ln" |
sed "s:^.: :g"
60 elif echo "$ln" |
grep "^$PREFIX" >/dev
/null
2>&1; then
63 field
="`echo "$ln" | cut -c1`"
66 1) typ
='DIR';; # menu, actually
67 7) typ
='(?)';; # query
68 8) typ
='TEL';; # TELnet (not TELephone)
72 +) typ
='SVR';; # redundant server
75 echo "$ln" |
sed "s:^.\\(.*\\):$typ $i $SL\\1$EL:g"
82 # $0 [FILE] [INDEX] [FIELD]
83 # Get a field from the given-by-index link in FILE
91 cat "$1" |
grep "^$PREFIX" |
tail -n+"$2" |
head -n 1 | cut
-f"$3"
94 # Save page content to 'tmp' file
95 tmp
="`mktemp -t gofetch.current_page.XXXXXX`"
97 rm -rf "$tmp" "$tmp.jpg"
101 if [ $MODE = 1 ]; then
102 echo "$SELECTOR" | nc
"$SERVER" "$PORT" |
sed 's:\r::g' > "$tmp"
104 echo "$SELECTOR" | nc
"$SERVER" "$PORT" > "$tmp"
107 # Process page content
110 # Special, fake mode, only from top-level
118 while [ "$CHOICE" != "" ]; do
119 cat_menu
"$tmp" |
less
120 read -p "[$SELECTOR]: " CHOICE
121 index
="`expr 1 \* "$CHOICE" 2>/dev/null`"
122 if [ "$index" != "" ]; then
123 goto_server
="`getsel "$tmp" $index 3`"
124 goto_sel
="`getsel "$tmp" $index 2`"
125 goto_port
="`getsel "$tmp" $index 4`"
126 goto_mode
="`getsel "$tmp" $index 1 | cut -c1`"
127 sh
"$0" "$goto_server" "$goto_sel" "$goto_port" "$goto_mode"
132 read -p "Query: " SEARCH
133 selQuery
="$SELECTOR $SEARCH"
135 exec sh
"$0" "$SERVER" "$selQuery" "$PORT" 1
138 telnet -l "$SELECTOR" "$SERVER" "$PORT"
142 echo "<BINARY FILE>" |
less
145 if convert
-h >/dev
/null
2>&1; then
146 if jp2a
-h >/dev
/null
2>&1; then
147 convert
"$tmp" "$tmp.jpg"
148 # not supported: --chars=" ░▒▓█"
149 jp2a
--border --colors --chars=" .-+=o8#"\
150 --width=74 "$tmp.jpg" |
less
152 echo "required program not found to view images: jp2a" \
156 echo "required program not found to view images: convert" \
161 echo "unknwon selector mode: <$MODE>" |
less