summaryrefslogtreecommitdiff
path: root/service/pixelated/bitmask_libraries/smtp.py
blob: 63d2d310087b515fd15470472dbb7f83ac8e16d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#
# Copyright (c) 2014 ThoughtWorks, Inc.
#
# Pixelated 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.
#
# Pixelated 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 Pixelated. If not, see <http://www.gnu.org/licenses/>.
import logging
import os
import requests
import random
from leap.mail.smtp import setup_smtp_gateway
from pixelated.bitmask_libraries.certs import LeapCertificate


logger = logging.getLogger(__name__)


class LeapSmtp(object):

    def __init__(self, provider, auth, keymanager=None):
        self.local_smtp_port_number = random.randrange(12000, 16000)
        self._provider = provider
        self.username = auth.username
        self.session_id = auth.session_id
        self.user_token = auth.token
        self._keymanager = keymanager
        self._remote_hostname, self._remote_port = provider.smtp_info()
        self._local_smtp_service_socket = None
        self._local_smtp_service = None

    def start(self):
        cert_path = self._provider._client_cert_path()
        email = '%s@%s' % (self.username, self._provider.domain)

        self._local_smtp_service, self._local_smtp_service_socket = setup_smtp_gateway(
            port=self.local_smtp_port_number,
            userid=str(email),
            keymanager=self._keymanager,
            smtp_host=self._remote_hostname.encode('UTF-8'),
            smtp_port=self._remote_port,
            smtp_cert=cert_path,
            smtp_key=cert_path,
            encrypted_only=False
        )

    def ensure_running(self):
        if not self._local_smtp_service:
            try:
                self.start()
            except:
                logger.warning("Couldn't start the SMTP server now, will try again when the user tries to use it")
                return False
        return True

    def stop(self):
        if self._local_smtp_service is not None:
            self._local_smtp_service_socket.stopListening()
            self._local_smtp_service.doStop()
            self._local_smtp_service_socket = None
            self._local_smtp_service = None