plugin xep-0163: added mood publishing

This commit is contained in:
Goffi 2011-02-04 01:06:57 +01:00
commit 0357b406ac
2 changed files with 64 additions and 3 deletions

View file

@ -59,6 +59,20 @@ class XEP_0060():
@param profile: profile which manage this handler"""
self.managedNodes.append((node_name, callback))
def publish(self, service, nodeIdentifier, items=None, profile_key='@DEFAULT@'):
profile = self.host.memory.getProfileName(profile_key)
if not profile:
err_mess = _('Trying to publish items with an unknown profile key [%s]') % profile_key
error(err_mess)
raise Exception(err_mess)
try:
client = self.clients[profile]
except KeyError:
err_mess = _('INTERNAL ERROR: no handler for required profile')
error(err_mess)
raise Exception(err_mess)
client.publish(service, nodeIdentifier, items, client.parent.jid)
class SatPubSubClient(pubsub.PubSubClient):
implements(disco.IDisco)

View file

@ -24,12 +24,15 @@ 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 twisted.words.xish import domish
from wokkel import disco
from wokkel import disco,pubsub
from wokkel.formats import Mood
NS_USER_MOOD = 'http://jabber.org/protocol/mood'
MANAGED_EVENTS=['MOOD']
PLUGIN_INFO = {
"name": "Personal Eventing Protocol Plugin",
"import_name": "XEP_0163",
@ -49,6 +52,7 @@ class XEP_0163():
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
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)
def disoInfoTrigger(self, disco_info, profile):
@ -68,6 +72,22 @@ class XEP_0163():
self.pep_events.add(name+"+notify")
self.host.plugins["XEP_0060"].addManagedNode(name, callback)
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 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"""
profile = self.host.memory.getProfileName(profile_key)
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:
error (_('Trying to send personal event for an unknown type'))
return 2
if event_type == "MOOD":
return self.sendMood(data, profile)
def userMoodCB(self, itemsEvent, profile):
try:
mood_elem = filter(lambda x:x.name == "mood", itemsEvent.items[0].children)[0]
@ -76,7 +96,34 @@ class XEP_0163():
return
_mood = Mood.fromXml(mood_elem)
if not _mood:
error(_("Error while parsing mood element"))
debug(_("No mood found"))
return
self.host.bridge.personalEvent(itemsEvent.sender.full(), "MOOD", {"mood":_mood.value or "", "text":_mood.text or ""}, profile)
def sendMood(self, data, profile):
"""Send XEP-0107's User Mood
@param data: must include mood and text
@param profile: profile which send the mood"""
try:
value = data['mood'].lower()
text = data['text'] if data.has_key('text') else ''
except KeyError:
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)
return 0
class UserMood(Mood, domish.Element):
"""Improved wokkel Mood which is also a domish.Element"""
def __init__(self, value, text=None):
Mood.__init__(self, value, text)
domish.Element.__init__(self, (NS_USER_MOOD, 'mood'))
self.addElement(value)
if text:
self.addElement('text', content=text)