File Coverage

blib/lib/POE/Component/IRC/Plugin/Google/Calculator.pm
Criterion Covered Total %
statement 15 96 15.6
branch 0 32 0.0
condition 0 6 0.0
subroutine 5 15 33.3
pod 1 6 16.6
total 21 155 13.5


line stmt bran cond sub pod time code
1             package POE::Component::IRC::Plugin::Google::Calculator;
2              
3 1     1   488006 use warnings;
  1         3  
  1         40  
4 1     1   7 use strict;
  1         3  
  1         38  
5              
6 1     1   7 use Carp;
  1         8  
  1         85  
7 1     1   5 use POE qw(Component::WWW::Google::Calculator);
  1         2  
  1         13  
8 1     1   512 use POE::Component::IRC::Plugin qw(:ALL);
  1         3  
  1         1695  
9              
10             our $VERSION = '0.04';
11              
12             sub new {
13 0     0 1   my $package = shift;
14 0 0         croak "Even number of arguments must be specified"
15             if @_ & 1;
16 0           my %args = @_;
17 0           $args{ lc $_ } = delete $args{ $_ } for keys %args;
18              
19             # fill in the defaults
20 0           %args = (
21             debug => 0,
22             auto => 1,
23             response_event => 'irc_google_calculator',
24             banned => [],
25             addressed => 1,
26             eat => 1,
27             trigger => qr/^calc\s+(?=\S)/i,
28             listen_for_input => [ qw(public notice privmsg) ],
29              
30             %args,
31             );
32              
33 0           $args{listen_for_input} = {
34 0 0         map { $_ => 1 } @{ $args{listen_for_input} || [] }
  0            
35             };
36              
37 0           return bless \%args, $package;
38             }
39              
40             sub _start {
41 0     0     my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
42 0           $self->{_session_id} = $_[SESSION]->ID();
43 0           $kernel->refcount_increment( $self->{_session_id}, __PACKAGE__ );
44            
45 0           $self->{poco} = POE::Component::WWW::Google::Calculator->spawn(
46             debug => $self->{debug},
47             );
48              
49 0           undef;
50             }
51              
52             sub PCI_register {
53 0     0 0   my ( $self, $irc ) = splice @_, 0, 2;
54            
55 0           $self->{irc} = $irc;
56            
57 0           $irc->plugin_register( $self, 'SERVER', qw(public notice msg) );
58            
59 0           $self->{_session_id} = POE::Session->create(
60             object_states => [
61             $self => [
62             qw(
63             _start
64             _shutdown
65             _calc_done
66             )
67             ],
68             ],
69             )->ID;
70              
71 0           return 1;
72             }
73              
74             sub _shutdown {
75 0     0     my ($kernel, $self) = @_[ KERNEL, OBJECT ];
76 0           $self->{poco}->shutdown;
77 0           $kernel->alarm_remove_all();
78 0           $kernel->refcount_decrement( $self->{_session_id}, __PACKAGE__ );
79 0           undef;
80             }
81              
82             sub PCI_unregister {
83 0     0 0   my $self = shift;
84            
85             # Plugin is dying make sure our POE session does as well.
86 0           $poe_kernel->call( $self->{_session_id} => '_shutdown' );
87            
88 0           delete $self->{irc};
89            
90 0           return 1;
91             }
92              
93             sub S_public {
94 0     0 0   my ( $self, $irc ) = splice @_, 0, 2;
95 0           my $who = ${ $_[0] };
  0            
96 0           my $channel = ${ $_[1] }->[0];
  0            
97 0           my $message = ${ $_[2] };
  0            
98 0           return $self->_parse_input( $irc, $who, $channel, $message, 'public' );
99             }
100              
101             sub S_notice {
102 0     0 0   my ( $self, $irc ) = splice @_, 0, 2;
103 0           my $who = ${ $_[0] };
  0            
104 0           my $channel = ${ $_[1] }->[0];
  0            
105 0           my $message = ${ $_[2] };
  0            
106 0           return $self->_parse_input( $irc, $who, $channel, $message, 'notice' );
107             }
108              
109             sub S_msg {
110 0     0 0   my ( $self, $irc ) = splice @_, 0, 2;
111 0           my $who = ${ $_[0] };
  0            
112 0           my $channel = ${ $_[1] }->[0];
  0            
113 0           my $message = ${ $_[2] };
  0            
114 0           return $self->_parse_input( $irc, $who, $channel, $message, 'privmsg' );
115             }
116              
117             sub _parse_input {
118 0     0     my ( $self, $irc, $who, $channel, $message, $type ) = @_;
119              
120 0 0         warn "Got input: [ who => $who, channel => $channel, "
121             . "mesage => $message ]"
122             if $self->{debug};
123              
124 0 0         return PCI_EAT_NONE
125             unless exists $self->{listen_for_input}{ $type };
126              
127 0           my $what;
128 0 0 0       if ( $self->{addressed} and $type eq 'public' ) {
129 0           my $my_nick = $irc->nick_name();
130 0           ($what) = $message =~ m/^\s*\Q$my_nick\E[\:\,\;\.]?\s*(.*)$/i;
131             }
132             else {
133 0           $what = $message;
134             }
135              
136 0 0 0       return PCI_EAT_NONE
137             unless defined $what and $what =~ s/$self->{trigger}//;
138              
139 0           $what =~ s/^\s+|\s+$//;
140              
141 0 0         return PCI_EAT_NONE
142             unless length $what;
143              
144 0 0         warn "Matched trigger: [ who => $who, channel => $channel, "
145             . "what => $what ]"
146             if $self->{debug};
147              
148 0 0         foreach my $ban_re ( @{ $self->{banned} || [] } ) {
  0            
149 0 0         return PCI_EAT_NONE
150             if $who =~ /$ban_re/;
151             }
152              
153 0           $self->{poco}->calc( {
154             session => $self->{_session_id},
155             event => '_calc_done',
156             term => $what,
157             _who => $who,
158             _channel => $channel,
159             _message => $message,
160             _type => $type,
161             }
162             );
163              
164 0 0         return $self->{eat} ? PCI_EAT_ALL : PCI_EAT_NONE;
165             }
166              
167             sub _calc_done {
168 0     0     my ( $kernel, $self, $in_ref ) = @_[ KERNEL, OBJECT, ARG0 ];
169              
170 0           my $response_message;
171              
172 0 0         if ( exists $in_ref->{error} ) {
173 0 0         $response_message = defined $in_ref->{error}
174             ? "Error: $in_ref->{error}"
175             : "Error: no result";
176             }
177             else {
178 0           $response_message = "Result: $in_ref->{out}";
179             }
180            
181 0           $self->{irc}->send_event( $self->{response_event} => {
182             result => $response_message,
183             term => $in_ref->{term},
184 0           map { $_ => $in_ref->{"_$_"} }
185             qw( who channel message type ),
186             }
187             );
188              
189 0 0         if ( $self->{auto} ) {
190 0 0         my $response_type = $in_ref->{_type} eq 'public'
191             ? 'privmsg'
192             : $in_ref->{_type};
193              
194 0 0         my $where = $in_ref->{_type} eq 'public'
195             ? $in_ref->{_channel}
196             : (split /!/, $in_ref->{_who})[0];
197              
198 0           $kernel->post( $self->{irc} =>
199             $response_type =>
200             $where =>
201             $response_message
202             );
203             }
204              
205 0           undef;
206             }
207              
208             1;
209              
210             __END__