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