2002-06-19 06:39:00 +02:00
|
|
|
# IMAP repository support
|
2011-09-30 16:53:18 +02:00
|
|
|
# Copyright (C) 2002-2011 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-03-03 11:05:15 +01:00
|
|
|
from offlineimap.repository.Base import BaseRepository
|
2011-06-06 13:12:23 +02:00
|
|
|
from offlineimap import folder, imaputil, imapserver, OfflineImapError
|
2003-04-18 04:18:34 +02:00
|
|
|
from offlineimap.folder.UIDMaps import MappedIMAPFolder
|
|
|
|
from offlineimap.threadutil import ExitNotifyThread
|
2011-05-11 18:25:13 +02:00
|
|
|
from threading import Event
|
|
|
|
import types
|
|
|
|
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
|
|
|
|
|
|
|
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
|
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):
|
|
|
|
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)
|
|
|
|
except Exception, e:
|
|
|
|
raise OfflineImapError("remotehosteval option for repository "\
|
|
|
|
"'%s' failed:\n%s" % (self, e),
|
|
|
|
OfflineImapError.ERROR.REPO)
|
|
|
|
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
|
|
|
|
raise OfflineImapError("No remote host for repository "\
|
|
|
|
"'%s' specified." % self,
|
|
|
|
OfflineImapError.ERROR.REPO)
|
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
|
|
|
|
2007-07-04 23:00:14 +02:00
|
|
|
user = self.getconf('remoteuser')
|
|
|
|
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())
|
2008-03-03 09:21:33 +01:00
|
|
|
except IOError, inst:
|
|
|
|
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())
|
2008-12-02 20:15:44 +01:00
|
|
|
except IOError, inst:
|
|
|
|
if inst.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
if netrcentry:
|
|
|
|
return netrcentry[0]
|
|
|
|
|
|
|
|
|
2003-04-18 04:18:34 +02:00
|
|
|
def getport(self):
|
|
|
|
return self.getconfint('remoteport', None)
|
|
|
|
|
|
|
|
def getssl(self):
|
|
|
|
return self.getconfboolean('ssl', 0)
|
|
|
|
|
2008-05-23 21:58:18 +02:00
|
|
|
def getsslclientcert(self):
|
|
|
|
return self.getconf('sslclientcert', None)
|
|
|
|
|
|
|
|
def getsslclientkey(self):
|
|
|
|
return self.getconf('sslclientkey', None)
|
|
|
|
|
2010-12-16 13:43:47 +01:00
|
|
|
def getsslcacertfile(self):
|
2011-03-15 11:18:19 +01:00
|
|
|
"""Return the absolute path of the CA certfile to use, if any"""
|
|
|
|
cacertfile = self.getconf('sslcacertfile', None)
|
|
|
|
if cacertfile is None:
|
|
|
|
return None
|
|
|
|
cacertfile = os.path.expanduser(cacertfile)
|
|
|
|
cacertfile = os.path.abspath(cacertfile)
|
|
|
|
if not os.path.isfile(cacertfile):
|
|
|
|
raise SyntaxWarning("CA certfile for repository '%s' could "
|
|
|
|
"not be found. No such file: '%s'" \
|
|
|
|
% (self.name, cacertfile))
|
|
|
|
return cacertfile
|
2010-12-16 13:43:47 +01:00
|
|
|
|
2011-09-12 09:50:41 +02:00
|
|
|
def get_ssl_fingerprint(self):
|
|
|
|
return self.getconf('cert_fingerprint', None)
|
|
|
|
|
2003-04-18 04:18:34 +02:00
|
|
|
def getpreauthtunnel(self):
|
|
|
|
return self.getconf('preauthtunnel', None)
|
|
|
|
|
|
|
|
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.
|
|
|
|
If all strategies fail we return None.
|
|
|
|
"""
|
|
|
|
# 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())
|
|
|
|
except IOError, inst:
|
|
|
|
if inst.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
if netrcentry:
|
|
|
|
user = self.getconf('remoteuser')
|
|
|
|
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())
|
|
|
|
except IOError, inst:
|
|
|
|
if inst.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
if netrcentry:
|
|
|
|
user = self.getconf('remoteuser')
|
|
|
|
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
|
|
|
|
|
2010-12-13 18:45:42 +01:00
|
|
|
|
2002-06-20 04:55:24 +02:00
|
|
|
def getfolder(self, foldername):
|
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):
|
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)
|
|
|
|
for string in listresult:
|
2003-04-18 04:18:34 +02:00
|
|
|
if string == None or \
|
|
|
|
(type(string) == types.StringType and string == ''):
|
2002-10-16 07:43:02 +02:00
|
|
|
# Bug in imaplib: empty strings in results from
|
|
|
|
# literals.
|
|
|
|
continue
|
2002-06-19 07:22:21 +02:00
|
|
|
flags, delim, name = imaputil.imapsplit(string)
|
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
|
|
|
# filter out the folder?
|
|
|
|
if not self.folderfilter(foldername):
|
|
|
|
self.ui.debug('imap', "Filtering out '%s'[%s] due to folderfilt"
|
|
|
|
"er" % (foldername, self))
|
|
|
|
retval[-1].sync_this = False
|
|
|
|
# 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:
|
|
|
|
imapobj.select(foldername, readonly = 1)
|
2011-08-12 08:56:56 +02:00
|
|
|
except OfflineImapError, e:
|
|
|
|
# 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
|
|
|
|
retval.append(self.getfoldertype()(self.imapserver,
|
|
|
|
foldername,
|
2011-09-16 10:54:23 +02:00
|
|
|
self))
|
2003-04-18 06:31:25 +02:00
|
|
|
finally:
|
|
|
|
self.imapserver.releaseconnection(imapobj)
|
|
|
|
|
2002-08-09 23:10:38 +02:00
|
|
|
retval.sort(lambda x, y: self.foldersort(x.getvisiblename(), y.getvisiblename()))
|
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."""
|
2011-09-30 16:53:18 +02:00
|
|
|
if self.getreference():
|
|
|
|
foldername = self.getreference() + self.getsep() + foldername
|
|
|
|
|
2003-04-18 04:18:34 +02:00
|
|
|
imapobj = self.imapserver.acquireconnection()
|
|
|
|
try:
|
2011-08-29 14:18:53 +02:00
|
|
|
self.ui._msg("Creating new IMAP folder '%s' on server %s" %\
|
|
|
|
(foldername, self))
|
|
|
|
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. "
|
|
|
|
"Server responded: %s" % \
|
|
|
|
(foldername, self, str(result)),
|
|
|
|
OfflineImapError.ERROR.FOLDER)
|
2003-04-18 04:18:34 +02:00
|
|
|
finally:
|
|
|
|
self.imapserver.releaseconnection(imapobj)
|
|
|
|
|
|
|
|
class MappedIMAPRepository(IMAPRepository):
|
|
|
|
def getfoldertype(self):
|
|
|
|
return MappedIMAPFolder
|