def xmpp_pubsub_set(self, conn, event):
     """Callback to handle XMPP PubSub commands."""
     self.logger.debug('Pubsub command: %s', event)
     self.logger.debug('Decoded event Node: %s', ustr(event))
     tag = event.getTag('pubsub')
     if tag and tag.getNamespace() == xmpp.protocol.NS_PUBSUB:
         publish = tag.getTag('publish')
         node = publish.getAttr('node')
         jid = publish.getAttr('jid')
         # TODO Check the sending JID can post to the JID/node
         entry = publish.getTag('item').getTag('entry')
         entry_id = str(uuid.uuid4())
         author = entry.getTag('author')
         author.setTagData('uri', 'acct:%s' % author.getTagData('name'))
         entry.setTagData('id', entry_id)
         entry.setTagData('published', entry.getTagData('updated'))
         entry.setTag('link', attrs={'rel': 'self', 'href':
             'xmpp:%s?pubsub;action=retrieve;node=%s;item=%s' % (self.jid,
                 node, entry_id)})
         self.storage.add_item(node, entry_id, ustr(entry))
         reply = event.buildReply('result')
         pubsub = reply.setTag('pubsub', namespace=xmpp.protocol.NS_PUBSUB)
         publish = pubsub.setTag('publish', attrs={'node': node})
         publish.setTag('item', attrs={'id': entry_id})
         conn.send(reply)
         for subscription in self.storage.get_node(node).subscriptions:
             message = xmpp.protocol.Message(
                 typ='headline', frm=self.jid, to=subscription.user)
             event = message.setTag('event', namespace=NS_PUBSUB_EVENT)
             items = event.setTag('items', attrs={'node': node})
             item = items.setTag('item', attrs={'id': entry_id})
             item.addChild(node=entry)
             conn.send(message)
         raise xmpp.protocol.NodeProcessed
예제 #2
0
def c14n(node):
	s = "<" + node.name
	if node.namespace:
		if not node.parent or node.parent.namespace != node.namespace:
			s = s + ' xmlns="%s"' % node.namespace

	sorted_attrs = node.attrs.keys()
	sorted_attrs.sort()
	for key in sorted_attrs:
		val = ustr(node.attrs[key])
		# like XMLescape() but with whitespace and without &gt;
		s = s + ' %s="%s"' % ( key, normalise_attr(val) )
	s = s + ">"
	cnt = 0
	if node.kids:
		for a in node.kids:
			if (len(node.data)-1) >= cnt:
				s = s + normalise_text(node.data[cnt])
			s = s + c14n(a)
			cnt=cnt+1
	if (len(node.data)-1) >= cnt: s = s + normalise_text(node.data[cnt])
	if not node.kids and s[-1:]=='>':
		s=s[:-1]+' />'
	else:
		s = s + "</" + node.name + ">"
	return s.encode('utf-8')
