comparison src/plugins/plugin_misc_groupblog.py @ 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 db4ae4d18f09
children ca13633d3b6b
comparison
equal deleted inserted replaced
533:52ab19ea940e 534:07f369ed3988
137 client.item_access_pubsub = entity 137 client.item_access_pubsub = entity
138 client._item_access_pubsub_pending.callback(None) 138 client._item_access_pubsub_pending.callback(None)
139 139
140 if hasattr(client,"_item_access_pubsub_pending"): 140 if hasattr(client,"_item_access_pubsub_pending"):
141 #XXX: we need to wait for item access pubsub service check 141 #XXX: we need to wait for item access pubsub service check
142 ignore = yield client._item_access_pubsub_pending 142 yield client._item_access_pubsub_pending
143 del client._item_access_pubsub_pending 143 del client._item_access_pubsub_pending
144 144
145 if not client.item_access_pubsub: 145 if not client.item_access_pubsub:
146 error(_("No item-access powered pubsub server found, can't use group blog")) 146 error(_("No item-access powered pubsub server found, can't use group blog"))
147 raise NoCompatiblePubSubServerFound 147 raise NoCompatiblePubSubServerFound
153 if event.nodeIdentifier.startswith(NS_NODE_PREFIX): 153 if event.nodeIdentifier.startswith(NS_NODE_PREFIX):
154 publisher = jid.JID(event.nodeIdentifier[len(NS_NODE_PREFIX):]) 154 publisher = jid.JID(event.nodeIdentifier[len(NS_NODE_PREFIX):])
155 origin_host = publisher.host.split('.') 155 origin_host = publisher.host.split('.')
156 event_host = event.sender.host.split('.') 156 event_host = event.sender.host.split('.')
157 #FIXME: basic origin check, must be improved 157 #FIXME: basic origin check, must be improved
158 #TODO: automatic security test
158 if (not (origin_host) 159 if (not (origin_host)
159 or len(event_host) < len(origin_host) 160 or len(event_host) < len(origin_host)
160 or event_host[-len(origin_host):] != origin_host): 161 or event_host[-len(origin_host):] != origin_host):
161 warning("Host incoherence between %s and %s (hack attempt ?)" % (unicode(event.sender), 162 warning("Host incoherence between %s and %s (hack attempt ?)" % (unicode(event.sender),
162 unicode(publisher))) 163 unicode(publisher)))
163 return 164 return
164 for item in event.items: 165 for item in event.items:
165 microblog_data = self.host.plugins["XEP-0277"].item2mbdata(item) 166 microblog_data = self.item2gbdata(item)
167
166 self.host.bridge.personalEvent(publisher.full(), "MICROBLOG", microblog_data, profile) 168 self.host.bridge.personalEvent(publisher.full(), "MICROBLOG", microblog_data, profile)
167 return False 169 return False
168 return True 170 return True
169 171
172
173 def _parseAccessData(self, microblog_data, item):
174 form_elts = filter(lambda elt: elt.name == "x", item.children)
175 for form_elt in form_elts:
176 form = data_form.Form.fromElement(form_elt)
177
178 if (form.formNamespace == NS_PUBSUB_ITEM_CONFIG):
179 access_model = form.get(OPT_ACCESS_MODEL, 'open')
180 if access_model == "roster":
181 try:
182 microblog_data["groups"] = '\n'.join(form.fields[OPT_ROSTER_GROUPS_ALLOWED].values)
183 except KeyError:
184 warning("No group found for roster access-model")
185 microblog_data["groups"] = ''
186
187 break
188
189 def item2gbdata(self, item):
190 """ Convert item to microblog data dictionary + add access data """
191 microblog_data = self.host.plugins["XEP-0277"].item2mbdata(item)
192 self._parseAccessData(microblog_data, item)
193 return microblog_data
170 194
171 195
172 def getNodeName(self, publisher): 196 def getNodeName(self, publisher):
173 """Retrieve the name of publisher's node 197 """Retrieve the name of publisher's node
174 @param publisher: publisher's jid 198 @param publisher: publisher's jid
250 274
251 def initialised(result): 275 def initialised(result):
252 profile, client = result 276 profile, client = result
253 d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(jid.JID(pub_jid)), 277 d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(jid.JID(pub_jid)),
254 max_items=max_items, profile_key=profile_key) 278 max_items=max_items, profile_key=profile_key)
255 d.addCallback(lambda items: map(self.host.plugins["XEP-0277"].item2mbdata, items)) 279 d.addCallback(lambda items: map(self.item2gbdata, items))
256 d.addErrback(lambda ignore: {}) #TODO: more complete error management (log !) 280 d.addErrback(lambda ignore: {}) #TODO: more complete error management (log !)
257 return d 281 return d
258 282
259 #TODO: we need to use the server corresponding the the host of the jid 283 #TODO: we need to use the server corresponding the the host of the jid
260 return self.initialise(profile_key).addCallback(initialised) 284 return self.initialise(profile_key).addCallback(initialised)
296 mblogs = [] 320 mblogs = []
297 321
298 for _jid in jids: 322 for _jid in jids:
299 d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(jid.JID(_jid)), 323 d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(jid.JID(_jid)),
300 max_items=max_items, profile_key=profile_key) 324 max_items=max_items, profile_key=profile_key)
301 d.addCallback(lambda items, source_jid: (source_jid, map(self.host.plugins["XEP-0277"].item2mbdata, items)), _jid) 325 d.addCallback(lambda items, source_jid: (source_jid, map(self.item2gbdata, items)), _jid)
302 mblogs.append(d) 326 mblogs.append(d)
303 dlist = defer.DeferredList(mblogs) 327 dlist = defer.DeferredList(mblogs)
304 dlist.addCallback(sendResult) 328 dlist.addCallback(sendResult)
305 329
306 return dlist 330 return dlist