HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //proc/thread-self/root/lib/python3/dist-packages/twisted/protocols/__pycache__/sip.cpython-38.pyc
U

��W[���6@s�dZddlZddlZddlZddlmZmZddlmZddl	m
Z
ddlmZm
Z
mZddlmZddlmZdd	lmZmZmZd
Zddd
ddddddd�	ZiZe��D]\ZZeee<q�[[dddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHd'dI�5ZdJdKdLdM�ZdNdO�ZdPdQ�Z e!�Z"GdRdS�dSe!�Z#dTdU�Z$GdVdW�dW�Z%d|dXdY�Z&dZd[�Z'd}d\d]�Z(Gd^d_�d_e)�Z*Gd`da�dae*�Z+Gdbdc�dc�Z,Gddde�dee,�Z-Gdfdg�dge,�Z.Gdhdi�diej/�Z0Gdjdk�dkej1�Z2Gdldm�dme�Z3Gdndo�do�Z4Gdpdq�dqe�Z5Gdrds�dse�Z6Gdtdu�due2�Z7Gdvdw�dwe�Z8Gdxdy�dye7�Z9ee5e6�Gdzd{�d{��Z:dS)~zP
Session Initialization Protocol.

Documented in RFC 2543.
[Superseded by 3261]
�N)�implementer�	Interface)�OrderedDict)�cred)�protocol�defer�reactor)�basic)�log)�_PY3�	iteritems�unicodei��i�m�e�l�c�f�s�t�v)	�call-id�contactzcontent-encoding�content-lengthzcontent-type�fromZsubject�to�viaZTryingZRingingzCall Is Being ForwardedZQueuedzSession ProgressZOKzMultiple ChoiceszMoved PermanentlyzMoved Temporarilyz	See Otherz	Use ProxyzAlternative ServicezBad RequestZUnauthorizedzPayment RequiredZ	Forbiddenz	Not FoundzMethod Not AllowedzNot AcceptablezProxy Authentication RequiredzRequest TimeoutZConflictZGonezLength RequiredzRequest Entity Too LargezRequest-URI Too LargezUnsupported Media TypezUnsupported URI Schemez
Bad ExtensionzExtension RequiredzInterval Too BriefzTemporarily UnavailablezCall/Transaction Does Not Existz
Loop Detectedz
Too Many HopszAddress IncompleteZ	Ambiguousz	Busy HerezRequest TerminatedzNot Acceptable HerezRequest PendingZUndecipherablezInternal Server ErrorzNot ImplementedzBad GatewayzService UnavailablezServer Time-outzSIP Version not supportedzMessage Too LargezBusy EverywhereZDeclinezDoes not exist anywhere)5�d������i,i-i.i/i1i|��i�i��i�i�i�i�i�i�i�i�i�i�i�i�i�i�i�i�i�i�i�i�i�i�i�i�i����i�i�i�i�iiXi[i\i^ZCSeqzCall-IDzWWW-Authenticate)�cseqr�www-authenticatecCsd�dd�|�d�D��S)zK
    Capitalize a string, making sure to treat '-' as a word separator
    �-cSsg|]}|���qS�)�
capitalize)�.0�xr+r+�7/usr/lib/python3/dist-packages/twisted/protocols/sip.py�
<listcomp>tsz"dashCapitalize.<locals>.<listcomp>)�join�split�rr+r+r/�dashCapitalizepsr4cCs0|d|dkrdkr,nn|dd�S|S)Nr����"�r+r3r+r+r/�unqxs r8c@sLeZdZdZeddddeddfdd�Zdd�Zd	d
�Ze	ee�Z
dd�ZdS)
�Viaa�
    A L{Via} is a SIP Via header, representing a segment of the path taken by
    the request.

    See RFC 3261, sections 8.1.1.7, 18.2.2, and 20.42.

    @ivar transport: Network protocol used for this leg. (Probably either "TCP"
    or "UDP".)
    @type transport: C{str}
    @ivar branch: Unique identifier for this request.
    @type branch: C{str}
    @ivar host: Hostname or IP for this leg.
    @type host: C{str}
    @ivar port: Port used for this leg.
    @type port C{int}, or None.
    @ivar rportRequested: Whether to request RFC 3581 client processing or not.
    @type rportRequested: C{bool}
    @ivar rportValue: Servers wishing to honor requests for RFC 3581 processing
    should set this parameter to the source port the request was received
    from.
    @type rportValue: C{int}, or None.

    @ivar ttl: Time-to-live for requests on multicast paths.
    @type ttl: C{int}, or None.
    @ivar maddr: The destination multicast address, if any.
    @type maddr: C{str}, or None.
    @ivar hidden: Obsolete in SIP 2.0.
    @type hidden: C{bool}
    @ivar otherParams: Any other parameters in the header.
    @type otherParams: C{dict}
    ZUDPNFc
Ks�||_||_||_||_||_||_|dkrJtjdtdd�d|_	d|_
n8|dkr`d|_	d|_
n"|tkrvd|_	d|_
n||_	d|_
||_|	|_
|
|_dS)a(
        Set parameters of this Via header. All arguments correspond to
        attributes of the same name.

        To maintain compatibility with old SIP
        code, the 'rport' argument is used to determine the values of
        C{rportRequested} and C{rportValue}. If None, C{rportRequested} is set
        to True. (The deprecated method for doing this is to pass True.) If an
        integer, C{rportValue} is set to the given value.

        Any arguments not explicitly named here are collected into the
        C{otherParams} dict.
        Tz+rport=True is deprecated since Twisted 9.0.�)�
