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