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