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