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