Bot-BasicBot-Pluggable

 view release on metacpan or  search on metacpan

lib/Bot/BasicBot/Pluggable/Module/Seen.pm  view on Meta::CPAN

package Bot::BasicBot::Pluggable::Module::Seen;
use base qw(Bot::BasicBot::Pluggable::Module);
use warnings;
use strict;

our $VERSION = '0.86';

sub init {
    my $self = shift;
    $self->config( { user_allow_hiding => 1 } );
}

sub help {
    return
"Tracks when and where people were last seen. 
Usage:
seen <nick>         (find out where 'nick' was last seen)
hide                (Start hiding yourself from 'seen' reporting)
unhide              (Stop  hiding yourself from 'seen' reporting)
hidechan      #chan (Hide a private channel from seen reporting)
unhidechan    #chan (Stop hiding a private channel from seen reporting)
privatechan   #chan (Don't show message contents from #chan in other channels)
unprivatechan #chan (Show message contents from #chan in other channels)
";
}

sub seen {
    my ( $self, $mess ) = @_;
    my $what = 'saying "' . $mess->{body} . '"';
    $self->update_seen( $mess->{who}, $mess->{channel}, $what );
    return;
}

sub chanpart {
    my ( $self, $mess ) = @_;
    my $what = 'leaving the channel';
    $self->update_seen( $mess->{who}, $mess->{channel}, $what );
    return;
}

sub chanjoin {
    my ( $self, $mess ) = @_;
    my $what = 'joining the channel';
    $self->update_seen( $mess->{who}, $mess->{channel}, $what );
    return;
}

sub update_seen {
    my ( $self, $who, $channel, $what ) = @_;
    my $nick = lc $who;
    $channel = lc $channel;
    my $ignore_channels = $self->get('user_ignore_channels') || {};
    return if exists $ignore_channels->{$channel};

    $self->set(
        "seen_$nick" => {
            time    => time,
            channel => $channel,
            what    => $channel ne 'msg' ? $what : '<private message>',
        }
    );
}

sub told {
    my ( $self, $mess ) = @_;
    my $body = $mess->{body};
    return unless defined $body;

    my ( $command, $param ) = split( /\s+/, $body, 2 );
    $command = lc($command);

    if ( $command eq "seen" and $param =~ /^(\S+)\??$/ ) {
        my $who  = lc($1);

        # First off, if they're asking about themselves, it's easy
        if ($who eq lc $mess->{who}) {
            $self->bot->emote(
                channel => $mess->{channel},
                body => "hands $who a mirror",
            );
            return;
        }

        my $seen = $self->get("seen_$who");
    
        my $ignore_channels = $self->get('user_ignore_channels') || {};
        my $hidden_channel = 
            $seen && exists $ignore_channels->{ lc $seen->{channel} };

        if (!$seen || $hidden_channel
            || ( $self->get("user_allow_hiding") and $self->get("hide_$who") )
        ) {
            return "Sorry, I haven't seen $1.";
        }

        # If the channel is private, and it's a different channel to the one we
        # are in, hide what they were saying
        my $private_channels = $self->get('user_private_channels') || {};
        if (exists $private_channels->{ lc $seen->{channel} }
            && $mess->{channel} ne $seen->{channel})
        {
            $seen->{what} = "saying something";
        }


        my $diff        = time - $seen->{time};
        my $time_string = secs_to_string($diff);
        return



( run in 2.082 seconds using v1.01-cache-2.11-cpan-8f98c5d2c55 )