]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Core.pl
perlMath -> Math
[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
41         /^\=/ and last;                 # botnick = heh is.
42         /wants you to know/ and last;
43
44         # symbols.
45         /(\"\*)/ and last;
46         /, / and last;
47         (/^'/ and /'$/) and last;
48         (/^"/ and /"$/) and last;
49
50         # delimiters.
51         /\=\>/ and last;                # '=>'.
52         /\;\;/ and last;                # ';;'.
53         /\|\|/ and last;                # '||'.
54
55         /^\Q$ident\E[\'\,\: ]/ and last;# dupe addressed.
56         /^[\-\, ]/ and last;
57         /\\$/ and last;                 # forgot shift for '?'.
58         /^all / and last;
59         /^also / and last;
60         / also$/ and last;
61         / and$/ and last;
62         /^because / and last;
63         /^but / and last;
64         /^gives / and last;
65         /^h(is|er) / and last;
66         /^if / and last;
67         / is,/ and last;
68         / it$/ and last;
69         /^or / and last;
70         / says$/ and last;
71         /^should / and last;
72         /^so / and last;
73         /^supposedly/ and last;
74         /^to / and last;
75         /^was / and last;
76         / which$/ and last;
77
78         # nasty bug I introduced _somehow_, probably by fixMySQLBug().
79         /\\\%/ and last;
80         /\\\_/ and last;
81
82         # weird/special stuff. also old blootbot or stock infobot bugs.
83         $rhs =~ /( \Q$ident\E's|\Q$ident\E's )/i and last; # ownership.
84
85         # duplication.
86         $rhs =~ /^\Q$lhs /i and last;
87         last if ($rhs =~ /^is /i and / is$/);
88
89         $valid++;
90     }
91
92     return $valid;
93 }
94
95 sub FactoidStuff {
96     # inter-infobot.
97     if ($msgType =~ /private/ and $message =~ s/^:INFOBOT://) {
98         ### identification.
99         &status("infobot <$nuh> identified") unless $bots{$nuh};
100         $bots{$nuh} = $who;
101
102         ### communication.
103
104         # query.
105         if ($message =~ /^QUERY (<.*?>) (.*)/) {        # query.
106             my ($target,$item) = ($1,$2);
107             $item =~ s/[.\?]$//;
108
109             &status(":INFOBOT:QUERY $who: $message");
110
111             if ($_ = &getFactoid($item)) {
112                 &msg($who, ":INFOBOT:REPLY $target $item =is=> $_");
113             }
114
115             return 'INFOBOT QUERY';
116         } elsif ($message =~ /^REPLY <(.*?)> (.*)/) {   # reply.
117             my ($target,$item) = ($1,$2);
118
119             &status(":INFOBOT:REPLY $who: $message");
120
121             my ($lhs,$mhs,$rhs) = $item =~ /^(.*?) =(.*?)=> (.*)/;
122
123             if ($param{'acceptUrl'} !~ /REQUIRE/ or $rhs =~ /(http|ftp|mailto|telnet|file):/) {
124                 &msg($target, "$who knew: $lhs $mhs $rhs");
125
126                 # "are" hack :)
127                 $rhs = "<REPLY> are" if ($mhs eq "are");
128                 &setFactInfo($lhs, "factoid_value", $rhs);
129             }
130
131             return 'INFOBOT REPLY';
132         } else {
133             &ERROR(":INFOBOT:UNKNOWN $who: $message");
134             return 'INFOBOT UNKNOWN';
135         }
136     }
137
138     # factoid forget.
139     if ($message =~ s/^forget\s+//i) {
140         return 'forget: no addr' unless ($addressed);
141
142         my $faqtoid = $message;
143         if ($faqtoid eq "") {
144             &help("forget");
145             return;
146         }
147
148         $faqtoid =~ tr/A-Z/a-z/;
149         $faqtoid =~ s/^cmd:/CMD:/; # basic command forgetting
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' to forget");
155             return;
156         }
157
158         # TODO: squeeze 3 getFactInfo calls into one?
159         my $author      = &getFactInfo($faqtoid, "created_by");
160         my $count       = &getFactInfo($faqtoid, "requested_count") || 0;
161         # don't delete if requested $limit times
162         my $limit       = &getChanConfDefault(
163                                 "factoidPreventForgetLimit", 100, $chan);
164         # don't delete if older than $limitage seconds (modified by requests below)
165         my $limitage    = &getChanConfDefault(
166                                 "factoidPreventForgetLimitTime", 7 * 24 * 60 * 60, $chan);
167         my $t           = &getFactInfo($faqtoid, "created_time") || 0;
168         my $age         = time() - $t;
169
170         # lets scale limitage from 1 (nearly 0) to $limit (full time).
171         $limitage       = $limitage*($count+1)/$limit if ($count < $limit);
172         # isauthor and isop.
173         my $isau        = (defined $author and &IsHostMatch($author) == 2) ? 1 : 0;
174         my $isop        = (&IsFlag("o") eq "o") ? 1 : 0;
175
176         if (IsFlag("r") ne "r" && !$isop) {
177             &msg($who, "you don't have access to remove factoids");
178             return;
179         }
180
181         return 'locked factoid' if (&IsLocked($faqtoid) == 1);
182
183         ###
184         ### lets go do some checking.
185         ###
186
187         # factoidPreventForgetLimitTime:
188         if (!($isop or $isau) and $age/(60*60*24) > $limitage) {
189             &msg($who, "cannot remove factoid '$faqtoid', too old. (" .
190                     $age/(60*60*24) . ">$limitage) use 'no,' instead");
191             return;
192         }
193
194         # factoidPreventForgetLimit:
195         if (!($isop or $isau) and $limit and $count > $limit) {
196             &msg($who, "will not delete '$faqtoid', count > limit ($count > $limit) use 'no, ' instead.");
197             return;
198         }
199
200         # this may eat some memory.
201         # prevent deletion if other factoids redirect to it.
202         # TODO: use hash instead of array.
203         my @list;
204         if (&getChanConf("factoidPreventForgetRedirect")) {
205             &status("Factoids/Core: forget: checking for redirect factoids");
206             @list = &searchTable("factoids", "factoid_key",
207                         "factoid_value", "^<REPLY> see ");
208         }
209
210         my $match = 0;
211         for (@list) {
212             my $f = $_;
213             my $v = &getFactInfo($f, "factoid_value");
214             my $fsafe = quotemeta($faqtoid);
215             next unless ($v =~ /^<REPLY> ?see( also)? $fsafe\.?$/i);
216
217             &DEBUG("Factoids/Core: match! ($f || $faqtoid)");
218
219             $match++;
220         }
221         # TODO: warn for op aswell, but allow force delete.
222         if (!$isop and $match) {
223             &msg($who, "uhm, other (redirection) factoids depend on this one.");
224             return;
225         }
226
227         # minimize abuse.
228         if (!$isop and &IsHostMatch($author) != 2) {
229             $cache{forget}{$h}++;
230
231             # warn.
232             if ($cache{forget}{$h} > 3) {
233                 &msg($who, "Stop abusing forget!");
234             }
235
236             # ignore.
237             # TODO: make forget limit configurable.
238             # TODO: make forget ignore time configurable.
239             if ($cache{forget}{$h} > 5) {
240                 &ignoreAdd(&makeHostMask($nuh), "*", 3*24*60, "abuse of forget");
241                 &msg($who, "forget: Ignoring you for abuse!");
242             }
243         }
244
245         # lets do it!
246
247         if (&IsParam("factoidDeleteDelay") or &IsChanConf("factoidDeleteDelay")) {
248             if (!($isop or $isau) and $faqtoid =~ / #DEL#$/) {
249                 &msg($who, "cannot delete it ($faqtoid).");
250                 return;
251             }
252
253             &status("forgot (safe delete): '$faqtoid' - ". scalar(gmtime));
254             ### TODO: check if the "backup" exists and overwrite it
255             my $check = &getFactoid("$faqtoid #DEL#");
256
257             if (!defined $check or $check =~ /^\s*$/) {
258                 if ($faqtoid !~ / #DEL#$/) {
259                     my $new = $faqtoid." #DEL#";
260
261                     my $backup = &getFactoid($new);
262                     if ($backup) {
263                         &DEBUG("forget: not overwriting backup: $faqtoid");
264                     } else {
265                         &status("forget: backing up '$faqtoid'");
266                         &setFactInfo($faqtoid, "factoid_key", $new);
267                         &setFactInfo($new, "modified_by", $who);
268                         &setFactInfo($new, "modified_time", time());
269                     }
270
271                 } else {
272                     &status("forget: not backing up $faqtoid.");
273                 }
274
275             } else {
276                 &status("forget: not overwriting backup!");
277             }
278         }
279
280         &status("forget: <$who> '$faqtoid' =is=> '$result'");
281         &delFactoid($faqtoid);
282
283         &performReply("i forgot $faqtoid");
284
285         $count{'Update'}++;
286
287         return;
288     }
289
290     # factoid unforget/undelete.
291     if ($message =~ s/^un(forget|delete)\s+//i) {
292         return 'unforget: no addr' unless ($addressed);
293
294         my $i = 0;
295         $i++ if (&IsParam("factoidDeleteDelay"));
296         $i++ if (&IsChanConf("factoidDeleteDelay"));
297         if (!$i) {
298             &performReply("safe delete has been disable so what is there to undelete?");
299             return;
300         }
301
302         my $faqtoid = $message;
303         if ($faqtoid eq "") {
304             &help("unforget");
305             return;
306         }
307
308         $faqtoid =~ tr/A-Z/a-z/;
309         $faqtoid =~ s/^cmd:/CMD:/; # basic command undeleteing
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' to rename");
387                 return;
388             }
389
390             # who == nick!user@host.
391             if (&IsFlag("m") ne "m" and $author !~ /^\Q$who\E\!/i) {
392                 &msg($who, "factoid '$from' 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' to modify");
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("Math") > 0 and $addressed) { # perl math.
500         &loadMyModule("Math");
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;