2 # User Command Extension Stubs
3 # WARN: this file does not reload on HUP.
9 use vars qw($who $msgType $conn $chan $message $ident $talkchannel
10 $bot_version $babel_lang_regex $bot_data_dir);
11 use vars qw(@vernick @vernicktodo);
12 use vars qw(%channels %cache %mask %userstats %myModules %cmdstats
13 %hooks_extra %lang %ver);
15 use vars qw($total $x $type $i $good);
17 $babel_lang_regex = "fr|sp|es|po|pt|it|ge|de|gr|en|zh|ja|jp|ko|kr|ru";
18 $w3search_regex = "google";
20 ### COMMAND HOOK IMPLEMENTATION.
21 # addCmdHook("SECTION", 'TEXT_HOOK',
24 # CheckModule => 1, # ???
25 # Module => 'blah.pl' # preload module.
26 # Identifier => 'config_label', # change to Config?
27 # Help => 'help_label',
28 # Cmdstats => 'text_label',)
33 my ($hashname, $ident, %hash) = @_;
35 if (exists ${"hooks_$hashname"}{$ident}) {
36 ### &WARN("aCH: cmd hooks \%$hashname{$ident} already exists.");
40 &VERB("aCH: added $ident",2); # use $hash{'Identifier'}?
41 ### hrm... prevent warnings?
42 ${"hooks_$hashname"}{$ident} = \%hash;
47 my ($hashname, $line) = @_;
48 $line =~ s/^\s+|\s+$//g; # again.
49 $line =~ /^(\S+)(\s+(.*))?$/;
50 my $cmd = $1; # command name is whitespaceless.
52 my @args = split(/\s+/, $flatarg || '');
57 if (!defined %{"hooks_$hashname"}) {
58 &WARN("cmd hooks \%$hashname does not exist.");
63 &WARN("cstubs: cmd == NULL.");
67 foreach (keys %{"hooks_$hashname"}) {
68 # rename to something else! like $id or $label?
71 next unless ($cmd =~ /^$ident$/i);
74 &WARN("pCH: Multiple hook match: $ident");
78 &status("hooks($hashname): $cmd matched '$ident' '$flatarg'");
79 my %hash = %{ ${"hooks_$hashname"}{$ident} };
81 if (!scalar keys %hash) {
82 &WARN("CmdHook: hash is NULL?");
86 if ($hash{NoArgs} and $flatarg) {
87 &DEBUG("cmd $ident does not take args ('$flatarg'); skipping.");
91 if (!exists $hash{CODEREF}) {
92 &ERROR("CODEREF undefined for $cmd or $ident.");
97 foreach (keys %hash) {
98 &VERB(" $cmd->$_ => '$hash{$_}'.",2);
102 if (exists $hash{'Help'} and !scalar(@args)) {
103 &help( $hash{'Help'} );
108 if (exists $hash{'Identifier'}) {
109 return 1 unless (&hasParam($hash{'Identifier'}));
113 if (exists $hash{'UserFlag'}) {
114 return 1 unless (&hasFlag($hash{'UserFlag'}));
117 ### FORKER,IDENTIFIER,CODEREF.
118 if (exists $hash{'Forker'}) {
119 $hash{'Identifier'} .= "-" if ($hash{'Forker'} eq "NULL");
121 if (exists $hash{'ArrayArgs'}) {
122 &Forker($hash{'Identifier'}, sub { \&{ $hash{'CODEREF'} }(@args) } );
124 &Forker($hash{'Identifier'}, sub { \&{ $hash{'CODEREF'} }($flatarg) } );
128 if (exists $hash{'Module'}) {
129 &loadMyModule($myModules{ $hash{'Module'} });
132 # check if CODEREF exists.
133 if (!defined &{ $hash{'CODEREF'} }) {
134 &WARN("coderef $hash{'CODEREF'} does not exist.");
136 &msg($who, "coderef does not exist for $ident.");
142 if (exists $hash{'ArrayArgs'}) {
143 &{ $hash{'CODEREF'} }(@args);
145 &{ $hash{'CODEREF'} }($flatarg);
150 if (exists $hash{'Cmdstats'}) {
151 $cmdstats{ $hash{'Cmdstats'} }++;
154 &VERB("hooks: End of command.",2);
164 ### START ADDING HOOKS.
166 &addCmdHook("extra", 'd?bugs', ('CODEREF' => 'DBugs::Parse',
167 'Forker' => 1, 'Identifier' => 'debianExtra',
168 'Cmdstats' => 'Debian Bugs') );
169 &addCmdHook("extra", 'dauthor', ('CODEREF' => 'Debian::searchAuthor',
170 'Forker' => 1, 'Identifier' => 'debian',
171 'Cmdstats' => 'Debian Author Search', 'Help' => "dauthor" ) );
172 &addCmdHook("extra", '(d|search)desc', ('CODEREF' => 'Debian::searchDescFE',
173 'Forker' => 1, 'Identifier' => 'debian',
174 'Cmdstats' => 'Debian Desc Search', 'Help' => "ddesc" ) );
175 &addCmdHook("extra", 'dnew', ('CODEREF' => 'DebianNew',
176 'Identifier' => 'debian' ) );
177 &addCmdHook("extra", 'dincoming', ('CODEREF' => 'Debian::generateIncoming',
178 'Forker' => 1, 'Identifier' => 'debian' ) );
179 &addCmdHook("extra", 'dstats', ('CODEREF' => 'Debian::infoStats',
180 'Forker' => 1, 'Identifier' => 'debian',
181 'Cmdstats' => 'Debian Statistics' ) );
182 &addCmdHook("extra", 'd?contents', ('CODEREF' => 'Debian::searchContents',
183 'Forker' => 1, 'Identifier' => 'debian',
184 'Cmdstats' => 'Debian Contents Search', 'Help' => "contents" ) );
185 &addCmdHook("extra", 'd?find', ('CODEREF' => 'Debian::DebianFind',
186 'Forker' => 1, 'Identifier' => 'debian',
187 'Cmdstats' => 'Debian Search', 'Help' => "find" ) );
188 #&addCmdHook("extra", 'insult', ('CODEREF' => 'Insult::Insult',
189 # 'Forker' => 1, 'Identifier' => 'insult', 'Help' => "insult" ) );
190 &addCmdHook("extra", 'kernel', ('CODEREF' => 'Kernel::Kernel',
191 'Forker' => 1, 'Identifier' => 'kernel',
192 'Cmdstats' => 'Kernel', 'NoArgs' => 1) );
193 &addCmdHook("extra", 'listauth', ('CODEREF' => 'CmdListAuth',
194 'Identifier' => 'search', Module => 'factoids',
195 'Help' => 'listauth') );
196 &addCmdHook("extra", 'quote', ('CODEREF' => 'Quote::Quote',
197 'Forker' => 1, 'Identifier' => 'quote',
198 'Help' => 'quote', 'Cmdstats' => 'Quote') );
199 &addCmdHook("extra", 'countdown', ('CODEREF' => 'Countdown',
200 'Module' => 'countdown', 'Identifier' => 'countdown',
201 'Cmdstats' => 'Countdown') );
202 &addCmdHook("extra", 'lart', ('CODEREF' => 'lart',
203 'Identifier' => 'lart', 'Help' => 'lart') );
204 &addCmdHook("extra", 'convert', ('CODEREF' => 'convert',
205 'Forker' => 1, 'Identifier' => 'units',
206 'Help' => 'convert') );
207 &addCmdHook("extra", '(cookie|random)', ('CODEREF' => 'cookie',
208 'Forker' => 1, 'Identifier' => 'factoids') );
209 &addCmdHook("extra", 'u(ser)?info', ('CODEREF' => 'userinfo',
210 'Identifier' => 'userinfo', 'Help' => 'userinfo',
211 'Module' => 'userinfo') );
212 &addCmdHook("extra", 'rootWarn', ('CODEREF' => 'CmdrootWarn',
213 'Identifier' => 'rootWarn', 'Module' => 'rootwarn') );
214 &addCmdHook("extra", 'seen', ('CODEREF' => 'seen', 'Identifier' =>
216 &addCmdHook("extra", 'dict', ('CODEREF' => 'Dict::Dict',
217 'Identifier' => 'dict', 'Help' => 'dict',
218 'Forker' => 1, 'Cmdstats' => 'Dict') );
219 &addCmdHook("extra", 'slashdot', ('CODEREF' => 'Slashdot::Slashdot',
220 'Identifier' => 'slashdot', 'Forker' => 1,
221 'Cmdstats' => 'Slashdot') );
222 &addCmdHook("extra", 'plug', ('CODEREF' => 'Plug::Plug',
223 'Identifier' => 'plug', 'Forker' => 1,
224 'Cmdstats' => 'Plug') );
225 &addCmdHook("extra", 'uptime', ('CODEREF' => 'uptime', 'Identifier' => 'uptime',
226 'Cmdstats' => 'Uptime') );
227 &addCmdHook("extra", 'nullski', ('CODEREF' => 'nullski', ) );
228 &addCmdHook("extra", 'verstats', ('CODEREF' => 'do_verstats' ) );
229 &addCmdHook("extra", 'weather', ('CODEREF' => 'Weather::Weather',
230 'Identifier' => 'weather', 'Help' => 'weather',
231 'Cmdstats' => 'weather', 'Forker' => 1) );
232 &addCmdHook("extra", 'bzflist', ('CODEREF' => 'BZFlag::list',
233 'Identifier' => 'bzflag', 'Cmdstats' => 'BZFlag',
235 &addCmdHook("extra", 'bzflist17', ('CODEREF' => 'BZFlag::list17',
236 'Identifier' => 'bzflag', 'Cmdstats' => 'BZFlag',
238 &addCmdHook("extra", 'bzfquery', ('CODEREF' => 'BZFlag::query',
239 'Identifier' => 'bzflag', 'Cmdstats' => 'BZFlag',
240 'Forker' => 1, 'Help' => 'bzflag') );
241 &addCmdHook("extra", 'zfi', ('CODEREF' => 'zfi::query',
242 'Identifier' => 'zfi', 'Cmdstats' => 'zfi',
244 &addCmdHook("extra", '(zippy|yow)', ('CODEREF' => 'zippy::get',
245 'Identifier' => 'zippy', 'Cmdstats' => 'zippy',
247 &addCmdHook("extra", 'zsi', ('CODEREF' => 'zsi::query',
248 'Identifier' => 'zsi', 'Cmdstats' => 'zsi',
250 &addCmdHook("extra", '(ex)?change', ('CODEREF' => 'Exchange::query',
251 'Identifier' => 'exchange', 'Cmdstats' => 'exchange',
253 &addCmdHook("extra", '(botmail|message)', ('CODEREF' => 'botmail::parse',
254 'Identifier' => 'botmail', 'Cmdstats' => 'botmail') );
255 &addCmdHook("extra", 'httpdtype', ('CODEREF' => 'HTTPDtype::HTTPDtype',
256 'Identifier' => 'httpdtype', 'Cmdstats' => 'httpdtype',
260 ### END OF ADDING HOOKS.
262 &status("CMD: loaded ".scalar(keys %hooks_extra)." EXTRA command hooks.");
265 if (!defined $message) {
266 &WARN("Modules: message is undefined. should never happen.");
270 # babel bot: Jonathan Feinberg++
273 (?:babel(?:fish)?|x|xlate|translate)
275 ($babel_lang_regex)\w* # from language?
277 ($babel_lang_regex)\w* # to language?
279 (.+) # The phrase to be translated
281 return unless (&hasParam("babelfish"));
283 &Forker("babelfish", sub { &babel::babelfish(lc $1, lc $2, $3); } );
285 $cmdstats{'BabelFish'}++;
289 my $debiancmd = 'conflicts?|depends?|desc|file|(?:d)?info|provides?';
290 $debiancmd .= '|recommends?|suggests?|maint|maintainer';
292 if ($message =~ /^($debiancmd)(\s+(.*))?$/i) {
293 return unless (&hasParam("debian"));
296 if (defined $package) {
297 &Forker("debian", sub { &Debian::infoPackages($1, $package); } );
305 # google searching. Simon++
306 if ($message =~ /^(?:search\s+)?($w3search_regex)\s+(?:for\s+)?['"]?(.*?)["']?\s*\?*$/i) {
307 return unless (&hasParam("wwwsearch"));
309 &Forker("wwwsearch", sub { &W3Search::W3Search($1,$2); } );
311 $cmdstats{'WWWSearch'}++;
315 # text counters. (eg: hehstats)
317 $itc = &getChanConf("ircTextCounters");
318 $itc = &findChanConf("ircTextCounters") unless ($itc);
319 return if ($itc && &do_text_counters($itc) == 1);
320 # end of text counters.
322 # list{keys|values}. xk++. Idea taken from #linuxwarez@EFNET
323 if ($message =~ /^list(\S+)(\s+(.*))?$/i) {
324 return unless (&hasParam("search"));
329 $thiscmd =~ s/^vals$/values/;
330 return if ($thiscmd ne "keys" && $thiscmd ne "values");
333 if (!defined $args or $args =~ /^\s*$/) {
334 &help("list". $thiscmd);
338 # suggested by asuffield and \broken.
339 if ($args =~ /^["']/ and $args =~ /["']$/) {
340 &DEBUG("list*: removed quotes.");
341 $args =~ s/^["']|["']$//g;
344 if (length $args < 2 && &IsFlag("o") ne "o") {
345 &msg($who, "search string is too short.");
349 &Forker("search", sub { &Search::Search($thiscmd, $args); } );
351 $cmdstats{'Factoid Search'}++;
355 # Nickometer. Adam Spiers++
356 if ($message =~ /^(?:lame|nick)ometer(?: for)? (\S+)/i) {
357 return unless (&hasParam("nickometer"));
359 my $term = (lc $1 eq 'me') ? $who : $1;
361 &loadMyModule($myModules{'nickometer'});
363 if ($term =~ /^$mask{chan}$/) {
364 &status("Doing nickometer for chan $term.");
366 if (!&validChan($term)) {
367 &msg($who, "error: channel is invalid.");
373 foreach (keys %{ $channels{lc $term}{''} }) {
376 &WARN("nickometer: nick in chan $term undefined?");
380 my $value = &nickometer($str);
381 $nickometer{$value}{$str} = 1;
385 ### TODO: compact with map?
387 foreach (sort {$b <=> $a} keys %nickometer) {
388 my $str = join(", ", sort keys %{ $nickometer{$_} });
389 push(@list, "$str ($_%)");
392 &pSReply( &formListReply(0, "Nickometer list for $term ", @list) );
398 my $percentage = &nickometer($term);
400 if ($percentage =~ /NaN/) {
401 $percentage = "off the scale";
403 $percentage = sprintf("%0.4f", $percentage);
404 $percentage =~ s/(\.\d+)0+$/$1/;
408 if ($msgType eq 'public') {
409 &say("'$term' is $percentage lame, $who");
411 &msg($who, "the 'lame nick-o-meter' reading for $term is $percentage, $who");
417 # Topic management. xk++
418 # may want to add a userflags for topic. -xk
419 if ($message =~ /^topic(\s+(.*))?$/i) {
420 return unless (&hasParam("topic"));
422 my $chan = $talkchannel;
423 my @args = split / /, $2 || "";
426 &msg($who,"Try 'help topic'");
430 $chan = lc(shift @args) if ($msgType eq 'private');
431 my $thiscmd = shift @args;
434 if ($msgType eq 'public' && $thiscmd =~ /^#/) {
435 &msg($who, "error: channel argument is not required.");
436 &msg($who, "\002Usage\002: topic <CMD>");
440 # topic over private:
441 if ($msgType eq 'private' && $chan !~ /^#/) {
442 &msg($who, "error: channel argument is required.");
443 &msg($who, "\002Usage\002: topic #channel <CMD>");
447 if (&validChan($chan) == 0) {
448 &msg($who,"error: invalid channel \002$chan\002");
452 # for semi-outsiders.
453 if (!&IsNickInChan($who,$chan)) {
454 &msg($who, "Failed. You ($who) are not in $chan, hey?");
459 &loadMyModule($myModules{'topic'});
460 &Topic($chan, $thiscmd, join(' ', @args));
461 $cmdstats{'Topic'}++;
466 if ($message =~ /^wingate$/i) {
467 return unless (&hasParam("wingate"));
469 my $reply = "Wingate statistics: scanned \002"
470 .scalar(keys %wingate)."\002 hosts";
471 my $queue = scalar(keys %wingateToDo);
473 $reply .= ". I have \002$queue\002 hosts in the queue";
474 $reply .= ". Started the scan ".&Time2String(time() - $wingaterun)." ago";
482 # do nothing and let the other routines have a go
489 &msg($who, "- Uptime for $ident -");
490 &msg($who, "Now: ". &Time2String(&uptimeNow()) ." running $bot_version");
492 foreach (&uptimeGetInfo()) {
494 my $time = &Time2String($1);
497 &msg($who, "$count: $time $2");
504 my($person) = lc shift;
507 if (!defined $person or $person =~ /^$/) {
510 my $i = &countKeys("seen");
511 &msg($who,"there ". &fixPlural("is",$i) ." \002$i\002 ".
512 "seen ". &fixPlural("entry",$i) ." that I know of.");
519 &seenFlush(); # very evil hack. oh well, better safe than sorry.
521 # TODO: convert to &sqlSelectRowHash();
522 my $select = "nick,time,channel,host,message";
523 if ($person eq "random") {
524 @seen = &randKey("seen", $select);
526 @seen = &sqlSelect("seen", $select, { nick => $person } );
529 if (scalar @seen < 2) {
531 &DEBUG("seen: _ => '$_'.");
533 &performReply("i haven't seen '$person'");
539 ### TODO: multi channel support. may require &IsNick() to return
540 ### all channels or something.
542 my @chans = &getNickInChans($seen[0]);
544 $reply = "$seen[0] is currently on";
548 next unless (exists $userstats{lc $seen[0]}{'Join'});
549 $reply .= " (".&Time2String(time() - $userstats{lc $seen[0]}{'Join'}).")";
552 if (&IsChanConf("seenStats") > 0) {
554 $i = $userstats{lc $seen[0]}{'Count'};
555 $reply .= ". Has said a total of \002$i\002 messages" if (defined $i);
556 $i = $userstats{lc $seen[0]}{'Time'};
557 $reply .= ". Is idling for ".&Time2String(time() - $i) if (defined $i);
560 my $howlong = &Time2String(time() - $seen[1]);
561 $reply = "$seen[0] <$seen[3]> was last seen on IRC ".
562 "in channel $seen[2], $howlong ago, ".
563 "saying\002:\002 '$seen[4]'.";
570 # User Information Services. requested by Flugh.
572 my ($arg) = join(' ',@_);
574 if ($arg =~ /^set(\s+(.*))?$/i) {
577 &help("userinfo set");
581 &UserInfoSet(split /\s+/, $arg, 2);
582 } elsif ($arg =~ /^unset(\s+(.*))?$/i) {
585 &help("userinfo unset");
589 &UserInfoSet($arg, "");
595 # cookie (random). xk++
599 # lets find that secret cookie.
600 my $target = ($msgType ne 'public') ? $who : $talkchannel;
601 my $cookiemsg = &getRandom(keys %{ $lang{'cookie'} });
604 ### WILL CHEW TONS OF MEM.
605 ### TODO: convert this to a Forker function!
607 my @list = &searchTable("factoids", "factoid_key", "factoid_value", $arg);
608 $key = &getRandom(@list);
609 $value = &getFactInfo($key, "factoid_value");
611 ($key,$value) = &randKey("factoids","factoid_key,factoid_value");
615 s/##KEY/\002$key\002/;
618 s/\$who/$who/; # cheap fix.
619 s/(\S+)?\s*<\S+>/$1 /;
623 if ($cookiemsg =~ s/^ACTION //i) {
624 &action($target, $cookiemsg);
626 &msg($target, $cookiemsg);
631 my $arg = join(' ',@_);
632 my ($from,$to) = ('','');
634 ($from,$to) = ($1,$2) if ($arg =~ /^(.*?) to (.*)$/i);
635 ($from,$to) = ($2,$1) if ($arg =~ /^(.*?) from (.*)$/i);
637 if (!$to or !$from) {
638 &msg($who, "Invalid format!");
643 &Units::convertUnits($from, $to);
649 my ($target) = &fixString($_[0]);
651 my $chan = $talkchannel;
653 if ($msgType eq 'private') {
654 if ($target =~ /^($mask{chan})\s+(.*)$/) {
659 &msg($who, "error: invalid format or missing arguments.");
665 my $line = &getRandomLineFromFile($bot_data_dir. "/blootbot.lart");
667 if ($target =~ /^(me|you|itself|\Q$ident\E)$/i) {
668 $line =~ s/WHO/$who/g;
670 $line =~ s/WHO/$target/g;
672 $line .= ", courtesy of $who" if ($extra);
674 &action($chan, $line);
676 &status("lart: error reading file?");
681 my $idx = "debian/Packages-sid.idx";
686 $error++ unless ( -e $idx);
687 $error++ unless ( -e "$idx-old");
690 $error = "no sid/sid-old index file found.";
691 &ERROR("Debian: $error");
697 open(IDX2, "$idx-old");
711 next if (exists $pkg{$_});
717 &::pSReply( &::formListReply(0, "New debian packages:", @new) );
723 if (!defined $chan) {
728 if (!&validChan($chan)) {
729 &msg($who, "chan $chan is invalid.");
733 if (scalar @vernick > scalar(keys %{ $channels{lc $chan}{''} })/4) {
734 &msg($who, "verstats already in progress for someone else.");
738 &msg($who, "Sending CTCP VERSION to $chan; results in 60s.");
739 $conn->ctcp("VERSION", $chan);
740 $cache{verstats}{chan} = $chan;
741 $cache{verstats}{who} = $who;
742 $cache{verstats}{msgType} = $msgType;
744 $conn->schedule(30, sub {
745 my $c = lc $cache{verstats}{chan};
748 foreach (keys %{ $channels{$c}{''} } ) {
749 next if (grep /^\Q$_\E$/i, @vernick);
750 push(@vernicktodo, $_);
756 $conn->schedule(60, sub {
758 my $c = lc $cache{verstats}{chan};
759 my $total = keys %{ $channels{$c}{''} };
761 $who = $cache{verstats}{who};
762 $msgType = $cache{verstats}{msgType};
763 delete $cache{verstats}; # sufficient?
765 foreach (keys %ver) {
766 $vtotal += scalar keys %{ $ver{$_} };
770 my $unknown = $total - $vtotal;
771 my $perc = sprintf("%.1f", $unknown * 100 / $total);
773 $sorted{$perc}{"unknown/cloak"} = "$unknown ($perc%)" if ($unknown);
775 foreach (keys %ver) {
776 my $count = scalar keys %{ $ver{$_} };
777 $perc = sprintf("%.01f", $count * 100 / $total);
778 $perc =~ s/.0$//; # lame compression.
780 $sorted{$perc}{$_} = "$count ($perc%)";
783 ### can be compressed to a map?
785 foreach ( sort { $b <=> $a } keys %sorted ) {
787 foreach (sort keys %{ $sorted{$perc} }) {
788 push(@list, "$_ - $sorted{$perc}{$_}");
792 # hack. this is one major downside to scheduling.
794 &pSReply( &formListReply(0, "IRC Client versions for $c ", @list) );
796 # clean up not-needed data structures.
806 last unless (scalar @vernicktodo);
808 my $n = shift(@vernicktodo);
809 $conn->ctcp("VERSION", $n);
812 return unless (scalar @vernicktodo);
814 $conn->schedule(3, \&verstats_flush() );
817 sub do_text_counters {
819 $itc =~ s/([^\w\s])/\\$1/g;
820 my $z = join '|', split ' ', $itc;
822 if ($msgType eq "privmsg" and $message =~ / ($mask{chan})$/) {
823 &DEBUG("ircTC: privmsg detected; chan = $1");
827 if ($message =~ /^_stats(\s+(\S+))$/i) {
833 if ($message =~ /^($z)stats(\s+(\S+))?$/i) {
840 # even more uglier with channel/time arguments.
842 # my $c = $chan || "PRIVATE";
843 my $where = "type=".&sqlQuote($type);
846 $where .= " AND channel=".&sqlQuote($c) if (defined $c);
848 &DEBUG("not using chan arg");
851 my $sum = (&sqlRawReturn("SELECT SUM(counter) FROM stats"
852 ." WHERE ".$where ))[0];
854 if (!defined $arg or $arg =~ /^\s*$/) {
855 # this is way fucking ugly.
857 # TODO: convert $where to hash
858 my %hash = &sqlSelectColHash("stats", "nick,counter",
860 $where." ORDER BY counter DESC LIMIT 3", 1
865 # unfortunately we have to sort it again!
867 foreach $i (sort { $b <=> $a } keys %hash) {
868 foreach (keys %{ $hash{$i} }) {
869 my $p = sprintf("%.01f", 100*$i/$sum);
871 push(@top, "\002$_\002 -- $i ($p%)");
876 $topstr = ". Top ".scalar(@top).": ".join(', ', @top);
880 &pSReply("total count of \037$type\037 on \002$c\002: $sum$topstr");
882 &pSReply("zero counter for \037$type\037.");
885 # TODO: convert $where to hash and use a sqlSelect
886 my $x = (&sqlRawReturn("SELECT SUM(counter) FROM stats".
887 " WHERE $where AND nick=".&sqlQuote($arg) ))[0];
889 if (!defined $x) { # !defined.
890 &pSReply("$arg has not said $type yet.");
895 # TODO: convert $where to hash
896 my @array = &sqlSelect("stats", "nick", undef,
897 $where." ORDER BY counter", 1
901 for ($i=0; $i<scalar @array; $i++) {
902 next unless ($array[0] =~ /^\Q$who\E$/);
908 my $total = scalar(@array);
910 if ($total and $good) {
911 my $pct = sprintf("%.01f", 100*(1+$total-$i)/$total);
912 $xtra = ", ranked $i\002/\002$total (percentile: \002$pct\002 %)";
915 my $pct1 = sprintf("%.01f", 100*$x/$sum);
916 &pSReply("\002$arg\002 has said \037$type\037 \002$x\002 times (\002$pct1\002 %)$xtra");
925 # even more uglier with channel/time arguments.
927 # my $c = $chan || "PRIVATE";
928 &DEBUG("not using chan arg") if (!defined $c);
930 # example of converting from RawReturn to sqlSelect.
931 my $where_href = (defined $c) ? { channel => $c } : "";
932 my $sum = &sqlSelect("stats", "SUM(counter)", $where_href);
934 if (!defined $arg or $arg =~ /^\s*$/) {
935 # this is way fucking ugly.
936 &DEBUG("_stats: !arg");
938 my %hash = &sqlSelectColHash("stats", "nick,counter",
940 " ORDER BY counter DESC LIMIT 3", 1
945 # unfortunately we have to sort it again!
947 foreach $i (sort { $b <=> $a } keys %hash) {
948 foreach (keys %{ $hash{$i} }) {
949 my $p = sprintf("%.01f", 100*$i/$sum);
951 push(@top, "\002$_\002 -- $i ($p%)");
957 $topstr = ". Top ".scalar(@top).": ".join(', ', @top);
961 &pSReply("total count of \037$type\037 on \002$c\002: $sum$topstr");
963 &pSReply("zero counter for \037$type\037.");
969 # TODO: add nick to where_href
970 my %hash = &sqlSelectColHash("stats", "type,counter",
971 $where_href, " AND nick=".&sqlQuote($arg)
973 # this is totally fucked... needs to be fixed... and cleaned up.
979 foreach (keys %hash) {
980 &DEBUG("_stats: hash{$_} => $hash{$_}");
982 # TODO: convert $where to hash
983 my @array = &sqlSelect("stats", "nick", undef,
984 $where." ORDER BY counter", 1);
987 for(my $i=0; $i<scalar @array; $i++) {
988 next unless ($array[0] =~ /^\Q$who\E$/);
994 $total = scalar(@array);
995 &DEBUG(" i => $i, good => $good, total => $total");
996 $x .= " ".$total."blah blah";
1001 if (!defined $x) { # !defined.
1002 &pSReply("$arg has not said $type yet.");
1007 if ($total and $good) {
1008 my $pct = sprintf("%.01f", 100*(1+$total-$ii)/$total);
1009 $xtra = ", ranked $ii\002/\002$total (percentile: \002$pct\002 %)";
1012 my $pct1 = sprintf("%.01f", 100*$x/$sum);
1013 &pSReply("\002$arg\002 has said \037$type\037 \002$x\002 times (\002$pct1\002 %)$xtra");
1018 return unless (defined $arg);
1020 #foreach (`$arg`) { &msg($who,$_); }