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