]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Core.pl
allow geeky forgetting of CMD:s
[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         $faqtoid =~ s/^cmd:/CMD:/; # basic command forgetting
151         my $result = &getFactoid($faqtoid);
152
153         # if it doesn't exist, well... it doesn't!
154         if (!defined $result) {
155             &performReply("i didn't have anything called '$faqtoid'");
156             return;
157         }
158
159         # todo: squeeze 3 getFactInfo calls into one?
160         my $author      = &getFactInfo($faqtoid, "created_by");
161         my $count       = &getFactInfo($faqtoid, "requested_count") || 0;
162         # don't delete if requested $limit times
163         my $limit       = &getChanConfDefault(
164                                 "factoidPreventForgetLimit", 100, $chan);
165         # don't delete if older than $limitage seconds (modified by requests below)
166         my $limitage    = &getChanConfDefault(
167                                 "factoidPreventForgetLimitTime", 7 * 24 * 60 * 60, $chan);
168         my $t           = &getFactInfo($faqtoid, "created_time") || 0;
169         my $age         = time() - $t;
170
171         # lets scale limitage from 1 (nearly 0) to $limit (full time).
172         $limitage       = $limitage*($count+1)/$limit if ($count < $limit);
173         # isauthor and isop.
174         my $isau        = (defined $author and &IsHostMatch($author) == 2) ? 1 : 0;
175         my $isop        = (&IsFlag("o") eq "o") ? 1 : 0;
176
177         if (IsFlag("r") ne "r" && !$isop) {
178             &msg($who, "you don't have access to remove factoids");
179             return;
180         }
181
182         return 'locked factoid' if (&IsLocked($faqtoid) == 1);
183
184         ###
185         ### lets go do some checking.
186         ###
187
188         # factoidPreventForgetLimitTime:
189         if (!($isop or $isau) and $age/(60*60*24) > $limitage) {
190             &msg($who, "cannot remove factoid '$faqtoid', too old. (" .
191                     $age/(60*60*24) . ">$limitage) use 'no,' instead");
192             return;
193         }
194
195         # factoidPreventForgetLimit:
196         if (!($isop or $isau) and $limit and $count > $limit) {
197             &msg($who, "will not delete '$faqtoid', count > limit ($count > $limit) use 'no, ' instead.");
198             return;
199         }
200
201         # this may eat some memory.
202         # prevent deletion if other factoids redirect to it.
203         # todo: use hash instead of array.
204         my @list;
205         if (&getChanConf("factoidPreventForgetRedirect")) {
206             &status("Factoids/Core: forget: checking for redirect factoids");
207             @list = &searchTable("factoids", "factoid_key",
208                         "factoid_value", "^<REPLY> see ");
209         }
210
211         my $match = 0;
212         for (@list) {
213             my $f = $_;
214             my $v = &getFactInfo($f, "factoid_value");
215             my $fsafe = quotemeta($faqtoid);
216             next unless ($v =~ /^<REPLY> ?see( also)? $fsafe\.?$/i);
217
218             &DEBUG("Factoids/Core: match! ($f || $faqtoid)");
219
220             $match++;
221         }
222         # todo: warn for op aswell, but allow force delete.
223         if (!$isop and $match) {
224             &msg($who, "uhm, other (redirection) factoids depend on this one.");
225             return;
226         }
227
228         # minimize abuse.
229         if (!$isop and &IsHostMatch($author) != 2) {
230             $cache{forget}{$h}++;
231
232             # warn.
233             if ($cache{forget}{$h} > 3) {
234                 &msg($who, "Stop abusing forget!");
235             }
236
237             # ignore.
238             # todo: make forget limit configurable.
239             # todo: make forget ignore time configurable.
240             if ($cache{forget}{$h} > 5) {
241                 &ignoreAdd(&makeHostMask($nuh), "*", 3*24*60, "abuse of forget");
242                 &msg($who, "forget: Ignoring you for abuse!");
243             }
244         }
245
246         # lets do it!
247
248         if (&IsParam("factoidDeleteDelay") or &IsChanConf("factoidDeleteDelay")) {
249             if (!($isop or $isau) and $faqtoid =~ / #DEL#$/) {
250                 &msg($who, "cannot delete it ($faqtoid).");
251                 return;
252             }
253
254             &status("forgot (safe delete): '$faqtoid' - ". scalar(gmtime));
255             ### TODO: check if the "backup" exists and overwrite it
256             my $check = &getFactoid("$faqtoid #DEL#");
257
258             if (!defined $check or $check =~ /^\s*$/) {
259                 if ($faqtoid !~ / #DEL#$/) {
260                     my $new = $faqtoid." #DEL#";
261
262                     my $backup = &getFactoid($new);
263                     if ($backup) {
264                         &DEBUG("forget: not overwriting backup: $faqtoid");
265                     } else {
266                         &status("forget: backing up '$faqtoid'");
267                         &setFactInfo($faqtoid, "factoid_key", $new);
268                         &setFactInfo($new, "modified_by", $who);
269                         &setFactInfo($new, "modified_time", time());
270                     }
271
272                 } else {
273                     &status("forget: not backing up $faqtoid.");
274                 }
275
276             } else {
277                 &status("forget: not overwriting backup!");
278             }
279         }
280
281         &status("forget: <$who> '$faqtoid' =is=> '$result'");
282         &delFactoid($faqtoid);
283
284         &performReply("i forgot $faqtoid");
285
286         $count{'Update'}++;
287
288         return;
289     }
290
291     # factoid unforget/undelete.
292     if ($message =~ s/^un(forget|delete)\s+//i) {
293         return 'unforget: no addr' unless ($addressed);
294
295         my $i = 0;
296         $i++ if (&IsParam("factoidDeleteDelay"));
297         $i++ if (&IsChanConf("factoidDeleteDelay"));
298         if (!$i) {
299             &performReply("safe delete has been disable so what is there to undelete?");
300             return;
301         }
302
303         my $faqtoid = $message;
304         if ($faqtoid eq "") {
305             &help("unforget");
306             return;
307         }
308
309         $faqtoid =~ tr/A-Z/a-z/;
310         my $result = &getFactoid($faqtoid." #DEL#");
311         my $check  = &getFactoid($faqtoid);
312
313         if (defined $check) {
314             &performReply("cannot undeleted '$faqtoid' because it already exists!");
315             return;
316         }
317
318         if (!defined $result) {
319             &performReply("that factoid was not backedup :/");
320             return;
321         }
322
323         &setFactInfo($faqtoid." #DEL#", "factoid_key",   $faqtoid);
324 #       &setFactInfo($faqtoid, "modified_by",   "");
325 #       &setFactInfo($faqtoid, "modified_time", 0);
326
327         $check  = &getFactoid($faqtoid);
328         # todo: check if $faqtoid." #DEL#" exists?
329         if (defined $check) {
330             &performReply("Successfully recovered '$faqtoid'.  Have fun now.");
331             $count{'Undelete'}++;
332         } else {
333             &performReply("did not recover '$faqtoid'.  What happened?");
334         }
335
336         return;
337     }
338
339     # factoid locking.
340     if ($message =~ /^((un)?lock)(\s+(.*))?\s*?$/i) {
341         return 'lock: no addr 2' unless ($addressed);
342
343         my $function = lc $1;
344         my $faqtoid  = lc $4;
345
346         if ($faqtoid eq "") {
347             &help($function);
348             return;
349         }
350
351         if (&getFactoid($faqtoid) eq "") {
352             &msg($who, "factoid \002$faqtoid\002 does not exist");
353             return;
354         }
355
356         if ($function eq "lock") {
357             # strongly requested by #debian on 19991028. -xk
358             if (1 and $faqtoid !~ /^\Q$who\E$/i and &IsFlag("o") ne "o") {
359                 &msg($who,"sorry, locking cannot be used since it can be abused unneccesarily.");
360                 &status("Replace 1 with 0 in Process.pl#~324 for locking support.");
361                 return;
362             }
363
364             &CmdLock($faqtoid);
365         } else {
366             &CmdUnLock($faqtoid);
367         }
368
369         return;
370     }
371
372     # factoid rename.
373     if ($message =~ s/^rename(\s+|$)//) {
374         return 'rename: no addr' unless ($addressed);
375
376         if ($message eq "") {
377             &help("rename");
378             return;
379         }
380
381         if ($message =~ /^'(.*)'\s+'(.*)'$/) {
382             my ($from,$to) = (lc $1, lc $2);
383
384             my $result = &getFactoid($from);
385             if (defined $result) {
386                 &performReply("i didn't have anything called '$from'");
387                 return;
388             }
389
390             my $author = &getFactInfo($from, "created_by");
391
392             # who == nick!user@host.
393             if (&IsFlag("m") ne "m" and $author !~ /^\Q$who\E\!/i) {
394                 &msg($who, "factoid '$form' is not yours to modify.");
395                 return;
396             }
397
398             if ($_ = &getFactoid($to)) {
399                 &performReply("destination factoid already exists.");
400                 return;
401             }
402
403             &setFactInfo($from,"factoid_key",$to);
404
405             &status("rename: <$who> '$from' is now '$to'");
406             &performReply("i renamed '$from' to '$to'");
407         } else {
408             &msg($who,"error: wrong format. ask me about 'help rename'.");
409         }
410
411         return;
412     }
413
414     # factoid substitution. (X =~ s/A/B/FLAG)
415     if ($message =~ m|^(.*?)\s+=~\s+s([/,#])(.+?)\2(.*?)\2([a-z]*);?\s*$|) {
416         my ($faqtoid,$delim,$op,$np,$flags) = (lc $1, $2, $3, $4, $5);
417         return 'subst: no addr' unless ($addressed);
418
419         # incorrect format.
420         if ($np =~ /$delim/) {
421             &msg($who,"looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
422             return;
423         }
424
425         # success.
426         if (my $result = &getFactoid($faqtoid)) {
427             return 'subst: locked' if (&IsLocked($faqtoid) == 1);
428             my $was = $result;
429
430             if (($flags eq "g" && $result =~ s/\Q$op/$np/gi) || $result =~ s/\Q$op/$np/i) {
431                 # excessive length.
432                 if (length $result > $param{'maxDataSize'}) {
433                     &performReply("that's too long");
434                     return;
435                 }
436                 # min length.
437                 my $faqauth = &getFactInfo($faqtoid, "created_by");
438                 if ((length $result)*2 < length $was and
439                         &IsFlag("o") ne "o" and
440                         &IsHostMask($faqauth) != 2
441                 ) {
442                     &performReply("too drastic change of factoid.");
443                 }
444
445                 &setFactInfo($faqtoid, "factoid_value", $result);
446                 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
447                 &performReply("OK");
448             } else {
449                 &performReply("that doesn't contain '$op'");
450             }
451         } else {
452             &performReply("i didn't have anything called '$faqtoid'");
453         }
454
455         return;
456     }
457
458     # Fix up $message for question.
459     my $question = $message;
460     for ($question) {
461         # fix the string.
462         s/^hey([, ]+)where/where/i;
463         s/\s+\?$/?/;
464         s/whois/who is/ig;
465         s/where can i find/where is/i;
466         s/how about/where is/i;
467         s/ da / the /ig;
468
469         # clear the string of useless words.
470         s/^(stupid )?q(uestion)?:\s+//i;
471         s/^(does )?(any|ne)(1|one|body) know //i;
472
473         s/^[uh]+m*[,\.]* +//i;
474
475         s/^well([, ]+)//i;
476         s/^still([, ]+)//i;
477         s/^(gee|boy|golly|gosh)([, ]+)//i;
478         s/^(well|and|but|or|yes)([, ]+)//i;
479
480         s/^o+[hk]+(a+y+)?([,. ]+)//i;
481         s/^g(eez|osh|olly)([,. ]+)//i;
482         s/^w(ow|hee|o+ho+)([,. ]+)//i;
483         s/^heya?,?( folks)?([,. ]+)//i;
484     }
485
486     if ($addressed and $message =~ s/^no([, ]+)(\Q$ident\E\,+)?\s*//i) {
487         $correction_plausible = 1;
488         &status("correction is plausible, initial negative and nick deleted ($&)") if ($param{VERBOSITY});
489     } else {
490         $correction_plausible = 0;
491     }
492
493     my $result = &doQuestion($question);
494     if (!defined $result or $result eq $noreply) {
495         return 'result from doQ undef.';
496     }
497
498     if (defined $result and $result !~ /^0?$/) {        # question.
499         &status("question: <$who> $message");
500         $count{'Question'}++;
501     } elsif (&IsChanConf("perlMath") > 0 and $addressed) { # perl math.
502         &loadMyModule("perlMath");
503         my $newresult = &perlMath();
504
505         if (defined $newresult and $newresult ne "") {
506             $cmdstats{'Maths'}++;
507             $result = $newresult;
508             &status("math: <$who> $message => $result");
509         }
510     }
511
512     if ($result !~ /^0?$/) {
513         &performStrictReply($result);
514         return;
515     }
516
517     # why would a friendly bot get passed here?
518     if (&IsParam("friendlyBots")) {
519         return if (grep lc($_) eq lc($who), split(/\s+/, $param{'friendlyBots'}));
520     }
521
522     # do the statement.
523     if (!defined &doStatement($message)) {
524         return;
525     }
526
527     return unless ($addressed and !$addrchar);
528
529     if (length $message > 64) {
530         &status("unparseable-moron: $message");
531 #       &performReply( &getRandom(keys %{ $lang{'moron'} }) );
532         $count{'Moron'}++;
533
534         &performReply("You are moron \002#". $count{'Moron'} ."\002");
535         return;
536     }
537
538     &status("unparseable: $message");
539     &performReply( &getRandom(keys %{ $lang{'dunno'} }) );
540     $count{'Dunno'}++;
541 }
542
543 1;