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