o
    Òó·ih(  ã                   @   s  d Z ddlZddlZddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZmZ ddlmZ d	Zej d
¡ZdZdZeejƒZeed�ZG dd„ dƒZdd„ ZedƒZdd„ Z G dd„ dƒZ!dd„ Z"dd„ Z#da$dd„ Z%dd„ Z&dd „ Z'd!d"„ Z(d#d$„ Z)dS )%z4
Implementation module for the I{mailmail} command.
é    N)ÚConfigParser)ÚStringIO)Úversion)Úreactor)ÚLoggerÚtextFileLogObserver)Úsmtpz/etc/mailmailz~/.twisted/mailmailz	127.0.0.1z�Subject: Failed Message Delivery

  Message delivery failed.  The following occurred:

  %s
--
The Twisted sendmail application.
)Zobserverc                   @   s   e Zd ZdZdS )ÚOptionsat  
    Store the values of the parsed command-line options to the I{mailmail}
    script.

    @type to: L{list} of L{str}
    @ivar to: The addresses to which to deliver this message.

    @type sender: L{str}
    @ivar sender: The address from which this message is being sent.

    @type body: C{file}
    @ivar body: The object from which the message is to be read.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úcC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/mail/scripts/mailmail.pyr	   (   s    r	   c                   C   s&   zt  ¡ W S  ty   t ¡  Y S w ©N)ÚosÚgetloginÚBaseExceptionÚgetpassÚgetuserr   r   r   r   r   8   s
   
ÿr   zUnsupported option.c           	   	   C   s²  t ƒ }dd„ | D ƒ|_tƒ |_d| v rtdtƒ tƒ ‚d| v r!t‚d| v r't‚d| v r<| |  d¡d  |_|j 	|j¡ d	| v sDd
| v rFt‚d| v rNd|_
nd|_
d| v rYd|_
nd|_
d| v sdd| v rhd|_nd|_d| v rqt‚d| v r€d|_|j|_g |_nd|_g |_g g g g g dœ}tƒ }	 d}tj ¡ }| ¡ sžqë| dd¡}|d  ¡ }|jrÇ|dv rÇ|j tj |d ¡d g¡ |dkrÆd}n|dkrÖtj |d ¡d |_||v rã||  |d ¡ |rê| |¡ q’|d sù| d|j› d�¡ |d �s|j�stdƒ‚| d d |j¡¡¡ |d �s"| d t ¡ › d�¡ | |¡ |j�rE|jD ]}z	|j 	|¡ W �q. t �yD   Y �q.w | !dd¡ t| "¡ tj #¡  ƒ|_$|S )!Nc                 S   s   g | ]	}|  d ¡s|‘qS )ú-)Ú
startswith)Ú.0Úer   r   r   Ú
<listcomp>D   s    z parseOptions.<locals>.<listcomp>z	--versionzmailmail version:z-bpz-bsz-Fé   z-iz-oiz-odbTFz-odfz-oepz-epz-omz-t)ÚfromÚtoÚccÚbccÚdatez: r   )r   r   r   r   r   zFrom: z
r   zNo recipients specified.zTo: {}
z, r    zDate: )%r	   r   r   ÚsenderÚprintr   Ú
SystemExitÚ_unsupportedOptionÚindexÚremoveZ
backgroundÚprintErrorsZrecipientsFromHeadersZexcludeAddressesZexludeAddressesr   ÚsysÚstdinÚreadlineÚstripÚsplitÚlowerÚextendÚemailÚutilsÚ	parseaddrÚappendÚwriteÚformatÚjoinr   Z
rfc822dater   ÚseekÚgetvalueÚreadÚbody)	ÚargvÚoZrequiredHeadersÚbufferr3   ÚlineÚhdrsÚhdrÚar   r   r   ÚparseOptionsB   sš   
û
€
ì



ÿrA   c                   @   s   e Zd ZdZdd„ ZdS )ÚConfigurationaí  

    @ivar allowUIDs: A list of UIDs which are allowed to send mail.
    @ivar allowGIDs: A list of GIDs which are allowed to send mail.
    @ivar denyUIDs: A list of UIDs which are not allowed to send mail.
    @ivar denyGIDs: A list of GIDs which are not allowed to send mail.

    @type defaultAccess: L{bool}
    @ivar defaultAccess: L{True} if access will be allowed when no other access
    control rule matches or L{False} if it will be denied in that case.

    @ivar useraccess: Either C{'allow'} to check C{allowUID} first
    or C{'deny'} to check C{denyUID} first.

    @ivar groupaccess: Either C{'allow'} to check C{allowGID} first or
    C{'deny'} to check C{denyGID} first.

    @ivar identities: A L{dict} mapping hostnames to credentials to use when
    sending mail to that host.

    @ivar smarthost: L{None} or a hostname through which all outgoing mail will
    be sent.

    @ivar domain: L{None} or the hostname with which to identify ourselves when
    connecting to an MTA.
    c                 C   s@   g | _ g | _g | _g | _d| _d| _i | _d | _d | _d| _	d S )NÚdenyT)
