# HG changeset patch # User Goffi # Date 1369084889 -7200 # Node ID 6f4c31192c7ce56624872bd9de040ef14658e5a4 # Parent bef0f893482ffd719a9bdcda230cc38e9abdc930 plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet): - PubSub options constants are moved to XEP-0060 - comments url are generated/parsed according to XEP-0277 - microblog data can now have the following keys: - "comments", with the url as given in the tag - "comments_service", with the jid of the PubSub service hosting the comments - "comments_node", with the parsed node - comments nodes use different access_model according to parent microblog item access - publisher is not verified yet, see FIXME warning - so far, comments node are automatically subscribed - some bug fixes diff -r bef0f893482f -r 6f4c31192c7c src/plugins/plugin_misc_groupblog.py --- a/src/plugins/plugin_misc_groupblog.py Sun Apr 07 23:27:07 2013 +0200 +++ b/src/plugins/plugin_misc_groupblog.py Mon May 20 23:21:29 2013 +0200 @@ -25,6 +25,9 @@ from zope.interface import implements +import uuid +import urllib + try: from twisted.words.protocols.xmlstream import XMPPHandler except ImportError: @@ -33,20 +36,18 @@ NS_PUBSUB = 'http://jabber.org/protocol/pubsub' NS_GROUPBLOG = 'http://goffi.org/protocol/groupblog' NS_NODE_PREFIX = 'urn:xmpp:groupblog:' +NS_COMMENT_PREFIX = 'urn:xmpp:comments:' #NS_PUBSUB_EXP = 'http://goffi.org/protocol/pubsub' #for non official features NS_PUBSUB_EXP = NS_PUBSUB # XXX: we can't use custom namespace as Wokkel's PubSubService use official NS NS_PUBSUB_ITEM_ACCESS = NS_PUBSUB_EXP + "#item-access" NS_PUBSUB_CREATOR_JID_CHECK = NS_PUBSUB_EXP + "#creator-jid-check" NS_PUBSUB_ITEM_CONFIG = NS_PUBSUB_EXP + "#item-config" NS_PUBSUB_AUTO_CREATE = NS_PUBSUB + "#auto-create" -OPT_ROSTER_GROUPS_ALLOWED = 'pubsub#roster_groups_allowed' -OPT_ACCESS_MODEL = 'pubsub#access_model' -OPT_PERSIST_ITEMS = 'pubsub#persist_items' -OPT_MAX_ITEMS = 'pubsub#max_items' -OPT_NODE_TYPE = 'pubsub#node_type' -OPT_SUBSCRIPTION_TYPE = 'pubsub#subscription_type' -OPT_SUBSCRIPTION_DEPTH = 'pubsub#subscription_depth' TYPE_COLLECTION = 'collection' +ACCESS_TYPE_MAP = { 'PUBLIC': 'open', + 'GROUP': 'roster', + 'JID': None, #JID is not yet managed + } PLUGIN_INFO = { "name": "Group blogging throught collections", @@ -83,9 +84,13 @@ info(_("Group blog plugin initialization")) self.host = host - host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='sasss', out_sign='', + host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='sassa{ss}s', out_sign='', method=self.sendGroupBlog) + host.bridge.addMethod("sendGroupBlogComment", ".plugin", in_sign='sss', out_sign='', + method=self.sendGroupBlogComment, + async=True) + host.bridge.addMethod("getLastGroupBlogs", ".plugin", in_sign='sis', out_sign='aa{ss}', method=self.getLastGroupBlogs, @@ -139,7 +144,7 @@ client.item_access_pubsub = entity client._item_access_pubsub_pending.callback(None) - if hasattr(client, "_item_access_pubsub_pending"): + if "_item_access_pubsub_pending" in client: #XXX: we need to wait for item access pubsub service check yield client._item_access_pubsub_pending del client._item_access_pubsub_pending @@ -152,7 +157,9 @@ def pubSubItemsReceivedTrigger(self, event, profile): """"Trigger which catch groupblogs events""" + if event.nodeIdentifier.startswith(NS_NODE_PREFIX): + # Microblog publisher = jid.JID(event.nodeIdentifier[len(NS_NODE_PREFIX):]) origin_host = publisher.host.split('.') event_host = event.sender.host.split('.') @@ -169,18 +176,32 @@ self.host.bridge.personalEvent(publisher.full(), "MICROBLOG", microblog_data, profile) return False + elif event.nodeIdentifier.startswith(NS_COMMENT_PREFIX): + # Comment + for item in event.items: + publisher = "" # FIXME: publisher attribute for item in SàT pubsub is not managed yet, so + # publisher is not checked and can be easily spoofed. This need to be fixed + # quickly. + microblog_data = self.item2gbdata(item) + microblog_data["comments_service"] = event.sender.userhost() + microblog_data["comments_node"] = event.nodeIdentifier + microblog_data["verified_publisher"] = "true" if publisher else "false" + + self.host.bridge.personalEvent(publisher.full() if publisher else microblog_data["author"], "MICROBLOG_COMMENT", microblog_data, profile) + return False return True def _parseAccessData(self, microblog_data, item): + P = self.host.plugins["XEP-0060"] form_elts = filter(lambda elt: elt.name == "x", item.children) for form_elt in form_elts: form = data_form.Form.fromElement(form_elt) if (form.formNamespace == NS_PUBSUB_ITEM_CONFIG): - access_model = form.get(OPT_ACCESS_MODEL, 'open') + access_model = form.get(P.OPT_ACCESS_MODEL, 'open') if access_model == "roster": try: - microblog_data["groups"] = '\n'.join(form.fields[OPT_ROSTER_GROUPS_ALLOWED].values) + microblog_data["groups"] = '\n'.join(form.fields[P.OPT_ROSTER_GROUPS_ALLOWED].values) except KeyError: warning("No group found for roster access-model") microblog_data["groups"] = '' @@ -199,24 +220,51 @@ @return: node's name (string)""" return NS_NODE_PREFIX + publisher.userhost() - def _publishMblog(self, service, client, access_type, access_list, message): + def _publishMblog(self, service, client, access_type, access_list, message, options): """Actually publish the message on the group blog @param service: jid of the item-access pubsub service @param client: SatXMPPClient of the published @param access_type: one of "PUBLIC", "GROUP", "JID" @param access_list: set of entities (empty list for all, groups or jids) allowed to see the item @param message: message to publish + @param options: dict which option name as key, which can be: + - allow_comments: True to accept comments, False else (default: False) """ - mblog_item = self.host.plugins["XEP-0277"].data2entry({'content': message}, client.profile) + node_name = self.getNodeName(client.jid) + mblog_data = {'content': message} + P = self.host.plugins["XEP-0060"] + access_model_value = ACCESS_TYPE_MAP[access_type] + + if options.get('allow_comments', 'False').lower() == 'true': + comments_node = "%s_%s__%s" % (NS_COMMENT_PREFIX, str(uuid.uuid4()), node_name) + mblog_data['comments'] = "xmpp:%(service)s?%(query)s" % {'service': service.userhost(), + 'query': urllib.urlencode([('node',comments_node.encode('utf-8'))])} + _options = {P.OPT_ACCESS_MODEL: access_model_value, + P.OPT_PERSIST_ITEMS: 1, + P.OPT_MAX_ITEMS: -1, + P.OPT_DELIVER_PAYLOADS: 1, + P.OPT_SEND_ITEM_SUBSCRIBE: 1, + P.OPT_PUBLISH_MODEL: "subscribers", #TODO: should be open if *both* node and item access_model are open (public node and item) + } + if access_model_value == 'roster': + _options[P.OPT_ROSTER_GROUPS_ALLOWED] = list(access_list) + + # FIXME: check comments node creation success, at the moment this is a potential security risk (if the node + # already exists, the creation will silently fail, but the comments link will stay the same, linking to a + # node owned by somebody else) + defer_blog = self.host.plugins["XEP-0060"].createNode(service, comments_node, _options, profile_key=client.profile) + + mblog_item = self.host.plugins["XEP-0277"].data2entry(mblog_data, client.profile) form = data_form.Form('submit', formNamespace=NS_PUBSUB_ITEM_CONFIG) + if access_type == "PUBLIC": if access_list: raise BadAccessListError("access_list must be empty for PUBLIC access") - access = data_form.Field(None, OPT_ACCESS_MODEL, value="open") + access = data_form.Field(None, P.OPT_ACCESS_MODEL, value=access_model_value) form.addField(access) elif access_type == "GROUP": - access = data_form.Field(None, OPT_ACCESS_MODEL, value="roster") - allowed = data_form.Field(None, OPT_ROSTER_GROUPS_ALLOWED, values=access_list) + access = data_form.Field(None, P.OPT_ACCESS_MODEL, value=access_model_value) + allowed = data_form.Field(None, P.OPT_ROSTER_GROUPS_ALLOWED, values=access_list) form.addField(access) form.addField(allowed) mblog_item.addChild(form.toElement()) @@ -225,34 +273,36 @@ else: error(_("Unknown access_type")) raise BadAccessTypeError - defer_blog = self.host.plugins["XEP-0060"].publish(service, self.getNodeName(client.jid), items=[mblog_item], profile_key=client.profile) + + defer_blog = self.host.plugins["XEP-0060"].publish(service, node_name, items=[mblog_item], profile_key=client.profile) defer_blog.addErrback(self._mblogPublicationFailed) def _mblogPublicationFailed(self, failure): #TODO return failure - def sendGroupBlog(self, access_type, access_list, message, profile_key='@DEFAULT@'): + def sendGroupBlog(self, access_type, access_list, message, options, profile_key='@NONE@'): """Publish a microblog with given item access @param access_type: one of "PUBLIC", "GROUP", "JID" @param access_list: list of authorized entity (empty list for PUBLIC ACCESS, list of groups or list of jids) for this item @param message: microblog + @param options: dict which option name as key, which can be: + - allow_comments: True to accept comments, False else (default: False) @profile_key: %(doc_profile)s """ - print "sendGroupBlog" def initialised(result): profile, client = result if access_type == "PUBLIC": if access_list: raise Exception("Publishers list must be empty when getting microblogs for all contacts") - self._publishMblog(client.item_access_pubsub, client, "PUBLIC", [], message) + self._publishMblog(client.item_access_pubsub, client, "PUBLIC", [], message, options) elif access_type == "GROUP": _groups = set(access_list).intersection(client.roster.getGroups()) # We only keep group which actually exist if not _groups: raise BadAccessListError("No valid group") - self._publishMblog(client.item_access_pubsub, client, "GROUP", _groups, message) + self._publishMblog(client.item_access_pubsub, client, "GROUP", _groups, message, options) elif access_type == "JID": raise NotImplementedError else: @@ -261,19 +311,56 @@ self.initialise(profile_key).addCallback(initialised) - def getLastGroupBlogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@'): + def sendGroupBlogComment(self, node_url, message, profile_key='@DEFAULT@'): + """Publish a comment in the given node + @param node url: link to the comments node as specified in XEP-0277 and given in microblog data's comments key + @param message: comment + @profile_key: %(doc_profile)s + """ + profile = self.host.memory.getProfileName(profile_key) + if not profile: + error(_("Unknown profile")) + raise Exception("Unknown profile") + + service, node = self.host.plugins["XEP-0277"].parseCommentUrl(node_url) + + mblog_data = {'content': message} + mblog_item = self.host.plugins["XEP-0277"].data2entry(mblog_data, profile) + + return self.host.plugins["XEP-0060"].publish(service, node, items=[mblog_item], profile_key=profile) + + def _itemsConstruction(self, items, pub_jid, client): + """ Transforms items to group blog data and manage comments node + @param items: iterable of items + @return: list of group blog data """ + ret = [] + for item in items: + gbdata = self.item2gbdata(item) + ret.append(gbdata) + # if there is a comments node, we subscribe to it + if "comments_node" in gbdata and pub_jid.userhostJID() != client.jid.userhostJID(): + try: + self.host.plugins["XEP-0060"].subscribe(gbdata["comments_service"], gbdata["comments_node"], + profile_key=client.profile) + self.host.plugins["XEP-0060"].getItems(gbdata["comments_service"], gbdata["comments_node"], profile_key=client.profile) + except KeyError: + warning("Missing key for comments") + return ret + + def getLastGroupBlogs(self, pub_jid_s, max_items=10, profile_key='@DEFAULT@'): """Get the last published microblogs - @param pub_jid: jid of the publisher + @param pub_jid_s: jid of the publisher @param max_items: how many microblogs we want to get (see XEP-0060 #6.5.7) @param profile_key: profile key @return: list of microblog data (dict) """ + pub_jid = jid.JID(pub_jid_s) def initialised(result): profile, client = result - d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(jid.JID(pub_jid)), + d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(pub_jid), max_items=max_items, profile_key=profile_key) - d.addCallback(lambda items: map(self.item2gbdata, items)) + d.addCallback(self._itemsConstruction, pub_jid, client) d.addErrback(lambda ignore: {}) # TODO: more complete error management (log !) return d @@ -317,10 +404,12 @@ mblogs = [] - for _jid in jids: - d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(jid.JID(_jid)), + for jid_s in jids: + _jid = jid.JID(jid_s) + d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(_jid), max_items=max_items, profile_key=profile_key) - d.addCallback(lambda items, source_jid: (source_jid, map(self.item2gbdata, items)), _jid) + d.addCallback(lambda items, source_jid: (source_jid, self._itemsConstruction(items, _jid, client)), jid_s) + mblogs.append(d) dlist = defer.DeferredList(mblogs) dlist.addCallback(sendResult) @@ -335,7 +424,7 @@ return self.initialise(profile_key).addCallback(initialised) #TODO: we need to use the server corresponding the the host of the jid - def subscribeGroupBlog(self, pub_jid, profile_key='@DEFAULT'): + def subscribeGroupBlog(self, pub_jid, profile_key='@DEFAULT@'): def initialised(result): profile, client = result d = self.host.plugins["XEP-0060"].subscribe(client.item_access_pubsub, self.getNodeName(jid.JID(pub_jid)), diff -r bef0f893482f -r 6f4c31192c7c src/plugins/plugin_xep_0060.py --- a/src/plugins/plugin_xep_0060.py Sun Apr 07 23:27:07 2013 +0200 +++ b/src/plugins/plugin_xep_0060.py Mon May 20 23:21:29 2013 +0200 @@ -18,12 +18,8 @@ # along with this program. If not, see . from logging import debug, info, error -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 wokkel import disco, iwokkel, pubsub +from wokkel import disco, pubsub from zope.interface import implements @@ -40,6 +36,16 @@ class XEP_0060(object): + OPT_ACCESS_MODEL = 'pubsub#access_model' + OPT_PERSIST_ITEMS = 'pubsub#persist_items' + OPT_MAX_ITEMS = 'pubsub#max_items' + OPT_DELIVER_PAYLOADS = 'pubsub#deliver_payloads' + OPT_SEND_ITEM_SUBSCRIBE = 'pubsub#send_item_subscribe' + OPT_NODE_TYPE = 'pubsub#node_type' + OPT_SUBSCRIPTION_TYPE = 'pubsub#subscription_type' + OPT_SUBSCRIPTION_DEPTH = 'pubsub#subscription_depth' + OPT_ROSTER_GROUPS_ALLOWED = 'pubsub#roster_groups_allowed' + OPT_PUBLISH_MODEL = 'pubsub#publish_model' def __init__(self, host): info(_("PubSub plugin initialization")) @@ -140,9 +146,9 @@ #TODO: manage delete event debug(_("Publish node deleted")) - def purgeReceived(self, event): - import pdb - pdb.set_trace() + # def purgeReceived(self, event): + + def getDiscoInfo(self, requestor, target, nodeIdentifier=''): _disco_info = [] diff -r bef0f893482f -r 6f4c31192c7c src/plugins/plugin_xep_0277.py --- a/src/plugins/plugin_xep_0277.py Sun Apr 07 23:27:07 2013 +0200 +++ b/src/plugins/plugin_xep_0277.py Mon May 20 23:21:29 2013 +0200 @@ -17,25 +17,18 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from logging import debug, info, error -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 logging import debug, info, warning, error +from twisted.words.protocols.jabber import jid +from sat.core import exceptions from sat.tools.xml_tools import ElementParser -from wokkel import disco, pubsub -from feed.atom import Entry, Author +from wokkel import pubsub +from feed import atom import uuid from time import time +import urlparse NS_MICROBLOG = 'urn:xmpp:microblog:0' -OPT_ACCESS_MODEL = 'pubsub#access_model' -OPT_PERSIST_ITEMS = 'pubsub#persist_items' -OPT_MAX_ITEMS = 'pubsub#max_items' -OPT_DELIVER_PAYLOADS = 'pubsub#deliver_payloads' -OPT_SEND_ITEM_SUBSCRIBE = 'pubsub#send_item_subscribe' PLUGIN_INFO = { "name": "Microblogging over XMPP Plugin", @@ -73,6 +66,20 @@ async=True, doc={}) + def parseCommentUrl(self, node_url): + parsed_url = urlparse.urlparse(node_url, 'xmpp') + service = jid.JID(parsed_url.path) + queries = parsed_url.query.split(';') + parsed_queries = dict() + for query in queries: + parsed_queries.update(urlparse.parse_qs(query)) + node = parsed_queries.get('node',[''])[0] + + if not node: + raise exceptions.DataError('Invalid comments link') + + return (service, node) + def item2mbdata(self, item): """Convert an XML Item to microblog data used in bridge API @param item: domish.Element of microblog item @@ -82,7 +89,7 @@ except KeyError: warning(_('No entry element in microblog item')) return - _entry = Entry().import_xml(entry_elt.toXml().encode('utf-8')) + _entry = atom.Entry().import_xml(entry_elt.toXml().encode('utf-8')) microblog_data = {} try: microblog_data['content'] = _entry.title.text @@ -90,6 +97,17 @@ microblog_data['author'] = _entry.authors[0].name.text microblog_data['timestamp'] = str(int(_entry.updated.tf)) microblog_data['id'] = item['id'] + for link in _entry.links: + try: + if link.attrs["title"] == "comments": + microblog_data['comments'] = link.attrs["href"] + service, node = self.parseCommentUrl(microblog_data["comments"]) + microblog_data['comments_service'] = service.full() + microblog_data['comments_node'] = node + break + except (KeyError, exceptions.DataError): + continue + except (AttributeError, KeyError): error(_('Error while parsing atom entry for microblogging event')) return {} @@ -117,13 +135,19 @@ @return: domish.Element""" _uuid = unicode(uuid.uuid1()) content = data['content'] - _entry = Entry() + _entry = atom.Entry() #FIXME: need to escape html _entry.title = unicode(content).encode('utf-8') - _entry.author = Author() + _entry.author = atom.Author() _entry.author.name = data.get('author', self.host.getJidNStream(profile)[0].userhost()).encode('utf-8') _entry.updated = float(data.get('timestamp', time())) _entry.id = str(_uuid) + if 'comments' in data: + link = atom.Link() + link.attrs['href'] = data['comments'] + link.attrs['rel'] = 'replies' + link.attrs['title'] = 'comments' + _entry.links.append(link) _entry_elt = ElementParser()(str(_entry).decode('utf-8')) item = pubsub.Item(payload=_entry_elt) item['id'] = _uuid @@ -149,10 +173,7 @@ @param pub_jid: jid of the publisher @param max_items: how many microblogs we want to get @param profile_key: profile key - @param callback: used for the async answer - @param errback: used for the async answer """ - assert(callback) d = self.host.plugins["XEP-0060"].getItems(jid.JID(pub_jid), NS_MICROBLOG, max_items=max_items, profile_key=profile_key) d.addCallback(lambda items: map(self.item2mbdata, items)) @@ -166,7 +187,8 @@ if not _jid: error(_("Can't find profile's jid")) return - _options = {OPT_ACCESS_MODEL: access, OPT_PERSIST_ITEMS: 1, OPT_MAX_ITEMS: -1, OPT_DELIVER_PAYLOADS: 1, OPT_SEND_ITEM_SUBSCRIBE: 1} + C = self.host.plugins["XEP-0060"] + _options = {C.OPT_ACCESS_MODEL: access, C.OPT_PERSIST_ITEMS: 1, C.OPT_MAX_ITEMS: -1, C.OPT_DELIVER_PAYLOADS: 1, C.OPT_SEND_ITEM_SUBSCRIBE: 1} def cb(result): #Node is created with right permission diff -r bef0f893482f -r 6f4c31192c7c src/tools/xml_tools.py --- a/src/tools/xml_tools.py Sun Apr 07 23:27:07 2013 +0200 +++ b/src/tools/xml_tools.py Mon May 20 23:21:29 2013 +0200 @@ -320,6 +320,6 @@ parser.ElementEvent = onElement parser.DocumentEndEvent = onEnd tmp = domish.Element((None, "s")) - tmp.addRawXml(string.replace('\n', '').replace('\t', '')) + tmp.addRawXml(string.replace('\n', ' ').replace('\t', ' ')) parser.parse(tmp.toXml().encode('utf-8')) return self.result.firstChildElement()