Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support iMIP invitations from Mail #33001

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/dav/lib/CalDAV/CalDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,10 @@ public function search(array $calendarInfo, $pattern, array $searchProperties,
}
}

if(isset($options['uid'])) {
$outerQuery->andWhere($outerQuery->expr()->eq('uid', $outerQuery->createNamedParameter($options['uid'])));
}

if (!empty($options['types'])) {
$or = $outerQuery->expr()->orX();
foreach ($options['types'] as $type) {
Expand Down
1 change: 0 additions & 1 deletion apps/dav/lib/CalDAV/CalendarHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ public function calendarSearch(array $filters, $limit = null, $offset = null) {
return $this->caldavBackend->calendarSearch($principalUri, $filters, $limit, $offset);
}


public function enableCachedSubscriptionsForThisRequest() {
$this->returnCachedSubscriptions = true;
}
Expand Down
71 changes: 64 additions & 7 deletions apps/dav/lib/CalDAV/CalendarImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@

use OCA\DAV\CalDAV\Auth\CustomPrincipalPlugin;
use OCA\DAV\CalDAV\InvitationResponse\InvitationResponseServer;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Calendar\Exceptions\CalendarException;
use OCP\Calendar\ICreateFromString;
use OCP\Constants;
use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Exception\Conflict;
use Sabre\VObject\Component\VCalendar;
use Sabre\VObject\Component\VEvent;
use Sabre\VObject\Document;
use Sabre\VObject\ITip\Message;
use Sabre\VObject\Property\VCard\DateTime;
use Sabre\VObject\Reader;
use function Sabre\Uri\split as uriSplit;

class CalendarImpl implements ICreateFromString {
Expand All @@ -46,13 +55,6 @@ class CalendarImpl implements ICreateFromString {
/** @var array */
private $calendarInfo;

/**
* CalendarImpl constructor.
*
* @param Calendar $calendar
* @param array $calendarInfo
* @param CalDavBackend $backend
*/
public function __construct(Calendar $calendar,
array $calendarInfo,
CalDavBackend $backend) {
Expand Down Expand Up @@ -177,4 +179,59 @@ public function createFromString(string $name, string $calendarData): void {
fclose($stream);
}
}

/**
* @throws CalendarException
*/
public function handleIMipMessage(string $name, string $calendarData): void {
$server = new InvitationResponseServer(false);

/** @var CustomPrincipalPlugin $plugin */
$plugin = $server->server->getPlugin('auth');
// we're working around the previous implementation
// that only allowed the public system principal to be used
// so set the custom principal here
$plugin->setCurrentPrincipal($this->calendar->getPrincipalURI());

if (empty($this->calendarInfo['uri'])) {
throw new CalendarException('Could not write to calendar as URI parameter is missing');
}
// Force calendar change URI
/** @var Schedule\Plugin $schedulingPlugin */
$schedulingPlugin = $server->server->getPlugin('caldav-schedule');
// Let sabre handle the rest
$iTipMessage = new Message();
/** @var VCalendar $vObject */
$vObject = Reader::read($calendarData);
/** @var VEvent $vEvent */
$vEvent = $vObject->{'VEVENT'};

if($vObject->{'METHOD'} === null) {
throw new CalendarException('No Method provided for scheduling data. Could not process message');
}

if(!isset($vEvent->{'ORGANIZER'}) || !isset($vEvent->{'ATTENDEE'})) {
throw new CalendarException('Could not process scheduling data, neccessary data missing from ICAL');
}
$orgaizer = $vEvent->{'ORGANIZER'}->getValue();
$attendee = $vEvent->{'ATTENDEE'}->getValue();

$iTipMessage->method = $vObject->{'METHOD'}->getValue();
if($iTipMessage->method === 'REPLY') {
if ($server->isExternalAttendee($vEvent->{'ATTENDEE'}->getValue())) {
Fixed Show fixed Hide fixed
$iTipMessage->recipient = $orgaizer;
} else {
$iTipMessage->recipient = $attendee;
}
$iTipMessage->sender = $attendee;
} else if($iTipMessage->method === 'CANCEL') {
$iTipMessage->recipient = $attendee;
$iTipMessage->sender = $orgaizer;
}
$iTipMessage->uid = isset($vEvent->{'UID'}) ? $vEvent->{'UID'}->getValue() : '';
$iTipMessage->component = 'VEVENT';
$iTipMessage->sequence = isset($vEvent->{'SEQUENCE'}) ? (int)$vEvent->{'SEQUENCE'}->getValue() : 0;
$iTipMessage->message = $vObject;
$schedulingPlugin->scheduleLocalDelivery($iTipMessage);
}
}
1 change: 1 addition & 0 deletions apps/dav/lib/CalDAV/CalendarProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace OCA\DAV\CalDAV;

use OCP\Calendar\ICalendarProvider;
use OCP\Calendar\ICreateFromString;
use OCP\IConfig;
use OCP\IL10N;
use Psr\Log\LoggerInterface;
Expand Down
79 changes: 79 additions & 0 deletions apps/dav/tests/unit/CalDAV/CalendarImplTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@
*/
namespace OCA\DAV\Tests\unit\CalDAV;

use OCA\DAV\CalDAV\Auth\CustomPrincipalPlugin;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\Calendar;
use OCA\DAV\CalDAV\CalendarImpl;
use OCA\DAV\CalDAV\InvitationResponse\InvitationResponseServer;
use OCA\DAV\CalDAV\Schedule\Plugin;
use PHPUnit\Framework\MockObject\MockObject;

class CalendarImplTest extends \Test\TestCase {

Expand All @@ -51,6 +55,7 @@ protected function setUp(): void {
'id' => 'fancy_id_123',
'{DAV:}displayname' => 'user readable name 123',
'{http://apple.com/ns/ical/}calendar-color' => '#AABBCC',
'uri' => '/this/is/a/uri'
];
$this->backend = $this->createMock(CalDavBackend::class);

Expand Down Expand Up @@ -125,4 +130,78 @@ public function testGetPermissionAll() {

$this->assertEquals(31, $this->calendarImpl->getPermissions());
}

public function testHandleImipMessage(): void {
$invitationResponseServer = $this->createConfiguredMock(InvitationResponseServer::class, [
'server' => $this->createConfiguredMock(CalDavBackend::class, [
'getPlugin' => [
'auth' => $this->createMock(CustomPrincipalPlugin::class),
'schedule' => $this->createMock(Plugin::class)
]
])
]);

$message = <<<EOF
BEGIN:VCALENDAR
PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
METHOD:REPLY
VERSION:2.0
BEGIN:VEVENT
ATTENDEE;PARTSTAT=mailto:lewis@stardew-tent-living.com:ACCEPTED
ORGANIZER:mailto:pierre@generalstore.com
UID:aUniqueUid
SEQUENCE:2
REQUEST-STATUS:2.0;Success
%sEND:VEVENT
END:VCALENDAR
EOF;

/** @var CustomPrincipalPlugin|MockObject $authPlugin */
$authPlugin = $invitationResponseServer->server->getPlugin('auth');
$authPlugin->expects(self::once())
->method('setPrincipalUri')
->with($this->calendar->getPrincipalURI());

/** @var Plugin|MockObject $schedulingPlugin */
$schedulingPlugin = $invitationResponseServer->server->getPlugin('caldav-schedule');
$schedulingPlugin->expects(self::once())
->method('setPathOfCalendarObjectChange')
->with('fullcalendarname');
}

public function testHandleImipMessageNoCalendarUri(): void {
$invitationResponseServer = $this->createConfiguredMock(InvitationResponseServer::class, [
'server' => $this->createConfiguredMock(CalDavBackend::class, [
'getPlugin' => [
'auth' => $this->createMock(CustomPrincipalPlugin::class),
'schedule' => $this->createMock(Plugin::class)
]
])
]);

$message = <<<EOF
BEGIN:VCALENDAR
PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
METHOD:REPLY
VERSION:2.0
BEGIN:VEVENT
ATTENDEE;PARTSTAT=mailto:lewis@stardew-tent-living.com:ACCEPTED
ORGANIZER:mailto:pierre@generalstore.com
UID:aUniqueUid
SEQUENCE:2
REQUEST-STATUS:2.0;Success
%sEND:VEVENT
END:VCALENDAR
EOF;

/** @var CustomPrincipalPlugin|MockObject $authPlugin */
$authPlugin = $invitationResponseServer->server->getPlugin('auth');
$authPlugin->expects(self::once())
->method('setPrincipalUri')
->with($this->calendar->getPrincipalURI());

unset($this->calendarInfo['uri']);
$this->expectException('CalendarException');
$this->calendarImpl->handleIMipMessage('filename.ics', $message);
}
}
Loading