blib/lib/Net/CardDAVTalk.pm

filter: Statementstmt 12.2% 32 / 261 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 75 MC/DCmcdc 0.0% 0 / 39 Subroutinesub 34.4% 10 / 29 Podpod 100% 17 / 17 totaltotal 11.2% 59 / 523 CC 100 SCAR 59.8
CC
100
Cov
7%
GetAddressBooks
1260.0
GetContactsMulti
156.0
SyncContactLinks
156.0
CRAP 393.7
↑ Summary
1 package Net::CardDAVTalk;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 1 ▶use Net::DAVTalk;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use base qw(Net::DAVTalk);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Text::JSContact qw(vcard_to_jscontact jscontact_to_vcard);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use XML::Spice;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use URI::Escape qw(uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶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.11
25  
26 =cut
27  
28 1 our $VERSION = '0.12';
29  
30 1 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 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
56 0 my ($Class, %Params) = @_;
57  
58 0 $Params{homesetns} = 'C';
59 0 $Params{homeset} = 'addressbook-home-set';
60 0 $Params{wellknown} = 'carddav';
61  
62 0 my $Self = $Class->SUPER::new(%Params);
63  
64 0 $Self->ns(C => 'urn:ietf:params:xml:ns:carddav');
65 0 $Self->ns(CY => 'http://cyrusimap.org/ns/');
66  
67 0 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 0 ▶sub GetAddressBooks {
Subroutine GetAddressBooks: not called CC=35 SCAR=71.4
Pod documented
208 0 my ($Self, %Args) = @_;
209  
210 0 ▶ my $props = $Args{Properties} || [];
Condition0%0/2
$Args{'Properties'} || []
l!l
00
MC/DC0%0/1
$Args{'Properties'} || []
$Args{'Properties'}
Truth table0%0/2
A
A= $Args{'Properties'}
Aresult
11
00
211  
212 0 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 0 my @AddressBooks;
230  
231 0 my $NS_C = $Self->ns('C');
232 0 my $NS_D = $Self->ns('D');
233 0 my $NS_CY = $Self->ns('CY');
234 0 ▶ foreach my $Response (@{$Response->{"{$NS_D}response"} || []}) {
Condition0%0/2
$$Response{"{$NS_D}response"} || []
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}response"} || []
$$Response{"{$NS_D}response"}
Truth table0%0/2
A
A= $$Response{"{$NS_D}response"}
Aresult
11
00
235 my $HRef = $Response->{"{$NS_D}href"}{content}
236 0 ▶ || next;
Condition0%0/2
$$Response{"{$NS_D}href"}{'content'} || (next)
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}href"}{'content'} || (next)
$$Response{"{$NS_D}href"}{'content'}
Truth table0%0/2
A
A= $$Response{"{$NS_D}href"}{'content'}
Aresult
11
00
237 0 my $Path = $Self->_unrequest_url($HRef);
238  
239 0 ▶ foreach my $Propstat (@{$Response->{"{$NS_D}propstat"} || []}) {
Condition0%0/2
$$Response{"{$NS_D}propstat"} || []
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}propstat"} || []
$$Response{"{$NS_D}propstat"}
Truth table0%0/2
A
A= $$Response{"{$NS_D}propstat"}
Aresult
11
00
240 0 ▶ 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 0 0
241  
242 # XXX - this is really quite specific and probably wrong-namespaced...
243 0 my $Perms = $Propstat->{"{$NS_D}prop"}{"{$NS_D}current-user-privilege-set"}{"{$NS_D}privilege"};
244  
245 0 my $CanSync;
246 0 my $Report = $Propstat->{"{$NS_D}prop"}{"{$NS_D}supported-report-set"}{"{$NS_D}supported-report"};
247 0 ▶ $Report = [] unless ($Report and ref($Report) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless $Report and ref $Report eq "ARRAY" 0 0 0
Condition0%0/3
$Report and ref $Report eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$Report and ref $Report eq "ARRAY"
$Report ref $Report eq "ARRAY"
Truth table0%0/3
A and B
A= $ReportB= ref $Report eq "ARRAY"
ABresult
0X0
100
111
248 0 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 0 my @ShareWith;
254 0 my $ace = $Propstat->{"{$NS_D}prop"}{"{$NS_D}acl"}{"{$NS_D}ace"};
255 0 ▶ $ace = [] unless ($ace and ref($ace) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless $ace and ref $ace eq "ARRAY" 0 0 0
Condition0%0/3
$ace and ref $ace eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$ace and ref $ace eq "ARRAY"
$ace ref $ace eq "ARRAY"
Truth table0%0/3
A and B
A= $aceB= ref $ace eq "ARRAY"
ABresult
0X0
100
111
256 0 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 0 0
Condition0%0/2
$Perms || []
l!l
00
MC/DC0%0/1
$Perms || []
$Perms
Truth table0%0/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 0 0
Condition0%0/2
$Perms || []
l!l
00
MC/DC0%0/1
$Perms || []
$Perms
Truth table0%0/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 0 0
Condition0%0/2
$Perms || []
l!l
00
MC/DC0%0/1
$Perms || []
$Perms
Truth table0%0/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 0 0
$CanSync ? : 0 0 0
Condition0%0/4
$Perms || []
l!l
00
$$Propstat{"{$NS_D}prop"}{"{$NS_D}sync-token"}{'content'} || ''
l!l
00
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 table0%0/2
A
A= $Perms
Aresult
11
00
Truth table0%0/2
A
A= $$Propstat{"{$NS_D}prop"}{"{$NS_D}sync-token"}{'content'}
Aresult
11
00
285 shareWith => (@ShareWith ? \@ShareWith : $JSON::false),
286 0 ▶ syncToken => $Propstat->{"{$NS_D}prop"}{"{$NS_D}sync-token"}{content} || '',
Branch: 1 branch
BranchTrueFalseTotal
grep({exists $$_{"{$NS_D}write-content"};} @{$Perms or [];}) ? : 0 0 0
Condition0%0/2
$$Propstat{"{$NS_D}prop"}{"{$NS_D}displayname"}{'content'} || ''
l!l
00
MC/DC0%0/1
$$Propstat{"{$NS_D}prop"}{"{$NS_D}displayname"}{'content'} || ''
$$Propstat{"{$NS_D}prop"}{"{$NS_D}displayname"}{'content'}
Truth table0%0/2
A
A= $$Propstat{"{$NS_D}prop"}{"{$NS_D}displayname"}{'content'}
Aresult
11
00
287 canSync => $CanSync ? $JSON::true : $JSON::false,
288 );
289  
290 0 push @AddressBooks, \%AddressBook;
291 }
292 }
293  
294 0 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 0 ▶sub GetContact {
Subroutine GetContact: not called CC=5 SCAR=34.0
Pod documented
385 0 my ($Self, $CPath) = @_;
386  
387 0 ▶ $CPath || confess "Get contact path not specified";
Branch: 1 branch
BranchTrueFalseTotal
$CPath or confess("Get contact path not specified") 0 0 0
388  
389 0 my $Response = $Self->Request(
390 'GET',
391 $CPath,
392 );
393  
394 my $Data = $Response && $Response->{content}
395 0 ▶ // return undef;
Condition0%0/5
$Response && $Response->{"content"} // (return undef)
l!l
00
$Response and $Response->{"content"}
!ll&&!rl&&r
000
MC/DC0%0/2
$Response && $Response->{"content"} // (return undef)
$Response $Response->{"content"}
Truth table0%0/3
A and B
A= $ResponseB= $Response->{"content"}
ABresult
111
0X0
100
396  
397 0 ▶ my $Card = eval { vcard_to_jscontact($Data) }
Condition0%0/2
eval { // (return undef)
l!l
00
MC/DC0%0/1
eval { do { vcard_to_jscontact($Data) } } // (return undef)
eval { do { vcard_to_jscontact($Data) } }
Truth table0%0/2
A
A= eval { do { vcard_to_jscontact($Data) } }
Aresult
11
00
398 // return undef;
399  
400 0 $Card->{CPath} = $CPath;
401  
402 0 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 0 ▶sub GetContactAndProps {
Subroutine GetContactAndProps: not called CC=10 SCAR=47.0
Pod documented
416 0 my ($Self, $CPath, $Props) = @_;
417 0 ▶ $Props //= [];
Condition0%0/2
$Props //= []
l!l
00
MC/DC0%0/1
$Props //= []
$Props
Truth table0%0/2
A
A= $Props
Aresult
11
00
418  
419 0 ▶ $CPath || confess "Get contact path not specified";
Branch: 1 branch
BranchTrueFalseTotal
$CPath or confess("Get contact path not specified") 0 0 0
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', $CPath),
432 ),
433 Depth => '0',
434 );
435  
436 0 my ($Contact, @Errors);
437  
438 0 my $NS_C = $Self->ns('C');
439 0 my $NS_D = $Self->ns('D');
440 0 ▶ foreach my $Response (@{$Response->{"{$NS_D}response"} || []}) {
Condition0%0/2
$$Response{"{$NS_D}response"} || []
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}response"} || []
$$Response{"{$NS_D}response"}
Truth table0%0/2
A
A= $$Response{"{$NS_D}response"}
Aresult
11
00
441 0 ▶ foreach my $Propstat (@{$Response->{"{$NS_D}propstat"} || []}) {
Condition0%0/2
$$Response{"{$NS_D}propstat"} || []
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}propstat"} || []
$$Response{"{$NS_D}propstat"}
Truth table0%0/2
A
A= $$Response{"{$NS_D}propstat"}
Aresult
11
00
442 0 ▶ my $VCard = eval { $Self->_ParseReportData($Response, $Propstat, $Props) } || do {
Condition0%0/3
eval { || do {
l!l&&r!l&&!r
000
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 table0%0/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 0 $Contact = $VCard;
448 }
449 }
450  
451 0 ▶ return wantarray ? ($Contact, \@Errors) : $Contact;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
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 0 ▶sub GetContacts {
Subroutine GetContacts: not called CC=2 SCAR=17.9
Pod documented
465 0 my ($Self, $Path, $Props) = @_;
466  
467 0 my $data = $Self->GetContactLinks($Path);
468 0 my @AllUrls = sort keys %$data;
469  
470 0 my ($Contacts, $Errors, $HRefs) = $Self->GetContactsMulti($Path, \@AllUrls, $Props);
471  
472 0 ▶ return wantarray ? ($Contacts, $Errors, $HRefs) : $Contacts;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
473 }
474  
475 =head2 $self->GetContactLinks($Path)
476  
477 Returns a hash of href => etag for every contact URL (type: text/(x-)?vcard)
478 inside the collection at \$Path.
479  
480 =cut
481  
482 0 ▶sub GetContactLinks {
Subroutine GetContactLinks: not called CC=8 SCAR=42.8
Pod documented
483 0 my ($Self, $Path) = @_;
484  
485 0 my $Response = $Self->Request(
486 'PROPFIND',
487 "$Path/",
488 x('D:propfind', $Self->NS(),
489 x('D:prop',
490 x('D:getcontenttype'),
491 x('D:getetag'),
492 ),
493 ),
494 Depth => '1',
495 );
496  
497 0 my %response;
498 0 my $NS_C = $Self->ns('C');
499 0 my $NS_D = $Self->ns('D');
500 0 ▶ foreach my $Response (@{$Response->{"{$NS_D}response"} || []}) {
Condition0%0/2
$$Response{"{$NS_D}response"} || []
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}response"} || []
$$Response{"{$NS_D}response"}
Truth table0%0/2
A
A= $$Response{"{$NS_D}response"}
Aresult
11
00
501 0 my $href = $Response->{"{$NS_D}href"}{content};
502 0 ▶ next unless $href;
Branch: 1 branch
BranchTrueFalseTotal
unless $href 0 0 0
503 0 ▶ if ($Response->{"{$NS_D}prop"}{"{$NS_D}getcontenttype"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$Response{"{$NS_D}prop"}{"{$NS_D}getcontenttype"}) 0 0 0
504 0 ▶ my $type = $Response->{"{$NS_D}prop"}{"{$NS_D}getcontenttype"}{content} || '';
Condition0%0/2
$$Response{"{$NS_D}prop"}{"{$NS_D}getcontenttype"}{'content'} || ''
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}prop"}{"{$NS_D}getcontenttype"}{'content'} || ''
$$Response{"{$NS_D}prop"}{"{$NS_D}getcontenttype"}{'content'}
Truth table0%0/2
A
A= $$Response{"{$NS_D}prop"}{"{$NS_D}getcontenttype"}{'content'}
Aresult
11
00
505 0 ▶ next unless $type =~ m{text/(x-)?vcard};
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ m[text/(x-)?vcard] 0 0 0
506 }
507 0 ▶ my $etag = $Response->{"{$NS_D}prop"}{"{$NS_D}getetag"}{content} || '';
Condition0%0/2
$$Response{"{$NS_D}prop"}{"{$NS_D}getetag"}{'content'} || ''
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}prop"}{"{$NS_D}getetag"}{'content'} || ''
$$Response{"{$NS_D}prop"}{"{$NS_D}getetag"}{'content'}
Truth table0%0/2
A
A= $$Response{"{$NS_D}prop"}{"{$NS_D}getetag"}{'content'}
Aresult
11
00
508 0 $response{$href} = $etag;
509 }
510  
511 0 return \%response;
512 }
513  
514 =head2 $self->GetContactsMulti($Path, $Urls, $Props)
515  
516 Does an addressbook-multiget on the \$Path for all the URLs in \$Urls
517 also fetching \$Props on top of the address-data and getetag.
518  
519 =cut
520  
521 0 ▶sub GetContactsMulti {
Subroutine GetContactsMulti: not called CC=12 SCAR=50.5
Pod documented
522 0 my ($Self, $Path, $Urls, $Props) = @_;
523 0 ▶ $Props //= [];
Condition0%0/2
$Props //= []
l!l
00
MC/DC0%0/1
$Props //= []
$Props
Truth table0%0/2
A
A= $Props
Aresult
11
00
524 0 my (@Contacts, @Errors, %Links);
525  
526 0 ▶ while (my @urls = splice(@$Urls, 0, $BATCHSIZE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@urls) = splice(@$Urls, 0, $Net::CardDAVTalk::BATCHSIZE)) 0 0 0
527 my $Response = $Self->Request(
528 'REPORT',
529 "$Path/",
530 x('C:addressbook-multiget', $Self->NS(),
531 x('D:prop',
532 x('D:getetag'),
533 x('C:address-data'),
534 0 map { x(join ":", @$_) } @$Props,
535 ),
536 0 map { x('D:href', $_) } @urls,
537 ),
538 Depth => '0',
539 );
540  
541 0 my $NS_C = $Self->ns('C');
542 0 my $NS_D = $Self->ns('D');
543 0 ▶ foreach my $Response (@{$Response->{"{$NS_D}response"} || []}) {
Condition0%0/2
$$Response{"{$NS_D}response"} || []
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}response"} || []
$$Response{"{$NS_D}response"}
Truth table0%0/2
A
A= $$Response{"{$NS_D}response"}
Aresult
11
00
544 0 my $href = $Response->{"{$NS_D}href"}{content};
545 0 ▶ next unless $href;
Branch: 1 branch
BranchTrueFalseTotal
unless $href 0 0 0
546 0 ▶ foreach my $Propstat (@{$Response->{"{$NS_D}propstat"} || []}) {
Condition0%0/2
$$Response{"{$NS_D}propstat"} || []
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}propstat"} || []
$$Response{"{$NS_D}propstat"}
Truth table0%0/2
A
A= $$Response{"{$NS_D}propstat"}
Aresult
11
00
547 0 ▶ my $etag = $Propstat->{"{$NS_D}prop"}{"{$NS_D}getetag"}{content} || '';
Condition0%0/2
$$Propstat{"{$NS_D}prop"}{"{$NS_D}getetag"}{'content'} || ''
l!l
00
MC/DC0%0/1
$$Propstat{"{$NS_D}prop"}{"{$NS_D}getetag"}{'content'} || ''
$$Propstat{"{$NS_D}prop"}{"{$NS_D}getetag"}{'content'}
Truth table0%0/2
A
A= $$Propstat{"{$NS_D}prop"}{"{$NS_D}getetag"}{'content'}
Aresult
11
00
548 0 ▶ my $VCard = eval { $Self->_ParseReportData($Response, $Propstat, $Props) } || do {
Condition0%0/3
eval { || do {
l!l&&r!l&&!r
000
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 table0%0/3
A || B
A= eval { do { $Self->_ParseReportData($Response, $Propstat, $Props) } }B= do { push @Errors, $@ if $@; next }
ABresult
1X1
011
000
549 0 ▶ push @Errors, $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
550 0 next;
551 };
552  
553 0 push @Contacts, $VCard;
554  
555 0 $Links{$href} = $etag;
556 }
557 }
558 }
559  
560 0 ▶ return wantarray ? (\@Contacts, \@Errors, \%Links) : \@Contacts;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
561 }
562  
563 =head2 $self->SyncContacts($Path, $Props, %Args)
564  
565 uses the argument 'syncToken' to find newly added and removed
566 cards from the server. Returns just the added/changed contacts
567 in scalar context, or a list of array of contacts, array of
568 removed, array of errors and the new syncToken as 4 items in
569 list context.
570  
571 =cut
572  
573 0 ▶sub SyncContacts {
Subroutine SyncContacts: not called CC=2 SCAR=17.9
Pod documented
574 0 my ($Self, $Path, $Props, %Args) = @_;
575  
576 0 my ($Added, $Removed, $Errors, $SyncToken) = $Self->SyncContactLinks($Path, %Args);
577  
578 0 my @AllUrls = sort keys %$Added;
579  
580 0 my ($Contacts, $ThisErrors, $Links) = $Self->GetContactsMulti($Path, \@AllUrls, $Props);
581 0 push @$Errors, @$ThisErrors;
582  
583 0 ▶ return wantarray ? ($Contacts, $Removed, $Errors, $SyncToken, $Links) : $Contacts;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
584 }
585  
586 =head2 $self->SyncContactLinks($Path, %Args)
587  
588 uses the argument 'syncToken' to find newly added and removed
589 cards from the server.
590  
591 Returns a list of:
592  
593 * Hash of href to etag for added/changed cargs
594 * List of href of removed cards
595 * List of errors
596 * Scalar value of new syncToken
597  
598 =cut
599  
600 0 ▶sub SyncContactLinks {
Subroutine SyncContactLinks: not called CC=12 SCAR=50.5
Pod documented
601 0 my ($Self, $Path, %Args) = @_;
602  
603 0 ▶ $Path || confess "Sync contacts path required";
Branch: 1 branch
BranchTrueFalseTotal
$Path or confess("Sync contacts path required") 0 0 0
604  
605 # WebDAV Collection Synchronization (RFC6578)
606 my $Response = $Self->Request(
607 'REPORT',
608 "$Path/",
609 x('D:sync-collection', $Self->NS(),
610 0 ▶ x('D:sync-token', ($Args{syncToken} ? ($Args{syncToken}) : ())),
Branch: 1 branch
BranchTrueFalseTotal
$Args{'syncToken'} ? : 0 0 0
611 x('D:sync-level', 1),
612 x('D:prop',
613 x('D:getetag'),
614 ),
615 ),
616 );
617  
618 0 my (%Added, @Removed, @Errors);
619  
620 0 my $NS_C = $Self->ns('C');
621 0 my $NS_D = $Self->ns('D');
622 0 ▶ foreach my $Response (@{$Response->{"{$NS_D}response"} || []}) {
Condition0%0/2
$$Response{"{$NS_D}response"} || []
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}response"} || []
$$Response{"{$NS_D}response"}
Truth table0%0/2
A
A= $$Response{"{$NS_D}response"}
Aresult
11
00
623 my $href = $Response->{"{$NS_D}href"}{content}
624 0 ▶ || next;
Condition0%0/2
$$Response{"{$NS_D}href"}{'content'} || (next)
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}href"}{'content'} || (next)
$$Response{"{$NS_D}href"}{'content'}
Truth table0%0/2
A
A= $$Response{"{$NS_D}href"}{'content'}
Aresult
11
00
625  
626 # For members that have been removed, the DAV:response MUST
627 # contain one DAV:status with a value set to '404 Not Found' and
628 # MUST NOT contain any DAV:propstat element
629 0 ▶ if (!$Response->{"{$NS_D}propstat"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$Response{"{$NS_D}propstat"}) 0 0 0
630 0 my $Status = $Response->{"{$NS_D}status"}{content};
631 0 ▶ if ($Status =~ m/ 404 /) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Status =~ / 404 /) { } 0 0 0
632 0 push @Removed, $href;
633 } else {
634 0 warn "ODD STATUS";
635 0 push @Errors, "Odd status in non-propstat response $href: $Status";
636 }
637 0 next;
638 }
639  
640 # For members that have changed (i.e., are new or have had their
641 # mapped resource modified), the DAV:response MUST contain at
642 # least one DAV:propstat element and MUST NOT contain any
643 # DAV:status element.
644 0 ▶ foreach my $Propstat (@{$Response->{"{$NS_D}propstat"} || []}) {
Condition0%0/2
$$Response{"{$NS_D}propstat"} || []
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}propstat"} || []
$$Response{"{$NS_D}propstat"}
Truth table0%0/2
A
A= $$Response{"{$NS_D}propstat"}
Aresult
11
00
645 0 my $Status = $Propstat->{"{$NS_D}status"}{content};
646  
647 0 ▶ if ($Status =~ m/ 200 /) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Status =~ / 200 /) { } 0 0 0
elsif ($Status =~ / 404 /) { } 0 0 0
648 0 my $etag = $Propstat->{"{$NS_D}prop"}{"{$NS_D}getetag"}{content};
649 0 $Added{$href} = $etag;
650 }
651 elsif ($Status =~ m/ 404 /) {
652 # Missing properties return 404 status response, ignore
653  
654 }
655 else {
656 0 warn "ODD STATUS";
657 0 push @Errors, "Odd status in propstat response $href: $Status";
658 }
659 }
660 }
661  
662 0 my $SyncToken = $Response->{"{$NS_D}sync-token"}{content};
663  
664 0 return (\%Added, \@Removed, \@Errors, $SyncToken);
665 }
666  
667 =head2 $self->MoveContact($Path, $NewPath)
668  
669 Move a contact to a new path (usually in a new addressbook) - both
670 paths are card paths.
671  
672 =cut
673  
674 0 ▶sub MoveContact {
Subroutine MoveContact: not called CC=3 SCAR=24.8
Pod documented
675 0 my ($Self, $CPath, $NewPath) = @_;
676  
677 0 ▶ $CPath || confess "Move contact path not specified";
Branch: 1 branch
BranchTrueFalseTotal
$CPath or confess("Move contact path not specified") 0 0 0
678 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
679  
680 0 $Self->Request(
681 'MOVE',
682 $CPath,
683 undef,
684 'Destination' => $Self->request_url($NewPath),
685 );
686  
687 0 return $NewPath;
688 }
689  
690 # }}}
691  
692 0 ▶sub _ParseReportData {
Subroutine _ParseReportData: not called CC=7 SCAR=40.3
693 0 my ($Self, $Response, $Propstat, $Props) = @_;
694  
695 0 my $NS_C = $Self->ns('C');
696 0 my $NS_D = $Self->ns('D');
697  
698 my $HRef = $Response->{"{$NS_D}href"}{content}
699 0 ▶ // return;
Condition0%0/2
$$Response{"{$NS_D}href"}{"content"} // (return)
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}href"}{"content"} // (return)
$$Response{"{$NS_D}href"}{"content"}
Truth table0%0/2
A
A= $$Response{"{$NS_D}href"}{"content"}
Aresult
11
00
700 0 my $CPath = $Self->_unrequest_url($HRef);
701  
702 my $Data = $Propstat->{"{$NS_D}prop"}{"{$NS_C}address-data"}{content}
703 0 ▶ // return;
Condition0%0/2
$$Propstat{"{$NS_D}prop"}{"{$NS_C}address-data"}{"content"} // (return)
l!l
00
MC/DC0%0/1
$$Propstat{"{$NS_D}prop"}{"{$NS_C}address-data"}{"content"} // (return)
$$Propstat{"{$NS_D}prop"}{"{$NS_C}address-data"}{"content"}
Truth table0%0/2
A
A= $$Propstat{"{$NS_D}prop"}{"{$NS_C}address-data"}{"content"}
Aresult
11
00
704  
705 0 my $Card = eval { vcard_to_jscontact($Data) };
706 0 ▶ return unless $Card;
Branch: 1 branch
BranchTrueFalseTotal
unless $Card 0 0 0
707  
708 0 $Card->{CPath} = $CPath;
709 0 $Card->{href} = $HRef;
710 0 $Card->{_raw} = $Data;
711  
712 0 my %ExtraProps;
713 0 for (@$Props) {
714 0 my ($NS, $PropName) = @$_;
715 0 my $NS_P = $Self->ns($NS);
716 my $PropValue = $Propstat->{"{$NS_D}prop"}{"{$NS_P}$PropName"}{content}
717 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
718 0 $ExtraProps{"${NS}:${PropName}"} = $PropValue;
719 }
720  
721 0 ▶ $Card->{meta} = \%ExtraProps if %ExtraProps;
Branch: 1 branch
BranchTrueFalseTotal
if %ExtraProps 0 0 0
722  
723 0 return $Card;
724 }
725  
726 0 ▶sub _unrequest_url {
Subroutine _unrequest_url: not called CC=2 SCAR=17.9
727 0 my $Self = shift;
728 0 my $Path = shift;
729  
730 0 ▶ if ($Path =~ m{^/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Path =~ m[^/]) { } 0 0 0
731 0 $Path =~ s#^\Q$Self->{basepath}\E/?##;
732 } else {
733 0 $Path =~ s#^\Q$Self->{url}\E/?##;
734 }
735 0 $Path =~ s#/$##;
736  
737 0 return $Path;
738 }
739  
740 =head1 AUTHOR
741  
742 Bron Gondwana, C<< <brong at cpan.org> >>
743  
744 =head1 BUGS
745  
746 Please report any bugs or feature requests to C<bug-net-carddavtalk at rt.cpan.org>, or through
747 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-CardDAVTalk>. I will be notified, and then you'll
748 automatically be notified of progress on your bug as I make changes.
749  
750  
751  
752  
753 =head1 SUPPORT
754  
755 You can find documentation for this module with the perldoc command.
756  
757 perldoc Net::CardDAVTalk
758  
759  
760 You can also look for information at:
761  
762 =over 4
763  
764 =item * RT: CPAN's request tracker (report bugs here)
765  
766 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-CardDAVTalk>
767  
768 =item * AnnoCPAN: Annotated CPAN documentation
769  
770 L<http://annocpan.org/dist/Net-CardDAVTalk>
771  
772 =item * CPAN Ratings
773  
774 L<http://cpanratings.perl.org/d/Net-CardDAVTalk>
775  
776 =item * Search CPAN
777  
778 L<http://search.cpan.org/dist/Net-CardDAVTalk/>
779  
780 =back
781  
782  
783 =head1 ACKNOWLEDGEMENTS
784  
785  
786 =head1 LICENSE AND COPYRIGHT
787  
788 Copyright 2015-2026 Fastmail Pty Ltd.
789  
790 This program is free software; you can redistribute it and/or modify it
791 under the same terms as Perl itself; that is, either the GNU General
792 Public License as published by the Free Software Foundation (version 1,
793 or at your option any later version), or the Artistic License.
794  
795 See L<http://dev.perl.org/licenses/> and the LICENSE file included with
796 this distribution for more information.
797  
798 =cut
799  
800 1 1; # End of Net::CardDAVTalk
↑ Summary