]> git.donarmstrong.com Git - infobot.git/blobdiff - src/IRC/Irc.pl
- moved scripts/setup_sql.pl to src/db_mysql as &createTables()
[infobot.git] / src / IRC / Irc.pl
index 36d57aae2ad52fe5a41f79e7c40c65d92577af65..49c931db836df443719adcb48033078e654f3722 100644 (file)
@@ -40,25 +40,25 @@ loop:;
        }
        next unless (exists $ircPort{$host});
 
-       my $retval = &irc($host, $ircPort{$host});
-       &DEBUG("ircloop: after irc()");
-
+       my $retval      = &irc($host, $ircPort{$host});
        next unless (defined $retval and $retval == 0);
-
        $error++;
 
        if ($error % 3 == 0 and $error != 0) {
-           &ERROR("CANNOT connect to this server; next!");
+           &status("IRC: Could not connect.");
+           &status("IRC: ");
            next;
        }
 
-       if ($error >= 3*3) {
-           &ERROR("CANNOT connect to any irc server; stopping.");
+       if ($error >= 3*2) {
+           &status("IRC: cannot connect to any IRC servers; stopping.");
+           &shutdown();
            exit 1;
        }
     }
 
-    &DEBUG("ircloop: end... going back.");
+    &status("IRC: ok, done one cycle of IRC servers; trying again.");
+
     &loadIRCServers();
     goto loop;
 }
@@ -90,13 +90,15 @@ sub 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'});
+
+    $conn = $irc->newconn(%args);
 
     if (!defined $conn) {
        &ERROR("irc: conn was not created!defined!!!");
@@ -107,6 +109,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";
 
@@ -136,6 +140,7 @@ sub irc {
        $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(315, \&on_endofwho);
        $conn->add_global_handler(324, \&on_modeis);
        $conn->add_global_handler(333, \&on_topicinfo);
        $conn->add_global_handler(352, \&on_who);
@@ -191,11 +196,16 @@ sub say {
            $pubcount++;
            $pubsize += length $msg;
 
-           if ( ($pubcount % 4) == 0 and $pubcount) {
+           my $i = &getChanConfDefault("sendPublicLimitLines", 3);
+           my $j = &getChanConfDefault("sendPublicLimitBytes", 1000);
+
+           if ( ($pubcount % $i) == 0 and $pubcount) {
                sleep 1;
-           } elsif ($pubsize > 1500) {
+               $pubsleep++;
+           } elsif ($pubsize > $j) {
                sleep 1;
-               $pubsize -= 1500;
+               $pubsize -= $j;
+               $pubsleep++;
            }
 
        } else {
@@ -230,11 +240,15 @@ sub msg {
            $msgcount++;
            $msgsize += length $msg;
 
-           if ( ($msgcount % 4) == 0 and $msgcount) {
+           my $i = &getChanConfDefault("sendPrivateLimitLines", 3);
+           my $j = &getChanConfDefault("sendPrivateLimitBytes", 1000);
+           if ( ($msgcount % $i) == 0 and $msgcount) {
                sleep 1;
-           } elsif ($msgsize > 1000) {
+               $msgsleep++;
+           } elsif ($msgsize > $j) {
                sleep 1;
-               $msgsize -= 1000;
+               $msgsize -= $j;
+               $msgsleep++;
            }
 
        } else {
@@ -283,11 +297,16 @@ sub notice {
        $notcount++;
        $notsize += length $txt;
 
-       if ( ($notcount % 3) == 0 and $notcount) {
+       my $i = &getChanConfDefault("sendNoticeLimitLines", 3);
+       my $j = &getChanConfDefault("sendNoticeLimitBytes", 1000);
+
+       if ( ($notcount % $i) == 0 and $notcount) {
            sleep 1;
-       } elsif ($notsize > 1000) {
+           $notsleep++;
+       } elsif ($notsize > $j) {
            sleep 1;
-           $notsize -= 1000;
+           $notsize -= $j;
+           $notsleep++;
        }
 
     } else {
@@ -299,7 +318,6 @@ sub notice {
     $conn->notice($target, $txt);
 }
 
-
 sub DCCBroadcast {
     my ($txt,$flag) = @_;
 
@@ -541,17 +559,45 @@ sub unban {
 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'.");
+    }
+
+    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(localtime). "]");
+       return;
+    }
+
     if ($nick =~ /^$mask{nick}$/) {
-       &DEBUG("nick: Changing nick to $nick (from $ident)");
        rawout("NICK ".$nick);
-       # unfortunately, on_nick doesn't catch ourself.
-       $ident  = $nick;
+
+       if (defined $ident) {
+           &status("nick: Changing nick to $nick (from $ident)");
+       } else {
+           &DEBUG("first time nick change.");
+           $ident      = $nick;
+       }
+
        return 1;
     }
     &DEBUG("nick: failed... why oh why (nick => $nick)");
@@ -571,8 +617,6 @@ sub invite {
 
 # Usage: &joinNextChan();
 sub joinNextChan {
-    &DEBUG("joinNextChan called.");
-
     if (scalar @joinchan) {
        $chan = shift @joinchan;
        &joinchan($chan);
@@ -581,25 +625,29 @@ sub joinNextChan {
            &status("joinNextChan: $i chans to join.");
        }
 
-    } else {
-       if (exists $cache{joinTime}) {
-           my $delta   = time() - $cache{joinTime};
-           my $timestr = &Time2String($delta);
-           my $rate    = sprintf("%.1f", $delta / &getJoinChans() );
-           delete $cache{joinTime};
+       return;
 
-           &DEBUG("time taken to join all chans: $timestr; rate: $rate sec/join");
-       }
-       # chanserv check: global channels, in case we missed one.
+    }
 
-       foreach ( &ChanConfList("chanServ_ops") ) {
-           &chanServCheck($_);
-       }
+    # !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};
+
+       &DEBUG("time taken to join all chans: $timestr; rate: $rate sec/join");
+    }
+
+    # 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;
 
@@ -611,8 +659,8 @@ sub GetNickInChans {
     return @array;
 }
 
-# Usage: &GetNicksInChan($chan);
-sub GetNicksInChan {
+# Usage: &getNicksInChan($chan);
+sub getNicksInChan {
     my ($chan) = @_;
     my @array;
 
@@ -667,8 +715,8 @@ sub validChan {
 }
 
 ###
-# Usage: &DeleteUserInfo($nick,@chans);
-sub DeleteUserInfo {
+# Usage: &delUserInfo($nick,@chans);
+sub delUserInfo {
     my ($nick,@chans) = @_;
     my ($mode,$chan);
 
@@ -689,7 +737,7 @@ sub clearChanVars {
 }
 
 sub clearIRCVars {
-###    &DEBUG("clearIRCVars() called!");
+#    &DEBUG("clearIRCVars() called!");
     undef %channels;
     undef %floodjoin;
 
@@ -724,12 +772,12 @@ sub getJoinChans {
 
     my $str;
     if (scalar @skip) {
-       $str = "gJC: channels not auto-joining: @skip";
+       $str = "channels not auto-joining: @skip (joining: @chans)";
     } else {
-       $str = "gJC: auto-joining all chans.";
+       $str = "auto-joining all chans: @chans";
     }
 
-    &status($str) if ($show);
+    &status("Chans: ".$str) if ($show);
 
     return @chans;
 }