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
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
2002-07-23 21:55:18 +02:00
|
|
|
import re, string
|
|
|
|
quotere = re.compile('^("(?:[^"]|\\\\")*")')
|
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('\\\\', '\\')
|
|
|
|
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
|
|
|
|
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-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] == '(':
|
|
|
|
# Needs rindex to properly process eg (FLAGS () UID 123)
|
|
|
|
rpareni = workstr.rindex(')') + 1
|
|
|
|
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
|
|
|
|
|
2002-07-24 01:36:44 +02:00
|
|
|
def flagsimap2maildir(flagstring):
|
2002-07-06 02:22:23 +02:00
|
|
|
flagmap = {'\\seen': 'S',
|
|
|
|
'\\answered': 'R',
|
|
|
|
'\\flagged': 'F',
|
|
|
|
'\\deleted': 'T',
|
|
|
|
'\\draft': 'D'}
|
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()]
|
2002-06-20 09:40:29 +02:00
|
|
|
for imapflag in imapflaglist:
|
|
|
|
if flagmap.has_key(imapflag):
|
|
|
|
retval.append(flagmap[imapflag])
|
|
|
|
retval.sort()
|
|
|
|
return retval
|
|
|
|
|
|
|
|
def flagsmaildir2imap(list):
|
|
|
|
flagmap = {'S': '\\Seen',
|
|
|
|
'R': '\\Answered',
|
|
|
|
'F': '\\Flagged',
|
|
|
|
'T': '\\Deleted',
|
|
|
|
'D': '\\Draft'}
|
|
|
|
retval = []
|
|
|
|
for mdflag in list:
|
|
|
|
if flagmap.has_key(mdflag):
|
|
|
|
retval.append(flagmap[mdflag])
|
|
|
|
retval.sort()
|
2002-06-21 03:55:06 +02:00
|
|
|
return '(' + ' '.join(retval) + ')'
|
2002-06-20 09:40:29 +02:00
|
|
|
|
2002-07-16 03:46:21 +02:00
|
|
|
def listjoin(list):
|
|
|
|
start = None
|
|
|
|
end = None
|
|
|
|
retval = []
|
|
|
|
|
|
|
|
def getlist(start, end):
|
|
|
|
if start == end:
|
|
|
|
return(str(start))
|
|
|
|
else:
|
|
|
|
return(str(start) + ":" + str(end))
|
|
|
|
|
|
|
|
|
|
|
|
for item in list:
|
|
|
|
if start == None:
|
|
|
|
# First item.
|
|
|
|
start = item
|
|
|
|
end = item
|
|
|
|
elif item == end + 1:
|
|
|
|
# An addition to the list.
|
|
|
|
end = item
|
|
|
|
else:
|
|
|
|
# Here on: starting a new list.
|
|
|
|
retval.append(getlist(start, end))
|
|
|
|
start = item
|
|
|
|
end = item
|
|
|
|
|
|
|
|
if start != None:
|
|
|
|
retval.append(getlist(start, end))
|
|
|
|
|
|
|
|
return ",".join(retval)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|