Correct IMAPAccount populate_handler

IMAP list with wrong delimiter could return [None], so trying with another delimiter in that case

darcs-hash:20080725215323-86b55-f4b7cc15e8b30aaebf2fe5233ab40e4409ebf9a2.gz
This commit is contained in:
David Rousselie
2008-07-25 23:53:23 +02:00
parent 5cdb8e8ba6
commit 56eb2f004c
2 changed files with 38 additions and 11 deletions

View File

@@ -435,7 +435,7 @@ class IMAPAccount(MailAccount):
current_folder = current_folder[folder] current_folder = current_folder[folder]
return current_folder.keys() return current_folder.keys()
def populate_handler(self): def populate_handler(self, try_other_delimiter=True, testing_delimiter="/"):
""" """
Handler called when populating account Handler called when populating account
""" """
@@ -445,20 +445,31 @@ class IMAPAccount(MailAccount):
typ, data = self.connection.list(self.mailbox) typ, data = self.connection.list(self.mailbox)
if typ == 'OK': if typ == 'OK':
line = data[0] line = data[0]
match = self._regexp_list.match(line) if line is None:
if match is not None: if try_other_delimiter:
self.delimiter = match.group(2) self.mailbox = self.mailbox.replace(testing_delimiter,
".")
self.populate_handler(False, ".")
return
else:
self.disconnect()
raise Exception("Cannot find mailbox " + self.mailbox)
else: else:
self.disconnect() match = self._regexp_list.match(line)
raise Exception("Cannot find delimiter for mailbox " if match is not None:
+ self.mailbox) self.delimiter = match.group(2)
else:
self.disconnect()
raise Exception("Cannot find delimiter for mailbox "
+ self.mailbox)
else: else:
self.disconnect() self.disconnect()
raise Exception("Cannot find mailbox " + self.mailbox) raise Exception("Cannot find mailbox " + self.mailbox)
self.disconnect() self.disconnect()
# replace any previous delimiter in self.mailbox by "/" # replace any previous delimiter in self.mailbox by "/"
if self.delimiter != "/": if self.delimiter != testing_delimiter:
self.mailbox = self.mailbox.replace(self.delimiter, "/") self.mailbox = self.mailbox.replace(testing_delimiter,
self.delimiter)
class POP3Account(MailAccount): class POP3Account(MailAccount):

View File

@@ -730,10 +730,10 @@ class IMAPAccount_TestCase(InheritableAccount_TestCase):
def test_populate_handler(self): def test_populate_handler(self):
self.assertEquals(".", self.imap_account.delimiter) self.assertEquals(".", self.imap_account.delimiter)
self.imap_account.mailbox = "INBOX.dir1.subdir2" self.imap_account.mailbox = "INBOX/dir1/subdir2"
def call_func(self): def call_func(self):
self.imap_account.populate_handler() self.imap_account.populate_handler()
self.assertEquals("INBOX/dir1/subdir2", self.imap_account.mailbox) self.assertEquals("INBOX.dir1.subdir2", self.imap_account.mailbox)
test_func = self.make_test(\ test_func = self.make_test(\
[lambda data: '* LIST () "." "INBOX.dir1.subdir2"\r\n' + \ [lambda data: '* LIST () "." "INBOX.dir1.subdir2"\r\n' + \
data.split()[0] + ' OK LIST completed\r\n'], data.split()[0] + ' OK LIST completed\r\n'],
@@ -741,6 +741,22 @@ class IMAPAccount_TestCase(InheritableAccount_TestCase):
call_func) call_func)
test_func() test_func()
def test_populate_handler_wrong_default_delimiter(self):
self.imap_account.delimiter = "/"
self.imap_account.mailbox = "INBOX/dir1/subdir2"
def call_func(self):
self.imap_account.populate_handler()
self.assertEquals("INBOX.dir1.subdir2", self.imap_account.mailbox)
self.assertEquals(".", self.imap_account.delimiter)
test_func = self.make_test(\
[lambda data: data.split()[0] + ' OK LIST completed\r\n',
lambda data: '* LIST () "." "INBOX.dir1.subdir2"\r\n' + \
data.split()[0] + ' OK LIST completed\r\n'],
["^[^ ]* LIST \"?INBOX/dir1/subdir2\"? \*",
"^[^ ]* LIST \"?INBOX.dir1.subdir2\"? \*"],
call_func)
test_func()
def test_populate_handler_wrong_mailbox(self): def test_populate_handler_wrong_mailbox(self):
self.assertEquals(".", self.imap_account.delimiter) self.assertEquals(".", self.imap_account.delimiter)
self.imap_account.mailbox = "INBOX.dir1.subdir2" self.imap_account.mailbox = "INBOX.dir1.subdir2"