2002-06-19 06:39:00 +02:00
|
|
|
# IMAP server support
|
2007-07-04 19:51:10 +02:00
|
|
|
# Copyright (C) 2002 - 2007 John Goerzen
|
2002-06-19 06:39:00 +02:00
|
|
|
# <jgoerzen@complete.org>
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
2009-02-10 01:27:48 +01:00
|
|
|
from offlineimap import imaplib2, imaplibutil, imaputil, threadutil
|
2002-10-07 22:59:02 +02:00
|
|
|
from offlineimap.ui import UIBase
|
2009-02-10 01:27:48 +01:00
|
|
|
from offlineimap.accounts import syncfolder
|
2002-07-03 15:04:40 +02:00
|
|
|
from threading import *
|
2008-08-03 00:04:32 +02:00
|
|
|
import thread, hmac, os, time
|
2008-03-09 06:46:51 +01:00
|
|
|
import base64
|
2002-10-07 22:59:02 +02:00
|
|
|
|
2008-09-16 23:51:11 +02:00
|
|
|
from StringIO import StringIO
|
|
|
|
from platform import system
|
|
|
|
|
2008-03-09 06:46:51 +01:00
|
|
|
try:
|
|
|
|
# do we have a recent pykerberos?
|
|
|
|
have_gss = False
|
|
|
|
import kerberos
|
|
|
|
if 'authGSSClientWrap' in dir(kerberos):
|
|
|
|
have_gss = True
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2002-07-03 15:04:40 +02:00
|
|
|
|
|
|
|
class UsefulIMAPMixIn:
|
|
|
|
def getstate(self):
|
|
|
|
return self.state
|
|
|
|
def getselectedfolder(self):
|
|
|
|
if self.getstate() == 'SELECTED':
|
|
|
|
return self.selectedfolder
|
|
|
|
return None
|
|
|
|
|
2003-06-02 23:17:29 +02:00
|
|
|
def select(self, mailbox='INBOX', readonly=None, force = 0):
|
2007-07-04 20:19:06 +02:00
|
|
|
if (not force) and self.getselectedfolder() == mailbox \
|
|
|
|
and self.is_readonly == readonly:
|
2002-07-03 15:04:40 +02:00
|
|
|
# No change; return.
|
|
|
|
return
|
|
|
|
result = self.__class__.__bases__[1].select(self, mailbox, readonly)
|
|
|
|
if result[0] != 'OK':
|
|
|
|
raise ValueError, "Error from select: %s" % str(result)
|
2002-07-10 13:18:07 +02:00
|
|
|
if self.getstate() == 'SELECTED':
|
2002-07-03 15:04:40 +02:00
|
|
|
self.selectedfolder = mailbox
|
|
|
|
else:
|
|
|
|
self.selectedfolder = None
|
|
|
|
|
2009-02-10 01:27:48 +01:00
|
|
|
def _mesg(self, s, tn=None, secs=None):
|
|
|
|
imaplibutil.new_mesg(self, s, tn, secs)
|
2007-07-04 19:51:10 +02:00
|
|
|
|
2009-02-10 01:27:48 +01:00
|
|
|
class UsefulIMAP4(UsefulIMAPMixIn, imaplib2.IMAP4):
|
2008-09-16 23:51:11 +02:00
|
|
|
# This is a hack around Darwin's implementation of realloc() (which
|
|
|
|
# Python uses inside the socket code). On Darwin, we split the
|
|
|
|
# message into 100k chunks, which should be small enough - smaller
|
|
|
|
# might start seriously hurting performance ...
|
|
|
|
|
|
|
|
def read(self, size):
|
|
|
|
if (system() == 'Darwin') and (size>0) :
|
|
|
|
read = 0
|
|
|
|
io = StringIO()
|
|
|
|
while read < size:
|
2009-02-10 01:27:48 +01:00
|
|
|
sz = min(size-read, 8192)
|
|
|
|
data = imaplib2.IMAP4.read (self, sz)
|
2008-09-16 23:51:11 +02:00
|
|
|
read += len(data)
|
|
|
|
io.write(data)
|
2009-02-10 01:27:48 +01:00
|
|
|
if len(data) < sz:
|
|
|
|
break
|
2008-09-16 23:51:11 +02:00
|
|
|
return io.getvalue()
|
|
|
|
else:
|
2009-02-10 01:27:48 +01:00
|
|
|
return imaplib2.IMAP4.read (self, size)
|
2008-09-16 23:51:11 +02:00
|
|
|
|
2008-03-03 15:22:44 +01:00
|
|
|
class UsefulIMAP4_SSL(UsefulIMAPMixIn, imaplibutil.WrappedIMAP4_SSL):
|
2008-09-16 23:51:11 +02:00
|
|
|
# This is the same hack as above, to be used in the case of an SSL
|
|
|
|
# connexion.
|
|
|
|
|
|
|
|
def read(self, size):
|
|
|
|
if (system() == 'Darwin') and (size>0) :
|
|
|
|
read = 0
|
|
|
|
io = StringIO()
|
|
|
|
while read < size:
|
2009-02-10 01:27:48 +01:00
|
|
|
sz = min(size-read,8192)
|
|
|
|
data = imaplibutil.WrappedIMAP4_SSL.read (self, sz)
|
2008-09-16 23:51:11 +02:00
|
|
|
read += len(data)
|
|
|
|
io.write(data)
|
2009-02-10 01:27:48 +01:00
|
|
|
if len(data) < sz:
|
|
|
|
break
|
2008-09-16 23:51:11 +02:00
|
|
|
return io.getvalue()
|
|
|
|
else:
|
|
|
|
return imaplibutil.WrappedIMAP4_SSL.read (self,size)
|
|
|
|
|
2007-07-04 20:17:27 +02:00
|
|
|
class UsefulIMAP4_Tunnel(UsefulIMAPMixIn, imaplibutil.IMAP4_Tunnel): pass
|
2002-06-19 06:39:00 +02:00
|
|
|
|
|
|
|
class IMAPServer:
|
2008-03-09 06:46:51 +01:00
|
|
|
GSS_STATE_STEP = 0
|
|
|
|
GSS_STATE_WRAP = 1
|
2003-04-18 04:18:34 +02:00
|
|
|
def __init__(self, config, reposname,
|
2002-11-05 00:15:42 +01:00
|
|
|
username = None, password = None, hostname = None,
|
2002-07-05 13:46:55 +02:00
|
|
|
port = None, ssl = 1, maxconnections = 1, tunnel = None,
|
2009-02-10 01:27:48 +01:00
|
|
|
reference = '""', sslclientcert = None, sslclientkey = None,
|
|
|
|
idlefolders = []):
|
2003-04-18 04:18:34 +02:00
|
|
|
self.reposname = reposname
|
2002-11-05 00:15:42 +01:00
|
|
|
self.config = config
|
2002-06-19 06:39:00 +02:00
|
|
|
self.username = username
|
|
|
|
self.password = password
|
2002-11-05 00:15:42 +01:00
|
|
|
self.passworderror = None
|
2007-07-05 06:04:14 +02:00
|
|
|
self.goodpassword = None
|
2002-06-19 06:39:00 +02:00
|
|
|
self.hostname = hostname
|
2002-07-05 08:36:34 +02:00
|
|
|
self.tunnel = tunnel
|
2002-06-19 06:39:00 +02:00
|
|
|
self.port = port
|
|
|
|
self.usessl = ssl
|
2008-05-23 21:58:18 +02:00
|
|
|
self.sslclientcert = sslclientcert
|
|
|
|
self.sslclientkey = sslclientkey
|
2002-06-19 07:22:21 +02:00
|
|
|
self.delim = None
|
|
|
|
self.root = None
|
2002-06-19 06:39:00 +02:00
|
|
|
if port == None:
|
|
|
|
if ssl:
|
|
|
|
self.port = 993
|
|
|
|
else:
|
|
|
|
self.port = 143
|
2002-07-03 15:04:40 +02:00
|
|
|
self.maxconnections = maxconnections
|
|
|
|
self.availableconnections = []
|
|
|
|
self.assignedconnections = []
|
2002-07-11 05:51:20 +02:00
|
|
|
self.lastowner = {}
|
2002-07-03 15:04:40 +02:00
|
|
|
self.semaphore = BoundedSemaphore(self.maxconnections)
|
|
|
|
self.connectionlock = Lock()
|
2002-07-05 13:46:55 +02:00
|
|
|
self.reference = reference
|
2009-02-10 01:27:48 +01:00
|
|
|
self.idlefolders = idlefolders
|
2008-03-09 06:46:51 +01:00
|
|
|
self.gss_step = self.GSS_STATE_STEP
|
|
|
|
self.gss_vc = None
|
|
|
|
self.gssapi = False
|
2002-06-19 06:39:00 +02:00
|
|
|
|
2002-11-05 00:15:42 +01:00
|
|
|
def getpassword(self):
|
2007-07-05 06:04:14 +02:00
|
|
|
if self.goodpassword != None:
|
|
|
|
return self.goodpassword
|
|
|
|
|
2002-11-05 00:15:42 +01:00
|
|
|
if self.password != None and self.passworderror == None:
|
|
|
|
return self.password
|
|
|
|
|
2003-04-18 04:18:34 +02:00
|
|
|
self.password = UIBase.getglobalui().getpass(self.reposname,
|
|
|
|
self.config,
|
2002-11-05 00:15:42 +01:00
|
|
|
self.passworderror)
|
|
|
|
self.passworderror = None
|
|
|
|
|
|
|
|
return self.password
|
|
|
|
|
2002-06-19 07:22:21 +02:00
|
|
|
def getdelim(self):
|
|
|
|
"""Returns this server's folder delimiter. Can only be called
|
2002-07-03 15:04:40 +02:00
|
|
|
after one or more calls to acquireconnection."""
|
2002-06-19 07:22:21 +02:00
|
|
|
return self.delim
|
|
|
|
|
|
|
|
def getroot(self):
|
|
|
|
"""Returns this server's folder root. Can only be called after one
|
2002-07-03 15:04:40 +02:00
|
|
|
or more calls to acquireconnection."""
|
2002-06-19 07:22:21 +02:00
|
|
|
return self.root
|
|
|
|
|
2002-07-03 15:04:40 +02:00
|
|
|
|
|
|
|
def releaseconnection(self, connection):
|
2007-07-04 20:34:02 +02:00
|
|
|
"""Releases a connection, returning it to the pool."""
|
2002-07-03 15:04:40 +02:00
|
|
|
self.connectionlock.acquire()
|
|
|
|
self.assignedconnections.remove(connection)
|
|
|
|
self.availableconnections.append(connection)
|
|
|
|
self.connectionlock.release()
|
|
|
|
self.semaphore.release()
|
|
|
|
|
2002-11-02 23:14:02 +01:00
|
|
|
def md5handler(self, response):
|
2002-11-05 08:11:28 +01:00
|
|
|
ui = UIBase.getglobalui()
|
2002-11-02 23:30:41 +01:00
|
|
|
challenge = response.strip()
|
2002-11-05 08:11:28 +01:00
|
|
|
ui.debug('imap', 'md5handler: got challenge %s' % challenge)
|
2002-11-06 02:10:14 +01:00
|
|
|
|
|
|
|
passwd = self.getpassword()
|
|
|
|
retval = self.username + ' ' + hmac.new(passwd, challenge).hexdigest()
|
2002-11-05 08:11:28 +01:00
|
|
|
ui.debug('imap', 'md5handler: returning %s' % retval)
|
2002-11-02 23:14:02 +01:00
|
|
|
return retval
|
|
|
|
|
2002-11-05 00:38:39 +01:00
|
|
|
def plainauth(self, imapobj):
|
|
|
|
UIBase.getglobalui().debug('imap',
|
|
|
|
'Attempting plain authentication')
|
|
|
|
imapobj.login(self.username, self.getpassword())
|
2008-03-09 06:46:51 +01:00
|
|
|
|
|
|
|
def gssauth(self, response):
|
|
|
|
data = base64.b64encode(response)
|
|
|
|
try:
|
|
|
|
if self.gss_step == self.GSS_STATE_STEP:
|
|
|
|
if not self.gss_vc:
|
|
|
|
rc, self.gss_vc = kerberos.authGSSClientInit('imap@' +
|
|
|
|
self.hostname)
|
|
|
|
response = kerberos.authGSSClientResponse(self.gss_vc)
|
|
|
|
rc = kerberos.authGSSClientStep(self.gss_vc, data)
|
|
|
|
if rc != kerberos.AUTH_GSS_CONTINUE:
|
|
|
|
self.gss_step = self.GSS_STATE_WRAP
|
|
|
|
elif self.gss_step == self.GSS_STATE_WRAP:
|
|
|
|
rc = kerberos.authGSSClientUnwrap(self.gss_vc, data)
|
|
|
|
response = kerberos.authGSSClientResponse(self.gss_vc)
|
|
|
|
rc = kerberos.authGSSClientWrap(self.gss_vc, response,
|
|
|
|
self.username)
|
|
|
|
response = kerberos.authGSSClientResponse(self.gss_vc)
|
|
|
|
except kerberos.GSSError, err:
|
|
|
|
# Kerberos errored out on us, respond with None to cancel the
|
|
|
|
# authentication
|
|
|
|
UIBase.getglobalui().debug('imap',
|
|
|
|
'%s: %s' % (err[0][0], err[1][0]))
|
|
|
|
return None
|
|
|
|
|
|
|
|
if not response:
|
|
|
|
response = ''
|
|
|
|
return base64.b64decode(response)
|
2002-11-05 00:38:39 +01:00
|
|
|
|
2002-07-03 15:04:40 +02:00
|
|
|
def acquireconnection(self):
|
|
|
|
"""Fetches a connection from the pool, making sure to create a new one
|
|
|
|
if needed, to obey the maximum connection limits, etc.
|
|
|
|
Opens a connection to the server and returns an appropriate
|
2002-06-19 06:39:00 +02:00
|
|
|
object."""
|
|
|
|
|
2002-07-03 15:04:40 +02:00
|
|
|
self.semaphore.acquire()
|
|
|
|
self.connectionlock.acquire()
|
2002-06-19 06:39:00 +02:00
|
|
|
imapobj = None
|
2002-07-03 15:04:40 +02:00
|
|
|
|
|
|
|
if len(self.availableconnections): # One is available.
|
2002-07-11 05:51:20 +02:00
|
|
|
# Try to find one that previously belonged to this thread
|
|
|
|
# as an optimization. Start from the back since that's where
|
|
|
|
# they're popped on.
|
|
|
|
threadid = thread.get_ident()
|
|
|
|
imapobj = None
|
|
|
|
for i in range(len(self.availableconnections) - 1, -1, -1):
|
|
|
|
tryobj = self.availableconnections[i]
|
|
|
|
if self.lastowner[tryobj] == threadid:
|
|
|
|
imapobj = tryobj
|
|
|
|
del(self.availableconnections[i])
|
|
|
|
break
|
|
|
|
if not imapobj:
|
|
|
|
imapobj = self.availableconnections[0]
|
|
|
|
del(self.availableconnections[0])
|
2002-07-03 15:04:40 +02:00
|
|
|
self.assignedconnections.append(imapobj)
|
2002-07-11 05:51:20 +02:00
|
|
|
self.lastowner[imapobj] = thread.get_ident()
|
2002-07-03 15:04:40 +02:00
|
|
|
self.connectionlock.release()
|
|
|
|
return imapobj
|
|
|
|
|
|
|
|
self.connectionlock.release() # Release until need to modify data
|
|
|
|
|
2002-11-05 00:15:42 +01:00
|
|
|
success = 0
|
|
|
|
while not success:
|
|
|
|
# Generate a new connection.
|
|
|
|
if self.tunnel:
|
2003-04-18 04:18:34 +02:00
|
|
|
UIBase.getglobalui().connecting('tunnel', self.tunnel)
|
2002-11-05 00:15:42 +01:00
|
|
|
imapobj = UsefulIMAP4_Tunnel(self.tunnel)
|
2002-11-20 05:57:45 +01:00
|
|
|
success = 1
|
2002-11-05 00:15:42 +01:00
|
|
|
elif self.usessl:
|
2003-04-18 04:18:34 +02:00
|
|
|
UIBase.getglobalui().connecting(self.hostname, self.port)
|
2008-05-23 21:58:18 +02:00
|
|
|
imapobj = UsefulIMAP4_SSL(self.hostname, self.port,
|
|
|
|
self.sslclientkey, self.sslclientcert)
|
2002-11-02 23:30:41 +01:00
|
|
|
else:
|
2003-04-18 04:18:34 +02:00
|
|
|
UIBase.getglobalui().connecting(self.hostname, self.port)
|
2002-11-05 00:15:42 +01:00
|
|
|
imapobj = UsefulIMAP4(self.hostname, self.port)
|
|
|
|
|
2007-07-04 19:51:10 +02:00
|
|
|
imapobj.mustquote = imaplibutil.mustquote
|
|
|
|
|
2002-11-05 00:15:42 +01:00
|
|
|
if not self.tunnel:
|
|
|
|
try:
|
2008-03-09 06:46:51 +01:00
|
|
|
# Try GSSAPI and continue if it fails
|
|
|
|
if 'AUTH=GSSAPI' in imapobj.capabilities and have_gss:
|
2002-11-05 00:15:42 +01:00
|
|
|
UIBase.getglobalui().debug('imap',
|
2008-03-09 06:46:51 +01:00
|
|
|
'Attempting GSSAPI authentication')
|
2002-11-05 00:38:39 +01:00
|
|
|
try:
|
2008-03-09 06:46:51 +01:00
|
|
|
imapobj.authenticate('GSSAPI', self.gssauth)
|
2002-11-05 00:38:39 +01:00
|
|
|
except imapobj.error, val:
|
2009-06-29 10:53:07 +02:00
|
|
|
self.gssapi = False
|
2008-03-09 06:46:51 +01:00
|
|
|
UIBase.getglobalui().debug('imap',
|
|
|
|
'GSSAPI Authentication failed')
|
|
|
|
else:
|
|
|
|
self.gssapi = True
|
|
|
|
self.password = None
|
|
|
|
|
|
|
|
if not self.gssapi:
|
|
|
|
if 'AUTH=CRAM-MD5' in imapobj.capabilities:
|
|
|
|
UIBase.getglobalui().debug('imap',
|
|
|
|
'Attempting CRAM-MD5 authentication')
|
|
|
|
try:
|
|
|
|
imapobj.authenticate('CRAM-MD5', self.md5handler)
|
|
|
|
except imapobj.error, val:
|
|
|
|
self.plainauth(imapobj)
|
|
|
|
else:
|
2002-11-05 00:38:39 +01:00
|
|
|
self.plainauth(imapobj)
|
2002-11-05 00:15:42 +01:00
|
|
|
# Would bail by here if there was a failure.
|
|
|
|
success = 1
|
2007-07-05 06:04:14 +02:00
|
|
|
self.goodpassword = self.password
|
2002-11-05 00:15:42 +01:00
|
|
|
except imapobj.error, val:
|
|
|
|
self.passworderror = str(val)
|
|
|
|
self.password = None
|
2002-06-19 07:22:21 +02:00
|
|
|
|
|
|
|
if self.delim == None:
|
2002-09-30 22:47:05 +02:00
|
|
|
listres = imapobj.list(self.reference, '""')[1]
|
|
|
|
if listres == [None] or listres == None:
|
|
|
|
# Some buggy IMAP servers do not respond well to LIST "" ""
|
|
|
|
# Work around them.
|
|
|
|
listres = imapobj.list(self.reference, '"*"')[1]
|
2002-06-19 07:22:21 +02:00
|
|
|
self.delim, self.root = \
|
2002-09-30 22:47:05 +02:00
|
|
|
imaputil.imapsplit(listres[0])[1:]
|
2002-06-20 09:40:29 +02:00
|
|
|
self.delim = imaputil.dequote(self.delim)
|
|
|
|
self.root = imaputil.dequote(self.root)
|
2002-06-19 07:22:21 +02:00
|
|
|
|
2002-07-03 15:04:40 +02:00
|
|
|
self.connectionlock.acquire()
|
|
|
|
self.assignedconnections.append(imapobj)
|
2002-07-11 05:51:20 +02:00
|
|
|
self.lastowner[imapobj] = thread.get_ident()
|
2002-07-03 15:04:40 +02:00
|
|
|
self.connectionlock.release()
|
2002-06-19 06:39:00 +02:00
|
|
|
return imapobj
|
|
|
|
|
2002-07-03 15:04:40 +02:00
|
|
|
def connectionwait(self):
|
|
|
|
"""Waits until there is a connection available. Note that between
|
|
|
|
the time that a connection becomes available and the time it is
|
|
|
|
requested, another thread may have grabbed it. This function is
|
|
|
|
mainly present as a way to avoid spawning thousands of threads
|
|
|
|
to copy messages, then have them all wait for 3 available connections.
|
|
|
|
It's OK if we have maxconnections + 1 or 2 threads, which is what
|
|
|
|
this will help us do."""
|
2002-07-04 02:02:10 +02:00
|
|
|
threadutil.semaphorewait(self.semaphore)
|
2002-07-03 15:04:40 +02:00
|
|
|
|
2002-06-21 09:25:24 +02:00
|
|
|
def close(self):
|
2002-07-03 15:04:40 +02:00
|
|
|
# Make sure I own all the semaphores. Let the threads finish
|
|
|
|
# their stuff. This is a blocking method.
|
|
|
|
self.connectionlock.acquire()
|
2002-07-04 02:02:10 +02:00
|
|
|
threadutil.semaphorereset(self.semaphore, self.maxconnections)
|
2002-07-03 15:04:40 +02:00
|
|
|
for imapobj in self.assignedconnections + self.availableconnections:
|
|
|
|
imapobj.logout()
|
|
|
|
self.assignedconnections = []
|
|
|
|
self.availableconnections = []
|
2002-07-11 05:51:20 +02:00
|
|
|
self.lastowner = {}
|
Apply patch to fix autorefresh with Kerberos
Patch from Eric Dorland
Closes: #470875
From: Wouter Verhelst
Subject: kerberos authentication works only the first time
Date: Fri, 14 Mar 2008 09:28:37 +0100
Package: offlineimap
Version: 5.99.8
Severity: normal
Hi,
I have the "autorefresh" configuration option specified in my
.offlineimaprc, and am now using the kerberos authentication.
However, this kerberos authentication seems to work only the first
time
offlineimap tries to fetch mails. The next time, it fails with this
output:
Thread 'Account sync Test' terminated with exception:
Traceback (most recent call last):
File "/var/lib/python-support/python2.4/offlineimap/threadutil.py",
line 153, in run
Thread.run(self)
File "/usr/lib/python2.4/threading.py", line 422, in run
self.__target(*self.__args, **self.__kwargs)
File "/var/lib/python-support/python2.4/offlineimap/accounts.py",
line 119, in syncrunner
self.sync()
File "/var/lib/python-support/python2.4/offlineimap/accounts.py",
line 148, in sync
remoterepos.syncfoldersto(localrepos, [statusrepos])
File
"/var/lib/python-support/python2.4/offlineimap/repository/Base.py",
line 135, in syncfoldersto
srcfolders = src.getfolders()
File
"/var/lib/python-support/python2.4/offlineimap/repository/IMAP.py",
line 192, in getfolders
listresult = imapobj.list(directory =
self.imapserver.reference)[1]
File "/usr/lib/python2.4/imaplib.py", line 469, in list
typ, dat = self._simple_command(name, directory, pattern)
File "/usr/lib/python2.4/imaplib.py", line 1028, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/usr/lib/python2.4/imaplib.py", line 787, in _command
raise self.error(
error: command LIST illegal in state NONAUTH
2009-04-21 07:03:28 +02:00
|
|
|
# reset kerberos state
|
|
|
|
self.gss_step = self.GSS_STATE_STEP
|
|
|
|
self.gss_vc = None
|
|
|
|
self.gssapi = False
|
2002-07-03 15:04:40 +02:00
|
|
|
self.connectionlock.release()
|
|
|
|
|
2002-07-10 13:18:07 +02:00
|
|
|
def keepalive(self, timeout, event):
|
|
|
|
"""Sends a NOOP to each connection recorded. It will wait a maximum
|
|
|
|
of timeout seconds between doing this, and will continue to do so
|
|
|
|
until the Event object as passed is true. This method is expected
|
|
|
|
to be invoked in a separate thread, which should be join()'d after
|
|
|
|
the event is set."""
|
2003-06-02 21:06:18 +02:00
|
|
|
ui = UIBase.getglobalui()
|
|
|
|
ui.debug('imap', 'keepalive thread started')
|
2002-07-10 13:18:07 +02:00
|
|
|
while 1:
|
2003-06-02 21:06:18 +02:00
|
|
|
ui.debug('imap', 'keepalive: top of loop')
|
2002-07-10 13:18:07 +02:00
|
|
|
if event.isSet():
|
2003-06-02 21:06:18 +02:00
|
|
|
ui.debug('imap', 'keepalive: event is set; exiting')
|
2002-07-10 13:18:07 +02:00
|
|
|
return
|
2003-06-02 21:06:18 +02:00
|
|
|
ui.debug('imap', 'keepalive: acquiring connectionlock')
|
2002-07-10 13:18:07 +02:00
|
|
|
self.connectionlock.acquire()
|
|
|
|
numconnections = len(self.assignedconnections) + \
|
|
|
|
len(self.availableconnections)
|
|
|
|
self.connectionlock.release()
|
2003-06-02 21:06:18 +02:00
|
|
|
ui.debug('imap', 'keepalive: connectionlock released')
|
2002-07-10 13:18:07 +02:00
|
|
|
threads = []
|
|
|
|
|
|
|
|
for i in range(numconnections):
|
2003-06-02 21:06:18 +02:00
|
|
|
ui.debug('imap', 'keepalive: processing connection %d of %d' % (i, numconnections))
|
2009-02-10 01:27:48 +01:00
|
|
|
if len(self.idlefolders) > i:
|
|
|
|
idler = IdleThread(self, self.idlefolders[i])
|
|
|
|
else:
|
|
|
|
idler = IdleThread(self)
|
|
|
|
idler.start()
|
|
|
|
threads.append(idler)
|
2003-06-02 21:06:18 +02:00
|
|
|
ui.debug('imap', 'keepalive: thread started')
|
|
|
|
|
2009-02-10 01:27:48 +01:00
|
|
|
ui.debug('imap', 'keepalive: waiting for timeout')
|
|
|
|
event.wait(timeout)
|
|
|
|
|
2003-06-02 21:06:18 +02:00
|
|
|
ui.debug('imap', 'keepalive: joining threads')
|
2002-07-10 13:18:07 +02:00
|
|
|
|
2009-02-10 01:27:48 +01:00
|
|
|
for idler in threads:
|
2002-07-10 13:18:07 +02:00
|
|
|
# Make sure all the commands have completed.
|
2009-02-10 01:27:48 +01:00
|
|
|
idler.stop()
|
|
|
|
idler.join()
|
2002-07-10 13:18:07 +02:00
|
|
|
|
2003-06-02 21:06:18 +02:00
|
|
|
ui.debug('imap', 'keepalive: bottom of loop')
|
|
|
|
|
2009-02-10 01:27:48 +01:00
|
|
|
class IdleThread(object):
|
|
|
|
def __init__(self, parent, folder=None):
|
|
|
|
self.parent = parent
|
|
|
|
self.folder = folder
|
|
|
|
self.event = Event()
|
|
|
|
if folder is None:
|
|
|
|
self.thread = Thread(target=self.noop)
|
|
|
|
else:
|
|
|
|
self.thread = Thread(target=self.idle)
|
|
|
|
self.thread.setDaemon(1)
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
self.thread.start()
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.event.set()
|
|
|
|
|
|
|
|
def join(self):
|
|
|
|
self.thread.join()
|
|
|
|
|
|
|
|
def noop(self):
|
|
|
|
imapobj = self.parent.acquireconnection()
|
2009-02-10 06:19:55 +01:00
|
|
|
self.event.wait()
|
2009-08-12 21:48:38 +02:00
|
|
|
imapobj.noop()
|
2009-02-10 01:27:48 +01:00
|
|
|
self.parent.releaseconnection(imapobj)
|
|
|
|
|
|
|
|
def dosync(self):
|
|
|
|
remoterepos = self.parent.repos
|
|
|
|
account = remoterepos.account
|
|
|
|
localrepos = account.localrepos
|
|
|
|
remoterepos = account.remoterepos
|
|
|
|
statusrepos = account.statusrepos
|
|
|
|
remotefolder = remoterepos.getfolder(self.folder)
|
|
|
|
syncfolder(account.name, remoterepos, remotefolder, localrepos, statusrepos, quick=False)
|
|
|
|
ui = UIBase.getglobalui()
|
|
|
|
ui.unregisterthread(currentThread())
|
|
|
|
|
|
|
|
def idle(self):
|
|
|
|
imapobj = self.parent.acquireconnection()
|
|
|
|
imapobj.select(self.folder)
|
|
|
|
self.parent.releaseconnection(imapobj)
|
|
|
|
while True:
|
|
|
|
if self.event.isSet():
|
|
|
|
return
|
|
|
|
self.needsync = False
|
|
|
|
def callback(args):
|
|
|
|
if not self.event.isSet():
|
|
|
|
self.needsync = True
|
|
|
|
self.event.set()
|
|
|
|
imapobj = self.parent.acquireconnection()
|
2009-08-12 21:48:38 +02:00
|
|
|
imapobj.idle(callback=callback)
|
2009-02-10 01:27:48 +01:00
|
|
|
self.event.wait()
|
|
|
|
if self.event.isSet():
|
|
|
|
imapobj.noop()
|
|
|
|
self.parent.releaseconnection(imapobj)
|
|
|
|
if self.needsync:
|
|
|
|
self.event.clear()
|
|
|
|
self.dosync()
|
|
|
|
|
2002-07-10 13:18:07 +02:00
|
|
|
class ConfigedIMAPServer(IMAPServer):
|
|
|
|
"""This class is designed for easier initialization given a ConfigParser
|
|
|
|
object and an account name. The passwordhash is used if
|
|
|
|
passwords for certain accounts are known. If the password for this
|
|
|
|
account is listed, it will be obtained from there."""
|
2003-04-18 04:18:34 +02:00
|
|
|
def __init__(self, repository, passwordhash = {}):
|
2002-07-10 13:18:07 +02:00
|
|
|
"""Initialize the object. If the account is not a tunnel,
|
|
|
|
the password is required."""
|
2003-04-18 04:18:34 +02:00
|
|
|
self.repos = repository
|
|
|
|
self.config = self.repos.getconfig()
|
|
|
|
usetunnel = self.repos.getpreauthtunnel()
|
|
|
|
if not usetunnel:
|
|
|
|
host = self.repos.gethost()
|
|
|
|
user = self.repos.getuser()
|
|
|
|
port = self.repos.getport()
|
|
|
|
ssl = self.repos.getssl()
|
2008-05-23 21:58:18 +02:00
|
|
|
sslclientcert = self.repos.getsslclientcert()
|
|
|
|
sslclientkey = self.repos.getsslclientkey()
|
2003-04-18 04:18:34 +02:00
|
|
|
reference = self.repos.getreference()
|
2009-02-10 01:27:48 +01:00
|
|
|
idlefolders = self.repos.getidlefolders()
|
2002-07-10 13:18:07 +02:00
|
|
|
server = None
|
|
|
|
password = None
|
2003-04-18 04:18:34 +02:00
|
|
|
|
|
|
|
if repository.getname() in passwordhash:
|
|
|
|
password = passwordhash[repository.getname()]
|
2002-07-10 13:18:07 +02:00
|
|
|
|
|
|
|
# Connect to the remote server.
|
|
|
|
if usetunnel:
|
2003-04-18 04:18:34 +02:00
|
|
|
IMAPServer.__init__(self, self.config, self.repos.getname(),
|
|
|
|
tunnel = usetunnel,
|
2002-07-10 13:18:07 +02:00
|
|
|
reference = reference,
|
2009-02-10 01:27:48 +01:00
|
|
|
idlefolders = idlefolders,
|
2003-04-18 04:18:34 +02:00
|
|
|
maxconnections = self.repos.getmaxconnections())
|
2002-07-10 13:18:07 +02:00
|
|
|
else:
|
|
|
|
if not password:
|
2003-04-18 04:18:34 +02:00
|
|
|
password = self.repos.getpassword()
|
|
|
|
IMAPServer.__init__(self, self.config, self.repos.getname(),
|
2002-11-05 00:15:42 +01:00
|
|
|
user, password, host, port, ssl,
|
2003-04-18 04:18:34 +02:00
|
|
|
self.repos.getmaxconnections(),
|
2008-05-23 21:58:18 +02:00
|
|
|
reference = reference,
|
2009-02-10 01:27:48 +01:00
|
|
|
idlefolders = idlefolders,
|
2008-05-23 21:58:18 +02:00
|
|
|
sslclientcert = sslclientcert,
|
|
|
|
sslclientkey = sslclientkey)
|