2002-06-19 08:08:59 +02:00
|
|
|
# Maildir folder support
|
2016-06-29 03:42:57 +02:00
|
|
|
# Copyright (C) 2002-2016 John Goerzen & contributors.
|
2002-06-19 07:55:12 +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
|
2003-04-16 21:23:45 +02:00
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2002-06-19 07:55:12 +02:00
|
|
|
#
|
|
|
|
# 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
|
2002-06-19 07:55:12 +02:00
|
|
|
|
2020-10-12 01:35:20 +02:00
|
|
|
import errno
|
2011-03-11 22:13:21 +01:00
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
import re
|
|
|
|
import os
|
2015-01-11 21:44:24 +01:00
|
|
|
from sys import exc_info
|
2003-05-06 21:26:12 +02:00
|
|
|
from threading import Lock
|
2020-09-03 20:44:08 +02:00
|
|
|
from hashlib import md5
|
2021-02-22 05:20:39 +01:00
|
|
|
from offlineimap import OfflineImapError
|
2016-06-29 03:42:57 +02:00
|
|
|
from .Base import BaseFolder
|
2011-06-13 15:22:37 +02:00
|
|
|
|
2011-08-30 11:01:49 +02:00
|
|
|
# Find the UID in a message filename
|
|
|
|
re_uidmatch = re.compile(',U=(\d+)')
|
|
|
|
# Find a numeric timestamp in a string (filename prefix)
|
2016-11-07 23:03:40 +01:00
|
|
|
re_timestampmatch = re.compile('(\d+)')
|
2002-07-23 21:55:18 +02:00
|
|
|
|
2015-10-28 10:56:16 +01:00
|
|
|
timehash = {}
|
2003-05-06 21:26:12 +02:00
|
|
|
timelock = Lock()
|
2002-06-22 06:32:29 +02:00
|
|
|
|
2016-11-07 23:03:40 +01:00
|
|
|
|
2015-10-28 10:56:16 +01:00
|
|
|
def _gettimeseq(date=None):
|
|
|
|
global timehash, timelock
|
2003-05-06 21:26:12 +02:00
|
|
|
timelock.acquire()
|
|
|
|
try:
|
2015-10-28 10:56:16 +01:00
|
|
|
if date is None:
|
2016-05-08 16:42:52 +02:00
|
|
|
date = int(time.time())
|
2016-05-08 17:22:04 +02:00
|
|
|
if date in timehash:
|
2015-10-28 10:56:16 +01:00
|
|
|
timehash[date] += 1
|
2003-05-06 21:26:12 +02:00
|
|
|
else:
|
2015-10-28 10:56:16 +01:00
|
|
|
timehash[date] = 0
|
2020-08-30 13:40:14 +02:00
|
|
|
return date, timehash[date]
|
2003-05-06 21:26:12 +02:00
|
|
|
finally:
|
|
|
|
timelock.release()
|
2002-06-19 07:55:12 +02:00
|
|
|
|
2016-11-07 23:03:40 +01:00
|
|
|
|
2002-06-19 08:08:59 +02:00
|
|
|
class MaildirFolder(BaseFolder):
|
2011-09-16 10:54:24 +02:00
|
|
|
def __init__(self, root, name, sep, repository):
|
2020-08-29 19:46:43 +02:00
|
|
|
self.sep = sep # needs to be set before super().__init__
|
2011-09-16 10:54:22 +02:00
|
|
|
super(MaildirFolder, self).__init__(name, repository)
|
2002-06-19 08:08:59 +02:00
|
|
|
self.root = root
|
2012-01-06 13:05:08 +01:00
|
|
|
# check if we should use a different infosep to support Win file systems
|
2011-07-12 09:34:02 +02:00
|
|
|
self.wincompatible = self.config.getdefaultboolean(
|
2020-08-29 19:46:43 +02:00
|
|
|
"Account " + self.accountname, "maildir-windows-compatible", False)
|
2011-08-30 11:01:17 +02:00
|
|
|
self.infosep = '!' if self.wincompatible else ':'
|
|
|
|
"""infosep is the separator between maildir name and flag appendix"""
|
2020-08-29 19:46:43 +02:00
|
|
|
self.re_flagmatch = re.compile('%s2,(\w*)' % self.infosep)
|
|
|
|
# self.ui is set in BaseFolder.init()
|
2011-08-30 11:01:49 +02:00
|
|
|
# Everything up to the first comma or colon (or ! if Windows):
|
2020-08-29 19:46:43 +02:00
|
|
|
self.re_prefixmatch = re.compile('([^' + self.infosep + ',]*)')
|
fix: don't loose local mails because of maxage
Suppose messages A and B were delivered to the remote folder at
"maxage + 1" days ago.
A was downloaded to the local folder "maxage + 1" days ago, but B was only
downloaded "maxage - 1" days ago (contrived scenario to illustrate the two
things that could happen). The behavior was that B gets deleted from the local
folder, but A did not. The expected behavior is that neither is deleted.
Starting where Base.py: __syncmessagesto_delete(self, dstfolder, statusfolder)
is called where:
- self is the remote folder
and
- dstfolder is the local folder.
It defines deletelist to be the list of messages in the status folder
messagelist that aren't in the remote folder messagelist with
not self.uidexists(uid)
A and B are both in the status folder. They're also both *NOT* in the remote
folder messagelist: this list is formed in IMAP.py: cachemessagelist(), which
calls _msgs_to_fetch(), which only asks the IMAP server for messages that are
"< maxage" days old.
Back to Base.py __syncmessagesto_delete(), look at the call
folder.deletemessages(deletelist), where folder is the local folder. This ends
up calling Maildir.py deletemessage() for each message on the deletelist. But we
see that this methods returns (instead of deleting anything) if the message is
in the local folder's messagelist. This messagelist was created by Maildir.py's
cachemessagelist(), which calls _scanfolder(), which tries to exclude messages
based on maxage. So at this point, we *WANT* A and B to be excluded -- then they
will be spared from deletion. This maxage check calls _iswithinmaxage(), and
actually does the date comparison based on the time found at the beginning of
the message's filename. These filenames were originally created in Maildir.py's
new_message_filename(), which calls _gettimeseq() to get the current time (i.e.
the time of retrieval).
Upshot: A's filename has an older timestamp than B's filename. A is excluded
from the local folder messagelist in _scanfolder(), hence spared from deletion
in deletemessage(); B is not excluded, and is deleted.
This patch does not address the timezone issue. As for the IMAP/timezone issue,
a similar issue is discussed in the thunderbird bug tracker here:
https://bugzilla.mozilla.org/show_bug.cgi?id=886534
In the end, they're solving a different problem, but they agree that
there is really no reliable way of guessing the IMAP server's internal
timezone.
Signed-off-by: Janna Martl <janna.martl109@gmail.com>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2015-03-07 10:50:33 +01:00
|
|
|
# folder's md, so we can match with recorded file md5 for validity.
|
2020-08-28 16:51:11 +02:00
|
|
|
self._foldermd5 = md5(self.getvisiblename().encode('utf-8')).hexdigest()
|
fix: don't loose local mails because of maxage
Suppose messages A and B were delivered to the remote folder at
"maxage + 1" days ago.
A was downloaded to the local folder "maxage + 1" days ago, but B was only
downloaded "maxage - 1" days ago (contrived scenario to illustrate the two
things that could happen). The behavior was that B gets deleted from the local
folder, but A did not. The expected behavior is that neither is deleted.
Starting where Base.py: __syncmessagesto_delete(self, dstfolder, statusfolder)
is called where:
- self is the remote folder
and
- dstfolder is the local folder.
It defines deletelist to be the list of messages in the status folder
messagelist that aren't in the remote folder messagelist with
not self.uidexists(uid)
A and B are both in the status folder. They're also both *NOT* in the remote
folder messagelist: this list is formed in IMAP.py: cachemessagelist(), which
calls _msgs_to_fetch(), which only asks the IMAP server for messages that are
"< maxage" days old.
Back to Base.py __syncmessagesto_delete(), look at the call
folder.deletemessages(deletelist), where folder is the local folder. This ends
up calling Maildir.py deletemessage() for each message on the deletelist. But we
see that this methods returns (instead of deleting anything) if the message is
in the local folder's messagelist. This messagelist was created by Maildir.py's
cachemessagelist(), which calls _scanfolder(), which tries to exclude messages
based on maxage. So at this point, we *WANT* A and B to be excluded -- then they
will be spared from deletion. This maxage check calls _iswithinmaxage(), and
actually does the date comparison based on the time found at the beginning of
the message's filename. These filenames were originally created in Maildir.py's
new_message_filename(), which calls _gettimeseq() to get the current time (i.e.
the time of retrieval).
Upshot: A's filename has an older timestamp than B's filename. A is excluded
from the local folder messagelist in _scanfolder(), hence spared from deletion
in deletemessage(); B is not excluded, and is deleted.
This patch does not address the timezone issue. As for the IMAP/timezone issue,
a similar issue is discussed in the thunderbird bug tracker here:
https://bugzilla.mozilla.org/show_bug.cgi?id=886534
In the end, they're solving a different problem, but they agree that
there is really no reliable way of guessing the IMAP server's internal
timezone.
Signed-off-by: Janna Martl <janna.martl109@gmail.com>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2015-03-07 10:50:33 +01:00
|
|
|
# Cache the full folder path, as we use getfullname() very often.
|
2011-06-10 17:32:39 +02:00
|
|
|
self._fullname = os.path.join(self.getroot(), self.getname())
|
2016-05-11 04:10:13 +02:00
|
|
|
# Modification time from 'Date' header.
|
|
|
|
utime_from_header_global = self.config.getdefaultboolean(
|
|
|
|
"general", "utime_from_header", False)
|
|
|
|
self._utime_from_header = self.config.getdefaultboolean(
|
|
|
|
self.repoconfname, "utime_from_header", utime_from_header_global)
|
2018-04-09 19:09:56 +02:00
|
|
|
# What do we substitute pathname separator in names (if any)
|
|
|
|
self.sep_subst = '-'
|
|
|
|
if os.path.sep == self.sep_subst:
|
|
|
|
self.sep_subst = '_'
|
2003-01-06 00:07:58 +01:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2002-06-20 07:14:35 +02:00
|
|
|
def getfullname(self):
|
2011-06-10 17:32:39 +02:00
|
|
|
"""Return the absolute file path to the Maildir folder (sans cur|new)"""
|
|
|
|
return self._fullname
|
2002-06-20 07:14:35 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2012-01-19 11:24:16 +01:00
|
|
|
def get_uidvalidity(self):
|
|
|
|
"""Retrieve the current connections UIDVALIDITY value
|
|
|
|
|
|
|
|
Maildirs have no notion of uidvalidity, so we just return a magic
|
2003-04-18 04:18:34 +02:00
|
|
|
token."""
|
|
|
|
return 42
|
2002-06-20 05:33:23 +02:00
|
|
|
|
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 _iswithintime(self, messagename, date):
|
|
|
|
"""Check to see if the given message is newer than date (a
|
|
|
|
time_struct) according to the maildir name which should begin
|
|
|
|
with a timestamp."""
|
2009-08-16 14:42:39 +02:00
|
|
|
|
2011-08-30 11:01:49 +02:00
|
|
|
timestampmatch = re_timestampmatch.search(messagename)
|
2013-08-27 13:57:55 +02:00
|
|
|
if not timestampmatch:
|
|
|
|
return True
|
2009-08-16 14:42:39 +02:00
|
|
|
timestampstr = timestampmatch.group()
|
2016-05-08 16:42:52 +02:00
|
|
|
timestamplong = int(timestampstr)
|
2020-08-30 13:40:14 +02:00
|
|
|
if timestamplong < time.mktime(date):
|
2009-08-16 14:42:39 +02:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2011-08-30 11:01:49 +02:00
|
|
|
def _parse_filename(self, filename):
|
|
|
|
"""Returns a messages file name components
|
|
|
|
|
|
|
|
Receives the file name (without path) of a msg. Usual format is
|
|
|
|
'<%d_%d.%d.%s>,U=<%d>,FMD5=<%s>:2,<FLAGS>' (pointy brackets
|
|
|
|
denoting the various components).
|
|
|
|
|
|
|
|
If FMD5 does not correspond with the current folder MD5, we will
|
|
|
|
return None for the UID & FMD5 (as it is not valid in this
|
|
|
|
folder). If UID or FMD5 can not be detected, we return `None`
|
|
|
|
for the respective element. If flags are empty or cannot be
|
|
|
|
detected, we return an empty flags list.
|
|
|
|
|
|
|
|
:returns: (prefix, UID, FMD5, flags). UID is a numeric "long"
|
2015-01-14 22:58:25 +01:00
|
|
|
type. flags is a set() of Maildir flags.
|
|
|
|
"""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2011-08-30 11:01:49 +02:00
|
|
|
prefix, uid, fmd5, flags = None, None, None, set()
|
|
|
|
prefixmatch = self.re_prefixmatch.match(filename)
|
|
|
|
if prefixmatch:
|
|
|
|
prefix = prefixmatch.group(1)
|
2020-08-29 19:46:43 +02:00
|
|
|
folderstr = ',FMD5=%s' % self._foldermd5
|
2011-08-30 11:01:49 +02:00
|
|
|
foldermatch = folderstr in filename
|
|
|
|
# If there was no folder MD5 specified, or if it mismatches,
|
|
|
|
# assume it is a foreign (new) message and ret: uid, fmd5 = None, None
|
2016-03-06 22:06:02 +01:00
|
|
|
|
|
|
|
# XXX: This is wrong behaviour: if FMD5 is missing or mismatches, assume
|
|
|
|
# the mail is new and **fix UID to None** to avoid any conflict.
|
|
|
|
|
|
|
|
# XXX: If UID is missing, I have no idea what FMD5 can do. Should be
|
|
|
|
# fixed to None in this case, too.
|
|
|
|
|
2011-08-30 11:01:49 +02:00
|
|
|
if foldermatch:
|
|
|
|
uidmatch = re_uidmatch.search(filename)
|
|
|
|
if uidmatch:
|
2016-05-08 16:42:52 +02:00
|
|
|
uid = int(uidmatch.group(1))
|
2011-08-30 11:01:49 +02:00
|
|
|
flagmatch = self.re_flagmatch.search(filename)
|
|
|
|
if flagmatch:
|
2015-11-20 20:09:14 +01:00
|
|
|
flags = set((c for c in flagmatch.group(1)))
|
2011-08-30 11:01:49 +02:00
|
|
|
return prefix, uid, fmd5, flags
|
2009-08-16 14:42:39 +02:00
|
|
|
|
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 _scanfolder(self, min_date=None, min_uid=None):
|
2011-08-30 11:01:49 +02:00
|
|
|
"""Cache the message list from a Maildir.
|
|
|
|
|
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
|
|
|
If min_date is set, this finds the min UID of all messages newer than
|
|
|
|
min_date and uses it as the real cutoff for considering messages.
|
|
|
|
This handles the edge cases where the date is much earlier than messages
|
|
|
|
with similar UID's (e.g. the UID was reassigned much later).
|
|
|
|
|
2016-08-13 16:25:19 +02:00
|
|
|
Maildir flags are:
|
|
|
|
D (draft) F (flagged) R (replied) S (seen) T (trashed),
|
|
|
|
plus lower-case letters for custom flags.
|
2015-01-14 22:58:25 +01:00
|
|
|
:returns: dict that can be used as self.messagelist.
|
|
|
|
"""
|
|
|
|
|
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
|
|
|
maxsize = self.getmaxsize()
|
|
|
|
|
2002-07-16 04:26:58 +02:00
|
|
|
retval = {}
|
2002-06-20 05:33:23 +02:00
|
|
|
files = []
|
2020-08-29 19:46:43 +02:00
|
|
|
nouidcounter = -1 # Messages without UIDs get negative UIDs.
|
2002-06-20 05:33:23 +02:00
|
|
|
for dirannex in ['new', 'cur']:
|
|
|
|
fulldirname = os.path.join(self.getfullname(), dirannex)
|
2011-08-30 11:01:49 +02:00
|
|
|
files.extend((dirannex, filename) for
|
2009-07-07 07:52:00 +02:00
|
|
|
filename in os.listdir(fulldirname))
|
2011-08-30 11:01:49 +02:00
|
|
|
|
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
|
|
|
date_excludees = {}
|
2011-08-30 11:01:49 +02:00
|
|
|
for dirannex, filename in files:
|
2015-12-05 12:49:41 +01:00
|
|
|
if filename.startswith('.'):
|
2020-08-29 19:46:43 +02:00
|
|
|
continue # Ignore dot files.
|
2011-08-30 11:01:49 +02:00
|
|
|
# We store just dirannex and filename, ie 'cur/123...'
|
|
|
|
filepath = os.path.join(dirannex, filename)
|
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
|
|
|
# Check maxsize if this message should be considered.
|
2016-08-13 16:25:19 +02:00
|
|
|
if maxsize and (os.path.getsize(
|
|
|
|
os.path.join(self.getfullname(), filepath)) > maxsize):
|
2011-08-30 11:01:49 +02:00
|
|
|
continue
|
|
|
|
|
2016-08-13 16:25:19 +02:00
|
|
|
prefix, uid, fmd5, flags = self._parse_filename(filename)
|
2020-08-29 19:46:43 +02:00
|
|
|
if uid is None: # Assign negative uid to upload it.
|
2002-06-20 06:14:54 +02:00
|
|
|
uid = nouidcounter
|
2002-06-22 06:39:02 +02:00
|
|
|
nouidcounter -= 1
|
2020-08-29 19:46:43 +02:00
|
|
|
else: # It comes from our folder.
|
2011-08-30 11:01:49 +02:00
|
|
|
uidmatch = re_uidmatch.search(filename)
|
2002-06-22 06:32:29 +02:00
|
|
|
if not uidmatch:
|
|
|
|
uid = nouidcounter
|
|
|
|
nouidcounter -= 1
|
|
|
|
else:
|
2016-05-08 16:42:52 +02:00
|
|
|
uid = int(uidmatch.group(1))
|
2020-08-30 11:15:00 +02:00
|
|
|
if min_uid is not None and uid > 0 and uid < min_uid:
|
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
|
|
|
continue
|
2020-08-30 11:15:00 +02:00
|
|
|
if min_date is not None and not self._iswithintime(filename, min_date):
|
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
|
|
|
# Keep track of messages outside of the time limit, because they
|
|
|
|
# still might have UID > min(UIDs of within-min_date). We hit
|
|
|
|
# this case for maxage if any message had a known/valid datetime
|
|
|
|
# and was re-uploaded because the UID in the filename got lost
|
|
|
|
# (e.g. local copy/move). On next sync, it was assigned a new
|
|
|
|
# UID from the server and will be included in the SEARCH
|
|
|
|
# condition. So, we must re-include them later in this method
|
|
|
|
# in order to avoid inconsistent lists of messages.
|
|
|
|
date_excludees[uid] = self.msglist_item_initializer(uid)
|
|
|
|
date_excludees[uid]['flags'] = flags
|
|
|
|
date_excludees[uid]['filename'] = filepath
|
|
|
|
else:
|
|
|
|
# 'filename' is 'dirannex/filename', e.g. cur/123,U=1,FMD5=1:2,S
|
|
|
|
retval[uid] = self.msglist_item_initializer(uid)
|
|
|
|
retval[uid]['flags'] = flags
|
|
|
|
retval[uid]['filename'] = filepath
|
2020-08-30 11:15:00 +02:00
|
|
|
if min_date is not None:
|
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
|
|
|
# Re-include messages with high enough uid's.
|
2016-05-08 16:55:13 +02:00
|
|
|
positive_uids = [uid for uid in retval if uid > 0]
|
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
|
|
|
if positive_uids:
|
|
|
|
min_uid = min(positive_uids)
|
2020-08-28 03:32:43 +02:00
|
|
|
for uid in list(date_excludees.keys()):
|
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
|
|
|
if uid > min_uid:
|
|
|
|
# This message was originally excluded because of
|
|
|
|
# its date. It is re-included now because we want all
|
|
|
|
# messages with UID > min_uid.
|
|
|
|
retval[uid] = date_excludees[uid]
|
2002-07-16 04:26:58 +02:00
|
|
|
return retval
|
|
|
|
|
2021-04-13 06:01:26 +02:00
|
|
|
def _quote_boundary_fix(self, raw_msg_bytes):
|
|
|
|
"""Modify a raw message to quote the boundary separator for multipart messages.
|
|
|
|
|
|
|
|
This function quotes only the first occurrence of the boundary field in
|
|
|
|
the email header, and quotes any boundary value. Improperly quoted
|
|
|
|
boundary fields can give the internal python email library issues.
|
|
|
|
|
|
|
|
:returns: The raw byte stream containing the quoted boundary
|
|
|
|
"""
|
|
|
|
# Use re.split to extract just the header, and search for the boundary in
|
|
|
|
# the context-type header and extract just the boundary and characters per
|
|
|
|
# RFC 2046 ( see https://tools.ietf.org/html/rfc2046#section-5.1.1 )
|
|
|
|
# We don't cap the length to 70 characters, because we are just trying to
|
|
|
|
# soft fix this message to resolve the python library looking for properly
|
|
|
|
# quoted boundaries.
|
|
|
|
try: boundary_field = \
|
2021-04-14 17:14:44 +02:00
|
|
|
re.search(b"content-type:.*(boundary=[\"]?[A-Za-z0-9'()+_,-./:=? ]+[\"]?)",
|
2021-04-13 06:01:26 +02:00
|
|
|
re.split(b'[\r]?\n[\r]?\n',raw_msg_bytes)[0],re.IGNORECASE).group(1)
|
|
|
|
except AttributeError:
|
|
|
|
# No match
|
|
|
|
return raw_msg_bytes
|
|
|
|
# get the boundary field, and strip off any trailing ws (against RFC rules, leading ws is OK)
|
|
|
|
# if it was already quoted, well then there was nothing to fix
|
|
|
|
boundary, value = boundary_field.split(b'=',1)
|
|
|
|
value = value.rstrip()
|
|
|
|
# ord(b'"') == 34
|
|
|
|
if value[0] == value[-1] == 34:
|
|
|
|
# Sanity Check - Do not requote if already quoted.
|
|
|
|
# A quoted boundary was the end goal so return the original
|
|
|
|
#
|
|
|
|
# No need to worry about if the original email did something like:
|
|
|
|
# boundary="ahahah " as the email library will trim the ws for us
|
|
|
|
return raw_msg_bytes
|
|
|
|
else:
|
|
|
|
new_field = b''.join([boundary,b'="',value,b'"'])
|
|
|
|
return(raw_msg_bytes.replace(boundary_field,new_field,1))
|
|
|
|
|
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
Daniel Jacobowitz patches
fixes deb#433732
Date: Sun, 30 Sep 2007 13:54:56 -0400
From: Daniel Jacobowitz <drow@false.org>
To: offlineimap@complete.org
Subject: Assorted patches
Here's the result of a lazy Sunday hacking on offlineimap. Sorry for
not breaking this into multiple patches. They're mostly logically
independent so just ask if that would make a difference.
First, a new -q (quick) option. The quick option means to only update
folders that seem to have had significant changes. For Maildir, any
change to any message UID or flags is significant, because checking
the flags doesn't add a significant cost. For IMAP, only a change to
the total number of messages or a change in the UID of the most recent
message is significant. This should catch everything except for
flags changes.
The difference in bandwidth is astonishing: a quick sync takes 80K
instead of 5.3MB, and 28 seconds instead of 90.
There's a configuration variable that lets you say every tenth sync
should update flags, but let all the intervening ones be lighter.
Second, a fix to the UID validity problems many people have been
reporting with Courier. As discussed in Debian bug #433732, I changed
the UID validity check to use SELECT unless the server complains that
the folder is read-only. This avoids the Courier bug (see the Debian
log for more details). This won't fix existing validity errors, you
need to remove the local status and validity files by hand and resync.
Third, some speedups in Maildir checking. It's still pretty slow
due to a combination of poor performance in os.listdir (never reads
more than 4K of directory entries at a time) and some semaphore that
leads to lots of futex wake operations, but at least this saves
20% or so of the CPU time running offlineimap on a single folder:
Time with quick refresh and md5 in loop: 4.75s user 0.46s system 12%
cpu 41.751 total
Time with quick refresh and md5 out of loop: 4.38s user 0.50s system
14% cpu 34.799 total
Time using string compare to check folder: 4.11s user 0.47s system 13%
cpu 34.788 total
And fourth, some display fixes for Curses.Blinkenlights. I made
warnings more visible, made the new quick sync message cyan, and
made all not explicitly colored messages grey. That last one was
really bugging me. Any time OfflineIMAP printed a warning in
this UI, it had even odds of coming out black on black!
Anyway, I hope these are useful. I'm happy to revise them if you see
a problem.
--
Daniel Jacobowitz
CodeSourcery
2007-10-01 23:20:37 +02:00
|
|
|
def quickchanged(self, statusfolder):
|
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
|
|
|
"""Returns True if the Maildir has changed
|
|
|
|
|
|
|
|
Assumes cachemessagelist() has already been called """
|
fix: don't loose local mails because of maxage
Suppose messages A and B were delivered to the remote folder at
"maxage + 1" days ago.
A was downloaded to the local folder "maxage + 1" days ago, but B was only
downloaded "maxage - 1" days ago (contrived scenario to illustrate the two
things that could happen). The behavior was that B gets deleted from the local
folder, but A did not. The expected behavior is that neither is deleted.
Starting where Base.py: __syncmessagesto_delete(self, dstfolder, statusfolder)
is called where:
- self is the remote folder
and
- dstfolder is the local folder.
It defines deletelist to be the list of messages in the status folder
messagelist that aren't in the remote folder messagelist with
not self.uidexists(uid)
A and B are both in the status folder. They're also both *NOT* in the remote
folder messagelist: this list is formed in IMAP.py: cachemessagelist(), which
calls _msgs_to_fetch(), which only asks the IMAP server for messages that are
"< maxage" days old.
Back to Base.py __syncmessagesto_delete(), look at the call
folder.deletemessages(deletelist), where folder is the local folder. This ends
up calling Maildir.py deletemessage() for each message on the deletelist. But we
see that this methods returns (instead of deleting anything) if the message is
in the local folder's messagelist. This messagelist was created by Maildir.py's
cachemessagelist(), which calls _scanfolder(), which tries to exclude messages
based on maxage. So at this point, we *WANT* A and B to be excluded -- then they
will be spared from deletion. This maxage check calls _iswithinmaxage(), and
actually does the date comparison based on the time found at the beginning of
the message's filename. These filenames were originally created in Maildir.py's
new_message_filename(), which calls _gettimeseq() to get the current time (i.e.
the time of retrieval).
Upshot: A's filename has an older timestamp than B's filename. A is excluded
from the local folder messagelist in _scanfolder(), hence spared from deletion
in deletemessage(); B is not excluded, and is deleted.
This patch does not address the timezone issue. As for the IMAP/timezone issue,
a similar issue is discussed in the thunderbird bug tracker here:
https://bugzilla.mozilla.org/show_bug.cgi?id=886534
In the end, they're solving a different problem, but they agree that
there is really no reliable way of guessing the IMAP server's internal
timezone.
Signed-off-by: Janna Martl <janna.martl109@gmail.com>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2015-03-07 10:50:33 +01:00
|
|
|
# Folder has different uids than statusfolder => TRUE.
|
2011-04-11 18:09:07 +02:00
|
|
|
if sorted(self.getmessageuidlist()) != \
|
|
|
|
sorted(statusfolder.getmessageuidlist()):
|
Daniel Jacobowitz patches
fixes deb#433732
Date: Sun, 30 Sep 2007 13:54:56 -0400
From: Daniel Jacobowitz <drow@false.org>
To: offlineimap@complete.org
Subject: Assorted patches
Here's the result of a lazy Sunday hacking on offlineimap. Sorry for
not breaking this into multiple patches. They're mostly logically
independent so just ask if that would make a difference.
First, a new -q (quick) option. The quick option means to only update
folders that seem to have had significant changes. For Maildir, any
change to any message UID or flags is significant, because checking
the flags doesn't add a significant cost. For IMAP, only a change to
the total number of messages or a change in the UID of the most recent
message is significant. This should catch everything except for
flags changes.
The difference in bandwidth is astonishing: a quick sync takes 80K
instead of 5.3MB, and 28 seconds instead of 90.
There's a configuration variable that lets you say every tenth sync
should update flags, but let all the intervening ones be lighter.
Second, a fix to the UID validity problems many people have been
reporting with Courier. As discussed in Debian bug #433732, I changed
the UID validity check to use SELECT unless the server complains that
the folder is read-only. This avoids the Courier bug (see the Debian
log for more details). This won't fix existing validity errors, you
need to remove the local status and validity files by hand and resync.
Third, some speedups in Maildir checking. It's still pretty slow
due to a combination of poor performance in os.listdir (never reads
more than 4K of directory entries at a time) and some semaphore that
leads to lots of futex wake operations, but at least this saves
20% or so of the CPU time running offlineimap on a single folder:
Time with quick refresh and md5 in loop: 4.75s user 0.46s system 12%
cpu 41.751 total
Time with quick refresh and md5 out of loop: 4.38s user 0.50s system
14% cpu 34.799 total
Time using string compare to check folder: 4.11s user 0.47s system 13%
cpu 34.788 total
And fourth, some display fixes for Curses.Blinkenlights. I made
warnings more visible, made the new quick sync message cyan, and
made all not explicitly colored messages grey. That last one was
really bugging me. Any time OfflineIMAP printed a warning in
this UI, it had even odds of coming out black on black!
Anyway, I hope these are useful. I'm happy to revise them if you see
a problem.
--
Daniel Jacobowitz
CodeSourcery
2007-10-01 23:20:37 +02:00
|
|
|
return True
|
fix: don't loose local mails because of maxage
Suppose messages A and B were delivered to the remote folder at
"maxage + 1" days ago.
A was downloaded to the local folder "maxage + 1" days ago, but B was only
downloaded "maxage - 1" days ago (contrived scenario to illustrate the two
things that could happen). The behavior was that B gets deleted from the local
folder, but A did not. The expected behavior is that neither is deleted.
Starting where Base.py: __syncmessagesto_delete(self, dstfolder, statusfolder)
is called where:
- self is the remote folder
and
- dstfolder is the local folder.
It defines deletelist to be the list of messages in the status folder
messagelist that aren't in the remote folder messagelist with
not self.uidexists(uid)
A and B are both in the status folder. They're also both *NOT* in the remote
folder messagelist: this list is formed in IMAP.py: cachemessagelist(), which
calls _msgs_to_fetch(), which only asks the IMAP server for messages that are
"< maxage" days old.
Back to Base.py __syncmessagesto_delete(), look at the call
folder.deletemessages(deletelist), where folder is the local folder. This ends
up calling Maildir.py deletemessage() for each message on the deletelist. But we
see that this methods returns (instead of deleting anything) if the message is
in the local folder's messagelist. This messagelist was created by Maildir.py's
cachemessagelist(), which calls _scanfolder(), which tries to exclude messages
based on maxage. So at this point, we *WANT* A and B to be excluded -- then they
will be spared from deletion. This maxage check calls _iswithinmaxage(), and
actually does the date comparison based on the time found at the beginning of
the message's filename. These filenames were originally created in Maildir.py's
new_message_filename(), which calls _gettimeseq() to get the current time (i.e.
the time of retrieval).
Upshot: A's filename has an older timestamp than B's filename. A is excluded
from the local folder messagelist in _scanfolder(), hence spared from deletion
in deletemessage(); B is not excluded, and is deleted.
This patch does not address the timezone issue. As for the IMAP/timezone issue,
a similar issue is discussed in the thunderbird bug tracker here:
https://bugzilla.mozilla.org/show_bug.cgi?id=886534
In the end, they're solving a different problem, but they agree that
there is really no reliable way of guessing the IMAP server's internal
timezone.
Signed-off-by: Janna Martl <janna.martl109@gmail.com>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2015-03-07 10:50:33 +01:00
|
|
|
# Also check for flag changes, it's quick on a Maildir.
|
2020-08-28 03:32:43 +02:00
|
|
|
for (uid, message) in list(self.getmessagelist().items()):
|
2011-04-11 18:09:07 +02:00
|
|
|
if message['flags'] != statusfolder.getmessageflags(uid):
|
Daniel Jacobowitz patches
fixes deb#433732
Date: Sun, 30 Sep 2007 13:54:56 -0400
From: Daniel Jacobowitz <drow@false.org>
To: offlineimap@complete.org
Subject: Assorted patches
Here's the result of a lazy Sunday hacking on offlineimap. Sorry for
not breaking this into multiple patches. They're mostly logically
independent so just ask if that would make a difference.
First, a new -q (quick) option. The quick option means to only update
folders that seem to have had significant changes. For Maildir, any
change to any message UID or flags is significant, because checking
the flags doesn't add a significant cost. For IMAP, only a change to
the total number of messages or a change in the UID of the most recent
message is significant. This should catch everything except for
flags changes.
The difference in bandwidth is astonishing: a quick sync takes 80K
instead of 5.3MB, and 28 seconds instead of 90.
There's a configuration variable that lets you say every tenth sync
should update flags, but let all the intervening ones be lighter.
Second, a fix to the UID validity problems many people have been
reporting with Courier. As discussed in Debian bug #433732, I changed
the UID validity check to use SELECT unless the server complains that
the folder is read-only. This avoids the Courier bug (see the Debian
log for more details). This won't fix existing validity errors, you
need to remove the local status and validity files by hand and resync.
Third, some speedups in Maildir checking. It's still pretty slow
due to a combination of poor performance in os.listdir (never reads
more than 4K of directory entries at a time) and some semaphore that
leads to lots of futex wake operations, but at least this saves
20% or so of the CPU time running offlineimap on a single folder:
Time with quick refresh and md5 in loop: 4.75s user 0.46s system 12%
cpu 41.751 total
Time with quick refresh and md5 out of loop: 4.38s user 0.50s system
14% cpu 34.799 total
Time using string compare to check folder: 4.11s user 0.47s system 13%
cpu 34.788 total
And fourth, some display fixes for Curses.Blinkenlights. I made
warnings more visible, made the new quick sync message cyan, and
made all not explicitly colored messages grey. That last one was
really bugging me. Any time OfflineIMAP printed a warning in
this UI, it had even odds of coming out black on black!
Anyway, I hope these are useful. I'm happy to revise them if you see
a problem.
--
Daniel Jacobowitz
CodeSourcery
2007-10-01 23:20:37 +02:00
|
|
|
return True
|
fix: don't loose local mails because of maxage
Suppose messages A and B were delivered to the remote folder at
"maxage + 1" days ago.
A was downloaded to the local folder "maxage + 1" days ago, but B was only
downloaded "maxage - 1" days ago (contrived scenario to illustrate the two
things that could happen). The behavior was that B gets deleted from the local
folder, but A did not. The expected behavior is that neither is deleted.
Starting where Base.py: __syncmessagesto_delete(self, dstfolder, statusfolder)
is called where:
- self is the remote folder
and
- dstfolder is the local folder.
It defines deletelist to be the list of messages in the status folder
messagelist that aren't in the remote folder messagelist with
not self.uidexists(uid)
A and B are both in the status folder. They're also both *NOT* in the remote
folder messagelist: this list is formed in IMAP.py: cachemessagelist(), which
calls _msgs_to_fetch(), which only asks the IMAP server for messages that are
"< maxage" days old.
Back to Base.py __syncmessagesto_delete(), look at the call
folder.deletemessages(deletelist), where folder is the local folder. This ends
up calling Maildir.py deletemessage() for each message on the deletelist. But we
see that this methods returns (instead of deleting anything) if the message is
in the local folder's messagelist. This messagelist was created by Maildir.py's
cachemessagelist(), which calls _scanfolder(), which tries to exclude messages
based on maxage. So at this point, we *WANT* A and B to be excluded -- then they
will be spared from deletion. This maxage check calls _iswithinmaxage(), and
actually does the date comparison based on the time found at the beginning of
the message's filename. These filenames were originally created in Maildir.py's
new_message_filename(), which calls _gettimeseq() to get the current time (i.e.
the time of retrieval).
Upshot: A's filename has an older timestamp than B's filename. A is excluded
from the local folder messagelist in _scanfolder(), hence spared from deletion
in deletemessage(); B is not excluded, and is deleted.
This patch does not address the timezone issue. As for the IMAP/timezone issue,
a similar issue is discussed in the thunderbird bug tracker here:
https://bugzilla.mozilla.org/show_bug.cgi?id=886534
In the end, they're solving a different problem, but they agree that
there is really no reliable way of guessing the IMAP server's internal
timezone.
Signed-off-by: Janna Martl <janna.martl109@gmail.com>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2015-03-07 10:50:33 +01:00
|
|
|
return False # Nope, nothing changed.
|
Daniel Jacobowitz patches
fixes deb#433732
Date: Sun, 30 Sep 2007 13:54:56 -0400
From: Daniel Jacobowitz <drow@false.org>
To: offlineimap@complete.org
Subject: Assorted patches
Here's the result of a lazy Sunday hacking on offlineimap. Sorry for
not breaking this into multiple patches. They're mostly logically
independent so just ask if that would make a difference.
First, a new -q (quick) option. The quick option means to only update
folders that seem to have had significant changes. For Maildir, any
change to any message UID or flags is significant, because checking
the flags doesn't add a significant cost. For IMAP, only a change to
the total number of messages or a change in the UID of the most recent
message is significant. This should catch everything except for
flags changes.
The difference in bandwidth is astonishing: a quick sync takes 80K
instead of 5.3MB, and 28 seconds instead of 90.
There's a configuration variable that lets you say every tenth sync
should update flags, but let all the intervening ones be lighter.
Second, a fix to the UID validity problems many people have been
reporting with Courier. As discussed in Debian bug #433732, I changed
the UID validity check to use SELECT unless the server complains that
the folder is read-only. This avoids the Courier bug (see the Debian
log for more details). This won't fix existing validity errors, you
need to remove the local status and validity files by hand and resync.
Third, some speedups in Maildir checking. It's still pretty slow
due to a combination of poor performance in os.listdir (never reads
more than 4K of directory entries at a time) and some semaphore that
leads to lots of futex wake operations, but at least this saves
20% or so of the CPU time running offlineimap on a single folder:
Time with quick refresh and md5 in loop: 4.75s user 0.46s system 12%
cpu 41.751 total
Time with quick refresh and md5 out of loop: 4.38s user 0.50s system
14% cpu 34.799 total
Time using string compare to check folder: 4.11s user 0.47s system 13%
cpu 34.788 total
And fourth, some display fixes for Curses.Blinkenlights. I made
warnings more visible, made the new quick sync message cyan, and
made all not explicitly colored messages grey. That last one was
really bugging me. Any time OfflineIMAP printed a warning in
this UI, it had even odds of coming out black on black!
Anyway, I hope these are useful. I'm happy to revise them if you see
a problem.
--
Daniel Jacobowitz
CodeSourcery
2007-10-01 23:20:37 +02:00
|
|
|
|
2014-08-03 14:47:26 +02:00
|
|
|
# Interface from BaseFolder
|
|
|
|
def msglist_item_initializer(self, uid):
|
|
|
|
return {'flags': set(), 'filename': '/no-dir/no-such-file/'}
|
|
|
|
|
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):
|
2015-02-13 17:02:33 +01:00
|
|
|
if self.ismessagelistempty():
|
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.ui.loadmessagelist(self.repository, self)
|
|
|
|
self.messagelist = self._scanfolder(min_date=min_date,
|
2020-08-29 19:46:43 +02:00
|
|
|
min_uid=min_uid)
|
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.ui.messagelistloaded(self.repository, self, self.getmessagecount())
|
2012-01-06 13:05:08 +01:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2002-06-20 06:59:57 +02:00
|
|
|
def getmessage(self, uid):
|
2021-02-22 05:09:33 +01:00
|
|
|
"""Returns an email message object."""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2002-07-24 01:36:44 +02:00
|
|
|
filename = self.messagelist[uid]['filename']
|
2011-06-10 17:32:40 +02:00
|
|
|
filepath = os.path.join(self.getfullname(), filename)
|
2021-02-09 20:58:30 +01:00
|
|
|
fd = open(filepath, 'rb')
|
2021-02-23 22:17:54 +01:00
|
|
|
retval = self.parser['8bit'].parse(fd)
|
2021-04-13 06:01:26 +02:00
|
|
|
try:
|
|
|
|
_ = retval.as_bytes(policy=self.policy['8bit'])
|
|
|
|
except UnicodeEncodeError as err:
|
|
|
|
if any(isinstance(defect, NoBoundaryInMultipartDefect) for defect in retval.defects):
|
|
|
|
# (Hopefully) Rare instance where multipart boundary is not
|
|
|
|
# properly quoted. Solve by fixing the boundary and parsing
|
|
|
|
fd.seek(0)
|
|
|
|
_buffer = fd.read()
|
|
|
|
retval = self.parser['8bit'].parsebytes(_quote_boundary_fix(_buffer))
|
|
|
|
else:
|
|
|
|
# Unknown issue which is causing failure of as_bytes()
|
|
|
|
ui.warn("Message has defects preventing it from being processed!")
|
2021-02-09 20:58:30 +01:00
|
|
|
fd.close()
|
|
|
|
return retval
|
2002-06-20 06:59:57 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2011-08-30 12:36:55 +02:00
|
|
|
def getmessagetime(self, uid):
|
2006-08-22 03:09:36 +02:00
|
|
|
filename = self.messagelist[uid]['filename']
|
2011-06-10 17:32:40 +02:00
|
|
|
filepath = os.path.join(self.getfullname(), filename)
|
2011-08-30 12:36:55 +02:00
|
|
|
return os.path.getmtime(filepath)
|
2006-08-22 03:09:36 +02:00
|
|
|
|
2020-10-10 15:00:34 +02:00
|
|
|
def new_message_filename(self, uid, flags=None, date=None):
|
2011-08-30 10:51:33 +02:00
|
|
|
"""Creates a new unique Maildir filename
|
|
|
|
|
|
|
|
:param uid: The UID`None`, or a set of maildir flags
|
|
|
|
:param flags: A set of maildir flags
|
2020-08-30 13:45:05 +02:00
|
|
|
:param flags: (optional) Date
|
2011-08-30 10:51:33 +02:00
|
|
|
:returns: String containing unique message filename"""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2020-10-10 15:00:34 +02:00
|
|
|
if flags is None:
|
|
|
|
flags = set()
|
|
|
|
|
2015-10-28 10:56:16 +01:00
|
|
|
timeval, timeseq = _gettimeseq(date)
|
2018-04-09 19:09:56 +02:00
|
|
|
uniq_name = '%d_%d.%d.%s,U=%d,FMD5=%s%s2,%s' % \
|
2020-08-29 19:46:43 +02:00
|
|
|
(timeval, timeseq, os.getpid(), socket.gethostname(),
|
|
|
|
uid, self._foldermd5, self.infosep, ''.join(sorted(flags)))
|
2018-04-09 19:09:56 +02:00
|
|
|
return uniq_name.replace(os.path.sep, self.sep_subst)
|
2013-07-21 21:00:23 +02:00
|
|
|
|
2021-02-09 20:58:30 +01:00
|
|
|
def save_to_tmp_file(self, filename, msg, policy=None):
|
2021-02-19 23:00:15 +01:00
|
|
|
"""Saves given message to the named temporary file in the
|
2012-10-16 20:20:35 +02:00
|
|
|
'tmp' subdirectory of $CWD.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
- filename: name of the temporary file;
|
2021-02-09 20:58:30 +01:00
|
|
|
- msg: Email message object
|
2012-10-16 20:20:35 +02:00
|
|
|
|
|
|
|
Returns: relative path to the temporary file
|
2015-01-01 21:41:11 +01:00
|
|
|
that was created."""
|
2012-10-16 20:20:35 +02:00
|
|
|
|
2021-02-09 20:58:30 +01:00
|
|
|
if policy is None:
|
|
|
|
output_policy = self.policy['8bit']
|
|
|
|
else:
|
|
|
|
output_policy = policy
|
2012-10-16 20:20:35 +02:00
|
|
|
tmpname = os.path.join('tmp', filename)
|
2016-02-19 12:33:57 +01:00
|
|
|
# Open file and write it out.
|
|
|
|
# XXX: why do we need to loop 7 times?
|
2012-10-16 20:20:35 +02:00
|
|
|
tries = 7
|
|
|
|
while tries:
|
|
|
|
tries = tries - 1
|
|
|
|
try:
|
|
|
|
fd = os.open(os.path.join(self.getfullname(), tmpname),
|
2020-08-29 19:46:43 +02:00
|
|
|
os.O_EXCL | os.O_CREAT | os.O_WRONLY, 0o666)
|
2012-10-16 20:20:35 +02:00
|
|
|
break
|
|
|
|
except OSError as e:
|
2016-02-19 12:33:57 +01:00
|
|
|
if not hasattr(e, 'EEXIST'):
|
|
|
|
raise
|
2020-10-12 01:35:20 +02:00
|
|
|
if e.errno == errno.EEXIST:
|
2012-10-16 20:20:35 +02:00
|
|
|
if tries:
|
2014-05-06 23:12:50 +02:00
|
|
|
time.sleep(0.23)
|
2012-10-16 20:20:35 +02:00
|
|
|
continue
|
|
|
|
severity = OfflineImapError.ERROR.MESSAGE
|
2020-09-03 20:42:30 +02:00
|
|
|
raise OfflineImapError(
|
|
|
|
"Unique filename %s already exists." %
|
|
|
|
filename, severity,
|
|
|
|
exc_info()[2])
|
2012-10-16 20:20:35 +02:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2021-02-09 20:58:30 +01:00
|
|
|
fd = os.fdopen(fd, 'wb')
|
|
|
|
fd.write(msg.as_bytes(policy=output_policy))
|
fix: don't loose local mails because of maxage
Suppose messages A and B were delivered to the remote folder at
"maxage + 1" days ago.
A was downloaded to the local folder "maxage + 1" days ago, but B was only
downloaded "maxage - 1" days ago (contrived scenario to illustrate the two
things that could happen). The behavior was that B gets deleted from the local
folder, but A did not. The expected behavior is that neither is deleted.
Starting where Base.py: __syncmessagesto_delete(self, dstfolder, statusfolder)
is called where:
- self is the remote folder
and
- dstfolder is the local folder.
It defines deletelist to be the list of messages in the status folder
messagelist that aren't in the remote folder messagelist with
not self.uidexists(uid)
A and B are both in the status folder. They're also both *NOT* in the remote
folder messagelist: this list is formed in IMAP.py: cachemessagelist(), which
calls _msgs_to_fetch(), which only asks the IMAP server for messages that are
"< maxage" days old.
Back to Base.py __syncmessagesto_delete(), look at the call
folder.deletemessages(deletelist), where folder is the local folder. This ends
up calling Maildir.py deletemessage() for each message on the deletelist. But we
see that this methods returns (instead of deleting anything) if the message is
in the local folder's messagelist. This messagelist was created by Maildir.py's
cachemessagelist(), which calls _scanfolder(), which tries to exclude messages
based on maxage. So at this point, we *WANT* A and B to be excluded -- then they
will be spared from deletion. This maxage check calls _iswithinmaxage(), and
actually does the date comparison based on the time found at the beginning of
the message's filename. These filenames were originally created in Maildir.py's
new_message_filename(), which calls _gettimeseq() to get the current time (i.e.
the time of retrieval).
Upshot: A's filename has an older timestamp than B's filename. A is excluded
from the local folder messagelist in _scanfolder(), hence spared from deletion
in deletemessage(); B is not excluded, and is deleted.
This patch does not address the timezone issue. As for the IMAP/timezone issue,
a similar issue is discussed in the thunderbird bug tracker here:
https://bugzilla.mozilla.org/show_bug.cgi?id=886534
In the end, they're solving a different problem, but they agree that
there is really no reliable way of guessing the IMAP server's internal
timezone.
Signed-off-by: Janna Martl <janna.martl109@gmail.com>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
2015-03-07 10:50:33 +01:00
|
|
|
# Make sure the data hits the disk.
|
2012-10-16 20:20:35 +02:00
|
|
|
fd.flush()
|
2016-10-25 21:34:38 +02:00
|
|
|
if self.dofsync():
|
2012-10-16 20:20:35 +02:00
|
|
|
os.fsync(fd)
|
|
|
|
fd.close()
|
|
|
|
|
|
|
|
return tmpname
|
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2021-02-09 20:58:30 +01:00
|
|
|
def savemessage(self, uid, msg, flags, rtime):
|
2011-09-16 11:44:50 +02:00
|
|
|
"""Writes a new message, with the specified uid.
|
|
|
|
|
|
|
|
See folder/Base for detail. Note that savemessage() does not
|
|
|
|
check against dryrun settings, so you need to ensure that
|
|
|
|
savemessage is never called in a dryrun mode."""
|
2016-11-07 23:03:40 +01:00
|
|
|
|
2007-06-13 05:42:15 +02:00
|
|
|
# This function only ever saves to tmp/,
|
|
|
|
# but it calls savemessageflags() to actually save to cur/ or new/.
|
2011-09-16 11:44:50 +02:00
|
|
|
self.ui.savemessage('maildir', uid, flags, self)
|
2002-06-21 03:03:30 +02:00
|
|
|
if uid < 0:
|
|
|
|
# We cannot assign a new uid.
|
|
|
|
return uid
|
2011-09-16 11:44:50 +02:00
|
|
|
|
2002-07-24 01:36:44 +02:00
|
|
|
if uid in self.messagelist:
|
2011-06-10 17:32:40 +02:00
|
|
|
# We already have it, just update flags.
|
2002-06-21 03:12:52 +02:00
|
|
|
self.savemessageflags(uid, flags)
|
2002-06-21 03:03:30 +02:00
|
|
|
return uid
|
2007-06-13 05:42:15 +02:00
|
|
|
|
2016-08-13 16:25:19 +02:00
|
|
|
# Use the mail timestamp given by either Date or Delivery-date mail
|
2015-10-28 10:56:16 +01:00
|
|
|
# headers.
|
|
|
|
message_timestamp = None
|
2016-11-07 23:04:08 +01:00
|
|
|
if self._filename_use_mail_timestamp is not False:
|
2015-10-28 10:56:16 +01:00
|
|
|
try:
|
2021-02-09 20:58:30 +01:00
|
|
|
message_timestamp = self.get_message_date(msg, 'Date')
|
2015-10-28 10:56:16 +01:00
|
|
|
if message_timestamp is None:
|
|
|
|
# Give a try with Delivery-date
|
2021-02-09 20:58:30 +01:00
|
|
|
message_timestamp = self.get_message_date(
|
|
|
|
msg, 'Delivery-date')
|
2016-08-13 16:25:19 +02:00
|
|
|
except Exception as e:
|
|
|
|
# This should never happen.
|
2015-10-28 10:56:16 +01:00
|
|
|
from offlineimap.ui import getglobalui
|
2021-02-09 20:58:30 +01:00
|
|
|
datestr = self.get_message_date(msg)
|
2015-10-28 10:56:16 +01:00
|
|
|
ui = getglobalui()
|
|
|
|
ui.warn("UID %d has invalid date %s: %s\n"
|
2020-08-29 19:46:43 +02:00
|
|
|
"Not using message timestamp as file prefix" %
|
|
|
|
(uid, datestr, e))
|
2016-11-07 23:04:08 +01:00
|
|
|
# No need to check if message_timestamp is None here since it
|
|
|
|
# would be overridden by _gettimeseq.
|
2015-10-28 10:56:16 +01:00
|
|
|
messagename = self.new_message_filename(uid, flags, date=message_timestamp)
|
2021-02-09 20:58:30 +01:00
|
|
|
tmpname = self.save_to_tmp_file(messagename, msg)
|
2015-04-02 14:41:47 +02:00
|
|
|
|
2016-05-11 04:10:13 +02:00
|
|
|
if self._utime_from_header is True:
|
2015-08-29 22:02:33 +02:00
|
|
|
try:
|
2021-02-09 20:58:30 +01:00
|
|
|
date = self.get_message_date(msg, 'Date')
|
2015-08-29 22:02:33 +02:00
|
|
|
if date is not None:
|
|
|
|
os.utime(os.path.join(self.getfullname(), tmpname),
|
2020-08-29 19:46:43 +02:00
|
|
|
(date, date))
|
2016-08-13 16:25:19 +02:00
|
|
|
# In case date is wrongly so far into the future as to be > max
|
|
|
|
# int32.
|
2015-08-29 22:02:33 +02:00
|
|
|
except Exception as e:
|
|
|
|
from offlineimap.ui import getglobalui
|
2021-02-09 20:58:30 +01:00
|
|
|
datestr = self.get_message_date(msg)
|
2015-08-29 22:02:33 +02:00
|
|
|
ui = getglobalui()
|
|
|
|
ui.warn("UID %d has invalid date %s: %s\n"
|
2020-08-29 19:46:43 +02:00
|
|
|
"Not changing file modification time" % (uid, datestr, e))
|
2007-03-28 22:23:18 +02:00
|
|
|
|
2014-08-03 14:47:26 +02:00
|
|
|
self.messagelist[uid] = self.msglist_item_initializer(uid)
|
|
|
|
self.messagelist[uid]['flags'] = flags
|
|
|
|
self.messagelist[uid]['filename'] = tmpname
|
2016-08-13 16:25:19 +02:00
|
|
|
# savemessageflags moves msg to 'cur' or 'new' as appropriate.
|
2002-06-21 03:12:52 +02:00
|
|
|
self.savemessageflags(uid, flags)
|
2011-01-05 17:00:57 +01:00
|
|
|
self.ui.debug('maildir', 'savemessage: returning uid %d' % uid)
|
2002-06-21 03:03:30 +02:00
|
|
|
return uid
|
2012-01-06 13:05:08 +01:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2002-06-20 07:14:35 +02:00
|
|
|
def getmessageflags(self, uid):
|
2002-07-24 01:36:44 +02:00
|
|
|
return self.messagelist[uid]['flags']
|
2002-06-20 07:14:35 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2002-06-20 07:14:35 +02:00
|
|
|
def savemessageflags(self, uid, flags):
|
2011-08-30 10:52:11 +02:00
|
|
|
"""Sets the specified message's flags to the given set.
|
2011-07-12 09:34:02 +02:00
|
|
|
|
2011-08-30 10:52:11 +02:00
|
|
|
This function moves the message to the cur or new subdir,
|
2011-09-16 11:44:50 +02:00
|
|
|
depending on the 'S'een flag.
|
2011-08-30 10:43:10 +02:00
|
|
|
|
2011-09-16 11:44:50 +02:00
|
|
|
Note that this function does not check against dryrun settings,
|
|
|
|
so you need to ensure that it is never called in a
|
|
|
|
dryrun mode."""
|
2014-08-03 14:47:26 +02:00
|
|
|
|
|
|
|
assert uid in self.messagelist
|
|
|
|
|
2011-08-30 10:52:11 +02:00
|
|
|
oldfilename = self.messagelist[uid]['filename']
|
|
|
|
dir_prefix, filename = os.path.split(oldfilename)
|
|
|
|
# If a message has been seen, it goes into 'cur'
|
|
|
|
dir_prefix = 'cur' if 'S' in flags else 'new'
|
2012-01-06 13:05:08 +01:00
|
|
|
|
2011-08-30 10:43:10 +02:00
|
|
|
if flags != self.messagelist[uid]['flags']:
|
2012-01-07 21:34:57 +01:00
|
|
|
# Flags have actually changed, construct new filename Strip
|
2015-11-20 20:09:14 +01:00
|
|
|
# off existing infostring
|
2012-01-07 13:10:41 +01:00
|
|
|
infomatch = self.re_flagmatch.search(filename)
|
2011-08-30 10:43:10 +02:00
|
|
|
if infomatch:
|
2020-08-29 19:46:43 +02:00
|
|
|
filename = filename[:-len(infomatch.group())] # strip off
|
|
|
|
infostr = '%s2,%s' % (self.infosep, ''.join(sorted(flags)))
|
2011-08-30 10:43:10 +02:00
|
|
|
filename += infostr
|
|
|
|
|
|
|
|
newfilename = os.path.join(dir_prefix, filename)
|
2020-08-30 13:40:14 +02:00
|
|
|
if newfilename != oldfilename:
|
2011-08-17 10:01:39 +02:00
|
|
|
try:
|
|
|
|
os.rename(os.path.join(self.getfullname(), oldfilename),
|
|
|
|
os.path.join(self.getfullname(), newfilename))
|
2012-02-05 10:14:23 +01:00
|
|
|
except OSError as e:
|
2020-09-03 20:42:30 +02:00
|
|
|
raise OfflineImapError(
|
|
|
|
"Can't rename file '%s' to '%s': %s" %
|
|
|
|
(oldfilename, newfilename, e[1]),
|
|
|
|
OfflineImapError.ERROR.FOLDER,
|
|
|
|
exc_info()[2])
|
2012-01-06 13:05:08 +01:00
|
|
|
|
2002-07-24 01:36:44 +02:00
|
|
|
self.messagelist[uid]['flags'] = flags
|
|
|
|
self.messagelist[uid]['filename'] = newfilename
|
2002-06-20 07:14:35 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2011-08-30 10:52:11 +02:00
|
|
|
def change_message_uid(self, uid, new_uid):
|
|
|
|
"""Change the message from existing uid to new_uid
|
2007-06-13 05:42:15 +02:00
|
|
|
|
2011-08-30 10:52:11 +02:00
|
|
|
This will not update the statusfolder UID, you need to do that yourself.
|
2020-08-30 13:45:05 +02:00
|
|
|
:param uid: Message UID
|
2011-08-30 10:52:11 +02:00
|
|
|
:param new_uid: (optional) If given, the old UID will be changed
|
2015-01-01 21:41:11 +01:00
|
|
|
to a new UID. The Maildir backend can implement this as
|
|
|
|
an efficient rename.
|
|
|
|
"""
|
|
|
|
|
2020-08-30 13:42:09 +02:00
|
|
|
if uid not in self.messagelist:
|
2020-08-29 19:46:43 +02:00
|
|
|
raise OfflineImapError("Cannot change unknown Maildir UID %s" % uid,
|
2016-07-16 18:00:59 +02:00
|
|
|
OfflineImapError.ERROR.MESSAGE)
|
2020-08-30 13:49:36 +02:00
|
|
|
if uid == new_uid:
|
|
|
|
return
|
2011-08-30 10:52:11 +02:00
|
|
|
|
|
|
|
oldfilename = self.messagelist[uid]['filename']
|
|
|
|
dir_prefix, filename = os.path.split(oldfilename)
|
|
|
|
flags = self.getmessageflags(uid)
|
2016-11-07 23:03:40 +01:00
|
|
|
# TODO: we aren't keeping the prefix timestamp so we don't honor the
|
|
|
|
# filename_use_mail_timestamp configuration option.
|
2012-10-23 20:05:59 +02:00
|
|
|
newfilename = os.path.join(dir_prefix,
|
2020-08-29 19:46:43 +02:00
|
|
|
self.new_message_filename(new_uid, flags))
|
2011-08-30 10:52:11 +02:00
|
|
|
os.rename(os.path.join(self.getfullname(), oldfilename),
|
2012-10-23 20:05:59 +02:00
|
|
|
os.path.join(self.getfullname(), newfilename))
|
2011-08-30 10:52:11 +02:00
|
|
|
self.messagelist[new_uid] = self.messagelist[uid]
|
2012-10-23 20:05:59 +02:00
|
|
|
self.messagelist[new_uid]['filename'] = newfilename
|
2011-08-30 10:52:11 +02:00
|
|
|
del self.messagelist[uid]
|
2013-07-21 21:00:23 +02:00
|
|
|
|
2014-03-16 13:27:35 +01:00
|
|
|
# Interface from BaseFolder
|
2002-06-20 07:14:35 +02:00
|
|
|
def deletemessage(self, uid):
|
2011-05-07 11:11:20 +02:00
|
|
|
"""Unlinks a message file from the Maildir.
|
|
|
|
|
|
|
|
:param uid: UID of a mail message
|
|
|
|
:type uid: String
|
|
|
|
:return: Nothing, or an Exception if UID but no corresponding file
|
|
|
|
found.
|
|
|
|
"""
|
2002-07-24 01:36:44 +02:00
|
|
|
filename = self.messagelist[uid]['filename']
|
2011-06-10 17:32:40 +02:00
|
|
|
filepath = os.path.join(self.getfullname(), filename)
|
2002-07-16 04:26:58 +02:00
|
|
|
try:
|
2011-06-10 17:32:40 +02:00
|
|
|
os.unlink(filepath)
|
2002-08-17 03:01:12 +02:00
|
|
|
except OSError:
|
2002-07-16 04:26:58 +02:00
|
|
|
# Can't find the file -- maybe already deleted?
|
|
|
|
newmsglist = self._scanfolder()
|
2020-08-29 19:46:43 +02:00
|
|
|
if uid in newmsglist: # Nope, try new filename.
|
2011-06-10 17:32:40 +02:00
|
|
|
filename = newmsglist[uid]['filename']
|
|
|
|
filepath = os.path.join(self.getfullname(), filename)
|
|
|
|
os.unlink(filepath)
|
2002-07-16 04:26:58 +02:00
|
|
|
# Yep -- return.
|
2020-08-29 19:46:43 +02:00
|
|
|
del (self.messagelist[uid])
|
2016-03-05 18:07:07 +01:00
|
|
|
|
|
|
|
def migratefmd5(self, dryrun=False):
|
|
|
|
"""Migrate FMD5 hashes from versions prior to 6.3.5
|
|
|
|
|
|
|
|
:param dryrun: Run in dry run mode
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
oldfmd5 = md5(self.name).hexdigest()
|
|
|
|
msglist = self._scanfolder()
|
2020-08-28 03:32:43 +02:00
|
|
|
for mkey, mvalue in list(msglist.items()):
|
2016-03-05 18:07:07 +01:00
|
|
|
filename = os.path.join(self.getfullname(), mvalue['filename'])
|
|
|
|
match = re.search("FMD5=([a-fA-F0-9]+)", filename)
|
|
|
|
if match is None:
|
|
|
|
self.ui.debug("maildir",
|
|
|
|
"File `%s' doesn't have an FMD5 assigned"
|
|
|
|
% filename)
|
|
|
|
elif match.group(1) == oldfmd5:
|
|
|
|
self.ui.info("Migrating file `%s' to FMD5 `%s'"
|
|
|
|
% (filename, self._foldermd5))
|
|
|
|
if not dryrun:
|
|
|
|
newfilename = filename.replace(
|
|
|
|
"FMD5=" + match.group(1), "FMD5=" + self._foldermd5)
|
|
|
|
try:
|
|
|
|
os.rename(filename, newfilename)
|
|
|
|
except OSError as e:
|
2020-09-03 20:42:30 +02:00
|
|
|
raise OfflineImapError(
|
|
|
|
"Can't rename file '%s' to '%s': %s" %
|
|
|
|
(filename, newfilename, e[1]),
|
|
|
|
OfflineImapError.ERROR.FOLDER,
|
|
|
|
exc_info()[2])
|
|
|
|
|
2016-03-05 18:07:07 +01:00
|
|
|
elif match.group(1) != self._foldermd5:
|
|
|
|
self.ui.warn(("Inconsistent FMD5 for file `%s':"
|
|
|
|
" Neither `%s' nor `%s' found")
|
|
|
|
% (filename, oldfmd5, self._foldermd5))
|