forked from mirror/django-mailbox
Refactored the imap transport based on the tests
Testing the switch to uid-based IMAP calls required better testing The better testing revealed some code that wasn't so pretty I fixed the tests and the code
This commit is contained in:
parent
b2abec1d40
commit
ec18a01880
3 changed files with 88 additions and 88 deletions
|
|
@ -1,17 +1,21 @@
|
|||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
from imaplib import IMAP4, IMAP4_SSL
|
||||
|
||||
from .base import EmailTransport, MessageParseError
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
MAX_MESSAGE_SIZE = getattr(
|
||||
settings,
|
||||
'DJANGO_MAILBOX_MAX_MESSAGE_SIZE',
|
||||
False
|
||||
)
|
||||
|
||||
class ImapTransport(EmailTransport):
|
||||
def __init__(self, hostname, port=None, ssl=False, archive=''):
|
||||
MAX_MESSAGE_SIZE = getattr(
|
||||
settings,
|
||||
'DJANGO_MAILBOX_MAX_MESSAGE_SIZE',
|
||||
False
|
||||
)
|
||||
self.hostname = hostname
|
||||
self.port = port
|
||||
self.archive = archive
|
||||
|
|
@ -30,10 +34,9 @@ class ImapTransport(EmailTransport):
|
|||
typ, msg = self.server.login(username, password)
|
||||
self.server.select()
|
||||
|
||||
|
||||
def _get_all_message_ids(self):
|
||||
# Fetch all the message uids
|
||||
response, message_ids = self.server.uid('search', None, 'ALL', )
|
||||
response, message_ids = self.server.uid('search', None, 'ALL')
|
||||
return message_ids[0].split(' ')
|
||||
|
||||
def _get_small_message_ids(self, message_ids):
|
||||
|
|
@ -45,21 +48,18 @@ class ImapTransport(EmailTransport):
|
|||
status, data = self.server.uid(
|
||||
'fetch',
|
||||
','.join(message_ids),
|
||||
'(BODY.PEEK[HEADER] RFC822.SIZE BODYSTRUCTURE)'
|
||||
'(RFC822.SIZE)'
|
||||
)
|
||||
|
||||
for each_msg in data:
|
||||
if isinstance(each_msg, tuple):
|
||||
try:
|
||||
metadata, structure = each_msg[0].split(' BODYSTRUCTURE ')
|
||||
uid = metadata.split('(')[1].split(' ')[1]
|
||||
size = metadata.split('(')[1].split(' ')[3]
|
||||
if int(size) <= int(self.MAX_MSG_SIZE):
|
||||
safe_message_ids.append(uid)
|
||||
except ValueError, e:
|
||||
print "ValueError: %s working on %s" % (e, each_msg[0])
|
||||
print each_msg
|
||||
pass
|
||||
try:
|
||||
uid = each_msg.split(' ')[2]
|
||||
size = each_msg.split(' ')[4].rstrip(')')
|
||||
if int(size) <= int(self.MAX_MSG_SIZE):
|
||||
safe_message_ids.append(uid)
|
||||
except ValueError, e:
|
||||
logger.warning("ValueError: %s working on %s" % (e, each_msg[0]))
|
||||
pass
|
||||
return safe_message_ids
|
||||
|
||||
def get_message(self):
|
||||
|
|
@ -69,7 +69,6 @@ class ImapTransport(EmailTransport):
|
|||
if self.MAX_MSG_SIZE:
|
||||
message_ids = self._get_small_message_ids(message_ids)
|
||||
|
||||
|
||||
if not message_ids:
|
||||
return
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue