-
Notifications
You must be signed in to change notification settings - Fork 2
/
jjc
executable file
·1305 lines (1108 loc) · 31.4 KB
/
jjc
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/awk -f
#
# This script parses irc messages and user input and maintains logs of all
# channels and private messages.
# Messages are read from stdin. Messages prefixed with "u " are
# considered user input, while "i " denotes irc messages. Messages
# prefixed with "s " are signals. Processed user input is printed to
# stdout.
BEGIN {
# The individual parts of an irc messages are separated by
# space(s), so we set awks field separator accordingly.
FS = " "
# The casemap array holds the various casemapping flavors. It
# also maps upper to lowercase characters. We map A-Z to a-z
# here, while the rest of the characters, of a certain
# casemapping, are set while parsing the ISUPPORT messages in
# on_isupport().
# The uppercase_range and lowercase_range variables also get
# complimented depending on the servers casemapping. They get
# used when checking whether or not a string contains any
# characters which would need folding.
casemap["ascii"] = "A-Z a-z"
casemap["rfc1459"] = "][\\~A-Z }{|^a-z"
casemap["strict"] = "][\\A-Z }{|a-z"
casemap["strict_rfc1459"] = casemap["strict"]
casemap["rfc1459_strict"] = casemap["strict"]
casemap["uppercase_range"] = "A-Z"
casemap["lowercase_range"] = "a-z"
# A-Z -> a-z
for (i = 65; i < 91; i++)
casemap[sprintf("%c", i)] = sprintf("%c", i + 32)
i = ""
# General settings and connection related state.
irc["connected"] = 0
irc["away"] = 0
irc["nick"] = ENVIRON["IRC_NICK"]
irc["nick_lc"] = irc_tolower(irc["nick"])
irc["user"] = ENVIRON["IRC_USER"]
irc["name"] = ENVIRON["IRC_REALNAME"]
irc["password"] = ENVIRON["IRC_PASSWORD"]
irc["host"] = ENVIRON["IRC_HOST"]
irc["network"] = irc["host"]
irc["logdir"] = ENVIRON["IRC_DIR"] "/" irc["host"]
irc["motdfile"] = irc["logdir"] "/motd"
# Initialize the channels array. It stores all joined channels and
# their users.
split("", channels)
# The command array maps an event to a command name. That name,
# appended to "prefix", is then used as command when calling
# an external event script via ext_command().
command["prefix"] = "irc_on_"
command["add"] = "query"
command["connect"] = "connect"
command["ctcp"] = "ctcp"
command["highlight"] = "highlight"
command["invite"] = "invite"
command["join"] = "join"
command["kick"] = "kick"
command["part"] = "part"
# The support array stores information obtained from the ISUPPORT
# messages and IRCv3 capabilities.
support["casemapping"] = "ascii"
support["chantypes"] = "#&"
support["prefix"] = "@+"
support["statusmsg"] = ""
# Try to create the "channels" directory while also creating all
# parent directories as needed.
cmd = "exec mkdir -p -- \"$IRC_DIR/$IRC_HOST/channels\" >&2"
if (system(cmd) != 0)
exit(1)
cmd = ""
# Initiate the irc login sequence which sends the PASS, NICK and
# USER messages to the server.
login(irc["nick"], irc["user"], irc["name"], irc["password"])
}
# Ignore malformed lines.
!/^[isu] [0-9]+ ./ { next }
# Our parent process receives the raw irc messages and reads user input.
# Before sending it to us, the read lines are marked with a leading "u"
# and "i" for user and irc respectively. Signals received by our parent
# are send as text prefixed with "s".
/^u / {
debug_usr()
# Ignore all user input until we are fully connected to the irc
# server, which is the case when all relevant ISUPPORT messages
# have been parsed.
if (!irc["connected"])
next
handle_user_input()
# All following code is specific to irc messages, so let's skip
# it.
next
}
function handle_user_input()
{
time = $2
# Commands are case-insensitive.
cmd = tolower($3)
# Having saved the command, we can now remove everything up to the
# actual parameters.
sub(/^u [0-9]+ +[^ ]+ ?/, "")
if (cmd == "") { print_out("Missing command") }
else if (cmd == "action") { cmd_msg("ACTION") }
else if (cmd == "me") { cmd_msg("ACTION") }
else if (cmd == "away") { cmd_generic("AWAY", 0, 0) }
else if (cmd == "invite") { cmd_generic("INVITE", 2, 2) }
else if (cmd == "join") { cmd_generic("JOIN", 1, 2) }
else if (cmd == "kick") { cmd_generic("KICK", 2, 0, 3) }
else if (cmd == "list") { cmd_list() }
else if (cmd == "ls") { cmd_list() }
else if (cmd == "message") { cmd_msg("PRIVMSG") }
else if (cmd == "msg") { cmd_msg("PRIVMSG") }
else if (cmd == "notice") { cmd_msg("NOTICE") }
else if (cmd == "names") { cmd_generic("NAMES", 1, 1) }
else if (cmd == "nick") { cmd_generic("NICK", 1, 1) }
else if (cmd == "mode") { cmd_generic("MODE", 1) }
else if (cmd == "part") { cmd_part() }
else if (cmd == "quit") { cmd_quit() }
else if (cmd == "raw") { cmd_raw() }
else if (cmd == "topic") { cmd_generic("TOPIC", 1, 1) }
else if (cmd == "topicset") { cmd_generic("TOPIC", 1, 0, 2) }
else if (cmd == "whois") { cmd_generic("WHOIS", 1, 0) }
else { print_out("Unknown command: " cmd) }
}
/^s / {
time = $2
signal = $3
# Reopen logs at their original location after log rotation.
if (signal == "SIGUSR1") {
print_out("Reopening all log files due to SIGUSR1")
close_all()
}
next
}
# Before actual handling of the various message types, the raw irc
# messages get parsed here.
/^i / {
parse_irc_input()
debug_irc()
}
# Dispatch the handling of parsed messages to separate functions.
cmd == "JOIN" { on_join(); next }
cmd == "QUIT" { on_quit(); next }
cmd == "PART" { on_part(); next }
cmd == "MODE" { on_mode(); next }
cmd == "PRIVMSG" { on_msg(); next }
cmd == "NICK" { on_nick(); next }
cmd == "NOTICE" { on_msg(); next }
cmd == "TOPIC" { on_topic(); next }
cmd == "KICK" { on_kick(); next }
cmd == "PING" { on_ping(); next }
cmd == "PONG" { next }
cmd ~ /^33[23]$/ { on_topicinfo(); next }
cmd == "353" { on_names(); next }
cmd == "366" { on_names(); next }
cmd == "INVITE" { on_invite(); next }
cmd == "301" { on_whois(); next }
cmd ~ /^31[1234789]$/ { on_whois(); next }
cmd ~ /^30[56]$/ { on_away(); next }
cmd == "221" { on_modereply(); next }
cmd ~ /^32[49]$/ { on_modereply(); next }
cmd == "341" { on_inviting(); next }
cmd == "367" { on_banlist(); next }
cmd == "401" { on_nosuchnick(); next }
cmd == "433" { on_nickinuse(); next }
cmd == "432" { on_erroneous(); next }
cmd ~ /^37[256]$/ { on_motd(); next }
cmd == "001" { on_welcome(); next }
cmd == "004" { on_myinfo(); next }
cmd == "005" { on_isupport(); next }
cmd == "CAP" { on_cap(); next }
{ on_generic(); next }
# Split irc messages into the global arrays "tag", "source" and "param".
# The irc command (name or numerical) is stored in the global variable
# "cmd". All arrays have a corresponding variable holding the number of
# array elements. They are called ntag, nsource and nparam.
function parse_irc_input( field, i, line, has_trailing, trailing)
{
time = $2
# Skip the "i <time>" prefix.
field = 3
# Parse tags.
if ($field ~ /^@/) {
ntag = split(substr($field, 2), tag, /;/)
field++
} else {
ntag = 0
}
# Parse source.
if ($field ~ /^:/) {
nsource = split(substr($field, 2), source, /[@!]/)
field++
} else {
nsource = 0
}
cmd = toupper($field)
field++
# In order to be able to strip everything up to the trailing
# parameter, which starts with " :", we first have to strip the
# same prefix from the source field. It should be the first
# occurence if there is a source field.
line = $0
if (nsource)
sub(/ *:/, " ", line)
# Find and extract the trailing parameter.
if (i = index(line, " :")) {
has_trailing = 1
trailing = substr(line, i+2)
# Remove the trailing parameter from the line.
line = substr(line, 1, i-1)
}
$0 = line
# Remove all fields which have been parsed already.
for (i = 1; i < field; i++)
$i = ""
# At this point, only parameters should be remaining. Split them
# into the param array.
nparam = split($0, param, " ")
# Push the trailing parameter to the end of the parameter array.
if (has_trailing)
param[++nparam] = trailing
}
## General functions #####################################################
function eprint(retval, text)
{
print text >"/dev/stderr"
exit retval
}
function stderr(text)
{
print "jjc:", text >"/dev/stderr"
}
function debug_usr()
{
if (ENVIRON["JJ_DEBUG"] == "") return
print "> " substr($0, 3) " <" >"/dev/stderr"
}
function debug_irc()
{
if (ENVIRON["JJ_DEBUG"] == "") return
printf "\033[33m%s\033[0m ", cmd >"/dev/stderr"
if (nsource)
for (i = 1; i <= nsource; i++)
printf "\033[30ms%d=\033[0m%s ",
i, source[i] >"/dev/stderr"
if (nparam)
for (i = 1; i <= nparam; i++)
printf "\033[30mp%d=\033[37m%s\033[0m ",
i, param[i] >"/dev/stderr"
printf "\n" >"/dev/stderr"
}
function close_all()
{
for (chan in channels)
close(chan_to_filename(chan))
# Also close server.log.
close(chan_to_filename())
}
function send(text)
{
printf "%s\r\n", text
fflush()
}
function print_out(text, chan, nick, suffix, file)
{
file = chan_to_filename(chan)
if (nick == "" || nick == irc["host"])
nick = "-"
printf "%u <%s>%s %s\n", time, nick, suffix, text >>file
fflush(file)
}
function login(nick, user, name, pass)
{
if (pass != "") send("PASS " pass)
send("CAP REQ :echo-message")
send("CAP END")
send("NICK " nick)
send("USER " user " localhost * :" name)
}
function irc_strcmp(a, b)
{
if (a == b)
return 1
else if (length(a) != length(b))
return 0
else
return irc_tolower(a) == irc_tolower(b)
}
function irc_tolower(str, char, i, lower)
{
# Nothing to do on all lowercase strings.
if (str !~ "[" casemap["uppercase_range"] "]")
return str
# Without concatenating the empty string, busybox awk thinks we
# want to call the function lower() here.
while (char = substr(str, ++i, 1))
lower = lower "" (char in casemap ? casemap[char] : char)
return lower
}
function set_nick(nick)
{
irc["nick"] = nick
irc["nick_lc"] = irc_tolower(nick)
return 1
}
function is_me(nick)
{
return irc_strcmp(nick, irc["nick"])
}
function is_chan(str)
{
# Return the position of the first character of "str" in the
# variable storing the supported channel prefixes. Returns 0 if
# there is no occurence.
return index(support["chantypes"], substr(str, 1, 1))
}
function nick_in_chan(nick, chan)
{
# Return the position of the first occurence of " nick ". Returns
# 0 if it can't be found. Because nicknames in the channels
# array are surrounded by spaces, we will always be able to match
# whole nicks, instead of getting a partial match when searching
# for "john" while that channel also has a user called "johndoe".
return index(channels[irc_tolower(chan)],
" " irc_tolower(nick) " ")
}
function chan_to_filename(chan)
{
# Because "chan" will be used as name for the logfile, we can't
# allow it to contain slashes and the null character. Might as
# well remove all control characters while we are at it.
gsub("[/[:cntrl:]]", "", chan)
# Figure out the target file.
if (chan == "" || chan == irc["host"])
return irc["logdir"] "/server.log"
else
return irc["logdir"] "/channels/" chan ".log"
}
function delete_chan(chan)
{
delete channels[chan]
return close(chan_to_filename(chan))
}
function _chan_add(str, chan)
{
# Channel has no users. We can just set the channel to "str"
# instead of appending it.
if (channels[chan] == "")
channels[chan] = str
# Channel does not contain "str", append it.
else if (!index(channels[chan], str))
channels[chan] = channels[chan] str
# Channel already contains "str".
else
return 0
return 1
}
function _chan_del(str, chan, pos)
{
# Only user in the channel is "str". Set the channel to an empty
# string.
if (channels[chan] == str) {
channels[chan] = ""
return 1
# "str" is not in the channel.
} else if (!(pos = index(channels[chan], str))) {
return 0
}
# Do the string stitching by extracting everything before and
# after "str".
channels[chan] = substr(channels[chan], 1, pos-1) \
substr(channels[chan], pos+length(str))
return 1
}
function add_nick_to_chan(nick, chan, text)
{
nick = irc_tolower(nick)
chan = irc_tolower(chan)
if (text)
print_out(text, chan)
# If it's us who is to be added to a channel, it means us joining
# triggered this function. Instead of adding our nick now, we let
# the next names reply (353) populate the list of nicknames.
if (!channels[chan])
return 1
else
return _chan_add(" " nick " ", chan)
}
function delete_nick_from_chan(nick, chan, text)
{
nick = irc_tolower(nick)
chan = irc_tolower(chan)
if (text)
print_out(text, chan)
# If it's us who is to be deleted, delete the entire channel
# instead.
if (!is_chan(chan) || nick == irc["nick_lc"]) {
print_out("Closed channel: " chan)
print_out("Channel closed", chan)
delete_chan(chan)
return 1
} else {
return _chan_del(" " nick " ", chan)
}
}
function delete_nick_from_all_chans(nick, text, chan, str)
{
nick = irc_tolower(nick)
str = " " nick " "
for (chan in channels) {
if (!index(channels[chan], str))
continue
if (text)
print_out(text, chan)
# If we are leaving a channel or removing a nickname from
# private message channels, delete the entire channel
# instead.
if (!is_chan(chan) || nick == irc["nick_lc"]) {
print_out("Closed channel: " chan)
print_out("Channel closed", chan)
delete_chan(chan)
continue
}
_chan_del(str, chan)
}
return 1
}
function rename_nick_in_all_chans(nick, new, text, chan, me)
{
me = is_me(new)
nick = " " irc_tolower(nick) " "
new = " " irc_tolower(new) " "
chan = irc_tolower(chan)
for (chan in channels) {
if (!index(channels[chan], nick))
continue
if (text)
print_out(text, chan)
# Close private message channels when others rename.
if (!is_chan(chan) && !me) {
print_out("Closed channel: " chan)
print_out("Channel closed", chan)
delete_chan(chan)
continue
}
_chan_del(nick, chan)
_chan_add(new, chan)
}
return 1
}
function get_nickuserhost()
{
# Return "nick [user@host.org]".
if (nsource > 1)
return source[1] " [" source[2] "@" source[3] "]"
else
return source[1]
}
function shell_escape(str)
{
# foo'bar -> 'foo'\''bar'.
gsub(/'/, "'\\''", str)
return "'" str "'"
}
function ext_command(name, chan, instigator, text,
casemapping, env, cmd)
{
if (!(name in command))
return
casemapping = casemap["uppercase_range"] " " \
casemap["lowercase_range"]
# Construct the environment for the external command.
env = sprintf("%s=%s %s=%s %s=%s %s=%s %s=%s %s=%s %s=%s",
"IRC_ME", shell_escape(irc["nick"]),
"IRC_NETWORK", shell_escape(irc["network"]),
"IRC_TEXT", shell_escape(text),
"IRC_WHERE", shell_escape(chan),
"IRC_WHO", shell_escape(instigator),
"IRC_CASEMAPPING", shell_escape(casemapping),
"IRC_AWAY", irc["away"] ? "1" : "")
# Assemble the final command name by adding the prefix and looking
# up the command.
name = command["prefix"] command[name]
# Exec the command using the additional environment variables.
cmd = "command -v %s >/dev/null 2>&1 && %s exec 1>/dev/null %s &"
system(sprintf(cmd, name, env, name))
}
## User commands #########################################################
function cmd_list( chan, str)
{
if (NF) {
print_out(cmd ": too many arguments")
return
} else if (!length(channels)) {
print_out("No open channels")
return
}
for (chan in channels)
str = str " " chan
print_out("Open channels:" str)
}
function cmd_msg(realcmd, new, suffix, target, text)
{
if (!NF) {
print_out(cmd ": not enough arguments")
return
} else if (index($1, ",")) {
print_out(cmd ": multiple targets is unsupported")
return
}
# Casefold and remove slashes/control characters from the target.
# We don't want them to end up in the logfile path.
target = irc_tolower($1)
gsub("[/[:cntrl:]]", "", target)
# Remove the first (target) argument from the record.
sub("^ *[^ ]+ ?", "")
# Allow whitespace as only argument. Also sets NF to 1.
if (!NF && $0 != "")
$1 = $0
# Message to channel.
if (is_chan(target)) {
if (!NF) {
print_out(cmd ": not enough arguments")
return
} else if (!(target in channels)) {
print_out(cmd ": cannot send to channel " \
target ": not joined")
return
}
# Message to a user with existing channel.
} else if (target in channels) {
if (!NF) {
print_out(cmd ": channel " target \
" already joined")
return
}
# Message to a user.
} else if (NF && support["IRCv3 echo-message"]) {
# With echo-message enabled, the channel will be created
# when we receive our own message from the server, instead
# of doing it manually.
} else {
# Create the channel by setting its members to us and the
# target.
channels[target] = " " target " " irc["nick_lc"] " "
print_out("New channel: " target)
print_out("Channel opened", target)
new = 1
}
# We only send the command and print it out when we actually have
# a message. This is necessary because we allow omitting the
# message when the target is a user, which is used to create
# private message channels without actually sending anything.
if (NF) {
if (realcmd == "ACTION") {
text = "\x01ACTION " $0 "\x01"
realcmd = "PRIVMSG"
suffix = "*a"
} else if (realcmd == "NOTICE") {
text = $0
suffix = "*n"
} else {
text = $0
suffix = "*"
}
send(realcmd " " target " :" text)
# No need to simulate our own messages when the
# echo-message feature is supported. We will receive it
# from the server.
if (support["IRCv3 echo-message"])
return
# Simulate our own message.
print_out($0, target, irc["nick"], suffix)
if (new)
ext_command("join", target)
} else if (new)
ext_command("join", target)
}
function cmd_generic(realcmd, arg_min, arg_max, trailing_arg,
colon, field, pos, word)
{
if (arg_min == "") arg_min = 0
if (arg_max == "") arg_max = 0
if (arg_max && arg_min > arg_max) return
# Allow only whitespace as argument; Also sets NF to 1.
if (!NF && $0 != "")
$1 = $0
if (NF < arg_min) {
print_out(cmd ": not enough arguments")
return
} else if (arg_max && NF > arg_max) {
print_out(cmd ": too many arguments")
return
}
# To mark field n and everything after as one parameter, a colon
# has to be added before it.
if (trailing_arg == 1) {
$0 = ":" $0
# To prevent the truncation of whitespace, which happens whenever
# one of the field variables is assigned a new value, we can't
# simply prepend a colon to field $n and then use $0. We have to
# cut $0 in half at the desired colon position. The following
# code finds that position and does the splicing.
} else if (trailing_arg && trailing_arg <= NF) {
field = 1
word = 0 # Wether or not we are in a word or inbetween.
# Find the first character of "trailing_arg".
for (pos = 1; pos <= length($0); pos++) {
if (substr($0, pos, 1) == " ") {
# If we just were in a word and are now on
# the first space after it, one more
# field is done.
if (word)
field++
word = 0
} else {
word = 1
}
if (field == trailing_arg) {
pos++
break
}
}
$0 = substr($0, 1, pos-1) colon substr($0, pos)
}
send(realcmd " " $0)
}
function cmd_part( chan)
{
if (!NF) {
print_out(cmd ": not enough arguments")
return
}
chan = irc_tolower($1)
# Parting a private message channel just closes it, while parting
# a normal channel actually sends the PART command.
if (!is_chan(chan)) {
# Only actual channels have a trailing reason parameter.
if (NF > 1) {
print_out(cmd ": too many arguments")
return
} else if (!(chan in channels)) {
print_out(cmd ": channel " $1 " does not exist")
return
}
print_out("Closed channel: " chan)
print_out("Channel closed", chan)
delete_chan(chan)
ext_command("part", chan, irc["nick"])
} else {
cmd_generic("PART", 1, 0, 2)
}
}
function cmd_quit()
{
print_out("Quitting...")
if ($0 == "")
send("QUIT")
else
send("QUIT :" $0)
}
function cmd_raw()
{
if (!NF)
print_out(cmd ": not enough arguments")
else
send($0)
}
## irc commands ##########################################################
function on_generic( i, text)
{
#:src 330 to from data :[is/was] logged in as
#:src 378 to from :is connecting from ...
#:src 671 to from :is using a secure connection
if (cmd == "330") {
param[2] = param[2] " " param[4] " " param[3]
nparam = nparam-2
} else if (cmd == "378" || cmd == "671") {
param[2] = param[2] " " param[3]
nparam--
}
for (i = is_me(param[1]) ? 2 : 1; i <= nparam; i++)
text = text " " param[i]
print_out(substr(text, 2))
}
function on_msg( char, ctcp, highlight, after, before,
new, pos, suffix, target, text, user)
{
# Privmsgs/notices from the server go into server.log.
if (!irc["connected"] || !nsource || source[1] == irc["host"]) {
source[1] = ""
target = ""
# Privmsgs to me go into the senders "channel".
} else if (is_me(param[1])) {
target = irc_tolower(source[1])
# Privmsgs to a channel.
} else {
target = irc_tolower(param[1])
# Extract leading status message characters from target.
if (support["statusmsg"]) {
# Find the first char not in "statusmsg".
for (pos = 1; pos <= length(target); pos++) {
char = substr(target, pos, 1)
if (!index(support["statusmsg"], char))
break
}
if (pos > 1) {
suffix = "s:" substr(target, 1, pos-1) ":"
target = substr(target, pos)
}
}
# The target channel not existing means the message target
# parameter must be a wildcard. This is the case for
# server-wide notifications for instance. Instead of
# using that wildcard as target channel, use source.
if (!is_me(source[1]) && !(target in channels))
target = irc_tolower(source[1])
}
user = get_nickuserhost()
text = param[nparam]
# CTCP/action.
if (text ~ /^\x01.*\x01$/) {
if (text ~ /^\x01ACTION /) {
suffix = suffix "a"
sub(/\x01ACTION /, "", text)
} else {
suffix = suffix "c"
ctcp = 1
}
gsub(/\x01/, "", text)
}
if (cmd == "NOTICE")
suffix = suffix "n"
# Create new private message channel if necessary.
if (target && !(target in channels)) {
channels[target] = " " target " " irc["nick_lc"] " "
print_out("New channel: " target, "", "", "!")
print_out("Channel opened", target)
new = 1
}
if (is_me(source[1])) {
suffix = suffix "*"
# Check if our nick appears in the text of another users message.
} else if (pos = index(irc_tolower(text), irc["nick_lc"])) {
# Sort of match only full words, but allow a leading @
# or whitespace as well as trailing punctuation and
# whitespace.
before = substr(" " text, pos, 1)
after = substr(text " ", pos + length(irc["nick"]), 1)
if ( \
before ~ /[@[:space:]]/ && \
after ~ /[:.,!?[:space:]]/ \
) {
suffix = suffix "!"
highlight = 1
print_out("Highlighted by " user " in " \
(is_chan(target) \
? target : "private message"),
"", "", "!")
}
}
print_out(text, target, source[1], suffix)
# Call external command. Newly added private message channels
# have precedence over highlights.
if (new) {
ext_command("join", target)
ext_command("add", target, user, text)
} else if (highlight)
ext_command("highlight", target, user, text)
if (ctcp)
ext_command("ctcp", target, user, text)
}
function on_cap( caps, i)
{
if (toupper(param[nparam - 1]) == "ACK") {
print_out("Enabled IRCv3 capabilities: " param[nparam])
split(param[nparam], caps, " ")
for (i in caps)
support["IRCv3 " caps[i]] = 1
}
}
function on_mode( i, user, target, text)
{
user = get_nickuserhost()
if (is_me(source[1]) && is_me(param[1])) {
target = ""
text = "You have set"
} else {
target = irc_tolower(param[1])
text = user " has set"
}
# Append all params.
for (i = 1; i <= nparam; i++)
text = text " " param[i]
print_out(text, target)
}
function on_join( nick, chan, user)
{
nick = source[1]; user = get_nickuserhost(); chan = param[1]
if (is_me(nick))
print_out("Channel opened", chan)
add_nick_to_chan(nick, chan, user " has joined " chan)
if (is_me(nick)) {
print_out("New channel: " chan)
ext_command("join", chan)
}
}
function on_part( nick, chan, user, text, reason)
{
nick = source[1]; user = get_nickuserhost(); chan = param[1]
text = user " has left " chan
# Part reason.
if (nparam > 1 && length(param[nparam])) {
reason = param[nparam]
text = text " (" reason ")"
}
delete_nick_from_chan(nick, chan, text)
if (is_me(nick))
ext_command("part", chan, user, reason)
}
function on_nick( nick, new, user)
{
nick = source[1]; user = get_nickuserhost(); new = param[nparam]
if (is_me(nick)) {
print_out("You are now known as " new)
set_nick(new)
}
rename_nick_in_all_chans(nick, new, \
user " has changed nick to " new)
}
function on_kick( chan, user, kicked, text, reason)
{
user = get_nickuserhost(); chan = param[1]; kicked = param[2]
text = user " has kicked " kicked
if (nparam > 2 && length(param[nparam])) {
reason = param[nparam]
text = text " (" reason ")"
}
delete_nick_from_chan(kicked, chan, text)
if (is_me(kicked))
ext_command("kick", chan, user, reason)
}
function on_invite( chan, user, invitee, text)
{
user = get_nickuserhost(); invitee = param[1]
chan = param[nparam]
text = user " has invited you to join " chan
print_out(text, "", "", "!")
ext_command("invite", chan, user)