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