Merge pull request #136 from aroig/gh/label-fix
Fix issue with multiple instances of gmail labels header
This commit is contained in:
commit
9b2c275561
@ -401,6 +401,9 @@ class BaseFolder(object):
|
||||
"""
|
||||
Adds new header to the provided message.
|
||||
|
||||
WARNING: This function is a bit tricky, and modifying it in the wrong way,
|
||||
may easily lead to data-loss.
|
||||
|
||||
Arguments:
|
||||
- content: message content, headers and body as a single string
|
||||
- linebreak: string that carries line ending
|
||||
@ -512,8 +515,8 @@ class BaseFolder(object):
|
||||
|
||||
def getmessageheader(self, content, name):
|
||||
"""
|
||||
Searches for the given header and returns its value.
|
||||
Header name is case-insensitive.
|
||||
Searches for the first occurence of the given header and returns
|
||||
its value. Header name is case-insensitive.
|
||||
|
||||
Arguments:
|
||||
- contents: message itself
|
||||
@ -535,6 +538,27 @@ class BaseFolder(object):
|
||||
return None
|
||||
|
||||
|
||||
def getmessageheaderlist(self, content, name):
|
||||
"""
|
||||
Searches for the given header and returns a list of values for
|
||||
that header.
|
||||
|
||||
Arguments:
|
||||
- contents: message itself
|
||||
- name: name of the header to be searched
|
||||
|
||||
Returns: list of header values or emptylist if no such header was found
|
||||
|
||||
"""
|
||||
self.ui.debug('', 'getmessageheaderlist: called to get %s' % name)
|
||||
eoh = self.__find_eoh(content)
|
||||
self.ui.debug('', 'getmessageheaderlist: eoh = %d' % eoh)
|
||||
headers = content[0:eoh]
|
||||
self.ui.debug('', 'getmessageheaderlist: headers = %s' % repr(headers))
|
||||
|
||||
return re.findall('^%s:(.*)$' % name, headers, flags = re.MULTILINE | re.IGNORECASE)
|
||||
|
||||
|
||||
def deletemessageheaders(self, content, header_list):
|
||||
"""
|
||||
Deletes headers in the given list from the message content.
|
||||
|
@ -93,6 +93,10 @@ class GmailFolder(IMAPFolder):
|
||||
labels = set()
|
||||
labels = labels - self.ignorelabels
|
||||
labels_str = imaputil.format_labels_string(self.labelsheader, sorted(labels))
|
||||
|
||||
# First remove old label headers that may be in the message content retrieved
|
||||
# from gmail Then add a labels header with current gmail labels.
|
||||
body = self.deletemessageheaders(body, self.labelsheader)
|
||||
body = self.addmessageheader(body, '\n', self.labelsheader, labels_str)
|
||||
|
||||
if len(body)>200:
|
||||
@ -189,8 +193,9 @@ class GmailFolder(IMAPFolder):
|
||||
if not self.synclabels:
|
||||
return super(GmailFolder, self).savemessage(uid, content, flags, rtime)
|
||||
|
||||
labels = imaputil.labels_from_header(self.labelsheader,
|
||||
self.getmessageheader(content, self.labelsheader))
|
||||
labels = set()
|
||||
for hstr in self.getmessageheaderlist(content, self.labelsheader):
|
||||
labels.update(imaputil.labels_from_header(self.labelsheader, hstr))
|
||||
|
||||
ret = super(GmailFolder, self).savemessage(uid, content, flags, rtime)
|
||||
self.savemessagelabels(ret, labels)
|
||||
|
@ -87,9 +87,10 @@ class GmailMaildirFolder(MaildirFolder):
|
||||
content = file.read()
|
||||
file.close()
|
||||
|
||||
self.messagelist[uid]['labels'] = \
|
||||
imaputil.labels_from_header(self.labelsheader,
|
||||
self.getmessageheader(content, self.labelsheader))
|
||||
self.messagelist[uid]['labels'] = set()
|
||||
for hstr in self.getmessageheaderlist(content, self.labelsheader):
|
||||
self.messagelist[uid]['labels'].update(
|
||||
imaputil.labels_from_header(self.labelsheader, hstr))
|
||||
self.messagelist[uid]['labels_cached'] = True
|
||||
|
||||
return self.messagelist[uid]['labels']
|
||||
@ -111,8 +112,10 @@ class GmailMaildirFolder(MaildirFolder):
|
||||
if not self.synclabels:
|
||||
return super(GmailMaildirFolder, self).savemessage(uid, content, flags, rtime)
|
||||
|
||||
labels = imaputil.labels_from_header(self.labelsheader,
|
||||
self.getmessageheader(content, self.labelsheader))
|
||||
labels = set()
|
||||
for hstr in self.getmessageheaderlist(content, self.labelsheader):
|
||||
labels.update(imaputil.labels_from_header(self.labelsheader, hstr))
|
||||
|
||||
ret = super(GmailMaildirFolder, self).savemessage(uid, content, flags, rtime)
|
||||
|
||||
# Update the mtime and labels
|
||||
@ -135,9 +138,9 @@ class GmailMaildirFolder(MaildirFolder):
|
||||
content = file.read()
|
||||
file.close()
|
||||
|
||||
oldlabels = imaputil.labels_from_header(self.labelsheader,
|
||||
self.getmessageheader(content, self.labelsheader))
|
||||
|
||||
oldlabels = set()
|
||||
for hstr in self.getmessageheaderlist(content, self.labelsheader):
|
||||
oldlabels.update(imaputil.labels_from_header(self.labelsheader, hstr))
|
||||
|
||||
labels = labels - ignorelabels
|
||||
ignoredlabels = oldlabels & ignorelabels
|
||||
@ -150,7 +153,11 @@ class GmailMaildirFolder(MaildirFolder):
|
||||
# Change labels into content
|
||||
labels_str = imaputil.format_labels_string(self.labelsheader,
|
||||
sorted(labels | ignoredlabels))
|
||||
|
||||
# First remove old labels header, and then add the new one
|
||||
content = self.deletemessageheaders(content, self.labelsheader)
|
||||
content = self.addmessageheader(content, '\n', self.labelsheader, labels_str)
|
||||
|
||||
rtime = self.messagelist[uid].get('rtime', None)
|
||||
|
||||
# write file with new labels to a unique file in tmp
|
||||
|
Loading…
Reference in New Issue
Block a user