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