]> git.donarmstrong.com Git - infobot.git/blobdiff - blootbot/src/Misc.pl
...
[infobot.git] / blootbot / src / Misc.pl
index 22cd0ea571df8f3d9379fad3662d273e10d783dc..be5e034a0343c3c6e715591e36101bfd52f71efb 100644 (file)
@@ -167,8 +167,13 @@ sub Time2String {
     my $time = shift;
     my $retval;
 
-    return("0s")
-               if (!defined $time or $time !~ /\d+/ or $time <= 0);
+    return("NULL s") if (!defined $time or $time !~ /\d+/);
+
+    my $prefix = "";
+    if ($time < 0) {
+       $time = - $time;
+       $prefix = "- ";
+    }
 
     my $s = int($time) % 60;
     my $m = int($time / 60) % 60;
@@ -180,7 +185,7 @@ sub Time2String {
     $retval .= sprintf(" \002%d\002m", $m) if ($m != 0);
     $retval .= sprintf(" \002%d\002s", $s) if ($s != 0);
 
-    return substr($retval, 1);
+    return $prefix.substr($retval, 1);
 }
 
 ###
@@ -249,6 +254,11 @@ sub fixPlural {
        return;
     }
 
+    if (!defined $int or $int =~ /^\D+$/) {
+       &WARN("fixPlural: int != defined or int");
+       return $str;
+    }
+
     if ($str eq "has") {
        $str = "have"   if ($int > 1);
     } elsif ($str eq "is") {
@@ -353,7 +363,7 @@ sub getRandomInt {
 
     srand();
 
-    if ($str =~ /^(\d+)$/) {
+    if ($str =~ /^(\d+(\.\d+)?)$/) {
        my $i = $1;
        my $fuzzy = int(rand 5);
        if ($i < 10) {
@@ -432,10 +442,19 @@ sub isStale {
        return 1;
     }
 
+    if (!defined $file) {
+       &WARN("isStale: file == NULL.");
+       return 1;
+    }
+
     return 1 unless ( -f $file);
-    return 1 if (time() - (stat($file))[9] > $age*60*60*24);
-    my $delta = time() - (stat($file))[9];
-    my $hage  = $age*60*60*24;
+    if ($file =~ /idx/) {
+       my $age2 = time() - (stat($file))[9];
+       &DEBUG("stale: $age2. (". &Time2String($age2) .")");
+    }
+    $age *= 60*60*24 if ($age >= 0 and $age < 30);
+
+    return 1 if (time() - (stat($file))[9] > $age);
     return 0;
 }
 
@@ -607,14 +626,13 @@ sub hasProfanity {
     return $profanity;
 }
 
-### rename to hasChanConf() ?
 sub hasParam {
     my ($param) = @_;
 
-    ### TODO: specific reason why it failed.
-    if (&IsChanConf($param)) {
+    if (&IsChanConf($param) or &IsParam($param)) {
        return 1;
     } else {
+       ### TODO: specific reason why it failed.
        &msg($who, "unfortunately, \002$param\002 is disabled in my configuration") unless ($addrchar);
        return 0;
     }
@@ -665,4 +683,11 @@ sub closePID {
     return 0 if ( -f $file{PID});
 }
 
+sub mkcrypt {
+    my($str) = @_;
+    my $salt = join '',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64];
+
+    return crypt($str, $salt);
+}
+
 1;