]> git.donarmstrong.com Git - infobot.git/blobdiff - src/IRC/Schedulers.pl
- another round of patches from lear. "we love you, lear!" j/k :o
[infobot.git] / src / IRC / Schedulers.pl
index eee0c5918af87452d17a07261201628ff4b12381..b1e28c44a8baca38ef5fd9c1e0e1a9595f9037ff 100644 (file)
@@ -29,7 +29,8 @@ sub setupSchedulers {
     &leakCheck(2);     # mandatory
     &ignoreCheck(1);   # mandatory
     &seenFlushOld(2);
-    &ircCheck(2);      # mandatory
+#    &ircCheck(2);     # mandatory
+    &ircCheck(1);      # mandatory
     &miscCheck(1);     # mandatory
     &miscCheck2(2);    # mandatory
     &shmFlush(1);      # mandatory
@@ -88,7 +89,7 @@ sub randomQuote {
        return if ($_[0] eq "2");       # defer.
     }
 
-    my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.randtext");
+    my $line = &getRandomLineFromFile($bot_data_dir. "/blootbot.randtext");
     if (!defined $line) {
        &ERROR("random Quote: weird error?");
        return;
@@ -245,8 +246,17 @@ sub seenFlushOld {
     my $max_time = &getChanConfDefault("seenMaxDays", 30) *60*60*24;
     my $delete   = 0;
 
-    if ($param{'DBType'} =~ /^pg|postgres|mysql/i) {
-       my $query = "SELECT nick,time FROM seen GROUP BY nick HAVING UNIX_TIMESTAMP() - time > $max_time";
+    if ($param{'DBType'} =~ /^pgsql|mysql/i) {
+       my $query;
+
+       if ($param{'DBType'} =~ /^mysql$/i) {
+           $query = "SELECT nick,time FROM seen GROUP BY nick HAVING ".
+                       "UNIX_TIMESTAMP() - time > $max_time";
+       } else {        # pgsql.
+           $query = "SELECT nick,time FROM seen WHERE ".
+               "extract(epoch from timestamp 'now') - time > $max_time";
+       }
+
        my $sth = $dbh->prepare($query);
        $sth->execute;
 
@@ -261,7 +271,7 @@ sub seenFlushOld {
        my $time = time();
 
        foreach (keys %seen) {
-           my $delta_time = $time - &dbGet("seen", "time", "nick='$_'");
+           my $delta_time = $time - &dbGet("seen", "time", "nick", $_);
            next unless ($delta_time > $max_time);
 
            &DEBUG("seenFlushOld: ".&Time2String($delta_time) );
@@ -362,18 +372,26 @@ sub chanlimitCheck {
        return if ($_[0] eq "2");
     }
 
+    my $str = join(' ', &ChanConfList("chanlimitcheck") );
+
     foreach $chan ( &ChanConfList("chanlimitcheck") ) {
        next unless (&validChan($chan));
 
+       if ($chan eq "_default") {
+           &WARN("chanlimit: we're doing $chan!! HELP ME!");
+           next;
+       }
+
        my $limitplus   = &getChanConfDefault("chanlimitcheckPlus", 5, $chan);
        my $newlimit    = scalar(keys %{ $channels{$chan}{''} }) + $limitplus;
        my $limit       = $channels{$chan}{'l'};
 
        if (scalar keys %netsplitservers) {
            if (defined $limit) {
-               &DEBUG("chanlimit: removing it for $chan.");
+               &status("chanlimit: netsplit; removing it for $chan.");
                &rawout("MODE $chan -l");
                $cache{chanlimitChange}{$chan} = time();
+               &status("chanlimit: netsplit; removed.");
            }
 
            next;
@@ -384,7 +402,6 @@ sub chanlimitCheck {
            ### run NAMES again and flush it.
        }
 
-       next unless (!defined $limit);
        if (defined $limit and $limit == $newlimit) {
            $cache{chanlimitChange}{$chan} = time();
            next;
@@ -399,13 +416,13 @@ sub chanlimitCheck {
        delete $cache{warn}{chanlimit}{$chan};
 
        if (!defined $limit) {
-           &status("ChanLimit: setting for first time or from netsplit, for $chan");
+           &status("chanLimit: setting for first time or from netsplit, for $chan");
        }
 
        if (exists $cache{chanlimitChange}{$chan}) {
            my $delta = time() - $cache{chanlimitChange}{$chan};
            if ($delta < $interval*60) {
-               &DEBUG("not going to change chanlimit! ($delta<$interval*60)");
+               &DEBUG("chanLimit: not going to change chanlimit! ($delta<$interval*60)");
                return;
            }
        }
@@ -419,7 +436,7 @@ sub netsplitCheck {
     my ($s1,$s2);
 
     if (@_) {
-       &ScheduleThis(30, "netsplitCheck");
+       &ScheduleThis(15, "netsplitCheck");
        return if ($_[0] eq "2");
     }
 
@@ -427,16 +444,18 @@ sub netsplitCheck {
     &DEBUG("running netsplitCheck... $cache{netsplitCache}");
 
     if (!scalar %netsplit and scalar %netsplitservers) {
-       &DEBUG("nsc: FIRST!!! ok hash netsplit is NULL; purging hash netsplitservers");
+       &DEBUG("nsC: !hash netsplit but hash netsplitservers <- removing!");
        undef %netsplitservers;
+       return;
     }
 
+    # well... this shouldn't happen since %netsplit code does it anyway.
     foreach $s1 (keys %netsplitservers) {
 
        foreach $s2 (keys %{ $netsplitservers{$s1} }) {
            my $delta = time() - $netsplitservers{$s1}{$s2};
 
-           if ($delta > 3600) {
+           if ($delta > 60*30) {
                &status("netsplit between $s1 and $s2 appears to be stale.");
                delete $netsplitservers{$s1}{$s2};
                &chanlimitCheck();
@@ -449,29 +468,37 @@ sub netsplitCheck {
 
     # %netsplit hash checker.
     my $count  = scalar keys %netsplit;
-    my(@delete);
+    my $delete = 0;
     foreach (keys %netsplit) {
-       if (&IsNickInAnyChan($_)) {
-           &DEBUG("netsplitC: $_ is in some chan; removing from netsplit list.");
+       if (&IsNickInAnyChan($_)) {     # why would this happen?
+#          &DEBUG("nsC: $_ is in some chan; removing from netsplit list.");
            delete $netsplit{$_};
+           $delete++;
            next;
        }
-       # todo: change time value?
-       next unless (time() - $netsplit{$_} > 60*30);
 
-       push(@delete, $_);
+       next unless (time() - $netsplit{$_} > 60*15);
+
+       $delete++;
        delete $netsplit{$_};
     }
 
-    if (@delete) {
-       my $str = scalar(@delete)."/".scalar(keys %netsplit);
-       &DEBUG("removed from netsplit list ($str): @delete");
+    # yet another hack.
+    foreach (keys %channels) {
+       my $i = $cache{maxpeeps}{$chan} || 0;
+       my $j = scalar(keys %{ $channels{$chan} });
+       next unless ($i > 10 and 0.25*$i > $j);
+
+       &DEBUG("netsplit: 0.25*max($i) > current($j); possible netsplit?");
+    }
+
+    if ($delete) {
+       my $j = scalar(keys %netsplit);
+       &DEBUG("nsC: removed from netsplit list: (before: $count; after: $j)");
     }
-    &DEBUG("nsC: netsplitservers: ".scalar(keys %netsplitservers) );
-    &DEBUG("nsC: netsplit: ".scalar(keys %netsplit) );
 
     if (!scalar %netsplit and scalar %netsplitservers) {
-       &DEBUG("nsc: ok hash netsplit is NULL; purging hash netsplitservers");
+       &DEBUG("nsC: ok hash netsplit is NULL; purging hash netsplitservers");
        undef %netsplitservers;
     }
 
@@ -523,10 +550,10 @@ sub seenFlush {
     $stats{'new'}      = 0;
     $stats{'old'}      = 0;
 
-    if ($param{'DBType'} =~ /^mysql|pg|postgres/i) {
+    if ($param{'DBType'} =~ /^(mysql|pgsql)$/i) {
        foreach $nick (keys %seencache) {
-           my $retval = &dbReplace("seen", (
-                       "nick" => $seencache{$nick}{'nick'},
+           my $retval = &dbReplace("seen", "nick", (
+                       "nick" => lc $seencache{$nick}{'nick'},
                        "time" => $seencache{$nick}{'time'},
                        "host" => $seencache{$nick}{'host'},
                        "channel" => $seencache{$nick}{'chan'},
@@ -541,7 +568,7 @@ sub seenFlush {
            ### old code.
            ###
 
-           my $exists = &dbGet("seen", "nick", "nick='$nick'");
+           my $exists = &dbGet("seen", "nick", "nick=".&dbQuote($nick) );
 
            if (defined $exists and $exists) {
                &dbUpdate("seen", "nick", $nick, (
@@ -687,7 +714,7 @@ sub ignoreCheck {
 
 sub ircCheck {
     if (@_) {
-       &ScheduleThis(60, "ircCheck");
+       &ScheduleThis(15, "ircCheck");
        return if ($_[0] eq "2");       # defer.
     }
 
@@ -790,8 +817,8 @@ sub miscCheck {
     }
 
     # make backup of important files.
-    &mkBackup( $bot_misc_dir."/blootbot.chan", 60*60*24*3);
-    &mkBackup( $bot_misc_dir."/blootbot.users", 60*60*24*3);
+    &mkBackup( $bot_state_dir."/blootbot.chan", 60*60*24*3);
+    &mkBackup( $bot_state_dir."/blootbot.users", 60*60*24*3);
     &mkBackup( $bot_base_dir."/blootbot-news.txt", 60*60*24*1);
 
     # flush cache{lobotomy}
@@ -826,14 +853,19 @@ sub miscCheck2 {
     my ($day,$month,$year) = (localtime(time()))[3,4,5];
     my $date = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
 
-    opendir(DIR,"$bot_base_dir/log");
+    if (!opendir(DIR,"$bot_log_dir")) {
+       &ERROR("misccheck2: log dir $bot_log_dir does not exist.");
+       closedir DIR;
+       return -1;
+    }
+
     while (my $f = readdir(DIR)) {
-       next unless ( -f "$bot_base_dir/log/$f");
+       next unless ( -f "$bot_log_dir/$f");
        next if ($f =~ /gz|bz2/);
        next unless ($f =~ /(\d{8})/);
        next if ($date eq $1);
 
-       &compress("$bot_base_dir/log/$f");
+       &compress("$bot_log_dir/$f");
     }
     closedir DIR;
 }
@@ -1078,7 +1110,7 @@ sub factoidCheck {
        my $fix = $_;
        $fix =~ s/ #DEL#$//g;
        my $agestr = &Time2String($time - $age);
-       &DEBUG("safedel: Removing '$_' for good. [$agestr old]");
+       &status("safedel: Removing '$_' for good. [$agestr old]");
 
        &delFactoid($_);
     }
@@ -1168,20 +1200,20 @@ sub mkBackup {
     my $backup         = 0;
 
     if (! -f $file) {
-       &WARN("mkB: file '$file' does not exist.");
+       &VERB("mkB: file '$file' does not exist.",2);
        return;
     }
 
+    my $age    = "New";
     if ( -e "$file~" ) {
-       $backup++ if ((stat $file)[9] - (stat "$file~")[9] > $time);
+       $backup++       if ((stat $file)[9] - (stat "$file~")[9] > $time);
+       my $delta       = time() - (stat "$file~")[9];
+       $age            = &Time2String($delta);
     } else {
        $backup++;
     }
-    return unless ($backup);
 
-    # should delta be time(file) - time(file~)?
-    my $delta  = time() - (stat "$file~")[9];
-    my $age    = &Time2String($delta);
+    return unless ($backup);
 
     ### TODO: do internal copying.
     &status("Backup: $file ($age)");