Skip to content

Commit

Permalink
Fix improper header separator for X-OfflineIMAP header
Browse files Browse the repository at this point in the history
For servers without UIDPLUS we are inserting additional header
just after transformation '\n' -> CRLF was done.  addmessageheaders()
was written to work with just '\n' as the separator, so X-OfflineIMAP
header wasn't preceeded by the CRLF, but just by '\n'.

Signed-off-by: Eygene Ryabinkin <rea@codelabs.ru>
  • Loading branch information
konvpalto committed Jun 1, 2014
1 parent 7770b5f commit e8db121
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
10 changes: 9 additions & 1 deletion Changelog.rst
Expand Up @@ -5,7 +5,15 @@ ChangeLog
:website: http://offlineimap.org


OfflineIMAP v6.5.6 (YYYY-MM-DD)
OfflineIMAP v6.5.6.1 (YYYY-MM-DD)
=================================

* Fix mangled message headers for servers without UIDPLUS:
X-OfflineIMAP was added with preceeding '\n' instead of
'\r\n' just before message was uploaded to the IMAP server.


OfflineIMAP v6.5.6 (2014-05-14)
===============================

* Fix IDLE mode regression (it didn't worked) introduced
Expand Down
22 changes: 16 additions & 6 deletions offlineimap/folder/Base.py
Expand Up @@ -413,25 +413,35 @@ def deletemessageslabels(self, uidlist, labels):
"""

def addmessageheader(self, content, headername, headervalue):
def addmessageheader(self, content, crlf, headername, headervalue):
"""
Adds new header to the provided message.
Arguments:
- content: message content, headers and body as a single string
- crlf: string that carries line ending
- headername: name of the header to add
- headervalue: value of the header to add
"""
self.ui.debug('',
'addmessageheader: called to add %s: %s' % (headername,
headervalue))
prefix = '\n'
prefix = crlf
suffix = ''
insertionpoint = content.find('\n\n')
insertionpoint = content.find(crlf + crlf)
if insertionpoint == 0 or insertionpoint == -1:
prefix = ''
suffix = '\n'
suffix = crlf
if insertionpoint == -1:
insertionpoint = 0
# When body starts immediately, without preceding '\n'
# (this shouldn't happen with proper mail messages, but
# we seen many broken ones), we should add '\n' to make
# new (and the only header, in this case) to be properly
# separated from the message body.
if content[0] != '\n':
suffix = suffix + '\n'
if content[0:len(crlf)] != crlf:
suffix = suffix + crlf

self.ui.debug('', 'addmessageheader: insertionpoint = %d' % insertionpoint)
headers = content[0:insertionpoint]
Expand Down
2 changes: 1 addition & 1 deletion offlineimap/folder/Gmail.py
Expand Up @@ -93,7 +93,7 @@ def getmessage(self, uid):
labels = set()
labels = labels - self.ignorelabels
labels_str = imaputil.format_labels_string(self.labelsheader, sorted(labels))
body = self.addmessageheader(body, self.labelsheader, labels_str)
body = self.addmessageheader(body, '\n', self.labelsheader, labels_str)

if len(body)>200:
dbg_output = "%s...%s" % (str(body)[:150], str(body)[-50:])
Expand Down
2 changes: 1 addition & 1 deletion offlineimap/folder/GmailMaildir.py
Expand Up @@ -141,7 +141,7 @@ def savemessagelabels(self, uid, labels, ignorelabels=set()):
# Change labels into content
labels_str = imaputil.format_labels_string(self.labelsheader,
sorted(labels | ignoredlabels))
content = self.addmessageheader(content, self.labelsheader, labels_str)
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
Expand Down
3 changes: 2 additions & 1 deletion offlineimap/folder/IMAP.py
Expand Up @@ -526,6 +526,7 @@ def savemessage(self, uid, content, flags, rtime):
# NB: imapobj to None.
try:
while retry_left:
# XXX: we can mangle message only once, out of the loop
# UIDPLUS extension provides us with an APPENDUID response.
use_uidplus = 'UIDPLUS' in imapobj.capabilities

Expand All @@ -535,7 +536,7 @@ def savemessage(self, uid, content, flags, rtime):
content)
self.ui.debug('imap', 'savemessage: header is: %s: %s' %\
(headername, headervalue))
content = self.addmessageheader(content, headername, headervalue)
content = self.addmessageheader(content, CRLF, headername, headervalue)

if len(content)>200:
dbg_output = "%s...%s" % (content[:150], content[-50:])
Expand Down

0 comments on commit e8db121

Please sign in to comment.