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