]> git.donarmstrong.com Git - infobot.git/blob - src/CommandStubs.pl
96ccce3b2eabe02c37126b714d6da9e95d00c28b
[infobot.git] / src / CommandStubs.pl
1 #
2 # User Command Extension Stubs
3 #
4
5 if (&IsParam("useStrict")) { use strict; }
6
7 $babel::lang_regex = "";        # lame fix.
8
9 ### PROPOSED COMMAND HOOK IMPLEMENTATION.
10 # addCmdHook("SECTION", 'TEXT_HOOK',
11 #       (CODEREF        => 'Blah', 
12 #       Forker          => 1,
13 #       CheckModule     => 1,                   # ???
14 #       Module          => 'blah.pl'            # preload module.
15 #       Identifier      => 'config_label',      # change to Config?
16 #       Help            => 'help_label',
17 #       Cmdstats        => 'text_label',)
18 #}
19 ###
20
21 sub addCmdHook {
22     my ($hashname, $ident, %hash) = @_;
23
24     if (exists ${"hooks_$hashname"}{$ident}) {
25 ###     &WARN("aCH: cmd hooks \%$hashname{$ident} already exists.");
26         return;
27     }
28
29     &VERB("aCH: added $ident",2);       # use $hash{'Identifier'}?
30     ### hrm... prevent warnings?
31     ${"hooks_$hashname"}{$ident} = \%hash;
32 }
33
34 # RUN IF ADDRESSED.
35 sub parseCmdHook {
36     my ($hashname, $line) = @_;
37     $line =~ /^(\S+)( (.*))?$/;
38     my @args    = split(' ', $3 || '');
39     my $flatarg = $3;
40     my $cmd     = $1;   # command name is whitespaceless.
41     my $done    = 0;
42
43     &shmFlush();
44
45     if (!defined %{"hooks_$hashname"}) {
46         &WARN("cmd hooks \%$hashname does not exist.");
47         return 0;
48     }
49
50     foreach (keys %{"hooks_$hashname"}) {
51         my $ident = $_;
52
53         next unless ($cmd =~ /^$ident$/i);
54
55         if ($done) {
56             &WARN("pCH: Multiple hook match: $ident");
57             next;
58         }
59
60         &DEBUG("pCH(hooks_$hashname): $cmd matched $ident");
61         my %hash = %{ ${"hooks_$hashname"}{$ident} };
62
63         if (!scalar keys %hash) {
64             &WARN("CmdHook: hash is NULL?");
65             return 1;
66         }
67
68         if (!exists $hash{CODEREF}) {
69             &ERROR("CODEREF undefined for $cmd or $ident.");
70             return 1;
71         }
72
73         ### DEBUG.
74         foreach (keys %hash) {
75             &DEBUG(" $cmd->$_ => '$hash{$_}'.");
76         }
77
78         ### HELP.
79         if (exists $hash{'Help'} and !scalar(@args)) {
80             &help( $hash{'Help'} );
81             return 1;
82         }
83
84         ### IDENTIFIER.
85         if (exists $hash{'Identifier'}) {
86             return unless (&hasParam($hash{'Identifier'}));
87         }
88
89         ### USER FLAGS.
90         if (exists $hash{'UserFlag'}) {
91             return unless (&hasFlag($hash{'UserFlag'}));
92         }
93
94         ### FORKER,IDENTIFIER,CODEREF.
95         if (exists $hash{'Forker'}) {
96             $hash{'Identifier'} .= "-" if ($hash{'Forker'} eq "NULL");
97
98             ### FLAT_ARG / ARRAY option.
99
100             &Forker($hash{'Identifier'}, sub { \&{$hash{'CODEREF'}}(@args) } );
101         } else {
102             if (exists $hash{'Module'}) {
103                 &loadMyModule($myModules{ $hash{'Module'} });
104             }
105
106             ### TODO: check if CODEREF exists.
107
108             if (exists $hash{'FlatArg'} and $hash{'FlatArg'} == 0) {
109                 &status("CmdHook: using args as array.");
110                 &{$hash{'CODEREF'}}(@args);
111             } else {
112                 &{$hash{'CODEREF'}}($flatarg);
113             }
114         }
115
116         ### CMDSTATS.
117         if (exists $hash{'Cmdstats'}) {
118             $cmdstats{ $hash{'Cmdstats'} }++;
119         }
120
121         &DEBUG("pCH: ended.");
122
123         $done = 1;
124     }
125
126     return 1 if ($done);
127     return 0;
128 }
129
130 ###
131 ### START ADDING HOOKS.
132 ###
133 &addCmdHook("extra", 'd?bugs', ('CODEREF' => 'debianBugs',
134         'Forker' => 1, 'Identifier' => 'debianExtra',
135         'Cmdstats' => 'Debian Bugs') );
136 &addCmdHook("extra", 'dauthor', ('CODEREF' => 'Debian::searchAuthor',
137         'Forker' => 1, 'Identifier' => 'debian',
138         'Cmdstats' => 'Debian Author Search', 'Help' => "dauthor" ) );
139 &addCmdHook("extra", '(d|search)desc', ('CODEREF' => 'Debian::searchDesc',
140         'Forker' => 1, 'Identifier' => 'debian',
141         'Cmdstats' => 'Debian Desc Search', 'Help' => "ddesc" ) );
142 &addCmdHook("extra", 'dnew', ('CODEREF' => 'DebianNew',
143         'Identifier' => 'debian' ) );
144 &addCmdHook("extra", 'dincoming', ('CODEREF' => 'Debian::generateIncoming',
145         'Forker' => 1, 'Identifier' => 'debian' ) );
146 &addCmdHook("extra", 'dstats', ('CODEREF' => 'Debian::infoStats',
147         'Forker' => 1, 'Identifier' => 'debian',
148         'Cmdstats' => 'Debian Statistics' ) );
149 &addCmdHook("extra", 'd?contents', ('CODEREF' => 'Debian::searchContents',
150         'Forker' => 1, 'Identifier' => 'debian',
151         'Cmdstats' => 'Debian Contents Search', 'Help' => "contents" ) );
152 &addCmdHook("extra", 'd?find', ('CODEREF' => 'Debian::DebianFind',
153         'Forker' => 1, 'Identifier' => 'debian',
154         'Cmdstats' => 'Debian Search', 'Help' => "find" ) );
155 &addCmdHook("extra", 'insult', ('CODEREF' => 'Insult::Insult',
156         'Forker' => 1, 'Identifier' => 'insult', 'Help' => "insult" ) );
157 &addCmdHook("extra", 'kernel', ('CODEREF' => 'Kernel::Kernel',
158         'Forker' => 1, 'Identifier' => 'kernel',
159         'Cmdstats' => 'Kernel') );
160 &addCmdHook("extra", 'listauth', ('CODEREF' => 'CmdListAuth',
161         'Identifier' => 'search', Module => 'factoids', 
162         'Help' => 'listauth') );
163 &addCmdHook("extra", 'quote', ('CODEREF' => 'Quote::Quote',
164         'Forker' => 1, 'Identifier' => 'quote',
165         'Help' => 'quote', 'Cmdstats' => 'Quote') );
166 &addCmdHook("extra", 'countdown', ('CODEREF' => 'Countdown',
167         'Module' => 'countdown', 'Identifier' => 'countdown',
168         'Cmdstats' => 'Countdown') );
169 &addCmdHook("extra", 'lart', ('CODEREF' => 'lart',
170         'Identifier' => 'lart', 'Help' => 'lart') );
171 &addCmdHook("extra", 'convert', ('CODEREF' => 'convert',
172         'Forker' => 1, 'Identifier' => 'units',
173         'Help' => 'convert') );
174 &addCmdHook("extra", '(cookie|random)', ('CODEREF' => 'cookie',
175         'Forker' => 1, 'Identifier' => 'factoids') );
176 &addCmdHook("extra", 'u(ser)?info', ('CODEREF' => 'userinfo',
177         'Identifier' => 'userinfo', 'Help' => 'userinfo',
178         'Module' => 'userinfo') );
179 &addCmdHook("extra", 'rootWarn', ('CODEREF' => 'CmdrootWarn',
180         'Identifier' => 'rootWarn', 'Module' => 'rootwarn') );
181 &addCmdHook("extra", 'seen', ('CODEREF' => 'seen', 'Identifier' =>
182         'seen') );
183 &addCmdHook("extra", 'dict', ('CODEREF' => 'Dict::Dict',
184         'Identifier' => 'dict', 'Help' => 'dict',
185         'Forker' => 1, 'Cmdstats' => 'Dict') );
186 &addCmdHook("extra", 'slashdot', ('CODEREF' => 'Slashdot::Slashdot',
187         'Identifier' => 'slashdot', 'Forker' => 1,
188         'Cmdstats' => 'Slashdot') );
189 &addCmdHook("extra", 'uptime', ('CODEREF' => 'uptime', 'Identifier' => 'uptime',
190         'Cmdstats' => 'Uptime') );
191 &addCmdHook("extra", 'nullski', ('CODEREF' => 'nullski', ) );
192 &addCmdHook("extra", 'crash', ('CODEREF' => 'crash' ) );
193 sub nullski { my ($arg) = @_; foreach (`$arg`) { &msg($who,$_); } }
194 &addCmdHook("extra", '(fm|freshmeat)', ('CODEREF' => 'Freshmeat::Freshmeat',
195         'Identifier' => 'freshmeat', 'Cmdstats' => 'Freshmeat',
196         'Forker' => 1, 'Help' => 'freshmeat') );
197 ###
198 ### END OF ADDING HOOKS.
199 ###
200 &status("CMD: loaded ".scalar(keys %hooks_extra)." EXTRA command hooks.");
201
202 sub Modules {
203     if (!defined $message) {
204         &WARN("Modules: message is undefined. should never happen.");
205         return;
206     }
207
208     # babel bot: Jonathan Feinberg++
209     if (&IsParam("babelfish") and $message =~ m{
210                 ^\s*
211                 (?:babel(?:fish)?|x|xlate|translate)
212                 \s+
213                 (to|from)               # direction of translation (through)
214                 \s+
215                 ($babel::lang_regex)\w* # which language?
216                 \s*
217                 (.+)                    # The phrase to be translated
218         }xoi) {
219
220         &Forker("babelfish", sub { &babel::babelfish(lc $1, lc $2, $3); } );
221
222         $cmdstats{'BabelFish'}++;
223         return;
224     }
225
226     if (&IsParam("debian")) {
227         my $debiancmd    = 'conflicts?|depends?|desc|file|info|provides?';
228         $debiancmd      .= '|recommends?|suggests?|maint|maintainer';
229         if ($message =~ /^($debiancmd)(\s+(.*))?$/i) {
230             my $package = lc $3;
231
232             if (defined $package) {
233                 &Forker("debian", sub { &Debian::infoPackages($1, $package); } );
234             } else {
235                 &help($1);
236             }
237
238             return;
239         }
240     }
241
242     # google searching. Simon++
243     if (&IsParam("wwwsearch") and $message =~ /^(?:search\s+)?(\S+)\s+for\s+['"]?(.*?)['"]?\s*\?*$/i) {
244         return unless (&hasParam("wwwsearch"));
245
246         &Forker("wwwsearch", sub { &W3Search::W3Search($1,$2); } );
247
248         $cmdstats{'WWWSearch'}++;
249         return;
250     }
251
252     # list{keys|values}. xk++. Idea taken from #linuxwarez@EFNET
253     if ($message =~ /^list(\S+)( (.*))?$/i) {
254         return unless (&hasParam("search"));
255
256         my $thiscmd     = lc($1);
257         my $args        = $3;
258
259         $thiscmd =~ s/^vals$/values/;
260         return if ($thiscmd ne "keys" && $thiscmd ne "values");
261
262         # Usage:
263         if (!defined $args) {
264             &help("list". $thiscmd);
265             return;
266         }
267
268         if (length $args == 1) {
269             &msg($who,"search string is too short.");
270             return;
271         }
272
273         &Forker("search", sub { &Search::Search($thiscmd, $args); } );
274
275         $cmdstats{'Factoid Search'}++;
276         return;
277     }
278
279     # Nickometer. Adam Spiers++
280     if ($message =~ /^(?:lame|nick)ometer(?: for)? (\S+)/i) {
281         return unless (&hasParam("nickometer"));
282
283         my $term = (lc $1 eq 'me') ? $who : $1;
284
285         &loadMyModule($myModules{'nickometer'});
286
287         if ($term =~ /^$mask{chan}$/) {
288             &DEBUG("nickometer: term == chan.");
289
290             if (!&validChan($term)) {
291                 &msg($who, "error: channel is invalid.");
292                 return;
293             }
294
295             # step 1.
296             my %nickometer;
297             foreach (keys %{ $channels{lc $term}{''} }) {
298                 if (!defined $_) {
299                     &WARN("nickometer: _ is undefined.");
300                     next;
301                 }
302
303                 my $value = &nickometer($_);
304                 $nickometer{$value}{$_} = 1;
305             }
306             # step 2.
307             ### TODO: compact with map?
308             my @list;
309             foreach (sort {$a <=> $b} keys %nickometer) {
310                 my $str = join(", ", sort keys %{$nickometer{$_}});
311                 push(@list, "$str ($_)");
312             }
313
314             &pSReply( &formListReply(0, "Nickometer list for $term ", @list) );
315
316             return;
317         }
318
319         my $percentage = &nickometer($term);
320
321         if ($percentage =~ /NaN/) {
322             $percentage = "off the scale";
323         } else {
324             $percentage = sprintf("%0.4f", $percentage);
325             $percentage =~ s/\.?0+$//;
326             $percentage .= '%';
327         }
328
329         if ($msgType eq 'public') {
330             &say("'$term' is $percentage lame, $who");
331         } else {
332             &msg($who, "the 'lame nick-o-meter' reading for $term is $percentage, $who");
333         }
334
335         return;
336     }
337
338     # Topic management. xk++
339     # may want to add a flag(??) for topic in the near future. -xk
340     if ($message =~ /^topic(\s+(.*))?$/i) {
341         return unless (&hasParam("topic"));
342
343         my $chan        = $talkchannel;
344         my @args        = split(/ /, $2);
345
346         if (!scalar @args) {
347             &msg($who,"Try 'help topic'");
348             return;
349         }
350
351         $chan           = lc(shift @args) if ($msgType eq 'private');
352         my $thiscmd     = shift @args;
353
354         # topic over public:
355         if ($msgType eq 'public' && $thiscmd =~ /^#/) {
356             &msg($who, "error: channel argument is not required.");
357             &msg($who, "\002Usage\002: topic <CMD>");
358             return;
359         }
360
361         # topic over private:
362         if ($msgType eq 'private' && $chan !~ /^#/) {
363             &msg($who, "error: channel argument is required.");
364             &msg($who, "\002Usage\002: topic #channel <CMD>");
365             return;
366         }
367
368         if (&validChan($chan) == 0) {
369             &msg($who,"error: invalid channel \002$chan\002");
370             return;
371         }
372
373         # for semi-outsiders.
374         if (!&IsNickInChan($who,$chan)) {
375             &msg($who, "Failed. You ($who) are not in $chan, hey?");
376             return;
377         }
378
379         # now lets do it.
380         &loadMyModule($myModules{'topic'});
381         &Topic($chan, $thiscmd, join(' ', @args));
382         $cmdstats{'Topic'}++;
383         return;
384     }
385
386     # wingate.
387     if ($message =~ /^wingate$/i) {
388         return unless (&hasParam("wingate"));
389
390         my $reply = "Wingate statistics: scanned \002"
391                         .scalar(keys %wingate)."\002 hosts";
392         my $queue = scalar(keys %wingateToDo);
393         if ($queue) {
394             $reply .= ".  I have \002$queue\002 hosts in the queue";
395             $reply .= ".  Started the scan ".&Time2String(time() - $wingaterun)." ago";
396         }
397
398         &performStrictReply("$reply.");
399
400         return;
401     }
402
403     # do nothing and let the other routines have a go
404     return "CONTINUE";
405 }
406
407 # Freshmeat. xk++
408 sub freshmeat {
409     my ($query) = @_;
410
411     if (!defined $query) {
412         &help("freshmeat");
413         &msg($who, "I have \002".&countKeys("freshmeat")."\002 entries.");
414         return;
415     }
416
417     &Freshmeat::Freshmeat($query);
418 }
419
420 # Uptime. xk++
421 sub uptime {
422     my $count = 1;
423     &msg($who, "- Uptime for $ident -");
424     &msg($who, "Now: ". &Time2String(&uptimeNow()) ." running $bot_version");
425
426     foreach (&uptimeGetInfo()) {
427         /^(\d+)\.\d+ (.*)/;
428         my $time = &Time2String($1);
429         my $info = $2;
430
431         &msg($who, "$count: $time $2");
432         $count++;
433     }
434 }
435
436 # seen.
437 sub seen {
438     my($person) = @_;
439
440     if (!defined $person) {
441         &help("seen");
442
443         my $i = &countKeys("seen");
444         &msg($who,"there ". &fixPlural("is",$i) ." \002$i\002 ".
445                 "seen ". &fixPlural("entry",$i) ." that I know of.");
446
447         return;
448     }
449
450     my @seen;
451     $person =~ s/\?*$//;
452
453     &seenFlush();       # very evil hack. oh well, better safe than sorry.
454
455     ### TODO: Support &dbGetRowInfo(); like in &FactInfo();
456     my $select = "nick,time,channel,host,message";
457     if ($person eq "random") {
458         @seen = &randKey("seen", $select);
459     } else {
460         @seen = &dbGet("seen", "nick", $person, $select);
461     }
462
463     if (scalar @seen < 2) {
464         foreach (@seen) {
465             &DEBUG("seen: _ => '$_'.");
466         }
467         &performReply("i haven't seen '$person'");
468         return;
469     }
470
471     # valid seen.
472     my $reply;
473     ### TODO: multi channel support. may require &IsNick() to return
474     ### all channels or something.
475     my @chans = &GetNickInChans($seen[0]);
476     if (scalar @chans) {
477         $reply = "$seen[0] is currently on";
478
479         foreach (@chans) {
480             $reply .= " ".$_;
481             next unless (exists $userstats{lc $seen[0]}{'Join'});
482             $reply .= " (".&Time2String(time() - $userstats{lc $seen[0]}{'Join'}).")";
483         }
484
485         if (&IsParam("seenStats")) {
486             my $i;
487             $i = $userstats{lc $seen[0]}{'Count'};
488             $reply .= ".  Has said a total of \002$i\002 messages" if (defined $i);
489             $i = $userstats{lc $seen[0]}{'Time'};
490             $reply .= ".  Is idling for ".&Time2String(time() - $i) if (defined $i);
491         }
492     } else {
493         my $howlong = &Time2String(time() - $seen[1]);
494         $reply = "$seen[0] <$seen[3]> was last seen on IRC ".
495                  "in channel $seen[2], $howlong ago, ".
496                  "saying\002:\002 '$seen[4]'.";
497     }
498
499     &performStrictReply($reply);
500     return;
501 }
502
503 # User Information Services. requested by Flugh.
504 sub userinfo {
505     my ($arg) = join(' ',@_);
506
507     if ($arg =~ /^set(\s+(.*))?$/i) {
508         $arg = $2;
509         if (!defined $arg) {
510             &help("userinfo set");
511             return;
512         }
513
514         &UserInfoSet(split /\s+/, $arg, 2);
515     } elsif ($arg =~ /^unset(\s+(.*))?$/i) {
516         $arg = $2;
517         if (!defined $arg) {
518             &help("userinfo unset");
519             return;
520         }
521
522         &UserInfoSet($arg, "");
523     } else {
524         &UserInfoGet($arg);
525     }
526 }
527
528 # cookie (random). xk++
529 sub cookie {
530     my ($arg) = @_;
531
532     # lets find that secret cookie.
533     my $target          = ($msgType ne 'public') ? $who : $talkchannel;
534     my $cookiemsg       = &getRandom(keys %{$lang{'cookie'}});
535     my ($key,$value);
536
537     ### WILL CHEW TONS OF MEM.
538     ### TODO: convert this to a Forker function!
539     if ($arg) {
540         my @list = &searchTable("factoids", "factoid_key", "factoid_value", $arg);
541         $key  = &getRandom(@list);
542         $val  = &getFactInfo("factoids", $key, "factoid_value");
543     } else {
544         ($key,$value) = &randKey("factoids","factoid_key,factoid_value");
545     }
546
547     for ($cookiemsg) {
548         s/##KEY/\002$key\002/;
549         s/##VALUE/$value/;
550         s/##WHO/$who/;
551         s/\$who/$who/;  # cheap fix.
552         s/(\S+)?\s*<\S+>/$1 /;
553         s/\s+/ /g;
554     }
555
556     if ($cookiemsg =~ s/^ACTION //i) {
557         &action($target, $cookiemsg);
558     } else {
559         &msg($target, $cookiemsg);
560     }
561 }
562
563 sub convert {
564     my $arg = join(' ',@_);
565     my ($from,$to) = ('','');
566
567     ($from,$to) = ($1,$2) if ($arg =~ /^(.*?) to (.*)$/i);
568     ($from,$to) = ($2,$1) if ($arg =~ /^(.*?) from (.*)$/i);
569
570     if (!$to or !$from) {
571         &msg($who, "Invalid format!");
572         &help("convert");
573         return;
574     }
575
576     &Units::convertUnits($from, $to);
577
578     return;
579 }
580
581 sub lart {
582     my ($target) = &fixString($_[0]);
583     my $extra   = 0;
584     my $chan    = $talkchannel;
585
586     if ($msgType eq 'private') {
587         if ($target =~ /^($mask{chan})\s+(.*)$/) {
588             $chan       = $1;
589             $target     = $2;
590             $extra      = 1;
591         } else {
592             &msg($who, "error: invalid format or missing arguments.");
593             &help("lart");
594             return;
595         }
596     }
597
598     my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.lart");
599     if (defined $line) {
600         if ($target =~ /^(me|you|itself|\Q$ident\E)$/i) {
601             $line =~ s/WHO/$who/g;
602         } else {
603             $line =~ s/WHO/$target/g;
604         }
605         $line .= ", courtesy of $who" if ($extra);
606
607         &action($chan, $line);
608     } else {
609         &status("lart: error reading file?");
610     }
611 }
612
613 sub DebianNew {
614     my $idx   = "debian/Packages-woody.idx";
615     my $error = 0;
616     my %pkg;
617     my @new;
618
619     $error++ unless ( -e $idx);
620     $error++ unless ( -e "$idx-old");
621
622     if ($error) {
623         $error = "no woody/woody-old index file found.";
624         &ERROR("Debian: $error");
625         &msg($who, $error);
626         return;
627     }
628
629     open(IDX1, $idx);
630     open(IDX2, "$idx-old");
631
632     while (<IDX2>) {
633         chop;
634         next if (/^\*/);
635
636         $pkg{$_} = 1;
637     }
638     close IDX2;
639
640     open(IDX1,$idx);
641     while (<IDX1>) {
642         chop;
643         next if (/^\*/);
644         next if (exists $pkg{$_});
645
646         push(@new);
647     }
648     close IDX1;
649
650     &::performStrictReply( &::formListReply(0, "New debian packages:", @new) );
651 }
652
653 1;