2 # Misc.pl: Miscellaneous stuff.
4 # Version: v0.1 (20010906)
10 use vars qw(%param %cache %lang %cmdstats %bots);
11 use vars qw($message $who $addressed $chan $h $nuh $ident $msgType
12 $correction_plausable);
14 # Usage: &validFactoid($lhs,$rhs);
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.
24 /^(it|that|there|what)('s)?(\s+|$)/ and last;
25 /^you('re)?(\s+|$)/ and last;
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;
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.
42 /^\=/ and last; # botnick = heh is.
43 /wants you to know/ and last;
48 (/^'/ and /'$/) and last;
49 (/^"/ and /"$/) and last;
52 /\=\>/ and last; # '=>'.
53 /\;\;/ and last; # ';;'.
54 /\|\|/ and last; # '||'.
56 /^\Q$ident\E[\'\,\: ]/ and last;# dupe addressed.
58 /\\$/ and last; # forgot shift for '?'.
66 /^h(is|er) / and last;
74 /^supposedly/ and last;
79 # nasty bug I introduced _somehow_, probably by fixMySQLBug().
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.
87 $rhs =~ /^\Q$lhs /i and last;
88 last if ($rhs =~ /^is /i and / is$/);
98 if ($msgType =~ /private/ and $message =~ s/^:INFOBOT://) {
100 &status("infobot <$nuh> identified") unless $bots{$nuh};
106 if ($message =~ /^QUERY (<.*?>) (.*)/) { # query.
107 my ($target,$item) = ($1,$2);
110 &status(":INFOBOT:QUERY $who: $message");
112 if ($_ = &getFactoid($item)) {
113 &msg($who, ":INFOBOT:REPLY $target $item =is=> $_");
116 return 'INFOBOT QUERY';
117 } elsif ($message =~ /^REPLY <(.*?)> (.*)/) { # reply.
118 my ($target,$item) = ($1,$2);
120 &status(":INFOBOT:REPLY $who: $message");
122 my ($lhs,$mhs,$rhs) = $item =~ /^(.*?) =(.*?)=> (.*)/;
124 if ($param{'acceptUrl'} !~ /REQUIRE/ or $rhs =~ /(http|ftp|mailto|telnet|file):/) {
125 &msg($target, "$who knew: $lhs $mhs $rhs");
128 $rhs = "<REPLY> are" if ($mhs eq "are");
129 &setFactInfo($lhs, "factoid_value", $rhs);
132 return 'INFOBOT REPLY';
134 &ERROR(":INFOBOT:UNKNOWN $who: $message");
135 return 'INFOBOT UNKNOWN';
140 if ($message =~ s/^forget\s+//i) {
141 return 'forget: no addr' unless ($addressed);
143 my $faqtoid = $message;
144 if ($faqtoid eq "") {
149 $faqtoid =~ tr/A-Z/a-z/;
150 $faqtoid =~ s/^cmd:/CMD:/; # basic command forgetting
151 my $result = &getFactoid($faqtoid);
153 # if it doesn't exist, well... it doesn't!
154 if (!defined $result) {
155 &performReply("i didn't have anything called '$faqtoid'");
159 # TODO: squeeze 3 getFactInfo calls into one?
160 my $author = &getFactInfo($faqtoid, "created_by");
161 my $count = &getFactInfo($faqtoid, "requested_count") || 0;
162 # don't delete if requested $limit times
163 my $limit = &getChanConfDefault(
164 "factoidPreventForgetLimit", 100, $chan);
165 # don't delete if older than $limitage seconds (modified by requests below)
166 my $limitage = &getChanConfDefault(
167 "factoidPreventForgetLimitTime", 7 * 24 * 60 * 60, $chan);
168 my $t = &getFactInfo($faqtoid, "created_time") || 0;
169 my $age = time() - $t;
171 # lets scale limitage from 1 (nearly 0) to $limit (full time).
172 $limitage = $limitage*($count+1)/$limit if ($count < $limit);
174 my $isau = (defined $author and &IsHostMatch($author) == 2) ? 1 : 0;
175 my $isop = (&IsFlag("o") eq "o") ? 1 : 0;
177 if (IsFlag("r") ne "r" && !$isop) {
178 &msg($who, "you don't have access to remove factoids");
182 return 'locked factoid' if (&IsLocked($faqtoid) == 1);
185 ### lets go do some checking.
188 # factoidPreventForgetLimitTime:
189 if (!($isop or $isau) and $age/(60*60*24) > $limitage) {
190 &msg($who, "cannot remove factoid '$faqtoid', too old. (" .
191 $age/(60*60*24) . ">$limitage) use 'no,' instead");
195 # factoidPreventForgetLimit:
196 if (!($isop or $isau) and $limit and $count > $limit) {
197 &msg($who, "will not delete '$faqtoid', count > limit ($count > $limit) use 'no, ' instead.");
201 # this may eat some memory.
202 # prevent deletion if other factoids redirect to it.
203 # TODO: use hash instead of array.
205 if (&getChanConf("factoidPreventForgetRedirect")) {
206 &status("Factoids/Core: forget: checking for redirect factoids");
207 @list = &searchTable("factoids", "factoid_key",
208 "factoid_value", "^<REPLY> see ");
214 my $v = &getFactInfo($f, "factoid_value");
215 my $fsafe = quotemeta($faqtoid);
216 next unless ($v =~ /^<REPLY> ?see( also)? $fsafe\.?$/i);
218 &DEBUG("Factoids/Core: match! ($f || $faqtoid)");
222 # TODO: warn for op aswell, but allow force delete.
223 if (!$isop and $match) {
224 &msg($who, "uhm, other (redirection) factoids depend on this one.");
229 if (!$isop and &IsHostMatch($author) != 2) {
230 $cache{forget}{$h}++;
233 if ($cache{forget}{$h} > 3) {
234 &msg($who, "Stop abusing forget!");
238 # TODO: make forget limit configurable.
239 # TODO: make forget ignore time configurable.
240 if ($cache{forget}{$h} > 5) {
241 &ignoreAdd(&makeHostMask($nuh), "*", 3*24*60, "abuse of forget");
242 &msg($who, "forget: Ignoring you for abuse!");
248 if (&IsParam("factoidDeleteDelay") or &IsChanConf("factoidDeleteDelay")) {
249 if (!($isop or $isau) and $faqtoid =~ / #DEL#$/) {
250 &msg($who, "cannot delete it ($faqtoid).");
254 &status("forgot (safe delete): '$faqtoid' - ". scalar(gmtime));
255 ### TODO: check if the "backup" exists and overwrite it
256 my $check = &getFactoid("$faqtoid #DEL#");
258 if (!defined $check or $check =~ /^\s*$/) {
259 if ($faqtoid !~ / #DEL#$/) {
260 my $new = $faqtoid." #DEL#";
262 my $backup = &getFactoid($new);
264 &DEBUG("forget: not overwriting backup: $faqtoid");
266 &status("forget: backing up '$faqtoid'");
267 &setFactInfo($faqtoid, "factoid_key", $new);
268 &setFactInfo($new, "modified_by", $who);
269 &setFactInfo($new, "modified_time", time());
273 &status("forget: not backing up $faqtoid.");
277 &status("forget: not overwriting backup!");
281 &status("forget: <$who> '$faqtoid' =is=> '$result'");
282 &delFactoid($faqtoid);
284 &performReply("i forgot $faqtoid");
291 # factoid unforget/undelete.
292 if ($message =~ s/^un(forget|delete)\s+//i) {
293 return 'unforget: no addr' unless ($addressed);
296 $i++ if (&IsParam("factoidDeleteDelay"));
297 $i++ if (&IsChanConf("factoidDeleteDelay"));
299 &performReply("safe delete has been disable so what is there to undelete?");
303 my $faqtoid = $message;
304 if ($faqtoid eq "") {
309 $faqtoid =~ tr/A-Z/a-z/;
310 $faqtoid =~ s/^cmd:/CMD:/; # basic command undeleteing
311 my $result = &getFactoid($faqtoid." #DEL#");
312 my $check = &getFactoid($faqtoid);
314 if (defined $check) {
315 &performReply("cannot undeleted '$faqtoid' because it already exists!");
319 if (!defined $result) {
320 &performReply("that factoid was not backedup :/");
324 &setFactInfo($faqtoid." #DEL#", "factoid_key", $faqtoid);
325 # &setFactInfo($faqtoid, "modified_by", "");
326 # &setFactInfo($faqtoid, "modified_time", 0);
328 $check = &getFactoid($faqtoid);
329 # TODO: check if $faqtoid." #DEL#" exists?
330 if (defined $check) {
331 &performReply("Successfully recovered '$faqtoid'. Have fun now.");
332 $count{'Undelete'}++;
334 &performReply("did not recover '$faqtoid'. What happened?");
341 if ($message =~ /^((un)?lock)(\s+(.*))?\s*?$/i) {
342 return 'lock: no addr 2' unless ($addressed);
344 my $function = lc $1;
347 if ($faqtoid eq "") {
352 if (&getFactoid($faqtoid) eq "") {
353 &msg($who, "factoid \002$faqtoid\002 does not exist");
357 if ($function eq "lock") {
358 # strongly requested by #debian on 19991028. -xk
359 if (1 and $faqtoid !~ /^\Q$who\E$/i and &IsFlag("o") ne "o") {
360 &msg($who,"sorry, locking cannot be used since it can be abused unneccesarily.");
361 &status("Replace 1 with 0 in Process.pl#~324 for locking support.");
367 &CmdUnLock($faqtoid);
374 if ($message =~ s/^rename(\s+|$)//) {
375 return 'rename: no addr' unless ($addressed);
377 if ($message eq "") {
382 if ($message =~ /^'(.*)'\s+'(.*)'$/) {
383 my ($from,$to) = (lc $1, lc $2);
385 my $result = &getFactoid($from);
386 if (defined $result) {
387 &performReply("i didn't have anything called '$from'");
391 my $author = &getFactInfo($from, "created_by");
393 # who == nick!user@host.
394 if (&IsFlag("m") ne "m" and $author !~ /^\Q$who\E\!/i) {
395 &msg($who, "factoid '$form' is not yours to modify.");
399 if ($_ = &getFactoid($to)) {
400 &performReply("destination factoid already exists.");
404 &setFactInfo($from,"factoid_key",$to);
406 &status("rename: <$who> '$from' is now '$to'");
407 &performReply("i renamed '$from' to '$to'");
409 &msg($who,"error: wrong format. ask me about 'help rename'.");
415 # factoid substitution. (X =~ s/A/B/FLAG)
416 if ($message =~ m|^(.*?)\s+=~\s+s([/,#])(.+?)\2(.*?)\2([a-z]*);?\s*$|) {
417 my ($faqtoid,$delim,$op,$np,$flags) = (lc $1, $2, $3, $4, $5);
418 return 'subst: no addr' unless ($addressed);
421 if ($np =~ /$delim/) {
422 &msg($who,"looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
427 if (my $result = &getFactoid($faqtoid)) {
428 return 'subst: locked' if (&IsLocked($faqtoid) == 1);
431 if (($flags eq "g" && $result =~ s/\Q$op/$np/gi) || $result =~ s/\Q$op/$np/i) {
433 if (length $result > $param{'maxDataSize'}) {
434 &performReply("that's too long");
438 my $faqauth = &getFactInfo($faqtoid, "created_by");
439 if ((length $result)*2 < length $was and
440 &IsFlag("o") ne "o" and
441 &IsHostMask($faqauth) != 2
443 &performReply("too drastic change of factoid.");
446 &setFactInfo($faqtoid, "factoid_value", $result);
447 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
450 &performReply("that doesn't contain '$op'");
453 &performReply("i didn't have anything called '$faqtoid'");
459 # Fix up $message for question.
460 my $question = $message;
463 s/^hey([, ]+)where/where/i;
466 s/where can i find/where is/i;
467 s/how about/where is/i;
470 # clear the string of useless words.
471 s/^(stupid )?q(uestion)?:\s+//i;
472 s/^(does )?(any|ne)(1|one|body) know //i;
474 s/^[uh]+m*[,\.]* +//i;
478 s/^(gee|boy|golly|gosh)([, ]+)//i;
479 s/^(well|and|but|or|yes)([, ]+)//i;
481 s/^o+[hk]+(a+y+)?([,. ]+)//i;
482 s/^g(eez|osh|olly)([,. ]+)//i;
483 s/^w(ow|hee|o+ho+)([,. ]+)//i;
484 s/^heya?,?( folks)?([,. ]+)//i;
487 if ($addressed and $message =~ s/^no([, ]+)(\Q$ident\E\,+)?\s*//i) {
488 $correction_plausible = 1;
489 &status("correction is plausible, initial negative and nick deleted ($&)") if ($param{VERBOSITY});
491 $correction_plausible = 0;
494 my $result = &doQuestion($question);
495 if (!defined $result or $result eq $noreply) {
496 return 'result from doQ undef.';
499 if (defined $result and $result !~ /^0?$/) { # question.
500 &status("question: <$who> $message");
501 $count{'Question'}++;
502 } elsif (&IsChanConf("perlMath") > 0 and $addressed) { # perl math.
503 &loadMyModule("perlMath");
504 my $newresult = &perlMath();
506 if (defined $newresult and $newresult ne "") {
507 $cmdstats{'Maths'}++;
508 $result = $newresult;
509 &status("math: <$who> $message => $result");
513 if ($result !~ /^0?$/) {
514 &performStrictReply($result);
518 # why would a friendly bot get passed here?
519 if (&IsParam("friendlyBots")) {
520 return if (grep lc($_) eq lc($who), split(/\s+/, $param{'friendlyBots'}));
524 if (!defined &doStatement($message)) {
528 return unless ($addressed and !$addrchar);
530 if (length $message > 64) {
531 &status("unparseable-moron: $message");
532 # &performReply( &getRandom(keys %{ $lang{'moron'} }) );
535 &performReply("You are moron \002#". $count{'Moron'} ."\002");
539 &status("unparseable: $message");
540 &performReply( &getRandom(keys %{ $lang{'dunno'} }) );