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"}) {
53 next unless ($cmd =~ /^$ident$/i);
56 &WARN("pCH: Multiple hook match: $ident");
60 &status("hooks($hashname): $cmd matched '$ident'");
61 my %hash = %{ ${"hooks_$hashname"}{$ident} };
63 if (!scalar keys %hash) {
64 &WARN("CmdHook: hash is NULL?");
68 if ($hash{NoArgs} and $flatarg) {
69 &DEBUG("cmd $ident does not take args; skipping.");
73 if (!exists $hash{CODEREF}) {
74 &ERROR("CODEREF undefined for $cmd or $ident.");
79 foreach (keys %hash) {
80 &VERB(" $cmd->$_ => '$hash{$_}'.",2);
84 if (exists $hash{'Help'} and !scalar(@args)) {
85 &help( $hash{'Help'} );
90 if (exists $hash{'Identifier'}) {
91 return 1 unless (&hasParam($hash{'Identifier'}));
95 if (exists $hash{'UserFlag'}) {
96 return 1 unless (&hasFlag($hash{'UserFlag'}));
99 ### FORKER,IDENTIFIER,CODEREF.
100 if (exists $hash{'Forker'}) {
101 $hash{'Identifier'} .= "-" if ($hash{'Forker'} eq "NULL");
103 if (exists $hash{'ArrayArgs'}) {
104 &Forker($hash{'Identifier'}, sub { \&{$hash{'CODEREF'}}(@args) } );
106 &Forker($hash{'Identifier'}, sub { \&{$hash{'CODEREF'}}($flatarg) } );
110 if (exists $hash{'Module'}) {
111 &loadMyModule($myModules{ $hash{'Module'} });
114 ### TODO: check if CODEREF exists.
116 if (exists $hash{'ArrayArgs'}) {
117 &{$hash{'CODEREF'}}(@args);
119 &{$hash{'CODEREF'}}($flatarg);
124 if (exists $hash{'Cmdstats'}) {
125 $cmdstats{ $hash{'Cmdstats'} }++;
128 &VERB("hooks: End of command.",2);
138 ### START ADDING HOOKS.
140 &addCmdHook("extra", 'd?bugs', ('CODEREF' => 'debianBugs',
141 'Forker' => 1, 'Identifier' => 'debianExtra',
142 'Cmdstats' => 'Debian Bugs') );
143 &addCmdHook("extra", 'dauthor', ('CODEREF' => 'Debian::searchAuthor',
144 'Forker' => 1, 'Identifier' => 'debian',
145 'Cmdstats' => 'Debian Author Search', 'Help' => "dauthor" ) );
146 &addCmdHook("extra", '(d|search)desc', ('CODEREF' => 'Debian::searchDescFE',
147 'Forker' => 1, 'Identifier' => 'debian',
148 'Cmdstats' => 'Debian Desc Search', 'Help' => "ddesc" ) );
149 &addCmdHook("extra", 'dnew', ('CODEREF' => 'DebianNew',
150 'Identifier' => 'debian' ) );
151 &addCmdHook("extra", 'dincoming', ('CODEREF' => 'Debian::generateIncoming',
152 'Forker' => 1, 'Identifier' => 'debian' ) );
153 &addCmdHook("extra", 'dstats', ('CODEREF' => 'Debian::infoStats',
154 'Forker' => 1, 'Identifier' => 'debian',
155 'Cmdstats' => 'Debian Statistics' ) );
156 &addCmdHook("extra", 'd?contents', ('CODEREF' => 'Debian::searchContents',
157 'Forker' => 1, 'Identifier' => 'debian',
158 'Cmdstats' => 'Debian Contents Search', 'Help' => "contents" ) );
159 &addCmdHook("extra", 'd?find', ('CODEREF' => 'Debian::DebianFind',
160 'Forker' => 1, 'Identifier' => 'debian',
161 'Cmdstats' => 'Debian Search', 'Help' => "find" ) );
162 &addCmdHook("extra", 'insult', ('CODEREF' => 'Insult::Insult',
163 'Forker' => 1, 'Identifier' => 'insult', 'Help' => "insult" ) );
164 &addCmdHook("extra", 'kernel', ('CODEREF' => 'Kernel::Kernel',
165 'Forker' => 1, 'Identifier' => 'kernel',
166 'Cmdstats' => 'Kernel', 'NoArgs' => 1) );
167 &addCmdHook("extra", 'listauth', ('CODEREF' => 'CmdListAuth',
168 'Identifier' => 'search', Module => 'factoids',
169 'Help' => 'listauth') );
170 &addCmdHook("extra", 'quote', ('CODEREF' => 'Quote::Quote',
171 'Forker' => 1, 'Identifier' => 'quote',
172 'Help' => 'quote', 'Cmdstats' => 'Quote') );
173 &addCmdHook("extra", 'countdown', ('CODEREF' => 'Countdown',
174 'Module' => 'countdown', 'Identifier' => 'countdown',
175 'Cmdstats' => 'Countdown') );
176 &addCmdHook("extra", 'lart', ('CODEREF' => 'lart',
177 'Identifier' => 'lart', 'Help' => 'lart') );
178 &addCmdHook("extra", 'convert', ('CODEREF' => 'convert',
179 'Forker' => 1, 'Identifier' => 'units',
180 'Help' => 'convert') );
181 &addCmdHook("extra", '(cookie|random)', ('CODEREF' => 'cookie',
182 'Forker' => 1, 'Identifier' => 'factoids') );
183 &addCmdHook("extra", 'u(ser)?info', ('CODEREF' => 'userinfo',
184 'Identifier' => 'userinfo', 'Help' => 'userinfo',
185 'Module' => 'userinfo') );
186 &addCmdHook("extra", 'rootWarn', ('CODEREF' => 'CmdrootWarn',
187 'Identifier' => 'rootWarn', 'Module' => 'rootwarn') );
188 &addCmdHook("extra", 'seen', ('CODEREF' => 'seen', 'Identifier' =>
190 &addCmdHook("extra", 'dict', ('CODEREF' => 'Dict::Dict',
191 'Identifier' => 'dict', 'Help' => 'dict',
192 'Forker' => 1, 'Cmdstats' => 'Dict') );
193 &addCmdHook("extra", 'slashdot', ('CODEREF' => 'Slashdot::Slashdot',
194 'Identifier' => 'slashdot', 'Forker' => 1,
195 'Cmdstats' => 'Slashdot') );
196 &addCmdHook("extra", 'uptime', ('CODEREF' => 'uptime', 'Identifier' => 'uptime',
197 'Cmdstats' => 'Uptime') );
198 &addCmdHook("extra", 'nullski', ('CODEREF' => 'nullski', ) );
199 &addCmdHook("extra", '(fm|freshmeat)', ('CODEREF' => 'Freshmeat::Freshmeat',
200 'Identifier' => 'freshmeat', 'Cmdstats' => 'Freshmeat',
201 'Forker' => 1, 'Help' => 'freshmeat') );
202 &addCmdHook("extra", 'verstats', ('CODEREF' => 'do_verstats' ) );
205 ### END OF ADDING HOOKS.
207 &status("CMD: loaded ".scalar(keys %hooks_extra)." EXTRA command hooks.");
210 if (!defined $message) {
211 &WARN("Modules: message is undefined. should never happen.");
215 # babel bot: Jonathan Feinberg++
216 if (&IsChanConf("babelfish") and $message =~ m{
218 (?:babel(?:fish)?|x|xlate|translate)
220 (to|from) # direction of translation (through)
222 ($babel_lang_regex)\w* # which language?
224 (.+) # The phrase to be translated
227 &Forker("babelfish", sub { &babel::babelfish(lc $1, lc $2, $3); } );
229 $cmdstats{'BabelFish'}++;
233 if (&IsChanConf("debian")) {
234 my $debiancmd = 'conflicts?|depends?|desc|file|info|provides?';
235 $debiancmd .= '|recommends?|suggests?|maint|maintainer';
236 if ($message =~ /^($debiancmd)(\s+(.*))?$/i) {
239 if (defined $package) {
240 &Forker("debian", sub { &Debian::infoPackages($1, $package); } );
249 # google searching. Simon++
250 if (&IsChanConf("wwwsearch") and $message =~ /^(?:search\s+)?(\S+)\s+for\s+['"]?(.*?)['"]?\s*\?*$/i) {
251 return unless (&hasParam("wwwsearch"));
253 &Forker("wwwsearch", sub { &W3Search::W3Search($1,$2); } );
255 $cmdstats{'WWWSearch'}++;
259 # list{keys|values}. xk++. Idea taken from #linuxwarez@EFNET
260 if ($message =~ /^list(\S+)( (.*))?$/i) {
261 return unless (&hasParam("search"));
263 my $thiscmd = lc($1);
266 # suggested by asuffield nad \broken.
267 if ($args =~ /^["']/ and $args =~ /["']$/) {
268 &DEBUG("list*: removed quotes.");
269 $args =~ s/^["']|["']$//g;
272 $thiscmd =~ s/^vals$/values/;
273 return if ($thiscmd ne "keys" && $thiscmd ne "values");
276 if (!defined $args) {
277 &help("list". $thiscmd);
281 if (length $args == 1) {
282 &msg($who,"search string is too short.");
286 &Forker("search", sub { &Search::Search($thiscmd, $args); } );
288 $cmdstats{'Factoid Search'}++;
292 # Nickometer. Adam Spiers++
293 if ($message =~ /^(?:lame|nick)ometer(?: for)? (\S+)/i) {
294 return unless (&hasParam("nickometer"));
296 my $term = (lc $1 eq 'me') ? $who : $1;
298 &loadMyModule($myModules{'nickometer'});
300 if ($term =~ /^$mask{chan}$/) {
301 &status("Doing nickometer for chan $term.");
303 if (!&validChan($term)) {
304 &msg($who, "error: channel is invalid.");
310 foreach (keys %{ $channels{lc $term}{''} }) {
313 &WARN("nickometer: nick in chan $term undefined?");
317 my $value = &nickometer($str);
318 $nickometer{$value}{$str} = 1;
322 ### TODO: compact with map?
324 foreach (sort {$b <=> $a} keys %nickometer) {
325 my $str = join(", ", sort keys %{$nickometer{$_}});
326 push(@list, "$str ($_%)");
329 &pSReply( &formListReply(0, "Nickometer list for $term ", @list) );
335 my $percentage = &nickometer($term);
337 if ($percentage =~ /NaN/) {
338 $percentage = "off the scale";
340 $percentage = sprintf("%0.4f", $percentage);
341 $percentage =~ s/\.?0+$//;
345 if ($msgType eq 'public') {
346 &say("'$term' is $percentage lame, $who");
348 &msg($who, "the 'lame nick-o-meter' reading for $term is $percentage, $who");
354 # Topic management. xk++
355 # may want to add a flag(??) for topic in the near future. -xk
356 if ($message =~ /^topic(\s+(.*))?$/i) {
357 return unless (&hasParam("topic"));
359 my $chan = $talkchannel;
360 my @args = split / /, $2 || "";
363 &msg($who,"Try 'help topic'");
367 $chan = lc(shift @args) if ($msgType eq 'private');
368 my $thiscmd = shift @args;
371 if ($msgType eq 'public' && $thiscmd =~ /^#/) {
372 &msg($who, "error: channel argument is not required.");
373 &msg($who, "\002Usage\002: topic <CMD>");
377 # topic over private:
378 if ($msgType eq 'private' && $chan !~ /^#/) {
379 &msg($who, "error: channel argument is required.");
380 &msg($who, "\002Usage\002: topic #channel <CMD>");
384 if (&validChan($chan) == 0) {
385 &msg($who,"error: invalid channel \002$chan\002");
389 # for semi-outsiders.
390 if (!&IsNickInChan($who,$chan)) {
391 &msg($who, "Failed. You ($who) are not in $chan, hey?");
396 &loadMyModule($myModules{'topic'});
397 &Topic($chan, $thiscmd, join(' ', @args));
398 $cmdstats{'Topic'}++;
403 if ($message =~ /^wingate$/i) {
404 return unless (&hasParam("wingate"));
406 my $reply = "Wingate statistics: scanned \002"
407 .scalar(keys %wingate)."\002 hosts";
408 my $queue = scalar(keys %wingateToDo);
410 $reply .= ". I have \002$queue\002 hosts in the queue";
411 $reply .= ". Started the scan ".&Time2String(time() - $wingaterun)." ago";
414 &performStrictReply("$reply.");
419 # do nothing and let the other routines have a go
427 if (!defined $query) {
429 &msg($who, "I have \002".&countKeys("freshmeat")."\002 entries.");
433 &Freshmeat::Freshmeat($query);
439 &msg($who, "- Uptime for $ident -");
440 &msg($who, "Now: ". &Time2String(&uptimeNow()) ." running $bot_version");
442 foreach (&uptimeGetInfo()) {
444 my $time = &Time2String($1);
447 &msg($who, "$count: $time $2");
456 if (!defined $person) {
459 my $i = &countKeys("seen");
460 &msg($who,"there ". &fixPlural("is",$i) ." \002$i\002 ".
461 "seen ". &fixPlural("entry",$i) ." that I know of.");
469 &seenFlush(); # very evil hack. oh well, better safe than sorry.
471 ### TODO: Support &dbGetRowInfo(); like in &FactInfo();
472 my $select = "nick,time,channel,host,message";
473 if ($person eq "random") {
474 @seen = &randKey("seen", $select);
476 @seen = &dbGet("seen", "nick", $person, $select);
479 if (scalar @seen < 2) {
481 &DEBUG("seen: _ => '$_'.");
483 &performReply("i haven't seen '$person'");
489 ### TODO: multi channel support. may require &IsNick() to return
490 ### all channels or something.
491 my @chans = &GetNickInChans($seen[0]);
493 $reply = "$seen[0] is currently on";
497 next unless (exists $userstats{lc $seen[0]}{'Join'});
498 $reply .= " (".&Time2String(time() - $userstats{lc $seen[0]}{'Join'}).")";
501 if (&IsParam("seenStats")) {
503 $i = $userstats{lc $seen[0]}{'Count'};
504 $reply .= ". Has said a total of \002$i\002 messages" if (defined $i);
505 $i = $userstats{lc $seen[0]}{'Time'};
506 $reply .= ". Is idling for ".&Time2String(time() - $i) if (defined $i);
509 my $howlong = &Time2String(time() - $seen[1]);
510 $reply = "$seen[0] <$seen[3]> was last seen on IRC ".
511 "in channel $seen[2], $howlong ago, ".
512 "saying\002:\002 '$seen[4]'.";
515 &performStrictReply($reply);
519 # User Information Services. requested by Flugh.
521 my ($arg) = join(' ',@_);
523 if ($arg =~ /^set(\s+(.*))?$/i) {
526 &help("userinfo set");
530 &UserInfoSet(split /\s+/, $arg, 2);
531 } elsif ($arg =~ /^unset(\s+(.*))?$/i) {
534 &help("userinfo unset");
538 &UserInfoSet($arg, "");
544 # cookie (random). xk++
548 # lets find that secret cookie.
549 my $target = ($msgType ne 'public') ? $who : $talkchannel;
550 my $cookiemsg = &getRandom(keys %{$lang{'cookie'}});
553 ### WILL CHEW TONS OF MEM.
554 ### TODO: convert this to a Forker function!
556 my @list = &searchTable("factoids", "factoid_key", "factoid_value", $arg);
557 $key = &getRandom(@list);
558 $val = &getFactInfo("factoids", $key, "factoid_value");
560 ($key,$value) = &randKey("factoids","factoid_key,factoid_value");
564 s/##KEY/\002$key\002/;
567 s/\$who/$who/; # cheap fix.
568 s/(\S+)?\s*<\S+>/$1 /;
572 if ($cookiemsg =~ s/^ACTION //i) {
573 &action($target, $cookiemsg);
575 &msg($target, $cookiemsg);
580 my $arg = join(' ',@_);
581 my ($from,$to) = ('','');
583 ($from,$to) = ($1,$2) if ($arg =~ /^(.*?) to (.*)$/i);
584 ($from,$to) = ($2,$1) if ($arg =~ /^(.*?) from (.*)$/i);
586 if (!$to or !$from) {
587 &msg($who, "Invalid format!");
592 &Units::convertUnits($from, $to);
598 my ($target) = &fixString($_[0]);
600 my $chan = $talkchannel;
602 if ($msgType eq 'private') {
603 if ($target =~ /^($mask{chan})\s+(.*)$/) {
608 &msg($who, "error: invalid format or missing arguments.");
614 my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.lart");
616 if ($target =~ /^(me|you|itself|\Q$ident\E)$/i) {
617 $line =~ s/WHO/$who/g;
619 $line =~ s/WHO/$target/g;
621 $line .= ", courtesy of $who" if ($extra);
623 &action($chan, $line);
625 &status("lart: error reading file?");
630 my $idx = "debian/Packages-woody.idx";
635 $error++ unless ( -e $idx);
636 $error++ unless ( -e "$idx-old");
639 $error = "no woody/woody-old index file found.";
640 &ERROR("Debian: $error");
646 open(IDX2, "$idx-old");
660 next if (exists $pkg{$_});
666 &::performStrictReply( &::formListReply(0, "New debian packages:", @new) );
672 if (!defined $chan) {
677 if (!&validChan($chan)) {
678 &msg($who, "chan $chan is invalid.");
682 if (scalar @vernick > scalar(keys %{ $channels{lc $chan}{''} })/4) {
683 &msg($who, "verstats already in progress for someone else.");
687 &msg($who, "Sending CTCP VERSION...");
688 $conn->ctcp("VERSION", $chan);
689 $cache{verstats}{chan} = $chan;
690 $cache{verstats}{who} = $who;
691 $cache{verstats}{msgType} = $msgType;
693 $conn->schedule(60, sub {
695 my $c = lc $cache{verstats}{chan};
696 my $total = keys %{ $channels{$c}{''} };
698 $who = $cache{verstats}{who};
699 $msgType = $cache{verstats}{msgType};
700 delete $cache{verstats}; # sufficient?
702 foreach (keys %ver) {
703 $vtotal += scalar keys %{ $ver{$_} };
707 my $unknown = $total - $vtotal;
708 my $perc = sprintf("%.1f", $unknown * 100 / $total);
710 $sorted{$perc}{"unknown/cloak"} = "$unknown ($perc%)";
712 foreach (keys %ver) {
713 my $count = scalar keys %{ $ver{$_} };
714 $perc = sprintf("%.01f", $count * 100 / $total);
715 $perc =~ s/.0$//; # lame compression.
717 $sorted{$perc}{$_} = "$count ($perc%)";
720 ### can be compressed to a map?
722 foreach ( sort { $b <=> $a } keys %sorted ) {
724 foreach (sort keys %{ $sorted{$perc} }) {
725 push(@list, "$_ - $sorted{$perc}{$_}");
729 &pSReply( &formListReply(0, "IRC Client versions for $c ", @list) );
731 # clean up not-needed data structures.
739 sub nullski { my ($arg) = @_; return unless (defined $arg);
740 foreach (`$arg`) { &msg($who,$_); } }