промежуточный

sqlitedict
inpos 2016-06-10 21:32:15 +03:00
parent 966ce51781
commit 0a34f252d4
2 changed files with 41 additions and 24 deletions

View File

@ -20,19 +20,18 @@ class IMAPUserAccount(object):
if not os.path.exists(mdir): if not os.path.exists(mdir):
os.makedirs(mdir) os.makedirs(mdir)
self.dir = mdir self.dir = mdir
if self.dir in IMAP_MBOX_REG.keys(): if self.dir in IMAP_MBOX_REG.iterkeys():
IMAP_MBOX_REG[self.dir][IMAP_ACC_CONN_NUM] += 1 IMAP_MBOX_REG[self.dir][IMAP_ACC_CONN_NUM] += 1
else: else:
IMAP_MBOX_REG[self.dir] = {} IMAP_MBOX_REG[self.dir] = {}
IMAP_MBOX_REG[self.dir][IMAP_ACC_CONN_NUM] = 0 IMAP_MBOX_REG[self.dir][IMAP_ACC_CONN_NUM] = 1
for m in conf.imap_auto_mbox.keys(): for m in conf.imap_auto_mbox.keys():
name = m name = m
if isinstance(m, unicode): if isinstance(m, unicode):
m = m.encode('imap4-utf-7') m = m.encode('imap4-utf-7')
if m not in IMAP_MBOX_REG[self.dir].keys(): if m not in IMAP_MBOX_REG[self.dir].iterkeys():
IMAP_MBOX_REG[self.dir][m] = self.create(m) IMAP_MBOX_REG[self.dir][m] = self.create(m)
IMAP_MBOX_REG[self.dir][m].setSpecial(conf.imap_auto_mbox[name]) IMAP_MBOX_REG[self.dir][m].setSpecial(conf.imap_auto_mbox[name])
IMAP_MBOX_REG[self.dir][m]._start_monitor()
self.subscribe(m) self.subscribe(m)
def _getMailbox(self, path): def _getMailbox(self, path):
@ -189,25 +188,27 @@ class IMAPServerProtocol(imap4.IMAP4Server):
def connectionLost(self, reason): def connectionLost(self, reason):
self.setTimeout(None) self.setTimeout(None)
self.transport.loseConnection()
if self.account and self.account.dir in IMAP_MBOX_REG.iterkeys(): if self.account and self.account.dir in IMAP_MBOX_REG.iterkeys():
#IMAP_MBOX_REG[self.account.dir][IMAP_ACC_CONN_NUM] -= 1 IMAP_MBOX_REG[self.account.dir][IMAP_ACC_CONN_NUM] -= 1
#if IMAP_MBOX_REG[self.account.dir][IMAP_ACC_CONN_NUM] <= 0: if IMAP_MBOX_REG[self.account.dir][IMAP_ACC_CONN_NUM] <= 0:
# for m in IMAP_MBOX_REG[self.account.dir].iterkeys(): for m in IMAP_MBOX_REG[self.account.dir].keys():
# if m == IMAP_ACC_CONN_NUM: if m == IMAP_ACC_CONN_NUM:
# continue continue
# IMAP_MBOX_REG[self.account.dir][m].listeners = [] for l in IMAP_MBOX_REG[self.account.dir][m].listeners:
# IMAP_MBOX_REG[self.account.dir][m].close() l.transport.loseConnection()
# del IMAP_MBOX_REG[self.account.dir][m] IMAP_MBOX_REG[self.account.dir][m].removeListener(l)
# del IMAP_MBOX_REG[self.account.dir] IMAP_MBOX_REG[self.account.dir][m].close()
if IMAP_MBOX_REG[self.account.dir][m].closed:
if self.mbox: del IMAP_MBOX_REG[self.account.dir][m]
print('+++ lost connection to %s' % (self.mbox.path,)) if IMAP_MBOX_REG[self.account.dir].keys() == []:
self.mbox.removeListener(self) del IMAP_MBOX_REG[self.account.dir]
self.mbox.close() self.account = None
if self.mbox.closed: def timeoutConnection(self):
del IMAP_MBOX_REG[self.account.dir][self.mbox.path.split('/')[-1]] self.sendLine('* BYE Autologout; connection idle too long')
self.account = None self.connectionLost('timeout')
self.state = 'timeout'
def _parseMbox(self, name): def _parseMbox(self, name):
if isinstance(name, unicode): if isinstance(name, unicode):
return name return name
@ -217,6 +218,24 @@ class IMAPServerProtocol(imap4.IMAP4Server):
#log.err() #log.err()
raise imap4.IllegalMailboxEncoding(name) raise imap4.IllegalMailboxEncoding(name)
def do_CLOSE(self, tag):
d = None
cmbx = imap4.ICloseableMailbox(self.mbox, None)
if cmbx is not None:
d = imap4.maybeDeferred(cmbx.close)
if d is not None:
d.addCallbacks(self.__cbClose, self.__ebClose, (tag,), None, (tag,), None)
else:
self.__cbClose(None, tag)
select_CLOSE = (do_CLOSE,)
def __cbClose(self, result, tag):
self.sendPositiveResponse(tag, 'CLOSE completed')
self.mbox.removeListener(self)
self.mbox = None
self.state = 'auth'
def _cbCopySelectedMailbox(self, mbox, tag, messages, mailbox, uid): def _cbCopySelectedMailbox(self, mbox, tag, messages, mailbox, uid):
if not isinstance(mbox, IMAPMailbox): if not isinstance(mbox, IMAPMailbox):
self.sendNegativeResponse(tag, 'No such mailbox: ' + mailbox) self.sendNegativeResponse(tag, 'No such mailbox: ' + mailbox)

View File

@ -299,9 +299,7 @@ class IMAPMailbox(ExtendedMaildir):
pass pass
def close(self): def close(self):
print('!!! simple closing %s' % (self.path,))
if len(self.listeners) == 0: if len(self.listeners) == 0:
print('!!! Closing %s !!!' % (self.path,))
self._stop_monitor() self._stop_monitor()
if conf.imap_expunge_on_close: if conf.imap_expunge_on_close:
self.expunge() self.expunge()