a
    î½j?'  ã                   @   s,   d Z ddlT ddlmZ G dd„ deƒZdS )a¹  Browser module provides DISCO server framework for your application.
This functionality can be used for very different purposes - from publishing
software version and supported features to building of "XMPP site" that users
can navigate with their disco browsers and interact with active content.

Such functionality is achieved via registering "DISCO handlers" that are
automatically called when user requests some node of your disco tree.
é   )Ú*)ÚPlugInc                   @   sX   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	d
„Zddd„Zddd„Z	ddd„Z
dd„ ZdS )ÚBrowsera	   WARNING! This class is for components only. It will not work in client mode!

        Standart xmpppy class that is ancestor of PlugIn and can be attached
        to your application.
        All processing will be performed in the handlers registered in the browser
        instance. You can register any number of handlers ensuring that for each
        node/jid combination only one (or none) handler registered.
        You can register static information or the fully-blown function that will
        calculate the answer dynamically.
        Example of static info (see XEP-0030, examples 13-14):
        # cl - your xmpppy connection instance.
        b=xmpp.browser.Browser()
        b.PlugIn(cl)
        items=[]
        item={}
        item['jid']='catalog.shakespeare.lit'
        item['node']='books'
        item['name']='Books by and about Shakespeare'
        items.append(item)
        item={}
        item['jid']='catalog.shakespeare.lit'
        item['node']='clothing'
        item['name']='Wear your literary taste with pride'
        items.append(item)
        item={}
        item['jid']='catalog.shakespeare.lit'
        item['node']='music'
        item['name']='Music from the time of Shakespeare'
        items.append(item)
        info={'ids':[], 'features':[]}
        b.setDiscoHandler({'items':items,'info':info})

        items should be a list of item elements.
        every item element can have any of these four keys: 'jid', 'node', 'name', 'action'
        info should be a dicionary and must have keys 'ids' and 'features'.
        Both of them should be lists:
            ids is a list of dictionaries and features is a list of text strings.
        Example (see XEP-0030, examples 1-2)
        # cl - your xmpppy connection instance.
        b=xmpp.browser.Browser()
        b.PlugIn(cl)
        items=[]
        ids=[]
        ids.append({'category':'conference','type':'text','name':'Play-Specific Chatrooms'})
        ids.append({'category':'directory','type':'chatroom','name':'Play-Specific Chatrooms'})
        features=[NS_DISCO_INFO,NS_DISCO_ITEMS,NS_MUC,NS_REGISTER,NS_SEARCH,NS_TIME,NS_VERSION]
        info={'ids':ids,'features':features}
        # info['xdata']=xmpp.protocol.DataForm() # XEP-0128
        b.setDiscoHandler({'items':[],'info':info})
    c                 C   s"   t  | ¡ d}g | _di i| _dS )z0Initialises internal variables. Used internally.ZbrowserÚ N)r   Ú__init__Z_exported_methodsÚ	_handlers)ÚselfZDBG_LINE© r	   úR/Users/admin/.hermes/hermes-agent/venv/lib/python3.9/site-packages/xmpp/browser.pyr   P   s    
zBrowser.__init__c                 C   s,   |j d| jdtd |j d| jdtd dS )ze Registers it's own iq handlers in your application dispatcher instance.
            Used internally.ÚiqÚget©ÚtypÚnsN)ZRegisterHandlerÚ_DiscoveryHandlerÚNS_DISCO_INFOÚNS_DISCO_ITEMS)r   Úownerr	   r	   r
   ÚpluginW   s    zBrowser.pluginc                 C   s0   | j jd| jdtd | j jd| jdtd dS )zj Unregisters browser's iq handlers from your application dispatcher instance.
            Used internally.r   r   r   N)Ú_ownerZUnregisterHandlerr   r   r   )r   r	   r	   r
   Úplugout]   s    zBrowser.plugouté    c                 C   sä   || j v r| j | }n$|r0i | j |< | j | }n
