]> git.donarmstrong.com Git - infobot.git/blob - src/CommandStubs.pl
Removed W3 regex
[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         &DEBUG("wwwsearch: 1 => $1");
200         &DEBUG("wwwsearch: 2 => $2");
201         &DEBUG("wwwsearch: 3 => $3");
202
203         &Forker("wwwsearch", sub { &W3Search::W3Search($1,$2,$3); } );
204
205         $cmdstats{'WWWSearch'}++;
206         return $noreply;
207     }
208
209     # list{keys|values}. xk++. Idea taken from #linuxwarez@EFNET
210     if ($message =~ /^list(\S+)( (.*))?$/i) {
211         return $noreply unless (&hasParam("search"));
212
213         my $thiscmd     = lc($1);
214         my $args        = $3;
215
216         $thiscmd =~ s/^vals$/values/;
217         return $noreply if ($thiscmd ne "keys" && $thiscmd ne "values");
218
219         # Usage:
220         if (!defined $args) {
221             &help("list". $thiscmd);
222             return $noreply;
223         }
224
225         if (length $args == 1) {
226             &msg($who,"search string is too short.");
227             return $noreply;
228         }
229
230         &Forker("search", sub { &Search::Search($thiscmd, $args); } );
231
232         $cmdstats{'Factoid Search'}++;
233         return $noreply;
234     }
235
236     # Nickometer. Adam Spiers++
237     if ($message =~ /^(?:lame|nick)ometer(?: for)? (\S+)/i) {
238         return $noreply unless (&hasParam("nickometer"));
239
240         my $term = (lc $1 eq 'me') ? $who : $1;
241         $term =~ s/\?+\s*//;
242
243         &loadMyModule($myModules{'nickometer'});
244         my $percentage = &nickometer($term);
245
246         if ($percentage =~ /NaN/) {
247             $percentage = "off the scale";
248         } else {
249             $percentage = sprintf("%0.4f", $percentage);
250             $percentage =~ s/\.?0+$//;
251             $percentage .= '%';
252         }
253
254         if ($msgType eq 'public') {
255             &say("'$term' is $percentage lame, $who");
256         } else {
257             &msg($who, "the 'lame nick-o-meter' reading for $term is $percentage, $who");
258         }
259
260         return $noreply;
261     }
262
263     # Topic management. xk++
264     # may want to add a flag(??) for topic in the near future. -xk
265     if ($message =~ /^topic(\s+(.*))?$/i) {
266         return $noreply unless (&hasParam("topic"));
267
268         my $chan        = $talkchannel;
269         my @args        = split(/ /, $2);
270
271         if (!scalar @args) {
272             &msg($who,"Try 'help topic'");
273             return $noreply;
274         }
275
276         $chan           = lc(shift @args) if ($msgType eq 'private');
277         my $thiscmd     = shift @args;
278
279         # topic over public:
280         if ($msgType eq 'public' && $thiscmd =~ /^#/) {
281             &msg($who, "error: channel argument is not required.");
282             &msg($who, "\002Usage\002: topic <CMD>");
283             return $noreply;
284         }
285
286         # topic over private:
287         if ($msgType eq 'private' && $chan !~ /^#/) {
288             &msg($who, "error: channel argument is required.");
289             &msg($who, "\002Usage\002: topic #channel <CMD>");
290             return $noreply;
291         }
292
293         if (&validChan($chan) == 0) {
294             &msg($who,"error: invalid channel \002$chan\002");
295             return $noreply;
296         }
297
298         # for semi-outsiders.
299         if (!&IsNickInChan($who,$chan)) {
300             &msg($who, "Failed. You ($who) are not in $chan, hey?");
301             return $noreply;
302         }
303
304         # now lets do it.
305         &loadMyModule($myModules{'topic'});
306         &Topic($chan, $thiscmd, join(' ', @args));
307         $cmdstats{'Topic'}++;
308         return $noreply;
309     }
310
311     # wingate.
312     if ($message =~ /^wingate$/i) {
313         return $noreply unless (&hasParam("wingate"));
314
315         my $reply = "Wingate statistics: scanned \002"
316                         .scalar(keys %wingate)."\002 hosts";
317         my $queue = scalar(keys %wingateToDo);
318         if ($queue) {
319             $reply .= ".  I have \002$queue\002 hosts in the queue";
320             $reply .= ".  Started the scan ".&Time2String(time() - $wingaterun)." ago";
321         }
322
323         &performStrictReply("$reply.");
324
325         return $noreply;
326     }
327
328     # do nothing and let the other routines have a go
329     return '';
330 }
331
332 # Freshmeat. xk++
333 sub freshmeat {
334     my ($query) = @_;
335
336     if (!defined $query) {
337         &help("freshmeat");
338         &msg($who, "I have \002".&countKeys("freshmeat")."\002 entries.");
339         return $noreply;
340     }
341
342     &Freshmeat::Freshmeat($query);
343 }
344
345 # Uptime. xk++
346 sub uptime {
347     my $count = 1;
348     &msg($who, "- Uptime for $ident -");
349     &msg($who, "Now: ". &Time2String(&uptimeNow()) ." running $bot_version");
350
351     foreach (&uptimeGetInfo()) {
352         /^(\d+)\.\d+ (.*)/;
353         my $time = &Time2String($1);
354         my $info = $2;
355
356         &msg($who, "$count: $time $2");
357         $count++;
358     }
359 }
360
361 # seen.
362 sub seen {
363     my($person) = @_;
364
365     if (!defined $person) {
366         &help("seen");
367
368         my $i = &countKeys("seen");
369         &msg($who,"there ". &fixPlural("is",$i) ." \002$i\002 ".
370                 "seen ". &fixPlural("entry",$i) ." that I know of.");
371
372         return $noreply;
373     }
374
375     my @seen;
376     $person =~ s/\?*$//;
377
378     &seenFlush();       # very evil hack. oh well, better safe than sorry.
379
380     ### TODO: Support &dbGetRowInfo(); like in &FactInfo();
381     my $select = "nick,time,channel,host,message";
382     if ($person eq "random") {
383         @seen = &randKey("seen", $select);
384     } else {
385         @seen = &dbGet("seen", "nick", $person, $select);
386     }
387
388     if (scalar @seen < 2) {
389         foreach (@seen) {
390             &DEBUG("seen: _ => '$_'.");
391         }
392         &performReply("i haven't seen '$person'");
393         return $noreply;
394     }
395
396     # valid seen.
397     my $reply;
398     ### TODO: multi channel support. may require &IsNick() to return
399     ### all channels or something.
400     my @chans = &GetNickInChans($seen[0]);
401     if (scalar @chans) {
402         $reply = "$seen[0] is currently on";
403
404         foreach (@chans) {
405             $reply .= " ".$_;
406             next unless (exists $userstats{lc $seen[0]}{'Join'});
407             $reply .= " (".&Time2String(time() - $userstats{lc $seen[0]}{'Join'}).")";
408         }
409
410         if (&IsParam("seenStats")) {
411             my $i;
412             $i = $userstats{lc $seen[0]}{'Count'};
413             $reply .= ".  Has said a total of \002$i\002 messages" if (defined $i);
414             $i = $userstats{lc $seen[0]}{'Time'};
415             $reply .= ".  Is idling for ".&Time2String(time() - $i) if (defined $i);
416         }
417     } else {
418         my $howlong = &Time2String(time() - $seen[1]);
419         $reply = "$seen[0] <$seen[3]> was last seen on IRC ".
420                  "in channel $seen[2], $howlong ago, ".
421                  "saying\002:\002 '$seen[4]'.";
422     }
423
424     &performStrictReply($reply);
425     return $noreply;
426 }
427
428 # User Information Services. requested by Flugh.
429 sub userinfo {
430     my ($arg) = join(' ',@_);
431
432     if ($arg =~ /^set(\s+(.*))?$/i) {
433         $arg = $2;
434         if (!defined $arg) {
435             &help("userinfo set");
436             return $noreply;
437         }
438
439         &UserInfoSet(split /\s+/, $arg, 2);
440     } elsif ($arg =~ /^unset(\s+(.*))?$/i) {
441         $arg = $2;
442         if (!defined $arg) {
443             &help("userinfo unset");
444             return $noreply;
445         }
446
447         &UserInfoSet($arg, "");
448     } else {
449         &UserInfoGet($arg);
450     }
451 }
452
453 # cookie (random). xk++
454 sub cookie {
455     my ($arg) = @_;
456
457     # lets find that secret cookie.
458     my $target          = ($msgType ne 'public') ? $who : $talkchannel;
459     my $cookiemsg       = &getRandom(keys %{$lang{'cookie'}});
460     my ($key,$value);
461
462     ### WILL CHEW TONS OF MEM.
463     ### TODO: convert this to a Forker function!
464     if ($arg) {
465         my @list = &searchTable("factoids", "factoid_key", "factoid_value", $arg);
466         $key  = &getRandom(@list);
467         $val  = &getFactInfo("factoids", $key, "factoid_value");
468     } else {
469         ($key,$value) = &randKey("factoids","factoid_key,factoid_value");
470     }
471
472     for ($cookiemsg) {
473         s/##KEY/\002$key\002/;
474         s/##VALUE/$value/;
475         s/##WHO/$who/;
476         s/\$who/$who/;  # cheap fix.
477         s/(\S+)?\s*<\S+>/$1 /;
478         s/\s+/ /g;
479     }
480
481     if ($cookiemsg =~ s/^ACTION //i) {
482         &action($target, $cookiemsg);
483     } else {
484         &msg($target, $cookiemsg);
485     }
486 }
487
488 sub convert {
489     my $arg = join(' ',@_);
490     my ($from,$to) = ('','');
491
492     ($from,$to) = ($1,$2) if ($arg =~ /^(.*?) to (.*)$/i);
493     ($from,$to) = ($2,$1) if ($arg =~ /^(.*?) from (.*)$/i);
494
495     if (!$to or !$from) {
496         &msg($who, "Invalid format!");
497         &help("convert");
498         return $noreply;
499     }
500
501     &Units::convertUnits($from, $to);
502
503     return $noreply;
504 }
505
506 sub lart {
507     my ($target) = &fixString($_[0]);
508     my $extra   = 0;
509     my $chan    = $talkchannel;
510
511     if ($msgType eq 'private') {
512         if ($target =~ /^($mask{chan})\s+(.*)$/) {
513             $chan       = $1;
514             $target     = $2;
515             $extra      = 1;
516         } else {
517             &msg($who, "error: invalid format or missing arguments.");
518             &help("lart");
519             return $noreply;
520         }
521     }
522
523     my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.lart");
524     if (defined $line) {
525         if ($target =~ /^(me|you|itself|\Q$ident\E)$/i) {
526             $line =~ s/WHO/$who/g;
527         } else {
528             $line =~ s/WHO/$target/g;
529         }
530         $line .= ", courtesy of $who" if ($extra);
531
532         &action($chan, $line);
533     } else {
534         &status("lart: error reading file?");
535     }
536 }
537
538 1;