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