blib/lib/Net/MitDK.pm

filter: Statementstmt 12.7% 41 / 321 Branchbran 0.0% 0 / 156 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 20.3% 13 / 64 Podpod 0.0% 0 / 23 totaltotal 8.7% 54 / 614 CC 96 SCAR 45.2
CC
96
Cov
8%
save
420.0
__ANON__
272.0
assemble_mail
56.0
CRAP 91.9
↑ Summary
1 package Net::MitDK;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '0.03';
6 1 ▶use Encode qw(encode decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MIME::Entity;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IO::Lambda qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use IO::Lambda::HTTP::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use IO::Lambda::HTTP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use JSON::XS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub new
17 {
18 0 ▶ my ( $class, %opt ) = @_;
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
19 0 my $self = bless {
20 profile => 'default',
21 ua => IO::Lambda::HTTP::UserAgent->new,
22 root => 'https://gateway.mit.dk/view/client',
23 mgr => Net::MitDK::ProfileManager->new,
24 session => {},
25 config => {},
26 %opt,
27 }, $class;
28  
29 0 ▶ $self->mgr->homepath( $opt{homepath}) if defined $opt{homepath};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"homepath"} 0 0 0
30  
31 0 ▶ if ( defined $self->{profile}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"profile"}) 0 0 0
32 0 my ($config, $error) = $self->mgr->load( $self->profile );
33 0 ▶ return (undef, $error) unless $config;
Branch: 1 branch
BranchTrueFalseTotal
unless $config 0 0 0
34 0 $self->{config} = $config;
35 }
36  
37 0 return $self;
38 }
39  
40 0 ▶sub config { $_[0]->{config} }
Subroutine config: not called CC=1 SCAR=6.9
Pod undocumented
41  
42 sub refresh_config
43 {
44 0 ▶ my $self = shift;
Subroutine refresh_config: not called CC=3 SCAR=24.8
Pod undocumented
45 0 ▶ if ( $self->mgr->refresh_needed( $self->profile ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mgr->refresh_needed($self->profile)) 0 0 0
46 0 my ($config, $error) = $self->mgr->load( $self->profile );
47 0 ▶ return (undef, $error) unless $config;
Branch: 1 branch
BranchTrueFalseTotal
unless $config 0 0 0
48 0 $self->{config} = $config;
49 }
50 0 return 1;
51 }
52  
53 0 ▶sub ua { $_[0]->{ua} }
Subroutine ua: not called CC=1 SCAR=6.9
Pod undocumented
54 0 ▶sub root { $_[0]->{root} }
Subroutine root: not called CC=1 SCAR=6.9
Pod undocumented
55 0 ▶sub mgr { $_[0]->{mgr} }
Subroutine mgr: not called CC=1 SCAR=6.9
Pod undocumented
56 0 ▶sub token { $_[0]->{config}->{token} }
Subroutine token: not called CC=1 SCAR=6.9
Pod undocumented
57  
58 sub profile
59 {
60 0 ▶ return $_[0]->{profile} unless $#_;
Subroutine profile: not called CC=4 SCAR=30.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $#_ 0 0 0
61 0 my ( $self, $profile ) = @_;
62 0 ▶ return undef if $profile eq $self->{profile};
Branch: 1 branch
BranchTrueFalseTotal
if $profile eq $self->{"profile"} 0 0 0
63  
64 0 my ($config, $error) = $self->mgr->load( $profile );
65 0 ▶ return $error unless $config;
Branch: 1 branch
BranchTrueFalseTotal
unless $config 0 0 0
66  
67 0 $self->{session} = {};
68 0 $self->{config} = $config;
69 0 $self->{profile} = $profile;
70  
71 0 return undef;
72 }
73  
74 sub request
75 {
76 0 ▶ my ($self, $method, $uri, $content, $options) = @_;
Subroutine request: not called CC=4 SCAR=30.0
Pod undocumented
77  
78 0 my ($ok, $error) = $self->refresh_config;
79 0 ▶ return lambda { undef, $error } unless $ok;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
80  
81 0 my %extra;
82 0 ▶ if ($method eq 'get' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'get') { } 0 0 0
83 0 $method = \&HTTP::Request::Common::GET;
84 0 $options = $content;
85 } else {
86 0 $method = \&HTTP::Request::Common::POST;
87 0 $extra{content} = encode_json($content);
88 0 $extra{'content-type'} = 'application/json';
89 }
90 0 ▶ $options //= {};
Condition0%0/2
$options //= {}
l!l
00
MC/DC0%0/1
$options //= {}
$options
Truth table0%0/2
A
A= $options
Aresult
11
00
91  
92 lambda {
93 0 ▶ my $token = $self->config->{token};
Subroutine __ANON__: not called CC=1 SCAR=6.9
94 context $self->ua->request( $method->(
95 $self->root . '/' . $uri,
96 ngdptoken => $token->{ngdp}->{access_token},
97 mitdktoken => $token->{dpp}->{access_token},
98 0 %extra
99 ));
100 tail {
101 0 ▶ my $response = shift;
Subroutine __ANON__: not called CC=16 SCAR=56.1
102 0 ▶ return (undef, $response) unless ref $response;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $response 0 0 0
103  
104 0 my $json;
105 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
106 0 ▶ if ( $response->header('content-type') eq 'application/json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->header('content-type') eq 'application/json') { } 0 0 0
107 0 eval { $json = decode_json($response->content) };
108 0 ▶ goto PLAIN if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
109 0 ▶ goto PLAIN if grep { ! exists $json->{$_} } qw(code message);
Branch: 1 branch
BranchTrueFalseTotal
if grep {not exists $json->{$_};} "code", "message" 0 0 0
110 0 my $err = "$json->{code}: $json->{message}";
111 0 $err .= "(" . join(' ', @{$_->{fieldError}}) . ')'
112 0 ▶ if $json->{fieldError} && ref($_->{fieldError}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $json->{"fieldError"} and ref $_->{"fieldError"} eq "ARRAY" 0 0 0
Condition0%0/3
$json->{"fieldError"} and ref $_->{"fieldError"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$json->{"fieldError"} and ref $_->{"fieldError"} eq "ARRAY"
$json->{"fieldError"} ref $_->{"fieldError"} eq "ARRAY"
Truth table0%0/3
A and B
A= $json->{"fieldError"}B= ref $_->{"fieldError"} eq "ARRAY"
ABresult
0X0
100
111
113 0 return undef, $err;
114 } else {
115 0 PLAIN:
116 return undef, $response->content
117 }
118 }
119  
120 0 ▶ return $response if $options->{raw};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"raw"} 0 0 0
121  
122 0 ▶ return undef, 'invalid content'
Branch: 1 branch
BranchTrueFalseTotal
unless $response->header("Content-Type") eq "application/json" 0 0 0
123 unless $response->header('Content-Type') eq 'application/json';
124  
125 0 eval { $json = decode_json($response->content) };
126 0 ▶ return undef, "invalid response ($@)"
Branch: 1 branch
BranchTrueFalseTotal
unless $json 0 0 0
127 unless $json;
128  
129 0 ▶ if ( $json->{errorMessages} && ref($json->{errorMessages}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($json->{"errorMessages"} and ref $json->{"errorMessages"} eq "ARRAY") 0 0 0
Condition0%0/3
$json->{"errorMessages"} and ref $json->{"errorMessages"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$json->{"errorMessages"} and ref $json->{"errorMessages"} eq "ARRAY"
$json->{"errorMessages"} ref $json->{"errorMessages"} eq "ARRAY"
Truth table0%0/3
A and B
A= $json->{"errorMessages"}B= ref $json->{"errorMessages"} eq "ARRAY"
ABresult
0X0
100
111
130 $error = join("\n", map {
131 0 my $err = "$_->{code}: $_->{message}";
132 0 $err .= "(" . join(' ', @{$_->{fieldError}}) . ')'
133 0 ▶ if $_->{fieldError} && ref($_->{fieldError}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"fieldError"} and ref $_->{"fieldError"} eq "ARRAY" 0 0 0
Condition0%0/3
$_->{"fieldError"} and ref $_->{"fieldError"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{"fieldError"} and ref $_->{"fieldError"} eq "ARRAY"
$_->{"fieldError"} ref $_->{"fieldError"} eq "ARRAY"
Truth table0%0/3
A and B
A= $_->{"fieldError"}B= ref $_->{"fieldError"} eq "ARRAY"
ABresult
0X0
100
111
134 0 $err
135 0 } @{ $json->{errorMessages} });
136 0 ▶ return undef, $error if length $error;
Branch: 1 branch
BranchTrueFalseTotal
if length $error 0 0 0
137 }
138  
139 0 return $json;
140 0 }};
141 }
142  
143 0 ▶sub get { shift->request( get => @_ ) }
Subroutine get: not called CC=1 SCAR=6.9
Pod undocumented
144 0 ▶sub post { shift->request( post => @_ ) }
Subroutine post: not called CC=1 SCAR=6.9
Pod undocumented
145  
146 sub first_login
147 {
148 0 ▶ my ($self, $json) = @_;
Subroutine first_login: not called CC=1 SCAR=6.9
Pod undocumented
149 0 return $self->authorization_refresh( $json->{refresh_token}, $json->{ngdp}->{refresh_token});
150 }
151  
152 sub renew_lease
153 {
154 0 ▶ my ($self) = @_;
Subroutine renew_lease: not called CC=1 SCAR=6.9
Pod undocumented
155 0 my $token = $self->config->{token};
156 0 return $self->authorization_refresh( $token->{dpp}->{refresh_token}, $token->{ngdp}->{refresh_token});
157 }
158  
159 sub update_config
160 {
161 0 ▶ my $self = shift;
Subroutine update_config: not called CC=1 SCAR=6.9
Pod undocumented
162 0 return $self->mgr->save( $self->profile, $self->{config});
163 }
164  
165 sub authorization_refresh
166 {
167 0 ▶ my ($self, $dpp, $ngdp) = @_;
Subroutine authorization_refresh: not called CC=1 SCAR=6.9
Pod undocumented
168 return lambda {
169 0 ▶ context $self->post('authorization/refresh?client_id=view-client-id-mobile-prod-1-id' => {
Subroutine __ANON__: not called CC=1 SCAR=6.9
170 dppRefreshToken => $dpp,
171 ngdpRefreshToken => $ngdp,
172 });
173 tail {
174 0 ▶ my ($json, $error) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
175 0 ▶ return $json, $error unless $json;
Branch: 1 branch
BranchTrueFalseTotal
unless $json 0 0 0
176 0 ▶ return undef, "bad response:".encode_json($json) unless exists $json->{dpp} and exists $json->{ngdp};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $json->{"dpp"} and exists $json->{"ngdp"} 0 0 0
Condition0%0/3
exists $json->{"dpp"} and exists $json->{"ngdp"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $json->{"dpp"} and exists $json->{"ngdp"}
exists $json->{"dpp"} exists $json->{"ngdp"}
Truth table0%0/3
A and B
A= exists $json->{"dpp"}B= exists $json->{"ngdp"}
ABresult
0X0
100
111
177  
178 0 $self->{config}->{token} = $json;
179 0 return $self->update_config;
180 0 }}
181 0 }
182  
183 sub mailboxes
184 {
185 0 ▶ my $self = shift;
Subroutine mailboxes: not called CC=1 SCAR=6.9
Pod undocumented
186  
187 return lambda {
188 0 ▶ return $self->{session}->{mailboxes} if $self->{session}->{mailboxes};
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"session"}{"mailboxes"} 0 0 0
189  
190 0 context $self->get('mailboxes');
191 tail {
192 0 ▶ my ( $json, $error ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
193 0 ▶ return ($json, $error) unless $json;
Branch: 1 branch
BranchTrueFalseTotal
unless $json 0 0 0
194  
195 0 ($json) = grep { $_->{dataSource} eq 'DP_PUBLIC' } @{$json->{groupedMailboxes}->[0]->{mailboxes}};
196 0 ▶ return (undef, "mailboxes: bad structure") unless $json;
Branch: 1 branch
BranchTrueFalseTotal
unless $json 0 0 0
197 0 return $self->{session}->{mailboxes} = $json;
198 0 }};
199 }
200  
201 sub folders
202 {
203 0 ▶ my $self = shift;
Subroutine folders: not called CC=1 SCAR=6.9
Pod undocumented
204  
205 return lambda {
206 0 ▶ return $self->{session}->{folders} if $self->{session}->{folders};
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"session"}{"folders"} 0 0 0
207  
208 0 context $self-> mailboxes;
209 tail {
210 0 ▶ return @_ unless $_[0];
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 0 0
211  
212 context $self->post('folders/query' => {
213 "mailboxes" => { DP_PUBLIC => $self->{session}->{mailboxes}->{id} }
214 0 });
215 tail {
216 0 ▶ my ( $json, $errors ) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
217 0 ▶ return ($json, $errors) unless $json;
Branch: 1 branch
BranchTrueFalseTotal
unless $json 0 0 0
218 0 my %folders;
219 0 ▶ while ( my ( $k, $v ) = each %{$json->{folders}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$json{"folders"};}) 0 0 0
220 0 $folders{$k} = $v->[0]->{id};
221 }
222 0 ▶ return (undef, "folders: bad structure") unless keys %folders;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %folders 0 0 0
223 0 return $self->{session}->{folders} = \%folders;
224 0 }}};
225 }
226  
227  
228 sub messages
229 {
230 0 ▶ my ( $self, $offset, $limit ) = @_;
Subroutine messages: not called CC=1 SCAR=6.9
Pod undocumented
231 return lambda {
232 0 ▶ context $self-> folders;
Subroutine __ANON__: not called CC=1 SCAR=6.9
233 tail {
234 0 ▶ return @_ unless $_[0];
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 0 0
235  
236 0 my $session = $self->{session};
237 context $self->post('messages/query' => {
238 size => $limit,
239 sortFields => ["receivedDateTime:DESC"],
240 folders => [{
241 dataSource => 'DP_PUBLIC',
242 foldersId => [$session->{folders}->{INBOX}],
243 mailboxId => $session->{mailboxes}->{id},
244 0 startIndex => $offset,
245 }],
246 });
247 tail {
248 @_
249 0 ▶ }}};
Subroutine __ANON__: not called CC=1 SCAR=6.9
250 }
251  
252 sub list_all_messages
253 {
254 0 ▶ my $self = shift;
Subroutine list_all_messages: not called CC=1 SCAR=6.9
Pod undocumented
255  
256 0 my $offset = 0;
257 0 my $limit = 100;
258  
259 0 my @ret;
260  
261 return lambda {
262 0 ▶ context $self->messages($offset, $limit);
Subroutine __ANON__: not called CC=1 SCAR=6.9
263 tail {
264 0 ▶ my ($json, $error) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
265 0 ▶ return ($json, $error) unless $json;
Branch: 1 branch
BranchTrueFalseTotal
unless $json 0 0 0
266  
267 0 push @ret, @{ $json->{results} };
268 0 ▶ return \@ret if @{ $json->{results} } < $limit;
Branch: 1 branch
BranchTrueFalseTotal
if @{$json->{"results"};} < $limit 0 0 0
269  
270 0 $offset += $limit;
271 0 context $self->messages($offset, $limit);
272 0 again;
273 0 }};
274 }
275  
276 sub fetch_file
277 {
278 0 ▶ my ( $self, $message, $document, $file ) = @_;
Subroutine fetch_file: not called CC=1 SCAR=6.9
Pod undocumented
279 0 return $self->get('DP_PUBLIC/' .
280 "mailboxes/$self->{session}->{mailboxes}->{id}/" .
281 "messages/$message->{id}/" .
282 "documents/$message->{documents}->[$document]->{id}/" .
283 "files/$message->{documents}->[$document]->{files}->[$file]->{id}/".
284 "content",
285  
286 {raw => 1},
287 );
288 }
289  
290 sub fetch_message_and_attachments
291 {
292 0 ▶ my ($self, $message) = @_;
Subroutine fetch_message_and_attachments: not called CC=1 SCAR=6.9
Pod undocumented
293 0 my @ret;
294  
295 return lambda {
296 0 ▶ my @files;
Subroutine __ANON__: not called CC=4 SCAR=30.0
297 0 my ( $ndoc, $nfile ) = (0,0);
298 0 for my $doc ( @{ $message->{documents} } ) {
299 0 for my $file ( @{ $doc->{files} } ) {
300 0 push @files, [ $ndoc, $nfile++ ];
301 }
302 0 $nfile = 0;
303 0 $ndoc++;
304 }
305 0 ▶ return [] unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 0 0
306  
307 0 ($ndoc, $nfile) = @{ shift @files };
308 0 context $self-> fetch_file($message, $ndoc, $nfile);
309 tail {
310 0 ▶ my ($resp, $error) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
311 0 ▶ return ($resp, $error) unless defined $resp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $resp 0 0 0
312  
313 0 push @ret, [ $ndoc, $nfile, $resp->content ];
314  
315 0 ▶ return \@ret unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 0 0
316 0 ($ndoc, $nfile) = @{ shift @files };
317  
318 0 context $self-> fetch_file($message, $ndoc, $nfile);
319 0 again;
320 0 }};
321 }
322  
323 sub safe_encode
324 {
325 0 ▶ my ($enc, $text) = @_;
Subroutine safe_encode: not called CC=3 SCAR=24.8
Pod undocumented
326 0 utf8::downgrade($text, 'fail silently please');
327 0 ▶ return (utf8::is_utf8($text) || $text =~ /[\x80-\xff]/) ? encode($enc, $text) : $text;
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($text) || $text =~ /[\x80-\xff]/ ? : 0 0 0
Condition0%0/3
utf8::is_utf8($text) or $text =~ /[\x80-\xff]/
l!l&&r!l&&!r
000
MC/DC0%0/2
utf8::is_utf8($text) or $text =~ /[\x80-\xff]/
utf8::is_utf8($text) $text =~ /[\x80-\xff]/
Truth table0%0/3
A or B
A= utf8::is_utf8($text)B= $text =~ /[\x80-\xff]/
ABresult
1X1
011
000
328 }
329  
330 sub assemble_mail
331 {
332 0 ▶ my ( $self, $msg, $attachments ) = @_;
Subroutine assemble_mail: not called CC=7 SCAR=40.3
Pod undocumented
333  
334 0 my $sender = $msg->{sender}->{label};
335  
336 0 ▶ my $received = $msg->{receivedDateTime} // '';
Condition0%0/2
$msg->{"receivedDateTime"} // ""
l!l
00
MC/DC0%0/1
$msg->{"receivedDateTime"} // ""
$msg->{"receivedDateTime"}
Truth table0%0/2
A
A= $msg->{"receivedDateTime"}
Aresult
11
00
337 0 my $date;
338 0 ▶ if ( $received =~ /^(\d{4})-(\d{2})-(\d{2})T(\d\d):(\d\d):(\d\d)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($received =~ /^(\d{4})-(\d{2})-(\d{2})T(\d\d):(\d\d):(\d\d)/) { } 0 0 0
339 0 $date = DateTime->new(
340 year => $1,
341 month => $2,
342 day => $3,
343 hour => $4,
344 minute => $5,
345 second => $6,
346 );
347 } else {
348 0 $date = DateTime->now;
349 }
350 0 $received = $date->strftime('%a, %d %b %Y %H:%M:%S %z');
351  
352 my $mail = MIME::Entity->build(
353 From => ( safe_encode('MIME-Q', $sender) . ' <noreply@mit.dk>' ) ,
354 To => ( safe_encode('MIME-Q', $self->{session}->{mailboxes}->{ownerName}) . ' <' . ( $ENV{USER} // 'you' ) . '@localhost>' ),
355 0 ▶ Subject => safe_encode('MIME-Header', $msg->{label}),
Condition0%0/2
$ENV{"USER"} // "you"
l!l
00
MC/DC0%0/1
$ENV{"USER"} // "you"
$ENV{"USER"}
Truth table0%0/2
A
A= $ENV{"USER"}
Aresult
11
00
356 Data => encode('utf-8', "Mail from $sender"),
357 Date => $received,
358 Charset => 'utf-8',
359 Encoding => 'quoted-printable',
360 'X-Net-MitDK' => "v/$VERSION",
361 );
362  
363 0 for ( @$attachments ) {
364 0 my ( $ndoc, $nfile, $body ) = @$_;
365 0 my $file = $msg->{documents}->[$ndoc]->{files}->[$nfile];
366 0 my $fn = $file->{filename};
367 0 Encode::_utf8_off($body);
368  
369 my $entity = $mail->attach(
370 Type => $file->{encodingFormat},
371 0 Encoding => 'base64',
372 Data => $body,
373 Filename => $fn,
374 );
375  
376 # XXX hack filename for utf8
377 0 ▶ next unless $fn =~ m/[^\x00-\x80]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $fn =~ /[^\x00-\x80]/ 0 0 0
378 0 $fn = Encode::encode('MIME-B', $fn);
379 0 for ( 'Content-disposition', 'Content-type') {
380 0 my $v = $entity->head->get($_);
381 0 $v =~ s/name="(.*)"/name="$fn"/;
382 0 $entity->head->replace($_, $v);
383 }
384 }
385  
386 return
387 0 'From noreply@localhost ' .
388 $date->strftime('%a %b %d %H:%M:%S %Y') . "\n" .
389 $mail->stringify
390 ;
391 }
392  
393 package
394 Net::MitDK::ProfileManager;
395  
396 1 ▶use Fcntl ':seek', ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
397 1 ▶use JSON::XS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
398  
399 sub new
400 {
401 0 ▶ my $self = bless {
Subroutine new: not called CC=1 SCAR=6.9
402 timestamps => {},
403 homepath => undef,
404 readonly => 0,
405 }, shift;
406 0 return $self;
407 }
408  
409 sub _homepath
410 {
411  
412 0 ▶ if ( exists $ENV{HOME}) {
Subroutine _homepath: not called CC=6 SCAR=37.4
Branch: 3 branches
BranchTrueFalseTotal
if (exists $ENV{'HOME'}) { } 0 0 0
elsif ($^O =~ /win/i and exists $ENV{'USERPROFILE'}) { } 0 0 0
elsif ($^O =~ /win/i and exists $ENV{'WINDIR'}) { } 0 0 0
413 0 return $ENV{HOME};
414 ▶ } elsif ( $^O =~ /win/i && exists $ENV{USERPROFILE}) {
Condition0%0/3
$^O =~ /win/i and exists $ENV{"USERPROFILE"}
!ll&&!rl&&r
000
MC/DC0%0/2
$^O =~ /win/i and exists $ENV{"USERPROFILE"}
$^O =~ /win/i exists $ENV{"USERPROFILE"}
Truth table0%0/3
A and B
A= $^O =~ /win/iB= exists $ENV{"USERPROFILE"}
ABresult
0X0
100
111
415 0 return $ENV{USERPROFILE};
416 ▶ } elsif ( $^O =~ /win/i && exists $ENV{WINDIR}) {
Condition0%0/3
$^O =~ /win/i and exists $ENV{"WINDIR"}
!ll&&!rl&&r
000
MC/DC0%0/2
$^O =~ /win/i and exists $ENV{"WINDIR"}
$^O =~ /win/i exists $ENV{"WINDIR"}
Truth table0%0/3
A and B
A= $^O =~ /win/iB= exists $ENV{"WINDIR"}
ABresult
0X0
100
111
417 0 return $ENV{WINDIR};
418 } else {
419 0 return '.';
420 }
421 }
422  
423 0 ▶sub readonly { $#_ ? $_[0]->{readonly} = $_[1] : $_[0]->{readonly} }
Subroutine readonly: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$#_ ? : 0 0 0
424  
425 sub homepath
426 {
427 0 ▶ $#_ ? $_[0]->{homepath} = $_[1] : ($_[0]->{homepath} // _homepath . '/.mitdk')
Subroutine homepath: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$#_ ? : 0 0 0
Condition0%0/2
$_[0]{"homepath"} // _homepath() . '/.mitdk'
l!l
00
MC/DC0%0/1
$_[0]{"homepath"} // _homepath() . '/.mitdk'
$_[0]{"homepath"}
Truth table0%0/2
A
A= $_[0]{"homepath"}
Aresult
11
00
428 }
429  
430 sub list
431 {
432 0 ▶ my $self = shift;
Subroutine list: not called CC=4 SCAR=30.0
433 0 my $home = $self->homepath;
434  
435 0 ▶ return unless -d $home;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $home 0 0 0
436 0 my @list;
437 0 for my $profile ( <$home/*.profile> ) {
438 0 ▶ $profile =~ m[\/([^\/]+)\.profile] or next;
Branch: 1 branch
BranchTrueFalseTotal
$profile =~ m[\/([^\/]+)\.profile] or next 0 0 0
439 0 push @list, $1;
440 }
441 0 return @list;
442 }
443  
444 sub create
445 {
446 0 ▶ my ($self, $profile, %opt) = @_;
Subroutine create: not called CC=4 SCAR=30.0
447 0 my $file = $self->homepath . "/$profile.profile";
448  
449 0 ▶ if ( -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) 0 0 0
450 0 ▶ return 2 if $opt{ok_if_exists};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"ok_if_exists"} 0 0 0
451 0 return (undef, "Profile exists already");
452 }
453  
454 0 ▶ return $self->save($profile, $opt{payload} // {} );
Condition0%0/2
$opt{"payload"} // {}
l!l
00
MC/DC0%0/1
$opt{"payload"} // {}
$opt{"payload"}
Truth table0%0/2
A
A= $opt{"payload"}
Aresult
11
00
455 }
456  
457 sub lock
458 {
459 0 ▶ my $f = shift;
Subroutine lock: not called CC=3 SCAR=24.8
460 0 ▶ return 1 if flock( $f, LOCK_NB | LOCK_EX);
Branch: 1 branch
BranchTrueFalseTotal
if flock $f, 6 0 0 0
461 0 sleep(1);
462 0 ▶ return 1 if flock( $f, LOCK_NB | LOCK_EX);
Branch: 1 branch
BranchTrueFalseTotal
if flock $f, 6 0 0 0
463 0 sleep(1);
464 0 return flock( $f, LOCK_NB | LOCK_EX);
465 }
466  
467 sub load
468 {
469 0 ▶ my ($self, $profile ) = @_;
Subroutine load: not called CC=5 SCAR=34.0
470 0 my $file = $self->homepath . "/$profile.profile";
471  
472 0 ▶ return (undef, "No such profile") unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
473 0 local $/;
474 0 ▶ open my $f, "<", $file or return (0, "Cannot open $file:$!");
Branch: 1 branch
BranchTrueFalseTotal
open my $f, "<", $file or return 0, "Cannot open ${file}:$!" 0 0 0
475 0 ▶ return (undef, "Cannot acquire lock on $file") unless lock($f);
Branch: 1 branch
BranchTrueFalseTotal
unless Net::MitDK::ProfileManager::lock($f) 0 0 0
476  
477 0 my $r = <$f>;
478 0 close $f;
479  
480 0 my $json;
481 0 eval { $json = decode_json($r) };
482 0 ▶ return (undef, "Corrupted profile $file: $@") unless $json;
Branch: 1 branch
BranchTrueFalseTotal
unless $json 0 0 0
483  
484 0 $self->{timestamps}->{$profile} = time;
485  
486 0 return $json;
487 }
488  
489 sub save
490 {
491 0 ▶ my ($self, $profile, $hash) = @_;
Subroutine save: not called CC=20 SCAR=60.4
492  
493 0 ▶ return (undef, "$profile is readonly") if $self->readonly;
Branch: 1 branch
BranchTrueFalseTotal
if $self->readonly 0 0 0
494  
495 0 my $home = $self->homepath;
496 0 ▶ unless ( -d $home ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $home) 0 0 0
497 0 ▶ mkdir $home or return (undef, "Cannot create $home: $!");
Branch: 1 branch
BranchTrueFalseTotal
mkdir $home or return undef, "Cannot create ${home}: $!" 0 0 0
498 0 ▶ return (undef, "cannot chmod 0750 $home:$!") unless chmod 0750, $home;
Branch: 1 branch
BranchTrueFalseTotal
unless chmod 0750, $home 0 0 0
499 0 ▶ if ( $^O !~ /win32/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O =~ /win32/i) 0 0 0
500 0 my ($name,$passwd,$uid,$gid) = getpwnam('nobody');
501 0 ▶ return (undef, "no user `nobody`") unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
502 0 ▶ return (undef, "cannot chown user:$name $home:$!") unless chown $>, $gid, $home;
Branch: 1 branch
BranchTrueFalseTotal
unless chown $>, $gid, $home 0 0 0
503 }
504 }
505  
506 0 my $json;
507 0 my $encoder = JSON::XS->new->ascii->pretty;
508 0 eval { $json = $encoder->encode($hash) };
509 0 ▶ return (undef, "Cannot serialize profile: $!") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
510  
511 0 my $file = "$home/$profile.profile";
512 0 my $f;
513 0 ▶ if ( -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) { } 0 0 0
514 0 ▶ open $f, "+<", $file or return (undef, "Cannot create $file:$!");
Branch: 1 branch
BranchTrueFalseTotal
open $f, "+<", $file or return undef, "Cannot create ${file}:$!" 0 0 0
515 0 ▶ return (undef, "Cannot acquire lock on $file") unless lock($f);
Branch: 1 branch
BranchTrueFalseTotal
unless Net::MitDK::ProfileManager::lock($f) 0 0 0
516 0 seek $f, 0, SEEK_SET;
517 0 ▶ truncate $f, 0 or return (undef, "Cannot save $file:$!");
Branch: 1 branch
BranchTrueFalseTotal
truncate $f, 0 or return undef, "Cannot save ${file}:$!" 0 0 0
518 } else {
519 0 ▶ open $f, ">", $file or return (undef, "Cannot create $file:$!");
Branch: 1 branch
BranchTrueFalseTotal
open $f, ">", $file or return undef, "Cannot create ${file}:$!" 0 0 0
520 }
521 0 ▶ print $f $json or return (undef, "Cannot save $file:$!");
Branch: 1 branch
BranchTrueFalseTotal
print $f $json or return undef, "Cannot save ${file}:$!" 0 0 0
522 0 ▶ close $f or return (undef, "Cannot save $file:$!");
Branch: 1 branch
BranchTrueFalseTotal
close $f or return undef, "Cannot save ${file}:$!" 0 0 0
523  
524 0 ▶ if ( $^O !~ /win32/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O =~ /win32/i) 0 0 0
525 0 ▶ return (undef, "cannot chmod 0640 $file:$!") unless chmod 0640, $file;
Branch: 1 branch
BranchTrueFalseTotal
unless chmod 0640, $file 0 0 0
526 0 my ($name,$passwd,$uid,$gid) = getpwnam('nobody');
527 0 ▶ return (undef, "no user `nobody`") unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
528 0 ▶ return (undef, "cannot chown user:$name $file:$!") unless chown $>, $gid, $file;
Branch: 1 branch
BranchTrueFalseTotal
unless chown $>, $gid, $file 0 0 0
529 }
530  
531 0 $self->{timestamps}->{$profile} = time;
532  
533 0 return 1;
534 }
535  
536 sub remove
537 {
538 0 ▶ my ($self, $profile) = @_;
Subroutine remove: not called CC=2 SCAR=17.9
539 0 ▶ unlink $self->homepath . "/$profile.profile" or return (undef, "Cannot remove $profile:$!");
Branch: 1 branch
BranchTrueFalseTotal
unlink $self->homepath . "/$profile.profile" or return undef, "Cannot remove ${profile}:$!" 0 0 0
540 0 return 1;
541 }
542  
543 sub refresh_needed
544 {
545 0 ▶ my ( $self, $profile ) = @_;
Subroutine refresh_needed: not called CC=3 SCAR=24.8
546 0 ▶ return 0 unless exists $self->{timestamps}->{$profile};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"timestamps"}{$profile} 0 0 0
547  
548 0 my $file = $self->homepath . "/$profile.profile";
549 0 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($file);
550 0 ▶ return 0 unless defined $mtime;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mtime 0 0 0
551  
552 0 return $mtime > $self->{timestamps}->{$profile};
553 }
554  
555 1 1;
556  
557 =pod
558  
559 =head1 NAME
560  
561 Net::MitDK - perl API for http://mit.dk/
562  
563 =head1 DESCRIPTION
564  
565 Read-only interface for MitDK. See README for more info.
566  
567 =head1 AUTHOR
568  
569 Dmitry Karasik <dmitry@karasik.eu.org>
570  
571 =cut
↑ Summary