Ú	allowUIDsÚdenyUIDsÚ	allowGIDsÚdenyGIDsÚ
useraccessÚgroupaccessÚ
identitiesÚ	smarthostÚdomainÚdefaultAccess)Úselfr   r   r   Ú__init__Ø   s   
zConfiguration.__init__N)r
   r   r   r   rO   r   r   r   r   rB   ¼   s    rB   c                 C   sÚ  t ƒ }t | tj¡s|S tƒ }| | ¡ |j}|j}|j}|j	}d||fd||ffD ]y\}}}	| 
|¡r£d|fd|	ffD ]>\}
}| ||
¡rz| ||
¡rz| ||
¡ d¡D ]$}zt|ƒ}W n tys   tjd|d  ¡ ||d� Y qUw | |¡ qUq<| |d	¡}d
d„ dd„ | d¡D ƒD ƒ}|d dkr�t||dƒ q*t||dƒ q*| 
d¡rÌ| d¡D ]\}}| dd¡}t|ƒdkrÆtjd|d� q®||j|< q®| 
d¡rë| dd¡rÞ| dd¡|_| dd¡rë| dd¡|_|S )NrH   rI   ZallowrC   ú,z6Illegal {prefix}ID in [{section}] section: {sectionID}r   )ÚprefixÚsectionÚ	sectionIDÚorderc                 S   ó   g | ]}|  ¡ ‘qS r   )r,   ©r   Úsr   r   r   r     ó    zloadConfig.<locals>.<listcomp>c                 S   rU   r   )r-   rV   r   r   r   r     rX   Úidentityú:r   é   z.Illegal entry in [identity] section: {section})rR   Ú	addressesrK   Zdefault_domain)rB   r   ÚaccessÚR_OKr   r8   rD   rE   rF   rG   Úhas_sectionÚ
has_optionÚgetr,   ÚintÚ
ValueErrorÚ_logÚerrorÚupperr2   ÚsetattrÚitemsÚlenrJ   rK   rL   )ÚpathÚcÚpÚauZduÚagZdgrR   r@   ÚdÚmodeÚLrS   rT   ÚhostZupÚpartsr   r   r   Ú
loadConfigç   sZ   



ûÿ	€€

rt   c                 C   s   t  ¡  d S r   ©r   Ústop)Úresultr   r   r   Úsuccess,  s   rx   c                 C   s   t  ¡  | ad S r   )r   rv   Úfailed)Úfr   r   r   Úfailure3  s   r{   c                 C   s.   t  | |j|j|j¡}| tt¡ t 	¡  d S r   )
r   Úsendmailr!   r   r9   ZaddCallbacksrx   r{   r   Úrun)rr   ÚoptionsÚidentro   r   r   r   r|   9  s   r|   c                 C   sf   |j g}d tjd tj d¡¡}tƒ }| j|d� tt	| 
¡  ƒ}t d|||¡}| dd„ ¡ d S )Nz2"Internally Generated Message ({})"<postmaster@{}>r   Úascii©ÚfileÚ	localhostc                 S   s   t  ¡ S r   ru   )Ú_r   r   r   Ú<lambda>H  s    zsenderror.<locals>.<lambda>)r!   r4   r(   r:   r   ÚDNSNAMEÚdecoder   ÚprintTracebackÚ	ERROR_FMTr7   r|   ZaddBoth)r{   r~   Z	recipientr!   re   r9   ro   r   r   r   Ú	senderror?  s   ÿrŠ   c                 C   s¦   t  ¡ }t  ¡ }| jdkr|| jv rdS || jv rdS n|| jv r#dS || jv r*dS | jdkrA|| jv r6dS || jv r=dS | j	 S || jv rHdS || jv rOdS | j	 S )NrC   TF)
r   ÚgetuidÚgetgidrH   rE   rD   rI   rG   rF   rM   )ÚconfÚuidÚgidr   r   r   rC   K  s.   


ÿ





û
rC   c                  C   sÂ   t tjdd … ƒ} ttƒ}ttƒ}t|ƒst|ƒr t d¡ d S |j	p'|j	p't
}|j ¡ }| |j¡ |jr;|jt_n|jrB|jt_t|| |ƒ tr_| jrXtjtjd� tdƒ‚tt| ƒ d S d S )Nr   zPermission deniedr�   )rA   r(   r:   rt   Ú
GLOBAL_CFGÚ	LOCAL_CFGrC   rd   re   rK   Ú	SMARTHOSTrJ   ÚcopyÚupdaterL   r   r†   r|   ry   r'   rˆ   Ústderrr#   rŠ   )r;   ZgConfZlConfrr   r   r   r   r   r}   h  s(   


ûr}   )*r   Úemail.utilsr/   r   r   r(   Úconfigparserr   Úior   Ztwisted.copyrightr   Ztwisted.internetr   Ztwisted.loggerr   r   Ztwisted.mailr   r�   rj   Ú
expanduserr‘   r’   r‰   r•   Z_logObserverrd   r	   r   r#   r$   rA   rB   rt   rx   ry   r{   r|   rŠ   rC   r}   r   r   r   r   Ú<module>   s<   


z+E