2 # User Command Extension Stubs
3 # WARN: this file does not reload on HUP.
6 if (&IsParam("useStrict")) { use strict; }
8 $babel_lang_regex = "fr|sp|po|pt|it|ge|de|gr|en";
10 ### COMMAND HOOK IMPLEMENTATION.
11 # addCmdHook("SECTION", 'TEXT_HOOK',
14 # CheckModule => 1, # ???
15 # Module => 'blah.pl' # preload module.
16 # Identifier => 'config_label', # change to Config?
17 # Help => 'help_label',
18 # Cmdstats => 'text_label',)
23 my ($hashname, $ident, %hash) = @_;
25 if (exists ${"hooks_$hashname"}{$ident}) {
26 ### &WARN("aCH: cmd hooks \%$hashname{$ident} already exists.");
30 &VERB("aCH: added $ident",2); # use $hash{'Identifier'}?
31 ### hrm... prevent warnings?
32 ${"hooks_$hashname"}{$ident} = \%hash;
37 my ($hashname, $line) = @_;
38 $line =~ /^(\S+)( (.*))?$/;
39 my $cmd = $1; # command name is whitespaceless.
41 my @args = split(/\s+/, $flatarg || '');
46 if (!defined %{"hooks_$hashname"}) {
47 &WARN("cmd hooks \%$hashname does not exist.");
51 foreach (keys %{"hooks_$hashname"}) {
52 # rename to something else! like $id or $label?
55 if (!defined $cmd or !defined $ident) {
56 &WARN("cstubs: cmd or ident == NULL. ($cmd, $ident)");
59 next unless ($cmd =~ /^$ident$/i);
62 &WARN("pCH: Multiple hook match: $ident");
66 &status("hooks($hashname): $cmd matched '$ident'");
67 my %hash = %{ ${"hooks_$hashname"}{$ident} };
69 if (!scalar keys %hash) {
70 &WARN("CmdHook: hash is NULL?");
74 if ($hash{NoArgs} and $flatarg) {
75 &DEBUG("cmd $ident does not take args; skipping.");
79 if (!exists $hash{CODEREF}) {
80 &ERROR("CODEREF undefined for $cmd or $ident.");
85 foreach (keys %hash) {
86 &VERB(" $cmd->$_ => '$hash{$_}'.",2);
90 if (exists $hash{'Help'} and !scalar(@args)) {
91 &help( $hash{'Help'} );
96 if (exists $hash{'Identifier'}) {
97 return 1 unless (&hasParam($hash{'Identifier'}));
101 if (exists $hash{'UserFlag'}) {
102 return 1 unless (&hasFlag($hash{'UserFlag'}));
105 ### FORKER,IDENTIFIER,CODEREF.
106 if (exists $hash{'Forker'}) {
107 $hash{'Identifier'} .= "-" if ($hash{'Forker'} eq "NULL");
109 if (exists $hash{'ArrayArgs'}) {
110 &Forker($hash{'Identifier'}, sub { \&{ $hash{'CODEREF'} }(@args) } );
112 &Forker($hash{'Identifier'}, sub { \&{ $hash{'CODEREF'} }($flatarg) } );
116 if (exists $hash{'Module'}) {
117 &loadMyModule($myModules{ $hash{'Module'} });
120 # check if CODEREF exists.
121 if (!defined &{ $hash{'CODEREF'} }) {
122 &WARN("coderef $hash{'CODEREF'} don't exist.");
124 &msg($who, "coderef does not exist for $ident.");
130 if (exists $hash{'ArrayArgs'}) {
131 &{ $hash{'CODEREF'} }(@args);
133 &{ $hash{'CODEREF'} }($flatarg);
138 if (exists $hash{'Cmdstats'}) {
139 $cmdstats{ $hash{'Cmdstats'} }++;
142 &VERB("hooks: End of command.",2);
152 ### START ADDING HOOKS.
154 &addCmdHook("extra", 'd?bugs', ('CODEREF' => 'DBugs::Parse',
155 'Forker' => 1, 'Identifier' => 'debianExtra',
156 'Cmdstats' => 'Debian Bugs') );
157 &addCmdHook("extra", 'dauthor', ('CODEREF' => 'Debian::searchAuthor',
158 'Forker' => 1, 'Identifier' => 'debian',
159 'Cmdstats' => 'Debian Author Search', 'Help' => "dauthor" ) );
160 &addCmdHook("extra", '(d|search)desc', ('CODEREF' => 'Debian::searchDescFE',
161 'Forker' => 1, 'Identifier' => 'debian',
162 'Cmdstats' => 'Debian Desc Search', 'Help' => "ddesc" ) );
163 &addCmdHook("extra", 'dnew', ('CODEREF' => 'DebianNew',
164 'Identifier' => 'debian' ) );
165 &addCmdHook("extra", 'dincoming', ('CODEREF' => 'Debian::generateIncoming',
166 'Forker' => 1, 'Identifier' => 'debian' ) );
167 &addCmdHook("extra", 'dstats', ('CODEREF' => 'Debian::infoStats',
168 'Forker' => 1, 'Identifier' => 'debian',
169 'Cmdstats' => 'Debian Statistics' ) );
170 &addCmdHook("extra", 'd?contents', ('CODEREF' => 'Debian::searchContents',
171 'Forker' => 1, 'Identifier' => 'debian',
172 'Cmdstats' => 'Debian Contents Search', 'Help' => "contents" ) );
173 &addCmdHook("extra", 'd?find', ('CODEREF' => 'Debian::DebianFind',
174 'Forker' => 1, 'Identifier' => 'debian',
175 'Cmdstats' => 'Debian Search', 'Help' => "find" ) );
176 &addCmdHook("extra", 'insult', ('CODEREF' => 'Insult::Insult',
177 'Forker' => 1, 'Identifier' => 'insult', 'Help' => "insult" ) );
178 &addCmdHook("extra", 'kernel', ('CODEREF' => 'Kernel::Kernel',
179 'Forker' => 1, 'Identifier' => 'kernel',
180 'Cmdstats' => 'Kernel', 'NoArgs' => 1) );
181 &addCmdHook("extra", 'listauth', ('CODEREF' => 'CmdListAuth',
182 'Identifier' => 'search', Module => 'factoids',
183 'Help' => 'listauth') );
184 &addCmdHook("extra", 'quote', ('CODEREF' => 'Quote::Quote',
185 'Forker' => 1, 'Identifier' => 'quote',
186 'Help' => 'quote', 'Cmdstats' => 'Quote') );
187 &addCmdHook("extra", 'countdown', ('CODEREF' => 'Countdown',
188 'Module' => 'countdown', 'Identifier' => 'countdown',
189 'Cmdstats' => 'Countdown') );
190 &addCmdHook("extra", 'lart', ('CODEREF' => 'lart',
191 'Identifier' => 'lart', 'Help' => 'lart') );
192 &addCmdHook("extra", 'convert', ('CODEREF' => 'convert',
193 'Forker' => 1, 'Identifier' => 'units',
194 'Help' => 'convert') );
195 &addCmdHook("extra", '(cookie|random)', ('CODEREF' => 'cookie',
196 'Forker' => 1, 'Identifier' => 'factoids') );
197 &addCmdHook("extra", 'u(ser)?info', ('CODEREF' => 'userinfo',
198 'Identifier' => 'userinfo', 'Help' => 'userinfo',
199 'Module' => 'userinfo') );
200 &addCmdHook("extra", 'rootWarn', ('CODEREF' => 'CmdrootWarn',
201 'Identifier' => 'rootWarn', 'Module' => 'rootwarn') );
202 &addCmdHook("extra", 'seen', ('CODEREF' => 'seen', 'Identifier' =>
204 &addCmdHook("extra", 'dict', ('CODEREF' => 'Dict::Dict',
205 'Identifier' => 'dict', 'Help' => 'dict',
206 'Forker' => 1, 'Cmdstats' => 'Dict') );
207 &addCmdHook("extra", 'slashdot', ('CODEREF' => 'Slashdot::Slashdot',
208 'Identifier' => 'slashdot', 'Forker' => 1,
209 'Cmdstats' => 'Slashdot') );
210 &addCmdHook("extra", 'uptime', ('CODEREF' => 'uptime', 'Identifier' => 'uptime',
211 'Cmdstats' => 'Uptime') );
212 &addCmdHook("extra", 'nullski', ('CODEREF' => 'nullski', ) );
213 &addCmdHook("extra", '(fm|freshmeat)', ('CODEREF' => 'Freshmeat::Freshmeat',
214 'Identifier' => 'freshmeat', 'Cmdstats' => 'Freshmeat',
215 'Forker' => 1, 'Help' => 'freshmeat') );
216 &addCmdHook("extra", 'verstats', ('CODEREF' => 'do_verstats' ) );
219 ### END OF ADDING HOOKS.
221 &status("CMD: loaded ".scalar(keys %hooks_extra)." EXTRA command hooks.");
224 if (!defined $message) {
225 &WARN("Modules: message is undefined. should never happen.");
229 # babel bot: Jonathan Feinberg++
232 (?:babel(?:fish)?|x|xlate|translate)
234 (to|from) # direction of translation (through)
236 ($babel_lang_regex)\w* # which language?
238 (.+) # The phrase to be translated
240 return unless (&hasParam("babelfish"));
242 &Forker("babelfish", sub { &babel::babelfish(lc $1, lc $2, $3); } );
244 $cmdstats{'BabelFish'}++;
248 my $debiancmd = 'conflicts?|depends?|desc|file|info|provides?';
249 $debiancmd .= '|recommends?|suggests?|maint|maintainer';
251 if ($message =~ /^($debiancmd)(\s+(.*))?$/i) {
252 return unless (&hasParam("debian"));
255 if (defined $package) {
256 &Forker("debian", sub { &Debian::infoPackages($1, $package); } );
264 # google searching. Simon++
265 if ($message =~ /^(?:search\s+)?(\S+)\s+for\s+['"]?(.*?)['"]?\s*\?*$/i) {
266 return unless (&hasParam("wwwsearch"));
268 &Forker("wwwsearch", sub { &W3Search::W3Search($1,$2); } );
270 $cmdstats{'WWWSearch'}++;
275 # warn: lets process it anyway.
276 if (1 and $_ = &getChanConf("ircTextCounters")) {
278 my $z = join '|', split ' ';
280 if ($message =~ /^($z)stats(\s+(\S+))?$/i) {
284 if (!defined $arg or $arg =~ /^\s*$/) {
285 my $x = (&dbRawReturn("SELECT SUM(counter) FROM stats WHERE type=".&dbQuote($type) ))[0];
288 &pSReply("total count of '$type': $x");
290 &pSReply("zero counter for '$type'.");
293 my $x = (&dbRawReturn("SELECT SUM(counter) FROM stats WHERE type=".
294 &dbQuote($type)." AND nick=".&dbQuote($arg) ))[0];
296 if (defined $x) { # defined.
297 &pSReply("$arg has said $type $x times");
299 &pSReply("$arg has not said $type yet.");
307 &DEBUG("regex failed: $@");
312 # list{keys|values}. xk++. Idea taken from #linuxwarez@EFNET
313 if ($message =~ /^list(\S+)(\s+(.*))?$/i) {
314 return unless (&hasParam("search"));
319 $thiscmd =~ s/^vals$/values/;
320 # $args =~ s/\s+$//g;
321 return if ($thiscmd ne "keys" && $thiscmd ne "values");
324 if (!defined $args or $args =~ /^\s*$/) {
325 &help("list". $thiscmd);
329 # suggested by asuffield and \broken.
330 if ($args =~ /^["']/ and $args =~ /["']$/) {
331 &DEBUG("list*: removed quotes.");
332 $args =~ s/^["']|["']$//g;
335 if (length $args == 1) {
336 &msg($who,"search string is too short.");
340 &Forker("search", sub { &Search::Search($thiscmd, $args); } );
342 $cmdstats{'Factoid Search'}++;
346 # Nickometer. Adam Spiers++
347 if ($message =~ /^(?:lame|nick)ometer(?: for)? (\S+)/i) {
348 return unless (&hasParam("nickometer"));
350 my $term = (lc $1 eq 'me') ? $who : $1;
352 &loadMyModule($myModules{'nickometer'});
354 if ($term =~ /^$mask{chan}$/) {
355 &status("Doing nickometer for chan $term.");
357 if (!&validChan($term)) {
358 &msg($who, "error: channel is invalid.");
364 foreach (keys %{ $channels{lc $term}{''} }) {
367 &WARN("nickometer: nick in chan $term undefined?");
371 my $value = &nickometer($str);
372 $nickometer{$value}{$str} = 1;
376 ### TODO: compact with map?
378 foreach (sort {$b <=> $a} keys %nickometer) {
379 my $str = join(", ", sort keys %{ $nickometer{$_} });
380 push(@list, "$str ($_%)");
383 &pSReply( &formListReply(0, "Nickometer list for $term ", @list) );
389 my $percentage = &nickometer($term);
391 if ($percentage =~ /NaN/) {
392 $percentage = "off the scale";
394 $percentage = sprintf("%0.4f", $percentage);
395 $percentage =~ s/\.?0+$//;
399 if ($msgType eq 'public') {
400 &say("'$term' is $percentage lame, $who");
402 &msg($who, "the 'lame nick-o-meter' reading for $term is $percentage, $who");
408 # Topic management. xk++
409 # may want to add a flag(??) for topic in the near future. -xk
410 if ($message =~ /^topic(\s+(.*))?$/i) {
411 return unless (&hasParam("topic"));
413 my $chan = $talkchannel;
414 my @args = split / /, $2 || "";
417 &msg($who,"Try 'help topic'");
421 $chan = lc(shift @args) if ($msgType eq 'private');
422 my $thiscmd = shift @args;
425 if ($msgType eq 'public' && $thiscmd =~ /^#/) {
426 &msg($who, "error: channel argument is not required.");
427 &msg($who, "\002Usage\002: topic <CMD>");
431 # topic over private:
432 if ($msgType eq 'private' && $chan !~ /^#/) {
433 &msg($who, "error: channel argument is required.");
434 &msg($who, "\002Usage\002: topic #channel <CMD>");
438 if (&validChan($chan) == 0) {
439 &msg($who,"error: invalid channel \002$chan\002");
443 # for semi-outsiders.
444 if (!&IsNickInChan($who,$chan)) {
445 &msg($who, "Failed. You ($who) are not in $chan, hey?");
450 &loadMyModule($myModules{'topic'});
451 &Topic($chan, $thiscmd, join(' ', @args));
452 $cmdstats{'Topic'}++;
457 if ($message =~ /^wingate$/i) {
458 return unless (&hasParam("wingate"));
460 my $reply = "Wingate statistics: scanned \002"
461 .scalar(keys %wingate)."\002 hosts";
462 my $queue = scalar(keys %wingateToDo);
464 $reply .= ". I have \002$queue\002 hosts in the queue";
465 $reply .= ". Started the scan ".&Time2String(time() - $wingaterun)." ago";
468 &performStrictReply("$reply.");
473 # do nothing and let the other routines have a go
481 if (!defined $query) {
483 &msg($who, "I have \002".&countKeys("freshmeat")."\002 entries.");
487 &Freshmeat::Freshmeat($query);
493 &msg($who, "- Uptime for $ident -");
494 &msg($who, "Now: ". &Time2String(&uptimeNow()) ." running $bot_version");
496 foreach (&uptimeGetInfo()) {
498 my $time = &Time2String($1);
501 &msg($who, "$count: $time $2");
510 if (!defined $person or $person =~ /^$/) {
513 my $i = &countKeys("seen");
514 &msg($who,"there ". &fixPlural("is",$i) ." \002$i\002 ".
515 "seen ". &fixPlural("entry",$i) ." that I know of.");
523 &seenFlush(); # very evil hack. oh well, better safe than sorry.
525 ### TODO: Support &dbGetColInfo(); like in &FactInfo();
526 my $select = "nick,time,channel,host,message";
527 if ($person eq "random") {
528 @seen = &randKey("seen", $select);
530 @seen = &dbGet("seen", $select, "nick='$person'");
533 if (scalar @seen < 2) {
535 &DEBUG("seen: _ => '$_'.");
537 &performReply("i haven't seen '$person'");
543 ### TODO: multi channel support. may require &IsNick() to return
544 ### all channels or something.
545 my @chans = &getNickInChans($seen[0]);
547 $reply = "$seen[0] is currently on";
551 next unless (exists $userstats{lc $seen[0]}{'Join'});
552 $reply .= " (".&Time2String(time() - $userstats{lc $seen[0]}{'Join'}).")";
555 if (&IsParam("seenStats")) {
557 $i = $userstats{lc $seen[0]}{'Count'};
558 $reply .= ". Has said a total of \002$i\002 messages" if (defined $i);
559 $i = $userstats{lc $seen[0]}{'Time'};
560 $reply .= ". Is idling for ".&Time2String(time() - $i) if (defined $i);
563 my $howlong = &Time2String(time() - $seen[1]);
564 $reply = "$seen[0] <$seen[3]> was last seen on IRC ".
565 "in channel $seen[2], $howlong ago, ".
566 "saying\002:\002 '$seen[4]'.";
569 &performStrictReply($reply);
573 # User Information Services. requested by Flugh.
575 my ($arg) = join(' ',@_);
577 if ($arg =~ /^set(\s+(.*))?$/i) {
580 &help("userinfo set");
584 &UserInfoSet(split /\s+/, $arg, 2);
585 } elsif ($arg =~ /^unset(\s+(.*))?$/i) {
588 &help("userinfo unset");
592 &UserInfoSet($arg, "");
598 # cookie (random). xk++
602 # lets find that secret cookie.
603 my $target = ($msgType ne 'public') ? $who : $talkchannel;
604 my $cookiemsg = &getRandom(keys %{ $lang{'cookie'} });
607 ### WILL CHEW TONS OF MEM.
608 ### TODO: convert this to a Forker function!
610 my @list = &searchTable("factoids", "factoid_key", "factoid_value", $arg);
611 $key = &getRandom(@list);
612 $val = &getFactInfo("factoids", $key, "factoid_value");
614 ($key,$value) = &randKey("factoids","factoid_key,factoid_value");
618 s/##KEY/\002$key\002/;
621 s/\$who/$who/; # cheap fix.
622 s/(\S+)?\s*<\S+>/$1 /;
626 if ($cookiemsg =~ s/^ACTION //i) {
627 &action($target, $cookiemsg);
629 &msg($target, $cookiemsg);
634 my $arg = join(' ',@_);
635 my ($from,$to) = ('','');
637 ($from,$to) = ($1,$2) if ($arg =~ /^(.*?) to (.*)$/i);
638 ($from,$to) = ($2,$1) if ($arg =~ /^(.*?) from (.*)$/i);
640 if (!$to or !$from) {
641 &msg($who, "Invalid format!");
646 &Units::convertUnits($from, $to);
652 my ($target) = &fixString($_[0]);
654 my $chan = $talkchannel;
656 if ($msgType eq 'private') {
657 if ($target =~ /^($mask{chan})\s+(.*)$/) {
662 &msg($who, "error: invalid format or missing arguments.");
668 my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.lart");
670 if ($target =~ /^(me|you|itself|\Q$ident\E)$/i) {
671 $line =~ s/WHO/$who/g;
673 $line =~ s/WHO/$target/g;
675 $line .= ", courtesy of $who" if ($extra);
677 &action($chan, $line);
679 &status("lart: error reading file?");
684 my $idx = "debian/Packages-woody.idx";
689 $error++ unless ( -e $idx);
690 $error++ unless ( -e "$idx-old");
693 $error = "no woody/woody-old index file found.";
694 &ERROR("Debian: $error");
700 open(IDX2, "$idx-old");
714 next if (exists $pkg{$_});
720 &::performStrictReply( &::formListReply(0, "New debian packages:", @new) );
726 if (!defined $chan) {
731 if (!&validChan($chan)) {
732 &msg($who, "chan $chan is invalid.");
736 if (scalar @vernick > scalar(keys %{ $channels{lc $chan}{''} })/4) {
737 &msg($who, "verstats already in progress for someone else.");
741 &msg($who, "Sending CTCP VERSION...");
742 $conn->ctcp("VERSION", $chan);
743 $cache{verstats}{chan} = $chan;
744 $cache{verstats}{who} = $who;
745 $cache{verstats}{msgType} = $msgType;
747 $conn->schedule(60, sub {
749 my $c = lc $cache{verstats}{chan};
750 my $total = keys %{ $channels{$c}{''} };
752 $who = $cache{verstats}{who};
753 $msgType = $cache{verstats}{msgType};
754 delete $cache{verstats}; # sufficient?
756 foreach (keys %ver) {
757 $vtotal += scalar keys %{ $ver{$_} };
761 my $unknown = $total - $vtotal;
762 my $perc = sprintf("%.1f", $unknown * 100 / $total);
764 $sorted{$perc}{"unknown/cloak"} = "$unknown ($perc%)";
766 foreach (keys %ver) {
767 my $count = scalar keys %{ $ver{$_} };
768 $perc = sprintf("%.01f", $count * 100 / $total);
769 $perc =~ s/.0$//; # lame compression.
771 $sorted{$perc}{$_} = "$count ($perc%)";
774 ### can be compressed to a map?
776 foreach ( sort { $b <=> $a } keys %sorted ) {
778 foreach (sort keys %{ $sorted{$perc} }) {
779 push(@list, "$_ - $sorted{$perc}{$_}");
783 &pSReply( &formListReply(0, "IRC Client versions for $c ", @list) );
785 # clean up not-needed data structures.
793 sub nullski { my ($arg) = @_; return unless (defined $arg);
794 foreach (`$arg`) { &msg($who,$_); } }