From aaf2b4077555b5ec896e0f88a5b5574f58c4d74b Mon Sep 17 00:00:00 2001
From: Kali Kaneko <kali@leap.se>
Date: Mon, 14 Sep 2015 23:19:58 -0400
Subject: [feat] use async events api

in this way, we're using twisted reactor instead of having another
thread with zmq's own copy of tornado ioloop.

Resolves: #7274
---
 mail/src/leap/mail/imap/server.py       |  4 ++--
 mail/src/leap/mail/imap/service/imap.py |  6 +++---
 mail/src/leap/mail/incoming/service.py  | 16 ++++++++--------
 mail/src/leap/mail/mail.py              |  5 ++---
 mail/src/leap/mail/outgoing/service.py  | 16 ++++++++--------
 mail/src/leap/mail/smtp/__init__.py     |  6 +++---
 mail/src/leap/mail/smtp/gateway.py      | 10 +++++-----
 7 files changed, 31 insertions(+), 32 deletions(-)

diff --git a/mail/src/leap/mail/imap/server.py b/mail/src/leap/mail/imap/server.py
index 39f483f..8f14936 100644
--- a/mail/src/leap/mail/imap/server.py
+++ b/mail/src/leap/mail/imap/server.py
@@ -27,7 +27,7 @@ from twisted.mail import imap4
 from twisted.python import log
 
 from leap.common.check import leap_assert, leap_assert_type
-from leap.common.events import emit, catalog
+from leap.common.events import emit_async, catalog
 from leap.soledad.client import Soledad
 
 # imports for LITERAL+ patch
@@ -224,7 +224,7 @@ class LEAPIMAPServer(imap4.IMAP4Server):
             # bad username, reject.
             raise cred.error.UnauthorizedLogin()
         # any dummy password is allowed so far. use realm instead!
-        emit(catalog.IMAP_CLIENT_LOGIN, "1")
+        emit_async(catalog.IMAP_CLIENT_LOGIN, "1")
         return imap4.IAccount, self.theAccount, lambda: None
 
     def do_FETCH(self, tag, messages, query, uid=0):
diff --git a/mail/src/leap/mail/imap/service/imap.py b/mail/src/leap/mail/imap/service/imap.py
index c3ae59a..cd31edf 100644
--- a/mail/src/leap/mail/imap/service/imap.py
+++ b/mail/src/leap/mail/imap/service/imap.py
@@ -28,7 +28,7 @@ from twisted.internet.protocol import ServerFactory
 from twisted.mail import imap4
 from twisted.python import log
 
-from leap.common.events import emit, catalog
+from leap.common.events import emit_async, catalog
 from leap.common.check import leap_check
 from leap.mail.imap.account import IMAPAccount
 from leap.mail.imap.server import LEAPIMAPServer
@@ -178,10 +178,10 @@ def run_service(store, **kwargs):
             reactor.listenTCP(manhole.MANHOLE_PORT, manhole_factory,
                               interface="127.0.0.1")
         logger.debug("IMAP4 Server is RUNNING in port  %s" % (port,))
-        emit(catalog.IMAP_SERVICE_STARTED, str(port))
+        emit_async(catalog.IMAP_SERVICE_STARTED, str(port))
 
         # FIXME -- change service signature
         return tport, factory
 
     # not ok, signal error.
-    emit(catalog.IMAP_SERVICE_FAILED_TO_START, str(port))
+    emit_async(catalog.IMAP_SERVICE_FAILED_TO_START, str(port))
diff --git a/mail/src/leap/mail/incoming/service.py b/mail/src/leap/mail/incoming/service.py
index 2e953a7..2a3a86a 100644
--- a/mail/src/leap/mail/incoming/service.py
+++ b/mail/src/leap/mail/incoming/service.py
@@ -38,7 +38,7 @@ from twisted.internet.task import LoopingCall
 from twisted.internet.task import deferLater
 from u1db import errors as u1db_errors
 
-from leap.common.events import emit, catalog
+from leap.common.events import emit_async, catalog
 from leap.common.check import leap_assert, leap_assert_type
 from leap.common.mail import get_email_charset
 from leap.keymanager import errors as keymanager_errors
@@ -231,7 +231,7 @@ class IncomingMail(Service):
         except InvalidAuthTokenError:
             # if the token is invalid, send an event so the GUI can
             # disable mail and show an error message.
