]> git.donarmstrong.com Git - infobot.git/blobdiff - src/IRC/Irc.pl
ws
[infobot.git] / src / IRC / Irc.pl
index 0e0049dfc991a4cd2bca91361426dbf899da929d..ad557ae6cc9dd8ec75037aedc794193fada20894 100644 (file)
@@ -5,52 +5,62 @@
 #      NOTE: Based on code by Kevin Lenzo & Patrick Cole  (c) 1997
 #
 
-if (&IsParam("useStrict")) { use strict; }
+use strict;
+no strict 'refs';
 
-# static scalar variables.
-$mask{ip}      = '(\d+)\.(\d+)\.(\d+)\.(\d+)';
-$mask{host}    = '[\d\w\_\-\/]+\.[\.\d\w\_\-\/]+';
-$mask{chan}    = '[\#\&]\S*';
-my $isnick1    = 'a-zA-Z\[\]\{\}\_\`\^\|\\\\';
-my $isnick2    = '0-9\-';
-$mask{nick}    = "[$isnick1]{1}[$isnick1$isnick2]*";
-$mask{nuh}     = '\S*!\S*\@\S*';
+use vars qw(%floodjoin %nuh %dcc %cache %channels %param %mask
+       %chanconf %orig %ircPort %ircstats %last %netsplit);
+use vars qw($irc $nickserv $ident $conn $msgType $who $talkchannel
+       $addressed);
+use vars qw($notcount $nottime $notsize $msgcount $msgtime $msgsize
+               $pubcount $pubtime $pubsize);
+use vars qw($b_blue $ob);
+use vars qw(@joinchan @ircServers);
+
+$nickserv      = 0;
 
 sub ircloop {
     my $error  = 0;
     my $lastrun = 0;
 
-    while (1) {
+loop:;
+    while (my $host = shift @ircServers) {
        # JUST IN CASE. irq was complaining about this.
        if ($lastrun == time()) {
-           &DEBUG("hrm... lastrun == time()");
+           &DEBUG("ircloop: hrm... lastrun == time()");
            $error++;
            sleep 10;
            next;
        }
 
-       foreach (@ircServers) {
-           if (!defined $_) {
-               &DEBUG("ircloop: ircServers[x] = NULL.");
-               $lastrun = time();
-               next;
-           }
-           next unless (exists $ircPort{$_});
+       if (!defined $host) {
+           &DEBUG("ircloop: ircServers[x] = NULL.");
+           $lastrun = time();
+           next;
+       }
+       next unless (exists $ircPort{$host});
 
-           my $retval = &irc($_, $ircPort{$_});
-           next unless (defined $retval and $retval == 0);
-           $error++;
-           if ($error % 3 == 0 and $error != 0) {
-               &ERROR("CANNOT connect to this server; next!");
-               next;
-           }
+       my $retval      = &irc($host, $ircPort{$host});
+       next unless (defined $retval and $retval == 0);
+       $error++;
 
-           if ($error >= 3*3) {
-               &ERROR("CANNOT connect to any irc server; stopping.");
-               exit 1;
-           }
+       if ($error % 3 == 0 and $error != 0) {
+           &status("IRC: Could not connect.");
+           &status("IRC: ");
+           next;
+       }
+
+       if ($error >= 3*2) {
+           &status("IRC: cannot connect to any IRC servers; stopping.");
+           &shutdown();
+           exit 1;
        }
     }
+
+    &status("IRC: ok, done one cycle of IRC servers; trying again.");
+
+    &loadIRCServers();
+    goto loop;
 }
 
 sub irc {
@@ -74,20 +84,29 @@ sub irc {
 
        my $resolve = inet_ntoa($packed);
        &status("  resolved to $resolve.");
+       ### warning in Sys/Hostname line 78???
+       ### caused inside Net::IRC?
     }
 
     $irc = new Net::IRC;
 
-    $conn = $irc->newconn(
+    my %args = (
                Nick    => $param{'ircNick'},
                Server  => $server,
                Port    => $port,
                Ircname => $param{'ircName'},
-               LocalAddr => $param{'ircHost'},
     );
+    $args{'LocalAddr'} = $param{'ircHost'} if ($param{'ircHost'});
+    $args{'Password'} = $param{'ircPasswd'} if ($param{'ircPasswd'});
+
+    $conn = $irc->newconn(%args);
 
     if (!defined $conn) {
-       &ERROR("irc: conn was not created!defined!!!");
+       &ERROR("IRC: connection failed.");
+       &ERROR("add \"set ircHost 0.0.0.0\" to your config. If that does not work");
+       &ERROR("Please check /etc/hosts to see if you have a localhost line like:");
+       &ERROR("127.0.0.1   localhost    localhost");
+       &ERROR("If this is still a problem, please contact the maintainer.");
        return 1;
     }
 
@@ -95,6 +114,8 @@ sub irc {
 
     # change internal timeout value for scheduler.
     $irc->{_timeout}   = 10;   # how about 60?
+    # Net::IRC debugging.
+    $irc->{_debug}     = 1;
 
     $ircstats{'Server'}        = "$server:$port";
 
@@ -119,11 +140,16 @@ sub irc {
        $conn->add_handler('nick',      \&on_nick);
        $conn->add_handler('quit',      \&on_quit);
        $conn->add_handler('notice',    \&on_notice);
-       $conn->add_handler('whoisuser', \&on_whoisuser);
+       $conn->add_handler('whoischannels', \&on_whoischannels);
+       $conn->add_handler('useronchannel', \&on_useronchannel);
+       $conn->add_handler('whois',     \&on_whois);
        $conn->add_handler('other',     \&on_other);
        $conn->add_global_handler('disconnect', \&on_disconnect);
        $conn->add_global_handler([251,252,253,254,255], \&on_init);
 ###    $conn->add_global_handler([251,252,253,254,255,302], \&on_init);
+       $conn->add_global_handler(303, \&on_ison); # notify.
+       $conn->add_global_handler(315, \&on_endofwho);
+       $conn->add_global_handler(422, \&on_endofwho); # nomotd.
        $conn->add_global_handler(324, \&on_modeis);
        $conn->add_global_handler(333, \&on_topicinfo);
        $conn->add_global_handler(352, \&on_who);
@@ -132,6 +158,12 @@ sub irc {
        $conn->add_global_handler(376, \&on_endofmotd); # on_connect.
        $conn->add_global_handler(433, \&on_nick_taken);
        $conn->add_global_handler(439, \&on_targettoofast);
+       # for proper joinnextChan behaviour
+       $conn->add_global_handler(471, \&on_chanfull);
+       $conn->add_global_handler(473, \&on_inviteonly);
+       $conn->add_global_handler(474, \&on_banned);
+       $conn->add_global_handler(475, \&on_badchankey);
+       $conn->add_global_handler(443, \&on_useronchan);
 
     # end of handler stuff.
 
@@ -161,19 +193,34 @@ sub say {
     my ($msg) = @_;
     if (!defined $msg) {
        $msg ||= "NULL";
-       &DEBUG("say: msg == $msg.");
-       return;
-    }
-
-    if ($msg eq $last{say} and length($msg) > 256) {
-       &status("say: detected repeated message; skipping.");
+       &WARN("say: msg == $msg.");
        return;
     }
-    $last{say} = $msg;
 
     &status("</$talkchannel> $msg");
     if (&whatInterface() =~ /IRC/) {
-       $msg = "zero" if ($msg =~ /^0+$/);
+       $msg    = "zero" if ($msg =~ /^0+$/);
+       my $t   = time();
+
+       if ($t == $pubtime) {
+           $pubcount++;
+           $pubsize += length $msg;
+
+           my $i = &getChanConfDefault("sendPublicLimitLines", 3);
+           my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
+
+           if ( ($pubcount % $i) == 0 and $pubcount) {
+               sleep 1;
+           } elsif ($pubsize > $j) {
+               sleep 1;
+               $pubsize -= $j;
+           }
+
+       } else {
+           $pubcount   = 0;
+           $pubtime    = $t;
+           $pubsize    = length $msg;
+       }
 
        $conn->privmsg($talkchannel, $msg);
     }
@@ -188,60 +235,95 @@ sub msg {
 
     if (!defined $msg) {
        $msg ||= "NULL";
-       &DEBUG("msg: msg == $msg.");
+       &WARN("msg: msg == $msg.");
        return;
     }
 
-    if ($msg eq $last{msg} and length($msg) > 256) {
-       &status("msg: detected repeated message; skipping.");
-       return;
+    &status(">$nick< $msg");
+
+    return unless (&whatInterface() =~ /IRC/);
+    my $t = time();
+
+    if ($t == $msgtime) {
+       $msgcount++;
+       $msgsize += length $msg;
+
+       my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
+       my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
+       if ( ($msgcount % $i) == 0 and $msgcount) {
+           sleep 1;
+       } elsif ($msgsize > $j) {
+           sleep 1;
+           $msgsize -= $j;
+       }
+
+    } else {
+       $msgcount       = 0;
+       $msgtime        = $t;
+       $msgsize        = length $msg;
     }
-    $last{msg} = $msg;
 
-    &status(">$nick< $msg");
-    $conn->privmsg($nick, $msg) if (&whatInterface() =~ /IRC/);
+    $conn->privmsg($nick, $msg);
 }
 
 # Usage: &action(nick || chan, txt);
 sub action {
     my ($target, $txt) = @_;
     if (!defined $txt) {
-       &DEBUG("action: txt == NULL.");
+       &WARN("action: txt == NULL.");
        return;
     }
 
-    my $rawout = "PRIVMSG $target :\001ACTION $txt\001";
-    if (length $rawout > 510) {
+    if (length $txt > 480) {
        &status("action: txt too long; truncating.");
-
-       chop($rawout) while (length($rawout) > 510);
-       $rawout .= "\001";
+       chop($txt) while (length $txt > 480);
     }
 
     &status("* $ident/$target $txt");
-    rawout($rawout);
+    $conn->me($target, $txt);
 }
 
-# Usage: &action(nick || chan, txt);
-sub notice{
+# Usage: &notice(nick || chan, txt);
+sub notice {
     my ($target, $txt) = @_;
     if (!defined $txt) {
-       &DEBUG("action: txt == NULL.");
+       &WARN("notice: txt == NULL.");
        return;
     }
 
     &status("-$target- $txt");
 
+    my $t      = time();
+
+    if ($t == $nottime) {
+       $notcount++;
+       $notsize += length $txt;
+
+       my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
+       my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
+
+       if ( ($notcount % $i) == 0 and $notcount) {
+           sleep 1;
+       } elsif ($notsize > $j) {
+           sleep 1;
+           $notsize -= $j;
+       }
+
+    } else {
+       $notcount       = 0;
+       $nottime        = $t;
+       $notsize        = length $txt;
+    }
+
     $conn->notice($target, $txt);
 }
 
-
 sub DCCBroadcast {
     my ($txt,$flag) = @_;
 
     ### FIXME: flag not supported yet.
 
-    foreach (keys %{$dcc{'CHAT'}}) {
+    foreach (keys %{ $dcc{'CHAT'} }) {
        $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
     }
 }
@@ -253,6 +335,12 @@ sub DCCBroadcast {
 # Usage: &performReply($reply);
 sub performReply {
     my ($reply) = @_;
+
+    if (!defined $reply or $reply =~ /^\s*$/) {
+       &DEBUG("performReply: reply == NULL.");
+       return;
+    }
+
     $reply =~ /([\.\?\s]+)$/;
 
     &checkMsgType($reply);
@@ -264,19 +352,20 @@ sub performReply {
            $reply = "$reply, ".$orig{who};
        }
        &say($reply);
+
     } elsif ($msgType eq 'private') {
-       if (rand() < 0.5) {
-           $reply = $reply;
-       } else {
+       if (rand() > 0.5) {
            $reply = "$reply, ".$orig{who};
        }
        &msg($who, $reply);
+
     } elsif ($msgType eq 'chat') {
        if (!exists $dcc{'CHAT'}{$who}) {
-           &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
+           &VERB("pSR: dcc{'CHAT'}{$who} does not exist.",2);
            return;
        }
        $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
+
     } else {
        &ERROR("PR: msgType invalid? ($msgType).");
     }
@@ -303,19 +392,26 @@ sub performStrictReply {
     } elsif ($msgType eq 'public') {
        &say($reply);
     } elsif ($msgType eq 'chat') {
-       &dccsay($who,$reply);
+       &dccsay(lc $who, $reply);
     } else {
        &ERROR("pSR: msgType invalid? ($msgType).");
     }
 }
 
 sub dccsay {
-    my ($who, $reply) = @_;
+    my($who, $reply) = @_;
+
+    if (!defined $reply or $reply =~ /^\s*$/) {
+       &WARN("dccsay: reply == NULL.");
+       return;
+    }
+
     if (!exists $dcc{'CHAT'}{$who}) {
-       &WARN("pSR: dcc{'CHAT'}{$who} does not exist.");
-       return '';
+       &VERB("pSR: dcc{'CHAT'}{$who} does not exist. (2)",2);
+       return;
     }
 
+    &status("=>$who<= $reply");                # dcc chat.
     $conn->privmsg($dcc{'CHAT'}{$who}, $reply);
 }
 
@@ -325,26 +421,31 @@ sub dcc_close {
 
     foreach $type (keys %dcc) {
        &FIXME("dcc_close: $who");
-       my @who = grep /^\Q$who\E$/i, keys %{$dcc{$type}};
+       my @who = grep /^\Q$who\E$/i, keys %{ $dcc{$type} };
        next unless (scalar @who);
        $who = $who[0];
+       &DEBUG("dcc_close... close $who!");
     }
 }
 
 sub joinchan {
-    my ($chankey) = @_;
-    my $chan = lc $chankey;
+    my ($chan) = @_;
+    my $key    = &getChanConf("chankey", $chan) || "";
 
-    if ($chankey =~ s/^($mask{chan}),\S+/ /) {
-       $chan = lc $1;
-    }
-
-    &status("joining $b_blue$chan$ob");
+    # forgot for about 2 years to implement channel keys when moving
+    # over to Net::IRC...
 
+    # hopefully validChan is right.
     if (&validChan($chan)) {
        &status("join: already on $chan");
     } else {
-       $conn->join($chan);
+       &status("joining $b_blue$chan$ob");
+
+       return if ($conn->join($chan, $key));
+
+       &DEBUG("joinchan: join failed. trying connect!");
+       &clearIRCVars();
+       $conn->connect();
     }
 }
 
@@ -355,13 +456,18 @@ sub part {
        next if ($chan eq "");
        $chan =~ tr/A-Z/a-z/;   # lowercase.
 
+       if ($chan !~ /^$mask{chan}$/) {
+           &WARN("part: chan is invalid ($chan)");
+           next;
+       }
+
        &status("parting $chan");
        if (!&validChan($chan)) {
-           &status("part: not on $chan");
-           next;
+           &WARN("part: not on $chan; doing anyway");
+#          next;
        }
 
-       rawout("PART $chan");
+       $conn->part($chan);
        # deletion of $channels{chan} is done in &entryEvt().
     }
 }
@@ -375,8 +481,9 @@ sub mode {
        return;
     }
 
-    &DEBUG("MODE $chan $modes");
+    &DEBUG("mode: MODE $chan $modes");
 
+    # should move to use Net::IRC's $conn->mode()... but too lazy.
     rawout("MODE $chan $modes");
 }
 
@@ -384,7 +491,7 @@ sub op {
     my ($chan, @who) = @_;
     my $os     = "o" x scalar(@who);
 
-    &mode($chan, "+$os ".@who);
+    &mode($chan, "+$os @who");
 }
 
 sub deop {
@@ -407,49 +514,59 @@ sub kick {
 
     foreach $chan (@chans) {
        if (!&IsNickInChan($nick,$chan)) {
-           &status("Kick: $nick is not on $chan.") if (scalar @chans == 1);
+           &status("kick: $nick is not on $chan.") if (scalar @chans == 1);
            next;
        }
 
        if (!exists $channels{$chan}{o}{$ident}) {
-           &status("Kick: do not have ops on $chan :(");
+           &status("kick: do not have ops on $chan :(");
            next;
        }
 
        &status("Kicking $nick from $chan.");
-       if ($msg eq "") {
-           &rawout("KICK $chan $nick");
-       } else {
-           &rawout("KICK $chan $nick :$msg");
-       }
+       $conn->kick($chan, $nick, $msg);
     }
 }
 
 sub ban {
     my ($mask,$chan) = @_;
-    my (@chans) = ($chan eq "") ? (keys %channels) : lc($chan);
+    my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
     my $ban    = 0;
 
-    if ($chan ne "" and &validChan($chan) == 0) {
+    if ($chan !~ /^\*?$/ and &validChan($chan) == 0) {
        &ERROR("ban: invalid channel $chan.");
        return;
     }
 
-    $nick =~ tr/A-Z/a-z/;
-
     foreach $chan (@chans) {
-       if (!&IsNickInChan($nick,$chan) and scalar @chans == 1) {
-           &status("Ban: $nick is not on $chan.");
+       if (!exists $channels{$chan}{o}{$ident}) {
+           &status("ban: do not have ops on $chan :(");
            next;
        }
 
+       &status("Banning $mask from $chan.");
+       &rawout("MODE $chan +b $mask");
+       $ban++;
+    }
+
+    return $ban;
+}
+
+sub unban {
+    my ($mask,$chan) = @_;
+    my (@chans) = ($chan =~ /^\*?$/) ? (keys %channels) : lc($chan);
+    my $ban    = 0;
+
+    &DEBUG("unban: mask = $mask, chan = @chans");
+
+    foreach $chan (@chans) {
        if (!exists $channels{$chan}{o}{$ident}) {
-           &status("Ban: do not have ops on $chan :(");
+           &status("unBan: do not have ops on $chan :(");
            next;
        }
 
-       &status("Banning $mask from $chan.");
-       &rawout("MODE $chan +b $mask");
+       &status("Removed ban $mask from $chan.");
+       &rawout("MODE $chan -b $mask");
        $ban++;
     }
 
@@ -459,25 +576,62 @@ sub ban {
 sub quit {
     my ($quitmsg) = @_;
     &status("QUIT $param{'ircNick'} has quit IRC ($quitmsg)");
-    $conn->quit($quitmsg);
+    if (defined $conn) {
+       $conn->quit($quitmsg);
+    } else {
+       &WARN("quit: could not quit!");
+    }
 }
 
 sub nick {
     my ($nick) = @_;
 
+    if (!defined $nick) {
+       &ERROR("nick: nick == NULL.");
+       return;
+    }
+
+    if (defined $ident and $nick eq $ident) {
+       &WARN("nick: nick == ident == '$ident'.");
+       return;
+    }
+
+    my $bad     = 0;
+    $bad++ if (exists $nuh{ $param{'ircNick'} });
+    $bad++ if (&IsNickInAnyChan($param{'ircNick'}));
+
+    if ($bad) {
+       &WARN("Nick: not going to try and get my nick back. [".
+               scalar(gmtime). "]");
+# hrm... over time we lose track of our own nick.
+#      return;
+    }
+
     if ($nick =~ /^$mask{nick}$/) {
-       rawout("NICK ".$nick);
+       &rawout("NICK ".$nick);
+
+       if (defined $ident) {
+           &status("nick: Changing nick to $nick (from $ident)");
+           # following shouldn't be here :(
+           $ident      = $nick;
+       } else {
+           &DEBUG("first time nick change.");
+           $ident      = $nick;
+       }
+
        return 1;
     }
+    &DEBUG("nick: failed... why oh why (nick => $nick)");
 
     return 0;
 }
 
 sub invite {
     my($who, $chan) = @_;
-    rawout("INVITE $who $chan");
-}
+    # todo: check if $who or $chan are invalid.
 
+    $conn->invite($who, $chan);
+}
 
 ##########
 # Channel related functions...
@@ -492,40 +646,42 @@ sub joinNextChan {
        if (my $i = scalar @joinchan) {
            &status("joinNextChan: $i chans to join.");
        }
+
        return;
     }
 
-    if ($nickserv < 1) {
-       &WARN("jNC: nickserv/chanserv not up.") if (!$nickserv);
-       $nickserv--;
+    # !scalar @joinchan:
+    my @c      = &getJoinChans();
+    if (exists $cache{joinTime} and scalar @c) {
+       my $delta       = time() - $cache{joinTime} - 5;
+       my $timestr     = &Time2String($delta);
+       my $rate        = sprintf("%.1f", $delta / @c);
+       delete $cache{joinTime};
+
+       &status("time taken to join all chans: $timestr; rate: $rate sec/join");
     }
 
-    my %chan = &getChanConfList("chanServ");
-    foreach $chan (keys %chan) {
-       next unless ($chan{$chan} > 0);
-           
-       if (!exists $channels{$chan}{'o'}{$ident}) {
-           &status("ChanServ ==> Requesting ops for $chan.");
-           &rawout("PRIVMSG ChanServ :OP $chan $ident");
-       }
+    # chanserv check: global channels, in case we missed one.
+    foreach ( &ChanConfList("chanServ_ops") ) {
+       &chanServCheck($_);
     }
 }
 
-# Usage: &GetNickInChans($nick,$chan);
-sub GetNickInChans {
+# Usage: &getNickInChans($nick);
+sub getNickInChans {
     my ($nick) = @_;
     my @array;
 
     foreach (keys %channels) {
-       next unless (grep /^\Q$nick\E$/i, keys %{$channels{$_}{''}});
+       next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$_}{''} });
        push(@array, $_);
     }
 
     return @array;
 }
 
-# Usage: &GetNicksInChan($chan);
-sub GetNicksInChan {
+# Usage: &getNicksInChan($chan);
+sub getNicksInChan {
     my ($chan) = @_;
     my @array;
 
@@ -537,17 +693,22 @@ sub IsNickInChan {
 
     $chan =~ tr/A-Z/a-z/;      # not lowercase unfortunately.
 
+    if ($chan =~ /^$/) {
+       &DEBUG("INIC: chan == NULL.");
+       return 0;
+    }
+
     if (&validChan($chan) == 0) {
        &ERROR("INIC: invalid channel $chan.");
        return 0;
     }
 
-    if (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}}) {
+    if (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''} }) {
        return 1;
     } else {
        foreach (keys %channels) {
            next unless (/[A-Z]/);
-           &DEBUG("hash channels contains mixed cased chan!!!");
+           &DEBUG("iNIC: hash channels contains mixed cased chan!!!");
        }
        return 0;
     }
@@ -555,9 +716,10 @@ sub IsNickInChan {
 
 sub IsNickInAnyChan {
     my ($nick) = @_;
+    my $chan;
 
     foreach $chan (keys %channels) {
-       next unless (grep /^\Q$nick\E$/i, keys %{$channels{$chan}{''}});
+       next unless (grep /^\Q$nick\E$/i, keys %{ $channels{$chan}{''}  });
        return 1;
     }
     return 0;
@@ -565,14 +727,25 @@ sub IsNickInAnyChan {
 
 # Usage: &validChan($chan);
 sub validChan {
+    # todo: use $c instead?
     my ($chan) = @_;
 
+    if (!defined $chan or $chan =~ /^\s*$/) {
+       return 0;
+    }
+
     if (lc $chan ne $chan) {
        &WARN("validChan: lc chan != chan. ($chan); fixing.");
        $chan =~ tr/A-Z/a-z/;
     }
 
-    if (exists $channels{$chan}) {
+    # it's possible that this check creates the hash if empty.
+    if (defined $channels{$chan} or exists $channels{$chan}) {
+       if ($chan =~ /^_?default$/) {
+#          &WARN("validC: chan cannot be _default! returning 0!");
+           return 0;
+       }
+
        return 1;
     } else {
        return 0;
@@ -580,13 +753,13 @@ sub validChan {
 }
 
 ###
-# Usage: &DeleteUserInfo($nick,@chans);
-sub DeleteUserInfo {
+# Usage: &delUserInfo($nick,@chans);
+sub delUserInfo {
     my ($nick,@chans) = @_;
     my ($mode,$chan);
 
     foreach $chan (@chans) {
-       foreach $mode (keys %{$channels{$chan}}) {
+       foreach $mode (keys %{ $channels{$chan} }) {
            # use grep here?
            next unless (exists $channels{$chan}{$mode}{$nick});
 
@@ -602,20 +775,24 @@ sub clearChanVars {
 }
 
 sub clearIRCVars {
-    &DEBUG("clearIRCVars() called!");
     undef %channels;
     undef %floodjoin;
 
-    @joinchan  = &getJoinChans();
+    @joinchan          = &getJoinChans(1);
+    $cache{joinTime}   = time();
 }
 
 sub getJoinChans {
+    my($show)  = @_;
     my @chans;
     my @skip;
 
     foreach (keys %chanconf) {
+       next if ($_ eq "_default");
+
        my $val = $chanconf{$_}{autojoin};
        my $skip = 0;
+
        if (defined $val) {
            $skip++ if ($val eq "0");
        } else {
@@ -630,33 +807,48 @@ sub getJoinChans {
        push(@chans, $_);
     }
 
+    my $str;
     if (scalar @skip) {
-       &status("channels not auto-joining: @skip");
+       $str = "channels not auto-joining: @skip (joining: @chans)";
+    } else {
+       $str = "auto-joining all chans: @chans";
     }
 
+    &status("Chans: ".$str) if ($show);
+
     return @chans;
 }
 
 sub closeDCC {
-    &DEBUG("closeDCC called.");
+#    &DEBUG("closeDCC called.");
+    my $type;
 
     foreach $type (keys %dcc) {
        next if ($type ne uc($type));
-       foreach (keys %{$dcc{$type}}) {
-           &DEBUG("closing DCC $type to $_ (FIXME).");
-           $dcc{$type}{$_}->close();
+
+       my $nick;
+       foreach $nick (keys %{ $dcc{$type} }) {
+           next unless (defined $nick);
+           &status("DCC CHAT: closing DCC $type to $nick.");
+           next unless (defined $dcc{$type}{$nick});
+
+           my $ref = $dcc{$type}{$nick};
+           &dccsay($nick, "bye bye, $nick") if ($type =~ /^chat$/i);
+           $dcc{$type}{$nick}->close();
+           delete $dcc{$type}{$nick};
+           &DEBUG("after close for $nick");
        }
+       delete $dcc{$type};
     }
 }
 
 sub joinfloodCheck {
     my($who,$chan,$userhost) = @_;
 
-    return unless (&IsParam("joinfloodCheck"));
+    return unless (&IsChanConf("joinfloodCheck"));
 
     if (exists $netsplit{lc $who}) {   # netsplit join.
-       &DEBUG("jfC: $who was in netsnipe; not checking.");
+       &DEBUG("joinfloodCheck: $who was in netsplit; not checking.");
     }
 
     if (exists $floodjoin{$chan}{$who}{Time}) {
@@ -671,36 +863,49 @@ sub joinfloodCheck {
        my $c = $_;
        my $count = scalar keys %{ $floodjoin{$c} };
        next unless ($count > 5);
-       &DEBUG("count => $count");
+       &DEBUG("joinflood: count => $count");
 
        my $time;
        foreach (keys %{ $floodjoin{$c} }) {
-           $time += $floodjoin{$c}{$_}{Time};
+           my $t = $floodjoin{$c}{$_}{Time};
+           next unless (defined $t);
+
+           $time += $t;
        }
-       &DEBUG("time => $time");
+       &DEBUG("joinflood: time => $time");
        $time /= $count;
 
-       &DEBUG("new time => $time");
+       &DEBUG("joinflood: new time => $time");
     }
 
     ### Clean it up.
     my $delete = 0;
+    my $time = time();
     foreach $chan (keys %floodjoin) {
        foreach $who (keys %{ $floodjoin{$chan} }) {
-           my $time = time() - $floodjoin{$chan}{$who}{Time};
-           next unless ($time > 10);
+           my $t       = $floodjoin{$chan}{$who}{Time};
+           next unless (defined $t);
+
+           my $delta   = $time - $t;
+           next unless ($delta > 10);
+
            delete $floodjoin{$chan}{$who};
            $delete++;
        }
     }
 
-    &DEBUG("jfC: $delete deleted.") if ($delete);
+    &DEBUG("joinfloodCheck: $delete deleted.") if ($delete);
 }
 
 sub getHostMask {
     my($n) = @_;
 
-    &FIXME("getHostMask...");
+    if (exists $nuh{$n}) {
+       return &makeHostMask($nuh{$n});
+    } else {
+       $cache{on_who_Hack} = 1;
+       $conn->who($n);
+    }
 }
 
 1;