-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.sh
executable file
·365 lines (320 loc) · 7.73 KB
/
bot.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env dash
#
# Copyright (C) 2024 Maria Lisina
# Copyright (C) 2024 Danil Lisin
# SPDX-License-Identifier: Apache-2.0
#
# Run this software with `env -i` to avoid variable conflict
set -e
umask 77
version="2.6.2"
dir="${0%/*}"
cache="${dir}/cache/${$}"
config="${dir}/config"
lists="${dir}/lists"
modules="${dir}/modules"
submodules="${dir}/submodules"
units="${dir}/units"
offset=-1
if [ -n "${1}" ]
then
while getopts ha:lg:r:m:t:ci:e:d:n:x: options
do
case "${options}" in
(h)
help=0
;;
(a)
api_address="${OPTARG}"
;;
(l)
api_address="127.0.0.1:8081"
;;
(g)
shorts_limit=${OPTARG}
;;
(r)
inline_limit=${OPTARG}
;;
(m)
caching_mode=${OPTARG}
;;
(t)
caching_time=${OPTARG}
;;
(c)
cache_clear=0
;;
(i)
internal_timeout=${OPTARG}
;;
(e)
external_timeout=${OPTARG}
;;
(d)
head_timeout=${OPTARG}
;;
(n)
internal_proxy="${OPTARG}"
;;
(x)
external_proxy="${OPTARG}"
;;
(*)
echo "See '${0} -h'"
exit 1
;;
esac
done
shift $((OPTIND - 1))
else
help=0
fi
if [ -n "${help}" ]
then
echo "Sekoohaka Bot v${version}" \
"\n\nUsage: ${0} [options] [token]" \
"\n\nOptions:" \
"\n -h\t\tShow help information" \
"\n -a <addr>\tTelegram Bot API address, default: api.telegram.org" \
"\n -l\t\tUse local Telegram Bot API, address: 127.0.0.1:8081" \
"\n -r <num>\tInline results limit, max: 50, default: 10" \
"\n -g <num>\tShortcuts storage limit, max: 10000, default: 100" \
"\n -m <mode>\tCaching mode, default: normal" \
"\n -t <secs>\tCaching time, max: 1000, default: 300 secs" \
"\n -c\t\tClear cache automatically" \
"\n -i <secs>\tTelegram Bot API connetion timeout, max: 10, default: 10 secs" \
"\n -e <secs>\tImage Boards API connetion timeout, max: 10, default: 5 secs" \
"\n -d <secs>\tHead request connetion timeout, max: 10, default: 2 secs" \
"\n -n <addr>\tProxy server for Telegram Bot API" \
"\n -x <addr>\tProxy server for Image Boards API" \
"\n\nCaching modes:" \
"\n none\t\tNo caching" \
"\n normal\tCache inline results and posts" \
"\n advanced\tExtract posts cache from inline results"
exit 0
fi
for required in busybox curl jq recode
do
if ! command -v ${required} > /dev/null
then
missing="${missing} ${required}"
fi
done
if [ -n "${missing}" ]
then
echo "Missing dependencies:${missing}" \
"\nFor more information follow: https://command-not-found.com/"
exit 1
fi
for function in base64 bc cat cut date find grep ls mkdir rm sed seq sha1sum sleep stat tr
do
if busybox ${function} --help > /dev/null 2>&1
then
alias ${function}="busybox ${function}"
else
missing="${missing} ${function}"
fi
done
if [ -n "${missing}" ]
then
echo "Missing BusyBox functions:${missing}" \
"\nUpdate your BusyBox or get a version with all the required functions"
exit 1
fi
if [ -z "${api_address}" ]
then
api_address="https://api.telegram.org"
fi
if [ -n "${shorts_limit}" ]
then
if ! test ${shorts_limit} -gt 0 > /dev/null 2>&1
then
echo "Illegal shortcuts limit number" \
"\nSee '${0} -h'"
exit 1
fi
if [ ${shorts_limit} -gt 10000 ]
then
shorts_limit=10000
fi
else
shorts_limit=100
fi
if [ -n "${inline_limit}" ]
then
if ! test ${inline_limit} -gt 0 > /dev/null 2>&1
then
echo "Illegal inline results limit number" \
"\nSee '${0} -h'"
exit 1
fi
if [ ${inline_limit} -gt 50 ]
then
inline_limit=50
fi
else
inline_limit=10
fi
if [ -n "${caching_mode}" ]
then
case "${caching_mode}" in
(none | normal | advanced)
;;
(*)
echo "Unrecognized caching mode ${caching_mode}" \
"\nSee '${0} -h'"
exit 1
;;
esac
else
caching_mode=normal
fi
if [ -n "${caching_time}" ]
then
if ! test ${caching_time} -gt 0 > /dev/null 2>&1
then
echo "Illegal caching time" \
"\nSee '${0} -h'"
exit 1
fi
if [ ${caching_time} -gt 1000 ]
then
caching_time=1000
fi
else
caching_time=300
fi
if [ -n "${internal_timeout}" ]
then
if ! test ${internal_timeout} -gt 0 > /dev/null 2>&1
then
echo "Illegal Telegram Bot API timeout" \
"\nSee '${0} -h'"
exit 1
fi
if [ ${internal_timeout} -gt 10 ]
then
internal_timeout=10
fi
else
internal_timeout=10
fi
if [ -n "${external_timeout}" ]
then
if ! test ${external_timeout} -gt 0 > /dev/null 2>&1
then
echo "Illegal Image Boards API timeout" \
"\nSee '${0} -h'"
exit 1
fi
if [ ${external_timeout} -gt 10 ]
then
external_timeout=10
fi
else
external_timeout=5
fi
if [ -n "${head_timeout}" ]
then
if ! test ${head_timeout} -gt 0 > /dev/null 2>&1
then
echo "Illegal head request timeout" \
"\nSee '${0} -h'"
exit 1
fi
if [ ${head_timeout} -gt 10 ]
then
head_timeout=10
fi
else
head_timeout=2
fi
if [ -n "${1}" ]
then
api_token="${1}"
shift
fi
if [ -n "${1}" ]
then
echo "Unrecognized action ${1}" \
"\nSee '${0} -h'"
exit 1
fi
until [ -n "${api_token}" ]
do
read -p "Telegram Bot API Token: " -r api_token
done
alias parameter="cut -d ' ' -f"
alias enhash="sha1sum | parameter 1"
alias htmlescape="sed -e 's/</\</g' -e 's/>/\>/g'"
alias urlencode="jq -Rr @uri"
for ascii in $(seq 33 126)
do
ascii_table="${ascii_table} $(printf "%b" "\0$(printf "%o" ${ascii})")"
done
rm -fr "${cache}"
mkdir -p "${cache}"
mkdir -p "${config}"
if ! [ -f "${lists}/aliases.txt" ]
then
cat "${lists}/aliases.txt.default" > "${lists}/aliases.txt"
fi
if ! [ -f "${lists}/blacklist.txt" ]
then
cat "${lists}/blacklist.txt.default" > "${lists}/blacklist.txt"
fi
if ! [ -f "${lists}/whitelist.txt" ]
then
cat "${lists}/whitelist.txt.default" > "${lists}/whitelist.txt"
fi
echo "PID: ${$}"
curl --get \
--max-time ${internal_timeout} \
--output "${cache}/getMe.json" \
--proxy "${internal_proxy}" \
--show-error \
--silent \
"${api_address}/bot${api_token}/getMe"
if ! jq -e '.' "${cache}/getMe.json" > /dev/null
then
echo "Failed to get the bot information"
exit 1
fi
username="$(jq -r '.result.username' "${cache}/getMe.json")"
if [ "${username}" = "null" ]
then
echo "Failed to authorize the bot"
exit 1
fi
echo "Bot: ${username}"
while trap 'wait && exit 0' INT TERM
do
if ! curl --data "offset=${offset}" \
--get \
--output "${cache}/getUpdates.json" \
--proxy "${internal_proxy}" \
--silent \
"${api_address}/bot${api_token}/getUpdates"
then
continue
fi
if ! update_id="$(jq -r '.result.[0].update_id' "${cache}/getUpdates.json")"
then
continue
fi
if [ "${update_id}" = "null" ]
then
continue
fi
update="${cache}/${update_id}.json"
if ! jq -c '.result.[0]' "${cache}/getUpdates.json" > "${update}"
then
continue
fi
for module in "${modules}"/*
do
. "${module}" &
done
offset=$((update_id + 1))
done