Spamworldpro Mini Shell
Spamworldpro


Server : Apache/2.4.52 (Ubuntu)
System : Linux webserver 6.8.0-49-generic #49~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Nov 6 17:42:15 UTC 2 x86_64
User : www-data ( 33)
PHP Version : 8.1.2-1ubuntu2.21
Disable Function : NONE
Directory :  /lib/python3/dist-packages/twisted/spread/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //lib/python3/dist-packages/twisted/spread/__pycache__/pb.cpython-310.pyc
o

�b_��@s�dZddlZddlmZddlmZmZddlmZm	Z	m
Z
mZddlm
Z
ddlmZmZddlmZdd	lmZmZmZdd
lmZmZddlmZddlmZdd
lmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2ddl3m4Z4m5Z5ddl6m7Z7m8Z8m9Z9m:Z:dZ;dZ<Gdd�de=�Z>Gdd�de>�Z?Gdd�de=�Z@Gdd�de=�ZAeGdd�d��ZBGdd�de=�ZCGdd�de�ZDeeD�Gd d!�d!��ZEGd"d#�d#e$�ZFee5�eGd$d%�d%e)ejG���ZHe1d&eH�Gd'd(�d(�ZIGd)d*�d*ejJe �ZKGd+d,�d,e'ejJ�ZLe1eKeL�dDd-d.�ZMGd/d0�d0ejN�ZOd1d2�ZPd3d4�ZQGd5d6�d6ejR�ZSGd7d8�d8ejT�ZUGd9d:�d:e
�ZVee!�Gd;d<�d<��ZWeeWe
e!�Gd=d>�d>�ZXGd?d@�d@e$eX�ZYeeeV�GdAdB�dBe$eX��ZZgdC�Z[dS)Ea�
Perspective Broker

"This isn't a professional opinion, but it's probably got enough
internet to kill you." --glyph

Introduction
============

This is a broker for proxies for and copies of objects.  It provides a
translucent interface layer to those proxies.

The protocol is not opaque, because it provides objects which represent the
remote proxies and require no context (server references, IDs) to operate on.

It is not transparent because it does I{not} attempt to make remote objects
behave identically, or even similarly, to local objects.  Method calls are
invoked asynchronously, and specific rules are applied when serializing
arguments.

To get started, begin with L{PBClientFactory} and L{PBServerFactory}.

@author: Glyph Lefkowitz
�N)�md5)�	Interface�implementer)�	Anonymous�
IAnonymous�ICredentials�IUsernameHashedPassword)�Portal)�defer�protocol)�styles)�failure�log�reflect)�cmp�
comparable)�registerAdapter)�banana)�	Cacheable�Copyable�IPBRoot�	Jellyable�NoSuchMethod�
Referenceable�RemoteCache�RemoteCacheObserver�
RemoteCopy�Root�Serializable�Viewable�	ViewPoint�copyTags�setCopierForClass�setCopierForClassTree�setFactoryForClass�setUnjellyableFactoryForClass�setUnjellyableForClass�setUnjellyableForClassTree)�
IJellyable�IUnjellyable)�_newInstance�globalSecurity�jelly�unjellyiiS"c@�eZdZdZdS)�
ProtocolErrorzN
    This error is raised when an invalid protocol statement is received.
    N��__name__�
__module__�__qualname__�__doc__�r5r5�3/usr/lib/python3/dist-packages/twisted/spread/pb.pyr/T�r/c@r.)�DeadReferenceErrorzx
    This error is raised when a method is called on a dead reference (one whose
    broker has been disconnected).
    Nr0r5r5r5r6r8Zr7r8c@r.)�Errora 
    This error can be raised to generate known error conditions.

    When a PB callable method (perspective_, remote_, view_) raises
    this error, it indicates that a traceback should not be printed,
    but instead, the string representation of the exception should be
    sent.
    Nr0r5r5r5r6r9ar7r9c@�eZdZdZdd�ZdS)�RemoteErrora�
    This class is used to wrap a string-ified exception from the remote side to
    be able to reraise it. (Raising string exceptions is no longer possible in
    Python 2.6+)

    The value of this exception will be a str() representation of the remote
    value.

    @ivar remoteType: The full import path of the exception class which was
        raised on the remote end.
    @type remoteType: C{str}

    @ivar remoteTraceback: The remote traceback.
    @type remoteTraceback: C{str}

    @note: It's not possible to include the remoteTraceback if this exception is
        thrown into a generator. It must be accessed as an attribute.
    cCst�||�||_||_dS�N)�	Exception�__init__�
remoteType�remoteTraceback)�selfr?�valuer@r5r5r6r>�s
zRemoteError.__init__N�r1r2r3r4r>r5r5r5r6r;lsr;c@�0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�RemoteMethodz>
    This is a translucent reference to a remote message.
    cCs||_||_dS)zT
        Initialize with a L{RemoteReference} and the name of this message.
        N)�obj�name)rArFrGr5r5r6r>�s
zRemoteMethod.__init__cCst|j|jf|�Sr<)rrFrG�rA�otherr5r5r6�__cmp__�szRemoteMethod.__cmp__cCst|j|jf�Sr<)�hashrFrG�rAr5r5r6�__hash__�szRemoteMethod.__hash__cOs(|jj�d|jj|jj|j�d�||�S)z8
        Asynchronously invoke a remote method.
        ��utf-8)rF�broker�_sendMessage�perspective�luidrG�encode)rA�args�kwr5r5r6�__call__�s
�zRemoteMethod.__call__N)r1r2r3r4r>rJrMrWr5r5r5r6rE�srEc@seZdZdS)�PBConnectionLostN)r1r2r3r5r5r5r6rX�srXc@r:)�IPerspectivea
    per*spec*tive, n. : The relationship of aspects of a subject to each
    other and to a whole: 'a perspective of history'; 'a need to view
    the problem in the proper perspective'.

    This is a Perspective Broker-specific wrapper for an avatar. That
    is to say, a PB-published view on to the business logic for the
    system's concept of a 'user'.

    The concept of attached/detached is no longer implemented by the
    framework. The realm is expected to implement such semantics if
    needed.
    cC�dS)a�
        This method is called when a network message is received.

        @arg broker: The Perspective Broker.

        @type message: str
        @arg message: The name of the method called by the other end.

        @type args: list in jelly format
        @arg args: The arguments that were passed by the other end. It
                   is recommend that you use the `unserialize' method of the
                   broker to decode this.

        @type kwargs: dict in jelly format
        @arg kwargs: The keyword arguments that were passed by the
                     other end.  It is recommended that you use the
                     `unserialize' method of the broker to decode this.

        @rtype: A jelly list.
        @return: It is recommended that you use the `serialize' method
                 of the broker on whatever object you need to return to
                 generate the return value.
        Nr5)rP�messagerU�kwargsr5r5r6�perspectiveMessageReceived��z'IPerspective.perspectiveMessageReceivedN�r1r2r3r4r]r5r5r5r6rY�srYc@r:)�Avatara
    A default IPerspective implementor.

    This class is intended to be subclassed, and a realm should return
    an instance of such a subclass when IPerspective is requested of
    it.

    A peer requesting a perspective will receive only a
    L{RemoteReference} to a pb.Avatar.  When a method is called on
    that L{RemoteReference}, it will translate to a method on the
    remote perspective named 'perspective_methodname'.  (For more
    information on invoking methods on other objects, see
    L{flavors.ViewPoint}.)
    c
Csv|�||�}|�||�}t|d|�}z	||i|��}Wnty1t�|�d|�d|����w|�|||||�S)ai
        This method is called when a network message is received.

        This will call::

            self.perspective_%(message)s(*broker.unserialize(args),
                                         **broker.unserialize(kw))

        to handle the method; subclasses of Avatar are expected to
        implement methods using this naming convention.
        zperspective_%sz didn't accept z and )�unserialize�getattr�	TypeErrorr�msg�	serialize)rArPr[rUrV�method�stater5r5r6r]�s
�z!Avatar.perspectiveMessageReceivedNr_r5r5r5r6r`�sr`c@seZdZdZddd�ZdS)�AsReferenceablez6
    A reference directed towards another object.
    �remotecCst||d�|_dS)N�MessageReceived)rb�remoteMessageReceived)rA�object�messageTyper5r5r6r>szAsReferenceable.__init__N)rirCr5r5r5r6rh�srhc@sheZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�ZdS)�RemoteReferencea]
    A translucent reference to a remote object.

    I may be a reference to a L{flavors.ViewPoint}, a
    L{flavors.Referenceable}, or an L{IPerspective} implementer (e.g.,
    pb.Avatar).  From the client's perspective, it is not possible to
    tell which except by convention.

    I am a "translucent" reference because although no additional
    bookkeeping overhead is given to the application programmer for
    manipulating a reference, return values are asynchronous.

    See also L{twisted.internet.defer}.

    @ivar broker: The broker I am obtained through.
    @type broker: L{Broker}
    cCs"||_||_||_||_g|_dS)z�(internal) Initialize me with a broker and a locally-unique ID.

        The ID is unique only to the particular Perspective Broker
        instance.
        N)rSrP�
doRefCountrR�disconnectCallbacks)rArRrPrSror5r5r6r>s

zRemoteReference.__init__cCs<t|�sJ�|j�|�t|j�dkr|j�|j�dSdS)z�
        Register a callback to be called if our broker gets disconnected.

        @param callback: a callable which will be called with one
                         argument, this instance.
        �N)�callablerp�append�lenrP�notifyOnDisconnect�
_disconnected�rA�callbackr5r5r6ru's
�z"RemoteReference.notifyOnDisconnectcCs(|j�|�|js|j�|j�dSdS)zu
        Remove a callback that was registered with notifyOnDisconnect.

        @param callback: a callable
        N)rp�removerP�dontNotifyOnDisconnectrvrwr5r5r6rz3s�z&RemoteReference.dontNotifyOnDisconnectcCs|jD]}||�qd|_dS)zN
        Called if we are disconnected and have callbacks registered.
        N)rprwr5r5r6rv=s


zRemoteReference._disconnectedcCs(|jr|j|jks
Jd��d|jfSdS)zc
        If I am being sent back to where I came from, serialize as a local backreference.
        z6Can't send references to brokers other than their own.slocal)s
unpersistablezReferences cannot be serialized)�invokerrPrS)rA�jellierr5r5r6�jellyForEs�
zRemoteReference.jellyForcCs|�|jj|j|dd�|S)Nrq)r>r{�unserializingPerspective)rA�	unjellier�unjellyListr5r5r6�
unjellyForQs�zRemoteReference.unjellyForcOs.t|t�s
|�d�}|j�d|j|j|||�S)a�
        Asynchronously invoke a remote method.

        @type _name: L{str}
        @param _name:  the name of the remote method to invoke
        @param args: arguments to serialize for the remote function
        @param kw:  keyword arguments to serialize for the remote function.
        @rtype:   L{twisted.internet.defer.Deferred}
        @returns: a Deferred which will be fired when the result of
                  this remote call is received.
        �utf8rN)�
isinstance�bytesrTrPrQrRrS)rA�_namerUrVr5r5r6�
callRemoteZs


�zRemoteReference.callRemotecCs
t||�S)zX

        @param key: The key.
        @return: A L{RemoteMethod} for this key.
        )rE)rA�keyr5r5r6�remoteMethodps
zRemoteReference.remoteMethodcCs0t|t�r|j|jkrt|j|j�St|j|�S)zM

        @param other: another L{RemoteReference} to compare me to.
        )r�rnrPrrSrHr5r5r6rJxs
zRemoteReference.__cmp__cCs|jS)z
        Hash me.
        )rSrLr5r5r6rM�szRemoteReference.__hash__cCs|jr|j�|j�dSdS)zD
        Do distributed reference counting on finalization.
        N)rorP�
sendDecRefrSrLr5r5r6�__del__�s�zRemoteReference.__del__N)r1r2r3r4r>rurzrvr}r�r�r�rJrMr�r5r5r5r6rns
	
rnric@s8eZdZdZddd�Zdefdd�Zdd	�Zd
d�ZdS)
�Localz3
    (internal) A reference to a local object.
    NcCs||_||_d|_dS)z
        Initialize.
        rqN)rlrR�refcount)rArlrRr5r5r6r>�s
zLocal.__init__�returncCsd|j�d|j�d�S)Nz
<pb.Local z ref:�>)rlr�rLr5r5r6�__repr__�szLocal.__repr__cC�|jd|_|jS)zi
        Increment the reference count.

        @return: the reference count after incrementing
        rq�r�rLr5r5r6�incref��zLocal.increfcCs|jd|_|jS)zi
        Decrement the reference count.

        @return: the reference count after decrementing
        rqr�rLr5r5r6�decref�r�zLocal.decrefr<)	r1r2r3r4r>�strr�r�r�r5r5r5r6r��s
	r�c@seZdZdZdZdd�ZdS)�CopyableFailurez}
    A L{flavors.RemoteCopy} and L{flavors.Copyable} version of
    L{twisted.python.failure.Failure} for serialization.
    rcCs�|j��}d|d<g|d<g|d<t|j�|d<t|jt�r$|j|d<nt�|j��	d�|d<|j
r:|��|d<|Sd	|d<|S)
z�
        Collect state related to the exception which occurred, discarding
        state which cannot reasonably be serialized.
        N�tb�frames�stackrB�typerO�	tracebackzTraceback unavailable
)�__dict__�copyr�rBr�r�r�r�qualrT�unsafeTracebacks�getTraceback)rArgr5r5r6�getStateToCopy�s
�zCopyableFailure.getStateToCopyN)r1r2r3r4r�r�r5r5r5r6r��sr�c@s*eZdZdZd	dd�Zdd�ZeZeZdS)
�
CopiedFailurea�
    A L{CopiedFailure} is a L{pb.RemoteCopy} of a L{failure.Failure}
    transferred via PB.

    @ivar type: The full import path of the exception class which was raised on
        the remote end.
    @type type: C{str}

    @ivar value: A str() representation of the remote value.
    @type value: L{CopiedFailure} or C{str}

    @ivar traceback: The remote traceback.
    @type traceback: C{str}
    Nr�defaultcCsT|durtj}|j}t|t�s|�d�}|�d�|�|d|j�|�d�dS)NrOzTraceback from remote host -- z: �
)r�logfiler�r�r��decode�writerB)rA�file�elideFrameworkCode�detail�failureTyper5r5r6�printTraceback�s


zCopiedFailure.printTracebackcCs|�t|j|j|j��S)a�
        Throw the original exception into the given generator, preserving
        traceback information if available. In the case of a L{CopiedFailure}
        where the exception type is a string, a L{pb.RemoteError} is thrown
        instead.

        @return: The next value yielded from the generator.
        @raise StopIteration: If there are no more values in the generator.
        @raise RemoteError: The wrapped remote exception.
        )�throwr;r�rBr�)rA�gr5r5r6�throwExceptionIntoGenerator�sz)CopiedFailure.throwExceptionIntoGenerator)Nrr�)r1r2r3r4r�r��printBriefTraceback�printDetailedTracebackr5r5r5r6r��s


r�cCstt|j�}||_|Sr<)r*r�r�r�)�failr��fr5r5r6�failure2Copyablesr�c@s�eZdZdZdZdZdZdefdd�Zdd�Z	d	d
�Z
dd�Zd
d�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�ZdZdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'Zd(d)�Zd*d+�Zd,d-�Zd^d.d/�Zd0d1�Zd2d3�Z d4d5�Z!d6d7�Z"d_d8d9�Z#d`d:d;�Z$d<d=�Z%d>d?�Z&d@dA�Z'dBdC�Z(dDdE�Z)dFdG�Z*dHdI�Z+dJdK�Z,dLdM�Z-dNdO�Z.dPdQ�Z/dRdS�Z0dTdU�Z1dVdW�Z2dXdY�Z3dZd[�Z4d\d]�Z5dS)a�Brokerz$
    I am a broker for objects.
    �NrqcCsrtj�||�d|_g|_g|_g|_i|_||_g|_	d|_
