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'}++;
270 # list{keys|values}. xk++. Idea taken from #linuxwarez@EFNET
271 if ($message =~ /^list(\S+)( (.*))?$/i) {
272 return unless (&hasParam("search"));
274 my $thiscmd = lc($1);
277 # suggested by asuffield nad \broken.
278 if ($args =~ /^["']/ and $args =~ /["']$/) {
279 &DEBUG("list*: removed quotes.");
280 $args =~ s/^["']|["']$//g;
283 $thiscmd =~ s/^vals$/values/;
284 return if ($thiscmd ne "keys" && $thiscmd ne "values");
287 if (!defined $args) {
288 &help("list". $thiscmd);
292 if (length $args == 1) {
293 &msg($who,"search string is too short.");
297 &Forker("search", sub { &Search::Search($thiscmd, $args); } );
299 $cmdstats{'Factoid Search'}++;
303 # Nickometer. Adam Spiers++
304 if ($message =~ /^(?:lame|nick)ometer(?: for)? (\S+)/i) {
305 return unless (&hasParam("nickometer"));
307 my $term = (lc $1 eq 'me') ? $who : $1;
309 &loadMyModule($myModules{'nickometer'});
311 if ($term =~ /^$mask{chan}$/) {
312 &status("Doing nickometer for chan $term.");
314 if (!&validChan($term)) {
315 &msg($who, "error: channel is invalid.");
321 foreach (keys %{ $channels{lc $term}{''} }) {
324 &WARN("nickometer: nick in chan $term undefined?");
328 my $value = &nickometer($str);
329 $nickometer{$value}{$str} = 1;
333 ### TODO: compact with map?
335 foreach (sort {$b <=> $a} keys %nickometer) {
336 my $str = join(", ", sort keys %{ $nickometer{$_} });
337 push(@list, "$str ($_%)");
340 &pSReply( &formListReply(0, "Nickometer list for $term ", @list) );
346 my $percentage = &nickometer($term);
348 if ($percentage =~ /NaN/) {
349 $percentage = "off the scale";
351 $percentage = sprintf("%0.4f", $percentage);
352 $percentage =~ s/\.?0+$//;
356 if ($msgType eq 'public') {
357 &say("'$term' is $percentage lame, $who");
359 &msg($who, "the 'lame nick-o-meter' reading for $term is $percentage, $who");
365 # Topic management. xk++
366 # may want to add a flag(??) for topic in the near future. -xk
367 if ($message =~ /^topic(\s+(.*))?$/i) {
368 return unless (&hasParam("topic"));
370 my $chan = $talkchannel;
371 my @args = split / /, $2 || "";
374 &msg($who,"Try 'help topic'");
378 $chan = lc(shift @args) if ($msgType eq 'private');
379 my $thiscmd = shift @args;
382 if ($msgType eq 'public' && $thiscmd =~ /^#/) {
383 &msg($who, "error: channel argument is not required.");
384 &msg($who, "\002Usage\002: topic <CMD>");
388 # topic over private:
389 if ($msgType eq 'private' && $chan !~ /^#/) {
390 &msg($who, "error: channel argument is required.");
391 &msg($who, "\002Usage\002: topic #channel <CMD>");
395 if (&validChan($chan) == 0) {
396 &msg($who,"error: invalid channel \002$chan\002");
400 # for semi-outsiders.
401 if (!&IsNickInChan($who,$chan)) {
402 &msg($who, "Failed. You ($who) are not in $chan, hey?");
407 &loadMyModule($myModules{'topic'});
408 &Topic($chan, $thiscmd, join(' ', @args));
409 $cmdstats{'Topic'}++;
414 if ($message =~ /^wingate$/i) {
415 return unless (&hasParam("wingate"));
417 my $reply = "Wingate statistics: scanned \002"
418 .scalar(keys %wingate)."\002 hosts";
419 my $queue = scalar(keys %wingateToDo);
421 $reply .= ". I have \002$queue\002 hosts in the queue";
422 $reply .= ". Started the scan ".&Time2String(time() - $wingaterun)." ago";
425 &performStrictReply("$reply.");
430 # do nothing and let the other routines have a go
438 if (!defined $query) {
440 &msg($who, "I have \002".&countKeys("freshmeat")."\002 entries.");
444 &Freshmeat::Freshmeat($query);
450 &msg($who, "- Uptime for $ident -");
451 &msg($who, "Now: ". &Time2String(&uptimeNow()) ." running $bot_version");
453 foreach (&uptimeGetInfo()) {
455 my $time = &Time2String($1);
458 &msg($who, "$count: $time $2");
467 if (!defined $person or $person =~ /^$/) {
470 my $i = &countKeys("seen");
471 &msg($who,"there ". &fixPlural("is",$i) ." \002$i\002 ".
472 "seen ". &fixPlural("entry",$i) ." that I know of.");
480 &seenFlush(); # very evil hack. oh well, better safe than sorry.
482 ### TODO: Support &dbGetColInfo(); like in &FactInfo();
483 my $select = "nick,time,channel,host,message";
484 if ($person eq "random") {
485 @seen = &randKey("seen", $select);
487 @seen = &dbGet("seen", "nick", $person, $select);
490 if (scalar @seen < 2) {
492 &DEBUG("seen: _ => '$_'.");
494 &performReply("i haven't seen '$person'");
500 ### TODO: multi channel support. may require &IsNick() to return
501 ### all channels or something.
502 my @chans = &getNickInChans($seen[0]);
504 $reply = "$seen[0] is currently on";
508 next unless (exists $userstats{lc $seen[0]}{'Join'});
509 $reply .= " (".&Time2String(time() - $userstats{lc $seen[0]}{'Join'}).")";
512 if (&IsParam("seenStats")) {
514 $i = $userstats{lc $seen[0]}{'Count'};
515 $reply .= ". Has said a total of \002$i\002 messages" if (defined $i);
516 $i = $userstats{lc $seen[0]}{'Time'};
517 $reply .= ". Is idling for ".&Time2String(time() - $i) if (defined $i);
520 my $howlong = &Time2String(time() - $seen[1]);
521 $reply = "$seen[0] <$seen[3]> was last seen on IRC ".
522 "in channel $seen[2], $howlong ago, ".
523 "saying\002:\002 '$seen[4]'.";
526 &performStrictReply($reply);
530 # User Information Services. requested by Flugh.
532 my ($arg) = join(' ',@_);
534 if ($arg =~ /^set(\s+(.*))?$/i) {
537 &help("userinfo set");
541 &UserInfoSet(split /\s+/, $arg, 2);
542 } elsif ($arg =~ /^unset(\s+(.*))?$/i) {
545 &help("userinfo unset");
549 &UserInfoSet($arg, "");
555 # cookie (random). xk++
559 # lets find that secret cookie.
560 my $target = ($msgType ne 'public') ? $who : $talkchannel;
561 my $cookiemsg = &getRandom(keys %{ $lang{'cookie'} });
564 ### WILL CHEW TONS OF MEM.
565 ### TODO: convert this to a Forker function!
567 my @list = &searchTable("factoids", "factoid_key", "factoid_value", $arg);
568 $key = &getRandom(@list);
569 $val = &getFactInfo("factoids", $key, "factoid_value");
571 ($key,$value) = &randKey("factoids","factoid_key,factoid_value");
575 s/##KEY/\002$key\002/;
578 s/\$who/$who/; # cheap fix.
579 s/(\S+)?\s*<\S+>/$1 /;
583 if ($cookiemsg =~ s/^ACTION //i) {
584 &action($target, $cookiemsg);
586 &msg($target, $cookiemsg);
591 my $arg = join(' ',@_);
592 my ($from,$to) = ('','');
594 ($from,$to) = ($1,$2) if ($arg =~ /^(.*?) to (.*)$/i);
595 ($from,$to) = ($2,$1) if ($arg =~ /^(.*?) from (.*)$/i);
597 if (!$to or !$from) {
598 &msg($who, "Invalid format!");
603 &Units::convertUnits($from, $to);
609 my ($target) = &fixString($_[0]);
611 my $chan = $talkchannel;
613 if ($msgType eq 'private') {
614 if ($target =~ /^($mask{chan})\s+(.*)$/) {
619 &msg($who, "error: invalid format or missing arguments.");
625 my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.lart");
627 if ($target =~ /^(me|you|itself|\Q$ident\E)$/i) {
628 $line =~ s/WHO/$who/g;
630 $line =~ s/WHO/$target/g;
632 $line .= ", courtesy of $who" if ($extra);
634 &action($chan, $line);
636 &status("lart: error reading file?");
641 my $idx = "debian/Packages-woody.idx";
646 $error++ unless ( -e $idx);
647 $error++ unless ( -e "$idx-old");
650 $error = "no woody/woody-old index file found.";
651 &ERROR("Debian: $error");
657 open(IDX2, "$idx-old");
671 next if (exists $pkg{$_});
677 &::performStrictReply( &::formListReply(0, "New debian packages:", @new) );
683 if (!defined $chan) {
688 if (!&validChan($chan)) {
689 &msg($who, "chan $chan is invalid.");
693 if (scalar @vernick > scalar(keys %{ $channels{lc $chan}{''} })/4) {
694 &msg($who, "verstats already in progress for someone else.");
698 &msg($who, "Sending CTCP VERSION...");
699 $conn->ctcp("VERSION", $chan);
700 $cache{verstats}{chan} = $chan;
701 $cache{verstats}{who} = $who;
702 $cache{verstats}{msgType} = $msgType;
704 $conn->schedule(60, sub {
706 my $c = lc $cache{verstats}{chan};
707 my $total = keys %{ $channels{$c}{''} };
709 $who = $cache{verstats}{who};
710 $msgType = $cache{verstats}{msgType};
711 delete $cache{verstats}; # sufficient?
713 foreach (keys %ver) {
714 $vtotal += scalar keys %{ $ver{$_} };
718 my $unknown = $total - $vtotal;
719 my $perc = sprintf("%.1f", $unknown * 100 / $total);
721 $sorted{$perc}{"unknown/cloak"} = "$unknown ($perc%)";
723 foreach (keys %ver) {
724 my $count = scalar keys %{ $ver{$_} };
725 $perc = sprintf("%.01f", $count * 100 / $total);
726 $perc =~ s/.0$//; # lame compression.
728 $sorted{$perc}{$_} = "$count ($perc%)";
731 ### can be compressed to a map?
733 foreach ( sort { $b <=> $a } keys %sorted ) {
735 foreach (sort keys %{ $sorted{$perc} }) {
736 push(@list, "$_ - $sorted{$perc}{$_}");
740 &pSReply( &formListReply(0, "IRC Client versions for $c ", @list) );
742 # clean up not-needed data structures.
750 sub nullski { my ($arg) = @_; return unless (defined $arg);
751 foreach (`$arg`) { &msg($who,$_); } }