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