view src/plugins/plugin_xep_0060.py @ 301:e33b3a777f10

plugin xep-0060: added getEntry method
author Goffi <goffi@goffi.org>
date Mon, 21 Feb 2011 01:36:52 +0100
parents 7c79d4a8c9e6
children 2b52a5da0978
line wrap: on
line source

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
SAT plugin for Publish-Subscribe (xep-0060)
Copyright (C) 2009, 2010, 2011  Jérôme Poisson (goffi@goffi.org)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from logging import debug, info, error
from twisted.internet import protocol
from twisted.words.protocols.jabber import client, jid
from twisted.words.protocols.jabber import error as jab_error
import twisted.internet.error

from wokkel import disco, iwokkel, pubsub

from zope.interface import implements

PLUGIN_INFO = {
"name": "Publish-Subscribe",
"import_name": "XEP-0060",
"type": "XEP",
"protocols": ["XEP-0060"],
"dependencies": [],
"main": "XEP_0060",
"handler": "yes",
"description": _("""Implementation of PubSub Protocol""")
}

class XEP_0060():

    def __init__(self, host):
        info(_("PubSub plugin initialization"))
        self.host = host
        self.managedNodes=[]
        self.clients={}
        """host.bridge.addMethod("getItems", ".communication", in_sign='ssa{ss}s', out_sign='as', method=self.getItems,
                              async = True,
                              doc = { 'summary':'retrieve items',
                                      'param_0':'service: pubsub service',
                                      'param_1':'node: node identifier',
                                      'param_2':'\n'.join(['options: can be:',
                                          '- max_items: see XEP-0060 #6.5.7',
                                          '- sub_id: subscription identifier, see XEP-0060 #7.2.2.2']),
                                      'param_3':'%(doc_profile)s',
                                      'return':'array of raw XML (content of the items)'
                                    })"""

    def getHandler(self, profile):
        self.clients[profile] = SatPubSubClient(self.host, self)
        return self.clients[profile]

    def addManagedNode(self, node_name, callback):
        """Add a handler for a namespace
        @param namespace: NS of the handler (will appear in disco info)
        @param callback: method to call when the handler is found
        @param profile: profile which manage this handler"""
        self.managedNodes.append((node_name, callback))

    def publish(self, service, nodeIdentifier, items=None, profile_key='@DEFAULT@'):
        profile = self.host.memory.getProfileName(profile_key)
        if not profile:
            err_mess = _('Trying to publish items with an unknown profile key [%s]') % profile_key
            error(err_mess)
            raise Exception(err_mess)
        try:
            client = self.clients[profile]
        except KeyError:
            err_mess = _('INTERNAL ERROR: no handler for required profile')
            error(err_mess)
            raise Exception(err_mess)
        client.publish(service, nodeIdentifier, items, client.parent.jid) 

    def getItems(self, service, node, max_items=None, sub_id=None, profile_key='@DEFAULT@'):
        profile = self.host.memory.getProfileName(profile_key)
        if not profile:
            err_mess = _('Trying to get items with an unknown profile key [%s]') % profile_key
            error(err_mess)
            raise Exception(err_mess)
        try:
            client = self.clients[profile]
        except KeyError:
            err_mess = _('INTERNAL ERROR: no handler for required profile')
            error(err_mess)
            raise Exception(err_mess)
        return client.items(service, node, max_items, sub_id, client.parent.jid)
        
class SatPubSubClient(pubsub.PubSubClient):
    implements(disco.IDisco)

    def __init__(self, host, parent_plugin):
        self.host=host
        self.parent_plugin = parent_plugin
        pubsub.PubSubClient.__init__(self)

    def connectionInitialized(self):
        pubsub.PubSubClient.connectionInitialized(self)
    
    def itemsReceived(self, event):
        for node in self.parent_plugin.managedNodes:
            if event.nodeIdentifier == node[0]:
                return node[1](event, self.parent.profile)

    def deleteReceived(self, event):
        import pdb
        pdb.set_trace()

    def purgeReceived(self, event):
        import pdb
        pdb.set_trace()

    def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
        _disco_info = []
        self.host.trigger.point("PubSub Disco Info", _disco_info, self.parent.profile)
        return _disco_info
    
    def getDiscoItems(self, requestor, target, nodeIdentifier=''):
        return []