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 &DEBUG("pCH(hooks_$hashname): $cmd matched $ident");
61 my %hash = %{ ${"hooks_$hashname"}{$ident} };
63 if (!scalar keys %hash) {
64 &WARN("CmdHook: hash is NULL?");
68 if (!exists $hash{CODEREF}) {
69 &ERROR("CODEREF undefined for $cmd or $ident.");
74 foreach (keys %hash) {
75 &DEBUG(" $cmd->$_ => '$hash{$_}'.");
79 if (exists $hash{'Help'} and !scalar(@args)) {
80 &help( $hash{'Help'} );
85 if (exists $hash{'Identifier'}) {
86 return 1 unless (&hasParam($hash{'Identifier'}));
90 if (exists $hash{'UserFlag'}) {
91 return 1 unless (&hasFlag($hash{'UserFlag'}));
94 ### FORKER,IDENTIFIER,CODEREF.
95 if (exists $hash{'Forker'}) {
96 $hash{'Identifier'} .= "-" if ($hash{'Forker'} eq "NULL");
98 if (exists $hash{'ArrayArgs'}) {
99 &Forker($hash{'Identifier'}, sub { \&{$hash{'CODEREF'}}(@args) } );
101 &Forker($hash{'Identifier'}, sub { \&{$hash{'CODEREF'}}($flatarg) } );
105 if (exists $hash{'Module'}) {
106 &loadMyModule($myModules{ $hash{'Module'} });
109 ### TODO: check if CODEREF exists.
111 if (exists $hash{'ArrayArgs'}) {
112 &{$hash{'CODEREF'}}(@args);
114 &{$hash{'CODEREF'}}($flatarg);
119 if (exists $hash{'Cmdstats'}) {
120 $cmdstats{ $hash{'Cmdstats'} }++;
123 &DEBUG("pCH: ended.");
133 ### START ADDING HOOKS.
135 &addCmdHook("extra", 'd?bugs', ('CODEREF' => 'debianBugs',
136 'Forker' => 1, 'Identifier' => 'debianExtra',
137 'Cmdstats' => 'Debian Bugs') );
138 &addCmdHook("extra", 'dauthor', ('CODEREF' => 'Debian::searchAuthor',
139 'Forker' => 1, 'Identifier' => 'debian',
140 'Cmdstats' => 'Debian Author Search', 'Help' => "dauthor" ) );
141 &addCmdHook("extra", '(d|search)desc', ('CODEREF' => 'Debian::searchDescFE',
142 'Forker' => 1, 'Identifier' => 'debian',
143 'Cmdstats' => 'Debian Desc Search', 'Help' => "ddesc" ) );
144 &addCmdHook("extra", 'dnew', ('CODEREF' => 'DebianNew',
145 'Identifier' => 'debian' ) );
146 &addCmdHook("extra", 'dincoming', ('CODEREF' => 'Debian::generateIncoming',
147 'Forker' => 1, 'Identifier' => 'debian' ) );
148 &addCmdHook("extra", 'dstats', ('CODEREF' => 'Debian::infoStats',
149 'Forker' => 1, 'Identifier' => 'debian',
150 'Cmdstats' => 'Debian Statistics' ) );
151 &addCmdHook("extra", 'd?contents', ('CODEREF' => 'Debian::searchContents',
152 'Forker' => 1, 'Identifier' => 'debian',
153 'Cmdstats' => 'Debian Contents Search', 'Help' => "contents" ) );
154 &addCmdHook("extra", 'd?find', ('CODEREF' => 'Debian::DebianFind',
155 'Forker' => 1, 'Identifier' => 'debian',
156 'Cmdstats' => 'Debian Search', 'Help' => "find" ) );
157 &addCmdHook("extra", 'insult', ('CODEREF' => 'Insult::Insult',
158 'Forker' => 1, 'Identifier' => 'insult', 'Help' => "insult" ) );
159 &addCmdHook("extra", 'kernel', ('CODEREF' => 'Kernel::Kernel',
160 'Forker' => 1, 'Identifier' => 'kernel',
161 'Cmdstats' => 'Kernel') );
162 &addCmdHook("extra", 'listauth', ('CODEREF' => 'CmdListAuth',
163 'Identifier' => 'search', Module => 'factoids',
164 'Help' => 'listauth') );
165 &addCmdHook("extra", 'quote', ('CODEREF' => 'Quote::Quote',
166 'Forker' => 1, 'Identifier' => 'quote',
167 'Help' => 'quote', 'Cmdstats' => 'Quote') );
168 &addCmdHook("extra", 'countdown', ('CODEREF' => 'Countdown',
169 'Module' => 'countdown', 'Identifier' => 'countdown',
170 'Cmdstats' => 'Countdown') );
171 &addCmdHook("extra", 'lart', ('CODEREF' => 'lart',
172 'Identifier' => 'lart', 'Help' => 'lart') );
173 &addCmdHook("extra", 'convert', ('CODEREF' => 'convert',
174 'Forker' => 1, 'Identifier' => 'units',
175 'Help' => 'convert') );
176 &addCmdHook("extra", '(cookie|random)', ('CODEREF' => 'cookie',
177 'Forker' => 1, 'Identifier' => 'factoids') );
178 &addCmdHook("extra", 'u(ser)?info', ('CODEREF' => 'userinfo',
179 'Identifier' => 'userinfo', 'Help' => 'userinfo',
180 'Module' => 'userinfo') );
181 &addCmdHook("extra", 'rootWarn', ('CODEREF' => 'CmdrootWarn',
182 'Identifier' => 'rootWarn', 'Module' => 'rootwarn') );
183 &addCmdHook("extra", 'seen', ('CODEREF' => 'seen', 'Identifier' =>
185 &addCmdHook("extra", 'dict', ('CODEREF' => 'Dict::Dict',
186 'Identifier' => 'dict', 'Help' => 'dict',
187 'Forker' => 1, 'Cmdstats' => 'Dict') );
188 &addCmdHook("extra", 'slashdot', ('CODEREF' => 'Slashdot::Slashdot',
189 'Identifier' => 'slashdot', 'Forker' => 1,
190 'Cmdstats' => 'Slashdot') );
191 &addCmdHook("extra", 'uptime', ('CODEREF' => 'uptime', 'Identifier' => 'uptime',
192 'Cmdstats' => 'Uptime') );
193 &addCmdHook("extra", 'nullski', ('CODEREF' => 'nullski', ) );
194 sub nullski { my ($arg) = @_; foreach (`$arg`) { &msg($who,$_); } }
195 &addCmdHook("extra", '(fm|freshmeat)', ('CODEREF' => 'Freshmeat::Freshmeat',
196 'Identifier' => 'freshmeat', 'Cmdstats' => 'Freshmeat',
197 'Forker' => 1, 'Help' => 'freshmeat') );
198 &addCmdHook("extra", 'verstats', ('CODEREF' => 'do_verstats' ) );
201 ### END OF ADDING HOOKS.
203 &status("CMD: loaded ".scalar(keys %hooks_extra)." EXTRA command hooks.");
206 if (!defined $message) {
207 &WARN("Modules: message is undefined. should never happen.");
211 # babel bot: Jonathan Feinberg++
212 if (&IsChanConf("babelfish") and $message =~ m{
214 (?:babel(?:fish)?|x|xlate|translate)
216 (to|from) # direction of translation (through)
218 ($babel_lang_regex)\w* # which language?
220 (.+) # The phrase to be translated
223 &Forker("babelfish", sub { &babel::babelfish(lc $1, lc $2, $3); } );
225 $cmdstats{'BabelFish'}++;
229 if (&IsChanConf("debian")) {
230 my $debiancmd = 'conflicts?|depends?|desc|file|info|provides?';
231 $debiancmd .= '|recommends?|suggests?|maint|maintainer';
232 if ($message =~ /^($debiancmd)(\s+(.*))?$/i) {
235 if (defined $package) {
236 &Forker("debian", sub { &Debian::infoPackages($1, $package); } );
245 # google searching. Simon++
246 if (&IsChanConf("wwwsearch") and $message =~ /^(?:search\s+)?(\S+)\s+for\s+['"]?(.*?)['"]?\s*\?*$/i) {
247 return unless (&hasParam("wwwsearch"));
249 &Forker("wwwsearch", sub { &W3Search::W3Search($1,$2); } );
251 $cmdstats{'WWWSearch'}++;
255 # list{keys|values}. xk++. Idea taken from #linuxwarez@EFNET
256 if ($message =~ /^list(\S+)( (.*))?$/i) {
257 return unless (&hasParam("search"));
259 my $thiscmd = lc($1);
262 $thiscmd =~ s/^vals$/values/;
263 return if ($thiscmd ne "keys" && $thiscmd ne "values");
266 if (!defined $args) {
267 &help("list". $thiscmd);
271 if (length $args == 1) {
272 &msg($who,"search string is too short.");
276 &Forker("search", sub { &Search::Search($thiscmd, $args); } );
278 $cmdstats{'Factoid Search'}++;
282 # Nickometer. Adam Spiers++
283 if ($message =~ /^(?:lame|nick)ometer(?: for)? (\S+)/i) {
284 return unless (&hasParam("nickometer"));
286 my $term = (lc $1 eq 'me') ? $who : $1;
288 &loadMyModule($myModules{'nickometer'});
290 if ($term =~ /^$mask{chan}$/) {
291 &status("Doing nickometer for chan $term.");
293 if (!&validChan($term)) {
294 &msg($who, "error: channel is invalid.");
300 foreach (keys %{ $channels{lc $term}{''} }) {
303 &WARN("nickometer: nick in chan $term undefined?");
307 my $value = &nickometer($str);
308 $nickometer{$value}{$str} = 1;
312 ### TODO: compact with map?
314 foreach (sort {$b <=> $a} keys %nickometer) {
315 my $str = join(", ", sort keys %{$nickometer{$_}});
316 push(@list, "$str ($_%)");
319 &pSReply( &formListReply(0, "Nickometer list for $term ", @list) );
325 my $percentage = &nickometer($term);
327 if ($percentage =~ /NaN/) {
328 $percentage = "off the scale";
330 $percentage = sprintf("%0.4f", $percentage);
331 $percentage =~ s/\.?0+$//;
335 if ($msgType eq 'public') {
336 &say("'$term' is $percentage lame, $who");
338 &msg($who, "the 'lame nick-o-meter' reading for $term is $percentage, $who");
344 # Topic management. xk++
345 # may want to add a flag(??) for topic in the near future. -xk
346 if ($message =~ /^topic(\s+(.*))?$/i) {
347 return unless (&hasParam("topic"));
349 my $chan = $talkchannel;
350 my @args = split(/ /, $2);
353 &msg($who,"Try 'help topic'");
357 $chan = lc(shift @args) if ($msgType eq 'private');
358 my $thiscmd = shift @args;
361 if ($msgType eq 'public' && $thiscmd =~ /^#/) {
362 &msg($who, "error: channel argument is not required.");
363 &msg($who, "\002Usage\002: topic <CMD>");
367 # topic over private:
368 if ($msgType eq 'private' && $chan !~ /^#/) {
369 &msg($who, "error: channel argument is required.");
370 &msg($who, "\002Usage\002: topic #channel <CMD>");
374 if (&validChan($chan) == 0) {
375 &msg($who,"error: invalid channel \002$chan\002");
379 # for semi-outsiders.
380 if (!&IsNickInChan($who,$chan)) {
381 &msg($who, "Failed. You ($who) are not in $chan, hey?");
386 &loadMyModule($myModules{'topic'});
387 &Topic($chan, $thiscmd, join(' ', @args));
388 $cmdstats{'Topic'}++;
393 if ($message =~ /^wingate$/i) {
394 return unless (&hasParam("wingate"));
396 my $reply = "Wingate statistics: scanned \002"
397 .scalar(keys %wingate)."\002 hosts";
398 my $queue = scalar(keys %wingateToDo);
400 $reply .= ". I have \002$queue\002 hosts in the queue";
401 $reply .= ". Started the scan ".&Time2String(time() - $wingaterun)." ago";
404 &performStrictReply("$reply.");
409 # do nothing and let the other routines have a go
417 if (!defined $query) {
419 &msg($who, "I have \002".&countKeys("freshmeat")."\002 entries.");
423 &Freshmeat::Freshmeat($query);
429 &msg($who, "- Uptime for $ident -");
430 &msg($who, "Now: ". &Time2String(&uptimeNow()) ." running $bot_version");
432 foreach (&uptimeGetInfo()) {
434 my $time = &Time2String($1);
437 &msg($who, "$count: $time $2");
446 if (!defined $person) {
449 my $i = &countKeys("seen");
450 &msg($who,"there ". &fixPlural("is",$i) ." \002$i\002 ".
451 "seen ". &fixPlural("entry",$i) ." that I know of.");
459 &seenFlush(); # very evil hack. oh well, better safe than sorry.
461 ### TODO: Support &dbGetRowInfo(); like in &FactInfo();
462 my $select = "nick,time,channel,host,message";
463 if ($person eq "random") {
464 @seen = &randKey("seen", $select);
466 @seen = &dbGet("seen", "nick", $person, $select);
469 if (scalar @seen < 2) {
471 &DEBUG("seen: _ => '$_'.");
473 &performReply("i haven't seen '$person'");
479 ### TODO: multi channel support. may require &IsNick() to return
480 ### all channels or something.
481 my @chans = &GetNickInChans($seen[0]);
483 $reply = "$seen[0] is currently on";
487 next unless (exists $userstats{lc $seen[0]}{'Join'});
488 $reply .= " (".&Time2String(time() - $userstats{lc $seen[0]}{'Join'}).")";
491 if (&IsParam("seenStats")) {
493 $i = $userstats{lc $seen[0]}{'Count'};
494 $reply .= ". Has said a total of \002$i\002 messages" if (defined $i);
495 $i = $userstats{lc $seen[0]}{'Time'};
496 $reply .= ". Is idling for ".&Time2String(time() - $i) if (defined $i);
499 my $howlong = &Time2String(time() - $seen[1]);
500 $reply = "$seen[0] <$seen[3]> was last seen on IRC ".
501 "in channel $seen[2], $howlong ago, ".
502 "saying\002:\002 '$seen[4]'.";
505 &performStrictReply($reply);
509 # User Information Services. requested by Flugh.
511 my ($arg) = join(' ',@_);
513 if ($arg =~ /^set(\s+(.*))?$/i) {
516 &help("userinfo set");
520 &UserInfoSet(split /\s+/, $arg, 2);
521 } elsif ($arg =~ /^unset(\s+(.*))?$/i) {
524 &help("userinfo unset");
528 &UserInfoSet($arg, "");
534 # cookie (random). xk++
538 # lets find that secret cookie.
539 my $target = ($msgType ne 'public') ? $who : $talkchannel;
540 my $cookiemsg = &getRandom(keys %{$lang{'cookie'}});
543 ### WILL CHEW TONS OF MEM.
544 ### TODO: convert this to a Forker function!
546 my @list = &searchTable("factoids", "factoid_key", "factoid_value", $arg);
547 $key = &getRandom(@list);
548 $val = &getFactInfo("factoids", $key, "factoid_value");
550 ($key,$value) = &randKey("factoids","factoid_key,factoid_value");
554 s/##KEY/\002$key\002/;
557 s/\$who/$who/; # cheap fix.
558 s/(\S+)?\s*<\S+>/$1 /;
562 if ($cookiemsg =~ s/^ACTION //i) {
563 &action($target, $cookiemsg);
565 &msg($target, $cookiemsg);
570 my $arg = join(' ',@_);
571 my ($from,$to) = ('','');
573 ($from,$to) = ($1,$2) if ($arg =~ /^(.*?) to (.*)$/i);
574 ($from,$to) = ($2,$1) if ($arg =~ /^(.*?) from (.*)$/i);
576 if (!$to or !$from) {
577 &msg($who, "Invalid format!");
582 &Units::convertUnits($from, $to);
588 my ($target) = &fixString($_[0]);
590 my $chan = $talkchannel;
592 if ($msgType eq 'private') {
593 if ($target =~ /^($mask{chan})\s+(.*)$/) {
598 &msg($who, "error: invalid format or missing arguments.");
604 my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.lart");
606 if ($target =~ /^(me|you|itself|\Q$ident\E)$/i) {
607 $line =~ s/WHO/$who/g;
609 $line =~ s/WHO/$target/g;
611 $line .= ", courtesy of $who" if ($extra);
613 &action($chan, $line);
615 &status("lart: error reading file?");
620 my $idx = "debian/Packages-woody.idx";
625 $error++ unless ( -e $idx);
626 $error++ unless ( -e "$idx-old");
629 $error = "no woody/woody-old index file found.";
630 &ERROR("Debian: $error");
636 open(IDX2, "$idx-old");
650 next if (exists $pkg{$_});
656 &::performStrictReply( &::formListReply(0, "New debian packages:", @new) );
662 if (!defined $chan) {
667 if (!&validChan($chan)) {
668 &msg($who, "chan $chan is invalid.");
672 if (scalar @vernick > scalar(keys %{ $channels{lc $chan}{''} })/4) {
673 &msg($who, "verstats already in progress for someone else.");
677 &msg($who, "Sending CTCP VERSION...");
678 $conn->ctcp("VERSION", $chan);
679 $cache{verstats}{chan} = $chan;
680 $cache{verstats}{who} = $who;
681 $cache{verstats}{msgType} = $msgType;
683 $conn->schedule(60, sub {
685 my $c = lc $cache{verstats}{chan};
686 my $total = keys %{ $channels{$c}{''} };
688 $who = $cache{verstats}{who};
689 $msgType = $cache{verstats}{msgType};
690 delete $cache{verstats}; # sufficient?
692 foreach (keys %ver) {
693 $vtotal += scalar keys %{ $ver{$_} };
697 my $unknown = $total - $vtotal;
698 my $perc = sprintf("%.1f", $unknown * 100 / $total);
700 $sorted{$perc}{"unknown/cloak"} = "$unknown ($perc%)";
702 foreach (keys %ver) {
703 my $count = scalar keys %{ $ver{$_} };
704 $perc = sprintf("%.01f", $count * 100 / $total);
705 $perc =~ s/.0$//; # lame compression.
707 $sorted{$perc}{$_} = "$count ($perc%)";
710 ### can be compressed to a map?
712 foreach ( sort { $b <=> $a } keys %sorted ) {
714 foreach (sort keys %{ $sorted{$perc} }) {
715 push(@list, "$_ - $sorted{$perc}{$_}");
719 &pSReply( &formListReply(0, "IRC Client versions for $c ", @list) );
721 # clean up not-needed data structures.