2005-04-16 21:35:25 +02:00
|
|
|
# Base folder support
|
2016-06-29 03:42:57 +02:00
|
|
|
# Copyright (C) 2002-2016 John Goerzen & contributors.
|
2005-04-16 21:35:25 +02: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
|
2006-08-12 06:15:55 +02:00
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2015-01-11 21:44:24 +01:00
|
|
|
|
2016-06-29 03:42:57 +02:00
|
|
|
import os.path
|
2016-10-24 03:19:26 +02:00
|
|
|
import shutil
|
|
|
|
from os import fsync, unlink
|
2016-06-28 02:35:35 +02:00
|
|
|
from sys import exc_info
|
|
|
|
from threading import Lock
|
2016-06-29 03:42:57 +02:00
|
|
|
|
2020-08-30 14:01:51 +02:00
|
|
|
try:
|
|
|
|
import portalocker
|
|
|
|
except:
|
|
|
|
try:
|
|
|
|
import fcntl
|
|
|
|
except:
|
|
|
|
pass # Ok if this fails, we can do without.
|
|
|
|
|
2012-06-05 02:45:10 +02:00
|
|
|
from offlineimap import OfflineImapError
|
2012-02-05 12:52:12 +01:00
|
|
|
from .IMAP import IMAPFolder
|
2005-04-16 21:35:25 +02:00
|
|
|
|
2016-05-17 19:56:52 +02:00
|
|
|
|
Simplify MappedIMAPFolder, fixing bugs
Previously, we instanciated an MappedImapFolder, and would cleverly (too
cleverly?) invoke methods on it casting it to an IMAPFolder by calling
methods such as: self._mb.cachemessages(self) where self._MB is the class
IMAPFolder and self and instance of MappedImapFolder. If
e.g. cachemessages() invokes a method uidexists() which exists for
MappedImapFolder, but not directly in IMAPFolder, I am not sure if
Python would at some point attempt to use the method of the wrong class.
Also, this leads to some twisted thinking as our class would in same
cases act as an IMAPFolder and in some cases as an MappedImapFOlder and
it is not always clear if we mean REMOTE UID or LOCAL UID.
This commit simplifies the class, by a)doing away with the complex Mixin
construct and directly inheriting from IMAPFOlder (so we get all the
IMAPFOlder methods that we can inherit). We instantiate self._mb as a
new instance of IMAPFolder which represents the local IMAP using local
UIDs, separating the MappedIMAPFolder construct logically from the
IMAPFolder somewhat.
In the long run, I would like to remove self._mb completely and simply
override any method that needs overriding, but let us take small and
understandable baby steps here.
Reported-and-tested-by: Vincent Beffara <vbeffara@gmail.com>
Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-06-24 11:28:30 +02:00
|
|
|
class MappedIMAPFolder(IMAPFolder):
|
|
|
|
"""IMAP class to map between Folder() instances where both side assign a uid
|
|
|
|
|
|
|
|
This Folder is used on the local side, while the remote side should
|
|
|
|
be an IMAPFolder.
|
2011-03-16 16:24:07 +01:00
|
|
|
|
|
|
|
Instance variables (self.):
|
2016-10-24 20:01:08 +02:00
|
|
|
dryrun: boolean.
|
2011-03-16 16:24:07 +01:00
|
|
|
r2l: dict mapping message uids: self.r2l[remoteuid]=localuid
|
|
|
|
l2r: dict mapping message uids: self.r2l[localuid]=remoteuid
|
|
|
|
#TODO: what is the difference, how are they used?
|
|
|
|
diskr2l: dict mapping message uids: self.r2l[remoteuid]=localuid
|
|
|
|
diskl2r: dict mapping message uids: self.r2l[localuid]=remoteuid"""
|
Simplify MappedIMAPFolder, fixing bugs
Previously, we instanciated an MappedImapFolder, and would cleverly (too
cleverly?) invoke methods on it casting it to an IMAPFolder by calling
methods such as: self._mb.cachemessages(self) where self._MB is the class
IMAPFolder and self and instance of MappedImapFolder. If
e.g. cachemessages() invokes a method uidexists() which exists for
MappedImapFolder, but not directly in IMAPFolder, I am not sure if
Python would at some point attempt to use the method of the wrong class.
Also, this leads to some twisted thinking as our class would in same
cases act as an IMAPFolder and in some cases as an MappedImapFOlder and
it is not always clear if we mean REMOTE UID or LOCAL UID.
This commit simplifies the class, by a)doing away with the complex Mixin
construct and directly inheriting from IMAPFOlder (so we get all the
IMAPFOlder methods that we can inherit). We instantiate self._mb as a
new instance of IMAPFolder which represents the local IMAP using local
UIDs, separating the MappedIMAPFolder construct logically from the
IMAPFolder somewhat.
In the long run, I would like to remove self._mb completely and simply
override any method that needs overriding, but let us take small and
understandable baby steps here.
Reported-and-tested-by: Vincent Beffara <vbeffara@gmail.com>
Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-06-24 11:28:30 +02:00
|
|
|
|
2017-10-02 01:26:29 +02:00
|
|
|
def __init__(self, imapserver, name, repository, decode=True):
|
|
|
|
IMAPFolder.__init__(self, imapserver, name, repository, decode=False)
|
2016-10-24 20:01:08 +02:00
|
|
|
self.dryrun = self.config.getdefaultboolean("general", "dry-run", True)
|
2005-04-16 21:35:25 +02:00
|
|
|
self.maplock = Lock()
|
2016-10-24 03:19:26 +02:00
|
|
|
self.diskr2l, self.diskl2r = self._loadmaps()
|
|
|
|
self.r2l, self.l2r = None, None
|
|
|
|
# Representing the local IMAP Folder using local UIDs.
|
|
|
|
# XXX: This should be removed since we inherit from IMAPFolder.
|
|
|
|
# See commit 3ce514e92ba7 to know more.
|
2017-10-02 01:26:29 +02:00
|
|
|
self._mb = IMAPFolder(imapserver, name, repository, decode=False)
|
2005-04-16 21:35:25 +02:00
|
|
|
|
|
|
|
def _getmapfilename(self):
|
|
|
|
return os.path.join(self.repository.getmapdir(),
|
|
|
|
self.getfolderbasename())
|
2013-07-21 21:00:23 +02:00
|
|
|
|
2005-04-16 21:35:25 +02:00
|
|
|
def _loadmaps(self):
|
2016-10-24 03:19:26 +02:00
|
|
|
mapfilename = self._getmapfilename()
|
2020-08-29 19:48:04 +02:00
|
|
|
mapfilenametmp = "%s.tmp" % mapfilename
|
|
|
|
mapfilenamelock = "%s.lock" % mapfilename
|
2016-10-24 03:19:26 +02:00
|
|
|
with self.maplock and open(mapfilenamelock, 'w') as mapfilelock:
|
|
|
|
try:
|
2020-08-30 14:01:51 +02:00
|
|
|
fcntl.lockf(mapfilelock, fcntl.LOCK_EX) # Blocks until acquired.
|
2016-10-24 03:19:26 +02:00
|
|
|
except NameError:
|
2020-08-29 19:48:04 +02:00
|
|
|
pass # Windows...
|
2016-10-24 03:19:26 +02:00
|
|
|
if os.path.exists(mapfilenametmp):
|
|
|
|
self.ui.warn("a previous run might have leave the UIDMaps file"
|
2020-08-29 19:48:04 +02:00
|
|
|
" in incorrect state; some sync operations might be done"
|
|
|
|
" again and some emails might become duplicated.")
|
2016-10-24 03:19:26 +02:00
|
|
|
unlink(mapfilenametmp)
|
2005-04-16 21:35:25 +02:00
|
|
|
if not os.path.exists(mapfilename):
|
2020-08-30 14:10:53 +02:00
|
|
|
return {}, {}
|
2005-04-16 21:35:25 +02:00
|
|
|
file = open(mapfilename, 'rt')
|
|
|
|
r2l = {}
|
|
|
|
l2r = {}
|
2016-06-28 03:03:11 +02:00
|
|
|
while True:
|
2005-04-16 21:35:25 +02:00
|
|
|
line = file.readline()
|
|
|
|
if not len(line):
|
|
|
|
break
|
2011-05-02 11:44:19 +02:00
|
|
|
try:
|
|
|
|
line = line.strip()
|
|
|
|
except ValueError:
|
2020-09-03 20:33:30 +02:00
|
|
|
raise Exception(
|
|
|
|
"Corrupt line '%s' in UID mapping file '%s'" %
|
|
|
|
(line, mapfilename),
|
|
|
|
exc_info()[2])
|
|
|
|
|
2005-04-16 21:35:25 +02:00
|
|
|
(str1, str2) = line.split(':')
|
2016-05-08 16:42:52 +02:00
|
|
|
loc = int(str1)
|
|
|
|
rem = int(str2)
|
2005-04-16 21:35:25 +02:00
|
|
|
r2l[rem] = loc
|
|
|
|
l2r[loc] = rem
|
2020-08-30 14:10:53 +02:00
|
|
|
return r2l, l2r
|
2005-04-16 21:35:25 +02:00
|
|
|
|
2016-06-28 03:03:11 +02:00
|
|
|
def _savemaps(self):
|
2016-10-24 20:01:08 +02:00
|
|
|
if self.dryrun is True:
|
|
|
|
return
|
|
|
|
|
2005-04-16 21:35:25 +02:00
|
|
|
mapfilename = self._getmapfilename()
|
2016-10-24 03:19:26 +02:00
|
|
|
# Do not use the map file directly to prevent from leaving it truncated.
|
2020-08-29 19:48:04 +02:00
|
|
|
mapfilenametmp = "%s.tmp" % mapfilename
|
|
|
|
mapfilenamelock = "%s.lock" % mapfilename
|
2016-10-24 03:19:26 +02:00
|
|
|
with self.maplock and open(mapfilenamelock, 'w') as mapfilelock:
|
2016-06-28 15:38:35 +02:00
|
|
|
# The "account" lock already prevents from multiple access by
|
|
|
|
# different processes. However, we still need to protect for
|
|
|
|
# multiple access from different threads.
|
|
|
|
try:
|
2020-08-30 14:01:51 +02:00
|
|
|
fcntl.lockf(mapfilelock, fcntl.LOCK_EX) # Blocks until acquired.
|
2016-06-28 15:38:35 +02:00
|
|
|
except NameError:
|
2020-08-29 19:48:04 +02:00
|
|
|
pass # Windows...
|
2016-10-24 03:19:26 +02:00
|
|
|
with open(mapfilenametmp, 'wt') as mapfilefd:
|
2020-08-28 03:32:43 +02:00
|
|
|
for (key, value) in list(self.diskl2r.items()):
|
2020-08-29 19:48:04 +02:00
|
|
|
mapfilefd.write("%d:%d\n" % (key, value))
|
2016-10-25 21:34:38 +02:00
|
|
|
if self.dofsync():
|
2016-10-24 03:19:26 +02:00
|
|
|
fsync(mapfilefd)
|
2016-06-28 15:38:35 +02:00
|
|
|
# The lock is released when the file descriptor ends.
|
2016-10-24 03:19:26 +02:00
|
|
|
shutil.move(mapfilenametmp, mapfilename)
|
2005-04-16 21:35:25 +02:00
|
|
|
|
|
|
|
def _uidlist(self, mapping, items):
|
2012-02-17 08:48:59 +01:00
|
|
|
try:
|
|
|
|
return [mapping[x] for x in items]
|
|
|
|
except KeyError as e:
|
2020-09-03 20:33:30 +02:00
|
|
|
raise OfflineImapError(
|
|
|
|
"Could not find UID for msg '{0}' (f:'{1}'."
|
|
|
|
" This is usually a bad thing and should be "
|
|
|
|
"reported on the mailing list.".format(
|
|
|
|
e.args[0], self),
|
|
|
|
OfflineImapError.ERROR.MESSAGE,
|
|
|
|
exc_info()[2])
|
2005-04-16 21:35:25 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
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):
|
|
|
|
self._mb.cachemessagelist(min_date=min_date, min_uid=min_uid)
|
Simplify MappedIMAPFolder, fixing bugs
Previously, we instanciated an MappedImapFolder, and would cleverly (too
cleverly?) invoke methods on it casting it to an IMAPFolder by calling
methods such as: self._mb.cachemessages(self) where self._MB is the class
IMAPFolder and self and instance of MappedImapFolder. If
e.g. cachemessages() invokes a method uidexists() which exists for
MappedImapFolder, but not directly in IMAPFolder, I am not sure if
Python would at some point attempt to use the method of the wrong class.
Also, this leads to some twisted thinking as our class would in same
cases act as an IMAPFolder and in some cases as an MappedImapFOlder and
it is not always clear if we mean REMOTE UID or LOCAL UID.
This commit simplifies the class, by a)doing away with the complex Mixin
construct and directly inheriting from IMAPFOlder (so we get all the
IMAPFOlder methods that we can inherit). We instantiate self._mb as a
new instance of IMAPFolder which represents the local IMAP using local
UIDs, separating the MappedIMAPFolder construct logically from the
IMAPFolder somewhat.
In the long run, I would like to remove self._mb completely and simply
override any method that needs overriding, but let us take small and
understandable baby steps here.
Reported-and-tested-by: Vincent Beffara <vbeffara@gmail.com>
Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-06-24 11:28:30 +02:00
|
|
|
reallist = self._mb.getmessagelist()
|
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
|
|
|
self.messagelist = self._mb.messagelist
|
2005-04-16 21:35:25 +02:00
|
|
|
|
2016-06-28 03:03:11 +02:00
|
|
|
with self.maplock:
|
2005-04-16 21:35:25 +02:00
|
|
|
# OK. Now we've got a nice list. First, delete things from the
|
|
|
|
# summary that have been deleted from the folder.
|
2020-08-28 03:32:43 +02:00
|
|
|
for luid in list(self.diskl2r.keys()):
|
2020-08-30 14:04:43 +02:00
|
|
|
if luid not in reallist:
|
2005-04-16 21:35:25 +02:00
|
|
|
ruid = self.diskl2r[luid]
|
2020-08-29 19:48:04 +02:00
|
|
|
# XXX: the following KeyError are sightly unexpected. This
|
2017-03-22 12:01:27 +01:00
|
|
|
# would require more digging to understand how it's
|
|
|
|
# possible.
|
|
|
|
errorMessage = ("unexpected error: key {} was not found "
|
2020-08-29 19:48:04 +02:00
|
|
|
"in memory, see "
|
|
|
|
"https://github.com/OfflineIMAP/offlineimap/issues/445"
|
|
|
|
" to know more."
|
|
|
|
)
|
2017-03-22 12:01:27 +01:00
|
|
|
try:
|
|
|
|
del self.diskr2l[ruid]
|
2020-08-30 13:55:30 +02:00
|
|
|
except KeyError:
|
2017-03-22 12:01:27 +01:00
|
|
|
self.ui.warn(errorMessage.format(ruid))
|
|
|
|
try:
|
|
|
|
del self.diskl2r[luid]
|
2020-08-30 13:55:30 +02:00
|
|
|
except KeyError:
|
2017-03-22 12:01:27 +01:00
|
|
|
self.ui.warn(errorMessage.format(ruid))
|
2005-04-16 21:35:25 +02:00
|
|
|
|
|
|
|
# Now, assign negative UIDs to local items.
|
2016-06-28 03:03:11 +02:00
|
|
|
self._savemaps()
|
2005-04-16 21:35:25 +02:00
|
|
|
nextneg = -1
|
|
|
|
|
|
|
|
self.r2l = self.diskr2l.copy()
|
|
|
|
self.l2r = self.diskl2r.copy()
|
|
|
|
|
2020-08-28 03:32:43 +02:00
|
|
|
for luid in list(reallist.keys()):
|
2020-08-30 14:04:43 +02:00
|
|
|
if luid not in self.l2r:
|
2005-04-16 21:35:25 +02:00
|
|
|
ruid = nextneg
|
|
|
|
nextneg -= 1
|
|
|
|
self.l2r[luid] = ruid
|
|
|
|
self.r2l[ruid] = luid
|
|
|
|
|
2014-01-05 16:07:03 +01:00
|
|
|
def dropmessagelistcache(self):
|
|
|
|
self._mb.dropmessagelistcache()
|
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2011-03-28 16:19:19 +02:00
|
|
|
def uidexists(self, ruid):
|
|
|
|
"""Checks if the (remote) UID exists in this Folder"""
|
2016-10-24 03:19:26 +02:00
|
|
|
|
2011-03-28 16:19:19 +02:00
|
|
|
# This implementation overrides the one in BaseFolder, as it is
|
|
|
|
# much more efficient for the mapped case.
|
|
|
|
return ruid in self.r2l
|
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2011-03-28 16:19:19 +02:00
|
|
|
def getmessageuidlist(self):
|
|
|
|
"""Gets a list of (remote) UIDs.
|
2016-10-24 03:19:26 +02:00
|
|
|
|
2011-03-28 16:19:19 +02:00
|
|
|
You may have to call cachemessagelist() before calling this function!"""
|
2016-10-24 03:19:26 +02:00
|
|
|
|
2011-03-28 16:19:19 +02:00
|
|
|
# This implementation overrides the one in BaseFolder, as it is
|
|
|
|
# much more efficient for the mapped case.
|
2020-08-28 03:32:43 +02:00
|
|
|
return list(self.r2l.keys())
|
2011-03-28 16:19:19 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2011-03-28 16:19:19 +02:00
|
|
|
def getmessagecount(self):
|
|
|
|
"""Gets the number of messages in this folder.
|
2016-10-24 03:19:26 +02:00
|
|
|
|
2011-03-28 16:19:19 +02:00
|
|
|
You may have to call cachemessagelist() before calling this function!"""
|
2016-10-24 03:19:26 +02:00
|
|
|
|
2011-03-28 16:19:19 +02:00
|
|
|
# This implementation overrides the one in BaseFolder, as it is
|
|
|
|
# much more efficient for the mapped case.
|
|
|
|
return len(self.r2l)
|
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2005-04-16 21:35:25 +02:00
|
|
|
def getmessagelist(self):
|
2016-10-24 03:19:26 +02:00
|
|
|
"""Gets the current message list.
|
|
|
|
|
|
|
|
This function's implementation is quite expensive for the mapped UID
|
|
|
|
case. You must call cachemessagelist() before calling this function!"""
|
2005-04-16 21:35:25 +02:00
|
|
|
|
|
|
|
retval = {}
|
Simplify MappedIMAPFolder, fixing bugs
Previously, we instanciated an MappedImapFolder, and would cleverly (too
cleverly?) invoke methods on it casting it to an IMAPFolder by calling
methods such as: self._mb.cachemessages(self) where self._MB is the class
IMAPFolder and self and instance of MappedImapFolder. If
e.g. cachemessages() invokes a method uidexists() which exists for
MappedImapFolder, but not directly in IMAPFolder, I am not sure if
Python would at some point attempt to use the method of the wrong class.
Also, this leads to some twisted thinking as our class would in same
cases act as an IMAPFolder and in some cases as an MappedImapFOlder and
it is not always clear if we mean REMOTE UID or LOCAL UID.
This commit simplifies the class, by a)doing away with the complex Mixin
construct and directly inheriting from IMAPFOlder (so we get all the
IMAPFOlder methods that we can inherit). We instantiate self._mb as a
new instance of IMAPFolder which represents the local IMAP using local
UIDs, separating the MappedIMAPFolder construct logically from the
IMAPFolder somewhat.
In the long run, I would like to remove self._mb completely and simply
override any method that needs overriding, but let us take small and
understandable baby steps here.
Reported-and-tested-by: Vincent Beffara <vbeffara@gmail.com>
Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-06-24 11:28:30 +02:00
|
|
|
localhash = self._mb.getmessagelist()
|
2016-06-28 03:03:11 +02:00
|
|
|
with self.maplock:
|
2016-05-16 19:48:50 +02:00
|
|
|
for key, value in list(localhash.items()):
|
2005-04-16 21:35:25 +02:00
|
|
|
try:
|
|
|
|
key = self.l2r[key]
|
|
|
|
except KeyError:
|
|
|
|
# Sometimes, the IMAP backend may put in a new message,
|
|
|
|
# then this function acquires the lock before the system
|
|
|
|
# has the chance to note it in the mapping. In that case,
|
|
|
|
# just ignore it.
|
|
|
|
continue
|
|
|
|
value = value.copy()
|
|
|
|
value['uid'] = self.l2r[value['uid']]
|
|
|
|
retval[key] = value
|
|
|
|
return retval
|
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2005-04-16 21:35:25 +02:00
|
|
|
def getmessage(self, uid):
|
2021-02-19 23:00:15 +01:00
|
|
|
"""Returns the specified message."""
|
Simplify MappedIMAPFolder, fixing bugs
Previously, we instanciated an MappedImapFolder, and would cleverly (too
cleverly?) invoke methods on it casting it to an IMAPFolder by calling
methods such as: self._mb.cachemessages(self) where self._MB is the class
IMAPFolder and self and instance of MappedImapFolder. If
e.g. cachemessages() invokes a method uidexists() which exists for
MappedImapFolder, but not directly in IMAPFolder, I am not sure if
Python would at some point attempt to use the method of the wrong class.
Also, this leads to some twisted thinking as our class would in same
cases act as an IMAPFolder and in some cases as an MappedImapFOlder and
it is not always clear if we mean REMOTE UID or LOCAL UID.
This commit simplifies the class, by a)doing away with the complex Mixin
construct and directly inheriting from IMAPFOlder (so we get all the
IMAPFOlder methods that we can inherit). We instantiate self._mb as a
new instance of IMAPFolder which represents the local IMAP using local
UIDs, separating the MappedIMAPFolder construct logically from the
IMAPFolder somewhat.
In the long run, I would like to remove self._mb completely and simply
override any method that needs overriding, but let us take small and
understandable baby steps here.
Reported-and-tested-by: Vincent Beffara <vbeffara@gmail.com>
Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-06-24 11:28:30 +02:00
|
|
|
return self._mb.getmessage(self.r2l[uid])
|
2005-04-16 21:35:25 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2021-02-19 23:00:15 +01:00
|
|
|
def savemessage(self, uid, msg, flags, rtime):
|
2005-04-16 21:35:25 +02:00
|
|
|
"""Writes a new message, with the specified uid.
|
|
|
|
|
2011-03-16 16:24:07 +01:00
|
|
|
The UIDMaps class will not return a newly assigned uid, as it
|
|
|
|
internally maps different uids between IMAP servers. So a
|
|
|
|
successful savemessage() invocation will return the same uid it
|
|
|
|
has been invoked with. As it maps between 2 IMAP servers which
|
|
|
|
means the source message must already have an uid, it requires a
|
|
|
|
positive uid to be passed in. Passing in a message with a
|
|
|
|
negative uid will do nothing and return the negative uid.
|
2005-04-16 21:35:25 +02:00
|
|
|
|
|
|
|
If the uid is > 0, the backend should set the uid to this, if it can.
|
|
|
|
If it cannot set the uid to that, it will save it anyway.
|
|
|
|
It will return the uid assigned in any case.
|
2011-09-16 11:44:50 +02:00
|
|
|
|
|
|
|
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.
|
2005-04-16 21:35:25 +02:00
|
|
|
"""
|
2016-10-24 03:19:26 +02:00
|
|
|
|
2011-09-16 11:44:50 +02:00
|
|
|
self.ui.savemessage('imap', uid, flags, self)
|
2011-03-16 16:24:07 +01:00
|
|
|
# Mapped UID instances require the source to already have a
|
|
|
|
# positive UID, so simply return here.
|
2005-04-16 21:35:25 +02:00
|
|
|
if uid < 0:
|
|
|
|
return uid
|
2011-03-16 16:24:07 +01:00
|
|
|
|
2015-02-22 14:08:39 +01:00
|
|
|
# If msg uid already exists, just modify the flags.
|
2005-04-16 21:35:25 +02:00
|
|
|
if uid in self.r2l:
|
|
|
|
self.savemessageflags(uid, flags)
|
|
|
|
return uid
|
2011-03-16 16:24:07 +01:00
|
|
|
|
2021-02-19 23:00:15 +01:00
|
|
|
newluid = self._mb.savemessage(-1, msg, flags, rtime)
|
2005-04-16 21:35:25 +02:00
|
|
|
if newluid < 1:
|
2017-06-12 00:18:53 +02:00
|
|
|
raise OfflineImapError("server of repository '%s' did not return "
|
2020-08-29 19:48:04 +02:00
|
|
|
"a valid UID (got '%s') for UID '%s' from '%s'" % (
|
|
|
|
self._mb.getname(), newluid, uid, self.getname()
|
|
|
|
),
|
|
|
|
OfflineImapError.ERROR.MESSAGE
|
|
|
|
)
|
2016-06-28 03:03:11 +02:00
|
|
|
with self.maplock:
|
2005-04-16 21:35:25 +02:00
|
|
|
self.diskl2r[newluid] = uid
|
|
|
|
self.diskr2l[uid] = newluid
|
|
|
|
self.l2r[newluid] = uid
|
|
|
|
self.r2l[uid] = newluid
|
2016-06-28 03:03:11 +02:00
|
|
|
self._savemaps()
|
2011-06-16 17:22:29 +02:00
|
|
|
return uid
|
2005-04-16 21:35:25 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2005-04-16 21:35:25 +02:00
|
|
|
def getmessageflags(self, uid):
|
Simplify MappedIMAPFolder, fixing bugs
Previously, we instanciated an MappedImapFolder, and would cleverly (too
cleverly?) invoke methods on it casting it to an IMAPFolder by calling
methods such as: self._mb.cachemessages(self) where self._MB is the class
IMAPFolder and self and instance of MappedImapFolder. If
e.g. cachemessages() invokes a method uidexists() which exists for
MappedImapFolder, but not directly in IMAPFolder, I am not sure if
Python would at some point attempt to use the method of the wrong class.
Also, this leads to some twisted thinking as our class would in same
cases act as an IMAPFolder and in some cases as an MappedImapFOlder and
it is not always clear if we mean REMOTE UID or LOCAL UID.
This commit simplifies the class, by a)doing away with the complex Mixin
construct and directly inheriting from IMAPFOlder (so we get all the
IMAPFOlder methods that we can inherit). We instantiate self._mb as a
new instance of IMAPFolder which represents the local IMAP using local
UIDs, separating the MappedIMAPFolder construct logically from the
IMAPFolder somewhat.
In the long run, I would like to remove self._mb completely and simply
override any method that needs overriding, but let us take small and
understandable baby steps here.
Reported-and-tested-by: Vincent Beffara <vbeffara@gmail.com>
Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-06-24 11:28:30 +02:00
|
|
|
return self._mb.getmessageflags(self.r2l[uid])
|
2005-04-16 21:35:25 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2006-08-22 03:09:36 +02:00
|
|
|
def getmessagetime(self, uid):
|
|
|
|
return None
|
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2005-04-16 21:35:25 +02:00
|
|
|
def savemessageflags(self, uid, flags):
|
2015-02-22 14:08:39 +01:00
|
|
|
"""Note that this function does not check against dryrun settings,
|
2011-09-16 11:44:50 +02:00
|
|
|
so you need to ensure that it is never called in a
|
|
|
|
dryrun mode."""
|
2015-02-22 14:08:39 +01:00
|
|
|
|
Simplify MappedIMAPFolder, fixing bugs
Previously, we instanciated an MappedImapFolder, and would cleverly (too
cleverly?) invoke methods on it casting it to an IMAPFolder by calling
methods such as: self._mb.cachemessages(self) where self._MB is the class
IMAPFolder and self and instance of MappedImapFolder. If
e.g. cachemessages() invokes a method uidexists() which exists for
MappedImapFolder, but not directly in IMAPFolder, I am not sure if
Python would at some point attempt to use the method of the wrong class.
Also, this leads to some twisted thinking as our class would in same
cases act as an IMAPFolder and in some cases as an MappedImapFOlder and
it is not always clear if we mean REMOTE UID or LOCAL UID.
This commit simplifies the class, by a)doing away with the complex Mixin
construct and directly inheriting from IMAPFOlder (so we get all the
IMAPFOlder methods that we can inherit). We instantiate self._mb as a
new instance of IMAPFolder which represents the local IMAP using local
UIDs, separating the MappedIMAPFolder construct logically from the
IMAPFolder somewhat.
In the long run, I would like to remove self._mb completely and simply
override any method that needs overriding, but let us take small and
understandable baby steps here.
Reported-and-tested-by: Vincent Beffara <vbeffara@gmail.com>
Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-06-24 11:28:30 +02:00
|
|
|
self._mb.savemessageflags(self.r2l[uid], flags)
|
2005-04-16 21:35:25 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2005-04-16 21:35:25 +02:00
|
|
|
def addmessageflags(self, uid, flags):
|
Simplify MappedIMAPFolder, fixing bugs
Previously, we instanciated an MappedImapFolder, and would cleverly (too
cleverly?) invoke methods on it casting it to an IMAPFolder by calling
methods such as: self._mb.cachemessages(self) where self._MB is the class
IMAPFolder and self and instance of MappedImapFolder. If
e.g. cachemessages() invokes a method uidexists() which exists for
MappedImapFolder, but not directly in IMAPFolder, I am not sure if
Python would at some point attempt to use the method of the wrong class.
Also, this leads to some twisted thinking as our class would in same
cases act as an IMAPFolder and in some cases as an MappedImapFOlder and
it is not always clear if we mean REMOTE UID or LOCAL UID.
This commit simplifies the class, by a)doing away with the complex Mixin
construct and directly inheriting from IMAPFOlder (so we get all the
IMAPFOlder methods that we can inherit). We instantiate self._mb as a
new instance of IMAPFolder which represents the local IMAP using local
UIDs, separating the MappedIMAPFolder construct logically from the
IMAPFolder somewhat.
In the long run, I would like to remove self._mb completely and simply
override any method that needs overriding, but let us take small and
understandable baby steps here.
Reported-and-tested-by: Vincent Beffara <vbeffara@gmail.com>
Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-06-24 11:28:30 +02:00
|
|
|
self._mb.addmessageflags(self.r2l[uid], flags)
|
2005-04-16 21:35:25 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2005-04-16 21:35:25 +02:00
|
|
|
def addmessagesflags(self, uidlist, flags):
|
Simplify MappedIMAPFolder, fixing bugs
Previously, we instanciated an MappedImapFolder, and would cleverly (too
cleverly?) invoke methods on it casting it to an IMAPFolder by calling
methods such as: self._mb.cachemessages(self) where self._MB is the class
IMAPFolder and self and instance of MappedImapFolder. If
e.g. cachemessages() invokes a method uidexists() which exists for
MappedImapFolder, but not directly in IMAPFolder, I am not sure if
Python would at some point attempt to use the method of the wrong class.
Also, this leads to some twisted thinking as our class would in same
cases act as an IMAPFolder and in some cases as an MappedImapFOlder and
it is not always clear if we mean REMOTE UID or LOCAL UID.
This commit simplifies the class, by a)doing away with the complex Mixin
construct and directly inheriting from IMAPFOlder (so we get all the
IMAPFOlder methods that we can inherit). We instantiate self._mb as a
new instance of IMAPFolder which represents the local IMAP using local
UIDs, separating the MappedIMAPFolder construct logically from the
IMAPFolder somewhat.
In the long run, I would like to remove self._mb completely and simply
override any method that needs overriding, but let us take small and
understandable baby steps here.
Reported-and-tested-by: Vincent Beffara <vbeffara@gmail.com>
Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-06-24 11:28:30 +02:00
|
|
|
self._mb.addmessagesflags(self._uidlist(self.r2l, uidlist),
|
2005-04-16 21:35:25 +02:00
|
|
|
flags)
|
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2011-08-30 10:52:11 +02:00
|
|
|
def change_message_uid(self, ruid, new_ruid):
|
|
|
|
"""Change the message from existing ruid to new_ruid
|
|
|
|
|
2020-10-11 23:57:17 +02:00
|
|
|
The old remote UID will be changed to a new
|
2011-08-30 10:52:11 +02:00
|
|
|
UID. The UIDMaps case handles this efficiently by simply
|
2020-10-11 23:57:17 +02:00
|
|
|
changing the mappings file.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ruid: Remote UID
|
|
|
|
new_ruid: New Remote UID
|
|
|
|
"""
|
2016-10-24 03:19:26 +02:00
|
|
|
|
2011-08-30 10:52:11 +02:00
|
|
|
if ruid not in self.r2l:
|
2020-08-29 19:48:04 +02:00
|
|
|
raise OfflineImapError("Cannot change unknown Maildir UID %s" %
|
|
|
|
ruid, OfflineImapError.ERROR.MESSAGE)
|
2020-08-30 13:53:57 +02:00
|
|
|
if ruid == new_ruid:
|
|
|
|
return # sanity check shortcut
|
|
|
|
|
2016-06-28 03:03:11 +02:00
|
|
|
with self.maplock:
|
2011-08-30 10:52:11 +02:00
|
|
|
luid = self.r2l[ruid]
|
|
|
|
self.l2r[luid] = new_ruid
|
|
|
|
del self.r2l[ruid]
|
|
|
|
self.r2l[new_ruid] = luid
|
2015-01-14 22:58:25 +01:00
|
|
|
# TODO: diskl2r|r2l are a pain to sync and should be done away with
|
|
|
|
# diskl2r only contains positive UIDs, so wrap in ifs.
|
2020-08-30 13:53:57 +02:00
|
|
|
if luid > 0:
|
|
|
|
self.diskl2r[luid] = new_ruid
|
|
|
|
if ruid > 0:
|
|
|
|
del self.diskr2l[ruid]
|
|
|
|
if new_ruid > 0:
|
|
|
|
self.diskr2l[new_ruid] = luid
|
2016-06-28 03:03:11 +02:00
|
|
|
self._savemaps()
|
2011-08-30 10:52:11 +02:00
|
|
|
|
2005-04-16 21:35:25 +02:00
|
|
|
def _mapped_delete(self, uidlist):
|
2016-06-28 03:03:11 +02:00
|
|
|
with self.maplock:
|
2005-04-16 21:35:25 +02:00
|
|
|
needssave = 0
|
|
|
|
for ruid in uidlist:
|
|
|
|
luid = self.r2l[ruid]
|
|
|
|
del self.r2l[ruid]
|
|
|
|
del self.l2r[luid]
|
|
|
|
if ruid > 0:
|
|
|
|
del self.diskr2l[ruid]
|
|
|
|
del self.diskl2r[luid]
|
|
|
|
needssave = 1
|
|
|
|
if needssave:
|
2016-06-28 03:03:11 +02:00
|
|
|
self._savemaps()
|
2005-04-16 21:35:25 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2005-04-16 21:35:25 +02:00
|
|
|
def deletemessageflags(self, uid, flags):
|
Simplify MappedIMAPFolder, fixing bugs
Previously, we instanciated an MappedImapFolder, and would cleverly (too
cleverly?) invoke methods on it casting it to an IMAPFolder by calling
methods such as: self._mb.cachemessages(self) where self._MB is the class
IMAPFolder and self and instance of MappedImapFolder. If
e.g. cachemessages() invokes a method uidexists() which exists for
MappedImapFolder, but not directly in IMAPFolder, I am not sure if
Python would at some point attempt to use the method of the wrong class.
Also, this leads to some twisted thinking as our class would in same
cases act as an IMAPFolder and in some cases as an MappedImapFOlder and
it is not always clear if we mean REMOTE UID or LOCAL UID.
This commit simplifies the class, by a)doing away with the complex Mixin
construct and directly inheriting from IMAPFOlder (so we get all the
IMAPFOlder methods that we can inherit). We instantiate self._mb as a
new instance of IMAPFolder which represents the local IMAP using local
UIDs, separating the MappedIMAPFolder construct logically from the
IMAPFolder somewhat.
In the long run, I would like to remove self._mb completely and simply
override any method that needs overriding, but let us take small and
understandable baby steps here.
Reported-and-tested-by: Vincent Beffara <vbeffara@gmail.com>
Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-06-24 11:28:30 +02:00
|
|
|
self._mb.deletemessageflags(self.r2l[uid], flags)
|
2005-04-16 21:35:25 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2005-04-16 21:35:25 +02:00
|
|
|
def deletemessagesflags(self, uidlist, flags):
|
Simplify MappedIMAPFolder, fixing bugs
Previously, we instanciated an MappedImapFolder, and would cleverly (too
cleverly?) invoke methods on it casting it to an IMAPFolder by calling
methods such as: self._mb.cachemessages(self) where self._MB is the class
IMAPFolder and self and instance of MappedImapFolder. If
e.g. cachemessages() invokes a method uidexists() which exists for
MappedImapFolder, but not directly in IMAPFolder, I am not sure if
Python would at some point attempt to use the method of the wrong class.
Also, this leads to some twisted thinking as our class would in same
cases act as an IMAPFolder and in some cases as an MappedImapFOlder and
it is not always clear if we mean REMOTE UID or LOCAL UID.
This commit simplifies the class, by a)doing away with the complex Mixin
construct and directly inheriting from IMAPFOlder (so we get all the
IMAPFOlder methods that we can inherit). We instantiate self._mb as a
new instance of IMAPFolder which represents the local IMAP using local
UIDs, separating the MappedIMAPFolder construct logically from the
IMAPFolder somewhat.
In the long run, I would like to remove self._mb completely and simply
override any method that needs overriding, but let us take small and
understandable baby steps here.
Reported-and-tested-by: Vincent Beffara <vbeffara@gmail.com>
Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-06-24 11:28:30 +02:00
|
|
|
self._mb.deletemessagesflags(self._uidlist(self.r2l, uidlist),
|
2020-08-29 19:48:04 +02:00
|
|
|
flags)
|
2005-04-16 21:35:25 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2005-04-16 21:35:25 +02:00
|
|
|
def deletemessage(self, uid):
|
Simplify MappedIMAPFolder, fixing bugs
Previously, we instanciated an MappedImapFolder, and would cleverly (too
cleverly?) invoke methods on it casting it to an IMAPFolder by calling
methods such as: self._mb.cachemessages(self) where self._MB is the class
IMAPFolder and self and instance of MappedImapFolder. If
e.g. cachemessages() invokes a method uidexists() which exists for
MappedImapFolder, but not directly in IMAPFolder, I am not sure if
Python would at some point attempt to use the method of the wrong class.
Also, this leads to some twisted thinking as our class would in same
cases act as an IMAPFolder and in some cases as an MappedImapFOlder and
it is not always clear if we mean REMOTE UID or LOCAL UID.
This commit simplifies the class, by a)doing away with the complex Mixin
construct and directly inheriting from IMAPFOlder (so we get all the
IMAPFOlder methods that we can inherit). We instantiate self._mb as a
new instance of IMAPFolder which represents the local IMAP using local
UIDs, separating the MappedIMAPFolder construct logically from the
IMAPFolder somewhat.
In the long run, I would like to remove self._mb completely and simply
override any method that needs overriding, but let us take small and
understandable baby steps here.
Reported-and-tested-by: Vincent Beffara <vbeffara@gmail.com>
Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-06-24 11:28:30 +02:00
|
|
|
self._mb.deletemessage(self.r2l[uid])
|
2005-04-16 21:35:25 +02:00
|
|
|
self._mapped_delete([uid])
|
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2005-04-16 21:35:25 +02:00
|
|
|
def deletemessages(self, uidlist):
|
Simplify MappedIMAPFolder, fixing bugs
Previously, we instanciated an MappedImapFolder, and would cleverly (too
cleverly?) invoke methods on it casting it to an IMAPFolder by calling
methods such as: self._mb.cachemessages(self) where self._MB is the class
IMAPFolder and self and instance of MappedImapFolder. If
e.g. cachemessages() invokes a method uidexists() which exists for
MappedImapFolder, but not directly in IMAPFolder, I am not sure if
Python would at some point attempt to use the method of the wrong class.
Also, this leads to some twisted thinking as our class would in same
cases act as an IMAPFolder and in some cases as an MappedImapFOlder and
it is not always clear if we mean REMOTE UID or LOCAL UID.
This commit simplifies the class, by a)doing away with the complex Mixin
construct and directly inheriting from IMAPFOlder (so we get all the
IMAPFOlder methods that we can inherit). We instantiate self._mb as a
new instance of IMAPFolder which represents the local IMAP using local
UIDs, separating the MappedIMAPFolder construct logically from the
IMAPFolder somewhat.
In the long run, I would like to remove self._mb completely and simply
override any method that needs overriding, but let us take small and
understandable baby steps here.
Reported-and-tested-by: Vincent Beffara <vbeffara@gmail.com>
Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2011-06-24 11:28:30 +02:00
|
|
|
self._mb.deletemessages(self._uidlist(self.r2l, uidlist))
|
2005-04-16 21:35:25 +02:00
|
|
|
self._mapped_delete(uidlist)
|