gopher.sh: fix LESS and colours
[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 # LINK_COLOR: escape sequences colour (def: 2)
15 # - : means no escape sequence
16 # 1 : means colour 1
17 # 2 : means colour 2
18 # [...]
19
20 SERVER="$1"
21 SELECTOR="$2"
22 PORT="$3"
23 MODE="$4"
24
25 # Defaults:
26 [ "$PORT" = "" ] && PORT=70
27 if [ "$MODE" = "" ]; then
28 # "" or dir-like selector? -> 1 ; if not -> 0
29 echo "$SELECTOR" | grep "/$" >/dev/null && MODE=1 || MODE=0
30 [ "$SELECTOR" = "" ] && MODE=1
31 fi
32
33 if [ "$SERVER" = "" ]; then
34 echo "Syntax error: $0 [SERVER] ([SELECTOR]) ([PORT]) ([MODE])" >&2
35 exit 2
36 fi
37
38 # can be "-" for no escape sequences
39 [ "$LINK_COLOR" = "" ] && LINK_COLOR=2
40
41 # Start and end link tags
42 SL=
43 EL=
44 if [ "$LINK_COLOR" != "-" ]; then
45 SL="`tput setf $LINK_COLOR``tput setaf $LINK_COLOR`"
46 EL="`tput init`"
47 export LESS="${LESS}-R"
48 fi
49
50 PREFIX="[0-9hIg]"
51
52 # $0 [FILE]
53 # Display a gopher menu for the given resource
54 cat_menu() {
55 i=0
56 cat "$1" | grep "^i\|^$PREFIX" | while read ln; do
57 if echo "$ln" | grep "^i" >/dev/null 2>&1; then
58 echo "$ln" | cut -f1 | sed "s:^.: :g"
59 elif echo "$ln" | grep "^$PREFIX" >/dev/null 2>&1; then
60 i=`expr $i + 1`
61 [ $i -le 9 ] && i=0$i
62 field="`echo "$ln" | cut -c1`"
63 case "$field" in
64 0) typ='TXT';;
65 1) typ='DIR';; # menu, actually
66 7) typ='(?)';; # query
67 8) typ='TEL';; # TELnet (not TELephone)
68 h) typ='WEB';; # HTML
69 I) typ='IMG';;
70 g) typ='GIF';;
71 +) typ='SVR';; # redundant server
72 *) typ='!!!';;
73 esac
74 echo "$ln" | sed "s:^.\\([^\t]*\\).*:$typ $i $SL\\1$EL:g"
75 fi
76 done
77 }
78
79 # $0 [FILE] [INDEX] [FIELD]
80 # Get a field from the given-by-index link in FILE
81 #
82 # Fields:
83 # 1 = type/name
84 # 2 = selector
85 # 3 = server
86 # 4 = port
87 getsel() {
88 cat "$1" | grep "^$PREFIX" | tail -n+"$2" | head -n 1 | cut -f"$3"
89 }
90
91 # Save page content to 'tmp' file
92 tmp="`mktemp -t gofetch.current_page.XXXXXX`"
93 finish() {
94 rm -rf "$tmp" "$tmp.jpg"
95 }
96 trap finish EXIT
97
98 if [ $MODE = 1 ]; then
99 echo "$SELECTOR" | nc "$SERVER" "$PORT" | sed 's:\r::g' > "$tmp"
100 else
101 echo "$SELECTOR" | nc "$SERVER" "$PORT" > "$tmp"
102 fi
103
104 # Process page content
105 case "$MODE" in
106 download)
107 # Special, fake mode, only from top-level
108 cat "$tmp"
109 ;;
110 0)
111 cat "$tmp" | less
112 ;;
113 1|+)
114 CHOICE=start
115 while [ "$CHOICE" != "" ]; do
116 cat_menu "$tmp" | less
117 read -p "[$SELECTOR]: " CHOICE
118 [ "$CHOICE" = q ] && exit 255 # force-quit
119 index="`expr 1 \* "$CHOICE" 2>/dev/null`"
120 if [ "$index" != "" ]; then
121 goto_server="`getsel "$tmp" $index 3`"
122 goto_sel="`getsel "$tmp" $index 2`"
123 goto_port="`getsel "$tmp" $index 4`"
124 goto_mode="`getsel "$tmp" $index 1 | cut -c1`"
125 sh "$0" "$goto_server" "$goto_sel" "$goto_port" "$goto_mode"
126 [ $? = 255 ] && exit 255 # force-quit
127 fi
128 done
129 ;;
130 7)
131 read -p "Query: " SEARCH
132 selQuery="$SELECTOR $SEARCH"
133
134 exec sh "$0" "$SERVER" "$selQuery" "$PORT" 1
135 ;;
136 8)
137 telnet -l "$SELECTOR" "$SERVER" "$PORT"
138 read DUMMY
139 ;;
140 9)
141 echo "<BINARY FILE>" | less
142 ;;
143 g|I)
144 if img2aa --help >/dev/null 2>&1; then
145 img2aa --mode=DITHERING \
146 --width=74 "$tmp" | less
147 elif jp2a -h >/dev/null 2>&1; then
148 if convert -h >/dev/null 2>&1; then
149 convert "$tmp" "$tmp.jpg"
150 jp2a --border --chars=" .-+=o8#"\
151 --width=74 "$tmp.jpg" | less
152 else
153 echo "required program not found to view images: convert" \
154 | less
155 fi
156 else
157 echo "required program not found to view images:" \
158 jp2a or img2aa | less
159 fi
160 ;;
161 *)
162 echo "unknwon selector mode: <$MODE>" | less
163 exit 3
164 ;;
165 esac
166