]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/Irc.pl
- added irc numeric 443 (useronchannel). should fix join problems.
[infobot.git] / src / IRC / Irc.pl
1 #
2 #    Irc.pl: IRC core 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 use vars qw($nickserv);
11 $nickserv       = 0;
12
13 # static scalar variables.
14 $mask{ip}       = '(\d+)\.(\d+)\.(\d+)\.(\d+)';
15 $mask{host}     = '[\d\w\_\-\/]+\.[\.\d\w\_\-\/]+';
16 $mask{chan}     = '[\#\&]\S*|_default';
17 my $isnick1     = 'a-zA-Z\[\]\{\}\_\`\^\|\\\\';
18 my $isnick2     = '0-9\-';
19 $mask{nick}     = "[$isnick1]{1}[$isnick1$isnick2]*";
20 $mask{nuh}      = '\S*!\S*\@\S*';
21
22 sub ircloop {
23     my $error   = 0;
24     my $lastrun = 0;
25
26 loop:;
27     while (my $host = shift @ircServers) {
28         # JUST IN CASE. irq was complaining about this.
29         if ($lastrun == time()) {
30             &DEBUG("ircloop: hrm... lastrun == time()");
31             $error++;
32             sleep 10;
33             next;
34         }
35
36         if (!defined $host) {
37             &DEBUG("ircloop: ircServers[x] = NULL.");
38             $lastrun = time();
39             next;
40         }
41         next unless (exists $ircPort{$host});
42
43         my $retval      = &irc($host, $ircPort{$host});
44         next unless (defined $retval and $retval == 0);
45         $error++;
46
47         if ($error % 3 == 0 and $error != 0) {
48             &status("IRC: Could not connect.");
49             &status("IRC: ");
50             next;
51         }
52
53         if ($error >= 3*2) {
54             &status("IRC: cannot connect to any IRC servers; stopping.");
55             &shutdown();
56             exit 1;
57         }
58     }
59
60     &status("IRC: ok, done one cycle of IRC servers; trying again.");
61
62     &loadIRCServers();
63     goto loop;
64 }
65
66 sub irc {
67     my ($server,$port) = @_;
68
69     my $iaddr = inet_aton($server);
70     my $paddr = sockaddr_in($port, $iaddr);
71     my $proto = getprotobyname('tcp');
72
73     select STDOUT;
74     &status("Connecting to port $port of server $server ...");
75
76     # host->ip.
77     if ($server =~ /\D$/) {
78         my $packed = scalar(gethostbyname($server));
79
80         if (!defined $packed) {
81             &status("  cannot resolve $server.");
82             return 0;
83         }
84
85         my $resolve = inet_ntoa($packed);
86         &status("  resolved to $resolve.");
87         ### warning in Sys/Hostname line 78???
88         ### caused inside Net::IRC?
89     }
90
91     $irc = new Net::IRC;
92
93     my %args = (
94                 Nick    => $param{'ircNick'},
95                 Server  => $server,
96                 Port    => $port,
97                 Ircname => $param{'ircName'},
98     );
99     $args{'LocalAddr'} = $param{'ircHost'} if ($param{'ircHost'});
100     $args{'Password'} = $param{'ircPasswd'} if ($param{'ircPasswd'});
101
102     $conn = $irc->newconn(%args);
103
104     if (!defined $conn) {
105         &ERROR("irc: conn was not created!defined!!!");
106         return 1;
107     }
108
109     &clearIRCVars();
110
111     # change internal timeout value for scheduler.
112     $irc->{_timeout}    = 10;   # how about 60?
113     # Net::IRC debugging.
114     $irc->{_debug}      = 1;
115
116     $ircstats{'Server'} = "$server:$port";
117
118     # handler stuff.
119         $conn->add_handler('caction',   \&on_action);
120         $conn->add_handler('cdcc',      \&on_dcc);
121         $conn->add_handler('cping',     \&on_ping);
122         $conn->add_handler('crping',    \&on_ping_reply);
123         $conn->add_handler('cversion',  \&on_version);
124         $conn->add_handler('crversion', \&on_crversion);
125         $conn->add_handler('dcc_open',  \&on_dcc_open);
126         $conn->add_handler('dcc_close', \&on_dcc_close);
127         $conn->add_handler('chat',      \&on_chat);
128         $conn->add_handler('msg',       \&on_msg);
129         $conn->add_handler('public',    \&on_public);
130         $conn->add_handler('join',      \&on_join);
131         $conn->add_handler('part',      \&on_part);
132         $conn->add_handler('topic',     \&on_topic);
133         $conn->add_handler('invite',    \&on_invite);
134         $conn->add_handler('kick',      \&on_kick);
135         $conn->add_handler('mode',      \&on_mode);
136         $conn->add_handler('nick',      \&on_nick);
137         $conn->add_handler('quit',      \&on_quit);
138         $conn->add_handler('notice',    \&on_notice);
139         $conn->add_handler('whoischannels', \&on_whoischannels);
140         $conn->add_handler('useronchannel', \&on_useronchannel);
141         $conn->add_handler('whois',     \&on_whois);
142         $conn->add_handler('other',     \&on_other);
143         $conn->add_global_handler('disconnect', \&on_disconnect);
144         $conn->add_global_handler([251,252,253,254,255], \&on_init);
145 ###     $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
146         $conn->add_global_handler(315, \&on_endofwho);
147         $conn->add_global_handler(422, \&on_endofwho); # nomotd.
148         $conn->add_global_handler(324, \&on_modeis);
149         $conn->add_global_handler(333, \&on_topicinfo);
150         $conn->add_global_handler(352, \&on_who);
151         $conn->add_global_handler(353, \&on_names);
152         $conn->add_global_handler(366, \&on_endofnames);
153         $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
154         $conn->add_global_handler(433, \&on_nick_taken);
155         $conn->add_global_handler(439, \&on_targettoofast);
156         # for proper joinnextChan behaviour
157         $conn->add_global_handler(471, \&on_chanfull);
158         $conn->add_global_handler(473, \&on_inviteonly);
159         $conn->add_global_handler(474, \&on_banned);
160         $conn->add_global_handler(475, \&on_badchankey);
161         $conn->add_global_handler(443, \&on_useronchan);
162
163     # end of handler stuff.
164
165     $irc->start;
166 }
167
168 ######################################################################
169 ######## IRC ALIASES   IRC ALIASES   IRC ALIASES   IRC ALIASES #######
170 ######################################################################
171
172 sub rawout {
173     my ($buf) = @_;
174     $buf =~ s/\n//gi;
175
176     # slow down a bit if traffic is "high".
177     # need to take into account time of last message sent.
178     if ($last{buflen} > 256 and length($buf) > 256) {
179         sleep 1;
180     }
181
182     $conn->sl($buf) if (&whatInterface() =~ /IRC/);
183
184     $last{buflen} = length($buf);
185 }
186
187 sub say {
188     my ($msg) = @_;
189     if (!defined $msg) {
190         $msg ||= "NULL";
191         &WARN("say: msg == $msg.");
192         return;
193     }
194
195     &status("</$talkchannel> $msg");
196     if (&whatInterface() =~ /IRC/) {
197         $msg    = "zero" if ($msg =~ /^0+$/);
198         my $t   = time();
199
200         if ($t == $pubtime) {
201             $pubcount++;
202             $pubsize += length $msg;
203
204             my $i = &getChanConfDefault("sendPublicLimitLines", 3);
205             my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
206
207             if ( ($pubcount % $i) == 0 and $pubcount) {
208                 sleep 1;
209             } elsif ($pubsize > $j) {
210                 sleep 1;
211                 $pubsize -= $j;
212             }
213
214         } else {
215             $pubcount   = 0;
216             $pubtime    = $t;
217             $pubsize    = length $msg;
218         }
219
220         $conn->privmsg($talkchannel, $msg);
221     }
222 }
223
224 sub msg {
225     my ($nick, $msg) = @_;
226     if (!defined $nick) {
227         &ERROR("msg: nick == NULL.");
228         return;
229     }
230
231     if (!defined $msg) {
232         $msg ||= "NULL";
233         &WARN("msg: msg == $msg.");
234         return;
235     }
236
237     if ($msgType =~ /chat/i) {
238         # todo: warn that we're using msg() to do DCC CHAT?
239         &dccsay($nick, $msg);
240         # todo: make dccsay deal with flood protection?
241         return;
242     }
243
244     &status(">$nick< $msg");
245
246     if (&whatInterface() =~ /IRC/) {
247         my $t = time();
248
249         if ($t == $msgtime) {
250             $msgcount++;
251             $msgsize += length $msg;
252
253             my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
254             my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
255             if ( ($msgcount % $i) == 0 and $msgcount) {
256                 sleep 1;
257             } elsif ($msgsize > $j) {
258                 sleep 1;
259                 $msgsize -= $j;
260             }
261
262         } else {
263             $msgcount   = 0;
264             $msgtime    = $t;
265             $msgsize    = length $msg;
266         }
267
268         $conn->privmsg($nick, $msg);
269     }
270 }
271
272 # Usage: &action(nick || chan, txt);
273 sub action {
274     my ($target, $txt) = @_;
275     if (!defined $txt) {
276         &WARN("action: txt == NULL.");
277         return;
278     }
279
280     my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
281     if (length $rawout > 510) {
282         &status("action: txt too long; truncating.");
283
284         chop($rawout) while (length($rawout) > 510);
285         $rawout .= "\001";
286     }
287
288     &status("* $ident/$target $txt");
289     rawout($rawout);
290 }
291
292 # Usage: &notice(nick || chan, txt);
293 sub notice {
294     my ($target, $txt) = @_;
295     if (!defined $txt) {
296         &WARN("notice: txt == NULL.");
297         return;
298     }
299
300     &status("-$target- $txt");
301
302     my $t       = time();
303
304     if ($t == $nottime) {
305         $notcount++;
306         $notsize += length $txt;
307
308         my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
309         my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
310
311         if ( ($notcount % $i) == 0 and $notcount) {
312             sleep 1;
313         } elsif ($notsize > $j) {
314             sleep 1;
315             $notsize -= $j;
316         }
317
318     } else {
319         $notcount       = 0;
320         $nottime        = $t;
321         $notsize        = length $txt;
322     }
323
324     $conn->notice($target, $txt);
325 }
326
327 sub DCCBroadcast {
328     my ($txt,$flag) = @_;
329
330     ### FIXME: flag not supported yet.
331
332     foreach (keys %{ $dcc{'CHAT'} }) {
333         $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
334     }
335 }
336
337 ##########
338 ### perform commands.
339 ###
340
341 # Usage: &performReply($reply);
342 sub performReply {
343     my ($reply) = @_;
344     $reply =~ /([\.\?\s]+)$/;
345
346     &checkMsgType($reply);
347
348     if ($msgType eq 'public') {
349         if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
350             $reply = "$orig{who}: ".$reply;
351         } else {
352             $reply = "$reply, ".$orig{who};
353         }
354         &say($reply);
355     } elsif ($msgType eq 'private') {
356         if (rand() < 0.5) {
357             $reply = $reply;
358         } else {
359             $reply = "$reply, ".$orig{who};
360         }
361         &msg($who, $reply);
362     } elsif ($msgType eq 'chat') {
363         if (!exists $dcc{'CHAT'}{$who}) {
364             &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
365             return;
366         }
367         $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
368     } else {
369         &ERROR("PR: msgType invalid? ($msgType).");
370     }
371 }
372
373 # ...
374 sub performAddressedReply {
375     return unless ($addressed);
376     &performReply(@_);
377 }
378
379 sub pSReply {
380     &performStrictReply(@_);
381 }
382
383 # Usage: &performStrictReply($reply);
384 sub performStrictReply {
385     my ($reply) = @_;
386
387     &checkMsgType($reply);
388
389     if ($msgType eq 'private') {
390         &msg($who, $reply);
391     } elsif ($msgType eq 'public') {
392         &say($reply);
393     } elsif ($msgType eq 'chat') {
394         &dccsay(lc $who, $reply);
395     } else {
396         &ERROR("pSR: msgType invalid? ($msgType).");
397     }
398 }
399
400 sub dccsay {
401     my($who, $reply) = @_;
402
403     if (!defined $reply or $reply =~ /^\s*$/) {
404         &WARN("dccsay: reply == NULL.");
405         return;
406     }
407
408     if (!exists $dcc{'CHAT'}{$who}) {
409         &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
410         return;
411     }
412
413     &status("=>$who<= $reply");         # dcc chat.
414     $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
415 }
416
417 sub dcc_close {
418     my($who) = @_;
419     my $type;
420
421     foreach $type (keys %dcc) {
422         &FIXME("dcc_close: $who");
423         my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
424         next unless (scalar @who);
425         $who = $who[0];
426         &DEBUG("dcc_close... close $who!");
427     }
428 }
429
430 sub joinchan {
431     my ($chankey) = @_;
432     my $chan = lc $chankey;
433
434     if ($chankey =~ s/^($mask{chan}),\S+/ /) {
435         $chan = lc $1;
436     }
437
438     # hopefully validChan is right.
439     if (&validChan($chan)) {
440         &status("join: already on $chan");
441     } else {
442         &status("joining $b_blue$chan$ob");
443         if (!$conn->join($chan)) {
444             &DEBUG("joinchan: join failed. trying connect!");
445             &clearIRCVars();
446             $conn->connect();
447         }
448     }
449 }
450
451 sub part {
452     my $chan;
453
454     foreach $chan (@_) {
455         next if ($chan eq "");
456         $chan =~ tr/A-Z/a-z/;   # lowercase.
457
458         if ($chan !~ /^$mask{chan}$/) {
459             &WARN("part: chan is invalid ($chan)");
460             next;
461         }
462
463         &status("parting $chan");
464         if (!&validChan($chan)) {
465             &WARN("part: not on $chan; doing anyway");
466 #           next;
467         }
468
469         rawout("PART $chan");
470         # deletion of $channels{chan} is done in &entryEvt().
471     }
472 }
473
474 sub mode {
475     my ($chan, @modes) = @_;
476     my $modes = join(" ", @modes);
477
478     if (&validChan($chan) == 0) {
479         &ERROR("mode: invalid chan => '$chan'.");
480         return;
481     }
482
483     &DEBUG("mode: MODE $chan $modes");
484
485     rawout("MODE $chan $modes");
486 }
487
488 sub op {
489     my ($chan, @who) = @_;
490     my $os      = "o" x scalar(@who);
491
492     &mode($chan, "+$os @who");
493 }
494
495 sub deop {
496     my ($chan, @who) = @_;
497     my $os = "o" x scalar(@who);
498
499     &mode($chan, "-$os ".@who);
500 }
501
502 sub kick {
503     my ($nick,$chan,$msg) = @_;
504     my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
505
506     if ($chan ne "" and &validChan($chan) == 0) {
507         &ERROR("kick: invalid channel $chan.");
508         return;
509     }
510
511     $nick =~ tr/A-Z/a-z/;
512
513     foreach $chan (@chans) {
514         if (!&IsNickInChan($nick,$chan)) {
515             &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
516             next;
517         }
518
519         if (!exists $channels{$chan}{o}{$ident}) {
520             &status("Kick: do not have ops on $chan :(");
521             next;
522         }
523
524         &status("Kicking $nick from $chan.");
525         if ($msg eq "") {
526             &rawout("KICK $chan $nick");
527         } else {
528             &rawout("KICK $chan $nick :$msg");
529         }
530     }
531 }
532
533 sub ban {
534     my ($mask,$chan) = @_;
535     my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
536     my $ban     = 0;
537
538     if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
539         &ERROR("ban: invalid channel $chan.");
540         return;
541     }
542
543     foreach $chan (@chans) {
544         if (!exists $channels{$chan}{o}{$ident}) {
545             &status("Ban: do not have ops on $chan :(");
546             next;
547         }
548
549         &status("Banning $mask from $chan.");
550         &rawout("MODE $chan +b $mask");
551         $ban++;
552     }
553
554     return $ban;
555 }
556
557 sub unban {
558     my ($mask,$chan) = @_;
559     my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
560     my $ban     = 0;
561
562     &DEBUG("unban: mask = $mask, chan = @chans");
563
564     foreach $chan (@chans) {
565         if (!exists $channels{$chan}{o}{$ident}) {
566             &status("unBan: do not have ops on $chan :(");
567             next;
568         }
569
570         &status("Removed ban $mask from $chan.");
571         &rawout("MODE $chan -b $mask");
572         $ban++;
573     }
574
575     return $ban;
576 }
577
578 sub quit {
579     my ($quitmsg) = @_;
580     &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
581     if (defined $conn) {
582         $conn->quit($quitmsg);
583     } else {
584         &WARN("quit: could not quit!");
585     }
586 }
587
588 sub nick {
589     my ($nick) = @_;
590
591     if (!defined $nick) {
592         &ERROR("nick: nick == NULL.");
593         return;
594     }
595
596     if (defined $ident and $nick eq $ident) {
597         &WARN("nick: nick == ident == '$ident'.");
598     }
599
600     my $bad     = 0;
601     $bad++ if (exists $nuh{ $param{'ircNick'} });
602     $bad++ if (&IsNickInAnyChan($param{'ircNick'}));
603
604     if ($bad) {
605         &WARN("Nick: not going to try and get my nick back. [".
606                 scalar(localtime). "]");
607         return;
608     }
609
610     if ($nick =~ /^$mask{nick}$/) {
611         rawout("NICK ".$nick);
612
613         if (defined $ident) {
614             &status("nick: Changing nick to $nick (from $ident)");
615         } else {
616             &DEBUG("first time nick change.");
617             $ident      = $nick;
618         }
619
620         return 1;
621     }
622     &DEBUG("nick: failed... why oh why (nick => $nick)");
623
624     return 0;
625 }
626
627 sub invite {
628     my($who, $chan) = @_;
629     rawout("INVITE $who $chan");
630 }
631
632
633 ##########
634 # Channel related functions...
635 #
636
637 # Usage: &joinNextChan();
638 sub joinNextChan {
639     if (scalar @joinchan) {
640         $chan = shift @joinchan;
641         &joinchan($chan);
642
643         if (my $i = scalar @joinchan) {
644             &status("joinNextChan: $i chans to join.");
645         }
646
647         return;
648     }
649
650     # !scalar @joinchan:
651     my @c       = &getJoinChans();
652     if (exists $cache{joinTime} and scalar @c) {
653         my $delta       = time() - $cache{joinTime} - 5;
654         my $timestr     = &Time2String($delta);
655         my $rate        = sprintf("%.1f", $delta / @c);
656         delete $cache{joinTime};
657
658         &status("time taken to join all chans: $timestr; rate: $rate sec/join");
659     }
660
661     # chanserv check: global channels, in case we missed one.
662     foreach ( &ChanConfList("chanServ_ops") ) {
663         &chanServCheck($_);
664     }
665 }
666
667 # Usage: &getNickInChans($nick);
668 sub getNickInChans {
669     my ($nick) = @_;
670     my @array;
671
672     foreach (keys %channels) {
673         next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
674         push(@array, $_);
675     }
676
677     return @array;
678 }
679
680 # Usage: &getNicksInChan($chan);
681 sub getNicksInChan {
682     my ($chan) = @_;
683     my @array;
684
685     return keys %{ $channels{$chan}{''} };
686 }
687
688 sub IsNickInChan {
689     my ($nick,$chan) = @_;
690
691     $chan =~ tr/A-Z/a-z/;       # not lowercase unfortunately.
692
693     if (&validChan($chan) == 0) {
694         &ERROR("INIC: invalid channel $chan.");
695         return 0;
696     }
697
698     if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
699         return 1;
700     } else {
701         foreach (keys %channels) {
702             next unless (/[A-Z]/);
703             &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
704         }
705         return 0;
706     }
707 }
708
709 sub IsNickInAnyChan {
710     my ($nick) = @_;
711
712     foreach $chan (keys %channels) {
713         next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''}  });
714         return 1;
715     }
716     return 0;
717 }
718
719 # Usage: &validChan($chan);
720 sub validChan {
721     # todo: use $c instead?
722     my ($chan) = @_;
723
724     if (!defined $chan or $chan =~ /^\s*$/) {
725         return 0;
726     }
727
728     if (lc $chan ne $chan) {
729         &WARN("validChan: lc chan != chan. ($chan); fixing.");
730         $chan =~ tr/A-Z/a-z/;
731     }
732
733     # it's possible that this check creates the hash if empty.
734     if (defined $channels{$chan} or exists $channels{$chan}) {
735         if ($chan eq "_default") {
736 #           &WARN("validC: chan cannot be _default! returning 0!");
737             return 0;
738         }
739
740         return 1;
741     } else {
742         return 0;
743     }
744 }
745
746 ###
747 # Usage: &delUserInfo($nick,@chans);
748 sub delUserInfo {
749     my ($nick,@chans) = @_;
750     my ($mode,$chan);
751
752     foreach $chan (@chans) {
753         foreach $mode (keys %{ $channels{$chan} }) {
754             # use grep here?
755             next unless (exists $channels{$chan}{$mode}{$nick});
756
757             delete $channels{$chan}{$mode}{$nick};
758         }
759     }
760 }
761
762 sub clearChanVars {
763     my ($chan) = @_;
764
765     delete $channels{$chan};
766 }
767
768 sub clearIRCVars {
769     &DEBUG("clearIRCVars() called!");
770     undef %channels;
771     undef %floodjoin;
772
773     @joinchan           = &getJoinChans(1);
774     $cache{joinTime}    = time();
775 }
776
777 sub getJoinChans {
778     my($show)   = @_;
779     my @chans;
780     my @skip;
781
782     foreach (keys %chanconf) {
783         next if ($_ eq "_default");
784
785         my $val = $chanconf{$_}{autojoin};
786         my $skip = 0;
787
788         if (defined $val) {
789             $skip++ if ($val eq "0");
790         } else {
791             $skip++;
792         }
793
794         if ($skip) {
795             push(@skip, $_);
796             next;
797         }
798
799         push(@chans, $_);
800     }
801
802     my $str;
803     if (scalar @skip) {
804         $str = "channels not auto-joining: @skip (joining: @chans)";
805     } else {
806         $str = "auto-joining all chans: @chans";
807     }
808
809     &status("Chans: ".$str) if ($show);
810
811     return @chans;
812 }
813
814 sub closeDCC {
815 #    &DEBUG("closeDCC called.");
816
817     foreach $type (keys %dcc) {
818         next if ($type ne uc($type));
819  
820         foreach $nick (keys %{ $dcc{$type} }) {
821             next unless (defined $nick);
822             &status("DCC CHAT: closing DCC $type to $nick.");
823             next unless (defined $dcc{$type}{$nick});
824
825             my $ref = $dcc{$type}{$nick};
826             &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
827             $dcc{$type}{$nick}->close();
828             delete $dcc{$type}{$nick};
829             &DEBUG("after close for $nick");
830         }
831         delete $dcc{$type};
832     }
833 }
834
835 sub joinfloodCheck {
836     my($who,$chan,$userhost) = @_;
837
838     return unless (&IsChanConf("joinfloodCheck"));
839
840     if (exists $netsplit{lc $who}) {    # netsplit join.
841         &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
842     }
843
844     if (exists $floodjoin{$chan}{$who}{Time}) {
845         &WARN("floodjoin{$chan}{$who} already exists?");
846     }
847
848     $floodjoin{$chan}{$who}{Time} = time();
849     $floodjoin{$chan}{$who}{Host} = $userhost;
850
851     ### Check...
852     foreach (keys %floodjoin) {
853         my $c = $_;
854         my $count = scalar keys %{ $floodjoin{$c} };
855         next unless ($count > 5);
856         &DEBUG("joinflood: count => $count");
857
858         my $time;
859         foreach (keys %{ $floodjoin{$c} }) {
860             $time += $floodjoin{$c}{$_}{Time};
861         }
862         &DEBUG("joinflood: time => $time");
863         $time /= $count;
864
865         &DEBUG("joinflood: new time => $time");
866     }
867
868     ### Clean it up.
869     my $delete = 0;
870     my $time = time();
871     foreach $chan (keys %floodjoin) {
872         foreach $who (keys %{ $floodjoin{$chan} }) {
873             my $t       = $floodjoin{$chan}{$who}{Time};
874             next unless (defined $t);
875
876             my $delta   = $time - $t;
877             next unless ($delta > 10);
878
879             delete $floodjoin{$chan}{$who};
880             $delete++;
881         }
882     }
883
884     &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
885 }
886
887 sub getHostMask {
888     my($n) = @_;
889
890     if (exists $nuh{$n}) {
891         return &makeHostMask($nuh{$n});
892     } else {
893         $cache{on_who_Hack} = 1;
894         &rawout("WHO $n");
895     }
896 }
897
898 1;