]> git.donarmstrong.com Git - infobot.git/blob - src/CommandStubs.pl
- irctextcounters: add percentage to top3
[infobot.git] / src / CommandStubs.pl
1 #
2 # User Command Extension Stubs
3 # WARN: this file does not reload on HUP.
4 #
5
6 if (&IsParam("useStrict")) { use strict; }
7
8 $babel_lang_regex = "fr|sp|po|pt|it|ge|de|gr|en";
9
10 ### COMMAND HOOK IMPLEMENTATION.
11 # addCmdHook("SECTION", 'TEXT_HOOK',
12 #       (CODEREF        => 'Blah', 
13 #       Forker          => 1,
14 #       CheckModule     => 1,                   # ???
15 #       Module          => 'blah.pl'            # preload module.
16 #       Identifier      => 'config_label',      # change to Config?
17 #       Help            => 'help_label',
18 #       Cmdstats        => 'text_label',)
19 #}
20 ###
21
22 sub addCmdHook {
23     my ($hashname, $ident, %hash) = @_;
24
25     if (exists ${"hooks_$hashname"}{$ident}) {
26 ###     &WARN("aCH: cmd hooks \%$hashname{$ident} already exists.");
27         return;
28     }
29
30     &VERB("aCH: added $ident",2);       # use $hash{'Identifier'}?
31     ### hrm... prevent warnings?
32     ${"hooks_$hashname"}{$ident} = \%hash;
33 }
34
35 # RUN IF ADDRESSED.
36 sub parseCmdHook {
37     my ($hashname, $line) = @_;
38     $line =~ /^(\S+)( (.*))?$/;
39     my $cmd     = $1;   # command name is whitespaceless.
40     my $flatarg = $3;
41     my @args    = split(/\s+/, $flatarg || '');
42     my $done    = 0;
43
44     &shmFlush();
45
46     if (!defined %{"hooks_$hashname"}) {
47         &WARN("cmd hooks \%$hashname does not exist.");
48         return 0;
49     }
50
51     if (!defined $cmd) {
52         &WARN("cstubs: cmd == NULL.");
53         return 0;
54     }
55
56     foreach (keys %{"hooks_$hashname"}) {
57         # rename to something else! like $id or $label?
58         my $ident = $_;
59
60         next unless ($cmd =~ /^$ident$/i);
61
62         if ($done) {
63             &WARN("pCH: Multiple hook match: $ident");
64             next;
65         }
66
67         &status("hooks($hashname): $cmd matched '$ident'");
68         my %hash = %{ ${"hooks_$hashname"}{$ident} };
69
70         if (!scalar keys %hash) {
71             &WARN("CmdHook: hash is NULL?");
72             return 1;
73         }
74
75         if ($hash{NoArgs} and $flatarg) {
76             &DEBUG("cmd $ident does not take args; skipping.");
77             next;
78         }
79
80         if (!exists $hash{CODEREF}) {
81             &ERROR("CODEREF undefined for $cmd or $ident.");
82             return 1;
83         }
84
85         ### DEBUG.
86         foreach (keys %hash) {
87             &VERB(" $cmd->$_ => '$hash{$_}'.",2);
88         }
89
90         ### HELP.
91         if (exists $hash{'Help'} and !scalar(@args)) {
92             &help( $hash{'Help'} );
93             return 1;
94         }
95
96         ### IDENTIFIER.
97         if (exists $hash{'Identifier'}) {
98             return 1 unless (&hasParam($hash{'Identifier'}));
99         }
100
101         ### USER FLAGS.
102         if (exists $hash{'UserFlag'}) {
103             return 1 unless (&hasFlag($hash{'UserFlag'}));
104         }
105
106         ### FORKER,IDENTIFIER,CODEREF.
107         if (exists $hash{'Forker'}) {
108             $hash{'Identifier'} .= "-" if ($hash{'Forker'} eq "NULL");
109
110             if (exists $hash{'ArrayArgs'}) {
111                 &Forker($hash{'Identifier'}, sub { \&{ $hash{'CODEREF'} }(@args) } );
112             } else {
113                 &Forker($hash{'Identifier'}, sub { \&{ $hash{'CODEREF'} }($flatarg) } );
114             }
115
116         } else {
117             if (exists $hash{'Module'}) {
118                 &loadMyModule($myModules{ $hash{'Module'} });
119             }
120
121             # check if CODEREF exists.
122             if (!defined &{ $hash{'CODEREF'} }) {
123                 &WARN("coderef $hash{'CODEREF'} don't exist.");
124                 if (defined $who) {
125                     &msg($who, "coderef does not exist for $ident.");
126                 }
127
128                 return 1;
129             }
130
131             if (exists $hash{'ArrayArgs'}) {
132                 &{ $hash{'CODEREF'} }(@args);
133             } else {
134                 &{ $hash{'CODEREF'} }($flatarg);
135             }
136         }
137
138         ### CMDSTATS.
139         if (exists $hash{'Cmdstats'}) {
140             $cmdstats{ $hash{'Cmdstats'} }++;
141         }
142
143         &VERB("hooks: End of command.",2);
144
145         $done = 1;
146     }
147
148     return 1 if ($done);
149     return 0;
150 }
151
152 ###
153 ### START ADDING HOOKS.
154 ###
155 &addCmdHook("extra", 'd?bugs', ('CODEREF' => 'DBugs::Parse',
156         'Forker' => 1, 'Identifier' => 'debianExtra',
157         'Cmdstats' => 'Debian Bugs') );
158 &addCmdHook("extra", 'dauthor', ('CODEREF' => 'Debian::searchAuthor',
159         'Forker' => 1, 'Identifier' => 'debian',
160         'Cmdstats' => 'Debian Author Search', 'Help' => "dauthor" ) );
161 &addCmdHook("extra", '(d|search)desc', ('CODEREF' => 'Debian::searchDescFE',
162         'Forker' => 1, 'Identifier' => 'debian',
163         'Cmdstats' => 'Debian Desc Search', 'Help' => "ddesc" ) );
164 &addCmdHook("extra", 'dnew', ('CODEREF' => 'DebianNew',
165         'Identifier' => 'debian' ) );
166 &addCmdHook("extra", 'dincoming', ('CODEREF' => 'Debian::generateIncoming',
167         'Forker' => 1, 'Identifier' => 'debian' ) );
168 &addCmdHook("extra", 'dstats', ('CODEREF' => 'Debian::infoStats',
169         'Forker' => 1, 'Identifier' => 'debian',
170         'Cmdstats' => 'Debian Statistics' ) );
171 &addCmdHook("extra", 'd?contents', ('CODEREF' => 'Debian::searchContents',
172         'Forker' => 1, 'Identifier' => 'debian',
173         'Cmdstats' => 'Debian Contents Search', 'Help' => "contents" ) );
174 &addCmdHook("extra", 'd?find', ('CODEREF' => 'Debian::DebianFind',
175         'Forker' => 1, 'Identifier' => 'debian',
176         'Cmdstats' => 'Debian Search', 'Help' => "find" ) );
177 &addCmdHook("extra", 'insult', ('CODEREF' => 'Insult::Insult',
178         'Forker' => 1, 'Identifier' => 'insult', 'Help' => "insult" ) );
179 &addCmdHook("extra", 'kernel', ('CODEREF' => 'Kernel::Kernel',
180         'Forker' => 1, 'Identifier' => 'kernel',
181         'Cmdstats' => 'Kernel', 'NoArgs' => 1) );
182 &addCmdHook("extra", 'listauth', ('CODEREF' => 'CmdListAuth',
183         'Identifier' => 'search', Module => 'factoids', 
184         'Help' => 'listauth') );
185 &addCmdHook("extra", 'quote', ('CODEREF' => 'Quote::Quote',
186         'Forker' => 1, 'Identifier' => 'quote',
187         'Help' => 'quote', 'Cmdstats' => 'Quote') );
188 &addCmdHook("extra", 'countdown', ('CODEREF' => 'Countdown',
189         'Module' => 'countdown', 'Identifier' => 'countdown',
190         'Cmdstats' => 'Countdown') );
191 &addCmdHook("extra", 'lart', ('CODEREF' => 'lart',
192         'Identifier' => 'lart', 'Help' => 'lart') );
193 &addCmdHook("extra", 'convert', ('CODEREF' => 'convert',
194         'Forker' => 1, 'Identifier' => 'units',
195         'Help' => 'convert') );
196 &addCmdHook("extra", '(cookie|random)', ('CODEREF' => 'cookie',
197         'Forker' => 1, 'Identifier' => 'factoids') );
198 &addCmdHook("extra", 'u(ser)?info', ('CODEREF' => 'userinfo',
199         'Identifier' => 'userinfo', 'Help' => 'userinfo',
200         'Module' => 'userinfo') );
201 &addCmdHook("extra", 'rootWarn', ('CODEREF' => 'CmdrootWarn',
202         'Identifier' => 'rootWarn', 'Module' => 'rootwarn') );
203 &addCmdHook("extra", 'seen', ('CODEREF' => 'seen', 'Identifier' =>
204         'seen') );
205 &addCmdHook("extra", 'dict', ('CODEREF' => 'Dict::Dict',
206         'Identifier' => 'dict', 'Help' => 'dict',
207         'Forker' => 1, 'Cmdstats' => 'Dict') );
208 &addCmdHook("extra", 'slashdot', ('CODEREF' => 'Slashdot::Slashdot',
209         'Identifier' => 'slashdot', 'Forker' => 1,
210         'Cmdstats' => 'Slashdot') );
211 &addCmdHook("extra", 'uptime', ('CODEREF' => 'uptime', 'Identifier' => 'uptime',
212         'Cmdstats' => 'Uptime') );
213 &addCmdHook("extra", 'nullski', ('CODEREF' => 'nullski', ) );
214 &addCmdHook("extra", '(fm|freshmeat)', ('CODEREF' => 'Freshmeat::Freshmeat',
215         'Identifier' => 'freshmeat', 'Cmdstats' => 'Freshmeat',
216         'Forker' => 1, 'Help' => 'freshmeat') );
217 &addCmdHook("extra", 'verstats', ('CODEREF' => 'do_verstats' ) );
218
219 ###
220 ### END OF ADDING HOOKS.
221 ###
222 &status("CMD: loaded ".scalar(keys %hooks_extra)." EXTRA command hooks.");
223
224 sub Modules {
225     if (!defined $message) {
226         &WARN("Modules: message is undefined. should never happen.");
227         return;
228     }
229
230     # babel bot: Jonathan Feinberg++
231     if ($message =~ m{
232                 ^\s*
233                 (?:babel(?:fish)?|x|xlate|translate)
234                 \s+
235                 (to|from)               # direction of translation (through)
236                 \s+
237                 ($babel_lang_regex)\w*  # which language?
238                 \s*
239                 (.+)                    # The phrase to be translated
240     }xoi) {
241         return unless (&hasParam("babelfish"));
242
243         &Forker("babelfish", sub { &babel::babelfish(lc $1, lc $2, $3); } );
244
245         $cmdstats{'BabelFish'}++;
246         return;
247     }
248
249     my $debiancmd        = 'conflicts?|depends?|desc|file|info|provides?';
250     $debiancmd          .= '|recommends?|suggests?|maint|maintainer';
251
252     if ($message =~ /^($debiancmd)(\s+(.*))?$/i) {
253         return unless (&hasParam("debian"));
254         my $package = lc $3;
255
256         if (defined $package) {
257             &Forker("debian", sub { &Debian::infoPackages($1, $package); } );
258         } else {
259             &help($1);
260         }
261
262         return;
263     }
264
265     # google searching. Simon++
266     if ($message =~ /^(?:search\s+)?(\S+)\s+for\s+['"]?(.*?)["']?\s*\?*$/i) {
267         return unless (&hasParam("wwwsearch"));
268
269         &Forker("wwwsearch", sub { &W3Search::W3Search($1,$2); } );
270
271         $cmdstats{'WWWSearch'}++;
272         return;
273     }
274
275     # text counters.
276     # warn: lets process it anyway.
277     if (1 and $_ = &getChanConf("ircTextCounters")) {
278         s/([^\w\s])/\\$1/g;
279         my $z = join '|', split ' ';
280
281         if ($message =~ /^($z)stats(\s+(\S+))?$/i) {
282             my $type    = $1;
283             my $arg     = $3;
284
285             if (!defined $arg or $arg =~ /^\s*$/) {
286                 # this is way fucking ugly.
287                 my $x = (&dbRawReturn("SELECT SUM(counter) FROM stats WHERE type=".&dbQuote($type) ))[0];
288                 my %hash = &dbGetCol("stats", "nick,counter", "type=".&dbQuote($type).
289                         " ORDER BY counter DESC LIMIT 3", 1);
290                 my $i;
291                 my @top;
292
293                 # unfortunately we have to sort it again!
294                 # todo: make dbGetCol return hash and array? too much effort.
295                 my $tp = 0;
296                 foreach $i (sort { $b <=> $a } keys %hash) {
297                     foreach (keys %{ $hash{$i} }) {
298                         my $p   = sprintf("%.01f", 100*$i/$x);
299                         $tp     += $p;
300                         push(@top, "\002$_\002 -- $i ($p%)");
301                     }
302                 }
303                 my $topstr = "";
304                 &DEBUG("tp => $tp");
305                 if (scalar @top) {
306                     $topstr = ".  Top ".scalar(@top).": ".join(', ', @top);
307                 }
308
309                 if (defined $x) {
310                     &pSReply("total count of '$type': $x$topstr");
311                 } else {
312                     &pSReply("zero counter for '$type'.");
313                 }
314             } else {
315                 my $x = (&dbRawReturn("SELECT SUM(counter) FROM stats WHERE type=".
316                         &dbQuote($type)." AND nick=".&dbQuote($arg) ))[0];
317
318                 if (defined $x) {       # defined.
319                     &pSReply("$arg has said $type $x times");
320                 } else {                # !defined.
321                     &pSReply("$arg has not said $type yet.");
322                 }
323             }
324
325             return;
326         }
327
328         if ($@) {
329             &DEBUG("regex failed: $@");
330             return;
331         }
332     }
333
334     # list{keys|values}. xk++. Idea taken from #linuxwarez@EFNET
335     if ($message =~ /^list(\S+)(\s+(.*))?$/i) {
336         return unless (&hasParam("search"));
337
338         my $thiscmd     = lc $1;
339         my $args        = $3 || "";
340
341         $thiscmd        =~ s/^vals$/values/;
342 #       $args           =~ s/\s+$//g;
343         return if ($thiscmd ne "keys" && $thiscmd ne "values");
344
345         # Usage:
346         if (!defined $args or $args =~ /^\s*$/) {
347             &help("list". $thiscmd);
348             return;
349         }
350
351         # suggested by asuffield and \broken.
352         if ($args =~ /^["']/ and $args =~ /["']$/) {
353             &DEBUG("list*: removed quotes.");
354             $args       =~ s/^["']|["']$//g;
355         }
356
357         if (length $args == 1) {
358             &msg($who,"search string is too short.");
359             return;
360         }
361
362         &Forker("search", sub { &Search::Search($thiscmd, $args); } );
363
364         $cmdstats{'Factoid Search'}++;
365         return;
366     }
367
368     # Nickometer. Adam Spiers++
369     if ($message =~ /^(?:lame|nick)ometer(?: for)? (\S+)/i) {
370         return unless (&hasParam("nickometer"));
371
372         my $term = (lc $1 eq 'me') ? $who : $1;
373
374         &loadMyModule($myModules{'nickometer'});
375
376         if ($term =~ /^$mask{chan}$/) {
377             &status("Doing nickometer for chan $term.");
378
379             if (!&validChan($term)) {
380                 &msg($who, "error: channel is invalid.");
381                 return;
382             }
383
384             # step 1.
385             my %nickometer;
386             foreach (keys %{ $channels{lc $term}{''} }) {
387                 my $str   = $_;
388                 if (!defined $str) {
389                     &WARN("nickometer: nick in chan $term undefined?");
390                     next;
391                 }
392
393                 my $value = &nickometer($str);
394                 $nickometer{$value}{$str} = 1;
395             }
396
397             # step 2.
398             ### TODO: compact with map?
399             my @list;
400             foreach (sort {$b <=> $a} keys %nickometer) {
401                 my $str = join(", ", sort keys %{ $nickometer{$_} });
402                 push(@list, "$str ($_%)");
403             }
404
405             &pSReply( &formListReply(0, "Nickometer list for $term ", @list) );
406             &DEBUG("test.");
407
408             return;
409         }
410
411         my $percentage = &nickometer($term);
412
413         if ($percentage =~ /NaN/) {
414             $percentage = "off the scale";
415         } else {
416             $percentage = sprintf("%0.4f", $percentage);
417             $percentage =~ s/\.?0+$//;
418             $percentage .= '%';
419         }
420
421         if ($msgType eq 'public') {
422             &say("'$term' is $percentage lame, $who");
423         } else {
424             &msg($who, "the 'lame nick-o-meter' reading for $term is $percentage, $who");
425         }
426
427         return;
428     }
429
430     # Topic management. xk++
431     # may want to add a userflags for topic. -xk
432     if ($message =~ /^topic(\s+(.*))?$/i) {
433         return unless (&hasParam("topic"));
434
435         my $chan        = $talkchannel;
436         my @args        = split / /, $2 || "";
437
438         if (!scalar @args) {
439             &msg($who,"Try 'help topic'");
440             return;
441         }
442
443         $chan           = lc(shift @args) if ($msgType eq 'private');
444         my $thiscmd     = shift @args;
445
446         # topic over public:
447         if ($msgType eq 'public' && $thiscmd =~ /^#/) {
448             &msg($who, "error: channel argument is not required.");
449             &msg($who, "\002Usage\002: topic <CMD>");
450             return;
451         }
452
453         # topic over private:
454         if ($msgType eq 'private' && $chan !~ /^#/) {
455             &msg($who, "error: channel argument is required.");
456             &msg($who, "\002Usage\002: topic #channel <CMD>");
457             return;
458         }
459
460         if (&validChan($chan) == 0) {
461             &msg($who,"error: invalid channel \002$chan\002");
462             return;
463         }
464
465         # for semi-outsiders.
466         if (!&IsNickInChan($who,$chan)) {
467             &msg($who, "Failed. You ($who) are not in $chan, hey?");
468             return;
469         }
470
471         # now lets do it.
472         &loadMyModule($myModules{'topic'});
473         &Topic($chan, $thiscmd, join(' ', @args));
474         $cmdstats{'Topic'}++;
475         return;
476     }
477
478     # wingate.
479     if ($message =~ /^wingate$/i) {
480         return unless (&hasParam("wingate"));
481
482         my $reply = "Wingate statistics: scanned \002"
483                         .scalar(keys %wingate)."\002 hosts";
484         my $queue = scalar(keys %wingateToDo);
485         if ($queue) {
486             $reply .= ".  I have \002$queue\002 hosts in the queue";
487             $reply .= ".  Started the scan ".&Time2String(time() - $wingaterun)." ago";
488         }
489
490         &performStrictReply("$reply.");
491
492         return;
493     }
494
495     # do nothing and let the other routines have a go
496     return "CONTINUE";
497 }
498
499 # Freshmeat. xk++
500 sub freshmeat {
501     my ($query) = @_;
502
503     if (!defined $query) {
504         &help("freshmeat");
505         &msg($who, "I have \002".&countKeys("freshmeat")."\002 entries.");
506         return;
507     }
508
509     &Freshmeat::Freshmeat($query);
510 }
511
512 # Uptime. xk++
513 sub uptime {
514     my $count = 1;
515     &msg($who, "- Uptime for $ident -");
516     &msg($who, "Now: ". &Time2String(&uptimeNow()) ." running $bot_version");
517
518     foreach (&uptimeGetInfo()) {
519         /^(\d+)\.\d+ (.*)/;
520         my $time = &Time2String($1);
521         my $info = $2;
522
523         &msg($who, "$count: $time $2");
524         $count++;
525     }
526 }
527
528 # seen.
529 sub seen {
530     my($person) = @_;
531
532     if (!defined $person or $person =~ /^$/) {
533         &help("seen");
534
535         my $i = &countKeys("seen");
536         &msg($who,"there ". &fixPlural("is",$i) ." \002$i\002 ".
537                 "seen ". &fixPlural("entry",$i) ." that I know of.");
538
539         return;
540     }
541
542     my @seen;
543     $person =~ s/\?*$//;
544
545     &seenFlush();       # very evil hack. oh well, better safe than sorry.
546
547     ### TODO: Support &dbGetColInfo(); like in &FactInfo();
548     my $select = "nick,time,channel,host,message";
549     if ($person eq "random") {
550         @seen = &randKey("seen", $select);
551     } else {
552         @seen = &dbGet("seen", $select, "nick=".&dbQuote($person) );
553     }
554
555     if (scalar @seen < 2) {
556         foreach (@seen) {
557             &DEBUG("seen: _ => '$_'.");
558         }
559         &performReply("i haven't seen '$person'");
560         return;
561     }
562
563     # valid seen.
564     my $reply;
565     ### TODO: multi channel support. may require &IsNick() to return
566     ### all channels or something.
567     my @chans = &getNickInChans($seen[0]);
568     if (scalar @chans) {
569         $reply = "$seen[0] is currently on";
570
571         foreach (@chans) {
572             $reply .= " ".$_;
573             next unless (exists $userstats{lc $seen[0]}{'Join'});
574             $reply .= " (".&Time2String(time() - $userstats{lc $seen[0]}{'Join'}).")";
575         }
576
577         if (&IsParam("seenStats")) {
578             my $i;
579             $i = $userstats{lc $seen[0]}{'Count'};
580             $reply .= ".  Has said a total of \002$i\002 messages" if (defined $i);
581             $i = $userstats{lc $seen[0]}{'Time'};
582             $reply .= ".  Is idling for ".&Time2String(time() - $i) if (defined $i);
583         }
584     } else {
585         my $howlong = &Time2String(time() - $seen[1]);
586         $reply = "$seen[0] <$seen[3]> was last seen on IRC ".
587                  "in channel $seen[2], $howlong ago, ".
588                  "saying\002:\002 '$seen[4]'.";
589     }
590
591     &pSReply($reply);
592     return;
593 }
594
595 # User Information Services. requested by Flugh.
596 sub userinfo {
597     my ($arg) = join(' ',@_);
598
599     if ($arg =~ /^set(\s+(.*))?$/i) {
600         $arg = $2;
601         if (!defined $arg) {
602             &help("userinfo set");
603             return;
604         }
605
606         &UserInfoSet(split /\s+/, $arg, 2);
607     } elsif ($arg =~ /^unset(\s+(.*))?$/i) {
608         $arg = $2;
609         if (!defined $arg) {
610             &help("userinfo unset");
611             return;
612         }
613
614         &UserInfoSet($arg, "");
615     } else {
616         &UserInfoGet($arg);
617     }
618 }
619
620 # cookie (random). xk++
621 sub cookie {
622     my ($arg) = @_;
623
624     # lets find that secret cookie.
625     my $target          = ($msgType ne 'public') ? $who : $talkchannel;
626     my $cookiemsg       = &getRandom(keys %{ $lang{'cookie'} });
627     my ($key,$value);
628
629     ### WILL CHEW TONS OF MEM.
630     ### TODO: convert this to a Forker function!
631     if ($arg) {
632         my @list = &searchTable("factoids", "factoid_key", "factoid_value", $arg);
633         $key  = &getRandom(@list);
634         $val  = &getFactInfo("factoids", $key, "factoid_value");
635     } else {
636         ($key,$value) = &randKey("factoids","factoid_key,factoid_value");
637     }
638
639     for ($cookiemsg) {
640         s/##KEY/\002$key\002/;
641         s/##VALUE/$value/;
642         s/##WHO/$who/;
643         s/\$who/$who/;  # cheap fix.
644         s/(\S+)?\s*<\S+>/$1 /;
645         s/\s+/ /g;
646     }
647
648     if ($cookiemsg =~ s/^ACTION //i) {
649         &action($target, $cookiemsg);
650     } else {
651         &msg($target, $cookiemsg);
652     }
653 }
654
655 sub convert {
656     my $arg = join(' ',@_);
657     my ($from,$to) = ('','');
658
659     ($from,$to) = ($1,$2) if ($arg =~ /^(.*?) to (.*)$/i);
660     ($from,$to) = ($2,$1) if ($arg =~ /^(.*?) from (.*)$/i);
661
662     if (!$to or !$from) {
663         &msg($who, "Invalid format!");
664         &help("convert");
665         return;
666     }
667
668     &Units::convertUnits($from, $to);
669
670     return;
671 }
672
673 sub lart {
674     my ($target) = &fixString($_[0]);
675     my $extra   = 0;
676     my $chan    = $talkchannel;
677
678     if ($msgType eq 'private') {
679         if ($target =~ /^($mask{chan})\s+(.*)$/) {
680             $chan       = $1;
681             $target     = $2;
682             $extra      = 1;
683         } else {
684             &msg($who, "error: invalid format or missing arguments.");
685             &help("lart");
686             return;
687         }
688     }
689
690     my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.lart");
691     if (defined $line) {
692         if ($target =~ /^(me|you|itself|\Q$ident\E)$/i) {
693             $line =~ s/WHO/$who/g;
694         } else {
695             $line =~ s/WHO/$target/g;
696         }
697         $line .= ", courtesy of $who" if ($extra);
698
699         &action($chan, $line);
700     } else {
701         &status("lart: error reading file?");
702     }
703 }
704
705 sub DebianNew {
706     my $idx   = "debian/Packages-woody.idx";
707     my $error = 0;
708     my %pkg;
709     my @new;
710
711     $error++ unless ( -e $idx);
712     $error++ unless ( -e "$idx-old");
713
714     if ($error) {
715         $error = "no woody/woody-old index file found.";
716         &ERROR("Debian: $error");
717         &msg($who, $error);
718         return;
719     }
720
721     open(IDX1, $idx);
722     open(IDX2, "$idx-old");
723
724     while (<IDX2>) {
725         chop;
726         next if (/^\*/);
727
728         $pkg{$_} = 1;
729     }
730     close IDX2;
731
732     open(IDX1,$idx);
733     while (<IDX1>) {
734         chop;
735         next if (/^\*/);
736         next if (exists $pkg{$_});
737
738         push(@new);
739     }
740     close IDX1;
741
742     &::performStrictReply( &::formListReply(0, "New debian packages:", @new) );
743 }
744
745 sub do_verstats {
746     my ($chan)  = @_;
747
748     if (!defined $chan) {
749         &help("verstats");
750         return;
751     }
752
753     if (!&validChan($chan)) {
754         &msg($who, "chan $chan is invalid.");
755         return;
756     }
757
758     if (scalar @vernick > scalar(keys %{ $channels{lc $chan}{''} })/4) {
759         &msg($who, "verstats already in progress for someone else.");
760         return;
761     }
762
763     &msg($who, "Sending CTCP VERSION...");
764     $conn->ctcp("VERSION", $chan);
765     $cache{verstats}{chan}      = $chan;
766     $cache{verstats}{who}       = $who;
767     $cache{verstats}{msgType}   = $msgType;
768
769     $conn->schedule(60, sub {
770         my $vtotal      = 0;
771         my $c           = lc $cache{verstats}{chan};
772         my $total       = keys %{ $channels{$c}{''} };
773         $chan           = $c;
774         $who            = $cache{verstats}{who};
775         $msgType        = $cache{verstats}{msgType};
776         delete $cache{verstats};        # sufficient?
777
778         foreach (keys %ver) {
779             $vtotal     += scalar keys %{ $ver{$_} };
780         }
781
782         my %sorted;
783         my $unknown     = $total - $vtotal;
784         my $perc        = sprintf("%.1f", $unknown * 100 / $total);
785         $perc           =~ s/.0$//;
786         $sorted{$perc}{"unknown/cloak"} = "$unknown ($perc%)";
787
788         foreach (keys %ver) {
789             my $count   = scalar keys %{ $ver{$_} };
790             $perc       = sprintf("%.01f", $count * 100 / $total);
791             $perc       =~ s/.0$//;     # lame compression.
792
793             $sorted{$perc}{$_} = "$count ($perc%)";
794         }
795
796         ### can be compressed to a map?
797         my @list;
798         foreach ( sort { $b <=> $a } keys %sorted ) {
799             my $perc = $_;
800             foreach (sort keys %{ $sorted{$perc} }) {
801                 push(@list, "$_ - $sorted{$perc}{$_}");
802             }
803         }
804
805         &pSReply( &formListReply(0, "IRC Client versions for $c ", @list) );
806
807         # clean up not-needed data structures.
808         undef %ver;
809         undef @vernick;
810     } );
811
812     return;
813 }
814
815 sub nullski { my ($arg) = @_; return unless (defined $arg);
816         foreach (`$arg`) { &msg($who,$_); } }
817
818 1;