offlineimap rev 525

"Automatic Subversion Change Mailer" <[email protected]> Sat, 26 Jul 2003 11:16:25 -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 11:16:14 -0500 (Sat, 26 Jul 2003)
New Revision: 525

Modified:
  offlineimap/branches/twisted/offlineimap/accounts.py
  offlineimap/branches/twisted/offlineimap/imaplib.py
  offlineimap/branches/twisted/offlineimap/init.py
  offlineimap/branches/twisted/offlineimap/syncmaster.py
  offlineimap/branches/twisted/offlineimap/threadutil.py

Log:
Checkpointing work modifying things for Twisted


Diff:
Modified: offlineimap/branches/twisted/offlineimap/accounts.py
==============================================================================
--- offlineimap/branches/twisted/offlineimap/accounts.py	2003-07-26 04:20:57 UTC (rev 524)
+++ offlineimap/branches/twisted/offlineimap/accounts.py	2003-07-26 16:16:14 UTC (rev 525)
@@ -18,10 +18,10 @@
 from offlineimap import threadutil, mbnames, CustomConfig
 import offlineimap.repository.Base, offlineimap.repository.LocalStatus
 from offlineimap.ui import UIBase
-from offlineimap.threadutil import InstanceLimitedThread, ExitNotifyThread
-from threading import Event
 import os
+from twisted.internet import defer
 
+
 def getaccountlist(customconfig):
     return customconfig.getsectionlist('Account')
 
@@ -122,112 +122,178 @@
     def getaccountmeta(self):
         return os.path.join(self.metadatadir, 'Account-' + self.name)
 
+    
+
     def sync(self):
         # We don't need an account lock because syncitall() goes through
         # each account once, then waits for all to finish.
-        try:
-            remoterepos = self.remoterepos
-            localrepos = self.localrepos
-            statusrepos = self.statusrepos
-            self.ui.syncfolders(remoterepos, localrepos)
-            remoterepos.syncfoldersto(localrepos)
+        remoterepos = self.remoterepos
+        localrepos = self.localrepos
+        statusrepos = self.statusrepos
+        self.ui.syncfolders(self.remoterepos, self.localrepos)
+        d = self.remoterepos.syncfoldersto(self.localrepos)
+        d.addCallback(self.sync_getfolders)
+        d.addCallback(self.sync_runfolders)
 
-            folderthreads = []
-            for remotefolder in remoterepos.getfolders():
-                thread = InstanceLimitedThread(\
-                    instancename = 'FOLDER_' + self.remoterepos.getname(),
-                    target = syncfolder,
-                    name = "Folder sync %s[%s]" % \
-                    (self.name, remotefolder.getvisiblename()),
-                    args = (self.name, remoterepos, remotefolder, localrepos,
-                            statusrepos))
-                thread.setDaemon(1)
-                thread.start()
-                folderthreads.append(thread)
-            threadutil.threadsreset(folderthreads)
-            mbnames.write()
-            localrepos.holdordropconnections()
-            remoterepos.holdordropconnections()
-        finally:
-            pass
+    def sync_getfolders(self, d):
+        return remoterepos.getfolders()
+
+    def sync_runfolders(self, remotefolders):
+        dl = defer.DeferredList()
+        for remotefolder in remotefolders:
+            sf = SyncFolder(self.name, remoterepos, remotefolder, localrepos,
+                            statusrepos)
+            d = sf.start()
+            dl.addDeferred(d)
+        dl.addCallback(self.sync_finish)
+
+    def sync_finish(self, d):
+        mbnames.write()
+        #localrepos.holdordropconnections()
+        #remoterepos.holdordropconnections()
     
 class SyncableAccount(Account, AccountSynchronizationMixin):
     pass
 
