
    Vh                         U d Z ddlmZmZmZ ddlmZmZmZ  G d de	      Z
dedeeedf   eeedf   f   fd	Zd
eedf   dedeedf   deeedf   eeedf   f   fdZi Zeedf   ed<   d Z G d d      Zy)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                       e Zd ZdZy)InvalidFormatzT
    The given string could not be parsed into a valid Jabber Identifier (JID).
    N)__name__
__module____qualname____doc__     R/home/dcms/DCMS/lib/python3.12/site-packages/twisted/words/protocols/jabber/jid.pyr
   r
      s    r   r
   	jidstringreturnNc                 L   d}d}d}| j                  d      }| j                  d      }|dk(  r|dk(  r| }nc| d| }| |dz   d xs d}nQ|dk(  r| d| xs d}| |dz   d }n:||k  r$| d| xs d}| |dz   |||z
  z    }| |dz   d xs d}n| d| }| |dz   d xs 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resourceuser_sepres_seps         r   parser       s	    DDH ~~c"HnnS!G2~b=D Qw'D 1/74Hb=Qx(0DDX\^,D'! 8,4 AGh<N0OP$Wq[]3;t !7+$Wq[]3;tdH%%r   r   r   r   c                    | r 	 t        j                  t        |             } nd} |st	        d      	 t        j                  t        |            }|r 	 t        j                  t        |            }nd}| ||fS # t        $ r t	        d      w xY w# t        $ r t	        d      w xY w# t        $ r t	        d      w xY w)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   s      r   r   r   M   s      	A##CI.D 677	A##CI.D 	A#++CM:H $!!+  	A ?@@	A  	A ?@@	A  	A ?@@	As"   A: B B* :BB'*B?JID__internJIDsc                 P    | t         v r	t         |    S t        |       }|t         | <   |S )z2
    Return interned JID.

    @rtype: L{JID}
    )r&   r%   )r   js     r   	internJIDr)   {   s/     L I&&	N"#Yr   c                       e Zd ZdZ	 	 ddeedf   deeeedf   eeedf   f   df   fdZd Zd Z	d Z
d	ed
efdZd Zd ZeZd
efdZy)r%   z
    Represents a stringprep'd Jabber ID.

    JID objects are hashable so they can be used in sets and as keys in
    dictionaries.
    Nr#   tuplec                     |rt        |      \  }}}n|rt        | \  }}}nt        d      || _        || _        || _        y )Nz?You must provide a value for either 'str' or 'tuple' arguments.)r    r   RuntimeErrorr   r   r   )selfr#   r+   r   r   ress         r   __init__zJID.__init__   sO    
 #CjOD$"ElOD$Q  		r   c                 h    | 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.   s    r   userhostzJID.userhost   s.     99ii[$))--99r   c                 P    | j                   rt        | j                               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)   r3   r2   s    r   userhostJIDzJID.userhostJID   s!     ==T]]_--Kr   c                    | j                   rO| 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   r2   s    r   fullzJID.full   sx     99}}))Adii[$--AA))Adii[11}}))Admm_55yy r   otherr   c                     t        |t              rO| j                  |j                  k(  xr4 | j                  |j                  k(  xr | 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.   r8   s     r   __eq__z
JID.__eq__   sU     eS!		UZZ' 4II+4MMU^^3 "!r   c                 Z    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   r2   s    r   __hash__zJID.__hash__   s"     TYY		4==9::r   c                 "    | j                         S )z
        Get unicode representation.

        Return the string representation of this JID as a unicode string.
        @see: L{full}
        r7   r2   s    r   __unicode__zJID.__unicode__   s     yy{r   c                 (    d| j                         z  S )z
        Get object representation.

        Returns a string that would create a new JID object that compares equal
        to this one.
        zJID(%r)rA   r2   s    r   __repr__zJID.__repr__   s     499;&&r   )NN)r   r   r   r   r   r#   r   r0   r3   r5   r7   objectboolr<   r?   rB   __str__rD   r   r   r   r%   r%      s     !%MQ39 U5d+S%T	2BBCTIJ$$!""F "t ""; G'# 'r   )r   typingr   r   r   .twisted.words.protocols.jabber.xmpp_stringprepr   r   r   	Exceptionr
   r#   r    r   r&   __annotations__r)   r%   r   r   r   <module>rL      s    & % I ,&S ,&U5d#3S%T	:J#JK ,&^("
T	
(""%("16sDy1A("
5dS%T	"223("V "$d3: #y' y'r   