ϪffdZddlZddlZddlZddlZddlmZddlm Z ddl m Z ddl m Z mZddlmZddlmZdd lmZmZmZdd lmZdd lmZGd d ZGddeej8ZGddeej<ZGdde j@Z!Gdde!Z"GddZ#GddZ$GddZ%Gdde%Z&dZ'dZ(Gd d!e)Z*Gd"d#e)Z+Gd$d%Z,y)&aI Infrastructure for relaying mail through a smart host. Traditional peer-to-peer email has been increasingly replaced by smart host configurations. Instead of sending mail directly to the recipient, a sender sends mail to a smart host. The smart host finds the mail exchange server for the recipient and sends on the message. N)Type)internet)protocol)Deferred DeferredList)DNSLookupError)connectionDone)bouncerelaysmtp)log)FailurecdeZdZdZdZedZejdZdZe fde ddfd Z y) ManagedRelayerMixinz SMTP Relayer which notifies a manager Notify the manager about successful mail, failed mail and broken connections c||_yNmanager)selfrs ;/usr/lib/python3/dist-packages/twisted/mail/relaymanager.py__init__zManagedRelayerMixin.__init__&s  c|jSr_factoryrs rfactoryzManagedRelayerMixin.factory)s }}rc||_yrr)rvalues rrzManagedRelayerMixin.factory-s  rc|jd}|tjvr'|jj |j |n&|jj |j ||jd=|jd=y)z` called when e-mail has been sent we will always get 0 or 1 addresses. rN)namesr SUCCESSr notifySuccessr notifyFailuremessages)rcoderespnumOk addressesr messages rsentMailzManagedRelayerMixin.sentMail1sd **Q- 4<<  LL & &t||W = LL & &t||W = MM!  JJqMrreasonreturnNcN|jj|jy)zm called when connection is broken notify manager we will try to send no more e-mail N)r notifyDoner)rr,s rconnectionLostz"ManagedRelayerMixin.connectionLost?s  -r) __name__ __module__ __qualname____doc__rpropertyrsetterr+r rr0rrrrsU ^^ 0>.W.$.rrceZdZdZdZy)SMTPManagedRelayeraV An SMTP managed relayer. This managed relayer is an SMTP client which is responsible for sending a set of messages and keeping an attempt manager informed about its progress. @type factory: L{SMTPManagedRelayerFactory} @ivar factory: The factory that created this relayer. This must be set by the factory. cxtj||tjj||g|i|y)a @type messages: L{list} of L{bytes} @param messages: The base filenames of messages to be relayed. @type manager: L{_AttemptManager} @param manager: An attempt manager. @type args: 1-L{tuple} of (0) L{bytes} or 2-L{tuple} of (0) L{bytes}, (1) L{int} @param args: Positional arguments for L{SMTPClient.__init__} @type kw: L{dict} @param kw: Keyword arguments for L{SMTPClient.__init__} N)rrr SMTPRelayerrr%rargskws rrzSMTPManagedRelayer.__init__Ts4 $$T73 ""4?D?B?rNr1r2r3r4rr7rrr9r9Hs  @rr9ceZdZdZdZy)ESMTPManagedRelayerz An ESMTP managed relayer. This managed relayer is an ESMTP client which is responsible for sending a set of messages and keeping an attempt manager informed about its progress. cxtj||tjj||g|i|y)a @type messages: L{list} of L{bytes} @param messages: The base filenames of messages to be relayed. @type manager: L{_AttemptManager} @param manager: An attempt manager. @type args: 3-L{tuple} of (0) L{bytes}, (1) L{None} or L{ClientContextFactory }, (2) L{bytes} or 4-L{tuple} of (0) L{bytes}, (1) L{None} or L{ClientContextFactory }, (2) L{bytes}, (3) L{int} @param args: Positional arguments for L{ESMTPClient.__init__} @type kw: L{dict} @param kw: Keyword arguments for L{ESMTPClient.__init__} N)rrr ESMTPRelayerr<s rrzESMTPManagedRelayer.__init__os4( $$T73 ##D(@T@R@rNr?r7rrrArAgs ArrAc2eZdZUdZeZded<dZdZdZ y)SMTPManagedRelayerFactorya A factory to create an L{SMTPManagedRelayer}. This factory creates a managed relayer which relays a set of messages over SMTP and informs an attempt manager of its progress. @ivar messages: See L{__init__} @ivar manager: See L{__init__} @type protocol: callable which returns L{SMTPManagedRelayer} @ivar protocol: A callable which returns a managed relayer for SMTP. See L{SMTPManagedRelayer.__init__} for parameters to the callable. @type pArgs: 1-L{tuple} of (0) L{bytes} or 2-L{tuple} of (0) L{bytes}, (1), L{int} @ivar pArgs: Positional arguments for L{SMTPClient.__init__} @type pKwArgs: L{dict} @ivar pKwArgs: Keyword arguments for L{SMTPClient.__init__} zType[protocol.Protocol]rc<||_||_||_||_y)a @type messages: L{list} of L{bytes} @param messages: The base filenames of messages to be relayed. @type manager: L{_AttemptManager} @param manager: An attempt manager. @type args: 1-L{tuple} of (0) L{bytes} or 2-L{tuple} of (0) L{bytes}, (1), L{int} @param args: Positional arguments for L{SMTPClient.__init__} @type kw: L{dict} @param kw: Keyword arguments for L{SMTPClient.__init__} N)r%rpArgspKwArgsr<s rrz"SMTPManagedRelayerFactory.__init__s !    rc|j|j|jg|ji|j}||_|S)a  Create an L{SMTPManagedRelayer}. @type addr: L{IAddress } provider @param addr: The address of the SMTP server. @rtype: L{SMTPManagedRelayer} @return: A managed relayer for SMTP. )rr%rrGrHr)raddrrs r buildProtocolz'SMTPManagedRelayerFactory.buildProtocolsH!4== MM4<< *.** 8<   rcp|jj||jj|y)aH Notify the attempt manager that a connection could not be established. @type connector: L{IConnector } provider @param connector: A connector. @type reason: L{Failure} @param reason: The reason the connection attempt failed. N)rnotifyNoConnectionr/)r connectorr,s rclientConnectionFailedz0SMTPManagedRelayerFactory.clientConnectionFaileds( ''- %rN) r1r2r3r4r9r__annotations__rrKrOr7rrrErEs#*+=H'<( &rrEc eZdZdZeZdZdZy)ESMTPManagedRelayerFactorya A factory to create an L{ESMTPManagedRelayer}. This factory creates a managed relayer which relays a set of messages over ESMTP and informs an attempt manager of its progress. @type protocol: callable which returns L{ESMTPManagedRelayer} @ivar protocol: A callable which returns a managed relayer for ESMTP. See L{ESMTPManagedRelayer.__init__} for parameters to the callable. @ivar secret: See L{__init__} @ivar contextFactory: See L{__init__} cV||_||_tj|||g|i|y)a @type messages: L{list} of L{bytes} @param messages: The base filenames of messages to be relayed. @type manager: L{_AttemptManager} @param manager: An attempt manager. @type secret: L{bytes} @param secret: A string for the authentication challenge response. @type contextFactory: L{None} or L{ClientContextFactory } @param contextFactory: An SSL context factory. @type args: 1-L{tuple} of (0) L{bytes} or 2-L{tuple} of (0) L{bytes}, (1), L{int} @param args: Positional arguments for L{SMTPClient.__init__} @param kw: Keyword arguments for L{SMTPClient.__init__} N)secretcontextFactoryrEr)rr%rrTrUr=r>s rrz#ESMTPManagedRelayerFactory.__init__s/* ,!**47PTPRPrc|jxr|j|}|j|j|j||jg|j i|j }||_|S)a Create an L{ESMTPManagedRelayer}. @type addr: L{IAddress } provider @param addr: The address of the ESMTP server. @rtype: L{ESMTPManagedRelayer} @return: A managed relayer for ESMTP. )rTrr%rrUrGrHr)rrJsrs rrKz(ESMTPManagedRelayerFactory.buildProtocolsp KK -DKK- 4== MM LL     ZZ  ll   rN)r1r2r3r4rArrrKr7rrrRrRs #HQ2rrRcteZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZy)Queuea. A queue for messages to be relayed. @ivar directory: See L{__init__} @type n: L{int} @ivar n: A number used to form unique filenames. @type waiting: L{dict} of L{bytes} @ivar waiting: The base filenames of messages waiting to be relayed. @type relayed: L{dict} of L{bytes} @ivar relayed: The base filenames of messages in the process of being relayed. @type noisy: L{bool} @ivar noisy: A flag which determines whether informational log messages will be generated (C{True}) or not (C{False}). Tc2||_|jy)z Initialize non-volatile state. @type directory: L{bytes} @param directory: The pathname of the directory holding messages in the queue. N) directory_init)rr[s rrzQueue.__init__*s# rcNd|_i|_i|_|jy)z, Initialize volatile state. rN)nwaitingrelayed readDirectoryrs rr\z Queue._init5s%   rcd|jiS)z Create a representation of the non-volatile state of the queue. @rtype: L{dict} mapping L{bytes} to L{object} @return: The non-volatile state of the queue. r[)r[rs r __getstate__zQueue.__getstate__>sT^^,,rcZ|jj||jy)z Restore the non-volatile state of the queue and recreate the volatile state. @type state: L{dict} mapping L{bytes} to L{object} @param state: The non-volatile state of the queue. N)__dict__updater\rstates r __setstate__zQueue.__setstate__Gs U# rctj|jD]}|dddk7r |j|dd!y)z> Scan the message directory for new messages. N-D)oslistdirr[ addMessagerr*s rrazQueue.readDirectoryRsCzz$..1 *Grs|t# OOGCRL )  *rc6|jjS)z Return the base filenames of messages waiting to be relayed. @rtype: L{list} of L{bytes} @return: The base filenames of messages waiting to be relayed. )r_keysrs r getWaitingzQueue.getWaiting\s||  ""rc2t|jdkDS)z Return an indication of whether the queue has messages waiting to be relayed. @rtype: L{bool} @return: C{True} if messages are waiting to be relayed. C{False} otherwise. r)lenr_rs r hasWaitingzQueue.hasWaitinges4<< 1$$rc6|jjS)z Return the base filenames of messages in the process of being relayed. @rtype: L{list} of L{bytes} @return: The base filenames of messages in the process of being relayed. )r`rrrs r getRelayedzQueue.getRelayedps||  ""rc<|j|=d|j|<y)z Mark a message as being relayed. @type message: L{bytes} @param message: The base filename of a message. N)r_r`rps r setRelayingzQueue.setRelayingz LL ! ! Wrc<|j|=d|j|<y)z Mark a message as waiting to be relayed. @type message: L{bytes} @param message: The base filename of a message. rzN)r`r_rps r setWaitingzQueue.setWaitingr|rc||jvr8d|j|<|jrtjd|zdzyyy)z Mark a message as waiting to be relayed unless it is in the process of being relayed. @type message: L{bytes} @param message: The base filename of a message. rzzSet  waitingN)r`r_noisyr msgrps rrozQueue.addMessagesE $,, &$%DLL !zz(:56 'rctjj|}tj|j |dztj|j |dz|j |=y)z Remove a message from the queue. @type message: L{bytes} @param message: The base filename of a message. rl-HN)rmpathbasenameremovegetPathr`rps rdonez Queue.donesX''""7+ $,,w'$./ $,,w'$./ LL !rcVtjj|j|S)z Return the full base pathname of a message in the queue. @type message: L{bytes} @param message: The base filename of a message. @rtype: L{bytes} @return: The full base pathname of the message. )rmrjoinr[rps rrz Queue.getPathsww||DNNG44rcz|j|5}tj|cdddS#1swYyxYw)a Get the envelope for a message. @type message: L{bytes} @param message: The base filename of a message. @rtype: L{list} of two L{bytes} @return: A list containing the origination and destination addresses for the message. N)getEnvelopeFilepickleload)rr*fs r getEnvelopezQueue.getEnvelopes4 ! !' * "a;;q> " " "s1:cpttjj|j|dzdS)z Return the envelope file for a message in the queue. @type message: L{bytes} @param message: The base filename of a message. @rtype: file @return: The envelope file for the message. rrb)openrmrrr[rps rrzQueue.getEnvelopeFiles(BGGLL4@$GGrc tjdtjd|jdt |}|jdz|_t tj j|j|dzd}tj j|j|dz}tj j|j|dz}t |d}ddl m }|||||fS) z Create a new message in the queue. @rtype: 2-L{tuple} of (0) file, (1) L{FileMessage} @return: The envelope file and a message receiver for a new message in the queue. _rzrwbz-Crlr) FileMessage) rmgetpidtimer^idrrrr[twisted.mail.mailr)rfname headerFile tempFilename finalFilename messageFilers rcreateNewMessagezQueue.createNewMessages99;-q Qtvvha4zB!"'',,t~~ut|DdK ww||DNNEDLA  T^^UT\B <. 1;{L-PPPrN)r1r2r3r4rrr\rcrirarsrvrxr{r~rorrrrrr7rrrYrYs`( E - *# %#"" 7 " 5 " HQrrYc<eZdZdZd dZdZdZdZdZdZ d Z y) _AttemptManagera* A manager for an attempt to relay a set of messages to a mail exchange server. @ivar manager: See L{__init__} @type _completionDeferreds: L{list} of L{Deferred} @ivar _completionDeferreds: Deferreds which are to be notified when the attempt to relay is finished. NcL||_g|_||_|sddlm}||_y)a @type manager: L{SmartHostSMTPRelayingManager} @param manager: A smart host. @type noisy: L{bool} @param noisy: A flag which determines whether informational log messages will be generated (L{True}) or not (L{False}). @type reactor: L{IReactorTime } provider @param reactor: A reactor which will be used to schedule delayed calls. rreactorN)r_completionDeferredsrtwisted.internetr)rrrrs rrz_AttemptManager.__init__s' $&!  0 rcf|jjt|jdS)z Return a deferred which will fire when the attempt to relay is finished. @rtype: L{Deferred} @return: A deferred which will fire when the attempt to relay is finished. )rappendrrs rgetCompletionDeferredz%_AttemptManager.getCompletionDeferreds+ !!((4((,,rc|jj|jtjj ||jj j|y)aY Remove a message from the relay queue and from the smart host's list of messages being relayed. @type relay: L{SMTPManagedRelayerFactory} @param relay: The factory for the relayer which sent the message. @type message: L{bytes} @param message: The path of the file holding the message. N)rmanagedrrmrrqueuerrr r*s r_finishz_AttemptManager._finishsH U#**277+;+;G+DE (rcp|jrtjd|z|j||y)aD Remove a message from the relay queue after it has been successfully sent. @type relay: L{SMTPManagedRelayerFactory} @param relay: The factory for the relayer which sent the message. @type message: L{bytes} @param message: The path of the file holding the message. z'success sending %s, removing from queueN)rr rrrs rr#z_AttemptManager.notifySuccesss+ :: GG=G H UG$rcF|jrtjd|ztjj |}|j jj|5}tj|\}}dddtjt|j jj|dz\}}}|j jj\}}|5tj ||g|ddd|j#D]}|j%||j'|j)||j jj|y#1swY xYw#1swYxYw)a7 Generate a bounce message for a message which cannot be relayed. @type relay: L{SMTPManagedRelayerFactory} @param relay: The factory for the relayer responsible for the message. @type message: L{bytes} @param message: The path of the file holding the message. zcould not relay Nrl)rr rrmrrrrrrrr generateBouncerrrdump splitlines lineReceived eomReceivedr) rr r*fpfrom_to bounceMessageoutgoingMessagelines rr$z_AttemptManager.notifyFailure+sN :: GG&0 1''""7+ \\   / / 8 (B BIE2 (#)#8#8 ##++G4t; .setWaitingp   ( +r) rrrr rrstrr callLaterr)rr msgsr~s rrMz"_AttemptManager.notifyNoConnection^s <<''.D :: GG1CI= > , r:t||/A/A4H LL  '  GG> ?  sBB65B6)TN) r1r2r3r4rrrr#r$r/rMr7rrrrs+ * - ) %A:,(rrcneZdZUdZeZeeje d<dZ dZ d dZ dZ dZdZd Zd Zd Zy) SmartHostSMTPRelayingManagera A smart host which uses SMTP managed relayers to send messages from the relay queue. L{checkState} must be called periodically at which time the state of the relay queue is checked and new relayers are created as needed. In order to relay a set of messages to a mail exchange server, a smart host creates an attempt manager and a managed relayer factory for that set of messages. When a connection is made with the mail exchange server, the managed relayer factory creates a managed relayer to send the messages. The managed relayer reports on its progress to the attempt manager which, in turn, updates the smart host's relay queue and information about its managed relayers. @ivar queue: See L{__init__}. @ivar maxConnections: See L{__init__}. @ivar maxMessagesPerConnection: See L{__init__}. @type fArgs: 3-L{tuple} of (0) L{list} of L{bytes}, (1) L{_AttemptManager}, (2) L{bytes} or 4-L{tuple} of (0) L{list} of L{bytes}, (1) L{_AttemptManager}, (2) L{bytes}, (3) L{int} @ivar fArgs: Positional arguments for L{SMTPManagedRelayerFactory.__init__}. @type fKwArgs: L{dict} @ivar fKwArgs: Keyword arguments for L{SMTPManagedRelayerFactory.__init__}. @type factory: callable which returns L{SMTPManagedRelayerFactory} @ivar factory: A callable which creates a factory for creating a managed relayer. See L{SMTPManagedRelayerFactory.__init__} for parameters to the callable. @type PORT: L{int} @ivar PORT: The port over which to connect to the SMTP server. @type mxcalc: L{None} or L{MXCalculator} @ivar mxcalc: A resource for mail exchange host lookups. @type managed: L{dict} mapping L{SMTPManagedRelayerFactory} to L{list} of L{bytes} @ivar managed: A mapping of factory for a managed relayer to filenames of messages the managed relayer is responsible for. rNcX||_||_i|_||_d|_i|_y)a Initialize a smart host. The default values specify connection limits appropriate for a low-volume smart host. @type queue: L{Queue} @param queue: A relay queue. @type maxConnections: L{int} @param maxConnections: The maximum number of concurrent connections to SMTP servers. @type maxMessagesPerConnection: L{int} @param maxMessagesPerConnection: The maximum number of messages for which a relayer will be given responsibility. r7N)maxConnectionsmaxMessagesPerConnectionrrfArgsfKwArgs)rrrrs rrz%SmartHostSMTPRelayingManager.__init__s0$-(@%    rc@|jj}|d=|S)z Create a representation of the non-volatile state of this object. @rtype: L{dict} mapping L{bytes} to L{object} @return: The non-volatile state of the queue. r)recopy)rdcts rrcz)SmartHostSMTPRelayingManager.__getstate__s"mm  "  N rcH|jj|i|_y)z Restore the non-volatile state of this object and recreate the volatile state. @type state: L{dict} mapping L{bytes} to L{object} @param state: The non-volatile state of the queue. N)rerfrrgs rriz)SmartHostSMTPRelayingManager.__setstate__s U# rc|jjt|j|jk\ry|jj sy|j S)af Check the state of the relay queue and, if possible, launch relayers to handle waiting messages. @rtype: L{None} or L{Deferred} @return: No return value if no further messages can be relayed or a deferred which fires when all of the SMTP connections initiated by this call have disconnected. N)rrarurrrv _checkStateMXrs r checkStatez'SmartHostSMTPRelayingManager.checkStatesP   " t||  3 3 3 zz$$& !!##rc<|jj}|ji}|D]}|jj|\}}tj j |\}}|jdd}t|dk7rtjd|z}|d} |jj||j| gj|jj|t||jt|j z k\sn|j"t%|_g} |j'D](\} } t)||jj*} |j,| | g|j.i|j0} t3t4j6j8| |j | <| j;|j"j=| }|j?d|j?|j@|jB| |jEfd|jE|jF| | | j+tI| S)N@rzzIllegal message destination: c,t|jSr)rname)mxs rz. sBGG rc0j||fdS)Nrz)errback)errrelayAttemptDeferreds rrz. s199#>DQGr)%rrsreverseremailutils parseaddrsplitrur rr{ setdefaultrrrrmxcalc MXCalculator iteritemsrrrrrrrmrrrgetMX addCallback _cbExchangePORT addErrback _ebExchanger)r nextMessages exchangesrrrrrJpartsdomainrelaysrrrconnectSetupDeferredrs @rrz*SmartHostSMTPRelayingManager._checkStateMXs zz,,.   C ..s3IE2..r2JD$JJsA&E5zQ7"<=1XF JJ " "3 '   , 3 3DJJ4F4Fs4K L9~$"5"5DLL8I"IJ  ;; &.DK%//1 0LFD%dDJJ,<,<=G"dll4N4::NNG$'(8(8$$?DLL !#*#@#@#B #';;#4#4V#< , ,-D E , ,T-=-=tyy' R + +G  ! + +D,<,.setWaiting;rrrrrN)r rrrrrr)rfailurerrr~rs rrz(SmartHostSMTPRelayingManager._ebExchange'sY" =FG  , -"j$**dll76KL LL !r)r )r1r2r3r4rErrr ClientFactoryrPrrrrcrirrrrr7rrrrwsN+Z-FGT((( )E D F2  $$$$L3*"rrceZdZdZeZy)SmartHostESMTPRelayingManagera] A smart host which uses ESMTP managed relayers to send messages from the relay queue. @type factory: callable which returns L{ESMTPManagedRelayerFactory} @ivar factory: A callable which creates a factory for creating a managed relayer. See L{ESMTPManagedRelayerFactory.__init__} for parameters to the callable. N)r1r2r3r4rRrr7rrr r Ds)Grr c$|jy)z Prompt a relaying manager to check state. @type manager: L{SmartHostSMTPRelayingManager} @param manager: A relaying manager. N)rrs r _checkStater Rs rc8tj|t|S)a Set up a periodic call to prompt a relaying manager to check state. @type manager: L{SmartHostSMTPRelayingManager} @param manager: A relaying manager. @type delay: L{float} @param delay: The number of seconds between calls. @rtype: L{TimerService } @return: A service which periodically reminds a relaying manager to check state. )r TimerServicer )rdelays rRelayStateHelperr\s   W ==rceZdZdZy)CanonicalNameLoopz An error indicating that when trying to look up a mail exchange host, a set of canonical name records was found which form a cycle and resolution was abandoned. Nr1r2r3r4r7rrrrmrrceZdZdZy)CanonicalNameChainTooLongz An error indicating that when trying to look up a mail exchange host, too many canonical name records which point to other canonical name records were encountered and resolution was abandoned. Nrr7rrrrurrrcFeZdZdZdZdZd dZdZdZd dZ d Z d Z d Z y)ra A utility for looking up mail exchange hosts and tracking whether they are working or not. @type clock: L{IReactorTime } provider @ivar clock: A reactor which will be used to schedule timeouts. @type resolver: L{IResolver } @ivar resolver: A resolver. @type badMXs: L{dict} mapping L{bytes} to L{float} @ivar badMXs: A mapping of non-functioning mail exchange hostname to time at which another attempt at contacting it may be made. @type timeOutBadMX: L{int} @ivar timeOutBadMX: Period in seconds between attempts to contact a non-functioning mail exchange host. @type fallbackToDomain: L{bool} @ivar fallbackToDomain: A flag indicating whether to attempt to use the hostname directly when no mail exchange can be found (C{True}) or not (C{False}). iTNc\i|_| ddlm}|}||_|ddlm}||_y)aU @type resolver: L{IResolver } provider or L{None} @param resolver: A resolver. @type clock: L{IReactorTime } provider or L{None} @param clock: A reactor which will be used to schedule timeouts. Nr)createResolverr)badMXstwisted.names.clientrresolverrrclock)rrrrs rrzMXCalculator.__init__s2   ;%'H  = 9 rc~|jj|jz|jt |<y)z Record that a mail exchange host is not currently functioning. @type mx: L{bytes} @param mx: The hostname of a mail exchange host. N)rseconds timeOutBadMXrrrrs rmarkBadzMXCalculator.markBads. $zz113d6G6GG CGrc> |j|=y#t$rYywxYw)z Record that a mail exchange host is functioning. @type mx: L{bytes} @param mx: The hostname of a mail exchange host. N)rrr s rmarkGoodzMXCalculator.markGoods$  B   s  c|jj|}|j|j|j|j|||j |j ||S)ad Find the name of a host that acts as a mail exchange server for a domain. @type domain: L{bytes} @param domain: A domain name. @type maximumCanonicalChainLength: L{int} @param maximumCanonicalChainLength: The maximum number of unique canonical name records to follow while looking up the mail exchange host. @rtype: L{Deferred} which successfully fires with L{Record_MX} @return: A deferred which succeeds with the MX record for the mail exchange server for the domain or fails if none can be found. )rlookupMailExchanger_filterRecords_cbMXr_ebMX)rrmaximumCanonicalChainLengthmailExchangeDeferreds rrzMXCalculator.getMXsc" $}}??G(()<)<=(( JJ ;  '' F;##rci}|dD]@}|jt|jgj|jB|S)af Organize the records of a DNS response by record name. @type records: 3-L{tuple} of (0) L{list} of L{RRHeader }, (1) L{list} of L{RRHeader }, (2) L{list} of L{RRHeader } @param records: Answer resource records, authority resource records and additional resource records. @rtype: L{dict} mapping L{bytes} to L{list} of L{IRecord } provider @return: A mapping of record name to record payload. r)rrrrpayload)rrecords recordBaganswers rr&zMXCalculator._filterRecordssL aj NF  V[[!12 6 = =fnn M NrcPddlm}m}t}g}|j |g}|r|j } | j |jk(ru|j|t| j} | |vr | |vrtt| S|| }g}n+|r|j| |dz Stt| S| j |jk(r|j!| j"| f|r|r|j%|D]l\} } t| j} | |j&vr| cS|j(j+|j&| z } | dk\s]|j&| =| cS|ddSt|j-d|S)a Try to find the mail exchange host for a domain from the given DNS records. This will attempt to resolve canonical name record results. It can recognize loops and will give up on non-cyclic chains after a specified number of lookups. @type answers: L{dict} mapping L{bytes} to L{list} of L{IRecord } provider @param answers: A mapping of record name to record payload. @type domain: L{bytes} @param domain: A domain name. @type cnamesLeft: L{int} @param cnamesLeft: The number of unique canonical name records left to follow while looking up the mail exchange host. @rtype: L{Record_MX } or L{Failure} @return: An MX record for the mail exchange host or a failure if one cannot be found. rdnserrorrzzNo MX records for ) twisted.namesr2r3setrpopTYPECNAMEaddrrrrrrMXr preferencesortrrr DNSNameError)ranswersr cnamesLeftr2r3 seenAliasesrpertinentRecordsrecord canonicalNamer;hostts rr'zMXCalculator._cbMXs4 -e  ";;vr2%))+F{{cii'' #FKK 0  G+% 3&'8'@AA'.}'=$ "I!#zz-aHH ''@'HII{{cff$  &"3"3V!<=9<  NN &/ "" F6;;'t{{*!MJJ&&(4;;t+<<6 D)!M "Q<? " 5--0B6*.MNO Orctddlmm|jrx|j j t jdj||jj|}fd}fd}|j|||S|jj rtd||S)aX Attempt to use the name of the domain directly when mail exchange lookup fails. @type failure: L{Failure} @param failure: The reason for the lookup failure. @type domain: L{bytes} @param domain: The domain name. @rtype: L{Record_MX } or L{Failure} @return: An MX record for the domain or a failure if the fallback to domain option is not in effect and an error, other than not finding an MX record, occurred during lookup. @raise IOError: When no MX record could be found and the fallback to domain option is not in effect. @raise DNSLookupError: When no MX record could be found and the fallback to domain option is in effect but no address for the domain could be found. rr1z:MX lookup failed; attempting to use hostname ({}) directlyc(j|S)N)r) Record_MX)rJr2s r cbResolvedz&MXCalculator._ebMX..cbResolvedds}}$}//rcN|jjtr)trapr=r)rr3s r ebResolvedz&MXCalculator._ebMX..ebResolvedgs++,$&&rzNo MX found for )r4r2r3fallbackToDomainrKr=r rformatr getHostByName addCallbackscheckOSError)rrrrrIrLr2r3s @@rr(zMXCalculator._ebMX@s. -  LL++ , GGLSS  ++F3A 0 ' NN:z 2H ]]5-- .,VJ78 8r)NN)) r1r2r3r4rrMrr!r#rr&r'r(r7rrrr}s;2L(H $2(NP`/rr)-r4 email.utilsrrmrrtypingrtwisted.applicationrrrtwisted.internet.deferrrtwisted.internet.errorrtwisted.internet.protocolr twisted.mailr r r twisted.pythonr twisted.python.failurerrr;r9rCrArrErRrYrrr r r Exceptionrrrr7rrr^s  (%914,,*'.'.T@,e.?.?@>A-u/A/AA@H& 6 6H&V>!:>BKQKQ\S(S(lJ"J"Z )$@ )>"  rrr