summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
authorDavid Baker <dave@matrix.org>2016-04-20 13:02:01 +0100
committerDavid Baker <dave@matrix.org>2016-04-20 13:02:01 +0100
commitf63bd4ff4704c9f7b6e23c76720dbd955a60c058 (patch)
treea503afc43fc4a6ab84190586f267a31c94cb466c /synapse
parentAdd single instance & logging stuff (diff)
downloadsynapse-f63bd4ff4704c9f7b6e23c76720dbd955a60c058.tar.xz
Send a rather basic email notif
Also pep8 fixes
Diffstat (limited to '')
-rw-r--r--synapse/config/emailconfig.py62
-rw-r--r--synapse/config/homeserver.py3
-rw-r--r--synapse/push/emailpusher.py32
-rw-r--r--synapse/push/mailer.py48
-rw-r--r--synapse/storage/event_push_actions.py1
-rw-r--r--synapse/storage/pusher.py2
6 files changed, 136 insertions, 12 deletions
diff --git a/synapse/config/emailconfig.py b/synapse/config/emailconfig.py
new file mode 100644
index 0000000000..978826627b
--- /dev/null
+++ b/synapse/config/emailconfig.py
@@ -0,0 +1,62 @@
+# -*- coding: utf-8 -*-
+# Copyright 2015, 2016 OpenMarket Ltd
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This file can't be called email.py because if it is, we cannot:
+import email.utils
+
+from ._base import Config
+
+
+class EmailConfig(Config):
+    """
+    Email Configuration
+    """
+
+    def read_config(self, config):
+        email_config = config.get("email", None)
+        if email_config:
+            self.email_enable_notifs = email_config.get("enable_notifs", True)
+            if (
+                "smtp_host" not in email_config or
+                "smtp_port" not in email_config or
+                "notif_from" not in email_config
+            ):
+                raise RuntimeError(
+                    "You must set smtp_host, smtp_port and notif_from "
+                    "to send email notifications"
+                )
+
+            self.email_smtp_host = email_config["smtp_host"]
+            self.email_smtp_port = email_config["smtp_port"]
+            self.email_notif_from = email_config["notif_from"]
+
+            # make sure it's valid
+            parsed = email.utils.parseaddr(self.email_notif_from)
+            if parsed[1] == '':
+                raise RuntimeError("Invalid notif_from address")
+        else:
+            self.email_enable_notifs = False
+            self.email_smtp_host = None
+            self.email_smtp_port = None
+            self.email_notif_from = None
+
+    def default_config(self, config_dir_path, server_name, **kwargs):
+        return """
+        # Enable sending emails for notification events
+        #email_config:
+        #   enable_notifs: false
+        #   smtp_host: "localhost"
+        #   smtp_port: 25
+        """
diff --git a/synapse/config/homeserver.py b/synapse/config/homeserver.py
index 9a80ac39ec..fc2445484c 100644
--- a/synapse/config/homeserver.py
+++ b/synapse/config/homeserver.py
@@ -31,13 +31,14 @@ from .cas import CasConfig
 from .password import PasswordConfig
 from .jwt import JWTConfig
 from .ldap import LDAPConfig
+from .emailconfig import EmailConfig
 
 
 class HomeServerConfig(TlsConfig, ServerConfig, DatabaseConfig, LoggingConfig,
                        RatelimitConfig, ContentRepositoryConfig, CaptchaConfig,
                        VoipConfig, RegistrationConfig, MetricsConfig, ApiConfig,
                        AppServiceConfig, KeyConfig, SAML2Config, CasConfig,
-                       JWTConfig, LDAPConfig, PasswordConfig,):
+                       JWTConfig, LDAPConfig, PasswordConfig, EmailConfig,):
     pass
 
 
diff --git a/synapse/push/emailpusher.py b/synapse/push/emailpusher.py
index 74e3a70562..820c8f8467 100644
--- a/synapse/push/emailpusher.py
+++ b/synapse/push/emailpusher.py
@@ -18,9 +18,10 @@ from twisted.internet import defer, reactor
 import logging
 
 from synapse.util.metrics import Measure
-from synapse.util.async import run_on_reactor
 from synapse.util.logcontext import LoggingContext
 
+from mailer import Mailer
+
 logger = logging.getLogger(__name__)
 
 # The amount of time we always wait before ever emailing about a notification
@@ -28,11 +29,11 @@ logger = logging.getLogger(__name__)
 DELAY_BEFORE_MAIL_MS = 2 * 60 * 1000
 
 THROTTLE_START_MS = 2 * 60 * 1000
