more style consistency
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
This commit is contained in:
@ -15,14 +15,15 @@
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
import os.path
|
||||
import re
|
||||
from sys import exc_info
|
||||
|
||||
from offlineimap import threadutil, emailutil
|
||||
from offlineimap import globals
|
||||
from offlineimap.ui import getglobalui
|
||||
from offlineimap.error import OfflineImapError
|
||||
import offlineimap.accounts
|
||||
import os.path
|
||||
import re
|
||||
from sys import exc_info
|
||||
|
||||
|
||||
class BaseFolder(object):
|
||||
@ -31,6 +32,7 @@ class BaseFolder(object):
|
||||
:para name: Path & name of folder minus root or reference
|
||||
:para repository: Repository() in which the folder is.
|
||||
"""
|
||||
|
||||
self.ui = getglobalui()
|
||||
# Save original name for folderfilter operations
|
||||
self.ffilter_name = name
|
||||
@ -56,15 +58,15 @@ class BaseFolder(object):
|
||||
|
||||
# Determine if we're running static or dynamic folder filtering
|
||||
# and check filtering status
|
||||
self._dynamic_folderfilter = \
|
||||
self.config.getdefaultboolean(repo, "dynamic_folderfilter", False)
|
||||
self._dynamic_folderfilter = self.config.getdefaultboolean(
|
||||
repo, "dynamic_folderfilter", False)
|
||||
self._sync_this = repository.should_sync_folder(self.ffilter_name)
|
||||
if self._dynamic_folderfilter:
|
||||
self.ui.debug('', "Running dynamic folder filtering on '%s'[%s]" \
|
||||
% (self.ffilter_name, repository))
|
||||
self.ui.debug('', "Running dynamic folder filtering on '%s'[%s]"%
|
||||
(self.ffilter_name, repository))
|
||||
elif not self._sync_this:
|
||||
self.ui.debug('', "Filtering out '%s'[%s] due to folderfilter" \
|
||||
% (self.ffilter_name, repository))
|
||||
self.ui.debug('', "Filtering out '%s'[%s] due to folderfilter"%
|
||||
(self.ffilter_name, repository))
|
||||
|
||||
# Passes for syncmessagesto
|
||||
self.syncmessagesto_passes = [('copying messages' , self.__syncmessagesto_copy),
|
||||
@ -115,17 +117,20 @@ class BaseFolder(object):
|
||||
|
||||
:param statusfolder: keeps track of the last known folder state.
|
||||
"""
|
||||
|
||||
return True
|
||||
|
||||
def getcopyinstancelimit(self):
|
||||
"""For threading folders, returns the instancelimitname for
|
||||
InstanceLimitedThreads."""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
def storesmessages(self):
|
||||
"""Should be true for any backend that actually saves message bodies.
|
||||
(Almost all of them). False for the LocalStatus backend. Saves
|
||||
us from having to slurp up messages just for localstatus purposes."""
|
||||
|
||||
return 1
|
||||
|
||||
def getvisiblename(self):
|
||||
@ -143,14 +148,17 @@ class BaseFolder(object):
|
||||
|
||||
def getrepository(self):
|
||||
"""Returns the repository object that this folder is within."""
|
||||
|
||||
return self.repository
|
||||
|
||||
def getroot(self):
|
||||
"""Returns the root of the folder, in a folder-specific fashion."""
|
||||
|
||||
return self.root
|
||||
|
||||
def getsep(self):
|
||||
"""Returns the separator for this folder type."""
|
||||
|
||||
return self.sep
|
||||
|
||||
def getfullname(self):
|
||||
@ -160,7 +168,8 @@ class BaseFolder(object):
|
||||
return self.getname()
|
||||
|
||||
def getfolderbasename(self):
|
||||
"""Return base file name of file to store Status/UID info in"""
|
||||
"""Return base file name of file to store Status/UID info in."""
|
||||
|
||||
if not self.name:
|
||||
basename = '.'
|
||||
else: #avoid directory hierarchies and file names such as '/'
|
||||
@ -188,6 +197,7 @@ class BaseFolder(object):
|
||||
|
||||
def _getuidfilename(self):
|
||||
"""provides UIDVALIDITY cache filename for class internal purposes"""
|
||||
|
||||
return os.path.join(self.repository.getuiddir(),
|
||||
self.getfolderbasename())
|
||||
|
||||
@ -196,6 +206,7 @@ class BaseFolder(object):
|
||||
|
||||
:returns: UIDVALIDITY as (long) number or None, if None had been
|
||||
saved yet."""
|
||||
|
||||
if hasattr(self, '_base_saved_uidvalidity'):
|
||||
return self._base_saved_uidvalidity
|
||||
uidfilename = self._getuidfilename()
|
||||
@ -212,6 +223,7 @@ class BaseFolder(object):
|
||||
|
||||
This function is not threadsafe, so don't attempt to call it
|
||||
from concurrent threads."""
|
||||
|
||||
newval = self.get_uidvalidity()
|
||||
uidfilename = self._getuidfilename()
|
||||
|
||||
@ -225,45 +237,50 @@ class BaseFolder(object):
|
||||
|
||||
This function needs to be implemented by each Backend
|
||||
:returns: UIDVALIDITY as a (long) number"""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
def cachemessagelist(self):
|
||||
"""Reads the message list from disk or network and stores it in
|
||||
memory for later use. This list will not be re-read from disk or
|
||||
memory unless this function is called again."""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
def getmessagelist(self):
|
||||
"""Gets the current message list.
|
||||
You must call cachemessagelist() before calling this function!"""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
def msglist_item_initializer(self, uid):
|
||||
"""
|
||||
Returns value for empty messagelist element with given UID.
|
||||
"""Returns value for empty messagelist element with given UID.
|
||||
|
||||
This function must initialize all fields of messagelist item
|
||||
and must be called every time when one creates new messagelist
|
||||
entry to ensure that all fields that must be present are present.
|
||||
entry to ensure that all fields that must be present are present."""
|
||||
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def uidexists(self, uid):
|
||||
"""Returns True if uid exists"""
|
||||
|
||||
return uid in self.getmessagelist()
|
||||
|
||||
def getmessageuidlist(self):
|
||||
"""Gets a list of UIDs.
|
||||
You may have to call cachemessagelist() before calling this function!"""
|
||||
|
||||
return self.getmessagelist().keys()
|
||||
|
||||
def getmessagecount(self):
|
||||
"""Gets the number of messages."""
|
||||
|
||||
return len(self.getmessagelist())
|
||||
|
||||
def getmessage(self, uid):
|
||||
"""Returns the content of the specified message."""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
def savemessage(self, uid, content, flags, rtime):
|
||||
@ -286,20 +303,23 @@ class BaseFolder(object):
|
||||
|
||||
Note that savemessage() does not check against dryrun settings,
|
||||
so you need to ensure that savemessage is never called in a
|
||||
dryrun mode.
|
||||
"""
|
||||
dryrun mode."""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
def getmessagetime(self, uid):
|
||||
"""Return the received time for the specified message."""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
def getmessagemtime(self, uid):
|
||||
"""Returns the message modification time of the specified message."""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
def getmessageflags(self, uid):
|
||||
"""Returns the flags for the specified message."""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
def savemessageflags(self, uid, flags):
|
||||
@ -308,6 +328,7 @@ class BaseFolder(object):
|
||||
Note that this function does not check against dryrun settings,
|
||||
so you need to ensure that it is never called in a
|
||||
dryrun mode."""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
def addmessageflags(self, uid, flags):
|
||||
@ -319,14 +340,15 @@ class BaseFolder(object):
|
||||
dryrun mode.
|
||||
|
||||
:param flags: A set() of flags"""
|
||||
|
||||
newflags = self.getmessageflags(uid) | flags
|
||||
self.savemessageflags(uid, newflags)
|
||||
|
||||
def addmessagesflags(self, uidlist, flags):
|
||||
"""
|
||||
Note that this function does not check against dryrun settings,
|
||||
"""Note that this function does not check against dryrun settings,
|
||||
so you need to ensure that it is never called in a
|
||||
dryrun mode."""
|
||||
|
||||
for uid in uidlist:
|
||||
self.addmessageflags(uid, flags)
|
||||
|
||||
@ -337,6 +359,7 @@ class BaseFolder(object):
|
||||
Note that this function does not check against dryrun settings,
|
||||
so you need to ensure that it is never called in a
|
||||
dryrun mode."""
|
||||
|
||||
newflags = self.getmessageflags(uid) - flags
|
||||
self.savemessageflags(uid, newflags)
|
||||
|
||||
@ -345,10 +368,10 @@ class BaseFolder(object):
|
||||
Note that this function does not check against dryrun settings,
|
||||
so you need to ensure that it is never called in a
|
||||
dryrun mode."""
|
||||
|
||||
for uid in uidlist:
|
||||
self.deletemessageflags(uid, flags)
|
||||
|
||||
|
||||
def getmessagelabels(self, uid):
|
||||
"""Returns the labels for the specified message."""
|
||||
raise NotImplementedError
|
||||
@ -359,6 +382,7 @@ class BaseFolder(object):
|
||||
Note that this function does not check against dryrun settings,
|
||||
so you need to ensure that it is never called in a
|
||||
dryrun mode."""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
def addmessagelabels(self, uid, labels):
|
||||
@ -370,14 +394,15 @@ class BaseFolder(object):
|
||||
dryrun mode.
|
||||
|
||||
:param labels: A set() of labels"""
|
||||
|
||||
newlabels = self.getmessagelabels(uid) | labels
|
||||
self.savemessagelabels(uid, newlabels)
|
||||
|
||||
def addmessageslabels(self, uidlist, labels):
|
||||
"""
|
||||
Note that this function does not check against dryrun settings,
|
||||
"""Note that this function does not check against dryrun settings,
|
||||
so you need to ensure that it is never called in a
|
||||
dryrun mode."""
|
||||
|
||||
for uid in uidlist:
|
||||
self.addmessagelabels(uid, labels)
|
||||
|
||||
@ -388,6 +413,7 @@ class BaseFolder(object):
|
||||
Note that this function does not check against dryrun settings,
|
||||
so you need to ensure that it is never called in a
|
||||
dryrun mode."""
|
||||
|
||||
newlabels = self.getmessagelabels(uid) - labels
|
||||
self.savemessagelabels(uid, newlabels)
|
||||
|
||||
@ -396,12 +422,12 @@ class BaseFolder(object):
|
||||
Note that this function does not check against dryrun settings,
|
||||
so you need to ensure that it is never called in a
|
||||
dryrun mode."""
|
||||
|
||||
for uid in uidlist:
|
||||
self.deletemessagelabels(uid, labels)
|
||||
|
||||
def addmessageheader(self, content, linebreak, headername, headervalue):
|
||||
"""
|
||||
Adds new header to the provided message.
|
||||
"""Adds new header to the provided message.
|
||||
|
||||
WARNING: This function is a bit tricky, and modifying it in the wrong way,
|
||||
may easily lead to data-loss.
|
||||
@ -454,9 +480,9 @@ class BaseFolder(object):
|
||||
This is the body\n
|
||||
next line\n
|
||||
"""
|
||||
self.ui.debug('',
|
||||
'addmessageheader: called to add %s: %s' % (headername,
|
||||
headervalue))
|
||||
|
||||
self.ui.debug('', 'addmessageheader: called to add %s: %s'%
|
||||
(headername, headervalue))
|
||||
|
||||
insertionpoint = content.find(linebreak * 2)
|
||||
if insertionpoint == -1:
|
||||
@ -490,24 +516,23 @@ class BaseFolder(object):
|
||||
if content[0:len(linebreak)] != linebreak:
|
||||
suffix = suffix + linebreak
|
||||
|
||||
self.ui.debug('', 'addmessageheader: insertionpoint = %d' % insertionpoint)
|
||||
self.ui.debug('', 'addmessageheader: insertionpoint = %d'% insertionpoint)
|
||||
headers = content[0:insertionpoint]
|
||||
self.ui.debug('', 'addmessageheader: headers = %s' % repr(headers))
|
||||
self.ui.debug('', 'addmessageheader: headers = %s'% repr(headers))
|
||||
new_header = prefix + ("%s: %s" % (headername, headervalue)) + suffix
|
||||
self.ui.debug('', 'addmessageheader: new_header = ' + repr(new_header))
|
||||
return headers + new_header + content[insertionpoint:]
|
||||
|
||||
|
||||
def __find_eoh(self, content):
|
||||
"""
|
||||
Searches for the point where mail headers end.
|
||||
""" Searches for the point where mail headers end.
|
||||
Either double '\n', or end of string.
|
||||
|
||||
Arguments:
|
||||
- content: contents of the message to search in
|
||||
Returns: position of the first non-header byte.
|
||||
|
||||
"""
|
||||
|
||||
eoh_cr = content.find('\n\n')
|
||||
if eoh_cr == -1:
|
||||
eoh_cr = len(content)
|
||||
@ -516,8 +541,7 @@ class BaseFolder(object):
|
||||
|
||||
|
||||
def getmessageheader(self, content, name):
|
||||
"""
|
||||
Searches for the first occurence of the given header and returns
|
||||
"""Searches for the first occurence of the given header and returns
|
||||
its value. Header name is case-insensitive.
|
||||
|
||||
Arguments:
|
||||
@ -525,13 +549,13 @@ class BaseFolder(object):
|
||||
- name: name of the header to be searched
|
||||
|
||||
Returns: header value or None if no such header was found
|
||||
|
||||
"""
|
||||
self.ui.debug('', 'getmessageheader: called to get %s' % name)
|
||||
|
||||
self.ui.debug('', 'getmessageheader: called to get %s'% name)
|
||||
eoh = self.__find_eoh(content)
|
||||
self.ui.debug('', 'getmessageheader: eoh = %d' % eoh)
|
||||
self.ui.debug('', 'getmessageheader: eoh = %d'% eoh)
|
||||
headers = content[0:eoh]
|
||||
self.ui.debug('', 'getmessageheader: headers = %s' % repr(headers))
|
||||
self.ui.debug('', 'getmessageheader: headers = %s'% repr(headers))
|
||||
|
||||
m = re.search('^%s:(.*)$' % name, headers, flags = re.MULTILINE | re.IGNORECASE)
|
||||
if m:
|
||||
@ -541,8 +565,7 @@ class BaseFolder(object):
|
||||
|
||||
|
||||
def getmessageheaderlist(self, content, name):
|
||||
"""
|
||||
Searches for the given header and returns a list of values for
|
||||
"""Searches for the given header and returns a list of values for
|
||||
that header.
|
||||
|
||||
Arguments:
|
||||
@ -550,8 +573,8 @@ class BaseFolder(object):
|
||||
- name: name of the header to be searched
|
||||
|
||||
Returns: list of header values or emptylist if no such header was found
|
||||
|
||||
"""
|
||||
|
||||
self.ui.debug('', 'getmessageheaderlist: called to get %s' % name)
|
||||
eoh = self.__find_eoh(content)
|
||||
self.ui.debug('', 'getmessageheaderlist: eoh = %d' % eoh)
|
||||
@ -562,27 +585,26 @@ class BaseFolder(object):
|
||||
|
||||
|
||||
def deletemessageheaders(self, content, header_list):
|
||||
"""
|
||||
Deletes headers in the given list from the message content.
|
||||
"""Deletes headers in the given list from the message content.
|
||||
|
||||
Arguments:
|
||||
- content: message itself
|
||||
- header_list: list of headers to be deleted or just the header name
|
||||
|
||||
We expect our message to have '\n' as line endings.
|
||||
|
||||
"""
|
||||
|
||||
if type(header_list) != type([]):
|
||||
header_list = [header_list]
|
||||
self.ui.debug('', 'deletemessageheaders: called to delete %s' % (header_list))
|
||||
self.ui.debug('', 'deletemessageheaders: called to delete %s'% (header_list))
|
||||
|
||||
if not len(header_list): return content
|
||||
|
||||
eoh = self.__find_eoh(content)
|
||||
self.ui.debug('', 'deletemessageheaders: end of headers = %d' % eoh)
|
||||
self.ui.debug('', 'deletemessageheaders: end of headers = %d'% eoh)
|
||||
headers = content[0:eoh]
|
||||
rest = content[eoh:]
|
||||
self.ui.debug('', 'deletemessageheaders: headers = %s' % repr(headers))
|
||||
self.ui.debug('', 'deletemessageheaders: headers = %s'% repr(headers))
|
||||
new_headers = []
|
||||
for h in headers.split('\n'):
|
||||
keep_it = True
|
||||
@ -609,16 +631,14 @@ class BaseFolder(object):
|
||||
raise NotImplementedError
|
||||
|
||||
def deletemessage(self, uid):
|
||||
"""
|
||||
Note that this function does not check against dryrun settings,
|
||||
"""Note that this function does not check against dryrun settings,
|
||||
so you need to ensure that it is never called in a
|
||||
dryrun mode."""
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
def deletemessages(self, uidlist):
|
||||
"""
|
||||
Note that this function does not check against dryrun settings,
|
||||
"""Note that this function does not check against dryrun settings,
|
||||
so you need to ensure that it is never called in a
|
||||
dryrun mode."""
|
||||
|
||||
@ -686,9 +706,8 @@ class BaseFolder(object):
|
||||
self.deletemessage(uid)
|
||||
else:
|
||||
raise OfflineImapError("Trying to save msg (uid %d) on folder "
|
||||
"%s returned invalid uid %d" % (uid,
|
||||
dstfolder.getvisiblename(), new_uid),
|
||||
OfflineImapError.ERROR.MESSAGE)
|
||||
"%s returned invalid uid %d"% (uid, dstfolder.getvisiblename(),
|
||||
new_uid), OfflineImapError.ERROR.MESSAGE)
|
||||
except (KeyboardInterrupt): # bubble up CTRL-C
|
||||
raise
|
||||
except OfflineImapError as e:
|
||||
@ -697,8 +716,7 @@ class BaseFolder(object):
|
||||
self.ui.error(e, exc_info()[2])
|
||||
except Exception as e:
|
||||
self.ui.error(e, exc_info()[2],
|
||||
msg="Copying message %s [acc: %s]" %\
|
||||
(uid, self.accountname))
|
||||
msg = "Copying message %s [acc: %s]"% (uid, self.accountname))
|
||||
raise #raise on unknown errors, so we can fix those
|
||||
|
||||
def __syncmessagesto_copy(self, dstfolder, statusfolder):
|
||||
@ -714,6 +732,7 @@ class BaseFolder(object):
|
||||
|
||||
This function checks and protects us from action in ryrun mode.
|
||||
"""
|
||||
|
||||
threads = []
|
||||
|
||||
copylist = filter(lambda uid: not \
|
||||
@ -754,6 +773,7 @@ class BaseFolder(object):
|
||||
|
||||
This function checks and protects us from action in ryrun mode.
|
||||
"""
|
||||
|
||||
deletelist = filter(lambda uid: uid>=0 \
|
||||
and not self.uidexists(uid),
|
||||
statusfolder.getmessageuidlist())
|
||||
@ -776,6 +796,7 @@ class BaseFolder(object):
|
||||
|
||||
This function checks and protects us from action in ryrun mode.
|
||||
"""
|
||||
|
||||
# For each flag, we store a list of uids to which it should be
|
||||
# added. Then, we can call addmessagesflags() to apply them in
|
||||
# bulk, rather than one call per message.
|
||||
@ -854,8 +875,8 @@ class BaseFolder(object):
|
||||
|
||||
:param dstfolder: Folderinstance to sync the msgs to.
|
||||
:param statusfolder: LocalStatus instance to sync against.
|
||||
|
||||
"""
|
||||
|
||||
for (passdesc, action) in self.syncmessagesto_passes:
|
||||
# bail out on CTRL-C or SIGTERM
|
||||
if offlineimap.accounts.Account.abort_NOW_signal.is_set():
|
||||
@ -883,6 +904,7 @@ class BaseFolder(object):
|
||||
MailDirFolder('foo') == IMAPFolder('foo') --> False
|
||||
MailDirFolder('foo') == MaildirFolder('foo') --> False
|
||||
"""
|
||||
|
||||
if isinstance(other, basestring):
|
||||
return other == self.name
|
||||
return id(self) == id(other)
|
||||
|
@ -20,6 +20,7 @@ import binascii
|
||||
import re
|
||||
import time
|
||||
from sys import exc_info
|
||||
|
||||
from .Base import BaseFolder
|
||||
from offlineimap import imaputil, imaplibutil, emailutil, OfflineImapError
|
||||
from offlineimap import globals
|
||||
@ -54,7 +55,7 @@ class IMAPFolder(BaseFolder):
|
||||
self.filterheaders = [h for h in re.split(r'\s*,\s*', fh_conf) if h]
|
||||
|
||||
|
||||
def __selectro(self, imapobj, force = False):
|
||||
def __selectro(self, imapobj, force=False):
|
||||
"""Select this folder when we do not need write access.
|
||||
|
||||
Prefer SELECT to EXAMINE if we can, since some servers
|
||||
@ -86,6 +87,7 @@ class IMAPFolder(BaseFolder):
|
||||
|
||||
UIDVALIDITY value will be cached on the first call.
|
||||
:returns: The UIDVALIDITY as (long) number."""
|
||||
|
||||
if hasattr(self, '_uidvalidity'):
|
||||
# use cached value if existing
|
||||
return self._uidvalidity
|
||||
@ -141,8 +143,7 @@ class IMAPFolder(BaseFolder):
|
||||
|
||||
|
||||
def _msgs_to_fetch(self, imapobj):
|
||||
"""
|
||||
Determines sequence numbers of messages to be fetched.
|
||||
"""Determines sequence numbers of messages to be fetched.
|
||||
|
||||
Message sequence numbers (MSNs) are more easily compacted
|
||||
into ranges which makes transactions slightly faster.
|
||||
@ -151,9 +152,8 @@ class IMAPFolder(BaseFolder):
|
||||
- imapobj: instance of IMAPlib
|
||||
|
||||
Returns: range(s) for messages or None if no messages
|
||||
are to be fetched.
|
||||
are to be fetched."""
|
||||
|
||||
"""
|
||||
res_type, imapdata = imapobj.select(self.getfullname(), True, True)
|
||||
if imapdata == [None] or imapdata[0] == '0':
|
||||
# Empty folder, no need to populate message list
|
||||
@ -162,9 +162,9 @@ class IMAPFolder(BaseFolder):
|
||||
# By default examine all messages in this folder
|
||||
msgsToFetch = '1:*'
|
||||
|
||||
maxage = self.config.getdefaultint("Account %s" % self.accountname,
|
||||
maxage = self.config.getdefaultint("Account %s"% self.accountname,
|
||||
"maxage", -1)
|
||||
maxsize = self.config.getdefaultint("Account %s" % self.accountname,
|
||||
maxsize = self.config.getdefaultint("Account %s"% self.accountname,
|
||||
"maxsize", -1)
|
||||
|
||||
# Build search condition
|
||||
@ -193,7 +193,7 @@ class IMAPFolder(BaseFolder):
|
||||
res_type, res_data = imapobj.search(None, search_cond)
|
||||
if res_type != 'OK':
|
||||
raise OfflineImapError("SEARCH in folder [%s]%s failed. "
|
||||
"Search string was '%s'. Server responded '[%s] %s'" % (
|
||||
"Search string was '%s'. Server responded '[%s] %s'"% (
|
||||
self.getrepository(), self, search_cond, res_type, res_data),
|
||||
OfflineImapError.ERROR.FOLDER)
|
||||
|
||||
@ -219,11 +219,11 @@ class IMAPFolder(BaseFolder):
|
||||
|
||||
# Get the flags and UIDs for these. single-quotes prevent
|
||||
# imaplib2 from quoting the sequence.
|
||||
res_type, response = imapobj.fetch("'%s'" % msgsToFetch,
|
||||
'(FLAGS UID)')
|
||||
res_type, response = imapobj.fetch("'%s'"%
|
||||
msgsToFetch, '(FLAGS UID)')
|
||||
if res_type != 'OK':
|
||||
raise OfflineImapError("FETCHING UIDs in folder [%s]%s failed. "
|
||||
"Server responded '[%s] %s'" % (
|
||||
"Server responded '[%s] %s'"% (
|
||||
self.getrepository(), self,
|
||||
res_type, response),
|
||||
OfflineImapError.ERROR.FOLDER)
|
||||
@ -238,7 +238,7 @@ class IMAPFolder(BaseFolder):
|
||||
messagestr = messagestr.split(' ', 1)[1]
|
||||
options = imaputil.flags2hash(messagestr)
|
||||
if not 'UID' in options:
|
||||
self.ui.warn('No UID in message with options %s' %\
|
||||
self.ui.warn('No UID in message with options %s'% \
|
||||
str(options),
|
||||
minor = 1)
|
||||
else:
|
||||
@ -255,16 +255,15 @@ class IMAPFolder(BaseFolder):
|
||||
|
||||
# Interface from BaseFolder
|
||||
def getmessage(self, uid):
|
||||
"""
|
||||
Retrieve message with UID from the IMAP server (incl body)
|
||||
"""Retrieve message with UID from the IMAP server (incl body)
|
||||
|
||||
After this function all CRLFs will be transformed to '\n'.
|
||||
|
||||
:returns: the message body or throws and OfflineImapError
|
||||
(probably severity MESSAGE) if e.g. no message with
|
||||
this UID could be found.
|
||||
|
||||
"""
|
||||
|
||||
imapobj = self.imapserver.acquireconnection()
|
||||
try:
|
||||
data = self._fetch_from_imap(imapobj, str(uid), 2)
|
||||
@ -281,7 +280,7 @@ class IMAPFolder(BaseFolder):
|
||||
else:
|
||||
dbg_output = data
|
||||
|
||||
self.ui.debug('imap', "Returned object from fetching %d: '%s'" %
|
||||
self.ui.debug('imap', "Returned object from fetching %d: '%s'"%
|
||||
(uid, dbg_output))
|
||||
|
||||
return data
|
||||
@ -307,6 +306,7 @@ class IMAPFolder(BaseFolder):
|
||||
headername == 'X-OfflineIMAP' and headervalue will be a
|
||||
random string
|
||||
"""
|
||||
|
||||
headername = 'X-OfflineIMAP'
|
||||
# We need a random component too. If we ever upload the same
|
||||
# mail twice (e.g. in different folders), we would still need to
|
||||
@ -322,20 +322,20 @@ class IMAPFolder(BaseFolder):
|
||||
|
||||
|
||||
def __savemessage_searchforheader(self, imapobj, headername, headervalue):
|
||||
self.ui.debug('imap', '__savemessage_searchforheader called for %s: %s' % \
|
||||
(headername, headervalue))
|
||||
self.ui.debug('imap', '__savemessage_searchforheader called for %s: %s'% \
|
||||
(headername, headervalue))
|
||||
# Now find the UID it got.
|
||||
headervalue = imapobj._quote(headervalue)
|
||||
try:
|
||||
matchinguids = imapobj.uid('search', 'HEADER', headername, headervalue)[1][0]
|
||||
except imapobj.error as err:
|
||||
# IMAP server doesn't implement search or had a problem.
|
||||
self.ui.debug('imap', "__savemessage_searchforheader: got IMAP error '%s' while attempting to UID SEARCH for message with header %s" % (err, headername))
|
||||
self.ui.debug('imap', "__savemessage_searchforheader: got IMAP error '%s' while attempting to UID SEARCH for message with header %s"% (err, headername))
|
||||
return 0
|
||||
self.ui.debug('imap', '__savemessage_searchforheader got initial matchinguids: ' + repr(matchinguids))
|
||||
|
||||
if matchinguids == '':
|
||||
self.ui.debug('imap', "__savemessage_searchforheader: UID SEARCH for message with header %s yielded no results" % headername)
|
||||
self.ui.debug('imap', "__savemessage_searchforheader: UID SEARCH for message with header %s yielded no results"% headername)
|
||||
return 0
|
||||
|
||||
matchinguids = matchinguids.split(' ')
|
||||
@ -343,7 +343,7 @@ class IMAPFolder(BaseFolder):
|
||||
repr(matchinguids))
|
||||
if len(matchinguids) != 1 or matchinguids[0] == None:
|
||||
raise ValueError("While attempting to find UID for message with "
|
||||
"header %s, got wrong-sized matchinguids of %s" %\
|
||||
"header %s, got wrong-sized matchinguids of %s"%\
|
||||
(headername, str(matchinguids)))
|
||||
return long(matchinguids[0])
|
||||
|
||||
@ -368,9 +368,9 @@ class IMAPFolder(BaseFolder):
|
||||
We need to locate the UID just after mail headers containing our
|
||||
X-OfflineIMAP line.
|
||||
|
||||
Returns UID when found, 0 when not found.
|
||||
"""
|
||||
self.ui.debug('imap', '__savemessage_fetchheaders called for %s: %s' % \
|
||||
Returns UID when found, 0 when not found."""
|
||||
|
||||
self.ui.debug('imap', '__savemessage_fetchheaders called for %s: %s'% \
|
||||
(headername, headervalue))
|
||||
|
||||
# run "fetch X:* rfc822.header"
|
||||
@ -381,7 +381,7 @@ class IMAPFolder(BaseFolder):
|
||||
# ascending.
|
||||
|
||||
if self.getmessagelist():
|
||||
start = 1+max(self.getmessagelist().keys())
|
||||
start = 1 + max(self.getmessagelist().keys())
|
||||
else:
|
||||
# Folder was empty - start from 1
|
||||
start = 1
|
||||
@ -390,7 +390,7 @@ class IMAPFolder(BaseFolder):
|
||||
# with the range X:*. So we use bytearray to stop imaplib from getting
|
||||
# in our way
|
||||
|
||||
result = imapobj.uid('FETCH', bytearray('%d:*' % start), 'rfc822.header')
|
||||
result = imapobj.uid('FETCH', bytearray('%d:*'% start), 'rfc822.header')
|
||||
if result[0] != 'OK':
|
||||
raise OfflineImapError('Error fetching mail headers: ' + '. '.join(result[1]),
|
||||
OfflineImapError.ERROR.MESSAGE)
|
||||
@ -401,7 +401,7 @@ class IMAPFolder(BaseFolder):
|
||||
for item in result:
|
||||
if found == 0 and type(item) == type( () ):
|
||||
# Walk just tuples
|
||||
if re.search("(?:^|\\r|\\n)%s:\s*%s(?:\\r|\\n)" % (headername, headervalue),
|
||||
if re.search("(?:^|\\r|\\n)%s:\s*%s(?:\\r|\\n)"% (headername, headervalue),
|
||||
item[1], flags=re.IGNORECASE):
|
||||
found = 1
|
||||
elif found == 1:
|
||||
@ -467,8 +467,8 @@ class IMAPFolder(BaseFolder):
|
||||
# or something. Argh. It seems that Time2Internaldate
|
||||
# will rause a ValueError if the year is 0102 but not 1902,
|
||||
# but some IMAP servers nonetheless choke on 1902.
|
||||
self.ui.debug('imap', "Message with invalid date %s. Server will use local time." \
|
||||
% datetuple)
|
||||
self.ui.debug('imap', "Message with invalid date %s. "
|
||||
"Server will use local time."% datetuple)
|
||||
return None
|
||||
|
||||
#produce a string representation of datetuple that works as
|
||||
@ -507,6 +507,7 @@ class IMAPFolder(BaseFolder):
|
||||
message is saved, but it's UID can not be found, it will
|
||||
return 0. If the message can't be written (folder is
|
||||
read-only for example) it will return -1."""
|
||||
|
||||
self.ui.savemessage('imap', uid, flags, self)
|
||||
|
||||
# already have it, just save modified flags
|
||||
@ -543,17 +544,17 @@ class IMAPFolder(BaseFolder):
|
||||
if not use_uidplus:
|
||||
# insert a random unique header that we can fetch later
|
||||
(headername, headervalue) = self.__generate_randomheader(
|
||||
content)
|
||||
self.ui.debug('imap', 'savemessage: header is: %s: %s' %\
|
||||
(headername, headervalue))
|
||||
content)
|
||||
self.ui.debug('imap', 'savemessage: header is: %s: %s'%
|
||||
(headername, headervalue))
|
||||
content = self.addmessageheader(content, CRLF, headername, headervalue)
|
||||
|
||||
if len(content)>200:
|
||||
dbg_output = "%s...%s" % (content[:150], content[-50:])
|
||||
else:
|
||||
dbg_output = content
|
||||
self.ui.debug('imap', "savemessage: date: %s, content: '%s'" %
|
||||
(date, dbg_output))
|
||||
self.ui.debug('imap', "savemessage: date: %s, content: '%s'"%
|
||||
(date, dbg_output))
|
||||
|
||||
try:
|
||||
# Select folder for append and make the box READ-WRITE
|
||||
@ -566,9 +567,8 @@ class IMAPFolder(BaseFolder):
|
||||
|
||||
#Do the APPEND
|
||||
try:
|
||||
(typ, dat) = imapobj.append(self.getfullname(),
|
||||
imaputil.flagsmaildir2imap(flags),
|
||||
date, content)
|
||||
(typ, dat) = imapobj.append(fullname,
|
||||
imaputil.flagsmaildir2imap(flags), date, content)
|
||||
# This should only catch 'NO' responses since append()
|
||||
# will raise an exception for 'BAD' responses:
|
||||
if typ != 'OK':
|
||||
@ -580,7 +580,7 @@ class IMAPFolder(BaseFolder):
|
||||
# and continue with the next account.
|
||||
msg = \
|
||||
"Saving msg (%s) in folder '%s', repository '%s' failed (abort). " \
|
||||
"Server responded: %s %s\n" % \
|
||||
"Server responded: %s %s\n"% \
|
||||
(msg_id, self, self.getrepository(), typ, dat)
|
||||
raise OfflineImapError(msg, OfflineImapError.ERROR.REPO)
|
||||
retry_left = 0 # Mark as success
|
||||
@ -592,7 +592,7 @@ class IMAPFolder(BaseFolder):
|
||||
if not retry_left:
|
||||
raise OfflineImapError("Saving msg (%s) in folder '%s', "
|
||||
"repository '%s' failed (abort). Server responded: %s\n"
|
||||
"Message content was: %s" %
|
||||
"Message content was: %s"%
|
||||
(msg_id, self, self.getrepository(), str(e), dbg_output),
|
||||
OfflineImapError.ERROR.MESSAGE)
|
||||
self.ui.error(e, exc_info()[2])
|
||||
@ -604,8 +604,8 @@ class IMAPFolder(BaseFolder):
|
||||
imapobj = None
|
||||
raise OfflineImapError("Saving msg (%s) folder '%s', repo '%s'"
|
||||
"failed (error). Server responded: %s\nMessage content was: "
|
||||
"%s" % (msg_id, self, self.getrepository(), str(e), dbg_output),
|
||||
OfflineImapError.ERROR.MESSAGE)
|
||||
"%s"% (msg_id, self, self.getrepository(), str(e), dbg_output),
|
||||
OfflineImapError.ERROR.MESSAGE)
|
||||
# Checkpoint. Let it write out stuff, etc. Eg searches for
|
||||
# just uploaded messages won't work if we don't do this.
|
||||
(typ,dat) = imapobj.check()
|
||||
@ -622,24 +622,24 @@ class IMAPFolder(BaseFolder):
|
||||
resp = imapobj._get_untagged_response('APPENDUID')
|
||||
if resp == [None] or resp is None:
|
||||
self.ui.warn("Server supports UIDPLUS but got no APPENDUID "
|
||||
"appending a message.")
|
||||
"appending a message.")
|
||||
return 0
|
||||
uid = long(resp[-1].split(' ')[1])
|
||||
if uid == 0:
|
||||
self.ui.warn("savemessage: Server supports UIDPLUS, but"
|
||||
" we got no usable uid back. APPENDUID reponse was "
|
||||
"'%s'" % str(resp))
|
||||
" we got no usable uid back. APPENDUID reponse was "
|
||||
"'%s'"% str(resp))
|
||||
else:
|
||||
# we don't support UIDPLUS
|
||||
uid = self.__savemessage_searchforheader(imapobj, headername,
|
||||
headervalue)
|
||||
headervalue)
|
||||
# See docs for savemessage in Base.py for explanation
|
||||
# of this and other return values
|
||||
if uid == 0:
|
||||
self.ui.debug('imap', 'savemessage: attempt to get new UID '
|
||||
'UID failed. Search headers manually.')
|
||||
uid = self.__savemessage_fetchheaders(imapobj, headername,
|
||||
headervalue)
|
||||
headervalue)
|
||||
self.ui.warn('imap', "savemessage: Searching mails for new "
|
||||
"Message-ID failed. Could not determine new UID.")
|
||||
finally:
|
||||
@ -649,22 +649,21 @@ class IMAPFolder(BaseFolder):
|
||||
self.messagelist[uid] = self.msglist_item_initializer(uid)
|
||||
self.messagelist[uid]['flags'] = flags
|
||||
|
||||
self.ui.debug('imap', 'savemessage: returning new UID %d' % uid)
|
||||
self.ui.debug('imap', 'savemessage: returning new UID %d'% uid)
|
||||
return uid
|
||||
|
||||
|
||||
def _fetch_from_imap(self, imapobj, uids, retry_num=1):
|
||||
"""
|
||||
Fetches data from IMAP server.
|
||||
"""Fetches data from IMAP server.
|
||||
|
||||
Arguments:
|
||||
- imapobj: IMAPlib object
|
||||
- uids: message UIDS
|
||||
- retry_num: number of retries to make
|
||||
|
||||
Returns: data obtained by this query.
|
||||
"""
|
||||
query = "(%s)" % (" ".join(self.imap_query))
|
||||
Returns: data obtained by this query."""
|
||||
|
||||
query = "(%s)"% (" ".join(self.imap_query))
|
||||
fails_left = retry_num # retry on dropped connection
|
||||
while fails_left:
|
||||
try:
|
||||
@ -683,7 +682,7 @@ class IMAPFolder(BaseFolder):
|
||||
#IMAP server says bad request or UID does not exist
|
||||
severity = OfflineImapError.ERROR.MESSAGE
|
||||
reason = "IMAP server '%s' failed to fetch messages UID '%s'."\
|
||||
"Server responded: %s %s" % (self.getrepository(), uids,
|
||||
"Server responded: %s %s"% (self.getrepository(), uids,
|
||||
res_type, data)
|
||||
if data == [None]:
|
||||
#IMAP server did not find a message with this UID
|
||||
@ -695,23 +694,21 @@ class IMAPFolder(BaseFolder):
|
||||
|
||||
|
||||
def _store_to_imap(self, imapobj, uid, field, data):
|
||||
"""
|
||||
Stores data to IMAP server
|
||||
"""Stores data to IMAP server
|
||||
|
||||
Arguments:
|
||||
- imapobj: instance of IMAPlib to use
|
||||
- uid: message UID
|
||||
- field: field name to be stored/updated
|
||||
- data: field contents
|
||||
|
||||
"""
|
||||
imapobj.select(self.getfullname())
|
||||
res_type, retdata = imapobj.uid('store', uid, field, data)
|
||||
if res_type != 'OK':
|
||||
severity = OfflineImapError.ERROR.MESSAGE
|
||||
reason = "IMAP server '%s' failed to store %s for message UID '%d'."\
|
||||
"Server responded: %s %s" % (self.getrepository(), field, uid,
|
||||
res_type, retdata)
|
||||
"Server responded: %s %s"% (
|
||||
self.getrepository(), field, uid, res_type, retdata)
|
||||
raise OfflineImapError(reason, severity)
|
||||
return retdata[0]
|
||||
|
||||
@ -724,12 +721,11 @@ class IMAPFolder(BaseFolder):
|
||||
dryrun mode."""
|
||||
imapobj = self.imapserver.acquireconnection()
|
||||
try:
|
||||
result = self._store_to_imap(imapobj, str(uid), 'FLAGS', imaputil.flagsmaildir2imap(flags))
|
||||
|
||||
result = self._store_to_imap(imapobj, str(uid), 'FLAGS',
|
||||
imaputil.flagsmaildir2imap(flags))
|
||||
except imapobj.readonly:
|
||||
self.ui.flagstoreadonly(self, [uid], flags)
|
||||
return
|
||||
|
||||
finally:
|
||||
self.imapserver.releaseconnection(imapobj)
|
||||
|
||||
@ -751,6 +747,7 @@ class IMAPFolder(BaseFolder):
|
||||
"""This is here for the sake of UIDMaps.py -- deletemessages must
|
||||
add flags and get a converted UID, and if we don't have noconvert,
|
||||
then UIDMaps will try to convert it twice."""
|
||||
|
||||
self.__addmessagesflags_noconvert(uidlist, flags)
|
||||
|
||||
# Interface from BaseFolder
|
||||
@ -770,9 +767,8 @@ class IMAPFolder(BaseFolder):
|
||||
self.ui.flagstoreadonly(self, uidlist, flags)
|
||||
return
|
||||
r = imapobj.uid('store',
|
||||
imaputil.uid_sequence(uidlist),
|
||||
operation + 'FLAGS',
|
||||
imaputil.flagsmaildir2imap(flags))
|
||||
imaputil.uid_sequence(uidlist), operation + 'FLAGS',
|
||||
imaputil.flagsmaildir2imap(flags))
|
||||
assert r[0] == 'OK', 'Error with store: ' + '. '.join(r[1])
|
||||
r = r[1]
|
||||
finally:
|
||||
@ -818,9 +814,9 @@ class IMAPFolder(BaseFolder):
|
||||
"""Change the message from existing uid to new_uid
|
||||
|
||||
If the backend supports it. IMAP does not and will throw errors."""
|
||||
|
||||
raise OfflineImapError('IMAP backend cannot change a messages UID from '
|
||||
'%d to %d' % (uid, new_uid),
|
||||
OfflineImapError.ERROR.MESSAGE)
|
||||
'%d to %d'% (uid, new_uid), OfflineImapError.ERROR.MESSAGE)
|
||||
|
||||
# Interface from BaseFolder
|
||||
def deletemessage(self, uid):
|
||||
|
Reference in New Issue
Block a user