58175702600c07be27fe6f97fddb896273c14163
[gofetch.git] / gopher.sh
1 #!/bin/sh
2
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)
8 # 0: plain text
9 # 1: menu (dir-like)
10 # ...
11 # download: fake mode to download the result without changes
12
13 # Manual:
14 # When asked for a number (non-dialog uses only), you can also
15 # enter 'n' or 'p' to get the next or previous item (+1 or -1)
16
17 # ENV variables:
18 # USE_DIALOG: force the usage of the command 'dialog'
19 # : true if dialog is found
20 # 0 : do not
21 # 1 : force the use of dialog
22 # LINK_COLOR: escape sequences colour (def: 2)
23 # - : means no escape sequence
24 # 1 : means colour 1
25 # 2 : means colour 2
26 # [...]
27 # INVERT : invert the output for image viewing (for white backgrounds)
28 # 0 : do not invert (default)
29 # 1 : invert
30
31 # EXIT Codes:
32 # 0: ok
33 # 1: syntax error
34 # 2: cannot contact server
35 # 3: unknown selector mode
36 # 255: special exit more 'q'
37
38 SERVER="$1"
39 SELECTOR="$2"
40 PORT="$3"
41 MODE="$4"
42
43 PREFIX="[0-9hIg+]"
44
45 # Defaults:
46 [ "$PORT" = "" ] && PORT=70
47 if [ "$MODE" = "" ]; then
48 # "" or dir-like selector? -> 1 ; if not -> 0
49 echo "$SELECTOR" | grep "/$" >/dev/null && MODE=1 || MODE=0
50 [ "$SELECTOR" = "" ] && MODE=1
51
52 # check explicit modes:
53 if echo "$SELECTOR" | grep "^/\($PREFIX\|download\)/" >/dev/null; then
54 MODE="`echo "$SELECTOR" | cut -f2 -d/`"
55 SELECTOR="`echo "$SELECTOR" | sed 's:^/[^/]*/::'`"
56 fi
57 fi
58
59 if [ "$SERVER" = "" ]; then
60 echo "Syntax error: $0 [SERVER] ([SELECTOR]) ([PORT]) ([MODE])" >&2
61 exit 1
62 fi
63
64 # can be "-" for no escape sequences
65 [ "$LINK_COLOR" = "" ] && LINK_COLOR=2
66
67 # Start and end link tags
68 SL=
69 EL=
70 if [ "$LINK_COLOR" != "-" ]; then
71 SL="`tput setf $LINK_COLOR``tput setaf $LINK_COLOR`"
72 EL="`tput init`"
73 export LESS="${LESS}-R"
74 fi
75
76 # 'dialog' or text
77 if [ "$USE_DIALOG" = "" ]; then
78 if dialog --help >/dev/null 2>&1; then
79 USE_DIALOG=1
80 else
81 USE_DIALOG=0
82 fi
83 fi
84
85 # Invert image viewer
86 if [ "$INVERT" = 1 ]; then
87 INVERT="--invert"
88 else
89 INVERT=
90 fi
91
92 # $0 [FILE] (dialog)
93 # Display a gopher menu for the given resource
94 cat_menu() {
95 i=0
96 cat "$1" | grep "^i\|^$PREFIX" | while read ln; do
97 if echo "$ln" | grep "^i" >/dev/null 2>&1; then
98 if [ "$2" != dialog ]; then
99 echo "$ln" | sed "s:^.\([^\t]*\).*$: \1:g"
100 else
101 echo "$ln" | sed 's:":'"''"':g;s:^.\([^\t]*\).*$:" " "\1":g'
102 fi
103 elif echo "$ln" | grep "^$PREFIX" >/dev/null 2>&1; then
104 i=`expr $i + 1`
105 [ $i -le 9 ] && i=0$i
106 field="`echo "$ln" | cut -c1`"
107 case "$field" in
108 0) typ='TXT';;
109 1) typ='DIR';; # menu, actually
110 7) typ='(?)';; # query
111 8) typ='TEL';; # TELnet (not TELephone)
112 h) typ='WEB';; # HTML
113 I) typ='IMG';;
114 g) typ='GIF';;
115 +) typ='SVR';; # redundant server
116 *) typ='!!!';;
117 esac
118 if [ "$2" != dialog ]; then
119 echo "$ln" | sed "s:^.\\([^\t]*\\).*:$typ $i $SL\\1$EL:g"
120 else
121 echo "$ln" | sed "s:"'"'":'':g;s:^.\\([^\t]*\\).*:"'"'"$typ $i"'"'" "'"\1"'":g"
122 fi
123 fi
124 done
125 }
126
127 # $0 [FILE] [INDEX] [FIELD]
128 # Get a field from the given-by-index link in FILE
129 #
130 # Fields:
131 # 1 = type/name
132 # 2 = selector
133 # 3 = server
134 # 4 = port
135 getsel() {
136 cat "$1" | grep "^$PREFIX" | tail -n+"$2" | head -n 1 | cut -f"$3"
137 }
138
139 # Save page content to 'tmp' file
140 tmp="`mktemp -t gofetch.current_page.XXXXXX`"
141 finish() {
142 rm -rf "$tmp" "$tmp.jpg" "$tmp.menu" "$tmp.choice"
143 }
144 trap finish EXIT
145
146 echo "$SELECTOR" | nc "$SERVER" "$PORT" > "$tmp"
147 if [ $? != 0 ]; then
148 echo Cannot contact gopher server "[$SERVER]" >&2
149 exit 2
150 fi
151
152 if [ $MODE = 1 ]; then
153 sed --in-place 's:\r::g' "$tmp"
154 fi
155
156 # Process page content
157 case "$MODE" in
158 download)
159 # Special, fake mode, only from top-level
160 cat "$tmp"
161 ;;
162 0)
163 cat "$tmp" | less
164 ;;
165 1|+)
166 CHOICE=0
167 while [ "$CHOICE" != "" ]; do
168 if [ "$USE_DIALOG" = 1 ]; then
169 > "$tmp.menu"
170 cat_menu "$tmp" dialog | while read ln; do
171 echo -n " $ln" >> "$tmp.menu"
172 done
173 [ "$LINES" = "" ] && LINES=`tput lines`
174 [ "$COLUMNS" = "" ] && COLUMNS=`tput cols`
175 title="$SERVER: $SELECTOR"
176 dialog --extra-button --extra-label Back \
177 --cancel-label Exit \
178 --no-shadow \
179 --menu "$title" \
180 "$LINES" "$COLUMNS" "$LINES" \
181 --file "$tmp.menu" 2>"$tmp.choice"
182 val=$?
183 clear
184
185 if [ $val = 3 ]; then
186 CHOICE=""
187 elif [ $val = 1 ]; then
188 CHOICE="q"
189 else
190 CHOICE="`cat "$tmp.choice" | cut -c5-`"
191 fi
192
193 rm "$tmp.menu"
194 rm "$tmp.choice"
195 else
196 cat_menu "$tmp" | less
197 read -p "[$SELECTOR]: " NEW_CHOICE
198 if [ "$NEW_CHOICE" = p ]; then
199 CHOICE=`expr "$CHOICE" - 1 2>/dev/null`
200 if [ "$CHOICE" -lt 0 ]; then
201 CHOICE=`cat "$tmp" | grep "^$PREFIX" | wc --lines`
202 fi
203 elif [ "$NEW_CHOICE" = n ]; then
204 CHOICE=`expr "$CHOICE" + 1 2>/dev/null`
205 else
206 CHOICE="$NEW_CHOICE"
207 fi
208 fi
209
210 [ "$CHOICE" = q ] && exit 255 # force-quit
211 index="`expr 1 \* "$CHOICE" 2>/dev/null`"
212 if [ "$index" != "" ]; then
213 goto_server="`getsel "$tmp" $index 3`"
214 goto_sel="`getsel "$tmp" $index 2`"
215 goto_port="`getsel "$tmp" $index 4`"
216 goto_mode="`getsel "$tmp" $index 1 | cut -c1`"
217 echo "Digging to [$goto_server:$goto_port] $index [$goto_sel]..."
218 sh "$0" "$goto_server" "$goto_sel" "$goto_port" "$goto_mode"
219 [ $? = 255 ] && exit 255 # force-quit
220 fi
221 done
222 ;;
223 7)
224 read -p "Query: " SEARCH
225 selQuery="$SELECTOR $SEARCH"
226
227 exec sh "$0" "$SERVER" "$selQuery" "$PORT" 1
228 ;;
229 8)
230 telnet -l "$SELECTOR" "$SERVER" "$PORT"
231 read DUMMY
232 ;;
233 9)
234 echo "<BINARY FILE>" | less
235 ;;
236 g|I)
237 if img2aa --help >/dev/null 2>&1; then
238 img2aa --mode=DITHERING \
239 $INVERT \
240 --width=74 "$tmp" | less
241 elif jp2a -h >/dev/null 2>&1; then
242 if convert -h >/dev/null 2>&1; then
243 convert "$tmp" "$tmp.jpg"
244 jp2a --border --chars=" .-+=o8#"\
245 $INVERT \
246 --width=74 "$tmp.jpg" | less
247 else
248 echo "required program not found to view images: convert" \
249 | less
250 fi
251 else
252 echo "required program not found to view images:" \
253 jp2a or img2aa | less
254 fi
255 ;;
256 *)
257 echo "unknwon selector mode: <$MODE>" | less
258 exit 3
259 ;;
260 esac
261