package Bot::BasicBot::Pluggable::Module::DirkMod; use strict; use warnings; use base 'Bot::BasicBot::Pluggable::Module::Infobot'; sub told { my ( $self, $mess ) = @_; my $body = $mess->{body}; return unless defined $body; # direct commands must be addressed. return unless $mess->{address}; if ( $body =~ /^forget\s+(.*)$/i ) { return $self->delete_factoid($1) ? "I forgot about $1." : "I don't know anything about $1."; } # search for a particular factoid. if ( $body =~ /^search\s+for\s+(.*)$/i ) { return "privmsg only, please" unless ( $mess->{channel} eq "msg" ); return "searching disabled" unless $self->get("user_allow_searching"); my @results = $self->search_factoid( split( /\s+/, $1 ) ); unless (@results) { return "I don't know anything about $1."; } $#results = $self->get("user_num_results") - 1 unless $#results < $self->get("user_num_results"); return "I know about: " . join( ", ", map { "'$_'" } @results ) . "."; } } sub fallback { my ( $self, $mess ) = @_; my $seen = $self->bot->module("MySeen"); my $body = $mess->{body} || ""; # request starts with "my", so we'll look for # a valid factoid for "$mess->{who}'s $object". $body =~ s/^my /$mess->{who}'s /; my %stopwords = map { lc($_) => 1 } split( /\s*[\s,\|]\s*/, $self->get("user_stopwords") ); # checks to see if something starts # (is|are) # and then removes if if is a stopword # this means that we treat "what is foo?" as "foo?" if ( $body =~ /^(.*?)\s+(is|are)\s+(.*)$/i ) { $body =~ s/^(.*?)\s+(is|are)\s+//i if $stopwords{$1}; } # answer a factoid. this is a crazy check which ensures we will ONLY answer # a factoid if a) there is, or isn't, a question mark, b) we have, or haven't, # been addressed, c) the factoid is bigger and smaller than our requirements, # and d) that it doesn't look like a to-be-learned factoid (which is important # if the user has disabled the requiring of the question mark for answering.) my $body_regexp = $self->get("user_require_question") && !$is_priv ? qr/\?+$/ : qr/[.!?]*$/; if ( $body =~ s/$body_regexp// and ( $mess->{address} or $self->get("user_passive_answer") ) and length($body) >= $self->get("user_min_length") and length($body) <= $self->get("user_max_length") and $body !~ /^(.*?)\s+(is|are)\s+(.*)$/i ) { # get the factoid and type of relationship my ( $is_are, $factoid, $literal ) = $self->get_factoid($body); if ( !$literal && $factoid && $factoid =~ /\|/ ) { my @f = split /\|/, $factoid; $factoid = $f[ int( rand( scalar @f ) ) ]; } # no factoid? unless ($factoid) { my @unknowns = split( /\|/, $self->get("user_unknown_responses") ); my $unknown = $unknowns[ int( rand( scalar(@unknowns) ) ) - 1 ]; return $mess->{address} ? $unknown : undef; } # variable substitution. $factoid =~ s/\$who/$mess->{who}/ig; $factoid =~ s/\$coords/get_player_pos($user)/eig; $factoid =~ s/\$seen/get_seen($self,$user)/eig; if ($factoid =~ /\$alert\$/i && $mess->{channel} =~ /dirkocraft$/i) { # if we see $ALERT$ and we're in the main channel $mess->{channel} = "#dirkocraft-alert"; $factoid =~ s/\$alert\$//ig; $self->bot->say({who => $mess->{channel}, channel => $mess->{channel}, body => $factoid}); return 1; # say nothing in the main channel } # emote? if ( $factoid =~ s/^\s*//i ) { $self->bot->emote( { who => $mess->{who}, channel => $mess->{channel}, body => $factoid } ); return 1; # replying with, or without a noun? hmMmMmmm?! } elsif ($literal) { $body =~ s!^literal\s+!!; return "$body =${is_are}= $factoid"; } else { return $factoid =~ s/^\s*//i ? $factoid : $factoid; } } # the only thing left is learning factoids. are we # addressed or are we willing to learn passively? # does it even look like a factoid? return unless ( $mess->{address} or $self->get("user_passive_learn") ); return unless ( $body =~ /^(.*?)\s+(is)\s+(.*)$/i or $body =~ /^(.*?)\s+(are)\s+(.*)$/i ); my ( $object, $is_are, $description ) = ( $1, $2, $3 ); my $literal = ( $object =~ s!^literal\s+!! ); # allow corrections and additions. my ( $nick, $replace, $also ) = ( $self->bot->nick, 0, 0 ); $replace = 1 if ( $object =~ s/no,?\s+//i ); # no, $object is $fact. $replace = 1 if ( $replace and $object =~ s/^\s*$nick,?\s*//i ) ; # no, $bot, $object is $fact. $also = 1 if ( $description =~ s/^also\s+//i ); # $object is also $fact. # ignore short, long, and stopword'd factoids. return if length($object) < $self->get("user_min_length"); return if length($object) > $self->get("user_max_length"); foreach ( keys %stopwords ) { return if $object =~ /^$_\b/; } # if we're replacing things, remove the factoid first. # $also check supports "no, $bot, $object is also $fact". if ( $replace and !$also ) { $self->delete_factoid($object); } # get any current factoid there might be. my ( $type, $current ) = $self->get_factoid($object); # we can't add without explicit instruction, # but shouldn't warn if this is passive. if ( $current and !$also and $mess->{address} ) { return "... but $object $type $current ..."; } elsif ( $current and !$also and !$mess->{address} ) { return; } # add this factoid. this comment is absolutely useless. excelsior. $self->add_factoid( $object, $is_are, split( /\s+or\s+/, $description ) ); # return an ack if we were addressed only return $mess->{address} ? "Okay." : 1; } 1;