stacklevelNF)�	transport�host�port�ttl�hidden�received�warnings�warn�DeprecationWarning�
rportValue�rportRequested�_absent�branch�maddr�otherParams)�selfr=r>r<r?r@rA�rportrHrI�kwr+r+r/�__init__�s2�zVia.__init__cCs&|jdkrdS|jdk	r|jSdSdS)zG
        Returns the rport value expected by the old SIP code.
        TN)rFrE�rKr+r+r/�	_getrport�s


z
Via._getrportcCs||_d|_dS)z�
        L{Base._fixupNAT} sets C{rport} directly, so this method sets
        C{rportValue} based on that.

        @param newRPort: The new rport value.
        @type newRPort: C{int}
        FN)rErF)rKZnewRPortr+r+r/�	_setrport�sz
Via._setrportcCs�d|j|j|jf}|jr"|d7}dD]&}t||�}|dk	r&|d||f7}q&|jr^|d7}n|jdk	rx|d|jf7}t|j	�
��}|D].\}}|dkr�|d|7}q�|d||f7}q�|S)	zI
        Serialize this header for use in a request or response.
        zSIP/2.0/%s %s:%sz;hidden)r?rHrIrAN�;%s=%sz;rportz	;rport=%s�;)r<r=r>r@�getattrrFrErL�sortedrJ�items)rKr�n�value�etc�krr+r+r/�toString�s"


zVia.toString)�__name__�
__module__�__qualname__�__doc__�PORTrGrNrPrQ�propertyrLr[r+r+r+r/r9�s �
,
r9cCsL|�d�}|d|dd�}}|�dd�\}}|��}i}|�d�\}}}	|dks\|dkrjtd	|f��|	|d
<d|kr�|�d�\}
}t|�|d<|
|d
<n||d
<|D]�}|���dd�}t|�dkr�|dd}}
n|\}}
|dkr�d|d<q�|�dd�}t|�dk�r|dd}}n|\}}|dk�r8t|�}|||<q�tf|�S)za
    Parse a Via header.

    @return: The parsed version of this header.
    @rtype: L{Via}
    rSrr7N� �/ZSIPz2.0zwrong protocol or version: %rr<�:r>r=�r@T�=)rLr?)r2�strip�
ValueError�int�lenr9)rX�partsZsent�paramsZprotocolinfoZby�resultZpnameZpversionr<r=r>�pZcomment�namer+r+r/�parseViaHeaders<



