view src/plugins/plugin_xep_0163.py @ 283:68cd30d982a5

core: added plugins for PubSub et PEP (first drafts) - plugin for XEP 0060, 0163 and 0107, based on Wokkel's pubsub - XEP 0115 plugin has been fixed, so generateHash can be called when features have changed (can be usefull to filter PEP)
author Goffi <goffi@goffi.org>
date Thu, 03 Feb 2011 18:06:25 +0100
parents
children 3b382fa0ac28
line wrap: on
line source

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

"""
SAT plugin for Personal Eventing Protocol (xep-0163)
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
from wokkel.formats import Mood

NS_USER_MOOD = 'http://jabber.org/protocol/mood'

PLUGIN_INFO = {
"name": "Personal Eventing Protocol Plugin",
"import_name": "XEP_0163",
"type": "XEP",
"protocols": ["XEP-0163", "XEP-0107"],
"dependencies": ["XEP-0060"],
"main": "XEP_0163",
"handler": "no",
"description": _("""Implementation of Personal Eventing Protocol""")
}

class XEP_0163():

    def __init__(self, host):
        info(_("PEP plugin initialization"))
        self.host = host
        self.pep_events=set()
        host.trigger.add("PubSub Disco Info", self.disoInfoTrigger)
        host.bridge.addSignal("personalEvent", ".communication", signature='ssa{ss}s') #args: from (jid), type(MOOD, TUNE, etc), data, profile
        self.addPEPEvent("MOOD", NS_USER_MOOD, self.userMoodCB)

    def disoInfoTrigger(self, disco_info, profile):
        """Add info from managed PEP
        @param disco_info: list of disco feature as returned by PubSub,
            will be filled with PEP features
        @param profile: profile we are handling"""
        disco_info.extend(map(disco.DiscoFeature, self.pep_events))
        return True

    def addPEPEvent(self, event_type, name, callback):
        """Add a Personal Eventing Protocol event manager
        @param event_type: type of the event (always uppercase), can be MOOD, TUNE, etc
        @param name: namespace of the node (e.g. http://jabber.org/protocol/mood for User Mood)
        @param callback: method to call when this event occur"""
        self.pep_events.add(name)
        self.pep_events.add(name+"+notify")
        self.host.plugins["XEP_0060"].addManagedNode(name, callback)

    def userMoodCB(self, itemsEvent, profile):
        try:
            mood_elem = filter(lambda x:x.name == "mood", itemsEvent.items[0].children)[0]
        except KeyError:
            error(_("Can't find mood element in mood event"))
            return
        _mood = Mood.fromXml(mood_elem)
        if not _mood:
            error(_("Error while parsing mood element"))
            return
        self.host.bridge.personalEvent(itemsEvent.sender.full(), "MOOD", {"mood":_mood.value or "", "text":_mood.text or ""}, profile)