updated pubsub plugin to use stanzas

This commit is contained in:
Nathan Fritz 2011-08-12 16:32:09 -07:00
parent 168203c94d
commit 0050c51124
3 changed files with 110 additions and 292 deletions

View file

@ -19,295 +19,110 @@ class xep_0060(base.base_plugin):
self.xep = '0060' self.xep = '0060'
self.description = 'Publish-Subscribe' self.description = 'Publish-Subscribe'
def create_node(self, jid, node, config=None, collection=False, ntype=None): def create_node(self, jid, node, config=None, ntype=None):
pubsub = ET.Element('{http://jabber.org/protocol/pubsub}pubsub') iq = IQ(sto=jid, stype='set', sfrom=self.xmpp.jid)
create = ET.Element('create') iq['pubsub']['create']['node'] = node
create.set('node', node) if ntype is None:
pubsub.append(create) ntype = 'leaf'
configure = ET.Element('configure') if config is not None:
if collection: if 'FORM_TYPE' in submitform.field:
ntype = 'collection' config.field['FORM_TYPE'].setValue('http://jabber.org/protocol/pubsub#node_config')
#if config is None:
# submitform = self.xmpp.plugin['xep_0004'].makeForm('submit')
#else:
if config is not None:
submitform = config
if 'FORM_TYPE' in submitform.field:
submitform.field['FORM_TYPE'].setValue('http://jabber.org/protocol/pubsub#node_config')
else: else:
submitform.addField('FORM_TYPE', 'hidden', value='http://jabber.org/protocol/pubsub#node_config') config.addField('FORM_TYPE', 'hidden', value='http://jabber.org/protocol/pubsub#node_config')
if ntype: if 'pubsub#node_type' in submitform.field:
if 'pubsub#node_type' in submitform.field: config.field['pubsub#node_type'].setValue(ntype)
submitform.field['pubsub#node_type'].setValue(ntype) else:
else: config.addField('pubsub#node_type', value=ntype)
submitform.addField('pubsub#node_type', value=ntype) iq['pubsub']['configure']['form'] = config
else: return iq.send()
if 'pubsub#node_type' in submitform.field:
submitform.field['pubsub#node_type'].setValue('leaf')
else:
submitform.addField('pubsub#node_type', value='leaf')
submitform['type'] = 'submit'
configure.append(submitform.xml)
pubsub.append(configure)
iq = self.xmpp.makeIqSet(pubsub)
iq.attrib['to'] = jid
iq.attrib['from'] = self.xmpp.boundjid.full
id = iq['id']
result = iq.send()
if result is False or result is None or result['type'] == 'error': return False
return True
def subscribe(self, jid, node, bare=True, subscribee=None): def subscribe(self, jid, node, bare=True, subscribee=None):
pubsub = ET.Element('{http://jabber.org/protocol/pubsub}pubsub') iq = IQ(sto=jid, sfrom=self.xmpp.jid, stype='set')
subscribe = ET.Element('subscribe') iq['pubsub']['subscribe']['node'] = node
subscribe.attrib['node'] = node if subscribee is None:
if subscribee is None: if bare:
if bare: iq['pubsub']['subscribe']['jid'] = self.xmpp.jid.bare
subscribe.attrib['jid'] = self.xmpp.boundjid.bare else:
else: iq['pubsub']['subscribe']['jid'] = self.xmpp.jid.full
subscribe.attrib['jid'] = self.xmpp.boundjid.full else:
else: iq['pubsub']['subscribe']['jid'] = subscribee
subscribe.attrib['jid'] = subscribee return iq.send()
pubsub.append(subscribe)
iq = self.xmpp.makeIqSet(pubsub)
iq.attrib['to'] = jid
iq.attrib['from'] = self.xmpp.boundjid.full
id = iq['id']
result = iq.send()
if result is False or result is None or result['type'] == 'error': return False
return True
def unsubscribe(self, jid, node, bare=True, subscribee=None): def unsubscribe(self, jid, node, subid=None, bare=True, subscribee=None):
pubsub = ET.Element('{http://jabber.org/protocol/pubsub}pubsub') iq = IQ(sto=jid, sfrom=self.xmpp.jid, stype='set')
unsubscribe = ET.Element('unsubscribe') iq['pubsub']['unsubscribe']['node'] = node
unsubscribe.attrib['node'] = node if subscribee is None:
if subscribee is None: if bare:
if bare: iq['pubsub']['unsubscribe']['jid'] = self.xmpp.jid.bare
unsubscribe.attrib['jid'] = self.xmpp.boundjid.bare else:
else: iq['pubsub']['unsubscribe']['jid'] = self.xmpp.jid.full
unsubscribe.attrib['jid'] = self.xmpp.boundjid.full else:
else: iq['pubsub']['unsubscribe']['jid'] = subscribee
unsubscribe.attrib['jid'] = subscribee if subid is not None:
pubsub.append(unsubscribe) iq['pubsub']['unsubscribe']['subid'] = subid
iq = self.xmpp.makeIqSet(pubsub) return iq.send()
iq.attrib['to'] = jid
iq.attrib['from'] = self.xmpp.boundjid.full
id = iq['id']
result = iq.send()
if result is False or result is None or result['type'] == 'error': return False
return True
def getNodeConfig(self, jid, node=None): # if no node, then grab default def get_node_config(self, jid, node=None): # if no node, then grab default
pubsub = ET.Element('{http://jabber.org/protocol/pubsub#owner}pubsub') iq = IQ(sto=jid, sfrom=self.xmpp.jid, stype='get')
if node is not None: if node is None:
configure = ET.Element('configure') iq['pubsub_owner']['default']
configure.attrib['node'] = node else:
else: iq['pubsub_owner']['configure']['node'] = node
configure = ET.Element('default') return iq.send()
pubsub.append(configure)
#TODO: Add configure support.
iq = self.xmpp.makeIqGet()
iq.append(pubsub)
iq.attrib['to'] = jid
iq.attrib['from'] = self.xmpp.boundjid.full
id = iq['id']
#self.xmpp.add_handler("<iq id='%s'/>" % id, self.handlerCreateNodeResponse)
result = iq.send()
if result is None or result == False or result['type'] == 'error':
log.warning("got error instead of config")
return False
if node is not None:
form = result.find('{http://jabber.org/protocol/pubsub#owner}pubsub/{http://jabber.org/protocol/pubsub#owner}configure/{jabber:x:data}x')
else:
form = result.find('{http://jabber.org/protocol/pubsub#owner}pubsub/{http://jabber.org/protocol/pubsub#owner}default/{jabber:x:data}x')
if not form or form is None:
log.error("No form found.")
return False
return Form(xml=form)
def getNodeSubscriptions(self, jid, node): def get_node_subscriptions(self, jid, node):
pubsub = ET.Element('{http://jabber.org/protocol/pubsub#owner}pubsub') iq = IQ(sto=jid, sfrom=self.xmpp.jid, stype='get')
subscriptions = ET.Element('subscriptions') iq['pubsub_owner']['subscriptions']['node'] = node
subscriptions.attrib['node'] = node return iq.send()
pubsub.append(subscriptions)
iq = self.xmpp.makeIqGet()
iq.append(pubsub)
iq.attrib['to'] = jid
iq.attrib['from'] = self.xmpp.boundjid.full
id = iq['id']
result = iq.send()
if result is None or result == False or result['type'] == 'error':
log.warning("got error instead of config")
return False
else:
results = result.findall('{http://jabber.org/protocol/pubsub#owner}pubsub/{http://jabber.org/protocol/pubsub#owner}subscriptions/{http://jabber.org/protocol/pubsub#owner}subscription')
if results is None:
return False
subs = {}
for sub in results:
subs[sub.get('jid')] = sub.get('subscription')
return subs
def getNodeAffiliations(self, jid, node): def get_node_affiliations(self, jid, node):
pubsub = ET.Element('{http://jabber.org/protocol/pubsub#owner}pubsub') iq = IQ(sto=jid, sfrom=self.xmpp.jid, stype='get')
affiliations = ET.Element('affiliations') iq['pubsub_owner']['affiliations']['node'] = node
affiliations.attrib['node'] = node return iq.send()
pubsub.append(affiliations)
iq = self.xmpp.makeIqGet()
iq.append(pubsub)
iq.attrib['to'] = jid
iq.attrib['from'] = self.xmpp.boundjid.full
id = iq['id']
result = iq.send()
if result is None or result == False or result['type'] == 'error':
log.warning("got error instead of config")
return False
else:
results = result.findall('{http://jabber.org/protocol/pubsub#owner}pubsub/{http://jabber.org/protocol/pubsub#owner}affiliations/{http://jabber.org/protocol/pubsub#owner}affiliation')
if results is None:
return False
subs = {}
for sub in results:
subs[sub.get('jid')] = sub.get('affiliation')
return subs
def deleteNode(self, jid, node): def delete_node(self, jid, node):
pubsub = ET.Element('{http://jabber.org/protocol/pubsub#owner}pubsub') iq = IQ(sto=jid, sfrom=self.xmpp.jid, stype='get')
iq = self.xmpp.makeIqSet() iq['pubsub_owner']['delete']['node'] = node
delete = ET.Element('delete') return iq.send()
delete.attrib['node'] = node
pubsub.append(delete)
iq.append(pubsub)
iq.attrib['to'] = jid
iq.attrib['from'] = self.xmpp.boundjid.full
result = iq.send()
if result is not None and result is not False and result['type'] != 'error':
return True
else:
return False
def set_node_config(self, jid, node, config):
iq = IQ(sto=jid, sfrom=self.xmpp.jid, stype='set')
iq['pubsub_owner']['configure']['node'] = node
iq['pubsub_owner']['configure']['config'] = config
return iq.send()
def setNodeConfig(self, jid, node, config): def publish(self, jid, node, items=[]):
pubsub = ET.Element('{http://jabber.org/protocol/pubsub#owner}pubsub') iq = IQ(sto=jid, sfrom=self.xmpp.jid, stype='set')
configure = ET.Element('configure') iq['pubsub']['publish']['node'] = node
configure.attrib['node'] = node for id, payload in items:
config = config.getXML('submit') item = stanza.pubsub.Item()
configure.append(config) if id is not None:
pubsub.append(configure) item['id'] = id
iq = self.xmpp.makeIqSet(pubsub) item['payload'] = payload
iq.attrib['to'] = jid iq['pubsub']['publish'].append(item)
iq.attrib['from'] = self.xmpp.boundjid.full return iq.send()
id = iq['id']
result = iq.send()
if result is None or result['type'] == 'error':
return False
return True
def setItem(self, jid, node, items=[]): def retract(self, jid, node, item):
pubsub = ET.Element('{http://jabber.org/protocol/pubsub}pubsub') iq = IQ(sto=jid, sfrom=self.xmpp.jid, stype='set')
publish = ET.Element('publish') iq['pubsub']['retract']['node'] = node
publish.attrib['node'] = node item = stanza.pubsub.Item()
for pub_item in items: item['id'] = item
id, payload = pub_item iq['pubsub']['retract'].append(item)
item = ET.Element('item') return iq.send()
if id is not None:
item.attrib['id'] = id
item.append(payload)
publish.append(item)
pubsub.append(publish)
iq = self.xmpp.makeIqSet(pubsub)
iq.attrib['to'] = jid
iq.attrib['from'] = self.xmpp.boundjid.full
id = iq['id']
result = iq.send()
if result is None or result is False or result['type'] == 'error': return False
return True
def addItem(self, jid, node, items=[]): def get_nodes(self, jid):
return self.setItem(jid, node, items) return self.xmpp.plugin['xep_0030'].get_items(jid)
def deleteItem(self, jid, node, item):
pubsub = ET.Element('{http://jabber.org/protocol/pubsub}pubsub')
retract = ET.Element('retract')
retract.attrib['node'] = node
itemn = ET.Element('item')
itemn.attrib['id'] = item
retract.append(itemn)
pubsub.append(retract)
iq = self.xmpp.makeIqSet(pubsub)
iq.attrib['to'] = jid
iq.attrib['from'] = self.xmpp.boundjid.full
id = iq['id']
result = iq.send()
if result is None or result is False or result['type'] == 'error': return False
return True
def getNodes(self, jid):
response = self.xmpp.plugin['xep_0030'].getItems(jid)
items = response.findall('{http://jabber.org/protocol/disco#items}query/{http://jabber.org/protocol/disco#items}item')
nodes = {}
if items is not None and items is not False:
for item in items:
nodes[item.get('node')] = item.get('name')
return nodes
def getItems(self, jid, node): def getItems(self, jid, node):
response = self.xmpp.plugin['xep_0030'].getItems(jid, node) return self.xmpp.plugin['xep_0030'].get_items(jid, node)
items = response.findall('{http://jabber.org/protocol/disco#items}query/{http://jabber.org/protocol/disco#items}item')
nodeitems = []
if items is not None and items is not False:
for item in items:
nodeitems.append(item.get('node'))
return nodeitems
def addNodeToCollection(self, jid, child, parent=''):
config = self.getNodeConfig(jid, child)
if not config or config is None:
self.lasterror = "Config Error"
return False
try:
config.field['pubsub#collection'].setValue(parent)
except KeyError:
log.warning("pubsub#collection doesn't exist in config, trying to add it")
config.addField('pubsub#collection', value=parent)
if not self.setNodeConfig(jid, child, config):
return False
return True
def modifyAffiliation(self, ps_jid, node, user_jid, affiliation):
if affiliation not in ('owner', 'publisher', 'member', 'none', 'outcast'):
raise TypeError
pubsub = ET.Element('{http://jabber.org/protocol/pubsub#owner}pubsub')
affs = ET.Element('affiliations')
affs.attrib['node'] = node
aff = ET.Element('affiliation')
aff.attrib['jid'] = user_jid
aff.attrib['affiliation'] = affiliation
affs.append(aff)
pubsub.append(affs)
iq = self.xmpp.makeIqSet(pubsub)
iq.attrib['to'] = ps_jid
iq.attrib['from'] = self.xmpp.boundjid.full
id = iq['id']
result = iq.send()
if result is None or result is False or result['type'] == 'error':
return False
return True
def addNodeToCollection(self, jid, child, parent=''):
config = self.getNodeConfig(jid, child)
if not config or config is None:
self.lasterror = "Config Error"
return False
try:
config.field['pubsub#collection'].setValue(parent)
except KeyError:
log.warning("pubsub#collection doesn't exist in config, trying to add it")
config.addField('pubsub#collection', value=parent)
if not self.setNodeConfig(jid, child, config):
return False
return True
def removeNodeFromCollection(self, jid, child):
self.addNodeToCollection(jid, child, '')
def modify_affiliation(self, jid, node, affiliation, user_jid=None):
iq = IQ(sto=jid, sfrom=self.xmpp.jid, stype='set')
iq['pubsub_owner']['affiliations']
aff = stanza.pubsub.Affiliation()
aff['node'] = node
if user_jid is not None:
aff['jid'] = user_jid
aff['affiliation'] = affiliation
iq['pubsub_owner']['affiliations'].append(aff)
return iq.send()

