X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=src%2FUserExtra.pl;h=067370bf861739b88093dee1d38a6f0753ed61a5;hb=c8f1d48a747128e1af914736ccc777ea7b5add44;hp=9923a092115a3aa652ae4c7d241a1d12bef254fb;hpb=c6ea16a8cf7e3bc0e1ef68d6f795520e6db4aebe;p=infobot.git diff --git a/src/UserExtra.pl b/src/UserExtra.pl index 9923a09..067370b 100644 --- a/src/UserExtra.pl +++ b/src/UserExtra.pl @@ -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."); } @@ -208,7 +219,7 @@ sub factstats { sub karma { my $target = lc( shift || $who ); - my $karma = &dbGet("karma", "nick",$target,"karma") || 0; + my $karma = &dbGet("stats", "counter", "nick='$target' and type='karma'") || 0; if ($karma != 0) { &pSReply("$target has karma of $karma"); @@ -313,12 +324,10 @@ sub tell { &status("tell: target = $target, query = $query"); # "intrusive". - if ($target !~ /^$mask{chan}$/ and !&IsNickInAnyChan($target)) { - &msg($who, "No, $target is not in any of my chans."); - return; - } - - ### TODO: don't "tell" if sender is not in target's channel. +# if ($target !~ /^$mask{chan}$/ and !&IsNickInAnyChan($target)) { +# &msg($who, "No, $target is not in any of my chans."); +# return; +# } # self. if ($target eq $ident) { # lc? @@ -346,7 +355,7 @@ sub tell { $message = $tell_obj; $done++ unless (&Modules()); - &DEBUG("setting old values of who and msgType."); + &VERB("tell: setting old values of who and msgType.",2); $who = $oldwho; $msgType = $oldmtype; @@ -380,10 +389,12 @@ sub DNS { my $dns = shift; my($match, $x, $y, $result); my $pid; + $dns =~ s/^\s+|\s+$//g; if ($dns =~ /(\d+\.\d+\.\d+\.\d+)/) { &status("DNS query by IP address: $in"); $match = $1; + $y = pack('C4', split(/\./, $match)); $x = (gethostbyaddr($y, &AF_INET)); @@ -392,12 +403,14 @@ sub DNS { } else { $result = "I can't seem to find that address in DNS"; } + } else { - &status("DNS query by name: $in"); - $x = join('.',unpack('C4',(gethostbyname($in))[4])); + + &status("DNS query by name: $dns"); + $x = join('.',unpack('C4',(gethostbyname($dns))[4])); if ($x !~ /^\s*$/) { - $result = $in." is ".$x; + $result = $dns." is ".$x; } else { $result = "I can\'t find that machine name"; } @@ -406,6 +419,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 +487,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 +502,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 +527,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 +685,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 +762,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 +777,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 ".