File Coverage

blib/lib/Bot/Cobalt/Plugin/Calc.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Bot::Cobalt::Plugin::Calc;
2             $Bot::Cobalt::Plugin::Calc::VERSION = '0.004003';
3 1     1   15382 use strictures 2;
  1         1154  
  1         34  
4              
5 1     1   611 use List::Objects::WithUtils;
  1         723  
  1         5  
6              
7 1     1   70327 use Bot::Cobalt;
  0            
  0            
8             use Bot::Cobalt::Common;
9              
10             use Bot::Cobalt::Plugin::Calc::Session;
11              
12             use POE;
13              
14             sub SESSID () { 0 }
15             sub CALC () { 1 }
16              
17             sub new {
18             bless [
19             undef, # SESSID
20             Bot::Cobalt::Plugin::Calc::Session->new, # CALC
21             ], shift
22             }
23              
24             sub Cobalt_register {
25             my ($self, $core) = splice @_, 0, 2;
26              
27             my $sess = POE::Session->create(
28             object_states => [
29             $self => +{
30             _start => 'px_start',
31             issue_calc => 'px_issue_calc',
32             calc_result => 'px_calc_result',
33             calc_error => 'px_calc_error',
34             },
35             ],
36             );
37             $self->[SESSID] = $sess->ID;
38              
39             register( $self, SERVER => 'public_cmd_calc' );
40             logger->info("Loaded: calc");
41             PLUGIN_EAT_NONE
42             }
43              
44             sub Cobalt_unregister {
45             my ($self, $core) = splice @_, 0, 2;
46             $poe_kernel->post( $self->[CALC]->session_id, 'shutdown' );
47             $poe_kernel->refcount_decrement( $self->[SESSID], 'Plugin loaded' );
48             logger->info("Unloaded");
49             PLUGIN_EAT_NONE
50             }
51              
52             sub Bot_public_cmd_calc {
53             my ($self, $core) = splice @_, 0, 2;
54             my $msg = ${ $_[0] };
55              
56             my $msgarr = $msg->message_array;
57             my $calcstr = join ' ', @$msgarr;
58             my $hints = hash(
59             context => $msg->context,
60             channel => $msg->channel,
61             nick => $msg->src_nick,
62             )->inflate;
63              
64             logger->debug("issue_calc '$calcstr'");
65             $poe_kernel->call( $self->[SESSID], issue_calc => $calcstr, $hints );
66            
67             PLUGIN_EAT_NONE
68             }
69              
70              
71             sub px_start {
72             my ($kernel, $self) = @_[KERNEL, OBJECT];
73             $kernel->refcount_increment( $_[SESSION]->ID, 'Plugin loaded' );
74             $self->[CALC]->start;
75             }
76              
77             sub px_issue_calc {
78             my ($kernel, $self) = @_[KERNEL, OBJECT];
79             logger->debug("got issue_calc, relaying to CALC");
80             $kernel->post( $self->[CALC]->session_id, calc => @_[ARG0, ARG1] )
81             }
82              
83             sub px_calc_result {
84             my ($kernel, $self) = @_[KERNEL, OBJECT];
85             my ($result, $hints) = @_[ARG0, ARG1];
86             logger->debug("got calc_result");
87             broadcast( message => $hints->context, $hints->channel,
88             $hints->nick . ": $result"
89             );
90             }
91              
92             sub px_calc_error {
93             my ($kernel, $self) = @_[KERNEL, OBJECT];
94             my ($error, $hints) = @_[ARG0, ARG1];
95             # if this is a bad-args warn there's no hints hash - but then it's also a
96             # bug and should warn() from ::Calc::Session
97             return unless keys %$hints;
98              
99             broadcast( message => $hints->context, $hints->channel,
100             $hints->nick . ": backend error: $error"
101             );
102             }
103              
104              
105             1;
106             __END__