| j d }|du rJdg}n| dd¡ d¡}|D ]b}|dkr~||v r~|| }q`|r¤|dkr¤t|t|i||< || }q`|s°d|v r¼|df  S  dS q`d|v sÐ|rØ|dfS tdƒ‚dS )a    Returns dictionary and key or None,None
            None - root node (w/o "node" attribute)
            /a/b/c - node
            /a/b/  - branch
            Set returns '' or None as the key
            get returns '' or None as the key or None as the dict.
            Used internally.r   Nú/z /)NNr   zCorrupted data)r   ÚreplaceÚsplitÚdictÚstrÚ	Exception)r   ÚnodeÚjidÚsetÚcurÚir	   r	   r
   Ú_traversePathc   s    

&zBrowser._traversePathr   c                 C   s4   |   d|||f d¡ |  ||d¡\}}|||< dS )a   This is the main method that you will use in this class.
            It is used to register supplied DISCO handler (or dictionary with static info)
            as handler of some disco tree branch.
            If you do not specify the node this handler will be used for all queried nodes.
            If you do not specify the jid this handler will be used for all queried JIDs.

            Usage:
            cl.Browser.setDiscoHandler(someDict,node,jid)
            or
            cl.Browser.setDiscoHandler(someDISCOHandler,node,jid)
            where

            someDict={
                'items':[
                          {'jid':'jid1','action':'action1','node':'node1','name':'name1'},
                          {'jid':'jid2','action':'action2','node':'node2','name':'name2'},
                          {'jid':'jid3','node':'node3','name':'name3'},
                          {'jid':'jid4','node':'node4'}
                        ],
                'info' :{
                          'ids':[
                                  {'category':'category1','type':'type1','name':'name1'},
                                  {'category':'category2','type':'type2','name':'name2'},
                                  {'category':'category3','type':'type3','name':'name3'},
                                ],
                          'features':['feature1','feature2','feature3','feature4'],
                          'xdata':DataForm
                        }
                     }

            and/or

            def someDISCOHandler(session,request,TYR):
                # if TYR=='items':  # returns items list of the same format as shown above
                # elif TYR=='info': # returns info dictionary of the same format as shown above
                # else: # this case is impossible for now.
        z(Registering handler %s for "%s" node->%sÚinfor   N)ÚDEBUGr#   )r   Úhandlerr   r   Úkeyr	   r	   r
   ÚsetDiscoHandlerz   s    &zBrowser.setDiscoHandlerc                 C   s    |   ||¡\}}|r|| S dS )z‹ Returns the previously registered DISCO handler
            that is resonsible for this node/jid combination.
            Used internally.N)r#   )r   r   r   r'   r	   r	   r
   ÚgetDiscoHandler¤   s    zBrowser.getDiscoHandlerc                 C   s2   |   ||¡\}}|r.|| }|t |t = |S dS )zÚ Unregisters DISCO handler that is resonsible for this
            node/jid combination. When handler is unregistered the branch
            is handled in the same way that it's parent branch from this moment.
        N)r#   r   r   )r   r   r   r'   r&   r	   r	   r
   ÚdelDiscoHandler«   s
    zBrowser.delDiscoHandlerc                 C   sè  |  ¡ }|r|}nd}|  || ¡ ¡}|sr|  d| ¡  ¡  d¡| d¡| ¡  d¡f d¡ | t|t	ƒ¡ t
‚|  d| ¡  ¡  d¡| d¡| ¡  d¡f d¡ | d¡}|r¾| |¡ | d¡}| ¡ tkr0t|ƒtkrì|d	 }n|||d	ƒ}|d
kr| t|t	ƒ¡ t
‚|D ]}	| d|	¡ qn¦| ¡ tkrÖt|ƒtkrV|d }
n|||dƒ}
|
d
kr€| t|t	ƒ¡ t
‚|
d D ]}| d|¡ qˆ|
d D ]}| dd|i¡ q¤d|
v rÖ|j|
d d | |¡ t
‚d
S )z¸ Servers DISCO iq request from the remote client.
            Automatically determines the best handler to use and calls it
            to handle the request. Used internally.
        ÚNonez3No Handler for request with jid->%s node->%s ns->%sÚutf8Úerrorz-Handling request with jid->%s node->%s ns->%sÚokÚresultÚqueryÚitemsNÚitemr$   ZidsÚidentityÚfeaturesÚfeatureÚvarZxdata)r   )ZgetQuerynoder)   ZgetTor%   Ú__str__ÚencodeZ
getQueryNSÚsendÚErrorZERR_ITEM_NOT_FOUNDZNodeProcessedZ
buildReplyZsetQuerynodeZgetTagr   Útyper   ZaddChildr   )r   ÚconnÚrequestr   Znodestrr&   ÚrepÚqÚlstr2   ÚdtÚidr5   r	   r	   r
   r   ¶   s>    44



 
zBrowser._DiscoveryHandlerN)r   )r   r   )r   r   )r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r#   r(   r)   r*   r   r	   r	   r	   r
   r      s   2

*

r   N)rF   Ú
dispatcherÚclientr   r   r	   r	   r	   r
   Ú<module>   s   	