o baP@sdZddlZddlZddlmZddlmZmZddlm Z ddl m Z ddl m Z mZddlmZmZdd lmZmZGd d d ZeeGd d d ZeejGdddZGdddejZGdddejZdS)z Mail service support. N) implementer)internetservice)Portal)defer) protocolssmtp)IAliasableDomainIDomain)logutilc@seZdZdZddZddZddZed4d d Zd d Z d dZ ddZ ddZ ddZ ddZdefddZdefddZd4ddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3ZdS)5DomainWithDefaultDictz 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__} cCs||_||_dS)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)selfrrr3/usr/lib/python3/dist-packages/twisted/mail/mail.py__init__$s zDomainWithDefaultDict.__init__cC ||_dS)z Set the default domain. @type domain: L{IDomain} provider @param domain: The default domain. N)r)rdomainrrrsetDefaultDomain/ z&DomainWithDefaultDict.setDefaultDomaincCstjdtdddS) 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)warningswarnDeprecationWarningrnamerrrhas_key8s zDomainWithDefaultDict.has_keyNcCs|}|D]}|||<q|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. r)klasskeysvaluedkrrrfromkeysOs zDomainWithDefaultDict.fromkeyscCdS)rrrr rrr __contains__cz"DomainWithDefaultDict.__contains__cCs|j||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 rrr __getitem__ss z!DomainWithDefaultDict.__getitem__cCs||j|<dS)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%rrr __setitem__s z!DomainWithDefaultDict.__setitem__cCs |j|=dS)z Delete the entry for a domain name in this dictionary. @type name: L{bytes} @param name: A domain name. Nr.r rrr __delitem__s z!DomainWithDefaultDict.__delitem__cC 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. )iterrrrrr__iter__rzDomainWithDefaultDict.__iter__cCr1)z Return the number of domains in this dictionary. @rtype: L{int} @return: The number of domains in this dictionary. )lenrr3rrr__len__rzDomainWithDefaultDict.__len__returncCd|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. zr.r3rrr__str__zDomainWithDefaultDict.__str__cCr8)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()r.r3rrr__repr__r;zDomainWithDefaultDict.__repr__cC|j||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. )rr,rkeyrrrrr,zDomainWithDefaultDict.getcCst|j|jS)z Make a copy of this dictionary. @rtype: L{DomainWithDefaultDict} @return: A copy of this dictionary. )r rcopyrr3rrrrBszDomainWithDefaultDict.copycC |jS)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 iteritemsr3rrrrDs zDomainWithDefaultDict.iteritemscCrC)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. )riterkeysr3rrrrE zDomainWithDefaultDict.iterkeyscCrC)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 itervaluesr3rrrrGs z DomainWithDefaultDict.itervaluescCrC)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$r3rrrr$s zDomainWithDefaultDict.keyscCrC)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. )rvaluesr3rrrrH rzDomainWithDefaultDict.valuescCrC)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. )ritemsr3rrrrI zDomainWithDefaultDict.itemscCrC)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. )rpopitemr3rrrrKrFzDomainWithDefaultDict.popitemcCs |j|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)rotherrrrrL*s zDomainWithDefaultDict.updatecCrC)zh Remove all items from this dictionary. @rtype: L{None} @return: None. )rclearr3rrrrN;rzDomainWithDefaultDict.clearcCr>)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 setdefaultr?rrrrODrAz DomainWithDefaultDict.setdefaultN)__name__ __module__ __qualname____doc__rrr" classmethodr(r*r-r/r0r4r6strr:r=r,rBrDrErGr$rHrIrKrLrNrOrrrrr s6                r c@0eZdZdZddZddZddZdd Zd S) BounceDomainzN A domain with no users. This can be used to block off a domain. cCs t|)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)ruserrrrexists^ zBounceDomain.existscCr))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)rrZprotocolrrr willRelayjr+zBounceDomain.willRelaycCr))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)rrZpasswordrrraddUserzs zBounceDomain.addUsercCsgS)z{ Return no credentials checkers for this domain. @rtype: L{list} @return: The empty list. rr3rrrgetCredentialsCheckerssz#BounceDomain.getCredentialsCheckersN)rQrRrSrTr[r^r`rarrrrrXVs   rXc@rW) 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__}. cCs||_||_||_dS)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)rrcr!rdrrrrs  zFileMessage.__init__cCs|j|ddS)zx Write a received line to the file. @type line: L{bytes} @param line: A received line.  N)rcwrite)rlinerrr lineReceivedszFileMessage.lineReceivedcCs&|jt|j|jt|jS)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. )rccloseosrenamer!rdrsucceedr3rrr eomReceiveds  zFileMessage.eomReceivedcCs|jt|jdS)zI Delete the file holding the partially received message. N)rcrirjremover!r3rrrconnectionLosts zFileMessage.connectionLostN)rQrRrSrTrrhrmrorrrrrbs  rbc@sleZdZdZdZdZdZdZdZddZ ddZ ddZ d d Z d d Z d dZddZddZddZdS) 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. NcCsBtj|tit|_i|_t|_|j |t ||_ dS)z. Initialize the mail service. N) r MultiServicerr rXrportalsFileMonitoringServicemonitorsetServiceParentr smtpPortalr3rrrrs  zMailService.__init__cCs t|S)z{ Create a POP3 protocol factory. @rtype: L{POP3Factory} @return: A POP3 protocol factory. )r POP3Factoryr3rrrgetPOP3FactoryrzMailService.getPOP3FactorycCt||jS)z Create an SMTP protocol factory. @rtype: L{SMTPFactory } @return: An SMTP protocol factory. )r SMTPFactoryrvr3rrrgetSMTPFactoryzMailService.getSMTPFactorycCry)z Create an ESMTP protocol factory. @rtype: L{ESMTPFactory } @return: An ESMTP protocol factory. )r ESMTPFactoryrvr3rrrgetESMTPFactoryr|zMailService.getESMTPFactorycCsTt|}t|j|||j|<||j|<|jr&t|r(| |jdSdSdS)z Add a domain for which the service will accept email. @type name: L{bytes} @param name: A domain name. @type domain: L{IDomain} provider @param domain: A domain object. N) rmapregisterCheckerrarrraliasesr providedBy setAliasGroup)rr!rportalrrr addDomains   zMailService.addDomaincCr)z Set the queue for outgoing emails. @type queue: L{Queue} @param queue: A queue for outgoing messages. N)queue)rrrrrsetQueuerzMailService.setQueuecGs,tj|vrt||}tj|ddfSt)a Return a message delivery for an authenticated SMTP user. @type avatarId: L{bytes} @param avatarId: A string which identifies an authenticated user. @type mind: L{None} @param mind: Unused. @type interfaces: n-L{tuple} of C{zope.interface.Interface} @param interfaces: A group of interfaces one of which the avatar must support. @rtype: 3-L{tuple} of (E{1}) L{IMessageDelivery}, (E{2}) L{ESMTPDomainDelivery}, (E{3}) no-argument callable @return: A tuple of the supported interface, a message delivery, and a logout function. @raise NotImplementedError: When the given interfaces do not include L{IMessageDelivery}. cSsdSrPrrrrr@sz+MailService.requestAvatar..)rIMessageDeliveryrESMTPDomainDeliveryNotImplementedError)ravatarIdmind interfacesarrr requestAvatar(s  zMailService.requestAvatarcCs |j|S)z Find the portal for a domain. @type name: L{bytes} @param name: A domain name. @rtype: L{Portal} @return: A portal. rrr rrr lookupPortalCr\zMailService.lookupPortalcCs |jdS)z Return the portal for the default domain. The default domain is named ''. @rtype: L{Portal} @return: The portal for the default domain. rr3rrr defaultPortalOrJzMailService.defaultPortal)rQrRrSrTrrrrrrvrrxr{r~rrrrrrrrrrps       rpc@sJeZdZdZddZddZddZdd Zdd d Zd dZ ddZ dS)rsa_ 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. cCsg|_ttgd|_dS)z9 Initialize the file monitoring service. <N)filesr2r IntervalDifferential intervalsr3rrrrrszFileMonitoringService.__init__cCstj||dS)z4 Start the file monitoring service. N)rService startService _setupMonitorr3rrrrys  z"FileMonitoringService.startServicecCs0ddlm}|j\}|_|||j|_dS)z4 Schedule the next monitoring call. r)reactorN)twisted.internetrrnextindex callLater_monitor_call)rrtrrrrs z#FileMonitoringService._setupMonitorcCs*tj||jr|jd|_dSdS)z3 Stop the file monitoring service. N)rr stopServicercancelr3rrrrs   z!FileMonitoringService.stopService cCsLztj|}Wn tyd}Ynw|j||||g|j|dS)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)rjpathgetmtime BaseExceptionrappendr addInterval)rr!callbackintervalmtimerrr monitorFiles  z!FileMonitoringService.monitorFilecCsNtt|jD]}||j|dkr$|j|j|d|j|=dSqdS)zi Stop monitoring a file. @type name: L{bytes} @param name: A file name. rrN)ranger5rrremoveInterval)rr!irrr unmonitorFilesz#FileMonitoringService.unmonitorFilecCsd|_|jdurA|j|jdd\}}}ztj|}Wn ty(d}Ynw||krAt|d||j|jd<||| dS)zG Monitor a file and make a callback if it has changed. Nrrz changed, notifying listener) rrrrjrrrr msgr)rr!rrnowrrrrs   zFileMonitoringService._monitorN)r) rQrRrSrTrrrrrrrrrrrrs[s  rs)rTrjrzope.interfacertwisted.applicationrrtwisted.cred.portalrrr twisted.mailrrtwisted.mail.interfacesr r twisted.pythonr r r rXIMessagerbrqrp TimerServicersrrrrs(   =97