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 $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 $w3search_regex = "google";
19 ### COMMAND HOOK IMPLEMENTATION.
20 # addCmdHook("SECTION", 'TEXT_HOOK',
23 # Module => 'blah.pl' # preload module.
24 # Identifier => 'config_label', # change to Config?
25 # Help => 'help_label',
26 # Cmdstats => 'text_label',)
31 my ($hashname, $ident, %hash) = @_;
33 if (exists ${"hooks_$hashname"}{$ident}) {
34 ### &WARN("aCH: cmd hooks \%$hashname{$ident} already exists.");
38 &VERB("aCH: added $ident",2); # use $hash{'Identifier'}?
39 ### hrm... prevent warnings?
40 ${"hooks_$hashname"}{$ident} = \%hash;
45 my ($hashname, $line) = @_;
46 $line =~ s/^\s+|\s+$//g; # again.
47 $line =~ /^(\S+)(\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' '$flatarg'");
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 (&IsChanConfOrWarn($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($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' => 'DebianExtra::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", 'metar', ('CODEREF' => 'Weather::Metar',
231 'Identifier' => 'Weather', 'Help' => 'weather',
232 'Cmdstats' => 'Weather', 'Forker' => 1) );
233 &addCmdHook("extra", 'bzflist', ('CODEREF' => 'BZFlag::list',
234 'Identifier' => 'BZFlag', 'Cmdstats' => 'BZFlag',
236 &addCmdHook("extra", 'bzflist17', ('CODEREF' => 'BZFlag::list17',
237 'Identifier' => 'BZFlag', 'Cmdstats' => 'BZFlag',
239 &addCmdHook("extra", 'bzfquery', ('CODEREF' => 'BZFlag::query',
240 'Identifier' => 'BZFlag', 'Cmdstats' => 'BZFlag',
242 &addCmdHook("extra", 'zfi', ('CODEREF' => 'zfi::query',
243 'Identifier' => 'zfi', 'Cmdstats' => 'zfi',
245 &addCmdHook("extra", '(zippy|yow)', ('CODEREF' => 'zippy::get',
246 'Identifier' => 'Zippy', 'Cmdstats' => 'Zippy',
248 &addCmdHook("extra", 'zsi', ('CODEREF' => 'zsi::query',
249 'Identifier' => 'zsi', 'Cmdstats' => 'zsi',
251 &addCmdHook("extra", '(ex)?change', ('CODEREF' => 'Exchange::query',
252 'Identifier' => 'Exchange', 'Cmdstats' => 'Exchange',
254 &addCmdHook("extra", '(botmail|message)', ('CODEREF' => 'botmail::parse',
255 'Identifier' => 'botmail', 'Cmdstats' => 'botmail') );
256 &addCmdHook("extra", 'HTTPDtype', ('CODEREF' => 'HTTPDtype::HTTPDtype',
257 'Identifier' => 'HTTPDtype', 'Cmdstats' => 'HTTPDtype',
259 &addCmdHook("extra", 'scramble', ('CODEREF' => 'scramble::scramble',
260 'Identifier' => 'scramble', 'Cmdstats' => 'scramble',
262 &addCmdHook("extra", 'md5(sum)?', ('CODEREF' => 'md5::md5',
263 'Identifier' => 'md5', 'Cmdstats' => 'md5',
265 &addCmdHook("extra", 'Rss', ('CODEREF' => 'Rss::Rss',
266 'Identifier' => 'Rss', 'Cmdstats' => 'Rss',
267 'Forker' => 1, 'Help' => 'rss') );
268 &addCmdHook("extra", 'wiki(pedia)?', ('CODEREF' => 'wikipedia::wikipedia',
269 'Identifier' => 'wikipedia', 'Cmdstats' => 'wikipedia',
270 'Forker' => 1, 'Help' => 'wikipedia') );
271 &addCmdHook("extra", 'page', ('CODEREF' => 'pager::page',
272 'Identifier' => 'pager', 'Cmdstats' => 'pager',
273 'Forker' => 1, 'Help' => 'page') );
274 &addCmdHook("extra", '(babel(fish)?|x|xlate|translate)', ('CODEREF' => 'babelfish::babelfish',
275 'Identifier' => 'babelfish', 'Cmdstats' => 'babelfish',
276 'Forker' => 1, 'Help' => 'babelfish') );
277 &addCmdHook("extra", 'wtf', ('CODEREF' => 'wtf::query',
278 'Identifier' => 'wtf', 'Cmdstats' => 'wtf',
279 'Forker' => 1, 'Help' => 'wtf') );
280 &addCmdHook("extra", '[ia]?spell', ('CODEREF' => 'spell::query',
281 'Identifier' => 'spell', 'Cmdstats' => 'spell',
282 'Forker' => 1, 'Help' => 'spell') );
283 &addCmdHook("extra", 'dns|d?nslookup|host', ('CODEREF' => 'dns::query',
284 'Identifier' => 'dns', 'Cmdstats' => 'dns',
285 'Forker' => 1, 'Help' => 'dns') );
286 &addCmdHook("extra", 'piglatin', ('CODEREF' => 'piglatin::piglatin',
287 'Identifier' => 'piglatin', 'Cmdstats' => 'piglatin',
290 ### END OF ADDING HOOKS.
292 &status("CMD: loaded ".scalar(keys %hooks_extra)." EXTRA command hooks.");
295 if (!defined $message) {
296 &WARN("Modules: message is undefined. should never happen.");
300 my $debiancmd = 'conflicts?|depends?|desc|file|(?:d)?info|provides?';
301 $debiancmd .= '|recommends?|suggests?|maint|maintainer';
303 if ($message =~ /^($debiancmd)(\s+(.*))?$/i) {
304 return unless (&IsChanConfOrWarn('Debian'));
307 if (defined $package) {
308 &Forker('Debian', sub { &Debian::infoPackages($1, $package); } );
316 # google searching. Simon++
317 if ($message =~ /^(?:search\s+)?($w3search_regex)\s+(?:for\s+)?['"]?(.*?)["']?\s*\?*$/i) {
318 return unless (&IsChanConfOrWarn('W3Search'));
320 &Forker('W3Search', sub { &W3Search::W3Search($1,$2); } );
322 $cmdstats{'W3Search'}++;
326 # text counters. (eg: hehstats)
328 $itc = &getChanConf('ircTextCounters');
329 $itc = &findChanConf('ircTextCounters') unless ($itc);
330 return if ($itc && &do_text_counters($itc) == 1);
331 # end of text counters.
333 # list{keys|values}. xk++. Idea taken from #linuxwarez@EFNET
334 if ($message =~ /^list(\S+)(\s+(.*))?$/i) {
335 return unless (&IsChanConfOrWarn('Search'));
340 $thiscmd =~ s/^vals$/values/;
341 return if ($thiscmd ne 'keys' && $thiscmd ne 'values');
344 if (!defined $args or $args =~ /^\s*$/) {
345 &help('list'. $thiscmd);
349 # suggested by asuffield and \broken.
350 if ($args =~ /^["']/ and $args =~ /["']$/) {
351 &DEBUG('list*: removed quotes.');
352 $args =~ s/^["']|["']$//g;
355 if (length $args < 2 && &IsFlag('o') ne 'o') {
356 &msg($who, 'search string is too short.');
360 &Forker('Search', sub { &Search::Search($thiscmd, $args); } );
362 $cmdstats{'Factoid Search'}++;
366 # Nickometer. Adam Spiers++
367 if ($message =~ /^(?:lame|nick)ometer(?: for)? (\S+)/i) {
368 return unless (&IsChanConfOrWarn("nickometer"));
370 my $term = (lc $1 eq 'me') ? $who : $1;
372 &loadMyModule('nickometer');
374 if ($term =~ /^$mask{chan}$/) {
375 &status("Doing nickometer for chan $term.");
377 if (!&validChan($term)) {
378 &msg($who, "error: channel is invalid.");
384 foreach (keys %{ $channels{lc $term}{''} }) {
387 &WARN("nickometer: nick in chan $term undefined?");
391 my $value = &nickometer($str);
392 $nickometer{$value}{$str} = 1;
396 ### TODO: compact with map?
398 foreach (sort {$b <=> $a} keys %nickometer) {
399 my $str = join(", ", sort keys %{ $nickometer{$_} });
400 push(@list, "$str ($_%)");
403 &performStrictReply( &formListReply(0, "Nickometer list for $term ", @list) );
409 my $percentage = &nickometer($term);
411 if ($percentage =~ /NaN/) {
412 $percentage = "off the scale";
414 $percentage = sprintf("%0.4f", $percentage);
415 $percentage =~ s/(\.\d+)0+$/$1/;
419 if ($msgType eq 'public') {
420 &say("'$term' is $percentage lame, $who");
422 &msg($who, "the 'lame nick-o-meter' reading for $term is $percentage, $who");
428 # Topic management. xk++
429 # may want to add a userflags for topic. -xk
430 if ($message =~ /^topic(\s+(.*))?$/i) {
431 return unless (&IsChanConfOrWarn('Topic'));
433 my $chan = $talkchannel;
434 my @args = split / /, $2 || "";
437 &msg($who,"Try 'help topic'");
441 $chan = lc(shift @args) if ($msgType eq 'private');
442 my $thiscmd = shift @args;
445 if ($msgType eq 'public' && $thiscmd =~ /^#/) {
446 &msg($who, "error: channel argument is not required.");
447 &msg($who, "\002Usage\002: topic <CMD>");
451 # topic over private:
452 if ($msgType eq 'private' && $chan !~ /^#/) {
453 &msg($who, "error: channel argument is required.");
454 &msg($who, "\002Usage\002: topic #channel <CMD>");
458 if (&validChan($chan) == 0) {
459 &msg($who,"error: invalid channel \002$chan\002");
463 # for semi-outsiders.
464 if (!&IsNickInChan($who,$chan)) {
465 &msg($who, "Failed. You ($who) are not in $chan, hey?");
470 &loadMyModule('Topic');
471 &Topic($chan, $thiscmd, join(' ', @args));
472 $cmdstats{'Topic'}++;
477 if ($message =~ /^wingate$/i) {
478 return unless (&IsChanConfOrWarn('Wingate'));
480 my $reply = "Wingate statistics: scanned \002"
481 .scalar(keys %wingate)."\002 hosts";
482 my $queue = scalar(keys %wingateToDo);
484 $reply .= ". I have \002$queue\002 hosts in the queue";
485 $reply .= ". Started the scan ".&Time2String(time() - $wingaterun)." ago";
488 &performStrictReply("$reply.");
493 # do nothing and let the other routines have a go
500 &msg($who, "- Uptime for $ident -");
501 &msg($who, "Now: ". &Time2String(&uptimeNow()) ." running $bot_version");
503 foreach (&uptimeGetInfo()) {
505 my $time = &Time2String($1);
508 &msg($who, "$count: $time $2");
515 my($person) = lc shift;
518 if (!defined $person or $person =~ /^$/) {
521 my $i = &countKeys("seen");
522 &msg($who,"there ". &fixPlural("is",$i) ." \002$i\002 ".
523 "seen ". &fixPlural("entry",$i) ." that I know of.");
530 &seenFlush(); # very evil hack. oh well, better safe than sorry.
532 # TODO: convert to &sqlSelectRowHash();
533 my $select = "nick,time,channel,host,message";
534 if ($person eq "random") {
535 @seen = &randKey("seen", $select);
537 @seen = &sqlSelect("seen", $select, { nick => $person } );
540 if (scalar @seen < 2) {
542 &DEBUG("seen: _ => '$_'.");
544 &performReply("i haven't seen '$person'");
550 ### TODO: multi channel support. may require &IsNick() to return
551 ### all channels or something.
553 my @chans = &getNickInChans($seen[0]);
555 $reply = "$seen[0] is currently on";
559 next unless (exists $userstats{lc $seen[0]}{'Join'});
560 $reply .= " (".&Time2String(time() - $userstats{lc $seen[0]}{'Join'}).")";
563 if (&IsChanConf("seenStats") > 0) {
565 $i = $userstats{lc $seen[0]}{'Count'};
566 $reply .= ". Has said a total of \002$i\002 messages" if (defined $i);
567 $i = $userstats{lc $seen[0]}{'Time'};
568 $reply .= ". Is idling for ".&Time2String(time() - $i) if (defined $i);
571 my $howlong = &Time2String(time() - $seen[1]);
572 $reply = "$seen[0] <$seen[3]> was last seen on IRC ".
573 "in channel $seen[2], $howlong ago, ".
574 "saying\002:\002 '$seen[4]'.";
577 &performStrictReply($reply);
581 # User Information Services. requested by Flugh.
583 my ($arg) = join(' ',@_);
585 if ($arg =~ /^set(\s+(.*))?$/i) {
588 &help('userinfo set');
592 &UserInfoSet(split /\s+/, $arg, 2);
593 } elsif ($arg =~ /^unset(\s+(.*))?$/i) {
596 &help('userinfo unset');
600 &UserInfoSet($arg, '');
606 # cookie (random). xk++
610 # lets find that secret cookie.
611 my $target = ($msgType ne 'public') ? $who : $talkchannel;
612 my $cookiemsg = &getRandom(keys %{ $lang{'cookie'} });
615 ### WILL CHEW TONS OF MEM.
616 ### TODO: convert this to a Forker function!
618 my @list = &searchTable("factoids", "factoid_key", "factoid_value", $arg);
619 $key = &getRandom(@list);
620 $value = &getFactInfo($key, "factoid_value");
622 ($key,$value) = &randKey("factoids","factoid_key,factoid_value");
626 s/##KEY/\002$key\002/;
629 s/\$who/$who/; # cheap fix.
630 s/(\S+)?\s*<\S+>/$1 /;
634 if ($cookiemsg =~ s/^ACTION //i) {
635 &action($target, $cookiemsg);
637 &msg($target, $cookiemsg);
642 my $arg = join(' ',@_);
643 my ($from,$to) = ('','');
645 ($from,$to) = ($1,$2) if ($arg =~ /^(.*?) to (.*)$/i);
646 ($from,$to) = ($2,$1) if ($arg =~ /^(.*?) from (.*)$/i);
648 if (!$to or !$from) {
649 &msg($who, "Invalid format!");
654 &Units::convertUnits($from, $to);
660 my ($target) = &fixString($_[0]);
662 my $chan = $talkchannel;
665 if ($msgType eq 'private') {
666 if ($target =~ /^($mask{chan})\s+(.*)$/) {
671 &msg($who, "error: invalid format or missing arguments.");
676 if ($target =~ /^(.*)(\s+for\s+.*)$/) {
681 my $line = &getRandomLineFromFile($bot_data_dir. "/blootbot.lart");
683 if ($target =~ /^(me|you|itself|\Q$ident\E)$/i) {
684 $line =~ s/WHO/$who/g;
686 $line =~ s/WHO/$target/g;
688 $line .= $for if ($for);
689 $line .= ", courtesy of $who" if ($extra);
691 &action($chan, $line);
693 &status("lart: error reading file?");
698 my $idx = "debian/Packages-sid.idx";
703 $error++ unless ( -e $idx);
704 $error++ unless ( -e "$idx-old");
707 $error = "no sid/sid-old index file found.";
708 &ERROR("Debian: $error");
714 open(IDX2, "$idx-old");
728 next if (exists $pkg{$_});
734 &::performStrictReply( &::formListReply(0, "New debian packages:", @new) );
740 if (!defined $chan) {
745 if (!&validChan($chan)) {
746 &msg($who, "chan $chan is invalid.");
750 if (scalar @vernick > scalar(keys %{ $channels{lc $chan}{''} })/4) {
751 &msg($who, "verstats already in progress for someone else.");
755 &msg($who, "Sending CTCP VERSION to $chan; results in 60s.");
756 $conn->ctcp("VERSION", $chan);
757 $cache{verstats}{chan} = $chan;
758 $cache{verstats}{who} = $who;
759 $cache{verstats}{msgType} = $msgType;
761 $conn->schedule(30, sub {
762 my $c = lc $cache{verstats}{chan};
765 foreach (keys %{ $channels{$c}{''} } ) {
766 next if (grep /^\Q$_\E$/i, @vernick);
767 push(@vernicktodo, $_);
773 $conn->schedule(60, sub {
775 my $c = lc $cache{verstats}{chan};
776 my $total = keys %{ $channels{$c}{''} };
778 $who = $cache{verstats}{who};
779 $msgType = $cache{verstats}{msgType};
780 delete $cache{verstats}; # sufficient?
782 foreach (keys %ver) {
783 $vtotal += scalar keys %{ $ver{$_} };
787 my $unknown = $total - $vtotal;
788 my $perc = sprintf("%.1f", $unknown * 100 / $total);
790 $sorted{$perc}{"unknown/cloak"} = "$unknown ($perc%)" if ($unknown);
792 foreach (keys %ver) {
793 my $count = scalar keys %{ $ver{$_} };
794 $perc = sprintf("%.01f", $count * 100 / $total);
795 $perc =~ s/.0$//; # lame compression.
797 $sorted{$perc}{$_} = "$count ($perc%)";
800 ### can be compressed to a map?
802 foreach ( sort { $b <=> $a } keys %sorted ) {
804 foreach (sort keys %{ $sorted{$perc} }) {
805 push(@list, "$_ - $sorted{$perc}{$_}");
809 # hack. this is one major downside to scheduling.
811 &performStrictReply( &formListReply(0, "IRC Client versions for $c ", @list) );
813 # clean up not-needed data structures.
823 last unless (scalar @vernicktodo);
825 my $n = shift(@vernicktodo);
826 $conn->ctcp("VERSION", $n);
829 return unless (scalar @vernicktodo);
831 $conn->schedule(3, \&verstats_flush() );
834 sub do_text_counters {
836 $itc =~ s/([^\w\s])/\\$1/g;
837 my $z = join '|', split ' ', $itc;
839 if ($msgType eq "privmsg" and $message =~ / ($mask{chan})$/) {
840 &DEBUG("ircTC: privmsg detected; chan = $1");
844 if ($message =~ /^_stats(\s+(\S+))$/i) {
850 if ($message =~ /^($z)stats(\s+(\S+))?$/i) {
857 # even more uglier with channel/time arguments.
859 # my $c = $chan || "PRIVATE";
860 my $where = "type=".&sqlQuote($type);
863 $where .= " AND channel=".&sqlQuote($c) if (defined $c);
865 &DEBUG("not using chan arg");
868 my $sum = (&sqlRawReturn("SELECT SUM(counter) FROM stats"
869 ." WHERE ".$where ))[0];
871 if (!defined $arg or $arg =~ /^\s*$/) {
872 # this is way fucking ugly.
874 # TODO: convert $where to hash
875 my %hash = &sqlSelectColHash("stats", "nick,counter",
877 $where." ORDER BY counter DESC LIMIT 3", 1
882 # unfortunately we have to sort it again!
884 foreach $i (sort { $b <=> $a } keys %hash) {
885 foreach (keys %{ $hash{$i} }) {
886 my $p = sprintf("%.01f", 100*$i/$sum);
888 push(@top, "\002$_\002 -- $i ($p%)");
893 $topstr = ". Top ".scalar(@top).": ".join(', ', @top);
897 &performStrictReply("total count of \037$type\037 on \002$c\002: $sum$topstr");
899 &performStrictReply("zero counter for \037$type\037.");
902 # TODO: convert $where to hash and use a sqlSelect
903 my $x = (&sqlRawReturn("SELECT SUM(counter) FROM stats".
904 " WHERE $where AND nick=".&sqlQuote($arg) ))[0];
906 if (!defined $x) { # !defined.
907 &performStrictReply("$arg has not said $type yet.");
912 # TODO: convert $where to hash
913 my @array = &sqlSelect("stats", "nick", undef,
914 $where." ORDER BY counter", 1
918 for ($i=0; $i<scalar @array; $i++) {
919 next unless ($array[0] =~ /^\Q$who\E$/);
925 my $total = scalar(@array);
927 if ($total and $good) {
928 my $pct = sprintf("%.01f", 100*(1+$total-$i)/$total);
929 $xtra = ", ranked $i\002/\002$total (percentile: \002$pct\002 %)";
932 my $pct1 = sprintf("%.01f", 100*$x/$sum);
933 &performStrictReply("\002$arg\002 has said \037$type\037 \002$x\002 times (\002$pct1\002 %)$xtra");
942 # even more uglier with channel/time arguments.
944 # my $c = $chan || "PRIVATE";
945 &DEBUG("not using chan arg") if (!defined $c);
947 # example of converting from RawReturn to sqlSelect.
948 my $where_href = (defined $c) ? { channel => $c } : "";
949 my $sum = &sqlSelect("stats", "SUM(counter)", $where_href);
951 if (!defined $arg or $arg =~ /^\s*$/) {
952 # this is way fucking ugly.
953 &DEBUG("_stats: !arg");
955 my %hash = &sqlSelectColHash("stats", "nick,counter",
957 " ORDER BY counter DESC LIMIT 3", 1
962 # unfortunately we have to sort it again!
964 foreach $i (sort { $b <=> $a } keys %hash) {
965 foreach (keys %{ $hash{$i} }) {
966 my $p = sprintf("%.01f", 100*$i/$sum);
968 push(@top, "\002$_\002 -- $i ($p%)");
974 $topstr = ". Top ".scalar(@top).": ".join(', ', @top);
978 &performStrictReply("total count of \037$type\037 on \002$c\002: $sum$topstr");
980 &performStrictReply("zero counter for \037$type\037.");
986 # TODO: add nick to where_href
987 my %hash = &sqlSelectColHash("stats", "type,counter",
988 $where_href, " AND nick=".&sqlQuote($arg)
990 # this is totally fucked... needs to be fixed... and cleaned up.
996 foreach (keys %hash) {
997 &DEBUG("_stats: hash{$_} => $hash{$_}");
999 # TODO: convert $where to hash
1000 my @array = &sqlSelect("stats", "nick", undef,
1001 $where." ORDER BY counter", 1);
1004 for(my $i=0; $i<scalar @array; $i++) {
1005 next unless ($array[0] =~ /^\Q$who\E$/);
1011 $total = scalar(@array);
1012 &DEBUG(" i => $i, good => $good, total => $total");
1013 $x .= " ".$total."blah blah";
1018 if (!defined $x) { # !defined.
1019 &performStrictReply("$arg has not said $type yet.");
1024 if ($total and $good) {
1025 my $pct = sprintf("%.01f", 100*(1+$total-$ii)/$total);
1026 $xtra = ", ranked $ii\002/\002$total (percentile: \002$pct\002 %)";
1029 my $pct1 = sprintf("%.01f", 100*$x/$sum);
1030 &performStrictReply("\002$arg\002 has said \037$type\037 \002$x\002 times (\002$pct1\002 %)$xtra");
1035 return unless (defined $arg);
1037 #foreach (`$arg`) { &msg($who,$_); }