]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/Irc.pl
mynick
[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
76     # host->ip.
77     my $resolve;
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         $resolve = inet_ntoa($packed);
87         ### warning in Sys/Hostname line 78???
88         ### caused inside Net::IRC?
89     }
90     &status("Connecting to port $port of server $server ($resolve) as $param{'ircNick'} ...");
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         &status("Connecting to port $port of server $server ($resolve) as $param{'ircNick2'} ...");
119         $args{'Nick'} = $param{'ircNick2'};
120         my $conn = $irc->newconn(%args);
121         $conn->maxlinelen($maxlinelen);
122     }
123
124     if ($param{'ircNick3'}) {
125         # prep for real multiple nick/server connects
126         # FIXME: all code should get nick/server out of self, not config
127         $args{'Nick'} = $param{'ircNick3'};
128         my $conn = $irc->newconn(%args);
129         $conn->maxlinelen($maxlinelen);
130     }
131
132     &clearIRCVars();
133
134     # change internal timeout value for scheduler.
135     $irc->{_timeout}    = 10;   # how about 60?
136     # Net::IRC debugging.
137     $irc->{_debug}      = 1;
138
139     $ircstats{'Server'} = "$server:$port";
140
141     # handler stuff.
142         $conn->add_global_handler('caction',    \&on_action);
143         $conn->add_global_handler('cdcc',       \&on_dcc);
144         $conn->add_global_handler('cping',      \&on_ping);
145         $conn->add_global_handler('crping',     \&on_ping_reply);
146         $conn->add_global_handler('cversion',   \&on_version);
147         $conn->add_global_handler('crversion',  \&on_crversion);
148         $conn->add_global_handler('dcc_open',   \&on_dcc_open);
149         $conn->add_global_handler('dcc_close',  \&on_dcc_close);
150         $conn->add_global_handler('chat',       \&on_chat);
151         $conn->add_global_handler('msg',        \&on_msg);
152         $conn->add_global_handler('public',     \&on_public);
153         $conn->add_global_handler('join',       \&on_join);
154         $conn->add_global_handler('part',       \&on_part);
155         $conn->add_global_handler('topic',      \&on_topic);
156         $conn->add_global_handler('invite',     \&on_invite);
157         $conn->add_global_handler('kick',       \&on_kick);
158         $conn->add_global_handler('mode',       \&on_mode);
159         $conn->add_global_handler('nick',       \&on_nick);
160         $conn->add_global_handler('quit',       \&on_quit);
161         $conn->add_global_handler('notice',     \&on_notice);
162         $conn->add_global_handler('whoischannels', \&on_whoischannels);
163         $conn->add_global_handler('useronchannel', \&on_useronchannel);
164         $conn->add_global_handler('whois',      \&on_whois);
165         $conn->add_global_handler('other',      \&on_other);
166         $conn->add_global_handler('disconnect', \&on_disconnect);
167         $conn->add_global_handler([251,252,253,254,255], \&on_init);
168 #       $conn->add_global_handler(302, \&on_init); # userhost
169         $conn->add_global_handler(303, \&on_ison); # notify.
170         $conn->add_global_handler(315, \&on_endofwho);
171         $conn->add_global_handler(422, \&on_endofwho); # nomotd.
172         $conn->add_global_handler(324, \&on_modeis);
173         $conn->add_global_handler(333, \&on_topicinfo);
174         $conn->add_global_handler(352, \&on_who);
175         $conn->add_global_handler(353, \&on_names);
176         $conn->add_global_handler(366, \&on_endofnames);
177         $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
178         $conn->add_global_handler(433, \&on_nick_taken);
179         $conn->add_global_handler(439, \&on_targettoofast);
180         # for proper joinnextChan behaviour
181         $conn->add_global_handler(471, \&on_chanfull);
182         $conn->add_global_handler(473, \&on_inviteonly);
183         $conn->add_global_handler(474, \&on_banned);
184         $conn->add_global_handler(475, \&on_badchankey);
185         $conn->add_global_handler(443, \&on_useronchan);
186
187     # end of handler stuff.
188
189     $irc->start;
190 }
191
192 ######################################################################
193 ######## IRC ALIASES   IRC ALIASES   IRC ALIASES   IRC ALIASES #######
194 ######################################################################
195
196 sub rawout {
197     my ($buf) = @_;
198     $buf =~ s/\n//gi;
199
200     # slow down a bit if traffic is "high".
201     # need to take into account time of last message sent.
202     if ($last{buflen} > 256 and length($buf) > 256) {
203         sleep 1;
204     }
205
206     $conn->sl($buf) if (&whatInterface() =~ /IRC/);
207
208     $last{buflen} = length($buf);
209 }
210
211 sub say {
212     my ($msg) = @_;
213     my $mynick = $conn->nick();
214     if (!defined $msg) {
215         $msg ||= "NULL";
216         &WARN("say: msg == $msg.");
217         return;
218     }
219
220
221     &status("<$mynick/$talkchannel> $msg");
222     if (&whatInterface() =~ /IRC/) {
223         $msg    = "zero" if ($msg =~ /^0+$/);
224         my $t   = time();
225
226         if ($t == $pubtime) {
227             $pubcount++;
228             $pubsize += length $msg;
229
230             my $i = &getChanConfDefault("sendPublicLimitLines", 3);
231             my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
232
233             if ( ($pubcount % $i) == 0 and $pubcount) {
234                 sleep 1;
235             } elsif ($pubsize > $j) {
236                 sleep 1;
237                 $pubsize -= $j;
238             }
239
240         } else {
241             $pubcount   = 0;
242             $pubtime    = $t;
243             $pubsize    = length $msg;
244         }
245
246         $conn->privmsg($talkchannel, $msg);
247     }
248 }
249
250 sub msg {
251     my ($nick, $msg) = @_;
252     if (!defined $nick) {
253         &ERROR("msg: nick == NULL.");
254         return;
255     }
256
257     if (!defined $msg) {
258         $msg ||= "NULL";
259         &WARN("msg: msg == $msg.");
260         return;
261     }
262
263     &status(">$nick< $msg");
264
265     return unless (&whatInterface() =~ /IRC/);
266     my $t = time();
267
268     if ($t == $msgtime) {
269         $msgcount++;
270         $msgsize += length $msg;
271
272         my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
273         my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
274         if ( ($msgcount % $i) == 0 and $msgcount) {
275             sleep 1;
276         } elsif ($msgsize > $j) {
277             sleep 1;
278             $msgsize -= $j;
279         }
280
281     } else {
282         $msgcount       = 0;
283         $msgtime        = $t;
284         $msgsize        = length $msg;
285     }
286
287     $conn->privmsg($nick, $msg);
288 }
289
290 # Usage: &action(nick || chan, txt);
291 sub action {
292     my ($target, $txt) = @_;
293     if (!defined $txt) {
294         &WARN("action: txt == NULL.");
295         return;
296     }
297
298     if (length $txt > 480) {
299         &status("action: txt too long; truncating.");
300         chop($txt) while (length $txt > 480);
301     }
302
303     &status("* $ident/$target $txt");
304     $conn->me($target, $txt);
305 }
306
307 # Usage: &notice(nick || chan, txt);
308 sub notice {
309     my ($target, $txt) = @_;
310     if (!defined $txt) {
311         &WARN("notice: txt == NULL.");
312         return;
313     }
314
315     &status("-$target- $txt");
316
317     my $t       = time();
318
319     if ($t == $nottime) {
320         $notcount++;
321         $notsize += length $txt;
322
323         my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
324         my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
325
326         if ( ($notcount % $i) == 0 and $notcount) {
327             sleep 1;
328         } elsif ($notsize > $j) {
329             sleep 1;
330             $notsize -= $j;
331         }
332
333     } else {
334         $notcount       = 0;
335         $nottime        = $t;
336         $notsize        = length $txt;
337     }
338
339     $conn->notice($target, $txt);
340 }
341
342 sub DCCBroadcast {
343     my ($txt,$flag) = @_;
344
345     ### FIXME: flag not supported yet.
346
347     foreach (keys %{ $dcc{'CHAT'} }) {
348         $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
349     }
350 }
351
352 ##########
353 ### perform commands.
354 ###
355
356 # Usage: &performReply($reply);
357 sub performReply {
358     my ($reply) = @_;
359
360     if (!defined $reply or $reply =~ /^\s*$/) {
361         &DEBUG("performReply: reply == NULL.");
362         return;
363     }
364
365     $reply =~ /([\.\?\s]+)$/;
366
367     &checkMsgType($reply);
368
369     if ($msgType eq 'public') {
370         if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
371             $reply = "$orig{who}: ".$reply;
372         } else {
373             $reply = "$reply, ".$orig{who};
374         }
375         &say($reply);
376
377     } elsif ($msgType eq 'private') {
378         if (rand() > 0.5) {
379             $reply = "$reply, ".$orig{who};
380         }
381         &msg($who, $reply);
382
383     } elsif ($msgType eq 'chat') {
384         if (!exists $dcc{'CHAT'}{$who}) {
385             &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
386             return;
387         }
388         $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
389
390     } else {
391         &ERROR("PR: msgType invalid? ($msgType).");
392     }
393 }
394
395 # ...
396 sub performAddressedReply {
397     return unless ($addressed);
398     &performReply(@_);
399 }
400
401 sub pSReply {
402     &performStrictReply(@_);
403 }
404
405 # Usage: &performStrictReply($reply);
406 sub performStrictReply {
407     my ($reply) = @_;
408
409     &checkMsgType($reply);
410
411     if ($msgType eq 'private') {
412         &msg($who, $reply);
413     } elsif ($msgType eq 'public') {
414         &say($reply);
415     } elsif ($msgType eq 'chat') {
416         &dccsay(lc $who, $reply);
417     } else {
418         &ERROR("pSR: msgType invalid? ($msgType).");
419     }
420 }
421
422 sub dccsay {
423     my($who, $reply) = @_;
424
425     if (!defined $reply or $reply =~ /^\s*$/) {
426         &WARN("dccsay: reply == NULL.");
427         return;
428     }
429
430     if (!exists $dcc{'CHAT'}{$who}) {
431         &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
432         return;
433     }
434
435     &status("=>$who<= $reply");         # dcc chat.
436     $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
437 }
438
439 sub dcc_close {
440     my($who) = @_;
441     my $type;
442
443     foreach $type (keys %dcc) {
444         &FIXME("dcc_close: $who");
445         my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
446         next unless (scalar @who);
447         $who = $who[0];
448         &DEBUG("dcc_close... close $who!");
449     }
450 }
451
452 sub joinchan {
453     my ($chan)  = @_;
454     my $key     = &getChanConf("chankey", $chan) || "";
455
456     # forgot for about 2 years to implement channel keys when moving
457     # over to Net::IRC...
458
459     # hopefully validChan is right.
460     if (&validChan($chan)) {
461         &status("join: already on $chan?");
462     }
463     #} else {
464         &status("joining $b_blue$chan$ob");
465
466         return if ($conn->join($chan, $key));
467         return if (&validChan($chan));
468
469         &DEBUG("joinchan: join failed. trying connect!");
470         &clearIRCVars();
471         $conn->connect();
472     #}
473 }
474
475 sub part {
476     my $chan;
477
478     foreach $chan (@_) {
479         next if ($chan eq "");
480         $chan =~ tr/A-Z/a-z/;   # lowercase.
481
482         if ($chan !~ /^$mask{chan}$/) {
483             &WARN("part: chan is invalid ($chan)");
484             next;
485         }
486
487         &status("parting $chan");
488         if (!&validChan($chan)) {
489             &WARN("part: not on $chan; doing anyway");
490 #           next;
491         }
492
493         $conn->part($chan);
494         # deletion of $channels{chan} is done in &entryEvt().
495     }
496 }
497
498 sub mode {
499     my ($chan, @modes) = @_;
500     my $modes = join(" ", @modes);
501
502     if (&validChan($chan) == 0) {
503         &ERROR("mode: invalid chan => '$chan'.");
504         return;
505     }
506
507     &DEBUG("mode: MODE $chan $modes");
508
509     # should move to use Net::IRC's $conn->mode()... but too lazy.
510     rawout("MODE $chan $modes");
511 }
512
513 sub op {
514     my ($chan, @who) = @_;
515     my $os      = "o" x scalar(@who);
516
517     &mode($chan, "+$os @who");
518 }
519
520 sub deop {
521     my ($chan, @who) = @_;
522     my $os = "o" x scalar(@who);
523
524     &mode($chan, "-$os ".@who);
525 }
526
527 sub kick {
528     my ($nick,$chan,$msg) = @_;
529     my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
530
531     if ($chan ne "" and &validChan($chan) == 0) {
532         &ERROR("kick: invalid channel $chan.");
533         return;
534     }
535
536     $nick =~ tr/A-Z/a-z/;
537
538     foreach $chan (@chans) {
539         if (!&IsNickInChan($nick,$chan)) {
540             &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
541             next;
542         }
543
544         if (!exists $channels{$chan}{o}{$ident}) {
545             &status("kick: do not have ops on $chan :(");
546             next;
547         }
548
549         &status("Kicking $nick from $chan.");
550         $conn->kick($chan, $nick, $msg);
551     }
552 }
553
554 sub ban {
555     my ($mask,$chan) = @_;
556     my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
557     my $ban     = 0;
558
559     if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
560         &ERROR("ban: invalid channel $chan.");
561         return;
562     }
563
564     foreach $chan (@chans) {
565         if (!exists $channels{$chan}{o}{$ident}) {
566             &status("ban: do not have ops on $chan :(");
567             next;
568         }
569
570         &status("Banning $mask from $chan.");
571         &rawout("MODE $chan +b $mask");
572         $ban++;
573     }
574
575     return $ban;
576 }
577
578 sub unban {
579     my ($mask,$chan) = @_;
580     my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
581     my $ban     = 0;
582
583     &DEBUG("unban: mask = $mask, chan = @chans");
584
585     foreach $chan (@chans) {
586         if (!exists $channels{$chan}{o}{$ident}) {
587             &status("unBan: do not have ops on $chan :(");
588             next;
589         }
590
591         &status("Removed ban $mask from $chan.");
592         &rawout("MODE $chan -b $mask");
593         $ban++;
594     }
595
596     return $ban;
597 }
598
599 sub quit {
600     my ($quitmsg) = @_;
601     if (defined $conn) {
602         &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
603         $conn->quit($quitmsg);
604     } else {
605         &WARN("quit: could not quit!");
606     }
607 }
608
609 sub nick {
610     my ($nick) = @_;
611
612     if (!defined $nick) {
613         &ERROR("nick: nick == NULL.");
614         return;
615     }
616
617     if (defined $ident and $nick eq $ident) {
618         &WARN("nick: nick == ident == '$ident'.");
619         return;
620     }
621
622     my $bad     = 0;
623     $bad++ if (exists $nuh{$conn->nick()});
624     $bad++ if (&IsNickInAnyChan($conn->nick()));
625
626     if ($bad) {
627         &WARN("Nick: not going to try and get my nick back. [".
628                 scalar(gmtime). "]");
629 # hrm... over time we lose track of our own nick.
630 #       return;
631     }
632
633 # FIXME broken for multiple nicks
634 #    if ($nick =~ /^$mask{nick}$/) {
635 #       &rawout("NICK ".$nick);
636 #
637 #       if (defined $ident) {
638 #           &status("nick: Changing nick to $nick (from $ident)");
639 #           # following shouldn't be here :(
640 #           $ident      = $nick;
641 #       } else {
642 #           &DEBUG("first time nick change.");
643 #           $ident      = $nick;
644 #       }
645 #
646 #       return 1;
647 #    }
648     &DEBUG("nick: failed... why oh why (nick => $nick)");
649
650     return 0;
651 }
652
653 sub invite {
654     my($who, $chan) = @_;
655     # TODO: check if $who or $chan are invalid.
656
657     $conn->invite($who, $chan);
658 }
659
660 ##########
661 # Channel related functions...
662 #
663
664 # Usage: &joinNextChan();
665 sub joinNextChan {
666     my $mynick = "UNDEF";
667     $mynick = $conn->nick() if $conn;
668     my @join = getJoinChans(1);
669
670     if (scalar @join) {
671         my $chan = shift @join;
672         &joinchan($chan);
673
674         if (my $i = scalar @join) {
675             &status("joinNextChan: $mynick $i chans to join.");
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 for $mynick 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;