comparison src/plugins/plugin_xep_0163.py @ 290:59a82af700e2

plugin xep 163: added generic sendPEPEvent method
author Goffi <goffi@goffi.org>
date Sun, 06 Feb 2011 23:46:48 +0100
parents 3b382fa0ac28
children 0aa6ca6cdbdd
comparison
equal deleted inserted replaced
289:0e54b1b0a8c8 290:59a82af700e2
29 from wokkel import disco,pubsub 29 from wokkel import disco,pubsub
30 from wokkel.formats import Mood 30 from wokkel.formats import Mood
31 31
32 NS_USER_MOOD = 'http://jabber.org/protocol/mood' 32 NS_USER_MOOD = 'http://jabber.org/protocol/mood'
33 33
34 MANAGED_EVENTS=['MOOD']
35
36 PLUGIN_INFO = { 34 PLUGIN_INFO = {
37 "name": "Personal Eventing Protocol Plugin", 35 "name": "Personal Eventing Protocol Plugin",
38 "import_name": "XEP_0163", 36 "import_name": "XEP-0163",
39 "type": "XEP", 37 "type": "XEP",
40 "protocols": ["XEP-0163", "XEP-0107"], 38 "protocols": ["XEP-0163", "XEP-0107"],
41 "dependencies": ["XEP-0060"], 39 "dependencies": ["XEP-0060"],
42 "main": "XEP_0163", 40 "main": "XEP_0163",
43 "handler": "no", 41 "handler": "no",
48 46
49 def __init__(self, host): 47 def __init__(self, host):
50 info(_("PEP plugin initialization")) 48 info(_("PEP plugin initialization"))
51 self.host = host 49 self.host = host
52 self.pep_events=set() 50 self.pep_events=set()
51 self.pep_out_cb={}
53 host.trigger.add("PubSub Disco Info", self.disoInfoTrigger) 52 host.trigger.add("PubSub Disco Info", self.disoInfoTrigger)
54 host.bridge.addSignal("personalEvent", ".communication", signature='ssa{ss}s') #args: from (jid), type(MOOD, TUNE, etc), data, profile 53 host.bridge.addSignal("personalEvent", ".communication", signature='ssa{ss}s') #args: from (jid), type(MOOD, TUNE, etc), data, profile
55 host.bridge.addMethod("sendPersonalEvent", ".communication", in_sign='sa{ss}s', out_sign='i', method=self.sendPersonalEvent) #args: type(MOOD, TUNE, etc), data, profile_key; return 0 or error_code 54 host.bridge.addMethod("sendPersonalEvent", ".communication", in_sign='sa{ss}s', out_sign='i', method=self.sendPersonalEvent) #args: type(MOOD, TUNE, etc), data, profile_key; return 0 or error_code
56 self.addPEPEvent("MOOD", NS_USER_MOOD, self.userMoodCB) 55 self.addPEPEvent("MOOD", NS_USER_MOOD, self.userMoodCB, self.sendMood)
57 56
58 def disoInfoTrigger(self, disco_info, profile): 57 def disoInfoTrigger(self, disco_info, profile):
59 """Add info from managed PEP 58 """Add info from managed PEP
60 @param disco_info: list of disco feature as returned by PubSub, 59 @param disco_info: list of disco feature as returned by PubSub,
61 will be filled with PEP features 60 will be filled with PEP features
62 @param profile: profile we are handling""" 61 @param profile: profile we are handling"""
63 disco_info.extend(map(disco.DiscoFeature, self.pep_events)) 62 disco_info.extend(map(disco.DiscoFeature, self.pep_events))
64 return True 63 return True
65 64
66 def addPEPEvent(self, event_type, name, callback): 65 def addPEPEvent(self, event_type, name, in_callback, out_callback, notify = True):
67 """Add a Personal Eventing Protocol event manager 66 """Add a Personal Eventing Protocol event manager
68 @param event_type: type of the event (always uppercase), can be MOOD, TUNE, etc 67 @param event_type: type of the event (always uppercase), can be MOOD, TUNE, etc
69 @param name: namespace of the node (e.g. http://jabber.org/protocol/mood for User Mood) 68 @param name: namespace of the node (e.g. http://jabber.org/protocol/mood for User Mood)
70 @param callback: method to call when this event occur""" 69 @param in_callback: method to call when this event occur
70 @param out_callback: method to call when we want to publish this event
71 @param notify: add autosubscribe (+notify) if True"""
72 self.pep_out_cb[event_type]=out_callback
71 self.pep_events.add(name) 73 self.pep_events.add(name)
72 self.pep_events.add(name+"+notify") 74 if notify:
73 self.host.plugins["XEP_0060"].addManagedNode(name, callback) 75 self.pep_events.add(name+"+notify")
76 self.host.plugins["XEP-0060"].addManagedNode(name, in_callback)
77
78 def sendPEPEvent(self, namespace, data, profile):
79 """Publish the event data
80 @param namespace: node namespace
81 @param data: domish.Element to use as payload
82 @param profile: profile which send the data"""
83
84 item = pubsub.Item(payload=data)
85 self.host.plugins["XEP-0060"].publish(None, namespace, [item], profile_key = profile)
74 86
75 def sendPersonalEvent(self, event_type, data, profile_key='@DEFAULT@'): 87 def sendPersonalEvent(self, event_type, data, profile_key='@DEFAULT@'):
76 """Send personal event after checking the data is alright 88 """Send personal event after checking the data is alright
77 @param event_type: type of event (eg: MOOD, TUNE), must be in MANAGED_EVENTS 89 @param event_type: type of event (eg: MOOD, TUNE), must be in self.pep_out_cb.keys()
78 @param data: dict of {string:string} of event_type dependant data 90 @param data: dict of {string:string} of event_type dependant data
79 @param profile_key: profile who send the event 91 @param profile_key: profile who send the event
80 @return: 0 if success, error code else""" 92 @return: 0 if success, error code else"""
81 profile = self.host.memory.getProfileName(profile_key) 93 profile = self.host.memory.getProfileName(profile_key)
82 if not profile: 94 if not profile:
83 error(_('Trying to send personal event with an unknown profile key [%s]') % profile_key) 95 error(_('Trying to send personal event with an unknown profile key [%s]') % profile_key)
84 return 1 96 return 1
85 if not event_type in MANAGED_EVENTS: 97 if not event_type in self.pep_out_cb.keys():
86 error (_('Trying to send personal event for an unknown type')) 98 error (_('Trying to send personal event for an unknown type'))
87 return 2 99 return 2
88 if event_type == "MOOD": 100 return self.pep_out_cb[event_type](data, profile)
89 return self.sendMood(data, profile)
90 101
91 def userMoodCB(self, itemsEvent, profile): 102 def userMoodCB(self, itemsEvent, profile):
92 try: 103 try:
93 mood_elem = filter(lambda x:x.name == "mood", itemsEvent.items[0].children)[0] 104 mood_elem = filter(lambda x:x.name == "mood", itemsEvent.items[0].children)[0]
94 except KeyError: 105 except KeyError:
109 text = data['text'] if data.has_key('text') else '' 120 text = data['text'] if data.has_key('text') else ''
110 except KeyError: 121 except KeyError:
111 error(_("Mood data must contain at least 'mood' key")) 122 error(_("Mood data must contain at least 'mood' key"))
112 return 3 123 return 3
113 _mood = UserMood(value, text) 124 _mood = UserMood(value, text)
114 item = pubsub.Item(payload=_mood) 125 self.sendPEPEvent(NS_USER_MOOD, _mood, profile)
115 jid, xmlstream = self.host.getJidNStream(profile)
116 assert(jid)
117 self.host.plugins["XEP_0060"].publish(None, NS_USER_MOOD, [item], profile_key = profile)
118 return 0 126 return 0
119 127
120 class UserMood(Mood, domish.Element): 128 class UserMood(Mood, domish.Element):
121 """Improved wokkel Mood which is also a domish.Element""" 129 """Improved wokkel Mood which is also a domish.Element"""
122 130