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