File Coverage

blib/lib/Bot/Cobalt/IRC/Message/Public.pm
Criterion Covered Total %
statement 32 33 96.9
branch 4 8 50.0
condition 3 7 42.8
subroutine 9 9 100.0
pod n/a
total 48 57 84.2


line stmt bran cond sub pod time code
1             package Bot::Cobalt::IRC::Message::Public;
2             $Bot::Cobalt::IRC::Message::Public::VERSION = '0.021001';
3 5     5   791 use v5.10;
  5         11  
4 5     5   19 use strictures 2;
  5         26  
  5         145  
5 5     5   732 use Scalar::Util 'blessed';
  5         7  
  5         205  
6              
7 5     5   412 use Bot::Cobalt;
  5         6  
  5         23  
8 5     5   3126 use Bot::Cobalt::Common;
  5         6  
  5         26  
9              
10             require Bot::Cobalt::Core;
11              
12 5     5   26 use Moo;
  5         6  
  5         23  
13             extends 'Bot::Cobalt::IRC::Message';
14              
15             has cmd => (
16             lazy => 1,
17             is => 'rw',
18             isa => Any,
19             predicate => 'has_cmd',
20             builder => '_build_cmd',
21             );
22              
23             has highlight => (
24             lazy => 1,
25             is => 'rw',
26             isa => Bool,
27             predicate => 'has_highlight',
28             builder => '_build_highlight',
29             );
30              
31             has myself => (
32             lazy => 1,
33             is => 'rw',
34             isa => Str,
35             builder => sub {
36 1     1   603 my ($self) = @_;
37 1         1 my $irc;
38 1 50 33     3 return ''
39             unless Bot::Cobalt::Core->has_instance
40             and $irc = irc_object($self->context);
41 0 0       0 $irc->nick_name || ''
42             },
43             );
44              
45             after message => sub {
46             my ($self, $value) = @_;
47             return unless defined $value;
48            
49             if ($self->has_highlight) {
50             $self->highlight( $self->_build_highlight );
51             }
52              
53             if ($self->has_cmd) {
54             $self->cmd( $self->_build_cmd );
55             }
56             };
57              
58             sub _build_highlight {
59 2     2   1025 my ($self) = @_;
60 2   100     16 my $me = $self->myself || return 0;
61 1         17 my $txt = $self->stripped;
62 1         37 $txt =~ /^${me}[,:;!-]?\s+/i
63             }
64              
65             sub _build_cmd {
66 3     3   889 my ($self) = @_;
67              
68             my $cmdchar = Bot::Cobalt::Core->has_instance ?
69 3 50 0     12 (core->get_core_cfg->opts->{CmdChar} // '!') : '!'
70             ;
71              
72 3 100       45 if ($self->stripped =~ /^${cmdchar}([^\s]+)/) {
73 1         87 my $message = $self->message_array;
74 1         78 shift @$message;
75             # shift above modifies the ref, but intentionally hit trigger:
76 1         15 $self->message_array($message);
77 1         16 return lc($1)
78             }
79             undef
80 2         39 }
81              
82              
83             1;
84             __END__