o bj;@sdZddlmZddlmZddlmZddlmZddl m Z m Z m Z m Z ddlmZddlmZd Zd d ZGd d d ZGddde jZGddde jZee jGdddejZGdddejZddZGdddZGddde j Z!dS)a External server-side components. Most Jabber server implementations allow for add-on components that act as a separate entity on the Jabber network, but use the server-to-server functionality of a regular Jabber IM server. These so-called 'external components' are connected to the Jabber server using the Jabber Component Protocol as defined in U{JEP-0114}. This module allows for writing external server-side component by assigning one or more services implementing L{ijabber.IService} up to L{ServiceManager}. The ServiceManager connects to the Jabber server and is responsible for the corresponding XML stream. ) implementer)service)defer)log)errorijabber jstrports xmlstream) internJID)domishzjabber:component:acceptcCst||}t|S)z XML stream factory for external server-side components. @param componentid: JID of the component. @type componentid: L{unicode} @param password: password used to authenticate to the server. @type password: C{str} )ConnectComponentAuthenticatorr XmlStreamFactory) componentidpasswordarJ/usr/lib/python3/dist-packages/twisted/words/protocols/jabber/component.pycomponentFactory!s rc@s(eZdZdZddZddZddZdS) ComponentInitiatingInitializerz External server-side component authentication initializer for the initiating entity. @ivar xmlstream: XML stream between server and component. @type xmlstream: L{xmlstream.XmlStream} cCs||_d|_dSN)r _deferredselfxsrrr__init__7s z'ComponentInitiatingInitializer.__init__cCs`|j}t|jjdf}t|j|jj}|t || d|j | |t |_|jS)N handshakez /handshake)r r Element namespace hashPasswordsid authenticatorr addContentstraddOnetimeObserver _cbHandshakesendrDeferredr)rrhsdigestrrr initialize;s  z)ComponentInitiatingInitializer.initializecCs|jj|j_|jddSr)r otherEntity thisEntityrcallback)r_rrrr$Gs z+ComponentInitiatingInitializer._cbHandshakeN)__name__ __module__ __qualname____doc__rr)r$rrrrr.s  rc@s$eZdZdZeZddZddZdS)r z Authenticator to permit an XmlStream to authenticate against a Jabber server as an external component (where the Authenticator is initiating the stream). cCstj||||_dS)z @type componentjid: C{str} @param componentjid: Jabber ID that this component wishes to bind to. @type password: C{str} @param password: Password/secret this component uses to authenticate. N)r ConnectAuthenticatorrr)r componentjidrrrrrWs z&ConnectComponentAuthenticator.__init__cCs$d|_tj||t|g|_dS)Nrr)versionr r2associateWithStreamr initializersrrrrr6csz1ConnectComponentAuthenticator.associateWithStreamN)r.r/r0r1NS_COMPONENT_ACCEPTrrr6rrrrr Ns  r c@s<eZdZdZeZddZddZddZdd Z d d Z d S) ListenComponentAuthenticatorz Authenticator for accepting components. @since: 8.2 @ivar secret: The shared secret used to authorized incoming component connections. @type secret: C{unicode}. cCs||_tj|dSr)secretr ListenAuthenticatorr)rr:rrrrvsz%ListenComponentAuthenticator.__init__cCsd|_tj||dS)z Associate the authenticator with a stream. This sets the stream's version to 0.0, because the XEP-0114 component protocol was not designed for XMPP 1.0. r4N)r5r r;r6rrrrr6zsz0ListenComponentAuthenticator.associateWithStreamcCsttj|||j|jkrtd}|j|dS|jjs+td}|j|dS|j |j d|j dS)aa Called by the stream when it has started. This examines the default namespace of the incoming stream and whether there is a requested hostname for the component. Then it generates a stream identifier, sends a response header and adds an observer for the first incoming element, triggering L{onElement}. zinvalid-namespaceNzimproper-addressing/*) r r; streamStarted defaultUrirr StreamErrorsendStreamErrorr+ sendHeaderr# onElement)r rootElementexcrrrr=s      z*ListenComponentAuthenticator.streamStartedcCsB|j|jf|jdfkr|t|dStd}|j|dS)a. Called on incoming XML Stanzas. The very first element received should be a request for handshake. Otherwise, the stream is dropped with a 'not-authorized' error. If a handshake request was received, the hash is extracted and passed to L{onHandshake}. rnot-authorizedN) urinamer onHandshaker"rr?r r@)relementrDrrrrBs  z&ListenComponentAuthenticator.onElementcCs^t|jjt|j}||krtjddd}|j|dS|jd|j |jtj dS)a9 Called upon receiving the handshake request. This checks that the given hash in C{handshake} is equal to a calculated hash, responding with a handshake reply or a stream error. If the handshake was ok, the stream is authorized, and XML Stanzas may be exchanged. rEz Invalid hash)textz N) r rrr"r:rr?r@r%dispatchSTREAM_AUTHD_EVENT)rrcalculatedHashrDrrrrHs   z(ListenComponentAuthenticator.onHandshakeN) r.r/r0r1r8rrr6r=rBrHrrrrr9js   r9c@0eZdZdZddZddZddZdd Zd S) Servicez1 External server-side component service. cCdSrrrrrrcomponentConnectedzService.componentConnectedcCrPrrrrrrcomponentDisconnectedrRzService.componentDisconnectedcCrPrrrrrrtransportConnectedrRzService.transportConnectedcCs|j|dS)a Send data over service parent's XML stream. @note: L{ServiceManager} maintains a queue for data sent using this method when there is no current established XML stream. This data is then sent as soon as a new stream has been established and initialized. Subsequently, L{componentConnected} will be called again. If this queueing is not desired, use C{send} on the XmlStream object (passed to L{componentConnected}) directly. @param obj: data to be sent over the XML stream. This is usually an object providing L{domish.IElement}, or serialized XML. See L{xmlstream.XmlStream} for details. N)parentr%robjrrrr%sz Service.sendN)r.r/r0r1rQrTrUr%rrrrrOs  rOc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)ServiceManagera[ Business logic for a managed component connection to a Jabber router. This service maintains a single connection to a Jabber router and provides facilities for packet routing and transmission. Business logic modules are services implementing L{ijabber.IService} (like subclasses of L{Service}), and added as sub-service. cCsztj|||_d|_g|_t|j||_|jtj |j |jtj |j |jtj |j|jj|_|jj|_dSr)r MultiServicerjabberIdr _packetQueuer _xsFactory addBootstrapSTREAM_CONNECTED_EVENT _connectedrL_authdSTREAM_END_EVENT _disconnectedremoveBootstrap)rjidrrrrrs  zServiceManager.__init__cCs|jSr)r]rSrrr getFactoryszServiceManager.getFactorycCs*||_|D] }tj|r||qdSr)r rIService providedByrU)rrcrrrr`s   zServiceManager._connectedcCsB|jD]}|j|qg|_|D] }tj|r||qdSr)r\r r%rrgrhrQ)rrprirrrras   zServiceManager._authdcCs(d|_|D] }tj|r|qdSr)r rrgrhrT)rr-rirrrrcs  zServiceManager._disconnectedcCs*|jdkr |j|dS|j|dS)a Send data over the XML stream. When there is no established XML stream, the data is queued and sent out when a new XML stream has been established and initialized. @param obj: data to be sent over the XML stream. This is usually an object providing L{domish.IElement}, or serialized XML. See L{xmlstream.XmlStream} for details. N)r r%r\appendrWrrrr%"s zServiceManager.sendN) r.r/r0r1rrfr`rarcr%rrrrrYs  rYcCs(t||}t||}|||S)z[ Constructs a pre-built L{ServiceManager}, using the specified strport string. )rYrclientrfsetServiceParent)rerstrportsvc client_svcrrrbuildServiceManager4s  rqc@rN) Routera XMPP Server's Router. A router connects the different components of the XMPP service and routes messages between them based on the given routing table. Connected components are trusted to have correct addressing in the stanzas they offer for routing. A route destination of L{None} adds a default route. Traffic for which no specific route exists, will be routed to this default route. @since: 8.2 @ivar routes: Routes based on the host part of JIDs. Maps host names to the L{EventDispatcher}s that should receive the traffic. A key of L{None} means the default route. @type routes: C{dict} cCs i|_dSr)routesrSrrrrU zRouter.__init__cCs||j|<|d|jdS)a Add a new route. The passed XML Stream C{xs} will have an observer for all stanzas added to route its outgoing traffic. In turn, traffic for C{destination} will be passed to this stream. @param destination: Destination of the route to be added as a host name or L{None} for the default route. @type destination: C{str} or L{None}. @param xs: XML Stream to register the route for. @type xs: L{EventDispatcher}. r<N)rs addObserverrouter destinationrrrraddRouteXs zRouter.addRoutecCs,|d|j||j|kr|j|=dSdS)a Remove a route. @param destination: Destination of the route that should be removed. @type destination: C{str}. @param xs: XML Stream to remove the route for. @type xs: L{EventDispatcher}. r<N)removeObserverrvrsrwrrr removeRouteis  zRouter.removeRoutecCs`t|d}td|d||j|jvr&|j|j|dS|jd|dS)z{ Route a stanza. @param stanza: The stanza to be routed. @type stanza: L{domish.Element}. toz Routing to z: N)JIDrmsgfulltoXmlhostrsr%)rstanzarxrrrrvvs  z Router.routeN)r.r/r0r1rryr{rvrrrrrr@s  rrc@s>eZdZdZdZdddZddZdd Zd d Zd d Z dS)XMPPComponentServerFactoryz XMPP Component Server factory. This factory accepts XMPP external component connections and makes the router service route traffic for a component's bound domain to that component. @since: 8.2 Fr:csP|_|_fdd}tj|tjjtjj d_ dS)Ncs tjSr)r9r:rrSrrauthenticatorFactoryrtzAXMPPComponentServerFactory.__init__..authenticatorFactoryr) routerr:r XmlStreamServerFactoryrr^r_onConnectionMaderLonAuthenticatedserial)rrr:rrrSrrs  z#XMPPComponentServerFactory.__init__csT|j_|jd7_fdd}fdd}|jr |_|_tj|jdS)z{ Called when a component connection was made. This enables traffic debugging on incoming streams. ctdj|fdS)Nz RECV (%d): %rrr~rbufrrr logDataInz>XMPPComponentServerFactory.onConnectionMade..logDataIncr)Nz SEND (%d): %rrrrrr logDataOutrz?XMPPComponentServerFactory.onConnectionMade..logDataOutN)r logTraffic rawDataInFn rawDataOutFnrur STREAM_ERROR_EVENTonError)rrrrrrrrs  z+XMPPComponentServerFactory.onConnectionMadecCs0|jj}|j|||tj|jd||dS)z Called when a component has successfully authenticated. Add the component to the routing table and establish a handler for a closed connection. rN)r+rrryrur rbonConnectionLost)rrrxrrrrs z*XMPPComponentServerFactory.onAuthenticatedcCst|ddS)Nz Stream Error)rerr)rreasonrrrrsz"XMPPComponentServerFactory.onErrorcCs|j||dSr)rr{)rrxrrrrrrsz+XMPPComponentServerFactory.onConnectionLostN)r:) r.r/r0r1rrrrrrrrrrrs   rN)"r1zope.interfacertwisted.applicationrtwisted.internetrtwisted.pythonrtwisted.words.protocols.jabberrrrr "twisted.words.protocols.jabber.jidr r}twisted.words.xishr r8rrr2r r;r9rgrOrZrYrqrrrrrrrrs&        V!R G