blib/lib/WWW/Mailman.pm

filter: Statementstmt 57.7% 97 / 168 Branchbran 54.4% 37 / 68 Conditioncond 28.5% 8 / 28 MC/DCmcdc 16.6% 3 / 18 Subroutinesub 60.0% 15 / 25 Podpod 100% 9 / 9 totaltotal 53.4% 169 / 316 CC 45 SCAR 24.1
CC
45
Cov
54%
_login_form
30.0
_load_uri
30.0
roster
30.0
CRAP 11.1
↑ Summary Build »
1 package WWW::Mailman;
2  
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 our $VERSION = '1.06';
12  
13 6 my @attributes = qw(
14 secure server prefix program list
15 email password moderator_password admin_password
16 );
17  
18 6 my %default = ( program => 'mailman' );
19  
20 6 my $action_re = qr/^(?:admin(?:db)?|edithtml|listinfo|options|private)$/;
21  
22 #
23 # ACCESSORS / MUTATORS
24 #
25  
26 # generic accessors
27 6 for my $attr (@attributes) {
28 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 54 *{$attr} = sub {
30 97 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
31 97 ▶ return defined $self->{$attr} ? $self->{$attr} : $default{$attr} || ''
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 76 21 97
defined $self->{$attr} ? : 57 19 76
Condition100%2/2
$default{$attr} or ""
l!l
181
MC/DC100%1/1
$default{$attr} or ""
$default{$attr}
Truth table100%2/2
A
A= $default{$attr}
Aresult
11
00
32 if !@_;
33 21 return $self->{$attr} = shift;
34 54 };
35 }
36  
37 # specialized accessors
38 sub uri {
39 7 ▶ my ( $self, $uri ) = @_;
Subroutine uri: called CC=9 SCAR=22.0
Pod documented
40 7 ▶ if ($uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri) { } 6 1 7
41 6 $uri = URI->new($uri);
42  
43 # @segments = @prefix, $program, $action, $list, @suffix
44 6 my $program = $self->program;
45 6 my ( undef, @segments ) = $uri->path_segments;
46 6 my @prefix;
47  
48 # the program name is found in the url
49 6 ▶ if( grep $_ eq $program, @segments ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep(($_ eq $program), @segments)) { } 4 2 6
elsif (grep(($_ =~ /$action_re/), @segments)) { } 1 1 2
50 4 ▶ push @prefix, shift @segments
Branch: 1 branch
BranchTrueFalseTotal
@segments and $segments[0] ne $program and do { push @prefix, shift @segments } 1 4 5
Condition66%2/3
@segments and $segments[0] ne $program
!ll&&!rl&&r
041
MC/DC50%1/2
@segments and $segments[0] ne $program
@segments $segments[0] ne $program
Truth table66%2/3
A and B
A= @segmentsB= $segments[0] ne $program
ABresult
0X0
100
111
51 while @segments && $segments[0] ne $program;
52 4 shift @segments; # drop the program name
53 4 ▶ croak "Invalid URL $uri: no action"
Branch: 1 branch
BranchTrueFalseTotal
unless shift @segments 1 3 4
54 if !shift @segments;
55 }
56  
57 # try to autodetect the program name
58 elsif( grep $_ =~ $action_re, @segments ) {
59 1 ▶ push @prefix, shift @segments
Branch: 1 branch
BranchTrueFalseTotal
@segments and not $segments[0] =~ /$action_re/ and do { push @prefix, shift @segments } 1 1 2
Condition66%2/3
@segments and not $segments[0] =~ /$action_re/
!ll&&!rl&&r
011
MC/DC50%1/2
@segments and not $segments[0] =~ /$action_re/
@segments not $segments[0] =~ /$action_re/
Truth table66%2/3
A and not(B)
A= @segmentsB= not $segments[0] =~ /$action_re/
ABresult
0X0
100
111
60 while @segments && $segments[0] !~ $action_re;
61 1 $self->program( pop @prefix ); # get the program name
62 1 shift @segments; # drop the action name
63 }
64  
65 # declare FAIL
66 else {
67 1 croak "Invalid URL $uri: no program segment found ($program)";
68 }
69  
70 # just keep the bits we need
71 4 $self->server( $uri->host );
72 4 $self->secure( $uri->scheme eq 'https' );
73 4 $self->userinfo( $uri->userinfo );
74 4 $self->prefix( join '/', @prefix );
75 4 $self->list( shift @segments );
76 }
77  
78 # create a generic listinfo URL
79 else {
80 1 $uri = $self->_uri_for('listinfo');
81 }
82 5 return $uri;
83 }
84  
85 sub userinfo {
86 36 ▶ my $self = shift;
Subroutine userinfo: called CC=5 SCAR=16.4
Pod documented
87 36 ▶ return defined $self->{userinfo} ? $self->{userinfo} : '' if !@_;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 23 13 36
defined $self->{'userinfo'} ? : 1 22 23
88 13 $self->{userinfo} = my $userinfo = shift;
89  
90 # update the credentials stored in the robot
91 13 ▶ if ( $self->robot ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->robot) 13 0 13
92 13 ▶ if ($userinfo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($userinfo) { } 0 13 13
93 0 $self->robot->credentials( split /:/, $userinfo, 2 );
94 }
95 else {
96 13 $self->robot->clear_credentials();
97 }
98 }
99  
100 13 return $userinfo;
101 }
102  
103 sub robot {
104 47 ▶ my $self = shift;
Subroutine robot: called CC=3 SCAR=11.0
Pod documented
105 47 ▶ return defined $self->{robot} ? $self->{robot} : '' if !@_;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 38 9 47
defined $self->{'robot'} ? : 30 8 38
106 9 $self->{robot} = shift;
107 9 $self->userinfo( $self->userinfo ); # update credentials
108 9 return $self->{robot};
109 }
110  
111 6 push @attributes, qw( uri userinfo robot );
112  
113 #
114 # CONSTRUCTOR
115 #
116  
117 sub new {
118 11 ▶ my ( $class, %args ) = @_;
Subroutine new: called CC=5 SCAR=16.1
Pod documented
119  
120 # create the object
121 11 my $self = bless {}, $class;
122  
123 # get the rest of attributes
124 $self->$_( delete $args{$_} )
125 11 for grep { exists $args{$_} } @attributes;
126  
127 # bring in the robot if needed
128 9 ▶ if ( !$self->robot ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->robot) 8 1 9
129 8 my %mech_options = (
130 agent => "WWW::Mailman/$VERSION",
131 stack_depth => 2, # make it a Bear of Very Little Brain
132 quiet => 1,
133 autocheck => 0, # Fancy my making a mistake like that
134 );
135 $mech_options{cookie_jar} = HTTP::Cookies->new(
136 file => delete $args{cookie_file},
137 ignore_discard => 1, # Promise me you'll never forget me
138 autosave => 1,
139 8 ▶ ) if exists $args{cookie_file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"cookie_file"} 1 7 8
140 8 $self->robot( WWW::Mechanize->new(%mech_options) );
141 }
142  
143 # some unknown parameters remain
144 9 ▶ croak "Unknown constructor parameters: @{ [ keys %args ] }"
Branch: 1 branch
BranchTrueFalseTotal
if keys %args 1 8 9
145 if keys %args;
146  
147 8 return $self;
148 }
149  
150 #
151 # PRIVATE METHODS
152 #
153 sub _uri_for {
154 13 ▶ my ( $self, $action, @options ) = @_;
Subroutine _uri_for: called CC=4 SCAR=14.0
155 13 my $uri = URI->new();
156 13 ▶ $uri->scheme( $self->secure ? 'https' : 'http' );
Branch: 1 branch
BranchTrueFalseTotal
$self->secure ? : 5 8 13
157 13 ▶ $uri->userinfo( $self->userinfo )
Branch: 1 branch
BranchTrueFalseTotal
if $self->userinfo 0 13 13
158 if $self->userinfo;
159 13 $uri->host( $self->server );
160 13 ▶ $uri->path( join '/', $self->prefix || (),
Condition66%2/3
$self->prefix || ()
l!l&&r!l&&!r
490
MC/DC0%0/2
$self->prefix || ()
$self->prefix ()
Truth table66%2/3
A || B
A= $self->prefixB= ()
ABresult
1X1
011
000
161 $self->program, $action, $self->list, @options );
162 13 return $uri;
163 }
164  
165 sub _login_form {
166 0 ▶ my ($self) = @_;
Subroutine _login_form: not called CC=5 SCAR=34.0
167 0 my $mech = $self->robot;
168  
169 # shortcut
170 0 ▶ return if !$mech->forms;
Branch: 1 branch
BranchTrueFalseTotal
unless $mech->forms 0 0 0
171  
172 0 my $form;
173  
174 # login is required if the form asks for:
175 # - a login/password
176 0 ▶ if ( $form = $mech->form_with_fields('password') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($form = $mech->form_with_fields('password')) { } 0 0 0
elsif ($form = $mech->form_with_fields('adminpw')) { } 0 0 0
177 0 $form->value( email => $self->email );
178 0 $form->value( password => $self->password );
179 }
180  
181 # - an admin (or moderator) password
182 elsif ( $form = $mech->form_with_fields('adminpw') ) {
183 0 ▶ $form->value( adminpw => $self->admin_password
Condition0%0/3
$self->admin_password || $self->moderator_password
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->admin_password || $self->moderator_password
$self->admin_password $self->moderator_password
Truth table0%0/3
A || B
A= $self->admin_passwordB= $self->moderator_password
ABresult
1X1
011
000
184 || $self->moderator_password );
185 }
186  
187 # otherwise, no authentication required
188  
189 0 return $form;
190 }
191  
192 sub _load_uri {
193 0 ▶ my ( $self, $uri ) = @_;
Subroutine _load_uri: not called CC=5 SCAR=34.0
194 0 my $mech = $self->robot;
195 0 $mech->get($uri);
196  
197 # authentication required?
198 0 ▶ if ( my $form = $self->_login_form ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $form = $self->_login_form) 0 0 0
199 0 $mech->request( $form->click );
200 0 ▶ croak "Couldn't login on $uri" if $self->_login_form;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_login_form 0 0 0
201 }
202  
203 # get the version if we don't have it yet
204 $self->{version} = $1
205 if !exists $self->{version}
206 0 ▶ && $mech->content =~ /<br>version (\d+\.\d+\.\d+\w*)</;
Branch: 1 branch
BranchTrueFalseTotal
if not exists $self->{"version"} and $mech->content =~ /<br>version (\d+\.\d+\.\d+\w*)</ 0 0 0
Condition0%0/3
not exists $self->{"version"} and $mech->content =~ /<br>version (\d+\.\d+\.\d+\w*)</
!ll&&!rl&&r
000
MC/DC0%0/2
not exists $self->{"version"} and $mech->content =~ /<br>version (\d+\.\d+\.\d+\w*)</
not exists $self->{"version"} $mech->content =~ /<br>version (\d+\.\d+\.\d+\w*)</
Truth table0%0/3
not(A) and B
A= not exists $self->{"version"}B= $mech->content =~ /<br>version (\d+\.\d+\.\d+\w*)</
ABresult
0X0
100
111
207  
208 # we're on!
209 }
210  
211 #
212 # INTERNAL UTILITY FUNCTIONS
213 #
214 sub _form_data {
215 return {
216 map {
217 0 ▶ $_->type eq 'submit' || $_->readonly
Subroutine _form_data: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$_->type eq 'submit' || $_->readonly ? : 0 0 0
Condition0%0/3
$_->type eq "submit" or $_->readonly
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->type eq "submit" or $_->readonly
$_->type eq "submit" $_->readonly
Truth table0%0/3
A or B
A= $_->type eq "submit"B= $_->readonly
ABresult
1X1
011
000
218 ? () # ignore buttons and read-only inputs
219 : ( $_->name => $_->value )
220 } $_[0]->inputs
221 };
222 }
223  
224 #
225 # ACTIONS
226 #
227  
228 # The option form has 5 submit buttons, listed here with their inputs:
229 #
230 # * change-of-address:
231 # - new-address
232 # - confirm-address
233 # - fullname
234 # - changeaddr-globally
235 # * unsub:
236 # - unsubconfirm
237 # * othersubs
238 # * emailpw
239 # * changepw:
240 # - newpw
241 # - confpw
242 # - pw-globally
243 # * options-submit:
244 # - disablemail
245 # - deliver-globally
246 # - digest
247 # - mime
248 # - mime-globally
249 # - dontreceive
250 # - ackposts
251 # - remind
252 # - remind-globally
253 # - conceal
254 # - rcvtopic
255 # - nodupes
256 # - nodupes-globally
257  
258 # most routines will be identical, so generate them:
259 {
260 6 my %options = (
261 address => 'change-of-address',
262 unsub => 'unsub',
263 changepw => 'changepw',
264 options => 'options-submit',
265 );
266 6 ▶ while ( my ( $method, $button ) = each %options ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %options) 24 6 30
267 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
268 *$method = sub {
269 0 ▶ my ( $self, $options ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
270  
271 # select the options form
272 0 my $mech = $self->robot;
273 0 ▶ $self->_load_uri(
Condition0%0/2
$self->email || ''
l!l
00
MC/DC0%0/1
$self->email || ''
$self->email
Truth table0%0/2
A
A= $self->email
Aresult
11
00
274 $self->_uri_for( 'options', $self->email || '' ) );
275 0 $mech->form_with_fields('fullname');
276  
277 # change of options
278 0 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) 0 0 0
279 0 $mech->set_fields(%$options);
280 0 $mech->click($button);
281 0 $mech->form_with_fields('fullname');
282 }
283  
284 0 return _form_data( $mech->current_form );
285 24 };
286 }
287 }
288  
289 # emailpw doesn't need any parameter
290 sub emailpw {
291 0 ▶ my ($self) = @_;
Subroutine emailpw: not called CC=3 SCAR=24.8
Pod documented
292  
293 # no auto-authenticate
294 0 my $mech = $self->robot;
295 0 $mech->get( my $uri = $self->_uri_for( 'options', $self->email ) );
296  
297 0 ▶ if ( $mech->form_with_fields('emailpw') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mech->form_with_fields('emailpw')) { } 0 0 0
elsif ($mech->form_with_fields('login-remind')) { } 0 0 0
298 0 $mech->click('emailpw');
299 }
300 elsif ( $mech->form_with_fields('login-remind') ) {
301 0 $mech->click('login-remind');
302 }
303 else {
304 0 croak "Unable to find a password email form on $uri";
305 }
306 }
307  
308 # othersubs needs some parsing to be useful
309 sub othersubs {
310 0 ▶ my ($self) = @_;
Subroutine othersubs: not called CC=1 SCAR=6.9
Pod documented
311 0 my $mech = $self->robot;
312 0 $self->_load_uri( $self->_uri_for( 'options', $self->email ) );
313 0 $mech->form_with_fields('fullname');
314 0 $mech->click('othersubs');
315  
316 0 my $uri = $mech->uri;
317 return
318 0 map { URI->new_abs( $_, $uri ) }
319 $mech->content =~ m{<li><a href="([^"]+)">[^<]+</a>}g;
320 }
321  
322 sub roster {
323 0 ▶ my ($self) = @_;
Subroutine roster: not called CC=5 SCAR=34.0
Pod documented
324 0 my $mech = $self->robot;
325 0 $self->_load_uri( $self->_uri_for('roster') );
326  
327 # try to detect authentication issues [private_roster]
328 0 ▶ if ( $mech->content !~ /<li>/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mech->content =~ /<li>/) 0 0 0
329  
330 # authenticate through listinfo
331 0 $mech->get( $self->_uri_for('listinfo') );
332 0 my $form = $mech->form_with_fields('roster-pw');
333  
334 # in case the roster is reserved to admins,
335 # we'll try the admin passwords first
336 0 ▶ my $password = $self->admin_password || $self->moderator_password;
Condition0%0/3
$self->admin_password || $self->moderator_password
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->admin_password || $self->moderator_password
$self->admin_password $self->moderator_password
Truth table0%0/3
A || B
A= $self->admin_passwordB= $self->moderator_password
ABresult
1X1
011
000
337 0 ▶ $mech->set_fields( 'roster-email' => $self->email ) if !$password;
Branch: 1 branch
BranchTrueFalseTotal
unless $password 0 0 0
338 0 ▶ $mech->set_fields( 'roster-pw' => $password || $self->password );
Condition0%0/3
$password || $self->password
l!l&&r!l&&!r
000
MC/DC0%0/2
$password || $self->password
$password $self->password
Truth table0%0/3
A || B
A= $passwordB= $self->password
ABresult
1X1
011
000
339 0 $mech->click('SubscriberRoster');
340 }
341  
342 # subscriber list may be empty, e.g. for privacy reasons
343 return
344  
345 # TODO: distinguishes types of subscribers
346 0 map { s/ at /@/; $_ } # [obscure_addresses]
347 $mech->content =~ m{<li><a href[^>]*>([^<]*)</a>}g;
348 }
349  
350 # most admin routines will be identical...
351 sub admin {
352 0 ▶ my ( $self, $section, $options ) = @_;
Subroutine admin: not called CC=2 SCAR=17.9
Pod documented
353 0 my $mech = $self->robot;
354 0 $self->_load_uri( $self->_uri_for( admin => $section ) );
355  
356 # get the main form
357 0 $mech->form_number(1);
358  
359 # change of options
360 0 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) 0 0 0
361 0 $mech->current_form->accept_charset('iso-8859-1');
362 0 $mech->set_fields(%$options);
363 0 $mech->click();
364 0 $mech->form_number(1);
365 }
366  
367 0 return _form_data( $mech->current_form );
368 }
369  
370 # so, use a bit of currying
371 6 for my $section (
372 qw(
373 general passwords language nondigest digest
374 bounce archive gateway autoreply contentfilter topics
375 )
376 )
377 {
378 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
379 0 ▶ *{"admin_$section"} = sub { shift->admin( "$section", @_ ) }
Subroutine __ANON__: not called CC=1 SCAR=1.2
380 66 }
381  
382 sub version {
383 0 ▶ my ($self) = @_;
Subroutine version: not called CC=2 SCAR=17.9
Pod documented
384 0 ▶ return $self->{version} if exists $self->{version};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"version"} 0 0 0
385  
386 # get it as part of a page download
387 0 $self->_load_uri( $self->_uri_for('listinfo') );
388 0 return $self->{version};
389 }
390  
391 6 1;
392  
393 __END__
↑ Summary Build »