]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/Irc.pl
join chan mangling for multiple nicks
[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(@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     }
449     #} else {
450         &status("joining $b_blue$chan$ob");
451
452         return if ($conn->join($chan, $key));
453         return if (&validChan($chan));
454
455         &DEBUG("joinchan: join failed. trying connect!");
456         &clearIRCVars();
457         $conn->connect();
458     #}
459 }
460
461 sub part {
462     my $chan;
463
464     foreach $chan (@_) {
465         next if ($chan eq "");
466         $chan =~ tr/A-Z/a-z/;   # lowercase.
467
468         if ($chan !~ /^$mask{chan}$/) {
469             &WARN("part: chan is invalid ($chan)");
470             next;
471         }
472
473         &status("parting $chan");
474         if (!&validChan($chan)) {
475             &WARN("part: not on $chan; doing anyway");
476 #           next;
477         }
478
479         $conn->part($chan);
480         # deletion of $channels{chan} is done in &entryEvt().
481     }
482 }
483
484 sub mode {
485     my ($chan, @modes) = @_;
486     my $modes = join(" ", @modes);
487
488     if (&validChan($chan) == 0) {
489         &ERROR("mode: invalid chan => '$chan'.");
490         return;
491     }
492
493     &DEBUG("mode: MODE $chan $modes");
494
495     # should move to use Net::IRC's $conn->mode()... but too lazy.
496     rawout("MODE $chan $modes");
497 }
498
499 sub op {
500     my ($chan, @who) = @_;
501     my $os      = "o" x scalar(@who);
502
503     &mode($chan, "+$os @who");
504 }
505
506 sub deop {
507     my ($chan, @who) = @_;
508     my $os = "o" x scalar(@who);
509
510     &mode($chan, "-$os ".@who);
511 }
512
513 sub kick {
514     my ($nick,$chan,$msg) = @_;
515     my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
516
517     if ($chan ne "" and &validChan($chan) == 0) {
518         &ERROR("kick: invalid channel $chan.");
519         return;
520     }
521
522     $nick =~ tr/A-Z/a-z/;
523
524     foreach $chan (@chans) {
525         if (!&IsNickInChan($nick,$chan)) {
526             &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
527             next;
528         }
529
530         if (!exists $channels{$chan}{o}{$ident}) {
531             &status("kick: do not have ops on $chan :(");
532             next;
533         }
534
535         &status("Kicking $nick from $chan.");
536         $conn->kick($chan, $nick, $msg);
537     }
538 }
539
540 sub ban {
541     my ($mask,$chan) = @_;
542     my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
543     my $ban     = 0;
544
545     if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
546         &ERROR("ban: invalid channel $chan.");
547         return;
548     }
549
550     foreach $chan (@chans) {
551         if (!exists $channels{$chan}{o}{$ident}) {
552             &status("ban: do not have ops on $chan :(");
553             next;
554         }
555
556         &status("Banning $mask from $chan.");
557         &rawout("MODE $chan +b $mask");
558         $ban++;
559     }
560
561     return $ban;
562 }
563
564 sub unban {
565     my ($mask,$chan) = @_;
566     my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
567     my $ban     = 0;
568
569     &DEBUG("unban: mask = $mask, chan = @chans");
570
571     foreach $chan (@chans) {
572         if (!exists $channels{$chan}{o}{$ident}) {
573             &status("unBan: do not have ops on $chan :(");
574             next;
575         }
576
577         &status("Removed ban $mask from $chan.");
578         &rawout("MODE $chan -b $mask");
579         $ban++;
580     }
581
582     return $ban;
583 }
584
585 sub quit {
586     my ($quitmsg) = @_;
587     if (defined $conn) {
588         &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
589         $conn->quit($quitmsg);
590     } else {
591         &WARN("quit: could not quit!");
592     }
593 }
594
595 sub nick {
596     my ($nick) = @_;
597
598     if (!defined $nick) {
599         &ERROR("nick: nick == NULL.");
600         return;
601     }
602
603     if (defined $ident and $nick eq $ident) {
604         &WARN("nick: nick == ident == '$ident'.");
605         return;
606     }
607
608     my $bad     = 0;
609     $bad++ if (exists $nuh{$conn->nick()});
610     $bad++ if (&IsNickInAnyChan($conn->nick()));
611
612     if ($bad) {
613         &WARN("Nick: not going to try and get my nick back. [".
614                 scalar(gmtime). "]");
615 # hrm... over time we lose track of our own nick.
616 #       return;
617     }
618
619     if ($nick =~ /^$mask{nick}$/) {
620         &rawout("NICK ".$nick);
621
622         if (defined $ident) {
623             &status("nick: Changing nick to $nick (from $ident)");
624             # following shouldn't be here :(
625             $ident      = $nick;
626         } else {
627             &DEBUG("first time nick change.");
628             $ident      = $nick;
629         }
630
631         return 1;
632     }
633     &DEBUG("nick: failed... why oh why (nick => $nick)");
634
635     return 0;
636 }
637
638 sub invite {
639     my($who, $chan) = @_;
640     # TODO: check if $who or $chan are invalid.
641
642     $conn->invite($who, $chan);
643 }
644
645 ##########
646 # Channel related functions...
647 #
648
649 # Usage: &joinNextChan();
650 sub joinNextChan {
651     my @join = getJoinChans(1);
652     my $mynick = "UNDEFINED";
653
654     $mynick = $conn->nick() if $conn;
655     if (scalar @join) {
656         my $chan = shift @join;
657         &joinchan($chan);
658
659         if (my $i = scalar @join) {
660             &status("joinNextChan: $mynick $i chans to join.");
661         }
662
663         return;
664     }
665
666     if (exists $cache{joinTime}) {
667         my $delta       = time() - $cache{joinTime} - 5;
668         my $timestr     = &Time2String($delta);
669         # FIXME: @join should be @in instead (hacked to 10)
670         #my $rate       = sprintf("%.1f", $delta / @in);
671         my $rate        = sprintf("%.1f", $delta / 10);
672         delete $cache{joinTime};
673
674         &status("time taken to join all chans: $timestr; rate: $rate sec/join");
675     }
676
677     # chanserv check: global channels, in case we missed one.
678     foreach ( &ChanConfList("chanServ_ops") ) {
679         &chanServCheck($_);
680     }
681 }
682
683 # Usage: &getNickInChans($nick);
684 sub getNickInChans {
685     my ($nick) = @_;
686     my @array;
687
688     foreach (keys %channels) {
689         next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
690         push(@array, $_);
691     }
692
693     return @array;
694 }
695
696 # Usage: &getNicksInChan($chan);
697 sub getNicksInChan {
698     my ($chan) = @_;
699     my @array;
700
701     return keys %{ $channels{$chan}{''} };
702 }
703
704 sub IsNickInChan {
705     my ($nick,$chan) = @_;
706
707     $chan =~ tr/A-Z/a-z/;       # not lowercase unfortunately.
708
709     if ($chan =~ /^$/) {
710         &DEBUG("INIC: chan == NULL.");
711         return 0;
712     }
713
714     if (&validChan($chan) == 0) {
715         &ERROR("INIC: invalid channel $chan.");
716         return 0;
717     }
718
719     if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
720         return 1;
721     } else {
722         foreach (keys %channels) {
723             next unless (/[A-Z]/);
724             &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
725         }
726         return 0;
727     }
728 }
729
730 sub IsNickInAnyChan {
731     my ($nick) = @_;
732     my $chan;
733
734     foreach $chan (keys %channels) {
735         next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''}  });
736         return 1;
737     }
738     return 0;
739 }
740
741 # Usage: &validChan($chan);
742 sub validChan {
743     # TODO: use $c instead?
744     my ($chan) = @_;
745
746     if (!defined $chan or $chan =~ /^\s*$/) {
747         return 0;
748     }
749
750     if (lc $chan ne $chan) {
751         &WARN("validChan: lc chan != chan. ($chan); fixing.");
752         $chan =~ tr/A-Z/a-z/;
753     }
754
755     # it's possible that this check creates the hash if empty.
756     if (defined $channels{$chan} or exists $channels{$chan}) {
757         if ($chan =~ /^_?default$/) {
758 #           &WARN("validC: chan cannot be _default! returning 0!");
759             return 0;
760         }
761
762         return 1;
763     } else {
764         return 0;
765     }
766 }
767
768 ###
769 # Usage: &delUserInfo($nick,@chans);
770 sub delUserInfo {
771     my ($nick,@chans) = @_;
772     my ($mode,$chan);
773
774     foreach $chan (@chans) {
775         foreach $mode (keys %{ $channels{$chan} }) {
776             # use grep here?
777             next unless (exists $channels{$chan}{$mode}{$nick});
778
779             delete $channels{$chan}{$mode}{$nick};
780         }
781     }
782 }
783
784 sub clearChanVars {
785     my ($chan) = @_;
786
787     delete $channels{$chan};
788 }
789
790 sub clearIRCVars {
791     undef %channels;
792     undef %floodjoin;
793
794     $cache{joinTime}    = time();
795 }
796
797 sub getJoinChans {
798     my($show)   = @_;
799
800     my @in;
801     my @skip;
802     my @join;
803
804     # can't join any if not connected
805     return @join if (!$conn);
806
807     my $nick = $conn->nick();
808
809     foreach (keys %chanconf) {
810         next if ($_ eq "_default");
811
812         my $skip = 0;
813         my $val = $chanconf{$_}{autojoin};
814
815         if (defined $val) {
816             $skip++ if ($val eq "0");
817             if ($val eq "1") {
818                 # convert old +autojoin to autojoin <nick>
819                 $val = $nick;
820                 $chanconf{$_}{autojoin} = $val;
821             }
822             $skip++ if ($val ne $nick);
823         } else {
824             $skip++;
825         }
826
827         if ($skip) {
828             push(@skip, $_);
829         } else {
830             if (defined $channels{$_} or exists $channels{$_}) {
831                 push(@in, $_);
832             } else {
833                 push(@join, $_);
834             }
835         }
836     }
837
838     my $str;
839     $str .= ' in:' . join(',', sort @in) if scalar @in;
840     $str .= ' skip:' . join(',', sort @skip) if scalar @skip;
841     $str .= ' join:' . join(',', sort @join) if scalar @join;
842
843     &status("Chans: ($nick)$str") if ($show);
844
845     return sort @join;
846 }
847
848 sub closeDCC {
849 #    &DEBUG("closeDCC called.");
850     my $type;
851
852     foreach $type (keys %dcc) {
853         next if ($type ne uc($type));
854
855         my $nick;
856         foreach $nick (keys %{ $dcc{$type} }) {
857             next unless (defined $nick);
858             &status("DCC CHAT: closing DCC $type to $nick.");
859             next unless (defined $dcc{$type}{$nick});
860
861             my $ref = $dcc{$type}{$nick};
862             &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
863             $dcc{$type}{$nick}->close();
864             delete $dcc{$type}{$nick};
865             &DEBUG("after close for $nick");
866         }
867         delete $dcc{$type};
868     }
869 }
870
871 sub joinfloodCheck {
872     my($who,$chan,$userhost) = @_;
873
874     return unless (&IsChanConf("joinfloodCheck"));
875
876     if (exists $netsplit{lc $who}) {    # netsplit join.
877         &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
878     }
879
880     if (exists $floodjoin{$chan}{$who}{Time}) {
881         &WARN("floodjoin{$chan}{$who} already exists?");
882     }
883
884     $floodjoin{$chan}{$who}{Time} = time();
885     $floodjoin{$chan}{$who}{Host} = $userhost;
886
887     ### Check...
888     foreach (keys %floodjoin) {
889         my $c = $_;
890         my $count = scalar keys %{ $floodjoin{$c} };
891         next unless ($count > 5);
892         &DEBUG("joinflood: count => $count");
893
894         my $time;
895         foreach (keys %{ $floodjoin{$c} }) {
896             my $t = $floodjoin{$c}{$_}{Time};
897             next unless (defined $t);
898
899             $time += $t;
900         }
901         &DEBUG("joinflood: time => $time");
902         $time /= $count;
903
904         &DEBUG("joinflood: new time => $time");
905     }
906
907     ### Clean it up.
908     my $delete = 0;
909     my $time = time();
910     foreach $chan (keys %floodjoin) {
911         foreach $who (keys %{ $floodjoin{$chan} }) {
912             my $t       = $floodjoin{$chan}{$who}{Time};
913             next unless (defined $t);
914
915             my $delta   = $time - $t;
916             next unless ($delta > 10);
917
918             delete $floodjoin{$chan}{$who};
919             $delete++;
920         }
921     }
922
923     &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
924 }
925
926 sub getHostMask {
927     my($n) = @_;
928
929     if (exists $nuh{$n}) {
930         return &makeHostMask($nuh{$n});
931     } else {
932         $cache{on_who_Hack} = 1;
933         $conn->who($n);
934     }
935 }
936
937 1;