blib/lib/Net/CardDAVTalk.pm

filter: Statementstmt 67.8% 169 / 249 Branchbran 35.2% 36 / 102 Conditioncond 44.4% 36 / 81 MC/DCmcdc 2.3% 1 / 42 Subroutinesub 68.9% 20 / 29 Podpod 100% 17 / 17 totaltotal 53.6% 279 / 520 CC 102 SCAR 42.5
CC
102
Cov
56%
GetAddressBooks
221.3
UpdateAddressBook
20.0
NewContact
20.0
CRAP 70.0
↑ Summary
1 package Net::CardDAVTalk;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 2 ▶use Net::DAVTalk;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use base qw(Net::DAVTalk);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Text::JSContact qw(vcard_to_jscontact jscontact_to_vcard);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use XML::Spice;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use URI::Escape qw(uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 =head1 NAME
19  
20 Net::CardDAVTalk - A library for talking to CardDAV servers
21  
22 =head1 VERSION
23  
24 Version 0.13
25  
26 =cut
27  
28 2 our $VERSION = '0.14';
29  
30 2 our $BATCHSIZE = 100;
31  
32  
33 =head1 SYNOPSIS
34  
35 This module maps from CardDAV to an old version of the FastMail API.
36 It's mostly useful as an example of how to talk CardDAV and for the
37 Cyrus IMAP test suite Cassandane.
38  
39 use Net::CardDAVTalk;
40  
41 my $foo = Net::CardDAVTalk->new();
42 ...
43  
44 =head1 SUBROUTINES/METHODS
45  
46 =head2 $class->new()
47  
48 Takes the same arguments as Net::DAVTalk and adds the single
49 namespace:
50  
51 C => 'urn:ietf:params:xml:ns:carddav'
52  
53 =cut
54  
55 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
56 8 my ($Class, %Params) = @_;
57  
58 8 $Params{homesetns} = 'C';
59 8 $Params{homeset} = 'addressbook-home-set';
60 8 $Params{wellknown} = 'carddav';
61  
62 8 my $Self = $Class->SUPER::new(%Params);
63  
64 8 $Self->ns(C => 'urn:ietf:params:xml:ns:carddav');
65 8 $Self->ns(CY => 'http://cyrusimap.org/ns/');
66  
67 8 return $Self;
68 }
69  
70 # Address book methods {{{
71  
72 =head2 $self->NewAddressBook($Path, %Args)
73  
74 Creates a new addressbook collection. Requires the full
75 path (unlike Net::CalDAVTalk, which creates paths by UUID)
76 and takes a single argument, the name:
77  
78 e.g.
79  
80 $CardDAV->NewAddressBook("Default", name => "Addressbook");
81  
82 =cut
83  
84 0 ▶sub NewAddressBook {
Subroutine NewAddressBook: not called CC=2 SCAR=17.9
Pod documented
85 0 my ($Self, $Path, %Args) = @_;
86  
87 0 ▶ $Path || confess 'New address book path not specified';
Branch: 1 branch
BranchTrueFalseTotal
$Path or confess("New address book path not specified") 0 0 0
88  
89 $Self->Request(
90 'MKCOL',
91 "$Path/",
92 x('D:mkcol', $Self->NS(),
93 x('D:set',
94 x('D:prop',
95 x('D:resourcetype',
96 x('D:collection'),
97 x('C:addressbook'),
98 ),
99 0 x('D:displayname', $Args{name}),
100 ),
101 ),
102 ),
103 );
104  
105 0 return $Path;
106 }
107  
108 =head2 $self->DeleteAddressBook($Path)
109  
110 Deletes the addressbook at the given path
111  
112 e.g.
113  
114 $CardDAV->DeleteAddressBook("Shared");
115  
116 =cut
117  
118 0 ▶sub DeleteAddressBook {
Subroutine DeleteAddressBook: not called CC=2 SCAR=17.9
Pod documented
119 0 my ($Self, $Path) = @_;
120  
121 0 ▶ $Path || confess 'Delete address book path not specified';
Branch: 1 branch
BranchTrueFalseTotal
$Path or confess("Delete address book path not specified") 0 0 0
122  
123 0 $Self->Request(
124 'DELETE',
125 "$Path/"
126 );
127  
128 0 return 1;
129 }
130  
131 =head2 $self->UpdateAddressBook($Path, %Args)
132  
133 Like 'new', but for an existing addressbook. For now, can only change
134 the name.
135  
136 e.g.
137  
138 $CardDAV->UpdateAddressBook("Default", name => "My Happy Addressbook");
139  
140 =cut
141  
142 0 ▶sub UpdateAddressBook {
Subroutine UpdateAddressBook: not called CC=4 SCAR=30.0
Pod documented
143 0 my ($Self, $Path, %Args) = @_;
144  
145 0 ▶ $Path || confess 'Update address book path not specified';
Branch: 1 branch
BranchTrueFalseTotal
$Path or confess("Update address book path not specified") 0 0 0
146  
147 0 my @Params;
148  
149 0 ▶ if (defined $Args{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Args{"name"}) 0 0 0
150 0 push @Params, x('D:displayname', $Args{name});
151 }
152  
153 0 ▶ return undef unless @Params;
Branch: 1 branch
BranchTrueFalseTotal
unless @Params 0 0 0
154  
155 0 $Self->Request(
156 'PROPPATCH',
157 "$Path/",
158 x('D:propertyupdate', $Self->NS(),
159 x('D:set',
160 x('D:prop',
161 @Params,
162 ),
163 ),
164 ),
165 );
166  
167 0 return 1;
168 }
169  
170 =head2 $self->GetAddressBook($Path, %Args)
171  
172 Calls 'GetAddressBooks' with the args, and greps for the one with the
173 matching path.
174  
175 e.g.
176  
177 my $AB = $CardDAV->GetAddressBook("Default");
178  
179 =cut
180  
181 0 ▶sub GetAddressBook {
Subroutine GetAddressBook: not called CC=2 SCAR=17.9
Pod documented
182 0 my ($Self, $Id, %Args) = @_;
183  
184 0 my $Data = $Self->GetAddressBooks(%Args);
185  
186 0 ▶ die "Can't read data" unless $Data;
Branch: 1 branch
BranchTrueFalseTotal
unless $Data 0 0 0
187 0 my ($AddressBook) = grep { $_->{path} eq $Id } @$Data;
188  
189 0 return $AddressBook;
190 }
191  
192 =head2 $self->GetAddressBooks(%Args)
193  
194 Get all the addressbooks on the server.
195  
196 Returns an arrayref of hashrefs
197  
198 e.g.
199  
200 my $ABs = $CardDAV->GetAddressBooks(Sync => 1);
201 foreach my $AB (@$ABs) {
202 say "$AB->{path}: $AB->{name}";
203 }
204  
205 =cut
206  
207 1 ▶sub GetAddressBooks {
Subroutine GetAddressBooks: called CC=36 SCAR=54.0
Pod documented
208 1 my ($Self, %Args) = @_;
209  
210 1 ▶ my $props = $Args{Properties} || [];
Condition50%1/2
$Args{'Properties'} || []
l!l
01
MC/DC0%0/1
$Args{'Properties'} || []
$Args{'Properties'}
Truth table50%1/2
A
A= $Args{'Properties'}
Aresult
11
00
211  
212 1 my $Response = $Self->Request(
213 'PROPFIND',
214 '',
215 x('D:propfind', $Self->NS(),
216 x('D:prop',
217 x('D:displayname'),
218 x('D:resourcetype'),
219 x('D:current-user-privilege-set'),
220 x('D:acl'),
221 x('D:sync-token'),
222 x('D:supported-report-set'),
223 @$props,
224 ),
225 ),
226 Depth => 1,
227 );
228  
229 1 my @AddressBooks;
230  
231 1 my $NS_C = $Self->ns('C');
232 1 my $NS_D = $Self->ns('D');
233 1 my $NS_CY = $Self->ns('CY');
234 1 ▶ foreach my $Response (@{$Response->{"{$NS_D}response"} || []}) {
Condition50%1/2
$$Response{"{$NS_D}response"} || []
l!l
10
MC/DC0%0/1
$$Response{"{$NS_D}response"} || []
$$Response{"{$NS_D}response"}
Truth table50%1/2
A
A= $$Response{"{$NS_D}response"}
Aresult
11
00
235 1 ▶ my $HRef = uri_unescape($Response->{"{$NS_D}href"}{content} // '')
Condition50%2/4
uri_unescape($$Response{"{$NS_D}href"}{'content'} // '') || (next)
l!l
10
$$Response{"{$NS_D}href"}{"content"} // ""
l!l
10
MC/DC0%0/1
uri_unescape($$Response{"{$NS_D}href"}{'content'} // '') || (next)
uri_unescape($$Response{"{$NS_D}href"}{'content'} // '')
MC/DC0%0/1
$$Response{"{$NS_D}href"}{"content"} // ""
$$Response{"{$NS_D}href"}{"content"}
Truth table50%1/2
A
A= uri_unescape($$Response{"{$NS_D}href"}{'content'} // '')
Aresult
11
00
Truth table50%1/2
A
A= $$Response{"{$NS_D}href"}{"content"}
Aresult
11
00
236 || next;
237 1 my $Path = $Self->_unrequest_url($HRef);
238  
239 1 ▶ foreach my $Propstat (@{$Response->{"{$NS_D}propstat"} || []}) {
Condition50%1/2
$$Response{"{$NS_D}propstat"} || []
l!l
10
MC/DC0%0/1
$$Response{"{$NS_D}propstat"} || []
$$Response{"{$NS_D}propstat"}
Truth table50%1/2
A
A= $$Response{"{$NS_D}propstat"}
Aresult
11
00
240 1 ▶ next unless $Propstat->{"{$NS_D}prop"}{"{$NS_D}resourcetype"}{"{$NS_C}addressbook"};
Branch: 1 branch
BranchTrueFalseTotal
unless $$Propstat{"{$NS_D}prop"}{"{$NS_D}resourcetype"}{"{$NS_C}addressbook"} 0 1 1
241  
242 # XXX - this is really quite specific and probably wrong-namespaced...
243 1 my $Perms = $Propstat->{"{$NS_D}prop"}{"{$NS_D}current-user-privilege-set"}{"{$NS_D}privilege"};
244  
245 1 my $CanSync;
246 1 my $Report = $Propstat->{"{$NS_D}prop"}{"{$NS_D}supported-report-set"}{"{$NS_D}supported-report"};
247 1 ▶ $Report = [] unless ($Report and ref($Report) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless $Report and ref $Report eq "ARRAY" 1 0 1
Condition33%1/3
$Report and ref $Report eq "ARRAY"
!ll&&!rl&&r
100
MC/DC0%0/2
$Report and ref $Report eq "ARRAY"
$Report ref $Report eq "ARRAY"
Truth table33%1/3
A and B
A= $ReportB= ref $Report eq "ARRAY"
ABresult
0X0
100
111
248 1 foreach my $item (@$Report) {
249 # XXX - do we want to check the other things too?
250 0 ▶ $CanSync = 1 if $item->{"{$NS_D}report"}{"{$NS_D}sync-collection"};
Branch: 1 branch
BranchTrueFalseTotal
if $$item{"{$NS_D}report"}{"{$NS_D}sync-collection"} 0 0 0
251 }
252  
253 1 my @ShareWith;
254 1 my $ace = $Propstat->{"{$NS_D}prop"}{"{$NS_D}acl"}{"{$NS_D}ace"};
255 1 ▶ $ace = [] unless ($ace and ref($ace) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless $ace and ref $ace eq "ARRAY" 1 0 1
Condition33%1/3
$ace and ref $ace eq "ARRAY"
!ll&&!rl&&r
100
MC/DC0%0/2
$ace and ref $ace eq "ARRAY"
$ace ref $ace eq "ARRAY"
Truth table33%1/3
A and B
A= $aceB= ref $ace eq "ARRAY"
ABresult
0X0
100
111
256 1 foreach my $Acl (@$ace) {
257 0 ▶ next if $Acl->{"{$NS_D}protected"}; # ignore admin ACLs
Branch: 1 branch
BranchTrueFalseTotal
if $$Acl{"{$NS_D}protected"} 0 0 0
258 0 ▶ my $user = uri_unescape($Acl->{"{$NS_D}principal"}{"{$NS_D}href"}{content} // '');
Condition0%0/2
$$Acl{"{$NS_D}principal"}{"{$NS_D}href"}{"content"} // ""
l!l
00
MC/DC0%0/1
$$Acl{"{$NS_D}principal"}{"{$NS_D}href"}{"content"} // ""
$$Acl{"{$NS_D}principal"}{"{$NS_D}href"}{"content"}
Truth table0%0/2
A
A= $$Acl{"{$NS_D}principal"}{"{$NS_D}href"}{"content"}
Aresult
11
00
259 0 ▶ next unless $user =~ m{^/dav/principals/user/([^/]+)};
Branch: 1 branch
BranchTrueFalseTotal
unless $user =~ m[^/dav/principals/user/([^/]+)] 0 0 0
260 0 my $email = $1;
261 0 ▶ next if $email eq 'admin';
Branch: 1 branch
BranchTrueFalseTotal
if $email eq "admin" 0 0 0
262 0 my %ShareObject = (
263 email => $email,
264 mayAdmin => $JSON::false,
265 mayWrite => $JSON::false,
266 mayRead => $JSON::false,
267 );
268 0 foreach my $item (@{$Acl->{"{$NS_D}grant"}{"{$NS_D}privilege"}}) {
269 0 ▶ $ShareObject{'mayAdmin'} = $JSON::true if $item->{"{$NS_CY}admin"};
Branch: 1 branch
BranchTrueFalseTotal
if $$item{"{$NS_CY}admin"} 0 0 0
270 0 ▶ $ShareObject{'mayWrite'} = $JSON::true if $item->{"{$NS_D}write-content"};
Branch: 1 branch
BranchTrueFalseTotal
if $$item{"{$NS_D}write-content"} 0 0 0
271 0 ▶ $ShareObject{'mayRead'} = $JSON::true if $item->{"{$NS_D}read"};
Branch: 1 branch
BranchTrueFalseTotal
if $$item{"{$NS_D}read"} 0 0 0
272 }
273  
274 0 push @ShareWith, \%ShareObject;
275 }
276  
277 my %AddressBook = (
278 href => $HRef,
279 path => $Path,
280 name => ($Propstat->{"{$NS_D}prop"}{"{$NS_D}displayname"}{content} || ''),
281 0 ▶ isReadOnly => (grep { exists $_->{"{$NS_D}write-content"} } @{$Perms || []}) ? $JSON::false : $JSON::true,
Branch: 1 branch
BranchTrueFalseTotal
grep({exists $$_{"{$NS_D}read"};} @{$Perms or [];}) ? : 0 1 1
Condition50%1/2
$Perms || []
l!l
01
MC/DC0%0/1
$Perms || []
$Perms
Truth table50%1/2
A
A= $Perms
Aresult
11
00
282 0 ▶ mayRead => (grep { exists $_->{"{$NS_D}read"} } @{$Perms || []}) ? $JSON::true : $JSON::false,
Branch: 1 branch
BranchTrueFalseTotal
grep({exists $$_{"{$NS_D}write-content"};} @{$Perms or [];}) ? : 0 1 1
Condition50%1/2
$Perms || []
l!l
01
MC/DC0%0/1
$Perms || []
$Perms
Truth table50%1/2
A
A= $Perms
Aresult
11
00
283 0 ▶ mayWrite => (grep { exists $_->{"{$NS_D}write-content"} } @{$Perms || []}) ? $JSON::true : $JSON::false,
Branch: 1 branch
BranchTrueFalseTotal
grep({exists $$_{"{$NS_CY}admin"};} @{$Perms or [];}) ? : 0 1 1
Condition50%1/2
$Perms || []
l!l
01
MC/DC0%0/1
$Perms || []
$Perms
Truth table50%1/2
A
A= $Perms
Aresult
11
00
284 0 ▶ mayAdmin => (grep { exists $_->{"{$NS_CY}admin"} } @{$Perms || []}) ? $JSON::true : $JSON::false,
Branch: 2 branches
BranchTrueFalseTotal
@ShareWith ? : 0 1 1
$CanSync ? : 0 1 1
Condition50%2/4
$Perms || []
l!l
01
$$Propstat{"{$NS_D}prop"}{"{$NS_D}sync-token"}{'content'} || ''
l!l
01
MC/DC0%0/1
$Perms || []
$Perms
MC/DC0%0/1
$$Propstat{"{$NS_D}prop"}{"{$NS_D}sync-token"}{'content'} || ''
$$Propstat{"{$NS_D}prop"}{"{$NS_D}sync-token"}{'content'}
Truth table50%1/2
A
A= $Perms
Aresult
11
00
Truth table50%1/2
A
A= $$Propstat{"{$NS_D}prop"}{"{$NS_D}sync-token"}{'content'}
Aresult
11
00
285 shareWith => (@ShareWith ? \@ShareWith : $JSON::false),
286 1 ▶ syncToken => $Propstat->{"{$NS_D}prop"}{"{$NS_D}sync-token"}{content} || '',
Branch: 1 branch
BranchTrueFalseTotal
grep({exists $$_{"{$NS_D}write-content"};} @{$Perms or [];}) ? : 0 1 1
Condition50%1/2
$$Propstat{"{$NS_D}prop"}{"{$NS_D}displayname"}{'content'} || ''
l!l
10
MC/DC0%0/1
$$Propstat{"{$NS_D}prop"}{"{$NS_D}displayname"}{'content'} || ''
$$Propstat{"{$NS_D}prop"}{"{$NS_D}displayname"}{'content'}
Truth table50%1/2
A
A= $$Propstat{"{$NS_D}prop"}{"{$NS_D}displayname"}{'content'}
Aresult
11
00
287 canSync => $CanSync ? $JSON::true : $JSON::false,
288 );
289  
290 1 push @AddressBooks, \%AddressBook;
291 }
292 }
293  
294 1 return \@AddressBooks;
295 }
296  
297 # }}}
298  
299 # Contact methods {{{
300  
301 =head2 $Self->NewContact($AddressBookPath, $Card)
302  
303 Create a new contact from a JSContact Card hashref.
304 Generates a UID if not present.
305  
306 Returns the full path to the card.
307  
308 =cut
309  
310 0 ▶sub NewContact {
Subroutine NewContact: not called CC=4 SCAR=30.0
Pod documented
311 0 my ($Self, $Path, $Card) = @_;
312  
313 0 ▶ $Path || confess "New contact path not specified";
Branch: 1 branch
BranchTrueFalseTotal
$Path or confess("New contact path not specified") 0 0 0
314 0 ▶ ref($Card) eq 'HASH' || confess "Invalid contact (expected hashref)";
Branch: 1 branch
BranchTrueFalseTotal
ref $Card eq "HASH" or confess("Invalid contact (expected hashref)") 0 0 0
315  
316 0 ▶ $Card->{uid} //= 'urn:uuid:' . $Self->genuuid();
Condition0%0/2
$Card->{"uid"} //= 'urn:uuid:' . $Self->genuuid
l!l
00
MC/DC0%0/1
$Card->{"uid"} //= 'urn:uuid:' . $Self->genuuid
$Card->{"uid"}
Truth table0%0/2
A
A= $Card->{"uid"}
Aresult
11
00
317 0 my $Uid = $Card->{uid};
318 0 $Uid =~ s/^urn:uuid://;
319  
320 0 my $VCardStr = jscontact_to_vcard($Card);
321  
322 0 $Self->Request(
323 'PUT',
324 "$Path/$Uid.vcf",
325 $VCardStr,
326 'Content-Type' => 'text/vcard',
327 'If-None-Match' => '*',
328 );
329  
330 0 return "$Path/$Uid.vcf";
331 }
332  
333 =head2 $self->DeleteContact($Path)
334  
335 Delete the contact at path $Path.
336  
337 =cut
338  
339 0 ▶sub DeleteContact {
Subroutine DeleteContact: not called CC=2 SCAR=17.9
Pod documented
340 0 my ($Self, $CPath) = @_;
341  
342 0 ▶ $CPath || confess "Delete contact path not specified";
Branch: 1 branch
BranchTrueFalseTotal
$CPath or confess("Delete contact path not specified") 0 0 0
343  
344 0 $Self->Request(
345 'DELETE',
346 $CPath,
347 );
348  
349 0 return $CPath;
350 }
351  
352 =head2 $Self->UpdateContact($Path, $Card)
353  
354 Like NewContact, but updates an existing contact.
355 Takes the full path and a JSContact Card hashref.
356  
357 =cut
358  
359 0 ▶sub UpdateContact {
Subroutine UpdateContact: not called CC=3 SCAR=24.8
Pod documented
360 0 my ($Self, $CPath, $Card) = @_;
361  
362 0 ▶ $CPath || confess "Update contact path not specified";
Branch: 1 branch
BranchTrueFalseTotal
$CPath or confess("Update contact path not specified") 0 0 0
363 0 ▶ ref($Card) eq 'HASH' || confess "Invalid contact (expected hashref)";
Branch: 1 branch
BranchTrueFalseTotal
ref $Card eq "HASH" or confess("Invalid contact (expected hashref)") 0 0 0
364  
365 0 my $VCardStr = jscontact_to_vcard($Card);
366  
367 0 $Self->Request(
368 'PUT',
369 $CPath,
370 $VCardStr,
371 'Content-Type' => 'text/vcard',
372 'If-Match' => '*',
373 );
374  
375 0 return $CPath;
376 }
377  
378 =head2 $Self->GetContact($Path)
379  
380 Fetch a specific contact by path. Returns a JSContact Card hashref.
381  
382 =cut
383  
384 1 ▶sub GetContact {
Subroutine GetContact: called CC=5 SCAR=17.3
Pod documented
385 1 my ($Self, $CPath) = @_;
386  
387 1 ▶ $CPath || confess "Get contact path not specified";
Branch: 1 branch
BranchTrueFalseTotal
$CPath or confess("Get contact path not specified") 1 0 1
388  
389 1 my $Response = $Self->Request(
390 'GET',
391 $CPath,
392 );
393  
394 my $Data = $Response && $Response->{content}
395 1 ▶ // return undef;
Condition40%2/5
$Response && $Response->{"content"} // (return undef)
l!l
10
$Response and $Response->{"content"}
!ll&&!rl&&r
001
MC/DC0%0/2
$Response && $Response->{"content"} // (return undef)
$Response $Response->{"content"}
Truth table33%1/3
A and B
A= $ResponseB= $Response->{"content"}
ABresult
111
0X0
100
396  
397 1 ▶ my $Card = eval { vcard_to_jscontact($Data) }
Condition50%1/2
eval { do { vcard_to_jscontact($Data) } } // (return undef)
l!l
10
MC/DC0%0/1
eval { do { vcard_to_jscontact($Data) } } // (return undef)
eval { do { vcard_to_jscontact($Data) } }
Truth table50%1/2
A
A= eval { do { vcard_to_jscontact($Data) } }
Aresult
11
00
398 // return undef;
399  
400 1 $Card->{CPath} = $CPath;
401  
402 1 return $Card;
403 }
404  
405 =head2 $Self->GetContactAndProps($Path, $Props)
406  
407 Use a multiget to fetch the properties in the arrayref as well
408 as the card content.
409  
410 Returns the card in scalar context - the card and an array of errors
411 in list context.
412  
413 =cut
414  
415 1 ▶sub GetContactAndProps {
Subroutine GetContactAndProps: called CC=10 SCAR=27.0
Pod documented
416 1 my ($Self, $CPath, $Props) = @_;
417 1 ▶ $Props //= [];
Condition50%1/2
$Props //= []
l!l
01
MC/DC0%0/1
$Props //= []
$Props
Truth table50%1/2
A
A= $Props
Aresult
11
00
418  
419 1 ▶ $CPath || confess "Get contact path not specified";
Branch: 1 branch
BranchTrueFalseTotal
$CPath or confess("Get contact path not specified") 1 0 1
420  
421 my $Response = $Self->Request(
422 'REPORT',
423 $CPath,
424 x('C:addressbook-multiget', $Self->NS(),
425 x('D:prop',
426 x('D:getetag'),
427 x('D:getcontenttype'),
428 x('C:address-data'),
429 0 map { x(join ":", @$_) } @$Props,
430 ),
431 x('D:href', $Self->request_href($CPath)),
432 ),
433 Depth => '0',
434 );
435  
436 1 my ($Contact, @Errors);
437  
438 1 my $NS_C = $Self->ns('C');
439 1 my $NS_D = $Self->ns('D');
440 1 ▶ foreach my $Response (@{$Response->{"{$NS_D}response"} || []}) {
Condition50%1/2
$$Response{"{$NS_D}response"} || []
l!l
10
MC/DC0%0/1
$$Response{"{$NS_D}response"} || []
$$Response{"{$NS_D}response"}
Truth table50%1/2
A
A= $$Response{"{$NS_D}response"}
Aresult
11
00
441 1 ▶ foreach my $Propstat (@{$Response->{"{$NS_D}propstat"} || []}) {
Condition50%1/2
$$Response{"{$NS_D}propstat"} || []
l!l
10
MC/DC0%0/1
$$Response{"{$NS_D}propstat"} || []
$$Response{"{$NS_D}propstat"}
Truth table50%1/2
A
A= $$Response{"{$NS_D}propstat"}
Aresult
11
00
442 1 ▶ my $VCard = eval { $Self->_ParseReportData($Response, $Propstat, $Props) } || do {
Condition33%1/3
eval { do { $Self->_ParseReportData($Response, $Propstat, $Props) } } || do { push @Errors, $@ if $@; next }
l!l&&r!l&&!r
100
MC/DC0%0/2
eval { do { $Self->_ParseReportData($Response, $Propstat, $Props) } } || do { push @Errors, $@ if $@; next }
eval { do { $Self->_ParseReportData($Response, $Propstat, $Props) } } do { push @Errors, $@ if $@; next }
Truth table33%1/3
A || B
A= eval { do { $Self->_ParseReportData($Response, $Propstat, $Props) } }B= do { push @Errors, $@ if $@; next }
ABresult
1X1
011
000
443 0 ▶ push @Errors, $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
444 0 next;
445 };
446  
447 1 $Contact = $VCard;
448 }
449 }
450  
451 1 ▶ return wantarray ? ($Contact, \@Errors) : $Contact;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
452 }
453  
454 =head2 $self->GetContacts($Path, $Props, %Args)
455  
456 Get multiple cards, possibly including props, using both a propfind
457 AND a multiget.
458  
459 Returns an arrayref of contact and an arrayref of errors (or just the
460 contacts in scalar context again)
461  
462 =cut
463  
464 1 ▶sub GetContacts {
Subroutine GetContacts: called CC=2 SCAR=7.0
Pod documented
465 1 my ($Self, $Path, $Props) = @_;
466  
467 1 my ($data, $LinkErrors) = $Self->GetContactLinks($Path);
468 1 my @AllUrls = sort keys %$data;
469  
470 1 my ($Contacts, $Errors, $HRefs) = $Self->GetContactsMulti($Path, \@AllUrls, $Props);
471 1 unshift @$Errors, @$LinkErrors;
472  
473 1 ▶ return wantarray ? ($Contacts, $Errors, $HRefs) : $Contacts;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
474 }
475  
476 =head2 $self->GetContactLinks($Path)
477  
478 Returns a hash of href => etag for every contact URL (type: text/(x-)?vcard)
479 inside the collection at \$Path. Collections (including \$Path itself)
480 are skipped.
481  
482 In list context, also returns an arrayref of errors for any propstat
483 responses with an unexpected status.
484  
485 my ($Links, $Errors) = $CardDAV->GetContactLinks($Path);
486  
487 =cut
488  
489 6 ▶sub GetContactLinks {
Subroutine GetContactLinks: called CC=18 SCAR=29.3
Pod documented
490 6 my ($Self, $Path) = @_;
491  
492 6 my $Response = $Self->Request(
493 'PROPFIND',
494 "$Path/",
495 x('D:propfind', $Self->NS(),
496 x('D:prop',
497 x('D:getcontenttype'),
498 x('D:getetag'),
499 x('D:resourcetype'),
500 ),
501 ),
502 Depth => '1',
503 );
504  
505 6 my (%response, @Errors);
506 6 my $NS_C = $Self->ns('C');
507 6 my $NS_D = $Self->ns('D');
508 6 ▶ foreach my $Response (@{$Response->{"{$NS_D}response"} || []}) {
Condition50%1/2
$$Response{"{$NS_D}response"} || []
l!l
60
MC/DC0%0/1
$$Response{"{$NS_D}response"} || []
$$Response{"{$NS_D}response"}
Truth table50%1/2
A
A= $$Response{"{$NS_D}response"}
Aresult
11
00
509 31 ▶ my $href = uri_unescape($Response->{"{$NS_D}href"}{content} // '');
Condition50%1/2
$$Response{"{$NS_D}href"}{"content"} // ""
l!l
310
MC/DC0%0/1
$$Response{"{$NS_D}href"}{"content"} // ""
$$Response{"{$NS_D}href"}{"content"}
Truth table50%1/2
A
A= $$Response{"{$NS_D}href"}{"content"}
Aresult
11
00
510 31 ▶ next unless $href;
Branch: 1 branch
BranchTrueFalseTotal
unless $href 0 31 31
511  
512 # Gather all the properties the server found, which may be split
513 # across several propstat elements
514 31 my %Props;
515 31 ▶ foreach my $Propstat (@{$Response->{"{$NS_D}propstat"} || []}) {
Condition50%1/2
$$Response{"{$NS_D}propstat"} || []
l!l
310
MC/DC0%0/1
$$Response{"{$NS_D}propstat"} || []
$$Response{"{$NS_D}propstat"}
Truth table50%1/2
A
A= $$Response{"{$NS_D}propstat"}
Aresult
11
00
516 40 ▶ my $Status = $Propstat->{"{$NS_D}status"}{content} // '';
Condition100%2/2
$$Propstat{"{$NS_D}status"}{"content"} // ""
l!l
373
MC/DC100%1/1
$$Propstat{"{$NS_D}status"}{"content"} // ""
$$Propstat{"{$NS_D}status"}{"content"}
Truth table100%2/2
A
A= $$Propstat{"{$NS_D}status"}{"content"}
Aresult
11
00
517  
518 40 ▶ if ($Status =~ m/ 200 /) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Status =~ / 200 /) { } 28 12 40
elsif ($Status =~ / 404 /) { } 6 6 12
519 28 ▶ %Props = (%Props, %{$Propstat->{"{$NS_D}prop"} || {}});
Condition50%1/2
$$Propstat{"{$NS_D}prop"} || {}
l!l
280
MC/DC0%0/1
$$Propstat{"{$NS_D}prop"} || {}
$$Propstat{"{$NS_D}prop"}
Truth table50%1/2
A
A= $$Propstat{"{$NS_D}prop"}
Aresult
11
00
520 }
521 elsif ($Status =~ m/ 404 /) {
522 # Missing properties return 404 status response, ignore
523 }
524 else {
525 6 push @Errors, "Odd status in propstat response $href: $Status";
526 }
527 }
528 31 ▶ next unless %Props;
Branch: 1 branch
BranchTrueFalseTotal
unless %Props 6 25 31
529  
530 25 ▶ next if $Props{"{$NS_D}resourcetype"}{"{$NS_D}collection"};
Branch: 1 branch
BranchTrueFalseTotal
if $Props{"{$NS_D}resourcetype"}{"{$NS_D}collection"} 7 18 25
531  
532 18 ▶ if ($Props{"{$NS_D}getcontenttype"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Props{"{$NS_D}getcontenttype"}) 13 5 18
533 13 ▶ my $type = $Props{"{$NS_D}getcontenttype"}{content} || '';
Condition50%1/2
$Props{"{$NS_D}getcontenttype"}{'content'} || ''
l!l
130
MC/DC0%0/1
$Props{"{$NS_D}getcontenttype"}{'content'} || ''
$Props{"{$NS_D}getcontenttype"}{'content'}
Truth table50%1/2
A
A= $Props{"{$NS_D}getcontenttype"}{'content'}
Aresult
11
00
534 13 ▶ next unless $type =~ m{text/(x-)?vcard};
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ m[text/(x-)?vcard] 3 10 13
535 }
536  
537 15 ▶ $response{$href} = $Props{"{$NS_D}getetag"}{content} || '';
Condition50%1/2
$Props{"{$NS_D}getetag"}{'content'} || ''
l!l
150
MC/DC0%0/1
$Props{"{$NS_D}getetag"}{'content'} || ''
$Props{"{$NS_D}getetag"}{'content'}
Truth table50%1/2
A
A= $Props{"{$NS_D}getetag"}{'content'}
Aresult
11
00
538 }
539  
540 6 ▶ return wantarray ? (\%response, \@Errors) : \%response;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 3 6
541 }
542  
543 =head2 $self->GetContactsMulti($Path, $Urls, $Props)
544  
545 Does an addressbook-multiget on the \$Path for all the URLs in \$Urls
546 also fetching \$Props on top of the address-data and getetag.
547  
548 =cut
549  
550 2 ▶sub GetContactsMulti {
Subroutine GetContactsMulti: called CC=13 SCAR=29.1
Pod documented
551 2 my ($Self, $Path, $Urls, $Props) = @_;
552 2 ▶ $Props //= [];
Condition50%1/2
$Props //= []
l!l
02
MC/DC0%0/1
$Props //= []
$Props
Truth table50%1/2
A
A= $Props
Aresult
11
00
553 2 my (@Contacts, @Errors, %Links);
554  
555 2 ▶ while (my @urls = splice(@$Urls, 0, $BATCHSIZE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@urls) = splice(@$Urls, 0, $Net::CardDAVTalk::BATCHSIZE)) 2 2 4
556 my $Response = $Self->Request(
557 'REPORT',
558 "$Path/",
559 x('C:addressbook-multiget', $Self->NS(),
560 x('D:prop',
561 x('D:getetag'),
562 x('C:address-data'),
563 0 map { x(join ":", @$_) } @$Props,
564 ),
565 2 map { x('D:href', $Self->request_href($_)) } @urls,
566 ),
567 Depth => '0',
568 );
569  
570 2 my $NS_C = $Self->ns('C');
571 2 my $NS_D = $Self->ns('D');
572 2 ▶ foreach my $Response (@{$Response->{"{$NS_D}response"} || []}) {
Condition50%1/2
$$Response{"{$NS_D}response"} || []
l!l
20
MC/DC0%0/1
$$Response{"{$NS_D}response"} || []
$$Response{"{$NS_D}response"}
Truth table50%1/2
A
A= $$Response{"{$NS_D}response"}
Aresult
11
00
573 5 ▶ my $href = uri_unescape($Response->{"{$NS_D}href"}{content} // '');
Condition50%1/2
$$Response{"{$NS_D}href"}{"content"} // ""
l!l
50
MC/DC0%0/1
$$Response{"{$NS_D}href"}{"content"} // ""
$$Response{"{$NS_D}href"}{"content"}
Truth table50%1/2
A
A= $$Response{"{$NS_D}href"}{"content"}
Aresult
11
00
574 5 ▶ next unless $href;
Branch: 1 branch
BranchTrueFalseTotal
unless $href 0 5 5
575 5 ▶ foreach my $Propstat (@{$Response->{"{$NS_D}propstat"} || []}) {
Condition50%1/2
$$Response{"{$NS_D}propstat"} || []
l!l
50
MC/DC0%0/1
$$Response{"{$NS_D}propstat"} || []
$$Response{"{$NS_D}propstat"}
Truth table50%1/2
A
A= $$Response{"{$NS_D}propstat"}
Aresult
11
00
576 5 ▶ my $etag = $Propstat->{"{$NS_D}prop"}{"{$NS_D}getetag"}{content} || '';
Condition50%1/2
$$Propstat{"{$NS_D}prop"}{"{$NS_D}getetag"}{'content'} || ''
l!l
50
MC/DC0%0/1
$$Propstat{"{$NS_D}prop"}{"{$NS_D}getetag"}{'content'} || ''
$$Propstat{"{$NS_D}prop"}{"{$NS_D}getetag"}{'content'}
Truth table50%1/2
A
A= $$Propstat{"{$NS_D}prop"}{"{$NS_D}getetag"}{'content'}
Aresult
11
00
577 5 ▶ my $VCard = eval { $Self->_ParseReportData($Response, $Propstat, $Props) } || do {
Condition33%1/3
eval { do { $Self->_ParseReportData($Response, $Propstat, $Props) } } || do { push @Errors, $@ if $@; next }
l!l&&r!l&&!r
500
MC/DC0%0/2
eval { do { $Self->_ParseReportData($Response, $Propstat, $Props) } } || do { push @Errors, $@ if $@; next }
eval { do { $Self->_ParseReportData($Response, $Propstat, $Props) } } do { push @Errors, $@ if $@; next }
Truth table33%1/3
A || B
A= eval { do { $Self->_ParseReportData($Response, $Propstat, $Props) } }B= do { push @Errors, $@ if $@; next }
ABresult
1X1
011
000
578 0 ▶ push @Errors, $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
579 0 next;
580 };
581  
582 5 push @Contacts, $VCard;
583  
584 5 $Links{$href} = $etag;
585 }
586 }
587 }
588  
589 2 ▶ return wantarray ? (\@Contacts, \@Errors, \%Links) : \@Contacts;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
590 }
591  
592 =head2 $self->SyncContacts($Path, $Props, %Args)
593  
594 uses the argument 'syncToken' to find newly added and removed
595 cards from the server. Returns just the added/changed contacts
596 in scalar context, or a list of array of contacts, array of
597 removed, array of errors and the new syncToken as 4 items in
598 list context.
599  
600 =cut
601  
602 0 ▶sub SyncContacts {
Subroutine SyncContacts: not called CC=2 SCAR=17.9
Pod documented
603 0 my ($Self, $Path, $Props, %Args) = @_;
604  
605 0 my ($Added, $Removed, $Errors, $SyncToken) = $Self->SyncContactLinks($Path, %Args);
606  
607 0 my @AllUrls = sort keys %$Added;
608  
609 0 my ($Contacts, $ThisErrors, $Links) = $Self->GetContactsMulti($Path, \@AllUrls, $Props);
610 0 push @$Errors, @$ThisErrors;
611  
612 0 ▶ return wantarray ? ($Contacts, $Removed, $Errors, $SyncToken, $Links) : $Contacts;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
613 }
614  
615 =head2 $self->SyncContactLinks($Path, %Args)
616  
617 uses the argument 'syncToken' to find newly added and removed
618 cards from the server (without one, every card is reported as added).
619  
620 Returns a list of:
621  
622 * Hash of href to etag for added/changed cards
623 * List of href of removed cards
624 * List of errors
625 * Scalar value of new syncToken
626  
627 and in scalar context just the hash. See Net::DAVTalk's SyncCollection,
628 which follows a server's truncated responses (RFC 6578 section 3.6), so
629 one call gets every change, and dies if the server gives no sync-token.
630  
631 =cut
632  
633 2 ▶sub SyncContactLinks {
Subroutine SyncContactLinks: called CC=2 SCAR=7.1
Pod documented
634 2 my ($Self, $Path, %Args) = @_;
635  
636 2 ▶ $Path || confess "Sync contacts path required";
Branch: 1 branch
BranchTrueFalseTotal
$Path or confess("Sync contacts path required") 2 0 2
637  
638 2 return $Self->SyncCollection($Path, %Args);
639 }
640  
641 =head2 $self->MoveContact($Path, $NewPath)
642  
643 Move a contact to a new path (usually in a new addressbook) - both
644 paths are card paths.
645  
646 =cut
647  
648 0 ▶sub MoveContact {
Subroutine MoveContact: not called CC=3 SCAR=24.8
Pod documented
649 0 my ($Self, $CPath, $NewPath) = @_;
650  
651 0 ▶ $CPath || confess "Move contact path not specified";
Branch: 1 branch
BranchTrueFalseTotal
$CPath or confess("Move contact path not specified") 0 0 0
652 0 ▶ $NewPath || confess "Move contact destination path not specified";
Branch: 1 branch
BranchTrueFalseTotal
$NewPath or confess("Move contact destination path not specified") 0 0 0
653  
654 0 $Self->Request(
655 'MOVE',
656 $CPath,
657 undef,
658 'Destination' => $Self->request_url($NewPath),
659 );
660  
661 0 return $NewPath;
662 }
663  
664 # }}}
665  
666 6 ▶sub _ParseReportData {
Subroutine _ParseReportData: called CC=7 SCAR=21.6
667 6 my ($Self, $Response, $Propstat, $Props) = @_;
668  
669 6 my $NS_C = $Self->ns('C');
670 6 my $NS_D = $Self->ns('D');
671  
672 my $HRef = $Response->{"{$NS_D}href"}{content}
673 6 ▶ // return;
Condition50%1/2
$$Response{"{$NS_D}href"}{"content"} // (return)
l!l
60
MC/DC0%0/1
$$Response{"{$NS_D}href"}{"content"} // (return)
$$Response{"{$NS_D}href"}{"content"}
Truth table50%1/2
A
A= $$Response{"{$NS_D}href"}{"content"}
Aresult
11
00
674 6 $HRef = uri_unescape($HRef);
675 6 my $CPath = $Self->_unrequest_url($HRef);
676  
677 my $Data = $Propstat->{"{$NS_D}prop"}{"{$NS_C}address-data"}{content}
678 6 ▶ // return;
Condition50%1/2
$$Propstat{"{$NS_D}prop"}{"{$NS_C}address-data"}{"content"} // (return)
l!l
60
MC/DC0%0/1
$$Propstat{"{$NS_D}prop"}{"{$NS_C}address-data"}{"content"} // (return)
$$Propstat{"{$NS_D}prop"}{"{$NS_C}address-data"}{"content"}
Truth table50%1/2
A
A= $$Propstat{"{$NS_D}prop"}{"{$NS_C}address-data"}{"content"}
Aresult
11
00
679  
680 6 my $Card = eval { vcard_to_jscontact($Data) };
681 6 ▶ return unless $Card;
Branch: 1 branch
BranchTrueFalseTotal
unless $Card 0 6 6
682  
683 6 $Card->{CPath} = $CPath;
684 6 $Card->{href} = $HRef;
685 6 $Card->{_raw} = $Data;
686  
687 6 my %ExtraProps;
688 6 for (@$Props) {
689 0 my ($NS, $PropName) = @$_;
690 0 my $NS_P = $Self->ns($NS);
691 my $PropValue = $Propstat->{"{$NS_D}prop"}{"{$NS_P}$PropName"}{content}
692 0 ▶ // next;
Condition0%0/2
$$Propstat{"{$NS_D}prop"}{"{$NS_P}$PropName"}{"content"} // (next)
l!l
00
MC/DC0%0/1
$$Propstat{"{$NS_D}prop"}{"{$NS_P}$PropName"}{"content"} // (next)
$$Propstat{"{$NS_D}prop"}{"{$NS_P}$PropName"}{"content"}
Truth table0%0/2
A
A= $$Propstat{"{$NS_D}prop"}{"{$NS_P}$PropName"}{"content"}
Aresult
11
00
693 0 $ExtraProps{"${NS}:${PropName}"} = $PropValue;
694 }
695  
696 6 ▶ $Card->{meta} = \%ExtraProps if %ExtraProps;
Branch: 1 branch
BranchTrueFalseTotal
if %ExtraProps 0 6 6
697  
698 6 return $Card;
699 }
700  
701 7 ▶sub _unrequest_url {
Subroutine _unrequest_url: called CC=2 SCAR=7.1
702 7 my $Self = shift;
703 7 my $Path = shift;
704  
705 7 ▶ if ($Path =~ m{^/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Path =~ m[^/]) { } 7 0 7
706 7 $Path =~ s#^\Q$Self->{basepath}\E/?##;
707 } else {
708 0 $Path =~ s#^\Q$Self->{url}\E/?##;
709 }
710 7 $Path =~ s#/$##;
711  
712 7 return $Path;
713 }
714  
715 =head1 AUTHOR
716  
717 Bron Gondwana, C<< <brong at cpan.org> >>
718  
719 =head1 BUGS
720  
721 Please report any bugs or feature requests to C<bug-net-carddavtalk at rt.cpan.org>, or through
722 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-CardDAVTalk>. I will be notified, and then you'll
723 automatically be notified of progress on your bug as I make changes.
724  
725  
726  
727  
728 =head1 SUPPORT
729  
730 You can find documentation for this module with the perldoc command.
731  
732 perldoc Net::CardDAVTalk
733  
734  
735 You can also look for information at:
736  
737 =over 4
738  
739 =item * RT: CPAN's request tracker (report bugs here)
740  
741 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-CardDAVTalk>
742  
743 =item * AnnoCPAN: Annotated CPAN documentation
744  
745 L<http://annocpan.org/dist/Net-CardDAVTalk>
746  
747 =item * CPAN Ratings
748  
749 L<http://cpanratings.perl.org/d/Net-CardDAVTalk>
750  
751 =item * Search CPAN
752  
753 L<http://search.cpan.org/dist/Net-CardDAVTalk/>
754  
755 =back
756  
757  
758 =head1 ACKNOWLEDGEMENTS
759  
760  
761 =head1 LICENSE AND COPYRIGHT
762  
763 Copyright 2015-2026 Fastmail Pty Ltd.
764  
765 This program is free software; you can redistribute it and/or modify it
766 under the same terms as Perl itself; that is, either the GNU General
767 Public License as published by the Free Software Foundation (version 1,
768 or at your option any later version), or the Artistic License.
769  
770 See L<http://dev.perl.org/licenses/> and the LICENSE file included with
771 this distribution for more information.
772  
773 =cut
774  
775 2 1; # End of Net::CardDAVTalk
↑ Summary