File Coverage

blib/lib/POE/Component/IRC/Plugin/BasePoCoWrap.pm
Criterion Covered Total %
statement 15 109 13.7
branch 0 34 0.0
condition 0 12 0.0
subroutine 5 17 29.4
pod 0 6 0.0
total 20 178 11.2


line stmt bran cond sub pod time code
1             package POE::Component::IRC::Plugin::BasePoCoWrap;
2              
3 1     1   205122 use warnings;
  1         3  
  1         24  
4 1     1   4 use strict;
  1         2  
  1         38  
5              
6             our $VERSION = '1.001001'; # VERSION
7              
8 1     1   6 use Carp;
  1         5  
  1         55  
9 1     1   4 use POE;
  1         1  
  1         6  
10 1     1   322 use POE::Component::IRC::Plugin qw(:ALL);
  1         2  
  1         1609  
11              
12             sub new {
13 0     0 0   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           my $self = bless {}, $package;
21 0           %args = (
22             debug => 0,
23             auto => 1,
24             response_event => 'irc_poco_wrap_response',
25             banned => [],
26             addressed => 1,
27             eat => 1,
28             trigger => qr/^poco_wrap\s+(?=\S)/i,
29             listen_for_input => [ qw(public notice privmsg) ],
30             response_types => {
31             public => 'public',
32             privmsg => 'privmsg',
33             notice => 'notice',
34             },
35              
36             $self->_make_default_args(),
37              
38             %args,
39             );
40              
41 0           $args{listen_for_input} = {
42 0 0         map { $_ => 1 } @{ $args{listen_for_input} || [] }
  0            
43             };
44              
45 0           $self->{ $_ } = delete $args{ $_ } for keys %args;
46              
47 0           return $self;
48             }
49              
50             sub _start {
51 0     0     my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
52 0           $self->{_session_id} = $_[SESSION]->ID();
53 0           $kernel->refcount_increment( $self->{_session_id}, __PACKAGE__ );
54              
55 0           $self->{poco} = $self->_make_poco;
56              
57 0           undef;
58             }
59              
60             sub PCI_register {
61 0     0 0   my ( $self, $irc ) = splice @_, 0, 2;
62              
63 0           $self->{irc} = $irc;
64              
65 0           $irc->plugin_register( $self, 'SERVER', qw(public notice msg) );
66              
67 0           $self->{_session_id} = POE::Session->create(
68             object_states => [
69             $self => [
70             qw(
71             _start
72             _shutdown
73             _poco_done
74             _poco_begin
75             )
76             ],
77             ],
78             )->ID;
79              
80 0           return 1;
81             }
82              
83             sub _shutdown {
84 0     0     my ($kernel, $self) = @_[ KERNEL, OBJECT ];
85 0           $self->{poco}->shutdown;
86 0           $kernel->alarm_remove_all();
87 0           $kernel->refcount_decrement( $self->{_session_id}, __PACKAGE__ );
88 0           undef;
89             }
90              
91             sub PCI_unregister {
92 0     0 0   my $self = shift;
93              
94             # Plugin is dying make sure our POE session does as well.
95 0           $poe_kernel->call( $self->{_session_id} => '_shutdown' );
96              
97 0           delete $self->{irc};
98              
99 0           return 1;
100             }
101              
102             sub S_public {
103 0     0 0   my ( $self, $irc ) = splice @_, 0, 2;
104 0           my $who = ${ $_[0] };
  0            
105 0           my $channel = ${ $_[1] }->[0];
  0            
106 0           my $message = ${ $_[2] };
  0            
107 0           return $self->_parse_input( $irc, $who, $channel, $message, 'public' );
108             }
109              
110             sub S_notice {
111 0     0 0   my ( $self, $irc ) = splice @_, 0, 2;
112 0           my $who = ${ $_[0] };
  0            
113 0           my $channel = ${ $_[1] }->[0];
  0            
114 0           my $message = ${ $_[2] };
  0            
115 0           return $self->_parse_input( $irc, $who, $channel, $message, 'notice' );
116             }
117              
118             sub S_msg {
119 0     0 0   my ( $self, $irc ) = splice @_, 0, 2;
120 0           my $who = ${ $_[0] };
  0            
121 0           my $channel = ${ $_[1] }->[0];
  0            
122 0           my $message = ${ $_[2] };
  0            
123 0           return $self->_parse_input( $irc, $who, $channel, $message, 'privmsg' );
124             }
125              
126             sub _parse_input {
127 0     0     my ( $self, $irc, $who, $channel, $message, $type ) = @_;
128              
129 0 0         warn "Got input: [ who => $who, channel => $channel, "
130             . "mesage => $message ]"
131             if $self->{debug};
132              
133 0 0         return PCI_EAT_NONE
134             unless exists $self->{listen_for_input}{ $type };
135              
136 0           my $what;
137 0 0 0       if ( $self->{addressed} and $type eq 'public' ) {
138 0           my $my_nick = $irc->nick_name();
139 0           ($what) = $message =~ m/^\s*\Q$my_nick\E[\:\,\;\.]?\s*(.*)$/i;
140             }
141             else {
142 0           $what = $message;
143             }
144              
145 0 0         return PCI_EAT_NONE
146             unless defined $what;
147              
148 0 0 0       return PCI_EAT_NONE
      0        
      0        
149             unless (
150             ( exists $self->{triggers}{ $type }
151             and $what =~ s/$self->{triggers}{$type}//
152             )
153             or
154             ( exists $self->{trigger} and $what =~ s/$self->{trigger}// )
155             );
156              
157 0           $what =~ s/^\s+|\s+$//g;
158              
159 0 0         warn "Matched trigger: [ who => $who, channel => $channel, "
160             . "what => $what ]"
161             if $self->{debug};
162              
163 0 0         foreach my $ban_re ( @{ $self->{banned} || [] } ) {
  0            
164 0 0         return PCI_EAT_NONE
165             if $who =~ /$ban_re/;
166             }
167              
168 0           $poe_kernel->post( $self->{_session_id} => _poco_begin => {
169             what => $what,
170             who => $who,
171             channel => $channel,
172             message => $message,
173             type => $type,
174             }
175             );
176              
177 0 0         return $self->{eat} ? PCI_EAT_ALL : PCI_EAT_NONE;
178             }
179              
180             sub _poco_begin {
181 0     0     my ( $self, $args_ref ) = @_[OBJECT, ARG0];
182 0           $self->_make_poco_call( $args_ref );
183             }
184              
185             sub _poco_done {
186 0     0     my ( $kernel, $self, $in_ref ) = @_[ KERNEL, OBJECT, ARG0 ];
187              
188 0           my $response_message
189             = $self->_make_response_message( @_[ARG0 .. $#_] );
190              
191 0           my $event_response;
192 0 0         if ( my $key = $self->_message_into_response_event( $in_ref ) ) {
193 0 0         if ( ref $key eq 'ARRAY' ) {
194 0           $in_ref->{ $key->[0] } = $response_message;
195 0           %$in_ref = (
196             %$in_ref,
197 0           %{ $key->[1] },
198             );
199             }
200             else {
201 0           $in_ref->{ $key } = $response_message;
202             }
203 0           $event_response = $in_ref;
204             }
205             else {
206 0           $event_response = $self->_make_response_event( $in_ref );
207             }
208              
209 0 0         $response_message = [ $response_message ]
210             unless ref $response_message eq 'ARRAY';
211              
212 0           $self->{irc}->send_event(
213             $self->{response_event} => $event_response,
214             );
215              
216 0 0         if ( $self->{auto} ) {
217 0           $in_ref->{_type} = $self->{response_types}{ $in_ref->{_type} };
218              
219 0 0         my $response_type = $in_ref->{_type} eq 'public'
220             ? 'privmsg'
221             : $in_ref->{_type};
222              
223 0 0         my $where = $in_ref->{_type} eq 'public'
224             ? $in_ref->{_channel}
225             : (split /!/, $in_ref->{_who})[0];
226              
227 0           for ( @$response_message ) {
228 0           $kernel->post( $self->{irc} =>
229             $response_type =>
230             $where =>
231             $_
232             );
233             }
234             }
235              
236 0           undef;
237             }
238              
239 0     0     sub _message_into_response_event { undef; }
240              
241             1;
242             __END__