# HG changeset patch # User Goffi # Date 1552333854 -3600 # Node ID 845ed0f71dd68b6fcc191b61ebd2b107b5348341 # Parent a4980d03b3a3d1e498aa3e8aa97cb9108eddb3a1 mam: don't crash on missing element in RSM on archive request diff -r a4980d03b3a3 -r 845ed0f71dd6 src/mam.py --- a/src/mam.py Mon Mar 11 20:43:51 2019 +0100 +++ b/src/mam.py Mon Mar 11 20:50:54 2019 +0100 @@ -88,7 +88,8 @@ if mam_request.orderBy: ext_data['order_by'] = mam_request.orderBy - d = self.backend.getItemsData(mam_request.node, mam_request.sender, mam_request.recipient, None, None, ext_data) + d = self.backend.getItemsData(mam_request.node, mam_request.sender, + mam_request.recipient, None, None, ext_data) def make_message(elt): # XXX: http://xmpp.org/extensions/xep-0297.html#sect-idp629952 (rule 3) @@ -107,17 +108,23 @@ if item_data.item.name == 'set' and item_data.item.uri == rsm.NS_RSM: assert rsm_elt is None rsm_elt = item_data.item - # XXX: we check if it is the last page using initial request data - # and RSM element data. In this case, we must have the "complete" - # attribute set to "true". - page_max = (int(rsm_elt.first['index']) + 1) * mam_request.rsm.max - count = int(unicode(rsm_elt.count)) - if page_max >= count: - # the maximum items which can be displayed is equal to or above - # the total number of items, which means we are complete - attributes['complete'] = "true" + if rsm_elt.first: + # XXX: we check if it is the last page using initial request data + # and RSM element data. In this case, we must have the + # "complete" + # attribute set to "true". + page_max = (int(rsm_elt.first['index']) + 1) * mam_request.rsm.max + count = int(unicode(rsm_elt.count)) + if page_max >= count: + # the maximum items which can be displayed is equal to or + # above the total number of items, which means we are complete + attributes['complete'] = "true" + else: + log.warnign(u"no element in RSM request: {xml}".format( + xml = rsm_elt.toXml())) elif item_data.item.name == 'item': - msg_data.append([item_data.item['id'], make_message(item_data.item), item_data.created]) + msg_data.append([item_data.item['id'], make_message(item_data.item), + item_data.created]) else: log.msg("WARNING: unknown element: {}".format(item_data.item.name)) if pep: