# HG changeset patch # User Goffi # Date 1653921434 -7200 # Node ID 23a51b139024d7e1f2557cfcb9ba59a4fbf5d44b # Parent cfa40fa108a4c8c4db2ecb70ba66f0bf8af4d71f pam: handle errors in onSubscribeResult diff -r cfa40fa108a4 -r 23a51b139024 sat_pubsub/pam.py --- a/sat_pubsub/pam.py Wed May 11 14:47:53 2022 +0200 +++ b/sat_pubsub/pam.py Mon May 30 16:37:14 2022 +0200 @@ -71,18 +71,32 @@ return jid.JID(iq_elt["from"]) def onSubscribeResult(self, iq_req_elt, iq_result_elt, pam_iq_elt): - destinee_jid = jid.JID(iq_req_elt["from"]) - sender_jid = jid.JID(iq_req_elt["to"]) - message_elt = domish.Element((None, "message")) - message_elt["to"] = destinee_jid.userhost() - message_elt["from"] = destinee_jid.userhost() - # XXX: we explicitely store the notification to be sure that all clients get it - message_elt.addElement(("urn:xmpp:hints", "store")) - notify_elt = message_elt.addElement((NS_PAM, "notify")) - notify_elt["service"] = sender_jid.full() - notify_elt.addChild(iq_result_elt.pubsub.subscription) - self.backend.privilege.sendMessage(message_elt) - pam_iq_result_elt = xmlstream.toResponse(pam_iq_elt, 'result') + subscription_elt = iq_result_elt.pubsub.subscription + if subscription_elt is not None: + destinee_jid = jid.JID(iq_req_elt["from"]) + sender_jid = jid.JID(iq_req_elt["to"]) + message_elt = domish.Element((None, "message")) + message_elt["to"] = destinee_jid.userhost() + message_elt["from"] = destinee_jid.userhost() + # XXX: we explicitely store the notification to be sure that all clients get it + message_elt.addElement(("urn:xmpp:hints", "store")) + notify_elt = message_elt.addElement((NS_PAM, "notify")) + notify_elt["service"] = sender_jid.full() + notify_elt.addChild(subscription_elt) + self.backend.privilege.sendMessage(message_elt) + pam_iq_result_elt = xmlstream.toResponse(pam_iq_elt, 'result') + else: + # no element, this must be an error + error_elt = iq_result_elt.error + if error_elt is None: + log.msg(f"Invalid reply received: {iq_result_elt.toXml()}") + error_elt = jabber_error.StanzaError( + "service-unavailable", + "received invalid reply from external pubsub service" + ).getElement() + pam_iq_result_elt = xmlstream.toResponse(pam_iq_elt, 'error') + pam_iq_result_elt.addChild(error_elt) + self.xmlstream.send(pam_iq_result_elt) async def onSubRequest(self, from_jid, iq_elt, subscribe=True):