]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Misc.pl
- irctextcounters: add percentage to top3
[infobot.git] / src / Misc.pl
index b190857e494deb4ed4c2793885ebce9a946b1b3a..ffc6fb6467a5f19784abed4c9969873d57b82998 100644 (file)
@@ -167,20 +167,26 @@ 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;
     my $h = int($time / 3600) % 24;
     my $d = int($time / 86400);
 
-    $retval .= sprintf(" \002%d\002d", $d) if ($d != 0);
-    $retval .= sprintf(" \002%d\002h", $h) if ($h != 0);
-    $retval .= sprintf(" \002%d\002m", $m) if ($m != 0);
-    $retval .= sprintf(" \002%d\002s", $s) if ($s != 0);
+    my @data;
+    push(@data, sprintf("\002%d\002d", $d)) if ($d != 0);
+    push(@data, sprintf("\002%d\002h", $h)) if ($h != 0);
+    push(@data, sprintf("\002%d\002m", $m)) if ($m != 0);
+    push(@data, sprintf("\002%d\002s", $s)) if ($s != 0 or !@data);
 
-    return substr($retval, 1);
+    return $prefix.join(' ', @data);
 }
 
 ###
@@ -203,7 +209,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) {
@@ -442,12 +448,16 @@ sub isStale {
        return 1;
     }
 
+    &DEBUG("!exist $file") if (! -f $file);
+
     return 1 unless ( -f $file);
     if ($file =~ /idx/) {
-       my $age = time() - (stat($file))[9];
-       &DEBUG("stale: $age. (". &Time2String($age) .")");
+       my $age2 = time() - (stat($file))[9];
+       &VERB("stale: $age2. (". &Time2String($age2) .")",2);
     }
-    return 1 if (time() - (stat($file))[9] > $age*60*60*24);
+    $age *= 60*60*24 if ($age >= 0 and $age < 30);
+
+    return 1 if (time() - (stat($file))[9] > $age);
     return 0;
 }
 
@@ -457,15 +467,21 @@ sub isStale {
 
 # Usage: &makeHostMask($host);
 sub makeHostMask {
-    my ($host) = @_;
+    my ($host) = @_;
+    my $nu     = "";
+
+    if ($host =~ s/^(\S+!\S+\@)//) {
+       &DEBUG("mHM: detected nick!user\@ for host arg; fixing");
+       $nu = $1;
+    }
 
     if ($host =~ /^$mask{ip}$/) {
-       return "$1.$2.$3.*";
+       return $nu."$1.$2.$3.*";
     }
 
     my @array = split(/\./, $host);
-    return $host if (scalar @array <= 3);
-    return "*.".join('.',@{array}[1..$#array]);
+    return $nu.$host if (scalar @array <= 3);
+    return $nu."*.".join('.',@{array}[1..$#array]);
 }
 
 # Usage: &makeRandom(int);
@@ -676,4 +692,26 @@ 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);
+}
+
+sub closeStats {
+    return unless (&getChanConfList("ircTextCounters"));
+
+    foreach (keys %cmdstats) {
+       my $type        = $_;
+       my $i = &dbGet("stats", "counter", "nick=".&dbQuote($type).
+                       " AND type='cmdstats'");
+       $i      += $cmdstats{$type};
+
+       &dbReplace("stats",
+               (nick => $type, type => "cmdstats", counter => $i)
+       );
+    }
+}
+
 1;