blib/lib/Net/Eboks.pm

filter: Statementstmt 17.9% 46 / 257 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 61 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 28.3% 15 / 53 Podpod 0.0% 0 / 24 totaltotal 11.7% 61 / 518 CC 74 SCAR 42.2
CC
74
Cov
11%
assemble_mail
306.0
response
132.0
__ANON__
110.0
CRAP 68.3
↑ Summary
1 package Net::Eboks;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Digest::SHA qw(sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use LWP::ConnCache;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use MIME::Entity;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use IO::Lambda qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use IO::Lambda::HTTP qw(http_request);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Crypt::OpenSSL::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 our $VERSION = '0.11';
20  
21 sub new
22 0 ▶{
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
23 0 my ( $class, %opts ) = @_;
24 my $self = bless {
25 cpr => '0000000000',
26 password => '',
27 country => 'DK',
28 type => 'P',
29 datetime => DateTime->now->strftime('%Y-%m-%d %H:%M:%SZ'),
30 root => 'rest.e-boks.dk',
31 mailapp => '/mobile/1/xml.svc/en-gb',
32 deviceid => 'DEADBEEF-1337-1337-1337-900000000002',
33  
34 nonce => '',
35 sessionid => '',
36 response => "3a1a51f235a8bd6bbc29b2caef986a1aeb77018d60ffdad9c5e31117e7b6ead3", # XXX
37 uid => undef,
38 uname => undef,
39 share_id => '0',
40 conn_cache => LWP::ConnCache->new,
41  
42 0 ▶ from => $ENV{MAILFROM} // 'noreply@e-boks.dk',
Condition0%0/2
$ENV{"MAILFROM"} // "noreply\@e-boks.dk"
l!l
00
MC/DC0%0/1
$ENV{"MAILFROM"} // "noreply\@e-boks.dk"
$ENV{"MAILFROM"}
Truth table0%0/2
A
A= $ENV{"MAILFROM"}
Aresult
11
00
43  
44 %opts,
45 }, $class;
46  
47 0 return $self;
48 }
49  
50 0 ▶sub set { $_[0]->{$_[1]} = $_[2] }
Subroutine set: not called CC=1 SCAR=6.9
Pod undocumented
51  
52 sub response
53 0 ▶{
Subroutine response: not called CC=11 SCAR=48.8
Pod undocumented
54 0 my ($self, $decode, $response) = @_;
55  
56 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
57 0 ▶ my $sl = $response->message // $response-> status_line;
Condition0%0/3
$response->message // $response->status_line
l!l&&r!l&&!r
000
MC/DC0%0/2
$response->message // $response->status_line
$response->message $response->status_line
Truth table0%0/3
A // B
A= $response->messageB= $response->status_line
ABresult
1X1
011
000
58 0 chomp $sl;
59 0 $sl =~ s/\+/ /g;
60 0 return undef, $sl;
61 }
62
63 0 for ( split /,\s*/, $response->header('x-eboks-authenticate')) {
64 0 ▶ warn "bad x-eboks-authenticate: $_\n" unless m/^(sessionid|nonce)="(.*?)"$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^(sessionid|nonce)="(.*?)"$/ 0 0 0
65 0 $self->{$1} = $2;
66 }
67
68 0 ▶ return $response->decoded_content unless $decode;
Branch: 1 branch
BranchTrueFalseTotal
unless $decode 0 0 0
69
70 0 ▶ my %options = ref($decode) ? %$decode : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $decode ? : 0 0 0
71 0 my $content = $response->decoded_content;
72 0 ▶ if ( $content !~ /[^\x00-\xff]/ && $content =~ /[\x80-\xff]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $content =~ /[^\x00-\xff]/ and $content =~ /[\x80-\xff]/) 0 0 0
Condition0%0/3
not $content =~ /[^\x00-\xff]/ and $content =~ /[\x80-\xff]/
!ll&&!rl&&r
000
MC/DC0%0/2
not $content =~ /[^\x00-\xff]/ and $content =~ /[\x80-\xff]/
not $content =~ /[^\x00-\xff]/ $content =~ /[\x80-\xff]/
Truth table0%0/3
not(A) and B
A= not $content =~ /[^\x00-\xff]/B= $content =~ /[\x80-\xff]/
ABresult
0X0
100
111
73 # try to upgrade
74 0 eval {
75 0 my $c = decode('latin1', $content);
76 0 $content = $c;
77 };
78 }
79 0 my $xml = XMLin($content, ForceArray => 1, %options);
80 0 ▶ if ( $xml && ref($xml) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml and ref $xml eq 'HASH') { } 0 0 0
Condition0%0/3
$xml and ref $xml eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$xml and ref $xml eq "HASH"
$xml ref $xml eq "HASH"
Truth table0%0/3
A and B
A= $xmlB= ref $xml eq "HASH"
ABresult
0X0
100
111
81 0 return $xml;
82 } else {
83 0 return undef, "xml returned is not a hash";
84 }
85 }
86  
87 sub login
88 0 ▶{
Subroutine login: not called CC=2 SCAR=17.9
Pod undocumented
89 0 my $self = shift;
90  
91 0 ▶ return undef if defined $self->{uid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"uid"} 0 0 0
92  
93 0 local $self->{challenge} = sha256_hex(sha256_hex(join(':', EBOKS => @{$self}{
94 0 qw(deviceid type cpr country password datetime)})));
95 0 my $authstr = 'logon ' . join(',', map { "$_=\"$self->{$_}\"" } qw(deviceid datetime challenge));
96 0 my $content = <<XML;
97 <Logon xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
98 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:eboks:mobile:1.0.0">
99 <User identity="$self->{cpr}" identityType="$self->{type}"
100 nationality="$self->{country}" pincode="$self->{password}"/>
101 </Logon>
102 XML
103  
104 my $login = HTTP::Request->new(
105 'PUT',
106 'https://' . $self->{root} . '/mobile/1/xml.svc/en-gb/session',
107 [
108 'Content-Type' => 'application/xml',
109 'Content-Length' => length($content),
110 'X-EBOKS-AUTHENTICATE' => $authstr,
111 'Accept' => '*/*',
112 'Accept-Language' => 'en-US',
113 'Accept-Encoding' => 'gzip,deflate',
114 'Host' => $self->{root},
115 0 ],
116 $content
117 );
118 0 $login->protocol('HTTP/1.1');
119  
120 0 ▶ return $login, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
121 0 my ($xml, $error) = $self-> response({ForceArray => 0}, @_);
122 0 ▶ return $xml, $error unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 0 0
123 0 ▶ return undef, "'User' is not present in response" unless exists $xml->{User};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $xml->{"User"} 0 0 0
124  
125 0 $self->{uid} = $xml->{User}->{userId};
126 0 $self->{uname} = $xml->{User}->{name};
127 0 return $self->{uname};
128 0 };
129 }
130  
131 sub login_nemid
132 0 ▶{
Subroutine login_nemid: not called CC=2 SCAR=17.9
Pod undocumented
133 0 my $self = shift;
134  
135 0 ▶ return undef if defined $self->{uid};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"uid"} 0 0 0
136  
137 # openssl genrsa -out id_rsa 2048
138 0 my $pk = Crypt::OpenSSL::RSA->new_private_key(<<'PVT');
139 -----BEGIN RSA PRIVATE KEY-----
140 MIIEpAIBAAKCAQEA2VUahnbWKIY4rn8jEthY9M2BoMIHoNQlY4YUL9pV+MpSKyy9
141 MjVKV6h8ERnj+1wxUJDR3ZJimYnvcruGqlSR+uhL8MJs7GqSSOL3zKbZiHmip1/j
142 /9Wzsu86VJibxd14/5r8OugIJDs+aeE6fxpKW1BtUiiUAvlbC4MwnAnCPemzl7gG
143 qi64xsSaVdoi0NzZpxI+ItP9x89eMw64F5GlIviGJ9hODyW3ckKSvgxEQGf7x9TN
144 toVt1Gxh4jdokalHmgNQy4zaqnzGLstl227HIEfbbzX/rK30FFVurFG0JAE9T7z7
145 b0S5RkGFx4GgKGRoFRd8HE+UptBa4JyvmvA3MQIDAQABAoIBAQCtEkbDWhOFxg2R
146 eJGXyk5c9OMMADhO7WKw9O9ShE7+hzAUTdaFC0ces3/JppKVc3+aJxnZl1+J4fyb
147 o5bEQgDWjPMc0dgoFV5VSNoJUb3eHu9W1tgcvjQShMww3i7+zTY0Z1oCFxGUuNEl
148 REVvPqKEQXItgT8Nd0H30wt815Cl9+RlaXMmNRq6aCB0GSUHpVGmgasiUejk7Zej
149 rp23LarcmZitiQXGt0yCbW35/6553Ph88W4cgfav6y+LKTxK06UkKh/QRJaCoKLV
150 BXVmb5HCZv5waU5eRaxV/TKTATDU45DuU3f76+OlQp0P/cH4EVXhC+95fLJ1XUxG
151 lSIQW+MhAoGBAO7N0o5llwbgNpxzZ6z4mRni3LUFpURozNT3q616qM7QGBRfDeHI
152 o1cZf8wRWmMBdA16iWT7xMTpRfHcHt8NW+XlzisQG5KOlrUh1ZtRrltFGPnxUd84
153 EgbUK+ArzU4mqZZMETZBmrJqVO1lB0dhrjqZ9SDUeXimNqZCoGHU/Z9VAoGBAOj7
154 d/bhp4OtRKBDgKF1IThikgnsZAcBfGCXaASrPFAbi1p4MNEHUNkQXG7V/PSxMlUf
155 y3U35Hsxexpq2Al98gw/TUBdgb/WfDJHole1fbXTd/Gh9H8RdMLSnOdLTCMXvW9r
156 e1DKt8/5fb87BE8xQUc9sXJ5mmx522WqEyOXZeBtAoGAbIUQCDHWXgOKDbLMDGi0
157 enUDwyeboOjXHHiohZ9WExWxu6AumMoqoCwwTTYdkxxX9sAWq9NV6f3wESbsyIQz
158 nNe/xwX84a72gb2sanbF+yf9X6fwgrXiS0Qj5C1DkR40tt4+fB94A1ga2/6rPh7/
159 pBXOtWqZAODXuNpSM+MsljkCgYBWV9u9wyMxyaUFP/8L1zzYiK9WviTT89kEcxg5
160 orxXc93RSXnN/cgYqdeXu/ZjOMhOg9oDNxOWFGBrCe3GlsZ9g3g9wmmzjum4OJQR
161 rVFJcXWiN0NFVFLRYPyFO4Kb/tBV2p948afti6julhCiyL5IiLSamDaCvSZyJvWw
162 2wsGgQKBgQCt6ityOP56Co60pUnhonmNLg98IvWnREn8xoGdjtrMuk+Ksf5sUX5i
163 fcogYnJ4ciLmJ37cVXfOtrRrDsjqSbHY07Oqb0qdIKPATJkiK0ltXG4hSvjB2LPU
164 XC53Clpk+n6+ltUJnUAFtl8g4jcUG9Bs+334WiX0n7Hx7yQlsvzBtA==
165 -----END RSA PRIVATE KEY-----
166 PVT
167 0 local $self->{challenge} = join(':', q(EBOKS), @{$self}{qw(deviceid type cpr country password datetime)});
168 0 $self->{challenge} = encode_base64($pk->sign($self->{challenge}));
169  
170 0 my $authstr = 'logon ' . join(',', map { "$_=\"$self->{$_}\"" } qw(deviceid datetime challenge));
171 0 my $content = <<XML;
172 <Logon xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
173 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:eboks:mobile:1.0.0">
174 <App version="$VERSION" os="$^O" osVersion="1" Device="Net-Eboks"/>
175 <User identity="$self->{cpr}" identityType="$self->{type}"
176 nationality="$self->{country}" pincode="$self->{password}"/>
177 </Logon>
178 XML
179  
180 my $login = HTTP::Request->new(
181 'PUT',
182 'https://' . $self->{root} . $self->{mailapp} . '/session',
183 [
184 'Content-Type' => 'application/xml',
185 'Content-Length' => length($content),
186 'X-EBOKS-AUTHENTICATE' => $authstr,
187 'Accept' => '*/*',
188 'Accept-Language' => 'en-US',
189 'Accept-Encoding' => 'gzip,deflate',
190 'Host' => $self->{root},
191 0 ],
192 $content
193 );
194 0 $login->protocol('HTTP/1.1');
195  
196 0 ▶ return $login, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
197 0 my ($xml, $error) = $self-> response({ForceArray => 0}, @_);
198 0 ▶ return $xml, $error unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 0 0
199 0 ▶ return undef, "'User' is not present in response" unless exists $xml->{User};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $xml->{"User"} 0 0 0
200  
201 0 $self->{uid} = $xml->{User}->{userId};
202 0 $self->{uname} = $xml->{User}->{name};
203 0 return $self->{uname};
204 0 };
205 }
206  
207 sub public_key
208 0 ▶{
Subroutine public_key: not called CC=1 SCAR=6.9
Pod undocumented
209  
210 # openssl rsa -in id_rsa -outform PEM -pubout -out id_rsa.pub
211 0 return join '', grep {!/^--/} split /\n/, <<'PUB';
212 -----BEGIN PUBLIC KEY-----
213 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2VUahnbWKIY4rn8jEthY
214 9M2BoMIHoNQlY4YUL9pV+MpSKyy9MjVKV6h8ERnj+1wxUJDR3ZJimYnvcruGqlSR
215 +uhL8MJs7GqSSOL3zKbZiHmip1/j/9Wzsu86VJibxd14/5r8OugIJDs+aeE6fxpK
216 W1BtUiiUAvlbC4MwnAnCPemzl7gGqi64xsSaVdoi0NzZpxI+ItP9x89eMw64F5Gl
217 IviGJ9hODyW3ckKSvgxEQGf7x9TNtoVt1Gxh4jdokalHmgNQy4zaqnzGLstl227H
218 IEfbbzX/rK30FFVurFG0JAE9T7z7b0S5RkGFx4GgKGRoFRd8HE+UptBa4JyvmvA3
219 MQIDAQAB
220 -----END PUBLIC KEY-----
221 PUB
222 }
223  
224 sub session_activate
225 0 ▶{
Subroutine session_activate: not called CC=1 SCAR=6.9
Pod undocumented
226 0 my ($self, $ticket) = @_;
227  
228 0 my $pubkey = $self->public_key;
229 0 my $authstr = join(',', map { "$_=\"$self->{$_}\"" } qw(deviceid nonce sessionid response));
230 0 my $content = <<XML;
231 <?xml version="1.0" encoding="utf-8"?>
232 <Activation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
233 xmlns:xsd="http://www.w3.org/2001/XMLSchema" deviceId="$self->{deviceid}"
234 deviceName="Net-Eboks" deviceOs="$^O" key="$pubkey" ticket="$ticket"
235 ticketType="KspWeb" xmlns="urn:eboks:mobile:1.0.0" />
236 XML
237  
238 my $login = HTTP::Request->new(
239 'PUT',
240 'https://' . $self->{root} . $self->{mailapp} . "/$self->{uid}/0/session/activate",
241 [
242 'Content-Type' => 'application/xml',
243 'Content-Length' => length($content),
244 'X-EBOKS-AUTHENTICATE' => $authstr,
245 'Accept' => '*/*',
246 'Accept-Language' => 'en-US',
247 'Accept-Encoding' => 'gzip,deflate',
248 'Host' => $self->{root},
249 0 ],
250 $content
251 );
252 0 $login->protocol('HTTP/1.1');
253  
254 0 ▶ return $login, sub { $self-> response(0, @_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
255 }
256  
257 0 ▶sub ua { LWP::UserAgent->new(conn_cache => shift->{conn_cache}) }
Subroutine ua: not called CC=1 SCAR=6.9
Pod undocumented
258  
259 sub get
260 0 ▶{
Subroutine get: not called CC=1 SCAR=6.9
Pod undocumented
261 0 my ($self, $path) = @_;
262 0 my $authstr = join(',', map { "$_=\"$self->{$_}\"" } qw(deviceid nonce sessionid response));
263 my $get = HTTP::Request->new(
264 'GET',
265 'https://' . $self->{root} . $self->{mailapp} . '/' . $path,
266 [
267 'X-EBOKS-AUTHENTICATE' => $authstr,
268 'Accept' => '*/*',
269 'Accept-Language' => 'en-US',
270 'Host' => $self->{root},
271 0 ],
272 );
273 0 $get->protocol('HTTP/1.1');
274 0 return $get;
275 }
276  
277 sub xmlget
278 0 ▶{
Subroutine xmlget: not called CC=1 SCAR=6.9
Pod undocumented
279 0 my ( $self, $uri, $path, %xmlopt ) = @_;
280 return
281 0 ▶ $self->get($uri), sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
282 0 my ($xml, $error) = $self-> response(\%xmlopt, @_);
283 0 ▶ return $xml, $error unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 0 0
284 0 ▶ for my $step ( @{ $path // [] } ) {
Condition0%0/2
$path // []
l!l
00
MC/DC0%0/1
$path // []
$path
Truth table0%0/2
A
A= $path
Aresult
11
00
285 0 ▶ return undef, "key '$step' not found" unless ref $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $xml 0 0 0
286 0 ▶ if ( ref($xml) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $xml eq 'ARRAY') { } 0 0 0
287 0 $xml = $xml->[$step];
288 } else {
289 0 $xml = $xml->{$step};
290 }
291 }
292  
293 0 ▶ my $key = $xmlopt{KeyAttr} // 'name';
Condition0%0/2
$xmlopt{"KeyAttr"} // "name"
l!l
00
MC/DC0%0/1
$xmlopt{"KeyAttr"} // "name"
$xmlopt{"KeyAttr"}
Truth table0%0/2
A
A= $xmlopt{"KeyAttr"}
Aresult
11
00
294 0 ▶ while ( my ( $k, $v ) = each %$xml ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$xml) 0 0 0
295 0 ▶ $v->{$key} = $k if defined($v) && ref($v) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and ref $v eq "HASH" 0 0 0
Condition0%0/3
defined $v and ref $v eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $v and ref $v eq "HASH"
defined $v ref $v eq "HASH"
Truth table0%0/3
A and B
A= defined $vB= ref $v eq "HASH"
ABresult
0X0
100
111
296 }
297  
298 0 return $xml;
299 0 };
300 }
301  
302 sub folders
303 0 ▶{
Subroutine folders: not called CC=3 SCAR=24.8
Pod undocumented
304 0 my ($self, $share_id) = @_;
305 0 ▶ return undef unless $self->{uid};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"uid"} 0 0 0
306 0 ▶ $share_id //= $self->{share_id};
Condition0%0/3
$share_id //= $self->{"share_id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$share_id //= $self->{"share_id"}
$share_id $self->{"share_id"}
Truth table0%0/3
A //= B
A= $share_idB= $self->{"share_id"}
ABresult
1X1
011
000
307 0 $self-> xmlget("$self->{uid}/$share_id/mail/folders", ['FolderInfo']);
308 }
309  
310 sub shares
311 0 ▶{
Subroutine shares: not called CC=2 SCAR=17.9
Pod undocumented
312 0 my $self = shift;
313 0 ▶ return undef unless $self->{uid};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"uid"} 0 0 0
314 0 $self-> xmlget("$self->{uid}/0/shares?listType=active", ['Share']);
315 }
316  
317 sub messages
318 0 ▶{
Subroutine messages: not called CC=5 SCAR=34.0
Pod undocumented
319 0 my ($self, $share_id, $folder_id, $offset, $limit) = @_;
320 0 ▶ return undef unless $self->{uid};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"uid"} 0 0 0
321 0 ▶ $share_id //= $self->{share_id};
Condition0%0/3
$share_id //= $self->{"share_id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$share_id //= $self->{"share_id"}
$share_id $self->{"share_id"}
Truth table0%0/3
A //= B
A= $share_idB= $self->{"share_id"}
ABresult
1X1
011
000
322 0 ▶ $limit //= 1;
Condition0%0/2
$limit //= 1
l!l
00
MC/DC0%0/1
$limit //= 1
$limit
Truth table0%0/2
A
A= $limit
Aresult
11
00
323 0 ▶ $offset //= 0;
Condition0%0/2
$offset //= 0
l!l
00
MC/DC0%0/1
$offset //= 0
$offset
Truth table0%0/2
A
A= $offset
Aresult
11
00
324 0 $self-> xmlget(
325 "$self->{uid}/$share_id/mail/folder/$folder_id?skip=$offset&take=$limit",
326 [ qw(Messages 0 MessageInfo) ],
327 KeyAttr => 'id'
328 );
329 }
330  
331 sub message
332 0 ▶{
Subroutine message: not called CC=3 SCAR=24.8
Pod undocumented
333 0 my ($self, $share_id, $folder_id, $message_id) = @_;
334 0 ▶ return undef unless $self->{uid};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"uid"} 0 0 0
335 0 ▶ $share_id //= $self->{share_id};
Condition0%0/3
$share_id //= $self->{"share_id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$share_id //= $self->{"share_id"}
$share_id $self->{"share_id"}
Truth table0%0/3
A //= B
A= $share_idB= $self->{"share_id"}
ABresult
1X1
011
000
336 0 $self-> xmlget(
337 "$self->{uid}/$share_id/mail/folder/$folder_id/message/$message_id",
338 [],
339 KeyAttr => 'id'
340 );
341 }
342  
343 sub content
344 0 ▶{
Subroutine content: not called CC=3 SCAR=24.8
Pod undocumented
345 0 my ( $self, $share_id, $folder_id, $content_id ) = @_;
346 0 ▶ return undef unless $self->{uid};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"uid"} 0 0 0
347 0 ▶ $share_id //= $self->{share_id};
Condition0%0/3
$share_id //= $self->{"share_id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$share_id //= $self->{"share_id"}
$share_id $self->{"share_id"}
Truth table0%0/3
A //= B
A= $share_idB= $self->{"share_id"}
ABresult
1X1
011
000
348 return
349 0 ▶ $self-> get( "$self->{uid}/$share_id/mail/folder/$folder_id/message/$content_id/content" ), sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
350 0 $self-> response( 0, @_ )
351 0 };
352 }
353  
354 0 ▶sub attachments { $_[1]->{Attachements}->[0]->{AttachmentInfo} }
Subroutine attachments: not called CC=1 SCAR=6.9
Pod undocumented
355  
356 0 ▶sub filename {
Subroutine filename: not called CC=2 SCAR=17.9
Pod undocumented
357 0 my $fn = $_[1]-> {name};
358 0 $fn =~ s[:\\\/][_];
359 0 my $fmt = lc($_[1]->{format});
360 0 ▶ $fmt = 'txt' if $fmt eq 'plain';
Branch: 1 branch
BranchTrueFalseTotal
if $fmt eq "plain" 0 0 0
361 return $fn . '.' .lc($_[1]->{format})
362 0 }
363  
364 sub mime_type
365 0 ▶{
Subroutine mime_type: not called CC=5 SCAR=34.0
Pod undocumented
366 0 my $fmt = lc $_[1]->{format};
367 0 ▶ if ( $fmt =~ /^(pdf)$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($fmt =~ /^(pdf)$/) { } 0 0 0
elsif ($fmt =~ /^(gif|jpg|jpeg|tiff|png|webp)$/) { } 0 0 0
elsif ($fmt =~ /^(txt|text|html|plain)$/) { } 0 0 0
368 0 return "application/$fmt";
369 } elsif ( $fmt =~ /^(gif|jpg|jpeg|tiff|png|webp)$/) {
370 0 return "image/$fmt";
371 } elsif ( $fmt =~ /^(txt|text|html|plain)$/) {
372 0 ▶ $fmt = 'plain' if $fmt =~ /^(txt|text)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $fmt =~ /^(txt|text)$/ 0 0 0
373 0 return "text/$fmt";
374 } else {
375 0 return "application/$fmt";
376 }
377 }
378  
379 sub first_value
380 0 ▶{
Subroutine first_value: not called CC=3 SCAR=24.8
Pod undocumented
381 0 my ($self, $entry) = @_;
382 0 ▶ if ( ref($entry) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $entry eq 'HASH') { } 0 0 0
elsif (ref $entry eq 'ARRAY') { } 0 0 0
383 0 my $k = (sort keys %$entry)[0];
384 0 return $entry->{$k};
385 } elsif ( ref($entry) eq 'ARRAY') {
386 0 return $entry->[0];
387 } else {
388 0 return "bad entry";
389 }
390 }
391  
392 sub safe_encode
393 0 ▶{
Subroutine safe_encode: not called CC=3 SCAR=24.8
Pod undocumented
394 0 my ($enc, $text) = @_;
395 0 utf8::downgrade($text, 'fail silently please');
396 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
397 }
398  
399 sub assemble_mail
400 0 ▶{
Subroutine assemble_mail: not called CC=17 SCAR=57.2
Pod undocumented
401 0 my ( $self, %opt ) = @_;
402  
403 0 my $msg = $opt{message};
404 0 my $sender = $self->first_value($msg->{Sender});
405 0 ▶ $sender = $sender->{content} if ref($sender) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $sender eq "HASH" 0 0 0
406 0 ▶ $sender //= 'unknown';
Condition0%0/2
$sender //= "unknown"
l!l
00
MC/DC0%0/1
$sender //= "unknown"
$sender
Truth table0%0/2
A
A= $sender
Aresult
11
00
407  
408 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
409 0 my $date;
410 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
411 0 $date = DateTime->new(
412 year => $1,
413 month => $2,
414 day => $3,
415 hour => $4,
416 minute => $5,
417 second => $6,
418 );
419 } else {
420 0 $date = DateTime->now;
421 }
422 0 $received = $date->strftime('%a, %d %b %Y %H:%M:%S %z');
423  
424 my $mail = MIME::Entity->build(
425 From => $opt{from} // ( safe_encode('MIME-Q', $sender) . " <$self->{from}>" ) ,
426 To => $opt{to} // ( safe_encode('MIME-Q', $self->{uname}) . ' <' . ( $ENV{USER} // 'you' ) . '@localhost>' ),
427 Subject => $opt{subject} // safe_encode('MIME-Header', $msg->{name}),
428 Data => $opt{data} // encode('utf-8', "Mail from $sender"),
429 Date => $opt{date} // $received,
430 Charset => 'utf-8',
431 Encoding => 'quoted-printable',
432 'X-Net-Eboks' => "v/$VERSION",
433 0 ▶ 'X-Net-Eboks-ShareId' => $opt{share_id} // '0',
Condition0%0/17
$opt{"from"} // safe_encode("MIME-Q", $sender) . qq[ <$self->{"from"}>]
l!l
00
$opt{"to"} // safe_encode("MIME-Q", $self->{"uname"}) . ' <' . ($ENV{"USER"} // "you") . '@localhost>'
l!l
00
$ENV{"USER"} // "you"
l!l
00
$opt{"subject"} // safe_encode("MIME-Header", $msg->{"name"})
l!l&&r!l&&!r
000
$opt{"data"} // &encode("utf-8", "Mail from $sender")
l!l&&r!l&&!r
000
$opt{"date"} // $received
l!l&&r!l&&!r
000
$opt{"share_id"} // 0
l!l
00
MC/DC0%0/1
$opt{"from"} // safe_encode("MIME-Q", $sender) . qq[ <$self->{"from"}>]
$opt{"from"}
MC/DC0%0/1
$opt{"to"} // safe_encode("MIME-Q", $self->{"uname"}) . ' <' . ($ENV{"USER"} // "you") . '@localhost>'
$opt{"to"}
MC/DC0%0/1
$ENV{"USER"} // "you"
$ENV{"USER"}
MC/DC0%0/2
$opt{"subject"} // safe_encode("MIME-Header", $msg->{"name"})
$opt{"subject"} safe_encode("MIME-Header", $msg->{"name"})
MC/DC0%0/2
$opt{"data"} // &encode("utf-8", "Mail from $sender")
$opt{"data"} &encode("utf-8", "Mail from $sender")
MC/DC0%0/2
$opt{"date"} // $received
$opt{"date"} $received
MC/DC0%0/1
$opt{"share_id"} // 0
$opt{"share_id"}
Truth table0%0/2
A
A= $opt{"from"}
Aresult
11
00
Truth table0%0/2
A
A= $opt{"to"}
Aresult
11
00
Truth table0%0/2
A
A= $ENV{"USER"}
Aresult
11
00
Truth table0%0/3
A // B
A= $opt{"subject"}B= safe_encode("MIME-Header", $msg->{"name"})
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $opt{"data"}B= &encode("utf-8", "Mail from $sender")
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $opt{"date"}B= $received
ABresult
1X1
011
000
Truth table0%0/2
A
A= $opt{"share_id"}
Aresult
11
00
434 );
435  
436 0 my @attachments;
437 0 ▶ push @attachments, [ $msg, $opt{body} ] if exists $opt{body};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt{"body"} 0 0 0
438  
439 0 my $attachments = $self->attachments($msg);
440 0 for my $att_id ( sort keys %$attachments ) {
441 0 push @attachments, [ $attachments->{$att_id}, $opt{attachments}->{$att_id} ];
442 }
443  
444 0 for ( @attachments ) {
445 0 my ( $msg, $body ) = @$_;
446 0 my $fn = $self->filename($msg);
447 0 Encode::_utf8_off($body);
448 0 my $entity = $mail->attach(
449 Type => $self->mime_type($msg),
450 Encoding => 'base64',
451 Data => $body,
452 Filename => $fn,
453 );
454  
455 # XXX hack filename for utf8
456 0 ▶ next unless $fn =~ m/[^\x00-\x80]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $fn =~ /[^\x00-\x80]/ 0 0 0
457 0 $fn = Encode::encode('MIME-B', $fn);
458 0 for ( 'Content-disposition', 'Content-type') {
459 0 my $v = $entity->head->get($_);
460 0 $v =~ s/name="(.*)"/name="$fn"/;
461 0 $entity->head->replace($_, $v);
462 }
463 }
464  
465 return
466 0 'From noreply@localhost ' .
467 $date->strftime('%a %b %d %H:%M:%S %Y') . "\n" .
468 $mail->stringify
469 ;
470 }
471  
472 sub fetch_request
473 0 ▶{
Subroutine fetch_request: not called CC=2 SCAR=17.9
Pod undocumented
474 0 my ($self, $request, $callback) = @_;
475 0 ▶ return lambda { undef, "bad request" } unless $request;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $request 0 0 0
476 0 ▶ return lambda {
Subroutine __ANON__: not called CC=1 SCAR=6.9
477 context $request,
478 conn_cache => $self->{conn_cache}, #XXX
479 0 keep_alive => 1; #XXX
480 0 ▶ http_request {
Subroutine __ANON__: not called CC=2 SCAR=17.9
481 0 my $response = shift;
482 0 ▶ return undef, $response unless ref $response;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $response 0 0 0
483 0 return $callback->($response);
484 0 }};
485 }
486  
487 sub fetch_message_and_attachments
488 0 ▶{
Subroutine fetch_message_and_attachments: not called CC=1 SCAR=6.9
Pod undocumented
489 0 my ($self, $message ) = @_;
490
491 0 ▶ return lambda {
Subroutine __ANON__: not called CC=1 SCAR=6.9
492 0 context $self-> fetch_request( $self->message( $message->{shareId}, $message->{folderId}, $message->{id} ) );
493 0 ▶ tail {
Subroutine __ANON__: not called CC=2 SCAR=17.9
494 0 my ($xml, $error) = @_;
495 0 ▶ return ($xml, $error) unless defined $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xml 0 0 0
496  
497 0 my $attachments = $self-> attachments( $xml );
498 0 my @attachments = keys %$attachments;
499 my %opt = (
500 message => $xml,
501 share_id => $message->{shareId},
502 0 attachments => {},
503 );
504  
505 0 context $self-> fetch_request( $self-> content( $message->{shareId}, $message->{folderId}, $message->{id} ));
506 0 ▶ tail {
Subroutine __ANON__: not called CC=3 SCAR=24.8
507 0 my ($body, $error) = @_;
508 0 ▶ return ($body, $error) unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 0 0
509 0 $opt{body} = $body;
510
511 0 ▶ my $att_id = shift @attachments or return \%opt;
Branch: 1 branch
BranchTrueFalseTotal
my $att_id = shift @attachments or return \%opt 0 0 0
512 0 context $self-> fetch_request( $self-> content( $message->{shareId}, $message->{folderId}, $att_id ));
513 0 ▶ tail {
Subroutine __ANON__: not called CC=3 SCAR=24.8
514 0 my ($att_body, $error) = @_;
515 0 ▶ return ($att_body, $error) unless defined $att_body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $att_body 0 0 0
516  
517 0 $opt{attachments}->{$att_id} = $att_body;
518 0 ▶ $att_id = shift @attachments or return \%opt;
Branch: 1 branch
BranchTrueFalseTotal
$att_id = shift @attachments or return \%opt 0 0 0
519 0 context $self-> fetch_request( $self-> content( $message->{shareId}, $message->{folderId}, $att_id ));
520 0 again;
521 0 }}}};
522 }
523  
524 sub list_all_messages
525 0 ▶{
Subroutine list_all_messages: not called CC=2 SCAR=17.9
Pod undocumented
526 0 my ( $self, $share_id, $folder_id ) = @_;
527  
528 0 my $offset = 0;
529 0 my $limit = 1000;
530 0 ▶ $share_id //= $self->{share_id};
Condition0%0/3
$share_id //= $self->{"share_id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$share_id //= $self->{"share_id"}
$share_id $self->{"share_id"}
Truth table0%0/3
A //= B
A= $share_idB= $self->{"share_id"}
ABresult
1X1
011
000
531  
532 0 my %ret;
533  
534 0 ▶ return lambda {
Subroutine __ANON__: not called CC=1 SCAR=6.9
535 0 context $self-> fetch_request( $self-> messages( $share_id, $folder_id, $offset, $limit ));
536 0 ▶ tail {
Subroutine __ANON__: not called CC=4 SCAR=30.0
537 0 my ($xml, $error) = @_;
538 0 ▶ return ($xml, $error) unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 0 0
539  
540 0 $_->{shareId} = $share_id for values %$xml;
541 0 %ret = ( %ret, %$xml );
542 0 ▶ return \%ret if keys(%$xml) < $limit;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$xml < $limit 0 0 0
543  
544 0 $offset += $limit;
545 0 context $self-> fetch_request( $self-> messages( $share_id, $folder_id, $offset, $limit ));
546 0 again;
547 0 }};
548 }
549  
550 1 1;
551  
552 =pod
553  
554 =head1 NAME
555  
556 Net::Eboks - perl API for http://eboks.dk/
557  
558 =head1 DESCRIPTION
559  
560 Read-only interface for eboks. See README for more info.
561  
562 =head1 AUTHOR
563  
564 Dmitry Karasik <dmitry@karasik.eu.org>
565  
566 =cut
↑ Summary