|
12 | 12 | use OCP\AppFramework\Utility\ITimeFactory; |
13 | 13 | use OCP\Calendar\Exceptions\CalendarException; |
14 | 14 | use OCP\Calendar\ICalendar; |
| 15 | +use OCP\Calendar\ICalendarIsShared; |
| 16 | +use OCP\Calendar\ICalendarIsWritable; |
15 | 17 | use OCP\Calendar\ICalendarProvider; |
16 | 18 | use OCP\Calendar\ICalendarQuery; |
17 | 19 | use OCP\Calendar\ICreateFromString; |
@@ -204,6 +206,87 @@ public function newQuery(string $principalUri): ICalendarQuery { |
204 | 206 | return new CalendarQuery($principalUri); |
205 | 207 | } |
206 | 208 |
|
| 209 | + /** |
| 210 | + * @since 31.0.0 |
| 211 | + * @throws \OCP\DB\Exception |
| 212 | + */ |
| 213 | + public function handleIMipRequest( |
| 214 | + string $principalUri, |
| 215 | + string $sender, |
| 216 | + string $recipient, |
| 217 | + string $calendarData, |
| 218 | + ): bool { |
| 219 | + |
| 220 | + $userCalendars = $this->getCalendarsForPrincipal($principalUri); |
| 221 | + if (empty($userCalendars)) { |
| 222 | + $this->logger->warning('iMip message could not be processed because user has no calendars'); |
| 223 | + return false; |
| 224 | + } |
| 225 | + |
| 226 | + /** @var VCalendar $vObject|null */ |
| 227 | + $calendarObject = Reader::read($calendarData); |
| 228 | + |
| 229 | + if (!isset($calendarObject->METHOD) || $calendarObject->METHOD->getValue() !== 'REQUEST') { |
| 230 | + $this->logger->warning('iMip message contains an incorrect or invalid method'); |
| 231 | + return false; |
| 232 | + } |
| 233 | + |
| 234 | + if (!isset($calendarObject->VEVENT)) { |
| 235 | + $this->logger->warning('iMip message contains no event'); |
| 236 | + return false; |
| 237 | + } |
| 238 | + |
| 239 | + $eventObject = $calendarObject->VEVENT; |
| 240 | + |
| 241 | + if (!isset($eventObject->UID)) { |
| 242 | + $this->logger->warning('iMip message event dose not contains a UID'); |
| 243 | + return false; |
| 244 | + } |
| 245 | + |
| 246 | + if (!isset($eventObject->ATTENDEE)) { |
| 247 | + $this->logger->warning('iMip message event dose not contains any attendees'); |
| 248 | + return false; |
| 249 | + } |
| 250 | + |
| 251 | + foreach ($eventObject->ATTENDEE as $entry) { |
| 252 | + $address = trim(str_replace('mailto:', '', $entry->getValue())); |
| 253 | + if ($address === $recipient) { |
| 254 | + $attendee = $address; |
| 255 | + break; |
| 256 | + } |
| 257 | + } |
| 258 | + if (!isset($attendee)) { |
| 259 | + $this->logger->warning('iMip message event does not contain a attendee that matches the recipient'); |
| 260 | + return false; |
| 261 | + } |
| 262 | + |
| 263 | + foreach ($userCalendars as $calendar) { |
| 264 | + |
| 265 | + if (!$calendar instanceof ICalendarIsWritable && !$calendar instanceof ICalendarIsShared) { |
| 266 | + continue; |
| 267 | + } |
| 268 | + |
| 269 | + if ($calendar->isDeleted() || !$calendar->isWritable() || $calendar->isShared()) { |
| 270 | + continue; |
| 271 | + } |
| 272 | + |
| 273 | + if (!empty($calendar->search($recipient, ['ATTENDEE'], ['uid' => $eventObject->UID->getValue()]))) { |
| 274 | + try { |
| 275 | + if ($calendar instanceof IHandleImipMessage) { |
| 276 | + $calendar->handleIMipMessage('', $calendarData); |
| 277 | + } |
| 278 | + return true; |
| 279 | + } catch (CalendarException $e) { |
| 280 | + $this->logger->error('An error occurred while processing the iMip message event', ['exception' => $e]); |
| 281 | + return false; |
| 282 | + } |
| 283 | + } |
| 284 | + } |
| 285 | + |
| 286 | + $this->logger->warning('iMip message event could not be processed because the no corresponding event was found in any calendar'); |
| 287 | + return false; |
| 288 | + } |
| 289 | + |
207 | 290 | /** |
208 | 291 | * @throws \OCP\DB\Exception |
209 | 292 | */ |
|
0 commit comments