rpc@s2eZdZdZddd�Zdd�Zdd�Zd	d
�ZdS)�URLz
    A SIP URL.
    Nc

Csl||_||_||_||_||_||_||_|
|_||_|	|_	|dkrLg|_
n||_
|dkrbi|_n||_dS�N)�usernamer=�passwordr>r<�usertype�method�tagr?rI�other�headers)
rKr=rsrtr>r<rurvr?rIrwrxryr+r+r/rN2s zURL.__init__cCs�g}|j}|d�|jdkrF||j�|jdkr>|d|j�|d�||j�|jdkrh|d|j�|jdkr�|d|j�dD]&}t||�}|dkr�|d||f�q�|jD]}|d|�q�|jr�|d	�|d
�	dd�|j�
�D���d
�	|�S)N�sip:z:%s�@z:%dz;user=%s�r<r?rIrvrwrRz;%s�?�&cSs*g|]"\}}dt�|�pt|�|f�qS)z%s=%s)�specialCases�getr4)r-�hrr+r+r/r0^sz URL.toString.<locals>.<listcomp>re)�appendrsrtr=r>rurTrxryr1rV)rKr�wrWrr+r+r/r[Is.







zURL.toStringcCs|��Srr)r[rOr+r+r/�__str__bszURL.__str__cCsd|j|j|j|j|jfS)Nz<URL %s:%s@%s:%r/%s>)rsrtr=r>r<rOr+r+r/�__repr__fszURL.__repr__)NNNNNNNNNNN)r\r]r^r_rNr[r�r�r+r+r+r/rq-s�
rqcCsi}|�d�s"td|dd���|dd��d�}|d|dd�}}|�dd�}t|�d	kr�|\}}	|�d
d�}
t|
�dkr�|
d|d<q�|
d|d<|
d|d<n|d}	|	�d
d�}t|�dkr�|d|d
<n|d|d
<t|d�|d<|dk�r||d
<|dk�r||d<|D]�}||dk�r|d|k�r|i|d<}
|�dd�\}}|�d�D]}|�d�\}}||
|<�q^|�dd�}t|�dk�r�|�dg��|��q|\}}|dk�r�||d<n8|dk�r�|dk�r�t|�}|||<n|�dg��|��qtf|�S)zV
    Return string into URL object.

    URIs are of form 'sip:user@example.com'.
    rzzunsupported scheme: N�rSrr7r{r:rdrsrtr=r>r5r}ryr~rfrx�userrur|r?)�
startswithrhr2rjri�
setdefaultr�rq)�urlr=r>�drkZ
userdomainrlZudpartsZuserpassZhostportZuppartsZhppartsrnr�ry�headerrZrZnvrorXr+r+r/�parseURLksV







r�cCsd|_d|_d|_i|_dS)z*
    Clean a URL from a Request line.
    N)r<rIr?ry)r�r+r+r/�cleanRequestURL�sr�cCs�|��}|�d�r&dt|||d�ifSi}|�dd�\}}|��}|�d�rX|dd�}|�d�rn|dd�}|�d	d�\}}t|||d�}|��}|r�|�d
�D] }|s�q�|�d�\}	}
|
||	<q�|r�d|_i|_d|_d|_|||fS)z�
    Return (name, uri, params) for From/To/Contact header.

    @param clean: remove unnecessary info, usually for From and To headers.
    rzre�r=r>�<r7r6Nr5�>rSrf)	rgr�r�r2�endswithr?ryr<rI)Zaddressr=r>�cleanrlror�ZparamstringrrZrr+r+r/�parseAddress�s2



r�c@seZdZddd�ZdS)�SIPErrorNcCs4|dkrt|}t�|d||f�||_||_dS)NzSIP error (%d): %s)�statusCodes�	ExceptionrN�code�phrase)rKr�r�r+r+r/rN�s
zSIPError.__init__)N�r\r]r^rNr+r+r+r/r��sr�c@seZdZdZdS)�RegistrationErrorz(
    Registration was not possible.
    N�r\r]r^r_r+r+r+r/r��sr�c@sDeZdZdZdZdd�Zdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dS)�Messagez
    A SIP message.
    NcCst�|_d|_d|_dS)Nrer)rry�body�finishedrOr+r+r/rN�szMessage.__init__cCs>|��}t�||�}|dkr&t|�|_|j�|g��|�dS)Nr)�lower�longHeadersr�ri�lengthryr�r�)rKrorXr+r+r/�	addHeader�s

