]> git.donarmstrong.com Git - infobot.git/blob - src/CommandStubs.pl
made freshmeat fork always
[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     my $cmd     = shift(@args);
36
37     &shmFlush();
38
39     foreach (keys %cmdhooks) {
40         my $ident = $_;
41
42         next unless ($cmd =~ /^$ident$/i);
43
44         &DEBUG("pCH: $cmd matched $ident");
45         my %hash = %{ $cmdhooks{$ident} };
46
47         ### DEBUG.
48         foreach (keys %hash) {
49             &DEBUG(" $cmd->$_ => '$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 ### ANY PROBLEMS WITH THIS? if so, add option to do either.
74 ###         &{$hash{'CODEREF'}}(@args);
75             &{$hash{'CODEREF'}}(join ' ', @args);
76         }
77
78         ### CMDSTATS.
79         if (exists $hash{'Cmdstats'}) {
80             $cmdstats{$hash{'Cmdstats'}}++;
81         }
82
83         &DEBUG("pCH: ended.");
84
85         return 1;
86     }
87
88     return 0;
89 }
90
91 &addCmdHook('d?bugs', ('CODEREF' => 'debianBugs',
92         'Forker' => 1, 'Identifier' => 'debianExtra',
93         'Cmdstats' => 'Debian Bugs') );
94 &addCmdHook('dauthor', ('CODEREF' => 'Debian::searchAuthor',
95         'Forker' => 1, 'Identifier' => 'debian',
96         'Cmdstats' => 'Debian Author Search', 'Help' => "dauthor" ) );
97 &addCmdHook('(d|search)desc', ('CODEREF' => 'Debian::searchDesc',
98         'Forker' => 1, 'Identifier' => 'debian',
99         'Cmdstats' => 'Debian Desc Search', 'Help' => "ddesc" ) );
100 &addCmdHook('dincoming', ('CODEREF' => 'Debian::generateIncoming',
101         'Forker' => 1, 'Identifier' => 'debian' ) );
102 &addCmdHook('dstats', ('CODEREF' => 'Debian::infoStats',
103         'Forker' => 1, 'Identifier' => 'debian',
104         'Cmdstats' => 'Debian Statistics' ) );
105 &addCmdHook('d?contents', ('CODEREF' => 'Debian::searchContents',
106         'Forker' => 1, 'Identifier' => 'debian',
107         'Cmdstats' => 'Debian Contents Search', 'Help' => "contents" ) );
108 &addCmdHook('d?find', ('CODEREF' => 'Debian::DebianFind',
109         'Forker' => 1, 'Identifier' => 'debian',
110         'Cmdstats' => 'Debian Search', 'Help' => "find" ) );
111 &addCmdHook('insult', ('CODEREF' => 'Insult::Insult',
112         'Forker' => 1, 'Identifier' => 'insult', 'Help' => "insult" ) );
113 &addCmdHook('kernel', ('CODEREF' => 'Kernel::Kernel',
114         'Forker' => 1, 'Identifier' => 'kernel',
115         'Cmdstats' => 'Kernel') );
116 &addCmdHook('listauth', ('CODEREF' => 'CmdListAuth',
117         'Identifier' => 'search', Module => 'factoids', 
118         'Help' => 'listauth') );
119 &addCmdHook('quote', ('CODEREF' => 'Quote::Quote',
120         'Forker' => 1, 'Identifier' => 'quote',
121         'Help' => 'quote', 'Cmdstats' => 'Quote') );
122 &addCmdHook('countdown', ('CODEREF' => 'Countdown',
123         'Module' => 'countdown', 'Identifier' => 'countdown',
124         'Cmdstats' => 'Countdown') );
125 &addCmdHook('lart', ('CODEREF' => 'lart',
126         'Identifier' => 'lart', 'Help' => 'lart') );
127 &addCmdHook('convert', ('CODEREF' => 'convert',
128         'Forker' => 1, 'Identifier' => 'units',
129         'Help' => 'convert') );
130 &addCmdHook('(cookie|random)', ('CODEREF' => 'cookie',
131         'Forker' => 1, 'Identifier' => 'factoids') );
132 &addCmdHook('u(ser)?info', ('CODEREF' => 'userinfo',
133         'Identifier' => 'userinfo', 'Help' => 'userinfo',
134         'Module' => 'userinfo') );
135 &addCmdHook('rootWarn', ('CODEREF' => 'CmdrootWarn',
136         'Identifier' => 'rootWarn', 'Module' => 'rootwarn') );
137 &addCmdHook('seen', ('CODEREF' => 'seen', 'Identifier' => 'seen') );
138 &addCmdHook('dict', ('CODEREF' => 'Dict::Dict',
139         'Identifier' => 'dict', 'Help' => 'dict',
140         'Forker' => 1, 'Cmdstats' => 'Dict') );
141 &addCmdHook('slashdot', ('CODEREF' => 'Slashdot::Slashdot',
142         'Identifier' => 'slashdot', 'Forker' => 1,
143         'Cmdstats' => 'Slashdot') );
144 &addCmdHook('uptime', ('CODEREF' => 'uptime', 'Identifier' => 'uptime',
145         'Cmdstats' => 'Uptime') );
146 &addCmdHook('nullski', ('CODEREF' => 'nullski', ) );
147 &addCmdHook('crash', ('CODEREF' => 'crash' ) );
148 sub nullski { my ($arg) = @_; foreach (`$arg`) { &msg($who,$_); } }
149 &addCmdHook('(fm|freshmeat)', ('CODEREF' => 'Freshmeat::Freshmeat',
150         'Identifier' => 'freshmeat', 'Cmdstats' => 'Freshmeat',
151         'Forker' => 1, 'Help' => 'freshmeat') );
152
153
154
155
156 &status("CMD: loaded ".scalar(keys %cmdhooks)." command hooks.");
157
158
159 sub Modules {
160     if (!defined $message) {
161         &WARN("Modules: message is undefined. should never happen.");
162         return;
163     }
164
165     # babel bot: Jonathan Feinberg++
166     if (&IsParam("babelfish") and $message =~ m{
167                 ^\s*
168                 (?:babel(?:fish)?|x|xlate|translate)
169                 \s+
170                 (to|from)               # direction of translation (through)
171                 \s+
172                 ($babel::lang_regex)\w* # which language?
173                 \s*
174                 (.+)                    # The phrase to be translated
175         }xoi) {
176
177         &Forker("babelfish", sub { &babel::babelfish(lc $1, lc $2, $3); } );
178
179         $cmdstats{'BabelFish'}++;
180         return $noreply;
181     }
182
183     if (&IsParam("debian")) {
184         my $debiancmd    = 'conflicts?|depends?|desc|file|info|provides?';
185         $debiancmd      .= '|recommends?|suggests?|maint|maintainer';
186         if ($message =~ /^($debiancmd)(\s+(.*))?$/i) {
187             my $package = lc $3;
188
189             if (defined $package) {
190                 &Forker("debian", sub { &Debian::infoPackages($1, $package); } );
191             } else {
192                 &help($1);
193             }
194
195             return $noreply;
196         }
197     }
198
199     # google searching. Simon++
200     if (&IsParam("wwwsearch") and $message =~ /^(?:search\s+)?($W3Search_regex)\s+for\s+['"]?(.*?)['"]?\s*\?*$/i) {
201         return $noreply unless (&hasParam("wwwsearch"));
202
203         &Forker("wwwsearch", sub { &W3Search::W3Search($1,$2,$param{'wwwsearch'}); } );
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;