d|_d|_i|_
i|_i|_i|_i|_i|_dS�Nr)r�Bananar>�disconnected�disconnects�failures�connects�localObjects�security�
pageProducers�currentRequestID�currentLocalIDr~�luids�remotelyCachedObjects�remotelyCachedLUIDs�locallyCachedObjects�waitingForAnswers�
_localCleanup)rA�isClientr�r5r5r6r>s"
zBroker.__init__cCsVtt|j�ddd�D]}|j|}|��|��s|j|=q|js)|j��dSdS)zM
        Called when the consumer attached to me runs out of buffer.
        rq���N)�rangertr��sendNextPage�stillPaging�	transport�unregisterProducer)rA�pageridx�pagerr5r5r6�resumeProducing7s
��zBroker.resumeProducingcC�dSr<r5rLr5r5r6�pauseProducingD�zBroker.pauseProducingcCr�r<r5rLr5r5r6�
stopProducingHr�zBroker.stopProducingcCs0|j�|�t|j�dkr|j�|d�dSdS)Nrqr)r�rsrtr��registerProducer)rAr�r5r5r6�registerPageProducerLs�zBroker.registerPageProducercCsjt|t�r1|d}t|t�s|�d�}d|}t||d�}|r)||dd��dS|�d|�dStd��)z:
        Evaluate an expression as it's received.
        rr�zproto_%sNrqsdidNotUnderstandzNon-list expression received.)r��listr�r�rb�sendCallr/)rA�sexp�command�
