comparison 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
comparison
equal deleted inserted replaced
282:6a0c6d8e119d 283:68cd30d982a5
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 SAT plugin for Personal Eventing Protocol (xep-0163)
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22 from logging import debug, info, error
23 from twisted.internet import protocol
24 from twisted.words.protocols.jabber import client, jid
25 from twisted.words.protocols.jabber import error as jab_error
26 import twisted.internet.error
27
28 from wokkel import disco
29 from wokkel.formats import Mood
30
31 NS_USER_MOOD = 'http://jabber.org/protocol/mood'
32
33 PLUGIN_INFO = {
34 "name": "Personal Eventing Protocol Plugin",
35 "import_name": "XEP_0163",
36 "type": "XEP",
37 "protocols": ["XEP-0163", "XEP-0107"],
38 "dependencies": ["XEP-0060"],
39 "main": "XEP_0163",
40 "handler": "no",
41 "description": _("""Implementation of Personal Eventing Protocol""")
42 }
43
44 class XEP_0163():
45
46 def __init__(self, host):
47 info(_("PEP plugin initialization"))
48 self.host = host
49 self.pep_events=set()
50 host.trigger.add("PubSub Disco Info", self.disoInfoTrigger)
51 host.bridge.addSignal("personalEvent", ".communication", signature='ssa{ss}s') #args: from (jid), type(MOOD, TUNE, etc), data, profile
52 self.addPEPEvent("MOOD", NS_USER_MOOD, self.userMoodCB)
53
54 def disoInfoTrigger(self, disco_info, profile):
55 """Add info from managed PEP
56 @param disco_info: list of disco feature as returned by PubSub,
57 will be filled with PEP features
58 @param profile: profile we are handling"""
59 disco_info.extend(map(disco.DiscoFeature, self.pep_events))
60 return True
61
62 def addPEPEvent(self, event_type, name, callback):
63 """Add a Personal Eventing Protocol event manager
64 @param event_type: type of the event (always uppercase), can be MOOD, TUNE, etc
65 @param name: namespace of the node (e.g. http://jabber.org/protocol/mood for User Mood)
66 @param callback: method to call when this event occur"""
67 self.pep_events.add(name)
68 self.pep_events.add(name+"+notify")
69 self.host.plugins["XEP_0060"].addManagedNode(name, callback)
70
71 def userMoodCB(self, itemsEvent, profile):
72 try:
73 mood_elem = filter(lambda x:x.name == "mood", itemsEvent.items[0].children)[0]
74 except KeyError:
75 error(_("Can't find mood element in mood event"))
76 return
77 _mood = Mood.fromXml(mood_elem)
78 if not _mood:
79 error(_("Error while parsing mood element"))
80 return
81 self.host.bridge.personalEvent(itemsEvent.sender.full(), "MOOD", {"mood":_mood.value or "", "text":_mood.text or ""}, profile)
82