2002-06-19 06:39:00 +02:00
|
|
|
# IMAP repository support
|
2015-01-01 21:41:11 +01:00
|
|
|
# Copyright (C) 2002-2015 John Goerzen & contributors
|
2002-06-19 06:39:00 +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 06:39:00 +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 06:39:00 +02:00
|
|
|
|
2011-05-11 18:25:13 +02:00
|
|
|
from threading import Event
|
|
|
|
import os
|
2011-08-12 08:56:56 +02:00
|
|
|
from sys import exc_info
|
2011-05-11 18:25:13 +02:00
|
|
|
import netrc
|
|
|
|
import errno
|
2002-06-19 06:39:00 +02:00
|
|
|
|
2015-01-01 21:41:11 +01:00
|
|
|
from offlineimap.repository.Base import BaseRepository
|
|
|
|
from offlineimap import folder, imaputil, imapserver, OfflineImapError
|
|
|
|
from offlineimap.folder.UIDMaps import MappedIMAPFolder
|
|
|
|
from offlineimap.threadutil import ExitNotifyThread
|
2015-01-18 08:45:46 +01:00
|
|
|
from offlineimap.utils.distro import get_os_sslcertfile, get_os_sslcertfile_searchpath
|
2015-01-01 21:41:11 +01:00
|
|
|
|
|
|
|
|
2002-06-19 06:39:00 +02:00
|
|
|
class IMAPRepository(BaseRepository):
|
2003-04-18 04:18:34 +02:00
|
|
|
def __init__(self, reposname, account):
|
|
|
|
"""Initialize an IMAPRepository object."""
|
|
|
|
BaseRepository.__init__(self, reposname, account)
|
2011-03-03 11:43:23 +01:00
|
|
|
# self.ui is being set by the BaseRepository
|
2011-06-06 13:12:23 +02:00
|
|
|
self._host = None
|
2011-08-12 08:31:09 +02:00
|
|
|
self.imapserver = imapserver.IMAPServer(self)
|
2002-06-19 07:55:12 +02:00
|
|
|
self.folders = None
|
2012-04-02 23:26:59 +02:00
|
|
|
if self.getconf('sep', None):
|
|
|
|
self.ui.info("The 'sep' setting is being ignored for IMAP "
|
2015-01-14 22:58:25 +01:00
|
|
|
"repository '%s' (it's autodetected)"% self)
|
2003-04-18 04:18:34 +02:00
|
|
|
|
|
|
|
def startkeepalive(self):
|
|
|
|
keepalivetime = self.getkeepalive()
|
|
|
|
if not keepalivetime: return
|
|
|
|
self.kaevent = Event()
|
|
|
|
self.kathread = ExitNotifyThread(target = self.imapserver.keepalive,
|
|
|
|
name = "Keep alive " + self.getname(),
|
|
|
|
args = (keepalivetime, self.kaevent))
|
|
|
|
self.kathread.setDaemon(1)
|
|
|
|
self.kathread.start()
|
|
|
|
|
2008-08-03 00:04:32 +02:00
|
|
|
def stopkeepalive(self):
|
2003-04-18 04:18:34 +02:00
|
|
|
if not hasattr(self, 'kaevent'):
|
|
|
|
# Keepalive is not active.
|
|
|
|
return
|
|
|
|
|
|
|
|
self.kaevent.set()
|
|
|
|
del self.kathread
|
|
|
|
del self.kaevent
|
|
|
|
|
|
|
|
def holdordropconnections(self):
|
|
|
|
if not self.getholdconnectionopen():
|
|
|
|
self.dropconnections()
|
|
|
|
|
|
|
|
def dropconnections(self):
|
|
|
|
self.imapserver.close()
|
|
|
|
|
|
|
|
def getholdconnectionopen(self):
|
2011-05-19 21:02:26 +02:00
|
|
|
if self.getidlefolders():
|
|
|
|
return 1
|
2003-04-18 04:18:34 +02:00
|
|
|
return self.getconfboolean("holdconnectionopen", 0)
|
|
|
|
|
|
|
|
def getkeepalive(self):
|
2011-05-19 21:02:30 +02:00
|
|
|
num = self.getconfint("keepalive", 0)
|
|
|
|
if num == 0 and self.getidlefolders():
|
2011-05-19 21:02:26 +02:00
|
|
|
return 29*60
|
2011-05-19 21:02:30 +02:00
|
|
|
else:
|
|
|
|
return num
|
2002-06-19 07:22:21 +02:00
|
|
|
|
2002-06-19 08:16:19 +02:00
|
|
|
def getsep(self):
|
2011-12-01 09:49:09 +01:00
|
|
|
"""Return the folder separator for the IMAP repository
|
|
|
|
|
|
|
|
This requires that self.imapserver has been initialized with an
|
|
|
|
acquireconnection() or it will still be `None`"""
|
|
|
|
assert self.imapserver.delim != None, "'%s' " \
|
|
|
|
"repository called getsep() before the folder separator was " \
|
2015-01-14 22:58:25 +01:00
|
|
|
"queried from the server"% self
|
2002-06-19 08:16:19 +02:00
|
|
|
return self.imapserver.delim
|
|
|
|
|
2003-04-18 04:18:34 +02:00
|
|
|
def gethost(self):
|
2011-06-06 13:12:23 +02:00
|
|
|
"""Return the configured hostname to connect to
|
|
|
|
|
|
|
|
:returns: hostname as string or throws Exception"""
|
|
|
|
if self._host: # use cached value if possible
|
|
|
|
return self._host
|
2006-10-17 21:55:03 +02:00
|
|
|
|
2011-06-06 13:12:23 +02:00
|
|
|
# 1) check for remotehosteval setting
|
2006-10-17 21:55:03 +02:00
|
|
|
if self.config.has_option(self.getsection(), 'remotehosteval'):
|
2007-07-04 23:00:14 +02:00
|
|
|
host = self.getconf('remotehosteval')
|
2011-06-06 13:12:23 +02:00
|
|
|
try:
|
|
|
|
host = self.localeval.eval(host)
|
2012-02-05 10:14:23 +01:00
|
|
|
except Exception as e:
|
2015-01-14 22:58:25 +01:00
|
|
|
raise OfflineImapError("remotehosteval option for repository "
|
|
|
|
"'%s' failed:\n%s"% (self, e), OfflineImapError.ERROR.REPO), \
|
|
|
|
None, exc_info()[2]
|
2011-06-06 13:12:23 +02:00
|
|
|
if host:
|
|
|
|
self._host = host
|
|
|
|
return self._host
|
|
|
|
# 2) check for plain remotehost setting
|
|
|
|
host = self.getconf('remotehost', None)
|
2007-07-04 23:00:14 +02:00
|
|
|
if host != None:
|
2011-06-06 13:12:23 +02:00
|
|
|
self._host = host
|
|
|
|
return self._host
|
2006-10-17 21:55:03 +02:00
|
|
|
|
2011-06-06 13:12:23 +02:00
|
|
|
# no success
|
2015-01-14 22:58:25 +01:00
|
|
|
raise OfflineImapError("No remote host for repository "
|
|
|
|
"'%s' specified."% self, OfflineImapError.ERROR.REPO)
|
2003-04-18 04:18:34 +02:00
|
|
|
|
2013-08-03 14:06:44 +02:00
|
|
|
def get_remote_identity(self):
|
2015-01-01 21:41:11 +01:00
|
|
|
"""Remote identity is used for certain SASL mechanisms
|
2013-08-03 14:06:44 +02:00
|
|
|
(currently -- PLAIN) to inform server about the ID
|
2015-01-01 21:41:11 +01:00
|
|
|
we want to authorize as instead of our login name."""
|
2013-08-03 14:06:44 +02:00
|
|
|
|
|
|
|
return self.getconf('remote_identity', default=None)
|
|
|
|
|
2013-08-07 13:43:51 +02:00
|
|
|
def get_auth_mechanisms(self):
|
|
|
|
supported = ["GSSAPI", "CRAM-MD5", "PLAIN", "LOGIN"]
|
|
|
|
# Mechanisms are ranged from the strongest to the
|
|
|
|
# weakest ones.
|
|
|
|
# TODO: we need DIGEST-MD5, it must come before CRAM-MD5
|
|
|
|
# TODO: due to the chosen-plaintext resistance.
|
|
|
|
default = ["GSSAPI", "CRAM-MD5", "PLAIN", "LOGIN"]
|
|
|
|
|
|
|
|
mechs = self.getconflist('auth_mechanisms', r',\s*',
|
|
|
|
default)
|
|
|
|
|
|
|
|
for m in mechs:
|
|
|
|
if m not in supported:
|
2015-01-14 22:58:25 +01:00
|
|
|
raise OfflineImapError("Repository %s: "% self + \
|
|
|
|
"unknown authentication mechanism '%s'"% m,
|
2013-08-07 13:43:51 +02:00
|
|
|
OfflineImapError.ERROR.REPO)
|
|
|
|
|
|
|
|
self.ui.debug('imap', "Using authentication mechanisms %s" % mechs)
|
|
|
|
return mechs
|
|
|
|
|
2013-08-03 14:06:44 +02:00
|
|
|
|
2003-04-18 04:18:34 +02:00
|
|
|
def getuser(self):
|
2007-07-04 23:00:14 +02:00
|
|
|
user = None
|
2006-10-17 21:55:03 +02:00
|
|
|
localeval = self.localeval
|
|
|
|
|
|
|
|
if self.config.has_option(self.getsection(), 'remoteusereval'):
|
2007-07-04 23:00:14 +02:00
|
|
|
user = self.getconf('remoteusereval')
|
|
|
|
if user != None:
|
|
|
|
return localeval.eval(user)
|
2006-10-17 21:55:03 +02:00
|
|
|
|
2014-05-31 16:42:25 +02:00
|
|
|
if self.config.has_option(self.getsection(), 'remoteuser'):
|
|
|
|
user = self.getconf('remoteuser')
|
2007-07-04 23:00:14 +02:00
|
|
|
if user != None:
|
|
|
|
return user
|
2003-04-18 04:18:34 +02:00
|
|
|
|
2008-03-03 09:21:33 +01:00
|
|
|
try:
|
2010-08-19 20:56:51 +02:00
|
|
|
netrcentry = netrc.netrc().authenticators(self.gethost())
|
2012-02-05 10:14:23 +01:00
|
|
|
except IOError as inst:
|
2008-03-03 09:21:33 +01:00
|
|
|
if inst.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
if netrcentry:
|
|
|
|
return netrcentry[0]
|
2003-04-18 04:18:34 +02:00
|
|
|
|
2008-12-02 20:15:44 +01:00
|
|
|
try:
|
2010-08-19 20:56:51 +02:00
|
|
|
netrcentry = netrc.netrc('/etc/netrc').authenticators(self.gethost())
|
2012-02-05 10:14:23 +01:00
|
|
|
except IOError as inst:
|
2012-01-08 19:57:03 +01:00
|
|
|
if inst.errno not in (errno.ENOENT, errno.EACCES):
|
2008-12-02 20:15:44 +01:00
|
|
|
raise
|
|
|
|
else:
|
|
|
|
if netrcentry:
|
|
|
|
return netrcentry[0]
|
|
|
|
|
|
|
|
|
2003-04-18 04:18:34 +02:00
|
|
|
def getport(self):
|
2013-07-10 03:17:01 +02:00
|
|
|
port = None
|
2013-06-20 11:38:45 +02:00
|
|
|
|
2013-07-10 03:17:01 +02:00
|
|
|
if self.config.has_option(self.getsection(), 'remoteporteval'):
|
|
|
|
port = self.getconf('remoteporteval')
|
|
|
|
if port != None:
|
|
|
|
return self.localeval.eval(port)
|
2013-06-20 11:38:45 +02:00
|
|
|
|
2003-04-18 04:18:34 +02:00
|
|
|
return self.getconfint('remoteport', None)
|
|
|
|
|
|
|
|
def getssl(self):
|
|
|
|
return self.getconfboolean('ssl', 0)
|
|
|
|
|
2008-05-23 21:58:18 +02:00
|
|
|
def getsslclientcert(self):
|
2014-11-02 09:49:26 +01:00
|
|
|
xforms = [os.path.expanduser, os.path.expandvars, os.path.abspath]
|
|
|
|
return self.getconf_xform('sslclientcert', xforms, None)
|
2008-05-23 21:58:18 +02:00
|
|
|
|
|
|
|
def getsslclientkey(self):
|
2014-11-02 09:49:26 +01:00
|
|
|
xforms = [os.path.expanduser, os.path.expandvars, os.path.abspath]
|
|
|
|
return self.getconf_xform('sslclientkey', xforms, None)
|
2008-05-23 21:58:18 +02:00
|
|
|
|
2010-12-16 13:43:47 +01:00
|
|
|
def getsslcacertfile(self):
|
2015-01-18 08:45:46 +01:00
|
|
|
"""Determines CA bundle.
|
|
|
|
|
|
|
|
Returns path to the CA bundle. It is either explicitely specified
|
|
|
|
or requested via "OS-DEFAULT" value (and we will search known
|
|
|
|
locations for the current OS and distribution).
|
|
|
|
|
|
|
|
If search via "OS-DEFAULT" route yields nothing, we will throw an
|
|
|
|
exception to make our callers distinguish between not specified
|
|
|
|
value and non-existent default CA bundle.
|
|
|
|
|
|
|
|
It is also an error to specify non-existent file via configuration:
|
|
|
|
it will error out later, but, perhaps, with less verbose explanation,
|
|
|
|
so we will also throw an exception. It is consistent with
|
|
|
|
the above behaviour, so any explicitely-requested configuration
|
|
|
|
that doesn't result in an existing file will give an exception.
|
|
|
|
"""
|
|
|
|
|
2014-11-02 09:49:26 +01:00
|
|
|
xforms = [os.path.expanduser, os.path.expandvars, os.path.abspath]
|
2015-01-18 08:45:46 +01:00
|
|
|
cacertfile = self.getconf_xform('sslcacertfile', xforms, None)
|
|
|
|
if self.getconf('sslcacertfile', None) == "OS-DEFAULT":
|
|
|
|
cacertfile = get_os_sslcertfile()
|
|
|
|
if cacertfile == None:
|
|
|
|
searchpath = get_os_sslcertfile_searchpath()
|
|
|
|
if searchpath:
|
|
|
|
reason = "Default CA bundle was requested, "\
|
|
|
|
"but no existing locations available. "\
|
|
|
|
"Tried %s." % (", ".join(searchpath))
|
|
|
|
else:
|
|
|
|
reason = "Default CA bundle was requested, "\
|
|
|
|
"but OfflineIMAP doesn't know any for your "\
|
|
|
|
"current operating system."
|
|
|
|
raise OfflineImapError(reason, OfflineImapError.ERROR.REPO)
|
2011-03-15 11:18:19 +01:00
|
|
|
if cacertfile is None:
|
|
|
|
return None
|
|
|
|
if not os.path.isfile(cacertfile):
|
2015-01-18 08:45:46 +01:00
|
|
|
reason = "CA certfile for repository '%s' couldn't be found. "\
|
|
|
|
"No such file: '%s'" % (self.name, cacertfile)
|
|
|
|
raise OfflineImapError(reason, OfflineImapError.ERROR.REPO)
|
2011-03-15 11:18:19 +01:00
|
|
|
return cacertfile
|
2010-12-16 13:43:47 +01:00
|
|
|
|
2013-07-07 23:18:59 +02:00
|
|
|
def getsslversion(self):
|
|
|
|
return self.getconf('ssl_version', None)
|
|
|
|
|
2011-09-12 09:50:41 +02:00
|
|
|
def get_ssl_fingerprint(self):
|
2015-01-01 21:41:11 +01:00
|
|
|
"""Return array of possible certificate fingerprints.
|
2014-05-06 23:22:29 +02:00
|
|
|
|
|
|
|
Configuration item cert_fingerprint can contain multiple
|
2015-01-01 21:41:11 +01:00
|
|
|
comma-separated fingerprints in hex form."""
|
2014-05-06 23:22:29 +02:00
|
|
|
|
|
|
|
value = self.getconf('cert_fingerprint', "")
|
|
|
|
return [f.strip().lower() for f in value.split(',') if f]
|
2011-09-12 09:50:41 +02:00
|
|
|
|
2003-04-18 04:18:34 +02:00
|
|
|
def getpreauthtunnel(self):
|
|
|
|
return self.getconf('preauthtunnel', None)
|
|
|
|
|
2013-05-03 15:56:20 +02:00
|
|
|
def gettransporttunnel(self):
|
|
|
|
return self.getconf('transporttunnel', None)
|
|
|
|
|
2003-04-18 04:18:34 +02:00
|
|
|
def getreference(self):
|
2011-09-30 16:53:18 +02:00
|
|
|
return self.getconf('reference', '')
|
2003-04-18 04:18:34 +02:00
|
|
|
|
2011-05-19 21:02:26 +02:00
|
|
|
def getidlefolders(self):
|
|
|
|
localeval = self.localeval
|
|
|
|
return localeval.eval(self.getconf('idlefolders', '[]'))
|
|
|
|
|
2003-04-18 04:18:34 +02:00
|
|
|
def getmaxconnections(self):
|
2011-05-19 21:02:26 +02:00
|
|
|
num1 = len(self.getidlefolders())
|
|
|
|
num2 = self.getconfint('maxconnections', 1)
|
|
|
|
return max(num1, num2)
|
2003-04-18 04:18:34 +02:00
|
|
|
|
|
|
|
def getexpunge(self):
|
|
|
|
return self.getconfboolean('expunge', 1)
|
|
|
|
|
|
|
|
def getpassword(self):
|
2010-12-13 18:45:42 +01:00
|
|
|
"""Return the IMAP password for this repository.
|
2006-10-17 21:55:03 +02:00
|
|
|
|
2010-12-13 18:45:42 +01:00
|
|
|
It tries to get passwords in the following order:
|
|
|
|
|
|
|
|
1. evaluate Repository 'remotepasseval'
|
|
|
|
2. read password from Repository 'remotepass'
|
|
|
|
3. read password from file specified in Repository 'remotepassfile'
|
|
|
|
4. read password from ~/.netrc
|
|
|
|
5. read password from /etc/netrc
|
2006-10-17 21:55:03 +02:00
|
|
|
|
2010-12-13 18:45:42 +01:00
|
|
|
On success we return the password.
|
2015-01-01 21:41:11 +01:00
|
|
|
If all strategies fail we return None."""
|
|
|
|
|
2010-12-13 18:45:42 +01:00
|
|
|
# 1. evaluate Repository 'remotepasseval'
|
|
|
|
passwd = self.getconf('remotepasseval', None)
|
|
|
|
if passwd != None:
|
|
|
|
return self.localeval.eval(passwd)
|
|
|
|
# 2. read password from Repository 'remotepass'
|
2003-04-18 04:18:34 +02:00
|
|
|
password = self.getconf('remotepass', None)
|
|
|
|
if password != None:
|
|
|
|
return password
|
2010-12-13 18:45:42 +01:00
|
|
|
# 3. read password from file specified in Repository 'remotepassfile'
|
2003-04-18 04:18:34 +02:00
|
|
|
passfile = self.getconf('remotepassfile', None)
|
|
|
|
if passfile != None:
|
|
|
|
fd = open(os.path.expanduser(passfile))
|
2003-04-29 03:52:03 +02:00
|
|
|
password = fd.readline().strip()
|
|
|
|
fd.close()
|
2007-07-04 23:00:14 +02:00
|
|
|
return password
|
2010-12-13 18:45:42 +01:00
|
|
|
# 4. read password from ~/.netrc
|
2008-03-03 09:21:33 +01:00
|
|
|
try:
|
|
|
|
netrcentry = netrc.netrc().authenticators(self.gethost())
|
2012-02-05 10:14:23 +01:00
|
|
|
except IOError as inst:
|
2008-03-03 09:21:33 +01:00
|
|
|
if inst.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
if netrcentry:
|
2014-05-31 16:42:25 +02:00
|
|
|
user = self.getuser()
|
2008-03-03 09:21:33 +01:00
|
|
|
if user == None or user == netrcentry[0]:
|
|
|
|
return netrcentry[2]
|
2010-12-13 18:45:42 +01:00
|
|
|
# 5. read password from /etc/netrc
|
2008-12-02 20:15:44 +01:00
|
|
|
try:
|
|
|
|
netrcentry = netrc.netrc('/etc/netrc').authenticators(self.gethost())
|
2012-02-05 10:14:23 +01:00
|
|
|
except IOError as inst:
|
2012-01-08 19:57:03 +01:00
|
|
|
if inst.errno not in (errno.ENOENT, errno.EACCES):
|
2008-12-02 20:15:44 +01:00
|
|
|
raise
|
|
|
|
else:
|
|
|
|
if netrcentry:
|
2014-05-31 16:42:25 +02:00
|
|
|
user = self.getuser()
|
2008-12-02 20:15:44 +01:00
|
|
|
if user == None or user == netrcentry[0]:
|
|
|
|
return netrcentry[2]
|
2010-12-13 18:45:42 +01:00
|
|
|
# no strategy yielded a password!
|
2003-04-18 04:18:34 +02:00
|
|
|
return None
|
|
|
|
|
2002-06-20 04:55:24 +02:00
|
|
|
def getfolder(self, foldername):
|
2015-01-11 13:54:53 +01:00
|
|
|
"""Return instance of OfflineIMAP representative folder."""
|
|
|
|
|
2011-09-16 10:54:26 +02:00
|
|
|
return self.getfoldertype()(self.imapserver, foldername, self)
|
2003-04-18 04:18:34 +02:00
|
|
|
|
|
|
|
def getfoldertype(self):
|
|
|
|
return folder.IMAP.IMAPFolder
|
2002-06-20 04:55:24 +02:00
|
|
|
|
2007-07-05 06:04:14 +02:00
|
|
|
def connect(self):
|
|
|
|
imapobj = self.imapserver.acquireconnection()
|
|
|
|
self.imapserver.releaseconnection(imapobj)
|
|
|
|
|
2007-07-06 18:46:29 +02:00
|
|
|
def forgetfolders(self):
|
|
|
|
self.folders = None
|
|
|
|
|
2002-06-19 07:22:21 +02:00
|
|
|
def getfolders(self):
|
2015-01-11 13:54:53 +01:00
|
|
|
"""Return a list of instances of OfflineIMAP representative folder."""
|
|
|
|
|
2002-06-19 07:55:12 +02:00
|
|
|
if self.folders != None:
|
|
|
|
return self.folders
|
2002-06-19 07:22:21 +02:00
|
|
|
retval = []
|
2002-07-04 02:15:32 +02:00
|
|
|
imapobj = self.imapserver.acquireconnection()
|
2008-10-01 07:33:57 +02:00
|
|
|
# check whether to list all folders, or subscribed only
|
|
|
|
listfunction = imapobj.list
|
2011-09-15 15:45:09 +02:00
|
|
|
if self.getconfboolean('subscribedonly', False):
|
2008-10-01 07:33:57 +02:00
|
|
|
listfunction = imapobj.lsub
|
2002-07-04 02:15:32 +02:00
|
|
|
try:
|
2008-10-01 07:33:57 +02:00
|
|
|
listresult = listfunction(directory = self.imapserver.reference)[1]
|
2002-07-04 02:15:32 +02:00
|
|
|
finally:
|
|
|
|
self.imapserver.releaseconnection(imapobj)
|
2015-01-11 10:00:37 +01:00
|
|
|
for s in listresult:
|
|
|
|
if s == None or \
|
|
|
|
(isinstance(s, basestring) and s == ''):
|
2002-10-16 07:43:02 +02:00
|
|
|
# Bug in imaplib: empty strings in results from
|
2012-02-05 12:23:49 +01:00
|
|
|
# literals. TODO: still relevant?
|
2002-10-16 07:43:02 +02:00
|
|
|
continue
|
2015-01-11 10:00:37 +01:00
|
|
|
flags, delim, name = imaputil.imapsplit(s)
|
2002-07-06 02:22:23 +02:00
|
|
|
flaglist = [x.lower() for x in imaputil.flagsplit(flags)]
|
|
|
|
if '\\noselect' in flaglist:
|
2002-06-19 07:22:21 +02:00
|
|
|
continue
|
2002-07-04 08:10:51 +02:00
|
|
|
foldername = imaputil.dequote(name)
|
2003-04-18 04:18:34 +02:00
|
|
|
retval.append(self.getfoldertype()(self.imapserver, foldername,
|
2011-09-16 10:54:23 +02:00
|
|
|
self))
|
2011-08-14 13:38:13 +02:00
|
|
|
# Add all folderincludes
|
2003-04-18 06:31:25 +02:00
|
|
|
if len(self.folderincludes):
|
|
|
|
imapobj = self.imapserver.acquireconnection()
|
|
|
|
try:
|
|
|
|
for foldername in self.folderincludes:
|
2003-04-18 09:06:04 +02:00
|
|
|
try:
|
2011-11-02 08:40:03 +01:00
|
|
|
imapobj.select(foldername, readonly = True)
|
2012-02-05 10:14:23 +01:00
|
|
|
except OfflineImapError as e:
|
2011-08-12 08:56:56 +02:00
|
|
|
# couldn't select this folderinclude, so ignore folder.
|
|
|
|
if e.severity > OfflineImapError.ERROR.FOLDER:
|
|
|
|
raise
|
|
|
|
self.ui.error(e, exc_info()[2],
|
|
|
|
'Invalid folderinclude:')
|
2003-04-18 09:06:04 +02:00
|
|
|
continue
|
2015-01-13 13:59:52 +01:00
|
|
|
retval.append(self.getfoldertype()(
|
|
|
|
self.imapserver, foldername, self))
|
2003-04-18 06:31:25 +02:00
|
|
|
finally:
|
|
|
|
self.imapserver.releaseconnection(imapobj)
|
2012-02-06 17:33:50 +01:00
|
|
|
|
|
|
|
if self.foldersort is None:
|
|
|
|
# default sorting by case insensitive transposed name
|
|
|
|
retval.sort(key=lambda x: str.lower(x.getvisiblename()))
|
|
|
|
else:
|
|
|
|
# do foldersort in a python3-compatible way
|
|
|
|
# http://bytes.com/topic/python/answers/844614-python-3-sorting-comparison-function
|
|
|
|
def cmp2key(mycmp):
|
|
|
|
"""Converts a cmp= function into a key= function
|
|
|
|
We need to keep cmp functions for backward compatibility"""
|
|
|
|
class K:
|
|
|
|
def __init__(self, obj, *args):
|
|
|
|
self.obj = obj
|
|
|
|
def __cmp__(self, other):
|
2013-08-27 14:23:12 +02:00
|
|
|
return mycmp(self.obj.getvisiblename(), other.obj.getvisiblename())
|
2012-02-06 17:33:50 +01:00
|
|
|
return K
|
|
|
|
retval.sort(key=cmp2key(self.foldersort))
|
|
|
|
|
2002-06-19 07:55:12 +02:00
|
|
|
self.folders = retval
|
2011-08-14 13:38:13 +02:00
|
|
|
return self.folders
|
2003-04-18 04:18:34 +02:00
|
|
|
|
|
|
|
def makefolder(self, foldername):
|
2011-08-29 14:18:53 +02:00
|
|
|
"""Create a folder on the IMAP server
|
|
|
|
|
2011-09-30 08:58:08 +02:00
|
|
|
This will not update the list cached in :meth:`getfolders`. You
|
|
|
|
will need to invoke :meth:`forgetfolders` to force new caching
|
|
|
|
when you are done creating folders yourself.
|
|
|
|
|
2011-08-29 14:18:53 +02:00
|
|
|
:param foldername: Full path of the folder to be created."""
|
2015-01-01 21:41:11 +01:00
|
|
|
|
2011-09-30 16:53:18 +02:00
|
|
|
if self.getreference():
|
|
|
|
foldername = self.getreference() + self.getsep() + foldername
|
2011-09-30 17:20:11 +02:00
|
|
|
if not foldername: # Create top level folder as folder separator
|
|
|
|
foldername = self.getsep()
|
2011-09-15 15:37:52 +02:00
|
|
|
self.ui.makefolder(self, foldername)
|
|
|
|
if self.account.dryrun:
|
|
|
|
return
|
2003-04-18 04:18:34 +02:00
|
|
|
imapobj = self.imapserver.acquireconnection()
|
|
|
|
try:
|
2011-08-29 14:18:53 +02:00
|
|
|
result = imapobj.create(foldername)
|
2003-04-18 04:18:34 +02:00
|
|
|
if result[0] != 'OK':
|
2011-08-29 14:18:53 +02:00
|
|
|
raise OfflineImapError("Folder '%s'[%s] could not be created. "
|
2015-01-14 22:58:25 +01:00
|
|
|
"Server responded: %s"% (foldername, self, str(result)),
|
|
|
|
OfflineImapError.ERROR.FOLDER)
|
2003-04-18 04:18:34 +02:00
|
|
|
finally:
|
|
|
|
self.imapserver.releaseconnection(imapobj)
|
2013-07-21 21:00:23 +02:00
|
|
|
|
2003-04-18 04:18:34 +02:00
|
|
|
class MappedIMAPRepository(IMAPRepository):
|
|
|
|
def getfoldertype(self):
|
|
|
|
return MappedIMAPFolder
|