View file

@ -23,10 +23,16 @@ class Affiliation(ElementBase):
namespace = 'http://jabber.org/protocol/pubsub' namespace = 'http://jabber.org/protocol/pubsub'
name = 'affiliation' name = 'affiliation'
plugin_attrib = name plugin_attrib = name
interfaces = set(('node', 'affiliation')) interfaces = set(('node', 'affiliation', 'jid'))
plugin_attrib_map = {} plugin_attrib_map = {}
plugin_tag_map = {} plugin_tag_map = {}
def setJid(self, value):
self._setAttr('jid', str(value))
def getJid(self):
return JID(self._getAttr('jid'))
class Affiliations(ElementBase): class Affiliations(ElementBase):
namespace = 'http://jabber.org/protocol/pubsub' namespace = 'http://jabber.org/protocol/pubsub'
name = 'affiliations' name = 'affiliations'
@ -36,12 +42,6 @@ class Affiliations(ElementBase):
plugin_tag_map = {} plugin_tag_map = {}
subitem = (Affiliation,) subitem = (Affiliation,)
def append(self, affiliation):
if not isinstance(affiliation, Affiliation):
raise TypeError
self.xml.append(affiliation.xml)
return self.iterables.append(affiliation)
registerStanzaPlugin(Pubsub, Affiliations) registerStanzaPlugin(Pubsub, Affiliations)
@ -164,7 +164,7 @@ class Unsubscribe(ElementBase):
namespace = 'http://jabber.org/protocol/pubsub' namespace = 'http://jabber.org/protocol/pubsub'
name = 'unsubscribe' name = 'unsubscribe'
plugin_attrib = name plugin_attrib = name
interfaces = set(('node', 'jid')) interfaces = set(('node', 'jid', 'subid'))
plugin_attrib_map = {} plugin_attrib_map = {}
plugin_tag_map = {} plugin_tag_map = {}

