POP3.pm

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