]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Misc.pl
converted %{$blah{$blah}} to %{ $blah{$blah} }
[infobot.git] / src / Misc.pl
index d105bf65209d02ab84f8d4134e231e675a4909b6..3db0cdce4d6c39adcfd7ee2071c677600bfd08fb 100644 (file)
@@ -12,6 +12,9 @@ sub help {
     my $file  = $bot_misc_dir."/blootbot.help";
     my %help  = ();
 
+    # crude hack for pSReply() to work as expected.
+    $msgType = "private" if ($msgType eq "public");
+
     if (!open(FILE, $file)) {
        &ERROR("FAILED loadHelp ($file): $!");
        return;
@@ -164,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;
@@ -177,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);
 }
 
 ###
@@ -200,7 +208,7 @@ sub fixFileList {
     # sort the hash list appropriately.
     foreach (sort keys %files) {
        my $file = $_;
-       my @keys = sort keys %{$files{$file}};
+       my @keys = sort keys %{ $files{$file} };
        my $i    = scalar(@keys);
 
        if ($i > 1) {
@@ -229,8 +237,9 @@ sub fixString {
        s/\s+/ /g;              # remove excessive whitespaces.
 
        next unless (defined $level);
-       &DEBUG("strip control chars?");
-       s/[\cA-\c_]//ig         # remove control characters.
+       if (s/[\cA-\c_]//ig) {          # remove control characters.
+           &DEBUG("stripped control chars");
+       }
     }
 
     return $str;
@@ -245,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") {
@@ -349,7 +363,7 @@ sub getRandomInt {
 
     srand();
 
-    if ($str =~ /^(\d+)$/) {
+    if ($str =~ /^(\d+(\.\d+)?)$/) {
        my $i = $1;
        my $fuzzy = int(rand 5);
        if ($i < 10) {
@@ -423,10 +437,24 @@ sub IsHostMatch {
 sub isStale {
     my ($file, $age) = @_;
 
+    if (!defined $age) {
+       &WARN("isStale: age == NULL.");
+       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;
 }
 
@@ -598,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;
     }
@@ -656,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;