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