offlineimap rev 526

"Automatic Subversion Change Mailer" <[email protected]> Sat, 26 Jul 2003 14:37:48 -0500 (CDT)
Newsgroups gmane.mail.imap.offlineimap.subversion
Message-ID <[email protected]>
You are receiving this message because
all commits get sent to this address.

Author: jgoerzen
Date: 2003-07-26 14:37:27 -0500 (Sat, 26 Jul 2003)
New Revision: 526

Modified:
  offlineimap/branches/twisted/offlineimap/imapserver.py

Log:
Work to refactor imapserver


Diff:
Modified: offlineimap/branches/twisted/offlineimap/imapserver.py
==============================================================================
--- offlineimap/branches/twisted/offlineimap/imapserver.py	2003-07-26 16:16:14 UTC (rev 525)
+++ offlineimap/branches/twisted/offlineimap/imapserver.py	2003-07-26 19:37:27 UTC (rev 526)
@@ -16,37 +16,36 @@
 #    along with this program; if not, write to the Free Software
 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-from offlineimap import imaplib, imaputil, threadutil
 from offlineimap.ui import UIBase
 from threading import *
 import thread, hmac, os
+from twisted.internet import defer, protocol, reactor
+from twisted.protocols.imap4 import IMAP4Client
+from twisted.internet.ssl import ClientContextFactory
 
 
-class UsefulIMAPMixIn:
-    def getstate(self):
-        return self.state
-    def getselectedfolder(self):
-        if self.getstate() == 'SELECTED':
-            return self.selectedfolder
-        return None
+class IMAPClientFactory(protocol.ClientFactory):
+    protocol = IMAP4Client
+    def __init__(self, imapserver):
+        self.imapserver = imapserver
+        
+    def startedConnection(self, connector):
+        print "Started to connect"
 
-    def select(self, mailbox='INBOX', readonly=None, force = 0):
-        if (not force) and self.getselectedfolder() == mailbox:
-            self.is_readonly = readonly
-            # 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)
-        if self.getstate() == 'SELECTED':
-            self.selectedfolder = mailbox
-        else:
-            self.selectedfolder = None
+    def clientConnectionLost(self, connector, reason):
+        print "Lost connection:", reason
 
-class UsefulIMAP4(UsefulIMAPMixIn, imaplib.IMAP4): pass
-class UsefulIMAP4_SSL(UsefulIMAPMixIn, imaplib.IMAP4_SSL): pass
-class UsefulIMAP4_Tunnel(UsefulIMAPMixIn, imaplib.IMAP4_Tunnel): pass
+    def clientconnectionFailed(self, connector, reason):
+        print "Connection failed.  Reason:", reason
 
+class MyIMAP(IMAP4Client):
+    def connectionMade(self):
+        print "Connectionmade."
+        self.factory.imapserver.connectionMade(self)
+
+class IMAPFactory(protocol.ClientFactory):
+    protocol = IMAP4Client
+
 class IMAPServer:
     def __init__(self, config, reposname,
                  username = None, password = None, hostname = None,
@@ -72,7 +71,7 @@
         self.availableconnections = []
         self.assignedconnections = []
         self.lastowner = {}
-        self.semaphore = BoundedSemaphore(self.maxconnections)
+        #self.semaphore = BoundedSemaphore(self.maxconnections)
         self.connectionlock = Lock()
         self.reference = reference
 
@@ -99,11 +98,12 @@
 
 
     def releaseconnection(self, connection):
-        self.connectionlock.acquire()
-        self.assignedconnections.remove(connection)
-        self.availableconnections.append(connection)
-        self.connectionlock.release()
-        self.semaphore.release()
+        #self.connectionlock.acquire()
+        #self.assignedconnections.remove(connection)
+        #self.availableconnections.append(connection)
+        #self.connectionlock.release()
+        #self.semaphore.release()
+        pass
 
     def md5handler(self, response):
         ui = UIBase.getglobalui()
@@ -118,7 +118,9 @@
     def plainauth(self, imapobj):
         UIBase.getglobalui().debug('imap',
                                    'Attempting plain authentication')
-        imapobj.login(self.username, self.getpassword())
+        d = imapobj.login(self.username, self.getpassword())
+        d.addCallback(self.handOverConnection)
+        d.addErrback(self.plainauth, (imapobj,))
         
 
     def acquireconnection(self):
@@ -127,6 +129,35 @@
         Opens a connection to the server and returns an appropriate
         object."""
 
+        print "Acquireconnection called."
+        # FIXME: stub for now.
+        d = defer.Deferred()
+        if hasattr(self, 'imapobj'):
+            print "Acquireconnection returning existing object."
+            d.callback(self.imapobj)
+            return d
+
+        if self.usessl:
+            reactor.connectSSL(self.hostname, self.port,
+                               IMAPClientFactory(self),
+                               ClientContextFactory())
+        else:
+            reactor.connectTCP(self.hostname, self.port,
+                               IMAPClientFactory(self))
+            
+        self.connectiondefer = d
+        return d
+
+    def connectionMade(self, protocol):
+        print "This is IMAPServer.connectionMade; handing over to plainauth"
+        self.imapobj = protocol
+        self.plainAuth(protocol)
+
+    def handOverConnection(self):
+        print "This is IMAPServer.handOverConnection"
+        self.connectiondefer.callback(self.imapobj)
+        return
+        
         self.semaphore.acquire()
         self.connectionlock.acquire()
         imapobj = None