File Coverage

blib/lib/Bot/Cobalt/Plugin/Calc.pm
Criterion Covered Total %
statement 19 53 35.8
branch 0 2 0.0
condition n/a
subroutine 7 14 50.0
pod 0 8 0.0
total 26 77 33.7


line stmt bran cond sub pod time code
1             package Bot::Cobalt::Plugin::Calc;
2             $Bot::Cobalt::Plugin::Calc::VERSION = '0.004004';
3 1     1   13482 use strictures 2;
  1         1084  
  1         30  
4              
5 1     1   567 use List::Objects::WithUtils;
  1         666  
  1         5  
6              
7 1     1   57772 use Bot::Cobalt;
  1         1867  
  1         4  
8 1     1   1054 use Bot::Cobalt::Common;
  1         105703  
  1         5  
9              
10 1     1   5524 use Bot::Cobalt::Plugin::Calc::Session;
  1         2  
  1         28  
11              
12 1     1   6 use POE;
  1         1  
  1         4  
13              
14             sub SESSID () { 0 }
15             sub CALC () { 1 }
16              
17             sub new {
18 1     1 0 49 bless [
19             undef, # SESSID
20             Bot::Cobalt::Plugin::Calc::Session->new, # CALC
21             ], shift
22             }
23              
24             sub Cobalt_register {
25 0     0 0   my ($self, $core) = splice @_, 0, 2;
26              
27 0           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 0           $self->[SESSID] = $sess->ID;
38              
39 0           register( $self, SERVER => 'public_cmd_calc' );
40 0           logger->info("Loaded: calc");
41 0           PLUGIN_EAT_NONE
42             }
43              
44             sub Cobalt_unregister {
45 0     0 0   my ($self, $core) = splice @_, 0, 2;
46 0           $poe_kernel->post( $self->[CALC]->session_id, 'shutdown' );
47 0           $poe_kernel->refcount_decrement( $self->[SESSID], 'Plugin loaded' );
48 0           logger->info("Unloaded");
49 0           PLUGIN_EAT_NONE
50             }
51              
52             sub Bot_public_cmd_calc {
53 0     0 0   my ($self, $core) = splice @_, 0, 2;
54 0           my $msg = ${ $_[0] };
  0            
55              
56 0           my $msgarr = $msg->message_array;
57 0           my $calcstr = join ' ', @$msgarr;
58 0           my $hints = hash(
59             context => $msg->context,
60             channel => $msg->channel,
61             nick => $msg->src_nick,
62             )->inflate;
63              
64 0           logger->debug("issue_calc '$calcstr'");
65 0           $poe_kernel->call( $self->[SESSID], issue_calc => $calcstr, $hints );
66            
67 0           PLUGIN_EAT_NONE
68             }
69              
70              
71             sub px_start {
72 0     0 0   my ($kernel, $self) = @_[KERNEL, OBJECT];
73 0           $kernel->refcount_increment( $_[SESSION]->ID, 'Plugin loaded' );
74 0           $self->[CALC]->start;
75             }
76              
77             sub px_issue_calc {
78 0     0 0   my ($kernel, $self) = @_[KERNEL, OBJECT];
79 0           logger->debug("got issue_calc, relaying to CALC");
80 0           $kernel->post( $self->[CALC]->session_id, calc => @_[ARG0, ARG1] )
81             }
82              
83             sub px_calc_result {
84 0     0 0   my ($kernel, $self) = @_[KERNEL, OBJECT];
85 0           my ($result, $hints) = @_[ARG0, ARG1];
86 0           logger->debug("got calc_result");
87 0           broadcast( message => $hints->context, $hints->channel,
88             $hints->nick . ": $result"
89             );
90             }
91              
92             sub px_calc_error {
93 0     0 0   my ($kernel, $self) = @_[KERNEL, OBJECT];
94 0           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 0 0         return unless keys %$hints;
98              
99 0           broadcast( message => $hints->context, $hints->channel,
100             $hints->nick . ": backend error: $error"
101             );
102             }
103              
104              
105             1;
106             __END__