diff 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
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0163.py	Sun Feb 06 23:40:29 2011 +0100
+++ b/src/plugins/plugin_xep_0163.py	Sun Feb 06 23:46:48 2011 +0100
@@ -31,11 +31,9 @@
 
 NS_USER_MOOD = 'http://jabber.org/protocol/mood'
 
-MANAGED_EVENTS=['MOOD']
-
 PLUGIN_INFO = {
 "name": "Personal Eventing Protocol Plugin",
-"import_name": "XEP_0163",
+"import_name": "XEP-0163",
 "type": "XEP",
 "protocols": ["XEP-0163", "XEP-0107"],
 "dependencies": ["XEP-0060"],
@@ -50,10 +48,11 @@
         info(_("PEP plugin initialization"))
         self.host = host
         self.pep_events=set()
+        self.pep_out_cb={}
         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
         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
-        self.addPEPEvent("MOOD", NS_USER_MOOD, self.userMoodCB)
+        self.addPEPEvent("MOOD", NS_USER_MOOD, self.userMoodCB, self.sendMood)
 
     def disoInfoTrigger(self, disco_info, profile):
         """Add info from managed PEP
@@ -63,18 +62,31 @@
         disco_info.extend(map(disco.DiscoFeature, self.pep_events))
         return True
 
-    def addPEPEvent(self, event_type, name, callback):
+    def addPEPEvent(self, event_type, name, in_callback, out_callback, notify = True):
         """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"""
+        @param in_callback: method to call when this event occur
+        @param out_callback: method to call when we want to publish this event
+        @param notify: add autosubscribe (+notify) if True"""
+        self.pep_out_cb[event_type]=out_callback
         self.pep_events.add(name)
-        self.pep_events.add(name+"+notify")
-        self.host.plugins["XEP_0060"].addManagedNode(name, callback)
+        if notify:
+            self.pep_events.add(name+"+notify")
+        self.host.plugins["XEP-0060"].addManagedNode(name, in_callback)
+
+    def sendPEPEvent(self, namespace, data, profile):
+        """Publish the event data
+        @param namespace: node namespace
+        @param data: domish.Element to use as payload
+        @param profile: profile which send the data"""
+
+        item = pubsub.Item(payload=data)
+        self.host.plugins["XEP-0060"].publish(None, namespace, [item], profile_key = profile)
 
     def sendPersonalEvent(self, event_type, data, profile_key='@DEFAULT@'):
         """Send personal event after checking the data is alright
-        @param event_type: type of event (eg: MOOD, TUNE), must be in MANAGED_EVENTS
+        @param event_type: type of event (eg: MOOD, TUNE), must be in self.pep_out_cb.keys()
         @param data: dict of {string:string} of event_type dependant data
         @param profile_key: profile who send the event
         @return: 0 if success, error code else"""
@@ -82,11 +94,10 @@
         if not profile:
             error(_('Trying to send personal event with an unknown profile key [%s]') % profile_key)
             return 1
-        if not event_type in MANAGED_EVENTS:
+        if not event_type in self.pep_out_cb.keys():
             error (_('Trying to send personal event for an unknown type'))
             return 2
-        if event_type == "MOOD":
-            return self.sendMood(data, profile)
+        return self.pep_out_cb[event_type](data, profile)
 
     def userMoodCB(self, itemsEvent, profile):
         try:
@@ -111,10 +122,7 @@
             error(_("Mood data must contain at least 'mood' key"))
             return 3
         _mood = UserMood(value, text)
-        item = pubsub.Item(payload=_mood)
-        jid, xmlstream = self.host.getJidNStream(profile)
-        assert(jid)
-        self.host.plugins["XEP_0060"].publish(None, NS_USER_MOOD, [item], profile_key = profile)
+        self.sendPEPEvent(NS_USER_MOOD, _mood, profile)
         return 0
 
 class UserMood(Mood, domish.Element):