annotate libervia/backend/plugins/plugin_xep_0372.py @ 4272:89a0999884ac default tip @

cli (list/set): add "--comments" argument.
author Goffi <goffi@goffi.org>
date Thu, 20 Jun 2024 14:46:55 +0200
parents 0d7bb4df2343
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia plugin for XEP-0372
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2022 Jérôme Poisson (goffi@goffi.org)
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from typing import Optional, Dict, Union
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from textwrap import dedent
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
21 from libervia.backend.core import exceptions
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
22 from libervia.backend.tools.common import uri as xmpp_uri
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
23
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.internet import defer
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber import jid
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.words.xish import domish
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from zope.interface import implementer
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from wokkel import disco, iwokkel
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
31 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
32 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
33 from libervia.backend.core.log import getLogger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
34 from libervia.backend.core.core_types import SatXMPPEntity
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
35 from libervia.backend.tools.common import data_format
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 log = getLogger(__name__)
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 PLUGIN_INFO = {
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 C.PI_NAME: "References",
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_IMPORT_NAME: "XEP-0372",
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_TYPE: C.PLUG_TYPE_XEP,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 C.PI_MODES: C.PLUG_MODE_BOTH,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 C.PI_PROTOCOLS: ["XEP-0372"],
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_DEPENDENCIES: ["XEP-0334"],
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 C.PI_MAIN: "XEP_0372",
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 C.PI_HANDLER: "yes",
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
49 C.PI_DESCRIPTION: _(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
50 dedent(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
51 """\
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 XEP-0372 (References) implementation
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 This plugin implement generic references and mentions.
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
55 """
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
56 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
57 ),
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 }
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
59
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 NS_REFS = "urn:xmpp:reference:0"
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 ALLOWED_TYPES = ("mention", "data")
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
63
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 class XEP_0372:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 namespace = NS_REFS
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
66
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 def __init__(self, host):
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 log.info(_("References plugin initialization"))
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
69 host.register_namespace("refs", NS_REFS)
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 self.host = host
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 self._h = host.plugins["XEP-0334"]
4051
c23cad65ae99 core: renamed `messageReceived` trigger to `message_received`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
72 host.trigger.add("message_received", self._message_received_trigger)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
73 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
74 "reference_send",
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 ".plugin",
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 in_sign="sssss",
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 out_sign="",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
78 method=self._send_reference,
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 async_=False,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 )
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
81
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
82 def get_handler(self, client):
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 return XEP_0372_Handler()
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
85 def ref_element_to_ref_data(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
86 self, reference_elt: domish.Element
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 ) -> Dict[str, Union[str, int, dict]]:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 ref_data: Dict[str, Union[str, int, dict]] = {
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 "uri": reference_elt["uri"],
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
90 "type": reference_elt["type"],
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 }
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
92
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 if ref_data["uri"].startswith("xmpp:"):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
94 ref_data["parsed_uri"] = xmpp_uri.parse_xmpp_uri(ref_data["uri"])
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
95
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 for attr in ("begin", "end"):
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 try:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 ref_data[attr] = int(reference_elt[attr])
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 except (KeyError, ValueError, TypeError):
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 continue
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
101
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 anchor = reference_elt.getAttribute("anchor")
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 if anchor is not None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 ref_data["anchor"] = anchor
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 if anchor.startswith("xmpp:"):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
106 ref_data["parsed_anchor"] = xmpp_uri.parse_xmpp_uri(anchor)
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 return ref_data
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
108
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
109 async def _message_received_trigger(
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 self,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 client: SatXMPPEntity,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 message_elt: domish.Element,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
113 post_treat: defer.Deferred,
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 ) -> bool:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 """Check if a direct invitation is in the message, and handle it"""
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 reference_elt = next(message_elt.elements(NS_REFS, "reference"), None)
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 if reference_elt is None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 return True
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
120 ref_data = self.ref_element_to_ref_data(reference_elt)
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 except KeyError:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 log.warning("invalid <reference> element: {reference_elt.toXml}")
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 return True
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
124
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
125 if not await self.host.trigger.async_point(
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 "XEP-0372_ref_received", client, message_elt, ref_data
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 ):
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 return False
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 return True
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
130
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
131 def build_ref_element(
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 self,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 uri: str,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 type_: str = "mention",
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 begin: Optional[int] = None,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 end: Optional[int] = None,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 anchor: Optional[str] = None,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 ) -> domish.Element:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 """Build and return the <reference> element"""
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 if type_ not in ALLOWED_TYPES:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 raise ValueError(f"Unknown type: {type_!r}")
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 reference_elt = domish.Element(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
143 (NS_REFS, "reference"), attribs={"uri": uri, "type": type_}
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 )
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 if begin is not None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 reference_elt["begin"] = str(begin)
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 if end is not None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 reference_elt["end"] = str(end)
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 if anchor is not None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 reference_elt["anchor"] = anchor
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 return reference_elt
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
152
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
153 def _send_reference(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
154 self, recipient: str, anchor: str, type_: str, extra_s: str, profile_key: str
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 ) -> defer.Deferred:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 recipient_jid = jid.JID(recipient)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
157 client = self.host.get_client(profile_key)
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 extra: dict = data_format.deserialise(extra_s, default={})
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
159 self.send_reference(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
160 client, uri=extra.get("uri"), type_=type_, anchor=anchor, to_jid=recipient_jid
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 )
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
162
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
163 def send_reference(
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 self,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 client: "SatXMPPEntity",
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 uri: Optional[str] = None,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 type_: str = "mention",
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 begin: Optional[int] = None,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 end: Optional[int] = None,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 anchor: Optional[str] = None,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 message_elt: Optional[domish.Element] = None,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
172 to_jid: Optional[jid.JID] = None,
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 ) -> None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 """Build and send a reference_elt
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
175
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 @param uri: URI pointing to referenced object (XMPP entity, Pubsub Item, etc)
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 if not set, "to_jid" will be used to build an URI to the entity
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 @param type_: type of reference
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 one of [ALLOWED_TYPES]
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 @param begin: optional begin index
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 @param end: optional end index
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 @param anchor: URI of refering object (message, pubsub item), when the refence
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 is not already in the wrapping message element. In other words, it's the
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 object where the reference appears.
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 @param message_elt: wrapping <message> element, if not set a new one will be
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 generated
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 @param to_jid: destinee of the reference. If not specified, "to" attribute of
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 message_elt will be used.
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
189
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 """
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 if uri is None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 if to_jid is None:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
193 raise exceptions.InternalError('"to_jid" must be set if "uri is None"')
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
194 uri = xmpp_uri.build_xmpp_uri(path=to_jid.full())
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 if message_elt is None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 message_elt = domish.Element((None, "message"))
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
197
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 if to_jid is not None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 message_elt["to"] = to_jid.full()
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 else:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 try:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 to_jid = jid.JID(message_elt["to"])
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 except (KeyError, RuntimeError):
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 raise exceptions.InternalError(
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 'invalid "to" attribute in given message element: '
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
206 "{message_elt.toXml()}"
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 )
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
208
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
209 message_elt.addChild(self.build_ref_element(uri, type_, begin, end, anchor))
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
210 self._h.add_hint_elements(message_elt, [self._h.HINT_STORE])
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 client.send(message_elt)
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
212
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
213
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 @implementer(iwokkel.IDisco)
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 class XEP_0372_Handler(XMPPHandler):
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
216
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 def getDiscoInfo(self, requestor, service, nodeIdentifier=""):
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 return [disco.DiscoFeature(NS_REFS)]
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
219
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 def getDiscoItems(self, requestor, service, nodeIdentifier=""):
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 return []