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