]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Core.pl
- ircTextCounters stuff moved into a separate function.
[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         # 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*60, "abuse of forget");
241                 &msg($who, "forget: Suck it!");
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         my $result = &getFactoid($faqtoid." #DEL#");
310         my $check  = &getFactoid($faqtoid);
311
312         if (defined $check) {
313             &performReply("cannot undeleted '$faqtoid' because it already exists!");
314             return;
315         }
316
317         if (!defined $result) {
318             &performReply("that factoid was not backedup :/");
319             return;
320         }
321
322         &setFactInfo($faqtoid." #DEL#", "factoid_key",   $faqtoid);
323 #       &setFactInfo($faqtoid, "modified_by",   "");
324 #       &setFactInfo($faqtoid, "modified_time", 0);
325
326         $check  = &getFactoid($faqtoid);
327         # todo: check if $faqtoid." #DEL#" exists?
328         if (defined $check) {
329             &performReply("Successfully recovered '$faqtoid'.  Have fun now.");
330             $count{'Undelete'}++;
331         } else {
332             &performReply("did not recover '$faqtoid'.  What happened?");
333         }
334
335         return;
336     }
337
338     # factoid locking.
339     if ($message =~ /^((un)?lock)(\s+(.*))?\s*?$/i) {
340         return 'lock: no addr 2' unless ($addressed);
341
342         my $function = lc $1;
343         my $faqtoid  = lc $4;
344
345         if ($faqtoid eq "") {
346             &help($function);
347             return;
348         }
349
350         if (&getFactoid($faqtoid) eq "") {
351             &msg($who, "factoid \002$faqtoid\002 does not exist");
352             return;
353         }
354
355         if ($function eq "lock") {
356             # strongly requested by #debian on 19991028. -xk
357             if (1 and $faqtoid !~ /^\Q$who\E$/i and &IsFlag("o") ne "o") {
358                 &msg($who,"sorry, locking cannot be used since it can be abused unneccesarily.");
359                 &status("Replace 1 with 0 in Process.pl#~324 for locking support.");
360                 return;
361             }
362
363             &CmdLock($faqtoid);
364         } else {
365             &CmdUnLock($faqtoid);
366         }
367
368         return;
369     }
370
371     # factoid rename.
372     if ($message =~ s/^rename(\s+|$)//) {
373         return 'rename: no addr' unless ($addressed);
374
375         if ($message eq "") {
376             &help("rename");
377             return;
378         }
379
380         if ($message =~ /^'(.*)'\s+'(.*)'$/) {
381             my ($from,$to) = (lc $1, lc $2);
382
383             my $result = &getFactoid($from);
384             if (defined $result) {
385                 &performReply("i didn't have anything called '$from'");
386                 return;
387             }
388
389             my $author = &getFactInfo($from, "created_by");
390
391             # who == nick!user@host.
392             if (&IsFlag("m") ne "m" and $author !~ /^\Q$who\E\!/i) {
393                 &msg($who, "factoid '$form' is not yours to modify.");
394                 return;
395             }
396
397             if ($_ = &getFactoid($to)) {
398                 &performReply("destination factoid already exists.");
399                 return;
400             }
401
402             &setFactInfo($from,"factoid_key",$to);
403
404             &status("rename: <$who> '$from' is now '$to'");
405             &performReply("i renamed '$from' to '$to'");
406         } else {
407             &msg($who,"error: wrong format. ask me about 'help rename'.");
408         }
409
410         return;
411     }
412
413     # factoid substitution. (X =~ s/A/B/FLAG)
414     if ($message =~ m|^(.*?)\s+=~\s+s([/,#])(.+?)\2(.*?)\2([a-z]*);?\s*$|) {
415         my ($faqtoid,$delim,$op,$np,$flags) = (lc $1, $2, $3, $4, $5);
416         return 'subst: no addr' unless ($addressed);
417
418         # incorrect format.
419         if ($np =~ /$delim/) {
420             &msg($who,"looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
421             return;
422         }
423
424         # success.
425         if (my $result = &getFactoid($faqtoid)) {
426             return 'subst: locked' if (&IsLocked($faqtoid) == 1);
427             my $was = $result;
428
429             if (($flags eq "g" && $result =~ s/\Q$op/$np/gi) || $result =~ s/\Q$op/$np/i) {
430                 # excessive length.
431                 if (length $result > $param{'maxDataSize'}) {
432                     &performReply("that's too long");
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                         &IsHostMask($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'");
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/ig;
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("perlMath") > 0 and $addressed) { # perl math.
501         &loadMyModule("perlMath");
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;