-            emit(catalog.SOLEDAD_INVALID_AUTH_TOKEN)
+            emit_async(catalog.SOLEDAD_INVALID_AUTH_TOKEN)
 
     def _signal_fetch_to_ui(self, doclist):
         """
@@ -247,7 +247,7 @@ class IncomingMail(Service):
             num_mails = len(doclist) if doclist is not None else 0
             if num_mails != 0:
                 log.msg("there are %s mails" % (num_mails,))
-            emit(catalog.MAIL_FETCHED_INCOMING,
+            emit_async(catalog.MAIL_FETCHED_INCOMING,
                  str(num_mails), str(fetched_ts))
             return doclist
 
@@ -255,7 +255,7 @@ class IncomingMail(Service):
         """
         Sends unread event to ui.
         """
-        emit(catalog.MAIL_UNREAD_MESSAGES,
+        emit_async(catalog.MAIL_UNREAD_MESSAGES,
              str(self._inbox_collection.count_unseen()))
 
     # process incoming mail.
@@ -279,7 +279,7 @@ class IncomingMail(Service):
         deferreds = []
         for index, doc in enumerate(doclist):
             logger.debug("processing doc %d of %d" % (index + 1, num_mails))
-            emit(catalog.MAIL_MSG_PROCESSING,
+            emit_async(catalog.MAIL_MSG_PROCESSING,
                  str(index), str(num_mails))
 
             keys = doc.content.keys()
@@ -329,7 +329,7 @@ class IncomingMail(Service):
                 decrdata = ""
                 success = False
 
-            emit(catalog.MAIL_MSG_DECRYPTED, "1" if success else "0")
+            emit_async(catalog.MAIL_MSG_DECRYPTED, "1" if success else "0")
             return self._process_decrypted_doc(doc, decrdata)
 
         d = self._keymanager.decrypt(
@@ -723,10 +723,10 @@ class IncomingMail(Service):
                 listener(result)
 
             def signal_deleted(doc_id):
-                emit(catalog.MAIL_MSG_DELETED_INCOMING)
+                emit_async(catalog.MAIL_MSG_DELETED_INCOMING)
                 return doc_id
 
-            emit(catalog.MAIL_MSG_SAVED_LOCALLY)
+            emit_async(catalog.MAIL_MSG_SAVED_LOCALLY)
             d = self._delete_incoming_message(doc)
             d.addCallback(signal_deleted)
             return d
diff --git a/mail/src/leap/mail/mail.py b/mail/src/leap/mail/mail.py
index 540a493..258574e 100644
--- a/mail/src/leap/mail/mail.py
+++ b/mail/src/leap/mail/mail.py
@@ -28,7 +28,7 @@ from twisted.internet import defer
 from twisted.python import log
 
 from leap.common.check import leap_assert_type
-from leap.common.events import emit, catalog
+from leap.common.events import emit_async, catalog
 from leap.common.mail import get_email_charset
 
 from leap.mail.adaptors.soledad import SoledadMailAdaptor
@@ -736,8 +736,7 @@ class MessageCollection(object):
         :param unseen: number of unseen messages.
         :type unseen: int
         """
-        # TODO change name of the signal, independent from imap now.
-        emit(catalog.MAIL_UNREAD_MESSAGES, str(unseen))
+        emit_async(catalog.MAIL_UNREAD_MESSAGES, str(unseen))
 
     def copy_msg(self, msg, new_mbox_uuid):
         """
diff --git a/mail/src/leap/mail/outgoing/service.py b/mail/src/leap/mail/outgoing/service.py
index 838a908..3708f33 100644
--- a/mail/src/leap/mail/outgoing/service.py
+++ b/mail/src/leap/mail/outgoing/service.py
@@ -31,7 +31,7 @@ from twisted.protocols.amp import ssl
 from twisted.python import log
 
 from leap.common.check import leap_assert_type, leap_assert
-from leap.common.events import emit, catalog
+from leap.common.events import emit_async, catalog
 from leap.keymanager.openpgp import OpenPGPKey
 from leap.keymanager.errors import KeyNotFound, KeyAddressMismatch
 from leap.mail import __version__
@@ -135,7 +135,7 @@ class OutgoingMail:
         """
         dest_addrstr = smtp_sender_result[1][0][0]
         log.msg('Message sent to %s' % dest_addrstr)
-        emit(catalog.SMTP_SEND_MESSAGE_SUCCESS, dest_addrstr)
+        emit_async(catalog.SMTP_SEND_MESSAGE_SUCCESS, dest_addrstr)
 
     def sendError(self, failure):
         """
@@ -145,7 +145,7 @@ class OutgoingMail:
         :type e: anything
         """
         # XXX: need to get the address from the exception to send signal
-        # emit(catalog.SMTP_SEND_MESSAGE_ERROR, self._user.dest.addrstr)
+        # emit_async(catalog.SMTP_SEND_MESSAGE_ERROR, self._user.dest.addrstr)
         err = failure.value
         log.err(err)
         raise err
@@ -178,7 +178,7 @@ class OutgoingMail:
             requireAuthentication=False,
             requireTransportSecurity=True)
         factory.domain = __version__
-        emit(catalog.SMTP_SEND_MESSAGE_START, recipient.dest.addrstr)
+        emit_async(catalog.SMTP_SEND_MESSAGE_START, recipient.dest.addrstr)
         reactor.connectSSL(
             self._host, self._port, factory,
             contextFactory=SSLContextFactory(self._cert, self._key))
@@ -240,7 +240,7 @@ class OutgoingMail:
             return d
 
         def signal_encrypt_sign(newmsg):
