annotate sat/plugins/plugin_xep_0428.py @ 3895:eb0a77bea363

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