View file

@ -65,11 +65,20 @@ class OwnerAffiliation(Affiliation):
plugin_tag_map = {} plugin_tag_map = {}
class OwnerConfigure(Configure): class OwnerConfigure(Configure):
name = 'configure'
plugin_attrib = 'configure'
namespace = 'http://jabber.org/protocol/pubsub#owner' namespace = 'http://jabber.org/protocol/pubsub#owner'
interfaces = set(('node', 'config')) interfaces = set(('node', 'config'))
plugin_attrib_map = {} plugin_attrib_map = {}
plugin_tag_map = {} plugin_tag_map = {}
def getConfig(self):
return self['form']
def setConfig(self, value):
self['form'].setStanzaValues(value.getStanzaValues())
return self
registerStanzaPlugin(PubsubOwner, OwnerConfigure) registerStanzaPlugin(PubsubOwner, OwnerConfigure)
class OwnerDefault(OwnerConfigure): class OwnerDefault(OwnerConfigure):
@ -78,12 +87,6 @@ class OwnerDefault(OwnerConfigure):
plugin_attrib_map = {} plugin_attrib_map = {}
plugin_tag_map = {} plugin_tag_map = {}
def getConfig(self):
return self['form']
def setConfig(self, value):
self['form'].setStanzaValues(value.getStanzaValues())
return self
registerStanzaPlugin(PubsubOwner, OwnerDefault) registerStanzaPlugin(PubsubOwner, OwnerDefault)
registerStanzaPlugin(OwnerDefault, xep_0004.Form) registerStanzaPlugin(OwnerDefault, xep_0004.Form)