zMessage.addHeadercCs|j|7_dSrr)r��rK�datar+r+r/�bodyDataReceived�szMessage.bodyDataReceivedcCs,|jdkr"|jt|j�kr"td��d|_dS)Nzwrong body lengthr7)r�rjr�rhr�rOr+r+r/�creationFinished�szMessage.creationFinishedcCs^d|��}|j��D]0\}}|D]"}|dt�|�p:t|�|f7}q"q|d7}||j7}|S)Nz%s
z%s: %s
z
)�_getHeaderLineryrVrr�r4r�)rKrrWZvsrr+r+r/r[s"
zMessage.toStringcCst�dSrr��NotImplementedErrorrOr+r+r/r�szMessage._getHeaderLine)r\r]r^r_r�rNr�r�r�r[r�r+r+r+r/r��s
r�c@s*eZdZdZd
dd�Zdd�Zdd�Zd	S)�Requestz
    A Request for a URI
    �SIP/2.0cCs:t�|�||_t|t�r"||_nt|�|_t|j�dSrr)r�rNrv�
isinstancerq�urir�r�)rKrvr��versionr+r+r/rNs


zRequest.__init__cCsdt|�|j|j��fS)Nz<SIP Request %d:%s %s>)�idrvr�r[rOr+r+r/r� szRequest.__repr__cCsd|j|j��fS)Nz
%s %s SIP/2.0)rvr�r[rOr+r+r/r�$szRequest._getHeaderLineN)r��r\r]r^r_rNr�r�r+r+r+r/r�s

r�c@s*eZdZdZd
dd�Zdd�Zdd	�ZdS)�Responsez%
    A Response to a URI Request
    Nr�cCs*t�|�||_|dkr t|}||_dSrr)r�rNr�r�r�)rKr�r�r�r+r+r/rN.s

zResponse.__init__cCsdt|�|jfS)Nz<SIP Response %d:%s>)r�r�rOr+r+r/r�6szResponse.__repr__cCsd|j|jfS)Nz
SIP/2.0 %s %s)r�r�rOr+r+r/r�:szResponse._getHeaderLine)Nr�r�r+r+r+r/r�)s
r�c@sxeZdZdZdZdZdZdZdZdd�Z	dd	d
�Z
dd�Zd
d�Zdd�Z
dd�Zdd�Zdd�Zddd�Zdd�ZdS)�MessagesParserz�
    A SIP messages parser.

    Expects dataReceived, dataDone repeatedly,
    in that order. Shouldn't be connected to actual transport.
    r�r7�	firstlinercCs||_|��dSrr)�messageReceived�reset)rKZmessageReceivedCallbackr+r+r/rNNszMessagesParser.__init__recCs,d|_d|_d|_d|_d|_|�|�dS)Nr�r)�stater��bodyReceived�messager�ZsetLineMode�rKZ
remainingDatar+r+r/r�SszMessagesParser.resetcCsd|_|��dS)N�invalid)r��
setRawModerOr+r+r/�invalidMessage\szMessagesParser.invalidMessagecCsb|��|jdkrdS|jdkr,|��dS|jdkr@|��n|j|jkrV|��ntd��dS)zI
        Clear out any buffered data that may be hanging around.
        r�Nr�zthis should never happen)ZclearLineBufferr�r�r��messageDoner��RuntimeErrorrOr+r+r/�dataDoneas




zMessagesParser.dataDonecCsHz&t|t�r|�d�}tj�||�Wnt��|��YnXdS)N�utf-8)	r�r
�encoder	�LineReceiver�dataReceivedr
�errr�r�r+r+r/r�vs

