]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/IrcHooks.pl
fixed - typo for join()
[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     ### DEBUGGING.
583     if ($statcount < 200) {
584         foreach $chan (grep /[A-Z]/, keys %channels) {
585             &DEBUG("leak: chan => '$chan'.");
586             my ($i,$j);
587             foreach $i (keys %{$channels{$chan}}) {  
588                 foreach (keys %{$channels{$chan}{$i}}) {
589                     &DEBUG("leak:   \$channels{$chan}{$i}{$_} ...");
590                 }
591             }
592         }
593     }
594
595
596     $msgtime = time();
597     $lastWho{$chan} = $nick;
598     ### TODO: use $nick or lc $nick?
599     if (&IsParam("seenStats")) {
600         $userstats{lc $nick}{'Count'}++;
601         $userstats{lc $nick}{'Time'} = time();
602     }
603
604 #    if (&IsParam("hehCounter")) {
605 #       #...
606 #    }
607
608     &hookMsg('public', $chan, $nick, $msg);
609     $chanstats{$chan}{'PublicMsg'}++;
610 }
611
612 sub on_quit {
613     my ($self, $event) = @_;
614     my $nick = $event->nick();
615     my $reason = ($event->args)[0];
616
617     foreach (keys %channels) {
618         # fixes inconsistent chanstats bug #1.
619         next unless (&IsNickInChan($nick,$_));
620         $chanstats{$_}{'SignOff'}++;
621     }
622     &DeleteUserInfo($nick, keys %channels);
623     if (exists $nuh{lc $nick}) {
624         delete $nuh{lc $nick};
625     } else {
626         &DEBUG("on_quit: nuh{lc $nick} does not exist! FIXME");
627     }
628     delete $userstats{lc $nick} if (&IsParam("seenStats"));
629
630     # should fix chanstats inconsistencies bug #2.
631     if ($reason=~/^($mask{host})\s($mask{host})$/) {    # netsplit.
632         $reason = "NETSPLIT: $1 <=> $2";
633
634         $netsplit{lc $nick} = time();
635         if (!exists $netsplitservers{$1}{$2}) {
636             &status("netsplit detected between $1 and $2.");
637             $netsplitservers{$1}{$2} = time();
638         }
639     }
640
641     &status(">>> $b_cyan$nick$ob has signed off IRC $b_red($ob$reason$b_red)$ob");
642     if ($nick =~ /^\Q$ident\E$/) {
643         &DEBUG("!!! THIS SHOULD NEVER HAPPEN. FIXME HOPEFULLY");
644     }
645     if ($nick !~ /^\Q$ident\E$/ and $nick =~ /^\Q$param{'ircNick'}\E$/i) {
646         &status("own nickname became free; changing.");
647         &nick($param{'ircNick'});
648     }
649 }
650
651 sub on_targettoofast {
652     my ($self, $event) = @_;
653     my $nick = $event->nick();
654     my $chan = ($event->to)[0];
655
656     &DEBUG("on_targettoofast: nick => '$nick'.");
657     &DEBUG("on_targettoofast: chan => '$chan'.");
658
659     foreach ($event->args) {
660         &DEBUG("on_targettoofast: args => '$_'.");
661     }
662
663 ###    .* wait (\d+) second/) {
664         &status("X1 $msg");
665         my $sleep = $3 + 10;
666
667         &status("going to sleep for $sleep...");
668         sleep $sleep;
669         &joinNextChan();
670 ### }
671 }
672
673 sub on_topic {
674     my ($self, $event) = @_;
675
676     if (scalar($event->args) == 1) {    # change.
677         my $topic = ($event->args)[0];
678         my $chan  = ($event->to)[0];
679         my $nick  = $event->nick();
680
681         ###
682         # WARNING:
683         #       race condition here. To fix, change '1' to '0'.
684         #       This will keep track of topics set by bot only.
685         ###
686         # UPDATE:
687         #       this may be fixed at a later date with topic queueing.
688         ###
689
690         $topic{$chan}{'Current'} = $topic if (1 and &IsParam("topic") == 1);
691         $chanstats{$chan}{'Topic'}++;
692
693         &status(">>> topic/$b_blue$chan$ob by $b_cyan$nick$ob -> $topic");
694     } else {                                            # join.
695         my ($nick, $chan, $topic) = $event->args;
696         if (&IsParam("topic")) {
697             $topic{$chan}{'Current'}    = $topic;
698             &topicAddHistory($chan,$topic);
699         }
700
701         $topic = &fixString($topic, 1);
702         &status(">>> topic/$b_blue$chan$ob is $topic");
703     }
704 }
705
706 sub on_topicinfo {
707     my ($self, $event) = @_;
708     my ($myself,$chan,$setby,$time) = $event->args();
709
710     my $timestr;
711     if (time() - $time > 60*60*24) {
712         $timestr        = "at ". localtime $time;
713     } else {
714         $timestr        = &Time2String(time() - $time) ." ago";
715     }
716
717     &status(">>> set by $b_cyan$setby$ob $timestr");
718 }
719
720 sub on_version {
721     my ($self, $event) = @_;
722     my $nick = $event->nick;
723
724     &status(">>> ${b_green}CTCP VERSION$ob request from $b_cyan$nick$ob");
725     $self->ctcp_reply($nick, "VERSION $bot_version");
726 }
727
728 sub on_who {
729     my ($self, $event) = @_;
730     my @args    = $event->args;
731
732     $nuh{lc $args[5]} = $args[5]."!".$args[2]."\@".$args[3];
733 }
734
735 sub on_whoisuser {
736     my ($self, $event) = @_;
737     my @args    = $event->args;
738
739     $nuh{lc $args[1]} = $args[1]."!".$args[2]."\@".$args[3];
740 }
741
742 #######################################################################
743 ####### IRC HOOK HELPERS   IRC HOOK HELPERS   IRC HOOK HELPERS ########
744 #######################################################################
745
746 #####
747 # Usage: &hookMode($chan, $modes, @targets);
748 sub hookMode {
749     my ($chan, $modes, @targets) = @_;
750     my $parity  = 0;
751
752     $chan = lc $chan;           # !!!.
753
754     my $mode;
755     foreach $mode (split(//, $modes)) {
756         # sign.
757         if ($mode =~ /[-+]/) {
758             $parity = 1         if ($mode eq "+");
759             $parity = 0         if ($mode eq "-");
760             next;
761         }
762
763         # mode with target.
764         if ($mode =~ /[bklov]/) {
765             my $target = shift @targets;
766
767             if ($parity) {
768                 $chanstats{$chan}{'Op'}++    if ($mode eq "o");
769                 $chanstats{$chan}{'Ban'}++   if ($mode eq "b");
770             } else {
771                 $chanstats{$chan}{'Deop'}++  if ($mode eq "o");
772                 $chanstats{$chan}{'Unban'}++ if ($mode eq "b");
773             }
774
775             # modes w/ target affecting nick => cache it.
776             if ($mode =~ /[ov]/) {
777                 $channels{$chan}{$mode}{$target}++      if  $parity;
778                 delete $channels{$chan}{$mode}{$target} if !$parity;
779             }
780
781             if ($mode =~ /[l]/) {
782                 $channels{$chan}{$mode} = $target       if  $parity;
783                 delete $channels{$chan}{$mode}          if !$parity;
784             }
785         }
786
787         # important channel modes, targetless.
788         if ($mode =~ /[mt]/) {
789             $channels{$chan}{$mode}++                   if  $parity;
790             delete $channels{$chan}{$mode}              if !$parity;
791         }
792     }
793 }
794
795 sub hookMsg {
796     ($msgType, $chan, $who, $message) = @_;
797     my $skipmessage     = 0;
798     $addressed          = 0;
799     $addressedother     = 0;
800     $orig{message}      = $message;
801     $orig{who}          = $who;
802     $addrchar           = 0;
803
804     $message    =~ s/[\cA-\c_]//ig;     # strip control characters
805     $message    =~ s/^\s+//;            # initial whitespaces.
806     $who        =~ tr/A-Z/a-z/;         # lowercase.
807
808     &showProc();
809
810     # addressing.
811     if ($msgType =~ /private/) {
812         # private messages.
813         $addressed = 1;
814     } else {
815         # public messages.
816         # addressing revamped by the xk.
817         ### below needs to be fixed...
818         if (&IsParam("addressCharacter")) {
819             if ($message =~ s/^$param{'addressCharacter'}//) {
820                 $addrchar  = 1;
821                 $addressed = 1;
822             }
823         }
824
825         if ($message =~ /^($mask{nick})([\;\:\>\, ]+) */) {
826             my $newmessage = $';
827             if ($1 =~ /^\Q$ident\E$/i) {
828                 $message   = $newmessage;
829                 $addressed = 1;
830             } else {
831                 # ignore messages addressed to other people or unaddressed.
832                 $skipmessage++ if ($2 ne "" and $2 !~ /^ /);
833             }
834         }
835     }
836
837     # Determine floodwho.
838     if ($msgType =~ /public/i) {                # public.
839         $floodwho = lc $chan;
840     } elsif ($msgType =~ /private/i) {  # private.
841         $floodwho = lc $who;
842     } else {                            # dcc?
843         &DEBUG("FIXME: floodwho = ???");
844     }
845
846     my ($count, $interval) = split(/:/, $param{'floodRepeat'} || "2:10");
847
848     # flood repeat protection.
849     if ($addressed) {
850         my $time = $flood{$floodwho}{$message};
851
852         if (defined $time and (time - $time < $interval)) {
853             ### public != personal who so the below is kind of pointless.
854             my @who;
855             foreach (keys %flood) {
856                 next if (/^\Q$floodwho\E$/ or /^\Q$chan\E$/);
857                 push(@who, grep /^\Q$message\E$/i, keys %{$flood{$_}});
858             }
859             if (scalar @who) {
860                 &msg($who, "you already said what ".
861                                 join(' ', @who)." have said.");
862             } else {
863                 &msg($who,"Someone already said that ". (time - $time) ." seconds ago" );
864             }
865
866             ### TODO: delete old floodwarn{} keys.
867             my $floodwarn = 0;
868             if (!exists $floodwarn{$floodwho}) {
869                 $floodwarn++;
870             } else {
871                 $floodwarn++ if (time() - $floodwarn{$floodwho} > $interval);
872             }
873
874             if ($floodwarn) {
875                 &status("FLOOD repetition detected from $floodwho.");
876                 $floodwarn{$floodwho} = time();
877             }
878
879             return;
880         }
881
882         if ($addrchar) {
883             &status("$b_cyan$who$ob is short-addressing me");
884         } else {
885             &status("$b_cyan$who$ob is addressing me");
886         }
887
888         $flood{$floodwho}{$message} = time();
889     }
890
891     ($count, $interval) = split(/:/, $param{'floodMessages'} || "5:30");
892     # flood overflow protection.
893     if ($addressed) {
894         foreach (keys %{$flood{$floodwho}}) {
895             next unless (time() - $flood{$floodwho}{$_} > $interval);
896             delete $flood{$floodwho}{$_};
897         }
898
899         my $i = scalar keys %{$flood{$floodwho}};
900         if ($i > $count) {
901             &msg($who,"overflow of messages ($i > $count)");
902             &status("FLOOD overflow detected from $floodwho; ignoring");
903
904             my $expire = $param{'ignoreAutoExpire'} || 5;
905             $ignoreList{"*!$uh"} = time() + ($expire * 60);
906             return;
907         }
908
909         $flood{$floodwho}{$message} = time();
910     }
911
912     # public.
913     if ($msgType =~ /public/i) {
914         $talkchannel = $chan;
915         &status("<$orig{who}/$chan> $orig{message}");
916     }
917
918     # private.
919     if ($msgType =~ /private/i) {
920         &status("[$orig{who}] $orig{message}");
921     }
922
923     if ((!$skipmessage or &IsParam("seenStoreAll")) and
924         &IsParam("seen") and
925         $msgType =~ /public/
926     ) {
927         $seencache{$who}{'time'} = time();
928         $seencache{$who}{'nick'} = $orig{who};
929         $seencache{$who}{'host'} = $uh;
930         $seencache{$who}{'chan'} = $talkchannel;
931         $seencache{$who}{'msg'}  = $orig{message};
932         $seencache{$who}{'msgcount'}++;
933     }
934
935     return if ($skipmessage);
936     return unless (&IsParam("minVolunteerLength") or $addressed);
937
938     local $ignore = 0;
939     foreach (keys %ignoreList) {
940         my $ignoreRE = $_;
941         my @parts = split /\*/, "a${ignoreRE}a";
942         my $recast = join '\S*', map quotemeta($_), @parts;
943         $recast =~ s/^a(.*)a$/$1/;
944         if ($nuh =~ /^$recast$/) {
945             $ignore++;
946             last;
947         }
948     }
949
950     if (defined $nuh) {
951         $userHandle = &verifyUser($who, $nuh);
952     } else {
953         &DEBUG("hookMsg: 'nuh' not defined?");
954     }
955
956 ### For extra debugging purposes...
957     if ($_ = &process()) {
958 #       &DEBUG("IrcHooks: process returned '$_'.");
959     }
960
961     return;
962 }
963
964 1;