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