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