예제 #3
0
 def xmpp_pubsub_get(self, conn, event):
     """Callback to handle XMPP PubSub queries."""
     self.logger.debug('Pubsub request: %s', ustr(event))
     tag = event.getTag('pubsub')
     if tag and (tag.getNamespace() == xmpp.protocol.NS_PUBSUB
                 or tag.getNamespace() == NS_PUBSUB_OWNER):
         child = tag.getChildren()[0]
         op = child.getName()
         node = child.getAttr('node')
         #rsm = tag.getTag('set', namespace=NS_RSM)
         #set_size = int(rsm.getTagData('max'))
         channel = self.storage.get_node(node)
         self.logger.debug('Got channel entries for node %s: %s', node,
                           channel)
         if channel is None:
             conn.send(xmpp.protocol.Error(event, xmpp.ERR_ITEM_NOT_FOUND))
             raise xmpp.protocol.NodeProcessed
         reply = event.buildReply('result')
         if op == u'items':
             pubsub = reply.setTag('pubsub',
                                   namespace=xmpp.protocol.NS_PUBSUB)
             items = pubsub.setTag('items', attrs={'node': node})
             for channel_item in sorted(channel.items,
                                        key=lambda x: x.updated,
                                        reverse=True):
                 item = items.setTag('item', attrs={'id': channel_item.id})
                 item.addChild(node=XML2Node(channel_item.xml))
         elif op == u'subscriptions':
             pubsub = reply.setTag('pubsub', namespace=NS_PUBSUB_OWNER)
             subscriptions = pubsub.setTag(u'subscriptions',
                                           attrs={u'node': node})
             for channel_item in channel.subscriptions:
                 subscriptions.setTag(u'subscription',
                                      attrs={
                                          u'jid':
                                          channel_item.user,
                                          u'subscription':
                                          channel_item.subscription
                                      })
         elif op == u'affiliations':
             pubsub = reply.setTag('pubsub', namespace=NS_PUBSUB_OWNER)
             affiliations = pubsub.setTag(u'affiliations')
             for channel_item in channel.affiliations:
                 affiliations.setTag(u'affiliation',
                                     attrs={
                                         u'jid':
                                         channel_item.user,
                                         u'affiliation':
                                         channel_item.affiliation
                                     })
         #rsm = pubsub.setTag('set', namespace=NS_RSM)
         #if len(resultset) > 0:
         #    rsm.setTagData('first', resultset[0].id if op == u'items' else
         #        resultset[0].user, attrs={'index': 0})
         #    rsm.setTagData('last', resultset[-1].id if op == u'items' else
         #        resultset[-1].jid)
         #rsm.setTagData('count', len(resultset))
         conn.send(reply)
         raise xmpp.protocol.NodeProcessed
 def xmpp_pubsub_get(self, conn, event):
     """Callback to handle XMPP PubSub queries."""
     self.logger.debug('Pubsub request: %s', ustr(event))
     tag = event.getTag('pubsub')
     if tag and (tag.getNamespace() == xmpp.protocol.NS_PUBSUB or
             tag.getNamespace() == NS_PUBSUB_OWNER):
         child = tag.getChildren()[0]
         op = child.getName()
         node = child.getAttr('node')
         #rsm = tag.getTag('set', namespace=NS_RSM)
         #set_size = int(rsm.getTagData('max'))
         channel = self.storage.get_node(node)
         self.logger.debug(
             'Got channel entries for node %s: %s', node, channel)
         if channel is None:
             conn.send(xmpp.protocol.Error(event, xmpp.ERR_ITEM_NOT_FOUND)) 
             raise xmpp.protocol.NodeProcessed
         reply = event.buildReply('result')
         if op == u'items':
             pubsub = reply.setTag('pubsub',
                     namespace=xmpp.protocol.NS_PUBSUB)
             items = pubsub.setTag('items', attrs={'node': node})
             for channel_item in sorted(
                     channel.items, key=lambda x: x.updated, reverse=True):
                 item = items.setTag('item', attrs={'id': channel_item.id})
                 item.addChild(node=XML2Node(channel_item.xml))
         elif op == u'subscriptions':
             pubsub = reply.setTag('pubsub',
                     namespace=NS_PUBSUB_OWNER)
             subscriptions = pubsub.setTag(
                 u'subscriptions', attrs={u'node': node})
             for channel_item in channel.subscriptions:
                 subscriptions.setTag(u'subscription', attrs={
                     u'jid': channel_item.user,
                     u'subscription': channel_item.subscription})
         elif op == u'affiliations':
             pubsub = reply.setTag('pubsub',
                     namespace=NS_PUBSUB_OWNER)
             affiliations = pubsub.setTag(u'affiliations')
             for channel_item in channel.affiliations:
                 affiliations.setTag(u'affiliation', attrs={
                     u'jid': channel_item.user,
                     u'affiliation': channel_item.affiliation
                 })
         #rsm = pubsub.setTag('set', namespace=NS_RSM)
         #if len(resultset) > 0:
         #    rsm.setTagData('first', resultset[0].id if op == u'items' else
         #        resultset[0].user, attrs={'index': 0})
         #    rsm.setTagData('last', resultset[-1].id if op == u'items' else
         #        resultset[-1].jid)
         #rsm.setTagData('count', len(resultset))
         conn.send(reply)
         raise xmpp.protocol.NodeProcessed
예제 #5
0
 def xmpp_pubsub_set(self, conn, event):
     """Callback to handle XMPP PubSub commands."""
     self.logger.debug('Pubsub command: %s', event)
     self.logger.debug('Decoded event Node: %s', ustr(event))
     tag = event.getTag('pubsub')
     if tag and tag.getNamespace() == xmpp.protocol.NS_PUBSUB:
         publish = tag.getTag('publish')
         node = publish.getAttr('node')
         jid = publish.getAttr('jid')
         # TODO Check the sending JID can post to the JID/node
         entry = publish.getTag('item').getTag('entry')
         entry_id = str(uuid.uuid4())
         author = entry.getTag('author')
         author.setTagData('uri', 'acct:%s' % author.getTagData('name'))
         entry.setTagData('id', entry_id)
         entry.setTagData('published', entry.getTagData('updated'))
         entry.setTag('link',
                      attrs={
                          'rel':
                          'self',
                          'href':
                          'xmpp:%s?pubsub;action=retrieve;node=%s;item=%s' %
                          (self.jid, node, entry_id)
                      })
         self.storage.add_item(node, entry_id, ustr(entry))
         reply = event.buildReply('result')
         pubsub = reply.setTag('pubsub', namespace=xmpp.protocol.NS_PUBSUB)
         publish = pubsub.setTag('publish', attrs={'node': node})
         publish.setTag('item', attrs={'id': entry_id})
         conn.send(reply)
         for subscription in self.storage.get_node(node).subscriptions:
             message = xmpp.protocol.Message(typ='headline',
                                             frm=self.jid,
                                             to=subscription.user)
             event = message.setTag('event', namespace=NS_PUBSUB_EVENT)
             items = event.setTag('items', attrs={'node': node})
             item = items.setTag('item', attrs={'id': entry_id})
             item.addChild(node=entry)
             conn.send(message)
         raise xmpp.protocol.NodeProcessed