zMessagesParser.dataReceivedcCst�dS)z2
        Expected to create self.message.
        Nr��rK�liner+r+r/�handleFirstLine�szMessagesParser.handleFirstLinecCs|��dSrr)r�r�r+r+r/�lineLengthExceeded�sz!MessagesParser.lineLengthExceededcCs4trt|t�r|�d�}|jdk�r�|�d�s8|�d�rF|dd�}q$|sNdSz|�dd�\}}}Wntk
r�|��YdSX|dkr�|j	r�zt
|�}Wntk
r�|��YdSXt||�|_n(|dkr�|j
r�t||�|_n|��dSd	|_dS|jd	k�st�|�r�|�d��s,|�d
��rJ|j\}}|||��f|_n�|j�rf|jj|j�d|_z|�dd�\}}Wn tk
�r�|��YdSX||��f|_|��dk�r0zt
|���|_Wn tk
�r�|��YdSXnBd
|_|j�r|jj|j�d|_|jdk�r(|��dS|��dS)Nr�r��
�
r7rbr:r�ry�	rdrr�r)rr��bytes�decoder�r�r2rhr��acceptResponsesrir�r��acceptRequestsr��AssertionErrorr��lstripr�r�r�r�r�)rKr��a�brr�rorXr+r+r/�lineReceived�sj


zMessagesParser.lineReceivedcCs2|jdkst�|j��|�|j�|�|�dS)Nr�)r�r�r�r�r�r�r�r+r+r/r��s
zMessagesParser.messageDonecCs�|jdkst�tr&t|t�r&|�d�}|jdkr4dS|jdkrL|j�|�ntt	|�}|j|j
}||kr�|j�|d|��|�||d��dS|j
|7_
|j�|�|j
|jkr�|��dS)N)r�r�r�r�)r�r�rr�r�r�r�r�r�rjr�r�)rKr�ZdataLenZexpectedLenr+r+r/�rawDataReceived�s"


zMessagesParser.rawDataReceivedN)re)re)r\r]r^r_r�r�r�r��debugrNr�r�r�r�r�r�r�r�r�r+r+r+r/r�?s 
	
D
r�c@s`eZdZdZeZdZdd�Zdd�Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dd�ZdS)�Basez1
    Base class for SIP clients and servers.
    FcCsg|_t|j�|_dSrr)�messagesr��
addMessage�parserrOr+r+r/rN�sz
Base.__init__cCs|j�|�dSrr)r�r�)rK�msgr+r+r/r��szBase.addMessagecCs�|j�|�|j��|jD]P}|�||�|jrHt�d|��|f�t	|t
�r`|�||�q|�||�qg|jdd�<dS)NzReceived %r from %r)
r�r�r�r��	_fixupNATr�r
r�r[r�r��handle_request�handle_response)rKr��addrrr+r+r/�datagramReceived�s


zBase.datagramReceivedcCsz|\}}t|jdd�}|j|krN||_|j|kr:||_|��|jdd<n(|jdkrv||_||_|��|jdd<dS)NrrT)rpryr=rAr>rLr[)rKr��
sourcePeer�srcHost�srcPortZ	senderViar+r+r/r�s


zBase._fixupNATcCsLt|jdd�}|jp|j}|jp.|jp.|j}t||d�}|�||�dS�zX
        Deliver response.

        Destination is based on topmost Via header.
        rrr�N�	rpryrAr=rLr>r`rq�sendMessage�rKZresponseMessageZdestViar=r>ZdestAddrr+r+r/�deliverResponses
zBase.deliverResponsecCs2t|�}dD] }|j�|g�dd�|j|<q|S�z9
        Create a response to a request message.
        )rrrrr(N�r�ryr��rKr�Zrequest�responseror+r+r/�responseFromRequest)szBase.responseFromRequestcCsj|jdkrtd��|jr.t�d|��|f�|��}t|t�rJ|�d�}|j�	||j
|jp`|jf�dS)z�
        Send a message.

        @param destURL: C{URL}. This should be a *physical* URL, not a logical one.
        @param message: The message to send.
        )ZudpNzonly UDP currently supportedzSending %r to %rr�N)
