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