U
    n5{iè  ã                	   @   sÔ   U d Z ddlmZmZmZ ddlmZmZmZ G dd„ de	ƒZ
eeeedf eeedf f dœdd	„Zeedf eeedf eeedf eeedf f d
œdd„Zi Zeedf ed< dd„ ZG dd„ dƒZdS )a$  
Jabber Identifier support.

This module provides an object to represent Jabber Identifiers (JIDs) and
parse string representations into them with proper checking for illegal
characters, case folding and canonicalisation through
L{stringprep<twisted.words.protocols.jabber.xmpp_stringprep>}.
é    )ÚDictÚTupleÚUnion)ÚnameprepÚnodeprepÚresourceprepc                   @   s   e Zd ZdZdS )ÚInvalidFormatzT
    The given string could not be parsed into a valid Jabber Identifier (JID).
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úF/tmp/pip-unpacked-wheel-mnhg7r0q/twisted/words/protocols/jabber/jid.pyr      s   r   N)Ú	jidstringÚreturnc                 C   sô   d}d}d}|   d¡}|   d¡}|dkrX|dkr6| }qè| d|… }| |d d… pTd}n�|dkr‚| d|… pnd}| |d d… }nf||k rÈ| d|… p˜d}| |d |||  … }| |d d… pÄd}n | d|… }| |d d… pæd}t|||ƒS )aÇ  
    Parse given JID string into its respective parts and apply stringprep.

    @param jidstring: string representation of a JID.
    @type jidstring: L{str}
    @return: tuple of (user, host, resource), each of type L{str} as
             the parsed and stringprep'd parts of the given JID. If the
             given string did not have a user or resource part, the respective
             field in the tuple will hold L{None}.
    @rtype: L{tuple}
    Nú@ú/éÿÿÿÿr   é   )ÚfindÚprep)r   ÚuserÚhostÚresourceZuser_sepZres_sepr   r   r   Úparse   s(    

r   )r   r   r   r   c                 C   s¼   | r6zt  t| ƒ¡} W q: tk
r2   tdƒ‚Y q:X nd} |sHtdƒ‚n0zt t|ƒ¡}W n tk
rv   tdƒ‚Y nX |r®zt t|ƒ¡}W q² tk
rª   tdƒ‚Y q²X nd}| ||fS )aP  
    Perform stringprep on all JID fragments.

    @param user: The user part of the JID.
    @type user: L{str}
    @param host: The host part of the JID.
    @type host: L{str}
    @param resource: The resource part of the JID.
    @type resource: L{str}
    @return: The given parts with stringprep applied.
    @rtype: L{tuple}
    zInvalid character in usernameNzServer address required.zInvalid character in hostnamezInvalid character in resource)r   ÚprepareÚstrÚUnicodeErrorr   r   r   )r   r   r   r   r   r   r   M   s&    
r   ÚJIDÚ__internJIDsc                 C   s(   | t krt |  S t| ƒ}|t | < |S dS )z2
    Return interned JID.

    @rtype: L{JID}
    N)r   r   )r   Újr   r   r   Ú	internJID{   s
    r!   c                	   @   s–   e Zd ZdZdeedf eeeedf eeedf f df dœdd„Zdd„ Zdd	„ Z	d
d„ Z
eedœdd„Zdd„ Zdd„ ZeZedœdd„ZdS )r   z‰
    Represents a stringprep'd Jabber ID.

    JID objects are hashable so they can be used in sets and as keys in
    dictionaries.
    N)r   Útuplec                 C   sF   |rt |ƒ\}}}n|r(t|Ž \}}}ntdƒ‚|| _|| _|| _d S )Nz?You must provide a value for either 'str' or 'tuple' arguments.)r   r   ÚRuntimeErrorr   r   r   )Úselfr   r"   r   r   Úresr   r   r   Ú__init__’   s    ÿzJID.__init__c                 C   s"   | j r| j › d| j› �S | jS dS )zÃ
        Extract the bare JID as a unicode string.

        A bare JID does not have a resource part, so this returns either
        C{user@host} or just C{host}.

        @rtype: L{str}
        r   N)r   r   ©r$   r   r   r   Úuserhost¤   s    	zJID.userhostc                 C   s   | j rt|  ¡ ƒS | S dS )a�  
        Extract the bare JID.

        A bare JID does not have a resource part, so this returns a
        L{JID} object representing either C{user@host} or just C{host}.

        If the object this method is called upon doesn't have a resource
        set, it will return itself. Otherwise, the bare JID object will
        be created, interned using L{internJID}.

        @rtype: L{JID}
        N)r   r!   r(   r'   r   r   r   ÚuserhostJID²   s    zJID.userhostJIDc                 C   s\   | j r:| jr&| j › d| j› d| j› �S | j › d| j› �S n| jrR| j› d| j› �S | jS dS )zW
        Return the string representation of this JID.

        @rtype: L{str}
        r   r   N)r   r   r   r'   r   r   r   ÚfullÄ   s    zJID.full)Úotherr   c                 C   s6   t |tƒr.| j|jko,| j|jko,| j|jkS tS dS )zá
        Equality comparison.

        L{JID}s compare equal if their user, host and resource parts all
        compare equal.  When comparing against instances of other types, it
        uses the default comparison.
        N)Ú
isinstancer   r   r   r   ÚNotImplemented)r$   r+   r   r   r   Ú__eq__Õ   s    

ÿ
ýz
JID.__eq__c                 C   s   t | j| j| jfƒS )a  
        Calculate hash.

        L{JID}s with identical constituent user, host and resource parts have
        equal hash values.  In combination with the comparison defined on JIDs,
        this allows for using L{JID}s in sets and as dictionary keys.
        )Úhashr   r   r   r'   r   r   r   Ú__hash__æ   s    zJID.__hash__c                 C   s   |   ¡ S )zŽ
        Get unicode representation.

        Return the string representation of this JID as a unicode string.
        @see: L{full}
        ©r*   r'   r   r   r   Ú__unicode__ð   s    zJID.__unicode__)r   c                 C   s   d|   ¡  S )z’
        Get object representation.

        Returns a string that would create a new JID object that compares equal
        to this one.
        zJID(%r)r1   r'   r   r   r   Ú__repr__ü   s    zJID.__repr__)NN)r	   r
   r   r   r   r   r   r&   r(   r)   r*   ÚobjectÚboolr.   r0   r2   Ú__str__r3   r   r   r   r   r   Š   s   	  ý
$ý

)r   Útypingr   r   r   Z.twisted.words.protocols.jabber.xmpp_stringprepr   r   r   Ú	Exceptionr   r   r   r   r   Ú__annotations__r!   r   r   r   r   r   Ú<module>   s   	*0
  
þ+