2 # User Command Extension Stubs
5 if (&IsParam("useStrict")) { use strict; }
7 $babel_lang_regex = "fr|sp|po|pt|it|ge|de|gr|en";
9 ### COMMAND HOOK IMPLEMENTATION.
10 # addCmdHook("SECTION", 'TEXT_HOOK',
13 # CheckModule => 1, # ???
14 # Module => 'blah.pl' # preload module.
15 # Identifier => 'config_label', # change to Config?
16 # Help => 'help_label',
17 # Cmdstats => 'text_label',)
22 my ($hashname, $ident, %hash) = @_;
24 if (exists ${"hooks_$hashname"}{$ident}) {
25 ### &WARN("aCH: cmd hooks \%$hashname{$ident} already exists.");
29 &VERB("aCH: added $ident",2); # use $hash{'Identifier'}?
30 ### hrm... prevent warnings?
31 ${"hooks_$hashname"}{$ident} = \%hash;
36 my ($hashname, $line) = @_;
37 $line =~ /^(\S+)( (.*))?$/;
38 my $cmd = $1; # command name is whitespaceless.
40 my @args = split(/\s+/, $flatarg || '');
45 if (!defined %{"hooks_$hashname"}) {
46 &WARN("cmd hooks \%$hashname does not exist.");
50 foreach (keys %{"hooks_$hashname"}) {
51 # rename to something else!
54 next unless ($cmd =~ /^$ident$/i);
57 &WARN("pCH: Multiple hook match: $ident");
61 &status("hooks($hashname): $cmd matched '$ident'");
62 my %hash = %{ ${"hooks_$hashname"}{$ident} };
64 if (!scalar keys %hash) {
65 &WARN("CmdHook: hash is NULL?");
69 if ($hash{NoArgs} and $flatarg) {
70 &DEBUG("cmd $ident does not take args; skipping.");
74 if (!exists $hash{CODEREF}) {
75 &ERROR("CODEREF undefined for $cmd or $ident.");
80 foreach (keys %hash) {
81 &VERB(" $cmd->$_ => '$hash{$_}'.",2);
85 if (exists $hash{'Help'} and !scalar(@args)) {
86 &help( $hash{'Help'} );
91 if (exists $hash{'Identifier'}) {
92 return 1 unless (&hasParam($hash{'Identifier'}));
96 if (exists $hash{'UserFlag'}) {
97 return 1 unless (&hasFlag($hash{'UserFlag'}));
100 ### FORKER,IDENTIFIER,CODEREF.
101 if (exists $hash{'Forker'}) {
102 $hash{'Identifier'} .= "-" if ($hash{'Forker'} eq "NULL");
104 if (exists $hash{'ArrayArgs'}) {
105 &Forker($hash{'Identifier'}, sub { \&{$hash{'CODEREF'}}(@args) } );
107 &Forker($hash{'Identifier'}, sub { \&{$hash{'CODEREF'}}($flatarg) } );
111 if (exists $hash{'Module'}) {
112 &loadMyModule($myModules{ $hash{'Module'} });
115 # check if CODEREF exists.
116 if (!defined &{ $hash{'CODEREF'} }) {
117 &WARN("coderef $hash{'CODEREF'} don't exist.");
119 &msg($who, "coderef does not exist for $ident.");
125 if (exists $hash{'ArrayArgs'}) {
126 &{$hash{'CODEREF'}}(@args);
128 &{$hash{'CODEREF'}}($flatarg);
133 if (exists $hash{'Cmdstats'}) {
134 $cmdstats{ $hash{'Cmdstats'} }++;
137 &VERB("hooks: End of command.",2);
147 ### START ADDING HOOKS.
149 &addCmdHook("extra", 'd?bugs', ('CODEREF' => 'debianBugs',
150 'Forker' => 1, 'Identifier' => 'debianExtra',
151 'Cmdstats' => 'Debian Bugs') );
152 &addCmdHook("extra", 'dauthor', ('CODEREF' => 'Debian::searchAuthor',
153 'Forker' => 1, 'Identifier' => 'debian',
154 'Cmdstats' => 'Debian Author Search', 'Help' => "dauthor" ) );
155 &addCmdHook("extra", '(d|search)desc', ('CODEREF' => 'Debian::searchDescFE',
156 'Forker' => 1, 'Identifier' => 'debian',
157 'Cmdstats' => 'Debian Desc Search', 'Help' => "ddesc" ) );
158 &addCmdHook("extra", 'dnew', ('CODEREF' => 'DebianNew',
159 'Identifier' => 'debian' ) );
160 &addCmdHook("extra", 'dincoming', ('CODEREF' => 'Debian::generateIncoming',
161 'Forker' => 1, 'Identifier' => 'debian' ) );
162 &addCmdHook("extra", 'dstats', ('CODEREF' => 'Debian::infoStats',
163 'Forker' => 1, 'Identifier' => 'debian',
164 'Cmdstats' => 'Debian Statistics' ) );
165 &addCmdHook("extra", 'd?contents', ('CODEREF' => 'Debian::searchContents',
166 'Forker' => 1, 'Identifier' => 'debian',
167 'Cmdstats' => 'Debian Contents Search', 'Help' => "contents" ) );
168 &addCmdHook("extra", 'd?find', ('CODEREF' => 'Debian::DebianFind',
169 'Forker' => 1, 'Identifier' => 'debian',
170 'Cmdstats' => 'Debian Search', 'Help' => "find" ) );
171 &addCmdHook("extra", 'insult', ('CODEREF' => 'Insult::Insult',
172 'Forker' => 1, 'Identifier' => 'insult', 'Help' => "insult" ) );
173 &addCmdHook("extra", 'kernel', ('CODEREF' => 'Kernel::Kernel',
174 'Forker' => 1, 'Identifier' => 'kernel',
175 'Cmdstats' => 'Kernel', 'NoArgs' => 1) );
176 &addCmdHook("extra", 'listauth', ('CODEREF' => 'CmdListAuth',
177 'Identifier' => 'search', Module => 'factoids',
178 'Help' => 'listauth') );
179 &addCmdHook("extra", 'quote', ('CODEREF' => 'Quote::Quote',
180 'Forker' => 1, 'Identifier' => 'quote',
181 'Help' => 'quote', 'Cmdstats' => 'Quote') );
182 &addCmdHook("extra", 'countdown', ('CODEREF' => 'Countdown',
183 'Module' => 'countdown', 'Identifier' => 'countdown',
184 'Cmdstats' => 'Countdown') );
185 &addCmdHook("extra", 'lart', ('CODEREF' => 'lart',
186 'Identifier' => 'lart', 'Help' => 'lart') );
187 &addCmdHook("extra", 'convert', ('CODEREF' => 'convert',
188 'Forker' => 1, 'Identifier' => 'units',
189 'Help' => 'convert') );
190 &addCmdHook("extra", '(cookie|random)', ('CODEREF' => 'cookie',
191 'Forker' => 1, 'Identifier' => 'factoids') );
192 &addCmdHook("extra", 'u(ser)?info', ('CODEREF' => 'userinfo',
193 'Identifier' => 'userinfo', 'Help' => 'userinfo',
194 'Module' => 'userinfo') );
195 &addCmdHook("extra", 'rootWarn', ('CODEREF' => 'CmdrootWarn',
196 'Identifier' => 'rootWarn', 'Module' => 'rootwarn') );
197 &addCmdHook("extra", 'seen', ('CODEREF' => 'seen', 'Identifier' =>
199 &addCmdHook("extra", 'dict', ('CODEREF' => 'Dict::Dict',
200 'Identifier' => 'dict', 'Help' => 'dict',
201 'Forker' => 1, 'Cmdstats' => 'Dict') );
202 &addCmdHook("extra", 'slashdot', ('CODEREF' => 'Slashdot::Slashdot',
203 'Identifier' => 'slashdot', 'Forker' => 1,
204 'Cmdstats' => 'Slashdot') );
205 &addCmdHook("extra", 'uptime', ('CODEREF' => 'uptime', 'Identifier' => 'uptime',
206 'Cmdstats' => 'Uptime') );
207 &addCmdHook("extra", 'nullski', ('CODEREF' => 'nullski', ) );
208 &addCmdHook("extra", '(fm|freshmeat)', ('CODEREF' => 'Freshmeat::Freshmeat',
209 'Identifier' => 'freshmeat', 'Cmdstats' => 'Freshmeat',
210 'Forker' => 1, 'Help' => 'freshmeat') );
211 &addCmdHook("extra", 'verstats', ('CODEREF' => 'do_verstats' ) );
214 ### END OF ADDING HOOKS.
216 &status("CMD: loaded ".scalar(keys %hooks_extra)." EXTRA command hooks.");
219 if (!defined $message) {
220 &WARN("Modules: message is undefined. should never happen.");
224 # babel bot: Jonathan Feinberg++
225 if (&IsChanConf("babelfish") and $message =~ m{
227 (?:babel(?:fish)?|x|xlate|translate)
229 (to|from) # direction of translation (through)
231 ($babel_lang_regex)\w* # which language?
233 (.+) # The phrase to be translated
236 &Forker("babelfish", sub { &babel::babelfish(lc $1, lc $2, $3); } );
238 $cmdstats{'BabelFish'}++;
242 if (&IsChanConf("debian")) {
243 my $debiancmd = 'conflicts?|depends?|desc|file|info|provides?';
244 $debiancmd .= '|recommends?|suggests?|maint|maintainer';
245 if ($message =~ /^($debiancmd)(\s+(.*))?$/i) {
248 if (defined $package) {
249 &Forker("debian", sub { &Debian::infoPackages($1, $package); } );
258 # google searching. Simon++
259 if (&IsChanConf("wwwsearch") and $message =~ /^(?:search\s+)?(\S+)\s+for\s+['"]?(.*?)['"]?\s*\?*$/i) {
260 return unless (&hasParam("wwwsearch"));
262 &Forker("wwwsearch", sub { &W3Search::W3Search($1,$2); } );
264 $cmdstats{'WWWSearch'}++;
268 # list{keys|values}. xk++. Idea taken from #linuxwarez@EFNET
269 if ($message =~ /^list(\S+)( (.*))?$/i) {
270 return unless (&hasParam("search"));
272 my $thiscmd = lc($1);
275 # suggested by asuffield nad \broken.
276 if ($args =~ /^["']/ and $args =~ /["']$/) {
277 &DEBUG("list*: removed quotes.");
278 $args =~ s/^["']|["']$//g;
281 $thiscmd =~ s/^vals$/values/;
282 return if ($thiscmd ne "keys" && $thiscmd ne "values");
285 if (!defined $args) {
286 &help("list". $thiscmd);
290 if (length $args == 1) {
291 &msg($who,"search string is too short.");
295 &Forker("search", sub { &Search::Search($thiscmd, $args); } );
297 $cmdstats{'Factoid Search'}++;
301 # Nickometer. Adam Spiers++
302 if ($message =~ /^(?:lame|nick)ometer(?: for)? (\S+)/i) {
303 return unless (&hasParam("nickometer"));
305 my $term = (lc $1 eq 'me') ? $who : $1;
307 &loadMyModule($myModules{'nickometer'});
309 if ($term =~ /^$mask{chan}$/) {
310 &status("Doing nickometer for chan $term.");
312 if (!&validChan($term)) {
313 &msg($who, "error: channel is invalid.");
319 foreach (keys %{ $channels{lc $term}{''} }) {
322 &WARN("nickometer: nick in chan $term undefined?");
326 my $value = &nickometer($str);
327 $nickometer{$value}{$str} = 1;
331 ### TODO: compact with map?
333 foreach (sort {$b <=> $a} keys %nickometer) {
334 my $str = join(", ", sort keys %{$nickometer{$_}});
335 push(@list, "$str ($_%)");
338 &pSReply( &formListReply(0, "Nickometer list for $term ", @list) );
344 my $percentage = &nickometer($term);
346 if ($percentage =~ /NaN/) {
347 $percentage = "off the scale";
349 $percentage = sprintf("%0.4f", $percentage);
350 $percentage =~ s/\.?0+$//;
354 if ($msgType eq 'public') {
355 &say("'$term' is $percentage lame, $who");
357 &msg($who, "the 'lame nick-o-meter' reading for $term is $percentage, $who");
363 # Topic management. xk++
364 # may want to add a flag(??) for topic in the near future. -xk
365 if ($message =~ /^topic(\s+(.*))?$/i) {
366 return unless (&hasParam("topic"));
368 my $chan = $talkchannel;
369 my @args = split / /, $2 || "";
372 &msg($who,"Try 'help topic'");
376 $chan = lc(shift @args) if ($msgType eq 'private');
377 my $thiscmd = shift @args;
380 if ($msgType eq 'public' && $thiscmd =~ /^#/) {
381 &msg($who, "error: channel argument is not required.");
382 &msg($who, "\002Usage\002: topic <CMD>");
386 # topic over private:
387 if ($msgType eq 'private' && $chan !~ /^#/) {
388 &msg($who, "error: channel argument is required.");
389 &msg($who, "\002Usage\002: topic #channel <CMD>");
393 if (&validChan($chan) == 0) {
394 &msg($who,"error: invalid channel \002$chan\002");
398 # for semi-outsiders.
399 if (!&IsNickInChan($who,$chan)) {
400 &msg($who, "Failed. You ($who) are not in $chan, hey?");
405 &loadMyModule($myModules{'topic'});
406 &Topic($chan, $thiscmd, join(' ', @args));
407 $cmdstats{'Topic'}++;
412 if ($message =~ /^wingate$/i) {
413 return unless (&hasParam("wingate"));
415 my $reply = "Wingate statistics: scanned \002"
416 .scalar(keys %wingate)."\002 hosts";
417 my $queue = scalar(keys %wingateToDo);
419 $reply .= ". I have \002$queue\002 hosts in the queue";
420 $reply .= ". Started the scan ".&Time2String(time() - $wingaterun)." ago";
423 &performStrictReply("$reply.");
428 # do nothing and let the other routines have a go
436 if (!defined $query) {
438 &msg($who, "I have \002".&countKeys("freshmeat")."\002 entries.");
442 &Freshmeat::Freshmeat($query);
448 &msg($who, "- Uptime for $ident -");
449 &msg($who, "Now: ". &Time2String(&uptimeNow()) ." running $bot_version");
451 foreach (&uptimeGetInfo()) {
453 my $time = &Time2String($1);
456 &msg($who, "$count: $time $2");
465 if (!defined $person) {
468 my $i = &countKeys("seen");
469 &msg($who,"there ". &fixPlural("is",$i) ." \002$i\002 ".
470 "seen ". &fixPlural("entry",$i) ." that I know of.");
478 &seenFlush(); # very evil hack. oh well, better safe than sorry.
480 ### TODO: Support &dbGetRowInfo(); like in &FactInfo();
481 my $select = "nick,time,channel,host,message";
482 if ($person eq "random") {
483 @seen = &randKey("seen", $select);
485 @seen = &dbGet("seen", "nick", $person, $select);
488 if (scalar @seen < 2) {
490 &DEBUG("seen: _ => '$_'.");
492 &performReply("i haven't seen '$person'");
498 ### TODO: multi channel support. may require &IsNick() to return
499 ### all channels or something.
500 my @chans = &GetNickInChans($seen[0]);
502 $reply = "$seen[0] is currently on";
506 next unless (exists $userstats{lc $seen[0]}{'Join'});
507 $reply .= " (".&Time2String(time() - $userstats{lc $seen[0]}{'Join'}).")";
510 if (&IsParam("seenStats")) {
512 $i = $userstats{lc $seen[0]}{'Count'};
513 $reply .= ". Has said a total of \002$i\002 messages" if (defined $i);
514 $i = $userstats{lc $seen[0]}{'Time'};
515 $reply .= ". Is idling for ".&Time2String(time() - $i) if (defined $i);
518 my $howlong = &Time2String(time() - $seen[1]);
519 $reply = "$seen[0] <$seen[3]> was last seen on IRC ".
520 "in channel $seen[2], $howlong ago, ".
521 "saying\002:\002 '$seen[4]'.";
524 &performStrictReply($reply);
528 # User Information Services. requested by Flugh.
530 my ($arg) = join(' ',@_);
532 if ($arg =~ /^set(\s+(.*))?$/i) {
535 &help("userinfo set");
539 &UserInfoSet(split /\s+/, $arg, 2);
540 } elsif ($arg =~ /^unset(\s+(.*))?$/i) {
543 &help("userinfo unset");
547 &UserInfoSet($arg, "");
553 # cookie (random). xk++
557 # lets find that secret cookie.
558 my $target = ($msgType ne 'public') ? $who : $talkchannel;
559 my $cookiemsg = &getRandom(keys %{$lang{'cookie'}});
562 ### WILL CHEW TONS OF MEM.
563 ### TODO: convert this to a Forker function!
565 my @list = &searchTable("factoids", "factoid_key", "factoid_value", $arg);
566 $key = &getRandom(@list);
567 $val = &getFactInfo("factoids", $key, "factoid_value");
569 ($key,$value) = &randKey("factoids","factoid_key,factoid_value");
573 s/##KEY/\002$key\002/;
576 s/\$who/$who/; # cheap fix.
577 s/(\S+)?\s*<\S+>/$1 /;
581 if ($cookiemsg =~ s/^ACTION //i) {
582 &action($target, $cookiemsg);
584 &msg($target, $cookiemsg);
589 my $arg = join(' ',@_);
590 my ($from,$to) = ('','');
592 ($from,$to) = ($1,$2) if ($arg =~ /^(.*?) to (.*)$/i);
593 ($from,$to) = ($2,$1) if ($arg =~ /^(.*?) from (.*)$/i);
595 if (!$to or !$from) {
596 &msg($who, "Invalid format!");
601 &Units::convertUnits($from, $to);
607 my ($target) = &fixString($_[0]);
609 my $chan = $talkchannel;
611 if ($msgType eq 'private') {
612 if ($target =~ /^($mask{chan})\s+(.*)$/) {
617 &msg($who, "error: invalid format or missing arguments.");
623 my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.lart");
625 if ($target =~ /^(me|you|itself|\Q$ident\E)$/i) {
626 $line =~ s/WHO/$who/g;
628 $line =~ s/WHO/$target/g;
630 $line .= ", courtesy of $who" if ($extra);
632 &action($chan, $line);
634 &status("lart: error reading file?");
639 my $idx = "debian/Packages-woody.idx";
644 $error++ unless ( -e $idx);
645 $error++ unless ( -e "$idx-old");
648 $error = "no woody/woody-old index file found.";
649 &ERROR("Debian: $error");
655 open(IDX2, "$idx-old");
669 next if (exists $pkg{$_});
675 &::performStrictReply( &::formListReply(0, "New debian packages:", @new) );
681 if (!defined $chan) {
686 if (!&validChan($chan)) {
687 &msg($who, "chan $chan is invalid.");
691 if (scalar @vernick > scalar(keys %{ $channels{lc $chan}{''} })/4) {
692 &msg($who, "verstats already in progress for someone else.");
696 &msg($who, "Sending CTCP VERSION...");
697 $conn->ctcp("VERSION", $chan);
698 $cache{verstats}{chan} = $chan;
699 $cache{verstats}{who} = $who;
700 $cache{verstats}{msgType} = $msgType;
702 $conn->schedule(60, sub {
704 my $c = lc $cache{verstats}{chan};
705 my $total = keys %{ $channels{$c}{''} };
707 $who = $cache{verstats}{who};
708 $msgType = $cache{verstats}{msgType};
709 delete $cache{verstats}; # sufficient?
711 foreach (keys %ver) {
712 $vtotal += scalar keys %{ $ver{$_} };
716 my $unknown = $total - $vtotal;
717 my $perc = sprintf("%.1f", $unknown * 100 / $total);
719 $sorted{$perc}{"unknown/cloak"} = "$unknown ($perc%)";
721 foreach (keys %ver) {
722 my $count = scalar keys %{ $ver{$_} };
723 $perc = sprintf("%.01f", $count * 100 / $total);
724 $perc =~ s/.0$//; # lame compression.
726 $sorted{$perc}{$_} = "$count ($perc%)";
729 ### can be compressed to a map?
731 foreach ( sort { $b <=> $a } keys %sorted ) {
733 foreach (sort keys %{ $sorted{$perc} }) {
734 push(@list, "$_ - $sorted{$perc}{$_}");
738 &pSReply( &formListReply(0, "IRC Client versions for $c ", @list) );
740 # clean up not-needed data structures.
748 sub nullski { my ($arg) = @_; return unless (defined $arg);
749 foreach (`$arg`) { &msg($who,$_); } }