-def syncfolder(accountname, remoterepos, remotefolder, localrepos,
+class SyncFolder:
+    def __init__(self, accountname, remoterepos, remotefolder, localrepos,
                statusrepos):
-    global mailboxes
-    ui = UIBase.getglobalui()
-    ui.registerthread(accountname)
-    # Load local folder.
-    localfolder = localrepos.\
-                  getfolder(remotefolder.getvisiblename().\
-                            replace(remoterepos.getsep(), localrepos.getsep()))
-    # Write the mailboxes
-    mbnames.add(accountname, localfolder.getvisiblename())
-    # Load local folder
-    ui.syncingfolder(remoterepos, remotefolder, localrepos, localfolder)
-    ui.loadmessagelist(localrepos, localfolder)
-    localfolder.cachemessagelist()
-    ui.messagelistloaded(localrepos, localfolder, len(localfolder.getmessagelist().keys()))
+        self.accountname = accountname
+        self.remoterepos = remoterepos
+        self.remotefolder = remotefolder
+        self.localrepos = localrepos
+        self.statusrepos = statusrepos
+        self.ui = UIBase.getglobalui()
 
+    def start(self):
+        self.ui.registerthread(self.accountname)
+        d = localrepos.\
+            getfolder(remotefolder.getvisiblename().\
+                      replace(remoterepos.getsep(), localrepos.getsep()))
+        d.addCallback(self.local_cache)
+        d.addCallback(self.status_load)
+        d.addCallback(self.status_cache)
+        d.addCallback(self.load_validities)
+        d.addCallback(self.check_validities)
+        d.addCallback(self.check_local_deleted)
+        d.addCallback(self.sync_remote_changes)
+        d.addCallback(self.finish)
+        return d
 
-    # Load status folder.
-    statusfolder = statusrepos.getfolder(remotefolder.getvisiblename().\
-                                         replace(remoterepos.getsep(),
-                                                 statusrepos.getsep()))
-    if localfolder.getuidvalidity() == None:
-        # This is a new folder, so delete the status cache to be sure
-        # we don't have a conflict.
-        statusfolder.deletemessagelist()
+    def local_cache(self, d):
+        self.localfolder = d
+        # Load local folder.
+        # Write the mailboxes
+        mbnames.add(accountname, self.localfolder.getvisiblename())
+        # Load local folder
+        ui.syncingfolder(self.remoterepos, self.remotefolder,
+                         self.localrepos, self.localfolder)
+        ui.loadmessagelist(self.localrepos, self.localfolder)
+        return self.localfolder.cachemessagelist()
+
+    def status_load(self, d):
+        ui.messagelistloaded(self.localrepos, self.localfolder,
+                             len(self.localfolder.getmessagelist().keys()))
+        # Load status folder.
+        #statusfolder =
+        return self.statusrepos.getfolder(self.remotefolder.getvisiblename().\
+                                          replace(self.remoterepos.getsep(),
+                                                  self.statusrepos.getsep()))
+    
+    def status_cache(self, d):
+        self.statusfolder = d
         
-    statusfolder.cachemessagelist()
+        if self.localfolder.getuidvalidity() == None:
+            # This is a new folder, so delete the status cache to be sure
+            # we don't have a conflict.
+            self.statusfolder.deletemessagelist()
+        
+        return self.statusfolder.cachemessagelist()
 
-    # If either the local or the status folder has messages and there is a UID
-    # validity problem, warn and abort.  If there are no messages, UW IMAPd
-    # loses UIDVALIDITY.  But we don't really need it if both local folders are
-    # empty.  So, in that case, just save it off.
-    if len(localfolder.getmessagelist()) or len(statusfolder.getmessagelist()):
-        if not localfolder.isuidvalidityok():
-            ui.validityproblem(localfolder, localfolder.getsaveduidvalidity(),
-                               localfolder.getuidvalidity())
-            return
-        if not remotefolder.isuidvalidityok():
-            ui.validityproblem(remotefolder, remotefolder.getsaveduidvalidity(),
-                               remotefolder.getuidvalidity())
-            return
-    else:
-        localfolder.saveuidvalidity()
-        remotefolder.saveuidvalidity()
+    def load_validities(self, d):
+        self.validities = {}
 
-    # Load remote folder.
-    ui.loadmessagelist(remoterepos, remotefolder)
-    remotefolder.cachemessagelist()
-    ui.messagelistloaded(remoterepos, remotefolder,
-                         len(remotefolder.getmessagelist().keys()))
+        dl = defer.DeferredList()
 
+        for folder, name in ((self.localfolder, 'local'),
+                             (self.remotefolder, 'remote')):
+            d = folder.getuidvalidity()
+            d.addCallback(self._set_validities, ('%s_uidvalidity' % name))
+            dl.addDeferred(d)
 
-    #
+            d = folder.isuidvalidityok()
+            d.addCallback(self._set_validities, ('%s_isuidvalidityok' % name))
+            dl.addDeferred(d)
+        return dl
 
-    if not statusfolder.isnewfolder():
-        # Delete local copies of remote messages.  This way,
-        # if a message's flag is modified locally but it has been
-        # deleted remotely, we'll delete it locally.  Otherwise, we
-        # try to modify a deleted message's flags!  This step
-        # need only be taken if a statusfolder is present; otherwise,
-        # there is no action taken *to* the remote repository.
+    def _set_validities(self, name, value):
+        self.validities[name] = value
 
-        remotefolder.syncmessagesto_delete(localfolder, [localfolder,
-                                                         statusfolder])
-        ui.syncingmessages(localrepos, localfolder, remoterepos, remotefolder)
-        localfolder.syncmessagesto(statusfolder, [remotefolder, statusfolder])
+    def check_validities(self, d):
+        # If either the local or the status folder has messages and
+        # there is a UID validity problem, warn and abort.  If there
+        # are no messages, UW IMAPd loses UIDVALIDITY.  But we don't
+        # really need it if both local folders are empty.  So, in that
+        # case, just save it off.
+        
+        if len(self.localfolder.getmessagelist()) or \
+               len(self.statusfolder.getmessagelist()):
+            if not self.validities['localfolder_isuidvalidityok']:
+                ui.validityproblem(self.localfolder,
+                                   self.localfolder.getsaveduidvalidity(),
+                                   self.validities['localfolder_uidvalidity'])
+                raise Exception, "Error: UID validity problem for local folder."
+            if not self.validities['remotefolder_isuidvalidityok']:
+                ui.validityproblem(self.remotefolder,
+                                   self.remotefolder.getsaveduidvalidity(),
+                                   self.validities['remotefolder_uidvalidity'])
+                rause Exception, "Error: UID validity problem for remote folder."
+        else:
+            self.localfolder.saveuidvalidity()
+            self.remotefolder.saveuidvalidity()
 
-    # Synchronize remote changes.
-    ui.syncingmessages(remoterepos, remotefolder, localrepos, localfolder)
-    remotefolder.syncmessagesto(localfolder, [localfolder, statusfolder])
+        # Load remote folder.
+        ui.loadmessagelist(self.remoterepos, self.remotefolder)
+        return self.remotefolder.cachemessagelist()
 
-    # Make sure the status folder is up-to-date.
-    ui.syncingmessages(localrepos, localfolder, statusrepos, statusfolder)
-    localfolder.syncmessagesto(statusfolder)
-    statusfolder.save()
 
+    def check_local_deleted(self, d):
+        ui.messagelistloaded(self.remoterepos, self.remotefolder,
+                             len(self.remotefolder.getmessagelist().keys()))
+
+
+
+        if not self.statusfolder.isnewfolder():
+            # Delete local copies of remote messages.  This way,
+            # if a message's flag is modified locally but it has been
+            # deleted remotely, we'll delete it locally.  Otherwise, we
+            # try to modify a deleted message's flags!  This step
+            # need only be taken if a statusfolder is present; otherwise,
+            # there is no action taken *to* the remote repository.
+            d = self.remotefolder.syncmessagesto_delete(self.localfolder,
+                                                    [self.localfolder,
+                                                     self.statusfolder])
+            # Now sync up the local folder.
+            d.addCallback(self.localfolder.syncmessagesto,
+                          (statusfolder, [remotefolder, statusfolder]))
+            #ui.syncingmessages(localrepos, localfolder, remoterepos, remotefolder)
+            return d
+        
+            #localfolder.syncmessagesto(statusfolder, [remotefolder, statusfolder])
+            
+    def sync_remote_changes(self, d):
+        # Synchronize remote changes.
+        ui.syncingmessages(self.remoterepos, self.remotefolder,
+                           self.localrepos, self.localfolder)
+        return remotefolder.syncmessagesto(localfolder,
+                                           [localfolder, statusfolder])
+
+    def sync_status(self, d):
+        # Make sure the status folder is up-to-date.
+        ui.syncingmessages(self.localrepos, self.localfolder,
+                           self.statusrepos, self.statusfolder)
+        return self.localfolder.syncmessagesto(self.statusfolder)
+
+    def finish(self, d):
+        self.statusfolder.save()
+

Deleted: offlineimap/branches/twisted/offlineimap/imaplib.py
==============================================================================
--- offlineimap/branches/twisted/offlineimap/imaplib.py	2003-07-26 04:20:57 UTC (rev 524)
+++ offlineimap/branches/twisted/offlineimap/imaplib.py	2003-07-26 16:16:14 UTC (rev 525)
@@ -1,1413 +0,0 @@
-"""IMAP4 client.
-
-Based on RFC 2060.
-
-Public class:           IMAP4
-Public variable:        Debug
-Public functions:       Internaldate2tuple
-                        Int2AP
-                        ParseFlags
-                        Time2Internaldate
-"""
-
-# Author: Piers Lauder <[email protected]> December 1997.
-#
-# Authentication code contributed by Donn Cave <[email protected]> June 1998.
-# String method conversion by ESR, February 2001.
-# GET/SETACL contributed by Anthony Baxter <[email protected]> April 2001.
-# IMAP4_SSL contributed by Tino Lange <[email protected]> March 2002.
-# GET/SETQUOTA contributed by Andreas Zeidler <[email protected]> June 2002.
-# IMAP4_Tunnel contributed by John Goerzen <[email protected]> July 2002
-
-__version__ = "2.52"
-
-import binascii, re, socket, time, random, sys, os
-from offlineimap.ui import UIBase
-
-__all__ = ["IMAP4", "Internaldate2tuple",
-           "Int2AP", "ParseFlags", "Time2Internaldate"]
-
-#       Globals
-
-CRLF = '\r\n'
-Debug = 0
-IMAP4_PORT = 143
-IMAP4_SSL_PORT = 993
-AllowedVersions = ('IMAP4REV1', 'IMAP4')        # Most recent first
-
-#       Commands
-
-Commands = {
-        # name            valid states
-        'APPEND':       ('AUTH', 'SELECTED'),
-        'AUTHENTICATE': ('NONAUTH',),
-        'CAPABILITY':   ('NONAUTH', 'AUTH', 'SELECTED', 'LOGOUT'),
-        'CHECK':        ('SELECTED',),
-        'CLOSE':        ('SELECTED',),
-        'COPY':         ('SELECTED',),
-        'CREATE':       ('AUTH', 'SELECTED'),
-        'DELETE':       ('AUTH', 'SELECTED'),
-        'EXAMINE':      ('AUTH', 'SELECTED'),
-        'EXPUNGE':      ('SELECTED',),
-        'FETCH':        ('SELECTED',),
-        'GETACL':       ('AUTH', 'SELECTED'),
-        'GETQUOTA':     ('AUTH', 'SELECTED'),
-        'GETQUOTAROOT': ('AUTH', 'SELECTED'),
-        'LIST':         ('AUTH', 'SELECTED'),
-        'LOGIN':        ('NONAUTH',),
-        'LOGOUT':       ('NONAUTH', 'AUTH', 'SELECTED', 'LOGOUT'),
-        'LSUB':         ('AUTH', 'SELECTED'),
-        'NAMESPACE':    ('AUTH', 'SELECTED'),
-        'NOOP':         ('NONAUTH', 'AUTH', 'SELECTED', 'LOGOUT'),
-        'PARTIAL':      ('SELECTED',),                                  # NB: obsolete
-        'RENAME':       ('AUTH', 'SELECTED'),
-        'SEARCH':       ('SELECTED',),
-        'SELECT':       ('AUTH', 'SELECTED'),
-        'SETACL':       ('AUTH', 'SELECTED'),
-        'SETQUOTA':     ('AUTH', 'SELECTED'),
-        'SORT':         ('SELECTED',),
-        'STATUS':       ('AUTH', 'SELECTED'),
-        'STORE':        ('SELECTED',),
-        'SUBSCRIBE':    ('AUTH', 'SELECTED'),
-        'UID':          ('SELECTED',),
-        'UNSUBSCRIBE':  ('AUTH', 'SELECTED'),
-        }
-
-#       Patterns to match server responses
-
-Continuation = re.compile(r'\+( (?P<data>.*))?')
-Flags = re.compile(r'.*FLAGS \((?P<flags>[^\)]*)\)')
-InternalDate = re.compile(r'.*INTERNALDATE "'
-        r'(?P<day>[ 123][0-9])-(?P<mon>[A-Z][a-z][a-z])-(?P<year>[0-9][0-9][0-9][0-9])'
-        r' (?P<hour>[0-9][0-9]):(?P<min>[0-9][0-9]):(?P<sec>[0-9][0-9])'
-        r' (?P<zonen>[-+])(?P<zoneh>[0-9][0-9])(?P<zonem>[0-9][0-9])'
-        r'"')
-Literal = re.compile(r'.*{(?P<size>\d+)}$')
-Response_code = re.compile(r'\[(?P<type>[A-Z-]+)( (?P<data>[^\]]*))?\]')
-Untagged_response = re.compile(r'\* (?P<type>[A-Z-]+)( (?P<data>.*))?')
-Untagged_status = re.compile(r'\* (?P<data>\d+) (?P<type>[A-Z-]+)( (?P<data2>.*))?')
-
-
-
-class IMAP4:
-
-    """IMAP4 client class.
-
-    Instantiate with: IMAP4([host[, port]])
-
-            host - host's name (default: localhost);
-            port - port number (default: standard IMAP4 port).
-
-    All IMAP4rev1 commands are supported by methods of the same
-    name (in lower-case).
-
-    All arguments to commands are converted to strings, except for
-    AUTHENTICATE, and the last argument to APPEND which is passed as
-    an IMAP4 literal.  If necessary (the string contains any
-    non-printing characters or white-space and isn't enclosed with
-    either parentheses or double quotes) each string is quoted.
-    However, the 'password' argument to the LOGIN command is always
-    quoted.  If you want to avoid having an argument string quoted
-    (eg: the 'flags' argument to STORE) then enclose the string in
-    parentheses (eg: "(\Deleted)").
-
-    Each command returns a tuple: (type, [data, ...]) where 'type'
-    is usually 'OK' or 'NO', and 'data' is either the text from the
-    tagged response, or untagged results from command.
-
-    Errors raise the exception class <instance>.error("<reason>").
-    IMAP4 server errors raise <instance>.abort("<reason>"),
-    which is a sub-class of 'error'. Mailbox status changes
-    from READ-WRITE to READ-ONLY raise the exception class
-    <instance>.readonly("<reason>"), which is a sub-class of 'abort'.
-
-    "error" exceptions imply a program error.
-    "abort" exceptions imply the connection should be reset, and
-            the command re-tried.
-    "readonly" exceptions imply the command should be re-tried.
-
-    Note: to use this module, you must read the RFCs pertaining
-    to the IMAP4 protocol, as the semantics of the arguments to
-    each IMAP4 command are left to the invoker, not to mention
-    the results.
-    """
-
-    class error(Exception): pass    # Logical errors - debug required
-    class abort(error): pass        # Service errors - close and retry
-    class readonly(abort): pass     # Mailbox status changed to READ-ONLY
-
-    mustquote = re.compile(r"[^\w!#$%&'*+,.:;<=>?^`|~-]")
-
-    def __init__(self, host = '', port = IMAP4_PORT):
-        self.debug = Debug
-        self.state = 'LOGOUT'
-        self.literal = None             # A literal argument to a command
-        self.tagged_commands = {}       # Tagged commands awaiting response
-        self.untagged_responses = {}    # {typ: [data, ...], ...}
-        self.continuation_response = '' # Last continuation response
-        self.is_readonly = None         # READ-ONLY desired state
-        self.tagnum = 0
-
-        # Open socket to server.
-
-        self.open(host, port)
-
-        # Create unique tag for this session,
-        # and compile tagged response matcher.
-
-        self.tagpre = Int2AP(random.randint(0, 31999))
-        self.tagre = re.compile(r'(?P<tag>'
-                        + self.tagpre
-                        + r'\d+) (?P<type>[A-Z]+) (?P<data>.*)')
-
-        # Get server welcome message,
-        # request and store CAPABILITY response.
-
-        if __debug__:
-            self._cmd_log_len = 10
-            self._cmd_log_idx = 0
-            self._cmd_log = {}           # Last `_cmd_log_len' interactions
-            if self.debug >= 1:
-                self._mesg('imaplib version %s' % __version__)
-                self._mesg('new IMAP4 connection, tag=%s' % self.tagpre)
-
-        self.welcome = self._get_response()
-        if 'PREAUTH' in self.untagged_responses:
-            self.state = 'AUTH'
-        elif 'OK' in self.untagged_responses:
-            self.state = 'NONAUTH'
-        else:
-            raise self.error(self.welcome)
-
-        cap = 'CAPABILITY'
-        self._simple_command(cap)
-        if not cap in self.untagged_responses:
-            raise self.error('no CAPABILITY response from server')
-        self.capabilities = tuple(self.untagged_responses[cap][-1].upper().split())
-
-        if __debug__:
-            if self.debug >= 3:
-                self._mesg('CAPABILITIES: %s' % `self.capabilities`)
-
-        for version in AllowedVersions:
-            if not version in self.capabilities:
-                continue
-            self.PROTOCOL_VERSION = version
-            return
-
-        raise self.error('server not IMAP4 compliant')
-
-
-    def __getattr__(self, attr):
-        #       Allow UPPERCASE variants of IMAP4 command methods.
-        if attr in Commands:
-            return getattr(self, attr.lower())
-        raise AttributeError("Unknown IMAP4 command: '%s'" % attr)
-
-
-
-    #       Overridable methods
-
-
-    def open(self, host = '', port = IMAP4_PORT):
-        """Setup connection to remote server on "host:port"
-            (default: localhost:standard IMAP4 port).
-        This connection will be used by the routines:
-            read, readline, send, shutdown.
-        """
-        self.host = host
-        self.port = port
-        #This connects to the first ip found ipv4/ipv6
-        #Added by Adriaan Peeters <[email protected]> based on a socket
-        #example from the python documentation:
-        #http://www.python.org/doc/lib/socket-example.html
-        res = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
-                                 socket.SOCK_STREAM)
-        af, socktype, proto, canonname, sa = res[0]
-        self.sock = socket.socket(af, socktype, proto)
-        self.sock.connect(sa)
-        
-        self.file = self.sock.makefile('rb')
-
-
-    def read(self, size):
-        """Read 'size' bytes from remote."""
-        retval = ''
-        while len(retval) < size:
-            retval += self.file.read(size - len(retval))
-        return retval
-
-    def readline(self):
-        """Read line from remote."""
-        return self.file.readline()
-
-
-    def send(self, data):
-        """Send data to remote."""
-        self.sock.sendall(data)
-
-
-    def shutdown(self):
-        """Close I/O established in "open"."""
-        self.file.close()
-        self.sock.close()
-
-
-    def socket(self):
-        """Return socket instance used to connect to IMAP4 server.
-
-        socket = <instance>.socket()
-        """
-        return self.sock
-
-
-
-    #       Utility methods
-
-
-    def recent(self):
-        """Return most recent 'RECENT' responses if any exist,
-        else prompt server for an update using the 'NOOP' command.
-
-        (typ, [data]) = <instance>.recent()
-
-        'data' is None if no new messages,
-        else list of RECENT responses, most recent last.
-        """
-        name = 'RECENT'
-        typ, dat = self._untagged_response('OK', [None], name)
-        if dat[-1]:
-            return typ, dat
-        typ, dat = self.noop()  # Prod server for response
-        return self._untagged_response(typ, dat, name)
-
-
-    def response(self, code):
-        """Return data for response 'code' if received, or None.
-
-        Old value for response 'code' is cleared.
-
-        (code, [data]) = <instance>.response(code)
-        """
-        return self._untagged_response(code, [None], code.upper())
-
-
-
-    #       IMAP4 commands
-
-
-    def append(self, mailbox, flags, date_time, message):
-        """Append message to named mailbox.
-
-        (typ, [data]) = <instance>.append(mailbox, flags, date_time, message)
-
-                All args except `message' can be None.
-        """
-        name = 'APPEND'
-        if not mailbox:
-            mailbox = 'INBOX'
-        if flags:
-            if (flags[0],flags[-1]) != ('(',')'):
-                flags = '(%s)' % flags
-        else:
-            flags = None
-        if date_time:
-            date_time = Time2Internaldate(date_time)
-        else:
-            date_time = None
-        self.literal = message
-        return self._simple_command(name, mailbox, flags, date_time)
-
-
-    def authenticate(self, mechanism, authobject):
-        """Authenticate command - requires response processing.
-
-        'mechanism' specifies which authentication mechanism is to
-        be used - it must appear in <instance>.capabilities in the
-        form AUTH=<mechanism>.
-
-        'authobject' must be a callable object:
-
-                data = authobject(response)
-
-        It will be called to process server continuation responses.
-        It should return data that will be encoded and sent to server.
-        It should return None if the client abort response '*' should
-        be sent instead.
-        """
-        mech = mechanism.upper()
-        cap = 'AUTH=%s' % mech
-        if not cap in self.capabilities:
-            raise self.error("Server doesn't allow %s authentication." % mech)
-        self.literal = _Authenticator(authobject).process
-        typ, dat = self._simple_command('AUTHENTICATE', mech)
-        if typ != 'OK':
-            raise self.error(dat[-1])
-        self.state = 'AUTH'
-        return typ, dat
-
-
-    def check(self):
-        """Checkpoint mailbox on server.
-
-        (typ, [data]) = <instance>.check()
-        """
-        return self._simple_command('CHECK')
-
-
-    def close(self):
-        """Close currently selected mailbox.
-
-        Deleted messages are removed from writable mailbox.
-        This is the recommended command before 'LOGOUT'.
-
-        (typ, [data]) = <instance>.close()
-        """
-        try:
-            typ, dat = self._simple_command('CLOSE')
-        finally:
-            self.state = 'AUTH'
-        return typ, dat
-
-
-    def copy(self, message_set, new_mailbox):
-        """Copy 'message_set' messages onto end of 'new_mailbox'.
-
-        (typ, [data]) = <instance>.copy(message_set, new_mailbox)
-        """
-        return self._simple_command('COPY', message_set, new_mailbox)
-
-
-    def create(self, mailbox):
-        """Create new mailbox.
-
-        (typ, [data]) = <instance>.create(mailbox)
-        """
-        return self._simple_command('CREATE', mailbox)
-
-
-    def delete(self, mailbox):
-        """Delete old mailbox.
-
-        (typ, [data]) = <instance>.delete(mailbox)
-        """
-        return self._simple_command('DELETE', mailbox)
-
-
-    def expunge(self):
-        """Permanently remove deleted items from selected mailbox.
-
-        Generates 'EXPUNGE' response for each deleted message.
-
-        (typ, [data]) = <instance>.expunge()
-
-        'data' is list of 'EXPUNGE'd message numbers in order received.
-        """
-        name = 'EXPUNGE'
-        typ, dat = self._simple_command(name)
-        return self._untagged_response(typ, dat, name)
-
-
-    def fetch(self, message_set, message_parts):
-        """Fetch (parts of) messages.
-
-        (typ, [data, ...]) = <instance>.fetch(message_set, message_parts)
-
-        'message_parts' should be a string of selected parts
-        enclosed in parentheses, eg: "(UID BODY[TEXT])".
-
-        'data' are tuples of message part envelope and data.
-        """
-        name = 'FETCH'
-        typ, dat = self._simple_command(name, message_set, message_parts)
-        return self._untagged_response(typ, dat, name)
-
-
-    def getacl(self, mailbox):
-        """Get the ACLs for a mailbox.
-
-        (typ, [data]) = <instance>.getacl(mailbox)
-        """
-        typ, dat = self._simple_command('GETACL', mailbox)
-        return self._untagged_response(typ, dat, 'ACL')
-
-
-    def getquota(self, root):
-        """Get the quota root's resource usage and limits.
-
-        Part of the IMAP4 QUOTA extension defined in rfc2087.
-
-        (typ, [data]) = <instance>.getquota(root)
-        """
-        typ, dat = self._simple_command('GETQUOTA', root)
-        return self._untagged_response(typ, dat, 'QUOTA')
-
-
-    def getquotaroot(self, mailbox):
-        """Get the list of quota roots for the named mailbox.
-
-        (typ, [[QUOTAROOT responses...], [QUOTA responses]]) = <instance>.getquotaroot(mailbox)
-        """
-        typ, dat = self._simple_command('GETQUOTA', root)
-        typ, quota = self._untagged_response(typ, dat, 'QUOTA')
-        typ, quotaroot = self._untagged_response(typ, dat, 'QUOTAROOT')
-        return typ, [quotaroot, quota]
-
-
-    def list(self, directory='""', pattern='*'):
-        """List mailbox names in directory matching pattern.
-
-        (typ, [data]) = <instance>.list(directory='""', pattern='*')
-
-        'data' is list of LIST responses.
-        """
-        name = 'LIST'
-        typ, dat = self._simple_command(name, directory, pattern)
-        return self._untagged_response(typ, dat, name)
-
-
-    def login(self, user, password):
-        """Identify client using plaintext password.
-
-        (typ, [data]) = <instance>.login(user, password)
-
-        NB: 'password' will be quoted.
-        """
-        #if not 'AUTH=LOGIN' in self.capabilities:
-        #       raise self.error("Server doesn't allow LOGIN authentication." % mech)
-        typ, dat = self._simple_command('LOGIN', user, self._quote(password))
-        if typ != 'OK':
-            raise self.error(dat[-1])
-        self.state = 'AUTH'
-        return typ, dat
-
-
-    def logout(self):
-        """Shutdown connection to server.
-
-        (typ, [data]) = <instance>.logout()
-
-        Returns server 'BYE' response.
-        """
-        self.state = 'LOGOUT'
-        try: typ, dat = self._simple_command('LOGOUT')
-        except: typ, dat = 'NO', ['%s: %s' % sys.exc_info()[:2]]
-        self.shutdown()
-        if 'BYE' in self.untagged_responses:
-            return 'BYE', self.untagged_responses['BYE']
-        return typ, dat
-
-
-    def lsub(self, directory='""', pattern='*'):
-        """List 'subscribed' mailbox names in directory matching pattern.
-
-        (typ, [data, ...]) = <instance>.lsub(directory='""', pattern='*')
-
-        'data' are tuples of message part envelope and data.
-        """
-        name = 'LSUB'
-        typ, dat = self._simple_command(name, directory, pattern)
-        return self._untagged_response(typ, dat, name)
-
-
-    def namespace(self):
-        """ Returns IMAP namespaces ala rfc2342
-
-        (typ, [data, ...]) = <instance>.namespace()
-        """
-        name = 'NAMESPACE'
-        typ, dat = self._simple_command(name)
-        return self._untagged_response(typ, dat, name)
-
-
-    def noop(self):
-        """Send NOOP command.
-
-        (typ, data) = <instance>.noop()
-        """
-        if __debug__:
-            if self.debug >= 3:
-                self._dump_ur(self.untagged_responses)
-        return self._simple_command('NOOP')
-
-
-    def partial(self, message_num, message_part, start, length):
-        """Fetch truncated part of a message.
-
-        (typ, [data, ...]) = <instance>.partial(message_num, message_part, start, length)
-
-        'data' is tuple of message part envelope and data.
-        """
-        name = 'PARTIAL'
-        typ, dat = self._simple_command(name, message_num, message_part, start, length)
-        return self._untagged_response(typ, dat, 'FETCH')
-
-
-    def rename(self, oldmailbox, newmailbox):
-        """Rename old mailbox name to new.
-
-        (typ, data) = <instance>.rename(oldmailbox, newmailbox)
-        """
-        return self._simple_command('RENAME', oldmailbox, newmailbox)
-
-
-    def search(self, charset, *criteria):
-        """Search mailbox for matching messages.
-
-        (typ, [data]) = <instance>.search(charset, criterium, ...)
-
-        'data' is space separated list of matching message numbers.
-        """
-        name = 'SEARCH'
-        if charset:
-            typ, dat = apply(self._simple_command, (name, 'CHARSET', charset) + criteria)
-        else:
-            typ, dat = apply(self._simple_command, (name,) + criteria)
-        return self._untagged_response(typ, dat, name)
-
-
-    def select(self, mailbox='INBOX', readonly=None):
-        """Select a mailbox.
-
-        Flush all untagged responses.
-
-        (typ, [data]) = <instance>.select(mailbox='INBOX', readonly=None)
-
-        'data' is count of messages in mailbox ('EXISTS' response).
-        """
-        # Mandated responses are ('FLAGS', 'EXISTS', 'RECENT', 'UIDVALIDITY')
-        self.untagged_responses = {}    # Flush old responses.
-        self.is_readonly = readonly
-        name = 'SELECT'
-        typ, dat = self._simple_command(name, mailbox)
-        if typ != 'OK':
-            self.state = 'AUTH'     # Might have been 'SELECTED'
-            return typ, dat
-        self.state = 'SELECTED'
-        if 'READ-ONLY' in self.untagged_responses \
-                and not readonly:
-            if __debug__:
-                if self.debug >= 1:
-                    self._dump_ur(self.untagged_responses)
-            raise self.readonly('%s is not writable' % mailbox)
-        return typ, self.untagged_responses.get('EXISTS', [None])
-
-
-    def setacl(self, mailbox, who, what):
-        """Set a mailbox acl.
-
-        (typ, [data]) = <instance>.create(mailbox, who, what)
-        """
-        return self._simple_command('SETACL', mailbox, who, what)
-
-
-    def setquota(self, root, limits):
-        """Set the quota root's resource limits.
-
-        (typ, [data]) = <instance>.setquota(root, limits)
-        """
-        typ, dat = self._simple_command('SETQUOTA', root, limits)
-        return self._untagged_response(typ, dat, 'QUOTA')
-
-
-    def sort(self, sort_criteria, charset, *search_criteria):
-        """IMAP4rev1 extension SORT command.
-
-        (typ, [data]) = <instance>.sort(sort_criteria, charset, search_criteria, ...)
-        """
-        name = 'SORT'
-        #if not name in self.capabilities:      # Let the server decide!
-        #       raise self.error('unimplemented extension command: %s' % name)
-        if (sort_criteria[0],sort_criteria[-1]) != ('(',')'):
-            sort_criteria = '(%s)' % sort_criteria
-        typ, dat = apply(self._simple_command, (name, sort_criteria, charset) + search_criteria)
-        return self._untagged_response(typ, dat, name)
-
-
-    def status(self, mailbox, names):
-        """Request named status conditions for mailbox.
-
-        (typ, [data]) = <instance>.status(mailbox, names)
-        """
-        name = 'STATUS'
-        #if self.PROTOCOL_VERSION == 'IMAP4':   # Let the server decide!
-        #    raise self.error('%s unimplemented in IMAP4 (obtain IMAP4rev1 server, or re-code)' % name)
-        typ, dat = self._simple_command(name, mailbox, names)
-        return self._untagged_response(typ, dat, name)
-
-
-    def store(self, message_set, command, flags):
-        """Alters flag dispositions for messages in mailbox.
-
-        (typ, [data]) = <instance>.store(message_set, command, flags)
-        """
-        if (flags[0],flags[-1]) != ('(',')'):
-            flags = '(%s)' % flags  # Avoid quoting the flags
-        typ, dat = self._simple_command('STORE', message_set, command, flags)
-        return self._untagged_response(typ, dat, 'FETCH')
-
-
-    def subscribe(self, mailbox):
-        """Subscribe to new mailbox.
-
-        (typ, [data]) = <instance>.subscribe(mailbox)
-        """
-        return self._simple_command('SUBSCRIBE', mailbox)
-
-
-    def uid(self, command, *args):
-        """Execute "command arg ..." with messages identified by UID,
-                rather than message number.
-
-        (typ, [data]) = <instance>.uid(command, arg1, arg2, ...)
-
-        Returns response appropriate to 'command'.
-        """
-        command = command.upper()
-        if not command in Commands:
-            raise self.error("Unknown IMAP4 UID command: %s" % command)
-        if self.state not in Commands[command]:
-            raise self.error('command %s illegal in state %s'
-                                    % (command, self.state))
-        name = 'UID'
-        typ, dat = apply(self._simple_command, (name, command) + args)
-        if command in ('SEARCH', 'SORT'):
-            name = command
-        else:
-            name = 'FETCH'
-        return self._untagged_response(typ, dat, name)
-
-
-    def unsubscribe(self, mailbox):
-        """Unsubscribe from old mailbox.
-
-        (typ, [data]) = <instance>.unsubscribe(mailbox)
-        """
-        return self._simple_command('UNSUBSCRIBE', mailbox)
-
-
-    def xatom(self, name, *args):
-        """Allow simple extension commands
-                notified by server in CAPABILITY response.
-
-        Assumes command is legal in current state.
-
-        (typ, [data]) = <instance>.xatom(name, arg, ...)
-
-        Returns response appropriate to extension command `name'.
-        """
-        name = name.upper()
-        #if not name in self.capabilities:      # Let the server decide!
-        #    raise self.error('unknown extension command: %s' % name)
-        if not name in Commands:
-            Commands[name] = (self.state,)
-        return apply(self._simple_command, (name,) + args)
-
-
-
-    #       Private methods
-
-
-    def _append_untagged(self, typ, dat):
-
-        if dat is None: dat = ''
-        ur = self.untagged_responses
-        if __debug__:
-            if self.debug >= 5:
-                self._mesg('untagged_responses[%s] %s += ["%s"]' %
-                        (typ, len(ur.get(typ,'')), dat))
-        if typ in ur:
-            ur[typ].append(dat)
-        else:
-            ur[typ] = [dat]
-
-
-    def _check_bye(self):
-        bye = self.untagged_responses.get('BYE')
-        if bye:
-            raise self.abort(bye[-1])
-
-
-    def _command(self, name, *args):
-
-        if self.state not in Commands[name]:
-            self.literal = None
-            raise self.error(
-            'command %s illegal in state %s' % (name, self.state))
-
-        for typ in ('OK', 'NO', 'BAD'):
-            if typ in self.untagged_responses:
-                del self.untagged_responses[typ]
-
-        if 'READ-ONLY' in self.untagged_responses \
-        and not self.is_readonly:
-            raise self.readonly('mailbox status changed to READ-ONLY')
-
-        tag = self._new_tag()
-        data = '%s %s' % (tag, name)
-        for arg in args:
-            if arg is None: continue
-            data = '%s %s' % (data, self._checkquote(arg))
-
-        literal = self.literal
-        if literal is not None:
-            self.literal = None
-            if type(literal) is type(self._command):
-                literator = literal
-            else:
-                literator = None
-                data = '%s {%s}' % (data, len(literal))
-
-        if __debug__:
-            if self.debug >= 4:
-                self._mesg('> %s' % data)
-            else:
-                self._log('> %s' % data)
-
-        try:
-            self.send('%s%s' % (data, CRLF))
-        except (socket.error, OSError), val:
-            raise self.abort('socket error: %s' % val)
-
-        if literal is None:
-            return tag
-
-        while 1:
-            # Wait for continuation response
-
-            while self._get_response():
-                if self.tagged_commands[tag]:   # BAD/NO?
-                    return tag
-
-            # Send literal
-
-            if literator:
-                literal = literator(self.continuation_response)
-
-            if __debug__:
-                if self.debug >= 4:
-                    self._mesg('write literal size %s' % len(literal))
-
-            try:
-                self.send(literal)
-                self.send(CRLF)
-            except (socket.error, OSError), val:
-                raise self.abort('socket error: %s' % val)
-
-            if not literator:
-                break
-
-        return tag
-
-
-    def _command_complete(self, name, tag):
-        self._check_bye()
-        try:
-            typ, data = self._get_tagged_response(tag)
-        except self.abort, val:
-            raise self.abort('command: %s => %s' % (name, val))
-        except self.error, val:
-            raise self.error('command: %s => %s' % (name, val))
-        self._check_bye()
-        if typ == 'BAD':
-            raise self.error('%s command error: %s %s' % (name, typ, data))
-        return typ, data
-
-
-    def _get_response(self):
-
-        # Read response and store.
-        #
-        # Returns None for continuation responses,
-        # otherwise first response line received.
-
-        resp = self._get_line()
-
-        # Command completion response?
-
-        if self._match(self.tagre, resp):
-            tag = self.mo.group('tag')
-            if not tag in self.tagged_commands:
-                raise self.abort('unexpected tagged response: %s' % resp)
-
-            typ = self.mo.group('type')
-            dat = self.mo.group('data')
-            self.tagged_commands[tag] = (typ, [dat])
-        else:
-            dat2 = None
-
-            # '*' (untagged) responses?
-
-            if not self._match(Untagged_response, resp):
-                if self._match(Untagged_status, resp):
-                    dat2 = self.mo.group('data2')
-
-            if self.mo is None:
-                # Only other possibility is '+' (continuation) response...
-
-                if self._match(Continuation, resp):
-                    self.continuation_response = self.mo.group('data')
-                    return None     # NB: indicates continuation
-
-                raise self.abort("unexpected response: '%s'" % resp)
-
-            typ = self.mo.group('type')
-            dat = self.mo.group('data')
-            if dat is None: dat = ''        # Null untagged response
-            if dat2: dat = dat + ' ' + dat2
-
-            # Is there a literal to come?
-
-            while self._match(Literal, dat):
-
-                # Read literal direct from connection.
-
-                size = int(self.mo.group('size'))
-                if __debug__:
-                    if self.debug >= 4:
-                        self._mesg('read literal size %s' % size)
-                data = self.read(size)
-
-                # Store response with literal as tuple
-
-                self._append_untagged(typ, (dat, data))
-
-                # Read trailer - possibly containing another literal
-
-                dat = self._get_line()
-
-            self._append_untagged(typ, dat)
-
-        # Bracketed response information?
-
-        if typ in ('OK', 'NO', 'BAD') and self._match(Response_code, dat):
-            self._append_untagged(self.mo.group('type'), self.mo.group('data'))
-
-        if __debug__:
-            if self.debug >= 1 and typ in ('NO', 'BAD', 'BYE'):
-                self._mesg('%s response: %s' % (typ, dat))
-
-        return resp
-
-
-    def _get_tagged_response(self, tag):
-
-        while 1:
-            result = self.tagged_commands[tag]
-            if result is not None:
-                del self.tagged_commands[tag]
-                return result
-
-            # Some have reported "unexpected response" exceptions.
-            # Note that ignoring them here causes loops.
-            # Instead, send me details of the unexpected response and
-            # I'll update the code in `_get_response()'.
-
-            try:
-                self._get_response()
-            except self.abort, val:
-                if __debug__:
-                    if self.debug >= 1:
-                        self.print_log()
-                raise
-
-
-    def _get_line(self):
-
-        line = self.readline()
-        if not line:
-            raise self.abort('socket error: EOF')
-
-        # Protocol mandates all lines terminated by CRLF
-
-        line = line[:-2]
-        if __debug__:
-            if self.debug >= 4:
-                self._mesg('< %s' % line)
-            else:
-                self._log('< %s' % line)
-        return line
-
-
-    def _match(self, cre, s):
-
-        # Run compiled regular expression match method on 's'.
-        # Save result, return success.
-
-        self.mo = cre.match(s)
-        if __debug__:
-            if self.mo is not None and self.debug >= 5:
-                self._mesg("\tmatched r'%s' => %s" % (cre.pattern, `self.mo.groups()`))
-        return self.mo is not None
-
-
-    def _new_tag(self):
-
-        tag = '%s%s' % (self.tagpre, self.tagnum)
-        self.tagnum = self.tagnum + 1
-        self.tagged_commands[tag] = None
-        return tag
-
-
-    def _checkquote(self, arg):
-
-        # Must quote command args if non-alphanumeric chars present,
-        # and not already quoted.
-
-        if type(arg) is not type(''):
-            return arg
-        if (arg[0],arg[-1]) in (('(',')'),('"','"')):
-            return arg
-        if self.mustquote.search(arg) is None:
-            return arg
-        return self._quote(arg)
-
-
-    def _quote(self, arg):
-
-        arg = arg.replace('\\', '\\\\')
-        arg = arg.replace('"', '\\"')
-
-        return '"%s"' % arg
-
-
-    def _simple_command(self, name, *args):
-
-        return self._command_complete(name, apply(self._command, (name,) + args))
-
-
-    def _untagged_response(self, typ, dat, name):
-
-        if typ == 'NO':
-            return typ, dat
-        if not name in self.untagged_responses:
-            return typ, [None]
-        data = self.untagged_responses[name]
-        if __debug__:
-            if self.debug >= 5:
-                self._mesg('untagged_responses[%s] => %s' % (name, data))
-        del self.untagged_responses[name]
-        return typ, data
-
-
-    if __debug__:
-
-        def _mesg(self, s, secs=None):
-            if secs is None:
-                secs = time.time()
-            tm = time.strftime('%M:%S', time.localtime(secs))
-            UIBase.getglobalui().debug('imap', '  %s.%02d %s' % (tm, (secs*100)%100, s))
-
-        def _dump_ur(self, dict):
-            # Dump untagged responses (in `dict').
-            l = dict.items()
-            if not l: return
-            t = '\n\t\t'
-            l = map(lambda x:'%s: "%s"' % (x[0], x[1][0] and '" "'.join(x[1]) or ''), l)
-            self._mesg('untagged responses dump:%s%s' % (t, t.join(l)))
-
-        def _log(self, line):
-            # Keep log of last `_cmd_log_len' interactions for debugging.
-            self._cmd_log[self._cmd_log_idx] = (line, time.time())
-            self._cmd_log_idx += 1
-            if self._cmd_log_idx >= self._cmd_log_len:
-                self._cmd_log_idx = 0
-
-        def print_log(self):
-            self._mesg('last %d IMAP4 interactions:' % len(self._cmd_log))
-            i, n = self._cmd_log_idx, self._cmd_log_len
-            while n:
-                try:
-                    apply(self._mesg, self._cmd_log[i])
-                except:
-                    pass
-                i += 1
-                if i >= self._cmd_log_len:
-                    i = 0
-                n -= 1
-
-class IMAP4_Tunnel(IMAP4):
-    """IMAP4 client class over a tunnel
-
-    Instantiate with: IMAP4_Tunnel(tunnelcmd)
-
-    tunnelcmd -- shell command to generate the tunnel.
-    The result will be in PREAUTH stage."""
-
-    def __init__(self, tunnelcmd):
-        IMAP4.__init__(self, tunnelcmd)
-
-    def open(self, host, port):
-        """The tunnelcmd comes in on host!"""
-        self.outfd, self.infd = os.popen2(host, "t", 0)
-
-    def read(self, size):
-        retval = ''
-        while len(retval) < size:
-            retval += self.infd.read(size - len(retval))
-        return retval
-
-    def readline(self):
-        return self.infd.readline()
-
-    def send(self, data):
-        self.outfd.write(data)
-
-    def shutdown(self):
-        self.infd.close()
-        self.outfd.close()
-        
-
-class sslwrapper:
-    def __init__(self, sslsock):
-        self.sslsock = sslsock
-        self.readbuf = ''
-
-    def write(self, s):
-        return self.sslsock.write(s)
-
-    def _read(self, n):
-        return self.sslsock.read(n)
-
-    def read(self, n):
-        if len(self.readbuf):
-            # Return the stuff in readbuf, even if less than n.
-            # It might contain the rest of the line, and if we try to
-            # read more, might block waiting for data that is not
-            # coming to arrive.
-            bytesfrombuf = min(n, len(self.readbuf))
-            retval = self.readbuf[:bytesfrombuf]
-            self.readbuf = self.readbuf[bytesfrombuf:]
-            return retval
-        retval = self._read(n)
-        if len(retval) > n:
-            self.readbuf = retval[n:]
-            return retval[:n]
-        return retval
-
-    def readline(self):
-        retval = ''
-        while 1:
-            linebuf = self.read(1024)
-            nlindex = linebuf.find("\n")
-            if nlindex != -1:
-                retval += linebuf[:nlindex + 1]
-                self.readbuf = linebuf[nlindex + 1:] + self.readbuf
-                return retval
-            else:
-                retval += linebuf
-                
-
-class IMAP4_SSL(IMAP4):
-
-    """IMAP4 client class over SSL connection
-
-    Instantiate with: IMAP4_SSL([host[, port[, keyfile[, certfile]]]])
-
-            host - host's name (default: localhost);
-            port - port number (default: standard IMAP4 SSL port).
-            keyfile - PEM formatted file that contains your private key (default: None);
-            certfile - PEM formatted certificate chain file (default: None);
-
-    for more documentation see the docstring of the parent class IMAP4.
-    """
-
-
-    def __init__(self, host = '', port = IMAP4_SSL_PORT, keyfile = None, certfile = None):
-        self.keyfile = keyfile
-        self.certfile = certfile
-        IMAP4.__init__(self, host, port)
-
-
-    def open(self, host = '', port = IMAP4_SSL_PORT):
-        """Setup connection to remote server on "host:port".
-            (default: localhost:standard IMAP4 SSL port).
-        This connection will be used by the routines:
-            read, readline, send, shutdown.
-        """
-        self.host = host
-        self.port = port
-        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        self.sock.connect((host, port))
-        if sys.version_info[0] <= 2 and sys.version_info[1] <= 2:
-            self.sslobj = socket.ssl(self.sock, self.keyfile, self.certfile)
-        else:
-            self.sslobj = socket.ssl(self.sock._sock, self.keyfile, self.certfile)
-        self.sslobj = sslwrapper(self.sslobj)
-
-
-    def read(self, size):
-        """Read 'size' bytes from remote."""
-        retval = ''
-        while len(retval) < size:
-            retval += self.sslobj.read(size - len(retval))
-        return retval
-
-
-    def readline(self):
-        """Read line from remote."""
-        return self.sslobj.readline()
-
-    def send(self, data):
-        """Send data to remote."""
-        byteswritten = 0
-        bytestowrite = len(data)
-        while byteswritten < bytestowrite:
-            byteswritten += self.sslobj.write(data[byteswritten:])
-
-
-    def shutdown(self):
-        """Close I/O established in "open"."""
-        self.sock.close()
-
-
-    def socket(self):
-        """Return socket instance used to connect to IMAP4 server.
-
-        socket = <instance>.socket()
-        """
-        return self.sock
-
-
-    def ssl(self):
-        """Return SSLObject instance used to communicate with the IMAP4 server.
-
-        ssl = <instance>.socket.ssl()
-        """
-        return self.sslobj
-
-
-
-class _Authenticator:
-
-    """Private class to provide en/decoding
-            for base64-based authentication conversation.
-    """
-
-    def __init__(self, mechinst):
-        self.mech = mechinst    # Callable object to provide/process data
-
-    def process(self, data):
-        ret = self.mech(self.decode(data))
-        if ret is None:
-            return '*'      # Abort conversation
-        return self.encode(ret)
-
-    def encode(self, inp):
-        #
-        #  Invoke binascii.b2a_base64 iteratively with
-        #  short even length buffers, strip the trailing
-        #  line feed from the result and append.  "Even"
-        #  means a number that factors to both 6 and 8,
-        #  so when it gets to the end of the 8-bit input
-        #  there's no partial 6-bit output.
-        #
-        oup = ''
-        while inp:
-            if len(inp) > 48:
-                t = inp[:48]
-                inp = inp[48:]
-            else:
-                t = inp
-                inp = ''
-            e = binascii.b2a_base64(t)
-            if e:
-                oup = oup + e[:-1]
-        return oup
-
-    def decode(self, inp):
-        if not inp:
-            return ''
-        return binascii.a2b_base64(inp)
-
-
-
-Mon2num = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,
-        'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
-
-def Internaldate2tuple(resp):
-    """Convert IMAP4 INTERNALDATE to UT.
-
-    Returns Python time module tuple.
-    """
-
-    mo = InternalDate.match(resp)
-    if not mo:
-        return None
-
-    mon = Mon2num[mo.group('mon')]
-    zonen = mo.group('zonen')
-
-    day = int(mo.group('day'))
-    year = int(mo.group('year'))
-    hour = int(mo.group('hour'))
-    min = int(mo.group('min'))
-    sec = int(mo.group('sec'))
-    zoneh = int(mo.group('zoneh'))
-    zonem = int(mo.group('zonem'))
-
-    # INTERNALDATE timezone must be subtracted to get UT
-
-    zone = (zoneh*60 + zonem)*60
-    if zonen == '-':
-        zone = -zone
-
-    tt = (year, mon, day, hour, min, sec, -1, -1, -1)
-
-    utc = time.mktime(tt)
-
-    # Following is necessary because the time module has no 'mkgmtime'.
-    # 'mktime' assumes arg in local timezone, so adds timezone/altzone.
-
-    lt = time.localtime(utc)
-    if time.daylight and lt[-1]:
-        zone = zone + time.altzone
-    else:
-        zone = zone + time.timezone
-
-    return time.localtime(utc - zone)
-
-
-
-def Int2AP(num):
-
-    """Convert integer to A-P string representation."""
-
-    val = ''; AP = 'ABCDEFGHIJKLMNOP'
-    num = int(abs(num))
-    while num:
-        num, mod = divmod(num, 16)
-        val = AP[mod] + val
-    return val
-
-
-
-def ParseFlags(resp):
-
-    """Convert IMAP4 flags response to python tuple."""
-
-    mo = Flags.match(resp)
-    if not mo:
-        return ()
-
-    return tuple(mo.group('flags').split())
-
-
-def Time2Internaldate(date_time):
-
-    """Convert 'date_time' to IMAP4 INTERNALDATE representation.
-
-    Return string in form: '"DD-Mmm-YYYY HH:MM:SS +HHMM"'
-    """
-
-    if isinstance(date_time, (int, float)):
-        tt = time.localtime(date_time)
-    elif isinstance(date_time, (tuple, time.struct_time)):
-        tt = date_time
-    elif isinstance(date_time, str) and (date_time[0],date_time[-1]) == ('"','"'):
-        return date_time        # Assume in correct format
-    else:
-        raise ValueError("date_time not of a known type")
-
-    dt = time.strftime("%d-%b-%Y %H:%M:%S", tt)
-    if dt[0] == '0':
-        dt = ' ' + dt[1:]
-    if time.daylight and tt[-1]:
-        zone = -time.altzone
-    else:
-        zone = -time.timezone
-    return '"' + dt + " %+03d%02d" % divmod(zone/60, 60) + '"'
-
-
-
-if __name__ == '__main__':
-
-    import getopt, getpass
-
-    try:
-        optlist, args = getopt.getopt(sys.argv[1:], 'd:')
-    except getopt.error, val:
-        pass
-
-    for opt,val in optlist:
-        if opt == '-d':
-            Debug = int(val)
-
-    if not args: args = ('',)
-
-    host = args[0]
-
-    USER = getpass.getuser()
-    PASSWD = getpass.getpass("IMAP password for %s on %s: " % (USER, host or "localhost"))
-
-    test_mesg = 'From: %(user)s@localhost%(lf)sSubject: IMAP4 test%(lf)s%(lf)sdata...%(lf)s' % {'user':USER, 'lf':CRLF}
-    test_seq1 = (
-    ('login', (USER, PASSWD)),
-    ('create', ('/tmp/xxx 1',)),
-    ('rename', ('/tmp/xxx 1', '/tmp/yyy')),
-    ('CREATE', ('/tmp/yyz 2',)),
-    ('append', ('/tmp/yyz 2', None, None, test_mesg)),
-    ('list', ('/tmp', 'yy*')),
-    ('select', ('/tmp/yyz 2',)),
-    ('search', (None, 'SUBJECT', 'test')),
-    ('fetch', ('1', '(FLAGS INTERNALDATE RFC822)')),
-    ('store', ('1', 'FLAGS', '(\Deleted)')),
-    ('namespace', ()),
-    ('expunge', ()),
-    ('recent', ()),
-    ('close', ()),
-    )
-
-    test_seq2 = (
-    ('select', ()),
-    ('response',('UIDVALIDITY',)),
-    ('uid', ('SEARCH', 'ALL')),
-    ('response', ('EXISTS',)),
-    ('append', (None, None, None, test_mesg)),
-    ('recent', ()),
-    ('logout', ()),
-    )
-
-    def run(cmd, args):
-        M._mesg('%s %s' % (cmd, args))
-        typ, dat = apply(getattr(M, cmd), args)
-        M._mesg('%s => %s %s' % (cmd, typ, dat))
-        return dat
-
-    try:
-        M = IMAP4(host)
-        M._mesg('PROTOCOL_VERSION = %s' % M.PROTOCOL_VERSION)
-        M._mesg('CAPABILITIES = %s' % `M.capabilities`)
-
-        for cmd,args in test_seq1:
-            run(cmd, args)
-
-        for ml in run('list', ('/tmp/', 'yy%')):
-            mo = re.match(r'.*"([^"]+)"$', ml)
-            if mo: path = mo.group(1)
-            else: path = ml.split()[-1]
-            run('delete', (path,))
-
-        for cmd,args in test_seq2:
-            dat = run(cmd, args)
-
-            if (cmd,args) != ('uid', ('SEARCH', 'ALL')):
-                continue
-
-            uid = dat[-1].split()
-            if not uid: continue
-            run('uid', ('FETCH', '%s' % uid[-1],
-                    '(FLAGS INTERNALDATE RFC822.SIZE RFC822.HEADER RFC822.TEXT)'))
-
-        print '\nAll tests OK.'
-
-    except:
-        print '\nTests failed.'
-
-        if not Debug:
-            print '''
-If you would like to see debugging output,
-try: %s -d5
-''' % sys.argv[0]
-
-        raise

