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.
41 /^\=/ and last; # botnick = heh is.
42 /wants you to know/ and last;
47 (/^'/ and /'$/) and last;
48 (/^"/ and /"$/) and last;
51 /\=\>/ and last; # '=>'.
52 /\;\;/ and last; # ';;'.
53 /\|\|/ and last; # '||'.
55 /^\Q$ident\E[\'\,\: ]/ and last;# dupe addressed.
57 /\\$/ and last; # forgot shift for '?'.
65 /^h(is|er) / and last;
73 /^supposedly/ and last;
78 # nasty bug I introduced _somehow_, probably by fixMySQLBug().
82 # weird/special stuff. also old blootbot or stock infobot bugs.
83 $rhs =~ /( \Q$ident\E's|\Q$ident\E's )/i and last; # ownership.
86 $rhs =~ /^\Q$lhs /i and last;
87 last if ($rhs =~ /^is /i and / is$/);
97 if ($msgType =~ /private/ and $message =~ s/^:INFOBOT://) {
99 &status("infobot <$nuh> identified") unless $bots{$nuh};
105 if ($message =~ /^QUERY (<.*?>) (.*)/) { # query.
106 my ($target,$item) = ($1,$2);
109 &status(":INFOBOT:QUERY $who: $message");
111 if ($_ = &getFactoid($item)) {
112 &msg($who, ":INFOBOT:REPLY $target $item =is=> $_");
115 return 'INFOBOT QUERY';
116 } elsif ($message =~ /^REPLY <(.*?)> (.*)/) { # reply.
117 my ($target,$item) = ($1,$2);
119 &status(":INFOBOT:REPLY $who: $message");
121 my ($lhs,$mhs,$rhs) = $item =~ /^(.*?) =(.*?)=> (.*)/;
123 if ($param{'acceptUrl'} !~ /REQUIRE/ or $rhs =~ /(http|ftp|mailto|telnet|file):/) {
124 &msg($target, "$who knew: $lhs $mhs $rhs");
127 $rhs = "<REPLY> are" if ($mhs eq "are");
128 &setFactInfo($lhs, "factoid_value", $rhs);
131 return 'INFOBOT REPLY';
133 &ERROR(":INFOBOT:UNKNOWN $who: $message");
134 return 'INFOBOT UNKNOWN';
139 if ($message =~ s/^forget\s+//i) {
140 return 'forget: no addr' unless ($addressed);
142 my $faqtoid = $message;
143 if ($faqtoid eq "") {
148 $faqtoid =~ tr/A-Z/a-z/;
149 my $result = &getFactoid($faqtoid);
151 # if it doesn't exist, well... it doesn't!
152 if (!defined $result) {
153 &performReply("i didn't have anything called '$faqtoid' to forget");
157 # TODO: squeeze 3 getFactInfo calls into one?
158 my $author = &getFactInfo($faqtoid, "created_by");
159 my $count = &getFactInfo($faqtoid, "requested_count") || 0;
160 # don't delete if requested $limit times
161 my $limit = &getChanConfDefault('factoidPreventForgetLimit', 100, $chan);
162 # don't delete if older than $limitage seconds (modified by requests below)
163 my $limitage = &getChanConfDefault('factoidPreventForgetLimitTime', 7 * 24 * 60 * 60, $chan);
164 my $t = &getFactInfo($faqtoid, "created_time") || 0;
165 my $age = time() - $t;
167 # lets scale limitage from 1 (nearly 0) to $limit (full time).
168 $limitage = $limitage*($count+1)/$limit if ($count < $limit);
170 my $isau = (defined $author and &IsHostMatch($author) == 2) ? 1 : 0;
171 my $isop = (&IsFlag("o") eq "o") ? 1 : 0;
173 if (IsFlag("r") ne "r" && !$isop) {
174 &msg($who, "you don't have access to remove factoids");
178 return 'locked factoid' if (&IsLocked($faqtoid) == 1);
181 ### lets go do some checking.
184 # factoidPreventForgetLimitTime:
185 if (!($isop or $isau) and $age/(60*60*24) > $limitage) {
186 &msg($who, "cannot remove factoid '$faqtoid', too old. (" .
187 $age/(60*60*24) . ">$limitage) use 'no,' instead");
191 # factoidPreventForgetLimit:
192 if (!($isop or $isau) and $limit and $count > $limit) {
193 &msg($who, "will not delete '$faqtoid', count > limit ($count > $limit) use 'no, ' instead.");
197 # this may eat some memory.
198 # prevent deletion if other factoids redirect to it.
199 # TODO: use hash instead of array.
201 if (&getChanConf("factoidPreventForgetRedirect")) {
202 &status("Factoids/Core: forget: checking for redirect factoids");
203 @list = &searchTable("factoids", "factoid_key",
204 "factoid_value", "^<REPLY> see ");
210 my $v = &getFactInfo($f, "factoid_value");
211 my $fsafe = quotemeta($faqtoid);
212 next unless ($v =~ /^<REPLY> ?see( also)? $fsafe\.?$/i);
214 &DEBUG("Factoids/Core: match! ($f || $faqtoid)");
218 # TODO: warn for op aswell, but allow force delete.
219 if (!$isop and $match) {
220 &msg($who, "uhm, other (redirection) factoids depend on this one.");
225 if (!$isop and &IsHostMatch($author) != 2) {
226 $cache{forget}{$h}++;
229 if ($cache{forget}{$h} > 3) {
230 &msg($who, "Stop abusing forget!");
234 # TODO: make forget limit configurable.
235 # TODO: make forget ignore time configurable.
236 if ($cache{forget}{$h} > 5) {
237 &ignoreAdd(&makeHostMask($nuh), "*", 3*24*60, "abuse of forget");
238 &msg($who, "forget: Ignoring you for abuse!");
244 if (&IsParam("factoidDeleteDelay") or &IsChanConf("factoidDeleteDelay")) {
245 if (!($isop or $isau) and $faqtoid =~ / #DEL#$/) {
246 &msg($who, "cannot delete it ($faqtoid).");
250 &status("forgot (safe delete): '$faqtoid' - ". scalar(gmtime));
251 ### TODO: check if the "backup" exists and overwrite it
252 my $check = &getFactoid("$faqtoid #DEL#");
254 if (!defined $check or $check =~ /^\s*$/) {
255 if ($faqtoid !~ / #DEL#$/) {
256 my $new = $faqtoid." #DEL#";
258 my $backup = &getFactoid($new);
260 &DEBUG("forget: not overwriting backup: $faqtoid");
262 &status("forget: backing up '$faqtoid'");
263 &setFactInfo($faqtoid, "factoid_key", $new);
264 &setFactInfo($new, "modified_by", $who);
265 &setFactInfo($new, "modified_time", time());
269 &status("forget: not backing up $faqtoid.");
273 &status("forget: not overwriting backup!");
277 &status("forget: <$who> '$faqtoid' =is=> '$result'");
278 &delFactoid($faqtoid);
280 &performReply("i forgot $faqtoid");
287 # factoid unforget/undelete.
288 if ($message =~ s/^un(forget|delete)\s+//i) {
289 return 'unforget: no addr' unless ($addressed);
292 $i++ if (&IsParam("factoidDeleteDelay"));
293 $i++ if (&IsChanConf("factoidDeleteDelay"));
295 &performReply("safe delete has been disable so what is there to undelete?");
299 my $faqtoid = $message;
300 if ($faqtoid eq "") {
305 $faqtoid =~ tr/A-Z/a-z/;
306 my $result = &getFactoid($faqtoid." #DEL#");
307 my $check = &getFactoid($faqtoid);
309 if (defined $check) {
310 &performReply("cannot undeleted '$faqtoid' because it already exists!");
314 if (!defined $result) {
315 &performReply("that factoid was not backedup :/");
319 &setFactInfo($faqtoid." #DEL#", "factoid_key", $faqtoid);
320 # &setFactInfo($faqtoid, "modified_by", "");
321 # &setFactInfo($faqtoid, "modified_time", 0);
323 $check = &getFactoid($faqtoid);
324 # TODO: check if $faqtoid." #DEL#" exists?
325 if (defined $check) {
326 &performReply("Successfully recovered '$faqtoid'. Have fun now.");
327 $count{'Undelete'}++;
329 &performReply("did not recover '$faqtoid'. What happened?");
336 if ($message =~ /^((un)?lock)(\s+(.*))?\s*?$/i) {
337 return 'lock: no addr 2' unless ($addressed);
339 my $function = lc $1;
342 if ($faqtoid eq "") {
347 if (&getFactoid($faqtoid) eq "") {
348 &msg($who, "factoid \002$faqtoid\002 does not exist");
352 if ($function eq "lock") {
353 # strongly requested by #debian on 19991028. -xk
354 if (1 and $faqtoid !~ /^\Q$who\E$/i and &IsFlag("o") ne "o") {
355 &msg($who,"sorry, locking cannot be used since it can be abused unneccesarily.");
356 &status("Replace 1 with 0 in Process.pl#~324 for locking support.");
362 &CmdUnLock($faqtoid);
369 if ($message =~ s/^rename(\s+|$)//) {
370 return 'rename: no addr' unless ($addressed);
372 if ($message eq "") {
377 if ($message =~ /^'(.*)'\s+'(.*)'$/) {
378 my ($from,$to) = (lc $1, lc $2);
380 my $result = &getFactoid($from);
381 if (!defined $result) {
382 &performReply("i didn't have anything called '$from' to rename");
386 # who == nick!user@host.
387 if (&IsFlag("m") ne "m" and $author !~ /^\Q$who\E\!/i) {
388 &msg($who, "factoid '$from' is not yours to modify.");
392 if ($_ = &getFactoid($to)) {
393 &performReply("destination factoid already exists.");
397 &setFactInfo($from,"factoid_key",$to);
399 &status("rename: <$who> '$from' is now '$to'");
400 &performReply("i renamed '$from' to '$to'");
402 &msg($who,"error: wrong format. ask me about 'help rename'.");
408 # factoid substitution. (X =~ s/A/B/FLAG)
409 if ($message =~ m|^(.*?)\s+=~\s+s([/,#])(.+?)\2(.*?)\2([a-z]*);?\s*$|) {
410 my ($faqtoid,$delim,$op,$np,$flags) = (lc $1, $2, $3, $4, $5);
411 return 'subst: no addr' unless ($addressed);
414 if ($np =~ /$delim/) {
415 &msg($who,"looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
420 if (my $result = &getFactoid($faqtoid)) {
421 return 'subst: locked' if (&IsLocked($faqtoid) == 1);
424 if (($flags eq "g" && $result =~ s/\Q$op/$np/gi) || $result =~ s/\Q$op/$np/i) {
426 if (length $result > $param{'maxDataSize'}) {
427 &performReply("that's too long");
431 my $faqauth = &getFactInfo($faqtoid, "created_by");
432 if ((length $result)*2 < length $was and
433 &IsFlag("o") ne "o" and
434 &IsHostMask($faqauth) != 2
436 &performReply("too drastic change of factoid.");
439 &setFactInfo($faqtoid, "factoid_value", $result);
440 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
443 &performReply("that doesn't contain '$op'");
446 &performReply("i didn't have anything called '$faqtoid' to modify");
452 # Fix up $message for question.
453 my $question = $message;
456 s/^hey([, ]+)where/where/i;
459 s/where can i find/where is/i;
460 s/how about/where is/i;
463 # clear the string of useless words.
464 s/^(stupid )?q(uestion)?:\s+//i;
465 s/^(does )?(any|ne)(1|one|body) know //i;
467 s/^[uh]+m*[,\.]* +//i;
471 s/^(gee|boy|golly|gosh)([, ]+)//i;
472 s/^(well|and|but|or|yes)([, ]+)//i;
474 s/^o+[hk]+(a+y+)?([,. ]+)//i;
475 s/^g(eez|osh|olly)([,. ]+)//i;
476 s/^w(ow|hee|o+ho+)([,. ]+)//i;
477 s/^heya?,?( folks)?([,. ]+)//i;
480 if ($addressed and $message =~ s/^no([, ]+)(\Q$ident\E\,+)?\s*//i) {
481 $correction_plausible = 1;
482 &status("correction is plausible, initial negative and nick deleted ($&)") if ($param{VERBOSITY});
484 $correction_plausible = 0;
487 my $result = &doQuestion($question);
488 if (!defined $result or $result eq $noreply) {
489 return 'result from doQ undef.';
492 if (defined $result and $result !~ /^0?$/) { # question.
493 &status("question: <$who> $message");
494 $count{'Question'}++;
495 } elsif (&IsChanConf('Math') > 0 and $addressed) { # perl math.
496 &loadMyModule('Math');
497 my $newresult = &perlMath();
499 if (defined $newresult and $newresult ne "") {
500 $cmdstats{'Maths'}++;
501 $result = $newresult;
502 &status("math: <$who> $message => $result");
506 if ($result !~ /^0?$/) {
507 &performStrictReply($result);
511 # why would a friendly bot get passed here?
512 if (&IsParam('friendlyBots')) {
513 return if (grep lc($_) eq lc($who), split(/\s+/, $param{'friendlyBots'}));
517 if (!defined &doStatement($message)) {
521 return unless ($addressed and !$addrchar);
523 if (length $message > 64) {
524 &status("unparseable-moron: $message");
525 # &performReply( &getRandom(keys %{ $lang{'moron'} }) );
528 &performReply("You are moron \002#". $count{'Moron'} ."\002");
532 &status("unparseable: $message");
533 &performReply( &getRandom(keys %{ $lang{'dunno'} }) );