U
    ]B                     @   sl   d Z ddlZddlZddlZddlmZ ddlmZ ee	Z
dd Zdd Zd	d
 Zdd ZdddZdS )z'Subscribes users to the EFF newsletter.    N)	constants)
interfacesc                 C   sB   | j dkr| jrtd dS | jdkr.t | _| jr>t| j  dS )zHigh level function to take care of EFF newsletter subscriptions.

    The user may be asked if they want to sign up for the newsletter if
    they have not already specified.

    :param .IConfig config: Client configuration.

    Nz$you didn't provide an e-mail address)emailZ	eff_email_report_failure_want_subscription	subscribe)Zconfig r   -/usr/lib/python3/dist-packages/certbot/eff.pyhandle_subscription   s    	

r
   c                  C   s    d} t jtj}|j| ddS )zDoes the user want to be subscribed to the EFF newsletter?

    :returns: True if we should subscribe the user, otherwise, False
    :rtype: bool

    a:  Would you be willing to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. F)default)zope	component
getUtilityr   ZIDisplayZyesno)promptZdisplayr   r   r	   r   !   s    r   c                 C   s6   t j}d| dd}td|| ttj||d dS )zhSubscribe the user to the EFF mailing list.

    :param str email: the e-mail address to subscribe

    jsonZ%eff_supporters_library_subscribe_form)Z	data_typer   Zform_idzSending POST request to %s:
%s)dataN)r   ZEFF_SUBSCRIBE_URIloggerdebug_check_responserequestsZpost)r   Zurlr   r   r   r	   r   2   s    r   c              	   C   sp   t d| j z |   |  d s,td W n< tjjk
rL   t  Y n  t	t
fk
rj   td Y nX dS )zCheck for errors in the server's response.

    If an error occurred, it will be reported to the user.

    :param requests.Response response: the server's response to the
        subscription request

    zReceived response:
%sZstatusz)your e-mail address appears to be invalidz,there was a problem with the server responseN)r   r   ZcontentZraise_for_statusr   r   r   
exceptionsZ	HTTPError
ValueErrorKeyError)Zresponser   r   r	   r   @   s    	
r   c                 C   sR   dg}| dk	r"| d | |  | d tjtj}|d||j dS )zNotify the user of failing to sign them up for the newsletter.

    :param reason: a phrase describing what the problem was
        beginning with a lowercase letter and no closing punctuation
    :type reason: `str` or `None`

    z4We were unable to subscribe you the EFF mailing listNz	 because z:. You can try again later by visiting https://act.eff.org. )	appendr   r   r   r   Z	IReporterZadd_messagejoinZLOW_PRIORITY)reasonmsgZreporterr   r   r	   r   T   s    


r   )N)__doc__Zloggingr   Zzope.componentr   Zcertbotr   r   Z	getLogger__name__r   r
   r   r   r   r   r   r   r   r	   <module>   s   