Modified: offlineimap/branches/twisted/offlineimap/init.py
==============================================================================
--- offlineimap/branches/twisted/offlineimap/init.py	2003-07-26 04:20:57 UTC (rev 524)
+++ offlineimap/branches/twisted/offlineimap/init.py	2003-07-26 16:16:14 UTC (rev 525)
@@ -119,33 +119,36 @@
     remoterepos = None
     localrepos = None
 
-    if options.has_key('-1'):
-        threadutil.initInstanceLimit("ACCOUNTLIMIT", 1)
-    else:
-        threadutil.initInstanceLimit("ACCOUNTLIMIT",
-                                     config.getdefaultint("general", "maxsyncaccounts", 1))
+    #if options.has_key('-1'):
+    #    threadutil.initInstanceLimit("ACCOUNTLIMIT", 1)
+    #else:
+    #    threadutil.initInstanceLimit("ACCOUNTLIMIT",
+    #                                 config.getdefaultint("general", "maxsyncaccounts", 1))
 
-    for reposname in config.getsectionlist('Repository'):
-        for instancename in ["FOLDER_" + reposname,
-                             "MSGCOPY_" + reposname]:
-            if options.has_key('-1'):
-                threadutil.initInstanceLimit(instancename, 1)
-            else:
-                threadutil.initInstanceLimit(instancename,
-                                             config.getdefaultint('Repository ' + reposname, "maxconnections", 1))
+    #for reposname in config.getsectionlist('Repository'):
+    #    for instancename in ["FOLDER_" + reposname,
+    #                         "MSGCOPY_" + reposname]:
+    #        if options.has_key('-1'):
+    #            threadutil.initInstanceLimit(instancename, 1)
+    #        else:
+    #            threadutil.initInstanceLimit(instancename,
+    #                                         config.getdefaultint('Repository ' + reposname, "maxconnections", 1))
 
