comparison sat/plugins/plugin_xep_0428.py @ 3800:2033fa3c5b85

plugin XEP-0428: Fallback Indication implementation: rel 367
author Goffi <goffi@goffi.org>
date Fri, 17 Jun 2022 14:15:23 +0200
parents
children 524856bd7b19
comparison
equal deleted inserted replaced
3799:60724ff3f273 3800:2033fa3c5b85
1 #!/usr/bin/env python3
2
3 # Copyright (C) 2009-2022 Jérôme Poisson (goffi@goffi.org)
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Affero General Public License for more details.
14
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 from typing import Optional
19
20 from twisted.words.protocols.jabber import xmlstream
21 from twisted.words.xish import domish
22 from wokkel import disco
23 from zope.interface import implementer
24
25 from sat.core.constants import Const as C
26 from sat.core.i18n import _
27 from sat.core.log import getLogger
28
29 log = getLogger(__name__)
30
31
32 PLUGIN_INFO = {
33 C.PI_NAME: "Fallback Indication",
34 C.PI_IMPORT_NAME: "XEP-0428",
35 C.PI_TYPE: "XEP",
36 C.PI_PROTOCOLS: ["XEP-0428"],
37 C.PI_MAIN: "XEP_0428",
38 C.PI_HANDLER: "yes",
39 C.PI_DESCRIPTION: _("""Implementation of XEP-0428 (Fallback Indication)"""),
40 }
41
42 NS_FALLBACK = "urn:xmpp:fallback:0"
43
44
45 class XEP_0428(object):
46
47 def __init__(self, host):
48 log.info(_("XEP-0428 (Fallback Indication) plugin initialization"))
49 host.registerNamespace("fallback", NS_FALLBACK)
50
51 def addFallbackElt(
52 self,
53 message_elt: domish.Element,
54 msg: Optional[str] = None
55 ) -> None:
56 """Add the fallback indication element
57
58 @param message_elt: <message> element where the indication must be
59 set
60 @param msg: message to show as fallback
61 Will be added as <body>
62 """
63 message_elt.addElement((NS_FALLBACK, "fallback"))
64 if msg is not None:
65 message_elt.addElement("body", content=msg)
66
67 def hasFallback(self, message_elt: domish.Element) -> bool:
68 """Tell if a message has a fallback indication"""
69 try:
70 next(message_elt.elements(NS_FALLBACK, "fallback"))
71 except StopIteration:
72 return False
73 else:
74 return True
75
76 def getHandler(self, __):
77 return XEP_0428_handler()
78
79
80 @implementer(disco.IDisco)
81 class XEP_0428_handler(xmlstream.XMPPHandler):
82
83 def getDiscoInfo(self, __, target, nodeIdentifier=""):
84 return [disco.DiscoFeature(NS_FALLBACK)]
85
86 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
87 return []