<s> import os <EOL> import StringIO <EOL> from twisted . internet import interfaces , defer , reactor <EOL> from twisted . cred import portal , credentials , checkers <EOL> from twisted . cred . error import UnauthorizedLogin <EOL> from twisted . mail import mail <EOL> from twisted . mail import pop3 <EOL> from twisted . mail . maildir import AbstractMaildirDomain , StringListMailbox , DirdbmDatabase <EOL> from zope . interface import implementer <EOL> from openmail . kvcred import RiakPasswordChecker <EOL> from openmail . kvmailbox import KVMailbox <EOL> from openmail . kvstore import RiakKV <EOL> INTERNAL_ERROR = '''<STR_LIT>''' <EOL> class KVdirMessage ( mail . FileMessage ) : <EOL> size = None <EOL> def __init__ ( self , address , fp , * a , ** kw ) : <EOL> header = "<STR_LIT>" % address <EOL> fp . write ( header ) <EOL> self . address = address <EOL> self . size = len ( header ) <EOL> self . finalName = a [ <NUM_LIT:1> ] <EOL> print self . finalName <EOL> self . kv = RiakKV ( ) <EOL> mail . FileMessage . __init__ ( self , fp , * a , ** kw ) <EOL> def lineReceived ( self , line ) : <EOL> mail . FileMessage . lineReceived ( self , line ) <EOL> self . size += len ( line ) + <NUM_LIT:1> <EOL> def eomReceived ( self ) : <EOL> contents = self . fp . getvalue ( ) <EOL> self . fp . close ( ) <EOL> self . kv . new_message ( self . address , contents ) <EOL> return defer . succeed ( self . finalName ) <EOL> def connectionLost ( self ) : <EOL> self . fp . close ( ) <EOL> def _generateMaildirName ( ) : <EOL> import time <EOL> return '<STR_LIT>' % int ( time . time ( ) ) <EOL> @ implementer ( portal . IRealm ) <EOL> class KVDomain ( AbstractMaildirDomain ) : <EOL> """<STR_LIT>""" <EOL> portal = None <EOL> _credcheckers = None <EOL> def __init__ ( self , service , root , postmaster = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> AbstractMaildirDomain . __init__ ( self , service , root ) <EOL> self . db = RiakKV ( ) <EOL> self . postmaster = postmaster <EOL> self . _credcheckers = [ RiakPasswordChecker ( ) ] <EOL> def startMessage ( self , user ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( user , str ) : <EOL> name , domain = user . split ( '<STR_LIT:@>' , <NUM_LIT:1> ) <EOL> else : <EOL> name , domain = user . dest . local , user . dest . domain <EOL> username = self . userDirectory ( name ) <EOL> dir = '<STR_LIT>' % username <EOL> fname = _generateMaildirName ( ) <EOL> filename = os . path . join ( dir , '<STR_LIT>' , fname ) <EOL> fp = StringIO . StringIO ( ) <EOL> return KVdirMessage ( '<STR_LIT>' % ( name , domain ) , fp , filename , <EOL> os . path . join ( dir , '<STR_LIT>' , fname ) ) <EOL> def userDirectory ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if not self . db . has_key ( name ) : <EOL> self . db . new_user ( name ) <EOL> return name <EOL> def addUser ( self , user , password ) : <EOL> self . db . add_user ( user ) <EOL> def getCredentialsCheckers ( self ) : <EOL> if self . _credcheckers is None : <EOL> self . _credcheckers = [ DirdbmDatabase ( self . dbm ) ] <EOL> return self . _credcheckers <EOL> def requestAvatar ( self , avatarId , mind , * interfaces ) : <EOL> if pop3 . IMailbox not in interfaces : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> if avatarId == checkers . ANONYMOUS : <EOL> mbox = StringListMailbox ( [ INTERNAL_ERROR ] ) <EOL> else : <EOL> mbox = KVMailbox ( '<STR_LIT:/>' + avatarId ) <EOL> return ( <EOL> pop3 . IMailbox , <EOL> mbox , <EOL> lambda : None <EOL> ) </s>
