Skip to content

Commit becf192

Browse files
committed
fix(scheduling): don't send iMIP emails to rooms / resources
Signed-off-by: Anna Larch <anna@nextcloud.com>
1 parent 9f196a5 commit becf192

File tree

3 files changed

+188
-12
lines changed

3 files changed

+188
-12
lines changed

apps/dav/lib/CalDAV/Schedule/IMipPlugin.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,18 @@
4141
use OCP\AppFramework\Utility\ITimeFactory;
4242
use OCP\Defaults;
4343
use OCP\IConfig;
44-
use OCP\IDBConnection;
45-
use OCP\IL10N;
46-
use OCP\IURLGenerator;
4744
use OCP\IUserManager;
48-
use OCP\L10N\IFactory as L10NFactory;
49-
use OCP\Mail\IEMailTemplate;
5045
use OCP\Mail\IMailer;
51-
use OCP\Security\ISecureRandom;
5246
use OCP\Util;
5347
use Psr\Log\LoggerInterface;
5448
use Sabre\CalDAV\Schedule\IMipPlugin as SabreIMipPlugin;
5549
use Sabre\DAV;
5650
use Sabre\DAV\INode;
5751
use Sabre\VObject\Component\VCalendar;
5852
use Sabre\VObject\Component\VEvent;
59-
use Sabre\VObject\Component\VTimeZone;
60-
use Sabre\VObject\DateTimeParser;
6153
use Sabre\VObject\ITip\Message;
6254
use Sabre\VObject\Parameter;
63-
use Sabre\VObject\Property;
6455
use Sabre\VObject\Reader;
65-
use Sabre\VObject\Recur\EventIterator;
6656

