From b3550db02dd24eb435789991a95db7ddb053fd5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Fri, 18 Dec 2015 11:56:25 +0100 Subject: [PATCH] Adding IMip scheduling which uses ownClouds mail delivery --- apps/dav/lib/caldav/schedule/imipplugin.php | 110 ++++++++++++++++++ apps/dav/lib/server.php | 6 +- .../unit/caldav/schedule/imipplugintest.php | 91 +++++++++++++++ lib/private/mail/message.php | 10 ++ 4 files changed, 214 insertions(+), 3 deletions(-) create mode 100644 apps/dav/lib/caldav/schedule/imipplugin.php create mode 100644 apps/dav/tests/unit/caldav/schedule/imipplugintest.php diff --git a/apps/dav/lib/caldav/schedule/imipplugin.php b/apps/dav/lib/caldav/schedule/imipplugin.php new file mode 100644 index 0000000000..eafdbe9c1b --- /dev/null +++ b/apps/dav/lib/caldav/schedule/imipplugin.php @@ -0,0 +1,110 @@ +mailer = $mailer; + $this->logger = $logger; + } + + /** + * Event handler for the 'schedule' event. + * + * @param ITip\Message $iTipMessage + * @return void + */ + function schedule(ITip\Message $iTipMessage) { + + // Not sending any emails if the system considers the update + // insignificant. + if (!$iTipMessage->significantChange) { + if (!$iTipMessage->scheduleStatus) { + $iTipMessage->scheduleStatus = '1.0;We got the message, but it\'s not significant enough to warrant an email'; + } + return; + } + + $summary = $iTipMessage->message->VEVENT->SUMMARY; + + if (parse_url($iTipMessage->sender, PHP_URL_SCHEME) !== 'mailto') { + return; + } + + if (parse_url($iTipMessage->recipient, PHP_URL_SCHEME) !== 'mailto') { + return; + } + + $sender = substr($iTipMessage->sender, 7); + $recipient = substr($iTipMessage->recipient, 7); + + $senderName = ($iTipMessage->senderName) ? $iTipMessage->senderName : null; + $recipientName = ($iTipMessage->recipientName) ? $iTipMessage->recipientName : null; + + $subject = 'SabreDAV iTIP message'; + switch (strtoupper($iTipMessage->method)) { + case 'REPLY' : + $subject = 'Re: ' . $summary; + break; + case 'REQUEST' : + $subject = $summary; + break; + case 'CANCEL' : + $subject = 'Cancelled: ' . $summary; + break; + } + + $contentType = 'text/calendar; charset=UTF-8; method=' . $iTipMessage->method; + + $message = $this->mailer->createMessage(); + + $message->setReplyTo([$sender => $senderName]) + ->setTo([$recipient => $recipientName]) + ->setSubject($subject) + ->setBody($iTipMessage->message->serialize(), $contentType); + try { + $failed = $this->mailer->send($message); + if ($failed) { + $this->logger->error('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' => implode(', ', $failed)]); + $iTipMessage->scheduleStatus = '5.0; EMail delivery failed'; + } + $iTipMessage->scheduleStatus = '1.1; Scheduling message is sent via iMip'; + } catch(\Exception $ex) { + $this->logger->logException($ex, ['app' => 'dav']); + $iTipMessage->scheduleStatus = '5.0; EMail delivery failed'; + } + } + +} diff --git a/apps/dav/lib/server.php b/apps/dav/lib/server.php index ffcbb02db7..a6ad878d29 100644 --- a/apps/dav/lib/server.php +++ b/apps/dav/lib/server.php @@ -2,12 +2,12 @@ namespace OCA\DAV; +use OCA\DAV\CalDAV\Schedule\IMipPlugin; use OCA\DAV\Connector\Sabre\Auth; use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin; use OCA\DAV\Files\CustomPropertiesBackend; use OCP\IRequest; use Sabre\DAV\Auth\Plugin; -use Sabre\HTTP\Util; class Server { @@ -19,6 +19,7 @@ class Server { $this->baseUri = $baseUri; $logger = \OC::$server->getLogger(); $dispatcher = \OC::$server->getEventDispatcher(); + $mailer = \OC::$server->getMailer(); $root = new RootCollection(); $this->server = new \OCA\DAV\Connector\Sabre\Server($root); @@ -49,9 +50,8 @@ class Server { // calendar plugins $this->server->addPlugin(new \Sabre\CalDAV\Plugin()); $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin()); - $senderEmail = \OCP\Util::getDefaultEmailAddress('no-reply'); $this->server->addPlugin(new \Sabre\CalDAV\Schedule\Plugin()); - $this->server->addPlugin(new \Sabre\CalDAV\Schedule\IMipPlugin($senderEmail)); + $this->server->addPlugin(new IMipPlugin($mailer, $logger)); $this->server->addPlugin(new \Sabre\CalDAV\SharingPlugin()); $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin()); $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin()); diff --git a/apps/dav/tests/unit/caldav/schedule/imipplugintest.php b/apps/dav/tests/unit/caldav/schedule/imipplugintest.php new file mode 100644 index 0000000000..49059e93ae --- /dev/null +++ b/apps/dav/tests/unit/caldav/schedule/imipplugintest.php @@ -0,0 +1,91 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\DAV\Tests\Unit\CalDAV\Schedule; + +use OC\Mail\Mailer; +use OCA\DAV\CalDAV\Schedule\IMipPlugin; +use OCP\ILogger; +use Sabre\VObject\Component\VCalendar; +use Sabre\VObject\ITip\Message; +use Test\TestCase; + +class IMipPluginTest extends TestCase { + + public function testDelivery() { + $mailMessage = new \OC\Mail\Message(new \Swift_Message()); + /** @var Mailer | \PHPUnit_Framework_MockObject_MockObject $mailer */ + $mailer = $this->getMockBuilder('OC\Mail\Mailer')->disableOriginalConstructor()->getMock(); + $mailer->method('createMessage')->willReturn($mailMessage); + $mailer->expects($this->once())->method('send'); + /** @var ILogger | \PHPUnit_Framework_MockObject_MockObject $logger */ + $logger = $this->getMockBuilder('OC\Log')->disableOriginalConstructor()->getMock(); + + $plugin = new IMipPlugin($mailer, $logger); + $message = new Message(); + $message->method = 'REQUEST'; + $message->message = new VCalendar(); + $message->message->add('VEVENT', [ + 'UID' => $message->uid, + 'SEQUENCE' => $message->sequence, + 'SUMMARY' => 'Fellowship meeting', + ]); + $message->sender = 'mailto:gandalf@wiz.ard'; + $message->recipient = 'mailto:frodo@hobb.it'; + + $plugin->schedule($message); + $this->assertEquals('1.1', $message->getScheduleStatus()); + $this->assertEquals('Fellowship meeting', $mailMessage->getSubject()); + $this->assertEquals(['frodo@hobb.it' => null], $mailMessage->getTo()); + $this->assertEquals(['gandalf@wiz.ard' => null], $mailMessage->getReplyTo()); + $this->assertEquals('text/calendar; charset=UTF-8; method=REQUEST', $mailMessage->getSwiftMessage()->getContentType()); + } + + public function testFailedDelivery() { + $mailMessage = new \OC\Mail\Message(new \Swift_Message()); + /** @var Mailer | \PHPUnit_Framework_MockObject_MockObject $mailer */ + $mailer = $this->getMockBuilder('OC\Mail\Mailer')->disableOriginalConstructor()->getMock(); + $mailer->method('createMessage')->willReturn($mailMessage); + $mailer->method('send')->willThrowException(new \Exception()); + /** @var ILogger | \PHPUnit_Framework_MockObject_MockObject $logger */ + $logger = $this->getMockBuilder('OC\Log')->disableOriginalConstructor()->getMock(); + + $plugin = new IMipPlugin($mailer, $logger); + $message = new Message(); + $message->method = 'REQUEST'; + $message->message = new VCalendar(); + $message->message->add('VEVENT', [ + 'UID' => $message->uid, + 'SEQUENCE' => $message->sequence, + 'SUMMARY' => 'Fellowship meeting', + ]); + $message->sender = 'mailto:gandalf@wiz.ard'; + $message->recipient = 'mailto:frodo@hobb.it'; + + $plugin->schedule($message); + $this->assertEquals('5.0', $message->getScheduleStatus()); + $this->assertEquals('Fellowship meeting', $mailMessage->getSubject()); + $this->assertEquals(['frodo@hobb.it' => null], $mailMessage->getTo()); + $this->assertEquals(['gandalf@wiz.ard' => null], $mailMessage->getReplyTo()); + $this->assertEquals('text/calendar; charset=UTF-8; method=REQUEST', $mailMessage->getSwiftMessage()->getContentType()); + } + +} diff --git a/lib/private/mail/message.php b/lib/private/mail/message.php index 0fdca49e6d..98a95e4e58 100644 --- a/lib/private/mail/message.php +++ b/lib/private/mail/message.php @@ -238,4 +238,14 @@ class Message { public function getSwiftMessage() { return $this->swiftMessage; } + + /** + * @param string $body + * @param string $contentType + * @return $this + */ + public function setBody($body, $contentType) { + $this->swiftMessage->setBody($body, $contentType); + return $this; + } }