summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/leap/bitmask/core')
-rw-r--r--src/leap/bitmask/core/mail_services.py78
-rw-r--r--src/leap/bitmask/core/service.py85
2 files changed, 95 insertions, 68 deletions
diff --git a/src/leap/bitmask/core/mail_services.py b/src/leap/bitmask/core/mail_services.py
index 04306b4..d2967f8 100644
--- a/src/leap/bitmask/core/mail_services.py
+++ b/src/leap/bitmask/core/mail_services.py
@@ -457,10 +457,11 @@ class StandardMailService(service.MultiService, HookableService):
self.addService(IncomingMailService(self))
def startService(self):
- log.msg('Starting Mail Service...')
+ log.msg('starting mail service')
super(StandardMailService, self).startService()
def stopService(self):
+ log.msg('stopping mail service')
super(StandardMailService, self).stopService()
def startInstance(self, userid, soledad, keymanager):
@@ -485,9 +486,6 @@ class StandardMailService(service.MultiService, HookableService):
d.addCallback(self._write_tokens_file, userid)
return d
- def stopInstance(self):
- pass
-
# hooks
def hook_on_new_keymanager_instance(self, **kw):
@@ -564,20 +562,26 @@ class IMAPService(service.Service):
name = 'imap'
def __init__(self, soledad_sessions):
- port, factory = imap.run_service(soledad_sessions)
-
- self._port = port
- self._factory = factory
self._soledad_sessions = soledad_sessions
+ self._port = None
+ self._factory = None
super(IMAPService, self).__init__()
def startService(self):
- log.msg('Starting IMAP Service')
+ log.msg('starting imap service')
+ port, factory = imap.run_service(
+ self._soledad_sessions, factory=self._factory)
+ self._port = port
+ self._factory = factory
super(IMAPService, self).startService()
def stopService(self):
- self._port.stopListening()
- self._factory.doStop()
+ log.msg("stopping imap service")
+ if self._port:
+ self._port.stopListening()
+ self._port = None
+ if self._factory:
+ self._factory.doStop()
super(IMAPService, self).stopService()
@@ -589,35 +593,47 @@ class SMTPService(service.Service):
basedir=DEFAULT_BASEDIR):
self._basedir = os.path.expanduser(basedir)
- port, factory = smtp.run_service(
- soledad_sessions, keymanager_sessions, sendmail_opts)
- self._port = port
- self._factory = factory
self._soledad_sessions = soledad_sessions
self._keymanager_sessions = keymanager_sessions
self._sendmail_opts = sendmail_opts
+ self._port = None
+ self._factory = None
super(SMTPService, self).__init__()
def startService(self):
- log.msg('Starting SMTP Service')
+ log.msg('starting smtp service')
+ port, factory = smtp.run_service(
+ self._soledad_sessions,
+ self._keymanager_sessions,
+ self._sendmail_opts,
+ factory=self._factory)
+ self._port = port
+ self._factory = factory
super(SMTPService, self).startService()
def stopService(self):
- # TODO cleanup all instances
+ log.msg('stopping smtp service')
+ if self._port:
+ self._port.stopListening()
+ self._port = None
+ if self._factory:
+ self._factory.doStop()
super(SMTPService, self).stopService()
-class IncomingMailService(service.Service):
+class IncomingMailService(service.MultiService):
+ """
+ Manage child services that check for incoming mail for individual users.
+ """
name = 'incoming_mail'
def __init__(self, mail_service):
super(IncomingMailService, self).__init__()
self._mail = mail_service
- self._instances = {}
def startService(self):
- log.msg('Starting IncomingMail Service')
+ log.msg('starting incoming mail service')
super(IncomingMailService, self).startService()
def stopService(self):
@@ -625,25 +641,14 @@ class IncomingMailService(service.Service):
# Individual accounts
- # TODO IncomingMail *IS* already a service.
- # I think we should better model the current Service
- # as a startInstance inside a container, and get this
- # multi-tenant service inside the leap.mail.incoming.service.
- # ... or just simply make it a multiService and set per-user
- # instances as Child of this parent.
-
def startInstance(self, userid):
soledad = self._mail.get_soledad_session(userid)
keymanager = self._mail.get_keymanager_session(userid)
- log.msg('Starting Incoming Mail instance for %s' % userid)
+ log.msg('setting up incoming mail service for %s' % userid)
self._start_incoming_mail_instance(
keymanager, soledad, userid)
- def stopInstance(self, userid):
- # TODO toggle offline!
- pass
-
def _start_incoming_mail_instance(self, keymanager, soledad,
userid, start_sync=True):
@@ -652,18 +657,13 @@ class IncomingMailService(service.Service):
keymanager, soledad,
inbox, userid,
check_period=INCOMING_CHECK_PERIOD)
- return incoming_mail
-
- def registerInstance(incoming_instance):
- self._instances[userid] = incoming_instance
- if start_sync:
- incoming_instance.startService()
+ incoming_mail.setName(userid)
+ self.addService(incoming_mail)
acc = Account(soledad, userid)
d = acc.callWhenReady(
lambda _: acc.get_collection_by_mailbox(INBOX_NAME))
d.addCallback(setUpIncomingMail)
- d.addCallback(registerInstance)
d.addErrback(log.err)
return d
diff --git a/src/leap/bitmask/core/service.py b/src/leap/bitmask/core/service.py
index e449a8c..de99ea5 100644
--- a/src/leap/bitmask/core/service.py
+++ b/src/leap/bitmask/core/service.py
@@ -19,10 +19,9 @@ Bitmask-core Service.
"""
import json
import resource
-from os.path import join, abspath
from twisted.internet import reactor
-from twisted.python import log, logfile
+from twisted.python import log
from leap.bitmask import __version__
from leap.bitmask.core import configurable
@@ -61,21 +60,19 @@ class BitmaskBackend(configurable.ConfigurableService):
on_start(self.init_bonafide)
if enabled('mail'):
- on_start(self.init_soledad)
- on_start(self.init_keymanager)
- on_start(self.init_mail)
+ on_start(self._init_mail_services)
if enabled('eip'):
- on_start(self.init_eip)
+ on_start(self._init_eip)
if enabled('zmq'):
- on_start(self.init_zmq)
+ on_start(self._init_zmq)
if enabled('web'):
- on_start(self.init_web)
+ on_start(self._init_web)
if enabled('websockets'):
- on_start(self.init_websockets)
+ on_start(self._init_websockets)
def init_events(self):
event_server.ensure_server()
@@ -93,47 +90,75 @@ class BitmaskBackend(configurable.ConfigurableService):
bf.register_hook('on_bonafide_auth', listener='keymanager')
bf.register_hook('on_bonafide_auth', listener='mail')
- def init_soledad(self):
+ def _start_child_service(self, name):
+ log.msg('starting backend child service: %s' % name)
+ service = self.getServiceNamed(name)
+ if service:
+ service.startService()
+
+ def _stop_child_service(self, name):
+ log.msg('stopping backend child service: %s' % name)
+ service = self.getServiceNamed(name)
+ if service:
+ service.stopService()
+
+ def _init_mail_services(self):
+ self._init_soledad()
+ self._init_keymanager()
+ self._init_mail()
+
+ def _start_mail_services(self):
+ self._start_child_service('soledad')
+ self._start_child_service('keymanager')
+ self._start_child_service('mail')
+
+ def _stop_mail_services(self):
+ self._stop_child_service('mail')
+ self._stop_child_service('keymanager')
+ self._stop_child_service('soledad')
+
+ def _init_soledad(self):
service = mail_services.SoledadService
- sol = self._maybe_start_service(
+ sol = self._maybe_init_service(
'soledad', service, self.basedir)
if sol:
sol.register_hook(
'on_new_soledad_instance', listener='keymanager')
- def init_keymanager(self):
+ def _init_keymanager(self):
service = mail_services.KeymanagerService
- km = self._maybe_start_service(
+ km = self._maybe_init_service(
'keymanager', service, self.basedir)
if km:
km.register_hook('on_new_keymanager_instance', listener='mail')
- def init_mail(self):
+ def _init_mail(self):
service = mail_services.StandardMailService
- self._maybe_start_service('mail', service, self.basedir)
+ self._maybe_init_service('mail', service, self.basedir)
- def init_eip(self):
+ def _init_eip(self):
# FIXME -- land EIP into leap.vpn
pass
- # self._maybe_start_service('eip', EIPService)
+ # self._maybe_init_service('eip', EIPService)
- def init_zmq(self):
+ def _init_zmq(self):
zs = _zmq.ZMQServerService(self)
zs.setServiceParent(self)
- def init_web(self):
+ def _init_web(self):
service = _web.HTTPDispatcherService
- self._maybe_start_service('web', service, self)
+ self._maybe_init_service('web', service, self)
- def init_websockets(self):
+ def _init_websockets(self):
from leap.bitmask.core import websocket
ws = websocket.WebSocketsDispatcherService(self)
ws.setServiceParent(self)
- def _maybe_start_service(self, label, klass, *args, **kw):
+ def _maybe_init_service(self, label, klass, *args, **kw):
try:
self.getServiceNamed(label)
except KeyError:
+ log.msg("initializing service: %s" % label)
service = klass(*args, **kw)
service.setName(label)
service.setServiceParent(self)
@@ -158,24 +183,26 @@ class BitmaskBackend(configurable.ConfigurableService):
self.set_config('services', service, 'True')
if service == 'mail':
- self.init_soledad()
- self.init_keymanager()
- self.init_mail()
+ self._init_mail_services()
+ self._start_mail_services()
elif service == 'eip':
- self.init_eip()
+ self._init_eip()
elif service == 'zmq':
- self.init_zmq()
+ self._init_zmq()
elif service == 'web':
- self.init_web()
+ self._init_web()
return {'enabled': 'ok'}
def do_disable_service(self, service):
assert service in self.service_names
- # TODO -- should stop also?
+
+ if service == 'mail':
+ self._stop_mail_services()
+
self.set_config('services', service, 'False')
return {'disabled': 'ok'}