]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/Irc.pl
- simonrvn found that .say is broken - msg() fixed ;)
[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 # static scalar variables.
21 $mask{ip}       = '(\d+)\.(\d+)\.(\d+)\.(\d+)';
22 $mask{host}     = '[\d\w\_\-\/]+\.[\.\d\w\_\-\/]+';
23 $mask{chan}     = '[\#\&]\S*|_default';
24 my $isnick1     = 'a-zA-Z\[\]\{\}\_\`\^\|\\\\';
25 my $isnick2     = '0-9\-';
26 $mask{nick}     = "[$isnick1]{1}[$isnick1$isnick2]*";
27 $mask{nuh}      = '\S*!\S*\@\S*';
28
29 $nickserv       = 0;
30
31 sub ircloop {
32     my $error   = 0;
33     my $lastrun = 0;
34
35 loop:;
36     while (my $host = shift @ircServers) {
37         # JUST IN CASE. irq was complaining about this.
38         if ($lastrun == time()) {
39             &DEBUG("ircloop: hrm... lastrun == time()");
40             $error++;
41             sleep 10;
42             next;
43         }
44
45         if (!defined $host) {
46             &DEBUG("ircloop: ircServers[x] = NULL.");
47             $lastrun = time();
48             next;
49         }
50         next unless (exists $ircPort{$host});
51
52         my $retval      = &irc($host, $ircPort{$host});
53         next unless (defined $retval and $retval == 0);
54         $error++;
55
56         if ($error % 3 == 0 and $error != 0) {
57             &status("IRC: Could not connect.");
58             &status("IRC: ");
59             next;
60         }
61
62         if ($error >= 3*2) {
63             &status("IRC: cannot connect to any IRC servers; stopping.");
64             &shutdown();
65             exit 1;
66         }
67     }
68
69     &status("IRC: ok, done one cycle of IRC servers; trying again.");
70
71     &loadIRCServers();
72     goto loop;
73 }
74
75 sub irc {
76     my ($server,$port) = @_;
77
78     my $iaddr = inet_aton($server);
79     my $paddr = sockaddr_in($port, $iaddr);
80     my $proto = getprotobyname('tcp');
81
82     select STDOUT;
83     &status("Connecting to port $port of server $server ...");
84
85     # host->ip.
86     if ($server =~ /\D$/) {
87         my $packed = scalar(gethostbyname($server));
88
89         if (!defined $packed) {
90             &status("  cannot resolve $server.");
91             return 0;
92         }
93
94         my $resolve = inet_ntoa($packed);
95         &status("  resolved to $resolve.");
96         ### warning in Sys/Hostname line 78???
97         ### caused inside Net::IRC?
98     }
99
100     $irc = new Net::IRC;
101
102     my %args = (
103                 Nick    => $param{'ircNick'},
104                 Server  => $server,
105                 Port    => $port,
106                 Ircname => $param{'ircName'},
107     );
108     $args{'LocalAddr'} = $param{'ircHost'} if ($param{'ircHost'});
109     $args{'Password'} = $param{'ircPasswd'} if ($param{'ircPasswd'});
110
111     $conn = $irc->newconn(%args);
112
113     if (!defined $conn) {
114         &ERROR("internal: perl IRC connection object does not exist.");
115         return 1;
116     }
117
118     &clearIRCVars();
119
120     # change internal timeout value for scheduler.
121     $irc->{_timeout}    = 10;   # how about 60?
122     # Net::IRC debugging.
123     $irc->{_debug}      = 1;
124
125     $ircstats{'Server'} = "$server:$port";
126
127     # handler stuff.
128         $conn->add_handler('caction',   \&on_action);
129         $conn->add_handler('cdcc',      \&on_dcc);
130         $conn->add_handler('cping',     \&on_ping);
131         $conn->add_handler('crping',    \&on_ping_reply);
132         $conn->add_handler('cversion',  \&on_version);
133         $conn->add_handler('crversion', \&on_crversion);
134         $conn->add_handler('dcc_open',  \&on_dcc_open);
135         $conn->add_handler('dcc_close', \&on_dcc_close);
136         $conn->add_handler('chat',      \&on_chat);
137         $conn->add_handler('msg',       \&on_msg);
138         $conn->add_handler('public',    \&on_public);
139         $conn->add_handler('join',      \&on_join);
140         $conn->add_handler('part',      \&on_part);
141         $conn->add_handler('topic',     \&on_topic);
142         $conn->add_handler('invite',    \&on_invite);
143         $conn->add_handler('kick',      \&on_kick);
144         $conn->add_handler('mode',      \&on_mode);
145         $conn->add_handler('nick',      \&on_nick);
146         $conn->add_handler('quit',      \&on_quit);
147         $conn->add_handler('notice',    \&on_notice);
148         $conn->add_handler('whoischannels', \&on_whoischannels);
149         $conn->add_handler('useronchannel', \&on_useronchannel);
150         $conn->add_handler('whois',     \&on_whois);
151         $conn->add_handler('other',     \&on_other);
152         $conn->add_global_handler('disconnect', \&on_disconnect);
153         $conn->add_global_handler([251,252,253,254,255], \&on_init);
154 ###     $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
155         $conn->add_global_handler(303, \&on_ison); # notify.
156         $conn->add_global_handler(315, \&on_endofwho);
157         $conn->add_global_handler(422, \&on_endofwho); # nomotd.
158         $conn->add_global_handler(324, \&on_modeis);
159         $conn->add_global_handler(333, \&on_topicinfo);
160         $conn->add_global_handler(352, \&on_who);
161         $conn->add_global_handler(353, \&on_names);
162         $conn->add_global_handler(366, \&on_endofnames);
163         $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
164         $conn->add_global_handler(433, \&on_nick_taken);
165         $conn->add_global_handler(439, \&on_targettoofast);
166         # for proper joinnextChan behaviour
167         $conn->add_global_handler(471, \&on_chanfull);
168         $conn->add_global_handler(473, \&on_inviteonly);
169         $conn->add_global_handler(474, \&on_banned);
170         $conn->add_global_handler(475, \&on_badchankey);
171         $conn->add_global_handler(443, \&on_useronchan);
172
173     # end of handler stuff.
174
175     $irc->start;
176 }
177
178 ######################################################################
179 ######## IRC ALIASES   IRC ALIASES   IRC ALIASES   IRC ALIASES #######
180 ######################################################################
181
182 sub rawout {
183     my ($buf) = @_;
184     $buf =~ s/\n//gi;
185
186     # slow down a bit if traffic is "high".
187     # need to take into account time of last message sent.
188     if ($last{buflen} > 256 and length($buf) > 256) {
189         sleep 1;
190     }
191
192     $conn->sl($buf) if (&whatInterface() =~ /IRC/);
193
194     $last{buflen} = length($buf);
195 }
196
197 sub say {
198     my ($msg) = @_;
199     if (!defined $msg) {
200         $msg ||= "NULL";
201         &WARN("say: msg == $msg.");
202         return;
203     }
204
205     &status("</$talkchannel> $msg");
206     if (&whatInterface() =~ /IRC/) {
207         $msg    = "zero" if ($msg =~ /^0+$/);
208         my $t   = time();
209
210         if ($t == $pubtime) {
211             $pubcount++;
212             $pubsize += length $msg;
213
214             my $i = &getChanConfDefault("sendPublicLimitLines", 3);
215             my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
216
217             if ( ($pubcount % $i) == 0 and $pubcount) {
218                 sleep 1;
219             } elsif ($pubsize > $j) {
220                 sleep 1;
221                 $pubsize -= $j;
222             }
223
224         } else {
225             $pubcount   = 0;
226             $pubtime    = $t;
227             $pubsize    = length $msg;
228         }
229
230         $conn->privmsg($talkchannel, $msg);
231     }
232 }
233
234 sub msg {
235     my ($nick, $msg) = @_;
236     if (!defined $nick) {
237         &ERROR("msg: nick == NULL.");
238         return;
239     }
240
241     if (!defined $msg) {
242         $msg ||= "NULL";
243         &WARN("msg: msg == $msg.");
244         return;
245     }
246
247     &status(">$nick< $msg");
248
249     return unless (&whatInterface() =~ /IRC/);
250     my $t = time();
251
252     if ($t == $msgtime) {
253         $msgcount++;
254         $msgsize += length $msg;
255
256         my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
257         my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
258         if ( ($msgcount % $i) == 0 and $msgcount) {
259             sleep 1;
260         } elsif ($msgsize > $j) {
261             sleep 1;
262             $msgsize -= $j;
263         }
264
265     } else {
266         $msgcount       = 0;
267         $msgtime        = $t;
268         $msgsize        = length $msg;
269     }
270
271     $conn->privmsg($nick, $msg);
272 }
273
274 # Usage: &action(nick || chan, txt);
275 sub action {
276     my ($target, $txt) = @_;
277     if (!defined $txt) {
278         &WARN("action: txt == NULL.");
279         return;
280     }
281
282     if (length $txt > 480) {
283         &status("action: txt too long; truncating.");
284         chop($txt) while (length $txt > 480);
285     }
286
287     &status("* $ident/$target $txt");
288     $conn->me($target, $txt);
289 }
290
291 # Usage: &notice(nick || chan, txt);
292 sub notice {
293     my ($target, $txt) = @_;
294     if (!defined $txt) {
295         &WARN("notice: txt == NULL.");
296         return;
297     }
298
299     &status("-$target- $txt");
300
301     my $t       = time();
302
303     if ($t == $nottime) {
304         $notcount++;
305         $notsize += length $txt;
306
307         my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
308         my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
309
310         if ( ($notcount % $i) == 0 and $notcount) {
311             sleep 1;
312         } elsif ($notsize > $j) {
313             sleep 1;
314             $notsize -= $j;
315         }
316
317     } else {
318         $notcount       = 0;
319         $nottime        = $t;
320         $notsize        = length $txt;
321     }
322
323     $conn->notice($target, $txt);
324 }
325
326 sub DCCBroadcast {
327     my ($txt,$flag) = @_;
328
329     ### FIXME: flag not supported yet.
330
331     foreach (keys %{ $dcc{'CHAT'} }) {
332         $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
333     }
334 }
335
336 ##########
337 ### perform commands.
338 ###
339
340 # Usage: &performReply($reply);
341 sub performReply {
342     my ($reply) = @_;
343
344     if (!defined $reply or $reply =~ /^\s*$/) {
345         &DEBUG("performReply: reply == NULL.");
346         return;
347     }
348
349     $reply =~ /([\.\?\s]+)$/;
350
351     &checkMsgType($reply);
352
353     if ($msgType eq 'public') {
354         if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
355             $reply = "$orig{who}: ".$reply;
356         } else {
357             $reply = "$reply, ".$orig{who};
358         }
359         &say($reply);
360
361     } elsif ($msgType eq 'private') {
362         if (rand() > 0.5) {
363             $reply = "$reply, ".$orig{who};
364         }
365         &msg($who, $reply);
366
367     } elsif ($msgType eq 'chat') {
368         if (!exists $dcc{'CHAT'}{$who}) {
369             &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
370             return;
371         }
372         $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
373
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         return;
602     }
603
604     my $bad     = 0;
605     $bad++ if (exists $nuh{ $param{'ircNick'} });
606     $bad++ if (&IsNickInAnyChan($param{'ircNick'}));
607
608     if ($bad) {
609         &WARN("Nick: not going to try and get my nick back. [".
610                 scalar(gmtime). "]");
611 # hrm... over time we lose track of our own nick.
612 #       return;
613     }
614
615     if ($nick =~ /^$mask{nick}$/) {
616         &rawout("NICK ".$nick);
617
618         if (defined $ident) {
619             &status("nick: Changing nick to $nick (from $ident)");
620             # following shouldn't be here :(
621             $ident      = $nick;
622         } else {
623             &DEBUG("first time nick change.");
624             $ident      = $nick;
625         }
626
627         return 1;
628     }
629     &DEBUG("nick: failed... why oh why (nick => $nick)");
630
631     return 0;
632 }
633
634 sub invite {
635     my($who, $chan) = @_;
636     # todo: check if $who or $chan are invalid.
637
638     $conn->invite($who, $chan);
639 }
640
641 ##########
642 # Channel related functions...
643 #
644
645 # Usage: &joinNextChan();
646 sub joinNextChan {
647     if (scalar @joinchan) {
648         my $chan = shift @joinchan;
649         &joinchan($chan);
650
651         if (my $i = scalar @joinchan) {
652             &status("joinNextChan: $i chans to join.");
653         }
654
655         return;
656     }
657
658     # !scalar @joinchan:
659     my @c       = &getJoinChans();
660     if (exists $cache{joinTime} and scalar @c) {
661         my $delta       = time() - $cache{joinTime} - 5;
662         my $timestr     = &Time2String($delta);
663         my $rate        = sprintf("%.1f", $delta / @c);
664         delete $cache{joinTime};
665
666         &status("time taken to join all chans: $timestr; rate: $rate sec/join");
667     }
668
669     # chanserv check: global channels, in case we missed one.
670     foreach ( &ChanConfList("chanServ_ops") ) {
671         &chanServCheck($_);
672     }
673 }
674
675 # Usage: &getNickInChans($nick);
676 sub getNickInChans {
677     my ($nick) = @_;
678     my @array;
679
680     foreach (keys %channels) {
681         next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
682         push(@array, $_);
683     }
684
685     return @array;
686 }
687
688 # Usage: &getNicksInChan($chan);
689 sub getNicksInChan {
690     my ($chan) = @_;
691     my @array;
692
693     return keys %{ $channels{$chan}{''} };
694 }
695
696 sub IsNickInChan {
697     my ($nick,$chan) = @_;
698
699     $chan =~ tr/A-Z/a-z/;       # not lowercase unfortunately.
700
701     if ($chan =~ /^$/) {
702         &DEBUG("INIC: chan == NULL.");
703         return 0;
704     }
705
706     if (&validChan($chan) == 0) {
707         &ERROR("INIC: invalid channel $chan.");
708         return 0;
709     }
710
711     if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
712         return 1;
713     } else {
714         foreach (keys %channels) {
715             next unless (/[A-Z]/);
716             &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
717         }
718         return 0;
719     }
720 }
721
722 sub IsNickInAnyChan {
723     my ($nick) = @_;
724     my $chan;
725
726     foreach $chan (keys %channels) {
727         next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''}  });
728         return 1;
729     }
730     return 0;
731 }
732
733 # Usage: &validChan($chan);
734 sub validChan {
735     # todo: use $c instead?
736     my ($chan) = @_;
737
738     if (!defined $chan or $chan =~ /^\s*$/) {
739         return 0;
740     }
741
742     if (lc $chan ne $chan) {
743         &WARN("validChan: lc chan != chan. ($chan); fixing.");
744         $chan =~ tr/A-Z/a-z/;
745     }
746
747     # it's possible that this check creates the hash if empty.
748     if (defined $channels{$chan} or exists $channels{$chan}) {
749         if ($chan =~ /^_?default$/) {
750 #           &WARN("validC: chan cannot be _default! returning 0!");
751             return 0;
752         }
753
754         return 1;
755     } else {
756         return 0;
757     }
758 }
759
760 ###
761 # Usage: &delUserInfo($nick,@chans);
762 sub delUserInfo {
763     my ($nick,@chans) = @_;
764     my ($mode,$chan);
765
766     foreach $chan (@chans) {
767         foreach $mode (keys %{ $channels{$chan} }) {
768             # use grep here?
769             next unless (exists $channels{$chan}{$mode}{$nick});
770
771             delete $channels{$chan}{$mode}{$nick};
772         }
773     }
774 }
775
776 sub clearChanVars {
777     my ($chan) = @_;
778
779     delete $channels{$chan};
780 }
781
782 sub clearIRCVars {
783     undef %channels;
784     undef %floodjoin;
785
786     @joinchan           = &getJoinChans(1);
787     $cache{joinTime}    = time();
788 }
789
790 sub getJoinChans {
791     my($show)   = @_;
792     my @chans;
793     my @skip;
794
795     foreach (keys %chanconf) {
796         next if ($_ eq "_default");
797
798         my $val = $chanconf{$_}{autojoin};
799         my $skip = 0;
800
801         if (defined $val) {
802             $skip++ if ($val eq "0");
803         } else {
804             $skip++;
805         }
806
807         if ($skip) {
808             push(@skip, $_);
809             next;
810         }
811
812         push(@chans, $_);
813     }
814
815     my $str;
816     if (scalar @skip) {
817         $str = "channels not auto-joining: @skip (joining: @chans)";
818     } else {
819         $str = "auto-joining all chans: @chans";
820     }
821
822     &status("Chans: ".$str) if ($show);
823
824     return @chans;
825 }
826
827 sub closeDCC {
828 #    &DEBUG("closeDCC called.");
829     my $type;
830
831     foreach $type (keys %dcc) {
832         next if ($type ne uc($type));
833  
834         my $nick;
835         foreach $nick (keys %{ $dcc{$type} }) {
836             next unless (defined $nick);
837             &status("DCC CHAT: closing DCC $type to $nick.");
838             next unless (defined $dcc{$type}{$nick});
839
840             my $ref = $dcc{$type}{$nick};
841             &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
842             $dcc{$type}{$nick}->close();
843             delete $dcc{$type}{$nick};
844             &DEBUG("after close for $nick");
845         }
846         delete $dcc{$type};
847     }
848 }
849
850 sub joinfloodCheck {
851     my($who,$chan,$userhost) = @_;
852
853     return unless (&IsChanConf("joinfloodCheck"));
854
855     if (exists $netsplit{lc $who}) {    # netsplit join.
856         &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
857     }
858
859     if (exists $floodjoin{$chan}{$who}{Time}) {
860         &WARN("floodjoin{$chan}{$who} already exists?");
861     }
862
863     $floodjoin{$chan}{$who}{Time} = time();
864     $floodjoin{$chan}{$who}{Host} = $userhost;
865
866     ### Check...
867     foreach (keys %floodjoin) {
868         my $c = $_;
869         my $count = scalar keys %{ $floodjoin{$c} };
870         next unless ($count > 5);
871         &DEBUG("joinflood: count => $count");
872
873         my $time;
874         foreach (keys %{ $floodjoin{$c} }) {
875             my $t = $floodjoin{$c}{$_}{Time};
876             next unless (defined $t);
877
878             $time += $t;
879         }
880         &DEBUG("joinflood: time => $time");
881         $time /= $count;
882
883         &DEBUG("joinflood: new time => $time");
884     }
885
886     ### Clean it up.
887     my $delete = 0;
888     my $time = time();
889     foreach $chan (keys %floodjoin) {
890         foreach $who (keys %{ $floodjoin{$chan} }) {
891             my $t       = $floodjoin{$chan}{$who}{Time};
892             next unless (defined $t);
893
894             my $delta   = $time - $t;
895             next unless ($delta > 10);
896
897             delete $floodjoin{$chan}{$who};
898             $delete++;
899         }
900     }
901
902     &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
903 }
904
905 sub getHostMask {
906     my($n) = @_;
907
908     if (exists $nuh{$n}) {
909         return &makeHostMask($nuh{$n});
910     } else {
911         $cache{on_who_Hack} = 1;
912         $conn->who($n);
913     }
914 }
915
916 1;