-    threadutil.initexitnotify()
-    t = ExitNotifyThread(target=syncmaster.syncitall,
-                         name='Sync Runner',
-                         kwargs = {'accounts': syncaccounts,
-                                   'config': config})
-    t.setDaemon(1)
-    t.start()
-    try:
-        threadutil.exitnotifymonitorloop(threadutil.threadexited)
-    except SystemExit:
-        raise
-    except:
-        ui.mainException()                  # Also expected to terminate.
+    #threadutil.initexitnotify()
+    #t = ExitNotifyThread(target=syncmaster.syncitall,
+    #                     name='Sync Runner',
+    #                     kwargs = {'accounts': syncaccounts,
+    #                               'config': config})
+    #t.setDaemon(1)
+    #t.start()
+    #try:
+    #    threadutil.exitnotifymonitorloop(threadutil.threadexited)
+    #except SystemExit:
+    #    raise
+    #except:
+    #    ui.mainException()                  # Also expected to terminate.
 
-        
+    
+    from twisted.internet import reactor
+    syncmaster.syncitall(accounts = syncaccounts, config = config)
+    reactor.run()

Modified: offlineimap/branches/twisted/offlineimap/syncmaster.py
==============================================================================
--- offlineimap/branches/twisted/offlineimap/syncmaster.py	2003-07-26 04:20:57 UTC (rev 524)
+++ offlineimap/branches/twisted/offlineimap/syncmaster.py	2003-07-26 16:16:14 UTC (rev 525)
@@ -16,30 +16,18 @@
 #    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, imapserver, repository, folder, mbnames, threadutil, version
