changeset 398:845ed0f71dd6

mam: don't crash on missing <first> element in RSM on archive request
author Goffi <goffi@goffi.org>
date Mon, 11 Mar 2019 20:50:54 +0100
parents a4980d03b3a3
children 2521c53bc359
files src/mam.py
diffstat 1 files changed, 18 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- 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 <first> 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: