]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/IrcHooks.pl
Initial revision
[infobot.git] / src / IRC / IrcHooks.pl
1 #
2 # IrcHooks.pl: IRC Hooks stuff.
3 #      Author: xk <xk@leguin.openprojects.net>
4 #     Version: 20000126
5 #        NOTE: Based on code by Kevin Lenzo & Patrick Cole  (c) 1997
6 #
7
8 if (&IsParam("useStrict")) { use strict; }
9
10 my $nickserv    = 0;
11
12 # GENERIC. TO COPY.
13 sub on_generic {
14     my ($self, $event) = @_;
15     my $nick = $event->nick();
16     my $chan = ($event->to)[0];
17
18     &DEBUG("on_generic: nick => '$nick'.");
19     &DEBUG("on_generic: chan => '$chan'.");
20
21     foreach ($event->args) {
22         &DEBUG("on_generic: args => '$_'.");
23     }
24 }
25
26 sub on_action {
27     my ($self, $event) = @_;
28     my ($nick, @args) = ($event->nick, $event->args);
29     my $chan = ($event->to)[0];
30
31     shift @args;
32
33     if ($chan eq $ident) {
34         &status("* [$nick] @args");
35     } else {
36         &status("* $nick/$chan @args");
37     }
38 }
39
40 sub on_chat {
41     my ($self, $event) = @_;
42     my $msg  = ($event->args)[0];
43     my $sock = ($event->to)[0];
44     my $nick = $event->nick();
45
46     if (!exists $nuh{lc $nick}) {
47         &DEBUG("chat: nuh{$nick} doesn't exist; hrm should retry.");
48         return;
49     } else {
50         $message        = $msg;
51         $who            = lc $nick;
52         $orig{who}      = $nick;
53         $orig{message}  = $msg;
54         $nuh            = $nuh{$who};
55         $uh             = (split /\!/, $nuh)[1];
56         $addressed      = 1;
57         $msgType        = 'chat';
58     }
59
60     if (!exists $dcc{'CHAT'}{$nick}) {
61         my $userHandle  = &verifyUser($who, $nuh);
62         my $crypto      = $userList{$userHandle}{'pass'};
63         my $success     = 0;
64
65         if (!defined $crypto) {
66             &DEBUG("chat: no pass required.");
67             $success++;
68         } elsif (&ckpasswd($msg, $crypto)) {
69             $self->privmsg($sock,"Authorized.");
70             $self->privmsg($sock,"I'll respond as if through /msg and addressed in public. Addition to that, access to 'user' commands will be allowed, like 'die' and 'jump'.");
71             # hrm... it's stupid to ask for factoids _within_ dcc chat.
72             # perhaps it should be default to commands, especially
73             # commands only authorized through DCC CHAT.
74             &status("DCC CHAT: passwd is ok.");
75             $success++;
76         } else {
77             &status("DCC CHAT: incorrect pass; closing connection.");
78             &DEBUG("chat: sock => '$sock'.");
79 ###         $sock->close();
80             &DEBUG("chat: after closing sock. FIXME");
81             ### BUG: close seizes bot. why?
82         }
83
84         if ($success) {
85             &status("DCC CHAT: user $nick is here!");
86             $dcc{'CHAT'}{$nick} = $sock;
87             &DCCBroadcast("$nick ($uh) has joined the chat arena.");
88         }
89
90         return;
91     }
92
93
94     $userHandle = &verifyUser($who, $nuh);
95     &status("$b_red=$b_cyan$who$b_red=$ob $message");
96     if ($message =~ s/^\.//) {  # dcc chat commands.
97         ### TODO: make use of &Forker(); here?
98         &loadMyModule($myModules{'ircdcc'});
99         return 'NOREPLY from userD' if (&userDCC() eq 'NOREPLY');
100         $conn->privmsg($dcc{'CHAT'}{$who}, "Invalid command.");
101
102     } else {                    # dcc chat arena.
103         foreach (keys %{$dcc{'CHAT'}}) {
104             $conn->privmsg($dcc{'CHAT'}{$_}, "<$who> $orig{message}");
105         }
106     }
107
108     return 'DCC CHAT MESSAGE';
109 }
110
111 sub on_endofmotd {
112     my ($self) = @_;
113
114     if (&IsParam("wingate")) {
115         my $file = "$infobot_base_dir/$param{'ircUser'}.wingate";
116         open(IN, $file);
117         while (<IN>) {
118             chop;
119             next unless (/^(\S+)\*$/);
120             push(@wingateBad, $_);
121         }
122         close IN;
123     }
124
125     ### TODO: move this to end of &joinNextChan()?
126     if ($firsttime) {
127         &DEBUG("on_EOM: calling sS in 60s.");
128         $conn->schedule(60, \&setupSchedulers, "");
129         $firsttime = 0;
130     }
131
132     if (&IsParam("ircUMode")) {
133         &status("Changing user modes to $param{'ircUMode'}.");
134         &rawout("MODE $ident $param{'ircUMode'}");
135     }
136
137     &status("End of motd. Now lets join some channels...");
138     if (!scalar @joinchan) {
139         &WARN("joinchan array is empty!!!");
140         @joinchan = split /[\t\s]+/, $param{'join_channels'};
141     }
142
143     &joinNextChan();
144 }
145
146 sub on_dcc {
147     my ($self, $event) = @_;
148     my $type = uc( ($event->args)[1] );
149     my $nick = $event->nick();
150
151     # pity Net::IRC doesn't store nuh. Here's a hack :)
152     $self->whois($nick);
153
154     if ($type eq 'SEND') {
155         # incoming DCC SEND. we're receiving a file.
156         $self->new_get($event, \*FH);
157     } elsif ($type eq 'CHAT') {
158         $self->new_chat($event);
159     } else {
160         &status("${b_green}DCC $type$ob unknown ...");
161     }
162 }
163
164 sub on_dcc_close {
165     my ($self, $event) = @_;
166     my $nick = $event->nick();
167     my $sock = ($event->to)[0];
168
169     &DEBUG("dcc_close: nick => '$nick'.");
170
171     if (exists $dcc{'SEND'}{$nick} and -f "temp/$nick.txt") {
172         &status("${b_green}DCC SEND$ob close from $b_cyan$nick$ob");
173
174         &status("dcc_close: purging $nick.txt from Debian.pl");
175         unlink "temp/$nick.txt";
176
177         delete $dcc{'SEND'}{$nick};
178     } elsif (exists $dcc{'CHAT'}{$nick} and $dcc{'CHAT'}{$nick} eq $sock) {
179         &status("${b_green}DCC CHAT$ob close from $b_cyan$nick$ob");
180         delete $dcc{'CHAT'}{$nick};
181     } else {
182         &status("${b_green}DCC$ob UNKNOWN close from $b_cyan$nick$ob");
183     }
184 }
185
186 sub on_dcc_open {
187     my ($self, $event) = @_;
188     my $type = uc( ($event->args)[0] );
189     my $nick = $event->nick();
190     my $sock = ($event->to)[0];
191     $msgType = 'chat';
192
193     if ($type eq 'SEND') {
194         &status("${b_green}DCC lGET$ob established with $b_cyan$nick$ob");
195     } elsif ($type eq 'CHAT') {
196         &status("${b_green}DCC CHAT$ob established with $b_cyan$nick$ob ($nuh{$nick})");
197         my $userHandle  = &verifyUser($nick, $nuh{lc $nick});
198         my $crypto      = $userList{$userHandle}{'pass'};
199         if (defined $crypto) {
200             $self->privmsg($sock,"Enter Password, $userHandle.");
201         } else {
202             $self->privmsg($sock,"Welcome to blootbot DCC CHAT interface, $userHandle.");
203         }
204     } else {
205         &status("${b_green}DCC $type$ob unknown ...");
206     }
207 }
208
209 sub on_disconnect {
210     my ($self, $event) = @_;
211     my $from = $event->from();
212     my $what = ($event->args)[0];
213
214     &status("disconnect from $from ($what).");
215     $ircstats{'DisconnectReason'} = $what;
216
217     # clear any variables on reconnection.
218     $nickserv = 0;
219
220     &clearIRCVars();
221
222     if (!$self->connect()) {
223         &WARN("not connected? help me. ircCheck() should reconnect me");
224     }
225 }
226
227 sub on_endofnames {
228     my ($self, $event) = @_;
229     my $chan = ($event->args)[1];
230
231     if (exists $jointime{$chan}) {
232         my $delta_time = sprintf("%.03f", &gettimeofday() - $jointime{$chan});
233         $delta_time    = 0      if ($delta_time < 0);
234
235         &status("$b_blue$chan$ob: sync in ${delta_time}s.");
236     }
237
238     rawout("MODE $chan");
239
240     my $txt;
241     my @array;
242     foreach ("o","v","") {
243         my $count = scalar(keys %{$channels{$chan}{$_}});
244         next unless ($count);
245
246         $txt = "total" if ($_ eq "");
247         $txt = "voice" if ($_ eq "v");
248         $txt = "ops"   if ($_ eq "o");
249
250         push(@array, "$count $txt");
251     }
252     my $chanstats = join(' || ', @array);
253     &status("$b_blue$chan$ob: [$chanstats]");
254
255     if (scalar @joinchan) {     # remaining channels to join.
256         &joinNextChan();
257     } else {
258         ### chanserv support.
259         ### TODO: what if we rejoin a channel.. need to set a var that
260         ###       we've done the request-for-ops-on-join.
261         return unless (&IsParam("chanServ_ops"));
262         return unless ($nickserv);
263
264         my @chans = split(/[\s\t]+/, $param{'chanServ_ops'});
265
266         foreach $chan (keys %channels) {
267             next unless (grep /^$chan$/i, @chans);
268
269             if (!exists $channels{$chan}{'o'}{$ident}) {
270                 &status("ChanServ ==> Requesting ops for $chan.");
271                 rawout("PRIVMSG ChanServ :OP $chan $ident");
272             }
273         }
274     }
275
276 }
277
278 sub on_init {
279     my ($self, $event) = @_;
280     my (@args) = ($event->args);
281     shift @args;
282
283     &status("@args");
284 }
285
286 sub on_invite {
287     my ($self, $event) = @_;
288     my $chan = ($event->args)[0];
289     my $nick = $event->nick;
290
291     &DEBUG("on_invite: chan => '$chan', nick => '$nick'.");
292
293     # chan + possible_key.
294     ### do we need to know the key if we're invited???
295     ### grep the channel list?
296     foreach (split /[\s\t]+/, $param{'join_channels'}) {
297         next unless /^\Q$chan\E(,\S+)?$/i;
298         s/,/ /;
299
300         next if ($nick =~ /^\Q$ident\E$/);
301         if (&validChan($chan)) {
302             &msg($who, "i'm already in \002$chan\002.");
303             next;
304         }
305
306         &status("invited to $b_blue$_$ob by $b_cyan$who$ob");
307         &joinchan($self, $_);
308     }
309 }
310
311 sub on_join {
312     my ($self, $event) = @_;
313     my ($user,$host) = split(/\@/, $event->userhost);
314     $chan       = lc( ($event->to)[0] );        # CASING!!!!
315     $who        = $event->nick();
316
317     $chanstats{$chan}{'Join'}++;
318     $userstats{lc $who}{'Join'} = time() if (&IsParam("seenStats"));
319
320     # netjoin detection.
321     my $netsplit = 0;
322     if (exists $netsplit{lc $who}) {
323         delete $netsplit{lc $who};
324         $netsplit = 1;
325     }
326
327     # how to tell if there's a netjoin???
328
329     my $netsplitstr = "";
330     $netsplitstr = " $b_yellow\[${ob}NETSPLIT VICTIM$b_yellow]$ob" if ($netsplit);
331     &status(">>> join/$b_blue$chan$ob $b_cyan$who$ob $b_yellow($ob$user\@$host$b_yellow)$ob$netsplitstr");
332
333     $channels{$chan}{''}{$who}++;
334     $nuh{lc $who} = $who."!".$user."\@".$host unless (exists $nuh{lc $who});
335
336     ### ROOTWARN:
337     &rootWarn($who,$user,$host,$chan)
338                 if (&IsParam("rootWarn") &&
339                     $user =~ /^r(oo|ew|00)t$/i &&
340                     $channels{$chan}{'o'}{$ident});
341
342     # used to determine sync time.
343     if ($who =~ /^$ident$/i) {
344         if (defined( my $whojoin = $joinverb{$chan} )) {
345             &msg($chan, "Okay, I'm here. (courtesy of $whojoin)");
346             delete $joinverb{$chan};
347         }
348
349         ### TODO: move this to &joinchan()?
350         $jointime{$chan} = &gettimeofday();
351         rawout("WHO $chan");
352     } else {
353         ### TODO: this may go wild on a netjoin :)
354         ### WINGATE:
355         &wingateCheck();
356     }
357 }
358
359 sub on_kick {
360     my ($self, $event) = @_;
361     my ($chan,$reason) = $event->args;
362     my $kicker  = $event->nick;
363     my $kickee  = ($event->to)[0];
364     my $uh      = $event->userhost();
365
366     &status(">>> kick/$b_blue$chan$ob [$b$kickee!$uh$ob] by $b_cyan$kicker$ob $b_yellow($ob$reason$b_yellow)$ob");
367
368     $chanstats{$chan}{'Kick'}++;
369
370     if ($kickee eq $ident) {
371         &clearChanVars($chan);
372
373         &status("SELF attempting to rejoin lost channel $chan");
374         &joinchan($chan);
375     } else {
376         &DeleteUserInfo($kickee,$chan);
377     }
378 }
379
380 sub on_mode {
381     my ($self, $event)  = @_;
382     my ($user, $host)   = split(/\@/, $event->userhost);
383     my @args = $event->args();
384     my $nick = $event->nick();
385     my $chan = ($event->to)[0];
386
387     $args[0] =~ s/\s$//;
388
389     if ($nick eq $chan) {       # UMODE
390         &status(">>> mode $b_yellow\[$ob$b@args$b_yellow\]$ob by $b_cyan$nick$ob");
391     } else {                    # MODE
392         &status(">>> mode/$b_blue$chan$ob $b_yellow\[$ob$b@args$b_yellow\]$ob by $b_cyan$nick$ob");
393         &hookMode($chan, @args);
394     }
395 }
396
397 sub on_modeis {
398     my ($self, $event) = @_;
399     my $nick = $event->nick();
400     my ($myself,$chan,@args) = $event->args();
401
402     &hookMode(lc $chan, @args);         # CASING.
403 }
404
405 sub on_msg {
406     my ($self, $event) = @_;
407     my $nick = $event->nick;
408     my $chan = lc ( ($event->to)[0] );  # CASING.
409     my $msg = ($event->args)[0];
410
411     ($user,$host) = split(/\@/, $event->userhost);
412     $uh         = $event->userhost();
413     $nuh        = $nick."!".$uh;
414
415     &hookMsg('private', $chan, $nick, $msg);
416 }
417
418 sub on_names {
419     my ($self, $event) = @_;
420     my @args = $event->args;
421     my $chan = lc $args[2];             # CASING, the last of them!
422
423     foreach (split / /, @args[3..$#args]) {
424         $channels{$chan}{'o'}{$_}++     if s/\@//;
425         $channels{$chan}{'v'}{$_}++     if s/\+//;
426         $channels{$chan}{''}{$_}++;
427     }
428 }
429
430 sub on_nick {
431     my ($self, $event) = @_;
432     my $nick = $event->nick();
433     my $newnick = ($event->args)[0];
434
435     my ($chan,$mode);
436     foreach $chan (keys %channels) {
437         foreach $mode (keys %{$channels{$chan}}) {
438             next unless (exists $channels{$chan}{$mode}{$nick});
439
440             $channels{$chan}{$mode}{$newnick} = $channels{$chan}{$mode}{$nick};
441         }
442     }
443     &DeleteUserInfo($nick,keys %channels);
444     $nuh{lc $newnick} = $nuh{lc $nick};
445     delete $nuh{lc $nick};
446
447     # successful self-nick change.
448     if ($nick eq $ident) {
449         &status(">>> I materialized into $b_green$newnick$ob from $nick");
450         $ident = $newnick;
451     } else {
452         &status(">>> $b_cyan$nick$ob materializes into $b_green$newnick$ob");
453     }
454 }
455
456 sub on_nick_taken {
457     my ($self) = @_;
458     my $nick = $self->nick;
459     my $newnick = substr($nick,0,8).int(rand(10));
460
461     &DEBUG("on_nick_taken: changing nick to $newnick.");
462     $self->nick($newnick);
463     $ident      = $newnick;
464 }
465
466 sub on_notice {
467     my ($self, $event) = @_;
468     my $nick = $event->nick();
469     my $chan = ($event->to)[0];
470     my $args = ($event->args)[0];
471
472     if ($nick =~ /^NickServ$/i) {               # nickserv.
473         &status("NickServ: <== '$args'");
474
475         if ($args =~ /^This nickname is registered/i) {
476             &status("nickserv told us to register; doing it.");
477             if (&IsParam("nickServ_pass")) {
478                 &status("NickServ: ==> Identifying.");
479                 &rawout("PRIVMSG NickServ :IDENTIFY $param{'nickServ_pass'}");
480                 return;
481             } else {
482                 &status("We can't tell nickserv a passwd ;(");
483             }
484         }
485
486         # password accepted.
487         if ($args =~ /^Password a/i) {
488             $nickserv++;
489         }
490     } elsif ($nick =~ /^ChanServ$/i) {          # chanserv.
491         &status("ChanServ: <== '$args'.");
492     } else {
493         if ($chan =~ /^$mask{chan}$/) { # channel notice.
494             &status("-$nick/$chan- $args");
495         } else {
496             $server = $nick unless (defined $server);
497             &status("-$nick- $args");   # private or server notice.
498         }
499     }
500 }
501
502 sub on_other {
503     my ($self, $event) = @_;
504     my $chan = ($event->to)[0];
505     my $nick = $event->nick;
506
507     &status("!!! other called.");
508     &status("!!! $event->args");
509 }
510
511 sub on_part {
512     my ($self, $event) = @_;
513     my $chan = lc( ($event->to)[0] );   # CASING!!!
514     my $nick = $event->nick;
515     my $userhost = $event->userhost;
516
517     $chanstats{$chan}{'Part'}++;
518     &DeleteUserInfo($nick,$chan);
519     &clearChanVars($chan) if ($nick eq $ident);
520     if (!&IsNickInAnyChan($nick) and &IsParam("seenStats")) {
521         delete $userstats{lc $nick};
522     }
523
524     &status(">>> part/$b_blue$chan$ob $b_cyan$nick$ob $b_yellow($ob$userhost$b_yellow)$ob");
525 }
526
527 sub on_ping {
528     my ($self, $event) = @_;
529     my $nick = $event->nick;
530
531     $self->ctcp_reply($nick, join(' ', ($event->args)));
532     &status(">>> ${b_green}CTCP PING$ob request from $b_cyan$nick$ob received.");
533 }
534
535 sub on_ping_reply {
536     my ($self, $event) = @_;
537     my $nick = $event->nick;
538     my $lag = time() - ($event->args)[1];
539
540     &status(">>> ${b_green}CTCP PING$ob reply from $b_cyan$nick$ob: $lag sec.");
541 }
542
543 sub on_public {
544     my ($self, $event) = @_;
545     my $msg  = ($event->args)[0];
546     my $chan = lc( ($event->to)[0] );   # CASING.
547     my $nick = $event->nick;
548     $uh      = $event->userhost();
549     $nuh     = $nick."!".$uh;
550     ($user,$host) = split(/\@/, $uh);
551
552     ### DEBUGGING.
553     if ($statcount < 200) {
554         foreach $chan (grep /[A-Z]/, keys %channels) {
555             &DEBUG("leak: chan => '$chan'.");
556             my ($i,$j);
557             foreach $i (keys %{$channels{$chan}}) {  
558                 foreach (keys %{$channels{$chan}{$i}}) {
559                     &DEBUG("leak:   \$channels{$chan}{$i}{$_} ...");
560                 }
561             }
562         }
563     }
564
565
566     $msgtime = time();
567     $lastWho{$chan} = $nick;
568     ### TODO: use $nick or lc $nick?
569     if (&IsParam("seenStats")) {
570         $userstats{lc $nick}{'Count'}++;
571         $userstats{lc $nick}{'Time'} = time();
572     }
573
574 #    if (&IsParam("hehCounter")) {
575 #       #...
576 #    }
577
578     &hookMsg('public', $chan, $nick, $msg);
579     $chanstats{$chan}{'PublicMsg'}++;
580 }
581
582 sub on_quit {
583     my ($self, $event) = @_;
584     my $nick = $event->nick();
585     my $reason = ($event->args)[0];
586
587     foreach (keys %channels) {
588         # fixes inconsistent chanstats bug #1.
589         next unless (&IsNickInChan($nick,$_));
590         $chanstats{$_}{'SignOff'}++;
591     }
592     &DeleteUserInfo($nick, keys %channels);
593     if (exists $nuh{lc $nick}) {
594         delete $nuh{lc $nick};
595     } else {
596         &DEBUG("on_quit: nuh{lc $nick} does not exist! FIXME");
597     }
598     delete $userstats{lc $nick} if (&IsParam("seenStats"));
599
600     # should fix chanstats inconsistencies bug #2.
601     if ($reason=~/^($mask{host})\s($mask{host})$/) {    # netsplit.
602         $reason = "NETSPLIT: $1 <=> $2";
603
604         $netsplit{lc $nick} = time();
605         if (!exists $netsplitservers{$1}{$2}) {
606             &status("netsplit detected between $1 and $2.");
607             $netsplitservers{$1}{$2} = time();
608         }
609     }
610
611     &status(">>> $b_cyan$nick$ob has signed off IRC $b_red($ob$reason$b_red)$ob");
612     if ($nick =~ /^\Q$ident\E$/) {
613         &DEBUG("!!! THIS SHOULD NEVER HAPPEN. FIXME HOPEFULLY");
614     }
615     if ($nick !~ /^\Q$ident\E$/ and $nick =~ /^\Q$param{'ircNick'}\E$/i) {
616         &status("own nickname became free; changing.");
617         &nick($param{'ircNick'});
618     }
619 }
620
621 sub on_targettoofast {
622     my ($self, $event) = @_;
623     my $nick = $event->nick();
624     my $chan = ($event->to)[0];
625
626     &DEBUG("on_targettoofast: nick => '$nick'.");
627     &DEBUG("on_targettoofast: chan => '$chan'.");
628
629     foreach ($event->args) {
630         &DEBUG("on_targettoofast: args => '$_'.");
631     }
632
633 ###    .* wait (\d+) second/) {
634         &status($msg);
635         my $sleep = $3 + 10;
636
637         &status("going to sleep for $sleep...");
638         sleep $sleep;
639         &joinNextChan();
640 ### }
641 }
642
643 sub on_topic {
644     my ($self, $event) = @_;
645
646     if (scalar($event->args) == 1) {    # change.
647         my $topic = ($event->args)[0];
648         my $chan  = ($event->to)[0];
649         my $nick  = $event->nick();
650
651         ###
652         # WARNING:
653         #       race condition here. To fix, change '1' to '0'.
654         #       This will keep track of topics set by bot only.
655         ###
656         # UPDATE:
657         #       this may be fixed at a later date with topic queueing.
658         ###
659
660         $topic{$chan}{'Current'} = $topic if (1 and &IsParam("topic") == 1);
661         $chanstats{$chan}{'Topic'}++;
662
663         &status(">>> topic/$b_blue$chan$ob by $b_cyan$nick$ob -> $topic");
664     } else {                                            # join.
665         my ($nick, $chan, $topic) = $event->args;
666         if (&IsParam("topic")) {
667             $topic{$chan}{'Current'}    = $topic;
668             &topicAddHistory($chan,$topic);
669         }
670
671         $topic = &fixString($topic, 1);
672         &status(">>> topic/$b_blue$chan$ob is $topic");
673     }
674 }
675
676 sub on_topicinfo {
677     my ($self, $event) = @_;
678     my ($myself,$chan,$setby,$time) = $event->args();
679
680     my $timestr;
681     if (time() - $time > 60*60*24) {
682         $timestr        = "at ". localtime $time;
683     } else {
684         $timestr        = &Time2String(time() - $time) ." ago";
685     }
686
687     &status(">>> set by $b_cyan$setby$ob $timestr");
688 }
689
690 sub on_version {
691     my ($self, $event) = @_;
692     my $nick = $event->nick;
693
694     &status(">>> ${b_green}CTCP VERSION$ob request from $b_cyan$nick$ob");
695     $self->ctcp_reply($nick, "VERSION $infobot_version");
696 }
697
698 sub on_who {
699     my ($self, $event) = @_;
700     my @args    = $event->args;
701
702     $nuh{lc $args[5]} = $args[5]."!".$args[2]."\@".$args[3];
703 }
704
705 sub on_whoisuser {
706     my ($self, $event) = @_;
707     my @args    = $event->args;
708
709     $nuh{lc $args[1]} = $args[1]."!".$args[2]."\@".$args[3];
710 }
711
712 #######################################################################
713 ####### IRC HOOK HELPERS   IRC HOOK HELPERS   IRC HOOK HELPERS ########
714 #######################################################################
715
716 #####
717 # Usage: &hookMode($chan, $modes, @targets);
718 sub hookMode {
719     my ($chan, $modes, @targets) = @_;
720     my $parity  = 0;
721
722     $chan = lc $chan;           # !!!.
723
724     my $mode;
725     foreach $mode (split(//, $modes)) {
726         # sign.
727         if ($mode =~ /[-+]/) {
728             $parity = 1         if ($mode eq "+");
729             $parity = 0         if ($mode eq "-");
730             next;
731         }
732
733         # mode with target.
734         if ($mode =~ /[bklov]/) {
735             my $target = shift @targets;
736
737             if ($parity) {
738                 $chanstats{$chan}{'Op'}++    if ($mode eq "o");
739                 $chanstats{$chan}{'Ban'}++   if ($mode eq "b");
740             } else {
741                 $chanstats{$chan}{'Deop'}++  if ($mode eq "o");
742                 $chanstats{$chan}{'Unban'}++ if ($mode eq "b");
743             }
744
745             # modes w/ target affecting nick => cache it.
746             if ($mode =~ /[ov]/) {
747                 $channels{$chan}{$mode}{$target}++      if  $parity;
748                 delete $channels{$chan}{$mode}{$target} if !$parity;
749             }
750
751             if ($mode =~ /[l]/) {
752                 $channels{$chan}{$mode} = $target       if  $parity;
753                 delete $channels{$chan}{$mode}          if !$parity;
754             }
755         }
756
757         # important channel modes, targetless.
758         if ($mode =~ /[mt]/) {
759             $channels{$chan}{$mode}++                   if  $parity;
760             delete $channels{$chan}{$mode}              if !$parity;
761         }
762     }
763 }
764
765 sub hookMsg {
766     ($msgType, $chan, $who, $message) = @_;
767     my $skipmessage     = 0;
768     $addressed          = 0;
769     $addressedother     = 0;
770     $orig{message}      = $message;
771     $orig{who}          = $who;
772     $addrchar           = 0;
773
774     $message    =~ s/[\cA-\c_]//ig;     # strip control characters
775     $message    =~ s/^\s+//;            # initial whitespaces.
776     $who        =~ tr/A-Z/a-z/;         # lowercase.
777
778     &showProc();
779
780     # addressing.
781     if ($msgType =~ /private/) {
782         # private messages.
783         $addressed = 1;
784     } else {
785         # public messages.
786         # addressing revamped by the xk.
787         ### below needs to be fixed...
788         if (&IsParam("addressCharacter")) {
789             if ($message =~ s/^$param{'addressCharacter'}//) {
790                 $addrchar  = 1;
791                 $addressed = 1;
792             }
793         }
794
795         if ($message =~ /^($mask{nick})([\;\:\>\, ]+) */) {
796             my $newmessage = $';
797             if ($1 =~ /^\Q$ident\E$/i) {
798                 $message   = $newmessage;
799                 $addressed = 1;
800             } else {
801                 # ignore messages addressed to other people or unaddressed.
802                 $skipmessage++ if ($2 ne "" and $2 !~ /^ /);
803             }
804         }
805     }
806
807     # Determine floodwho.
808     if ($msgType =~ /public/i) {                # public.
809         $floodwho = lc $chan;
810     } elsif ($msgType =~ /private/i) {  # private.
811         $floodwho = lc $who;
812     } else {                            # dcc?
813         &DEBUG("FIXME: floodwho = ???");
814     }
815
816     my ($count, $interval) = split(/:/, $param{'floodRepeat'} || "2:10");
817
818     # flood repeat protection.
819     if ($addressed) {
820         my $time = $flood{$floodwho}{$message};
821
822         if (defined $time and (time - $time < $interval)) {
823             ### public != personal who so the below is kind of pointless.
824             my @who;
825             foreach (keys %flood) {
826                 next if (/^\Q$floodwho\E$/ or /^\Q$chan\E$/);
827                 push(@who, grep /^\Q$message\E$/i, keys %{$flood{$_}});
828             }
829             if (scalar @who) {
830                 &msg($who, "you already said what ".join(@who)." have said.");
831             } else {
832                 &msg($who,"Someone already said that ". (time - $time) ." seconds ago" );
833             }
834
835             ### TODO: delete old floodwarn{} keys.
836             my $floodwarn = 0;
837             if (!exists $floodwarn{$floodwho}) {
838                 $floodwarn++;
839             } else {
840                 $floodwarn++ if (time() - $floodwarn{$floodwho} > $interval);
841             }
842
843             if ($floodwarn) {
844                 &status("FLOOD repetition detected from $floodwho.");
845                 $floodwarn{$floodwho} = time();
846             }
847
848             return;
849         }
850
851         if ($addrchar) {
852             &status("$b_cyan$who$ob is short-addressing me");
853         } else {
854             &status("$b_cyan$who$ob is addressing me");
855         }
856
857         $flood{$floodwho}{$message} = time();
858     }
859
860     ($count, $interval) = split(/:/, $param{'floodMessages'} || "5:30");
861     # flood overflow protection.
862     if ($addressed) {
863         foreach (keys %{$flood{$floodwho}}) {
864             next unless (time() - $flood{$floodwho}{$_} > $interval);
865             delete $flood{$floodwho}{$_};
866         }
867
868         my $i = scalar keys %{$flood{$floodwho}};
869         if ($i > $count) {
870             &msg($who,"overflow of messages ($i > $count)");
871             &status("FLOOD overflow detected from $floodwho; ignoring");
872
873             my $expire = $param{'ignoreAutoExpire'} || 5;
874             $ignoreList{"*!$uh"} = time() + ($expire * 60);
875             return;
876         }
877
878         $flood{$floodwho}{$message} = time();
879     }
880
881     # public.
882     if ($msgType =~ /public/i) {
883         $talkchannel = $chan;
884         &status("<$orig{who}/$chan> $orig{message}");
885     }
886
887     # private.
888     if ($msgType =~ /private/i) {
889         &status("[$orig{who}] $orig{message}");
890     }
891
892     return if ($skipmessage);
893     return unless (&IsParam("minVolunteerLength") or $addressed);
894
895     local $ignore = 0;
896     foreach (keys %ignoreList) {
897         my $ignoreRE = $_;
898         my @parts = split /\*/, "a${ignoreRE}a";
899         my $recast = join '\S*', map quotemeta($_), @parts;
900         $recast =~ s/^a(.*)a$/$1/;
901         if ($nuh =~ /^$recast$/) {
902             $ignore++;
903             last;
904         }
905     }
906
907     if (defined $nuh) {
908         $userHandle = &verifyUser($who, $nuh);
909     } else {
910         &DEBUG("hookMsg: 'nuh' not defined?");
911     }
912
913 ### For extra debugging purposes...
914     if ($_ = &process()) {
915 #       &DEBUG("IrcHooks: process returned '$_'.");
916     }
917
918     return;
919 }
920
921 1;