o b1@sdZddlZddlmZddlmZmZddlmZddl m Z m Z m Z m Z mZddlmZGdd d ejejZGd d d Zd d ZdS)z0 Support for creating mail servers with twistd. N)internet)checkersstrcred) endpoints)aliasmailmaildirrelay relaymanager)usagec@seZdZdZdZgdgdgZgdgdgdgdgZd d d Zej d e id Z dZ ddZ ddZddZeZddZeZddZeZddZeZddZeZddZeZdd ZeZd!d"Zd#d$Zd%S)&Optionsa An options list parser for twistd mail. @type synopsis: L{bytes} @ivar synopsis: A description of options for use in the usage message. @type optParameters: L{list} of L{list} of (0) L{bytes}, (1) L{bytes}, (2) L{object}, (3) L{bytes}, (4) L{None} or callable which takes L{bytes} and returns L{object} @ivar optParameters: Information about supported parameters. See L{Options } for details. @type optFlags: L{list} of L{list} of (0) L{bytes}, (1) L{bytes} or L{None}, (2) L{bytes} @ivar optFlags: Information about supported flags. See L{Options } for details. @type _protoDefaults: L{dict} mapping L{bytes} to L{int} @ivar _protoDefaults: A mapping of default service to port. @type compData: L{Completions } @ivar compData: Metadata for the shell tab completion system. @type longdesc: L{bytes} @ivar longdesc: A long description of the plugin for use in the usage message. @type service: L{MailService} @ivar service: The email service. @type last_domain: L{IDomain} provider or L{None} @ivar last_domain: The most recently specified domain. z [options])r RNz[Relay messages according to their envelope 'To', using the given path as a queue directory.)hostnameHNz.The hostname by which to identify this server.)esmtpEz!Use RFC 1425/1869 SMTP extensions)disable-anonymousNz+Disallow non-authenticated SMTP connections)zno-pop3Nz Disable the default POP3 server.)zno-smtpNz Disable the default SMTP server.iiY)pop3smtpr) optActionsa An SMTP / POP3 email server plugin for twistd. Examples: 1. SMTP and POP server twistd mail --maildirdbmdomain=example.com=/tmp/example.com --user=joe=password Starts an SMTP server that only accepts emails to joe@example.com and saves them to /tmp/example.com. Also starts a POP mail server which will allow a client to log in using username: joe@example.com and password: password and collect any email that has been saved in /tmp/example.com. 2. SMTP relay twistd mail --relay=/tmp/mail_queue Starts an SMTP server that accepts emails to any email address and relays them to an appropriate remote SMTP server. Queued emails will be temporarily stored in /tmp/mail_queue. cCs4tj|t|_d|_|jD]}g||<qdS)z: Parse options and create a mail service. N)r r __init__r MailServiceservice last_domain_protoDefaults)selfrr2/usr/lib/python3/dist-packages/twisted/mail/tap.pyrks    zOptions.__init__cCs&ddlm}||t||dS)a Add an endpoint to a service. @type service: L{bytes} @param service: A service, either C{b'smtp'} or C{b'pop3'}. @type description: L{bytes} @param description: An endpoint description string or a TCP port number. rreactorN)twisted.internetrappendrserverFromString)rr descriptionrrrr addEndpointus zOptions.addEndpointcC|d|dS)z Add a POP3 port listener on the specified endpoint. You can listen on multiple ports by specifying multiple --pop3 options. rNr$rr#rrropt_pop3zOptions.opt_pop3cCr%)z Add an SMTP port listener on the specified endpoint. You can listen on multiple ports by specifying multiple --smtp options. rNr&r'rrropt_smtpr)zOptions.opt_smtpcCs$|jr |jd|jdStd)zM Make the most recently specified domain the default domain. z2Specify a domain before specifying using --defaultN)rr addDomainr UsageErrorrrrr opt_defaults zOptions.opt_defaultcCsXz |d\}}Wn tytdwt|jtj ||_ |j ||j dS)a; Generate an SMTP/POP3 virtual domain. This option requires an argument of the form 'NAME=PATH' where NAME is the DNS domain name for which email will be accepted and where PATH is a the filesystem path to a Maildir folder. [Example: 'example.com=/tmp/example.com'] =z>Argument to --maildirdbmdomain must be of the form 'name=path'N) split ValueErrorr r-rMaildirDirdbmDomainrospathabspathrr,)rdomainnamer5rrropt_maildirdbmdomains  zOptions.opt_maildirdbmdomaincCsPz |dd\}}Wn tytdw|jr#|j||dStd)zG Add a user and password to the last specified domain. r0z6Argument to --user must be of the form 'user=password'z(Specify a domain before specifying usersN)r1r2r r-raddUser)r user_passuserpasswordrrropt_users  zOptions.opt_usercCs d|j_dS)z@ Send undeliverable messages to the postmaster. r:N)r postmasterr.rrropt_bounce_to_postmasters z Options.opt_bounce_to_postmastercCst|jdur5tj|jr*t|jj|}|j||jj |t |jj|jdSt d|jjjft d)zR Specify an aliases(5) file to use for the last specified domain. Nz%s does not support alias filesz*Specify a domain before specifying aliases)rrIAliasableDomain providedByr loadAliasFilerdomains setAliasGroupmonitor monitorFile AliasUpdaterr r- __class____name__)rfilenamealiasesrrr opt_aliasess    zOptions.opt_aliasescCs4||r||S|d|rgSt||j|gS)a Return a list of endpoints for the specified service, constructing defaults if necessary. If no endpoints were configured for the service and the protocol was not explicitly disabled with a I{--no-*} option, a default endpoint for the service is created. @type reactor: L{IReactorTCP } provider @param reactor: If any endpoints are created, the reactor with which they are created. @type service: L{bytes} @param service: The type of service for which to retrieve endpoints, either C{b'pop3'} or C{b'smtp'}. @rtype: L{list} of L{IStreamServerEndpoint } provider @return: The endpoints for the specified service as configured by the command line parameters. zno-)rTCP4ServerEndpointr)rrrrrr _getEndpointss  zOptions._getEndpointscCsddlm}|dr|ddurtdd|vr'|dD] }|jj|q|ds4|jjtd }|j D]}| ||||<||rId }q9|sQtd dS) z Check the validity of the specified set of options and configure authentication. @raise UsageError: When the set of options is invalid. rrrrNz--esmtp requires --hostname credCheckersrFTz You cannot disable all protocols) r rr r-r smtpPortalregisterCheckerrAllowAnonymousAccessrrP)rrchanythingrrrr postOptions s"     zOptions.postOptionsN)rK __module__ __qualname____doc__synopsis optParametersoptFlagsrr CompletionsCompleteHostnamescompDatalongdescrr$r(opt_pr*opt_sr/opt_Dr9opt_dr?opt_urAopt_brNopt_ArPrWrrrrr sD"    "r c@s eZdZdZddZddZdS)rIz A callable object which updates the aliases for a domain from an aliases(5) file. @ivar domains: See L{__init__}. @ivar domain: See L{__init__}. cCs||_||_dS)z @type domains: L{dict} mapping L{bytes} to L{IDomain} provider @param domains: A mapping of domain name to domain object @type domain: L{IAliasableDomain} provider @param domain: The domain to update. N)rEr7)rrEr7rrrr2s zAliasUpdater.__init__cCs|jt|j|dS)z Update the aliases for a domain from an aliases(5) file. @type new: L{bytes} @param new: The name of an aliases(5) file. N)r7rFrrDrE)rnewrrr__call__=szAliasUpdater.__call__N)rKrXrYrZrrjrrrrrI)s rIc Csh|dr tj}|jj}ntj}|jj}|drc|d}tj|s&t ||j t |t |j}||jj}|drF|jd7_|j|df7_t|d}||j|jj||dr|j}|dD]}t||} | |jqp|dr|}|dr|d|_|jf|_|drd|j|_|dD]}t||} | |jq|jS)a Configure a service for operating a mail server. The returned service may include POP3 servers, SMTP servers, or both, depending on the configuration passed in. If there are multiple servers, they will share all of their non-network state (i.e. the same user accounts are available on all of them). @type config: L{Options } @param config: Configuration options specifying which servers to include in the returned service and where they should keep mail data. @rtype: L{IService } provider @return: A service which contains the requested mail servers. rr )NNrr:rr)r SmartHostESMTPRelayingManagerrgetESMTPFactorySmartHostSMTPRelayingManagergetSMTPFactoryr4r5isdirmkdirsetQueueQueuer DomainQueuerqueuefArgsRelayStateHelpersetServiceParentrEsetDefaultDomaingetPOP3FactoryrStreamServerEndpointServicer7) configrmType smtpFactorydirdefaultmanagerhelperfendpointsvcrrr makeServiceGsD               r)rZr4twisted.applicationr twisted.credrrr r twisted.mailrrrr r twisted.pythonr r AuthOptionMixinrIrrrrrs