Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 57a0e03
Showing
2 changed files
with
429 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from twisted.mail import imap4, maildir | ||
from twisted.internet import reactor, defer, protocol | ||
from twisted.cred import portal, checkers, credentials | ||
from twisted.cred import error as credError | ||
from twisted.python import filepath | ||
from zope.interface import implements | ||
import time, os, random, pickle | ||
|
||
from twittermail import TwitterUserAccount, TwitterImapMailbox, TwitterCredentialsChecker, ObjCache | ||
|
||
import email | ||
|
||
class MailUserRealm(object): | ||
implements(portal.IRealm) | ||
avatarInterfaces = { | ||
imap4.IAccount: TwitterUserAccount, | ||
} | ||
|
||
def __init__(self, cache): | ||
self.cache = cache | ||
|
||
def requestAvatar(self, avatarId, mind, *interfaces): | ||
for requestedInterface in interfaces: | ||
if self.avatarInterfaces.has_key(requestedInterface): | ||
# return an instance of the correct class | ||
avatarClass = self.avatarInterfaces[requestedInterface] | ||
avatar = avatarClass(self.cache) | ||
# null logout function: take no arguments and do nothing | ||
logout = lambda: None | ||
return defer.succeed((requestedInterface, avatar, logout)) | ||
|
||
# none of the requested interfaces was supported | ||
raise KeyError("None of the requested interfaces is supported") | ||
|
||
class IMAPServerProtocol(imap4.IMAP4Server): | ||
"Subclass of imap4.IMAP4Server that adds debugging." | ||
debug = True | ||
|
||
def lineReceived(self, line): | ||
if self.debug: | ||
print "CLIENT:", line | ||
imap4.IMAP4Server.lineReceived(self, line) | ||
|
||
def sendLine(self, line): | ||
imap4.IMAP4Server.sendLine(self, line) | ||
if self.debug: | ||
print "SERVER:", line | ||
|
||
class IMAPFactory(protocol.Factory): | ||
protocol = IMAPServerProtocol | ||
portal = None # placeholder | ||
|
||
def buildProtocol(self, address): | ||
p = self.protocol() | ||
p.portal = self.portal | ||
p.factory = self | ||
return p | ||
|
||
if __name__ == "__main__": | ||
|
||
cache = ObjCache() | ||
|
||
portal = portal.Portal(MailUserRealm(cache)) | ||
portal.registerChecker(TwitterCredentialsChecker(cache)) | ||
|
||
factory = IMAPFactory() | ||
factory.portal = portal | ||
|
||
reactor.listenTCP(1143, factory) | ||
reactor.run() | ||
|
||
|
Oops, something went wrong.