]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/IrcHooks.pl
added reason on ban.
[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 = lc $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 = lc $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{$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 = lc $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;
426     push(@bans, keys %{ $bans{$chan} }) if (exists $bans{$chan});
427     push(@bans, keys %{ $bans{"*"} })  if (exists $bans{"*"});
428     foreach (@bans) {
429         my $ban = $_;
430         s/\?/./g;
431         s/\*/\\S*/g;
432         my $mask        = $_;
433         next unless ($nuh =~ /^$mask$/i);
434
435         ### TODO: check $channels{$chan}{'b'} if ban already exists.
436         foreach (keys %{ $channels{$chan}{'b'} }) {
437             &DEBUG(" bans_on_chan($chan) => $_");
438         }
439
440         my $reason = "no reason";
441         foreach ($chan, "*") {
442             next unless (exists $bans{$_});
443             next unless (exists $bans{$_}{$mask});
444
445             my @array   = @{ $bans{$_}{$mask} };
446             $reason     ||= $array[4];
447             last;
448         }
449         &DEBUG("on_join: ban: reason => '$reason'.");
450
451         &ban($ban, $chan);
452         &kick($who, $chan, $reason);
453
454         last;
455     }
456
457     ### ROOTWARN:
458     &rootWarn($who,$user,$host,$chan)
459                 if (&IsChanConf("rootWarn") &&
460                     $user =~ /^r(oo|ew|00)t$/i &&
461                     $channels{$chan}{'o'}{$ident});
462
463     ### chanlimit check.
464     &chanLimitVerify($chan);
465
466     # used to determine sync time.
467     if ($who =~ /^$ident$/i) {
468         if (defined( my $whojoin = $cache{join}{$chan} )) {
469             &msg($chan, "Okay, I'm here. (courtesy of $whojoin)");
470             delete $cache{join}{$chan};
471         }
472
473         ### TODO: move this to &joinchan()?
474         $cache{jointime}{$chan} = &gettimeofday();
475         rawout("WHO $chan");
476     } else {
477         ### TODO: this may go wild on a netjoin :)
478         ### WINGATE:
479         &wingateCheck();
480     }
481 }
482
483 sub on_kick {
484     my ($self, $event) = @_;
485     my ($chan,$reason) = $event->args;
486     my $kicker  = $event->nick;
487     my $kickee  = ($event->to)[0];
488     my $uh      = $event->userhost();
489
490     &status(">>> kick/$b_blue$chan$ob [$b$kickee!$uh$ob] by $b_cyan$kicker$ob $b_yellow($ob$reason$b_yellow)$ob");
491
492     $chan = lc $chan;   # forgot about this, found by xsdg, 20001229.
493     $chanstats{$chan}{'Kick'}++;
494
495     if ($kickee eq $ident) {
496         &clearChanVars($chan);
497
498         &status("SELF attempting to rejoin lost channel $chan");
499         &joinchan($chan);
500     } else {
501         &DeleteUserInfo($kickee,$chan);
502     }
503 }
504
505 sub on_mode {
506     my ($self, $event)  = @_;
507     my ($user, $host)   = split(/\@/, $event->userhost);
508     my @args = $event->args();
509     my $nick = $event->nick();
510     my $chan = ($event->to)[0];
511
512     $args[0] =~ s/\s$//;
513
514     if ($nick eq $chan) {       # UMODE
515         &status(">>> mode $b_yellow\[$ob$b@args$b_yellow\]$ob by $b_cyan$nick$ob");
516     } else {                    # MODE
517         &status(">>> mode/$b_blue$chan$ob $b_yellow\[$ob$b@args$b_yellow\]$ob by $b_cyan$nick$ob");
518         &hookMode($chan, @args);
519     }
520 }
521
522 sub on_modeis {
523     my ($self, $event) = @_;
524     my $nick = $event->nick();
525     my ($myself,$chan,@args) = $event->args();
526
527     &hookMode(lc $chan, @args);         # CASING.
528 }
529
530 sub on_msg {
531     my ($self, $event) = @_;
532     my $nick = $event->nick;
533     my $msg  = ($event->args)[0];
534
535     ($user,$host) = split(/\@/, $event->userhost);
536     $uh         = $event->userhost();
537     $nuh        = $nick."!".$uh;
538
539     &hookMsg('private', undef, $nick, $msg);
540 }
541
542 sub on_names {
543     my ($self, $event) = @_;
544     my @args = $event->args;
545     my $chan = lc $args[2];             # CASING, the last of them!
546
547     foreach (split / /, @args[3..$#args]) {
548         $channels{$chan}{'o'}{$_}++     if s/\@//;
549         $channels{$chan}{'v'}{$_}++     if s/\+//;
550         $channels{$chan}{''}{$_}++;
551     }
552 }
553
554 sub on_nick {
555     my ($self, $event) = @_;
556     my $nick = $event->nick();
557     my $newnick = ($event->args)[0];
558
559     if (exists $netsplit{lc $newnick}) {
560         &status("Netsplit: $newnick/$nick came back from netsplit and changed to original nick! removing from hash.");
561         delete $netsplit{lc $newnick};
562     }
563
564     my ($chan,$mode);
565     foreach $chan (keys %channels) {
566         foreach $mode (keys %{$channels{$chan}}) {
567             next unless (exists $channels{$chan}{$mode}{$nick});
568
569             $channels{$chan}{$mode}{$newnick} = $channels{$chan}{$mode}{$nick};
570         }
571     }
572     &DeleteUserInfo($nick,keys %channels);
573     $nuh{lc $newnick} = $nuh{lc $nick};
574     delete $nuh{lc $nick};
575
576     # successful self-nick change.
577     if ($nick eq $ident) {
578         &status(">>> I materialized into $b_green$newnick$ob from $nick");
579         $ident = $newnick;
580     } else {
581         &status(">>> $b_cyan$nick$ob materializes into $b_green$newnick$ob");
582     }
583 }
584
585 sub on_nick_taken {
586     my ($self) = @_;
587     my $nick = $self->nick;
588     my $newnick = substr($nick,0,7)."-";
589
590     &status("nick taken; changing to temporary nick.");
591     &nick($newnick);
592     &getNickInUse(1);
593 }
594
595 sub on_notice {
596     my ($self, $event) = @_;
597     my $nick = $event->nick();
598     my $chan = ($event->to)[0];
599     my $args = ($event->args)[0];
600
601     if ($nick =~ /^NickServ$/i) {               # nickserv.
602         &status("NickServ: <== '$args'");
603
604         if ($args =~ /^This nickname is registered/i) {
605             &status("nickserv told us to register; doing it.");
606             if (&IsParam("nickServ_pass")) {
607                 &status("NickServ: ==> Identifying.");
608                 &rawout("PRIVMSG NickServ :IDENTIFY $param{'nickServ_pass'}");
609                 return;
610             } else {
611                 &status("We can't tell nickserv a passwd ;(");
612             }
613         }
614
615         # password accepted.
616         if ($args =~ /^Password a/i) {
617             $nickserv++;
618         }
619     } elsif ($nick =~ /^ChanServ$/i) {          # chanserv.
620         &status("ChanServ: <== '$args'.");
621     } else {
622         if ($chan =~ /^$mask{chan}$/) { # channel notice.
623             &status("-$nick/$chan- $args");
624         } else {
625             $server = $nick unless (defined $server);
626             &status("-$nick- $args");   # private or server notice.
627         }
628     }
629 }
630
631 sub on_other {
632     my ($self, $event) = @_;
633     my $chan = ($event->to)[0];
634     my $nick = $event->nick;
635
636     &status("!!! other called.");
637     &status("!!! $event->args");
638 }
639
640 sub on_part {
641     my ($self, $event) = @_;
642     my $chan = lc( ($event->to)[0] );   # CASING!!!
643     my $nick = $event->nick;
644     my $userhost = $event->userhost;
645
646     if (exists $floodjoin{$chan}{$nick}{Time}) {
647         delete $floodjoin{$chan}{$nick};
648     }
649
650     $chanstats{$chan}{'Part'}++;
651     &DeleteUserInfo($nick,$chan);
652     &clearChanVars($chan) if ($nick eq $ident);
653     if (!&IsNickInAnyChan($nick) and &IsChanConf("seenStats")) {
654         delete $userstats{lc $nick};
655     }
656
657     &status(">>> part/$b_blue$chan$ob $b_cyan$nick$ob $b_yellow($ob$userhost$b_yellow)$ob");
658 }
659
660 sub on_ping {
661     my ($self, $event) = @_;
662     my $nick = $event->nick;
663
664     $self->ctcp_reply($nick, join(' ', ($event->args)));
665     &status(">>> ${b_green}CTCP PING$ob request from $b_cyan$nick$ob received.");
666 }
667
668 sub on_ping_reply {
669     my ($self, $event) = @_;
670     my $nick = $event->nick;
671     my $lag = time() - ($event->args)[1];
672
673     &status(">>> ${b_green}CTCP PING$ob reply from $b_cyan$nick$ob: $lag sec.");
674 }
675
676 sub on_public {
677     my ($self, $event) = @_;
678     my $msg  = ($event->args)[0];
679     my $chan = lc( ($event->to)[0] );   # CASING.
680     my $nick = $event->nick;
681     $uh      = $event->userhost();
682     $nuh     = $nick."!".$uh;
683     ($user,$host) = split(/\@/, $uh);
684
685     if ($bot_pid != $$) {
686         &ERROR("run-away fork; exiting.");
687         &delForked($forker);
688     }
689
690     ### DEBUGGING.
691     if ($statcount < 200) {
692         foreach $chan (grep /[A-Z]/, keys %channels) {
693             &DEBUG("leak: chan => '$chan'.");
694             my ($i,$j);
695             foreach $i (keys %{$channels{$chan}}) {  
696                 foreach (keys %{$channels{$chan}{$i}}) {
697                     &DEBUG("leak:   \$channels{$chan}{$i}{$_} ...");
698                 }
699             }
700         }
701     }
702
703
704     $msgtime = time();
705     $lastWho{$chan} = $nick;
706     ### TODO: use $nick or lc $nick?
707     if (&IsChanConf("seenStats")) {
708         $userstats{lc $nick}{'Count'}++;
709         $userstats{lc $nick}{'Time'} = time();
710     }
711
712 #    if (&IsChanConf("hehCounter")) {
713 #       #...
714 #    }
715
716     &hookMsg('public', $chan, $nick, $msg);
717     $chanstats{$chan}{'PublicMsg'}++;
718 }
719
720 sub on_quit {
721     my ($self, $event) = @_;
722     my $nick = $event->nick();
723     my $reason = ($event->args)[0];
724
725     my $count   = 0;
726     foreach (keys %channels) {
727         # fixes inconsistent chanstats bug #1.
728         if (!&IsNickInChan($nick,$_)) {
729             $count++;
730             next;
731         }
732         $chanstats{$_}{'SignOff'}++;
733     }
734
735     if ($count == scalar keys %channels) {
736         &DEBUG("on_quit: nick $nick was not found in any chan.");
737     }
738
739     &DeleteUserInfo($nick, keys %channels);
740
741     if (exists $nuh{lc $nick}) {
742         delete $nuh{lc $nick};
743     } else {
744         &DEBUG("on_quit: nuh{lc $nick} does not exist! FIXME");
745     }
746     delete $userstats{lc $nick} if (&IsChanConf("seenStats"));
747
748     # should fix chanstats inconsistencies bug #2.
749     if ($reason=~/^($mask{host})\s($mask{host})$/) {    # netsplit.
750         $reason = "NETSPLIT: $1 <=> $2";
751
752         if (&ChanConfList("chanlimitcheck") and !scalar keys %netsplit) {
753             &DEBUG("on_quit: netsplit detected; disabling chan limit.");
754             &rawout("MODE $chan -l");
755         }
756
757         $netsplit{lc $nick} = time();
758         if (!exists $netsplitservers{$1}{$2}) {
759             &status("netsplit detected between $1 and $2.");
760             $netsplitservers{$1}{$2} = time();
761         }
762     }
763
764     &status(">>> $b_cyan$nick$ob has signed off IRC $b_red($ob$reason$b_red)$ob");
765     if ($nick =~ /^\Q$ident\E$/) {
766         &DEBUG("^^^ THIS SHOULD NEVER HAPPEN.");
767     }
768
769     if ($nick !~ /^\Q$ident\E$/ and $nick =~ /^\Q$param{'ircNick'}\E$/i) {
770         &status("own nickname became free; changing.");
771         &nick($param{'ircNick'});
772     }
773 }
774
775 sub on_targettoofast {
776     my ($self, $event) = @_;
777     my $nick = $event->nick();
778     my($me,$chan,$why) = $event->args();
779
780     ### TODO: incomplete.
781 ###    .* wait (\d+) second/) {
782         &status("on_ttf: X1 $msg") if (defined $msg);
783         my $sleep = 5;
784         &status("going to sleep for $sleep...");
785         sleep $sleep;
786         &joinNextChan();
787 ### }
788 }
789
790 sub on_topic {
791     my ($self, $event) = @_;
792
793     if (scalar($event->args) == 1) {    # change.
794         my $topic = ($event->args)[0];
795         my $chan  = ($event->to)[0];
796         my $nick  = $event->nick();
797
798         ###
799         # WARNING:
800         #       race condition here. To fix, change '1' to '0'.
801         #       This will keep track of topics set by bot only.
802         ###
803         # UPDATE:
804         #       this may be fixed at a later date with topic queueing.
805         ###
806
807         $topic{$chan}{'Current'} = $topic if (1);
808         $chanstats{$chan}{'Topic'}++;
809
810         &status(">>> topic/$b_blue$chan$ob by $b_cyan$nick$ob -> $topic");
811     } else {                                            # join.
812         my ($nick, $chan, $topic) = $event->args;
813         if (&IsChanConf("topic")) {
814             $topic{$chan}{'Current'}    = $topic;
815             &topicAddHistory($chan,$topic);
816         }
817
818         $topic = &fixString($topic, 1);
819         &status(">>> topic/$b_blue$chan$ob is $topic");
820     }
821 }
822
823 sub on_topicinfo {
824     my ($self, $event) = @_;
825     my ($myself,$chan,$setby,$time) = $event->args();
826
827     my $timestr;
828     if (time() - $time > 60*60*24) {
829         $timestr        = "at ". localtime $time;
830     } else {
831         $timestr        = &Time2String(time() - $time) ." ago";
832     }
833
834     &status(">>> set by $b_cyan$setby$ob $timestr");
835 }
836
837 sub on_crversion {
838     my ($self, $event) = @_;
839     my $nick    = $event->nick();
840     my $ver;
841
842     if (scalar $event->args() != 1) {   # old.
843         $ver    = join ' ', $event->args();
844         $ver    =~ s/^VERSION //;
845     } else {                            # new.
846         $ver    = ($event->args())[0];
847     }
848
849     if (grep /^\Q$nick\E$/i, @vernick) {
850         &WARN("nick $nick found in vernick; skipping.");
851         return;
852     }
853     push(@vernick, $nick);
854
855     if ($ver =~ /bitchx/i) {
856         $ver{bitchx}{$nick}     = $ver;
857     } elsif ($ver =~ /xc\!|xchat/i) {
858         $ver{xchat}{$nick}      = $ver;
859     } elsif ($ver =~ /irssi/i) {
860         $ver{irssi}{$nick}      = $ver;
861     } elsif ($ver =~ /epic/i) {
862         $ver{epic}{$nick}       = $ver;
863     } elsif ($ver =~ /mirc/i) {
864         $ver{mirc}{$nick}       = $ver;
865     } elsif ($ver =~ /ircle/i) {
866         $ver{ircle}{$nick}      = $ver;
867     } elsif ($ver =~ /ircII/i) {
868         $ver{ircII}{$nick}      = $ver;
869     } elsif ($ver =~ /sirc /i) {
870         $ver{sirc}{$nick}       = $ver;
871     } elsif ($ver =~ /kvirc/i) {
872         $ver{kvirc}{$nick}      = $ver;
873     } elsif ($ver =~ /eggdrop/i) {
874         $ver{eggdrop}{$nick}    = $ver;
875     } elsif ($ver =~ /xircon/i) {
876         $ver{xircon}{$nick}     = $ver;
877     } else {
878         $ver{other}{$nick}      = $ver;
879     }
880 }
881
882 sub on_version {
883     my ($self, $event) = @_;
884     my $nick = $event->nick;
885
886     &status(">>> ${b_green}CTCP VERSION$ob request from $b_cyan$nick$ob");
887     $self->ctcp_reply($nick, "VERSION $bot_version");
888 }
889
890 sub on_who {
891     my ($self, $event) = @_;
892     my @args    = $event->args;
893
894     $nuh{lc $args[5]} = $args[5]."!".$args[2]."\@".$args[3];
895 }
896
897 sub on_whoisuser {
898     my ($self, $event) = @_;
899     my @args    = $event->args;
900
901     &DEBUG("on_whoisuser: @args");
902
903     $nuh{lc $args[1]} = $args[1]."!".$args[2]."\@".$args[3];
904 }
905
906 #######################################################################
907 ####### IRC HOOK HELPERS   IRC HOOK HELPERS   IRC HOOK HELPERS ########
908 #######################################################################
909
910 #####
911 # Usage: &hookMode($chan, $modes, @targets);
912 sub hookMode {
913     my ($chan, $modes, @targets) = @_;
914     my $parity  = 0;
915
916     $chan = lc $chan;           # !!!.
917
918     my $mode;
919     foreach $mode (split(//, $modes)) {
920         # sign.
921         if ($mode =~ /[-+]/) {
922             $parity = 1         if ($mode eq "+");
923             $parity = 0         if ($mode eq "-");
924             next;
925         }
926
927         # mode with target.
928         if ($mode =~ /[bklov]/) {
929             my $target = shift @targets;
930
931             if ($parity) {
932                 $chanstats{$chan}{'Op'}++    if ($mode eq "o");
933                 $chanstats{$chan}{'Ban'}++   if ($mode eq "b");
934             } else {
935                 $chanstats{$chan}{'Deop'}++  if ($mode eq "o");
936                 $chanstats{$chan}{'Unban'}++ if ($mode eq "b");
937             }
938
939             # modes w/ target affecting nick => cache it.
940             if ($mode =~ /[bov]/) {
941                 $channels{$chan}{$mode}{$target}++      if  $parity;
942                 delete $channels{$chan}{$mode}{$target} if !$parity;
943             }
944
945             if ($mode =~ /[l]/) {
946                 $channels{$chan}{$mode} = $target       if  $parity;
947                 delete $channels{$chan}{$mode}          if !$parity;
948             }
949         }
950
951         # important channel modes, targetless.
952         if ($mode =~ /[mt]/) {
953             $channels{$chan}{$mode}++                   if  $parity;
954             delete $channels{$chan}{$mode}              if !$parity;
955         }
956     }
957 }
958
959 sub hookMsg {
960     ($msgType, $chan, $who, $message) = @_;
961     my $skipmessage     = 0;
962     $addressed          = 0;
963     $addressedother     = 0;
964     $orig{message}      = $message;
965     $orig{who}          = $who;
966     $addrchar           = 0;
967
968     $message    =~ s/[\cA-\c_]//ig;     # strip control characters
969     $message    =~ s/^\s+//;            # initial whitespaces.
970     $who        =~ tr/A-Z/a-z/;         # lowercase.
971
972     &showProc();
973
974     # addressing.
975     if ($msgType =~ /private/) {
976         # private messages.
977         $addressed = 1;
978     } else {
979         # public messages.
980         # addressing revamped by the xk.
981         ### below needs to be fixed...
982         if (&IsParam("addressCharacter")) {
983             if ($message =~ s/^$param{'addressCharacter'}//) {
984                 $addrchar  = 1;
985                 $addressed = 1;
986             }
987         }
988
989         if ($message =~ /^($mask{nick})([\;\:\>\, ]+) */) {
990             my $newmessage = $';
991             if ($1 =~ /^\Q$ident\E$/i) {
992                 $message   = $newmessage;
993                 $addressed = 1;
994             } else {
995                 # ignore messages addressed to other people or unaddressed.
996                 $skipmessage++ if ($2 ne "" and $2 !~ /^ /);
997             }
998         }
999     }
1000
1001     # Determine floodwho.
1002     my $c       = "_default";
1003     if ($msgType =~ /public/i) {                # public.
1004         $floodwho = $c = lc $chan;
1005     } elsif ($msgType =~ /private/i) {  # private.
1006         $floodwho = lc $who;
1007     } else {                            # dcc?
1008         &DEBUG("FIXME: floodwho = ???");
1009     }
1010
1011     my $val = &getChanConfDefault("floodRepeat", "2:10", $c);
1012     my ($count, $interval) = split /:/, $val;
1013
1014     # flood repeat protection.
1015     if ($addressed) {
1016         my $time = $flood{$floodwho}{$message} || 0;
1017
1018         if ($msgType eq "public" and (time() - $time < $interval)) {
1019             ### public != personal who so the below is kind of pointless.
1020             my @who;
1021             foreach (keys %flood) {
1022                 next if (/^\Q$floodwho\E$/);
1023                 next if (defined $chan and /^\Q$chan\E$/);
1024
1025                 push(@who, grep /^\Q$message\E$/i, keys %{ $flood{$_} });
1026             }
1027
1028             if (scalar @who) {
1029                 &msg($who, "you already said what ".
1030                                 join(' ', @who)." have said.");
1031             } else {
1032                 &msg($who,"Someone already said that ". (time - $time) ." seconds ago" );
1033             }
1034
1035             ### TODO: delete old floodwarn{} keys.
1036             my $floodwarn = 0;
1037             if (!exists $floodwarn{$floodwho}) {
1038                 $floodwarn++;
1039             } else {
1040                 $floodwarn++ if (time() - $floodwarn{$floodwho} > $interval);
1041             }
1042
1043             if ($floodwarn) {
1044                 &status("FLOOD repetition detected from $floodwho.");
1045                 $floodwarn{$floodwho} = time();
1046             }
1047
1048             return;
1049         }
1050
1051         if ($addrchar) {
1052             &status("$b_cyan$who$ob is short-addressing me");
1053         } elsif ($msgType eq "private") {       # private.
1054             &status("$b_cyan$who$ob is /msg'ing me");
1055         } else {                                # public?
1056             &status("$b_cyan$who$ob is addressing me");
1057         }
1058
1059         $flood{$floodwho}{$message} = time();
1060     } elsif ($msgType eq "public" and &IsChanConf("kickOnRepeat")) {
1061         # unaddressed, public only.
1062
1063         ### TODO: use a separate "short-time" hash.
1064         my @data;
1065         @data   = keys %{ $flood{$floodwho} } if (exists $flood{$floodwho});
1066     }
1067
1068     $val = &getChanConfDefault("floodMessages", "5:30", $c);
1069     ($count, $interval) = split /:/, $val;
1070
1071     # flood overflow protection.
1072     if ($addressed) {
1073         foreach (keys %{$flood{$floodwho}}) {
1074             next unless (time() - $flood{$floodwho}{$_} > $interval);
1075             delete $flood{$floodwho}{$_};
1076         }
1077
1078         my $i = scalar keys %{$flood{$floodwho}};
1079         if ($i > $count) {
1080             &msg($who,"overflow of messages ($i > $count)");
1081             &status("FLOOD overflow detected from $floodwho; ignoring");
1082
1083             my $expire = $param{'ignoreAutoExpire'} || 5;
1084             &ignoreAdd("*!$uh", $chan, $expire, "flood overflow auto-detected.");
1085             return;
1086         }
1087
1088         $flood{$floodwho}{$message} = time();
1089     }
1090
1091     my @ignore;
1092     if ($msgType =~ /public/i) {                    # public.
1093         $talkchannel    = $chan;
1094         &status("<$orig{who}/$chan> $orig{message}");
1095         push(@ignore, keys %{ $ignore{$chan} }) if (exists $ignore{$chan});
1096     } elsif ($msgType =~ /private/i) {             # private.
1097         &status("[$orig{who}] $orig{message}");
1098         $talkchannel    = undef;
1099         $chan           = "_default";
1100     } else {
1101         &DEBUG("unknown msgType => $msgType.");
1102     }
1103     push(@ignore, keys %{ $ignore{"*"} }) if (exists $ignore{"*"});
1104
1105     if ((!$skipmessage or &IsChanConf("seenStoreAll")) and
1106         &IsChanConf("seen") and
1107         $msgType =~ /public/
1108     ) {
1109         $seencache{$who}{'time'} = time();
1110         $seencache{$who}{'nick'} = $orig{who};
1111         $seencache{$who}{'host'} = $uh;
1112         $seencache{$who}{'chan'} = $talkchannel;
1113         $seencache{$who}{'msg'}  = $orig{message};
1114         $seencache{$who}{'msgcount'}++;
1115     }
1116
1117     return if ($skipmessage);
1118     return unless (&IsParam("minVolunteerLength") or $addressed);
1119
1120     foreach (@ignore) {
1121         s/\*/\\S*/g;
1122
1123         next unless (eval { $nuh =~ /^$_$/i });
1124
1125         &status("IGNORE <$who> $message");
1126         return;
1127     }
1128
1129     if (defined $nuh) {
1130         if (!defined $userHandle) {
1131             &DEBUG("line 1074: need verifyUser?");
1132             &verifyUser($who, $nuh);
1133         }
1134     } else {
1135         &DEBUG("hookMsg: 'nuh' not defined?");
1136     }
1137
1138 ### For extra debugging purposes...
1139     if ($_ = &process()) {
1140 #       &DEBUG("IrcHooks: process returned '$_'.");
1141     }
1142
1143     return;
1144 }
1145
1146 sub chanLimitVerify {
1147     my($chan)   = @_;
1148     my $l       = $channels{$chan}{'l'};
1149
1150     # only change it if it's not set.
1151     if (defined $l and &IsChanConf("chanlimitcheck")) {
1152         my $plus = &getChanConfDefault("chanlimitcheckPlus", 5, $chan);
1153         my $nowl = scalar(keys %{ $channels{$chan}{''} });
1154         my $delta       = $nowl - $l;
1155         $delta          =~ s/^\-//;
1156
1157         if ($plus <= 3) {
1158             &WARN("clc: stupid to have plus at $plus, fix it!");
1159         }
1160
1161         ### todo: check if we have ops.
1162         ### todo: if not, check if nickserv/chanserv is avail.
1163         ### todo: unify code with chanlimitcheck()
1164         if ($delta > 3) {
1165             &WARN("clc: nowl($nowl) > l($l) - 3");
1166             &rawout("MODE $chan +l ".($nowl+$plus) );
1167         }
1168     }
1169 }
1170
1171 1;