comparison libervia/backend/plugins/plugin_xep_0280.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/plugins/plugin_xep_0280.py@c23cad65ae99
children 9162d3480b9e
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3
4 # SAT plugin for managing xep-0280
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from libervia.backend.core.i18n import _, D_
21 from libervia.backend.core.log import getLogger
22
23 log = getLogger(__name__)
24 from libervia.backend.core import exceptions
25 from libervia.backend.core.constants import Const as C
26 from twisted.words.protocols.jabber.error import StanzaError
27 from twisted.internet import defer
28 from wokkel import disco, iwokkel
29 from zope.interface import implementer
30
31 try:
32 from twisted.words.protocols.xmlstream import XMPPHandler
33 except ImportError:
34 from wokkel.subprotocols import XMPPHandler
35
36
37 PARAM_CATEGORY = "Misc"
38 PARAM_NAME = "carbon"
39 PARAM_LABEL = D_("Message carbons")
40 NS_CARBONS = "urn:xmpp:carbons:2"
41
42 PLUGIN_INFO = {
43 C.PI_NAME: "XEP-0280 Plugin",
44 C.PI_IMPORT_NAME: "XEP-0280",
45 C.PI_TYPE: "XEP",
46 C.PI_PROTOCOLS: ["XEP-0280"],
47 C.PI_DEPENDENCIES: [],
48 C.PI_MAIN: "XEP_0280",
49 C.PI_HANDLER: "yes",
50 C.PI_DESCRIPTION: D_("""Implementation of Message Carbons"""),
51 }
52
53
54 class XEP_0280(object):
55 #  TODO: param is only checked at profile connection
56 # activate carbons on param change even after profile connection
57 # TODO: chat state notifications are not handled yet (and potentially other XEPs?)
58
59 params = """
60 <params>
61 <individual>
62 <category name="{category_name}" label="{category_label}">
63 <param name="{param_name}" label="{param_label}" value="true" type="bool" security="0" />
64 </category>
65 </individual>
66 </params>
67 """.format(
68 category_name=PARAM_CATEGORY,
69 category_label=D_(PARAM_CATEGORY),
70 param_name=PARAM_NAME,
71 param_label=PARAM_LABEL,
72 )
73
74 def __init__(self, host):
75 log.info(_("Plugin XEP_0280 initialization"))
76 self.host = host
77 host.memory.update_params(self.params)
78 host.trigger.add("message_received", self.message_received_trigger, priority=200000)
79
80 def get_handler(self, client):
81 return XEP_0280_handler()
82
83 def set_private(self, message_elt):
84 """Add a <private/> element to a message
85
86 this method is intented to be called on final domish.Element by other plugins
87 (in particular end 2 end encryption plugins)
88 @param message_elt(domish.Element): <message> stanza
89 """
90 if message_elt.name != "message":
91 log.error("addPrivateElt must be used with <message> stanzas")
92 return
93 message_elt.addElement((NS_CARBONS, "private"))
94
95 @defer.inlineCallbacks
96 def profile_connected(self, client):
97 """activate message carbons on connection if possible and activated in config"""
98 activate = self.host.memory.param_get_a(
99 PARAM_NAME, PARAM_CATEGORY, profile_key=client.profile
100 )
101 if not activate:
102 log.info(_("Not activating message carbons as requested in params"))
103 return
104 try:
105 yield self.host.check_features(client, (NS_CARBONS,))
106 except exceptions.FeatureNotFound:
107 log.warning(_("server doesn't handle message carbons"))
108 else:
109 log.info(_("message carbons available, enabling it"))
110 iq_elt = client.IQ()
111 iq_elt.addElement((NS_CARBONS, "enable"))
112 try:
113 yield iq_elt.send()
114 except StanzaError as e:
115 log.warning("Can't activate message carbons: {}".format(e))
116 else:
117 log.info(_("message carbons activated"))
118
119 def message_received_trigger(self, client, message_elt, post_treat):
120 """get message and handle it if carbons namespace is present"""
121 carbons_elt = None
122 for e in message_elt.elements():
123 if e.uri == NS_CARBONS:
124 carbons_elt = e
125 break
126
127 if carbons_elt is None:
128 # this is not a message carbons,
129 # we continue normal behaviour
130 return True
131
132 if message_elt["from"] != client.jid.userhost():
133 log.warning(
134 "The message carbon received is not from our server, hack attempt?\n{xml}".format(
135 xml=message_elt.toXml()
136 )
137 )
138 return
139 forwarded_elt = next(carbons_elt.elements(C.NS_FORWARD, "forwarded"))
140 cc_message_elt = next(forwarded_elt.elements(C.NS_CLIENT, "message"))
141
142 # we replace the wrapping message with the CCed one
143 # and continue the normal behaviour
144 if carbons_elt.name == "received":
145 message_elt["from"] = cc_message_elt["from"]
146 elif carbons_elt.name == "sent":
147 try:
148 message_elt["to"] = cc_message_elt["to"]
149 except KeyError:
150 # we may not have "to" in case of message from ourself (from an other
151 # device)
152 pass
153 else:
154 log.warning(
155 "invalid message carbons received:\n{xml}".format(
156 xml=message_elt.toXml()
157 )
158 )
159 return False
160
161 del message_elt.children[:]
162 for c in cc_message_elt.children:
163 message_elt.addChild(c)
164
165 return True
166
167 @implementer(iwokkel.IDisco)
168 class XEP_0280_handler(XMPPHandler):
169
170 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
171 return [disco.DiscoFeature(NS_CARBONS)]
172
173 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
174 return []