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!
55 next unless ($cmd =~ /^$ident$/i);
58 &WARN("pCH: Multiple hook match: $ident");
62 &status("hooks($hashname): $cmd matched '$ident'");
63 my %hash = %{ ${"hooks_$hashname"}{$ident} };
65 if (!scalar keys %hash) {
66 &WARN("CmdHook: hash is NULL?");
70 if ($hash{NoArgs} and $flatarg) {
71 &DEBUG("cmd $ident does not take args; skipping.");
75 if (!exists $hash{CODEREF}) {
76 &ERROR("CODEREF undefined for $cmd or $ident.");
81 foreach (keys %hash) {
82 &VERB(" $cmd->$_ => '$hash{$_}'.",2);
86 if (exists $hash{'Help'} and !scalar(@args)) {
87 &help( $hash{'Help'} );
92 if (exists $hash{'Identifier'}) {
93 return 1 unless (&hasParam($hash{'Identifier'}));
97 if (exists $hash{'UserFlag'}) {
98 return 1 unless (&hasFlag($hash{'UserFlag'}));
101 ### FORKER,IDENTIFIER,CODEREF.
102 if (exists $hash{'Forker'}) {
103 $hash{'Identifier'} .= "-" if ($hash{'Forker'} eq "NULL");
105 if (exists $hash{'ArrayArgs'}) {
106 &Forker($hash{'Identifier'}, sub { \&{ $hash{'CODEREF'} }(@args) } );
108 &Forker($hash{'Identifier'}, sub { \&{ $hash{'CODEREF'} }($flatarg) } );
112 if (exists $hash{'Module'}) {
113 &loadMyModule($myModules{ $hash{'Module'} });
116 # check if CODEREF exists.
117 if (!defined &{ $hash{'CODEREF'} }) {
118 &WARN("coderef $hash{'CODEREF'} don't exist.");
120 &msg($who, "coderef does not exist for $ident.");
126 if (exists $hash{'ArrayArgs'}) {
127 &{ $hash{'CODEREF'} }(@args);
129 &{ $hash{'CODEREF'} }($flatarg);
134 if (exists $hash{'Cmdstats'}) {
135 $cmdstats{ $hash{'Cmdstats'} }++;
138 &VERB("hooks: End of command.",2);
148 ### START ADDING HOOKS.
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' =>
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' ) );
215 ### END OF ADDING HOOKS.
217 &status("CMD: loaded ".scalar(keys %hooks_extra)." EXTRA command hooks.");
220 if (!defined $message) {
221 &WARN("Modules: message is undefined. should never happen.");
225 # babel bot: Jonathan Feinberg++
228 (?:babel(?:fish)?|x|xlate|translate)
230 (to|from) # direction of translation (through)
232 ($babel_lang_regex)\w* # which language?
234 (.+) # The phrase to be translated
236 return unless (&hasParam("babelfish"));
238 &Forker("babelfish", sub { &babel::babelfish(lc $1, lc $2, $3); } );
240 $cmdstats{'BabelFish'}++;
244 my $debiancmd = 'conflicts?|depends?|desc|file|info|provides?';
245 $debiancmd .= '|recommends?|suggests?|maint|maintainer';
247 if ($message =~ /^($debiancmd)(\s+(.*))?$/i) {
248 return unless (&hasParam("debian"));
251 if (defined $package) {
252 &Forker("debian", sub { &Debian::infoPackages($1, $package); } );
260 # google searching. Simon++
261 if ($message =~ /^(?:search\s+)?(\S+)\s+for\s+['"]?(.*?)['"]?\s*\?*$/i) {
262 return unless (&hasParam("wwwsearch"));
264 &Forker("wwwsearch", sub { &W3Search::W3Search($1,$2); } );
266 $cmdstats{'WWWSearch'}++;
271 if ($_ = &getChanConf("ircTextCounters")) {
273 my $z = join '|', split ' ';
275 if ($message =~ /^($z)stats(\s+(\S+))?$/i) {
279 if (!defined $arg or $arg =~ /^\s*$/) {
280 my $x = (&dbRawReturn("SELECT SUM(counter) FROM stats WHERE type=".&dbQuote($type) ))[0];
283 &pSReply("total count of '$type': $x");
285 &pSReply("zero counter for '$type'.");
288 my $x = (&dbRawReturn("SELECT SUM(counter) FROM stats WHERE type=".
289 &dbQuote($type)." AND nick=".&dbQuote($arg) ))[0];
291 if (defined $x) { # defined.
292 &pSReply("$arg has said $type $x times");
294 &pSReply("$arg has not said $type yet.");
302 &DEBUG("regex failed: $@");
307 # list{keys|values}. xk++. Idea taken from #linuxwarez@EFNET
308 if ($message =~ /^list(\S+)( (.*))?$/i) {
309 return unless (&hasParam("search"));
311 my $thiscmd = lc($1);
312 $thiscmd =~ s/^vals$/values/;
313 return if ($thiscmd ne "keys" && $thiscmd ne "values");
319 if (!defined $args or $args =~ /^\s*$/) {
320 &help("list". $thiscmd);
324 # suggested by asuffield and \broken.
325 if ($args =~ /^["']/ and $args =~ /["']$/) {
326 &DEBUG("list*: removed quotes.");
327 $args =~ s/^["']|["']$//g;
330 if (length $args == 1) {
331 &msg($who,"search string is too short.");
335 &Forker("search", sub { &Search::Search($thiscmd, $args); } );
337 $cmdstats{'Factoid Search'}++;
341 # Nickometer. Adam Spiers++
342 if ($message =~ /^(?:lame|nick)ometer(?: for)? (\S+)/i) {
343 return unless (&hasParam("nickometer"));
345 my $term = (lc $1 eq 'me') ? $who : $1;
347 &loadMyModule($myModules{'nickometer'});
349 if ($term =~ /^$mask{chan}$/) {
350 &status("Doing nickometer for chan $term.");
352 if (!&validChan($term)) {
353 &msg($who, "error: channel is invalid.");
359 foreach (keys %{ $channels{lc $term}{''} }) {
362 &WARN("nickometer: nick in chan $term undefined?");
366 my $value = &nickometer($str);
367 $nickometer{$value}{$str} = 1;
371 ### TODO: compact with map?
373 foreach (sort {$b <=> $a} keys %nickometer) {
374 my $str = join(", ", sort keys %{ $nickometer{$_} });
375 push(@list, "$str ($_%)");
378 &pSReply( &formListReply(0, "Nickometer list for $term ", @list) );
384 my $percentage = &nickometer($term);
386 if ($percentage =~ /NaN/) {
387 $percentage = "off the scale";
389 $percentage = sprintf("%0.4f", $percentage);
390 $percentage =~ s/\.?0+$//;
394 if ($msgType eq 'public') {
395 &say("'$term' is $percentage lame, $who");
397 &msg($who, "the 'lame nick-o-meter' reading for $term is $percentage, $who");
403 # Topic management. xk++
404 # may want to add a flag(??) for topic in the near future. -xk
405 if ($message =~ /^topic(\s+(.*))?$/i) {
406 return unless (&hasParam("topic"));
408 my $chan = $talkchannel;
409 my @args = split / /, $2 || "";
412 &msg($who,"Try 'help topic'");
416 $chan = lc(shift @args) if ($msgType eq 'private');
417 my $thiscmd = shift @args;
420 if ($msgType eq 'public' && $thiscmd =~ /^#/) {
421 &msg($who, "error: channel argument is not required.");
422 &msg($who, "\002Usage\002: topic <CMD>");
426 # topic over private:
427 if ($msgType eq 'private' && $chan !~ /^#/) {
428 &msg($who, "error: channel argument is required.");
429 &msg($who, "\002Usage\002: topic #channel <CMD>");
433 if (&validChan($chan) == 0) {
434 &msg($who,"error: invalid channel \002$chan\002");
438 # for semi-outsiders.
439 if (!&IsNickInChan($who,$chan)) {
440 &msg($who, "Failed. You ($who) are not in $chan, hey?");
445 &loadMyModule($myModules{'topic'});
446 &Topic($chan, $thiscmd, join(' ', @args));
447 $cmdstats{'Topic'}++;
452 if ($message =~ /^wingate$/i) {
453 return unless (&hasParam("wingate"));
455 my $reply = "Wingate statistics: scanned \002"
456 .scalar(keys %wingate)."\002 hosts";
457 my $queue = scalar(keys %wingateToDo);
459 $reply .= ". I have \002$queue\002 hosts in the queue";
460 $reply .= ". Started the scan ".&Time2String(time() - $wingaterun)." ago";
463 &performStrictReply("$reply.");
468 # do nothing and let the other routines have a go
476 if (!defined $query) {
478 &msg($who, "I have \002".&countKeys("freshmeat")."\002 entries.");
482 &Freshmeat::Freshmeat($query);
488 &msg($who, "- Uptime for $ident -");
489 &msg($who, "Now: ". &Time2String(&uptimeNow()) ." running $bot_version");
491 foreach (&uptimeGetInfo()) {
493 my $time = &Time2String($1);
496 &msg($who, "$count: $time $2");
505 if (!defined $person or $person =~ /^$/) {
508 my $i = &countKeys("seen");
509 &msg($who,"there ". &fixPlural("is",$i) ." \002$i\002 ".
510 "seen ". &fixPlural("entry",$i) ." that I know of.");
518 &seenFlush(); # very evil hack. oh well, better safe than sorry.
520 ### TODO: Support &dbGetColInfo(); like in &FactInfo();
521 my $select = "nick,time,channel,host,message";
522 if ($person eq "random") {
523 @seen = &randKey("seen", $select);
525 @seen = &dbGet("seen", $select, "nick='$person'");
528 if (scalar @seen < 2) {
530 &DEBUG("seen: _ => '$_'.");
532 &performReply("i haven't seen '$person'");
538 ### TODO: multi channel support. may require &IsNick() to return
539 ### all channels or something.
540 my @chans = &getNickInChans($seen[0]);
542 $reply = "$seen[0] is currently on";
546 next unless (exists $userstats{lc $seen[0]}{'Join'});
547 $reply .= " (".&Time2String(time() - $userstats{lc $seen[0]}{'Join'}).")";
550 if (&IsParam("seenStats")) {
552 $i = $userstats{lc $seen[0]}{'Count'};
553 $reply .= ". Has said a total of \002$i\002 messages" if (defined $i);
554 $i = $userstats{lc $seen[0]}{'Time'};
555 $reply .= ". Is idling for ".&Time2String(time() - $i) if (defined $i);
558 my $howlong = &Time2String(time() - $seen[1]);
559 $reply = "$seen[0] <$seen[3]> was last seen on IRC ".
560 "in channel $seen[2], $howlong ago, ".
561 "saying\002:\002 '$seen[4]'.";
564 &performStrictReply($reply);
568 # User Information Services. requested by Flugh.
570 my ($arg) = join(' ',@_);
572 if ($arg =~ /^set(\s+(.*))?$/i) {
575 &help("userinfo set");
579 &UserInfoSet(split /\s+/, $arg, 2);
580 } elsif ($arg =~ /^unset(\s+(.*))?$/i) {
583 &help("userinfo unset");
587 &UserInfoSet($arg, "");
593 # cookie (random). xk++
597 # lets find that secret cookie.
598 my $target = ($msgType ne 'public') ? $who : $talkchannel;
599 my $cookiemsg = &getRandom(keys %{ $lang{'cookie'} });
602 ### WILL CHEW TONS OF MEM.
603 ### TODO: convert this to a Forker function!
605 my @list = &searchTable("factoids", "factoid_key", "factoid_value", $arg);
606 $key = &getRandom(@list);
607 $val = &getFactInfo("factoids", $key, "factoid_value");
609 ($key,$value) = &randKey("factoids","factoid_key,factoid_value");
613 s/##KEY/\002$key\002/;
616 s/\$who/$who/; # cheap fix.
617 s/(\S+)?\s*<\S+>/$1 /;
621 if ($cookiemsg =~ s/^ACTION //i) {
622 &action($target, $cookiemsg);
624 &msg($target, $cookiemsg);
629 my $arg = join(' ',@_);
630 my ($from,$to) = ('','');
632 ($from,$to) = ($1,$2) if ($arg =~ /^(.*?) to (.*)$/i);
633 ($from,$to) = ($2,$1) if ($arg =~ /^(.*?) from (.*)$/i);
635 if (!$to or !$from) {
636 &msg($who, "Invalid format!");
641 &Units::convertUnits($from, $to);
647 my ($target) = &fixString($_[0]);
649 my $chan = $talkchannel;
651 if ($msgType eq 'private') {
652 if ($target =~ /^($mask{chan})\s+(.*)$/) {
657 &msg($who, "error: invalid format or missing arguments.");
663 my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.lart");
665 if ($target =~ /^(me|you|itself|\Q$ident\E)$/i) {
666 $line =~ s/WHO/$who/g;
668 $line =~ s/WHO/$target/g;
670 $line .= ", courtesy of $who" if ($extra);
672 &action($chan, $line);
674 &status("lart: error reading file?");
679 my $idx = "debian/Packages-woody.idx";
684 $error++ unless ( -e $idx);
685 $error++ unless ( -e "$idx-old");
688 $error = "no woody/woody-old index file found.";
689 &ERROR("Debian: $error");
695 open(IDX2, "$idx-old");
709 next if (exists $pkg{$_});
715 &::performStrictReply( &::formListReply(0, "New debian packages:", @new) );
721 if (!defined $chan) {
726 if (!&validChan($chan)) {
727 &msg($who, "chan $chan is invalid.");
731 if (scalar @vernick > scalar(keys %{ $channels{lc $chan}{''} })/4) {
732 &msg($who, "verstats already in progress for someone else.");
736 &msg($who, "Sending CTCP VERSION...");
737 $conn->ctcp("VERSION", $chan);
738 $cache{verstats}{chan} = $chan;
739 $cache{verstats}{who} = $who;
740 $cache{verstats}{msgType} = $msgType;
742 $conn->schedule(60, sub {
744 my $c = lc $cache{verstats}{chan};
745 my $total = keys %{ $channels{$c}{''} };
747 $who = $cache{verstats}{who};
748 $msgType = $cache{verstats}{msgType};
749 delete $cache{verstats}; # sufficient?
751 foreach (keys %ver) {
752 $vtotal += scalar keys %{ $ver{$_} };
756 my $unknown = $total - $vtotal;
757 my $perc = sprintf("%.1f", $unknown * 100 / $total);
759 $sorted{$perc}{"unknown/cloak"} = "$unknown ($perc%)";
761 foreach (keys %ver) {
762 my $count = scalar keys %{ $ver{$_} };
763 $perc = sprintf("%.01f", $count * 100 / $total);
764 $perc =~ s/.0$//; # lame compression.
766 $sorted{$perc}{$_} = "$count ($perc%)";
769 ### can be compressed to a map?
771 foreach ( sort { $b <=> $a } keys %sorted ) {
773 foreach (sort keys %{ $sorted{$perc} }) {
774 push(@list, "$_ - $sorted{$perc}{$_}");
778 &pSReply( &formListReply(0, "IRC Client versions for $c ", @list) );
780 # clean up not-needed data structures.
788 sub nullski { my ($arg) = @_; return unless (defined $arg);
789 foreach (`$arg`) { &msg($who,$_); } }