]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/Irc.pl
connect info
[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 @join = getJoinChans(1);
667     my $mynick = "UNDEFINED";
668
669     $mynick = $conn->nick() if $conn;
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
678         return;
679     }
680
681     if (exists $cache{joinTime}) {
682         my $delta       = time() - $cache{joinTime} - 5;
683         my $timestr     = &Time2String($delta);
684         # FIXME: @join should be @in instead (hacked to 10)
685         #my $rate       = sprintf("%.1f", $delta / @in);
686         my $rate        = sprintf("%.1f", $delta / 10);
687         delete $cache{joinTime};
688
689         &status("time taken to join all chans: $timestr; rate: $rate sec/join");
690     }
691
692     # chanserv check: global channels, in case we missed one.
693     foreach ( &ChanConfList("chanServ_ops") ) {
694         &chanServCheck($_);
695     }
696 }
697
698 # Usage: &getNickInChans($nick);
699 sub getNickInChans {
700     my ($nick) = @_;
701     my @array;
702
703     foreach (keys %channels) {
704         next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
705         push(@array, $_);
706     }
707
708     return @array;
709 }
710
711 # Usage: &getNicksInChan($chan);
712 sub getNicksInChan {
713     my ($chan) = @_;
714     my @array;
715
716     return keys %{ $channels{$chan}{''} };
717 }
718
719 sub IsNickInChan {
720     my ($nick,$chan) = @_;
721
722     $chan =~ tr/A-Z/a-z/;       # not lowercase unfortunately.
723
724     if ($chan =~ /^$/) {
725         &DEBUG("INIC: chan == NULL.");
726         return 0;
727     }
728
729     if (&validChan($chan) == 0) {
730         &ERROR("INIC: invalid channel $chan.");
731         return 0;
732     }
733
734     if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
735         return 1;
736     } else {
737         foreach (keys %channels) {
738             next unless (/[A-Z]/);
739             &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
740         }
741         return 0;
742     }
743 }
744
745 sub IsNickInAnyChan {
746     my ($nick) = @_;
747     my $chan;
748
749     foreach $chan (keys %channels) {
750         next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''}  });
751         return 1;
752     }
753     return 0;
754 }
755
756 # Usage: &validChan($chan);
757 sub validChan {
758     # TODO: use $c instead?
759     my ($chan) = @_;
760
761     if (!defined $chan or $chan =~ /^\s*$/) {
762         return 0;
763     }
764
765     if (lc $chan ne $chan) {
766         &WARN("validChan: lc chan != chan. ($chan); fixing.");
767         $chan =~ tr/A-Z/a-z/;
768     }
769
770     # it's possible that this check creates the hash if empty.
771     if (defined $channels{$chan} or exists $channels{$chan}) {
772         if ($chan =~ /^_?default$/) {
773 #           &WARN("validC: chan cannot be _default! returning 0!");
774             return 0;
775         }
776
777         return 1;
778     } else {
779         return 0;
780     }
781 }
782
783 ###
784 # Usage: &delUserInfo($nick,@chans);
785 sub delUserInfo {
786     my ($nick,@chans) = @_;
787     my ($mode,$chan);
788
789     foreach $chan (@chans) {
790         foreach $mode (keys %{ $channels{$chan} }) {
791             # use grep here?
792             next unless (exists $channels{$chan}{$mode}{$nick});
793
794             delete $channels{$chan}{$mode}{$nick};
795         }
796     }
797 }
798
799 sub clearChanVars {
800     my ($chan) = @_;
801
802     delete $channels{$chan};
803 }
804
805 sub clearIRCVars {
806     undef %channels;
807     undef %floodjoin;
808
809     $cache{joinTime}    = time();
810 }
811
812 sub getJoinChans {
813     my($show)   = @_;
814
815     my @in;
816     my @skip;
817     my @join;
818
819     # can't join any if not connected
820     return @join if (!$conn);
821
822     my $nick = $conn->nick();
823
824     foreach (keys %chanconf) {
825         next if ($_ eq "_default");
826
827         my $skip = 0;
828         my $val = $chanconf{$_}{autojoin};
829
830         if (defined $val) {
831             $skip++ if ($val eq "0");
832             if ($val eq "1") {
833                 # convert old +autojoin to autojoin <nick>
834                 $val = $nick;
835                 $chanconf{$_}{autojoin} = $val;
836             }
837             $skip++ if ($val ne $nick);
838         } else {
839             $skip++;
840         }
841
842         if ($skip) {
843             push(@skip, $_);
844         } else {
845             if (defined $channels{$_} or exists $channels{$_}) {
846                 push(@in, $_);
847             } else {
848                 push(@join, $_);
849             }
850         }
851     }
852
853     my $str;
854     $str .= ' in:' . join(',', sort @in) if scalar @in;
855     $str .= ' skip:' . join(',', sort @skip) if scalar @skip;
856     $str .= ' join:' . join(',', sort @join) if scalar @join;
857
858     &status("Chans: ($nick)$str") if ($show);
859
860     return sort @join;
861 }
862
863 sub closeDCC {
864 #    &DEBUG("closeDCC called.");
865     my $type;
866
867     foreach $type (keys %dcc) {
868         next if ($type ne uc($type));
869
870         my $nick;
871         foreach $nick (keys %{ $dcc{$type} }) {
872             next unless (defined $nick);
873             &status("DCC CHAT: closing DCC $type to $nick.");
874             next unless (defined $dcc{$type}{$nick});
875
876             my $ref = $dcc{$type}{$nick};
877             &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
878             $dcc{$type}{$nick}->close();
879             delete $dcc{$type}{$nick};
880             &DEBUG("after close for $nick");
881         }
882         delete $dcc{$type};
883     }
884 }
885
886 sub joinfloodCheck {
887     my($who,$chan,$userhost) = @_;
888
889     return unless (&IsChanConf("joinfloodCheck"));
890
891     if (exists $netsplit{lc $who}) {    # netsplit join.
892         &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
893     }
894
895     if (exists $floodjoin{$chan}{$who}{Time}) {
896         &WARN("floodjoin{$chan}{$who} already exists?");
897     }
898
899     $floodjoin{$chan}{$who}{Time} = time();
900     $floodjoin{$chan}{$who}{Host} = $userhost;
901
902     ### Check...
903     foreach (keys %floodjoin) {
904         my $c = $_;
905         my $count = scalar keys %{ $floodjoin{$c} };
906         next unless ($count > 5);
907         &DEBUG("joinflood: count => $count");
908
909         my $time;
910         foreach (keys %{ $floodjoin{$c} }) {
911             my $t = $floodjoin{$c}{$_}{Time};
912             next unless (defined $t);
913
914             $time += $t;
915         }
916         &DEBUG("joinflood: time => $time");
917         $time /= $count;
918
919         &DEBUG("joinflood: new time => $time");
920     }
921
922     ### Clean it up.
923     my $delete = 0;
924     my $time = time();
925     foreach $chan (keys %floodjoin) {
926         foreach $who (keys %{ $floodjoin{$chan} }) {
927             my $t       = $floodjoin{$chan}{$who}{Time};
928             next unless (defined $t);
929
930             my $delta   = $time - $t;
931             next unless ($delta > 10);
932
933             delete $floodjoin{$chan}{$who};
934             $delete++;
935         }
936     }
937
938     &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
939 }
940
941 sub getHostMask {
942     my($n) = @_;
943
944     if (exists $nuh{$n}) {
945         return &makeHostMask($nuh{$n});
946     } else {
947         $cache{on_who_Hack} = 1;
948         $conn->who($n);
949     }
950 }
951
952 1;