-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathirclib.inc.sh
executable file
·471 lines (402 loc) · 8.99 KB
/
irclib.inc.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/bin/bash
# http://tldp.org/LDP/abs/html/refcards.html#AEN22518 REFERENCE CARDS
[ -f /etc/hostname ] && HOSTNAME=`cat /etc/hostname`
[ -z "$HOSTNAME" ] && {
echo "$0" "-" No hostname defined
exit 1
}
. include upvars irc.inc upvars array.class
OURNICK="ircproxybot"
SERVER_ADDR=irc
SERVER_PORT=7776
IRC_NICK=${HOSTNAME//[^a-zA-Z0-9]/_}
IRC_PASS=fuckzynga
HAVEOP=0
# . date.sh
# . rfc1459.inc.sh
function_trap_term() {
write "QUIT :SIGTERM (Are we rebooting?)"
echo "SIGTEM" >> $0.$$.log
exit 13
}
function_trap() {
echo "^C (SIGINT) detected..."
write "QUIT :^C (SIGINT)"
echo "SIGINT" >> $0.$$.log
exit 13
}
function_exists() {
declare -f -F $1 > /dev/null
return $?
}
number_format() {
dollar_amt=$1
length=`echo $dollar_amt | awk '{ print length($0) }'`
mod=`expr $length % 3`
div3=`expr $length / 3`
if [[ $mod -ne 0 ]]
then
dollar_pt0=`echo $dollar_amt | cut -c 1-$mod`
fi
dollar_fin=`echo "${dollar_pt0}"`
modp1=`expr $mod + 1`
incr=`expr $mod + 3`
i="0"
while [ $i -lt $div3 ]
do
mySub=`echo $dollar_amt | cut -c ${modp1}-${incr}`
if [[ $modp1 -ne 1 ]]
then
dollar_fin=`echo ${dollar_fin},${mySub}`
else
dollar_fin=`echo ${dollar_fin}${mySub}`
fi
incr=`expr $incr + 3`
modp1=`expr $modp1 + 3`
((i++))
done
dollar_fin=`echo $dollar_fin`
echo -n $dollar_fin
}
write() {
echo ">>> " "$@"
echo "$@" >&5
}
pipe_to_privmsg() {
[ -n "$1" ] && TO="$1" || TO="$FROM"
# IFS=: read -r header value <<< "$mail"
while IFS= read -r LINE
do
# echo "$LINE"
write NOTICE $TO ":$LINE"
done
}
away() {
if [ -z "$1" ]
then
write "AWAY"
else
write "AWAY" ":$@"
fi
}
# [ 332 ] :irc5.foonet.com 332 ec #proxies :BashMan's Lair
on332() {
OURNICK="$3"
# [ -n "$INTERVAL" ] && printf 'Was offline for %s' "$INTERVAL" | pipe_to_privmsg
}
# Ops
# [ MODE ] :[email protected] MODE #proxies +o ec
onMODE() {
CHANNEL=$3
MODE=$4
NICK=$5
# echo MODE: Channel:$3 Mode:$4 Nick:$5 OurNick:$6
if [ "$MODE" == "+o" -a "$NICK" == "$OURNICK" ]
then
HAVEOP=1
write "WHO $CHANNEL"
fi
}
# [ 352 ] :irc5.foonet.com 352 ec #proxies chips25 proxy irc5.foonet.com chips25 H@ :0 chips25
# [ 352 ] :irc5.foonet.com 352 ec #proxies c14 proxy irc5.foonet.com c14 H@ :0 c14
# [ 352 ] :irc5.foonet.com 352 ec #proxies Pod B00D6373.D2380C72.C7E358E6.IP irc6.foonet.com Pod H :1 Pod
# [ 352 ] :irc5.foonet.com 352 ec #proxies win1 proxy irc5.foonet.com win1 H :0 win1
# [ 315 ] :irc5.foonet.com 315 ec #proxies :End of /WHO list.
on315() {
# daemon_watch | pipe_to_privmsg "#proxies" &
:
}
on352() {
CHANNEL=$4
USER=$5
VHOST=$6
SERVER=$7
NAME=$8
STATUS=$9
if [ "$STATUS" == "H" -a "$VHOST" == "proxy" ]
then
addBot "$NAME"
write "MODE $CHANNEL +o $NAME"
fi
}
# [ JOIN ] :[email protected] JOIN :#proxies
onJOIN() {
FROM_ADDR="$( split_prefix ADDR "$1" )"
FROM_NICK="$( split_prefix NICK "$1" )"
CHANNEL=$( strip_colon "$3" )
if [ "$FROM_ADDR" == "proxy" ]; then
addBot "$FROM_NICK"
if [ "$HAVEOP" -eq "1" ]; then
write "MODE $CHANNEL +o $FROM_NICK"
fi
fi
}
whois() {
write "WHOIS cyrus"
# [ 313 ] :irc5.foonet.com 313 ec cyrus :is a Network Administrator
}
in_array() {
needle="$1"
shift
while [ $# -gt 0 ]; do
if [ "$needle" == "$1" ]; then
return 0
fi
shift
done
return 1
}
isAdmin() {
in_array "$1" "${ADMIN[@]}"
}
on313() {
WHO=$4
isAdmin "$WHO" ||
ADMINS=( "${ADMINS[@]}" "$WHO" )
}
# [ 353 ] :irc5.foonet.com 353 ec = #proxies :ec @chips7 @chips26 @chips23 @chips4 @chips5 @chips3 @centos8 servant tank ...
# "<channel> :[[@|+]<nick> [[@|+]<nick> [...]]]"
BOTS_B4_ME=0
on353() {
shift 4
CHANNEL=$1
shift
while shift
do
HASH=${1:0:1}
if [ "$HASH" = "#" ]
then
# Channel Message
USER=${1:1}
elif [ "$HASH" = "+" ]
then
USER=${1:1}
elif [ "$HASH" = "@" ]
then
USER=${1:1}
addBot "$USER"
else
# Private Message
USER=${1}
fi
echo Noticed user $USER
(( BOTS_B4_ME ++ ))
done
}
# [ 366 ] :irc5.foonet.com 366 ec #proxies :End of /NAMES list.
on366() {
CHANNEL=$4
# write "WHO $4"
}
# on251() if :; then # just an alternate way of surrounding a function
# on251() {
# echo "base on251 called"
# write VHOST proxy wingtips
# write WATCH "+vpc_cw1zeg"
# write JOIN "#proxies"
# [[ $OURNICK == "ec" ]] && # [[ ${#knownBots[@]} == 0 ]] &&
# for bot in $known_bots
# do
# write WATCH "+$bot"
# done
# # write WATCH +cyrus
# }
on600() {
addBot "$4"
echo $4 has logged on
}
on601() {
removeBot "$4"
echo $4 has logged off
}
on604() {
addBot "$4"
echo $4 already online
}
on605() {
removeBot "$4"
# :irc5.foonet.com 605 ec vpc_cw1zeg * * 0 :is offline
echo \"$4\" is offline
}
declare -A knownBots=()
declare -A missingBots=()
declare -A presentBots=()
addBot()
{
echo "adding bot: $1"
presentBots["$1"]="$1"
}
removeBot()
{
unset presentBots["$1"]
}
onQUIT() {
# not even sure this works
echo "onQUIT: $@"
return
local WHO=$( split_prefix NICK "$1" )
if [[ $WHO == $IRC_NICK ]]; then
write NICK $IRC_NICK
fi
shift 2
MSG="$@"
MSG=$( strip_colon "${MSG:1}" )
declare -a MAR
MAR=( $MSG )
# Lowercase conversion: n=`echo $fname | tr A-Z a-z
# PRIVMSG: "Quit: ^C (SIGINT)"
removeBot "$WHO"
case "${MAR[1]}" in
Ping )
echo $WHO just timed out | pipe_to_privmsg
;;
* )
echo $WHO just quit: $MSG | pipe_to_privmsg
esac
}
onNOTICE() {
# :[email protected] NOTICE #wwii :This is a notice, you will be ponged to death.
PREFIX="$1"
FROM=$( split_prefix NICK "$1" )
shift 3
onPRIVMSG "$PREFIX" PRIVMSG "$FROM" "$@"
}
onPRIVMSG() {
HASH=${3:0:1}
if [ "$HASH" = "#" ]
then
# Channel Message
FROM=$3
else
FROM=$( split_prefix NICK "$1" )
# Replace all matches of $substring with $replacement
# ${string%%substring} Strip longest match of $substring from back of $string
# expr index "$string" $substring Numerical position in $string of first character in $substring* that matches [0 if no match, first character counts as position 1]
# expr match "$string" '$substring' Length of matching $substring* at beginning of $string
fi
shift 3
MSG="$@"
MSG=${MSG:1}
MAR=( ${MSG} )
if [ "${MAR[0]%,}" == "$IRC_NICK" ]; then
CMD=${MAR[1]}
ARG=${MAR[2]}
else
CMD=${MAR[0]}
ARG=${MAR[1]}
fi
# Lowercase conversion: n=`echo $fname | tr A-Z a-z
echo PRIVMSG: \"$MSG\"
case "${CMD}" in
aldskjfhasdkjfh )
echo test | pipe_to_privmsg
;;
esac
}
# if [ -n "$IRC_LAST_PACKET" ]
# then
# NOW=$(now)
# INTERVAL=$(elapsed "$IRC_LAST_PACKET" "$NOW")
# fi
main.loop() {
# export
read -t 300 -u 5 -r REPLY
el=$?
# printf "errorlevel: %2s lenreply: %3s\n" $el "${#REPLY}"
if [ "$el" -ne "0" ]
then
echo "Connection EOF/timeout Errorlevel $el" >> $0.$$.log
return $el
fi
LEN=${#REPLY}
# if [ "$LEN" -gt "0" ]
# then
# IRC_LAST_PACKET=$(now)
# export IRC_LAST_PACKET
# fi
REPLY="${REPLY:0:$((LEN-1))}"
declare -a RARRAY
IFS=" " RARRAY=( $REPLY )
PREFIX=${RARRAY[0]}
CODE=${RARRAY[1]}
# echo [ "$CODE" ] "$REPLY"
case $PREFIX in
PING ) unset RARRAY[0]
write PONG "${RARRAY[@]}"
;;
ERROR ) # [ :Closing ] ERROR :Closing Link: sup_er_fragi[CPE-124-190-251-137.gqle1.lon.bigpond.net.au] (Ping timeout)
echo "$REPLY"
exit 1
;;
* ) function_exists on$CODE && on$CODE ${RARRAY[@]} || \
case $CODE in #{{{
# 251 ) write JOIN "#proxies"
# ;;
433 ) write NICK ${IRC_NICK}_$$
;;
NOTICE ) ;;
JOIN ) ;;
PRIVMSG )
# Unknown IRC code PRIVMSG
# [ PRIVMSG ] :[email protected] PRIVMSG #proxies :hello idiot
# [ PRIVMSG ] :[email protected] PRIVMSG ec :hello idiot
# element_count=${#colors[@]}
# Special syntax to extract number of elements in array.
# element_count=${#colors[*]} works also.
#
# The "@" variable allows word splitting within quotes
#+ (extracts variables separated by whitespace).
#
# This corresponds to the behavior of "$@" and "$*"
#+ in positional parameters.
onPRIVMSG ${RARRAY[@]}
;;
ERROR ) exit 0
;;
FAIL ) echo Failed
break
;;
OK ) ;;
SENT ) exit 0
;;
"" ) echo Empty Reply, Exiting
exit 0
;;
* ) C1={$CODE:0:1}
case $C1 in
4 ) echo Unknown Error
;;
5 ) echo Unknown Error
;;
* ) NOP=0
;;
esac
;;
esac #}}}
;;
esac
}
irc.start() {
pid=$$
unset IFS
if [ -e /proc/self/fd/5 ]
then
exec 5<&-
fi
sleep 1
exec 5<>/dev/tcp/$SERVER_ADDR/$SERVER_PORT
el=$?
if [ "$el" -ne "0" ]
then
echo errorlevel $el on connection attempt to "$SERVER_ADDR :$SERVER_PORT"
exit $el
fi
write PASS $IRC_PASS
write NICK $IRC_NICK
write USER $HOSTNAME \* \* $HOSTNAME
while true
do
main.loop
done
echo "Connection Closed"
}