r<r�r�r
r�r[r�r
r��writer=r>r`)rKZdestURLr�r�r+r+r/r�4s


zBase.sendMessagecCst�dS)z�
        Override to define behavior for requests received

        @type message: C{Message}
        @type addr: C{tuple}
        Nr��rKr�r�r+r+r/r�EszBase.handle_requestcCst�dS)z�
        Override to define behavior for responses received.

        @type message: C{Message}
        @type addr: C{tuple}
        Nr�r�r+r+r/r�OszBase.handle_responseN)r\r]r^r_r`r�rNr�r�r�r�r�r�r�r�r+r+r+r/r��s
r�c@seZdZdZdS)�IContactz(
    A user of a registrar or proxy
    Nr�r+r+r+r/r�Zsr�c@seZdZdd�ZdS)�RegistrationcCs||_||_dSrr)�secondsToExpiry�
contactURL)rKr�r�r+r+r/rNbszRegistration.__init__Nr�r+r+r+r/r�asr�c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�	IRegistryz?
    Allows registration of logical->physical URL mapping.
    cCsdS)z�
        Register the physical address of a logical URL.

        @return: Deferred of C{Registration} or failure with RegistrationError.
        Nr+��	domainURL�
logicalURL�physicalURLr+r+r/�registerAddressmszIRegistry.registerAddresscCsdS)z�
        Unregister the physical address of a logical URL.

        @return: Deferred of C{Registration} or failure with RegistrationError.
        Nr+r�r+r+r/�unregisterAddressuszIRegistry.unregisterAddresscCsdS)z�
        Get registration info for logical URL.

        @return: Deferred of C{Registration} object or failure of LookupError.
        Nr+�r�r+r+r/�getRegistrationInfo}szIRegistry.getRegistrationInfoN)r\r]r^r_r�r�r�r+r+r+r/r�hsr�c@seZdZdZdd�ZdS)�ILocatorz<
    Allow looking up physical address for logical URL.
    cCsdS)z�
        Return physical URL of server for logical URL of user.

        @param logicalURL: a logical C{URL}.
        @return: Deferred which becomes URL or fails with LookupError.
        Nr+r�r+r+r/�
getAddress�szILocator.getAddressN)r\r]r^r_r�r+r+r+r/r��sr�c@sfeZdZdZeZdZdefdd�Zdd�Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dd�ZdS)�Proxyz
    SIP proxy.
    NcCs"|p
t��|_||_t�|�dS)z�
        Create new instance.

        @param host: our hostname/IP as set in Via headers.
        @param port: our port as set in Via headers.
        N)�socketZgetfqdnr=r>r�rN)rKr=r>r+r+r/rN�szProxy.__init__cCst|j|jd�S)z<
        Return value of Via header for this proxy.
        r�)r9r=r>rOr+r+r/�getVia�szProxy.getViac
s�t�d�jd�}|dkr �j}z|�|�}WnZtk
rd}z����|j���W5d}~XYnBt������d���YnX|dk	r�|�	��fdd��dS)Nzhandle_%s_requestr&cs����|j���Srr)r�r�r�)r�r�rKr+r/�<lambda>�sz&Proxy.handle_request.<locals>.<lambda>)
rTrv�handle_request_defaultr�r�r�r�r
r��
addErrback)rKr�r�rr�rr+rr/r��s$zProxy.handle_requestcs�|\}}�fdd�}���}|��|jdkr<t�d�dS|jd�d|���t|jdddd	�\}}}	�j�|�}
|
�	�j
|�|
��j|�dS)
z�
        Default request handler.

        Default behaviour for OPTIONS and unknown methods for proxies
        is to forward message on to the client.

        Since at the moment we are stateless proxy, that's basically
        everything.
        cs|��|jdd<��||�S)Nrr)r[ryr�)r�r�rOr+r/�_mungContactHeader�sz8Proxy.handle_request_default.<locals>._mungContactHeaderrzDropping looped message.Nrrr7�r�)
