<s> """<STR_LIT>""" <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> from UserDict import UserDict <EOL> from gevent import socket , monkey , Timeout <EOL> monkey . patch_all ( ) <EOL> from gevent . server import StreamServer <EOL> import ssl <EOL> from ssl import CERT_NONE <EOL> from . channel import SMTPChannel <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , '<STR_LIT>' ] <EOL> class ConnectionTimeout ( Exception ) : <EOL> pass <EOL> NEWLINE = '<STR_LIT:\n>' <EOL> EMPTYSTRING = '<STR_LIT>' <EOL> COMMASPACE = '<STR_LIT:U+002CU+0020>' <EOL> class SSLSettings ( UserDict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , keyfile = None , certfile = None , <EOL> ssl_version = '<STR_LIT>' , ca_certs = None , <EOL> do_handshake_on_connect = True , cert_reqs = CERT_NONE , <EOL> suppress_ragged_eofs = True , ciphers = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> UserDict . __init__ ( self ) <EOL> self . data . update ( dict ( keyfile = keyfile , <EOL> certfile = certfile , <EOL> server_side = True , <EOL> ssl_version = getattr ( ssl , ssl_version , ssl . PROTOCOL_SSLv23 ) , <EOL> ca_certs = ca_certs , <EOL> do_handshake_on_connect = do_handshake_on_connect , <EOL> cert_reqs = cert_reqs , <EOL> suppress_ragged_eofs = suppress_ragged_eofs , <EOL> ciphers = ciphers ) ) <EOL> class SMTPServer ( StreamServer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , localaddr = None , remoteaddr = None , <EOL> timeout = <NUM_LIT> , data_size_limit = <NUM_LIT> , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . relay = bool ( remoteaddr ) <EOL> self . remoteaddr = remoteaddr <EOL> self . localaddr = localaddr <EOL> if not self . localaddr : <EOL> self . localaddr = ( '<STR_LIT:127.0.0.1>' , <NUM_LIT> ) <EOL> self . ssl = None <EOL> self . timeout = int ( timeout ) <EOL> self . data_size_limit = int ( data_size_limit ) <EOL> if '<STR_LIT>' in kwargs : <EOL> self . ssl = SSLSettings ( ** kwargs ) <EOL> super ( SMTPServer , self ) . __init__ ( self . localaddr , self . handle ) <EOL> def handle ( self , sock , addr ) : <EOL> logger . debug ( '<STR_LIT>' , * addr [ : <NUM_LIT:2> ] ) <EOL> if self . relay and not addr [ <NUM_LIT:0> ] in self . remoteaddr : <EOL> logger . debug ( '<STR_LIT>' , * addr [ : <NUM_LIT:2> ] ) <EOL> return <EOL> try : <EOL> with Timeout ( self . timeout , ConnectionTimeout ) : <EOL> sc = SMTPChannel ( self , sock , addr , self . data_size_limit ) <EOL> while not sc . closed : <EOL> sc . handle_read ( ) <EOL> except ConnectionTimeout : <EOL> logger . warn ( '<STR_LIT>' , * addr [ : <NUM_LIT:2> ] ) <EOL> try : <EOL> sc . smtp_TIMEOUT ( ) <EOL> except Exception as err : <EOL> logger . debug ( err ) <EOL> except Exception as err : <EOL> logger . error ( err ) <EOL> def process_message ( self , peer , mailfrom , rcpttos , data ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def process_rcpt ( self , address ) : <EOL> """<STR_LIT>""" <EOL> pass <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> logger . debug ( '<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> logger . debug ( '<STR_LIT>' ) <EOL> refused = e . recipients <EOL> except ( socket . error , smtplib . SMTPException ) , e : <EOL> logger . debug ( '<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 </s>
