Skip to content

Commit

Permalink
Fixed bug on folders names with space in folder.MailBoxFolderManager.…
Browse files Browse the repository at this point in the history
…exists/list
  • Loading branch information
ikvk committed Apr 23, 2020
1 parent 2643868 commit 071c58e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,4 @@ Thanks to:
* `Mitrich3000 <https://github.com/Mitrich3000>`_
* `audemed44 <https://github.com/audemed44>`_
* `mkalioby <https://github.com/mkalioby>`_
* `atlas0fd00m <https://github.com/atlas0fd00m>`_
11 changes: 6 additions & 5 deletions imap_tools/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,16 @@ def list(self, folder: str or bytes = '', search_args: str = '*', subscribed_onl
"""
folder_item_re = re.compile(r'\((?P<flags>[\S ]*)\) "(?P<delim>[\S ]+)" (?P<name>.+)')
command = 'LSUB' if subscribed_only else 'LIST'
typ, data = self.mailbox.box._simple_command(command, self._encode_folder(folder), search_args)
typ, data = self.mailbox.box._simple_command(
command, self._encode_folder(folder), self._encode_folder(search_args))
typ, data = self.mailbox.box._untagged_response(typ, data, command)
result = list()
for folder_item in data:
if not folder_item:
continue
folder_match = re.search(folder_item_re, imap_utf7.decode(folder_item))
folder = folder_match.groupdict()
if folder['name'].startswith('"') and folder['name'].endswith('"'):
folder['name'] = folder['name'][1:len(folder['name']) - 1]
result.append(folder)
folder_dict = folder_match.groupdict()
if folder_dict['name'].startswith('"') and folder_dict['name'].endswith('"'):
folder_dict['name'] = folder_dict['name'][1:-1]
result.append(folder_dict)
return result
32 changes: 16 additions & 16 deletions imap_tools/imap_utf7.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

# ENCODING
# --------
def _modified_base64(s):
return binascii.b2a_base64(s.encode('utf-16be')).rstrip(b'\n=').replace(b'/', b',')
def _modified_base64(value: str):
return binascii.b2a_base64(value.encode('utf-16be')).rstrip(b'\n=').replace(b'/', b',')


def _do_b64(_in, r):
Expand All @@ -22,45 +22,45 @@ def _do_b64(_in, r):
del _in[:]


def encode(s: str) -> bytes:
def encode(value: str) -> bytes:
res = []
_in = []
for c in s:
ord_c = ord(c)
for char in value:
ord_c = ord(char)
if 0x20 <= ord_c <= 0x25 or 0x27 <= ord_c <= 0x7e:
_do_b64(_in, res)
res.append(c.encode())
elif c == '&':
res.append(char.encode())
elif char == '&':
_do_b64(_in, res)
res.append(b'&-')
else:
_in.append(c)
_in.append(char)
_do_b64(_in, res)
return b''.join(res)


# DECODING
# --------
def _modified_unbase64(s):
return binascii.a2b_base64(s.replace(b',', b'/') + b'===').decode('utf-16be')
def _modified_unbase64(value: bytearray):
return binascii.a2b_base64(value.replace(b',', b'/') + b'===').decode('utf-16be')


def decode(s: bytes) -> str:
def decode(value: bytes) -> str:
res = []
decode_arr = bytearray()
for c in s:
if c == ord('&') and not decode_arr:
for char in value:
if char == ord('&') and not decode_arr:
decode_arr.append(ord('&'))
elif c == ord('-') and decode_arr:
elif char == ord('-') and decode_arr:
if len(decode_arr) == 1:
res.append('&')
else:
res.append(_modified_unbase64(decode_arr[1:]))
decode_arr = bytearray()
elif decode_arr:
decode_arr.append(c)
decode_arr.append(char)
else:
res.append(chr(c))
res.append(chr(char))
if decode_arr:
res.append(_modified_unbase64(decode_arr[1:]))
return ''.join(res)
4 changes: 4 additions & 0 deletions release_notes.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.14.1
======
* Fixed bug on folders names with space in folder.MailBoxFolderManager.exists/list

0.14.0
======
* Improved parse logic for message.MailMessage.flags
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='imap_tools',
version='0.14.0',
version='0.14.1',
packages=setuptools.find_packages(),
url='https://github.com/ikvk/imap_tools',
license='Apache-2.0',
Expand Down
2 changes: 2 additions & 0 deletions todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ try to imitate long poll by NOOP
check https://docs.python.org/release/3.8.1/library/email.utils.html

check possibility to implement SOCKS without/with dependencies

folder.list fails on names with \\ "" chars

0 comments on commit 071c58e

Please sign in to comment.