comparison src/plugins/plugin_misc_groupblog.py @ 707:890fbf2d7fdd

plugin XEP-0277, groupblog: rich text management for receiving microblogs
author Goffi <goffi@goffi.org>
date Thu, 14 Nov 2013 18:36:02 +0100
parents 80e9d3ecb272
children 6aa71c853bf5
comparison
equal deleted inserted replaced
706:80e9d3ecb272 707:890fbf2d7fdd
174 if (not (origin_host) 174 if (not (origin_host)
175 or len(event_host) < len(origin_host) 175 or len(event_host) < len(origin_host)
176 or event_host[-len(origin_host):] != origin_host): 176 or event_host[-len(origin_host):] != origin_host):
177 warning("Host incoherence between %s and %s (hack attempt ?)" % (unicode(event.sender), 177 warning("Host incoherence between %s and %s (hack attempt ?)" % (unicode(event.sender),
178 unicode(publisher))) 178 unicode(publisher)))
179 return 179 return False
180 180
181 client = self.host.getClient(profile) 181 client = self.host.getClient(profile)
182 182
183 for gbdata in self._itemsConstruction(event.items, publisher, client): 183 def gbdataManagementMicroblog(gbdata):
184 self.host.bridge.personalEvent(publisher.full(), "MICROBLOG", gbdata, profile) 184 for gbdatum in gbdata:
185 self.host.bridge.personalEvent(publisher.full(), "MICROBLOG", gbdatum, profile)
186
187 d = self._itemsConstruction(event.items, publisher, client)
188 d.addCallback(gbdataManagementMicroblog)
185 return False 189 return False
186 190
187 elif event.nodeIdentifier.startswith(NS_COMMENT_PREFIX): 191 elif event.nodeIdentifier.startswith(NS_COMMENT_PREFIX):
188 # Comment 192 # Comment
189 for microblog_data in self._handleCommentsItems(event.items, event.sender, event.nodeIdentifier): 193 def gbdataManagementComments(gbdata):
190 publisher = None # FIXME: see below (_handleCommentsItems) 194 for gbdatum in gbdata:
191 self.host.bridge.personalEvent(publisher.full() if publisher else microblog_data["author"], "MICROBLOG", microblog_data, profile) 195 publisher = None # FIXME: see below (_handleCommentsItems)
196 self.host.bridge.personalEvent(publisher.full() if publisher else gbdatum["author"], "MICROBLOG", gbdatum, profile)
197 d = self._handleCommentsItems(event.items, event.sender, event.nodeIdentifier)
198 d.addCallback(gbdataManagementComments)
192 return False 199 return False
193 return True 200 return True
194 201
202 @defer.inlineCallbacks
195 def _handleCommentsItems(self, items, service, node_identifier): 203 def _handleCommentsItems(self, items, service, node_identifier):
196 """ Convert comments items to groupblog data, and send them as signals 204 """ Convert comments items to groupblog data, and send them as signals
197 @param items: comments items 205 @param items: comments items
198 @param service: jid of the PubSub service used 206 @param service: jid of the PubSub service used
199 @param node_identifier: comments node 207 @param node_identifier: comments node
202 ret = [] 210 ret = []
203 for item in items: 211 for item in items:
204 publisher = "" # FIXME: publisher attribute for item in SàT pubsub is not managed yet, so 212 publisher = "" # FIXME: publisher attribute for item in SàT pubsub is not managed yet, so
205 # publisher is not checked and can be easily spoofed. This need to be fixed 213 # publisher is not checked and can be easily spoofed. This need to be fixed
206 # quickly. 214 # quickly.
207 microblog_data = self.item2gbdata(item, "comment") 215 microblog_data = yield self.item2gbdata(item, "comment")
208 microblog_data["service"] = service.userhost() 216 microblog_data["service"] = service.userhost()
209 microblog_data["node"] = node_identifier 217 microblog_data["node"] = node_identifier
210 microblog_data["verified_publisher"] = "true" if publisher else "false" 218 microblog_data["verified_publisher"] = "true" if publisher else "false"
211 ret.append(microblog_data) 219 ret.append(microblog_data)
212 return ret 220 defer.returnValue(ret)
213 221
214 def _parseAccessData(self, microblog_data, item): 222 def _parseAccessData(self, microblog_data, item):
215 P = self.host.plugins["XEP-0060"] 223 P = self.host.plugins["XEP-0060"]
216 form_elts = [child for child in item.elements() if child.name == "x"] 224 form_elts = [child for child in item.elements() if child.name == "x"]
217 for form_elt in form_elts: 225 for form_elt in form_elts:
226 warning("No group found for roster access-model") 234 warning("No group found for roster access-model")
227 microblog_data["groups"] = '' 235 microblog_data["groups"] = ''
228 236
229 break 237 break
230 238
239 @defer.inlineCallbacks
231 def item2gbdata(self, item, _type="main_item"): 240 def item2gbdata(self, item, _type="main_item"):
232 """ Convert item to microblog data dictionary + add access data """ 241 """ Convert item to microblog data dictionary + add access data """
233 microblog_data = self.host.plugins["XEP-0277"].item2mbdata(item) 242 microblog_data = yield self.host.plugins["XEP-0277"].item2mbdata(item)
234 microblog_data["type"] = _type 243 microblog_data["type"] = _type
235 self._parseAccessData(microblog_data, item) 244 self._parseAccessData(microblog_data, item)
236 return microblog_data 245 defer.returnValue(microblog_data)
237 246
238 def getNodeName(self, publisher): 247 def getNodeName(self, publisher):
239 """Retrieve the name of publisher's node 248 """Retrieve the name of publisher's node
240 @param publisher: publisher's jid 249 @param publisher: publisher's jid
241 @return: node's name (string)""" 250 @return: node's name (string)"""
357 mblog_data = {'content': message} 366 mblog_data = {'content': message}
358 entry_d = self.host.plugins["XEP-0277"].data2entry(mblog_data, profile) 367 entry_d = self.host.plugins["XEP-0277"].data2entry(mblog_data, profile)
359 entry_d.addCallback(lambda mblog_item: self.host.plugins["XEP-0060"].publish(service, node, items=[mblog_item], profile_key=profile)) 368 entry_d.addCallback(lambda mblog_item: self.host.plugins["XEP-0060"].publish(service, node, items=[mblog_item], profile_key=profile))
360 return entry_d 369 return entry_d
361 370
371 @defer.inlineCallbacks
362 def _itemsConstruction(self, items, pub_jid, client): 372 def _itemsConstruction(self, items, pub_jid, client):
363 """ Transforms items to group blog data and manage comments node 373 """ Transforms items to group blog data and manage comments node
364 @param items: iterable of items 374 @param items: iterable of items
365 @param pub_jid: jid of the publisher or None to use items data 375 @param pub_jid: jid of the publisher or None to use items data
366 @param client: SatXMPPClient instance 376 @param client: SatXMPPClient instance
367 @return: list of group blog data """ 377 @return: deferred which fire list of group blog data """
368 # TODO: use items data when pub_jid is None 378 # TODO: use items data when pub_jid is None
369 ret = [] 379 ret = []
370 for item in items: 380 for item in items:
371 gbdata = self.item2gbdata(item) 381 gbdata = yield self.item2gbdata(item)
372 ret.append(gbdata) 382 ret.append(gbdata)
373 # if there is a comments node, we subscribe to it 383 # if there is a comments node, we subscribe to it
374 if "comments_node" in gbdata: 384 if "comments_node" in gbdata:
375 try: 385 try:
376 # every comments node must be subscribed, except if we are the publisher (we are already subscribed in this case) 386 # every comments node must be subscribed, except if we are the publisher (we are already subscribed in this case)
377 if pub_jid.userhostJID() != client.jid.userhostJID(): 387 if pub_jid.userhostJID() != client.jid.userhostJID():
378 self.host.plugins["XEP-0060"].subscribe(jid.JID(gbdata["comments_service"]), gbdata["comments_node"], 388 self.host.plugins["XEP-0060"].subscribe(jid.JID(gbdata["comments_service"]), gbdata["comments_node"],
379 profile_key=client.profile) 389 profile_key=client.profile)
380 except KeyError: 390 except KeyError:
381 warning("Missing key for comments") 391 warning("Missing key for comments")
382 return ret 392 defer.returnValue(ret)
383 393
384 def getLastGroupBlogs(self, pub_jid_s, max_items=10, profile_key='@NONE@'): 394 def getLastGroupBlogs(self, pub_jid_s, max_items=10, profile_key='@NONE@'):
385 """Get the last published microblogs 395 """Get the last published microblogs
386 @param pub_jid_s: jid of the publisher 396 @param pub_jid_s: jid of the publisher
387 @param max_items: how many microblogs we want to get (see XEP-0060 #6.5.7) 397 @param max_items: how many microblogs we want to get (see XEP-0060 #6.5.7)
456 else: 466 else:
457 raise UnknownType 467 raise UnknownType
458 468
459 mblogs = [] 469 mblogs = []
460 470
471
461 for jid_s in jids: 472 for jid_s in jids:
462 _jid = jid.JID(jid_s) 473 _jid = jid.JID(jid_s)
463 d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(_jid), 474 d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(_jid),
464 max_items=max_items, profile_key=profile_key) 475 max_items=max_items, profile_key=profile_key)
465 d.addCallback(lambda items, source_jid: (source_jid, self._itemsConstruction(items, _jid, client)), jid_s) 476 d.addCallback(self._itemsConstruction, _jid, client)
477 d.addCallback(lambda gbdata, source_jid: (source_jid, gbdata), jid_s)
466 478
467 mblogs.append(d) 479 mblogs.append(d)
468 dlist = defer.DeferredList(mblogs) 480 dlist = defer.DeferredList(mblogs)
469 dlist.addCallback(sendResult) 481 dlist.addCallback(sendResult)
470 482