]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Core.pl
- nice patch from Phil Edwards <pme@devphil.com>. Thanks!
[infobot.git] / src / Factoids / Core.pl
1 #
2 #   Misc.pl: Miscellaneous stuff.
3 #    Author: dms
4 #   Version: v0.1 (20010906)
5 #   Created: 20010906
6 #
7
8 # use strict;   # TODO
9
10 use vars qw(%param %cache %lang %cmdstats %bots);
11 use vars qw($message $who $addressed $chan $h $nuh $ident $msgType
12         $correction_plausable);
13
14 # Usage: &validFactoid($lhs,$rhs);
15 sub validFactoid {
16     my ($lhs,$rhs) = @_;
17     my $valid = 0;
18
19     for (lc $lhs) {
20         # allow the following only if they have been made on purpose.
21         if ($rhs ne "" and $rhs !~ /^</) {
22             / \Q$ident$/i and last;     # someone said i'm something.
23             /^i('m)? / and last;
24             /^(it|that|there|what)('s)?(\s+|$)/ and last;
25             /^you('re)?(\s+|$)/ and last;
26
27             /^(where|who|why|when|how)(\s+|$)/ and last;
28             /^(this|that|these|those|they)(\s+|$)/ and last;
29             /^(every(one|body)|we) / and last;
30
31             /^say / and last;
32         }
33
34         # uncaught commands.
35         /^add topic / and last;         # topic management.
36         /( add$| add |^add )/ and last; # borked teach statement.
37         /^learn / and last;             # teach. damn morons.
38         /^tell (\S+) about / and last;  # tell.
39         /\=\~/ and last;                # substituition.
40         /^\S+ to \S+ \S+/ and last;     # babelfish.
41
42         /^\=/ and last;                 # botnick = heh is.
43         /wants you to know/ and last;
44
45         # symbols.
46         /(\"\*)/ and last;
47         /, / and last;
48         (/^'/ and /'$/) and last;
49         (/^"/ and /"$/) and last;
50
51         # delimiters.
52         /\=\>/ and last;                # '=>'.
53         /\;\;/ and last;                # ';;'.
54         /\|\|/ and last;                # '||'.
55
56         /^\Q$ident\E[\'\,\: ]/ and last;# dupe addressed.
57         /^[\-\, ]/ and last;
58         /\\$/ and last;                 # forgot shift for '?'.
59         /^all / and last;
60         /^also / and last;
61         / also$/ and last;
62         / and$/ and last;
63         /^because / and last;
64         /^but / and last;
65         /^gives / and last;
66         /^h(is|er) / and last;
67         /^if / and last;
68         / is,/ and last;
69         / it$/ and last;
70         /^or / and last;
71         / says$/ and last;
72         /^should / and last;
73         /^so / and last;
74         /^supposedly/ and last;
75         /^to / and last;
76         /^was / and last;
77         / which$/ and last;
78
79         # nasty bug I introduced _somehow_, probably by fixMySQLBug().
80         /\\\%/ and last;
81         /\\\_/ and last;
82
83         # weird/special stuff. also old blootbot or stock infobot bugs.
84         $rhs =~ /( \Q$ident\E's|\Q$ident\E's )/i and last; # ownership.
85
86         # duplication.
87         $rhs =~ /^\Q$lhs /i and last;
88         last if ($rhs =~ /^is /i and / is$/);
89
90         $valid++;
91     }
92
93     return $valid;
94 }
95
96 sub FactoidStuff {
97     # inter-infobot.
98     if ($msgType =~ /private/ and $message =~ s/^:INFOBOT://) {
99         ### identification.
100         &status("infobot <$nuh> identified") unless $bots{$nuh};
101         $bots{$nuh} = $who;
102
103         ### communication.
104
105         # query.
106         if ($message =~ /^QUERY (<.*?>) (.*)/) {        # query.
107             my ($target,$item) = ($1,$2);
108             $item =~ s/[.\?]$//;
109
110             &status(":INFOBOT:QUERY $who: $message");
111
112             if ($_ = &getFactoid($item)) {
113                 &msg($who, ":INFOBOT:REPLY $target $item =is=> $_");
114             }
115
116             return 'INFOBOT QUERY';
117         } elsif ($message =~ /^REPLY <(.*?)> (.*)/) {   # reply.
118             my ($target,$item) = ($1,$2);
119
120             &status(":INFOBOT:REPLY $who: $message");
121
122             my ($lhs,$mhs,$rhs) = $item =~ /^(.*?) =(.*?)=> (.*)/;
123
124             if ($param{'acceptUrl'} !~ /REQUIRE/ or $rhs =~ /(http|ftp|mailto|telnet|file):/) {
125                 &msg($target, "$who knew: $lhs $mhs $rhs");
126
127                 # "are" hack :)
128                 $rhs = "<REPLY> are" if ($mhs eq "are");
129                 &setFactInfo($lhs, "factoid_value", $rhs);
130             }
131
132             return 'INFOBOT REPLY';
133         } else {
134             &ERROR(":INFOBOT:UNKNOWN $who: $message");
135             return 'INFOBOT UNKNOWN';
136         }
137     }
138
139     # factoid forget.
140     if ($message =~ s/^forget\s+//i) {
141         return 'forget: no addr' unless ($addressed);
142
143         my $faqtoid = $message;
144         if ($faqtoid eq "") {
145             &help("forget");
146             return;
147         }
148
149         $faqtoid =~ tr/A-Z/a-z/;
150         my $result = &getFactoid($faqtoid);
151
152         # if it doesn't exist, well... it doesn't!
153         if (!defined $result) {
154             &performReply("i didn't have anything called '$faqtoid'");
155             return;
156         }
157
158         my $author      = &getFactInfo($faqtoid, "created_by");
159         my $count       = &getFactInfo($faqtoid, "requested_count") || 0;
160         # don't delete if requested $limit times
161         my $limit       = &getChanConfDefault(
162                                 "factoidPreventForgetLimit", 100, $chan);
163         # don't delete if older than $limitage seconds (modified by requests below)
164         my $limitage    = &getChanConfDefault(
165                                 "factoidPreventForgetLimitTime", 7 * 24 * 60 * 60, $chan);
166         my $t           = &getFactInfo($faqtoid, "created_time") || 0;
167         my $age         = time() - $t;
168
169         # lets scale limitage from 1 (nearly 0) to $limit (full time).
170         $limitage       = $limitage*($count+1)/$limit if ($count < $limit);
171         # isauthor and isop.
172         my $isau        = (defined $author and &IsHostMatch($author) == 2) ? 1 : 0;
173         my $isop        = (&IsFlag("o") eq "o") ? 1 : 0;
174
175         if (IsFlag("r") ne "r" && !$isop) {
176             &msg($who, "you don't have access to remove factoids");
177             return;
178         }
179
180         return 'locked factoid' if (&IsLocked($faqtoid) == 1);
181
182         ###
183         ### lets go do some checking.
184         ###
185
186         # factoidPreventForgetLimitTime:
187         if (!($isop or $isau) and $age/(60*60*24) > $limitage) {
188             &msg($who, "cannot remove factoid '$faqtoid', too old. (" .
189                     $age/(60*60*24) . ">$limitage) use 'no,' instead");
190             return;
191         }
192
193         # factoidPreventForgetLimit:
194         if (!($isop or $isau) and $limit and $count > $limit) {
195             &msg($who, "will not delete '$faqtoid', count > limit ($count > $limit) use 'no, ' instead.");
196             return;
197         }
198
199         # this may eat some memory.
200         # prevent deletion if other factoids redirect to it.
201         # todo: use hash instead of array.
202         my @list;
203         if (&getChanConf("factoidPreventForgetRedirect")) {
204             &status("Factoids/Core: forget: checking for redirect factoids");
205             @list = &searchTable("factoids", "factoid_key",
206                         "factoid_value", "^<REPLY> see ");
207         }
208
209         my $match = 0;
210         for (@list) {
211             my $f = $_;
212             my $v = &getFactInfo($f, "factoid_value");
213             my $fsafe = quotemeta($faqtoid);
214             next unless ($v =~ /^<REPLY> ?see( also)? $fsafe\.?$/i);
215
216             &DEBUG("Factoids/Core: match! ($f || $faqtoid)");
217
218             $match++;
219         }
220         # todo: warn for op aswell, but allow force delete.
221         if (!$isop and $match) {
222             &msg($who, "uhm, other (redirection) factoids depend on this one.");
223             return;
224         }
225
226         # minimize abuse.
227         if (!$isop and &IsHostMatch($author) != 2) {
228             $cache{forget}{$h}++;
229
230             # warn.
231             if ($cache{forget}{$h} > 3) {
232                 &msg($who, "Stop abusing forget!");
233             }
234
235             # ignore.
236             # todo: make forget limit configurable.
237             # todo: make forget ignore time configurable.
238             if ($cache{forget}{$h} > 5) {
239                 &ignoreAdd(&makeHostMask($nuh), "*", 3*24*60*60, "abuse of forget");
240                 &msg($who, "forget: Suck it!");
241             }
242         }
243
244         # lets do it!
245
246         if (&IsParam("factoidDeleteDelay") or &IsChanConf("factoidDeleteDelay")) {
247             if (!($isop or $isau) and $faqtoid =~ / #DEL#$/) {
248                 &msg($who, "cannot delete it ($faqtoid).");
249                 return;
250             }
251
252             &status("forgot (safe delete): '$faqtoid' - ". scalar(gmtime));
253             ### TODO: check if the "backup" exists and overwrite it
254             my $check = &getFactoid("$faqtoid #DEL#");
255
256             if (!defined $check or $check =~ /^\s*$/) {
257                 if ($faqtoid !~ / #DEL#$/) {
258                     my $new = $faqtoid." #DEL#";
259
260                     my $backup = &getFactoid($new);
261                     if ($backup) {
262                         &DEBUG("forget: not overwriting backup: $faqtoid");
263                     } else {
264                         &status("forget: backing up '$faqtoid'");
265                         &setFactInfo($faqtoid, "factoid_key", $new);
266                         &setFactInfo($new, "modified_by", $who);
267                         &setFactInfo($new, "modified_time", time());
268                     }
269
270                 } else {
271                     &status("forget: not backing up $faqtoid.");
272                 }
273
274             } else {
275                 &status("forget: not overwriting backup!");
276             }
277         }
278
279         &status("forget: <$who> '$faqtoid' =is=> '$result'");
280         &delFactoid($faqtoid);
281
282         &performReply("i forgot $faqtoid");
283
284         $count{'Update'}++;
285
286         return;
287     }
288
289     # factoid unforget/undelete.
290     if ($message =~ s/^un(forget|delete)\s+//i) {
291         return 'unforget: no addr' unless ($addressed);
292
293         my $i = 0;
294         $i++ if (&IsParam("factoidDeleteDelay"));
295         $i++ if (&IsChanConf("factoidDeleteDelay"));
296         if (!$i) {
297             &performReply("safe delete has been disable so what is there to undelete?");
298             return;
299         }
300
301         my $faqtoid = $message;
302         if ($faqtoid eq "") {
303             &help("unforget");
304             return;
305         }
306
307         $faqtoid =~ tr/A-Z/a-z/;
308         my $result = &getFactoid($faqtoid." #DEL#");
309         my $check  = &getFactoid($faqtoid);
310
311         if (defined $check) {
312             &performReply("cannot undeleted '$faqtoid' because it already exists!");
313             return;
314         }
315
316         if (!defined $result) {
317             &performReply("that factoid was not backedup :/");
318             return;
319         }
320
321         &setFactInfo($faqtoid." #DEL#", "factoid_key",   $faqtoid);
322 #       &setFactInfo($faqtoid, "modified_by",   "");
323 #       &setFactInfo($faqtoid, "modified_time", 0);
324
325         $check  = &getFactoid($faqtoid);
326         # todo: check if $faqtoid." #DEL#" exists?
327         if (defined $check) {
328             &performReply("Successfully recovered '$faqtoid'.  Have fun now.");
329             $count{'Undelete'}++;
330         } else {
331             &performReply("did not recover '$faqtoid'.  What happened?");
332         }
333
334         return;
335     }
336
337     # factoid locking.
338     if ($message =~ /^((un)?lock)(\s+(.*))?\s*?$/i) {
339         return 'lock: no addr 2' unless ($addressed);
340
341         my $function = lc $1;
342         my $faqtoid  = lc $4;
343
344         if ($faqtoid eq "") {
345             &help($function);
346             return;
347         }
348
349         if (&getFactoid($faqtoid) eq "") {
350             &msg($who, "factoid \002$faqtoid\002 does not exist");
351             return;
352         }
353
354         if ($function eq "lock") {
355             # strongly requested by #debian on 19991028. -xk
356             if (1 and $faqtoid !~ /^\Q$who\E$/i and &IsFlag("o") ne "o") {
357                 &msg($who,"sorry, locking cannot be used since it can be abused unneccesarily.");
358                 &status("Replace 1 with 0 in Process.pl#~324 for locking support.");
359                 return;
360             }
361
362             &CmdLock($faqtoid);
363         } else {
364             &CmdUnLock($faqtoid);
365         }
366
367         return;
368     }
369
370     # factoid rename.
371     if ($message =~ s/^rename(\s+|$)//) {
372         return 'rename: no addr' unless ($addressed);
373
374         if ($message eq "") {
375             &help("rename");
376             return;
377         }
378
379         if ($message =~ /^'(.*)'\s+'(.*)'$/) {
380             my ($from,$to) = (lc $1, lc $2);
381
382             my $result = &getFactoid($from);
383             if (defined $result) {
384                 &performReply("i didn't have anything called '$from'");
385                 return;
386             }
387
388             my $author = &getFactInfo($from, "created_by");
389
390             # who == nick!user@host.
391             if (&IsFlag("m") ne "m" and $author !~ /^\Q$who\E\!/i) {
392                 &msg($who, "factoid '$form' is not yours to modify.");
393                 return;
394             }
395
396             if ($_ = &getFactoid($to)) {
397                 &performReply("destination factoid already exists.");
398                 return;
399             }
400
401             &setFactInfo($from,"factoid_key",$to);
402
403             &status("rename: <$who> '$from' is now '$to'");
404             &performReply("i renamed '$from' to '$to'");
405         } else {
406             &msg($who,"error: wrong format. ask me about 'help rename'.");
407         }
408
409         return;
410     }
411
412     # factoid substitution. (X =~ s/A/B/FLAG)
413     if ($message =~ m|^(.*?)\s+=~\s+s([/,#])(.+?)\2(.*?)\2([a-z]*);?\s*$|) {
414         my ($faqtoid,$delim,$op,$np,$flags) = (lc $1, $2, $3, $4, $5);
415         return 'subst: no addr' unless ($addressed);
416
417         # incorrect format.
418         if ($np =~ /$delim/) {
419             &msg($who,"looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
420             return;
421         }
422
423         # success.
424         if (my $result = &getFactoid($faqtoid)) {
425             return 'subst: locked' if (&IsLocked($faqtoid) == 1);
426             my $was = $result;
427
428             if (($flags eq "g" && $result =~ s/\Q$op/$np/gi) || $result =~ s/\Q$op/$np/i) {
429                 # excessive length.
430                 if (length $result > $param{'maxDataSize'}) {
431                     &performReply("that's too long");
432                     return;
433                 }
434                 # min length.
435                 my $faqauth = &getFactInfo($faqtoid, "created_by");
436                 if ((length $result)*2 < length $was and
437                         &IsFlag("o") ne "o" and
438                         &IsHostMask($faqauth) != 2
439                 ) {
440                     &performReply("too drastic change of factoid.");
441                 }
442
443                 &setFactInfo($faqtoid, "factoid_value", $result);
444                 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
445                 &performReply("OK");
446             } else {
447                 &performReply("that doesn't contain '$op'");
448             }
449         } else {
450             &performReply("i didn't have anything called '$faqtoid'");
451         }
452
453         return;
454     }
455
456     # Fix up $message for question.
457     my $question = $message;
458     for ($question) {
459         # fix the string.
460         s/^hey([, ]+)where/where/i;
461         s/\s+\?$/?/;
462         s/whois/who is/ig;
463         s/where can i find/where is/i;
464         s/how about/where is/i;
465         s/ da / the /ig;
466
467         # clear the string of useless words.
468         s/^(stupid )?q(uestion)?:\s+//i;
469         s/^(does )?(any|ne)(1|one|body) know //i;
470
471         s/^[uh]+m*[,\.]* +//i;
472
473         s/^well([, ]+)//i;
474         s/^still([, ]+)//i;
475         s/^(gee|boy|golly|gosh)([, ]+)//i;
476         s/^(well|and|but|or|yes)([, ]+)//i;
477
478         s/^o+[hk]+(a+y+)?([,. ]+)//i;
479         s/^g(eez|osh|olly)([,. ]+)//i;
480         s/^w(ow|hee|o+ho+)([,. ]+)//i;
481         s/^heya?,?( folks)?([,. ]+)//i;
482     }
483
484     if ($addressed and $message =~ s/^no([, ]+)(\Q$ident\E\,+)?\s*//i) {
485         $correction_plausible = 1;
486         &status("correction is plausible, initial negative and nick deleted ($&)") if ($param{VERBOSITY});
487     } else {
488         $correction_plausible = 0;
489     }
490
491     my $result = &doQuestion($question);
492     if (!defined $result or $result eq $noreply) {
493         return 'result from doQ undef.';
494     }
495
496     if (defined $result and $result !~ /^0?$/) {        # question.
497         &status("question: <$who> $message");
498         $count{'Question'}++;
499     } elsif (&IsChanConf("perlMath") > 0 and $addressed) { # perl math.
500         &loadMyModule("perlMath");
501         my $newresult = &perlMath();
502
503         if (defined $newresult and $newresult ne "") {
504             $cmdstats{'Maths'}++;
505             $result = $newresult;
506             &status("math: <$who> $message => $result");
507         }
508     }
509
510     if ($result !~ /^0?$/) {
511         &performStrictReply($result);
512         return;
513     }
514
515     # why would a friendly bot get passed here?
516     if (&IsParam("friendlyBots")) {
517         return if (grep lc($_) eq lc($who), split(/\s+/, $param{'friendlyBots'}));
518     }
519
520     # do the statement.
521     if (!defined &doStatement($message)) {
522         return;
523     }
524
525     return unless ($addressed and !$addrchar);
526
527     if (length $message > 64) {
528         &status("unparseable-moron: $message");
529 #       &performReply( &getRandom(keys %{ $lang{'moron'} }) );
530         $count{'Moron'}++;
531
532         &performReply("You are moron \002#". $count{'Moron'} ."\002");
533         return;
534     }
535
536     &status("unparseable: $message");
537     &performReply( &getRandom(keys %{ $lang{'dunno'} }) );
538     $count{'Dunno'}++;
539 }
540
541 1;