methodNamerfr5r5r6�expressionReceivedQs


zBroker.expressionReceivedcCs$||jkrtd|j�d|����dS)z�
        Protocol message: (version version-number)

        Check to make sure that both ends of the protocol are speaking
        the same version dialect.

        @param vnum: The version number.
        zVersion Incompatibility: � N)�versionr/)rA�vnumr5r5r6�
proto_versiones

�zBroker.proto_versioncGs|�|�dS)z�
        Utility method to send an expression to the other side of the connection.

        @param exp: The expression.
        N)�sendEncoded)rA�expr5r5r6r�rszBroker.sendCallcCst�d|�dS)a 
        Respond to stock 'C{didNotUnderstand}' message.

        Log the command that was not understood and continue. (Note:
        this will probably be changed to close the connection or raise
        an exception in the future.)

        @param command: The command to log.
        zDidn't understand command: %rN)rrd)rAr�r5r5r6�proto_didNotUnderstandzs
zBroker.proto_didNotUnderstandc	CsT|�d|j�|jD]}z|�Wq
tyt��Yq
wd|_|j�|�dS)zF
        Initialize. Called after Banana negotiation is done.
        sversionN)r�r�r��
BaseExceptionr�deferr�factory�clientConnectionMade�rA�notifierr5r5r6�connectionReady�s

�zBroker.connectionReadyc	Cs:|jD]}z|�Wqtyt��Yqwd|_dSr<)r�r�rr�r�r5r5r6�connectionFailed�s

�
zBroker.connectionFailedc	Csd|_d|_|jr*|j��D]}z|�t�t|���Wqty)t	�
�Yqw|j��D]!}|j}|j
}z|�|t|||��Wq/tyPt	�
�Yq/w|jdd�D]}z|�WqXtylt	�
�YqXwd|_d|_d|_d|_d|_d|_d|_d|_dS)zg
        The connection was lost.

        @param reason: message to put in L{failure.Failure}
        rqN)r�r�r��values�errbackr
�FailurerXr�rr�r�rlrR�stoppedObservingrr��
localSecurity�remoteSecurityr�r�r�)rA�reason�d�lobj�	cacheablerRr�r5r5r6�connectionLost�sB���
�
zBroker.connectionLostcC�t|�sJ�|j�|�dS)zQ

        @param notifier: callback to call when the Broker disconnects.
        N)rrr�rsr�r5r5r6ru��zBroker.notifyOnDisconnectcCr�)zT

        @param notifier: callback to call if the Broker fails to connect.
        N)rrr�rsr�r5r5r6�notifyOnFail�r�zBroker.notifyOnFailcCsPt|�sJ�|jdur z|�WdStyt��YdSw|j�|�dS)zN

        @param notifier: callback to call when the Broker connects.
        N)rrr�r�r�errrsr�r5r5r6�notifyOnConnect�s
�zBroker.notifyOnConnectcCs(z	|j�|�WdStyYdSw)zY

        @param notifier: callback to remove from list of disconnect callbacks.
        N)r�ry�
ValueErrorr�r5r5r6rz�s
�zBroker.dontNotifyOnDisconnectcCs2t|t�r
|�d�}|j�|�}|durdS|jS)z�
        Get a local object for a locally unique ID.

        @return: An object previously stored with L{registerReference} or
            L{None} if there is no object which corresponds to the given
            identifier.
        r�N)r�r�rTr��getrl)rArS�lobr5r5r6�localObjectForID�s

zBroker.localObjectForIDrcCs�|dusJ�|��}|j�|�}|durEt|j�tkr3|jd|_|jdkr/|j��t	d��t	d��|�
�}t|�|j|<||j|<|S|j|��|S)z�
        Store a persistent reference to a local object and map its
        id() to a generated, session-unique ID.

        @param object: a local object
        @return: the generated ID
        Nrq�z.Maximum PB reference count exceeded.  Goodbye.z$Maximum PB reference count exceeded.)
