changeset 534:07f369ed3988

plugin group blog: item configuration data (which groups are allowed) are parsed and added to microblog data
author Goffi <goffi@goffi.org>
date Tue, 30 Oct 2012 00:54:42 +0100
parents 52ab19ea940e
children 790be337cc41
files src/plugins/plugin_misc_groupblog.py
diffstat 1 files changed, 28 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_misc_groupblog.py	Sun Oct 28 18:40:14 2012 +0100
+++ b/src/plugins/plugin_misc_groupblog.py	Tue Oct 30 00:54:42 2012 +0100
@@ -139,7 +139,7 @@
         
         if hasattr(client,"_item_access_pubsub_pending"):
             #XXX: we need to wait for item access pubsub service check
-            ignore = yield client._item_access_pubsub_pending
+            yield client._item_access_pubsub_pending
             del client._item_access_pubsub_pending
 
         if not client.item_access_pubsub:
@@ -155,6 +155,7 @@
             origin_host = publisher.host.split('.')
             event_host = event.sender.host.split('.')
             #FIXME: basic origin check, must be improved
+            #TODO: automatic security test
             if (not (origin_host)
                 or len(event_host) < len(origin_host)
                 or event_host[-len(origin_host):] != origin_host):
@@ -162,12 +163,35 @@
                                                                                  unicode(publisher)))
                 return
             for item in event.items:
-                microblog_data = self.host.plugins["XEP-0277"].item2mbdata(item)
+                microblog_data = self.item2gbdata(item)
+
                 self.host.bridge.personalEvent(publisher.full(), "MICROBLOG", microblog_data, profile)
             return False
         return True
 
 
+    def _parseAccessData(self, microblog_data, item):
+        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')
+                if access_model == "roster":
+                    try:
+                        microblog_data["groups"] = '\n'.join(form.fields[OPT_ROSTER_GROUPS_ALLOWED].values)
+                    except KeyError:
+                        warning("No group found for roster access-model")
+                        microblog_data["groups"] = ''
+                
+                break
+
+    def item2gbdata(self, item):
+        """ Convert item to microblog data dictionary + add access data """
+        microblog_data = self.host.plugins["XEP-0277"].item2mbdata(item)
+        self._parseAccessData(microblog_data, item)
+        return microblog_data
+
 
     def getNodeName(self, publisher):
         """Retrieve the name of publisher's node
@@ -252,7 +276,7 @@
             profile, client = result
             d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(jid.JID(pub_jid)),
                                                        max_items=max_items, profile_key=profile_key)
-            d.addCallback(lambda items: map(self.host.plugins["XEP-0277"].item2mbdata, items))
+            d.addCallback(lambda items: map(self.item2gbdata, items))
             d.addErrback(lambda ignore: {}) #TODO: more complete error management (log !)
             return d
 
@@ -298,7 +322,7 @@
             for _jid in jids:
                 d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(jid.JID(_jid)),
                                                            max_items=max_items, profile_key=profile_key)
-                d.addCallback(lambda items, source_jid: (source_jid, map(self.host.plugins["XEP-0277"].item2mbdata, items)), _jid)
+                d.addCallback(lambda items, source_jid: (source_jid, map(self.item2gbdata, items)), _jid)
                 mblogs.append(d)
             dlist = defer.DeferredList(mblogs)
             dlist.addCallback(sendResult)