aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/lib/CalDAV/Schedule/Plugin.php
blob: 3e4f47461a3ae95d07848ab91099e155d493dc48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
<?php
/**
 * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OCA\DAV\CalDAV\Schedule;

use DateTimeZone;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\Calendar;
use OCA\DAV\CalDAV\CalendarHome;
use OCA\DAV\CalDAV\CalendarObject;
use OCA\DAV\CalDAV\DefaultCalendarValidator;
use OCP\IConfig;
use Psr\Log\LoggerInterface;
use Sabre\CalDAV\ICalendar;
use Sabre\CalDAV\ICalendarObject;
use Sabre\CalDAV\Schedule\ISchedulingObject;
use Sabre\DAV\Exception as DavException;
use Sabre\DAV\INode;
use Sabre\DAV\IProperties;
use Sabre\DAV\PropFind;
use Sabre\DAV\Server;
use Sabre\DAV\Xml\Property\LocalHref;
use Sabre\DAVACL\IACL;
use Sabre\DAVACL\IPrincipal;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
use Sabre\VObject\Component;
use Sabre\VObject\Component\VCalendar;
use Sabre\VObject\Component\VEvent;
use Sabre\VObject\DateTimeParser;
use Sabre\VObject\FreeBusyGenerator;
use Sabre\VObject\ITip;
use Sabre\VObject\ITip\SameOrganizerForAllComponentsException;
use Sabre\VObject\Parameter;
use Sabre\VObject\Property;
use Sabre\VObject\Reader;
use function Sabre\Uri\split;

class Plugin extends \Sabre\CalDAV\Schedule\Plugin {

	/** @var ITip\Message[] */
	private $schedulingResponses = [];

	/** @var string|null */
	private $pathOfCalendarObjectChange = null;

	public const CALENDAR_USER_TYPE = '{' . self::NS_CALDAV . '}calendar-user-type';
	public const SCHEDULE_DEFAULT_CALENDAR_URL = '{' . Plugin::NS_CALDAV . '}schedule-default-calendar-URL';

	/**
	 * @param IConfig $config
	 */
	public function __construct(
		private IConfig $config,
		private LoggerInterface $logger,
		private DefaultCalendarValidator $defaultCalendarValidator,
	) {
	}

	/**
	 * Initializes the plugin
	 *
	 * @param Server $server
	 * @return void
	 */
	public function initialize(Server $server) {
		parent::initialize($server);
		$server->on('propFind', [$this, 'propFindDefaultCalendarUrl'], 90);
		$server->on('afterWriteContent', [$this, 'dispatchSchedulingResponses']);
		$server->on('afterCreateFile', [$this, 'dispatchSchedulingResponses']);

		// We allow mutating the default calendar URL through the CustomPropertiesBackend
		// (oc_properties table)
		$server->protectedProperties = array_filter(
			$server->protectedProperties,
			static fn (string $property) => $property !== self::SCHEDULE_DEFAULT_CALENDAR_URL,
		);
	}

	/**
	 * Allow manual setting of the object change URL
	 * to support public write
	 *
	 * @param string $path
	 */
	public function setPathOfCalendarObjectChange(string $path): void {
		$this->pathOfCalendarObjectChange = $path;
	}

	/**
	 * This method handler is invoked during fetching of properties.
	 *
	 * We use this event to add calendar-auto-schedule-specific properties.
	 *
	 * @param PropFind $propFind
	 * @param INode $node
	 * @return void
	 */
	public function propFind(PropFind $propFind, INode $node) {
		if ($node instanceof IPrincipal) {
			// overwrite Sabre/Dav's implementation
			$propFind->handle(self::CALENDAR_USER_TYPE, function () use ($node) {
				if ($node instanceof IProperties) {
					$props = $node->getProperties([self::CALENDAR_USER_TYPE]);

					if (isset($props[self::CALENDAR_USER_TYPE])) {
						return $props[self::CALENDAR_USER_TYPE];
					}
				}

				return 'INDIVIDUAL';
			});
		}

		parent::propFind($propFind, $node);
	}

	/**
	 * Returns a list of addresses that are associated with a principal.
	 *
	 * @param string $principal
	 * @return array
	 */
	protected function getAddressesForPrincipal($principal) {
		$result = parent::getAddressesForPrincipal($principal);

		if ($result === null) {
			$result = [];
		}
		
		// iterate through items and html decode values
		foreach ($result as $key => $value) {
			$result[$key] = urldecode($value);
		}

		return $result;
	}

	/**
	 * @param RequestInterface $request
	 * @param ResponseInterface $response
	 * @param VCalendar $vCal
	 * @param mixed $calendarPath
	 * @param mixed $modified
	 * @param mixed $isNew
	 */
	public function calendarObjectChange(RequestInterface $request, ResponseInterface $response, VCalendar $vCal, $calendarPath, &$modified, $isNew) {
		// Save the first path we get as a calendar-object-change request
		if (!$this->pathOfCalendarObjectChange) {
			$this->pathOfCalendarObjectChange = $request->getPath();
		}

		try {

			if (!$this->scheduleReply($this->server->httpRequest)) {
				return;
			}
			
			/** @var Calendar $calendarNode */
			$calendarNode = $this->server->tree->getNodeForPath($calendarPath);
			// extract addresses for owner
			$addresses = $this->getAddressesForPrincipal($calendarNode->getOwner());
			// determain if request is from a sharee
			if ($calendarNode->isShared()) {
				// extract addresses for sharee and add to address collection
				$addresses = array_merge(
					$addresses,
					$this->getAddressesForPrincipal($calendarNode->getPrincipalURI())
				);
			}
			// determine if we are updating a calendar event
			if (!$isNew) {
				// retrieve current calendar event node
				/** @var CalendarObject $currentNode */
				$currentNode = $this->server->tree->getNodeForPath($request->getPath());
				// convert calendar event string data to VCalendar object
				/** @var \Sabre\VObject\Component\VCalendar $currentObject */
				$currentObject = Reader::read($currentNode->get());
			} else {
				$currentObject = null;
			}
			// process request
			$this->processICalendarChange($currentObject, $vCal, $addresses, [], $modified);
	
			if ($currentObject) {
				// Destroy circular references so PHP will GC the object.
				$currentObject->destroy();
			}
			
		} catch (SameOrganizerForAllComponentsException $e) {
			$this->handleSameOrganizerException($e, $vCal, $calendarPath);
		}
	}

	/**
	 * @inheritDoc
	 */
	public function beforeUnbind($path): void {
		try {
			parent::beforeUnbind($path);
		} catch (SameOrganizerForAllComponentsException $e) {
			$node = $this->server->tree->getNodeForPath($path);
			if (!$node instanceof ICalendarObject || $node instanceof ISchedulingObject) {
				throw $e;
			}

			/** @var VCalendar $vCal */
			$vCal = Reader::read($node->get());
			$this->handleSameOrganizerException($e, $vCal, $path);
		}
	}

	/**
	 * @inheritDoc
	 */
	public function scheduleLocalDelivery(ITip\Message $iTipMessage):void {
		/** @var VEvent|null $vevent */
		$vevent = $iTipMessage->message->VEVENT ?? null;

		// Strip VALARMs from incoming VEVENT
		if ($vevent && isset($vevent->VALARM)) {
			$vevent->remove('VALARM');
		}

		parent::scheduleLocalDelivery($iTipMessage);
		// We only care when the message was successfully delivered locally
		// Log all possible codes returned from the parent method that mean something went wrong
		// 3.7, 3.8, 5.0, 5.2
		if ($iTipMessage->scheduleStatus !== '1.2;Message delivered locally') {
			$this->logger->debug('Message not delivered locally with status: ' . $iTipMessage->scheduleStatus);
			return;
		}
		// We only care about request. reply and cancel are properly handled
		// by parent::scheduleLocalDelivery already
		if (strcasecmp($iTipMessage->method, 'REQUEST') !== 0) {
			return;
		}

		// If parent::scheduleLocalDelivery set scheduleStatus to 1.2,
		// it means that it was successfully delivered locally.
		// Meaning that the ACL plugin is loaded and that a principal
		// exists for the given recipient id, no need to double check
		/** @var \Sabre\DAVACL\Plugin $aclPlugin */
		$aclPlugin = $this->server->getPlugin('acl');
		$principalUri = $aclPlugin->getPrincipalByUri($iTipMessage->recipient);
		$calendarUserType = $this->getCalendarUserTypeForPrincipal($principalUri);
		if (strcasecmp($calendarUserType, 'ROOM') !== 0 && strcasecmp($calendarUserType, 'RESOURCE') !== 0) {
			$this->logger->debug('Calendar user type is room or resource, not processing further');
			return;
		}

		$attendee = $this->getCurrentAttendee($iTipMessage);
		if (!$attendee) {
			$this->logger->debug('No attendee set for scheduling message');
			return;
		}

		// We only respond when a response was actually requested
		$rsvp = $this->getAttendeeRSVP($attendee);
		if (!$rsvp) {
			$this->logger->debug('No RSVP requested for attendee ' . $attendee->getValue());
			return;
		}

		if (!$vevent) {
			$this->logger->debug('No VEVENT set to process on scheduling message');
			return;
		}

		// We don't support autoresponses for recurrencing events for now
		if (isset($vevent->RRULE) || isset($vevent->RDATE)) {
			$this->logger->debug('VEVENT is a recurring event, autoresponding not supported');
			return;
		}

		$dtstart = $vevent->DTSTART;
		$dtend = $this->getDTEndFromVEvent($vevent);
		$uid = $vevent->UID->getValue();
		$sequence = isset($vevent->SEQUENCE) ? $vevent->SEQUENCE->getValue() : 0;
		$recurrenceId = isset($vevent->{'RECURRENCE-ID'}) ? $vevent->{'RECURRENCE-ID'}->serialize() : '';

		$message = <<<EOF
BEGIN:VCALENDAR
PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
METHOD:REPLY
VERSION:2.0
BEGIN:VEVENT
ATTENDEE;PARTSTAT=%s:%s
ORGANIZER:%s
UID:%s
SEQUENCE:%s
REQUEST-STATUS:2.0;Success
%sEND:VEVENT
END:VCALENDAR
EOF;

		if ($this->isAvailableAtTime($attendee->getValue(), $dtstart->getDateTime(), $dtend->getDateTime(), $uid)) {
			$partStat = 'ACCEPTED';
		} else {
			$partStat = 'DECLINED';
		}

		$vObject = Reader::read(vsprintf($message, [
			$partStat,
			$iTipMessage->recipient,
			$iTipMessage->sender,
			$uid,
			$sequence,
			$recurrenceId
		]));

		$responseITipMessage = new ITip\Message();
		$responseITipMessage->uid = $uid;
		$responseITipMessage->component = 'VEVENT';
		$responseITipMessage->method = 'REPLY';
		$responseITipMessage->sequence = $sequence;
		$responseITipMessage->sender = $iTipMessage->recipient;
		$responseITipMessage->recipient = $iTipMessage->sender;
		$responseITipMessage->message = $vObject;

		// We can't dispatch them now already, because the organizers calendar-object
		// was not yet created. Hence Sabre/DAV won't find a calendar-object, when we
		// send our reply.
		$this->schedulingResponses[] = $responseITipMessage;
	}

	/**
	 * @param string $uri
	 */
	public function dispatchSchedulingResponses(string $uri):void {
		if ($uri !== $this->pathOfCalendarObjectChange) {
			return;
		}

		foreach ($this->schedulingResponses as $schedulingResponse) {
			$this->scheduleLocalDelivery($schedulingResponse);
		}
	}

	/**
	 * Always use the personal calendar as target for scheduled events
	 *
	 * @param PropFind $propFind
	 * @param INode $node
	 * @return void
	 */
	public function propFindDefaultCalendarUrl(PropFind $propFind, INode $node) {
		if ($node instanceof IPrincipal) {
			$propFind->handle(self::SCHEDULE_DEFAULT_CALENDAR_URL, function () use ($node) {
				/** @var \OCA\DAV\CalDAV\Plugin $caldavPlugin */
				$caldavPlugin = $this->server->getPlugin('caldav');
				$principalUrl = $node->getPrincipalUrl();

				$calendarHomePath = $caldavPlugin->getCalendarHomeForPrincipal($principalUrl);
				if (!$calendarHomePath) {
					return null;
				}

				$isResourceOrRoom = str_starts_with($principalUrl, 'principals/calendar-resources') ||
					str_starts_with($principalUrl, 'principals/calendar-rooms');

				if (str_starts_with($principalUrl, 'principals/users')) {
					[, $userId] = split($principalUrl);
					$uri = $this->config->getUserValue($userId, 'dav', 'defaultCalendar', CalDavBackend::PERSONAL_CALENDAR_URI);
					$displayName = CalDavBackend::PERSONAL_CALENDAR_NAME;
				} elseif ($isResourceOrRoom) {
					$uri = CalDavBackend::RESOURCE_BOOKING_CALENDAR_URI;
					$displayName = CalDavBackend::RESOURCE_BOOKING_CALENDAR_NAME;
				} else {
					// How did we end up here?
					// TODO - throw exception or just ignore?
					return null;
				}

				/** @var CalendarHome $calendarHome */
				$calendarHome = $this->server->tree->getNodeForPath($calendarHomePath);
				$currentCalendarDeleted = false;
				if (!$calendarHome->childExists($uri) || $currentCalendarDeleted = $this->isCalendarDeleted($calendarHome, $uri)) {
					// If the default calendar doesn't exist
					if ($isResourceOrRoom) {
						// Resources or rooms can't be in the trashbin, so we're fine
						$this->createCalendar($calendarHome, $principalUrl, $uri, $displayName);
					} else {
						// And we're not handling scheduling on resource/room booking
						$userCalendars = [];
						/**
						 * If the default calendar of the user isn't set and the
						 * fallback doesn't match any of the user's calendar
						 * try to find the first "personal" calendar we can write to
						 * instead of creating a new one.
						 * A appropriate personal calendar to receive invites:
						 * - isn't a calendar subscription
						 * - user can write to it (no virtual/3rd-party calendars)
						 * - calendar isn't a share
						 * - calendar supports VEVENTs
						 */
						foreach ($calendarHome->getChildren() as $node) {
							if (!($node instanceof Calendar)) {
								continue;
							}

							try {
								$this->defaultCalendarValidator->validateScheduleDefaultCalendar($node);
							} catch (DavException $e) {
								continue;
							}

							$userCalendars[] = $node;
						}

						if (count($userCalendars) > 0) {
							// Calendar backend returns calendar by calendarorder property
							$uri = $userCalendars[0]->getName();
						} else {
							// Otherwise if we have really nothing, create a new calendar
							if ($currentCalendarDeleted) {
								// If the calendar exists but is deleted, we need to purge it first
								// This may cause some issues in a non synchronous database setup
								$calendar = $this->getCalendar($calendarHome, $uri);
								if ($calendar instanceof Calendar) {
									$calendar->disableTrashbin();
									$calendar->delete();
								}
							}
							$this->createCalendar($calendarHome, $principalUrl, $uri, $displayName);
						}
					}
				}

				$result = $this->server->getPropertiesForPath($calendarHomePath . '/' . $uri, [], 1);
				if (empty($result)) {
					return null;
				}

				return new LocalHref($result[0]['href']);
			});
		}
	}

	/**
	 * Returns a list of addresses that are associated with a principal.
	 *
	 * @param string $principal
	 * @return string|null
	 */
	protected function getCalendarUserTypeForPrincipal($principal):?string {
		$calendarUserType = '{' . self::NS_CALDAV . '}calendar-user-type';
		$properties = $this->server->getProperties(
			$principal,
			[$calendarUserType]
		);

		// If we can't find this information, we'll stop processing
		if (!isset($properties[$calendarUserType])) {
			return null;
		}

		return $properties[$calendarUserType];
	}

	/**
	 * @param ITip\Message $iTipMessage
	 * @return null|Property
	 */
	private function getCurrentAttendee(ITip\Message $iTipMessage):?Property {
		/** @var VEvent $vevent */
		$vevent = $iTipMessage->message->VEVENT;
		$attendees = $vevent->select('ATTENDEE');
		foreach ($attendees as $attendee) {
			/** @var Property $attendee */
			if (strcasecmp($attendee->getValue(), $iTipMessage->recipient) === 0) {
				return $attendee;
			}
		}
		return null;
	}

	/**
	 * @param Property|null $attendee
	 * @return bool
	 */
	private function getAttendeeRSVP(?Property $attendee = null):bool {
		if ($attendee !== null) {
			$rsvp = $attendee->offsetGet('RSVP');
			if (($rsvp instanceof Parameter) && (strcasecmp($rsvp->getValue(), 'TRUE') === 0)) {
				return true;
			}
		}
		// RFC 5545 3.2.17: default RSVP is false
		return false;
	}

	/**
	 * @param VEvent $vevent
	 * @return Property\ICalendar\DateTime
	 */
	private function getDTEndFromVEvent(VEvent $vevent):Property\ICalendar\DateTime {
		if (isset($vevent->DTEND)) {
			return $vevent->DTEND;
		}

		if (isset($vevent->DURATION)) {
			$isFloating = $vevent->DTSTART->isFloating();
			/** @var Property\ICalendar\DateTime $end */
			$end = clone $vevent->DTSTART;
			$endDateTime = $end->getDateTime();
			$endDateTime = $endDateTime->add(DateTimeParser::parse($vevent->DURATION->getValue()));
			$end->setDateTime($endDateTime, $isFloating);
			return $end;
		}

		if (!$vevent->DTSTART->hasTime()) {
			$isFloating = $vevent->DTSTART->isFloating();
			/** @var Property\ICalendar\DateTime $end */
			$end = clone $vevent->DTSTART;
			$endDateTime = $end->getDateTime();
			$endDateTime = $endDateTime->modify('+1 day');
			$end->setDateTime($endDateTime, $isFloating);
			return $end;
		}

		return clone $vevent->DTSTART;
	}

	/**
	 * @param string $email
	 * @param \DateTimeInterface $start
	 * @param \DateTimeInterface $end
	 * @param string $ignoreUID
	 * @return bool
	 */
	private function isAvailableAtTime(string $email, \DateTimeInterface $start, \DateTimeInterface $end, string $ignoreUID):bool {
		// This method is heavily inspired by Sabre\CalDAV\Schedule\Plugin::scheduleLocalDelivery
		// and Sabre\CalDAV\Schedule\Plugin::getFreeBusyForEmail

		$aclPlugin = $this->server->getPlugin('acl');
		$this->server->removeListener('propFind', [$aclPlugin, 'propFind']);

		$result = $aclPlugin->principalSearch(
			['{http://sabredav.org/ns}email-address' => $this->stripOffMailTo($email)],
			[
				'{DAV:}principal-URL',
				'{' . self::NS_CALDAV . '}calendar-home-set',
				'{' . self::NS_CALDAV . '}schedule-inbox-URL',
				'{http://sabredav.org/ns}email-address',

			]
		);
		$this->server->on('propFind', [$aclPlugin, 'propFind'], 20);


		// Grabbing the calendar list
		$objects = [];
		$calendarTimeZone = new DateTimeZone('UTC');

		$homePath = $result[0][200]['{' . self::NS_CALDAV . '}calendar-home-set']->getHref();
		/** @var Calendar $node */
		foreach ($this->server->tree->getNodeForPath($homePath)->getChildren() as $node) {
			
			if (!$node instanceof ICalendar) {
				continue;
			}

			// Getting the list of object uris within the time-range
			$urls = $node->calendarQuery([
				'name' => 'VCALENDAR',
				'comp-filters' => [
					[
						'name' => 'VEVENT',
						'is-not-defined' => false,
						'time-range' => [
							'start' => $start,
							'end' => $end,
						],
						'comp-filters' => [],
						'prop-filters' => [],
					],
					[
						'name' => 'VEVENT',
						'is-not-defined' => false,
						'time-range' => null,
						'comp-filters' => [],
						'prop-filters' => [
							[
								'name' => 'UID',
								'is-not-defined' => false,
								'time-range' => null,
								'text-match' => [
									'value' => $ignoreUID,
									'negate-condition' => true,
									'collation' => 'i;octet',
								],
								'param-filters' => [],
							],
						]
					],
				],
				'prop-filters' => [],
				'is-not-defined' => false,
				'time-range' => null,
			]);

			foreach ($urls as $url) {
				$objects[] = $node->getChild($url)->get();
			}
		}

		$inboxProps = $this->server->getProperties(
			$result[0][200]['{' . self::NS_CALDAV . '}schedule-inbox-URL']->getHref(),
			['{' . self::NS_CALDAV . '}calendar-availability']
		);

		$vcalendar = new VCalendar();
		$vcalendar->METHOD = 'REPLY';

		$generator = new FreeBusyGenerator();
		$generator->setObjects($objects);
		$generator->setTimeRange($start, $end);
		$generator->setBaseObject($vcalendar);
		$generator->setTimeZone($calendarTimeZone);

		if (isset($inboxProps['{' . self::NS_CALDAV . '}calendar-availability'])) {
			$generator->setVAvailability(
				Reader::read(
					$inboxProps['{' . self::NS_CALDAV . '}calendar-availability']
				)
			);
		}

		$result = $generator->getResult();
		if (!isset($result->VFREEBUSY)) {
			return false;
		}

		/** @var Component $freeBusyComponent */
		$freeBusyComponent = $result->VFREEBUSY;
		$freeBusyProperties = $freeBusyComponent->select('FREEBUSY');
		// If there is no Free-busy property at all, the time-range is empty and available
		if (count($freeBusyProperties) === 0) {
			return true;
		}

		// If more than one Free-Busy property was returned, it means that an event
		// starts or ends inside this time-range, so it's not available and we return false
		if (count($freeBusyProperties) > 1) {
			return false;
		}

		/** @var Property $freeBusyProperty */
		$freeBusyProperty = $freeBusyProperties[0];
		if (!$freeBusyProperty->offsetExists('FBTYPE')) {
			// If there is no FBTYPE, it means it's busy
			return false;
		}

		$fbTypeParameter = $freeBusyProperty->offsetGet('FBTYPE');
		if (!($fbTypeParameter instanceof Parameter)) {
			return false;
		}

		return (strcasecmp($fbTypeParameter->getValue(), 'FREE') === 0);
	}

	/**
	 * @param string $email
	 * @return string
	 */
	private function stripOffMailTo(string $email): string {
		if (stripos($email, 'mailto:') === 0) {
			return substr($email, 7);
		}

		return $email;
	}

	private function getCalendar(CalendarHome $calendarHome, string $uri): INode {
		return $calendarHome->getChild($uri);
	}

	private function isCalendarDeleted(CalendarHome $calendarHome, string $uri): bool {
		$calendar = $this->getCalendar($calendarHome, $uri);
		return $calendar instanceof Calendar && $calendar->isDeleted();
	}

	private function createCalendar(CalendarHome $calendarHome, string $principalUri, string $uri, string $displayName): void {
		$calendarHome->getCalDAVBackend()->createCalendar($principalUri, $uri, [
			'{DAV:}displayname' => $displayName,
		]);
	}

	/**
	 * Try to handle the given exception gracefully or throw it if necessary.
	 *
	 * @throws SameOrganizerForAllComponentsException If the exception should not be ignored
	 */
	private function handleSameOrganizerException(
		SameOrganizerForAllComponentsException $e,
		VCalendar $vCal,
		string $calendarPath,
	): void {
		// This is very hacky! However, we want to allow saving events with multiple
		// organizers. Those events are not RFC compliant, but sometimes imported from major
		// external calendar services (e.g. Google). If the current user is not an organizer of
		// the event we ignore the exception as no scheduling messages will be sent anyway.

		// It would be cleaner to patch Sabre to validate organizers *after* checking if
		// scheduling messages are necessary. Currently, organizers are validated first and
		// afterwards the broker checks if messages should be scheduled. So the code will throw
		// even if the organizers are not relevant. This is to ensure compliance with RFCs but
		// a bit too strict for real world usage.

		if (!isset($vCal->VEVENT)) {
			throw $e;
		}

		$calendarNode = $this->server->tree->getNodeForPath($calendarPath);
		if (!($calendarNode instanceof IACL)) {
			// Should always be an instance of IACL but just to be sure
			throw $e;
		}

		$addresses = $this->getAddressesForPrincipal($calendarNode->getOwner());
		foreach ($vCal->VEVENT as $vevent) {
			if (in_array($vevent->ORGANIZER->getNormalizedValue(), $addresses, true)) {
				// User is an organizer => throw the exception
				throw $e;
			}
		}
	}
}