U
    ]1                     @   s   d 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
 ddlmZ eeZejjZd+d
dZdd Zd,ddZd-ddZdd Zdd Zd.ddZd/ddZdd Zdd Zdd  Zd!d" Zd#d$ Zd0d%d&Z d'd( Z!d)d* Z"dS )1z+Contains UI methods for LE user operations.    N)errors)
interfaces)util)misc)osFTc                 C   s   d}d}d tjtdd}|r>| r8||7 }d}qBd}nd}z(ttjj	| rZ|| n|dd\}}W n$ t
jk
r   d	}t
|Y nX |tjkr|rt
d
qt
dnt|r|S |r||7 }d}t|} qBdS )aJ  Prompt for valid email address.

    :param bool invalid: True if an invalid address was provided by the user
    :param bool optional: True if the user can use
        --register-unsafely-without-email to avoid providing an e-mail

    :returns: e-mail address
    :rtype: str

    :raises errors.Error: if the user cancels

    z-There seem to be problems with that address. zBEnter email address (used for urgent renewal and security notices)z

If you really want to skip this, you can run the client with --register-unsafely-without-email but make sure you then backup your account key from {0}

ZconfigaccountsFTforce_interactivezoYou should register before running non-interactively, or provide --agree-tos and --email <email_address> flags.zHAn e-mail address or --register-unsafely-without-email must be provided.z#An e-mail address must be provided.N)formatr   pathjoinr   Zget_default_folderz_utilr   IDisplayinputr   ZMissingCommandlineFlagdisplay_utilOKErrorr   Z
safe_emailbool)ZinvalidZoptionalZinvalid_prefixmsgZunsafe_suggestionZsuggest_unsafecodeZemail r   5/usr/lib/python3/dist-packages/certbot/display/ops.py	get_email   sD     


r   c                 C   s>   dd | D }t tjjd|dd\}}|tjkr:| | S dS )zuChoose an account.

    :param list accounts: Containing at least one
        :class:`~certbot.account.Account`

    c                 S   s   g | ]
}|j qS r   )Zslug).0Zaccr   r   r   
<listcomp>R   s     z"choose_account.<locals>.<listcomp>zPlease choose an accountTr   N)r   r   r   Zmenur   r   )r   labelsr   indexr   r   r   choose_accountJ   s    
  

r   c                 C   s0   t tjj|| dd\}}|tjkr,|r,|S g S )zDisplay screen to let user pick one or multiple values from the provided
    list.

    :param list values: Values to select from

    :returns: List of selected values
    :rtype: list
    T)tagsr	   )r   r   r   	checklistr   r   )valuesquestionr   itemsr   r   r   choose_valuesZ   s    	
  
r#   c                 C   s\   | dkrt d t S t|  }t|}|s8tdS t||\}}|tjkrX|rX|S g S )aQ  Display screen to select domains to validate.

    :param installer: An installer object
    :type installer: :class:`certbot.interfaces.IInstaller`

    :param `str` question: Overriding dialog question to ask the user if asked
        to choose from domain names.

    :returns: List of selected names
    :rtype: `list` of `str`

    Nz$No installer, picking names manuallyz1No names were found in your configuration files. )	loggerdebug_choose_names_manuallylistZget_all_namesget_valid_domains_filter_namesr   r   )Z	installerr!   domainsnamesr   r   r   r   choose_namesi   s    
r,   c              	   C   sB   g }| D ]4}z| t| W q tjk
r:   Y qY qX q|S )zHelper method for choose_names that implements basic checks
     on domain names

    :param list domains: Domain names to validate
    :return: List of valid domains
    :rtype: list
    )appendr   enforce_domain_sanityr   ConfigurationError)r*   Zvalid_domainsdomainr   r   r   r(      s    r(   c                 C   s   t | dd dS )zSort FQDNs by SLD (and if many, by their subdomains)

    :param list FQDNs: list of domain names

    :returns: Sorted list of domain names
    :rtype: list
    c                 S   s   |  dd d d dd  S )N.   )split)Zfqdnr   r   r   <lambda>       z_sort_names.<locals>.<lambda>)key)sorted)ZFQDNsr   r   r   _sort_names   s    r9   c                 C   sD   t | }|r|}nd}ttjj||ddd\}} |dd | D fS )a  Determine which names the user would like to select from a list.

    :param list names: domain names

    :returns: tuple of the form (`code`, `names`) where
        `code` - str display exit code
        `names` - list of names selected
    :rtype: tuple

    z1Which names would you like to activate HTTPS for?	--domainsT)r   cli_flagr	   c                 S   s   g | ]}t |qS r   )str)r   sr   r   r   r      s     z!_filter_names.<locals>.<listcomp>)r9   r   r   r   r   )r+   Zoverride_questionZsorted_namesr!   r   r   r   r   r)      s    
   
r)    c           
      C   s2  t tjj| d ddd\}}|tjkr.t }d}zt|}W n$ tk
rf   g }d	t
j}Y nX t|D ]L\}}zt|||< W qp tjk
r } zt|||< W 5 d}~X Y qpX qp|rd	t
j}|D ]}|d		t
j|||  }q|d
	t
