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 # this is way fucking ugly.
286 my $x = (&dbRawReturn("SELECT SUM(counter) FROM stats WHERE type=".&dbQuote($type) ))[0];
287 my %hash = &dbGetCol("stats", "nick,counter", "type=".&dbQuote($type).
288 " ORDER BY counter DESC LIMIT 3", 1);
292 # unfortunately we have to sort it again!
293 # todo: make dbGetCol return hash and array? too much effort.
294 foreach $i (sort { $b <=> $a } keys %hash) {
295 foreach (keys %{ $hash{$i} }) {
296 push(@top, "$_ - $i");
301 $topstr = ". Top ".scalar(@top).": ".join(', ', @top);
305 &pSReply("total count of '$type': $x$topstr");
307 &pSReply("zero counter for '$type'.");
310 my $x = (&dbRawReturn("SELECT SUM(counter) FROM stats WHERE type=".
311 &dbQuote($type)." AND nick=".&dbQuote($arg) ))[0];
313 if (defined $x) { # defined.
314 &pSReply("$arg has said $type $x times");
316 &pSReply("$arg has not said $type yet.");
324 &DEBUG("regex failed: $@");
329 # list{keys|values}. xk++. Idea taken from #linuxwarez@EFNET
330 if ($message =~ /^list(\S+)(\s+(.*))?$/i) {
331 return unless (&hasParam("search"));
336 $thiscmd =~ s/^vals$/values/;
337 # $args =~ s/\s+$//g;
338 return if ($thiscmd ne "keys" && $thiscmd ne "values");
341 if (!defined $args or $args =~ /^\s*$/) {
342 &help("list". $thiscmd);
346 # suggested by asuffield and \broken.
347 if ($args =~ /^["']/ and $args =~ /["']$/) {
348 &DEBUG("list*: removed quotes.");
349 $args =~ s/^["']|["']$//g;
352 if (length $args == 1) {
353 &msg($who,"search string is too short.");
357 &Forker("search", sub { &Search::Search($thiscmd, $args); } );
359 $cmdstats{'Factoid Search'}++;
363 # Nickometer. Adam Spiers++
364 if ($message =~ /^(?:lame|nick)ometer(?: for)? (\S+)/i) {
365 return unless (&hasParam("nickometer"));
367 my $term = (lc $1 eq 'me') ? $who : $1;
369 &loadMyModule($myModules{'nickometer'});
371 if ($term =~ /^$mask{chan}$/) {
372 &status("Doing nickometer for chan $term.");
374 if (!&validChan($term)) {
375 &msg($who, "error: channel is invalid.");
381 foreach (keys %{ $channels{lc $term}{''} }) {
384 &WARN("nickometer: nick in chan $term undefined?");
388 my $value = &nickometer($str);
389 $nickometer{$value}{$str} = 1;
393 ### TODO: compact with map?
395 foreach (sort {$b <=> $a} keys %nickometer) {
396 my $str = join(", ", sort keys %{ $nickometer{$_} });
397 push(@list, "$str ($_%)");
400 &pSReply( &formListReply(0, "Nickometer list for $term ", @list) );
406 my $percentage = &nickometer($term);
408 if ($percentage =~ /NaN/) {
409 $percentage = "off the scale";
411 $percentage = sprintf("%0.4f", $percentage);
412 $percentage =~ s/\.?0+$//;
416 if ($msgType eq 'public') {
417 &say("'$term' is $percentage lame, $who");
419 &msg($who, "the 'lame nick-o-meter' reading for $term is $percentage, $who");
425 # Topic management. xk++
426 # may want to add a userflags for topic. -xk
427 if ($message =~ /^topic(\s+(.*))?$/i) {
428 return unless (&hasParam("topic"));
430 my $chan = $talkchannel;
431 my @args = split / /, $2 || "";
434 &msg($who,"Try 'help topic'");
438 $chan = lc(shift @args) if ($msgType eq 'private');
439 my $thiscmd = shift @args;
442 if ($msgType eq 'public' && $thiscmd =~ /^#/) {
443 &msg($who, "error: channel argument is not required.");
444 &msg($who, "\002Usage\002: topic <CMD>");
448 # topic over private:
449 if ($msgType eq 'private' && $chan !~ /^#/) {
450 &msg($who, "error: channel argument is required.");
451 &msg($who, "\002Usage\002: topic #channel <CMD>");
455 if (&validChan($chan) == 0) {
456 &msg($who,"error: invalid channel \002$chan\002");
460 # for semi-outsiders.
461 if (!&IsNickInChan($who,$chan)) {
462 &msg($who, "Failed. You ($who) are not in $chan, hey?");
467 &loadMyModule($myModules{'topic'});
468 &Topic($chan, $thiscmd, join(' ', @args));
469 $cmdstats{'Topic'}++;
474 if ($message =~ /^wingate$/i) {
475 return unless (&hasParam("wingate"));
477 my $reply = "Wingate statistics: scanned \002"
478 .scalar(keys %wingate)."\002 hosts";
479 my $queue = scalar(keys %wingateToDo);
481 $reply .= ". I have \002$queue\002 hosts in the queue";
482 $reply .= ". Started the scan ".&Time2String(time() - $wingaterun)." ago";
485 &performStrictReply("$reply.");
490 # do nothing and let the other routines have a go
498 if (!defined $query) {
500 &msg($who, "I have \002".&countKeys("freshmeat")."\002 entries.");
504 &Freshmeat::Freshmeat($query);
510 &msg($who, "- Uptime for $ident -");
511 &msg($who, "Now: ". &Time2String(&uptimeNow()) ." running $bot_version");
513 foreach (&uptimeGetInfo()) {
515 my $time = &Time2String($1);
518 &msg($who, "$count: $time $2");
527 if (!defined $person or $person =~ /^$/) {
530 my $i = &countKeys("seen");
531 &msg($who,"there ". &fixPlural("is",$i) ." \002$i\002 ".
532 "seen ". &fixPlural("entry",$i) ." that I know of.");
540 &seenFlush(); # very evil hack. oh well, better safe than sorry.
542 ### TODO: Support &dbGetColInfo(); like in &FactInfo();
543 my $select = "nick,time,channel,host,message";
544 if ($person eq "random") {
545 @seen = &randKey("seen", $select);
547 @seen = &dbGet("seen", $select, "nick='$person'");
550 if (scalar @seen < 2) {
552 &DEBUG("seen: _ => '$_'.");
554 &performReply("i haven't seen '$person'");
560 ### TODO: multi channel support. may require &IsNick() to return
561 ### all channels or something.
562 my @chans = &getNickInChans($seen[0]);
564 $reply = "$seen[0] is currently on";
568 next unless (exists $userstats{lc $seen[0]}{'Join'});
569 $reply .= " (".&Time2String(time() - $userstats{lc $seen[0]}{'Join'}).")";
572 if (&IsParam("seenStats")) {
574 $i = $userstats{lc $seen[0]}{'Count'};
575 $reply .= ". Has said a total of \002$i\002 messages" if (defined $i);
576 $i = $userstats{lc $seen[0]}{'Time'};
577 $reply .= ". Is idling for ".&Time2String(time() - $i) if (defined $i);
580 my $howlong = &Time2String(time() - $seen[1]);
581 $reply = "$seen[0] <$seen[3]> was last seen on IRC ".
582 "in channel $seen[2], $howlong ago, ".
583 "saying\002:\002 '$seen[4]'.";
586 &performStrictReply($reply);
590 # User Information Services. requested by Flugh.
592 my ($arg) = join(' ',@_);
594 if ($arg =~ /^set(\s+(.*))?$/i) {
597 &help("userinfo set");
601 &UserInfoSet(split /\s+/, $arg, 2);
602 } elsif ($arg =~ /^unset(\s+(.*))?$/i) {
605 &help("userinfo unset");
609 &UserInfoSet($arg, "");
615 # cookie (random). xk++
619 # lets find that secret cookie.
620 my $target = ($msgType ne 'public') ? $who : $talkchannel;
621 my $cookiemsg = &getRandom(keys %{ $lang{'cookie'} });
624 ### WILL CHEW TONS OF MEM.
625 ### TODO: convert this to a Forker function!
627 my @list = &searchTable("factoids", "factoid_key", "factoid_value", $arg);
628 $key = &getRandom(@list);
629 $val = &getFactInfo("factoids", $key, "factoid_value");
631 ($key,$value) = &randKey("factoids","factoid_key,factoid_value");
635 s/##KEY/\002$key\002/;
638 s/\$who/$who/; # cheap fix.
639 s/(\S+)?\s*<\S+>/$1 /;
643 if ($cookiemsg =~ s/^ACTION //i) {
644 &action($target, $cookiemsg);
646 &msg($target, $cookiemsg);
651 my $arg = join(' ',@_);
652 my ($from,$to) = ('','');
654 ($from,$to) = ($1,$2) if ($arg =~ /^(.*?) to (.*)$/i);
655 ($from,$to) = ($2,$1) if ($arg =~ /^(.*?) from (.*)$/i);
657 if (!$to or !$from) {
658 &msg($who, "Invalid format!");
663 &Units::convertUnits($from, $to);
669 my ($target) = &fixString($_[0]);
671 my $chan = $talkchannel;
673 if ($msgType eq 'private') {
674 if ($target =~ /^($mask{chan})\s+(.*)$/) {
679 &msg($who, "error: invalid format or missing arguments.");
685 my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.lart");
687 if ($target =~ /^(me|you|itself|\Q$ident\E)$/i) {
688 $line =~ s/WHO/$who/g;
690 $line =~ s/WHO/$target/g;
692 $line .= ", courtesy of $who" if ($extra);
694 &action($chan, $line);
696 &status("lart: error reading file?");
701 my $idx = "debian/Packages-woody.idx";
706 $error++ unless ( -e $idx);
707 $error++ unless ( -e "$idx-old");
710 $error = "no woody/woody-old index file found.";
711 &ERROR("Debian: $error");
717 open(IDX2, "$idx-old");
731 next if (exists $pkg{$_});
737 &::performStrictReply( &::formListReply(0, "New debian packages:", @new) );
743 if (!defined $chan) {
748 if (!&validChan($chan)) {
749 &msg($who, "chan $chan is invalid.");
753 if (scalar @vernick > scalar(keys %{ $channels{lc $chan}{''} })/4) {
754 &msg($who, "verstats already in progress for someone else.");
758 &msg($who, "Sending CTCP VERSION...");
759 $conn->ctcp("VERSION", $chan);
760 $cache{verstats}{chan} = $chan;
761 $cache{verstats}{who} = $who;
762 $cache{verstats}{msgType} = $msgType;
764 $conn->schedule(60, sub {
766 my $c = lc $cache{verstats}{chan};
767 my $total = keys %{ $channels{$c}{''} };
769 $who = $cache{verstats}{who};
770 $msgType = $cache{verstats}{msgType};
771 delete $cache{verstats}; # sufficient?
773 foreach (keys %ver) {
774 $vtotal += scalar keys %{ $ver{$_} };
778 my $unknown = $total - $vtotal;
779 my $perc = sprintf("%.1f", $unknown * 100 / $total);
781 $sorted{$perc}{"unknown/cloak"} = "$unknown ($perc%)";
783 foreach (keys %ver) {
784 my $count = scalar keys %{ $ver{$_} };
785 $perc = sprintf("%.01f", $count * 100 / $total);
786 $perc =~ s/.0$//; # lame compression.
788 $sorted{$perc}{$_} = "$count ($perc%)";
791 ### can be compressed to a map?
793 foreach ( sort { $b <=> $a } keys %sorted ) {
795 foreach (sort keys %{ $sorted{$perc} }) {
796 push(@list, "$_ - $sorted{$perc}{$_}");
800 &pSReply( &formListReply(0, "IRC Client versions for $c ", @list) );
802 # clean up not-needed data structures.
810 sub nullski { my ($arg) = @_; return unless (defined $arg);
811 foreach (`$arg`) { &msg($who,$_); } }