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