]> git.donarmstrong.com Git - infobot.git/blobdiff - src/UserExtra.pl
- moved scripts/setup_sql.pl to src/db_mysql as &createTables()
[infobot.git] / src / UserExtra.pl
index 9923a092115a3aa652ae4c7d241a1d12bef254fb..2fd29f94b8f3664965929a1e86c266efb3e285eb 100644 (file)
@@ -33,8 +33,10 @@ use vars qw(%channels %chanstats %cmdstats);
        Help => 'tell', Identifier => 'allowTelling',
        Cmdstats => 'Tell') );
 &addCmdHook("main", 'news', ('CODEREF' => 'News::Parse', 
-       Module => 'news', ) );
-#      Module => 'news', Identifier => 'news') );
+       Module => 'news', 'Cmdstats' => 'News' ) );
+&addCmdHook("main", 'countrystats', ('CODEREF' => 'countryStats', 
+#      Forker => "NULL",
+ ) );
 
 &status("CMD: loaded ".scalar(keys %hooks_main)." MAIN command hooks.");
 
@@ -147,11 +149,20 @@ sub chaninfo {
     $reply .= ".  At the moment, ". &IJoin(@array);
 
     # Step 3:
-    ### TODO: what's wrong with the following?
-    my %new = map { $userstats{$_}{'Count'} => $_ } keys %userstats;
-    my($count) = (sort { $b <=> $a } keys %new)[0];
+    my %new;
+    foreach (keys %userstats) {
+       next unless (exists $userstats{$_}{'Count'});
+       if ($userstats{$_}{'Count'} =~ /^\D+$/) {
+           &WARN("userstats{$_}{Count} is non-digit.");
+           next;
+       }
+
+       $new{$_} = $userstats{$_}{'Count'};
+    }
+
+    my($count) = (sort { $a <=> $b } keys %new)[0];
     if ($count) {
-       $reply .= ".  \002$new{$count}\002 has said the most with a total of \002$count\002 messages";
+       $reply .= ".  \002$count\002 has said the most with a total of \002$new{$count}\002 messages";
     }
     &pSReply("$reply.");
 }