�processUniqueIDr�r�rtr��MAX_BROKER_REFS�maxBrokerRefsViolationsr��loseConnectionr9�
newLocalIDr�r�)rArl�puidrSr5r5r6�registerReferences 	


�zBroker.registerReferencecCs2t|t�r
|�d�}|dusJ�t|�|j|<dS)z�
        Store a special (string) ID for this object.

        This is how you specify a 'base' set of objects that the remote
        protocol can connect to.

        @param name: An ID.
        @param object: The object.
        r�N)r�r�rTr�r�)rArGrlr5r5r6�setNameForLocals


zBroker.setNameForLocalcCs"t|t�r
|�d�}td||d�S)a
        Returns an object from the remote name mapping.

        Note that this does not check the validity of the name, only
        creates a translucent reference for it.

        @param name: The name to look up.
        @return: An object which maps to the name.
        r�Nr)r�r�rTrn)rArGr5r5r6�
remoteForName,s


zBroker.remoteForNamecCs2|��}|j�|�}|dur|r|j|��|S)a

        @param instance: The instance to look up.
        @param incref: Flag to specify whether to increment the
                       reference.
        @return: An ID that says what this instance is cached as
                 remotely, or L{None} if it's not.
        N)rr�r�r�r�)rA�instancer�rrSr5r5r6�cachedRemotelyAs;s

zBroker.cachedRemotelyAscCs|j|jS)zk

        @param luid: The LUID to look up.
        @return: An instance which is cached remotely.
        )r�rl)rArSr5r5r6�remotelyCachedForLUIDKszBroker.remotelyCachedForLUIDcCsn|��}|��}t|j�tkr'|jd|_|jdkr#|j��td��td��||j	|<t
||j�|j|<|S)z3
        XXX

        @return: A new LUID.
        rqrz*Maximum PB cache count exceeded.  Goodbye.z Maximum PB cache count exceeded.)rrrtr�rrr�rr9r�r��serializingPerspective)rArrrSr5r5r6�
cacheRemotelySs


zBroker.cacheRemotelycCs||j|<dS)zL(internal)

        Store a non-filled-out cached instance locally.
        N�r��rA�cidrr5r5r6�cacheLocallyhszBroker.cacheLocallycCs|j|}|Sr<rrr5r5r6�cachedLocallyAsos
zBroker.cachedLocallyAscCs�t|tj�r|j|jdd�||||d�d�|S||_||_||_||_zt	||j
d|�Wd|_d|_d|_d|_Sd|_d|_d|_d|_w)a
        Jelly an object according to the remote security rules for this broker.

        @param object: The object to jelly.
        @param perspective: The perspective.
        @param method: The method.
        @param args: Arguments.
        @param kw: Keyword arguments.
        cSs|Sr<r5)�xr5r5r6�<lambda>�r^z"Broker.serialize.<locals>.<lambda>)rRrfrUrV)�callbackKeywordsN)r�r
�Deferred�addCallbacksrer�jellyMethod�	jellyArgs�jellyKwr,r�)rArlrRrfrUrVr5r5r6ress2��
�zBroker.serializecCs(||_zt||jd|�Wd|_Sd|_w)z�
        Unjelly an sexp according to the local security rules for this broker.

        @param sexp: The object to unjelly.
        @param perspective: The perspective.
        N)r~r-r�)rAr�rRr5r5r6ra�szBroker.unserializecCr�)z3

        @return: A newly generated LUID.
        rq)r�rLr5r5r6r��zBroker.newLocalIDcCr�)z9

        @return: A newly generated request ID.
        rq)r�rLr5r5r6�newRequestID�rzBroker.newRequestIDc	Csd}d}d}	d|vr|d}|d=d|vr|d}|d=d|vr/|s$|r(Jd��|d}	|d=|jr6td��z|j|||d�}
|j|||d�}WntyXt�t���YSw|��}|	rxt�	�}
|
|j
|<|sl|rwt�d�|
�
||�nd}
|�|d	||||	|
|�|
S)
Nrq�
pbcallback�	pberrback�pbanswerz*You can't specify a no-answer requirement.zCalling Stale Broker)rRrfz&warning! using deprecated "pbcallback"smessage)r�r8rer�r
r�r
r�rrr�rrdrr�)rA�prefixrR�objectIDr[rUrV�pbc�pbe�answerRequired�netArgs�netKw�	requestID�rvalr5r5r6rQ�sP�

��	zBroker._sendMessagec	C�|�|j||||||�dSr<)�_recvMessager�rAr)r#r[r&r'r(r5r5r6�
proto_message���zBroker.proto_messagec	Cr+r<)r,rr-r5r5r6�proto_cachemessage�r/zBroker.proto_cachemessagec

Cs@t|t�s
|�d�}z||�}|durtd��|�||||�}	Wn]ty^}
z2|rLt|
t�s6|j�|
j�r=|�	|
|�n|�	t
|
�|�WYd}
~
dSWYd}
~
dSWYd}
~
dSd}
~
wty}|rvtj
ddd�t
�}|�	||�t��YdSw|r�t|	tj�r�|f}|	j|j|j||d�dS|�|	|�dSdS)a�
        Received a message-send.

        Look up message based on object, unserialize the arguments, and
        invoke it with args, and send an 'answer' or 'error' response.

        @param findObjMethod: A callable which takes C{objectID} as argument.
        @param requestID: The requiest ID.
        @param objectID: The object ID.
        @param message: The message.
        @param answerRequired:
        @param netArgs: Arguments.
        @param netKw: Keyword arguments.
        r�NzInvalid Object ID�)Peer will receive following PB traceback:T)�isError)�callbackArgs�errbackArgs)r�r�r�r9rkrr��isClassAllowed�	__class__�
