annotate libervia/backend/plugins/plugin_xep_0428.py @ 4305:4cd4922de876

tests: reformat tests using black.
author Goffi <goffi@goffi.org>
date Thu, 26 Sep 2024 16:11:56 +0200
parents 0d7bb4df2343
children
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
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
25 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
26 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
27 from libervia.backend.core.log import getLogger
3800
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",
4166
a1f7040b5a15 plugin XEP-0424: message retraction update:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
36 C.PI_MODES: C.PLUG_MODE_BOTH,
3800
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 C.PI_PROTOCOLS: ["XEP-0428"],
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_MAIN: "XEP_0428",
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 C.PI_HANDLER: "yes",
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 C.PI_DESCRIPTION: _("""Implementation of XEP-0428 (Fallback Indication)"""),
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
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 NS_FALLBACK = "urn:xmpp:fallback:0"
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
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 class XEP_0428(object):
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
47
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 def __init__(self, host):
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 log.info(_("XEP-0428 (Fallback Indication) plugin initialization"))
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3800
diff changeset
50 host.register_namespace("fallback", NS_FALLBACK)
3800
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3800
diff changeset
52 def add_fallback_elt(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4166
diff changeset
53 self, message_elt: domish.Element, msg: Optional[str] = None
3800
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 ) -> None:
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 """Add the fallback indication element
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
56
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 @param message_elt: <message> element where the indication must be
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 set
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 @param msg: message to show as fallback
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 Will be added as <body>
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 """
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 message_elt.addElement((NS_FALLBACK, "fallback"))
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 if msg is not None:
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 message_elt.addElement("body", content=msg)
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3800
diff changeset
66 def has_fallback(self, message_elt: domish.Element) -> bool:
3800
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 """Tell if a message has a fallback indication"""
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 try:
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 next(message_elt.elements(NS_FALLBACK, "fallback"))
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 except StopIteration:
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 return False
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 else:
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 return True
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
74
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3800
diff changeset
75 def get_handler(self, __):
3800
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 return XEP_0428_handler()
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
77
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 @implementer(disco.IDisco)
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 class XEP_0428_handler(xmlstream.XMPPHandler):
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
81
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 def getDiscoInfo(self, __, target, nodeIdentifier=""):
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 return [disco.DiscoFeature(NS_FALLBACK)]
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
2033fa3c5b85 plugin XEP-0428: Fallback Indication implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 return []