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