# Slixmpp: The Slick XMPP Library# Copyright (C) 2020 Mathieu Pasquet <mathieui@mathieui.net># This file is part of Slixmpp.# See the file LICENSE for copying permissiofromabcimportABCtry:fromtypingimportLiteralexceptImportError:fromtyping_extensionsimportLiteralfromslixmpp.stanzaimportMessagefromslixmpp.xmlstreamimport(ElementBase,register_stanza_plugin,)NS="urn:xmpp:fallback:0"
[docs]classFallback(ElementBase):namespace=NSname="fallback"plugin_attrib="fallback"plugin_multi_attrib="fallbacks"interfaces={"for"}def_find_fallback(self,fallback_for:str)->"Fallback":ifself["for"]==fallback_for:returnselfforfallbackinself.parent()["fallbacks"]:iffallback["for"]==fallback_for:returnfallbackraiseAttributeError("No fallback for this namespace",fallback_for)
[docs]defget_stripped_body(self,fallback_for:str,element:Literal["body","subject"]="body")->str:""" Get the body of a message, with the fallback part stripped :param fallback_for: namespace of the fallback to strip :param element: set this to "subject" get the stripped subject instead of body :return: body (or subject) content minus the fallback part """fallback=self._find_fallback(fallback_for)start=fallback[element]["start"]end=fallback[element]["end"]body=self.parent()[element]ifstart==end==0:return""ifstart<=end<len(body):returnbody[:start]+body[end:]else:returnbody