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 use Encode qw(decode_utf8 encode_utf8);
16 # Usage: &validFactoid($lhs,$rhs);
18 my ( $lhs, $rhs ) = @_;
23 # allow the following only if they have been made on purpose.
24 if ( $rhs ne '' and $rhs !~ /^</ ) {
25 / \Q$ident\E$/i and last; # someone said i'm something.
27 /^(it|that|there|what)('s)?(\s+|$)/ and last;
28 /^you('re)?(\s+|$)/ and last;
30 /^(where|who|why|when|how)(\s+|$)/ and last;
31 /^(this|that|these|those|they)(\s+|$)/ and last;
32 /^(every(one|body)|we) / and last;
38 /^add topic / and last; # topic management.
39 /( add$| add |^add )/ and last; # borked teach statement.
40 /^learn / and last; # teach. damn morons.
41 /^tell (\S+) about / and last; # tell.
42 /\=\~/ and last; # substituition.
44 /^\=/ and last; # botnick = heh is.
45 /wants you to know/ and last;
50 ( /^'/ and /'$/ ) and last;
51 ( /^"/ and /"$/ ) and last;
54 /\=\>/ and last; # '=>'.
55 /\;\;/ and last; # ';;'.
56 /\|\|/ and last; # '||'.
58 /^\Q$ident\E[\'\,\: ]/ and last; # dupe addressed.
60 /\\$/ and last; # forgot shift for '?'.
68 /^h(is|er) / and last;
76 /^supposedly/ and last;
81 # nasty bug I introduced _somehow_, probably by fixMySQLBug().
85 # weird/special stuff. also old infobot bugs.
86 $rhs =~ /( \Q$ident\E's|\Q$ident\E's )/i and last; # ownership.
89 $rhs =~ /^\Q$lhs\E /i and last;
90 last if ( $rhs =~ /^is /i and / is$/ );
101 if ( $msgType =~ /private/ and $message =~ s/^:INFOBOT:// ) {
103 &status("infobot <$nuh> identified") unless $bots{$nuh};
109 if ( $message =~ /^QUERY (<.*?>) (.*)/ ) { # query.
110 my ( $target, $item ) = ( $1, $2 );
113 &status(":INFOBOT:QUERY $who: $message");
115 if ( $_ = &getFactoid($item) ) {
116 &msg( $who, ":INFOBOT:REPLY $target $item =is=> $_" );
119 return 'INFOBOT QUERY';
121 elsif ( $message =~ /^REPLY <(.*?)> (.*)/ ) { # reply.
122 my ( $target, $item ) = ( $1, $2 );
124 &status(":INFOBOT:REPLY $who: $message");
126 my ( $lhs, $mhs, $rhs ) = $item =~ /^(.*?) =(.*?)=> (.*)/;
128 if ( $param{'acceptUrl'} !~ /REQUIRE/
129 or $rhs =~ /(http|ftp|mailto|telnet|file):/ )
131 &msg( $target, "$who knew: $lhs $mhs $rhs" );
134 $rhs = "<REPLY> are" if ( $mhs eq 'are' );
135 &setFactInfo( $lhs, 'factoid_value', $rhs );
138 return 'INFOBOT REPLY';
141 &ERROR(":INFOBOT:UNKNOWN $who: $message");
142 return 'INFOBOT UNKNOWN';
147 if ( $message =~ s/^forget\s+//i ) {
148 return 'forget: no addr' unless ($addressed);
150 my $faqtoid = $message;
151 if ( $faqtoid eq '' ) {
156 $faqtoid =~ tr/A-Z/a-z/;
157 my $result = &getFactoid($faqtoid);
159 # if it doesn't exist, well... it doesn't!
160 if ( !defined $result ) {
161 &performReply("i didn't have anything called '$faqtoid' to forget");
165 # TODO: squeeze 3 getFactInfo calls into one?
166 my $author = &getFactInfo( $faqtoid, 'created_by' );
167 my $count = &getFactInfo( $faqtoid, 'requested_count' ) || 0;
169 # don't delete if requested $limit times
171 &getChanConfDefault( 'factoidPreventForgetLimit', 100, $chan );
173 # don't delete if older than $limitage seconds (modified by requests below)
174 my $limitage = &getChanConfDefault( 'factoidPreventForgetLimitTime',
175 7 * 24 * 60 * 60, $chan );
176 my $t = &getFactInfo( $faqtoid, 'created_time' ) || 0;
177 my $age = time() - $t;
179 # lets scale limitage from 1 (nearly 0) to $limit (full time).
180 $limitage = $limitage * ( $count + 1 ) / $limit if ( $count < $limit );
183 my $isau = ( defined $author and &IsHostMatch($author) == 2 ) ? 1 : 0;
184 my $isop = ( &IsFlag('o') eq 'o' ) ? 1 : 0;
186 if ( IsFlag('r') ne 'r' && !$isop ) {
187 &msg( $who, "you don't have access to remove factoids" );
191 return 'locked factoid' if ( &IsLocked($faqtoid) == 1 );
194 ### lets go do some checking.
197 # factoidPreventForgetLimitTime:
198 if ( !( $isop or $isau ) and $age / ( 60 * 60 * 24 ) > $limitage ) {
200 "cannot remove factoid '$faqtoid', too old. ("
201 . $age / ( 60 * 60 * 24 )
202 . ">$limitage) use 'no,' instead" );
206 # factoidPreventForgetLimit:
207 if ( !( $isop or $isau ) and $limit and $count > $limit ) {
209 "will not delete '$faqtoid', count > limit ($count > $limit) use 'no, ' instead."
214 # this may eat some memory.
215 # prevent deletion if other factoids redirect to it.
216 # TODO: use hash instead of array.
218 if ( &getChanConf('factoidPreventForgetRedirect') ) {
219 &status("Factoids/Core: forget: checking for redirect factoids");
221 &searchTable( 'factoids', 'factoid_key', 'factoid_value',
228 my $v = &getFactInfo( $f, 'factoid_value' );
229 my $fsafe = quotemeta($faqtoid);
230 next unless ( $v =~ /^<REPLY> ?see( also)? $fsafe\.?$/i );
232 &DEBUG("Factoids/Core: match! ($f || $faqtoid)");
237 # TODO: warn for op aswell, but allow force delete.
238 if ( !$isop and $match ) {
240 "uhm, other (redirection) factoids depend on this one." );
245 if ( !$isop and &IsHostMatch($author) != 2 ) {
246 $cache{forget}{$h}++;
249 if ( $cache{forget}{$h} > 3 ) {
250 &msg( $who, "Stop abusing forget!" );
254 # TODO: make forget limit configurable.
255 # TODO: make forget ignore time configurable.
256 if ( $cache{forget}{$h} > 5 ) {
258 &makeHostMask($nuh), '*',
262 &msg( $who, "forget: Ignoring you for abuse!" );
268 if ( &IsParam('factoidDeleteDelay')
269 or &IsChanConf('factoidDeleteDelay') > 0 )
271 if ( !( $isop or $isau ) and $faqtoid =~ / #DEL#$/ ) {
272 &msg( $who, "cannot delete it ($faqtoid)." );
276 &status( "forgot (safe delete): '$faqtoid' - " . scalar(gmtime) );
277 ### TODO: check if the 'backup' exists and overwrite it
278 my $check = &getFactoid("$faqtoid #DEL#");
280 if ( !defined $check or $check =~ /^\s*$/ ) {
281 if ( $faqtoid !~ / #DEL#$/ ) {
282 my $new = $faqtoid . " #DEL#";
284 my $backup = &getFactoid($new);
286 &DEBUG("forget: not overwriting backup: $faqtoid");
289 &status("forget: backing up '$faqtoid'");
290 &setFactInfo( $faqtoid, 'factoid_key', $new );
291 &setFactInfo( $new, 'modified_by', $who );
292 &setFactInfo( $new, 'modified_time', time() );
297 &status("forget: not backing up $faqtoid.");
302 &status("forget: not overwriting backup!");
306 &status("forget: <$who> '$faqtoid' =is=> '$result'");
307 &delFactoid($faqtoid);
309 &performReply("i forgot $faqtoid");
316 # factoid unforget/undelete.
317 if ( $message =~ s/^un(forget|delete)\s+//i ) {
318 return 'unforget: no addr' unless ($addressed);
321 $i++ if ( &IsParam('factoidDeleteDelay') );
322 $i++ if ( &IsChanConf('factoidDeleteDelay') > 0 );
325 "safe delete has been disable so what is there to undelete?");
329 my $faqtoid = $message;
330 if ( $faqtoid eq '' ) {
335 $faqtoid =~ tr/A-Z/a-z/;
336 my $result = &getFactoid( $faqtoid . " #DEL#" );
337 my $check = &getFactoid($faqtoid);
339 if ( defined $check ) {
341 "cannot undeleted '$faqtoid' because it already exists!");
345 if ( !defined $result ) {
346 &performReply("that factoid was not backedup :/");
350 &setFactInfo( $faqtoid . " #DEL#", 'factoid_key', $faqtoid );
352 # &setFactInfo($faqtoid, 'modified_by', '');
353 # &setFactInfo($faqtoid, 'modified_time', 0);
355 $check = &getFactoid($faqtoid);
357 # TODO: check if $faqtoid." #DEL#" exists?
358 if ( defined $check ) {
359 &performReply("Successfully recovered '$faqtoid'. Have fun now.");
360 $count{'Undelete'}++;
363 &performReply("did not recover '$faqtoid'. What happened?");
370 if ( $message =~ /^((un)?lock)(\s+(.*))?\s*?$/i ) {
371 return 'lock: no addr 2' unless ($addressed);
373 my $function = lc $1;
376 if ( $faqtoid eq '' ) {
381 if ( &getFactoid($faqtoid) eq '' ) {
382 &msg( $who, "factoid \002$faqtoid\002 does not exist" );
386 if ( $function eq 'lock' ) {
388 # strongly requested by #debian on 19991028. -xk
389 if ( 1 and $faqtoid !~ /^\Q$who\E$/i and &IsFlag('o') ne 'o' ) {
391 "sorry, locking cannot be used since it can be abused unneccesarily."
394 "Replace 1 with 0 in Process.pl#~324 for locking support.");
401 &CmdUnLock($faqtoid);
408 if ( $message =~ s/^rename(\s+|$)// ) {
409 return 'rename: no addr' unless ($addressed);
411 if ( $message eq '' ) {
416 if ( $message =~ /^'(.*)'\s+'(.*)'$/ ) {
417 my ( $from, $to ) = ( lc $1, lc $2 );
419 my $result = &getFactoid($from);
420 if ( !defined $result ) {
422 "i didn't have anything called '$from' to rename");
426 # author == nick!user@host
428 my $author = &getFactInfo( $from, 'created_by' );
432 # Can they even modify factoids?
433 if ( &IsFlag('m') ne 'm'
434 and &IsFlag('M') ne 'M'
435 and &IsFlag('o') ne 'o' )
437 &performReply("You do not have permission to modify factoids");
440 # If they have +M but they didnt create the factoid
442 elsif ( &IsFlag('M') eq 'M'
443 and $who !~ /^\Q$created_by\E$/i
444 and &IsFlag('m') ne 'm'
445 and &IsFlag('o') ne 'o' )
447 &performReply("factoid '$from' is not yours to modify.");
451 # Else they have permission, so continue
453 if ( $_ = &getFactoid($to) ) {
454 &performReply("destination factoid already exists.");
458 &setFactInfo( $from, 'factoid_key', $to );
460 &status("rename: <$who> '$from' is now '$to'");
461 &performReply("i renamed '$from' to '$to'");
464 &msg( $who, "error: wrong format. ask me about 'help rename'." );
470 # factoid substitution. (X =~ s/A/B/FLAG)
471 my $message_utf8 = decode_utf8($message);
472 if ( $message_utf8 =~ m|^(.*?)\s+=~\s+s([/,#])(.+?)\2(.*?)\2([a-z]*);?\s*$| ) {
473 my ( $faqtoid, $delim, $op, $np, $flags ) = ( lc $1, $2, $3, $4, $5 );
474 return 'subst: no addr' unless ($addressed);
477 if ( $np =~ /$delim/ ) {
479 "looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'."
485 if ( my $result = &getFactoid(encode_utf8($faqtoid)) ) {
486 return 'subst: locked' if ( &IsLocked(encode_utf8($faqtoid)) == 1 );
488 $result = decode_utf8($result);
489 my $faqauth = &getFactInfo( encode_utf8($faqtoid), 'created_by' );
491 if ( ( $flags eq 'g' && $result =~ s/\Q$op/$np/gi )
492 || $result =~ s/\Q$op/$np/i )
494 my $author = $faqauth;
498 # Can they even modify factoids?
499 if ( &IsFlag('m') ne 'm'
500 and &IsFlag('M') ne 'M'
501 and &IsFlag('o') ne 'o' )
504 "You do not have permission to modify factoids");
507 # If they have +M but they didnt create the factoid
509 elsif ( &IsFlag('M') eq 'M'
510 and $who !~ /^\Q$created_by\E$/i
511 and &IsFlag('m') ne 'm'
512 and &IsFlag('o') ne 'o' )
514 &performReply("factoid '$faqtoid' is not yours to modify.");
519 if ( length encode_utf8($result) > $param{'maxDataSize'} ) {
520 &performReply("that's too long");
525 if ( length $result == 0 ) {
527 "factoid would be empty. Use forget instead.");
532 if ( ( length $result ) * 2 < length $was
533 and &IsFlag('o') ne 'o'
534 and &IsHostMatch($faqauth) != 2 )
536 &performReply("too drastic change of factoid.");
539 &setFactInfo( encode_utf8($faqtoid), 'factoid_value', encode_utf8($result) );
540 &status("update: '".encode_utf8($faqtoid)."' =is=> '".encode_utf8($result)."'; was '$was'");
544 &performReply("that doesn't contain '".encode_utf8($op)."'");
548 &performReply("i didn't have anything called '".encode_utf8($faqtoid)."' to modify");
554 # Fix up $message for question.
555 my $question = $message;
561 s/^whois //i; # Must match ^, else factoids with "whois" anywhere break
563 s/^what is (a|an)?//i;
565 s/^where can i (find|get|download)//i;
569 # clear the string of useless words.
570 s/^(stupid )?q(uestion)?:\s+//i;
571 s/^(does )?(any|ne)(1|one|body) know //i;
573 s/^[uh]+m*[,\.]* +//i;
577 s/^(gee|boy|golly|gosh)([, ]+)//i;
578 s/^(well|and|but|or|yes)([, ]+)//i;
580 s/^o+[hk]+(a+y+)?([,. ]+)//i;
581 s/^g(eez|osh|olly)([,. ]+)//i;
582 s/^w(ow|hee|o+ho+)([,. ]+)//i;
583 s/^heya?,?( folks)?([,. ]+)//i;
586 if ( $addressed and $message =~ s/^no([, ]+)(\Q$ident\E\,+)?\s*//i ) {
587 $correction_plausible = 1;
589 "correction is plausible, initial negative and nick deleted ($&)")
590 if ( $param{VERBOSITY} );
593 $correction_plausible = 0;
596 my $result = &doQuestion($question);
597 &::DEBUG("question:$question:returned:".(defined $result?$result:'undef'));
598 if ( !defined $result or $result eq $noreply ) {
599 return 'result from doQ undef.';
602 if ( defined $result and $result !~ /^0?$/ ) { # question.
603 &status("question: <$who> $message");
604 $count{'Question'}++;
606 elsif ( &IsChanConf('Math') > 0 and $addressed ) { # perl math.
607 &loadMyModule('Math');
608 my $newresult = &perlMath();
610 if ( defined $newresult and $newresult ne '' ) {
611 $cmdstats{'Maths'}++;
612 $result = $newresult;
613 &status("math: <$who> $message => $result");
617 if ( $result !~ /^0?$/ ) {
618 &performStrictReply($result);
622 # why would a friendly bot get passed here?
623 if ( &IsParam('friendlyBots') ) {
625 if ( grep lc($_) eq lc($who),
626 split( /\s+/, $param{'friendlyBots'} ) );
630 if ( !defined &doStatement($message) ) {
634 return unless ( $addressed and !$addrchar );
636 if ( length $message > 64 ) {
637 &status("unparseable-moron: $message");
639 # &performReply( &getRandom(keys %{ $lang{'moron'} }) );
642 &performReply( "You are person \002#" . $count{'Moron'} . "\002 to send an unparseable request" );
646 &status("unparseable: $message");
647 &performReply( &getRandom( keys %{ $lang{'dunno'} } ) );
653 # vim:ts=4:sw=4:expandtab:tw=80