_sendErrorr�r�rrdr�r
rr�_sendAnswer�_sendFailureOrError)
rA�
findObjMethodr)r#r[r&r'r(rl�	netResult�er�rUr5r5r6r,�sF

��
��
��zBroker._recvMessagecCs|�d||�dS)z�
        (internal) Send an answer to a previously sent message.

        @param netResult: The answer.
        @param requestID: The request ID.
        sanswerN�r�)rAr;r)r5r5r6r8@�zBroker._sendAnswercC�&|j|}|j|=|�|�|��dS)z�
        (internal) Got an answer to a previously sent message.

        Look up the appropriate callback and call it.

        @param requestID: The request ID.
        @param netResult: The answer.
        N)r�rxra)rAr)r;r�r5r5r6�proto_answerIs
	zBroker.proto_answercCs.|�t�dur|�||�dS|�||�dS)z�
        Call L{_sendError} or L{_sendFailure}, depending on whether C{fail}
        represents an L{Error} subclass or not.

        @param fail: The failure.
        @param requestID: The request ID.
        N)�checkr9�_sendFailurer7�rAr�r)r5r5r6r9VszBroker._sendFailureOrErrorcCs$t�d�t�|�|�||�dS)zz
        Log error and then send it.

        @param fail: The failure.
        @param requestID: The request ID.
        r1N)rrdr�r7rCr5r5r6rBcs

zBroker._sendFailurecCstt|tj�r$t|jt�s|j�|jj�r|j}nt|t�s$t	||j
j�}t|t�r.|j
j|_|�d||�
|��dS)z�
        (internal) Send an error for a previously sent message.

        @param fail: The failure.
        @param requestID: The request ID.
        serrorN)r�r
r�rBrr�r5r6r�r�r�r�r�rerCr5r5r6r7ns�


zBroker._sendErrorcCr?)z}
        (internal) Deal with an error.

        @param requestID: The request ID.
        @param fail: The failure.
        N)r�r�ra)rAr)r�r�r5r5r6�proto_error�s
zBroker.proto_errorcC�|�d|�dS)z^
        (internal) Send a DECREF directive.

        @param objectID: The object ID.
        sdecrefNr=�rAr#r5r5r6r���zBroker.sendDecRefcCsft|t�r
|�d�}|j|��}|dkr1|j|j��}|j|=|j|=|j�	|dd���dSdS)z�
        (internal) Decrement the reference count of an object.

        If the reference count is zero, it will free the reference to this
        object.

        @param objectID: The object ID.
        r�rcSr�r<r5r5r5r5r6r�r^z%Broker.proto_decref.<locals>.<lambda>N)
r�r�rTr�r�rlrr�r��pop)rAr#�refsrr5r5r6�proto_decref�s
	
�zBroker.proto_decrefcCrE)z_
        (internal) Send a DECACHE directive.

        @param objectID: The object ID.
        sdecacheNr=rFr5r5r6�decCacheRef�rGzBroker.decCacheRefcCs�|j|��}|dkrD|j|}|j}|j}z|�|t|||��Wn
ty/t��Ynw|�	�}|j
|=|j|=|�d|�dSdS)z�
        (internal) Decrement the reference count of a cached object.

        If the reference count is zero, free the reference, then send an
        'uncached' directive.

        @param objectID: The object ID.
        rsuncacheN)r�r�rlrRr�rr�rr�rr�r�)rAr#rIr�r�rRrr5r5r6�
proto_decache�s"	
���zBroker.proto_decachecCs|j|}d|_|j|=dS)zx
        (internal) Tell the client it is now OK to uncache an object.

        @param objectID: The object ID.
        N)r�rP)rAr#rFr5r5r6�
proto_uncache�s
zBroker.proto_uncache�r)NNNNr<)6r1r2r3r4r��usernamer�r+r>r�r�r�r�r�r�r�r�r�r�r�r�rur�r�rzrrrr	r
rr
rrrrerarrrQr.r0r,r8r@r9rBr7rDr�rJrKrLrMr5r5r5r6r�sf&


	)



*)

B	

r�cCs>t�}|�|�|��}t�}|�|�|�|�|��}|S)z�
    Respond to a challenge.

    This is useful for challenge/response authentication.

    @param challenge: A challenge.
    @param password: A password.
    @return: The password hashed twice.
    )r�update�digest)�	challenge�password�m�hashedPassword�doubleHashedPasswordr5r5r6�respond�s



rWcCs.tdd�tt�dd��D��}t|���}|S)z%

    @return: Some random data.
    css�|]	}t�dd�VqdS)�A�ZN)�random�randint)�.0rr5r5r6�	<genexpr>�s�zchallenge.<locals>.<genexpr>��)r�r�rZ�	randrangerrQ)�crapr5r5r6rR�srRc@s�eZdZdZeZdZdefdd�Zdd�Z	dd�Z
d	d
�Zdd�Zddd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd dd�ZdS)!�PBClientFactoryz�
    Client factory for PB brokers.

    As with all client factories, use with reactor.connectTCP/SSL/etc..
    getPerspective and getRootObject can be called either before or
    after the connect.
    FcCs||_||_|��dS)aE
        @param unsafeTracebacks: if set, tracebacks for exceptions will be sent
            over the wire.
        @type unsafeTracebacks: C{bool}

        @param security: security options used by the broker, default to
            C{globalSecurity}.
        @type security: L{twisted.spread.jelly.SecurityOptions}
        N)r�r��_reset)rAr�r�r5r5r6r>s
