utf8: implement utf8foldernames option

If utf8foldernames is enabled on account level all folder names read
from the IMAP server will immediately be reencoded to UTF-8. Names
will be treated as UTF-8 as long as the IMAP server isn't contacted again,
for which they are reencoded to IMAP4-UTF-7.

This means that any further processing such as nametrans, folderfilter
etc. will act upon the UTF-8 names, which will have to be documented
carefully.

NOTE 1:
GMail repositories and folders inherit from the IMAP... classes, so I don't
know yet if these changes have ugly side-effects. But web research suggests
that GMail IMAP folders are equally encoded in UTF-7 so that should work
identically here and incorporate the same improvements.

NOTE 2:
I could not test the behaviour with idlefolders as I didn't get this option
to work at all, not even with the latest stable version.

NOTE 3:
I *did* test to sync an IMAP repository against another IMAP repository.

Signed-off-by: Urs Liska <git@ursliska.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
This commit is contained in:
Urs Liska
2017-10-02 01:26:29 +02:00
committed by Nicolas Sebrecht
parent 14d83dbf48
commit 36d726763d
6 changed files with 42 additions and 25 deletions

View File

@ -41,10 +41,17 @@ MSGCOPY_NAMESPACE = 'MSGCOPY_'
class IMAPFolder(BaseFolder):
def __init__(self, imapserver, name, repository):
# FIXME: decide if unquoted name is from the responsability of the
# caller or not, but not both.
def __init__(self, imapserver, name, repository, decode=True):
# decode the folder name from IMAP4_utf_7 to utf_8 if
# - utf8foldernames is enabled for the *account*
# - the decode argument is given
# (default True is used when the folder name is the result of
# querying the IMAP server, while False is used when creating
# a folder object from a locally available utf_8 name)
# In any case the given name is first dequoted.
name = imaputil.dequote(name)
if decode and repository.account.utf_8_support:
name = imaputil.IMAP_utf8(name)
self.sep = imapserver.delim
super(IMAPFolder, self).__init__(name, repository)
if repository.getdecodefoldernames():
@ -69,7 +76,6 @@ class IMAPFolder(BaseFolder):
if self.repository.getidlefolders():
self.idle_mode = True
def __selectro(self, imapobj, force=False):
"""Select this folder when we do not need write access.
@ -80,9 +86,15 @@ class IMAPFolder(BaseFolder):
:param: Enforce new SELECT even if we are on that folder already.
:returns: raises :exc:`OfflineImapError` severity FOLDER on error"""
try:
imapobj.select(self.getfullname(), force = force)
imapobj.select(self.getfullIMAPname(), force = force)
except imapobj.readonly:
imapobj.select(self.getfullname(), readonly = True, force = force)
imapobj.select(self.getfullIMAPname(), readonly = True, force = force)
def getfullIMAPname(self):
name = self.getfullname()
if self.repository.account.utf_8_support:
name = imaputil.utf8_IMAP(name)
return name
# Interface from BaseFolder
def suggeststhreads(self):
@ -147,7 +159,7 @@ class IMAPFolder(BaseFolder):
imapobj = self.imapserver.acquireconnection()
try:
# Select folder and get number of messages.
restype, imapdata = imapobj.select(self.getfullname(), True,
restype, imapdata = imapobj.select(self.getfullIMAPname(), True,
True)
self.imapserver.releaseconnection(imapobj)
except OfflineImapError as e:
@ -213,7 +225,7 @@ class IMAPFolder(BaseFolder):
res_data.remove(0)
return res_data
res_type, imapdata = imapobj.select(self.getfullname(), True, True)
res_type, imapdata = imapobj.select(self.getfullIMAPname(), True, True)
if imapdata == [None] or imapdata[0] == '0':
# Empty folder, no need to populate message list.
return None
@ -630,7 +642,7 @@ class IMAPFolder(BaseFolder):
try:
# Select folder for append and make the box READ-WRITE.
imapobj.select(self.getfullname())
imapobj.select(self.getfullIMAPname())
except imapobj.readonly:
# readonly exception. Return original uid to notify that
# we did not save the message. (see savemessage in Base.py)
@ -639,7 +651,7 @@ class IMAPFolder(BaseFolder):
# Do the APPEND.
try:
(typ, dat) = imapobj.append(self.getfullname(),
(typ, dat) = imapobj.append(self.getfullIMAPname(),
imaputil.flagsmaildir2imap(flags), date, content)
# This should only catch 'NO' responses since append()
# will raise an exception for 'BAD' responses:
@ -753,7 +765,7 @@ class IMAPFolder(BaseFolder):
fails_left = retry_num # Retry on dropped connection.
while fails_left:
try:
imapobj.select(self.getfullname(), readonly=True)
imapobj.select(self.getfullIMAPname(), readonly=True)
res_type, data = imapobj.uid('fetch', uids, query)
break
except imapobj.abort as e:
@ -813,7 +825,7 @@ class IMAPFolder(BaseFolder):
- field: field name to be stored/updated
- data: field contents
"""
imapobj.select(self.getfullname())
imapobj.select(self.getfullIMAPname())
res_type, retdata = imapobj.uid('store', uid, field, data)
if res_type != 'OK':
severity = OfflineImapError.ERROR.MESSAGE
@ -874,7 +886,7 @@ class IMAPFolder(BaseFolder):
imapobj = self.imapserver.acquireconnection()
try:
try:
imapobj.select(self.getfullname())
imapobj.select(self.getfullIMAPname())
except imapobj.readonly:
self.ui.flagstoreadonly(self, uidlist, flags)
return
@ -949,7 +961,7 @@ class IMAPFolder(BaseFolder):
imapobj = self.imapserver.acquireconnection()
try:
try:
imapobj.select(self.getfullname())
imapobj.select(self.getfullIMAPname())
except imapobj.readonly:
self.ui.deletereadonly(self, uidlist)
return

View File

@ -40,8 +40,8 @@ class MappedIMAPFolder(IMAPFolder):
diskr2l: dict mapping message uids: self.r2l[remoteuid]=localuid
diskl2r: dict mapping message uids: self.r2l[localuid]=remoteuid"""
def __init__(self, *args, **kwargs):
IMAPFolder.__init__(self, *args, **kwargs)
def __init__(self, imapserver, name, repository, decode=True):
IMAPFolder.__init__(self, imapserver, name, repository, decode=False)
self.dryrun = self.config.getdefaultboolean("general", "dry-run", True)
self.maplock = Lock()
self.diskr2l, self.diskl2r = self._loadmaps()
@ -49,7 +49,7 @@ class MappedIMAPFolder(IMAPFolder):
# Representing the local IMAP Folder using local UIDs.
# XXX: This should be removed since we inherit from IMAPFolder.
# See commit 3ce514e92ba7 to know more.
self._mb = IMAPFolder(*args, **kwargs)
self._mb = IMAPFolder(imapserver, name, repository, decode=False)
def _getmapfilename(self):
return os.path.join(self.repository.getmapdir(),