| 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
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ( defined $self->{profile}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | my ($config, $error) = $self->mgr->load( $self->profile ); | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | return (undef, $error) unless $config; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 46 | 0 | my ($config, $error) = $self->mgr->load( $self->profile ); | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | return (undef, $error) unless $config; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 61 | 0 | my ( $self, $profile ) = @_; | |||||||||||||||||||||||||||
| 62 | 0 | ▶ | return undef if $profile eq $self->{profile}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | my ($config, $error) = $self->mgr->load( $profile ); | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | return $error unless $config; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | my %extra; | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | if ($method eq 'get' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 //= {}
MC/DC0%0/1
$options //= {}
$options
Truth table0%0/2
A
A= $options
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | my $json; | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | unless ($response->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if ( $response->header('content-type') eq 'application/json') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | eval { $json = decode_json($response->content) }; | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | goto PLAIN if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | goto PLAIN if grep { ! exists $json->{$_} } qw(code message); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$json->{"fieldError"} and ref $_->{"fieldError"} eq "ARRAY"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | return undef, 'invalid content' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 127 | unless $json; | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ( $json->{errorMessages} && ref($json->{errorMessages}) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$json->{"errorMessages"} and ref $json->{"errorMessages"} eq "ARRAY"
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"
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$_->{"fieldError"} and ref $_->{"fieldError"} eq "ARRAY"
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"
| |||||||||||||||||||||||||||||
| 134 | 0 | $err | |||||||||||||||||||||||||||
| 135 | 0 | } @{ $json->{errorMessages} }); | |||||||||||||||||||||||||||
| 136 | 0 | ▶ | return undef, $error if length $error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | return undef, "bad response:".encode_json($json) unless exists $json->{dpp} and exists $json->{ngdp}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $json->{"dpp"} and exists $json->{"ngdp"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 218 | 0 | my %folders; | |||||||||||||||||||||||||||
| 219 | 0 | ▶ | while ( my ( $k, $v ) = each %{$json->{folders}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | 0 | $folders{$k} = $v->[0]->{id}; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | 0 | ▶ | return (undef, "folders: bad structure") unless keys %folders; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 0 | push @ret, @{ $json->{results} }; | |||||||||||||||||||||||||||
| 268 | 0 | ▶ | return \@ret if @{ $json->{results} } < $limit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 0 | push @ret, [ $ndoc, $nfile, $resp->content ]; | |||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 0 | ▶ | return \@ret unless @files; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
utf8::is_utf8($text) or $text =~ /[\x80-\xff]/
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]/
| |||||||||||||||||||||||||||||
| 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"} // ""
MC/DC0%0/1
$msg->{"receivedDateTime"} // ""
$msg->{"receivedDateTime"}
Truth table0%0/2
A
A= $msg->{"receivedDateTime"}
| |||||||||||||||||||||||||||||
| 337 | 0 | my $date; | |||||||||||||||||||||||||||
| 338 | 0 | ▶ | if ( $received =~ /^(\d{4})-(\d{2})-(\d{2})T(\d\d):(\d\d):(\d\d)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$ENV{"USER"} // "you"
$ENV{"USER"}
Truth table0%0/2
A
A= $ENV{"USER"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 413 | 0 | return $ENV{HOME}; | |||||||||||||||||||||||||||
| 414 | ▶ | } elsif ( $^O =~ /win/i && exists $ENV{USERPROFILE}) { | |||||||||||||||||||||||||||
|
Condition0%0/3
$^O =~ /win/i and exists $ENV{"USERPROFILE"}
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"}
| |||||||||||||||||||||||||||||
| 415 | 0 | return $ENV{USERPROFILE}; | |||||||||||||||||||||||||||
| 416 | ▶ | } elsif ( $^O =~ /win/i && exists $ENV{WINDIR}) { | |||||||||||||||||||||||||||
|
Condition0%0/3
$^O =~ /win/i and exists $ENV{"WINDIR"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/2
$_[0]{"homepath"} // _homepath() . '/.mitdk'
MC/DC0%0/1
$_[0]{"homepath"} // _homepath() . '/.mitdk'
$_[0]{"homepath"}
Truth table0%0/2
A
A= $_[0]{"homepath"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 436 | 0 | my @list; | |||||||||||||||||||||||||||
| 437 | 0 | for my $profile ( <$home/*.profile> ) { | |||||||||||||||||||||||||||
| 438 | 0 | ▶ | $profile =~ m[\/([^\/]+)\.profile] or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 450 | 0 | ▶ | return 2 if $opt{ok_if_exists}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 451 | 0 | return (undef, "Profile exists already"); | |||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | 0 | ▶ | return $self->save($profile, $opt{payload} // {} ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$opt{"payload"} // {}
MC/DC0%0/1
$opt{"payload"} // {}
$opt{"payload"}
Truth table0%0/2
A
A= $opt{"payload"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 461 | 0 | sleep(1); | |||||||||||||||||||||||||||
| 462 | 0 | ▶ | return 1 if flock( $f, LOCK_NB | LOCK_EX); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 473 | 0 | local $/; | |||||||||||||||||||||||||||
| 474 | 0 | ▶ | open my $f, "<", $file or return (0, "Cannot open $file:$!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 475 | 0 | ▶ | return (undef, "Cannot acquire lock on $file") unless lock($f); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | 0 | my $home = $self->homepath; | |||||||||||||||||||||||||||
| 496 | 0 | ▶ | unless ( -d $home ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 497 | 0 | ▶ | mkdir $home or return (undef, "Cannot create $home: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 498 | 0 | ▶ | return (undef, "cannot chmod 0750 $home:$!") unless chmod 0750, $home; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 499 | 0 | ▶ | if ( $^O !~ /win32/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 500 | 0 | my ($name,$passwd,$uid,$gid) = getpwnam('nobody'); | |||||||||||||||||||||||||||
| 501 | 0 | ▶ | return (undef, "no user `nobody`") unless defined $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 502 | 0 | ▶ | return (undef, "cannot chown user:$name $home:$!") unless chown $>, $gid, $home; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | 0 | my $file = "$home/$profile.profile"; | |||||||||||||||||||||||||||
| 512 | 0 | my $f; | |||||||||||||||||||||||||||
| 513 | 0 | ▶ | if ( -f $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 514 | 0 | ▶ | open $f, "+<", $file or return (undef, "Cannot create $file:$!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 515 | 0 | ▶ | return (undef, "Cannot acquire lock on $file") unless lock($f); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 516 | 0 | seek $f, 0, SEEK_SET; | |||||||||||||||||||||||||||
| 517 | 0 | ▶ | truncate $f, 0 or return (undef, "Cannot save $file:$!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 518 | } else { | ||||||||||||||||||||||||||||
| 519 | 0 | ▶ | open $f, ">", $file or return (undef, "Cannot create $file:$!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||
| 521 | 0 | ▶ | print $f $json or return (undef, "Cannot save $file:$!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 522 | 0 | ▶ | close $f or return (undef, "Cannot save $file:$!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | 0 | ▶ | if ( $^O !~ /win32/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 525 | 0 | ▶ | return (undef, "cannot chmod 0640 $file:$!") unless chmod 0640, $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 526 | 0 | my ($name,$passwd,$uid,$gid) = getpwnam('nobody'); | |||||||||||||||||||||||||||
| 527 | 0 | ▶ | return (undef, "no user `nobody`") unless defined $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 528 | 0 | ▶ | return (undef, "cannot chown user:$name $file:$!") unless chown $>, $gid, $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||