From 2a0117b67f6ae92cedc1fe39d2d15bd484c87150 Mon Sep 17 00:00:00 2001 From: Dustin Demuth Date: Thu, 2 Jun 2016 10:52:00 +0200 Subject: [PATCH] According to https://github.com/martinrusev/imbox/issues/68 the software was altered, in order to use the systems SSL-Context as default. It allows to toggle the use of the SSL-Default Context. It does not allow to use a custom context. Considerations: * If No SSL-Validation should be done, the ImboxClass should be adapted, in order to achieve configuration of the ssl-context. This commit does not contain this alteration. Other Changes: * Reformatted long lines --- imbox/imap.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/imbox/imap.py b/imbox/imap.py index daef0f4..a40c55c 100644 --- a/imbox/imap.py +++ b/imbox/imap.py @@ -1,25 +1,34 @@ from imaplib import IMAP4, IMAP4_SSL import logging +import ssl as pythonssllib + logger = logging.getLogger(__name__) + class ImapTransport(object): - def __init__(self, hostname, port=None, ssl=False): + def __init__(self, hostname, port=None, ssl=True, usesslcontext=True): self.hostname = hostname self.port = port if ssl: - self.transport = IMAP4_SSL + if usesslcontext = True: + context = pythonssllib.create_default_context() + else: + context = None + if not self.port: self.port = 993 + self.server = self.IMAP4_SSL(self.hostname, self.port, + ssl_context=context) else: - self.transport = IMAP4 if not self.port: self.port = 143 - self.server = self.transport(self.hostname, self.port) - logger.debug("Created IMAP4 transport for {host}:{port}".format(host=self.hostname, port=self.port)) + self.server = self.IMAP4(self.hostname, self.port) + logger.debug("Created IMAP4 transport for {host}:{port}" + .format(host=self.hostname, port=self.port)) def list_folders(self): logger.debug("List all folders in mailbox") @@ -28,5 +37,6 @@ class ImapTransport(object): def connect(self, username, password): self.server.login(username, password) self.server.select() - logger.debug("Logged into server {} and selected mailbox 'INBOX'".format(self.hostname)) + logger.debug("Logged into server {} and selected mailbox 'INBOX'" + .format(self.hostname)) return self.server