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