rr[ryr
r��insertr��locatorr��addCallbackr�r�_cantForwardRequest)rKr�r�r�r�rZ	viaHeaderror�Ztagsr�r+rOr/r�s

zProxy.handle_request_defaultcCs,|�t�|jdd=|�|�d|��dS)Nrrr%)�trap�LookupErrorryr�r��rK�errorr�r+r+r/r�s
zProxy._cantForwardRequestcCsLt|jdd�}|jp|j}|jp.|jp.|j}t||d�}|�||�dSr�r�r�r+r+r/r��s
zProxy.deliverResponsecCs2t|�}dD] }|j�|g�dd�|j|<q|Sr�r�r�r+r+r/r��szProxy.responseFromRequestcCslt|jdd�}|j|jf|j|jfkr8t�d�dS|jdd=|jds^|�||�dS|�|�dS)z+
        Default response handler.
        rrz&Dropping incorrectly addressed messageN)rpryr=r>r
r��gotResponser�)rKr�r�rr+r+r/r�s

zProxy.handle_responsecCsdS)zJ
        Called with responses that are addressed at this server.
        Nr+r�r+r+r/rszProxy.gotResponse)r\r]r^r_r`r
rNrr�rrr�r�r�rr+r+r+r/r�s
rc@seZdZdd�Zdd�ZdS)�IAuthorizercCsdS)z�
        Generate a challenge the client may respond to.

        @type peer: C{tuple}
        @param peer: The client's address

        @rtype: C{str}
        @return: The challenge string
        Nr+)Zpeerr+r+r/�getChallengeszIAuthorizer.getChallengecCsdS)zf
        Create a credentials object from the given response.

        @type response: C{str}
        Nr+)r�r+r+r/r�*szIAuthorizer.decodeN)r\r]r^rr�r+r+r+r/rsrc@s�eZdZdZdZdZiZdd�Zdd�Zdd�Z	d	d
�Z
dd�Zd
d�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�ZdS)�
RegisterProxyzk
    A proxy that allows registration for a specific domain.

    Unregistered users won't be handled.
    NcOstj|f|�|�i|_dSrr)rrNZliveChallenges)rK�argsrMr+r+r/rN@szRegisterProxy.__init__cCs|\}}dSrrr+�rKr�Z	host_portr=r>r+r+r/�handle_ACK_requestEsz RegisterProxy.handle_ACK_requestcCsL|\}}|jdkr"|�|||�n&d|jkr:|�|||�S|�|||�SdS)z`
        Handle a registration request.

        Currently registration is not proxied.
        N�
authorization)�portal�registerry�unauthorized�loginrr+r+r/�handle_REGISTER_requestOs

