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