POP3.pm

filter: Statementstmt 69.3% 138 / 199 Branchbran 39.4% 30 / 76 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 67.6% 23 / 34 Podpod 90.0% 9 / 10 totaltotal 59.7% 205 / 343 CC 52 SCAR 22.5
CC
52
Cov
60%
register
42.0
_unregister
42.0
spawn
8.0
CRAP 9.5
↑ Summary POP3/Constants.pm »
1 package POE::Component::Server::POP3;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use POE qw(Wheel::SocketFactory Wheel::ReadWrite Filter::Line);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use base qw(POE::Component::Pluggable);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use POE::Component::Pluggable::Constants qw(:ALL);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 $VERSION = '0.10';
12  
13 sub spawn {
14 1 ▶ my $package = shift;
Subroutine spawn: called CC=7 SCAR=20.8
Pod documented
15 1 my %opts = @_;
16 1 $opts{lc $_} = delete $opts{$_} for keys %opts;
17 1 my $options = delete $opts{options};
18 1 ▶ $opts{version} = join('-', __PACKAGE__, $VERSION ) unless $opts{version};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"version"} 1 0 1
19 1 ▶ $opts{handle_connects} = 1 unless defined $opts{handle_connects} and !$opts{handle_connects};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"handle_connects"} and not $opts{"handle_connects"} 1 0 1
Condition33%1/3
defined $opts{"handle_connects"} and not $opts{"handle_connects"}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $opts{"handle_connects"} and not $opts{"handle_connects"}
defined $opts{"handle_connects"} not $opts{"handle_connects"}
Truth table33%1/3
A and not(B)
A= defined $opts{"handle_connects"}B= not $opts{"handle_connects"}
ABresult
0X0
100
111
20 1 ▶ $opts{hostname} = 'localhost' unless defined $opts{hostname};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"hostname"} 1 0 1
21 1 my $self = bless \%opts, $package;
22 1 $self->_pluggable_init( prefix => 'pop3d_', types => [ 'POP3D', 'POP3C' ], debug => 1 );
23 1 ▶ $self->{session_id} = POE::Session->create(
Branch: 1 branch
BranchTrueFalseTotal
ref $options eq 'HASH' ? : 1 0 1
24 object_states => [
25 $self => { shutdown => '_shutdown',
26 send_event => '__send_event',
27 send_to_client => '_send_to_client',
28 disconnect => '_disconnect',
29 },
30 $self => [ qw(_start register unregister _accept_client _accept_failed _conn_input _conn_error _conn_flushed _conn_alarm _send_to_client __send_event _disconnect) ],
31 ],
32 heap => $self,
33 ( ref($options) eq 'HASH' ? ( options => $options ) : () ),
34 )->ID();
35 1 return $self;
36 }
37  
38 sub session_id {
39 1 ▶ return $_[0]->{session_id};
Subroutine session_id: called CC=1 SCAR=0.0
Pod documented
40 }
41  
42 sub getsockname {
43 1 ▶ return unless $_[0]->{listener};
Subroutine getsockname: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"listener"} 0 1 1
44 1 return $_[0]->{listener}->getsockname();
45 }
46  
47 sub _conn_exists {
48 8 ▶ my ($self,$wheel_id) = @_;
Subroutine _conn_exists: called CC=3 SCAR=12.5
49 8 ▶ return 0 unless $wheel_id and defined $self->{clients}->{ $wheel_id };
Branch: 1 branch
BranchTrueFalseTotal
unless $wheel_id and defined $self->{"clients"}{$wheel_id} 0 8 8
Condition33%1/3
$wheel_id and defined $self->{"clients"}{$wheel_id}
!ll&&!rl&&r
008
MC/DC0%0/2
$wheel_id and defined $self->{"clients"}{$wheel_id}
$wheel_id defined $self->{"clients"}{$wheel_id}
Truth table33%1/3
A and B
A= $wheel_idB= defined $self->{"clients"}{$wheel_id}
ABresult
0X0
100
111
50 8 return 1;
51 }
52  
53 sub _valid_cmd {
54 2 ▶ my $self = shift;
Subroutine _valid_cmd: called CC=3 SCAR=11.0
55 2 ▶ my $cmd = shift || return;
Condition50%1/2
shift() || (return)
l!l
20
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
56 2 $cmd = lc $cmd;
57 2 ▶ return 0 unless grep { $_ eq $cmd } @{ $self->{cmds} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $cmd;} @{$$self{"cmds"};} 1 1 2
58 1 return 1;
59 }
60  
61 sub shutdown {
62 1 ▶ my $self = shift;
Subroutine shutdown: called CC=1 SCAR=0.0
Pod documented
63 1 $poe_kernel->post( $self->{session_id}, 'shutdown' );
64 }
65  
66 sub _start {
67 1 ▶ my ($kernel,$self,$sender) = @_[KERNEL,OBJECT,SENDER];
Subroutine _start: called CC=5 SCAR=16.5
68 1 $self->{session_id} = $_[SESSION]->ID();
69 1 ▶ if ( $self->{alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'alias'}) { } 0 1 1
70 0 $kernel->alias_set( $self->{alias} );
71 }
72 else {
73 1 $kernel->refcount_increment( $self->{session_id} => __PACKAGE__ );
74 }
75 1 ▶ if ( $kernel != $sender ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kernel != $sender) 1 0 1
76 1 my $sender_id = $sender->ID;
77 1 $self->{events}->{'pop3d_all'}->{$sender_id} = $sender_id;
78 1 $self->{sessions}->{$sender_id}->{'ref'} = $sender_id;
79 1 $kernel->refcount_increment($sender_id, __PACKAGE__);
80 1 $kernel->post( $sender, 'pop3d_registered', $self );
81 1 $kernel->detach_myself();
82 }
83  
84 1 $self->{filter} = POE::Filter::Line->new( Literal => "\015\012" );
85  
86 1 $self->{cmds} = [ qw(stat list retr dele noop rset top uidl user pass apop quit) ];
87  
88 $self->{listener} = POE::Wheel::SocketFactory->new(
89 ( defined $self->{address} ? ( BindAddress => $self->{address} ) : () ),
90 1 ▶ ( defined $self->{port} ? ( BindPort => $self->{port} ) : ( BindPort => 110 ) ),
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'address'} ? : 1 0 1
defined $self->{'port'} ? : 1 0 1
91 SuccessEvent => '_accept_client',
92 FailureEvent => '_accept_failed',
93 SocketDomain => AF_INET, # Sets the socket() domain
94 SocketType => SOCK_STREAM, # Sets the socket() type
95 SocketProtocol => 'tcp', # Sets the socket() protocol
96 Reuse => 'on', # Lets the port be reused
97 );
98  
99 1 return;
100 }
101  
102 sub _accept_client {
103 1 ▶ my ($kernel,$self,$socket,$peeraddr,$peerport) = @_[KERNEL,OBJECT,ARG0..ARG2];
Subroutine _accept_client: called CC=3 SCAR=11.1
104 1 my $sockaddr = inet_ntoa( ( unpack_sockaddr_in ( CORE::getsockname $socket ) )[1] );
105 1 my $sockport = ( unpack_sockaddr_in ( CORE::getsockname $socket ) )[0];
106 1 $peeraddr = inet_ntoa( $peeraddr );
107  
108 my $wheel = POE::Wheel::ReadWrite->new(
109 Handle => $socket,
110 Filter => $self->{filter},
111 1 InputEvent => '_conn_input',
112 ErrorEvent => '_conn_error',
113 FlushedEvent => '_conn_flushed',
114 );
115  
116 1 ▶ return unless $wheel;
Branch: 1 branch
BranchTrueFalseTotal
unless $wheel 0 1 1
117
118 1 my $id = $wheel->ID();
119 1 $self->{clients}->{ $id } =
120 {
121 wheel => $wheel,
122 peeraddr => $peeraddr,
123 peerport => $peerport,
124 sockaddr => $sockaddr,
125 sockport => $sockport,
126 };
127 1 $self->_send_event( 'pop3d_connection', $id, $peeraddr, $peerport, $sockaddr, $sockport );
128  
129 1 ▶ $self->{clients}->{ $id }->{alarm} = $kernel->delay_set( '_conn_alarm', $self->{time_out} || 300, $id );
Condition50%1/2
$self->{'time_out'} || 300
l!l
01
MC/DC0%0/1
$self->{'time_out'} || 300
$self->{'time_out'}
Truth table50%1/2
A
A= $self->{'time_out'}
Aresult
11
00
130 1 return;
131 }
132  
133 sub _accept_failed {
134 0 ▶ my ($kernel,$self,$operation,$errnum,$errstr,$wheel_id) = @_[KERNEL,OBJECT,ARG0..ARG3];
Subroutine _accept_failed: not called CC=1 SCAR=6.9
135 0 warn "Wheel $wheel_id generated $operation error $errnum: $errstr\n";
136 0 delete $self->{listener};
137 0 $self->_send_event( 'pop3d_listener_failed', $operation, $errnum, $errstr );
138 0 return;
139 }
140  
141 sub _conn_input {
142 2 ▶ my ($kernel,$self,$input,$id) = @_[KERNEL,OBJECT,ARG0,ARG1];
Subroutine _conn_input: called CC=6 SCAR=18.2
143 2 ▶ return unless $self->_conn_exists( $id );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_conn_exists($id) 0 2 2
144 2 ▶ $kernel->delay_adjust( $self->{clients}->{ $id }->{alarm}, $self->{time_out} || 300 );
Condition50%1/2
$self->{'time_out'} || 300
l!l
02
MC/DC0%0/1
$self->{'time_out'} || 300
$self->{'time_out'}
Truth table50%1/2
A
A= $self->{'time_out'}
Aresult
11
00
145 2 $input =~ s/^\s+//g;
146 2 $input =~ s/\s+$//g;
147 2 my @args = split /\s+/, $input, 2;
148 2 my $cmd = shift @args;
149 2 ▶ return unless $cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd 0 2 2
150 2 ▶ unless ( $self->_valid_cmd( $cmd ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_valid_cmd($cmd)) 1 1 2
151 1 $self->send_to_client( $id, '-ERR' );
152 1 return;
153 }
154 1 $cmd = lc $cmd;
155 1 ▶ $self->{clients}->{ $id }->{quit} = 1 if $cmd eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "quit" 1 0 1
156 1 $self->_send_event( 'pop3d_cmd_' . $cmd, $id, @args );
157 1 return;
158 }
159  
160 sub _conn_error {
161 0 ▶ my ($self,$errstr,$id) = @_[OBJECT,ARG2,ARG3];
Subroutine _conn_error: not called CC=2 SCAR=17.9
162 0 ▶ return unless $self->_conn_exists( $id );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_conn_exists($id) 0 0 0
163 0 delete $self->{clients}->{ $id };
164 0 $self->_send_event( 'pop3d_disconnected', $id );
165 0 return;
166 }
167  
168 sub _conn_flushed {
169 3 ▶ my ($self,$id) = @_[OBJECT,ARG0];
Subroutine _conn_flushed: called CC=3 SCAR=11.0
170 3 ▶ return unless $self->_conn_exists( $id );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_conn_exists($id) 0 3 3
171 3 ▶ return unless $self->{clients}->{ $id }->{quit};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"clients"}{$id}{"quit"} 2 1 3
172 1 delete $self->{clients}->{ $id };
173 1 $self->_send_event( 'pop3d_disconnected', $id );
174 1 return;
175 }
176  
177 sub _conn_alarm {
178 0 ▶ my ($kernel,$self,$id) = @_[KERNEL,OBJECT,ARG0];
Subroutine _conn_alarm: not called CC=2 SCAR=17.9
179 0 ▶ return unless $self->_conn_exists( $id );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_conn_exists($id) 0 0 0
180 0 delete $self->{clients}->{ $id };
181 0 $self->_send_event( 'pop3d_disconnected', $id );
182 0 return;
183 }
184  
185 sub _shutdown {
186 1 ▶ my ($kernel,$self) = @_[KERNEL,OBJECT];
Subroutine _shutdown: called CC=3 SCAR=11.0
187 1 delete $self->{listener};
188 1 delete $self->{clients};
189 1 $kernel->alarm_remove_all();
190 1 $kernel->alias_remove( $_ ) for $kernel->alias_list();
191 1 ▶ $kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ ) unless $self->{alias};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"alias"} 1 0 1
192 1 $self->_pluggable_destroy();
193 1 $self->_unregister_sessions();
194 1 return;
195 }
196  
197 sub register {
198 0 ▶ my ($kernel, $self, $session, $sender, @events) =
Subroutine register: not called CC=6 SCAR=37.4
Pod documented
199 @_[KERNEL, OBJECT, SESSION, SENDER, ARG0 .. $#_];
200  
201 0 ▶ unless (@events) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@events) 0 0 0
202 0 warn "register: Not enough arguments";
203 0 return;
204 }
205  
206 0 my $sender_id = $sender->ID();
207  
208 0 foreach (@events) {
209 0 ▶ $_ = "pop3d_" . $_ unless /^_/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^_/ 0 0 0
210 0 $self->{events}->{$_}->{$sender_id} = $sender_id;
211 0 $self->{sessions}->{$sender_id}->{'ref'} = $sender_id;
212 0 ▶ unless ($self->{sessions}->{$sender_id}->{refcnt}++ or $session == $sender) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"sessions"}{$sender_id}{"refcnt"}++ or $session == $sender) 0 0 0
Condition0%0/3
$self->{"sessions"}{$sender_id}{"refcnt"}++ or $session == $sender
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"sessions"}{$sender_id}{"refcnt"}++ or $session == $sender
$self->{"sessions"}{$sender_id}{"refcnt"}++ $session == $sender
Truth table0%0/3
A or B
A= $self->{"sessions"}{$sender_id}{"refcnt"}++B= $session == $sender
ABresult
1X1
011
000
213 0 $kernel->refcount_increment($sender_id, __PACKAGE__);
214 }
215 }
216  
217 0 $kernel->post( $sender, 'pop3d_registered', $self );
218 0 return;
219 }
220  
221 sub unregister {
222 0 ▶ my ($kernel, $self, $session, $sender, @events) =
Subroutine unregister: not called CC=2 SCAR=17.9
Pod documented
223 @_[KERNEL, OBJECT, SESSION, SENDER, ARG0 .. $#_];
224  
225 0 ▶ unless (@events) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@events) 0 0 0
226 0 warn "unregister: Not enough arguments";
227 0 return;
228 }
229  
230 0 $self->_unregister($session,$sender,@events);
231 0 undef;
232 }
233  
234 sub _unregister {
235 0 ▶ my ($self,$session,$sender) = splice @_,0,3;
Subroutine _unregister: not called CC=6 SCAR=37.4
236 0 my $sender_id = $sender->ID();
237  
238 0 foreach (@_) {
239 0 ▶ $_ = "pop3d_" . $_ unless /^_/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^_/ 0 0 0
240 0 my $blah = delete $self->{events}->{$_}->{$sender_id};
241 0 ▶ unless ( $blah ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($blah) 0 0 0
242 0 warn "$sender_id hasn't registered for '$_' events\n";
243 0 next;
244 }
245 0 ▶ if (--$self->{sessions}->{$sender_id}->{refcnt} <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$self->{"sessions"}{$sender_id}{"refcnt"} <= 0) 0 0 0
246 0 delete $self->{sessions}->{$sender_id};
247 0 ▶ unless ($session == $sender) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session == $sender) 0 0 0
248 0 $poe_kernel->refcount_decrement($sender_id, __PACKAGE__);
249 }
250 }
251 }
252 0 undef;
253 }
254  
255 sub _unregister_sessions {
256 1 ▶ my $self = shift;
Subroutine _unregister_sessions: called CC=4 SCAR=14.1
257 1 my $pop3d_id = $self->session_id();
258 1 foreach my $session_id ( keys %{ $self->{sessions} } ) {
259 1 ▶ if (--$self->{sessions}->{$session_id}->{refcnt} <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$self->{"sessions"}{$session_id}{"refcnt"} <= 0) 1 0 1
260 1 delete $self->{sessions}->{$session_id};
261 1 ▶ $poe_kernel->refcount_decrement($session_id, __PACKAGE__)
Branch: 1 branch
BranchTrueFalseTotal
unless $session_id eq $pop3d_id 1 0 1
262 unless ( $session_id eq $pop3d_id );
263 }
264 }
265 }
266  
267 sub __send_event {
268 0 ▶ my( $self, $event, @args ) = @_[ OBJECT, ARG0, ARG1 .. $#_ ];
Subroutine __send_event: not called CC=1 SCAR=6.9
269 0 $self->_send_event( $event, @args );
270 0 return;
271 }
272  
273 sub _pluggable_event {
274 0 ▶ my $self = shift;
Subroutine _pluggable_event: not called CC=1 SCAR=6.9
275 0 $poe_kernel->post( $self->{session_id}, '__send_event', @_ );
276 }
277  
278 sub send_event {
279 0 ▶ my $self = shift;
Subroutine send_event: not called CC=1 SCAR=6.9
Pod documented
280 0 $poe_kernel->post( $self->{session_id}, '__send_event', @_ );
281 }
282  
283 sub _send_event {
284 3 ▶ my $self = shift;
Subroutine _send_event: called CC=5 SCAR=16.2
285 3 my ($event, @args) = @_;
286 3 my $kernel = $POE::Kernel::poe_kernel;
287 3 my $session = $kernel->get_active_session()->ID();
288 3 my %sessions;
289  
290 my @extra_args;
291  
292 3 ▶ return 1 if $self->_pluggable_process( 'POP3D', $event, \( @args ), \@extra_args ) == PLUGIN_EAT_ALL;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_pluggable_process("POP3D", $event, \(@args), \@extra_args) == 4 0 3 3
293  
294 3 ▶ push @args, @extra_args if scalar @extra_args;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @extra_args 0 3 3
295  
296 3 $sessions{$_} = $_ for (values %{$self->{events}->{'pop3d_all'}}, values %{$self->{events}->{$event}});
297  
298 3 $kernel->post( $_ => $event => @args ) for values %sessions;
299 3 undef;
300 }
301  
302 sub disconnect {
303 0 ▶ my $self = shift;
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod documented
304 0 $poe_kernel->call( $self->{session_id}, '_disconnect', @_ );
305 }
306  
307 sub _disconnect {
308 0 ▶ my ($kernel,$self,$id) = @_[KERNEL,OBJECT,ARG0];
Subroutine _disconnect: not called CC=2 SCAR=17.9
309 0 ▶ return unless $self->_conn_exists( $id );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_conn_exists($id) 0 0 0
310 0 $self->{clients}->{ $id }->{quit} = 1;
311 0 return 1;
312 }
313  
314 sub send_to_client {
315 3 ▶ my $self = shift;
Subroutine send_to_client: called CC=1 SCAR=0.0
Pod documented
316 3 $poe_kernel->call( $self->{session_id}, '_send_to_client', @_ );
317 }
318  
319 sub _send_to_client {
320 3 ▶ my ($kernel,$self,$id,$output) = @_[KERNEL,OBJECT,ARG0..ARG1];
Subroutine _send_to_client: called CC=4 SCAR=14.5
321 3 ▶ return unless $self->_conn_exists( $id );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_conn_exists($id) 0 3 3
322 3 ▶ return unless defined $output;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output 0 3 3
323  
324 3 ▶ return 1 if $self->_pluggable_process( 'POP3C', 'response', $id, \$output ) == PLUGIN_EAT_ALL;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_pluggable_process("POP3C", "response", $id, \$output) == 4 0 3 3
325  
326 3 $self->{clients}->{ $id }->{wheel}->put($output);
327 3 return 1;
328 }
329  
330 sub POP3D_connection {
331 1 ▶ my ($self,$pop3d) = splice @_, 0, 2;
Subroutine POP3D_connection: called CC=2 SCAR=7.0
Pod undocumented
332 1 my $id = ${ $_[0] };
333 1 ▶ return PLUGIN_EAT_NONE unless $self->{handle_connects};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"handle_connects"} 0 1 1
334 1 $self->send_to_client( $id, join ' ', '+OK POP3', $self->{hostname}, $self->{version}, 'server ready' );
335 1 return PLUGIN_EAT_NONE;
336 }
337  
338 3 'poppet';
339  
340 __END__
↑ Summary POP3/Constants.pm »