zPBClientFactory.__init__cCs|jd|jd�}||_|S)zP
        Build the broker instance, passing the security options to it.
        T�r�r�)rr�r�)rA�addr�pr5r5r6�
buildProtocolszPBClientFactory.buildProtocolcCsg|_d|_d|_dSr<)�rootObjectRequests�_broker�_rootrLr5r5r6rcs
zPBClientFactory._resetcCs&|j}|��|D]}|�|�q	dSr<)rhrcr�)rAr��	deferredsr�r5r5r6�_failAlls
�zPBClientFactory._failAllcCs|�|�dSr<)rl)rA�	connectorr�r5r5r6�clientConnectionFailed%sz&PBClientFactory.clientConnectionFailedrcCs"|r
d|_d|_dS|�|�dS)zJ
        Reconnecting subclasses should call with reconnecting=1.
        N)rirjrl)rArmr��reconnectingr5r5r6�clientConnectionLost(s
z$PBClientFactory.clientConnectionLostcCs8||_|�d�|_|j}g|_|D]}|�|j�qdS)N�root)rir
rjrhrx)rArP�dsr�r5r5r6r�4s�z$PBClientFactory.clientConnectionMadecCs2|jr
|jjs
t�|j�St��}|j�|�|S)ze
        Get root object of remote PB server.

        @return: Deferred of the root object.
        )rir�r
�succeedrjrrhrs)rAr�r5r5r6�
getRootObject<s
zPBClientFactory.getRootObjectcCs|jr|jj��dSdS)z�
        If the factory is connected, close the connection.

        Note that if you set up the factory to reconnect, you will need to
        implement extra logic to prevent automatic reconnection after this
        is called.
        N)rir�rrLr5r5r6�
disconnectHs�zPBClientFactory.disconnectcCs|�d|��|j||�S)N�login)r��addCallback�_cbResponse)rArqrOrS�clientr5r5r6�_cbSendUsernameSs�zPBClientFactory._cbSendUsernamecCs|\}}|�dt||�|�S)NrW)r�rW)rA�
challengesrSryrR�
challengerr5r5r6rxXszPBClientFactory._cbResponsecCs|�d|�S)ab
        Attempt an anonymous login on the given remote root object.

        @type root: L{RemoteReference}
        @param root: The object on which to attempt the login, most likely
            returned by a call to L{PBClientFactory.getRootObject}.

        @param client: A jellyable object which will be used as the I{mind}
            parameter for the login attempt.

        @rtype: L{Deferred}
        @return: A L{Deferred} which will be called back with a
            L{RemoteReference} to an avatar when anonymous login succeeds, or
            which will errback if anonymous login fails.
        �loginAnonymous)r�)rArqryr5r5r6�_cbLoginAnonymous\sz!PBClientFactory._cbLoginAnonymousNcCs>|��}t�|�r|�|j|�|S|�|j|j|j|�|S)a�
        Login and get perspective from remote PB server.

        Currently the following credentials are supported::

            L{twisted.cred.credentials.IUsernamePassword}
            L{twisted.cred.credentials.IAnonymous}

        @rtype: L{Deferred}
        @return: A L{Deferred} which will be called back with a
            L{RemoteReference} for the avatar logged in to, or which will
            errback if login fails.
        )rtr�
providedByrwr~rzrOrS)rA�credentialsryr�r5r5r6rvns
��zPBClientFactory.loginrNr<)r1r2r3r4r�rr�r+r>rgrcrlrnrpr�rtrurzrxr~rvr5r5r5r6rb�s"
rbc@s6eZdZdZdZeZdefdd�Zdd�Z	dd�Z
d	S)
�PBServerFactorya�
    Server factory for perspective broker.

    Login is done using a Portal object, whose realm is expected to return
    avatars implementing IPerspective. The credential checkers in the portal
    should accept IUsernameHashedPassword or IUsernameMD5Password.

    Alternatively, any object providing or adaptable to L{IPBRoot} can be
    used instead of a portal to provide the root object of the PB server.
    FcCst|�|_||_||_dS)a�
        @param root: factory providing the root Referenceable used by the broker.
        @type root: object providing or adaptable to L{IPBRoot}.

        @param unsafeTracebacks: if set, tracebacks for exceptions will be sent
            over the wire.
        @type unsafeTracebacks: C{bool}

        @param security: security options used by the broker, default to
            C{globalSecurity}.
        @type security: L{twisted.spread.jelly.SecurityOptions}
        N)rrqr�r�)rArqr�r�r5r5r6r>�s


zPBServerFactory.__init__cCs.|jd|jd�}||_|�d|j�|��|S)zT
        Return a Broker attached to the factory (as the service provider).
        Frdrq)rr�r�r	rq�
