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