Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_xep_0297.py @ 4382:b897d98b2c51 default tip
plugin XEP-0297: Reworked `forward` method and add bridge method:
`Forward` method has been reworked and now includes a fallback. XEP-0297 ask to not use
fallback, but following a discussion on xsf@, we agreed that this is a legacy thing and a
fallback should nowadays be used, I'll propose a patch to the specification.
A `message_forward` has been added to bridge.
rel 461
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 04 Jul 2025 12:33:42 +0200 |
parents | 4b842c1fb686 |
children |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
1274 | 3 |
4 # SAT plugin for Stanza Forwarding (XEP-0297) | |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
1766 | 6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org) |
1274 | 7 |
8 # This program is free software: you can redistribute it and/or modify | |
9 # it under the terms of the GNU Affero General Public License as published by | |
10 # the Free Software Foundation, either version 3 of the License, or | |
11 # (at your option) any later version. | |
12 | |
13 # This program is distributed in the hope that it will be useful, | |
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 # GNU Affero General Public License for more details. | |
17 | |
18 # You should have received a copy of the GNU Affero General Public License | |
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | |
4382
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
21 from datetime import datetime |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
22 from typing import cast |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
23 |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
24 from dateutil import tz |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
25 from twisted.internet import defer |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
26 from twisted.words.protocols.jabber import jid |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
27 from twisted.words.protocols.jabber.xmlstream import XMPPHandler |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
28 from twisted.words.xish import domish |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
29 from wokkel import disco, iwokkel |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
30 from zope.interface import implementer |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
31 |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
32 from libervia.backend import G |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
33 from libervia.backend.core import exceptions |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
34 from libervia.backend.core.constants import Const as C |
4382
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
35 from libervia.backend.core.core_types import SatXMPPEntity |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
36 from libervia.backend.models.core import MessageData |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
37 from libervia.backend.core.i18n import D_, _ |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
38 from libervia.backend.core.log import getLogger |
4382
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
39 from libervia.backend.memory.sqla_mapping import History |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
40 from libervia.backend.plugins.plugin_xep_0428 import XEP_0428 |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
41 from libervia.backend.tools.common.date_utils import date_fmt |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
42 |
4382
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
43 |
3913
944f51f9c2b4
core (xmpp): make `send` a blocking method, fix `sendMessageData` calls:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
44 |
1274 | 45 log = getLogger(__name__) |
46 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
47 |
1274 | 48 |
49 PLUGIN_INFO = { | |
3028 | 50 C.PI_NAME: "Stanza Forwarding", |
51 C.PI_IMPORT_NAME: "XEP-0297", | |
52 C.PI_TYPE: "XEP", | |
53 C.PI_PROTOCOLS: ["XEP-0297"], | |
4382
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
54 C.PI_DEPENDENCIES: ["XEP-0428"], |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
55 C.PI_MAIN: "XEP_0297", |
3028 | 56 C.PI_HANDLER: "yes", |
57 C.PI_DESCRIPTION: D_("""Implementation of Stanza Forwarding"""), | |
1274 | 58 } |
59 | |
4382
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
60 NS_FORWARD = "urn:xmpp:forward:0" |
1274 | 61 |
4382
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
62 |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
63 class XEP_0297: |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
64 # TODO: Handle forwarded message reception, for now the fallback will be used. |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
65 # TODO: Add a method to forward Pubsub Item, and notably blog content. |
1274 | 66 |
67 def __init__(self, host): | |
3028 | 68 log.info(_("Stanza Forwarding plugin initialization")) |
1274 | 69 self.host = host |
4382
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
70 self._fallback = cast(XEP_0428, host.plugins["XEP-0428"]) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
71 host.register_namespace("forward", NS_FORWARD) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
72 host.bridge.add_method( |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
73 "message_forward", |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
74 ".plugin", |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
75 in_sign="sss", |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
76 out_sign="", |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
77 method=self._forward, |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
78 async_=True, |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
79 ) |
1274 | 80 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3913
diff
changeset
|
81 def get_handler(self, client): |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2137
diff
changeset
|
82 return XEP_0297_handler(self, client.profile) |
1274 | 83 |
84 @classmethod | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3913
diff
changeset
|
85 def update_uri(cls, element, uri): |
1274 | 86 """Update recursively the element URI. |
87 | |
88 @param element (domish.Element): element to update | |
89 @param uri (unicode): new URI | |
90 """ | |
91 # XXX: we need this because changing the URI of an existing element | |
92 # containing children doesn't update the children's blank URI. | |
93 element.uri = uri | |
94 element.defaultUri = uri | |
95 for child in element.children: | |
96 if isinstance(child, domish.Element) and not child.uri: | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3913
diff
changeset
|
97 XEP_0297.update_uri(child, uri) |
1274 | 98 |
4382
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
99 def _forward( |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
100 self, |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
101 message_id: str, |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
102 recipient_jid_s: str, |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
103 profile_key: str |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
104 ) -> defer.Deferred[None]: |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
105 client = self.host.get_client(profile_key) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
106 recipient_jid = jid.JID(recipient_jid_s) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
107 return defer.ensureDeferred(self.forward_by_id(client, message_id, recipient_jid)) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
108 |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
109 async def forward_by_id( |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
110 self, |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
111 client: SatXMPPEntity, |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
112 message_id: str, |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
113 recipient_jid: jid.JID |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
114 ) -> None: |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
115 history = cast(History, await G.storage.get( |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
116 client, |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
117 History, |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
118 History.uid, |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
119 message_id, |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
120 joined_loads=[History.messages, History.subjects, History.thread], |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
121 )) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
122 if not history: |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
123 raise exceptions.NotFound( |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
124 f"No history found with message {message_id!r}." |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
125 ) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
126 # FIXME: Q&D way to get MessageData from History. History should have a proper |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
127 # way to do that. |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
128 serialised_history = history.serialise() |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
129 serialised_history["from"] = jid.JID(serialised_history["from"]) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
130 serialised_history["to"] = jid.JID(serialised_history["to"]) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
131 mess_data = MessageData(serialised_history) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
132 client.generate_message_xml(mess_data) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
133 message_elt = cast(domish.Element, mess_data["xml"]) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
134 timestamp_float = float(history.timestamp or history.received_timestamp) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
135 timestamp = datetime.fromtimestamp(timestamp_float, tz.tzutc()) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
136 |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
137 fallback_lines = [ |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
138 f"{client.jid.userhost()} is forwarding this message to you:", |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
139 f"[{date_fmt(timestamp_float)}]", |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
140 f"From: {history.source_jid.full()}", |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
141 f"To: {history.dest_jid.full()}" |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
142 ] |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
143 |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
144 for subject in history.subjects: |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
145 if subject.language: |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
146 fallback_lines.append(f"Subject [{subject.language}]: {subject.subject}") |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
147 else: |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
148 fallback_lines.append(f"Subject: {subject.subject}") |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
149 |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
150 for message in history.messages: |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
151 if message.language: |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
152 fallback_lines.append(f"Message [{message.language}]: {message.message}") |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
153 else: |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
154 fallback_lines.append(f"Message: {message.message}") |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
155 |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
156 fallback_msg = "\n".join(fallback_lines) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
157 |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
158 await self.forward(client, message_elt, recipient_jid, timestamp, fallback_msg) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
159 |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
160 async def forward( |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
161 self, |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
162 client: SatXMPPEntity, |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
163 stanza: domish.Element, |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
164 to_jid: jid.JID, |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
165 timestamp: datetime|None, |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
166 fallback_msg: str | None = None |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
167 ): |
1274 | 168 """Forward a message to the given JID. |
169 | |
4382
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
170 @param client: client instance. |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
171 @param stanza: original stanza to be forwarded. |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
172 @param to_jid: recipient JID. |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
173 @param timestamp: offset-aware timestamp of the original reception. |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
174 @param body: optional description. |
1274 | 175 @return: a Deferred when the message has been sent |
176 """ | |
4382
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
177 message_elt = domish.Element((None, "message")) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
178 message_elt["to"] = to_jid.full() |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
179 message_elt["type"] = stanza["type"] |
1274 | 180 |
2698 | 181 forwarded_elt = domish.Element((C.NS_FORWARD, "forwarded")) |
4382
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
182 if timestamp: |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
183 delay_elt = self.host.plugins["XEP-0203"].delay(timestamp) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
184 forwarded_elt.addChild(delay_elt) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
185 if not stanza.uri: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3913
diff
changeset
|
186 XEP_0297.update_uri(stanza, "jabber:client") |
1274 | 187 forwarded_elt.addChild(stanza) |
188 | |
4382
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
189 message_elt.addChild(domish.Element((None, "body"))) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
190 message_elt.addChild(forwarded_elt) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
191 self._fallback.add_fallback_elt(message_elt, NS_FORWARD, fallback_msg) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
192 return await client.send_message_data( |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
193 MessageData({"xml": message_elt, "extra": {}}) |
b897d98b2c51
plugin XEP-0297: Reworked `forward` method and add bridge method:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
194 ) |
1274 | 195 |
196 | |
3028 | 197 @implementer(iwokkel.IDisco) |
1274 | 198 class XEP_0297_handler(XMPPHandler): |
199 | |
200 def __init__(self, plugin_parent, profile): | |
201 self.plugin_parent = plugin_parent | |
202 self.host = plugin_parent.host | |
203 self.profile = profile | |
204 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
205 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
2698 | 206 return [disco.DiscoFeature(C.NS_FORWARD)] |
1274 | 207 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
208 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
1274 | 209 return [] |