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