rootObject)rAre�protor5r5r6rg�szPBServerFactory.buildProtocolcCr�r<r5)rArr5r5r6r��r�z$PBServerFactory.clientConnectionMadeN)r1r2r3r4r�r�rr+r>rgr�r5r5r5r6r��s	r�c@� eZdZdZdd�Zdd�ZdS)�IUsernameMD5Passwordab
    I encapsulate a username and a hashed password.

    This credential is used for username/password over PB. CredentialCheckers
    which check this kind of credential must store the passwords in plaintext
    form or as a MD5 digest.

    @type username: C{str} or C{Deferred}
    @ivar username: The username associated with these credentials.
    cCrZ)a�
        Validate these credentials against the correct password.

        @type password: C{str}
        @param password: The correct, plaintext password against which to
            check.

        @rtype: C{bool} or L{Deferred}
        @return: C{True} if the credentials represented by this object match the
            given password, C{False} if they do not, or a L{Deferred} which will
            be called back with one of these values.
        Nr5�rSr5r5r6�
checkPassword�r^z"IUsernameMD5Password.checkPasswordcCrZ)a�
        Validate these credentials against the correct MD5 digest of the
        password.

        @type password: C{str}
        @param password: The correct MD5 digest of a password against which to
            check.

        @rtype: C{bool} or L{Deferred}
        @return: C{True} if the credentials represented by this object match the
            given digest, C{False} if they do not, or a L{Deferred} which will
            be called back with one of these values.
        Nr5r�r5r5r6�checkMD5Password�r^z%IUsernameMD5Password.checkMD5PasswordN)r1r2r3r4r�r�r5r5r5r6r��sr�c@r�)�_PortalRootz/
    Root object, used to login to portal.
    cCs
||_dSr<)�portal)rAr�r5r5r6r>�s
z_PortalRoot.__init__cCst|j|�Sr<)�_PortalWrapperr�)rArPr5r5r6r��sz_PortalRoot.rootObjectN)r1r2r3r4r>r�r5r5r5r6r��sr�c@r:)�_JellyableAvatarMixinzk
    Helper class for code which deals with avatars which PB must be capable of
    sending to a peer.
    csT|\}}�t�|�st|d�}|��}�g��fdd�}||jj|<|j�|�|S)z�
        Ensure that the avatar to be returned to the client is jellyable and
        set up disconnection notification to call the realm's logout object.
        rRcs �sdS�d}�d=|�dSr�r5)�fn��logoutr5r6�maybeLogouts

z3_JellyableAvatarMixin._cbLogin.<locals>.maybeLogout)r(rrhrrPr�ru)rA�result�	interface�avatarrr�r5r�r6�_cbLogin�s


z_JellyableAvatarMixin._cbLoginN)r1r2r3r4r�r5r5r5r6r��sr�c@s(eZdZdZdd�Zdd�Zdd�ZdS)	r�z=
    Root Referenceable object, used to login to portal.
    cCs||_||_dSr<)r�rP)rAr�rPr5r5r6r>s
z_PortalWrapper.__init__cCst�}|t|j|j||�fS)z[
        Start of username/password login.

        @param username: The username.
        )rR�_PortalAuthChallengerr�rP)rArO�cr5r5r6�remote_loginsz_PortalWrapper.remote_logincCs"|j�t�|t�}|�|j�|S)aU
        Attempt an anonymous login.

        @param mind: An object to use as the mind parameter to the portal login
            call (possibly None).

        @rtype: L{Deferred}
        @return: A Deferred which will be called back with an avatar when login
            succeeds or which will be errbacked if login fails somehow.
        )r�rvrrYrwr�)rA�mindr�r5r5r6�remote_loginAnonymous&sz$_PortalWrapper.remote_loginAnonymousN)r1r2r3r4r>r�r�r5r5r5r6r�s
	r�c@rD)r�z5
    Called with response to password challenge.
    cCs||_||_||_||_dSr<)r�rPrOrR)rAr�rPrOrRr5r5r6r><s
z_PortalAuthChallenger.__init__cCs&||_|j�||t�}|�|j�|Sr<)�responser�rvrYrwr�)rAr�r�r�r5r5r6�remote_respondBsz$_PortalAuthChallenger.remote_respondcCs|�t|����S)z�
        L{IUsernameHashedPassword}

        @param password: The password.
        @return: L{_PortalAuthChallenger.checkMD5Password}
        )r�rrQ)rArSr5r5r6r�Hr>z#_PortalAuthChallenger.checkPasswordcCs.t�}|�|�|�|j�|��}|j|kS)z�
        L{IUsernameMD5Password}

        @param md5Password:
        @rtype: L{bool}
        @return: L{True} if password matches.
        )rrPrRrQr�)rA�md5Password�md�correctr5r5r6r�Qs


z&_PortalAuthChallenger.checkMD5PasswordN)r1r2r3r4r>r�r�r�r5r5r5r6r�6s	r�)(rrrrrr rrrrrrrr!r&r%r'r"r$r#r�portnor/r8r9rXrErYr`rhrnr�r�r�r�rWrRrbr�r�rN)\r4rZ�hashlibr�zope.interfacerr�twisted.cred.credentialsrrrr�twisted.cred.portalr	�twisted.internetr
r�twisted.persistedr�twisted.pythonr
rr�twisted.python.compatrr�twisted.python.componentsr�twisted.spreadr�twisted.spread.flavorsrrrrrrrrrrrrr r!r"r#r$r%r&r'�twisted.spread.interfacesr(r)�twisted.spread.jellyr*r+r,r-rr�r=r/r8r9r;rErXrYr`rh�	Ephemeralrnr�r�r�r�r�r�r�rWrR�
ClientFactoryrb�
ServerFactoryr�r�r�r�r�r��__all__r5r5r5r6�<module>stX )(	
	$
+
W
0*#")

Spamworldpro Mini