-from offlineimap.threadutil import InstanceLimitedThread, ExitNotifyThread
+from offlineimap import mbnames
 import offlineimap.accounts
 from offlineimap.accounts import SyncableAccount
 from offlineimap.ui import UIBase
-import re, os, os.path, offlineimap, sys
 from ConfigParser import ConfigParser
-from threading import *
 
-def syncaccount(threads, config, accountname):
+def syncaccount(config, accountname):
     account = SyncableAccount(config, accountname)
-    thread = InstanceLimitedThread(instancename = 'ACCOUNTLIMIT',
-                                   target = account.syncrunner,
-                                   name = "Account sync %s" % accountname)
-    thread.setDaemon(1)
-    thread.start()
-    threads.add(thread)
+    account.syncrunner()
     
 def syncitall(accounts, config):
-    currentThread().setExitMessage('SYNC_WITH_TIMER_TERMINATE')
     ui = UIBase.getglobalui()
-    threads = threadutil.threadlist()
     mbnames.init(config, accounts)
     for accountname in accounts:
-        syncaccount(threads, config, accountname)
-    # Wait for the threads to finish.
-    threads.reset()
+        syncaccount(config, accountname)

Deleted: offlineimap/branches/twisted/offlineimap/threadutil.py
==============================================================================
--- offlineimap/branches/twisted/offlineimap/threadutil.py	2003-07-26 04:20:57 UTC (rev 524)
+++ offlineimap/branches/twisted/offlineimap/threadutil.py	2003-07-26 16:16:14 UTC (rev 525)
@@ -1,291 +0,0 @@
-# Copyright (C) 2002, 2003 John Goerzen
-# Thread support module
-# <[email protected]>
-#
-#    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
-#    the Free Software Foundation; either version 2 of the License, or
-#    (at your option) any later version.
-#
-#    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
-#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-
-from threading import *
-from StringIO import StringIO
-import sys, traceback, thread, profile
-from offlineimap.ui import UIBase       # for getglobalui()
-
-profiledir = None
-
-def setprofiledir(newdir):
-    global profiledir
-    profiledir = newdir
-
-######################################################################
-# General utilities
-######################################################################
-
-def semaphorereset(semaphore, originalstate):
-    """Wait until the semaphore gets back to its original state -- all acquired
-    resources released."""
-    for i in range(originalstate):
-        semaphore.acquire()
-    # Now release these.
-    for i in range(originalstate):
-        semaphore.release()
-        
-def semaphorewait(semaphore):
-    semaphore.acquire()
-    semaphore.release()
-    
-def threadsreset(threadlist):
-    for thr in threadlist:
-        thr.join()
-
-class threadlist:
-    def __init__(self):
-        self.lock = Lock()
-        self.list = []
-
-    def add(self, thread):
-        self.lock.acquire()
-        try:
-            self.list.append(thread)
-        finally:
-            self.lock.release()
-
-    def remove(self, thread):
-        self.lock.acquire()
-        try:
-            self.list.remove(thread)
-        finally:
-            self.lock.release()
-
-    def pop(self):
-        self.lock.acquire()
-        try:
-            if not len(self.list):
-                return None
-            return self.list.pop()
-        finally:
-            self.lock.release()
-
-    def reset(self):
-        while 1:
-            thread = self.pop()
-            if not thread:
-                return
-            thread.join()
-            
-
-######################################################################
-# Exit-notify threads
-######################################################################
-
-exitcondition = Condition(Lock())
-exitthreads = []
-inited = 0
-
-def initexitnotify():
-    """Initialize the exit notify system.  This MUST be called from the
-    SAME THREAD that will call monitorloop BEFORE it calls monitorloop.
-    This SHOULD be called before the main thread starts any other
-    ExitNotifyThreads, or else it may miss the ability to catch the exit
-    status from them!"""
-    pass
-
-def exitnotifymonitorloop(callback):
-    """Enter an infinite "monitoring" loop.  The argument, callback,
-    defines the function to call when an ExitNotifyThread has terminated.
-    That function is called with a single argument -- the ExitNotifyThread
-    that has terminated.  The monitor will not continue to monitor for
-    other threads until the function returns, so if it intends to perform
-    long calculations, it should start a new thread itself -- but NOT
-    an ExitNotifyThread, or else an infinite loop may result.  Furthermore,
-    the monitor will hold the lock all the while the other thread is waiting.
-    """
-    global exitcondition, exitthreads
-    while 1:                            # Loop forever.
-        exitcondition.acquire()
-        try:
-            while not len(exitthreads):
-                exitcondition.wait(1)
-
-            while len(exitthreads):
-                callback(exitthreads.pop(0)) # Pull off in order added!
-        finally:
-            exitcondition.release()
-
-def threadexited(thread):
-    """Called when a thread exits."""
-    ui = UIBase.getglobalui()
-    if thread.getExitCause() == 'EXCEPTION':
-        if isinstance(thread.getExitException(), SystemExit):
-            # Bring a SystemExit into the main thread.
-            # Do not send it back to UI layer right now.
-            # Maybe later send it to ui.terminate?
-            raise SystemExit
-        ui.threadException(thread)      # Expected to terminate
-        sys.exit(100)                   # Just in case...
-        os._exit(100)
-    elif thread.getExitMessage() == 'SYNC_WITH_TIMER_TERMINATE':
-        ui.terminate()
-        # Just in case...
-        sys.exit(100)
-        os._exit(100)
-    else:
-        ui.threadExited(thread)
-
-class ExitNotifyThread(Thread):
-    """This class is designed to alert a "monitor" to the fact that a thread has
-    exited and to provide for the ability for it to find out why."""
-    def run(self):
-        global exitcondition, exitthreads, profiledir
-        self.threadid = thread.get_ident()
-        try:
-            if not profiledir:          # normal case
-                Thread.run(self)
-            else:
-                prof = profile.Profile()
-                try:
-                    prof = prof.runctx("Thread.run(self)", globals(), locals())
-                except SystemExit:
-                    pass
-                prof.dump_stats( \
-                            profiledir + "/" + str(self.threadid) + "_" + \
-                            self.getName() + ".prof")
-        except:
-            self.setExitCause('EXCEPTION')
-            self.setExitException(sys.exc_info()[1])
-            sbuf = StringIO()
-            traceback.print_exc(file = sbuf)
-            self.setExitStackTrace(sbuf.getvalue())
-        else:
-            self.setExitCause('NORMAL')
-        if not hasattr(self, 'exitmessage'):
-            self.setExitMessage(None)
-        exitcondition.acquire()
-        exitthreads.append(self)
-        exitcondition.notify()
-        exitcondition.release()
-
-    def setExitCause(self, cause):
-        self.exitcause = cause
-    def getExitCause(self):
-        """Returns the cause of the exit, one of:
-        'EXCEPTION' -- the thread aborted because of an exception
-        'NORMAL' -- normal termination."""
-        return self.exitcause
-    def setExitException(self, exc):
-        self.exitexception = exc
-    def getExitException(self):
-        """If getExitCause() is 'EXCEPTION', holds the value from
-        sys.exc_info()[1] for this exception."""
-        return self.exitexception
-    def setExitStackTrace(self, st):
-        self.exitstacktrace = st
-    def getExitStackTrace(self):
-        """If getExitCause() is 'EXCEPTION', returns a string representing
-        the stack trace for this exception."""
-        return self.exitstacktrace
-    def setExitMessage(self, msg):
-        """Sets the exit message to be fetched by a subsequent call to
-        getExitMessage.  This message may be any object or type except
-        None."""
-        self.exitmessage = msg
-    def getExitMessage(self):
-        """For any exit cause, returns the message previously set by
-        a call to setExitMessage(), or None if there was no such message
-        set."""
-        return self.exitmessage
-            
-
-######################################################################
-# Instance-limited threads
-######################################################################
-
-instancelimitedsems = {}
-instancelimitedlock = Lock()
-
-def initInstanceLimit(instancename, instancemax):
-    """Initialize the instance-limited thread implementation to permit
-    up to intancemax threads with the given instancename."""
-    instancelimitedlock.acquire()
-    if not instancelimitedsems.has_key(instancename):
-        instancelimitedsems[instancename] = BoundedSemaphore(instancemax)
-    instancelimitedlock.release()
-
-class InstanceLimitedThread(ExitNotifyThread):
-    def __init__(self, instancename, *args, **kwargs):
-        self.instancename = instancename
-                                                   
-        apply(ExitNotifyThread.__init__, (self,) + args, kwargs)
-
-    def start(self):
-        instancelimitedsems[self.instancename].acquire()
-        ExitNotifyThread.start(self)
-        
-    def run(self):
-        try:
-            ExitNotifyThread.run(self)
-        finally:
-            instancelimitedsems[self.instancename].release()
-        
-    
-######################################################################
-# Multi-lock -- capable of handling a single thread requesting a lock
-# multiple times
-######################################################################
-
-class MultiLock:
-    def __init__(self):
-        self.lock = Lock()
-        self.statuslock = Lock()
-        self.locksheld = {}
-
-    def acquire(self):
-        """Obtain a lock.  Provides nice support for a single
-        thread trying to lock it several times -- as may be the case
-        if one I/O-using object calls others, while wanting to make it all
-        an atomic operation.  Keeps a "lock request count" for the current
-        thread, and acquires the lock when it goes above zero, releases when
-        it goes below one.
-
-        This call is always blocking."""
-        
-        # First, check to see if this thread already has a lock.
-        # If so, increment the lock count and just return.
-        self.statuslock.acquire()
-        try:
-            threadid = thread.get_ident()
-
-            if threadid in self.locksheld:
-                self.locksheld[threadid] += 1
-                return
-            else:
-                # This is safe because it is a per-thread structure
-                self.locksheld[threadid] = 1
-        finally:
-            self.statuslock.release()
-        self.lock.acquire()
-
-    def release(self):
-        self.statuslock.acquire()
-        try:
-            threadid = thread.get_ident()
-            if self.locksheld[threadid] > 1:
-                self.locksheld[threadid] -= 1
-                return
-            else:
-                del self.locksheld[threadid]
-                self.lock.release()
-        finally:
-            self.statuslock.release()
-
-