-            emit(catalog.SMTP_END_ENCRYPT_AND_SIGN,
+            emit_async(catalog.SMTP_END_ENCRYPT_AND_SIGN,
                  "%s,%s" % (self._from_address, to_address))
             return newmsg, recipient
 
@@ -248,18 +248,18 @@ class OutgoingMail:
             failure.trap(KeyNotFound, KeyAddressMismatch)
 
             log.msg('Will send unencrypted message to %s.' % to_address)
-            emit(catalog.SMTP_START_SIGN, self._from_address)
+            emit_async(catalog.SMTP_START_SIGN, self._from_address)
             d = self._sign(message, from_address)
             d.addCallback(signal_sign)
             return d
 
         def signal_sign(newmsg):
-            emit(catalog.SMTP_END_SIGN, self._from_address)
+            emit_async(catalog.SMTP_END_SIGN, self._from_address)
             return newmsg, recipient
 
         log.msg("Will encrypt the message with %s and sign with %s."
                 % (to_address, from_address))
-        emit(catalog.SMTP_START_ENCRYPT_AND_SIGN,
+        emit_async(catalog.SMTP_START_ENCRYPT_AND_SIGN,
              "%s,%s" % (self._from_address, to_address))
         d = self._maybe_attach_key(origmsg, from_address, to_address)
         d.addCallback(maybe_encrypt_and_sign)
diff --git a/mail/src/leap/mail/smtp/__init__.py b/mail/src/leap/mail/smtp/__init__.py
index 2ff14d7..a77a414 100644
--- a/mail/src/leap/mail/smtp/__init__.py
+++ b/mail/src/leap/mail/smtp/__init__.py
@@ -24,7 +24,7 @@ from twisted.internet import reactor
 from twisted.internet.error import CannotListenError
 from leap.mail.outgoing.service import OutgoingMail
 
-from leap.common.events import emit, catalog
+from leap.common.events import emit_async, catalog
 from leap.mail.smtp.gateway import SMTPFactory
 
 logger = logging.getLogger(__name__)
@@ -65,12 +65,12 @@ def setup_smtp_gateway(port, userid, keymanager, smtp_host, smtp_port,
     factory = SMTPFactory(userid, keymanager, encrypted_only, outgoing_mail)
     try:
         tport = reactor.listenTCP(port, factory, interface="localhost")
-        emit(catalog.SMTP_SERVICE_STARTED, str(port))
+        emit_async(catalog.SMTP_SERVICE_STARTED, str(port))
         return factory, tport
     except CannotListenError:
         logger.error("STMP Service failed to start: "
                      "cannot listen in port %s" % port)
-        emit(catalog.SMTP_SERVICE_FAILED_TO_START, str(port))
+        emit_async(catalog.SMTP_SERVICE_FAILED_TO_START, str(port))
     except Exception as exc:
         logger.error("Unhandled error while launching smtp gateway service")
         logger.exception(exc)
diff --git a/mail/src/leap/mail/smtp/gateway.py b/mail/src/leap/mail/smtp/gateway.py
index 7dae907..dd110e0 100644
--- a/mail/src/leap/mail/smtp/gateway.py
+++ b/mail/src/leap/mail/smtp/gateway.py
@@ -37,7 +37,7 @@ from twisted.python import log
 
 from email.Header import Header
 from leap.common.check import leap_assert_type
-from leap.common.events import emit, catalog
+from leap.common.events import emit_async, catalog
 from leap.keymanager.openpgp import OpenPGPKey
 from leap.keymanager.errors import KeyNotFound
 from leap.mail.utils import validate_address
@@ -204,18 +204,18 @@ class SMTPDelivery(object):
         # verify if recipient key is available in keyring
         def found(_):
             log.msg("Accepting mail for %s..." % user.dest.addrstr)
-            emit(catalog.SMTP_RECIPIENT_ACCEPTED_ENCRYPTED, user.dest.addrstr)
+            emit_async(catalog.SMTP_RECIPIENT_ACCEPTED_ENCRYPTED, user.dest.addrstr)
 
         def not_found(failure):
             failure.trap(KeyNotFound)
 
             # if key was not found, check config to see if will send anyway
             if self._encrypted_only:
-                emit(catalog.SMTP_RECIPIENT_REJECTED, user.dest.addrstr)
+                emit_async(catalog.SMTP_RECIPIENT_REJECTED, user.dest.addrstr)
                 raise smtp.SMTPBadRcpt(user.dest.addrstr)
             log.msg("Warning: will send an unencrypted message (because "
                     "encrypted_only' is set to False).")
-            emit(
+            emit_async(
                 catalog.SMTP_RECIPIENT_ACCEPTED_UNENCRYPTED,
                 user.dest.addrstr)
 
@@ -309,7 +309,7 @@ class EncryptedMessage(object):
         """
         log.msg("Connection lost unexpectedly!")
         log.err()
-        emit(catalog.SMTP_CONNECTION_LOST, self._user.dest.addrstr)
+        emit_async(catalog.SMTP_CONNECTION_LOST, self._user.dest.addrstr)
         # unexpected loss of connection; don't save
 
         self._lines = []
-- 
cgit v1.2.3