U
    
W[Q                     @   sh   d Z ddlmZ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d
d ZG dd de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>}.
    )_PY3unicode)nodeprepresourceprepnameprepc                   @   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rX|dkr6| }q| d| }| |d d pTd}n|dkr| d| pnd}| |d d }nf||k r| d| pd}| |d |||   }| |d d pd}n | d| }| |d d pd}t|||S )a  
    Parse given JID string into its respective parts and apply stringprep.

    @param jidstring: string representation of a JID.
    @type jidstring: L{unicode}
    @return: tuple of (user, host, resource), each of type L{unicode} 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resourceZuser_sepZres_sepr   r   r   parse   s(    

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 |rztt|}W q tk
r   tdY qX nd}| ||fS )a\  
    Perform stringprep on all JID fragments.

    @param user: The user part of the JID.
    @type user: L{unicode}
    @param host: The host part of the JID.
    @type host: L{unicode}
    @param resource: The resource part of the JID.
    @type resource: L{unicode}
    @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   Zpreparer   UnicodeErrorr   r   r   )r   r   r   r   r   r   r   D   s&    
r   c                 C   s(   | t krt |  S t| }|t | < |S dS )z2
    Return interned JID.

    @rtype: L{JID}
    N)__internJIDsJID)r   jr   r   r   	internJIDn   s
    r   c                   @   sb   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZerVeZ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   )selfstrtupler   r   resr   r   r   __init__   s    zJID.__init__c                 C   s    | j rd| j | jf 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{unicode}
        %s@%sN)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   sR   | j r2| jr d| j | j| jf S d| j | jf S n| jrHd| j| jf S | jS dS )z[
        Return the string representation of this JID.

        @rtype: L{unicode}
        z%s@%s/%sr$   z%s/%sN)r   r   r   r%   r   r   r   full   s    zJID.fullc                 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   otherr   r   r   __eq__   s    


z
JID.__eq__c                 C   s    |  |}|tkr|S | S dS )z
        Inequality comparison.

        This negates L{__eq__} for comparison with JIDs and uses the default
        comparison for other types.
        N)r,   r*   )r   r+   resultr   r   r   __ne__   s    
z
JID.__ne__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(   r,   r.   r0   r2   r   __str__r3   r   r   r   r   r   |   s   


r   N)r   Ztwisted.python.compatr   r   Z.twisted.words.protocols.jabber.xmpp_stringprepr   r   r   	Exceptionr   r   r   r   r   objectr   r   r   r   r   <module>   s   .(