App-RoboBot

 view release on metacpan or  search on metacpan

lib/App/RoboBot/Message.pm  view on Meta::CPAN

package App::RoboBot::Message;
$App::RoboBot::Message::VERSION = '4.004';
use v5.20;

use namespace::autoclean;

use Moose;
use MooseX::ClassAttribute;
use MooseX::SetOnce;

use DateTime;

use App::RoboBot::Parser;
use App::RoboBot::Response;

has 'raw' => (
    is       => 'rw',
    isa      => 'Str',
    required => 1,
);

has 'expression' => (
    is        => 'rw',
    isa       => 'Object',
    predicate => 'has_expression',
);

has 'sender' => (
    is       => 'rw',
    isa      => 'App::RoboBot::Nick',
    traits   => [qw( SetOnce )],
    required => 1,
);

has 'channel' => (
    is        => 'rw',
    isa       => 'App::RoboBot::Channel',
    traits    => [qw( SetOnce )],
    predicate => 'has_channel',
    trigger   => \&update_response_channel,
);

has 'network' => (
    is       => 'rw',
    isa      => 'Object',
    traits   => [qw( SetOnce )],
    required => 1,
);

has 'timestamp' => (
    is       => 'ro',
    isa      => 'DateTime',
    default  => sub { DateTime->now },
    required => 1,
);

has 'response' => (
    is        => 'rw',
    isa       => 'App::RoboBot::Response',
    predicate => 'has_response',
);

has 'vars' => (
    is      => 'rw',
    isa     => 'HashRef',
    default => sub { {} },
);

has 'bot' => (
    is       => 'ro',
    isa      => 'App::RoboBot',
    required => 1,
);

class_has 'log' => (
    is          => 'rw',
    predicate   => 'has_logger',
);

sub BUILD {
    my ($self) = @_;

    $self->log($self->bot->logger('core.message')) unless $self->has_logger;

    $self->log->debug(sprintf('Constructing new message object on network %s.', $self->network->name));

    $self->response(App::RoboBot::Response->new(
        bot     => $self->bot,
        network => $self->network,
        nick    => $self->sender,
    ));

    $self->log->debug('Empty response for message initialized.');

    $self->response->channel($self->channel) if $self->has_channel;

    # Short circuit if there's no actual message. This is not an error condition
    # though, since internal/dummy Message objects get created in some plugins
    # where expression evaluations are performed outside the context of a normal
    # user-generated message.
    return if length($self->raw) < 1;

    $self->log->debug(sprintf('Message length greater than 0 (%d). Proceeded with message processing.', length($self->raw)));

    # If the message is nothing but "help" or "!help" then convert it to "(help)"
    if ($self->raw =~ m{^\s*\!?help\s*$}oi) {
        $self->raw("(help)");
    }

    # If the very first character is an exclamation point, check the following
    # non-whitespace characters to see if they match a known command. If they
    # do, convert the incoming message to a simple expression to allow people
    # to interact with the bot using the older "!command arg arg arg" syntax.



( run in 0.512 second using v1.01-cache-2.11-cpan-d7f47b0818f )