j }|r*t tjj|dd}	|	r.t S n|S g S )zManually input names for those without an installer.

    :param str prompt_prefix: string to prepend to prompt for domains

    :returns: list of provided names
    :rtype: `list` of `str`

    zCPlease enter in your domain name(s) (comma and/or space separated) r:   T)r;   r	   r>   ziInternationalized domain names are not presently supported.{0}{0}Would you like to re-enter the names?{0}Nz<One or more of the entered domain names was not valid:{0}{0}z{1}: {2}{0}z+{0}Would you like to re-enter the names?{0}r   )r   r   r   r   r   r   dictZseparate_list_inputUnicodeEncodeErrorr
   r   linesep	enumerater   r.   r   r/   r<   Zyesnor&   )
Zprompt_prefixr   Zinput_Zinvalid_domainsZretry_messageZdomain_listir0   eZretryr   r   r   r&      s^    	
 

   r&   c              	   C   s4   t tjjdt| tjtjt	| dd dS )zsDisplay a box confirming the installation of HTTPS.

    :param list domains: domain names which were enabled

    zdCongratulations! You have successfully enabled {0}{1}{1}You should test your configuration at:{1}{2}FpauseN
r   r   r   notificationr
   _gen_https_namesr   rA   r   _gen_ssl_lab_urlsr*   r   r   r   success_installation   s    
rL   c              	   C   s4   t tjjdt| tjtjt	| dd dS )zDisplay a box confirming the renewal of an existing certificate.

    :param list domains: domain names which were renewed

    zYour existing certificate has been successfully renewed, and the new certificate has been installed.{1}{1}The new certificate covers the following domains: {0}{1}{1}You should test your configuration at:{1}{2}FrE   NrG   rK   r   r   r   success_renewal   s    
rM   c                 C   s"   t tjjd| tjdd dS )zDisplay a box confirming a certificate has been revoked.

    :param list cert_path: path to certificate which was revoked.

    z\Congratulations! You have successfully revoked the certificate that was located at {0}{1}{1}FrE   N)r   r   r   rH   r
   r   rA   )Z	cert_pathr   r   r   success_revocation  s    
rN   c                 C   s   dd | D S )zNReturns a list of urls.

    :param list domains: Each domain is a 'str'

    c                 S   s   g | ]}d | qS )z1https://www.ssllabs.com/ssltest/analyze.html?d=%sr   r   domr   r   r   r   !  s     z%_gen_ssl_lab_urls.<locals>.<listcomp>r   rK   r   r   r   rJ     s    rJ   c                 C   sl   t | dkrd| d S t | dkr2dj| dS t | dkrhddd	d
 | dd D d| d S dS )zReturns a string of the https domains.

    Domains are formatted nicely with https:// prepended to each.

    :param list domains: Each domain is a 'str'

    r3   zhttps://{0}r      z%https://{dom[0]} and https://{dom[1]})rP   z	{0}{1}{2}z, c                 s   s   | ]}d | V  qdS )z
https://%sNr   rO   r   r   r   	<genexpr>2  s     z#_gen_https_names.<locals>.<genexpr>Nr2   z, and https://r>   )lenr
   r   rK   r   r   r   rI   $  s    rI   c              
   K   s   |d k	rZz|| W nD t jk
rX } z$tjd||dd td|W 5 d }~X Y nX | |fd|i|\}}|tjkrz|| ||fW S  t jk
r } z2tjd||dd tj	
tjjt|dd W 5 d }~X Y qX qZ||fS qZd S )	Nz>Encountered invalid default value "%s" when prompting for "%s"T)exc_infozInvalid default "{0}"defaultz/Validator rejected "%s" when prompting for "%s"FrE   )r   r   r$   r%   AssertionErrorr
   r   r   zope	component
getUtilityr   r   rH   r<   )method	validatormessagerU   kwargserrorr   rawr   r   r   _get_validated9  s.     

0r`   c                 O   s   t tjtjj| f||S )a	  Like `~certbot.interfaces.IDisplay.input`, but with validation.

    :param callable validator: A method which will be called on the
        supplied input. If the method raises a `errors.Error`, its
        text will be displayed and the user will be re-prompted.
    :param list `*args`: Arguments to be passed to `~certbot.interfaces.IDisplay.input`.
    :param dict `**kwargs`: Arguments to be passed to `~certbot.interfaces.IDisplay.input`.
    :return: as `~certbot.interfaces.IDisplay.input`
    :rtype: tuple
    )r`   rW   rX   rY   r   r   r   r[   argsr]   r   r   r   validated_inputT  s    rc   c                 O   s   t tjtjj| f||S )a=  Like `~certbot.interfaces.IDisplay.directory_select`, but with validation.

    :param callable validator: A method which will be called on the
        supplied input. If the method raises a `errors.Error`, its
        text will be displayed and the user will be re-prompted.
    :param list `*args`: Arguments to be passed to `~certbot.interfaces.IDisplay.directory_select`.
    :param dict `**kwargs`: Arguments to be passed to
        `~certbot.interfaces.IDisplay.directory_select`.
    :return: as `~certbot.interfaces.IDisplay.directory_select`
    :rtype: tuple
    )r`   rW   rX   rY   r   r   Zdirectory_selectra   r   r   r   validated_directoryc  s    rd   )FT)N)N)N)r>   )N)#__doc__ZloggingZzope.componentrW   Zcertbotr   r   r   Zcertbot.compatr   r   Zcertbot.displayr   Z	getLogger__name__r$   rX   rY   r   r   r   r#   r,   r(   r9   r)   r&   rL   rM   rN   rJ   rI   r`   rc   rd   r   r   r   r   <module>   s4   

7



6	
