From 9d1c5cf7b5f700639f5655c0e55a745007dda32b Mon Sep 17 00:00:00 2001 From: Leonardo Pistone Date: Thu, 20 Dec 2018 15:08:35 +0100 Subject: [PATCH] Handle mailboxes with spaces The IMAP4 protocol optionally uses double quoting when mailboxes contain spaces. This is always allowed. So to fix this, first we use shlex to perform double-quote-aware splitting (a bit like bash would split command line arguments) and then we send folder names with quotes to the IMAP server. I tested this manually with a couple real IMAP servers. --- mail_check_mailbox_size/models/fetchmail.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mail_check_mailbox_size/models/fetchmail.py b/mail_check_mailbox_size/models/fetchmail.py index d1566a0e..ee8e400e 100644 --- a/mail_check_mailbox_size/models/fetchmail.py +++ b/mail_check_mailbox_size/models/fetchmail.py @@ -1,4 +1,5 @@ import logging +import shlex from odoo import models, fields, api, _ _logger = logging.getLogger(__name__) @@ -53,12 +54,11 @@ class Fetchmail(models.Model): number_of_messages_all = 0 size_all = 0 for item in list: - x = item.decode().split() + x = shlex.split(item.decode()) mailbox = x[-1] - # Select the desired folder result, number_of_messages = imap_server.select( - mailbox, readonly=1) + '"{}"'.format(mailbox), readonly=1) if result != 'OK': _logger.info( "Server %s responded %s for folder %s"