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