]> source.dussan.org Git - nextcloud-server.git/commitdiff
Added Hook before Message is send 14722/head
authorArne Hamann <kontakt+github@arne.email>
Sat, 16 Mar 2019 01:29:03 +0000 (02:29 +0100)
committerChristoph Wurst <christoph@winzerhof-wurst.at>
Tue, 24 Mar 2020 19:57:27 +0000 (20:57 +0100)
Signed-off-by: Arne Hamann <kontakt+github@arne.email>
lib/composer/composer/autoload_classmap.php
lib/composer/composer/autoload_static.php
lib/private/Mail/Mailer.php
lib/private/Mail/Message.php
lib/private/Server.php
lib/public/Mail/Events/BeforeMessageSent.php [new file with mode: 0644]
tests/lib/Mail/MailerTest.php
tests/lib/Mail/MessageTest.php

index 0595abb7353729dae78989fab8b302a7fdc510f6..94addec59c6b4edcc0aac526c1736fd8d51373c2 100644 (file)
@@ -372,6 +372,7 @@ return array(
     'OCP\\Log\\ILogFactory' => $baseDir . '/lib/public/Log/ILogFactory.php',
     'OCP\\Log\\IWriter' => $baseDir . '/lib/public/Log/IWriter.php',
     'OCP\\Log\\RotationTrait' => $baseDir . '/lib/public/Log/RotationTrait.php',
+    'OCP\\Mail\\Events\\BeforeMessageSent' => $baseDir . '/lib/public/Mail/Events/BeforeMessageSent.php',
     'OCP\\Mail\\IAttachment' => $baseDir . '/lib/public/Mail/IAttachment.php',
     'OCP\\Mail\\IEMailTemplate' => $baseDir . '/lib/public/Mail/IEMailTemplate.php',
     'OCP\\Mail\\IMailer' => $baseDir . '/lib/public/Mail/IMailer.php',
index d9739745d8fa0fd9bab3979c7617d9854d006bcc..d8f68ccd5ea388b5ba6385ba86e6e13dbc39cc82 100644 (file)
@@ -401,6 +401,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
         'OCP\\Log\\ILogFactory' => __DIR__ . '/../../..' . '/lib/public/Log/ILogFactory.php',
         'OCP\\Log\\IWriter' => __DIR__ . '/../../..' . '/lib/public/Log/IWriter.php',
         'OCP\\Log\\RotationTrait' => __DIR__ . '/../../..' . '/lib/public/Log/RotationTrait.php',
+        'OCP\\Mail\\Events\\BeforeMessageSent' => __DIR__ . '/../../..' . '/lib/public/Mail/Events/BeforeMessageSent.php',
         'OCP\\Mail\\IAttachment' => __DIR__ . '/../../..' . '/lib/public/Mail/IAttachment.php',
         'OCP\\Mail\\IEMailTemplate' => __DIR__ . '/../../..' . '/lib/public/Mail/IEMailTemplate.php',
         'OCP\\Mail\\IMailer' => __DIR__ . '/../../..' . '/lib/public/Mail/IMailer.php',
index df5f2687daa6b6fd00979d2cf1b0becf7d06436e..47a7f8a7c9d150a283a4ef2492337a5953ab6f7a 100644 (file)
@@ -12,6 +12,7 @@ declare(strict_types=1);
  * @author Lukas Reschke <lukas@statuscode.ch>
  * @author Morris Jobke <hey@morrisjobke.de>
  * @author Roeland Jago Douma <roeland@famdouma.nl>
+ * @author Arne Hamann <github@arne.email>
  *
  * @license AGPL-3.0
  *
@@ -34,6 +35,7 @@ namespace OC\Mail;
 use Egulias\EmailValidator\EmailValidator;
 use Egulias\EmailValidator\Validation\RFCValidation;
 use OCP\Defaults;
+use OCP\EventDispatcher\IEventDispatcher;
 use OCP\IConfig;
 use OCP\IL10N;
 use OCP\ILogger;
@@ -42,6 +44,8 @@ use OCP\Mail\IAttachment;
 use OCP\Mail\IEMailTemplate;
 use OCP\Mail\IMailer;
 use OCP\Mail\IMessage;
+use OCP\Mail\Events\BeforeMessageSent;
+
 
 /**
  * Class Mailer provides some basic functions to create a mail message that can be used in combination with
@@ -74,6 +78,8 @@ class Mailer implements IMailer {
        private $urlGenerator;
        /** @var IL10N */
        private $l10n;
+       /** @var IEventDispatcher */
+       private $dispatcher;
 
        /**
         * @param IConfig $config
@@ -81,17 +87,20 @@ class Mailer implements IMailer {
         * @param Defaults $defaults
         * @param IURLGenerator $urlGenerator
         * @param IL10N $l10n
+        * @param IEventDispatcher $dispatcher
         */
        public function __construct(IConfig $config,
                                                 ILogger $logger,
                                                 Defaults $defaults,
                                                 IURLGenerator $urlGenerator,
-                                                IL10N $l10n) {
+                                                IL10N $l10n,
+                                                IEventDispatcher $dispatcher) {
                $this->config = $config;
                $this->logger = $logger;
                $this->defaults = $defaults;
                $this->urlGenerator = $urlGenerator;
                $this->l10n = $l10n;
+               $this->dispatcher = $dispatcher;
        }
 
        /**
@@ -182,6 +191,9 @@ class Mailer implements IMailer {
                        $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger));
                }
 
+
+               $this->dispatcher->dispatchTyped(new BeforeMessageSent($message));
+
                $mailer->send($message->getSwiftMessage(), $failedRecipients);
 
                // Debugging logging
index 4ea9da532b6ebc8592fe984de5a966dd36eb12b0..9437bd0216f707d5a065d2bd0235e87d0570bee6 100644 (file)
@@ -11,6 +11,7 @@ declare(strict_types=1);
  * @author Morris Jobke <hey@morrisjobke.de>
  * @author Roeland Jago Douma <roeland@famdouma.nl>
  * @author Thomas Müller <thomas.mueller@tmit.eu>
+ * @author Arne Hamann <github@arne.email>
  *
  * @license AGPL-3.0
  *
@@ -112,7 +113,7 @@ class Message implements IMessage {
         * @return array
         */
        public function getFrom(): array {
-               return $this->swiftMessage->getFrom();
+               return $this->swiftMessage->getFrom() ?? [];
        }
 
        /**
@@ -156,7 +157,7 @@ class Message implements IMessage {
         * @return array
         */
        public function getTo(): array {
-               return $this->swiftMessage->getTo();
+               return $this->swiftMessage->getTo() ?? [];
        }
 
        /**
@@ -178,7 +179,7 @@ class Message implements IMessage {
         * @return array
         */
        public function getCc(): array {
-               return $this->swiftMessage->getCc();
+               return $this->swiftMessage->getCc() ?? [];
        }
 
        /**
@@ -200,7 +201,7 @@ class Message implements IMessage {
         * @return array
         */
        public function getBcc(): array {
-               return $this->swiftMessage->getBcc();
+               return $this->swiftMessage->getBcc() ?? [];
        }
 
        /**
@@ -256,6 +257,14 @@ class Message implements IMessage {
                return $this;
        }
 
+       /**
+        * Get's the underlying SwiftMessage
+        * @param Swift_Message $swiftMessage
+        */
+       public function setSwiftMessage(Swift_Message $swiftMessage): void {
+               $this->swiftMessage = $swiftMessage;
+       }
+
        /**
         * Get's the underlying SwiftMessage
         * @return Swift_Message
index 3300920edb7ac9933bc105152fd020a1d461de0d..971b144e1d0fcba66c96ce8913c01e0416711507 100644 (file)
@@ -988,7 +988,8 @@ class Server extends ServerContainer implements IServerContainer {
                                $c->getLogger(),
                                $c->query(Defaults::class),
                                $c->getURLGenerator(),
-                               $c->getL10N('lib')
+                               $c->getL10N('lib'),
+                               $c->query(IEventDispatcher::class)
                        );
                });
                $this->registerDeprecatedAlias('Mailer', IMailer::class);
diff --git a/lib/public/Mail/Events/BeforeMessageSent.php b/lib/public/Mail/Events/BeforeMessageSent.php
new file mode 100644 (file)
index 0000000..e14a357
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright 2020 Arne Hamann <github@arne.email>
+ *
+ * @author Arne Hamann <github@arne.email>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCP\Mail\Events;
+
+use OCP\EventDispatcher\Event;
+use OCP\Mail\IMessage;
+
+/**
+ * @since 19.0.0
+ */
+class BeforeMessageSent extends Event {
+
+       /** @var IMessage */
+       private $message;
+
+       /**
+        * @param IMessage $message
+        * @since 19.0.0
+        */
+       public function __construct(IMessage $message) {
+               parent::__construct();
+               $this->message = $message;
+       }
+
+       /**
+        * @return IMessage
+        * @since 19.0.0
+        */
+       public function getMessage(): IMessage {
+               return $this->message;
+       }
+
+}
index 3a08cd9acf2a08a445345c3e251f2d3946195f62..1d19929fe00863b9ac3392f2397460ac101349cc 100644 (file)
@@ -1,6 +1,9 @@
 <?php
 /**
  * Copyright (c) 2014-2015 Lukas Reschke <lukas@owncloud.com>
+ *
+ * @author Arne Hamann <github@arne.email>
+ *
  * This file is licensed under the Affero General Public License version 3 or
  * later.
  * See the COPYING-README file.
 
 namespace Test\Mail;
 
+
 use OC\Mail\EMailTemplate;
 use OC\Mail\Mailer;
+use OC\Mail\Message;
 use OCP\Defaults;
+use OCP\EventDispatcher\IEventDispatcher;
 use OCP\IConfig;
 use OCP\IL10N;
 use OCP\ILogger;
 use OCP\IURLGenerator;
+use OCP\Mail\Events\BeforeMessageSent;
+use OCP\Mail\IMessage;
 use Test\TestCase;
+use Swift_SwiftException;
 
 class MailerTest extends TestCase {
        /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
@@ -30,6 +39,9 @@ class MailerTest extends TestCase {
        private $l10n;
        /** @var Mailer */
        private $mailer;
+       /** @var IEventDispatcher */
+       private $dispatcher;
+
 
        protected function setUp(): void {
                parent::setUp();
@@ -39,12 +51,14 @@ class MailerTest extends TestCase {
                $this->logger = $this->createMock(ILogger::class);
                $this->urlGenerator = $this->createMock(IURLGenerator::class);
                $this->l10n = $this->createMock(IL10N::class);
+               $this->dispatcher = $this->createMock(IEventDispatcher::class);
                $this->mailer = new Mailer(
                        $this->config,
                        $this->logger,
                        $this->defaults,
                        $this->urlGenerator,
-                       $this->l10n
+                       $this->l10n,
+                       $this->dispatcher
                );
        }
 
@@ -117,6 +131,21 @@ class MailerTest extends TestCase {
                $this->assertInstanceOf(\Swift_SendmailTransport::class, $mailer->getTransport());
        }
 
+       public function testEvents() {
+               $message = $this->createMock(Message::class);
+
+               $event = new BeforeMessageSent($message);
+               $this->dispatcher->expects($this->at(0))
+                       ->method('dispatchTyped')
+                       ->with($this->equalTo($event));
+
+               # We do not care at this point about errors in Swiftmailer
+               try {
+                       $this->mailer->send($message);
+               } catch (Swift_SwiftException $e) {
+               }
+       }
+
        public function testCreateMessage() {
                $this->config
                        ->expects($this->any())
index e0a0f468a50597829168bfcc64b099c0275f7137..ab62cfcfdcbedbd63086479575a4cc1188d922c9 100644 (file)
@@ -27,7 +27,17 @@ class MessageTest extends TestCase {
                        array(array('lukas@owncloud.com' => 'Lukas Reschke'), array('lukas@owncloud.com' => 'Lukas Reschke')),
                        array(array('lukas@owncloud.com' => 'Lukas Reschke', 'lukas@öwnclöüd.com', 'lukäs@owncloud.örg' => 'Lükäs Réschke'),
                                array('lukas@owncloud.com' => 'Lukas Reschke', 'lukas@xn--wncld-iuae2c.com', 'lukäs@owncloud.xn--rg-eka' => 'Lükäs Réschke')),
-                       array(array('lukas@öwnclöüd.com'), array('lukas@xn--wncld-iuae2c.com'))
+                       array(array('lukas@öwnclöüd.com'), array('lukas@xn--wncld-iuae2c.com')),
+               );
+       }
+
+       /**
+        * @return array
+        */
+       public function getMailAddressProvider() {
+               return array(
+                       array(NULL, array()),
+                       array(array('lukas@owncloud.com' => 'Lukas Reschke'), array('lukas@owncloud.com' => 'Lukas Reschke')),
                );
        }
 
@@ -59,13 +69,20 @@ class MessageTest extends TestCase {
                $this->message->setFrom(array('lukas@owncloud.com'));
        }
 
-       public function testGetFrom() {
+
+       /**
+        * @dataProvider getMailAddressProvider
+        *
+        * @param $swiftresult
+        * @param $return
+        */
+       public function testGetFrom($swiftresult, $return) {
                $this->swiftMessage
                        ->expects($this->once())
                        ->method('getFrom')
-                       ->will($this->returnValue(array('lukas@owncloud.com')));
+                       ->will($this->returnValue($swiftresult));
 
-               $this->assertSame(array('lukas@owncloud.com'), $this->message->getFrom());
+               $this->assertSame($return, $this->message->getFrom());
        }
 
        public function testSetReplyTo() {
@@ -93,13 +110,16 @@ class MessageTest extends TestCase {
                $this->message->setTo(array('lukas@owncloud.com'));
        }
 
-       public function testGetTo() {
+       /**
+        * @dataProvider  getMailAddressProvider
+        */
+       public function testGetTo($swiftresult,$return) {
                $this->swiftMessage
                        ->expects($this->once())
                        ->method('getTo')
-                       ->will($this->returnValue(array('lukas@owncloud.com')));
+                       ->will($this->returnValue($swiftresult));
 
-               $this->assertSame(array('lukas@owncloud.com'), $this->message->getTo());
+               $this->assertSame($return, $this->message->getTo());
        }
 
        public function testSetCc() {
@@ -110,13 +130,16 @@ class MessageTest extends TestCase {
                $this->message->setCc(array('lukas@owncloud.com'));
        }
 
-       public function testGetCc() {
+       /**
+        * @dataProvider  getMailAddressProvider
+        */
+       public function testGetCc($swiftresult,$return) {
                $this->swiftMessage
                        ->expects($this->once())
                        ->method('getCc')
-                       ->will($this->returnValue(array('lukas@owncloud.com')));
+                       ->will($this->returnValue($swiftresult));
 
-               $this->assertSame(array('lukas@owncloud.com'), $this->message->getCc());
+               $this->assertSame($return, $this->message->getCc());
        }
 
        public function testSetBcc() {
@@ -127,13 +150,16 @@ class MessageTest extends TestCase {
                $this->message->setBcc(array('lukas@owncloud.com'));
        }
 
-       public function testGetBcc() {
+       /**
+        * @dataProvider  getMailAddressProvider
+        */
+       public function testGetBcc($swiftresult,$return) {
                $this->swiftMessage
                        ->expects($this->once())
                        ->method('getBcc')
-                       ->will($this->returnValue(array('lukas@owncloud.com')));
+                       ->will($this->returnValue($swiftresult));
 
-               $this->assertSame(array('lukas@owncloud.com'), $this->message->getBcc());
+               $this->assertSame($return, $this->message->getBcc());
        }
 
        public function testSetSubject() {