From 0a34f252d40677a0ef346e47f759921a813c5941 Mon Sep 17 00:00:00 2001 From: inpos Date: Fri, 10 Jun 2016 21:32:15 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D1=8B=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mech_imap.py | 63 +++++++++++++++++++++++++++-------------- serpent/imap/mailbox.py | 2 -- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/mech_imap.py b/mech_imap.py index 51b2069..2426d11 100644 --- a/mech_imap.py +++ b/mech_imap.py @@ -20,19 +20,18 @@ class IMAPUserAccount(object): if not os.path.exists(mdir): os.makedirs(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 else: 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(): name = m if isinstance(m, unicode): 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].setSpecial(conf.imap_auto_mbox[name]) - IMAP_MBOX_REG[self.dir][m]._start_monitor() self.subscribe(m) def _getMailbox(self, path): @@ -189,25 +188,27 @@ class IMAPServerProtocol(imap4.IMAP4Server): def connectionLost(self, reason): self.setTimeout(None) + self.transport.loseConnection() if self.account and self.account.dir in IMAP_MBOX_REG.iterkeys(): - #IMAP_MBOX_REG[self.account.dir][IMAP_ACC_CONN_NUM] -= 1 - #if IMAP_MBOX_REG[self.account.dir][IMAP_ACC_CONN_NUM] <= 0: - # for m in IMAP_MBOX_REG[self.account.dir].iterkeys(): - # if m == IMAP_ACC_CONN_NUM: - # continue - # IMAP_MBOX_REG[self.account.dir][m].listeners = [] - # IMAP_MBOX_REG[self.account.dir][m].close() - # del IMAP_MBOX_REG[self.account.dir][m] - # del IMAP_MBOX_REG[self.account.dir] - - if self.mbox: - print('+++ lost connection to %s' % (self.mbox.path,)) - self.mbox.removeListener(self) - self.mbox.close() - if self.mbox.closed: - del IMAP_MBOX_REG[self.account.dir][self.mbox.path.split('/')[-1]] - self.account = None - + IMAP_MBOX_REG[self.account.dir][IMAP_ACC_CONN_NUM] -= 1 + if IMAP_MBOX_REG[self.account.dir][IMAP_ACC_CONN_NUM] <= 0: + for m in IMAP_MBOX_REG[self.account.dir].keys(): + if m == IMAP_ACC_CONN_NUM: + continue + for l in IMAP_MBOX_REG[self.account.dir][m].listeners: + l.transport.loseConnection() + IMAP_MBOX_REG[self.account.dir][m].removeListener(l) + IMAP_MBOX_REG[self.account.dir][m].close() + if IMAP_MBOX_REG[self.account.dir][m].closed: + del IMAP_MBOX_REG[self.account.dir][m] + if IMAP_MBOX_REG[self.account.dir].keys() == []: + del IMAP_MBOX_REG[self.account.dir] + self.account = None + def timeoutConnection(self): + self.sendLine('* BYE Autologout; connection idle too long') + self.connectionLost('timeout') + self.state = 'timeout' + def _parseMbox(self, name): if isinstance(name, unicode): return name @@ -217,6 +218,24 @@ class IMAPServerProtocol(imap4.IMAP4Server): #log.err() 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): if not isinstance(mbox, IMAPMailbox): self.sendNegativeResponse(tag, 'No such mailbox: ' + mailbox) diff --git a/serpent/imap/mailbox.py b/serpent/imap/mailbox.py index 17c1de5..3fdbc04 100644 --- a/serpent/imap/mailbox.py +++ b/serpent/imap/mailbox.py @@ -299,9 +299,7 @@ class IMAPMailbox(ExtendedMaildir): pass def close(self): - print('!!! simple closing %s' % (self.path,)) if len(self.listeners) == 0: - print('!!! Closing %s !!!' % (self.path,)) self._stop_monitor() if conf.imap_expunge_on_close: self.expunge()