]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/Irc.pl
line len
[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     if (!defined $msg) {
213         $msg ||= "NULL";
214         &WARN("say: msg == $msg.");
215         return;
216     }
217
218     &status("</$talkchannel> $msg");
219     if (&whatInterface() =~ /IRC/) {
220         $msg    = "zero" if ($msg =~ /^0+$/);
221         my $t   = time();
222
223         if ($t == $pubtime) {
224             $pubcount++;
225             $pubsize += length $msg;
226
227             my $i = &getChanConfDefault("sendPublicLimitLines", 3);
228             my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
229
230             if ( ($pubcount % $i) == 0 and $pubcount) {
231                 sleep 1;
232             } elsif ($pubsize > $j) {
233                 sleep 1;
234                 $pubsize -= $j;
235             }
236
237         } else {
238             $pubcount   = 0;
239             $pubtime    = $t;
240             $pubsize    = length $msg;
241         }
242
243         $conn->privmsg($talkchannel, $msg);
244     }
245 }
246
247 sub msg {
248     my ($nick, $msg) = @_;
249     if (!defined $nick) {
250         &ERROR("msg: nick == NULL.");
251         return;
252     }
253
254     if (!defined $msg) {
255         $msg ||= "NULL";
256         &WARN("msg: msg == $msg.");
257         return;
258     }
259
260     &status(">$nick< $msg");
261
262     return unless (&whatInterface() =~ /IRC/);
263     my $t = time();
264
265     if ($t == $msgtime) {
266         $msgcount++;
267         $msgsize += length $msg;
268
269         my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
270         my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
271         if ( ($msgcount % $i) == 0 and $msgcount) {
272             sleep 1;
273         } elsif ($msgsize > $j) {
274             sleep 1;
275             $msgsize -= $j;
276         }
277
278     } else {
279         $msgcount       = 0;
280         $msgtime        = $t;
281         $msgsize        = length $msg;
282     }
283
284     $conn->privmsg($nick, $msg);
285 }
286
287 # Usage: &action(nick || chan, txt);
288 sub action {
289     my ($target, $txt) = @_;
290     if (!defined $txt) {
291         &WARN("action: txt == NULL.");
292         return;
293     }
294
295     if (length $txt > 480) {
296         &status("action: txt too long; truncating.");
297         chop($txt) while (length $txt > 480);
298     }
299
300     &status("* $ident/$target $txt");
301     $conn->me($target, $txt);
302 }
303
304 # Usage: &notice(nick || chan, txt);
305 sub notice {
306     my ($target, $txt) = @_;
307     if (!defined $txt) {
308         &WARN("notice: txt == NULL.");
309         return;
310     }
311
312     &status("-$target- $txt");
313
314     my $t       = time();
315
316     if ($t == $nottime) {
317         $notcount++;
318         $notsize += length $txt;
319
320         my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
321         my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
322
323         if ( ($notcount % $i) == 0 and $notcount) {
324             sleep 1;
325         } elsif ($notsize > $j) {
326             sleep 1;
327             $notsize -= $j;
328         }
329
330     } else {
331         $notcount       = 0;
332         $nottime        = $t;
333         $notsize        = length $txt;
334     }
335
336     $conn->notice($target, $txt);
337 }
338
339 sub DCCBroadcast {
340     my ($txt,$flag) = @_;
341
342     ### FIXME: flag not supported yet.
343
344     foreach (keys %{ $dcc{'CHAT'} }) {
345         $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
346     }
347 }
348
349 ##########
350 ### perform commands.
351 ###
352
353 # Usage: &performReply($reply);
354 sub performReply {
355     my ($reply) = @_;
356
357     if (!defined $reply or $reply =~ /^\s*$/) {
358         &DEBUG("performReply: reply == NULL.");
359         return;
360     }
361
362     $reply =~ /([\.\?\s]+)$/;
363
364     &checkMsgType($reply);
365
366     if ($msgType eq 'public') {
367         if (rand() < 0.5 or $reply =~ /[\.\?]$/) {
368             $reply = "$orig{who}: ".$reply;
369         } else {
370             $reply = "$reply, ".$orig{who};
371         }
372         &say($reply);
373
374     } elsif ($msgType eq 'private') {
375         if (rand() > 0.5) {
376             $reply = "$reply, ".$orig{who};
377         }
378         &msg($who, $reply);
379
380     } elsif ($msgType eq 'chat') {
381         if (!exists $dcc{'CHAT'}{$who}) {
382             &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
383             return;
384         }
385         $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
386
387     } else {
388         &ERROR("PR: msgType invalid? ($msgType).");
389     }
390 }
391
392 # ...
393 sub performAddressedReply {
394     return unless ($addressed);
395     &performReply(@_);
396 }
397
398 sub pSReply {
399     &performStrictReply(@_);
400 }
401
402 # Usage: &performStrictReply($reply);
403 sub performStrictReply {
404     my ($reply) = @_;
405
406     &checkMsgType($reply);
407
408     if ($msgType eq 'private') {
409         &msg($who, $reply);
410     } elsif ($msgType eq 'public') {
411         &say($reply);
412     } elsif ($msgType eq 'chat') {
413         &dccsay(lc $who, $reply);
414     } else {
415         &ERROR("pSR: msgType invalid? ($msgType).");
416     }
417 }
418
419 sub dccsay {
420     my($who, $reply) = @_;
421
422     if (!defined $reply or $reply =~ /^\s*$/) {
423         &WARN("dccsay: reply == NULL.");
424         return;
425     }
426
427     if (!exists $dcc{'CHAT'}{$who}) {
428         &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
429         return;
430     }
431
432     &status("=>$who<= $reply");         # dcc chat.
433     $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
434 }
435
436 sub dcc_close {
437     my($who) = @_;
438     my $type;
439
440     foreach $type (keys %dcc) {
441         &FIXME("dcc_close: $who");
442         my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
443         next unless (scalar @who);
444         $who = $who[0];
445         &DEBUG("dcc_close... close $who!");
446     }
447 }
448
449 sub joinchan {
450     my ($chan)  = @_;
451     my $key     = &getChanConf("chankey", $chan) || "";
452
453     # forgot for about 2 years to implement channel keys when moving
454     # over to Net::IRC...
455
456     # hopefully validChan is right.
457     if (&validChan($chan)) {
458         &status("join: already on $chan?");
459     }
460     #} else {
461         &status("joining $b_blue$chan$ob");
462
463         return if ($conn->join($chan, $key));
464         return if (&validChan($chan));
465
466         &DEBUG("joinchan: join failed. trying connect!");
467         &clearIRCVars();
468         $conn->connect();
469     #}
470 }
471
472 sub part {
473     my $chan;
474
475     foreach $chan (@_) {
476         next if ($chan eq "");
477         $chan =~ tr/A-Z/a-z/;   # lowercase.
478
479         if ($chan !~ /^$mask{chan}$/) {
480             &WARN("part: chan is invalid ($chan)");
481             next;
482         }
483
484         &status("parting $chan");
485         if (!&validChan($chan)) {
486             &WARN("part: not on $chan; doing anyway");
487 #           next;
488         }
489
490         $conn->part($chan);
491         # deletion of $channels{chan} is done in &entryEvt().
492     }
493 }
494
495 sub mode {
496     my ($chan, @modes) = @_;
497     my $modes = join(" ", @modes);
498
499     if (&validChan($chan) == 0) {
500         &ERROR("mode: invalid chan => '$chan'.");
501         return;
502     }
503
504     &DEBUG("mode: MODE $chan $modes");
505
506     # should move to use Net::IRC's $conn->mode()... but too lazy.
507     rawout("MODE $chan $modes");
508 }
509
510 sub op {
511     my ($chan, @who) = @_;
512     my $os      = "o" x scalar(@who);
513
514     &mode($chan, "+$os @who");
515 }
516
517 sub deop {
518     my ($chan, @who) = @_;
519     my $os = "o" x scalar(@who);
520
521     &mode($chan, "-$os ".@who);
522 }
523
524 sub kick {
525     my ($nick,$chan,$msg) = @_;
526     my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
527
528     if ($chan ne "" and &validChan($chan) == 0) {
529         &ERROR("kick: invalid channel $chan.");
530         return;
531     }
532
533     $nick =~ tr/A-Z/a-z/;
534
535     foreach $chan (@chans) {
536         if (!&IsNickInChan($nick,$chan)) {
537             &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
538             next;
539         }
540
541         if (!exists $channels{$chan}{o}{$ident}) {
542             &status("kick: do not have ops on $chan :(");
543             next;
544         }
545
546         &status("Kicking $nick from $chan.");
547         $conn->kick($chan, $nick, $msg);
548     }
549 }
550
551 sub ban {
552     my ($mask,$chan) = @_;
553     my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
554     my $ban     = 0;
555
556     if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
557         &ERROR("ban: invalid channel $chan.");
558         return;
559     }
560
561     foreach $chan (@chans) {
562         if (!exists $channels{$chan}{o}{$ident}) {
563             &status("ban: do not have ops on $chan :(");
564             next;
565         }
566
567         &status("Banning $mask from $chan.");
568         &rawout("MODE $chan +b $mask");
569         $ban++;
570     }
571
572     return $ban;
573 }
574
575 sub unban {
576     my ($mask,$chan) = @_;
577     my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
578     my $ban     = 0;
579
580     &DEBUG("unban: mask = $mask, chan = @chans");
581
582     foreach $chan (@chans) {
583         if (!exists $channels{$chan}{o}{$ident}) {
584             &status("unBan: do not have ops on $chan :(");
585             next;
586         }
587
588         &status("Removed ban $mask from $chan.");
589         &rawout("MODE $chan -b $mask");
590         $ban++;
591     }
592
593     return $ban;
594 }
595
596 sub quit {
597     my ($quitmsg) = @_;
598     if (defined $conn) {
599         &status("QUIT " . $conn->nick() . " has quit IRC ($quitmsg)");
600         $conn->quit($quitmsg);
601     } else {
602         &WARN("quit: could not quit!");
603     }
604 }
605
606 sub nick {
607     my ($nick) = @_;
608
609     if (!defined $nick) {
610         &ERROR("nick: nick == NULL.");
611         return;
612     }
613
614     if (defined $ident and $nick eq $ident) {
615         &WARN("nick: nick == ident == '$ident'.");
616         return;
617     }
618
619     my $bad     = 0;
620     $bad++ if (exists $nuh{$conn->nick()});
621     $bad++ if (&IsNickInAnyChan($conn->nick()));
622
623     if ($bad) {
624         &WARN("Nick: not going to try and get my nick back. [".
625                 scalar(gmtime). "]");
626 # hrm... over time we lose track of our own nick.
627 #       return;
628     }
629
630 # FIXME broken for multiple nicks
631 #    if ($nick =~ /^$mask{nick}$/) {
632 #       &rawout("NICK ".$nick);
633 #
634 #       if (defined $ident) {
635 #           &status("nick: Changing nick to $nick (from $ident)");
636 #           # following shouldn't be here :(
637 #           $ident      = $nick;
638 #       } else {
639 #           &DEBUG("first time nick change.");
640 #           $ident      = $nick;
641 #       }
642 #
643 #       return 1;
644 #    }
645     &DEBUG("nick: failed... why oh why (nick => $nick)");
646
647     return 0;
648 }
649
650 sub invite {
651     my($who, $chan) = @_;
652     # TODO: check if $who or $chan are invalid.
653
654     $conn->invite($who, $chan);
655 }
656
657 ##########
658 # Channel related functions...
659 #
660
661 # Usage: &joinNextChan();
662 sub joinNextChan {
663     my @join = getJoinChans(1);
664     my $mynick = "UNDEFINED";
665
666     $mynick = $conn->nick() if $conn;
667     if (scalar @join) {
668         my $chan = shift @join;
669         &joinchan($chan);
670
671         if (my $i = scalar @join) {
672             &status("joinNextChan: $mynick $i chans to join.");
673         }
674
675         return;
676     }
677
678     if (exists $cache{joinTime}) {
679         my $delta       = time() - $cache{joinTime} - 5;
680         my $timestr     = &Time2String($delta);
681         # FIXME: @join should be @in instead (hacked to 10)
682         #my $rate       = sprintf("%.1f", $delta / @in);
683         my $rate        = sprintf("%.1f", $delta / 10);
684         delete $cache{joinTime};
685
686         &status("time taken to join all chans: $timestr; rate: $rate sec/join");
687     }
688
689     # chanserv check: global channels, in case we missed one.
690     foreach ( &ChanConfList("chanServ_ops") ) {
691         &chanServCheck($_);
692     }
693 }
694
695 # Usage: &getNickInChans($nick);
696 sub getNickInChans {
697     my ($nick) = @_;
698     my @array;
699
700     foreach (keys %channels) {
701         next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
702         push(@array, $_);
703     }
704
705     return @array;
706 }
707
708 # Usage: &getNicksInChan($chan);
709 sub getNicksInChan {
710     my ($chan) = @_;
711     my @array;
712
713     return keys %{ $channels{$chan}{''} };
714 }
715
716 sub IsNickInChan {
717     my ($nick,$chan) = @_;
718
719     $chan =~ tr/A-Z/a-z/;       # not lowercase unfortunately.
720
721     if ($chan =~ /^$/) {
722         &DEBUG("INIC: chan == NULL.");
723         return 0;
724     }
725
726     if (&validChan($chan) == 0) {
727         &ERROR("INIC: invalid channel $chan.");
728         return 0;
729     }
730
731     if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
732         return 1;
733     } else {
734         foreach (keys %channels) {
735             next unless (/[A-Z]/);
736             &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
737         }
738         return 0;
739     }
740 }
741
742 sub IsNickInAnyChan {
743     my ($nick) = @_;
744     my $chan;
745
746     foreach $chan (keys %channels) {
747         next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''}  });
748         return 1;
749     }
750     return 0;
751 }
752
753 # Usage: &validChan($chan);
754 sub validChan {
755     # TODO: use $c instead?
756     my ($chan) = @_;
757
758     if (!defined $chan or $chan =~ /^\s*$/) {
759         return 0;
760     }
761
762     if (lc $chan ne $chan) {
763         &WARN("validChan: lc chan != chan. ($chan); fixing.");
764         $chan =~ tr/A-Z/a-z/;
765     }
766
767     # it's possible that this check creates the hash if empty.
768     if (defined $channels{$chan} or exists $channels{$chan}) {
769         if ($chan =~ /^_?default$/) {
770 #           &WARN("validC: chan cannot be _default! returning 0!");
771             return 0;
772         }
773
774         return 1;
775     } else {
776         return 0;
777     }
778 }
779
780 ###
781 # Usage: &delUserInfo($nick,@chans);
782 sub delUserInfo {
783     my ($nick,@chans) = @_;
784     my ($mode,$chan);
785
786     foreach $chan (@chans) {
787         foreach $mode (keys %{ $channels{$chan} }) {
788             # use grep here?
789             next unless (exists $channels{$chan}{$mode}{$nick});
790
791             delete $channels{$chan}{$mode}{$nick};
792         }
793     }
794 }
795
796 sub clearChanVars {
797     my ($chan) = @_;
798
799     delete $channels{$chan};
800 }
801
802 sub clearIRCVars {
803     undef %channels;
804     undef %floodjoin;
805
806     $cache{joinTime}    = time();
807 }
808
809 sub getJoinChans {
810     my($show)   = @_;
811
812     my @in;
813     my @skip;
814     my @join;
815
816     # can't join any if not connected
817     return @join if (!$conn);
818
819     my $nick = $conn->nick();
820
821     foreach (keys %chanconf) {
822         next if ($_ eq "_default");
823
824         my $skip = 0;
825         my $val = $chanconf{$_}{autojoin};
826
827         if (defined $val) {
828             $skip++ if ($val eq "0");
829             if ($val eq "1") {
830                 # convert old +autojoin to autojoin <nick>
831                 $val = $nick;
832                 $chanconf{$_}{autojoin} = $val;
833             }
834             $skip++ if ($val ne $nick);
835         } else {
836             $skip++;
837         }
838
839         if ($skip) {
840             push(@skip, $_);
841         } else {
842             if (defined $channels{$_} or exists $channels{$_}) {
843                 push(@in, $_);
844             } else {
845                 push(@join, $_);
846             }
847         }
848     }
849
850     my $str;
851     $str .= ' in:' . join(',', sort @in) if scalar @in;
852     $str .= ' skip:' . join(',', sort @skip) if scalar @skip;
853     $str .= ' join:' . join(',', sort @join) if scalar @join;
854
855     &status("Chans: ($nick)$str") if ($show);
856
857     return sort @join;
858 }
859
860 sub closeDCC {
861 #    &DEBUG("closeDCC called.");
862     my $type;
863
864     foreach $type (keys %dcc) {
865         next if ($type ne uc($type));
866
867         my $nick;
868         foreach $nick (keys %{ $dcc{$type} }) {
869             next unless (defined $nick);
870             &status("DCC CHAT: closing DCC $type to $nick.");
871             next unless (defined $dcc{$type}{$nick});
872
873             my $ref = $dcc{$type}{$nick};
874             &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
875             $dcc{$type}{$nick}->close();
876             delete $dcc{$type}{$nick};
877             &DEBUG("after close for $nick");
878         }
879         delete $dcc{$type};
880     }
881 }
882
883 sub joinfloodCheck {
884     my($who,$chan,$userhost) = @_;
885
886     return unless (&IsChanConf("joinfloodCheck"));
887
888     if (exists $netsplit{lc $who}) {    # netsplit join.
889         &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
890     }
891
892     if (exists $floodjoin{$chan}{$who}{Time}) {
893         &WARN("floodjoin{$chan}{$who} already exists?");
894     }
895
896     $floodjoin{$chan}{$who}{Time} = time();
897     $floodjoin{$chan}{$who}{Host} = $userhost;
898
899     ### Check...
900     foreach (keys %floodjoin) {
901         my $c = $_;
902         my $count = scalar keys %{ $floodjoin{$c} };
903         next unless ($count > 5);
904         &DEBUG("joinflood: count => $count");
905
906         my $time;
907         foreach (keys %{ $floodjoin{$c} }) {
908             my $t = $floodjoin{$c}{$_}{Time};
909             next unless (defined $t);
910
911             $time += $t;
912         }
913         &DEBUG("joinflood: time => $time");
914         $time /= $count;
915
916         &DEBUG("joinflood: new time => $time");
917     }
918
919     ### Clean it up.
920     my $delete = 0;
921     my $time = time();
922     foreach $chan (keys %floodjoin) {
923         foreach $who (keys %{ $floodjoin{$chan} }) {
924             my $t       = $floodjoin{$chan}{$who}{Time};
925             next unless (defined $t);
926
927             my $delta   = $time - $t;
928             next unless ($delta > 10);
929
930             delete $floodjoin{$chan}{$who};
931             $delete++;
932         }
933     }
934
935     &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
936 }
937
938 sub getHostMask {
939     my($n) = @_;
940
941     if (exists $nuh{$n}) {
942         return &makeHostMask($nuh{$n});
943     } else {
944         $cache{on_who_Hack} = 1;
945         $conn->who($n);
946     }
947 }
948
949 1;