]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/Irc.pl
print possible fix as error message.
[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 use strict;
9 no strict 'refs';
10
11 use vars qw(%floodjoin %nuh %dcc %cache %channels %param %mask
12         %chanconf %orig %ircPort %ircstats %last %netsplit);
13 use vars qw($irc $nickserv $ident $conn $msgType $who $talkchannel
14         $addressed);
15 use vars qw($notcount $nottime $notsize $msgcount $msgtime $msgsize
16                 $pubcount $pubtime $pubsize);
17 use vars qw($b_blue $ob);
18 use vars qw(@joinchan @ircServers);
19
20 $nickserv       = 0;
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: connection failed.");
106         &ERROR("Please check /etc/hosts to see if you have a localhost line like:");
107         &ERROR("127.0.0.1   localhost    localhost");
108         &ERROR("If this is still a problem, please contact the maintainer.");
109         return 1;
110     }
111
112     &clearIRCVars();
113
114     # change internal timeout value for scheduler.
115     $irc->{_timeout}    = 10;   # how about 60?
116     # Net::IRC debugging.
117     $irc->{_debug}      = 1;
118
119     $ircstats{'Server'} = "$server:$port";
120
121     # handler stuff.
122         $conn->add_handler('caction',   \&on_action);
123         $conn->add_handler('cdcc',      \&on_dcc);
124         $conn->add_handler('cping',     \&on_ping);
125         $conn->add_handler('crping',    \&on_ping_reply);
126         $conn->add_handler('cversion',  \&on_version);
127         $conn->add_handler('crversion', \&on_crversion);
128         $conn->add_handler('dcc_open',  \&on_dcc_open);
129         $conn->add_handler('dcc_close', \&on_dcc_close);
130         $conn->add_handler('chat',      \&on_chat);
131         $conn->add_handler('msg',       \&on_msg);
132         $conn->add_handler('public',    \&on_public);
133         $conn->add_handler('join',      \&on_join);
134         $conn->add_handler('part',      \&on_part);
135         $conn->add_handler('topic',     \&on_topic);
136         $conn->add_handler('invite',    \&on_invite);
137         $conn->add_handler('kick',      \&on_kick);
138         $conn->add_handler('mode',      \&on_mode);
139         $conn->add_handler('nick',      \&on_nick);
140         $conn->add_handler('quit',      \&on_quit);
141         $conn->add_handler('notice',    \&on_notice);
142         $conn->add_handler('whoischannels', \&on_whoischannels);
143         $conn->add_handler('useronchannel', \&on_useronchannel);
144         $conn->add_handler('whois',     \&on_whois);
145         $conn->add_handler('other',     \&on_other);
146         $conn->add_global_handler('disconnect', \&on_disconnect);
147         $conn->add_global_handler([251,252,253,254,255], \&on_init);
148 ###     $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
149         $conn->add_global_handler(303, \&on_ison); # notify.
150         $conn->add_global_handler(315, \&on_endofwho);
151         $conn->add_global_handler(422, \&on_endofwho); # nomotd.
152         $conn->add_global_handler(324, \&on_modeis);
153         $conn->add_global_handler(333, \&on_topicinfo);
154         $conn->add_global_handler(352, \&on_who);
155         $conn->add_global_handler(353, \&on_names);
156         $conn->add_global_handler(366, \&on_endofnames);
157         $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
158         $conn->add_global_handler(433, \&on_nick_taken);
159         $conn->add_global_handler(439, \&on_targettoofast);
160         # for proper joinnextChan behaviour
161         $conn->add_global_handler(471, \&on_chanfull);
162         $conn->add_global_handler(473, \&on_inviteonly);
163         $conn->add_global_handler(474, \&on_banned);
164         $conn->add_global_handler(475, \&on_badchankey);
165         $conn->add_global_handler(443, \&on_useronchan);
166
167     # end of handler stuff.
168
169     $irc->start;
170 }
171
172 ######################################################################
173 ######## IRC ALIASES   IRC ALIASES   IRC ALIASES   IRC ALIASES #######
174 ######################################################################
175
176 sub rawout {
177     my ($buf) = @_;
178     $buf =~ s/\n//gi;
179
180     # slow down a bit if traffic is "high".
181     # need to take into account time of last message sent.
182     if ($last{buflen} > 256 and length($buf) > 256) {
183         sleep 1;
184     }
185
186     $conn->sl($buf) if (&whatInterface() =~ /IRC/);
187
188     $last{buflen} = length($buf);
189 }
190
191 sub say {
192     my ($msg) = @_;
193     if (!defined $msg) {
194         $msg ||= "NULL";
195         &WARN("say: msg == $msg.");
196         return;
197     }
198
199     &status("</$talkchannel> $msg");
200     if (&whatInterface() =~ /IRC/) {
201         $msg    = "zero" if ($msg =~ /^0+$/);
202         my $t   = time();
203
204         if ($t == $pubtime) {
205             $pubcount++;
206             $pubsize += length $msg;
207
208             my $i = &getChanConfDefault("sendPublicLimitLines", 3);
209             my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
210
211             if ( ($pubcount % $i) == 0 and $pubcount) {
212                 sleep 1;
213             } elsif ($pubsize > $j) {
214                 sleep 1;
215                 $pubsize -= $j;
216             }
217
218         } else {
219             $pubcount   = 0;
220             $pubtime    = $t;
221             $pubsize    = length $msg;
222         }
223
224         $conn->privmsg($talkchannel, $msg);
225     }
226 }
227
228 sub msg {
229     my ($nick, $msg) = @_;
230     if (!defined $nick) {
231         &ERROR("msg: nick == NULL.");
232         return;
233     }
234
235     if (!defined $msg) {
236         $msg ||= "NULL";
237         &WARN("msg: msg == $msg.");
238         return;
239     }
240
241     &status(">$nick< $msg");
242
243     return unless (&whatInterface() =~ /IRC/);
244     my $t = time();
245
246     if ($t == $msgtime) {
247         $msgcount++;
248         $msgsize += length $msg;
249
250         my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
251         my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
252         if ( ($msgcount % $i) == 0 and $msgcount) {
253             sleep 1;
254         } elsif ($msgsize > $j) {
255             sleep 1;
256             $msgsize -= $j;
257         }
258
259     } else {
260         $msgcount       = 0;
261         $msgtime        = $t;
262         $msgsize        = length $msg;
263     }
264
265     $conn->privmsg($nick, $msg);
266 }
267
268 # Usage: &action(nick || chan, txt);
269 sub action {
270     my ($target, $txt) = @_;
271     if (!defined $txt) {
272         &WARN("action: txt == NULL.");
273         return;
274     }
275
276     if (length $txt > 480) {
277         &status("action: txt too long; truncating.");
278         chop($txt) while (length $txt > 480);
279     }
280
281     &status("* $ident/$target $txt");
282     $conn->me($target, $txt);
283 }
284
285 # Usage: &notice(nick || chan, txt);
286 sub notice {
287     my ($target, $txt) = @_;
288     if (!defined $txt) {
289         &WARN("notice: txt == NULL.");
290         return;
291     }
292
293     &status("-$target- $txt");
294
295     my $t       = time();
296
297     if ($t == $nottime) {
298         $notcount++;
299         $notsize += length $txt;
300
301         my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
302         my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
303
304         if ( ($notcount % $i) == 0 and $notcount) {
305             sleep 1;
306         } elsif ($notsize > $j) {
307             sleep 1;
308             $notsize -= $j;
309         }
310
311     } else {
312         $notcount       = 0;
313         $nottime        = $t;
314         $notsize        = length $txt;
315     }
316
317     $conn->notice($target, $txt);
318 }
319
320 sub DCCBroadcast {
321     my ($txt,$flag) = @_;
322
323     ### FIXME: flag not supported yet.
324
325     foreach (keys %{ $dcc{'CHAT'} }) {
326         $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
327     }
328 }
329
330 ##########
331 ### perform commands.
332 ###
333
334 # Usage: &performReply($reply);
335 sub performReply {
336     my ($reply) = @_;
337
338     if (!defined $reply or $reply =~ /^\s*$/) {
339         &DEBUG("performReply: reply == NULL.");
340         return;
341     }
342
343     $reply =~ /([\.\?\s]+)$/;
344
345     &checkMsgType($reply);
346
347     if ($msgType eq 'public') {
348         if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
349             $reply = "$orig{who}: ".$reply;
350         } else {
351             $reply = "$reply, ".$orig{who};
352         }
353         &say($reply);
354
355     } elsif ($msgType eq 'private') {
356         if (rand() > 0.5) {
357             $reply = "$reply, ".$orig{who};
358         }
359         &msg($who, $reply);
360
361     } elsif ($msgType eq 'chat') {
362         if (!exists $dcc{'CHAT'}{$who}) {
363             &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
364             return;
365         }
366         $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
367
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 ($chan)  = @_;
432     my $key     = &getChanConf("chankey", $chan) || "";
433
434     # forgot for about 2 years to implement channel keys when moving
435     # over to Net::IRC...
436
437     # hopefully validChan is right.
438     if (&validChan($chan)) {
439         &status("join: already on $chan");
440     } else {
441         &status("joining $b_blue$chan$ob");
442
443         return if ($conn->join($chan, $key));
444
445         &DEBUG("joinchan: join failed. trying connect!");
446         &clearIRCVars();
447         $conn->connect();
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         $conn->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     # should move to use Net::IRC's $conn->mode()... but too lazy.
486     rawout("MODE $chan $modes");
487 }
488
489 sub op {
490     my ($chan, @who) = @_;
491     my $os      = "o" x scalar(@who);
492
493     &mode($chan, "+$os @who");
494 }
495
496 sub deop {
497     my ($chan, @who) = @_;
498     my $os = "o" x scalar(@who);
499
500     &mode($chan, "-$os ".@who);
501 }
502
503 sub kick {
504     my ($nick,$chan,$msg) = @_;
505     my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
506
507     if ($chan ne "" and &validChan($chan) == 0) {
508         &ERROR("kick: invalid channel $chan.");
509         return;
510     }
511
512     $nick =~ tr/A-Z/a-z/;
513
514     foreach $chan (@chans) {
515         if (!&IsNickInChan($nick,$chan)) {
516             &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
517             next;
518         }
519
520         if (!exists $channels{$chan}{o}{$ident}) {
521             &status("kick: do not have ops on $chan :(");
522             next;
523         }
524
525         &status("Kicking $nick from $chan.");
526         $conn->kick($chan, $nick, $msg);
527     }
528 }
529
530 sub ban {
531     my ($mask,$chan) = @_;
532     my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
533     my $ban     = 0;
534
535     if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
536         &ERROR("ban: invalid channel $chan.");
537         return;
538     }
539
540     foreach $chan (@chans) {
541         if (!exists $channels{$chan}{o}{$ident}) {
542             &status("ban: do not have ops on $chan :(");
543             next;
544         }
545
546         &status("Banning $mask from $chan.");
547         &rawout("MODE $chan +b $mask");
548         $ban++;
549     }
550
551     return $ban;
552 }
553
554 sub unban {
555     my ($mask,$chan) = @_;
556     my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
557     my $ban     = 0;
558
559     &DEBUG("unban: mask = $mask, chan = @chans");
560
561     foreach $chan (@chans) {
562         if (!exists $channels{$chan}{o}{$ident}) {
563             &status("unBan: do not have ops on $chan :(");
564             next;
565         }
566
567         &status("Removed ban $mask from $chan.");
568         &rawout("MODE $chan -b $mask");
569         $ban++;
570     }
571
572     return $ban;
573 }
574
575 sub quit {
576     my ($quitmsg) = @_;
577     &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
578     if (defined $conn) {
579         $conn->quit($quitmsg);
580     } else {
581         &WARN("quit: could not quit!");
582     }
583 }
584
585 sub nick {
586     my ($nick) = @_;
587
588     if (!defined $nick) {
589         &ERROR("nick: nick == NULL.");
590         return;
591     }
592
593     if (defined $ident and $nick eq $ident) {
594         &WARN("nick: nick == ident == '$ident'.");
595         return;
596     }
597
598     my $bad     = 0;
599     $bad++ if (exists $nuh{ $param{'ircNick'} });
600     $bad++ if (&IsNickInAnyChan($param{'ircNick'}));
601
602     if ($bad) {
603         &WARN("Nick: not going to try and get my nick back. [".
604                 scalar(gmtime). "]");
605 # hrm... over time we lose track of our own nick.
606 #       return;
607     }
608
609     if ($nick =~ /^$mask{nick}$/) {
610         &rawout("NICK ".$nick);
611
612         if (defined $ident) {
613             &status("nick: Changing nick to $nick (from $ident)");
614             # following shouldn't be here :(
615             $ident      = $nick;
616         } else {
617             &DEBUG("first time nick change.");
618             $ident      = $nick;
619         }
620
621         return 1;
622     }
623     &DEBUG("nick: failed... why oh why (nick => $nick)");
624
625     return 0;
626 }
627
628 sub invite {
629     my($who, $chan) = @_;
630     # todo: check if $who or $chan are invalid.
631
632     $conn->invite($who, $chan);
633 }
634
635 ##########
636 # Channel related functions...
637 #
638
639 # Usage: &joinNextChan();
640 sub joinNextChan {
641     if (scalar @joinchan) {
642         my $chan = shift @joinchan;
643         &joinchan($chan);
644
645         if (my $i = scalar @joinchan) {
646             &status("joinNextChan: $i chans to join.");
647         }
648
649         return;
650     }
651
652     # !scalar @joinchan:
653     my @c       = &getJoinChans();
654     if (exists $cache{joinTime} and scalar @c) {
655         my $delta       = time() - $cache{joinTime} - 5;
656         my $timestr     = &Time2String($delta);
657         my $rate        = sprintf("%.1f", $delta / @c);
658         delete $cache{joinTime};
659
660         &status("time taken to join all chans: $timestr; rate: $rate sec/join");
661     }
662
663     # chanserv check: global channels, in case we missed one.
664     foreach ( &ChanConfList("chanServ_ops") ) {
665         &chanServCheck($_);
666     }
667 }
668
669 # Usage: &getNickInChans($nick);
670 sub getNickInChans {
671     my ($nick) = @_;
672     my @array;
673
674     foreach (keys %channels) {
675         next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
676         push(@array, $_);
677     }
678
679     return @array;
680 }
681
682 # Usage: &getNicksInChan($chan);
683 sub getNicksInChan {
684     my ($chan) = @_;
685     my @array;
686
687     return keys %{ $channels{$chan}{''} };
688 }
689
690 sub IsNickInChan {
691     my ($nick,$chan) = @_;
692
693     $chan =~ tr/A-Z/a-z/;       # not lowercase unfortunately.
694
695     if ($chan =~ /^$/) {
696         &DEBUG("INIC: chan == NULL.");
697         return 0;
698     }
699
700     if (&validChan($chan) == 0) {
701         &ERROR("INIC: invalid channel $chan.");
702         return 0;
703     }
704
705     if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
706         return 1;
707     } else {
708         foreach (keys %channels) {
709             next unless (/[A-Z]/);
710             &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
711         }
712         return 0;
713     }
714 }
715
716 sub IsNickInAnyChan {
717     my ($nick) = @_;
718     my $chan;
719
720     foreach $chan (keys %channels) {
721         next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''}  });
722         return 1;
723     }
724     return 0;
725 }
726
727 # Usage: &validChan($chan);
728 sub validChan {
729     # todo: use $c instead?
730     my ($chan) = @_;
731
732     if (!defined $chan or $chan =~ /^\s*$/) {
733         return 0;
734     }
735
736     if (lc $chan ne $chan) {
737         &WARN("validChan: lc chan != chan. ($chan); fixing.");
738         $chan =~ tr/A-Z/a-z/;
739     }
740
741     # it's possible that this check creates the hash if empty.
742     if (defined $channels{$chan} or exists $channels{$chan}) {
743         if ($chan =~ /^_?default$/) {
744 #           &WARN("validC: chan cannot be _default! returning 0!");
745             return 0;
746         }
747
748         return 1;
749     } else {
750         return 0;
751     }
752 }
753
754 ###
755 # Usage: &delUserInfo($nick,@chans);
756 sub delUserInfo {
757     my ($nick,@chans) = @_;
758     my ($mode,$chan);
759
760     foreach $chan (@chans) {
761         foreach $mode (keys %{ $channels{$chan} }) {
762             # use grep here?
763             next unless (exists $channels{$chan}{$mode}{$nick});
764
765             delete $channels{$chan}{$mode}{$nick};
766         }
767     }
768 }
769
770 sub clearChanVars {
771     my ($chan) = @_;
772
773     delete $channels{$chan};
774 }
775
776 sub clearIRCVars {
777     undef %channels;
778     undef %floodjoin;
779
780     @joinchan           = &getJoinChans(1);
781     $cache{joinTime}    = time();
782 }
783
784 sub getJoinChans {
785     my($show)   = @_;
786     my @chans;
787     my @skip;
788
789     foreach (keys %chanconf) {
790         next if ($_ eq "_default");
791
792         my $val = $chanconf{$_}{autojoin};
793         my $skip = 0;
794
795         if (defined $val) {
796             $skip++ if ($val eq "0");
797         } else {
798             $skip++;
799         }
800
801         if ($skip) {
802             push(@skip, $_);
803             next;
804         }
805
806         push(@chans, $_);
807     }
808
809     my $str;
810     if (scalar @skip) {
811         $str = "channels not auto-joining: @skip (joining: @chans)";
812     } else {
813         $str = "auto-joining all chans: @chans";
814     }
815
816     &status("Chans: ".$str) if ($show);
817
818     return @chans;
819 }
820
821 sub closeDCC {
822 #    &DEBUG("closeDCC called.");
823     my $type;
824
825     foreach $type (keys %dcc) {
826         next if ($type ne uc($type));
827  
828         my $nick;
829         foreach $nick (keys %{ $dcc{$type} }) {
830             next unless (defined $nick);
831             &status("DCC CHAT: closing DCC $type to $nick.");
832             next unless (defined $dcc{$type}{$nick});
833
834             my $ref = $dcc{$type}{$nick};
835             &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
836             $dcc{$type}{$nick}->close();
837             delete $dcc{$type}{$nick};
838             &DEBUG("after close for $nick");
839         }
840         delete $dcc{$type};
841     }
842 }
843
844 sub joinfloodCheck {
845     my($who,$chan,$userhost) = @_;
846
847     return unless (&IsChanConf("joinfloodCheck"));
848
849     if (exists $netsplit{lc $who}) {    # netsplit join.
850         &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
851     }
852
853     if (exists $floodjoin{$chan}{$who}{Time}) {
854         &WARN("floodjoin{$chan}{$who} already exists?");
855     }
856
857     $floodjoin{$chan}{$who}{Time} = time();
858     $floodjoin{$chan}{$who}{Host} = $userhost;
859
860     ### Check...
861     foreach (keys %floodjoin) {
862         my $c = $_;
863         my $count = scalar keys %{ $floodjoin{$c} };
864         next unless ($count > 5);
865         &DEBUG("joinflood: count => $count");
866
867         my $time;
868         foreach (keys %{ $floodjoin{$c} }) {
869             my $t = $floodjoin{$c}{$_}{Time};
870             next unless (defined $t);
871
872             $time += $t;
873         }
874         &DEBUG("joinflood: time => $time");
875         $time /= $count;
876
877         &DEBUG("joinflood: new time => $time");
878     }
879
880     ### Clean it up.
881     my $delete = 0;
882     my $time = time();
883     foreach $chan (keys %floodjoin) {
884         foreach $who (keys %{ $floodjoin{$chan} }) {
885             my $t       = $floodjoin{$chan}{$who}{Time};
886             next unless (defined $t);
887
888             my $delta   = $time - $t;
889             next unless ($delta > 10);
890
891             delete $floodjoin{$chan}{$who};
892             $delete++;
893         }
894     }
895
896     &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
897 }
898
899 sub getHostMask {
900     my($n) = @_;
901
902     if (exists $nuh{$n}) {
903         return &makeHostMask($nuh{$n});
904     } else {
905         $cache{on_who_Hack} = 1;
906         $conn->who($n);
907     }
908 }
909
910 1;