ϪfaPLdZddlZddlZddlmZddlmZmZddlm Z ddl m Z ddl m Z mZddlmZmZdd lmZmZGd d ZeeGd d Zeej.GddZGddej2ZGddej6Zy)z Mail service support. N) implementer)internetservice)Portal)defer) protocolssmtp)IAliasableDomainIDomain)logutilceZdZdZdZdZdZeddZdZ dZ d Z d Z d Z d Zd efdZd efdZddZdZdZdZdZdZdZdZdZdZdZdZy)DomainWithDefaultDictz A simulated dictionary for mapping domain names to domain objects with a default value for non-existing keys. @ivar domains: See L{__init__} @ivar default: See L{__init__} c ||_||_y)z @type domains: L{dict} of L{bytes} -> L{IDomain} provider @param domains: A mapping of domain name to domain object. @type default: L{IDomain} provider @param default: The default domain. N)domainsdefault)selfrrs 3/usr/lib/python3/dist-packages/twisted/mail/mail.py__init__zDomainWithDefaultDict.__init__$s  c||_y)z Set the default domain. @type domain: L{IDomain} provider @param domain: The default domain. N)r)rdomains rsetDefaultDomainz&DomainWithDefaultDict.setDefaultDomain/s  rc<tjdtdy) Test for the presence of a domain name in this dictionary. This always returns C{True} because a default value will be returned if the name doesn't exist in this dictionary. @type name: L{bytes} @param name: A domain name. @rtype: L{bool} @return: C{True} to indicate that the domain name is in this dictionary. zotwisted.mail.mail.DomainWithDefaultDict.has_key was deprecated in Twisted 16.3.0. Use the `in` keyword instead.)category stacklevel)warningswarnDeprecationWarningrnames rhas_keyzDomainWithDefaultDict.has_key8s"   ,(  rNc,|}|D]}|||< |S)a Create a new L{DomainWithDefaultDict} with the specified keys. @type keys: iterable of L{bytes} @param keys: Domain names to serve as keys in the new dictionary. @type value: L{None} or L{IDomain} provider @param value: A domain object to serve as the value for all new keys in the dictionary. @rtype: L{DomainWithDefaultDict} @return: A new dictionary. )klasskeysvaluedks rfromkeyszDomainWithDefaultDict.fromkeysOs( G AAaD rcy)rrr'r#s r __contains__z"DomainWithDefaultDict.__contains__csrcN|jj||jS)a- Look up a domain name and, if it is present, return the domain object associated with it. Otherwise return the default domain. @type name: L{bytes} @param name: A domain name. @rtype: L{IDomain} provider or L{None} @return: A domain object. )rgetrr#s r __getitem__z!DomainWithDefaultDict.__getitem__ss||dll33rc"||j|<y)z Associate a domain object with a domain name in this dictionary. @type name: L{bytes} @param name: A domain name. @type value: L{IDomain} provider @param value: A domain object. Nr)rr$r*s r __setitem__z!DomainWithDefaultDict.__setitem__s# Trc|j|=y)z Delete the entry for a domain name in this dictionary. @type name: L{bytes} @param name: A domain name. Nr4r#s r __delitem__z!DomainWithDefaultDict.__delitem__s LL rc,t|jS)z Return an iterator over the domain names in this dictionary. @rtype: iterator over L{bytes} @return: An iterator over the domain names. )iterrrs r__iter__zDomainWithDefaultDict.__iter__sDLL!!rc,t|jS)z Return the number of domains in this dictionary. @rtype: L{int} @return: The number of domains in this dictionary. )lenrr:s r__len__zDomainWithDefaultDict.__len__s4<<  rreturnc"d|jdS)z Build an informal string representation of this dictionary. @rtype: L{bytes} @return: A string containing the mapping of domain names to domain objects. zr4r:s r__str__zDomainWithDefaultDict.__str__s)a88rc"d|jdS)z Build an "official" string representation of this dictionary. @rtype: L{bytes} @return: A pseudo-executable string describing the underlying domain mapping of this object. zDomainWithDefaultDict()r4r:s r__repr__zDomainWithDefaultDict.__repr__s( ~Q77rc:|jj||S)a Look up a domain name in this dictionary. @type key: L{bytes} @param key: A domain name. @type default: L{IDomain} provider or L{None} @param default: A domain object to be returned if the domain name is not in this dictionary. @rtype: L{IDomain} provider or L{None} @return: The domain object associated with the domain name if it is in this dictionary. Otherwise, the default value. )rr1rkeyrs rr1zDomainWithDefaultDict.gets||W--rc^t|jj|jS)z Make a copy of this dictionary. @rtype: L{DomainWithDefaultDict} @return: A copy of this dictionary. )rrcopyrr:s rrJzDomainWithDefaultDict.copys"%T\\%6%6%8$,,GGrc6|jjS)a Return an iterator over the domain name/domain object pairs in the dictionary. Using the returned iterator while adding or deleting entries from the dictionary may result in a L{RuntimeError} or failing to iterate over all the domain name/domain object pairs. @rtype: iterator over 2-L{tuple} of (E{1}) L{bytes}, (E{2}) L{IDomain} provider or L{None} @return: An iterator over the domain name/domain object pairs. )r iteritemsr:s rrLzDomainWithDefaultDict.iteritemss||%%''rc6|jjS)ae Return an iterator over the domain names in this dictionary. Using the returned iterator while adding or deleting entries from the dictionary may result in a L{RuntimeError} or failing to iterate over all the domain names. @rtype: iterator over L{bytes} @return: An iterator over the domain names. )riterkeysr:s rrNzDomainWithDefaultDict.iterkeyss||$$&&rc6|jjS)a Return an iterator over the domain objects in this dictionary. Using the returned iterator while adding or deleting entries from the dictionary may result in a L{RuntimeError} or failing to iterate over all the domain objects. @rtype: iterator over L{IDomain} provider or L{None} @return: An iterator over the domain objects. )r itervaluesr:s rrPz DomainWithDefaultDict.itervaluess||&&((rc6|jjS)z Return a list of all domain names in this dictionary. @rtype: L{list} of L{bytes} @return: The domain names in this dictionary. )rr)r:s rr)zDomainWithDefaultDict.keyss||  ""rc6|jjS)z Return a list of all domain objects in this dictionary. @rtype: L{list} of L{IDomain} provider or L{None} @return: The domain objects in this dictionary. )rvaluesr:s rrSzDomainWithDefaultDict.values s||""$$rc6|jjS)a Return a list of all domain name/domain object pairs in this dictionary. @rtype: L{list} of 2-L{tuple} of (E{1}) L{bytes}, (E{2}) L{IDomain} provider or L{None} @return: Domain name/domain object pairs in this dictionary. )ritemsr:s rrUzDomainWithDefaultDict.itemss||!!##rc6|jjS)aE Remove a random domain name/domain object pair from this dictionary and return it as a tuple. @rtype: 2-L{tuple} of (E{1}) L{bytes}, (E{2}) L{IDomain} provider or L{None} @return: A domain name/domain object pair. @raise KeyError: When this dictionary is empty. )rpopitemr:s rrWzDomainWithDefaultDict.popitems||##%%rc8|jj|S)a Update this dictionary with domain name/domain object pairs from another dictionary. When this dictionary contains a domain name which is in the other dictionary, its value will be overwritten. @type other: L{dict} of L{bytes} -> L{IDomain} provider and/or L{bytes} -> L{None} @param other: Another dictionary of domain name/domain object pairs. @rtype: L{None} @return: None. )rupdate)rothers rrYzDomainWithDefaultDict.update*s||""5))rc6|jjS)zh Remove all items from this dictionary. @rtype: L{None} @return: None. )rclearr:s rr\zDomainWithDefaultDict.clear;s||!!##rc:|jj||S)a Return the domain object associated with the domain name if it is present in this dictionary. Otherwise, set the value for the domain name to the default and return that value. @type key: L{bytes} @param key: A domain name. @type default: L{IDomain} provider @param default: A domain object. @rtype: L{IDomain} provider or L{None} @return: The domain object associated with the domain name. )r setdefaultrGs rr^z DomainWithDefaultDict.setdefaultDs||&&sG44rN)__name__ __module__ __qualname____doc__rrr% classmethodr-r/r2r5r7r;r>strrBrEr1rJrLrNrPr)rSrUrWrYr\r^r'rrrrs .& 4 #"!998#8."H ( ' )#% $ &*"$5rrc(eZdZdZdZdZdZdZy) BounceDomainzN A domain with no users. This can be used to block off a domain. c,tj|)z Raise an exception to indicate that the user does not exist in this domain. @type user: L{User} @param user: A user. @raise SMTPBadRcpt: When the given user does not exist in this domain. )r SMTPBadRcpt)rusers rexistszBounceDomain.exists^st$$rcy)aq Indicate that this domain will not relay. @type user: L{Address} @param user: The destination address. @type protocol: L{Protocol } @param protocol: The protocol over which the message to be relayed is being received. @rtype: L{bool} @return: C{False}. Fr')rrjprotocols r willRelayzBounceDomain.willRelayjsrcy)z Ignore attempts to add a user to this domain. @type user: L{bytes} @param user: A username. @type password: L{bytes} @param password: A password. Nr')rrjpasswords raddUserzBounceDomain.addUserzs rcgS)z{ Return no credentials checkers for this domain. @rtype: L{list} @return: The empty list. r'r:s rgetCredentialsCheckersz#BounceDomain.getCredentialsCheckerss  rN)r`rarbrcrkrnrqrsr'rrrgrgVs %  rrgc(eZdZdZdZdZdZdZy) FileMessagez A message receiver which delivers a message to a file. @ivar fp: See L{__init__}. @ivar name: See L{__init__}. @ivar finalName: See L{__init__}. c.||_||_||_y)a @type fp: file-like object @param fp: The file in which to store the message while it is being received. @type name: L{bytes} @param name: The full path name of the temporary file. @type finalName: L{bytes} @param finalName: The full path name that should be given to the file holding the message after it has been fully received. N)fpr$ finalName)rrwr$rxs rrzFileMessage.__init__s "rc@|jj|dzy)zx Write a received line to the file. @type line: L{bytes} @param line: A received line.  N)rwwrite)rlines r lineReceivedzFileMessage.lineReceiveds  dUl#rc|jjtj|j|j t j|j S)z At the end of message, rename the file holding the message to its final name. @rtype: L{Deferred} which successfully results in L{bytes} @return: A deferred which returns the final name of the file. )rwcloseosrenamer$rxrsucceedr:s r eomReceivedzFileMessage.eomReceiveds:   $))T^^,}}T^^,,rcv|jjtj|jy)zI Delete the file holding the partially received message. N)rwrrremover$r:s rconnectionLostzFileMessage.connectionLosts!   $))rN)r`rarbrcrr}rrr'rrrurus#"$ -rrucZeZdZdZdZdZdZdZdZdZ dZ dZ dZ dZ dZd Zd Zd Zy) MailServicea An email service. @type queue: L{Queue} or L{None} @ivar queue: A queue for outgoing messages. @type domains: L{dict} of L{bytes} -> L{IDomain} provider @ivar domains: A mapping of supported domain name to domain object. @type portals: L{dict} of L{bytes} -> L{Portal} @ivar portals: A mapping of domain name to authentication portal. @type aliases: L{None} or L{dict} of L{bytes} -> L{IAlias} provider @ivar aliases: A mapping of domain name to alias. @type smtpPortal: L{Portal} @ivar smtpPortal: A portal for authentication for the SMTP server. @type monitor: L{FileMonitoringService} @ivar monitor: A service to monitor changes to files. Nctjj|tit |_i|_t|_|jj|t||_ y)z. Initialize the mail service. N) r MultiServicerrrgrportalsFileMonitoringServicemonitorsetServiceParentr smtpPortalr:s rrzMailService.__init__sV %%d+,R@  ,.  %%d+ ,rc,tj|S)z{ Create a POP3 protocol factory. @rtype: L{POP3Factory} @return: A POP3 protocol factory. )r POP3Factoryr:s rgetPOP3FactoryzMailService.getPOP3Factorys$$T**rcBtj||jS)z Create an SMTP protocol factory. @rtype: L{SMTPFactory } @return: An SMTP protocol factory. )r SMTPFactoryrr:s rgetSMTPFactoryzMailService.getSMTPFactorys$$T4??;;rcBtj||jS)z Create an ESMTP protocol factory. @rtype: L{ESMTPFactory } @return: An ESMTP protocol factory. )r ESMTPFactoryrr:s rgetESMTPFactoryzMailService.getESMTPFactorys%%dDOO<z+MailService.requestAvatar..@sr)r IMessageDeliveryrESMTPDomainDeliveryNotImplementedError)ravatarIdmind interfacesas r requestAvatarzMailService.requestAvatar(s@,  J .--dH=A((!\9 9!##rc |j|S)z Find the portal for a domain. @type name: L{bytes} @param name: A domain name. @rtype: L{Portal} @return: A portal. rr#s r lookupPortalzMailService.lookupPortalCs||D!!rc |jdS)z Return the portal for the default domain. The default domain is named ''. @rtype: L{Portal} @return: The portal for the default domain. rr:s r defaultPortalzMailService.defaultPortalOs||Br)r`rarbrcrrrrrrrrrrrrrrr'rrrrsO. EGGGJ '+<=/"$6 "  rrc<eZdZdZdZdZdZdZd dZdZ dZ y ) ra_ A service for monitoring changes to files. @type files: L{list} of L{list} of (E{1}) L{float}, (E{2}) L{bytes}, (E{3}) callable which takes a L{bytes} argument, (E{4}) L{float} @ivar files: Information about files to be monitored. Each list entry provides the following information for a file: interval in seconds between checks, filename, callback function, time of last modification to the file. @type intervals: L{_IntervalDifferentialIterator } @ivar intervals: Intervals between successive file checks. @type _call: L{IDelayedCall } provider @ivar _call: The next scheduled call to check a file. @type index: L{int} @ivar index: The index of the next file to be checked. cZg|_ttjgd|_y)z9 Initialize the file monitoring service. <N)filesr9r IntervalDifferential intervalsr:s rrzFileMonitoringService.__init__rs$ d77B?@rcbtjj||jy)z4 Start the file monitoring service. N)rService startService _setupMonitorr:s rrz"FileMonitoringService.startServiceys" $$T* rcddlm}|jj\}|_|j ||j |_y)z4 Schedule the next monitoring call. r)reactorN)twisted.internetrrnextindex callLater_monitor_call)rrts rrz#FileMonitoringService._setupMonitors7 -++- 4:&&q$--8 rctjj||jr"|jj d|_yy)z3 Stop the file monitoring service. N)rr stopServicercancelr:s rrz!FileMonitoringService.stopServices9 ##D) :: JJ   DJ rc tjj|}|jj ||||g|j j|y#t$rd}YHwxYw)av Start monitoring a file for changes. @type name: L{bytes} @param name: The name of a file to monitor. @type callback: callable which takes a L{bytes} argument @param callback: The function to call when the file has changed. @type interval: L{float} @param interval: The interval in seconds between checks. rN)rpathgetmtime BaseExceptionrappendr addInterval)rr$callbackintervalmtimes r monitorFilez!FileMonitoringService.monitorFiles` GG$$T*E 8T8U;< ""8, E sA A*)A*ctt|jD]Q}||j|dk(s|jj |j|d|j|=yy)zi Stop monitoring a file. @type name: L{bytes} @param name: A file name. rrN)ranger=rrremoveInterval)rr$is r unmonitorFilez#FileMonitoringService.unmonitorFilesbs4::' Atzz!}Q''--djjmA.>?JJqM  rcnd|_|j|j|jdd\}}} tjj |}||kDr !D(E gg&&t,U{4& <=>,/ 4::&q)  !  sB&& B43B4N) ) r`rarbrcrrrrrrrr'rrrr[s+,A9-( rr)rcrr zope.interfacertwisted.applicationrrtwisted.cred.portalrrr twisted.mailrr twisted.mail.interfacesr r twisted.pythonr r rrgIMessagerurr TimerServicerr'rrrs   &1&#)=$x5x5v  W666r T]]444nP '&&P fgH11gr