-THROTTLE_MAX_MS = (2 * 60 * 1000) * (2**11)  # ~3 days
+THROTTLE_MAX_MS = (2 * 60 * 1000) * (2 ** 11)  # ~3 days
 
 # If no event triggers a notification for this long after the previous,
 # the throttle is released.
-THROTTLE_RESET_AFTER_MS = (2 * 60 * 1000) * (2**11)  # ~3 days
+THROTTLE_RESET_AFTER_MS = (2 * 60 * 1000) * (2 ** 11)  # ~3 days
 
 
 class EmailPusher(object):
@@ -59,12 +60,22 @@ class EmailPusher(object):
 
         self.processing = False
 
+        if self.hs.config.email_enable_notifs:
+            self.mailer = Mailer(
+                self.store,
+                self.hs.config.email_smtp_host, self.hs.config.email_smtp_port,
+                self.hs.config.email_notif_from,
+            )
+        else:
+            self.mailer = None
+
     @defer.inlineCallbacks
     def on_started(self):
-        self.throttle_params = yield self.store.get_throttle_params_by_room(
-            self.pusher_id
-        )
-        yield self._process()
+        if self.mailer is not None:
+            self.throttle_params = yield self.store.get_throttle_params_by_room(
+                self.pusher_id
+            )
+            yield self._process()
 
     def on_stop(self):
         if self.timed_call:
@@ -102,6 +113,7 @@ class EmailPusher(object):
                 finally:
                     self.processing = False
 
+    @defer.inlineCallbacks
     def _unsafe_process(self):
         """
         Main logic of the push loop without the wrapper function that sets
@@ -241,5 +253,7 @@ class EmailPusher(object):
 
     @defer.inlineCallbacks
     def send_notification(self, push_action):
-        yield run_on_reactor()
-        logger.error("sending notif email for user %r", self.user_id)
\ No newline at end of file
+        logger.info("Sending notif email for user %r", self.user_id)
+        yield self.mailer.send_notification_mail(
+            self.user_id, self.email, push_action
+        )
diff --git a/synapse/push/mailer.py b/synapse/push/mailer.py
new file mode 100644
index 0000000000..93d3866ec7
--- /dev/null
+++ b/synapse/push/mailer.py
@@ -0,0 +1,48 @@
+# -*- coding: utf-8 -*-
+# Copyright 2016 OpenMarket Ltd
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from twisted.internet import defer
+
+import smtplib
+import email.utils
+import email.mime.multipart
+from email.mime.text import MIMEText
+
+
+class Mailer(object):
+    def __init__(self, store, smtp_host, smtp_port, notif_from):
+        self.store = store
+        self.smtp_host = smtp_host
+        self.smtp_port = smtp_port
+        self.notif_from = notif_from
+
+    @defer.inlineCallbacks
+    def send_notification_mail(self, user_id, email_address, push_action):
+        raw_from = email.utils.parseaddr(self.notif_from)[1]
+        raw_to = email.utils.parseaddr(email_address)[1]
+
+        if raw_to == '':
+            raise RuntimeError("Invalid 'to' address")
+
+        plainText = "yo dawg, you got notifications!"
+
+        text_part = MIMEText(plainText, "plain")
+        text_part['Subject'] = "New Matrix Notifications"
+        text_part['From'] = self.notif_from
+        text_part['To'] = email_address
+
+        smtp = smtplib.SMTP(self.smtp_host, self.smtp_port)
+        smtp.sendmail(raw_from, raw_to, text_part.as_string())
+        smtp.quit()
\ No newline at end of file
diff --git a/synapse/storage/event_push_actions.py b/synapse/storage/event_push_actions.py
index ad512b2f07..f2af8bdb36 100644
--- a/synapse/storage/event_push_actions.py
+++ b/synapse/storage/event_push_actions.py
@@ -202,7 +202,6 @@ class EventPushActionsStore(SQLBaseStore):
         result = yield self.runInteraction("get_time_of_last_push_action_before", f)
         defer.returnValue(result[0] if result is not None else None)
 
-
     @defer.inlineCallbacks
     def get_latest_push_action_stream_ordering(self):
         def f(txn):
diff --git a/synapse/storage/pusher.py b/synapse/storage/pusher.py
index caef9b59a5..5fb47d418a 100644
--- a/synapse/storage/pusher.py
+++ b/synapse/storage/pusher.py
@@ -256,4 +256,4 @@ class PusherStore(SQLBaseStore):
             {"pusher": pusher_id, "room_id": room_id},
             params,
             desc="set_throttle_params"
-        )
\ No newline at end of file
+        )