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