CalDAVTalk.pm

filter: Statementstmt 15.3% 78 / 509 Branchbran 0.0% 0 / 282 Conditioncond 1.8% 2 / 106 MC/DCmcdc 0.0% 0 / 62 Subroutinesub 39.2% 20 / 51 Podpod 92.3% 24 / 26 totaltotal 11.9% 124 / 1036 CC 207 SCAR 64.0
CC
207
Cov
9%
GetCalendars
2352.0
GetEventsMulti
600.0
_updateEvent
552.0
CRAP 604.5
↑ Summary CalDAVTalk/TimeZones.pm »
1 package Net::CalDAVTalk;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Net::DAVTalk;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use base qw(Net::DAVTalk);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use DateTime::Format::ICal;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use DateTime::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use JSON::XS qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Text::JSCalendar;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Text::VCardFast qw(vcard2hash);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use XML::Spice;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Digest::SHA qw(sha1_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use URI::Escape qw(uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 our $BATCHSIZE = 100;
22  
23  
24  
25  
26 our (
27 3 $DefaultCalendarColour,
28 $DefaultDisplayName,
29 );
30  
31 3 our $UTC = DateTime::TimeZone::UTC->new();
32 3 our $FLOATING = DateTime::TimeZone::Floating->new();
33 3 our $LOCALE = DateTime::Locale->load('en_US');
34  
35 # Beginning and End of time as used for "all event" date ranges
36 # Reducing this range may result in events disappearing from FastMail
37 # calendars, as we think they have been deleted from the other end,
38 # so best to avoid this.
39 # However, from my tests, the events should be resurrected once this date
40 # window includes them again.
41  
42 3 my $BoT = '1970-01-01T00:00:00';
43 3 my $EoT = '2038-01-19T00:00:00';
44  
45 my (
46 3 %ColourNames,
47 %UTCLinks,
48 %EventKeys,
49 );
50  
51 BEGIN {
52 3 ▶ $DefaultCalendarColour = '#0252D4';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 3 $DefaultDisplayName = 'Untitled Calendar';
54  
55 3 %EventKeys = (
56 '' => {
57 uid => [0, 'string', 1, undef],
58 relatedTo => [0, 'string', 0, undef],
59 prodId => [0, 'string', 0, undef],
60 created => [0, 'utcdate', 0, undef],
61 updated => [0, 'utcdate', 1, undef],
62 sequence => [0, 'number', 0, undef],
63 title => [0, 'string', 0, ''],
64 description => [0, 'string', 0, ''],
65 links => [0, 'object', 0, undef],
66 locale => [0, 'string', 0, undef],
67 localizations => [0, 'patch', 0, undef],
68 locations => [0, 'object', 0, undef],
69 isAllDay => [0, 'bool', 0, $JSON::false],
70 start => [0, 'localdate', 1, undef],
71 timeZone => [0, 'timezone', 0, undef],
72 duration => [0, 'duration', 0, undef],
73 recurrenceRule => [0, 'object', 0, undef],
74 recurrenceOverrides => [0, 'patch', 0, undef],
75 status => [0, 'string', 0, undef],
76 showAsFree => [0, 'bool', 0, undef],
77 replyTo => [0, 'object', 0, undef],
78 participants => [0, 'object', 0, undef],
79 useDefaultAlerts => [0, 'bool', 0, $JSON::false],
80 alerts => [0, 'object', 0, undef],
81 },
82 replyTo => {
83 imip => [0, 'mailto', 0, undef],
84 web => [0, 'href', 0, undef],
85 },
86 links => {
87 href => [0, 'string', 1, undef],
88 type => [0, 'string', 0, undef],
89 size => [0, 'number', 0, undef],
90 rel => [0, 'string', 1, undef],
91 title => [0, 'string', 1, undef],
92 properties => [0, 'string', 1, undef],
93 },
94 locations => {
95 name => [0, 'string', 0, undef],
96 accessInstructions => [0, 'string', 0, undef],
97 rel => [0, 'string', 0, 'unknown'],
98 timeZone => [0, 'timezone', 0, undef],
99 address => [0, 'object', 0, undef],
100 coordinates => [0, 'string', 0, undef],
101 uri => [0, 'string', 0, undef],
102 },
103 recurrenceRule => {
104 frequency => [0, 'string', 1, undef],
105 interval => [0, 'number', 0, undef],
106 rscale => [0, 'string', 0, 'gregorian'],
107 skip => [0, 'string', 0, 'omit'],
108 firstDayOfWeek => [0, 'string', 0, 'monday'],
109 byDay => [1, 'object', 0, undef],
110 byDate => [1, 'number', 0, undef],
111 byMonth => [1, 'string', 0, undef],
112 byYearDay => [1, 'number', 0, undef],
113 byWeekNo => [1, 'number', 0, undef],
114 byHour => [1, 'number', 0, undef],
115 byMinute => [1, 'number', 0, undef],
116 bySecond => [1, 'number', 0, undef],
117 bySetPosition => [1, 'number', 0, undef],
118 count => [0, 'number', 0, undef],
119 until => [0, 'localdate', 0, undef],
120 },
121 byDay => {
122 day => [0, 'string', 1, undef],
123 nthOfPeriod => [0, 'number', 0, undef],
124 },
125 participants => {
126 name => [0, 'string', 1, undef],
127 email => [0, 'string', 1, undef],
128 kind => [0, 'string', 0, 'unknown'],
129 roles => [1, 'string', 1, undef],
130 locationId => [0, 'string', 0, undef],
131 scheduleStatus => [0, 'string', 0, 'needs-action'],
132 schedulePriority => [0, 'string', 0, 'required'],
133 scheduleRSVP => [0, 'bool', 0, $JSON::false],
134 scheduleUpdated => [0, 'utcdate', 0, undef],
135 memberOf => [1, 'string', 0, undef],
136 },
137 alerts => {
138 relativeTo => [0, 'string', 0, 'before-start'],
139 offset => [0, 'duration', 1, undef],
140 action => [0, 'object', 1, undef],
141 },
142 action => {
143 type => [0, 'string', 1, undef],
144 },
145 );
146  
147 # Colour names defined in CSS Color Module Level 3
148 # http://www.w3.org/TR/css3-color/
149  
150 %ColourNames
151 3 = map { $_ => 1 }
152 qw{
153 aliceblue
154 antiquewhite
155 aqua
156 aquamarine
157 azure
158 beige
159 bisque
160 black
161 blanchedalmond
162 blue
163 blueviolet
164 brown
165 burlywood
166 cadetblue
167 chartreuse
168 chocolate
169 coral
170 cornflowerblue
171 cornsilk
172 crimson
173 cyan
174 darkblue
175 darkcyan
176 darkgoldenrod
177 darkgray
178 darkgreen
179 darkgrey
180 darkkhaki
181 darkmagenta
182 darkolivegreen
183 darkorange
184 darkorchid
185 darkred
186 darksalmon
187 darkseagreen
188 darkslateblue
189 darkslategray
190 darkslategrey
191 darkturquoise
192 darkviolet
193 deeppink
194 deepskyblue
195 dimgray
196 dimgrey
197 dodgerblue
198 firebrick
199 floralwhite
200 forestgreen
201 fuchsia
202 gainsboro
203 ghostwhite
204 gold
205 goldenrod
206 gray
207 green
208 greenyellow
209 grey
210 honeydew
211 hotpink
212 indianred
213 indigo
214 ivory
215 khaki
216 lavender
217 lavenderblush
218 lawngreen
219 lemonchiffon
220 lightblue
221 lightcoral
222 lightcyan
223 lightgoldenrodyellow
224 lightgray
225 lightgreen
226 lightgrey
227 lightpink
228 lightsalmon
229 lightseagreen
230 lightskyblue
231 lightslategray
232 lightslategrey
233 lightsteelblue
234 lightyellow
235 lime
236 limegreen
237 linen
238 magenta
239 maroon
240 mediumaquamarine
241 mediumblue
242 mediumorchid
243 mediumpurple
244 mediumseagreen
245 mediumslateblue
246 mediumspringgreen
247 mediumturquoise
248 mediumvioletred
249 midnightblue
250 mintcream
251 mistyrose
252 moccasin
253 navajowhite
254 navy
255 oldlace
256 olive
257 olivedrab
258 orange
259 orangered
260 orchid
261 palegoldenrod
262 palegreen
263 paleturquoise
264 palevioletred
265 papayawhip
266 peachpuff
267 peru
268 pink
269 plum
270 powderblue
271 purple
272 red
273 rosybrown
274 royalblue
275 saddlebrown
276 salmon
277 sandybrown
278 seagreen
279 seashell
280 sienna
281 silver
282 skyblue
283 slateblue
284 slategray
285 slategrey
286 snow
287 springgreen
288 steelblue
289 tan
290 teal
291 thistle
292 tomato
293 turquoise
294 violet
295 wheat
296 white
297 whitesmoke
298 yellow
299 yellowgreen
300 };
301  
302 3 %UTCLinks = (
303 'Etc/GMT-0' => 1,
304 'Etc/GMT+0' => 1,
305 'Etc/GMT0' => 1,
306 'Etc/GMT' => 1,
307 'Etc/Greenwich' => 1,
308 'Etc/UCT' => 1,
309 'Etc/Universal' => 1,
310 'Etc/UTC' => 1,
311 'Etc/Zulu' => 1,
312 'GMT' => 1,
313 'UCT' => 1,
314 'UTC' => 1,
315 );
316 }
317  
318  
319 =head1 NAME
320  
321 Net::CalDAVTalk - Module to talk CalDAV and give a JSON interface to the data
322  
323 =head1 VERSION
324  
325 Version 0.16
326  
327 =cut
328  
329 3 our $VERSION = '0.16';
330  
331  
332 =head1 SYNOPSIS
333  
334 This module is the initial release of the code used at FastMail for talking
335 to CalDAV servers. It's quite specific to an early version of our API, so
336 while it might be useful to others, it's being pushed to CPAN more because
337 the Cassandane test suite needs it.
338  
339 See Net::DAVTalk for details on how to specify hosts and paths.
340  
341 my $CalDAV = Net::CalDAVTalk->new(
342 user => $service->user(),
343 password => $service->pass(),
344 host => $service->host(),
345 port => $service->port(),
346 scheme => 'http',
347 url => '/',
348 expandurl => 1,
349 );
350  
351 or using DNS:
352  
353 my $domain = $user;
354 $domain =~ s/.*\@//;
355  
356 my $url;
357 my ($reply) = $Resolver->search("_caldavs._tcp.$domain", "srv");
358 if ($reply) {
359 my @d = $reply->answer;
360 if (@d) {
361 my $host = $d[0]->target();
362 my $port = $d[0]->port();
363 $url = "https://$host";
364 $url .= ":$port" unless $port eq 443;
365 }
366 }
367  
368 This will use the '/.well-known/caldav' address to find the actual current user
369 principal, and from there the calendar-home-set for further operations.
370  
371 my $foo = Net::CalDAVTalk->new(
372 user => $user,
373 password => $password,
374 url => $url,
375 expandurl => 1,
376 );
377  
378  
379 =head1 SUBROUTINES/METHODS
380  
381 =head2 new(%args)
382  
383 Takes the same arguments as Net::DAVTalk and adds the caldav namespaces
384 and some Cyrus specific namespaces for all XML requests.
385  
386 A => 'http://apple.com/ns/ical/'
387 C => 'urn:ietf:params:xml:ns:caldav'
388 CY => 'http://cyrusimap.org/ns/'
389 UF => 'http://cyrusimap.org/ns/userflag/'
390 SF => 'http://cyrusimap.org/ns/sysflag/'
391  
392 =cut
393  
394 sub new {
395 1 ▶ my ($Class, %Params) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
396  
397 1 $Params{homesetns} = 'C';
398 1 $Params{homeset} = 'calendar-home-set';
399 1 $Params{wellknown} = 'caldav';
400  
401 1 my $Self = $Class->SUPER::new(%Params);
402  
403 1 $Self->ns(A => 'http://apple.com/ns/ical/');
404 1 $Self->ns(C => 'urn:ietf:params:xml:ns:caldav');
405 1 $Self->ns(CY => 'http://cyrusimap.org/ns/');
406 1 $Self->ns(UF => 'http://cyrusimap.org/ns/userflag/');
407 1 $Self->ns(SF => 'http://cyrusimap.org/ns/sysflag/');
408  
409 1 return $Self;
410 }
411  
412 sub _jscal {
413 15 ▶ my $Self = shift;
Subroutine _jscal: called CC=2 SCAR=7.0
414 15 ▶ $Self->{_jscal} ||= Text::JSCalendar->new();
Condition66%2/3
$Self->{"_jscal"} ||= "Text::JSCalendar"->new
l!l&&r!l&&!r
1410
MC/DC0%0/2
$Self->{"_jscal"} ||= "Text::JSCalendar"->new
$Self->{"_jscal"} "Text::JSCalendar"->new
Truth table66%2/3
A ||= B
A= $Self->{"_jscal"}B= "Text::JSCalendar"->new
ABresult
1X1
011
000
415 15 return $Self->{_jscal};
416 }
417  
418 =head2 $self->tz($name)
419  
420 Returns a DateTime::TimeZone object for the given name, but caches
421 the result for speed.
422  
423 =cut
424  
425 sub tz {
426 0 ▶ my $Self = shift;
Subroutine tz: not called CC=4 SCAR=30.0
Pod documented
427 0 my $tzName = shift;
428 0 ▶ return $FLOATING unless defined $tzName;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tzName 0 0 0
429 0 ▶ return $UTC if $UTCLinks{$tzName};
Branch: 1 branch
BranchTrueFalseTotal
if $UTCLinks{$tzName} 0 0 0
430 0 ▶ unless (exists $Self->{_tz}{$tzName}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Self->{"_tz"}{$tzName}) 0 0 0
431 0 $Self->{_tz}{$tzName} = DateTime::TimeZone->new(name => $tzName);
432 }
433 0 return $Self->{_tz}{$tzName};
434 }
435  
436 =head2 $self->logger(sub { })
437  
438 Sets a function to receive all log messages. Gets called with the first
439 argument being a level name, and then a list of items to log:
440  
441 e.g.
442  
443 $CalDAV->logger(sub {
444 my $level = shift;
445 return if ($level eq 'debug' and not $ENV{DEBUG_CALDAV});
446 warn "LOG $level: $_\n" for @_;
447 });
448  
449 =cut
450  
451 sub logger {
452 0 ▶ my $Self = shift;
Subroutine logger: not called CC=2 SCAR=17.9
Pod documented
453  
454 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
455 0 $Self->{logger} = shift;
456 }
457  
458 0 return $Self->{logger};
459 }
460  
461 =head2 $self->DeleteCalendar($calendarId)
462  
463 Delete the named calendar from the server (shorturl - see Net::DAVTalk)
464  
465 =cut
466  
467 =head2 $Cal->DeleteCalendar($calendarId)
468  
469 Delete the calendar with collection name $calendarId (full or relative path)
470  
471 e.g.
472  
473 $Cal->DeleteCalendar('Default');
474  
475 =cut
476  
477 sub DeleteCalendar {
478 0 ▶ my ($Self, $calendarId) = @_;
Subroutine DeleteCalendar: not called CC=2 SCAR=17.9
Pod documented
479  
480 0 ▶ unless ($calendarId) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($calendarId) 0 0 0
481 0 confess 'Calendar not specified';
482 }
483  
484 $Self->Request(
485 0 'DELETE',
486 "$calendarId/",
487 );
488  
489 0 return 1;
490 }
491  
492 sub _fixColour {
493 0 ▶ my $color = lc(shift || '');
Subroutine _fixColour: not called CC=7 SCAR=40.3
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
494  
495 0 ▶ return $color if $ColourNames{$color};
Branch: 1 branch
BranchTrueFalseTotal
if $ColourNames{$color} 0 0 0
496 0 ▶ return $DefaultCalendarColour unless $color =~ m/^\s*(\#[a-f0-9]{3,8})\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $color =~ /^\s*(\#[a-f0-9]{3,8})\s*$/ 0 0 0
497 0 $color = $1;
498 0 ▶ return uc($color) if length($color) == 7;
Branch: 1 branch
BranchTrueFalseTotal
if length $color == 7 0 0 0
499  
500 # Optional digit is for transparency (RGBA)
501 0 ▶ if ( $color =~ m/^#(.)(.)(.).?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color =~ /^#(.)(.)(.).?$/) 0 0 0
502 0 return uc "#$1$1$2$2$3$3";
503 }
504  
505 # Last two digits are for transparency (RGBA)
506 0 ▶ if ( length($color) == 9 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $color == 9) 0 0 0
507 0 return uc(substr($color,0,7));
508 }
509  
510 0 return $DefaultCalendarColour;
511 }
512  
513  
514 =head2 $self->GetCalendar($calendarId)
515  
516 Get a single calendar from the server by calendarId
517 (currently implemented very inefficiently as a get
518 of all calendars. Returns undef if the calendar
519 doesn't exist.
520  
521 e.g
522 my $Calendar = $CalDAV->GetCalendar('Default');
523  
524 =cut
525  
526 sub GetCalendar {
527 0 ▶ my ($Self, $CalendarId) = @_;
Subroutine GetCalendar: not called CC=3 SCAR=24.8
Pod documented
528 0 my $Calendars = $Self->GetCalendars();
529 0 ▶ die "No calendars" unless ($Calendars and @$Calendars);
Branch: 1 branch
BranchTrueFalseTotal
unless $Calendars and @$Calendars 0 0 0
Condition0%0/3
$Calendars and @$Calendars
!ll&&!rl&&r
000
MC/DC0%0/2
$Calendars and @$Calendars
$Calendars @$Calendars
Truth table0%0/3
A and B
A= $CalendarsB= @$Calendars
ABresult
0X0
100
111
530 0 my ($Calendar) = grep { $_->{id} eq $CalendarId } @$Calendars;
531 0 return $Calendar;
532 }
533  
534 =head2 $self->GetCalendars(Properties => [])
535  
536 Fetch all the calendars on the server. You can request additional
537 properties, but they aren't parsed well yet.
538  
539 e.g
540  
541 my $Calendars = $CalDAV->GetCalendars();
542 foreach my $Cal (@$Calendars) {
543 # do stuff
544 }
545  
546 =cut
547  
548 sub GetCalendars {
549 0 ▶ my ($Self, %Args) = @_;
Subroutine GetCalendars: not called CC=48 SCAR=77.6
Pod documented
550  
551 # XXX To generalise for CPAN:
552 # XXX - the PROPFIND should be D:allprop unless $Args{Properties} is set
553 # XXX - return all properties as object attributes without renaming
554 # XXX - translate property names to our own liking within ME::CalDAV
555  
556 0 my %Properties = map { $_ => 1 } (
557 'D:displayname',
558 'D:resourcetype',
559 'A:calendar-color',
560 'D:current-user-privilege-set',
561 'D:acl',
562 'A:calendar-order',
563 'C:calendar-timezone',
564 'D:sync-token',
565 'D:supported-report-set',
566 'C:supported-calendar-data',
567 0 ▶ @{$Args{Properties} || []},
Branch: 1 branch
BranchTrueFalseTotal
$Args{"Properties"} or [] 0 0 0
568 );
569  
570 my $Response = $Self->Request(
571 'PROPFIND',
572 '',
573 x('D:propfind', $Self->NS(),
574 x('D:prop',
575 0 map { x($_) } keys %Properties,
576 ),
577 ),
578 Depth => 1,
579 );
580  
581 0 my @Calendars;
582  
583 0 my $NS_A = $Self->ns('A');
584 0 my $NS_C = $Self->ns('C');
585 0 my $NS_CY = $Self->ns('CY');
586 0 my $NS_D = $Self->ns('D');
587 0 ▶ foreach my $Response (@{$Response->{"{$NS_D}response"} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$$Response{"{$NS_D}response"} or [] 0 0 0
588 0 ▶ next unless $Response->{"{$NS_D}href"}{content};
Branch: 1 branch
BranchTrueFalseTotal
unless $$Response{"{$NS_D}href"}{"content"} 0 0 0
589 0 my $href = uri_unescape($Response->{"{$NS_D}href"}{content});
590  
591 # grab the short version of the path
592 0 my $calendarId = $Self->shortpath($href);
593 # and remove trailing slash always
594 0 $calendarId =~ s{/$}{};
595  
596 0 ▶ foreach my $Propstat (@{$Response->{"{$NS_D}propstat"} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$$Response{"{$NS_D}propstat"} or [] 0 0 0
597 0 ▶ next unless $Propstat->{"{$NS_D}prop"}{"{$NS_D}resourcetype"}{"{$NS_C}calendar"};
Branch: 1 branch
BranchTrueFalseTotal
unless $$Propstat{"{$NS_D}prop"}{"{$NS_D}resourcetype"}{"{$NS_C}calendar"} 0 0 0
598  
599 # XXX - this should be moved into ME::CalDAV::GetCalendars()
600 0 my $visData = $Propstat->{"{$NS_D}prop"}{"{$NS_C}X-FM-isVisible"}{content};
601 0 ▶ my $isVisible = (not defined($visData) or $visData) ? $JSON::true : $JSON::false;
Branch: 1 branch
BranchTrueFalseTotal
!defined($visData) || $visData ? : 0 0 0
Condition0%0/3
not defined $visData or $visData
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $visData or $visData
not defined $visData $visData
Truth table0%0/3
not(A) or B
A= not defined $visDataB= $visData
ABresult
1X1
011
000
602  
603 0 my %Privileges = (
604 mayAdmin => $JSON::false,
605 mayWrite => $JSON::false,
606 mayRead => $JSON::false,
607 mayReadFreeBusy => $JSON::false,
608 );
609  
610 0 my $Priv = $Propstat->{"{$NS_D}prop"}{"{$NS_D}current-user-privilege-set"}{"{$NS_D}privilege"};
611 0 ▶ $Priv = [] unless ($Priv and ref($Priv) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless $Priv and ref $Priv eq "ARRAY" 0 0 0
Condition0%0/3
$Priv and ref $Priv eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$Priv and ref $Priv eq "ARRAY"
$Priv ref $Priv eq "ARRAY"
Truth table0%0/3
A and B
A= $PrivB= ref $Priv eq "ARRAY"
ABresult
0X0
100
111
612 0 foreach my $item (@$Priv) {
613 0 ▶ $Privileges{'mayAdmin'} = $JSON::true if $item->{"{$NS_CY}admin"};
Branch: 1 branch
BranchTrueFalseTotal
if $$item{"{$NS_CY}admin"} 0 0 0
614 0 ▶ $Privileges{'mayWrite'} = $JSON::true if $item->{"{$NS_D}write-content"};
Branch: 1 branch
BranchTrueFalseTotal
if $$item{"{$NS_D}write-content"} 0 0 0
615 0 ▶ $Privileges{'mayRead'} = $JSON::true if $item->{"{$NS_D}read"};
Branch: 1 branch
BranchTrueFalseTotal
if $$item{"{$NS_D}read"} 0 0 0
616 0 ▶ $Privileges{'mayReadFreeBusy'} = $JSON::true if $item->{"{$NS_C}read-free-busy"};
Branch: 1 branch
BranchTrueFalseTotal
if $$item{"{$NS_C}read-free-busy"} 0 0 0
617 }
618  
619 0 my $CanSync;
620 0 my $Report = $Propstat->{"{$NS_D}prop"}{"{$NS_D}supported-report-set"}{"{$NS_D}supported-report"};
621 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
622 0 foreach my $item (@$Report) {
623 # XXX - do we want to check the other things too?
624 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
625 }
626  
627 0 my $CanEvent;
628 0 my $Type = $Propstat->{"{$NS_D}prop"}{"{$NS_C}supported-calendar-data"}{"{$NS_C}calendar-data"};
629 0 ▶ $Type = [] unless ($Type and ref($Type) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless $Type and ref $Type eq "ARRAY" 0 0 0
Condition0%0/3
$Type and ref $Type eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$Type and ref $Type eq "ARRAY"
$Type ref $Type eq "ARRAY"
Truth table0%0/3
A and B
A= $TypeB= ref $Type eq "ARRAY"
ABresult
0X0
100
111
630 0 foreach my $item (@$Type) {
631 0 ▶ next unless $item->{"\@content-type"};
Branch: 1 branch
BranchTrueFalseTotal
unless $item->{"\@content-type"} 0 0 0
632 0 ▶ $CanEvent = 1 if $item->{"\@content-type"}{content} eq "application/event+json";
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"\@content-type"}{"content"} eq "application/event+json" 0 0 0
633 }
634  
635 # XXX - temporary compat
636 0 ▶ $Privileges{isReadOnly} = $Privileges{mayWrite} ? $JSON::false : $JSON::true;
Branch: 1 branch
BranchTrueFalseTotal
$Privileges{'mayWrite'} ? : 0 0 0
637  
638 0 my @ShareWith;
639 0 my $ace = $Propstat->{"{$NS_D}prop"}{"{$NS_D}acl"}{"{$NS_D}ace"};
640 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
641 0 foreach my $Acl (@$ace) {
642 0 ▶ next if $Acl->{"{$NS_D}protected"}; # ignore admin ACLs
Branch: 1 branch
BranchTrueFalseTotal
if $$Acl{"{$NS_D}protected"} 0 0 0
643 0 ▶ next unless $Acl->{"{$NS_D}grant"};
Branch: 1 branch
BranchTrueFalseTotal
unless $$Acl{"{$NS_D}grant"} 0 0 0
644 0 ▶ next unless $Acl->{"{$NS_D}grant"}{"{$NS_D}privilege"};
Branch: 1 branch
BranchTrueFalseTotal
unless $$Acl{"{$NS_D}grant"}{"{$NS_D}privilege"} 0 0 0
645 0 ▶ next unless ref($Acl->{"{$NS_D}grant"}{"{$NS_D}privilege"}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$Acl{"{$NS_D}grant"}{"{$NS_D}privilege"} eq "ARRAY" 0 0 0
646 # XXX - freeBusyPublic here? Or should we do it via the web server?
647 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
648 0 ▶ next unless $user =~ m{^/dav/principals/user/([^/]+)};
Branch: 1 branch
BranchTrueFalseTotal
unless $user =~ m[^/dav/principals/user/([^/]+)] 0 0 0
649 0 my $email = $1;
650 0 ▶ next if $email eq 'admin';
Branch: 1 branch
BranchTrueFalseTotal
if $email eq "admin" 0 0 0
651 0 my %ShareObject = (
652 email => $email,
653 mayAdmin => $JSON::false,
654 mayWrite => $JSON::false,
655 mayRead => $JSON::false,
656 mayReadFreeBusy => $JSON::false,
657 );
658 0 foreach my $item (@{$Acl->{"{$NS_D}grant"}{"{$NS_D}privilege"}}) {
659 0 ▶ $ShareObject{'mayAdmin'} = $JSON::true if $item->{"{$NS_CY}admin"};
Branch: 1 branch
BranchTrueFalseTotal
if $$item{"{$NS_CY}admin"} 0 0 0
660 0 ▶ $ShareObject{'mayWrite'} = $JSON::true if $item->{"{$NS_D}write-content"};
Branch: 1 branch
BranchTrueFalseTotal
if $$item{"{$NS_D}write-content"} 0 0 0
661 0 ▶ $ShareObject{'mayRead'} = $JSON::true if $item->{"{$NS_D}read"};
Branch: 1 branch
BranchTrueFalseTotal
if $$item{"{$NS_D}read"} 0 0 0
662 0 ▶ $ShareObject{'mayReadFreeBusy'} = $JSON::true if $item->{"{$NS_C}read-free-busy"};
Branch: 1 branch
BranchTrueFalseTotal
if $$item{"{$NS_C}read-free-busy"} 0 0 0
663 }
664  
665 0 push @ShareWith, \%ShareObject;
666 }
667  
668 my %Cal = (
669 id => $calendarId,
670 name => ($Propstat->{"{$NS_D}prop"}{"{$NS_D}displayname"}{content} || $DefaultDisplayName),
671 href => $href,
672 color => _fixColour($Propstat->{"{$NS_D}prop"}{"{$NS_A}calendar-color"}{content}),
673 timeZone => $Propstat->{"{$NS_D}prop"}{"{$NS_C}calendar-timezone"}{content},
674 isVisible => $isVisible,
675 precedence => int($Propstat->{"{$NS_D}prop"}{"{$NS_A}calendar-order"}{content} || 1),
676 0 ▶ syncToken => ($Propstat->{"{$NS_D}prop"}{"{$NS_D}sync-token"}{content} || ''),
Branch: 3 branches
BranchTrueFalseTotal
@ShareWith ? : 0 0 0
$CanSync ? : 0 0 0
$CanEvent ? : 0 0 0
Condition0%0/7
$$Propstat{"{$NS_D}prop"}{"{$NS_D}displayname"}{'content'} || $Net::CalDAVTalk::DefaultDisplayName
l!l&&r!l&&!r
000
$$Propstat{"{$NS_D}prop"}{"{$NS_A}calendar-order"}{'content'} || 1
l!l
00
$$Propstat{"{$NS_D}prop"}{"{$NS_D}sync-token"}{'content'} || ''
l!l
00
MC/DC0%0/2
$$Propstat{"{$NS_D}prop"}{"{$NS_D}displayname"}{'content'} || $Net::CalDAVTalk::DefaultDisplayName
$$Propstat{"{$NS_D}prop"}{"{$NS_D}displayname"}{'content'} $Net::CalDAVTalk::DefaultDisplayName
MC/DC0%0/1
$$Propstat{"{$NS_D}prop"}{"{$NS_A}calendar-order"}{'content'} || 1
$$Propstat{"{$NS_D}prop"}{"{$NS_A}calendar-order"}{'content'}
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/3
A || B
A= $$Propstat{"{$NS_D}prop"}{"{$NS_D}displayname"}{'content'}B= $Net::CalDAVTalk::DefaultDisplayName
ABresult
1X1
011
000
Truth table0%0/2
A
A= $$Propstat{"{$NS_D}prop"}{"{$NS_A}calendar-order"}{'content'}
Aresult
11
00
Truth table0%0/2
A
A= $$Propstat{"{$NS_D}prop"}{"{$NS_D}sync-token"}{'content'}
Aresult
11
00
677 shareWith => (@ShareWith ? \@ShareWith : $JSON::false),
678 canSync => ($CanSync ? $JSON::true : $JSON::false),
679 _can_event => ($CanEvent ? $JSON::true : $JSON::false),
680 %Privileges,
681 );
682  
683  
684 0 push @Calendars, \%Cal;
685 }
686 }
687  
688 0 return \@Calendars;
689 }
690  
691 =head2 $self->NewCalendar($Args)
692  
693 Create a new calendar. The Args are the as the things returned by GetCalendars,
694 except that if you don't provide 'id' (same as shorturl), then a UUID will be
695 generated for you. It's recommended to not provide 'id' unless you need to
696 create a specific path for compatibility with other things, and to use 'name'
697 to identify the calendar for users. 'name' is stored as DAV:displayname.
698  
699 e.g.
700  
701 my $Id = $CalDAV->NewCalendar({name => 'My Calendar', color => 'aqua'});
702  
703 (Color names will be translated based on the CSS name list)
704  
705 =cut
706  
707 sub NewCalendar {
708 0 ▶ my ($Self, $Args) = @_;
Subroutine NewCalendar: not called CC=11 SCAR=48.8
Pod documented
709  
710 0 ▶ unless (ref($Args) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $Args eq "HASH") 0 0 0
711 0 confess 'Invalid calendar';
712 }
713  
714 # The URL should be "/$calendarId/" but this isn't true with Zimbra (Yahoo!
715 # Calendar). It will accept a MKCALENDAR at "/$calendarId/" but will rewrite
716 # the calendar's URL to be "/$HTMLEscapedDisplayName/". I'm sure MKCALENDAR
717 # should follow WebDAV's MKCOL method here, but it's not specified in CalDAV.
718  
719 # default values
720 0 ▶ $Args->{id} //= $Self->genuuid();
Condition0%0/3
$Args->{"id"} //= $Self->genuuid
l!l&&r!l&&!r
000
MC/DC0%0/2
$Args->{"id"} //= $Self->genuuid
$Args->{"id"} $Self->genuuid
Truth table0%0/3
A //= B
A= $Args->{"id"}B= $Self->genuuid
ABresult
1X1
011
000
721 0 ▶ $Args->{name} //= $DefaultDisplayName;
Condition0%0/3
$Args->{"name"} //= $Net::CalDAVTalk::DefaultDisplayName
l!l&&r!l&&!r
000
MC/DC0%0/2
$Args->{"name"} //= $Net::CalDAVTalk::DefaultDisplayName
$Args->{"name"} $Net::CalDAVTalk::DefaultDisplayName
Truth table0%0/3
A //= B
A= $Args->{"name"}B= $Net::CalDAVTalk::DefaultDisplayName
ABresult
1X1
011
000
722  
723 0 my $calendarId = $Args->{id};
724  
725 0 my @Properties;
726  
727 0 push @Properties, x('D:displayname', $Args->{name});
728  
729 0 ▶ if (exists $Args->{isVisible}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Args->{"isVisible"}) 0 0 0
730 0 ▶ push @Properties, x('C:X-FM-isVisible', ($Args->{isVisible} ? 1 : 0));
Branch: 1 branch
BranchTrueFalseTotal
$Args->{'isVisible'} ? : 0 0 0
731 }
732  
733 0 ▶ if (exists $Args->{color}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Args->{"color"}) 0 0 0
734 0 push @Properties, x('A:calendar-color', _fixColour($Args->{color}));
735 }
736  
737 0 ▶ if (exists $Args->{timeZone}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Args->{"timeZone"}) 0 0 0
738 0 push @Properties, x('C:calendar-timezone', $Args->{timeZone});
739 }
740  
741 0 ▶ if (exists $Args->{precedence}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Args->{"precedence"}) 0 0 0
742 0 ▶ unless (($Args->{precedence} // '') =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (($Args->{"precedence"} // "") =~ /^\d+$/) 0 0 0
Condition0%0/2
$Args->{"precedence"} // ""
l!l
00
MC/DC0%0/1
$Args->{"precedence"} // ""
$Args->{"precedence"}
Truth table0%0/2
A
A= $Args->{"precedence"}
Aresult
11
00
743 0 confess "Invalid precedence ($Args->{precedence}) (expected int >= 0)";
744 }
745  
746 0 push @Properties, x('A:calendar-order', $Args->{precedence});
747 }
748  
749 $Self->Request(
750 0 'MKCALENDAR',
751 "$calendarId/",
752 x('C:mkcalendar', $Self->NS(),
753 x('D:set',
754 x('D:prop', @Properties),
755 ),
756 ),
757 );
758  
759 0 return $calendarId;
760 }
761  
762 =head2 $self->UpdateCalendar($Args)
763  
764 Like 'NewCalendar', but updates an existing calendar, and 'id' is required.
765 Returns the id, just like NewCalendar.
766  
767 =cut
768  
769 sub UpdateCalendar {
770 0 ▶ my ($Self, $Args, $Prev) = @_;
Subroutine UpdateCalendar: not called CC=12 SCAR=50.5
Pod documented
771  
772 0 ▶ unless (ref($Args) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $Args eq "HASH") 0 0 0
773 0 confess 'Invalid calendar';
774 }
775  
776 0 my %Calendar = %{$Args};
777 0 my $calendarId = $Calendar{id};
778  
779 0 ▶ unless ($calendarId) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($calendarId) 0 0 0
780 0 confess 'Calendar not specified';
781 }
782  
783 0 my @Params;
784  
785 0 ▶ if (defined $Calendar{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Calendar{"name"}) 0 0 0
786 0 push @Params, x('D:displayname', $Calendar{name});
787 }
788  
789 0 ▶ if (defined $Calendar{color}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Calendar{"color"}) 0 0 0
790 0 push @Params, x('A:calendar-color', _fixColour($Calendar{color}));
791 }
792  
793 0 ▶ if (exists $Args->{timeZone}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Args->{"timeZone"}) 0 0 0
794 0 push @Params, x('C:calendar-timezone', $Args->{timeZone});
795 }
796  
797 0 ▶ if (exists $Calendar{isVisible}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Calendar{"isVisible"}) 0 0 0
798 0 ▶ push @Params, x('C:X-FM-isVisible', $Calendar{isVisible} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$Calendar{'isVisible'} ? : 0 0 0
799 }
800  
801 0 ▶ if (exists $Calendar{precedence}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Calendar{"precedence"}) 0 0 0
802 0 ▶ unless (($Calendar{precedence} ||'') =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (($Calendar{"precedence"} || "") =~ /^\d+$/) 0 0 0
Condition0%0/2
$Calendar{'precedence'} || ''
l!l
00
MC/DC0%0/1
$Calendar{'precedence'} || ''
$Calendar{'precedence'}
Truth table0%0/2
A
A= $Calendar{'precedence'}
Aresult
11
00
803 0 confess "Invalid precedence ($Calendar{precedence})";
804 }
805  
806 0 push @Params, x('A:calendar-order', $Calendar{precedence});
807 }
808  
809 0 ▶ return $calendarId unless @Params;
Branch: 1 branch
BranchTrueFalseTotal
unless @Params 0 0 0
810  
811 0 $Self->Request(
812 'PROPPATCH',
813 "$calendarId/",
814 x('D:propertyupdate', $Self->NS(),
815 x('D:set',
816 x('D:prop',
817 @Params,
818 ),
819 ),
820 ),
821 );
822  
823 0 return $calendarId;
824 }
825  
826 # Event methods
827  
828 =head2 $self->DeleteEvent($Event|$href)
829  
830 Given a single event or the href to the event, delete that event,
831 delete it from the server.
832  
833 Returns true.
834  
835 =cut
836  
837 sub DeleteEvent {
838 0 ▶ my ($Self) = shift;
Subroutine DeleteEvent: not called CC=3 SCAR=24.8
Pod documented
839 0 my ($Event) = @_;
840  
841 0 ▶ confess "Need an event" unless $Event;
Branch: 1 branch
BranchTrueFalseTotal
unless $Event 0 0 0
842  
843 0 ▶ $Event = { href => $Event, summary => $Event } unless ref($Event) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $Event eq "HASH" 0 0 0
844  
845 $Self->Request(
846 'DELETE',
847 $Event->{href},
848 0 );
849  
850 0 return 1;
851 }
852  
853 =head2 $self->GetEvents($calendarId, %Args)
854  
855 Fetches some or all of the events in a calendar.
856  
857 Supported args:
858  
859 href => [] - perform a multi-get on just these fullpath urls.
860 after+before => ISO8601 - date range to query
861  
862 In scalar context returns an arrayref of events. In list context
863 returns both an arrayref of events and an arrayref of errors:
864  
865 e.g.
866  
867 my ($Events, $Errors) = $CalDAV->GetEvents('Default');
868  
869 =cut
870  
871 sub GetEvents {
872 0 ▶ my ($Self, $calendarId, %Args) = @_;
Subroutine GetEvents: not called CC=2 SCAR=17.9
Pod documented
873  
874 0 my $urls = $Self->GetEventLinks($calendarId, %Args);
875  
876 0 my @AllUrls = sort keys %$urls;
877  
878 0 my ($Events, $Errors, $Links) = $Self->GetEventsMulti($calendarId, \@AllUrls, %Args);
879  
880 0 ▶ return wantarray ? ($Events, $Errors, $Links) : $Events;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
881 }
882  
883 =head2 $self->GetEventsMulti($calendarId, $Urls, %Args)
884  
885 Fetches the events in Urs from the calendar
886  
887 Supported args:
888  
889 * ContentType
890 * Version
891  
892 For the calendar-data response
893  
894 In scalar context returns an arrayref of events. In list context
895 returns an array of:
896  
897 * arrayref of events
898 * arrayref of errors:
899 * hash of href to getetag
900  
901 =cut
902  
903 sub GetEventsMulti {
904 0 ▶ my ($Self, $calendarId, $Urls, %Args) = @_;
Subroutine GetEventsMulti: not called CC=24 SCAR=64.0
Pod documented
905  
906 0 ▶ confess "Need a calendarId" unless $calendarId;
Branch: 1 branch
BranchTrueFalseTotal
unless $calendarId 0 0 0
907  
908 0 my @Annotations;
909 0 ▶ my $AnnotNames = $Args{Annotations} || [];
Condition0%0/2
$Args{'Annotations'} || []
l!l
00
MC/DC0%0/1
$Args{'Annotations'} || []
$Args{'Annotations'}
Truth table0%0/2
A
A= $Args{'Annotations'}
Aresult
11
00
910 0 foreach my $key (@$AnnotNames) {
911 0 ▶ my $name = ($key =~ m/:/ ? $key : "C:$key");
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /:/ ? : 0 0 0
912 0 push @Annotations, x($name);
913 }
914  
915 0 my %CalProps;
916 0 ▶ if ($Args{ContentType}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Args{"ContentType"}) 0 0 0
917 0 $CalProps{'content-type'} = $Args{ContentType};
918 }
919 0 ▶ if ($Args{Version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Args{"Version"}) 0 0 0
920 0 $CalProps{'version'} = $Args{Version};
921 }
922  
923 0 my (@Events, @Errors, %Links);
924  
925 0 ▶ while (my @urls = splice(@$Urls, 0, $BATCHSIZE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@urls) = splice(@$Urls, 0, $Net::CalDAVTalk::BATCHSIZE)) 0 0 0
926 my $Response = $Self->Request(
927 'REPORT',
928 "$calendarId/",
929 x('C:calendar-multiget', $Self->NS(),
930 x('D:prop',
931 x('C:calendar-data', \%CalProps),
932 x('D:getetag'),
933 @Annotations,
934 ),
935 0 map { x('D:href', $_) } @urls,
936  
937 ),
938 Depth => 1,
939 );
940  
941 0 my $NS_A = $Self->ns('A');
942 0 my $NS_C = $Self->ns('C');
943 0 my $NS_D = $Self->ns('D');
944 0 ▶ foreach my $Response (@{$Response->{"{$NS_D}response"} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$$Response{"{$NS_D}response"} or [] 0 0 0
945 0 ▶ my $href = uri_unescape($Response->{"{$NS_D}href"}{content} // '');
Condition0%0/2
$$Response{"{$NS_D}href"}{"content"} // ""
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}href"}{"content"} // ""
$$Response{"{$NS_D}href"}{"content"}
Truth table0%0/2
A
A= $$Response{"{$NS_D}href"}{"content"}
Aresult
11
00
946 0 ▶ next unless $href;
Branch: 1 branch
BranchTrueFalseTotal
unless $href 0 0 0
947 0 ▶ foreach my $Propstat (@{$Response->{"{$NS_D}propstat"} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$$Response{"{$NS_D}propstat"} or [] 0 0 0
948 0 my $etag = $Propstat->{"{$NS_D}prop"}{"{$NS_D}getetag"}{content};
949 0 $Links{$href} = $etag;
950 0 my $Prop = $Propstat->{"{$NS_D}prop"}{"{$NS_C}calendar-data"};
951 0 my $Data = $Prop->{content};
952 0 ▶ next unless $Data;
Branch: 1 branch
BranchTrueFalseTotal
unless $Data 0 0 0
953  
954 0 my $Event;
955  
956 0 ▶ if ($Prop->{'-content-type'} and $Prop->{'-content-type'} =~ m{application/event\+json}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Prop->{'-content-type'} and $Prop->{'-content-type'} =~ m[application/event\+json]) { } 0 0 0
Condition0%0/3
$Prop->{"-content-type"} and $Prop->{"-content-type"} =~ m[application/event\+json]
!ll&&!rl&&r
000
MC/DC0%0/2
$Prop->{"-content-type"} and $Prop->{"-content-type"} =~ m[application/event\+json]
$Prop->{"-content-type"} $Prop->{"-content-type"} =~ m[application/event\+json]
Truth table0%0/3
A and B
A= $Prop->{"-content-type"}B= $Prop->{"-content-type"} =~ m[application/event\+json]
ABresult
0X0
100
111
957 # JSON event is in API format already
958 0 $Event = eval { decode_json($Data) };
959 }
960 else {
961 # returns an array, but there should only be one UID per file
962 0 ($Event) = eval { $Self->vcalendarToEvents($Data) };
963 }
964  
965 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
966 0 push @Errors, $@;
967 0 next;
968 }
969 0 ▶ next unless $Event;
Branch: 1 branch
BranchTrueFalseTotal
unless $Event 0 0 0
970  
971 0 ▶ if ($Args{Full}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Args{"Full"}) 0 0 0
972 0 $Event->{_raw} = $Data;
973 }
974  
975 0 $Event->{href} = $href;
976 0 $Event->{id} = $Self->shortpath($href);
977  
978 0 foreach my $key (@$AnnotNames) {
979 0 my $propns = $NS_C;
980 0 my $name = $key;
981 0 ▶ if ($key =~ m/(.*):(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /(.*):(.*)/) 0 0 0
982 0 $name = $2;
983 0 $propns = $Self->ns($1);
984 }
985 0 my $AData = $Propstat->{"{$NS_D}prop"}{"{$propns}$name"}{content};
986 0 ▶ next unless $AData;
Branch: 1 branch
BranchTrueFalseTotal
unless $AData 0 0 0
987 0 $Event->{annotation}{$name} = $AData;
988 }
989  
990 0 push @Events, $Event;
991 }
992 }
993 }
994  
995 0 ▶ return wantarray ? (\@Events, \@Errors, \%Links) : \@Events;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
996 }
997  
998 =head2 $self->GetEventLinks($calendarId, %Args)
999  
1000 Fetches the URLs of calendar events in a calendar.
1001  
1002 Supported args:
1003  
1004 after+before => ISO8601 - date range to query
1005  
1006 returns a hash of href to etag
1007  
1008 =cut
1009  
1010 sub GetEventLinks {
1011 0 ▶ my ($Self, $calendarId, %Args) = @_;
Subroutine GetEventLinks: not called CC=13 SCAR=52.0
Pod documented
1012 0 ▶ confess "Need a calendarId" unless $calendarId;
Branch: 1 branch
BranchTrueFalseTotal
unless $calendarId 0 0 0
1013  
1014 0 my @Extra;
1015 0 ▶ if ($Args{AlwaysRange} || $Args{after} || $Args{before}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Args{"AlwaysRange"} or $Args{"after"} or $Args{"before"}) 0 0 0
Condition0%0/6
$Args{"AlwaysRange"} or $Args{"after"} or $Args{"before"}
l!l&&r!l&&!r
000
$Args{"AlwaysRange"} or $Args{"after"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$Args{"AlwaysRange"} or $Args{"after"} or $Args{"before"}
$Args{"AlwaysRange"} $Args{"after"} $Args{"before"}
Truth table0%0/4
A or B or C
A= $Args{"AlwaysRange"}B= $Args{"after"}C= $Args{"before"}
ABCresult
1XX1
01X1
0011
0000
1016 0 ▶ my $Start = _wireDate($Args{after} || $BoT);
Condition0%0/3
$Args{'after'} || $BoT
l!l&&r!l&&!r
000
MC/DC0%0/2
$Args{'after'} || $BoT
$Args{'after'} $BoT
Truth table0%0/3
A || B
A= $Args{'after'}B= $BoT
ABresult
1X1
011
000
1017 0 ▶ my $End = _wireDate($Args{before} || $EoT);
Condition0%0/3
$Args{'before'} || $EoT
l!l&&r!l&&!r
000
MC/DC0%0/2
$Args{'before'} || $EoT
$Args{'before'} $EoT
Truth table0%0/3
A || B
A= $Args{'before'}B= $EoT
ABresult
1X1
011
000
1018 0 push @Extra, x('C:time-range', {
1019 start => $Start->strftime('%Y%m%dT000000Z'),
1020 end => $End->strftime('%Y%m%dT000000Z'),
1021 });
1022 }
1023  
1024 0 my $Response = $Self->Request(
1025 'REPORT',
1026 "$calendarId/",
1027 x('C:calendar-query', $Self->NS(),
1028 x('D:prop',
1029 x('D:getetag'),
1030 ),
1031 x('C:filter',
1032 x('C:comp-filter', { name => 'VCALENDAR' },
1033 x('C:comp-filter', { name => 'VEVENT' },
1034 @Extra,
1035 ),
1036 ),
1037 ),
1038 ),
1039 Depth => 1,
1040 );
1041  
1042 0 my (%Links, @Errors);
1043  
1044 0 my $NS_A = $Self->ns('A');
1045 0 my $NS_C = $Self->ns('C');
1046 0 my $NS_D = $Self->ns('D');
1047 0 ▶ foreach my $Response (@{$Response->{"{$NS_D}response"} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$$Response{"{$NS_D}response"} or [] 0 0 0
1048 0 ▶ my $href = uri_unescape($Response->{"{$NS_D}href"}{content} // '');
Condition0%0/2
$$Response{"{$NS_D}href"}{"content"} // ""
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}href"}{"content"} // ""
$$Response{"{$NS_D}href"}{"content"}
Truth table0%0/2
A
A= $$Response{"{$NS_D}href"}{"content"}
Aresult
11
00
1049 0 ▶ next unless $href;
Branch: 1 branch
BranchTrueFalseTotal
unless $href 0 0 0
1050 0 ▶ foreach my $Propstat (@{$Response->{"{$NS_D}propstat"} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$$Response{"{$NS_D}propstat"} or [] 0 0 0
1051 0 my $etag = $Propstat->{"{$NS_D}prop"}{"{$NS_D}getetag"}{content};
1052 0 $Links{$href} = $etag;
1053 }
1054 }
1055  
1056 0 return \%Links;
1057 }
1058  
1059 =head2 $self->GetEvent($href)
1060  
1061 Just get a single event (calls GetEvents with that href)
1062  
1063 =cut
1064  
1065 sub GetEvent {
1066 0 ▶ my ($Self, $href, %Args) = @_;
Subroutine GetEvent: not called CC=3 SCAR=24.8
Pod documented
1067  
1068 # XXX - API
1069 0 my $calendarId = $href;
1070 0 $calendarId =~ s{/[^/]*$}{};
1071  
1072 0 my ($Events, $Errors) = $Self->GetEventsMulti($calendarId, [$Self->fullpath($href)], %Args);
1073  
1074 0 ▶ die "Errors @$Errors" if @$Errors;
Branch: 1 branch
BranchTrueFalseTotal
if @$Errors 0 0 0
1075 0 ▶ die "Multiple items returned for $href" if @$Events > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$Events > 1 0 0 0
1076  
1077 0 return $Events->[0];
1078 }
1079  
1080 =head2 $self->GetFreeBusy($calendarId, %Args)
1081  
1082 Like 'GetEvents' but uses a free-busy-query and then generates
1083 synthetic events out of the result.
1084  
1085 Doesn't have a 'href' parameter, just the before/after range.
1086  
1087 =cut
1088  
1089 sub GetFreeBusy {
1090 0 ▶ my ($Self, $calendarId, %Args) = @_;
Subroutine GetFreeBusy: not called CC=16 SCAR=56.1
Pod documented
1091  
1092 # validate parameters {{{
1093  
1094 0 ▶ confess "Need a calendarId" unless $calendarId;
Branch: 1 branch
BranchTrueFalseTotal
unless $calendarId 0 0 0
1095  
1096 0 my @Query;
1097 0 ▶ if ($Args{AlwaysRange} || $Args{after} || $Args{before}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Args{"AlwaysRange"} or $Args{"after"} or $Args{"before"}) 0 0 0
Condition0%0/6
$Args{"AlwaysRange"} or $Args{"after"} or $Args{"before"}
l!l&&r!l&&!r
000
$Args{"AlwaysRange"} or $Args{"after"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$Args{"AlwaysRange"} or $Args{"after"} or $Args{"before"}
$Args{"AlwaysRange"} $Args{"after"} $Args{"before"}
Truth table0%0/4
A or B or C
A= $Args{"AlwaysRange"}B= $Args{"after"}C= $Args{"before"}
ABCresult
1XX1
01X1
0011
0000
1098 0 ▶ my $Start = _wireDate($Args{after} || $BoT);
Condition0%0/3
$Args{'after'} || $BoT
l!l&&r!l&&!r
000
MC/DC0%0/2
$Args{'after'} || $BoT
$Args{'after'} $BoT
Truth table0%0/3
A || B
A= $Args{'after'}B= $BoT
ABresult
1X1
011
000
1099 0 ▶ my $End = _wireDate($Args{before} || $EoT);
Condition0%0/3
$Args{'before'} || $EoT
l!l&&r!l&&!r
000
MC/DC0%0/2
$Args{'before'} || $EoT
$Args{'before'} $EoT
Truth table0%0/3
A || B
A= $Args{'before'}B= $EoT
ABresult
1X1
011
000
1100  
1101 0 push @Query,
1102 x('C:time-range', {
1103 start => $Start->strftime('%Y%m%dT000000Z'),
1104 end => $End->strftime('%Y%m%dT000000Z'),
1105 });
1106 }
1107  
1108 # }}}
1109  
1110 0 my $Response = $Self->Request(
1111 'REPORT',
1112 "$calendarId/",
1113 x('C:free-busy-query', $Self->NS(),
1114 @Query,
1115 ),
1116 Depth => 1,
1117 );
1118  
1119 0 ▶ my $Data = eval { vcard2hash($Response->{content}, multival => ['rrule'], only_one => 1) }
Branch: 1 branch
BranchTrueFalseTotal
my $Data = eval { do { vcard2hash($Response->{"content"}, "multival", ["rrule"], "only_one", 1) } } or confess("Error parsing VFreeBusy data: $@") 0 0 0
1120 or confess "Error parsing VFreeBusy data: $@";
1121  
1122 0 my @result;
1123 my @errors;
1124 0 my $now = DateTime->now();
1125 0 foreach my $item (@{$Data->{objects}[0]{objects}}) {
1126 0 ▶ next unless $item->{type} eq 'vfreebusy';
Branch: 1 branch
BranchTrueFalseTotal
unless $item->{"type"} eq "vfreebusy" 0 0 0
1127 0 foreach my $line (@{$item->{properties}{freebusy}}) {
1128 0 my ($Start, $End) = split '/', $line->{value};
1129 0 my ($StartTime, $IsAllDay) = $Self->_makeDateObj($Start, 'UTC', 'UTC');
1130 0 my $EndTime;
1131 0 ▶ if ($End =~ m/^[+-]?P/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($End =~ /^[+-]?P/i) { } 0 0 0
1132 0 ▶ my $Duration = eval { DateTime::Format::ICal->parse_duration(uc $End) }
Condition0%0/2
eval { || (next)
l!l
00
MC/DC0%0/1
eval { do { 'DateTime::Format::ICal'->parse_duration(uc $End) } } || (next)
eval { do { 'DateTime::Format::ICal'->parse_duration(uc $End) } }
Truth table0%0/2
A
A= eval { do { 'DateTime::Format::ICal'->parse_duration(uc $End) } }
Aresult
11
00
1133 || next;
1134 0 $EndTime = $StartTime->clone()->add($Duration);
1135 } else {
1136 0 ($EndTime) = $Self->_makeDateObj($End, 'UTC', 'UTC');
1137 }
1138 0 my $duration = $Self->_make_duration($EndTime->subtract_datetime($StartTime));
1139 my $NewEvent = {
1140 timeZone => 'Etc/UTC',
1141 start => $StartTime->iso8601(),
1142 duration => $duration,
1143 0 ▶ title => ($Args{name} // ''),
Branch: 1 branch
BranchTrueFalseTotal
$IsAllDay ? : 0 0 0
Condition0%0/2
$Args{"name"} // ""
l!l
00
MC/DC0%0/1
$Args{"name"} // ""
$Args{"name"}
Truth table0%0/2
A
A= $Args{"name"}
Aresult
11
00
1144 isAllDay => ($IsAllDay ? $JSON::true : $JSON::false),
1145 updated => $now->iso8601(),
1146 };
1147  
1148 # Generate a uid that should remain the same for this freebusy entry
1149 0 $NewEvent->{uid} = _hexkey($NewEvent) . '-freebusyauto';
1150 $NewEvent->{isAllDay} =
1151 0 ▶ $NewEvent->{isAllDay} ? $JSON::true : $JSON::false;
Branch: 1 branch
BranchTrueFalseTotal
$NewEvent->{'isAllDay'} ? : 0 0 0
1152 0 push @result, $NewEvent;
1153 }
1154 }
1155  
1156 0 return (\@result, \@errors);
1157 }
1158  
1159 =head2 $self->SyncEvents($calendarId, %Args)
1160  
1161 Like GetEvents, but if you pass a syncToken argument, then it will
1162 fetch changes since that token (obtained from an earlier GetCalendars
1163 call).
1164  
1165 In scalar context still just returns new events, in list context returns
1166 Events, Removed and Errors.
1167  
1168 e.g.
1169  
1170 my ($Events, $Removed, $Errors) = $CalDAV->SyncEvents('Default', syncToken => '...');
1171  
1172 =cut
1173  
1174 sub SyncEvents {
1175 0 ▶ my ($Self, $calendarId, %Args) = @_;
Subroutine SyncEvents: not called CC=2 SCAR=17.9
Pod documented
1176  
1177 0 my ($Added, $Removed, $Errors, $SyncToken) = $Self->SyncEventLinks($calendarId, %Args);
1178  
1179 0 my @AllUrls = sort keys %$Added;
1180  
1181 0 my ($Events, $ThisErrors, $Links) = $Self->GetEventsMulti($calendarId, \@AllUrls, %Args);
1182 0 push @$Errors, @$ThisErrors;
1183  
1184 0 ▶ return wantarray ? ($Events, $Removed, $Errors, $SyncToken, $Links) : $Events;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1185 }
1186  
1187 =head2 $self->SyncEventLinks($calendarId, %Args)
1188  
1189 Like GetEventLinks, but if you pass a syncToken argument, then it will
1190 fetch changes since that token (obtained from an earlier GetCalendars
1191 or SyncEvent* call).
1192  
1193 In scalar context still just returns Added, in list context returns
1194 Added, Removed, Errors and new token:
1195  
1196 * Added: hash of href to etag - added or changed
1197 * Removed: array of href
1198 * Errors: array of descritive string
1199 * NewToken: scalar opaque DAV:sync-token
1200  
1201 e.g.
1202  
1203 my ($Added, $Removed, $Errors, $NewToken)
1204 = $CalDAV->SyncEventLinks('Default', syncToken => '...');
1205  
1206 =cut
1207  
1208 sub SyncEventLinks {
1209 0 ▶ my ($Self, $calendarId, %Args) = @_;
Subroutine SyncEventLinks: not called CC=12 SCAR=50.5
Pod documented
1210  
1211 0 ▶ confess "Need a calendarId" unless $calendarId;
Branch: 1 branch
BranchTrueFalseTotal
unless $calendarId 0 0 0
1212  
1213 my $Response = $Self->Request(
1214 'REPORT',
1215 "$calendarId/",
1216 x('D:sync-collection', $Self->NS(),
1217 0 ▶ x('D:sync-token', ($Args{syncToken} ? ($Args{syncToken}) : ())),
Branch: 1 branch
BranchTrueFalseTotal
$Args{'syncToken'} ? : 0 0 0
1218 x('D:sync-level', 1),
1219 x('D:prop',
1220 x('D:getetag'),
1221 ),
1222 ),
1223 );
1224  
1225 0 my $NS_A = $Self->ns('A');
1226 0 my $NS_C = $Self->ns('C');
1227 0 my $NS_D = $Self->ns('D');
1228  
1229 0 my $SyncToken = $Response->{"{$NS_D}sync-token"}{content};
1230 0 ▶ confess "NO SYNC TOKEN RETURNED " . Dumper($Response) unless $SyncToken;
Branch: 1 branch
BranchTrueFalseTotal
unless $SyncToken 0 0 0
1231  
1232 0 my (%Added, @Removed, @Errors);
1233 0 ▶ foreach my $Response (@{$Response->{"{$NS_D}response"} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$$Response{"{$NS_D}response"} or [] 0 0 0
1234 0 ▶ my $href = uri_unescape($Response->{"{$NS_D}href"}{content} // '');
Condition0%0/2
$$Response{"{$NS_D}href"}{"content"} // ""
l!l
00
MC/DC0%0/1
$$Response{"{$NS_D}href"}{"content"} // ""
$$Response{"{$NS_D}href"}{"content"}
Truth table0%0/2
A
A= $$Response{"{$NS_D}href"}{"content"}
Aresult
11
00
1235 0 ▶ next unless $href;
Branch: 1 branch
BranchTrueFalseTotal
unless $href 0 0 0
1236  
1237 0 ▶ unless ($Response->{"{$NS_D}propstat"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$Response{"{$NS_D}propstat"}) 0 0 0
1238 0 push @Removed, $href;
1239 0 next;
1240 }
1241  
1242 0 ▶ foreach my $Propstat (@{$Response->{"{$NS_D}propstat"} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$$Response{"{$NS_D}propstat"} or [] 0 0 0
1243 0 my $status = $Propstat->{"{$NS_D}status"}{content};
1244 0 ▶ if ($status =~ m/ 200 /) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status =~ / 200 /) { } 0 0 0
1245 0 my $etag = $Propstat->{"{$NS_D}prop"}{"{$NS_D}getetag"}{content};
1246 0 $Added{$href} = $etag;
1247 }
1248 else {
1249 0 push @Errors, "Odd status $status";
1250 }
1251 }
1252 }
1253  
1254 0 return (\%Added, \@Removed, \@Errors, $SyncToken);
1255 }
1256  
1257 =head2 $self->NewEvent($calendarId, $Args)
1258  
1259 Create a new event in the named calendar. If you don't specify 'uid' then
1260 a UUID will be created. You should only specify the UID if you need to for
1261 syncing purposes - it's better to auto-generate otherwise.
1262  
1263 Returns the href, but also updates 'uid' in $Args.
1264  
1265 Also updates 'sequence'.
1266  
1267 Special keys consumed from $Args (not stored in the event):
1268  
1269 =over 4
1270  
1271 =item _no_schedule
1272  
1273 If true, sends C<Schedule-Reply: false> on the PUT request, suppressing
1274 iTIP scheduling replies from the server (RFC 6638 section 8.1).
1275  
1276 =back
1277  
1278 e.g.
1279  
1280 my $href = $CalDAV->NewEvent('Default', $Args);
1281 my $newuid = $Args->{uid};
1282  
1283 =cut
1284  
1285 sub NewEvent {
1286 0 ▶ my ($Self, $calendarId, $Args) = @_;
Subroutine NewEvent: not called CC=11 SCAR=48.8
Pod documented
1287  
1288 0 ▶ confess "Need a calendarId" unless $calendarId;
Branch: 1 branch
BranchTrueFalseTotal
unless $calendarId 0 0 0
1289  
1290 0 ▶ confess "invalid event" unless ref($Args) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $Args eq "HASH" 0 0 0
1291  
1292 0 my $UseEvent = delete $Args->{_put_event_json};
1293 0 my $no_schedule = delete $Args->{_no_schedule};
1294 0 ▶ my @sched_header = $no_schedule ? ('Schedule-Reply' => 'false') : ();
Branch: 1 branch
BranchTrueFalseTotal
$no_schedule ? : 0 0 0
1295  
1296 # calculate updated sequence numbers
1297 0 ▶ unless (exists $Args->{sequence}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Args->{"sequence"}) 0 0 0
1298 0 $Args->{sequence} = 1;
1299 }
1300  
1301 0 ▶ if ($Args->{exceptions}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Args->{"exceptions"}) 0 0 0
1302 0 foreach my $recurrenceId (sort keys %{$Args->{exceptions}}) {
1303 0 my $val = $Args->{exceptions}{$recurrenceId};
1304 0 ▶ next unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 0 0
1305 0 ▶ next if exists $val->{sequence};
Branch: 1 branch
BranchTrueFalseTotal
if exists $val->{"sequence"} 0 0 0
1306  
1307 0 $val->{sequence} = $Args->{sequence};
1308 }
1309 }
1310  
1311 0 ▶ $Args->{uid} //= $Self->genuuid();
Condition0%0/3
$Args->{"uid"} //= $Self->genuuid
l!l&&r!l&&!r
000
MC/DC0%0/2
$Args->{"uid"} //= $Self->genuuid
$Args->{"uid"} $Self->genuuid
Truth table0%0/3
A //= B
A= $Args->{"uid"}B= $Self->genuuid
ABresult
1X1
011
000
1312 0 my $uid = $Args->{uid};
1313 0 my $path = $uid;
1314 0 $path =~ tr/[a-zA-Z0-9\@\.\_\-]//cd;
1315 0 my $href = "$calendarId/$path.ics";
1316  
1317 0 ▶ if ($UseEvent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UseEvent) { } 0 0 0
1318 0 $Self->Request(
1319 'PUT',
1320 $href,
1321 encode_json($Args),
1322 'Content-Type' => 'application/event+json',
1323 @sched_header,
1324 );
1325 }
1326 else {
1327 0 my $VCalendar = $Self->_argsToVCalendar($Args);
1328 0 $Self->Request(
1329 'PUT',
1330 $href,
1331 $VCalendar->as_string(),
1332 'Content-Type' => 'text/calendar',
1333 @sched_header,
1334 );
1335 }
1336  
1337 0 return $href;
1338 }
1339  
1340 =head2 $self->UpdateEvent($href, $Args)
1341  
1342 Like NewEvent, but you only need to specify keys that you want to change,
1343 and it takes the full href to the card instead of the containing calendar.
1344  
1345 Accepts the same special keys as NewEvent (e.g. C<_no_schedule>).
1346  
1347 =cut
1348  
1349 sub UpdateEvent {
1350 0 ▶ my ($Self, $href, $Args) = @_;
Subroutine UpdateEvent: not called CC=3 SCAR=24.8
Pod documented
1351  
1352 0 my $UseEvent = delete $Args->{_put_event_json};
1353 0 my $no_schedule = delete $Args->{_no_schedule};
1354 0 ▶ my @sched_header = $no_schedule ? ('Schedule-Reply' => 'false') : ();
Branch: 1 branch
BranchTrueFalseTotal
$no_schedule ? : 0 0 0
1355  
1356 0 my ($OldEvent, $NewEvent) = $Self->_updateEvent($href, $Args);
1357  
1358 0 ▶ if ($UseEvent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UseEvent) { } 0 0 0
1359 0 $Self->Request(
1360 'PUT',
1361 $href,
1362 encode_json($NewEvent),
1363 'Content-Type' => 'application/event+json',
1364 @sched_header,
1365 );
1366 }
1367 else {
1368 0 my $VCalendar = $Self->_argsToVCalendar($NewEvent);
1369 0 $Self->Request(
1370 'PUT',
1371 $href,
1372 $VCalendar->as_string(),
1373 'Content-Type' => 'text/calendar',
1374 @sched_header,
1375 );
1376 }
1377  
1378 0 return 1;
1379 }
1380  
1381 sub _updateEvent {
1382 0 ▶ my ($Self, $href, $Args) = @_;
Subroutine _updateEvent: not called CC=23 SCAR=63.1
1383  
1384 0 my $OldEvent = $Self->GetEvent($href);
1385  
1386 0 ▶ confess "Error getting old event for $href"
Branch: 1 branch
BranchTrueFalseTotal
unless $OldEvent 0 0 0
1387 unless $OldEvent;
1388  
1389 # Merge patch onto old event (both are flat JSCalendar hashes)
1390 0 my %NewEvent = %$OldEvent;
1391 0 for my $key (keys %$Args) {
1392 0 ▶ if (defined $Args->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Args->{$key}) { } 0 0 0
1393 0 $NewEvent{$key} = $Args->{$key};
1394 } else {
1395 0 delete $NewEvent{$key};
1396 }
1397 }
1398  
1399 # calculate updated sequence numbers
1400 0 ▶ unless (exists $Args->{sequence}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Args->{"sequence"}) 0 0 0
1401 0 ▶ $NewEvent{sequence} = ($OldEvent->{sequence} || 0) + 1;
Condition0%0/2
$OldEvent->{'sequence'} || 0
l!l
00
MC/DC0%0/1
$OldEvent->{'sequence'} || 0
$OldEvent->{'sequence'}
Truth table0%0/2
A
A= $OldEvent->{'sequence'}
Aresult
11
00
1402 }
1403  
1404 0 ▶ if ($NewEvent{exceptions}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($NewEvent{"exceptions"}) 0 0 0
1405 0 foreach my $recurrenceId (sort keys %{$NewEvent{exceptions}}) {
1406 0 my $val = $NewEvent{exceptions}{$recurrenceId};
1407 0 ▶ next unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 0 0
1408 0 ▶ next if exists $val->{sequence};
Branch: 1 branch
BranchTrueFalseTotal
if exists $val->{"sequence"} 0 0 0
1409  
1410 0 my $old = $OldEvent->{exceptions}{$recurrenceId};
1411 0 my $sequence = $NewEvent{sequence};
1412 0 ▶ if ($old && exists $old->{sequence}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old and exists $old->{"sequence"}) 0 0 0
Condition0%0/3
$old and exists $old->{"sequence"}
!ll&&!rl&&r
000
MC/DC0%0/2
$old and exists $old->{"sequence"}
$old exists $old->{"sequence"}
Truth table0%0/3
A and B
A= $oldB= exists $old->{"sequence"}
ABresult
0X0
100
111
1413 0 ▶ $sequence = $old->{sequence} + 1 unless $sequence > $old->{sequence};
Branch: 1 branch
BranchTrueFalseTotal
unless $sequence > $old->{"sequence"} 0 0 0
1414 }
1415 0 ▶ $val->{sequence} = $sequence if $sequence > $NewEvent{sequence};
Branch: 1 branch
BranchTrueFalseTotal
if $sequence > $NewEvent{"sequence"} 0 0 0
1416 }
1417 }
1418  
1419 0 ▶ if ($NewEvent{recurrenceOverrides}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($NewEvent{"recurrenceOverrides"}) 0 0 0
1420 0 foreach my $recurrenceId (sort keys %{$NewEvent{recurrenceOverrides}}) {
1421 0 my $val = $NewEvent{recurrenceOverrides}{$recurrenceId};
1422 0 ▶ next unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 0 0
1423 0 ▶ next if exists $val->{sequence};
Branch: 1 branch
BranchTrueFalseTotal
if exists $val->{"sequence"} 0 0 0
1424  
1425 0 ▶ my $old = ($OldEvent->{recurrenceOverrides} || {})->{$recurrenceId};
Condition0%0/2
$OldEvent->{'recurrenceOverrides'} || {}
l!l
00
MC/DC0%0/1
$OldEvent->{'recurrenceOverrides'} || {}
$OldEvent->{'recurrenceOverrides'}
Truth table0%0/2
A
A= $OldEvent->{'recurrenceOverrides'}
Aresult
11
00
1426 0 my $sequence = $NewEvent{sequence};
1427 0 ▶ if ($old && exists $old->{sequence}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old and exists $old->{"sequence"}) 0 0 0
Condition0%0/3
$old and exists $old->{"sequence"}
!ll&&!rl&&r
000
MC/DC0%0/2
$old and exists $old->{"sequence"}
$old exists $old->{"sequence"}
Truth table0%0/3
A and B
A= $oldB= exists $old->{"sequence"}
ABresult
0X0
100
111
1428 0 ▶ $sequence = $old->{sequence} + 1 unless $sequence > $old->{sequence};
Branch: 1 branch
BranchTrueFalseTotal
unless $sequence > $old->{"sequence"} 0 0 0
1429 }
1430 0 ▶ $val->{sequence} = $sequence if $sequence > $NewEvent{sequence};
Branch: 1 branch
BranchTrueFalseTotal
if $sequence > $NewEvent{"sequence"} 0 0 0
1431 }
1432 }
1433  
1434 0 $NewEvent{href} = $href;
1435  
1436 0 return ($OldEvent, \%NewEvent);
1437 }
1438  
1439 =head2 $self->AnnotateEvent($href, $Args)
1440  
1441 Instead of actually changing an event itself, use proppatch to
1442 add or remove properties on the event.
1443  
1444 =cut
1445  
1446 sub AnnotateEvent {
1447 0 ▶ my ($Self, $href, $Args) = @_;
Subroutine AnnotateEvent: not called CC=8 SCAR=42.8
Pod documented
1448  
1449 0 my $OldEvent = $Self->GetEvent($href);
1450  
1451 0 ▶ confess "Error getting old event for $href"
Branch: 1 branch
BranchTrueFalseTotal
unless $OldEvent 0 0 0
1452 unless $OldEvent;
1453  
1454 0 my @Set;
1455 my @Remove;
1456 0 foreach my $key (sort keys %$Args) {
1457 0 ▶ my $name = ($key =~ m/:/ ? $key : "C:$key");
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /:/ ? : 0 0 0
1458 0 ▶ if (defined $Args->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Args->{$key}) { } 0 0 0
1459 0 push @Set, x($name, $Args->{$key});
1460 }
1461 else {
1462 0 push @Remove, x($name);
1463 }
1464 }
1465  
1466 0 my @Params;
1467 0 ▶ push @Params, x('D:set', x('D:prop', @Set)) if @Set;
Branch: 1 branch
BranchTrueFalseTotal
if @Set 0 0 0
1468 0 ▶ push @Params, x('D:remove', x('D:prop', @Remove)) if @Remove;
Branch: 1 branch
BranchTrueFalseTotal
if @Remove 0 0 0
1469 0 ▶ return undef unless @Params;
Branch: 1 branch
BranchTrueFalseTotal
unless @Params 0 0 0
1470  
1471 0 $Self->Request(
1472 'PROPPATCH',
1473 $href,
1474 x('D:propertyupdate', $Self->NS(), @Params),
1475 );
1476  
1477 0 return 1;
1478 }
1479  
1480 =head2 $self->MoveEvent($href, $newCalendarId)
1481  
1482 Move an event into a new calendar. Returns the new href.
1483  
1484 =cut
1485  
1486 sub MoveEvent {
1487 0 ▶ my ($Self, $href, $newCalendarId) = @_;
Subroutine MoveEvent: not called CC=3 SCAR=24.8
Pod documented
1488  
1489 0 my $OldEvent = $Self->GetEvent($href);
1490  
1491 0 ▶ return unless $OldEvent;
Branch: 1 branch
BranchTrueFalseTotal
unless $OldEvent 0 0 0
1492  
1493 0 my $dest = $href;
1494 0 $dest =~ s{.*/}{$newCalendarId/};
1495 0 ▶ return if $href eq $dest;
Branch: 1 branch
BranchTrueFalseTotal
if $href eq $dest 0 0 0
1496  
1497 0 $Self->Request(
1498 'MOVE',
1499 $href,
1500 undef,
1501 'Destination' => $Self->fullpath($dest),
1502 );
1503  
1504 0 warn "CAL: MoveEvent $Self->{user} ($href => $dest)\n";
1505  
1506 0 return $dest;
1507 }
1508  
1509 # ============================================================
1510 # Conversion functions - delegated to Text::JSCalendar
1511 # ============================================================
1512  
1513 =head2 $NewEvent = Net::CalDAVTalk->NormaliseEvent($Event);
1514  
1515 Doesn't change the original event, but removes any keys which are the same as their default value
1516  
1517 =cut
1518  
1519 sub NormaliseEvent {
1520 0 ▶ my ($class, $Event, $Root) = @_;
Subroutine NormaliseEvent: not called CC=1 SCAR=6.9
Pod documented
1521 0 return Text::JSCalendar->NormaliseEvent($Event, $Root);
1522 }
1523  
1524 =head2 Net::CalDAVTalk->CompareEvents($Event1, $Event2);
1525  
1526 Returns true if the events are identical
1527  
1528 =cut
1529  
1530 sub CompareEvents {
1531 0 ▶ my ($class, $Event1, $Event2) = @_;
Subroutine CompareEvents: not called CC=1 SCAR=6.9
Pod documented
1532 0 return Text::JSCalendar->CompareEvents($Event1, $Event2);
1533 }
1534  
1535 sub vcalendarToEvents {
1536 10 ▶ my $Self = shift;
Subroutine vcalendarToEvents: called CC=1 SCAR=0.0
Pod undocumented
1537 10 return $Self->_jscal->vcalendarToEvents(@_);
1538 }
1539  
1540 sub _argsToVCalendar {
1541 5 ▶ my $Self = shift;
Subroutine _argsToVCalendar: called CC=1 SCAR=0.0
1542 5 return $Self->_jscal->_argsToVCalendar(@_);
1543 }
1544  
1545 sub eventsToVCalendar {
1546 0 ▶ my $Self = shift;
Subroutine eventsToVCalendar: not called CC=1 SCAR=6.9
Pod undocumented
1547 0 return $Self->_jscal->eventsToVCalendar(@_);
1548 }
1549  
1550 # ============================================================
1551 # Minimal helpers still used by GetFreeBusy/GetEventLinks
1552 # ============================================================
1553  
1554 sub _wireDate {
1555 0 ▶ return Text::JSCalendar::_wireDate(@_);
Subroutine _wireDate: not called CC=1 SCAR=6.9
1556 }
1557  
1558 sub _makeDateObj {
1559 0 ▶ my $Self = shift;
Subroutine _makeDateObj: not called CC=1 SCAR=6.9
1560 0 return $Self->_jscal->_makeDateObj(@_);
1561 }
1562  
1563 sub _make_duration {
1564 0 ▶ my $Self = shift;
Subroutine _make_duration: not called CC=1 SCAR=6.9
1565 0 return $Self->_jscal->_make_duration(@_);
1566 }
1567  
1568 sub _hexkey {
1569 0 ▶ return Text::JSCalendar::_hexkey(@_);
Subroutine _hexkey: not called CC=1 SCAR=6.9
1570 }
1571  
1572 sub _stripNonICal {
1573 0 ▶ my $Self = shift;
Subroutine _stripNonICal: not called CC=5 SCAR=34.0
1574 0 my $Event = shift;
1575  
1576 0 delete $Event->{alerts};
1577 0 delete $Event->{attendees};
1578 0 delete $Event->{organizer};
1579 0 delete $Event->{participants};
1580 0 delete $Event->{replyTo};
1581  
1582 0 ▶ foreach my $exception (values %{$Event->{exceptions} || $Event->{recurrenceOverrides} || {}}) {
Branch: 1 branch
BranchTrueFalseTotal
$Event->{"exceptions"} or $Event->{"recurrenceOverrides"} or {} 0 0 0
Condition0%0/3
$Event->{"exceptions"} or $Event->{"recurrenceOverrides"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$Event->{"exceptions"} or $Event->{"recurrenceOverrides"}
$Event->{"exceptions"} $Event->{"recurrenceOverrides"}
Truth table0%0/3
A or B
A= $Event->{"exceptions"}B= $Event->{"recurrenceOverrides"}
ABresult
1X1
011
000
1583 0 ▶ next unless $exception;
Branch: 1 branch
BranchTrueFalseTotal
unless $exception 0 0 0
1584 0 $Self->_stripNonICal($exception);
1585 }
1586 }
1587  
1588 =head2 $self->UpdateAddressSet($DisplayName, $EmailAddress)
1589  
1590 Set the address set and display name for the calendar user (if supported)
1591  
1592 =cut
1593  
1594 sub UpdateAddressSet {
1595 0 ▶ my ($Self, $NewDisplayName, $NewAddressSet) = @_;
Subroutine UpdateAddressSet: not called CC=5 SCAR=34.0
Pod documented
1596  
1597 0 my ($DisplayName, $AddressSet) = $Self->GetProps(\$Self->{principal}, 'D:displayname', [ 'C:calendar-user-address-set', 'D:href' ]);
1598  
1599 0 ▶ if (!$AddressSet || $AddressSet ne "mailto:" . $NewAddressSet ||
Branch: 1 branch
BranchTrueFalseTotal
if (not $AddressSet or $AddressSet ne 'mailto:' . $NewAddressSet or not $DisplayName or $DisplayName ne $NewDisplayName) 0 0 0
Condition0%0/9
not $AddressSet or $AddressSet ne 'mailto:' . $NewAddressSet or not $DisplayName or $DisplayName ne $NewDisplayName
l!l&&r!l&&!r
000
not $AddressSet or $AddressSet ne 'mailto:' . $NewAddressSet or not $DisplayName
l!l&&r!l&&!r
000
not $AddressSet or $AddressSet ne 'mailto:' . $NewAddressSet
l!l&&r!l&&!r
000
MC/DC0%0/4
not $AddressSet or $AddressSet ne 'mailto:' . $NewAddressSet or not $DisplayName or $DisplayName ne $NewDisplayName
not $AddressSet $AddressSet ne 'mailto:' . $NewAddressSet not $DisplayName $DisplayName ne $NewDisplayName
Truth table0%0/5
not(A) or B or not(C) or D
A= not $AddressSetB= $AddressSet ne 'mailto:' . $NewAddressSetC= not $DisplayNameD= $DisplayName ne $NewDisplayName
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1600 !$DisplayName || $DisplayName ne $NewDisplayName) {
1601 0 $Self->Request(
1602 'PROPPATCH',
1603 "",
1604 x('D:propertyupdate', $Self->NS(),
1605 x('D:set',
1606 x('D:prop',
1607 x('D:displayname', $NewDisplayName),
1608 x('C:calendar-user-address-set', "mailto:" . $NewAddressSet),
1609 )
1610 )
1611 )
1612 );
1613 0 return 1;
1614 }
1615  
1616 0 return 0;
1617 }
1618  
1619 =head2 $self->GetICal($calendarId, $isFreeBusy)
1620  
1621 Given a calender, fetch all the events and generate an ical format file
1622 suitable for import into a client.
1623  
1624 =cut
1625  
1626 sub GetICal {
1627 0 ▶ my $Self = shift;
Subroutine GetICal: not called CC=7 SCAR=40.3
Pod documented
1628 0 my $calendarId = shift;
1629 0 my $isFreeBusy = shift;
1630  
1631 0 ▶ confess "Need a calendarId" unless $calendarId;
Branch: 1 branch
BranchTrueFalseTotal
unless $calendarId 0 0 0
1632  
1633 0 my $Calendars = $Self->GetCalendars();
1634 0 foreach my $Cal (@$Calendars) {
1635 0 ▶ next unless $calendarId eq $Cal->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $calendarId eq $Cal->{"id"} 0 0 0
1636 0 ▶ my ($Events, $Errors) = $isFreeBusy ?
Branch: 1 branch
BranchTrueFalseTotal
$isFreeBusy ? : 0 0 0
1637 $Self->GetFreeBusy($calendarId) :
1638 $Self->GetEvents($calendarId);
1639 0 ▶ return undef if @$Errors;
Branch: 1 branch
BranchTrueFalseTotal
if @$Errors 0 0 0
1640 0 $Self->_stripNonICal($_) for @$Events;
1641 my $VCalendar = $Self->_argsToVCalendar($Events,
1642 method => 'PUBLISH',
1643 'x-wr-calname' => $Cal->{name},
1644 'x-wr-timezone' => $Cal->{timeZone},
1645 'x-apple-calendar-color' => $Cal->{color},
1646 # XXX - do we want to add our sync-token here or something?
1647 0 );
1648 0 return ($VCalendar->as_string(), $Cal);
1649 }
1650 0 return undef; # 404
1651 }
1652  
1653 =head1 AUTHOR
1654  
1655 Bron Gondwana, C<< <brong at cpan.org> >>
1656  
1657 =head1 BUGS
1658  
1659 Please report any bugs or feature requests to C<bug-net-caldavtalk at rt.cpan.org>, or through
1660 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-CalDAVTalk>. I will be notified, and then you'll
1661 automatically be notified of progress on your bug as I make changes.
1662  
1663  
1664  
1665  
1666 =head1 SUPPORT
1667  
1668 You can find documentation for this module with the perldoc command.
1669  
1670 perldoc Net::CalDAVTalk
1671  
1672  
1673 You can also look for information at:
1674  
1675 =over 4
1676  
1677 =item * RT: CPAN's request tracker (report bugs here)
1678  
1679 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-CalDAVTalk>
1680  
1681 =item * AnnoCPAN: Annotated CPAN documentation
1682  
1683 L<http://annocpan.org/dist/Net-CalDAVTalk>
1684  
1685 =item * CPAN Ratings
1686  
1687 L<http://cpanratings.perl.org/d/Net-CalDAVTalk>
1688  
1689 =item * Search CPAN
1690  
1691 L<http://search.cpan.org/dist/Net-CalDAVTalk/>
1692  
1693 =back
1694  
1695  
1696 =head1 ACKNOWLEDGEMENTS
1697  
1698  
1699 =head1 LICENSE AND COPYRIGHT
1700  
1701 Copyright 2015 FastMail Pty Ltd.
1702  
1703 This program is free software; you can redistribute it and/or modify it
1704 under the terms of the the Artistic License (2.0). You may obtain a
1705 copy of the full license at:
1706  
1707 L<http://www.perlfoundation.org/artistic_license_2_0>
1708  
1709 Any use, modification, and distribution of the Standard or Modified
1710 Versions is governed by this Artistic License. By using, modifying or
1711 distributing the Package, you accept this license. Do not use, modify,
1712 or distribute the Package, if you do not accept this license.
1713  
1714 If your Modified Version has been derived from a Modified Version made
1715 by someone other than you, you are nevertheless required to ensure that
1716 your Modified Version complies with the requirements of this license.
1717  
1718 This license does not grant you the right to use any trademark, service
1719 mark, tradename, or logo of the Copyright Holder.
1720  
1721 This license includes the non-exclusive, worldwide, free-of-charge
1722 patent license to make, have made, use, offer to sell, sell, import and
1723 otherwise transfer the Package with respect to any patent claims
1724 licensable by the Copyright Holder that are necessarily infringed by the
1725 Package. If you institute patent litigation (including a cross-claim or
1726 counterclaim) against any party alleging that the Package constitutes
1727 direct or contributory patent infringement, then this Artistic License
1728 to you shall terminate on the date that such litigation is filed.
1729  
1730 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
1731 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
1732 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
1733 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
1734 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
1735 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
1736 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
1737 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1738  
1739  
1740 =cut
1741  
1742 3 1; # End of Net::CalDAVTalk
↑ Summary CalDAVTalk/TimeZones.pm »