]> git.donarmstrong.com Git - infobot.git/blobdiff - src/UserExtra.pl
- remove several debug messages; convert some to VERB()
[infobot.git] / src / UserExtra.pl
index 075e0c2703b8d4f19e2432efc69da1ee614fb4d8..92bb19537093209d74ca076a65a90b50e52d80c2 100644 (file)
@@ -30,8 +30,13 @@ use vars qw(%channels %chanstats %cmdstats);
        Help => 'nslookup', Identifier => 'allowDNS',
        Forker => "NULL", ) );
 &addCmdHook("main", 'tell|explain', ('CODEREF' => 'tell', 
-       Help => 'tell', Identifier => 'allowTelling', ) );
-
+       Help => 'tell', Identifier => 'allowTelling',
+       Cmdstats => 'Tell') );
+&addCmdHook("main", 'news', ('CODEREF' => 'News::Parse', 
+       Module => 'news', ) );
+&addCmdHook("main", 'countrystats', ('CODEREF' => 'countryStats', 
+#      Forker => "NULL",
+ ) );
 
 &status("CMD: loaded ".scalar(keys %hooks_main)." MAIN command hooks.");
 
@@ -57,13 +62,13 @@ sub chaninfo {
                &ircCheck();
                next;
            }
-           push(@array, "$_ (".scalar(keys %{$channels{$_}{''}}).")");
+           push(@array, "$_ (".scalar(keys %{ $channels{$_}{''} }).")");
        }
        &pSReply($reply.": ".join(' ', @array));
 
        ### total user count.
        foreach $chan (keys %channels) {
-           $tucount += scalar(keys %{$channels{$chan}{''}});
+           $tucount += scalar(keys %{ $channels{$chan}{''} });
        }
 
        ### unique user count.
@@ -75,6 +80,10 @@ sub chaninfo {
                push(@nicks, $_);
            }
        }
+       &DEBUG("nicks => '".scalar(@nicks)."'...");
+       if (scalar @nicks != $uucount) {
+           &DEBUG("nicks != uucount...");
+       }
 
        my $chans = scalar(keys %channels);
        &pSReply(
@@ -96,7 +105,7 @@ sub chaninfo {
 
     # Step 1:
     my @array;
-    foreach (sort keys %{$chanstats{$chan}}) {
+    foreach (sort keys %{ $chanstats{$chan} }) {
        my $int = $chanstats{$chan}{$_};
        next unless ($int);
 
@@ -116,7 +125,7 @@ sub chaninfo {
                - $chanstats{$chan}{'Part'};
 
     if ($delta_stats) {
-       my $total = scalar(keys %{$channels{$chan}{''}});
+       my $total = scalar(keys %{ $channels{$chan}{''} });
        &status("chaninfo: join ~= signoff + part (drift of $delta_stats < $total).");
 
        if ($delta_stats > $total) {
@@ -128,7 +137,7 @@ sub chaninfo {
     undef @array;
     my $type;
     foreach ("v","o","") {
-       my $int = scalar(keys %{$channels{$chan}{$_}});
+       my $int = scalar(keys %{ $channels{$chan}{$_} });
        next unless ($int);
 
        $type = "Voice" if ($_ eq "v");
@@ -140,11 +149,15 @@ 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 %new;
+    foreach (keys %userstats) {
+       next unless (exists $userstats{$_}{'Count'});
+       $new{$_} = $userstats{$_}{'Count'};
+    }
+
     my($count) = (sort { $b <=> $a } 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.");
 }
@@ -160,14 +173,14 @@ sub cmdstats {
 
     my %countstats;
     foreach (keys %cmdstats) {
-       $countstats{$cmdstats{$_}}{$_} = 1;
+       $countstats{ $cmdstats{$_} }{$_} = 1;
     }
 
     foreach (sort {$b <=> $a} keys %countstats) {
        my $int = $_;
        next unless ($int);
 
-       foreach (keys %{$countstats{$int}}) {
+       foreach (keys %{ $countstats{$int} }) {
            push(@array, "\002$int\002 of $_");
        }
     }
@@ -399,6 +412,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.
@@ -407,7 +480,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);
@@ -419,10 +495,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)) {
@@ -438,7 +520,7 @@ sub userCommands {
 
     # hex.
     if ($message =~ /^hex(\s+(.*))?$/i) {
-       return unless (&IsParam("allowConv"));
+       return unless (&hasParam("allowConv"));
        my $arg = $2;
 
        if (!defined $arg) {
@@ -462,13 +544,27 @@ sub userCommands {
     }
 
     # crypt.
-    if ($message =~ /^crypt\s+(\S+)\s*(?:,| )\s*(\S+)/) {
-       # word salt.
-       &pSReply(crypt($1, $2));
-       return;
-    }
+    if ($message =~ /^crypt(\s+(.*))?$/i) {
+       my @args        = split /\s+/, $2;
+
+       if (!scalar @args or scalar @args > 2) {
+           &help("crypt");
+           return;
+       }
 
+       if (scalar @args == 2) {
+           if (length $args[0] != 2) {
+               &msg($who, "invalid format...");
+               return;
+           }
 
+           &pSReply( crypt($args[1], $args[0]) );
+       } else {
+           &pSReply( &mkcrypt($args[0]) );
+       }
+
+       return;
+    }
 
     # cycle.
     if ($message =~ /^(cycle)(\s+(\S+))?$/i) {
@@ -602,17 +698,29 @@ sub userCommands {
     }
 
     # ircstats.
-    if ($message =~ /^ircstats$/i) {
+    if ($message =~ /^ircstats?$/i) {
+       $ircstats{'TotalTime'}  ||= 0;
+       $ircstats{'OffTime'}    ||= 0;
+
        my $count       = $ircstats{'ConnectCount'};
        my $format_time = &Time2String(time() - $ircstats{'ConnectTime'});
-       my $total_time  = time() - $ircstats{'ConnectTime'} + $ircstats{'TotalTime'};
+       my $total_time  = time() - $ircstats{'ConnectTime'} +
+                               $ircstats{'TotalTime'};
        my $reply;
 
-       &DEBUG("ircstats: total_time => $total_time.");
-       &DEBUG("ircstats: offtime => $ircstats{'OffTime'}");
+       my $connectivity = 100 * ($total_time - $ircstats{'OffTime'}) /
+                               $total_time;
+       my $p = sprintf("%.03f", $connectivity);
+       $p =~ s/(\.\d*)0+$/$1/;
+       if ($p =~ s/\.0$//) {
+           # this should not happen... but why...
+       } else {
+           $p =~ s/\.$//
+       }
 
-       foreach (keys %ircstats) {
-           &DEBUG("ircstats: $_ => '$ircstats{$_}'.");
+       if ($total_time != (time() - $ircstats{'ConnectTime'}) ) {
+           my $tt_format = &Time2String($total_time);
+           &DEBUG("tt_format => $tt_format");
        }
 
        ### RECONNECT COUNT.
@@ -622,13 +730,14 @@ sub userCommands {
        } else {
            $reply = "Currently I'm hooked up to $ircstats{'Server'} but only".
                " for $format_time.  ".
-               "I had to reconnect \002$count\002 times.";
+               "I had to reconnect \002$count\002 times.".
+               "   Connectivity: $p %";
        }
 
        ### REASON.
        my $reason = $ircstats{'DisconnectReason'};
        if (defined $reason) {
-           $reply .= "  I was last disconnected for '$reason'.";
+           $reply .= ".  I was last disconnected for '$reason'.";
        }
 
        &pSReply($reply);