comparison src/plugins/plugin_xep_0163.py @ 704:3c304929af74

plugin XEP-0277, group blogs: proper asynchronous methods for sending blogs.
author Goffi <goffi@goffi.org>
date Thu, 14 Nov 2013 17:51:35 +0100
parents 69a8bfd266a5
children bfabeedbf32e
comparison
equal deleted inserted replaced
703:0c2c1dfb79e4 704:3c304929af74
24 import twisted.internet.error 24 import twisted.internet.error
25 from twisted.words.xish import domish 25 from twisted.words.xish import domish
26 26
27 from wokkel import disco, pubsub 27 from wokkel import disco, pubsub
28 from wokkel.formats import Mood 28 from wokkel.formats import Mood
29 from sat.core import exceptions
29 30
30 NS_USER_MOOD = 'http://jabber.org/protocol/mood' 31 NS_USER_MOOD = 'http://jabber.org/protocol/mood'
31 32
32 PLUGIN_INFO = { 33 PLUGIN_INFO = {
33 "name": "Personal Eventing Protocol Plugin", 34 "name": "Personal Eventing Protocol Plugin",
48 self.host = host 49 self.host = host
49 self.pep_events = set() 50 self.pep_events = set()
50 self.pep_out_cb = {} 51 self.pep_out_cb = {}
51 host.trigger.add("PubSub Disco Info", self.disoInfoTrigger) 52 host.trigger.add("PubSub Disco Info", self.disoInfoTrigger)
52 host.bridge.addSignal("personalEvent", ".plugin", signature='ssa{ss}s') # args: from (jid), type(MOOD, TUNE, etc), data, profile 53 host.bridge.addSignal("personalEvent", ".plugin", signature='ssa{ss}s') # args: from (jid), type(MOOD, TUNE, etc), data, profile
53 host.bridge.addMethod("sendPersonalEvent", ".plugin", 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", ".plugin", in_sign='sa{ss}s', out_sign='', method=self.sendPersonalEvent, async=True) # args: type(MOOD, TUNE, etc), data, profile_key;
54 self.addPEPEvent("MOOD", NS_USER_MOOD, self.userMoodCB, self.sendMood) 55 self.addPEPEvent("MOOD", NS_USER_MOOD, self.userMoodCB, self.sendMood)
55 56
56 def disoInfoTrigger(self, disco_info, profile): 57 def disoInfoTrigger(self, disco_info, profile):
57 """Add info from managed PEP 58 """Add info from managed PEP
58 @param disco_info: list of disco feature as returned by PubSub, 59 @param disco_info: list of disco feature as returned by PubSub,
64 def addPEPEvent(self, event_type, name, in_callback, out_callback=None, notify=True): 65 def addPEPEvent(self, event_type, name, in_callback, out_callback=None, notify=True):
65 """Add a Personal Eventing Protocol event manager 66 """Add a Personal Eventing Protocol event manager
66 @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
67 @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)
68 @param in_callback: method to call when this event occur 69 @param in_callback: method to call when this event occur
69 @param out_callback: method to call when we want to publish this event 70 @param out_callback: method to call when we want to publish this event (must return a deferred)
70 @param notify: add autosubscribe (+notify) if True""" 71 @param notify: add autosubscribe (+notify) if True"""
71 if out_callback: 72 if out_callback:
72 self.pep_out_cb[event_type] = out_callback 73 self.pep_out_cb[event_type] = out_callback
73 self.pep_events.add(name) 74 self.pep_events.add(name)
74 if notify: 75 if notify:
80 @param namespace: node namespace 81 @param namespace: node namespace
81 @param data: domish.Element to use as payload 82 @param data: domish.Element to use as payload
82 @param profile: profile which send the data""" 83 @param profile: profile which send the data"""
83 84
84 item = pubsub.Item(payload=data) 85 item = pubsub.Item(payload=data)
85 self.host.plugins["XEP-0060"].publish(None, namespace, [item], profile_key=profile) 86 return self.host.plugins["XEP-0060"].publish(None, namespace, [item], profile_key=profile)
86 87
87 def sendPersonalEvent(self, event_type, data, profile_key='@DEFAULT@'): 88 def sendPersonalEvent(self, event_type, data, profile_key='@DEFAULT@'):
88 """Send personal event after checking the data is alright 89 """Send personal event after checking the data is alright
89 @param event_type: type of event (eg: MOOD, TUNE), must be in self.pep_out_cb.keys() 90 @param event_type: type of event (eg: MOOD, TUNE), must be in self.pep_out_cb.keys()
90 @param data: dict of {string:string} of event_type dependant data 91 @param data: dict of {string:string} of event_type dependant data
91 @param profile_key: profile who send the event 92 @param profile_key: profile who send the event
92 @return: 0 if success, error code else""" 93 """
93 profile = self.host.memory.getProfileName(profile_key) 94 profile = self.host.memory.getProfileName(profile_key)
94 if not profile: 95 if not profile:
95 error(_('Trying to send personal event with an unknown profile key [%s]') % profile_key) 96 error(_('Trying to send personal event with an unknown profile key [%s]') % profile_key)
96 return 1 97 raise exceptions.ProfileUnknownError
97 if not event_type in self.pep_out_cb.keys(): 98 if not event_type in self.pep_out_cb.keys():
98 error(_('Trying to send personal event for an unknown type')) 99 error(_('Trying to send personal event for an unknown type'))
99 return 2 100 raise DataError('Type unknown')
100 return self.pep_out_cb[event_type](data, profile) 101 return self.pep_out_cb[event_type](data, profile)
101 102
102 def userMoodCB(self, itemsEvent, profile): 103 def userMoodCB(self, itemsEvent, profile):
103 if not itemsEvent.items: 104 if not itemsEvent.items:
104 debug(_("No item found")) 105 debug(_("No item found"))
120 @param profile: profile which send the mood""" 121 @param profile: profile which send the mood"""
121 try: 122 try:
122 value = data['mood'].lower() 123 value = data['mood'].lower()
123 text = data['text'] if 'text' in data else '' 124 text = data['text'] if 'text' in data else ''
124 except KeyError: 125 except KeyError:
125 error(_("Mood data must contain at least 'mood' key")) 126 raise exceptions.DataError("Mood data must contain at least 'mood' key")
126 return 3
127 mood = UserMood(value, text) 127 mood = UserMood(value, text)
128 self.sendPEPEvent(NS_USER_MOOD, mood, profile) 128 return self.sendPEPEvent(NS_USER_MOOD, mood, profile)
129 return 0
130 129
131 130
132 class UserMood(Mood, domish.Element): 131 class UserMood(Mood, domish.Element):
133 """Improved wokkel Mood which is also a domish.Element""" 132 """Improved wokkel Mood which is also a domish.Element"""
134 133