[Twisted-Python] IMAP4 plain authentication

David Watson mail at david-watson.net
Mon Jan 23 02:39:10 MST 2006


Hi,

My first post to this list, so apologies in advance if this has already been
discussed. I`ve been experimenting with some of the sample code from Abe
Fetting’s (very good) Twisted book and I`m a little stuck with one IMAP
authentication method.

The code is:

from twisted.protocols import imap4
from twisted.internet import protocol, defer

class IMAPFolderListProtocol(imap4.IMAP4Client):

    def serverGreeting(self, capabilities):
        self.registerAuthenticator(
            imap4.CramMD5ClientAuthenticator(self.factory.username))
        self.registerAuthenticator(
            imap4.LOGINAuthenticator(self.factory.username))
        authenticating = self.authenticate(self.factory.password)
        authenticating.addCallback(self.__loggedIn)
        authenticating.chainDeferred(self.factory.deferred)

    def __loggedIn(self, results):
        return self.list("", "*").addCallback(self.__gotMailboxList)

    def __gotMailboxList(self, list):
        return [boxInfo[2] for boxInfo in list]

    def connectionLost(self, reason):
        if not self.factory.deferred.called:
            # connection was lost unexpectedly!
            self.factory.deferred.errback(reason)

class IMAPFolderListFactory(protocol.ClientFactory):
    protocol = IMAPFolderListProtocol
    
    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.deferred = defer.Deferred()
        
    def clientConnectionFailed(self, connection, reason):
        self.deferred.errback(reason)

if __name__ == "__main__":
    from twisted.internet import reactor
    import sys, getpass
    
    def printMailboxList(list):
        list.sort()
        for box in list:
            print box
        reactor.stop()

    def handleError(error):
        print >> sys.stderr, "Error:", error.getErrorMessage()
        reactor.stop()

    if not len(sys.argv) == 3:
        print "Usage: %s server login" % sys.argv[0]
        sys.exit(1)
        
    server = sys.argv[1]
    user = sys.argv[2]
    password = getpass.getpass("Password: ")
    factory = IMAPFolderListFactory(user, password)
    factory.deferred.addCallback(
        printMailboxList).addErrback(
        handleError)
    reactor.connectTCP(server, 143, factory)
    reactor.run()

However, when I try to connect to my IMAP4 server using this code, I get:

Error: No supported authentication schemes available: Server supports
['PLAIN'], client supports ['CRAM-MD5', 'LOGIN']

I can`t seem to find a suitable PLAIN method in IMAP4client, so before I
re-invent the wheel, is there a standard Twisted way to handle this
situation (or am I simply missing something obvious)?

Thanks,

David





More information about the Twisted-Python mailing list