Make folders containing quotes work

imaputil.imapsplit did not cope with strings that contained encoded
quotation marks, e.g. a folder name '"Make" Magazine' would fail and
crash OfflineImap. Make it work by adapting the regex that we use to
extract the first quote to also work with encoded \" quotes. (We do no
sanity checks that there is an even number of such marks within a string
though)

This commit makes such folders work. This was reported and analyzed by
Mark Eichin.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
This commit is contained in:
Sebastian Spaeth 2012-01-20 11:01:27 +01:00
parent d2f8504757
commit c7df907cf6
2 changed files with 18 additions and 7 deletions

View File

@ -13,6 +13,9 @@ others.
New Features
------------
* Make folders containing quotes work rather than crashing
(reported by Mark Eichin)
Changes
-------

View File

@ -25,7 +25,12 @@ try: # python 2.6 has set() built in
except NameError:
from sets import Set as set
quotere = re.compile('^("(?:[^"]|\\\\")*")')
# find the first quote in a string
quotere = re.compile(
r"""(?P<quote>"(?:\\"|[^"])*") # Quote, possibly containing encoded
# quotation mark
\s*(?P<rest>.*)$ # Whitespace & remainder of string""",
re.VERBOSE)
def debug(*args):
msg = []
@ -128,6 +133,7 @@ def imapsplit(imapstring):
workstr = imapstring.strip()
retval = []
while len(workstr):
# handle parenthized fragments (...()...)
if workstr[0] == '(':
rparenc = 1 # count of right parenthesis to match
rpareni = 1 # position to examine
@ -141,9 +147,10 @@ def imapsplit(imapstring):
workstr = workstr[rpareni:].lstrip()
retval.append(parenlist)
elif workstr[0] == '"':
quotelist = quotere.search(workstr).group(1)
workstr = workstr[len(quotelist):].lstrip()
retval.append(quotelist)
# quoted fragments '"...\"..."'
m = quotere.match(workstr)
retval.append(m.group('quote'))
workstr = m.group('rest')
else:
splits = string.split(workstr, maxsplit = 1)
splitslen = len(splits)
@ -161,6 +168,7 @@ def imapsplit(imapstring):
elif splitslen == 0:
# There was not even an unquoted word.
break
getglobalui().warn("%s->%s" % (imapstring, retval))
return retval
flagmap = [('\\Seen', 'S'),