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