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