Make GmailFolder sync GMail labels

When synclabels config flag is set to "yes" for the GMail repo,
offlineimap fetches the message labels along with the messages, and
embeds them into the body under the header X-Keywords (or whatever
'labelsheader' was set to), as a comma separated list.

It also adds an extra pass to savemessageto, that performs label
synchronization on existing messages from GMail to local, the same way
it is done with flags.

We also introduce GmailMaildir repository that adds functionality to
change message labels.  It keeps track of messages modification time,
so one can quickly detect when the labels may have changed.

Signed-off-by: Eygene Ryabinkin <rea@codelabs.ru>
This commit is contained in:
Abdó Roig-Maranges
2012-10-16 20:20:35 +02:00
committed by Eygene Ryabinkin
parent 9319ae212b
commit 0e4afa9132
15 changed files with 1071 additions and 69 deletions

View File

@ -36,6 +36,13 @@ class GmailRepository(IMAPRepository):
'ssl', 'yes')
IMAPRepository.__init__(self, reposname, account)
if self.account.getconfboolean('synclabels', 0) and \
self.account.getconf('status_backend', 'plain') != 'sqlite':
raise OfflineImapError("The Gmail repository needs the sqlite backend to sync labels.\n"
"To enable it add 'status_backend = sqlite' in the account section",
OfflineImapError.ERROR.REPO)
def gethost(self):
"""Return the server name to connect to.
@ -71,4 +78,3 @@ class GmailRepository(IMAPRepository):
def getspamfolder(self):
#: Gmail also deletes messages upon EXPUNGE in the Spam folder
return self.getconf('spamfolder','[Gmail]/Spam')

View File

@ -0,0 +1,37 @@
# Maildir repository support
# 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
from offlineimap.repository.Maildir import MaildirRepository
from offlineimap.folder.GmailMaildir import GmailMaildirFolder
from offlineimap.error import OfflineImapError
class GmailMaildirRepository(MaildirRepository):
def __init__(self, reposname, account):
"""Initialize a MaildirRepository object. Takes a path name
to the directory holding all the Maildir directories."""
super(GmailMaildirRepository, self).__init__(reposname, account)
if self.account.getconfboolean('synclabels', 0) and \
self.account.getconf('status_backend', 'plain') != 'sqlite':
raise OfflineImapError("The GmailMaildir repository needs the sqlite backend to sync labels.\n"
"To enable it add 'status_backend = sqlite' in the account section",
OfflineImapError.ERROR.REPO)
def getfoldertype(self):
return GmailMaildirFolder

View File

@ -177,10 +177,9 @@ class MaildirRepository(BaseRepository):
self.debug(" This is maildir folder '%s'." % foldername)
if self.getconfboolean('restoreatime', False):
self._append_folder_atimes(foldername)
retval.append(folder.Maildir.MaildirFolder(self.root,
foldername,
self.getsep(),
self))
fd = self.getfoldertype()(self.root, foldername,
self.getsep(), self)
retval.append(fd)
if self.getsep() == '/' and dirname != '':
# Recursively check sub-directories for folders too.
@ -194,6 +193,9 @@ class MaildirRepository(BaseRepository):
self.folders = self._getfolders_scandir(self.root)
return self.folders
def getfoldertype(self):
return folder.Maildir.MaildirFolder
def forgetfolders(self):
"""Forgets the cached list of folders, if any. Useful to run
after a sync run."""

View File

@ -23,6 +23,7 @@ except ImportError: #python2
from offlineimap.repository.IMAP import IMAPRepository, MappedIMAPRepository
from offlineimap.repository.Gmail import GmailRepository
from offlineimap.repository.Maildir import MaildirRepository
from offlineimap.repository.GmailMaildir import GmailMaildirRepository
from offlineimap.repository.LocalStatus import LocalStatusRepository
from offlineimap.error import OfflineImapError
@ -46,7 +47,8 @@ class Repository(object):
elif reqtype == 'local':
name = account.getconf('localrepository')
typemap = {'IMAP': MappedIMAPRepository,
'Maildir': MaildirRepository}
'Maildir': MaildirRepository,
'GmailMaildir': GmailMaildirRepository}
elif reqtype == 'status':
# create and return a LocalStatusRepository
@ -84,4 +86,3 @@ class Repository(object):
:param regtype: 'remote', 'local', or 'status'
"""
pass