From dc930b18907f9db6e69ba7b073218f4038b08250 Mon Sep 17 00:00:00 2001 From: Goffi Date: Thu, 5 Dec 2019 23:05:16 +0100 Subject: [PATCH] plugin XEP-0045: don't fail on `item-not-found` with MAM: Some servers don't store permanently the whole history of MUC with MAM. As a result, an "item-not-found" stanza error can be received when message id used as reference doesn't exist anymore on the server. When this case happens, the history is retrieved in the same way as for a newly joined room (i.e. last 50 messages are requested). --- sat/plugins/plugin_xep_0045.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/sat/plugins/plugin_xep_0045.py b/sat/plugins/plugin_xep_0045.py index f465f9e6..7ecf75e5 100644 --- a/sat/plugins/plugin_xep_0045.py +++ b/sat/plugins/plugin_xep_0045.py @@ -20,9 +20,9 @@ from sat.core.i18n import _, D_ from sat.core.constants import Const as C from sat.core.log import getLogger -log = getLogger(__name__) from twisted.internet import defer from twisted.words.protocols.jabber import jid +from twisted.words.protocols.jabber import error as xmpp_error from twisted.python import failure from sat.core import exceptions @@ -41,6 +41,9 @@ from wokkel import rsm from wokkel import mam +log = getLogger(__name__) + + PLUGIN_INFO = { C.PI_NAME: "XEP-0045 Plugin", C.PI_IMPORT_NAME: "XEP-0045", @@ -979,8 +982,22 @@ class SatMUCClient(muc.MUCClient): complete = False count = 0 while not complete: - mam_data = yield self._mam.getArchives(client, mam_req, - service=room_jid) + try: + mam_data = yield self._mam.getArchives(client, mam_req, + service=room_jid) + except xmpp_error.StanzaError as e: + if last_mess and e.condition == 'item-not-found': + log.info( + f"requested item (with id {stanza_id!r}) can't be found in " + f"history of {room_jid}, history has probably been purged on " + f"server.") + # we get last items like for a new room + rsm_req = rsm.RSMRequest(max_=50, before='') + mam_req = mam.MAMRequest(rsm_=rsm_req) + no_loop=True + continue + else: + raise e elt_list, rsm_response, mam_response = mam_data complete = True if no_loop else mam_response["complete"] # we update MAM request for next iteration