o
    ~¨Êh  ã                   @   sž   d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZmZmZ er:d dlmZ d	d
„ Z			ddd„Z		ddd„ZdS )é    )Úwith_statement)Úbasename)ÚloaderÚContext)ÚEmailMultiAlternatives)Úsix)Ú
smart_text)Ú	USE_GNUPGÚ
GNUPG_HOMEÚALWAYS_TRUSTÚGNUPG_ENCODING)ÚGPGc                 C   sB   |d }g }|   ¡ D ]}|d |kr| dd„ |d D ƒ¡ q
|S )z>
    Takes a key and extracts the email addresses for it.
    Úfingerprintc                 S   s$   g | ]}|r|  d ¡d  d¡‘qS )ú<éÿÿÿÿú>)ÚsplitÚstrip)Ú.0Úaddress© r   úD/var/www/html/env/lib/python3.10/site-packages/email_extras/utils.pyÚ
<listcomp>   s    ÿz%addresses_for_key.<locals>.<listcomp>Úuids)Ú	list_keysÚextend)ÚgpgÚkeyr   Ú	addressesr   r   r   Úaddresses_for_key   s   
ÿ€r   FNc	              	      sš  t |tjƒr	|g}i ‰tr1ddlm}	 |	jj|d�D ]}
|
jˆ|
j	< qˆr1t
td�‰ tdur1tˆ _‡ ‡fdd„}g }|durn|D ]-}t|dƒsht|d	ƒ�}| t|ƒ| ¡ f¡ W d  ƒ n1 sbw   Y  q@| |¡ q@‡fd
d„|D ƒ}|r||gn|}dd„ ˆD ƒ}|| D ]A}t| |||ƒ||||d�}|dur¥| |||ƒd¡ |D ]}|d }ˆ |d ¡r¸|d7 }| |||d |ƒ¡ q§|j|d� q‰dS )z–
    Sends a multipart email containing text and html versions which
    are encrypted for each recipient that has a valid gpg key
    installed.
    r   )ÚAddress)Úaddress__in)Ú	gnupghomeNc                    s,   |d ˆv rˆ j | |d td�}t|ƒS | S )Nr   )Úalways_trust)Úencryptr   r   )ÚbodyÚ	addr_listÚ	encrypted©r   Úkey_addressesr   r   Úencrypt_if_key8   s   ÿz!send_mail.<locals>.encrypt_if_keyÚ__iter__Úrbc                    s   g | ]}|ˆ vr|‘qS r   r   ©r   Úaddr)r)   r   r   r   N   s    zsend_mail.<locals>.<listcomp>c                 S   s   g | ]}|g‘qS r   r   r-   r   r   r   r   P   s    )Ú
connectionÚheadersz	text/htmlz.ascé   )Úfail_silently)Ú
isinstancer   Ústring_typesr	   Úemail_extras.modelsr    ÚobjectsÚfilterÚuse_ascr   r   r
   r   ÚencodingÚhasattrÚopenÚappendr   Úreadr   Úattach_alternativeÚgetÚattachÚsend)ÚsubjectÚ	body_textÚ	addr_fromÚaddr_tor2   ÚattachmentsÚ	body_htmlr/   r0   r    r   r*   Úattachments_partsÚ
attachmentÚfÚunencryptedr'   r&   ÚmsgÚpartsÚnamer   r(   r   Ú	send_mail   sR   


ÿ€ýÿórO   c	           
         s@   ˆ du ri ‰ ‡ ‡fdd„}	t | |	dƒ|||||	dƒ||d�	 dS )z�
    Send email rendering text and html versions for the specified
    template name using the context dictionary passed in.
    Nc                    s    dˆ| f }t  |¡ tˆ ƒ¡S )Nzemail_extras/%s.%s)r   Úget_templateÚrenderr   )ÚextrN   ©ÚcontextÚtemplater   r   rQ   m   s   z"send_mail_template.<locals>.renderÚtxtÚhtml)r2   rF   rG   r/   r0   )rO   )
rB   rU   rD   rE   r2   rF   rT   r/   r0   rQ   r   rS   r   Úsend_mail_templatea   s   
ýrX   )FNNNN)Ú
__future__r   Úos.pathr   Údjango.templater   r   Údjango.core.mailr   Údjango.utilsr   Údjango.utils.encodingr   Úemail_extras.settingsr	   r
   r   r   Úgnupgr   r   rO   rX   r   r   r   r   Ú<module>   s"    
þDþ