diff options
Diffstat (limited to 'service/pixelated')
-rw-r--r-- | service/pixelated/adapter/mailstore/leap_mailstore.py | 2 | ||||
-rw-r--r-- | service/pixelated/adapter/search/__init__.py | 10 | ||||
-rw-r--r-- | service/pixelated/adapter/services/mail_sender.py | 28 | ||||
-rw-r--r-- | service/pixelated/bitmask_libraries/provider.py | 8 | ||||
-rw-r--r-- | service/pixelated/bitmask_libraries/session.py | 66 | ||||
-rw-r--r-- | service/pixelated/bitmask_libraries/smtp.py | 78 | ||||
-rw-r--r-- | service/pixelated/config/services.py | 9 |
7 files changed, 120 insertions, 81 deletions
diff --git a/service/pixelated/adapter/mailstore/leap_mailstore.py b/service/pixelated/adapter/mailstore/leap_mailstore.py index 2754c624..519b124a 100644 --- a/service/pixelated/adapter/mailstore/leap_mailstore.py +++ b/service/pixelated/adapter/mailstore/leap_mailstore.py @@ -223,7 +223,7 @@ class LeapMailStore(MailStore): def get_mails(self, mail_ids): deferreds = [] for mail_id in mail_ids: - deferreds.append(self.get_mail(mail_id)) + deferreds.append(self.get_mail(mail_id, include_body=True)) return defer.gatherResults(deferreds, consumeErrors=True) diff --git a/service/pixelated/adapter/search/__init__.py b/service/pixelated/adapter/search/__init__.py index 56ab2255..065dd5e5 100644 --- a/service/pixelated/adapter/search/__init__.py +++ b/service/pixelated/adapter/search/__init__.py @@ -23,7 +23,7 @@ import time from pixelated.adapter.model.status import Status from pixelated.adapter.search.contacts import contacts_suggestions from whoosh.index import FileIndex -from whoosh.fields import Schema, ID, KEYWORD, TEXT, NUMERIC +from whoosh.fields import Schema, ID, KEYWORD, TEXT, NUMERIC, NGRAMWORDS from whoosh.qparser import QueryParser from whoosh.qparser import MultifieldParser from whoosh.writing import AsyncWriter @@ -103,9 +103,9 @@ class SearchEngine(object): to=KEYWORD(stored=False, commas=True), cc=KEYWORD(stored=False, commas=True), bcc=KEYWORD(stored=False, commas=True), - subject=TEXT(stored=False), + subject=NGRAMWORDS(stored=False), date=NUMERIC(stored=False, sortable=True, bits=64, signed=False), - body=TEXT(stored=False), + body=NGRAMWORDS(stored=False), tag=KEYWORD(stored=True, commas=True), flags=KEYWORD(stored=True, commas=True), raw=TEXT(stored=False)) @@ -116,7 +116,7 @@ class SearchEngine(object): def index_mail(self, mail): with AsyncWriter(self._index) as writer: - self._index_mail(writer, mail) + self._index_mail(writer, mail) def _index_mail(self, writer, mail): mdict = mail.as_dict() @@ -197,7 +197,7 @@ class SearchEngine(object): .replace('-in:', 'AND NOT tag:') .replace('in:all', '*') ) - return MultifieldParser(['raw', 'body'], self._index.schema).parse(query) + return MultifieldParser(['body', 'subject', 'raw'], self._index.schema).parse(query) def remove_from_index(self, mail_id): with AsyncWriter(self._index) as writer: diff --git a/service/pixelated/adapter/services/mail_sender.py b/service/pixelated/adapter/services/mail_sender.py index 262a6e18..ca1e99d7 100644 --- a/service/pixelated/adapter/services/mail_sender.py +++ b/service/pixelated/adapter/services/mail_sender.py @@ -15,10 +15,11 @@ # along with Pixelated. If not, see <http://www.gnu.org/licenses/>. from StringIO import StringIO from email.utils import parseaddr +from leap.mail.outgoing.service import OutgoingMail from twisted.internet.defer import Deferred, fail from twisted.mail.smtp import SMTPSenderFactory -from twisted.internet import reactor +from twisted.internet import reactor, defer from pixelated.support.functional import flatten @@ -29,6 +30,31 @@ class SMTPDownException(Exception): class MailSender(object): + def __init__(self, smtp_config, keymanager): + self._smtp_config = smtp_config + self._keymanager = keymanager + + def sendmail(self, mail): + recipients = flatten([mail.to, mail.cc, mail.bcc]) + outgoing_mail = self._create_outgoing_mail() + deferreds = [] + + for recipient in recipients: + deferreds.append(outgoing_mail.send_message(mail.to_smtp_format(), recipient)) + + return defer.gatherResults(deferreds) + + def _create_outgoing_mail(self): + return OutgoingMail(str(self._smtp_config.account_email), + self._keymanager, + self._smtp_config.cert_path, + self._smtp_config.cert_path, + str(self._smtp_config.remote_smtp_host), + int(self._smtp_config.remote_smtp_port)) + + +class LocalSmtpMailSender(object): + def __init__(self, account_email_address, smtp): self.smtp = smtp self.account_email_address = account_email_address diff --git a/service/pixelated/bitmask_libraries/provider.py b/service/pixelated/bitmask_libraries/provider.py index b7f82f8a..a529208d 100644 --- a/service/pixelated/bitmask_libraries/provider.py +++ b/service/pixelated/bitmask_libraries/provider.py @@ -14,6 +14,7 @@ # 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 json +import os from leap.common.certs import get_digest import requests @@ -95,6 +96,13 @@ class LeapProvider(object): if fingerprint.strip() != digest: raise Exception('Certificate fingerprints don\'t match! Expected [%s] but got [%s]' % (fingerprint.strip(), digest)) + def smtp_info(self): + json_data = self.fetch_smtp_json() + hosts = json_data['hosts'] + hostname = hosts.keys()[0] + host = hosts[hostname] + return host['hostname'], host['port'] + def _validated_get(self, url): session = requests.session() try: diff --git a/service/pixelated/bitmask_libraries/session.py b/service/pixelated/bitmask_libraries/session.py index da62b084..3f8e6de6 100644 --- a/service/pixelated/bitmask_libraries/session.py +++ b/service/pixelated/bitmask_libraries/session.py @@ -16,17 +16,18 @@ import errno import traceback import sys - import os +import requests + +from twisted.internet import reactor, defer +from pixelated.bitmask_libraries.certs import LeapCertificate +from pixelated.adapter.mailstore import LeapMailStore from leap.mail.incoming.service import IncomingMail -from twisted.internet import reactor -from .nicknym import NickNym from leap.auth import SRPAuth -from pixelated.adapter.mailstore import LeapMailStore -from .soledad import SoledadSessionFactory -from .smtp import LeapSmtp from leap.mail.imap.account import IMAPAccount -from twisted.internet import defer +from .nicknym import NickNym +from .smtp import LeapSmtp, LeapSMTPConfig +from .soledad import SoledadSessionFactory from leap.common.events import ( register, @@ -122,6 +123,36 @@ class LeapSession(object): raise +class SmtpCertDownloader(object): + + def __init__(self, provider, auth): + self._provider = provider + self._auth = auth + + def download(self): + cert_url = '%s/%s/cert' % (self._provider.api_uri, self._provider.api_version) + cookies = {"_session_id": self._auth.session_id} + headers = {} + headers["Authorization"] = 'Token token="{0}"'.format(self._auth.token) + response = requests.get( + cert_url, + verify=LeapCertificate(self._provider).provider_api_cert, + cookies=cookies, + timeout=self._provider.config.timeout_in_s, + headers=headers) + response.raise_for_status() + + client_cert = response.content + + return client_cert + + def download_to(self, target_file): + client_cert = self.download() + + with open(target_file, 'w') as f: + f.write(client_cert) + + class LeapSessionFactory(object): def __init__(self, provider): self._provider = provider @@ -149,10 +180,29 @@ class LeapSessionFactory(object): nicknym = self._create_nicknym(account_email, auth.token, auth.uuid, soledad) - smtp = LeapSmtp(self._provider, auth, nicknym.keymanager) + self._download_smtp_cert(auth) + + smtp_host, smtp_port = self._provider.smtp_info() + smtp_config = LeapSMTPConfig(account_email, self._smtp_client_cert_path(), smtp_host, smtp_port) + smtp = LeapSmtp(smtp_config, nicknym.keymanager) return LeapSession(self._provider, auth, mail_store, soledad, nicknym, smtp) + def _download_smtp_cert(self, auth): + cert_path = self._smtp_client_cert_path() + + if not os.path.exists(os.path.dirname(cert_path)): + os.makedirs(os.path.dirname(cert_path)) + + SmtpCertDownloader(self._provider, auth).download_to(cert_path) + + def _smtp_client_cert_path(self): + return os.path.join( + self._config.leap_home, + "providers", + self._provider.domain, + "keys", "client", "smtp.pem") + def _lookup_session(self, key): global SESSIONS if key in SESSIONS: diff --git a/service/pixelated/bitmask_libraries/smtp.py b/service/pixelated/bitmask_libraries/smtp.py index ff2792fb..f4ab00f7 100644 --- a/service/pixelated/bitmask_libraries/smtp.py +++ b/service/pixelated/bitmask_libraries/smtp.py @@ -14,86 +14,40 @@ # 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 LeapSMTPConfig(object): + + def __init__(self, account_email, cert_path, remote_smtp_host, remote_smtp_port): + self.account_email = account_email + self.cert_path = cert_path + self.remote_smtp_host = remote_smtp_host + self.remote_smtp_port = remote_smtp_port + + class LeapSmtp(object): - def __init__(self, provider, auth, keymanager=None): + def __init__(self, smtp_config, 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._smtp_config = smtp_config self._keymanager = keymanager - self._remote_hostname, self._remote_port = self._discover_remote_smtp_server() self._local_smtp_service_socket = None self._local_smtp_service = None - def smtp_info(self): - return ('localhost', self.local_smtp_port_number) - - def _discover_remote_smtp_server(self): - json_data = self._provider.fetch_smtp_json() - hosts = json_data['hosts'] - hostname = hosts.keys()[0] - host = hosts[hostname] - - hostname = host['hostname'] - port = host['port'] - - return hostname, port - - def _download_client_certificates(self): - cert_path = self._client_cert_path() - - if not os.path.exists(os.path.dirname(cert_path)): - os.makedirs(os.path.dirname(cert_path)) - - cert_url = '%s/%s/cert' % (self._provider.api_uri, self._provider.api_version) - cookies = {"_session_id": self.session_id} - headers = {} - headers["Authorization"] = 'Token token="{0}"'.format(self.user_token) - response = requests.get( - cert_url, - verify=LeapCertificate(self._provider).provider_api_cert, - cookies=cookies, - timeout=self._provider.config.timeout_in_s, - headers=headers) - response.raise_for_status() - - client_cert = response.content - - with open(cert_path, 'w') as f: - f.write(client_cert) - - def _client_cert_path(self): - return os.path.join( - self._provider.config.leap_home, - "providers", - self._provider.domain, - "keys", "client", "smtp.pem") - def start(self): - self._download_client_certificates() - cert_path = self._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), + userid=str(self._smtp_config.account_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, + smtp_host=self._smtp_config.remote_smtp_host.encode('UTF-8'), + smtp_port=self._smtp_config.remote_smtp_port, + smtp_cert=self._smtp_config.cert_path, + smtp_key=self._smtp_config.cert_path, encrypted_only=False ) diff --git a/service/pixelated/config/services.py b/service/pixelated/config/services.py index 41a357dc..cd475228 100644 --- a/service/pixelated/config/services.py +++ b/service/pixelated/config/services.py @@ -1,7 +1,7 @@ from pixelated.adapter.mailstore.searchable_mailstore import SearchableMailStore from pixelated.adapter.services.mail_service import MailService from pixelated.adapter.model.mail import InputMail -from pixelated.adapter.services.mail_sender import MailSender +from pixelated.adapter.services.mail_sender import LocalSmtpMailSender, MailSender # , MailSender from pixelated.adapter.search import SearchEngine from pixelated.adapter.services.draft_service import DraftService from pixelated.adapter.listeners.mailbox_indexer_listener import listen_all_mailboxes @@ -55,11 +55,12 @@ class Services(object): self.search_engine = search_engine def setup_mail_service(self, leap_session, search_engine): - # if False: FIXME - # yield pixelated_mailboxes.add_welcome_mail_for_fresh_user() - pixelated_mail_sender = MailSender( + pixelated_mail_sender = LocalSmtpMailSender( leap_session.account_email(), leap_session.smtp) + + MailSender(leap_session.smtp._smtp_config, leap_session.nicknym.keymanager) + return MailService( pixelated_mail_sender, leap_session.mail_store, |