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