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