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