@@ -346,7 +357,7 @@ sub tell {
        $message        = $tell_obj;
        $done++ unless (&Modules());
 
-       &DEBUG("setting old values of who and msgType.");
+       &VERB("teel: setting old values of who and msgType.",2);
        $who            = $oldwho;
        $msgType        = $oldmtype;
 
@@ -406,6 +417,66 @@ sub DNS {
     &performReply($result);
 }
 
+sub countryStats {
+    if (exists $cache{countryStats}) {
+       &msg($who,"countrystats is already running!");
+       return;
+    }
+
+    if ($chan eq "") {
+       $chan = $_[0];
+    }
+
+    if ($chan eq "") {
+       &help("countrystats");
+       return;
+    }
+
+    &rawout("WHO $chan");
+    $cache{countryStats}{chan} = $chan;
+    $cache{countryStats}{mtype}        = $msgType;
+    $cache{countryStats}{who}  = $who;
+    $cache{on_who_Hack}                = 1;
+}
+
+sub do_countrystats {
+    $chan      = $cache{countryStats}{chan};
+    $msgType   = $cache{countryStats}{mtype};
+    $who       = $cache{countryStats}{who};
+
+    my $total  = 0;
+    my %cstats;
+    foreach (keys %{ $cache{nuhInfo} }) {
+       my $h = $cache{nuhInfo}{$_}{Host};
+
+       if ($h =~ /^.*\.(\D+)$/) {      # host
+           $cstats{$1}++;
+       } else {                        # ip
+           $cstats{unresolve}++;
+       }
+       $total++;
+    }
+    my %count;
+    foreach (keys %cstats) {
+       $count{ $cstats{$_} }{$_} = 1;
+    }
+
+    my @list;
+    foreach (sort {$b <=> $a} keys %count) {
+       my $str = join(", ", sort keys %{ $count{$_} });
+#      push(@list, "$str ($_)");
+       my $perc        = sprintf("%.01f", 100 * $_ / $total);
+       $perc           =~ s/\.0+$//;
+       push(@list, "$str ($_, $perc %)");
+    }
+
+    # todo: move this into a scheduler like nickometer
+    $msgType   = "private";
+    &pSReply( &formListReply(0, "Country Stats ", @list) );
+
+    delete $cache{countryStats};
+    delete $cache{on_who_Hack};
+}
 
 ###
 ### amalgamated commands.
@@ -414,7 +485,10 @@ sub DNS {
 sub userCommands {
     # conversion: ascii.
     if ($message =~ /^(asci*|chr) (\d+)$/) {
-       return unless (&IsParam("allowConv"));
+       &DEBUG("ascii/chr called ...");
+       return unless (&hasParam("allowConv"));
+
+       &DEBUG("ascii/chr called");
 
        $arg    = $2;
        $result = chr($arg);
@@ -426,10 +500,16 @@ sub userCommands {
     }
 
     # conversion: ord.
-    if ($message =~ /^ord (.)$/) {
-       return unless (&IsParam("allowConv"));
+    if ($message =~ /^ord(\s+(.*))$/) {
+       return unless (&hasParam("allowConv"));
+
+       $arg = $2;
+
+       if (!defined $arg or length $arg != 1) {
+           &help("ord");
+           return;
+       }
 
-       $arg = $1;
        if (ord($arg) < 32) {
            $arg = chr(ord($arg) + 64);
            if ($arg eq chr(64)) {
@@ -445,7 +525,7 @@ sub userCommands {
 
     # hex.
     if ($message =~ /^hex(\s+(.*))?$/i) {
-       return unless (&IsParam("allowConv"));
+       return unless (&hasParam("allowConv"));
        my $arg = $2;
 
        if (!defined $arg) {
@@ -603,21 +683,26 @@ sub userCommands {
 
        # utime(13) + stime(14).
        my $cpu_usage   = sprintf("%.01f", ($data[13]+$data[14]) / 100 );
+       # cutime(15) + cstime (16).
+       my $cpu_usage2  = sprintf("%.01f", ($data[15]+$data[16]) / 100 );
        my $time        = time() - $^T;
        my $raw_perc    = $cpu_usage*100/$time;
+       my $raw_perc2   = $cpu_usage2*100/$time;
        my $perc;
+       my $perc2;
 
        if ($raw_perc > 1) {
            $perc       = sprintf("%.01f", $raw_perc);
+           $perc2      = sprintf("%.01f", $raw_perc2);
        } elsif ($raw_perc > 0.1) {
            $perc       = sprintf("%.02f", $raw_perc);
+           $perc2      = sprintf("%.02f", $raw_perc2);
        } else {                        # <=0.1
            $perc       = sprintf("%.03f", $raw_perc);
+           $perc2      = sprintf("%.03f", $raw_perc2);
        }
 
-       &pSReply("Total CPU usage: $cpu_usage s ... Percentage CPU used: $perc %");
-       &DEBUG("15 => $data[15] (cutime)");
-       &DEBUG("16 => $data[16] (cstime)");
+       &pSReply("Total CPU usage: $cpu_usage s ... Percentage CPU used: $perc % (+childs: $perc2 %)");
 
        return;
     }
@@ -675,6 +760,10 @@ sub userCommands {
        my $startString = scalar(localtime $^T);
        my $upString    = &Time2String(time() - $^T);
        my $count       = &countKeys("factoids");
+       $count{'Commands'}      = 0;
+       foreach (keys %cmdstats) {
+           $count{'Commands'} += $cmdstats{$_};
+       }
 
        &pSReply(
        "Since $startString, there have been".
@@ -686,6 +775,8 @@ sub userCommands {
                &fixPlural("dunno",$count{'Dunno'}).
          " and \002$count{'Moron'}\002 ".
                &fixPlural("moron",$count{'Moron'}).
+         " and \002$count{'Commands'}\002 ".
+               &fixPlural("command",$count{'Commands'}).
          ".  I have been awake for $upString this session, and ".
          "currently reference \002$count\002 factoids.  ".
          "I'm using about \002$memusage\002 ".