6757
/**
6858
* iMIP handler.
@@ -199,6 +189,20 @@ public function schedule(Message $iTipMessage) {
199189
// we also might not have an old event as this could be a new
200190
// invitation, or a new recurrence exception
201191
$attendee = $this->imipService->getCurrentAttendee($iTipMessage);
192+
if($attendee === null) {
193+
$uid = $vEvent->UID ?? 'no UID found';
194+
$this->logger->debug('Could not find recipient ' . $recipient . ' as attendee for event with UID ' . $uid);
195+
$iTipMessage->scheduleStatus = '5.0;EMail delivery failed';
196+
return;
197+
}
198+
// Don't send emails to things
199+
if($this->imipService->isRoomOrResource($attendee)) {
200+
$this->logger->debug('No invitation sent as recipient is room or resource', [
201+
'attendee' => $recipient,
202+
]);
203+
$iTipMessage->scheduleStatus = '1.0;We got the message, but it\'s not significant enough to warrant an email';
204+
return;
205+
}
202206
$this->imipService->setL10n($attendee);
203207

204208
// Build the sender name.

apps/dav/lib/CalDAV/Schedule/IMipService.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,4 +673,17 @@ public function getReplyingAttendee(Message $iTipMessage): ?Property {
673673
}
674674
return null;
675675
}
676+
677+
public function isRoomOrResource(Property $attendee): bool {
678+
$cuType = $attendee->offsetGet('CUTYPE');
679+
if(!$cuType instanceof Parameter) {
680+
return false;
681+
}
682+
$type = $cuType->getValue() ?? 'INDIVIDUAL';
683+
if (\in_array(strtoupper($type), ['RESOURCE', 'ROOM', 'UNKNOWN'], true)) {
684+
// Don't send emails to things
685+
return true;
686+
}
687+
return false;
688+
}
676689
}

apps/dav/tests/unit/CalDAV/Schedule/IMipPluginTest.php

Lines changed: 161 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use Sabre\VObject\Component\VCalendar;
4646
use Sabre\VObject\Component\VEvent;
4747
use Sabre\VObject\ITip\Message;
48+
use Sabre\VObject\Property;
4849
use Test\TestCase;
4950
use function array_merge;
5051

@@ -183,6 +184,13 @@ public function testParsingSingle(): void {
183184
'meeting_title' => 'Fellowship meeting without (!) Boromir',
184185
'attendee_name' => 'frodo@hobb.it'
185186
];
187+
$attendees = $newVevent->select('ATTENDEE');
188+
$atnd = '';
189+
foreach ($attendees as $attendee) {
190+
if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
191+
$atnd = $attendee;
192+
}
193+
}
186194
$this->plugin->setVCalendar($oldVCalendar);
187195
$this->service->expects(self::once())
188196
->method('getLastOccurrence')
@@ -194,6 +202,14 @@ public function testParsingSingle(): void {
194202
$this->eventComparisonService->expects(self::once())
195203
->method('findModified')
196204
->willReturn(['new' => [$newVevent], 'old' => [$oldVEvent]]);
205+
$this->service->expects(self::once())
206+
->method('getCurrentAttendee')
207+
->with($message)
208+
->willReturn($atnd);
209+
$this->service->expects(self::once())
210+
->method('isRoomOrResource')
211+
->with($atnd)
212+
->willReturn(false);
197213
$this->service->expects(self::once())
198214
->method('buildBodyData')
199215
->with($newVevent, $oldVEvent)
@@ -232,6 +248,91 @@ public function testParsingSingle(): void {
232248
$this->assertEquals('1.1', $message->getScheduleStatus());
233249
}
234250

251+
public function testAttendeeIsResource(): void {
252+
$message = new Message();
253+
$message->method = 'REQUEST';
254+
$newVCalendar = new VCalendar();
255+
$newVevent = new VEvent($newVCalendar, 'one', array_merge([
256+
'UID' => 'uid-1234',
257+
'SEQUENCE' => 1,
258+
'SUMMARY' => 'Fellowship meeting without (!) Boromir',
259+
'DTSTART' => new \DateTime('2016-01-01 00:00:00')
260+
], []));
261+
$newVevent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
262+
$newVevent->add('ATTENDEE', 'mailto:' . 'the-shire@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'The Shire', 'CUTYPE' => 'ROOM']);
263+
$message->message = $newVCalendar;
264+
$message->sender = 'mailto:gandalf@wiz.ard';
265+
$message->senderName = 'Mr. Wizard';
266+
$message->recipient = 'mailto:' . 'the-shire@hobb.it';
267+
// save the old copy in the plugin
268+
$oldVCalendar = new VCalendar();
269+
$oldVEvent = new VEvent($oldVCalendar, 'one', [
270+
'UID' => 'uid-1234',
271+
'SEQUENCE' => 0,
272+
'SUMMARY' => 'Fellowship meeting',
273+
'DTSTART' => new \DateTime('2016-01-01 00:00:00')
274+
]);
275+
$oldVEvent->add('ORGANIZER', 'mailto:gandalf@wiz.ard');
276+
$oldVEvent->add('ATTENDEE', 'mailto:' . 'the-shire@hobb.it', ['RSVP' => 'TRUE', 'CN' => 'The Shire', 'CUTYPE' => 'ROOM']);
277+
$oldVEvent->add('ATTENDEE', 'mailto:' . 'boromir@tra.it.or', ['RSVP' => 'TRUE']);
278+
$oldVCalendar->add($oldVEvent);
279+
$data = ['invitee_name' => 'Mr. Wizard',
280+
'meeting_title' => 'Fellowship meeting without (!) Boromir',
281+
'attendee_name' => 'frodo@hobb.it'
282+
];
283+
$attendees = $newVevent->select('ATTENDEE');
284+
$room = '';
285+
foreach ($attendees as $attendee) {
286+
if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
287+
$room = $attendee;
288+
}
289+
}
290+
$this->plugin->setVCalendar($oldVCalendar);
291+
$this->service->expects(self::once())
292+
->method('getLastOccurrence')
293+
->willReturn('1496912700');
294+
$this->mailer->expects(self::once())
295+
->method('validateMailAddress')
296+
->with('the-shire@hobb.it')
297+
->willReturn(true);
298+
$this->eventComparisonService->expects(self::once())
299+
->method('findModified')
300+
->willReturn(['new' => [$newVevent], 'old' => [$oldVEvent]]);
301+
$this->service->expects(self::once())
302+
->method('getCurrentAttendee')
303+
->with($message)
304+
->willReturn($room);
305+
$this->service->expects(self::once())
306+
->method('isRoomOrResource')
307+
->with($room)
308+
->willReturn(true);
309+
$this->service->expects(self::never())
310+
->method('buildBodyData');
311+
$this->userManager->expects(self::never())
312+
->method('getDisplayName');
313+
$this->service->expects(self::never())
314+
->method('getFrom');
315+
$this->service->expects(self::never())
316+
->method('addSubjectAndHeading');
317+
$this->service->expects(self::never())
318+
->method('addBulletList');
319+
$this->service->expects(self::never())
320+
->method('getAttendeeRsvpOrReqForParticipant');
321+
$this->config->expects(self::never())
322+
->method('getAppValue');
323+
$this->service->expects(self::never())
324+
->method('createInvitationToken');
325+
$this->service->expects(self::never())
326+
->method('addResponseButtons');
327+
$this->service->expects(self::never())
328+
->method('addMoreOptionsButton');
329+
$this->mailer->expects(self::never())
330+
->method('send');
331+
$this->plugin->schedule($message);
332+
$this->assertEquals('1.0', $message->getScheduleStatus());
333+
}
334+
335+
235336
public function testParsingRecurrence(): void {
236337
$message = new Message();
237338
$message->method = 'REQUEST';
@@ -274,6 +375,13 @@ public function testParsingRecurrence(): void {
274375
'meeting_title' => 'Elevenses',
275376
'attendee_name' => 'frodo@hobb.it'
276377
];
378+
$attendees = $newVevent->select('ATTENDEE');
379+
$atnd = '';
380+
foreach ($attendees as $attendee) {
381+
if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
382+
$atnd = $attendee;
383+
}
384+
}
277385
$this->plugin->setVCalendar($oldVCalendar);
278386
$this->service->expects(self::once())
279387
->method('getLastOccurrence')
@@ -285,6 +393,14 @@ public function testParsingRecurrence(): void {
285393
$this->eventComparisonService->expects(self::once())
286394
->method('findModified')
287395
->willReturn(['old' => [] ,'new' => [$newVevent]]);
396+
$this->service->expects(self::once())
397+
->method('getCurrentAttendee')
398+
->with($message)
399+
->willReturn($atnd);
400+
$this->service->expects(self::once())
401+
->method('isRoomOrResource')
402+
->with($atnd)
403+
->willReturn(false);
288404
$this->service->expects(self::once())
289405
->method('buildBodyData')
290406
->with($newVevent, null)
@@ -384,6 +500,13 @@ public function testFailedDelivery(): void {
384500
'meeting_title' => 'Fellowship meeting without (!) Boromir',
385501
'attendee_name' => 'frodo@hobb.it'
386502
];
503+
$attendees = $newVevent->select('ATTENDEE');
504+
$atnd = '';
505+
foreach ($attendees as $attendee) {
506+
if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
507+
$atnd = $attendee;
508+
}
509+
}
387510
$this->plugin->setVCalendar($oldVcalendar);
388511
$this->service->expects(self::once())
389512
->method('getLastOccurrence')
@@ -395,6 +518,14 @@ public function testFailedDelivery(): void {
395518
$this->eventComparisonService->expects(self::once())
396519
->method('findModified')
397520
->willReturn(['old' => [] ,'new' => [$newVevent]]);
521+
$this->service->expects(self::once())
522+
->method('getCurrentAttendee')
523+
->with($message)
524+
->willReturn($atnd);
525+
$this->service->expects(self::once())
526+
->method('isRoomOrResource')
527+
->with($atnd)
528+
->willReturn(false);
398529
$this->service->expects(self::once())
399530
->method('buildBodyData')
400531
->with($newVevent, null)
@@ -458,7 +589,13 @@ public function testNoOldEvent(): void {
458589
'meeting_title' => 'Fellowship meeting',
459590
'attendee_name' => 'frodo@hobb.it'
460591
];
461-
592+
$attendees = $newVevent->select('ATTENDEE');
593+
$atnd = '';
594+
foreach ($attendees as $attendee) {
595+
if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
596+
$atnd = $attendee;
597+
}
598+
}
462599
$this->service->expects(self::once())
463600
->method('getLastOccurrence')
464601
->willReturn('1496912700');
@@ -470,6 +607,14 @@ public function testNoOldEvent(): void {
470607
->method('findModified')
471608
->with($newVCalendar, null)
472609
->willReturn(['old' => [] ,'new' => [$newVevent]]);
610+
$this->service->expects(self::once())
611+
->method('getCurrentAttendee')
612+
->with($message)
613+
->willReturn($atnd);
614+
$this->service->expects(self::once())
615+
->method('isRoomOrResource')
616+
->with($atnd)
617+
->willReturn(false);
473618
$this->service->expects(self::once())
474619
->method('buildBodyData')
475620
->with($newVevent, null)
@@ -530,7 +675,13 @@ public function testNoButtons(): void {
530675
'meeting_title' => 'Fellowship meeting',
531676
'attendee_name' => 'frodo@hobb.it'
532677
];
533-
678+
$attendees = $newVevent->select('ATTENDEE');
679+
$atnd = '';
680+
foreach ($attendees as $attendee) {
681+
if (strcasecmp($attendee->getValue(), $message->recipient) === 0) {
682+
$atnd = $attendee;
683+
}
684+
}
534685
$this->service->expects(self::once())
535686
->method('getLastOccurrence')
536687
->willReturn('1496912700');
@@ -542,6 +693,14 @@ public function testNoButtons(): void {
542693
->method('findModified')
543694
->with($newVCalendar, null)
544695
->willReturn(['old' => [] ,'new' => [$newVevent]]);
696+
$this->service->expects(self::once())
697+
->method('getCurrentAttendee')
698+
->with($message)
699+
->willReturn($atnd);
700+
$this->service->expects(self::once())
701+
->method('isRoomOrResource')
702+
->with($atnd)
703+
->willReturn(false);
545704
$this->service->expects(self::once())
546705
->method('buildBodyData')
547706
->with($newVevent, null)

0 commit comments

Comments
 (0)