2 # User Command Extension Stubs
3 # WARN: this file does not reload on HUP.
8 use vars qw($who $msgType $conn $chan $message $ident $talkchannel
9 $bot_version $babel_lang_regex $bot_data_dir);
10 use vars qw(@vernick @vernicktodo);
11 use vars qw(%channels %cache %mask %userstats %myModules %cmdstats
12 %hooks_extra %lang %ver);
14 use vars qw($total $x $type $i $good);
16 $babel_lang_regex = "fr|sp|es|po|pt|it|ge|de|gr|en|zh|ja|jp|ko|kr|ru";
17 $w3search_regex = "google";
19 ### COMMAND HOOK IMPLEMENTATION.
20 # addCmdHook("SECTION", 'TEXT_HOOK',
23 # CheckModule => 1, # ???
24 # Module => 'blah.pl' # preload module.
25 # Identifier => 'config_label', # change to Config?
26 # Help => 'help_label',
27 # Cmdstats => 'text_label',)
32 my ($hashname, $ident, %hash) = @_;
34 if (exists ${"hooks_$hashname"}{$ident}) {
35 ### &WARN("aCH: cmd hooks \%$hashname{$ident} already exists.");
39 &VERB("aCH: added $ident",2); # use $hash{'Identifier'}?
40 ### hrm... prevent warnings?
41 ${"hooks_$hashname"}{$ident} = \%hash;
46 my ($hashname, $line) = @_;
47 $line =~ /^(\S+)( (.*))?$/;
48 my $cmd = $1; # command name is whitespaceless.
50 my @args = split(/\s+/, $flatarg || '');
55 if (!defined %{"hooks_$hashname"}) {
56 &WARN("cmd hooks \%$hashname does not exist.");
61 &WARN("cstubs: cmd == NULL.");
65 foreach (keys %{"hooks_$hashname"}) {
66 # rename to something else! like $id or $label?
69 next unless ($cmd =~ /^$ident$/i);
72 &WARN("pCH: Multiple hook match: $ident");
76 &status("hooks($hashname): $cmd matched '$ident'");
77 my %hash = %{ ${"hooks_$hashname"}{$ident} };
79 if (!scalar keys %hash) {
80 &WARN("CmdHook: hash is NULL?");
84 if ($hash{NoArgs} and $flatarg) {
85 &DEBUG("cmd $ident does not take args ('$flatarg'); skipping.");
89 if (!exists $hash{CODEREF}) {
90 &ERROR("CODEREF undefined for $cmd or $ident.");
95 foreach (keys %hash) {
96 &VERB(" $cmd->$_ => '$hash{$_}'.",2);
100 if (exists $hash{'Help'} and !scalar(@args)) {
101 &help( $hash{'Help'} );
106 if (exists $hash{'Identifier'}) {
107 return 1 unless (&hasParam($hash{'Identifier'}));
111 if (exists $hash{'UserFlag'}) {
112 return 1 unless (&hasFlag($hash{'UserFlag'}));
115 ### FORKER,IDENTIFIER,CODEREF.
116 if (exists $hash{'Forker'}) {
117 $hash{'Identifier'} .= "-" if ($hash{'Forker'} eq "NULL");
119 if (exists $hash{'ArrayArgs'}) {
120 &Forker($hash{'Identifier'}, sub { \&{ $hash{'CODEREF'} }(@args) } );
122 &Forker($hash{'Identifier'}, sub { \&{ $hash{'CODEREF'} }($flatarg) } );
126 if (exists $hash{'Module'}) {
127 &loadMyModule($myModules{ $hash{'Module'} });
130 # check if CODEREF exists.
131 if (!defined &{ $hash{'CODEREF'} }) {
132 &WARN("coderef $hash{'CODEREF'} does not exist.");
134 &msg($who, "coderef does not exist for $ident.");
140 if (exists $hash{'ArrayArgs'}) {
141 &{ $hash{'CODEREF'} }(@args);
143 &{ $hash{'CODEREF'} }($flatarg);
148 if (exists $hash{'Cmdstats'}) {
149 $cmdstats{ $hash{'Cmdstats'} }++;
152 &VERB("hooks: End of command.",2);
162 ### START ADDING HOOKS.
164 &addCmdHook("extra", 'd?bugs', ('CODEREF' => 'DBugs::Parse',
165 'Forker' => 1, 'Identifier' => 'debianExtra',
166 'Cmdstats' => 'Debian Bugs') );
167 &addCmdHook("extra", 'dauthor', ('CODEREF' => 'Debian::searchAuthor',
168 'Forker' => 1, 'Identifier' => 'debian',
169 'Cmdstats' => 'Debian Author Search', 'Help' => "dauthor" ) );
170 &addCmdHook("extra", '(d|search)desc', ('CODEREF' => 'Debian::searchDescFE',
171 'Forker' => 1, 'Identifier' => 'debian',
172 'Cmdstats' => 'Debian Desc Search', 'Help' => "ddesc" ) );
173 &addCmdHook("extra", 'dnew', ('CODEREF' => 'DebianNew',
174 'Identifier' => 'debian' ) );
175 &addCmdHook("extra", 'dincoming', ('CODEREF' => 'Debian::generateIncoming',
176 'Forker' => 1, 'Identifier' => 'debian' ) );
177 &addCmdHook("extra", 'dstats', ('CODEREF' => 'Debian::infoStats',
178 'Forker' => 1, 'Identifier' => 'debian',
179 'Cmdstats' => 'Debian Statistics' ) );
180 &addCmdHook("extra", 'd?contents', ('CODEREF' => 'Debian::searchContents',
181 'Forker' => 1, 'Identifier' => 'debian',
182 'Cmdstats' => 'Debian Contents Search', 'Help' => "contents" ) );
183 &addCmdHook("extra", 'd?find', ('CODEREF' => 'Debian::DebianFind',
184 'Forker' => 1, 'Identifier' => 'debian',
185 'Cmdstats' => 'Debian Search', 'Help' => "find" ) );
186 #&addCmdHook("extra", 'insult', ('CODEREF' => 'Insult::Insult',
187 # 'Forker' => 1, 'Identifier' => 'insult', 'Help' => "insult" ) );
188 &addCmdHook("extra", 'kernel', ('CODEREF' => 'Kernel::Kernel',
189 'Forker' => 1, 'Identifier' => 'kernel',
190 'Cmdstats' => 'Kernel', 'NoArgs' => 1) );
191 &addCmdHook("extra", 'listauth', ('CODEREF' => 'CmdListAuth',
192 'Identifier' => 'search', Module => 'factoids',
193 'Help' => 'listauth') );
194 &addCmdHook("extra", 'quote', ('CODEREF' => 'Quote::Quote',
195 'Forker' => 1, 'Identifier' => 'quote',
196 'Help' => 'quote', 'Cmdstats' => 'Quote') );
197 &addCmdHook("extra", 'countdown', ('CODEREF' => 'Countdown',
198 'Module' => 'countdown', 'Identifier' => 'countdown',
199 'Cmdstats' => 'Countdown') );
200 &addCmdHook("extra", 'lart', ('CODEREF' => 'lart',
201 'Identifier' => 'lart', 'Help' => 'lart') );
202 &addCmdHook("extra", 'convert', ('CODEREF' => 'convert',
203 'Forker' => 1, 'Identifier' => 'units',
204 'Help' => 'convert') );
205 &addCmdHook("extra", '(cookie|random)', ('CODEREF' => 'cookie',
206 'Forker' => 1, 'Identifier' => 'factoids') );
207 &addCmdHook("extra", 'u(ser)?info', ('CODEREF' => 'userinfo',
208 'Identifier' => 'userinfo', 'Help' => 'userinfo',
209 'Module' => 'userinfo') );
210 &addCmdHook("extra", 'rootWarn', ('CODEREF' => 'CmdrootWarn',
211 'Identifier' => 'rootWarn', 'Module' => 'rootwarn') );
212 &addCmdHook("extra", 'seen', ('CODEREF' => 'seen', 'Identifier' =>
214 &addCmdHook("extra", 'dict', ('CODEREF' => 'Dict::Dict',
215 'Identifier' => 'dict', 'Help' => 'dict',
216 'Forker' => 1, 'Cmdstats' => 'Dict') );
217 &addCmdHook("extra", 'slashdot', ('CODEREF' => 'Slashdot::Slashdot',
218 'Identifier' => 'slashdot', 'Forker' => 1,
219 'Cmdstats' => 'Slashdot') );
220 &addCmdHook("extra", 'plug', ('CODEREF' => 'Plug::Plug',
221 'Identifier' => 'plug', 'Forker' => 1,
222 'Cmdstats' => 'Plug') );
223 &addCmdHook("extra", 'uptime', ('CODEREF' => 'uptime', 'Identifier' => 'uptime',
224 'Cmdstats' => 'Uptime') );
225 &addCmdHook("extra", 'nullski', ('CODEREF' => 'nullski', ) );
226 &addCmdHook("extra", 'verstats', ('CODEREF' => 'do_verstats' ) );
227 &addCmdHook("extra", 'weather', ('CODEREF' => 'Weather::Weather',
228 'Identifier' => 'weather', 'Help' => 'weather',
229 'Cmdstats' => 'weather', 'Forker' => 1) );
230 &addCmdHook("extra", 'bzflist', ('CODEREF' => 'BZFlag::list',
231 'Identifier' => 'bzflag', 'Cmdstats' => 'BZFlag',
233 &addCmdHook("extra", 'bzfquery', ('CODEREF' => 'BZFlag::query',
234 'Identifier' => 'bzflag', 'Cmdstats' => 'BZFlag',
235 'Forker' => 1, 'Help' => 'bzflag') );
236 &addCmdHook("extra", 'zfi', ('CODEREF' => 'zfi::query',
237 'Identifier' => 'zfi', 'Cmdstats' => 'zfi',
239 &addCmdHook("extra", '(zippy|yow)', ('CODEREF' => 'zippy::get',
240 'Identifier' => 'zippy', 'Cmdstats' => 'zippy',
242 &addCmdHook("extra", 'zsi', ('CODEREF' => 'zsi::query',
243 'Identifier' => 'zsi', 'Cmdstats' => 'zsi',
245 &addCmdHook("extra", '(ex)?change', ('CODEREF' => 'Exchange::query',
246 'Identifier' => 'exchange', 'Cmdstats' => 'exchange',
248 &addCmdHook("extra", '(botmail|message)', ('CODEREF' => 'botmail::parse',
249 'Identifier' => 'botmail', 'Cmdstats' => 'botmail') );
252 ### END OF ADDING HOOKS.
254 &status("CMD: loaded ".scalar(keys %hooks_extra)." EXTRA command hooks.");
257 if (!defined $message) {
258 &WARN("Modules: message is undefined. should never happen.");
262 # babel bot: Jonathan Feinberg++
265 (?:babel(?:fish)?|x|xlate|translate)
267 ($babel_lang_regex)\w* # from language?
269 ($babel_lang_regex)\w* # to language?
271 (.+) # The phrase to be translated
273 return unless (&hasParam("babelfish"));
275 &Forker("babelfish", sub { &babel::babelfish(lc $1, lc $2, $3); } );
277 $cmdstats{'BabelFish'}++;
281 my $debiancmd = 'conflicts?|depends?|desc|file|info|provides?';
282 $debiancmd .= '|recommends?|suggests?|maint|maintainer';
284 if ($message =~ /^($debiancmd)(\s+(.*))?$/i) {
285 return unless (&hasParam("debian"));
288 if (defined $package) {
289 &Forker("debian", sub { &Debian::infoPackages($1, $package); } );
297 # google searching. Simon++
298 if ($message =~ /^(?:search\s+)?($w3search_regex)\s+for\s+['"]?(.*?)["']?\s*\?*$/i) {
299 return unless (&hasParam("wwwsearch"));
301 &Forker("wwwsearch", sub { &W3Search::W3Search($1,$2); } );
303 $cmdstats{'WWWSearch'}++;
307 # text counters. (eg: hehstats)
309 $itc = &getChanConf("ircTextCounters");
310 $itc = &findChanConf("ircTextCounters") unless ($itc);
311 return if ($itc && &do_text_counters($itc) == 1);
312 # end of text counters.
314 # list{keys|values}. xk++. Idea taken from #linuxwarez@EFNET
315 if ($message =~ /^list(\S+)(\s+(.*))?$/i) {
316 return unless (&hasParam("search"));
321 $thiscmd =~ s/^vals$/values/;
322 return if ($thiscmd ne "keys" && $thiscmd ne "values");
325 if (!defined $args or $args =~ /^\s*$/) {
326 &help("list". $thiscmd);
330 # suggested by asuffield and \broken.
331 if ($args =~ /^["']/ and $args =~ /["']$/) {
332 &DEBUG("list*: removed quotes.");
333 $args =~ s/^["']|["']$//g;
336 if (length $args < 2 && &IsFlag("o") ne "o") {
337 &msg($who, "search string is too short.");
341 &Forker("search", sub { &Search::Search($thiscmd, $args); } );
343 $cmdstats{'Factoid Search'}++;
347 # Nickometer. Adam Spiers++
348 if ($message =~ /^(?:lame|nick)ometer(?: for)? (\S+)/i) {
349 return unless (&hasParam("nickometer"));
351 my $term = (lc $1 eq 'me') ? $who : $1;
353 &loadMyModule($myModules{'nickometer'});
355 if ($term =~ /^$mask{chan}$/) {
356 &status("Doing nickometer for chan $term.");
358 if (!&validChan($term)) {
359 &msg($who, "error: channel is invalid.");
365 foreach (keys %{ $channels{lc $term}{''} }) {
368 &WARN("nickometer: nick in chan $term undefined?");
372 my $value = &nickometer($str);
373 $nickometer{$value}{$str} = 1;
377 ### TODO: compact with map?
379 foreach (sort {$b <=> $a} keys %nickometer) {
380 my $str = join(", ", sort keys %{ $nickometer{$_} });
381 push(@list, "$str ($_%)");
384 &pSReply( &formListReply(0, "Nickometer list for $term ", @list) );
390 my $percentage = &nickometer($term);
392 if ($percentage =~ /NaN/) {
393 $percentage = "off the scale";
395 $percentage = sprintf("%0.4f", $percentage);
396 $percentage =~ s/(\.\d+)0+$/$1/;
400 if ($msgType eq 'public') {
401 &say("'$term' is $percentage lame, $who");
403 &msg($who, "the 'lame nick-o-meter' reading for $term is $percentage, $who");
409 # Topic management. xk++
410 # may want to add a userflags for topic. -xk
411 if ($message =~ /^topic(\s+(.*))?$/i) {
412 return unless (&hasParam("topic"));
414 my $chan = $talkchannel;
415 my @args = split / /, $2 || "";
418 &msg($who,"Try 'help topic'");
422 $chan = lc(shift @args) if ($msgType eq 'private');
423 my $thiscmd = shift @args;
426 if ($msgType eq 'public' && $thiscmd =~ /^#/) {
427 &msg($who, "error: channel argument is not required.");
428 &msg($who, "\002Usage\002: topic <CMD>");
432 # topic over private:
433 if ($msgType eq 'private' && $chan !~ /^#/) {
434 &msg($who, "error: channel argument is required.");
435 &msg($who, "\002Usage\002: topic #channel <CMD>");
439 if (&validChan($chan) == 0) {
440 &msg($who,"error: invalid channel \002$chan\002");
444 # for semi-outsiders.
445 if (!&IsNickInChan($who,$chan)) {
446 &msg($who, "Failed. You ($who) are not in $chan, hey?");
451 &loadMyModule($myModules{'topic'});
452 &Topic($chan, $thiscmd, join(' ', @args));
453 $cmdstats{'Topic'}++;
458 if ($message =~ /^wingate$/i) {
459 return unless (&hasParam("wingate"));
461 my $reply = "Wingate statistics: scanned \002"
462 .scalar(keys %wingate)."\002 hosts";
463 my $queue = scalar(keys %wingateToDo);
465 $reply .= ". I have \002$queue\002 hosts in the queue";
466 $reply .= ". Started the scan ".&Time2String(time() - $wingaterun)." ago";
474 # do nothing and let the other routines have a go
481 &msg($who, "- Uptime for $ident -");
482 &msg($who, "Now: ". &Time2String(&uptimeNow()) ." running $bot_version");
484 foreach (&uptimeGetInfo()) {
486 my $time = &Time2String($1);
489 &msg($who, "$count: $time $2");
496 my($person) = lc shift;
499 if (!defined $person or $person =~ /^$/) {
502 my $i = &countKeys("seen");
503 &msg($who,"there ". &fixPlural("is",$i) ." \002$i\002 ".
504 "seen ". &fixPlural("entry",$i) ." that I know of.");
511 &seenFlush(); # very evil hack. oh well, better safe than sorry.
513 # TODO: convert to &sqlSelectRowHash();
514 my $select = "nick,time,channel,host,message";
515 if ($person eq "random") {
516 @seen = &randKey("seen", $select);
518 @seen = &sqlSelect("seen", $select, { nick => $person } );
521 if (scalar @seen < 2) {
523 &DEBUG("seen: _ => '$_'.");
525 &performReply("i haven't seen '$person'");
531 ### TODO: multi channel support. may require &IsNick() to return
532 ### all channels or something.
534 my @chans = &getNickInChans($seen[0]);
536 $reply = "$seen[0] is currently on";
540 next unless (exists $userstats{lc $seen[0]}{'Join'});
541 $reply .= " (".&Time2String(time() - $userstats{lc $seen[0]}{'Join'}).")";
544 if (&IsParam("seenStats")) {
546 $i = $userstats{lc $seen[0]}{'Count'};
547 $reply .= ". Has said a total of \002$i\002 messages" if (defined $i);
548 $i = $userstats{lc $seen[0]}{'Time'};
549 $reply .= ". Is idling for ".&Time2String(time() - $i) if (defined $i);
552 my $howlong = &Time2String(time() - $seen[1]);
553 $reply = "$seen[0] <$seen[3]> was last seen on IRC ".
554 "in channel $seen[2], $howlong ago, ".
555 "saying\002:\002 '$seen[4]'.";
562 # User Information Services. requested by Flugh.
564 my ($arg) = join(' ',@_);
566 if ($arg =~ /^set(\s+(.*))?$/i) {
569 &help("userinfo set");
573 &UserInfoSet(split /\s+/, $arg, 2);
574 } elsif ($arg =~ /^unset(\s+(.*))?$/i) {
577 &help("userinfo unset");
581 &UserInfoSet($arg, "");
587 # cookie (random). xk++
591 # lets find that secret cookie.
592 my $target = ($msgType ne 'public') ? $who : $talkchannel;
593 my $cookiemsg = &getRandom(keys %{ $lang{'cookie'} });
596 ### WILL CHEW TONS OF MEM.
597 ### TODO: convert this to a Forker function!
599 my @list = &searchTable("factoids", "factoid_key", "factoid_value", $arg);
600 $key = &getRandom(@list);
601 $value = &getFactInfo($key, "factoid_value");
603 ($key,$value) = &randKey("factoids","factoid_key,factoid_value");
607 s/##KEY/\002$key\002/;
610 s/\$who/$who/; # cheap fix.
611 s/(\S+)?\s*<\S+>/$1 /;
615 if ($cookiemsg =~ s/^ACTION //i) {
616 &action($target, $cookiemsg);
618 &msg($target, $cookiemsg);
623 my $arg = join(' ',@_);
624 my ($from,$to) = ('','');
626 ($from,$to) = ($1,$2) if ($arg =~ /^(.*?) to (.*)$/i);
627 ($from,$to) = ($2,$1) if ($arg =~ /^(.*?) from (.*)$/i);
629 if (!$to or !$from) {
630 &msg($who, "Invalid format!");
635 &Units::convertUnits($from, $to);
641 my ($target) = &fixString($_[0]);
643 my $chan = $talkchannel;
645 if ($msgType eq 'private') {
646 if ($target =~ /^($mask{chan})\s+(.*)$/) {
651 &msg($who, "error: invalid format or missing arguments.");
657 my $line = &getRandomLineFromFile($bot_data_dir. "/blootbot.lart");
659 if ($target =~ /^(me|you|itself|\Q$ident\E)$/i) {
660 $line =~ s/WHO/$who/g;
662 $line =~ s/WHO/$target/g;
664 $line .= ", courtesy of $who" if ($extra);
666 &action($chan, $line);
668 &status("lart: error reading file?");
673 my $idx = "debian/Packages-sid.idx";
678 $error++ unless ( -e $idx);
679 $error++ unless ( -e "$idx-old");
682 $error = "no sid/sid-old index file found.";
683 &ERROR("Debian: $error");
689 open(IDX2, "$idx-old");
703 next if (exists $pkg{$_});
709 &::pSReply( &::formListReply(0, "New debian packages:", @new) );
715 if (!defined $chan) {
720 if (!&validChan($chan)) {
721 &msg($who, "chan $chan is invalid.");
725 if (scalar @vernick > scalar(keys %{ $channels{lc $chan}{''} })/4) {
726 &msg($who, "verstats already in progress for someone else.");
730 &msg($who, "Sending CTCP VERSION to $chan; results in 60s.");
731 $conn->ctcp("VERSION", $chan);
732 $cache{verstats}{chan} = $chan;
733 $cache{verstats}{who} = $who;
734 $cache{verstats}{msgType} = $msgType;
736 $conn->schedule(30, sub {
737 my $c = lc $cache{verstats}{chan};
740 foreach (keys %{ $channels{$c}{''} } ) {
741 next if (grep /^\Q$_\E$/i, @vernick);
742 push(@vernicktodo, $_);
748 $conn->schedule(60, sub {
750 my $c = lc $cache{verstats}{chan};
751 my $total = keys %{ $channels{$c}{''} };
753 $who = $cache{verstats}{who};
754 $msgType = $cache{verstats}{msgType};
755 delete $cache{verstats}; # sufficient?
757 foreach (keys %ver) {
758 $vtotal += scalar keys %{ $ver{$_} };
762 my $unknown = $total - $vtotal;
763 my $perc = sprintf("%.1f", $unknown * 100 / $total);
765 $sorted{$perc}{"unknown/cloak"} = "$unknown ($perc%)" if ($unknown);
767 foreach (keys %ver) {
768 my $count = scalar keys %{ $ver{$_} };
769 $perc = sprintf("%.01f", $count * 100 / $total);
770 $perc =~ s/.0$//; # lame compression.
772 $sorted{$perc}{$_} = "$count ($perc%)";
775 ### can be compressed to a map?
777 foreach ( sort { $b <=> $a } keys %sorted ) {
779 foreach (sort keys %{ $sorted{$perc} }) {
780 push(@list, "$_ - $sorted{$perc}{$_}");
784 # hack. this is one major downside to scheduling.
786 &pSReply( &formListReply(0, "IRC Client versions for $c ", @list) );
788 # clean up not-needed data structures.
798 last unless (scalar @vernicktodo);
800 my $n = shift(@vernicktodo);
801 $conn->ctcp("VERSION", $n);
804 return unless (scalar @vernicktodo);
806 $conn->schedule(3, \&verstats_flush() );
809 sub do_text_counters {
811 $itc =~ s/([^\w\s])/\\$1/g;
812 my $z = join '|', split ' ', $itc;
814 if ($msgType eq "privmsg" and $message =~ / ($mask{chan})$/) {
815 &DEBUG("ircTC: privmsg detected; chan = $1");
819 if ($message =~ /^_stats(\s+(\S+))$/i) {
825 if ($message =~ /^($z)stats(\s+(\S+))?$/i) {
832 # even more uglier with channel/time arguments.
834 # my $c = $chan || "PRIVATE";
835 my $where = "type=".&sqlQuote($type);
838 $where .= " AND channel=".&sqlQuote($c) if (defined $c);
840 &DEBUG("not using chan arg");
843 my $sum = (&sqlRawReturn("SELECT SUM(counter) FROM stats"
844 ." WHERE ".$where ))[0];
846 if (!defined $arg or $arg =~ /^\s*$/) {
847 # this is way fucking ugly.
849 # TODO convert $where to hash
850 my %hash = &sqlSelectColHash("stats", "nick,counter",
852 $where." ORDER BY counter DESC LIMIT 3", 1
857 # unfortunately we have to sort it again!
859 foreach $i (sort { $b <=> $a } keys %hash) {
860 foreach (keys %{ $hash{$i} }) {
861 my $p = sprintf("%.01f", 100*$i/$sum);
863 push(@top, "\002$_\002 -- $i ($p%)");
868 $topstr = ". Top ".scalar(@top).": ".join(', ', @top);
872 &pSReply("total count of \037$type\037 on \002$c\002: $sum$topstr");
874 &pSReply("zero counter for \037$type\037.");
877 # TODO convert $where to hash and use a sqlSelect
878 my $x = (&sqlRawReturn("SELECT SUM(counter) FROM stats".
879 " WHERE $where AND nick=".&sqlQuote($arg) ))[0];
881 if (!defined $x) { # !defined.
882 &pSReply("$arg has not said $type yet.");
887 # TODO convert $where to hash
888 my @array = &sqlSelect("stats", "nick", undef,
889 $where." ORDER BY counter", 1
893 for ($i=0; $i<scalar @array; $i++) {
894 next unless ($array[0] =~ /^\Q$who\E$/);
900 my $total = scalar(@array);
902 if ($total and $good) {
903 my $pct = sprintf("%.01f", 100*(1+$total-$i)/$total);
904 $xtra = ", ranked $i\002/\002$total (percentile: \002$pct\002 %)";
907 my $pct1 = sprintf("%.01f", 100*$x/$sum);
908 &pSReply("\002$arg\002 has said \037$type\037 \002$x\002 times (\002$pct1\002 %)$xtra");
917 # even more uglier with channel/time arguments.
919 # my $c = $chan || "PRIVATE";
920 &DEBUG("not using chan arg") if (!defined $c);
922 # example of converting from RawReturn to sqlSelect.
923 my $where_href = (defined $c) ? { channel => $c } : "";
924 my $sum = &sqlSelect("stats", "SUM(counter)", $where_href);
926 if (!defined $arg or $arg =~ /^\s*$/) {
927 # this is way fucking ugly.
928 &DEBUG("_stats: !arg");
930 my %hash = &sqlSelectColHash("stats", "nick,counter",
932 " ORDER BY counter DESC LIMIT 3", 1
937 # unfortunately we have to sort it again!
939 foreach $i (sort { $b <=> $a } keys %hash) {
940 foreach (keys %{ $hash{$i} }) {
941 my $p = sprintf("%.01f", 100*$i/$sum);
943 push(@top, "\002$_\002 -- $i ($p%)");
949 $topstr = ". Top ".scalar(@top).": ".join(', ', @top);
953 &pSReply("total count of \037$type\037 on \002$c\002: $sum$topstr");
955 &pSReply("zero counter for \037$type\037.");
961 # TODO add nick to where_href
962 my %hash = &sqlSelectColHash("stats", "type,counter",
963 $where_href, " AND nick=".&sqlQuote($arg)
965 # this is totally fucked... needs to be fixed... and cleaned up.
971 foreach (keys %hash) {
972 &DEBUG("_stats: hash{$_} => $hash{$_}");
974 # TODO convert $where to hash
975 my @array = &sqlSelect("stats", "nick", undef,
976 $where." ORDER BY counter", 1);
979 for(my $i=0; $i<scalar @array; $i++) {
980 next unless ($array[0] =~ /^\Q$who\E$/);
986 $total = scalar(@array);
987 &DEBUG(" i => $i, good => $good, total => $total");
988 $x .= " ".$total."blah blah";
993 if (!defined $x) { # !defined.
994 &pSReply("$arg has not said $type yet.");
999 if ($total and $good) {
1000 my $pct = sprintf("%.01f", 100*(1+$total-$ii)/$total);
1001 $xtra = ", ranked $ii\002/\002$total (percentile: \002$pct\002 %)";
1004 my $pct1 = sprintf("%.01f", 100*$x/$sum);
1005 &pSReply("\002$arg\002 has said \037$type\037 \002$x\002 times (\002$pct1\002 %)$xtra");
1008 sub nullski { my ($arg) = @_; return unless (defined $arg);
1009 foreach (`$arg`) { &msg($who,$_); } }