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 fucking ugly but it works :-)
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 nick DESC LIMIT 3", 1);
291 # unfortunately we have to sort it again!
292 # todo: make dbGetCol return hash and array? too much effort.
293 foreach $i (sort { $b <=> $a } keys %hash) {
294 foreach (keys %{ $hash{$i} }) {
295 push(@top, "$_ - $i");
300 $topstr = ". Top ".scalar(@top).": ".join(', ', @top);
304 &pSReply("total count of '$type': $x$topstr");
306 &pSReply("zero counter for '$type'.");
309 my $x = (&dbRawReturn("SELECT SUM(counter) FROM stats WHERE type=".
310 &dbQuote($type)." AND nick=".&dbQuote($arg) ))[0];
312 if (defined $x) { # defined.
313 &pSReply("$arg has said $type $x times");
315 &pSReply("$arg has not said $type yet.");
323 &DEBUG("regex failed: $@");
328 # list{keys|values}. xk++. Idea taken from #linuxwarez@EFNET
329 if ($message =~ /^list(\S+)(\s+(.*))?$/i) {
330 return unless (&hasParam("search"));
335 $thiscmd =~ s/^vals$/values/;
336 # $args =~ s/\s+$//g;
337 return if ($thiscmd ne "keys" && $thiscmd ne "values");
340 if (!defined $args or $args =~ /^\s*$/) {
341 &help("list". $thiscmd);
345 # suggested by asuffield and \broken.
346 if ($args =~ /^["']/ and $args =~ /["']$/) {
347 &DEBUG("list*: removed quotes.");
348 $args =~ s/^["']|["']$//g;
351 if (length $args == 1) {
352 &msg($who,"search string is too short.");
356 &Forker("search", sub { &Search::Search($thiscmd, $args); } );
358 $cmdstats{'Factoid Search'}++;
362 # Nickometer. Adam Spiers++
363 if ($message =~ /^(?:lame|nick)ometer(?: for)? (\S+)/i) {
364 return unless (&hasParam("nickometer"));
366 my $term = (lc $1 eq 'me') ? $who : $1;
368 &loadMyModule($myModules{'nickometer'});
370 if ($term =~ /^$mask{chan}$/) {
371 &status("Doing nickometer for chan $term.");
373 if (!&validChan($term)) {
374 &msg($who, "error: channel is invalid.");
380 foreach (keys %{ $channels{lc $term}{''} }) {
383 &WARN("nickometer: nick in chan $term undefined?");
387 my $value = &nickometer($str);
388 $nickometer{$value}{$str} = 1;
392 ### TODO: compact with map?
394 foreach (sort {$b <=> $a} keys %nickometer) {
395 my $str = join(", ", sort keys %{ $nickometer{$_} });
396 push(@list, "$str ($_%)");
399 &pSReply( &formListReply(0, "Nickometer list for $term ", @list) );
405 my $percentage = &nickometer($term);
407 if ($percentage =~ /NaN/) {
408 $percentage = "off the scale";
410 $percentage = sprintf("%0.4f", $percentage);
411 $percentage =~ s/\.?0+$//;
415 if ($msgType eq 'public') {
416 &say("'$term' is $percentage lame, $who");
418 &msg($who, "the 'lame nick-o-meter' reading for $term is $percentage, $who");
424 # Topic management. xk++
425 # may want to add a userflags for topic. -xk
426 if ($message =~ /^topic(\s+(.*))?$/i) {
427 return unless (&hasParam("topic"));
429 my $chan = $talkchannel;
430 my @args = split / /, $2 || "";
433 &msg($who,"Try 'help topic'");
437 $chan = lc(shift @args) if ($msgType eq 'private');
438 my $thiscmd = shift @args;
441 if ($msgType eq 'public' && $thiscmd =~ /^#/) {
442 &msg($who, "error: channel argument is not required.");
443 &msg($who, "\002Usage\002: topic <CMD>");
447 # topic over private:
448 if ($msgType eq 'private' && $chan !~ /^#/) {
449 &msg($who, "error: channel argument is required.");
450 &msg($who, "\002Usage\002: topic #channel <CMD>");
454 if (&validChan($chan) == 0) {
455 &msg($who,"error: invalid channel \002$chan\002");
459 # for semi-outsiders.
460 if (!&IsNickInChan($who,$chan)) {
461 &msg($who, "Failed. You ($who) are not in $chan, hey?");
466 &loadMyModule($myModules{'topic'});
467 &Topic($chan, $thiscmd, join(' ', @args));
468 $cmdstats{'Topic'}++;
473 if ($message =~ /^wingate$/i) {
474 return unless (&hasParam("wingate"));
476 my $reply = "Wingate statistics: scanned \002"
477 .scalar(keys %wingate)."\002 hosts";
478 my $queue = scalar(keys %wingateToDo);
480 $reply .= ". I have \002$queue\002 hosts in the queue";
481 $reply .= ". Started the scan ".&Time2String(time() - $wingaterun)." ago";
484 &performStrictReply("$reply.");
489 # do nothing and let the other routines have a go
497 if (!defined $query) {
499 &msg($who, "I have \002".&countKeys("freshmeat")."\002 entries.");
503 &Freshmeat::Freshmeat($query);
509 &msg($who, "- Uptime for $ident -");
510 &msg($who, "Now: ". &Time2String(&uptimeNow()) ." running $bot_version");
512 foreach (&uptimeGetInfo()) {
514 my $time = &Time2String($1);
517 &msg($who, "$count: $time $2");
526 if (!defined $person or $person =~ /^$/) {
529 my $i = &countKeys("seen");
530 &msg($who,"there ". &fixPlural("is",$i) ." \002$i\002 ".
531 "seen ". &fixPlural("entry",$i) ." that I know of.");
539 &seenFlush(); # very evil hack. oh well, better safe than sorry.
541 ### TODO: Support &dbGetColInfo(); like in &FactInfo();
542 my $select = "nick,time,channel,host,message";
543 if ($person eq "random") {
544 @seen = &randKey("seen", $select);
546 @seen = &dbGet("seen", $select, "nick='$person'");
549 if (scalar @seen < 2) {
551 &DEBUG("seen: _ => '$_'.");
553 &performReply("i haven't seen '$person'");
559 ### TODO: multi channel support. may require &IsNick() to return
560 ### all channels or something.
561 my @chans = &getNickInChans($seen[0]);
563 $reply = "$seen[0] is currently on";
567 next unless (exists $userstats{lc $seen[0]}{'Join'});
568 $reply .= " (".&Time2String(time() - $userstats{lc $seen[0]}{'Join'}).")";
571 if (&IsParam("seenStats")) {
573 $i = $userstats{lc $seen[0]}{'Count'};
574 $reply .= ". Has said a total of \002$i\002 messages" if (defined $i);
575 $i = $userstats{lc $seen[0]}{'Time'};
576 $reply .= ". Is idling for ".&Time2String(time() - $i) if (defined $i);
579 my $howlong = &Time2String(time() - $seen[1]);
580 $reply = "$seen[0] <$seen[3]> was last seen on IRC ".
581 "in channel $seen[2], $howlong ago, ".
582 "saying\002:\002 '$seen[4]'.";
585 &performStrictReply($reply);
589 # User Information Services. requested by Flugh.
591 my ($arg) = join(' ',@_);
593 if ($arg =~ /^set(\s+(.*))?$/i) {
596 &help("userinfo set");
600 &UserInfoSet(split /\s+/, $arg, 2);
601 } elsif ($arg =~ /^unset(\s+(.*))?$/i) {
604 &help("userinfo unset");
608 &UserInfoSet($arg, "");
614 # cookie (random). xk++
618 # lets find that secret cookie.
619 my $target = ($msgType ne 'public') ? $who : $talkchannel;
620 my $cookiemsg = &getRandom(keys %{ $lang{'cookie'} });
623 ### WILL CHEW TONS OF MEM.
624 ### TODO: convert this to a Forker function!
626 my @list = &searchTable("factoids", "factoid_key", "factoid_value", $arg);
627 $key = &getRandom(@list);
628 $val = &getFactInfo("factoids", $key, "factoid_value");
630 ($key,$value) = &randKey("factoids","factoid_key,factoid_value");
634 s/##KEY/\002$key\002/;
637 s/\$who/$who/; # cheap fix.
638 s/(\S+)?\s*<\S+>/$1 /;
642 if ($cookiemsg =~ s/^ACTION //i) {
643 &action($target, $cookiemsg);
645 &msg($target, $cookiemsg);
650 my $arg = join(' ',@_);
651 my ($from,$to) = ('','');
653 ($from,$to) = ($1,$2) if ($arg =~ /^(.*?) to (.*)$/i);
654 ($from,$to) = ($2,$1) if ($arg =~ /^(.*?) from (.*)$/i);
656 if (!$to or !$from) {
657 &msg($who, "Invalid format!");
662 &Units::convertUnits($from, $to);
668 my ($target) = &fixString($_[0]);
670 my $chan = $talkchannel;
672 if ($msgType eq 'private') {
673 if ($target =~ /^($mask{chan})\s+(.*)$/) {
678 &msg($who, "error: invalid format or missing arguments.");
684 my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.lart");
686 if ($target =~ /^(me|you|itself|\Q$ident\E)$/i) {
687 $line =~ s/WHO/$who/g;
689 $line =~ s/WHO/$target/g;
691 $line .= ", courtesy of $who" if ($extra);
693 &action($chan, $line);
695 &status("lart: error reading file?");
700 my $idx = "debian/Packages-woody.idx";
705 $error++ unless ( -e $idx);
706 $error++ unless ( -e "$idx-old");
709 $error = "no woody/woody-old index file found.";
710 &ERROR("Debian: $error");
716 open(IDX2, "$idx-old");
730 next if (exists $pkg{$_});
736 &::performStrictReply( &::formListReply(0, "New debian packages:", @new) );
742 if (!defined $chan) {
747 if (!&validChan($chan)) {
748 &msg($who, "chan $chan is invalid.");
752 if (scalar @vernick > scalar(keys %{ $channels{lc $chan}{''} })/4) {
753 &msg($who, "verstats already in progress for someone else.");
757 &msg($who, "Sending CTCP VERSION...");
758 $conn->ctcp("VERSION", $chan);
759 $cache{verstats}{chan} = $chan;
760 $cache{verstats}{who} = $who;
761 $cache{verstats}{msgType} = $msgType;
763 $conn->schedule(60, sub {
765 my $c = lc $cache{verstats}{chan};
766 my $total = keys %{ $channels{$c}{''} };
768 $who = $cache{verstats}{who};
769 $msgType = $cache{verstats}{msgType};
770 delete $cache{verstats}; # sufficient?
772 foreach (keys %ver) {
773 $vtotal += scalar keys %{ $ver{$_} };
777 my $unknown = $total - $vtotal;
778 my $perc = sprintf("%.1f", $unknown * 100 / $total);
780 $sorted{$perc}{"unknown/cloak"} = "$unknown ($perc%)";
782 foreach (keys %ver) {
783 my $count = scalar keys %{ $ver{$_} };
784 $perc = sprintf("%.01f", $count * 100 / $total);
785 $perc =~ s/.0$//; # lame compression.
787 $sorted{$perc}{$_} = "$count ($perc%)";
790 ### can be compressed to a map?
792 foreach ( sort { $b <=> $a } keys %sorted ) {
794 foreach (sort keys %{ $sorted{$perc} }) {
795 push(@list, "$_ - $sorted{$perc}{$_}");
799 &pSReply( &formListReply(0, "IRC Client versions for $c ", @list) );
801 # clean up not-needed data structures.
809 sub nullski { my ($arg) = @_; return unless (defined $arg);
810 foreach (`$arg`) { &msg($who,$_); } }