2008-01-03 04:56:55 +01:00
|
|
|
# Gmail IMAP folder support
|
2017-08-15 01:24:23 +02:00
|
|
|
# Copyright (C) 2002-2017 John Goerzen & contributors.
|
2008-01-03 04:56:55 +01:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2017-10-26 18:23:15 +02:00
|
|
|
"""Folder implementation to support features of the Gmail IMAP server."""
|
|
|
|
|
2012-10-16 20:20:35 +02:00
|
|
|
import re
|
2015-01-11 21:44:24 +01:00
|
|
|
from sys import exc_info
|
2016-07-18 18:40:17 +02:00
|
|
|
from offlineimap import imaputil, imaplibutil, OfflineImapError
|
2012-10-16 20:20:35 +02:00
|
|
|
import offlineimap.accounts
|
2012-02-05 12:52:12 +01:00
|
|
|
from .IMAP import IMAPFolder
|
2008-01-03 04:56:55 +01:00
|
|
|
|
2015-01-14 22:58:25 +01:00
|
|
|
|
2008-01-03 04:56:55 +01:00
|
|
|
class GmailFolder(IMAPFolder):
|
|
|
|
"""Folder implementation to support features of the Gmail IMAP server.
|
2012-02-04 21:08:44 +01:00
|
|
|
|
|
|
|
Removing a message from a folder will only remove the "label" from
|
|
|
|
the message and keep it in the "All mails" folder. To really delete
|
|
|
|
a message it needs to be copied to the Trash folder. However, this
|
|
|
|
is dangerous as our folder moves are implemented as a 1) delete in
|
|
|
|
one folder and 2) append to the other. If 2 comes before 1, this
|
|
|
|
will effectively delete the message from all folders. So we cannot
|
|
|
|
do that until we have a smarter folder move mechanism.
|
2008-01-03 04:56:55 +01:00
|
|
|
|
|
|
|
For more information on the Gmail IMAP server:
|
|
|
|
http://mail.google.com/support/bin/answer.py?answer=77657&topic=12815
|
2012-10-16 20:20:35 +02:00
|
|
|
https://developers.google.com/google-apps/gmail/imap_extensions
|
2008-01-03 04:56:55 +01:00
|
|
|
"""
|
|
|
|
|
2017-10-26 18:23:15 +02:00
|
|
|
def __init__(self, imapserver, name, repository, decode=True):
|
|
|
|
super(GmailFolder, self).__init__(imapserver, name, repository, decode)
|
2012-10-16 20:20:35 +02:00
|
|
|
|
|
|
|
# The header under which labels are stored
|
|
|
|
self.labelsheader = self.repository.account.getconf('labelsheader', 'X-Keywords')
|
|
|
|
|
|
|
|
# enables / disables label sync
|
|
|
|
self.synclabels = self.repository.account.getconfboolean('synclabels', False)
|
|
|
|
|
|
|
|
# if synclabels is enabled, add a 4th pass to sync labels
|
|
|
|
if self.synclabels:
|
|
|
|
self.imap_query.insert(0, 'X-GM-LABELS')
|
2016-07-18 18:40:17 +02:00
|
|
|
self.syncmessagesto_passes.append(self.syncmessagesto_labels)
|
2012-10-16 20:20:35 +02:00
|
|
|
|
|
|
|
# Labels to be left alone
|
2016-07-18 18:40:17 +02:00
|
|
|
ignorelabels = self.repository.account.getconf('ignorelabels', '')
|
2020-10-10 17:24:18 +02:00
|
|
|
self.ignorelabels = set([v for v in re.split(r'\s*,\s*', ignorelabels) if len(v)])
|
2012-10-16 20:20:35 +02:00
|
|
|
|
|
|
|
def getmessage(self, uid):
|
|
|
|
"""Retrieve message with UID from the IMAP server (incl body). Also
|
|
|
|
gets Gmail labels and embeds them into the message.
|
|
|
|
|
|
|
|
:returns: the message body or throws and OfflineImapError
|
|
|
|
(probably severity MESSAGE) if e.g. no message with
|
|
|
|
this UID could be found.
|
|
|
|
"""
|
2016-12-19 06:31:35 +01:00
|
|
|
data = self._fetch_from_imap(str(uid), self.retrycount)
|
2012-10-16 20:20:35 +02:00
|
|
|
|
|
|
|
# data looks now e.g.
|
2020-09-02 10:00:20 +02:00
|
|
|
# ['320 (X-GM-LABELS (...) UID 17061 BODY[] {2565}','msgbody....']
|
|
|
|
# we only asked for one message, and that msg is in data[1].
|
|
|
|
# msbody is in [1].
|
|
|
|
body = data[1].replace("\r\n", "\n")
|
2012-10-16 20:20:35 +02:00
|
|
|
|
|
|
|
# Embed the labels into the message headers
|
|
|
|
if self.synclabels:
|
2018-05-05 01:26:42 +02:00
|
|
|
m = re.search('X-GM-LABELS\s*[(](.*)[)]', data[0][0])
|
2012-10-16 20:20:35 +02:00
|
|
|
if m:
|
|
|
|
labels = set([imaputil.dequote(lb) for lb in imaputil.imapsplit(m.group(1))])
|
|
|
|
else:
|
|
|
|
labels = set()
|
|
|
|
labels = labels - self.ignorelabels
|
2012-11-28 18:29:23 +01:00
|
|
|
labels_str = imaputil.format_labels_string(self.labelsheader, sorted(labels))
|
2014-11-20 14:16:48 +01:00
|
|
|
|
|
|
|
# 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)
|
2014-06-01 20:09:44 +02:00
|
|
|
body = self.addmessageheader(body, '\n', self.labelsheader, labels_str)
|
2012-10-16 20:20:35 +02:00
|
|
|
|
2020-08-29 19:41:19 +02:00
|
|
|
if len(body) > 200:
|
|
|
|
dbg_output = "%s...%s" % (str(body)[:150], str(body)[-50:])
|
2012-10-16 20:20:35 +02:00
|
|
|
else:
|
|
|
|
dbg_output = body
|
|
|
|
|
2020-08-29 19:41:19 +02:00
|
|
|
self.ui.debug('imap', "Returned object from fetching %d: '%s'" %
|
2012-10-16 20:20:35 +02:00
|
|
|
(uid, dbg_output))
|
|
|
|
return body
|
|
|
|
|
|
|
|
def getmessagelabels(self, uid):
|
|
|
|
if 'labels' in self.messagelist[uid]:
|
|
|
|
return self.messagelist[uid]['labels']
|
|
|
|
else:
|
|
|
|
return set()
|
|
|
|
|
2014-08-03 14:47:26 +02:00
|
|
|
# Interface from BaseFolder
|
|
|
|
def msglist_item_initializer(self, uid):
|
|
|
|
return {'uid': uid, 'flags': set(), 'labels': set(), 'time': 0}
|
|
|
|
|
2012-10-16 20:20:35 +02:00
|
|
|
# TODO: merge this code with the parent's cachemessagelist:
|
|
|
|
# TODO: they have too much common logics.
|
maxage: fix timezone issues, remove IMAP-IMAP support, add startdate option
1. When using maxage, local and remote messagelists are supposed to only
contain messages from at most maxage days ago. But local and remote used
different timezones to calculate what "maxage days ago" means, resulting
in removals on one side. Now, we ask the local folder for maxage days'
worth of mail, find the lowest UID, and then ask the remote folder for
all UID's starting with that lowest one.
2. maxage was fundamentally wrong in the IMAP-IMAP case: it assumed that
remote messages have UIDs in the same order as their local counterparts,
which could be false, e.g. when messages are copied in quick succession.
So, remove support for maxage in the IMAP-IMAP case.
3. Add startdate option for IMAP-IMAP syncs: use messages from the given
repository starting at startdate, and all messages from the other
repository. In the first sync, the other repository must be empty.
4. Allow maxage to be specified either as number of days to sync (as
previously) or as a fixed date.
Signed-off-by: Janna Martl <janna.martl109@gmail.com>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2015-04-07 10:14:11 +02:00
|
|
|
def cachemessagelist(self, min_date=None, min_uid=None):
|
2012-10-16 20:20:35 +02:00
|
|
|
if not self.synclabels:
|
maxage: fix timezone issues, remove IMAP-IMAP support, add startdate option
1. When using maxage, local and remote messagelists are supposed to only
contain messages from at most maxage days ago. But local and remote used
different timezones to calculate what "maxage days ago" means, resulting
in removals on one side. Now, we ask the local folder for maxage days'
worth of mail, find the lowest UID, and then ask the remote folder for
all UID's starting with that lowest one.
2. maxage was fundamentally wrong in the IMAP-IMAP case: it assumed that
remote messages have UIDs in the same order as their local counterparts,
which could be false, e.g. when messages are copied in quick succession.
So, remove support for maxage in the IMAP-IMAP case.
3. Add startdate option for IMAP-IMAP syncs: use messages from the given
repository starting at startdate, and all messages from the other
repository. In the first sync, the other repository must be empty.
4. Allow maxage to be specified either as number of days to sync (as
previously) or as a fixed date.
Signed-off-by: Janna Martl <janna.martl109@gmail.com>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2015-04-07 10:14:11 +02:00
|
|
|
return super(GmailFolder, self).cachemessagelist(
|
|
|
|
min_date=min_date, min_uid=min_uid)
|
2012-10-16 20:20:35 +02:00
|
|
|
|
2016-04-09 18:12:18 +02:00
|
|
|
self.dropmessagelistcache()
|
2012-10-16 20:20:35 +02:00
|
|
|
|
|
|
|
self.ui.collectingdata(None, self)
|
|
|
|
imapobj = self.imapserver.acquireconnection()
|
|
|
|
try:
|
maxage: fix timezone issues, remove IMAP-IMAP support, add startdate option
1. When using maxage, local and remote messagelists are supposed to only
contain messages from at most maxage days ago. But local and remote used
different timezones to calculate what "maxage days ago" means, resulting
in removals on one side. Now, we ask the local folder for maxage days'
worth of mail, find the lowest UID, and then ask the remote folder for
all UID's starting with that lowest one.
2. maxage was fundamentally wrong in the IMAP-IMAP case: it assumed that
remote messages have UIDs in the same order as their local counterparts,
which could be false, e.g. when messages are copied in quick succession.
So, remove support for maxage in the IMAP-IMAP case.
3. Add startdate option for IMAP-IMAP syncs: use messages from the given
repository starting at startdate, and all messages from the other
repository. In the first sync, the other repository must be empty.
4. Allow maxage to be specified either as number of days to sync (as
previously) or as a fixed date.
Signed-off-by: Janna Martl <janna.martl109@gmail.com>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2015-04-07 10:14:11 +02:00
|
|
|
msgsToFetch = self._msgs_to_fetch(
|
|
|
|
imapobj, min_date=min_date, min_uid=min_uid)
|
2012-10-16 20:20:35 +02:00
|
|
|
if not msgsToFetch:
|
2020-08-29 19:41:19 +02:00
|
|
|
return # No messages to sync
|
2012-10-16 20:20:35 +02:00
|
|
|
|
2021-02-20 01:27:08 +01:00
|
|
|
# Get the flags and UIDs for these.
|
2012-10-16 20:20:35 +02:00
|
|
|
#
|
|
|
|
# NB: msgsToFetch are sequential numbers, not UID's
|
2021-02-20 01:27:08 +01:00
|
|
|
res_type, response = imapobj.fetch("%s" % msgsToFetch,
|
2020-08-29 19:41:19 +02:00
|
|
|
'(FLAGS X-GM-LABELS UID)')
|
2012-10-16 20:20:35 +02:00
|
|
|
if res_type != 'OK':
|
2020-09-03 20:36:16 +02:00
|
|
|
raise OfflineImapError(
|
|
|
|
"FETCHING UIDs in folder [%s]%s failed. " %
|
|
|
|
(self.getrepository(), self) +
|
|
|
|
"Server responded '[%s] %s'" %
|
|
|
|
(res_type, response),
|
|
|
|
OfflineImapError.ERROR.FOLDER,
|
|
|
|
exc_info()[2])
|
2012-10-16 20:20:35 +02:00
|
|
|
finally:
|
|
|
|
self.imapserver.releaseconnection(imapobj)
|
|
|
|
|
|
|
|
for messagestr in response:
|
|
|
|
# looks like: '1 (FLAGS (\\Seen Old) X-GM-LABELS (\\Inbox \\Favorites) UID 4807)' or None if no msg
|
|
|
|
# Discard initial message number.
|
2020-08-30 11:15:00 +02:00
|
|
|
if messagestr is None:
|
2012-10-16 20:20:35 +02:00
|
|
|
continue
|
2021-02-20 01:27:08 +01:00
|
|
|
# We need a str messagestr
|
|
|
|
if isinstance(messagestr, bytes):
|
|
|
|
messagestr = messagestr.decode(encoding='utf-8')
|
2012-10-16 20:20:35 +02:00
|
|
|
messagestr = messagestr.split(' ', 1)[1]
|
2018-05-05 01:26:42 +02:00
|
|
|
# e.g.: {'X-GM-LABELS': '("Webserver (RW.net)" "\\Inbox" GInbox)', 'FLAGS': '(\\Seen)', 'UID': '275440'}
|
2012-10-16 20:20:35 +02:00
|
|
|
options = imaputil.flags2hash(messagestr)
|
2020-08-30 13:20:17 +02:00
|
|
|
if 'UID' not in options:
|
2020-08-30 13:19:27 +02:00
|
|
|
self.ui.warn('No UID in message with options %s' %
|
|
|
|
str(options), minor=1)
|
2012-10-16 20:20:35 +02:00
|
|
|
else:
|
2016-05-08 16:42:52 +02:00
|
|
|
uid = int(options['UID'])
|
2014-08-03 14:47:26 +02:00
|
|
|
self.messagelist[uid] = self.msglist_item_initializer(uid)
|
2012-10-16 20:20:35 +02:00
|
|
|
flags = imaputil.flagsimap2maildir(options['FLAGS'])
|
2018-05-05 01:26:42 +02:00
|
|
|
# e.g.: '("Webserver (RW.net)" "\\Inbox" GInbox)'
|
|
|
|
m = re.search('^[(](.*)[)]', options['X-GM-LABELS'])
|
2012-10-16 20:20:35 +02:00
|
|
|
if m:
|
|
|
|
labels = set([imaputil.dequote(lb) for lb in imaputil.imapsplit(m.group(1))])
|
|
|
|
else:
|
|
|
|
labels = set()
|
|
|
|
labels = labels - self.ignorelabels
|
2021-02-20 01:27:08 +01:00
|
|
|
if isinstance(messagestr, str):
|
|
|
|
messagestr = bytes(messagestr, 'utf-8')
|
2012-10-16 20:20:35 +02:00
|
|
|
rtime = imaplibutil.Internaldate2epoch(messagestr)
|
|
|
|
self.messagelist[uid] = {'uid': uid, 'flags': flags, 'labels': labels, 'time': rtime}
|
|
|
|
|
|
|
|
def savemessage(self, uid, content, flags, rtime):
|
|
|
|
"""Save the message on the Server
|
|
|
|
|
|
|
|
This backend always assigns a new uid, so the uid arg is ignored.
|
|
|
|
|
|
|
|
This function will update the self.messagelist dict to contain
|
|
|
|
the new message after sucessfully saving it, including labels.
|
|
|
|
|
|
|
|
See folder/Base for details. Note that savemessage() does not
|
|
|
|
check against dryrun settings, so you need to ensure that
|
|
|
|
savemessage is never called in a dryrun mode.
|
|
|
|
|
2020-08-30 13:18:18 +02:00
|
|
|
:param uid: Message UID
|
|
|
|
:param content: Message content
|
|
|
|
:param flags: Message flags
|
2012-10-16 20:20:35 +02:00
|
|
|
:param rtime: A timestamp to be used as the mail date
|
|
|
|
:returns: the UID of the new message as assigned by the server. If the
|
|
|
|
message is saved, but it's UID can not be found, it will
|
|
|
|
return 0. If the message can't be written (folder is
|
|
|
|
read-only for example) it will return -1."""
|
|
|
|
|
|
|
|
if not self.synclabels:
|
|
|
|
return super(GmailFolder, self).savemessage(uid, content, flags, rtime)
|
|
|
|
|
2014-11-20 14:03:15 +01:00
|
|
|
labels = set()
|
|
|
|
for hstr in self.getmessageheaderlist(content, self.labelsheader):
|
|
|
|
labels.update(imaputil.labels_from_header(self.labelsheader, hstr))
|
2012-10-16 20:20:35 +02:00
|
|
|
|
|
|
|
ret = super(GmailFolder, self).savemessage(uid, content, flags, rtime)
|
|
|
|
self.savemessagelabels(ret, labels)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def _messagelabels_aux(self, arg, uidlist, labels):
|
|
|
|
"""Common code to savemessagelabels and addmessagelabels"""
|
|
|
|
labels = labels - self.ignorelabels
|
|
|
|
uidlist = [uid for uid in uidlist if uid > 0]
|
|
|
|
if len(uidlist) > 0:
|
|
|
|
imapobj = self.imapserver.acquireconnection()
|
|
|
|
try:
|
|
|
|
labels_str = '(' + ' '.join([imaputil.quote(lb) for lb in labels]) + ')'
|
|
|
|
# Coalesce uid's into ranges
|
|
|
|
uid_str = imaputil.uid_sequence(uidlist)
|
|
|
|
result = self._store_to_imap(imapobj, uid_str, arg, labels_str)
|
|
|
|
|
|
|
|
except imapobj.readonly:
|
2014-05-06 23:12:50 +02:00
|
|
|
self.ui.labelstoreadonly(self, uidlist, labels)
|
2012-10-16 20:20:35 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
finally:
|
|
|
|
self.imapserver.releaseconnection(imapobj)
|
|
|
|
|
|
|
|
if result:
|
|
|
|
retlabels = imaputil.flags2hash(imaputil.imapsplit(result)[1])['X-GM-LABELS']
|
|
|
|
retlabels = set([imaputil.dequote(lb) for lb in imaputil.imapsplit(retlabels)])
|
|
|
|
return retlabels
|
|
|
|
return None
|
|
|
|
|
|
|
|
def savemessagelabels(self, uid, labels):
|
|
|
|
"""Change a message's labels to `labels`.
|
|
|
|
|
|
|
|
Note that this function does not check against dryrun settings,
|
|
|
|
so you need to ensure that it is never called in a dryrun mode."""
|
|
|
|
if uid in self.messagelist and 'labels' in self.messagelist[uid]:
|
|
|
|
oldlabels = self.messagelist[uid]['labels']
|
|
|
|
else:
|
|
|
|
oldlabels = set()
|
|
|
|
labels = labels - self.ignorelabels
|
|
|
|
newlabels = labels | (oldlabels & self.ignorelabels)
|
|
|
|
if oldlabels != newlabels:
|
|
|
|
result = self._messagelabels_aux('X-GM-LABELS', [uid], newlabels)
|
|
|
|
if result:
|
|
|
|
self.messagelist[uid]['labels'] = newlabels
|
|
|
|
else:
|
|
|
|
self.messagelist[uid]['labels'] = oldlabels
|
|
|
|
|
|
|
|
def addmessageslabels(self, uidlist, labels):
|
|
|
|
"""Add `labels` to all messages in uidlist.
|
|
|
|
|
|
|
|
Note that this function does not check against dryrun settings,
|
|
|
|
so you need to ensure that it is never called in a dryrun mode."""
|
|
|
|
|
|
|
|
labels = labels - self.ignorelabels
|
|
|
|
result = self._messagelabels_aux('+X-GM-LABELS', uidlist, labels)
|
|
|
|
if result:
|
|
|
|
for uid in uidlist:
|
|
|
|
self.messagelist[uid]['labels'] = self.messagelist[uid]['labels'] | labels
|
|
|
|
|
|
|
|
def deletemessageslabels(self, uidlist, labels):
|
|
|
|
"""Delete `labels` from all messages in uidlist.
|
|
|
|
|
|
|
|
Note that this function does not check against dryrun settings,
|
|
|
|
so you need to ensure that it is never called in a dryrun mode."""
|
|
|
|
|
|
|
|
labels = labels - self.ignorelabels
|
|
|
|
result = self._messagelabels_aux('-X-GM-LABELS', uidlist, labels)
|
|
|
|
if result:
|
|
|
|
for uid in uidlist:
|
|
|
|
self.messagelist[uid]['labels'] = self.messagelist[uid]['labels'] - labels
|
|
|
|
|
2020-08-29 19:41:19 +02:00
|
|
|
def copymessageto(self, uid, dstfolder, statusfolder, register=1):
|
2012-10-16 20:20:35 +02:00
|
|
|
"""Copies a message from self to dst if needed, updating the status
|
|
|
|
|
|
|
|
Note that this function does not check against dryrun settings,
|
|
|
|
so you need to ensure that it is never called in a
|
|
|
|
dryrun mode.
|
|
|
|
|
|
|
|
:param uid: uid of the message to be copied.
|
|
|
|
:param dstfolder: A BaseFolder-derived instance
|
|
|
|
:param statusfolder: A LocalStatusFolder instance
|
|
|
|
:param register: whether we should register a new thread."
|
|
|
|
:returns: Nothing on success, or raises an Exception."""
|
|
|
|
|
|
|
|
# Check if we are really copying
|
|
|
|
realcopy = uid > 0 and not dstfolder.uidexists(uid)
|
|
|
|
|
|
|
|
# first copy the message
|
|
|
|
super(GmailFolder, self).copymessageto(uid, dstfolder, statusfolder, register)
|
|
|
|
|
|
|
|
# sync labels and mtime now when the message is new (the embedded labels are up to date)
|
|
|
|
# otherwise we may be spending time for nothing, as they will get updated on a later pass.
|
|
|
|
if realcopy and self.synclabels:
|
|
|
|
try:
|
|
|
|
mtime = dstfolder.getmessagemtime(uid)
|
|
|
|
labels = dstfolder.getmessagelabels(uid)
|
|
|
|
statusfolder.savemessagelabels(uid, labels, mtime=mtime)
|
|
|
|
|
2013-07-27 23:25:13 +02:00
|
|
|
# dstfolder is not GmailMaildir.
|
2012-10-16 20:20:35 +02:00
|
|
|
except NotImplementedError:
|
|
|
|
return
|
|
|
|
|
|
|
|
def syncmessagesto_labels(self, dstfolder, statusfolder):
|
|
|
|
"""Pass 4: Label Synchronization (Gmail only)
|
|
|
|
|
|
|
|
Compare label mismatches in self with those in statusfolder. If
|
|
|
|
msg has a valid UID and exists on dstfolder (has not e.g. been
|
|
|
|
deleted there), sync the labels change to both dstfolder and
|
|
|
|
statusfolder.
|
|
|
|
|
|
|
|
This function checks and protects us from action in dryrun mode.
|
|
|
|
"""
|
|
|
|
# This applies the labels message by message, as this makes more sense for a
|
|
|
|
# Maildir target. If applied with an other Gmail IMAP target it would not be
|
|
|
|
# the fastest thing in the world though...
|
|
|
|
uidlist = []
|
|
|
|
|
|
|
|
# filter the uids (fast)
|
|
|
|
try:
|
|
|
|
for uid in self.getmessageuidlist():
|
|
|
|
# bail out on CTRL-C or SIGTERM
|
|
|
|
if offlineimap.accounts.Account.abort_NOW_signal.is_set():
|
|
|
|
break
|
|
|
|
|
|
|
|
# Ignore messages with negative UIDs missed by pass 1 and
|
|
|
|
# don't do anything if the message has been deleted remotely
|
|
|
|
if uid < 0 or not dstfolder.uidexists(uid):
|
|
|
|
continue
|
|
|
|
|
|
|
|
selflabels = self.getmessagelabels(uid) - self.ignorelabels
|
|
|
|
|
|
|
|
if statusfolder.uidexists(uid):
|
|
|
|
statuslabels = statusfolder.getmessagelabels(uid) - self.ignorelabels
|
|
|
|
else:
|
|
|
|
statuslabels = set()
|
|
|
|
|
|
|
|
if selflabels != statuslabels:
|
|
|
|
uidlist.append(uid)
|
|
|
|
|
|
|
|
# now sync labels (slow)
|
|
|
|
mtimes = {}
|
|
|
|
labels = {}
|
|
|
|
for i, uid in enumerate(uidlist):
|
|
|
|
# bail out on CTRL-C or SIGTERM
|
|
|
|
if offlineimap.accounts.Account.abort_NOW_signal.is_set():
|
|
|
|
break
|
|
|
|
|
|
|
|
selflabels = self.getmessagelabels(uid) - self.ignorelabels
|
|
|
|
|
|
|
|
if statusfolder.uidexists(uid):
|
|
|
|
statuslabels = statusfolder.getmessagelabels(uid) - self.ignorelabels
|
|
|
|
else:
|
|
|
|
statuslabels = set()
|
|
|
|
|
|
|
|
if selflabels != statuslabels:
|
2020-08-29 19:41:19 +02:00
|
|
|
self.ui.settinglabels(uid, i + 1, len(uidlist), sorted(selflabels), dstfolder)
|
2012-10-16 20:20:35 +02:00
|
|
|
if self.repository.account.dryrun:
|
2020-08-29 19:41:19 +02:00
|
|
|
continue # don't actually add in a dryrun
|
|
|
|
dstfolder.savemessagelabels(uid, selflabels, ignorelabels=self.ignorelabels)
|
2012-10-16 20:20:35 +02:00
|
|
|
mtime = dstfolder.getmessagemtime(uid)
|
|
|
|
mtimes[uid] = mtime
|
|
|
|
labels[uid] = selflabels
|
|
|
|
|
|
|
|
# Update statusfolder in a single DB transaction. It is safe, as if something fails,
|
|
|
|
# statusfolder will be updated on the next run.
|
|
|
|
statusfolder.savemessageslabelsbulk(labels)
|
|
|
|
statusfolder.savemessagesmtimebulk(mtimes)
|
|
|
|
|
|
|
|
except NotImplementedError:
|
|
|
|
self.ui.warn("Can't sync labels. You need to configure a local repository of type GmailMaildir")
|