<s> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> import errno <EOL> import getopt <EOL> import time <EOL> import socket <EOL> import asyncore <EOL> import asynchat <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> program = sys . argv [ <NUM_LIT:0> ] <EOL> __version__ = '<STR_LIT>' <EOL> class Devnull : <EOL> def write ( self , msg ) : pass <EOL> def flush ( self ) : pass <EOL> DEBUGSTREAM = Devnull ( ) <EOL> NEWLINE = '<STR_LIT:\n>' <EOL> EMPTYSTRING = '<STR_LIT>' <EOL> COMMASPACE = '<STR_LIT:U+002CU+0020>' <EOL> def usage ( code , msg = '<STR_LIT>' ) : <EOL> print >> sys . stderr , __doc__ % globals ( ) <EOL> if msg : <EOL> print >> sys . stderr , msg <EOL> sys . exit ( code ) <EOL> class SMTPChannel ( asynchat . async_chat ) : <EOL> COMMAND = <NUM_LIT:0> <EOL> DATA = <NUM_LIT:1> <EOL> def __init__ ( self , server , conn , addr ) : <EOL> asynchat . async_chat . __init__ ( self , conn ) <EOL> self . __server = server <EOL> self . __conn = conn <EOL> self . __addr = addr <EOL> self . __line = [ ] <EOL> self . __state = self . COMMAND <EOL> self . __greeting = <NUM_LIT:0> <EOL> self . __mailfrom = None <EOL> self . __rcpttos = [ ] <EOL> self . __data = '<STR_LIT>' <EOL> self . __fqdn = socket . getfqdn ( ) <EOL> self . __peer = conn . getpeername ( ) <EOL> print >> DEBUGSTREAM , '<STR_LIT>' , repr ( self . __peer ) <EOL> self . push ( '<STR_LIT>' % ( self . __fqdn , __version__ ) ) <EOL> self . set_terminator ( '<STR_LIT:\r\n>' ) <EOL> def push ( self , msg ) : <EOL> asynchat . async_chat . push ( self , msg + '<STR_LIT:\r\n>' ) <EOL> def collect_incoming_data ( self , data ) : <EOL> self . __line . append ( data ) <EOL> def found_terminator ( self ) : <EOL> line = EMPTYSTRING . join ( self . __line ) <EOL> print >> DEBUGSTREAM , '<STR_LIT>' , repr ( line ) <EOL> self . __line = [ ] <EOL> if self . __state == self . COMMAND : <EOL> if not line : <EOL> self . push ( '<STR_LIT>' ) <EOL> return <EOL> method = None <EOL> i = line . find ( '<STR_LIT:U+0020>' ) <EOL> if i < <NUM_LIT:0> : <EOL> command = line . upper ( ) <EOL> arg = None <EOL> else : <EOL> command = line [ : i ] . upper ( ) <EOL> arg = line [ i + <NUM_LIT:1> : ] . strip ( ) <EOL> method = getattr ( self , '<STR_LIT>' + command , None ) <EOL> if not method : <EOL> self . push ( '<STR_LIT>' % command ) <EOL> return <EOL> method ( arg ) <EOL> return <EOL> else : <EOL> if self . __state != self . DATA : <EOL> self . push ( '<STR_LIT>' ) <EOL> return <EOL> data = [ ] <EOL> for text in line . split ( '<STR_LIT:\r\n>' ) : <EOL> if text and text [ <NUM_LIT:0> ] == '<STR_LIT:.>' : <EOL> data . append ( text [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> data . append ( text ) <EOL> self . __data = NEWLINE . join ( data ) <EOL> status = self . __server . process_message ( self . __peer , <EOL> self . __mailfrom , <EOL> self . __rcpttos , <EOL> self . __data ) <EOL> self . __rcpttos = [ ] <EOL> self . __mailfrom = None <EOL> self . __state = self . COMMAND <EOL> self . set_terminator ( '<STR_LIT:\r\n>' ) <EOL> if not status : <EOL> self . push ( '<STR_LIT>' ) <EOL> else : <EOL> self . push ( status ) <EOL> def smtp_HELO ( self , arg ) : <EOL> if not arg : <EOL> self . push ( '<STR_LIT>' ) <EOL> return <EOL> if self . __greeting : <EOL> self . push ( '<STR_LIT>' ) <EOL> else : <EOL> self . __greeting = arg <EOL> self . push ( '<STR_LIT>' % self . __fqdn ) <EOL> def smtp_NOOP ( self , arg ) : <EOL> if arg : <EOL> self . push ( '<STR_LIT>' ) <EOL> else : <EOL> self . push ( '<STR_LIT>' ) <EOL> def smtp_QUIT ( self , arg ) : <EOL> self . push ( '<STR_LIT>' ) <EOL> self . close_when_done ( ) <EOL> def __getaddr ( self , keyword , arg ) : <EOL> address = None <EOL> keylen = len ( keyword ) <EOL> if arg [ : keylen ] . upper ( ) == keyword : <EOL> address = arg [ keylen : ] . strip ( ) <EOL> if not address : <EOL> pass <EOL> elif address [ <NUM_LIT:0> ] == '<STR_LIT:<>' and address [ - <NUM_LIT:1> ] == '<STR_LIT:>>' and address != '<STR_LIT>' : <EOL> address = address [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> return address <EOL> def smtp_MAIL ( self , arg ) : <EOL> print >> DEBUGSTREAM , '<STR_LIT>' , arg <EOL> address = self . __getaddr ( '<STR_LIT>' , arg ) if arg else None <EOL> if not address : <EOL> self . push ( '<STR_LIT>' ) <EOL> return <EOL> if self . __mailfrom : <EOL> self . push ( '<STR_LIT>' ) <EOL> return <EOL> self . __mailfrom = address <EOL> print >> DEBUGSTREAM , '<STR_LIT>' , self . __mailfrom <EOL> self . push ( '<STR_LIT>' ) <EOL> def smtp_RCPT ( self , arg ) : <EOL> print >> DEBUGSTREAM , '<STR_LIT>' , arg <EOL> if not self . __mailfrom : <EOL> self . push ( '<STR_LIT>' ) <EOL> return <EOL> address = self . __getaddr ( '<STR_LIT>' , arg ) if arg else None <EOL> if not address : <EOL> self . push ( '<STR_LIT>' ) <EOL> return <EOL> self . __rcpttos . append ( address ) <EOL> print >> DEBUGSTREAM , '<STR_LIT>' , self . __rcpttos <EOL> self . push ( '<STR_LIT>' ) <EOL> def smtp_RSET ( self , arg ) : <EOL> if arg : <EOL> self . push ( '<STR_LIT>' ) <EOL> return <EOL> self . __mailfrom = None <EOL> self . __rcpttos = [ ] <EOL> self . __data = '<STR_LIT>' <EOL> self . __state = self . COMMAND <EOL> self . push ( '<STR_LIT>' ) <EOL> def smtp_DATA ( self , arg ) : <EOL> if not self . __rcpttos : <EOL> self . push ( '<STR_LIT>' ) <EOL> return <EOL> if arg : <EOL> self . push ( '<STR_LIT>' ) <EOL> return <EOL> self . __state = self . DATA <EOL> self . set_terminator ( '<STR_LIT>' ) <EOL> self . push ( '<STR_LIT>' ) <EOL> class SMTPServer ( asyncore . dispatcher ) : <EOL> def __init__ ( self , localaddr , remoteaddr ) : <EOL> self . _localaddr = localaddr <EOL> self . _remoteaddr = remoteaddr <EOL> asyncore . dispatcher . __init__ ( self ) <EOL> self . create_socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> self . set_reuse_addr ( ) <EOL> self . bind ( localaddr ) <EOL> self . listen ( <NUM_LIT:5> ) <EOL> print >> DEBUGSTREAM , '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , time . ctime ( time . time ( ) ) , <EOL> localaddr , remoteaddr ) <EOL> def handle_accept ( self ) : <EOL> conn , addr = self . accept ( ) <EOL> print >> DEBUGSTREAM , '<STR_LIT>' % repr ( addr ) <EOL> channel = SMTPChannel ( self , conn , addr ) <EOL> def process_message ( self , peer , mailfrom , rcpttos , data ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class DebuggingServer ( SMTPServer ) : <EOL> def process_message ( self , peer , mailfrom , rcpttos , data ) : <EOL> inheaders = <NUM_LIT:1> <EOL> lines = data . split ( '<STR_LIT:\n>' ) <EOL> print '<STR_LIT>' <EOL> for line in lines : <EOL> if inheaders and not line : <EOL> print '<STR_LIT>' , peer [ <NUM_LIT:0> ] <EOL> inheaders = <NUM_LIT:0> <EOL> print line <EOL> print '<STR_LIT>' <EOL> class PureProxy ( SMTPServer ) : <EOL> def process_message ( self , peer , mailfrom , rcpttos , data ) : <EOL> lines = data . split ( '<STR_LIT:\n>' ) <EOL> i = <NUM_LIT:0> <EOL> for line in lines : <EOL> if not line : <EOL> break <EOL> i += <NUM_LIT:1> <EOL> lines . insert ( i , '<STR_LIT>' % peer [ <NUM_LIT:0> ] ) <EOL> data = NEWLINE . join ( lines ) <EOL> refused = self . _deliver ( mailfrom , rcpttos , data ) <EOL> print >> DEBUGSTREAM , '<STR_LIT>' , refused <EOL> def _deliver ( self , mailfrom , rcpttos , data ) : <EOL> import smtplib <EOL> refused = { } <EOL> try : <EOL> s = smtplib . SMTP ( ) <EOL> s . connect ( self . _remoteaddr [ <NUM_LIT:0> ] , self . _remoteaddr [ <NUM_LIT:1> ] ) <EOL> try : <EOL> refused = s . sendmail ( mailfrom , rcpttos , data ) <EOL> finally : <EOL> s . quit ( ) <EOL> except smtplib . SMTPRecipientsRefused , e : <EOL> print >> DEBUGSTREAM , '<STR_LIT>' <EOL> refused = e . recipients <EOL> except ( socket . error , smtplib . SMTPException ) , e : <EOL> print >> DEBUGSTREAM , '<STR_LIT>' , e . __class__ <EOL> errcode = getattr ( e , '<STR_LIT>' , - <NUM_LIT:1> ) <EOL> errmsg = getattr ( e , '<STR_LIT>' , '<STR_LIT:ignore>' ) <EOL> for r in rcpttos : <EOL> refused [ r ] = ( errcode , errmsg ) <EOL> return refused <EOL> class MailmanProxy ( PureProxy ) : <EOL> def process_message ( self , peer , mailfrom , rcpttos , data ) : <EOL> from cStringIO import StringIO <EOL> from Mailman import Utils <EOL> from Mailman import Message <EOL> from Mailman import MailList <EOL> listnames = [ ] <EOL> for rcpt in rcpttos : <EOL> local = rcpt . lower ( ) . split ( '<STR_LIT:@>' ) [ <NUM_LIT:0> ] <EOL> parts = local . split ( '<STR_LIT:->' ) <EOL> if len ( parts ) > <NUM_LIT:2> : <EOL> continue <EOL> listname = parts [ <NUM_LIT:0> ] <EOL> if len ( parts ) == <NUM_LIT:2> : <EOL> command = parts [ <NUM_LIT:1> ] <EOL> else : <EOL> command = '<STR_LIT>' <EOL> if not Utils . list_exists ( listname ) or command not in ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> continue <EOL> listnames . append ( ( rcpt , listname , command ) ) <EOL> for rcpt , listname , command in listnames : <EOL> rcpttos . remove ( rcpt ) <EOL> print >> DEBUGSTREAM , '<STR_LIT>' , '<STR_LIT:U+0020>' . join ( rcpttos ) <EOL> if rcpttos : <EOL> refused = self . _deliver ( mailfrom , rcpttos , data ) <EOL> print >> DEBUGSTREAM , '<STR_LIT>' , refused <EOL> mlists = { } <EOL> s = StringIO ( data ) <EOL> msg = Message . Message ( s ) <EOL> if not msg . getheader ( '<STR_LIT>' ) : <EOL> msg [ '<STR_LIT>' ] = mailfrom <EOL> if not msg . getheader ( '<STR_LIT:date>' ) : <EOL> msg [ '<STR_LIT>' ] = time . ctime ( time . time ( ) ) <EOL> for rcpt , listname , command in listnames : <EOL> print >> DEBUGSTREAM , '<STR_LIT>' , rcpt <EOL> mlist = mlists . get ( listname ) <EOL> if not mlist : <EOL> mlist = MailList . MailList ( listname , lock = <NUM_LIT:0> ) <EOL> mlists [ listname ] = mlist <EOL> if command == '<STR_LIT>' : <EOL> msg . Enqueue ( mlist , tolist = <NUM_LIT:1> ) <EOL> elif command == '<STR_LIT>' : <EOL> msg . Enqueue ( mlist , toadmin = <NUM_LIT:1> ) <EOL> elif command == '<STR_LIT>' : <EOL> msg . Enqueue ( mlist , toowner = <NUM_LIT:1> ) <EOL> elif command == '<STR_LIT>' : <EOL> msg . Enqueue ( mlist , torequest = <NUM_LIT:1> ) <EOL> elif command in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if command == '<STR_LIT>' : <EOL> msg [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> msg [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> msg . Enqueue ( mlist , torequest = <NUM_LIT:1> ) <EOL> class Options : <EOL> setuid = <NUM_LIT:1> <EOL> classname = '<STR_LIT>' <EOL> def parseargs ( ) : <EOL> global DEBUGSTREAM <EOL> try : <EOL> opts , args = getopt . getopt ( <EOL> sys . argv [ <NUM_LIT:1> : ] , '<STR_LIT>' , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:version>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> except getopt . error , e : <EOL> usage ( <NUM_LIT:1> , e ) <EOL> options = Options ( ) <EOL> for opt , arg in opts : <EOL> if opt in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> usage ( <NUM_LIT:0> ) <EOL> elif opt in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> print >> sys . stderr , __version__ <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> elif opt in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> options . setuid = <NUM_LIT:0> <EOL> elif opt in ( '<STR_LIT:-c>' , '<STR_LIT>' ) : <EOL> options . classname = arg <EOL> elif opt in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> DEBUGSTREAM = sys . stderr <EOL> if len ( args ) < <NUM_LIT:1> : <EOL> localspec = '<STR_LIT>' <EOL> remotespec = '<STR_LIT>' <EOL> elif len ( args ) < <NUM_LIT:2> : <EOL> localspec = args [ <NUM_LIT:0> ] <EOL> remotespec = '<STR_LIT>' <EOL> elif len ( args ) < <NUM_LIT:3> : <EOL> localspec = args [ <NUM_LIT:0> ] <EOL> remotespec = args [ <NUM_LIT:1> ] <EOL> else : <EOL> usage ( <NUM_LIT:1> , '<STR_LIT>' % COMMASPACE . join ( args ) ) <EOL> i = localspec . find ( '<STR_LIT::>' ) <EOL> if i < <NUM_LIT:0> : <EOL> usage ( <NUM_LIT:1> , '<STR_LIT>' % localspec ) <EOL> options . localhost = localspec [ : i ] <EOL> try : <EOL> options . localport = int ( localspec [ i + <NUM_LIT:1> : ] ) <EOL> except ValueError : <EOL> usage ( <NUM_LIT:1> , '<STR_LIT>' % localspec ) <EOL> i = remotespec . find ( '<STR_LIT::>' ) <EOL> if i < <NUM_LIT:0> : <EOL> usage ( <NUM_LIT:1> , '<STR_LIT>' % remotespec ) <EOL> options . remotehost = remotespec [ : i ] <EOL> try : <EOL> options . remoteport = int ( remotespec [ i + <NUM_LIT:1> : ] ) <EOL> except ValueError : <EOL> usage ( <NUM_LIT:1> , '<STR_LIT>' % remotespec ) <EOL> return options <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> options = parseargs ( ) <EOL> if options . setuid : <EOL> try : <EOL> import pwd <EOL> except ImportError : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> nobody = pwd . getpwnam ( '<STR_LIT>' ) [ <NUM_LIT:2> ] <EOL> try : <EOL> os . setuid ( nobody ) <EOL> except OSError , e : <EOL> if e . errno != errno . EPERM : raise <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> classname = options . classname <EOL> if "<STR_LIT:.>" in classname : <EOL> lastdot = classname . rfind ( "<STR_LIT:.>" ) <EOL> mod = __import__ ( classname [ : lastdot ] , globals ( ) , locals ( ) , [ "<STR_LIT>" ] ) <EOL> classname = classname [ lastdot + <NUM_LIT:1> : ] <EOL> else : <EOL> import __main__ as mod <EOL> class_ = getattr ( mod , classname ) <EOL> proxy = class_ ( ( options . localhost , options . localport ) , <EOL> ( options . remotehost , options . remoteport ) ) <EOL> try : <EOL> asyncore . loop ( ) <EOL> except KeyboardInterrupt : <EOL> pass </s>