z%RegisterProxy.handle_REGISTER_requestc	Cs�|�d|�}t|j�D]Z\}}|�||f�}|dkrHd|��|jf}nd|��||jf}|j�dg��|�q|�	|�dS)Nr$z
%s realm="%s"z%s %s,realm="%s"r))
r�r�authorizersr�titler=ryr�r�r�)	rKr�r=r>rZschemeZauthZchalrXr+r+r/raszRegisterProxy.unauthorizedc	Cs�|jdd�dd�}|j�|d���}|r�z|�|d�}Wn:tk
rV�Yq�t��|�	|�
d|��Yq�X|jd|j7_|j
�|dt��|j|||��|j|||��tj�n|�	|�
d|��dS)Nrrr7r&r{r')ryr2rr�r�r�r�r
r�r�r�rsr=rrr�r�_cbLoginr�_ebLogin)rKr�r=r>rkr�rr+r+r/rms2���zRegisterProxy.logincCs|\}}}|�|||�dSrr)r)rKZi_a_lr�r=r>rr�rr+r+r/r �s
zRegisterProxy._cbLogincCs |�tjj�|�|||�dSrr)r
rrZUnauthorizedLoginr)rKZfailurer�r=r>r+r+r/r!�szRegisterProxy._ebLoginc
Cs�t|jdddd�\}}}d}d|jkr8|jdd}|j�ddg�ddkr`|�|||�nV|dk	r�t|||d	�\}}}|j�|j||�}	n|j�|�}	|	j|j	|j
|f|fd
�dS)z-
        Allow all users to register
        rrr7rNr�expires�0r�)ZcallbackArgsZerrbackArgs)r�ryr��
unregister�registryr�r�r�ZaddCallbacks�_cbRegister�_ebRegister)
rKr�r=r>ro�toURLrlrr�r�r+r+r/r�s
�zRegisterProxy.registercCsT|�d|�}|jdkr:|�d|j���|�dd|j�|�dd�|�|�dS)Nr"rr"z%drr#)r�r�r�r[r�r�)rK�registrationr�r�r+r+r/r&�s
zRegisterProxy._cbRegistercCs|�tt�dSrr)r
r�rrr+r+r/r'�szRegisterProxy._ebRegisterc		Cs�zt|jdd�}Wn&tk
r<|�|�d|��YnPX|dkr�|dkrTd}nt|�\}}}|j�|j||�}|�	|j
|��|j|�dS)Nr"rr#�*)
riryrhr�r�r�r%r�r�r�
_cbUnregisterr�
_ebUnregister)	rKr�r(rr"r�rorlr�r+r+r/r$�s�zRegisterProxy.unregistercCs@|�d|�}|j�dg��|j���|�dd�|�|�dS)Nr"rr"r#)r�ryr�r�r�r[r�r�)rKr)r�r�r+r+r/r+�szRegisterProxy._cbUnregistercCsdSrrr+)rKr)r�r+r+r/r,�szRegisterProxy._ebUnregister)r\r]r^r_rr%rrNrrrrr r!rr&r'r$r+r,r+r+r+r/r3s"
	rc@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�InMemoryRegistryz6
    A simplistic registry for a specific domain.
    cCs||_i|_dSrr)�domain�users)rKr.r+r+r/rN�szInMemoryRegistry.__init__cCsR|j|jkrt�td��S|j|jkr@|j|j\}}t�|�St�td��SdS�Nzunknown domain�no such user)r=r.r�failrrsr/�succeed�rKZuserURI�dcr�r+r+r/r��s
zInMemoryRegistry.getAddresscCsh|j|jkrt�td��S|j|jkrV|j|j\}}t�tt	|�
�t���|��St�td��SdSr0)r=r.rr2rrsr/r3r�ri�getTime�timer4r+r+r/r��s z$InMemoryRegistry.getRegistrationInfocCsVz|j|\}}Wn"tk
r4t�td��YSX|��|j|=t�td|��S)Nr1r)r/�KeyErrorrr2rZcancelr3r�)rKrsr5r�r+r+r/�_expireRegistration�sz$InMemoryRegistry._expireRegistrationcCs�|j|jkr$t�d�t�td��S|j|jkrHt�d�t�td��S|j|jkrp|j|j\}}|�	d�nt
�d|j|j�}t�d|�
�|�
�f�||f|j|j<t�tt|��t���|��S)Nz(Registration for domain we don't handle.r%izRegistered %s at %s)r=r.r
r�rr2r�rsr/r�rZ	callLaterr9r[r3r�rir6r7)rKr�r�r�r5�oldr+r+r/r��s

z InMemoryRegistry.registerAddresscCs|�|j�Srr)r9rs)rKr�r�r�r+r+r/r�
sz"InMemoryRegistry.unregisterAddressN)
r\r]r^r_rNr�r�r9r�r�r+r+r+r/r-�s

r-)NN)NNr);r_rr7rBZzope.interfacerr�collectionsrZtwistedrZtwisted.internetrrrZtwisted.protocolsr	Ztwisted.pythonr
Ztwisted.python.compatrrr
r`ZshortHeadersr�rVrZrr�rr4r8�objectrGr9rprqr�r�r�r�r�r�r�r�r�r�r�ZDatagramProtocolr�r�r�r�r�rrrr-r+r+r+r/�<module>s��
�>�,>
8
$
./m	!