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