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!
55 next unless ($cmd =~ /^$ident$/i);
58 &WARN("pCH: Multiple hook match: $ident");
62 &status("hooks($hashname): $cmd matched '$ident'");
63 my %hash = %{ ${"hooks_$hashname"}{$ident} };
65 if (!scalar keys %hash) {
66 &WARN("CmdHook: hash is NULL?");
70 if ($hash{NoArgs} and $flatarg) {
71 &DEBUG("cmd $ident does not take args; skipping.");
75 if (!exists $hash{CODEREF}) {
76 &ERROR("CODEREF undefined for $cmd or $ident.");
81 foreach (keys %hash) {
82 &VERB(" $cmd->$_ => '$hash{$_}'.",2);
86 if (exists $hash{'Help'} and !scalar(@args)) {
87 &help( $hash{'Help'} );
92 if (exists $hash{'Identifier'}) {
93 return 1 unless (&hasParam($hash{'Identifier'}));
97 if (exists $hash{'UserFlag'}) {
98 return 1 unless (&hasFlag($hash{'UserFlag'}));
101 ### FORKER,IDENTIFIER,CODEREF.
102 if (exists $hash{'Forker'}) {
103 $hash{'Identifier'} .= "-" if ($hash{'Forker'} eq "NULL");
105 if (exists $hash{'ArrayArgs'}) {
106 &Forker($hash{'Identifier'}, sub { \&{ $hash{'CODEREF'} }(@args) } );
108 &Forker($hash{'Identifier'}, sub { \&{ $hash{'CODEREF'} }($flatarg) } );
112 if (exists $hash{'Module'}) {
113 &loadMyModule($myModules{ $hash{'Module'} });
116 # check if CODEREF exists.
117 if (!defined &{ $hash{'CODEREF'} }) {
118 &WARN("coderef $hash{'CODEREF'} don't exist.");
120 &msg($who, "coderef does not exist for $ident.");
126 if (exists $hash{'ArrayArgs'}) {
127 &{ $hash{'CODEREF'} }(@args);
129 &{ $hash{'CODEREF'} }($flatarg);
134 if (exists $hash{'Cmdstats'}) {
135 $cmdstats{ $hash{'Cmdstats'} }++;
138 &VERB("hooks: End of command.",2);
148 ### START ADDING HOOKS.
150 &addCmdHook("extra", 'd?bugs', ('CODEREF' => 'DBugs::Parse',
151 'Forker' => 1, 'Identifier' => 'debianExtra',
152 'Cmdstats' => 'Debian Bugs') );
153 &addCmdHook("extra", 'dauthor', ('CODEREF' => 'Debian::searchAuthor',
154 'Forker' => 1, 'Identifier' => 'debian',
155 'Cmdstats' => 'Debian Author Search', 'Help' => "dauthor" ) );
156 &addCmdHook("extra", '(d|search)desc', ('CODEREF' => 'Debian::searchDescFE',
157 'Forker' => 1, 'Identifier' => 'debian',
158 'Cmdstats' => 'Debian Desc Search', 'Help' => "ddesc" ) );
159 &addCmdHook("extra", 'dnew', ('CODEREF' => 'DebianNew',
160 'Identifier' => 'debian' ) );
161 &addCmdHook("extra", 'dincoming', ('CODEREF' => 'Debian::generateIncoming',
162 'Forker' => 1, 'Identifier' => 'debian' ) );
163 &addCmdHook("extra", 'dstats', ('CODEREF' => 'Debian::infoStats',
164 'Forker' => 1, 'Identifier' => 'debian',
165 'Cmdstats' => 'Debian Statistics' ) );
166 &addCmdHook("extra", 'd?contents', ('CODEREF' => 'Debian::searchContents',
167 'Forker' => 1, 'Identifier' => 'debian',
168 'Cmdstats' => 'Debian Contents Search', 'Help' => "contents" ) );
169 &addCmdHook("extra", 'd?find', ('CODEREF' => 'Debian::DebianFind',
170 'Forker' => 1, 'Identifier' => 'debian',
171 'Cmdstats' => 'Debian Search', 'Help' => "find" ) );
172 &addCmdHook("extra", 'insult', ('CODEREF' => 'Insult::Insult',
173 'Forker' => 1, 'Identifier' => 'insult', 'Help' => "insult" ) );
174 &addCmdHook("extra", 'kernel', ('CODEREF' => 'Kernel::Kernel',
175 'Forker' => 1, 'Identifier' => 'kernel',
176 'Cmdstats' => 'Kernel', 'NoArgs' => 1) );
177 &addCmdHook("extra", 'listauth', ('CODEREF' => 'CmdListAuth',
178 'Identifier' => 'search', Module => 'factoids',
179 'Help' => 'listauth') );
180 &addCmdHook("extra", 'quote', ('CODEREF' => 'Quote::Quote',
181 'Forker' => 1, 'Identifier' => 'quote',
182 'Help' => 'quote', 'Cmdstats' => 'Quote') );
183 &addCmdHook("extra", 'countdown', ('CODEREF' => 'Countdown',
184 'Module' => 'countdown', 'Identifier' => 'countdown',
185 'Cmdstats' => 'Countdown') );
186 &addCmdHook("extra", 'lart', ('CODEREF' => 'lart',
187 'Identifier' => 'lart', 'Help' => 'lart') );
188 &addCmdHook("extra", 'convert', ('CODEREF' => 'convert',
189 'Forker' => 1, 'Identifier' => 'units',
190 'Help' => 'convert') );
191 &addCmdHook("extra", '(cookie|random)', ('CODEREF' => 'cookie',
192 'Forker' => 1, 'Identifier' => 'factoids') );
193 &addCmdHook("extra", 'u(ser)?info', ('CODEREF' => 'userinfo',
194 'Identifier' => 'userinfo', 'Help' => 'userinfo',
195 'Module' => 'userinfo') );
196 &addCmdHook("extra", 'rootWarn', ('CODEREF' => 'CmdrootWarn',
197 'Identifier' => 'rootWarn', 'Module' => 'rootwarn') );
198 &addCmdHook("extra", 'seen', ('CODEREF' => 'seen', 'Identifier' =>
200 &addCmdHook("extra", 'dict', ('CODEREF' => 'Dict::Dict',
201 'Identifier' => 'dict', 'Help' => 'dict',
202 'Forker' => 1, 'Cmdstats' => 'Dict') );
203 &addCmdHook("extra", 'slashdot', ('CODEREF' => 'Slashdot::Slashdot',
204 'Identifier' => 'slashdot', 'Forker' => 1,
205 'Cmdstats' => 'Slashdot') );
206 &addCmdHook("extra", 'uptime', ('CODEREF' => 'uptime', 'Identifier' => 'uptime',
207 'Cmdstats' => 'Uptime') );
208 &addCmdHook("extra", 'nullski', ('CODEREF' => 'nullski', ) );
209 &addCmdHook("extra", '(fm|freshmeat)', ('CODEREF' => 'Freshmeat::Freshmeat',
210 'Identifier' => 'freshmeat', 'Cmdstats' => 'Freshmeat',
211 'Forker' => 1, 'Help' => 'freshmeat') );
212 &addCmdHook("extra", 'verstats', ('CODEREF' => 'do_verstats' ) );
215 ### END OF ADDING HOOKS.
217 &status("CMD: loaded ".scalar(keys %hooks_extra)." EXTRA command hooks.");
220 if (!defined $message) {
221 &WARN("Modules: message is undefined. should never happen.");
225 # babel bot: Jonathan Feinberg++
226 if (&IsChanConf("babelfish") and $message =~ m{
228 (?:babel(?:fish)?|x|xlate|translate)
230 (to|from) # direction of translation (through)
232 ($babel_lang_regex)\w* # which language?
234 (.+) # The phrase to be translated
237 &Forker("babelfish", sub { &babel::babelfish(lc $1, lc $2, $3); } );
239 $cmdstats{'BabelFish'}++;
243 if (&IsChanConf("debian")) {
244 my $debiancmd = 'conflicts?|depends?|desc|file|info|provides?';
245 $debiancmd .= '|recommends?|suggests?|maint|maintainer';
246 if ($message =~ /^($debiancmd)(\s+(.*))?$/i) {
249 if (defined $package) {
250 &Forker("debian", sub { &Debian::infoPackages($1, $package); } );
259 # google searching. Simon++
260 if (&IsChanConf("wwwsearch") and $message =~ /^(?:search\s+)?(\S+)\s+for\s+['"]?(.*?)['"]?\s*\?*$/i) {
261 return unless (&hasParam("wwwsearch"));
263 &Forker("wwwsearch", sub { &W3Search::W3Search($1,$2); } );
265 $cmdstats{'WWWSearch'}++;
269 # list{keys|values}. xk++. Idea taken from #linuxwarez@EFNET
270 if ($message =~ /^list(\S+)( (.*))?$/i) {
271 return unless (&hasParam("search"));
273 my $thiscmd = lc($1);
276 # suggested by asuffield nad \broken.
277 if ($args =~ /^["']/ and $args =~ /["']$/) {
278 &DEBUG("list*: removed quotes.");
279 $args =~ s/^["']|["']$//g;
282 $thiscmd =~ s/^vals$/values/;
283 return if ($thiscmd ne "keys" && $thiscmd ne "values");
286 if (!defined $args) {
287 &help("list". $thiscmd);
291 if (length $args == 1) {
292 &msg($who,"search string is too short.");
296 &Forker("search", sub { &Search::Search($thiscmd, $args); } );
298 $cmdstats{'Factoid Search'}++;
302 # Nickometer. Adam Spiers++
303 if ($message =~ /^(?:lame|nick)ometer(?: for)? (\S+)/i) {
304 return unless (&hasParam("nickometer"));
306 my $term = (lc $1 eq 'me') ? $who : $1;
308 &loadMyModule($myModules{'nickometer'});
310 if ($term =~ /^$mask{chan}$/) {
311 &status("Doing nickometer for chan $term.");
313 if (!&validChan($term)) {
314 &msg($who, "error: channel is invalid.");
320 foreach (keys %{ $channels{lc $term}{''} }) {
323 &WARN("nickometer: nick in chan $term undefined?");
327 my $value = &nickometer($str);
328 $nickometer{$value}{$str} = 1;
332 ### TODO: compact with map?
334 foreach (sort {$b <=> $a} keys %nickometer) {
335 my $str = join(", ", sort keys %{ $nickometer{$_} });
336 push(@list, "$str ($_%)");
339 &pSReply( &formListReply(0, "Nickometer list for $term ", @list) );
345 my $percentage = &nickometer($term);
347 if ($percentage =~ /NaN/) {
348 $percentage = "off the scale";
350 $percentage = sprintf("%0.4f", $percentage);
351 $percentage =~ s/\.?0+$//;
355 if ($msgType eq 'public') {
356 &say("'$term' is $percentage lame, $who");
358 &msg($who, "the 'lame nick-o-meter' reading for $term is $percentage, $who");
364 # Topic management. xk++
365 # may want to add a flag(??) for topic in the near future. -xk
366 if ($message =~ /^topic(\s+(.*))?$/i) {
367 return unless (&hasParam("topic"));
369 my $chan = $talkchannel;
370 my @args = split / /, $2 || "";
373 &msg($who,"Try 'help topic'");
377 $chan = lc(shift @args) if ($msgType eq 'private');
378 my $thiscmd = shift @args;
381 if ($msgType eq 'public' && $thiscmd =~ /^#/) {
382 &msg($who, "error: channel argument is not required.");
383 &msg($who, "\002Usage\002: topic <CMD>");
387 # topic over private:
388 if ($msgType eq 'private' && $chan !~ /^#/) {
389 &msg($who, "error: channel argument is required.");
390 &msg($who, "\002Usage\002: topic #channel <CMD>");
394 if (&validChan($chan) == 0) {
395 &msg($who,"error: invalid channel \002$chan\002");
399 # for semi-outsiders.
400 if (!&IsNickInChan($who,$chan)) {
401 &msg($who, "Failed. You ($who) are not in $chan, hey?");
406 &loadMyModule($myModules{'topic'});
407 &Topic($chan, $thiscmd, join(' ', @args));
408 $cmdstats{'Topic'}++;
413 if ($message =~ /^wingate$/i) {
414 return unless (&hasParam("wingate"));
416 my $reply = "Wingate statistics: scanned \002"
417 .scalar(keys %wingate)."\002 hosts";
418 my $queue = scalar(keys %wingateToDo);
420 $reply .= ". I have \002$queue\002 hosts in the queue";
421 $reply .= ". Started the scan ".&Time2String(time() - $wingaterun)." ago";
424 &performStrictReply("$reply.");
429 # do nothing and let the other routines have a go
437 if (!defined $query) {
439 &msg($who, "I have \002".&countKeys("freshmeat")."\002 entries.");
443 &Freshmeat::Freshmeat($query);
449 &msg($who, "- Uptime for $ident -");
450 &msg($who, "Now: ". &Time2String(&uptimeNow()) ." running $bot_version");
452 foreach (&uptimeGetInfo()) {
454 my $time = &Time2String($1);
457 &msg($who, "$count: $time $2");
466 if (!defined $person or $person =~ /^$/) {
469 my $i = &countKeys("seen");
470 &msg($who,"there ". &fixPlural("is",$i) ." \002$i\002 ".
471 "seen ". &fixPlural("entry",$i) ." that I know of.");
479 &seenFlush(); # very evil hack. oh well, better safe than sorry.
481 ### TODO: Support &dbGetRowInfo(); like in &FactInfo();
482 my $select = "nick,time,channel,host,message";
483 if ($person eq "random") {
484 @seen = &randKey("seen", $select);
486 @seen = &dbGet("seen", "nick", $person, $select);
489 if (scalar @seen < 2) {
491 &DEBUG("seen: _ => '$_'.");
493 &performReply("i haven't seen '$person'");
499 ### TODO: multi channel support. may require &IsNick() to return
500 ### all channels or something.
501 my @chans = &GetNickInChans($seen[0]);
503 $reply = "$seen[0] is currently on";
507 next unless (exists $userstats{lc $seen[0]}{'Join'});
508 $reply .= " (".&Time2String(time() - $userstats{lc $seen[0]}{'Join'}).")";
511 if (&IsParam("seenStats")) {
513 $i = $userstats{lc $seen[0]}{'Count'};
514 $reply .= ". Has said a total of \002$i\002 messages" if (defined $i);
515 $i = $userstats{lc $seen[0]}{'Time'};
516 $reply .= ". Is idling for ".&Time2String(time() - $i) if (defined $i);
519 my $howlong = &Time2String(time() - $seen[1]);
520 $reply = "$seen[0] <$seen[3]> was last seen on IRC ".
521 "in channel $seen[2], $howlong ago, ".
522 "saying\002:\002 '$seen[4]'.";
525 &performStrictReply($reply);
529 # User Information Services. requested by Flugh.
531 my ($arg) = join(' ',@_);
533 if ($arg =~ /^set(\s+(.*))?$/i) {
536 &help("userinfo set");
540 &UserInfoSet(split /\s+/, $arg, 2);
541 } elsif ($arg =~ /^unset(\s+(.*))?$/i) {
544 &help("userinfo unset");
548 &UserInfoSet($arg, "");
554 # cookie (random). xk++
558 # lets find that secret cookie.
559 my $target = ($msgType ne 'public') ? $who : $talkchannel;
560 my $cookiemsg = &getRandom(keys %{ $lang{'cookie'} });
563 ### WILL CHEW TONS OF MEM.
564 ### TODO: convert this to a Forker function!
566 my @list = &searchTable("factoids", "factoid_key", "factoid_value", $arg);
567 $key = &getRandom(@list);
568 $val = &getFactInfo("factoids", $key, "factoid_value");
570 ($key,$value) = &randKey("factoids","factoid_key,factoid_value");
574 s/##KEY/\002$key\002/;
577 s/\$who/$who/; # cheap fix.
578 s/(\S+)?\s*<\S+>/$1 /;
582 if ($cookiemsg =~ s/^ACTION //i) {
583 &action($target, $cookiemsg);
585 &msg($target, $cookiemsg);
590 my $arg = join(' ',@_);
591 my ($from,$to) = ('','');
593 ($from,$to) = ($1,$2) if ($arg =~ /^(.*?) to (.*)$/i);
594 ($from,$to) = ($2,$1) if ($arg =~ /^(.*?) from (.*)$/i);
596 if (!$to or !$from) {
597 &msg($who, "Invalid format!");
602 &Units::convertUnits($from, $to);
608 my ($target) = &fixString($_[0]);
610 my $chan = $talkchannel;
612 if ($msgType eq 'private') {
613 if ($target =~ /^($mask{chan})\s+(.*)$/) {
618 &msg($who, "error: invalid format or missing arguments.");
624 my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.lart");
626 if ($target =~ /^(me|you|itself|\Q$ident\E)$/i) {
627 $line =~ s/WHO/$who/g;
629 $line =~ s/WHO/$target/g;
631 $line .= ", courtesy of $who" if ($extra);
633 &action($chan, $line);
635 &status("lart: error reading file?");
640 my $idx = "debian/Packages-woody.idx";
645 $error++ unless ( -e $idx);
646 $error++ unless ( -e "$idx-old");
649 $error = "no woody/woody-old index file found.";
650 &ERROR("Debian: $error");
656 open(IDX2, "$idx-old");
670 next if (exists $pkg{$_});
676 &::performStrictReply( &::formListReply(0, "New debian packages:", @new) );
682 if (!defined $chan) {
687 if (!&validChan($chan)) {
688 &msg($who, "chan $chan is invalid.");
692 if (scalar @vernick > scalar(keys %{ $channels{lc $chan}{''} })/4) {
693 &msg($who, "verstats already in progress for someone else.");
697 &msg($who, "Sending CTCP VERSION...");
698 $conn->ctcp("VERSION", $chan);
699 $cache{verstats}{chan} = $chan;
700 $cache{verstats}{who} = $who;
701 $cache{verstats}{msgType} = $msgType;
703 $conn->schedule(60, sub {
705 my $c = lc $cache{verstats}{chan};
706 my $total = keys %{ $channels{$c}{''} };
708 $who = $cache{verstats}{who};
709 $msgType = $cache{verstats}{msgType};
710 delete $cache{verstats}; # sufficient?
712 foreach (keys %ver) {
713 $vtotal += scalar keys %{ $ver{$_} };
717 my $unknown = $total - $vtotal;
718 my $perc = sprintf("%.1f", $unknown * 100 / $total);
720 $sorted{$perc}{"unknown/cloak"} = "$unknown ($perc%)";
722 foreach (keys %ver) {
723 my $count = scalar keys %{ $ver{$_} };
724 $perc = sprintf("%.01f", $count * 100 / $total);
725 $perc =~ s/.0$//; # lame compression.
727 $sorted{$perc}{$_} = "$count ($perc%)";
730 ### can be compressed to a map?
732 foreach ( sort { $b <=> $a } keys %sorted ) {
734 foreach (sort keys %{ $sorted{$perc} }) {
735 push(@list, "$_ - $sorted{$perc}{$_}");
739 &pSReply( &formListReply(0, "IRC Client versions for $c ", @list) );
741 # clean up not-needed data structures.
749 sub nullski { my ($arg) = @_; return unless (defined $arg);
750 foreach (`$arg`) { &msg($who,$_); } }