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