o
    ¯bß  ã                   @   st   U d Z ddlmZ ddlmZmZmZ G dd„ deƒZdd„ Z	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)Ú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   úD/usr/lib/python3/dist-packages/twisted/words/protocols/jabber/jid.pyr      s    r   c                 C   sô   d}d}d}|   d¡}|   d¡}|dkr,|dkr| }nY| d|… }| |d d… p*d}nH|dkrA| d|… p7d}| |d d… }n3||k rd| d|… pLd}| |d |||  … }| |d d… pbd}n| d|… }| |d d… psd}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)Ú	jidstringÚuserÚhostÚresourceÚuser_sepÚres_sepr   r   r   Úparse   s(   

r   c                 C   s¤   | rz	t  t| ƒ¡} W n ty   tdƒ‚w d} |stdƒ‚z	t t|ƒ¡}W n ty3   tdƒ‚w |rKz	t t|ƒ¡}W n tyJ   tdƒ‚w 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 v rt |  S t| ƒ}|t | < |S )z2
    Return interned JID.

    @rtype: L{JID}
    )r   r   )r   Újr   r   r   Ú	internJIDy   s
   r    c                   @   sf   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdede	fdd„Z
dd„ Zdd„ ZeZdef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.
    Nc                 C   sH   |s|st dƒ‚|rt|ƒ\}}}nt|Ž \}}}|| _|| _|| _d S )Nz?You must provide a value for either 'str' or 'tuple' arguments.)ÚRuntimeErrorr   r   r   r   r   )Úselfr   Útupler   r   Úresr   r   r   Ú__init__   s   ÿ
zJID.__init__c                 C   s   | j r| j › d| j› S | j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   )r   r   ©r"   r   r   r   ÚuserhostŸ   s   	zJID.userhostc                 C   s   | j r	t|  ¡ ƒS | 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}
        )r   r    r'   r&   r   r   r   ÚuserhostJID­   s   zJID.userhostJIDc                 C   sV   | j r| jr| j › d| j› d| j› S | j › d| j› S | jr(| j› d| j› S | jS )zW
        Return the string representation of this JID.

        @rtype: L{str}
        r   r   )r   r   r   r&   r   r   r   Úfull¿   s   zJID.fullÚotherÚreturnc                 C   s2   t |tƒr| j|jko| j|jko| j|jkS t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.
        )Ú
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__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)   ÚobjectÚboolr.   r0   r2   Ú__str__r   r3   r   r   r   r   r   ˆ   s    


N)r
   Útypingr   Ú.twisted.words.protocols.jabber.xmpp_stringprepr   r   r   Ú	Exceptionr   r   r   r   r   Ú__annotations__r    r   r   r   r   r   Ú<module>   s   	/)