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