2002-06-19 06:09:11 +02:00
|
|
|
# IMAP utility module
|
|
|
|
# Copyright (C) 2002 John Goerzen
|
|
|
|
# <jgoerzen@complete.org>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2003-04-16 21:23:45 +02:00
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2002-06-19 06:09:11 +02:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
2006-08-12 06:15:55 +02:00
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-06-19 06:09:11 +02:00
|
|
|
|
2011-03-11 22:13:21 +01:00
|
|
|
import re
|
|
|
|
import string
|
|
|
|
import types
|
2011-01-05 17:00:55 +01:00
|
|
|
from offlineimap.ui import getglobalui
|
2002-07-23 21:55:18 +02:00
|
|
|
quotere = re.compile('^("(?:[^"]|\\\\")*")')
|
2002-10-01 20:57:56 +02:00
|
|
|
|
|
|
|
def debug(*args):
|
|
|
|
msg = []
|
|
|
|
for arg in args:
|
|
|
|
msg.append(str(arg))
|
2011-01-05 17:00:55 +01:00
|
|
|
getglobalui().debug('imap', " ".join(msg))
|
2002-06-19 06:09:11 +02:00
|
|
|
|
|
|
|
def dequote(string):
|
|
|
|
"""Takes a string which may or may not be quoted and returns it, unquoted.
|
|
|
|
This function does NOT consider parenthised lists to be quoted.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not (string[0] == '"' and string[-1] == '"'):
|
|
|
|
return string
|
|
|
|
string = string[1:-1] # Strip off quotes.
|
|
|
|
string = string.replace('\\"', '"')
|
|
|
|
string = string.replace('\\\\', '\\')
|
2002-10-01 20:57:56 +02:00
|
|
|
debug("dequote() returning:", string)
|
2002-06-19 06:09:11 +02:00
|
|
|
return string
|
|
|
|
|
2002-06-19 07:22:21 +02:00
|
|
|
def flagsplit(string):
|
|
|
|
if string[0] != '(' or string[-1] != ')':
|
|
|
|
raise ValueError, "Passed string '%s' is not a flag list" % string
|
2002-06-20 08:26:28 +02:00
|
|
|
return imapsplit(string[1:-1])
|
|
|
|
|
|
|
|
def options2hash(list):
|
|
|
|
retval = {}
|
|
|
|
counter = 0
|
|
|
|
while (counter < len(list)):
|
|
|
|
retval[list[counter]] = list[counter + 1]
|
|
|
|
counter += 2
|
2002-10-01 20:57:56 +02:00
|
|
|
debug("options2hash returning:", retval)
|
2002-06-20 08:26:28 +02:00
|
|
|
return retval
|
|
|
|
|
|
|
|
def flags2hash(string):
|
|
|
|
return options2hash(flagsplit(string))
|
2002-06-19 07:22:21 +02:00
|
|
|
|
2002-07-23 21:55:18 +02:00
|
|
|
def imapsplit(imapstring):
|
2002-06-19 06:09:11 +02:00
|
|
|
"""Takes a string from an IMAP conversation and returns a list containing
|
|
|
|
its components. One example string is:
|
|
|
|
|
|
|
|
(\\HasNoChildren) "." "INBOX.Sent"
|
|
|
|
|
|
|
|
The result from parsing this will be:
|
|
|
|
|
|
|
|
['(\\HasNoChildren)', '"."', '"INBOX.Sent"']"""
|
2002-10-01 20:57:56 +02:00
|
|
|
|
2002-10-07 22:18:02 +02:00
|
|
|
if type(imapstring) != types.StringType:
|
|
|
|
debug("imapsplit() got a non-string input; working around.")
|
|
|
|
# Sometimes, imaplib will throw us a tuple if the input
|
|
|
|
# contains a literal. See Python bug
|
|
|
|
# #619732 at https://sourceforge.net/tracker/index.php?func=detail&aid=619732&group_id=5470&atid=105470
|
|
|
|
# One example is:
|
|
|
|
# result[0] = '() "\\\\" Admin'
|
|
|
|
# result[1] = ('() "\\\\" {19}', 'Folder\\2')
|
|
|
|
#
|
|
|
|
# This function will effectively get result[0] or result[1], so
|
|
|
|
# if we get the result[1] version, we need to parse apart the tuple
|
|
|
|
# and figure out what to do with it. Each even-numbered
|
|
|
|
# part of it should end with the {} number, and each odd-numbered
|
|
|
|
# part should be directly a part of the result. We'll
|
|
|
|
# artificially quote it to help out.
|
|
|
|
retval = []
|
|
|
|
for i in range(len(imapstring)):
|
|
|
|
if i % 2: # Odd: quote then append.
|
|
|
|
arg = imapstring[i]
|
|
|
|
# Quote code lifted from imaplib
|
|
|
|
arg = arg.replace('\\', '\\\\')
|
|
|
|
arg = arg.replace('"', '\\"')
|
|
|
|
arg = '"%s"' % arg
|
|
|
|
debug("imapsplit() non-string [%d]: Appending %s" %\
|
|
|
|
(i, arg))
|
|
|
|
retval.append(arg)
|
|
|
|
else:
|
|
|
|
# Even -- we have a string that ends with a literal
|
|
|
|
# size specifier. We need to strip off that, then run
|
|
|
|
# what remains through the regular imapsplit parser.
|
|
|
|
# Recursion to the rescue.
|
|
|
|
arg = imapstring[i]
|
2002-10-08 22:18:11 +02:00
|
|
|
arg = re.sub('\{\d+\}$', '', arg)
|
2002-10-07 22:18:02 +02:00
|
|
|
debug("imapsplit() non-string [%d]: Feeding %s to recursion" %\
|
2002-10-10 07:37:37 +02:00
|
|
|
(i, arg))
|
2002-10-16 01:07:02 +02:00
|
|
|
retval.extend(imapsplit(arg))
|
2002-10-07 22:18:02 +02:00
|
|
|
debug("imapsplit() non-string: returning %s" % str(retval))
|
|
|
|
return retval
|
|
|
|
|
2002-07-23 21:55:18 +02:00
|
|
|
workstr = imapstring.strip()
|
2002-06-19 06:09:11 +02:00
|
|
|
retval = []
|
|
|
|
while len(workstr):
|
2002-07-23 21:55:18 +02:00
|
|
|
if workstr[0] == '(':
|
2003-01-03 03:05:14 +01:00
|
|
|
rparenc = 1 # count of right parenthesis to match
|
|
|
|
rpareni = 1 # position to examine
|
|
|
|
while rparenc: # Find the end of the group.
|
|
|
|
if workstr[rpareni] == ')': # end of a group
|
|
|
|
rparenc -= 1
|
|
|
|
elif workstr[rpareni] == '(': # start of a group
|
|
|
|
rparenc += 1
|
|
|
|
rpareni += 1 # Move to next character.
|
2002-07-23 21:55:18 +02:00
|
|
|
parenlist = workstr[0:rpareni]
|
|
|
|
workstr = workstr[rpareni:].lstrip()
|
2002-06-19 06:09:11 +02:00
|
|
|
retval.append(parenlist)
|
|
|
|
elif workstr[0] == '"':
|
2002-07-23 21:55:18 +02:00
|
|
|
quotelist = quotere.search(workstr).group(1)
|
|
|
|
workstr = workstr[len(quotelist):].lstrip()
|
2002-06-19 06:09:11 +02:00
|
|
|
retval.append(quotelist)
|
|
|
|
else:
|
2002-07-23 21:55:18 +02:00
|
|
|
splits = string.split(workstr, maxsplit = 1)
|
|
|
|
splitslen = len(splits)
|
|
|
|
# The unquoted word is splits[0]; the remainder is splits[1]
|
|
|
|
if splitslen == 2:
|
|
|
|
# There's an unquoted word, and more string follows.
|
|
|
|
retval.append(splits[0])
|
|
|
|
workstr = splits[1] # split will have already lstripped it
|
|
|
|
continue
|
|
|
|
elif splitslen == 1:
|
|
|
|
# We got a last unquoted word, but nothing else
|
|
|
|
retval.append(splits[0])
|
|
|
|
# Nothing remains. workstr would be ''
|
|
|
|
break
|
|
|
|
elif splitslen == 0:
|
|
|
|
# There was not even an unquoted word.
|
|
|
|
break
|
2002-06-19 06:09:11 +02:00
|
|
|
return retval
|
|
|
|
|
2010-12-08 18:46:46 +01:00
|
|
|
flagmap = [('\\Seen', 'S'),
|
|
|
|
('\\Answered', 'R'),
|
|
|
|
('\\Flagged', 'F'),
|
|
|
|
('\\Deleted', 'T'),
|
|
|
|
('\\Draft', 'D')]
|
|
|
|
|
2002-07-24 01:36:44 +02:00
|
|
|
def flagsimap2maildir(flagstring):
|
2002-06-20 09:40:29 +02:00
|
|
|
retval = []
|
2002-07-24 01:36:44 +02:00
|
|
|
imapflaglist = [x.lower() for x in flagstring[1:-1].split()]
|
2010-12-08 18:46:46 +01:00
|
|
|
for imapflag, maildirflag in flagmap:
|
|
|
|
if imapflag.lower() in imapflaglist:
|
|
|
|
retval.append(maildirflag)
|
2002-06-20 09:40:29 +02:00
|
|
|
retval.sort()
|
|
|
|
return retval
|
|
|
|
|
2010-12-08 18:46:46 +01:00
|
|
|
def flagsmaildir2imap(maildirflaglist):
|
2002-06-20 09:40:29 +02:00
|
|
|
retval = []
|
2010-12-08 18:46:46 +01:00
|
|
|
for imapflag, maildirflag in flagmap:
|
|
|
|
if maildirflag in maildirflaglist:
|
|
|
|
retval.append(imapflag)
|
2002-06-20 09:40:29 +02:00
|
|
|
retval.sort()
|
2002-06-21 03:55:06 +02:00
|
|
|
return '(' + ' '.join(retval) + ')'
|
2002-06-20 09:40:29 +02:00
|
|
|
|
2011-08-22 12:21:11 +02:00
|
|
|
def uid_sequence(uidlist):
|
|
|
|
"""Collapse UID lists into shorter sequence sets
|
|
|
|
|
|
|
|
[1,2,3,4,5,10,12,13] will return "1:5,10,12:13". This function does
|
|
|
|
not sort the list, and only collapses if subsequent entries form a
|
|
|
|
range.
|
|
|
|
:returns: The collapsed UID list as string"""
|
|
|
|
def getrange(start, end):
|
2002-07-16 03:46:21 +02:00
|
|
|
if start == end:
|
|
|
|
return(str(start))
|
2011-08-22 12:21:11 +02:00
|
|
|
return "%s:%s" % (start, end)
|
2002-07-16 03:46:21 +02:00
|
|
|
|
2011-08-22 12:21:11 +02:00
|
|
|
if not len(uidlist): return '' # Empty list, return
|
|
|
|
start, end = None, None
|
|
|
|
retval = []
|
2002-07-16 03:46:21 +02:00
|
|
|
|
2011-08-22 12:21:11 +02:00
|
|
|
for item in iter(uidlist):
|
|
|
|
item = int(item)
|
|
|
|
if start == None: # First item
|
|
|
|
start, end = item, item
|
|
|
|
elif item == end + 1: # Next item in a range
|
|
|
|
end = item
|
|
|
|
else: # Starting a new range
|
|
|
|
retval.append(getrange(start, end))
|
|
|
|
start, end = item, item
|
2002-07-16 03:46:21 +02:00
|
|
|
|
2011-08-22 12:21:11 +02:00
|
|
|
retval.append(getrange(start, end)) # Add final range/item
|
2002-07-16 03:46:21 +02:00
|
|
|
return ",".join(retval)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|