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