annotate libervia/backend/plugins/plugin_xep_0372.py @ 4202:b26339343076

core: use a user specific directory for PID file: default location of pid file is now specific to logged user, this allow to run several instances of Libervia by different users on the same machine without PID conflicts.
author Goffi <goffi@goffi.org>
date Sun, 14 Jan 2024 17:48:02 +0100
parents 4b842c1fb686
children 0d7bb4df2343
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",
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 C.PI_DESCRIPTION: _(dedent("""\
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 XEP-0372 (References) implementation
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 This plugin implement generic references and mentions.
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 }
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 NS_REFS = "urn:xmpp:reference:0"
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 ALLOWED_TYPES = ("mention", "data")
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 class XEP_0372:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 namespace = NS_REFS
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 def __init__(self, host):
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 log.info(_("References plugin initialization"))
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
65 host.register_namespace("refs", NS_REFS)
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.host = host
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self._h = host.plugins["XEP-0334"]
4051
c23cad65ae99 core: renamed `messageReceived` trigger to `message_received`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
68 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
69 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
70 "reference_send",
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 ".plugin",
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 in_sign="sssss",
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 out_sign="",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
74 method=self._send_reference,
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 async_=False,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 )
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
77
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
78 def get_handler(self, client):
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 return XEP_0372_Handler()
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
80
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
81 def ref_element_to_ref_data(
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 self,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 reference_elt: domish.Element
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 ) -> Dict[str, Union[str, int, dict]]:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 ref_data: Dict[str, Union[str, int, dict]] = {
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 "uri": reference_elt["uri"],
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 "type": reference_elt["type"]
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 }
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
89
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 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
91 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
92
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 for attr in ("begin", "end"):
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 try:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 ref_data[attr] = int(reference_elt[attr])
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 except (KeyError, ValueError, TypeError):
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 continue
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
98
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 anchor = reference_elt.getAttribute("anchor")
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 if anchor is not None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 ref_data["anchor"] = anchor
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 if anchor.startswith("xmpp:"):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
103 ref_data["parsed_anchor"] = xmpp_uri.parse_xmpp_uri(anchor)
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 return ref_data
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
105
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
106 async def _message_received_trigger(
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 self,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 client: SatXMPPEntity,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 message_elt: domish.Element,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 post_treat: defer.Deferred
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 ) -> bool:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 """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
113 reference_elt = next(message_elt.elements(NS_REFS, "reference"), None)
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 if reference_elt is None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 return True
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
117 ref_data = self.ref_element_to_ref_data(reference_elt)
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 except KeyError:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 log.warning("invalid <reference> element: {reference_elt.toXml}")
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 return True
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
121
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
122 if not await self.host.trigger.async_point(
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 "XEP-0372_ref_received", client, message_elt, ref_data
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 ):
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 return False
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 return True
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
127
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
128 def build_ref_element(
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 self,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 uri: str,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 type_: str = "mention",
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 begin: Optional[int] = None,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 end: Optional[int] = None,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 anchor: Optional[str] = None,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 ) -> domish.Element:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 """Build and return the <reference> element"""
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 if type_ not in ALLOWED_TYPES:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 raise ValueError(f"Unknown type: {type_!r}")
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 reference_elt = domish.Element(
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 (NS_REFS, "reference"),
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 attribs={"uri": uri, "type": type_}
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 )
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 if begin is not None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 reference_elt["begin"] = str(begin)
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 if end is not None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 reference_elt["end"] = str(end)
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 if anchor is not None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 reference_elt["anchor"] = anchor
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 return reference_elt
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
150
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
151 def _send_reference(
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 self,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 recipient: str,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 anchor: str,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 type_: str,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 extra_s: str,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 profile_key: str
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 ) -> defer.Deferred:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 recipient_jid = jid.JID(recipient)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
160 client = self.host.get_client(profile_key)
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 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
162 self.send_reference(
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 client,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 uri=extra.get("uri"),
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 type_=type_,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 anchor=anchor,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 to_jid=recipient_jid
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 )
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
169
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
170 def send_reference(
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 self,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 client: "SatXMPPEntity",
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 uri: Optional[str] = None,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 type_: str = "mention",
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 begin: Optional[int] = None,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 end: Optional[int] = None,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 anchor: Optional[str] = None,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 message_elt: Optional[domish.Element] = None,
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 to_jid: Optional[jid.JID] = None
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 ) -> None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 """Build and send a reference_elt
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
182
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 @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
184 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
185 @param type_: type of reference
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 one of [ALLOWED_TYPES]
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 @param begin: optional begin index
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 @param end: optional end index
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 @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
190 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
191 object where the reference appears.
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 @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
193 generated
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 @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
195 message_elt will be used.
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
196
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 uri is None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 if to_jid is None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 raise exceptions.InternalError(
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 '"to_jid" must be set if "uri is None"'
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
203 uri = xmpp_uri.build_xmpp_uri(path=to_jid.full())
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 if message_elt is None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 message_elt = domish.Element((None, "message"))
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
206
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 if to_jid is not None:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 message_elt["to"] = to_jid.full()
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 else:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 try:
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 to_jid = jid.JID(message_elt["to"])
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 except (KeyError, RuntimeError):
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 raise exceptions.InternalError(
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 'invalid "to" attribute in given message element: '
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 '{message_elt.toXml()}'
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
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3830
diff changeset
218 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
219 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
220 client.send(message_elt)
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
221
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
222
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 @implementer(iwokkel.IDisco)
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 class XEP_0372_Handler(XMPPHandler):
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
225
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 def getDiscoInfo(self, requestor, service, nodeIdentifier=""):
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 return [disco.DiscoFeature(NS_REFS)]
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
228
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 def getDiscoItems(self, requestor, service, nodeIdentifier=""):
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 return []