Mercurial > libervia-backend
comparison libervia/backend/plugins/plugin_xep_0184.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_0184.py@524856bd7b19 |
children | 0d7bb4df2343 |
comparison
equal
deleted
inserted
replaced
4070:d10748475025 | 4071:4b842c1fb686 |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 # SAT plugin for managing xep-0184 | |
5 # Copyright (C) 2009-2016 Geoffrey POUZET (chteufleur@kingpenguin.tk) | |
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 from libervia.backend.core.i18n import _ | |
20 from libervia.backend.core.constants import Const as C | |
21 from libervia.backend.core.log import getLogger | |
22 from twisted.internet import reactor | |
23 from twisted.words.protocols.jabber import xmlstream, jid | |
24 from twisted.words.xish import domish | |
25 | |
26 log = getLogger(__name__) | |
27 | |
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 NS_MESSAGE_DELIVERY_RECEIPTS = "urn:xmpp:receipts" | |
38 | |
39 MSG = "message" | |
40 | |
41 MSG_CHAT = "/" + MSG + '[@type="chat"]' | |
42 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_REQUEST = ( | |
43 MSG_CHAT + '/request[@xmlns="' + NS_MESSAGE_DELIVERY_RECEIPTS + '"]' | |
44 ) | |
45 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_RECEIVED = ( | |
46 MSG_CHAT + '/received[@xmlns="' + NS_MESSAGE_DELIVERY_RECEIPTS + '"]' | |
47 ) | |
48 | |
49 MSG_NORMAL = "/" + MSG + '[@type="normal"]' | |
50 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_REQUEST = ( | |
51 MSG_NORMAL + '/request[@xmlns="' + NS_MESSAGE_DELIVERY_RECEIPTS + '"]' | |
52 ) | |
53 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_RECEIVED = ( | |
54 MSG_NORMAL + '/received[@xmlns="' + NS_MESSAGE_DELIVERY_RECEIPTS + '"]' | |
55 ) | |
56 | |
57 | |
58 PARAM_KEY = "Privacy" | |
59 PARAM_NAME = "Enable message delivery receipts" | |
60 ENTITY_KEY = PARAM_KEY + "_" + PARAM_NAME | |
61 | |
62 | |
63 PLUGIN_INFO = { | |
64 C.PI_NAME: "XEP-0184 Plugin", | |
65 C.PI_IMPORT_NAME: "XEP-0184", | |
66 C.PI_TYPE: "XEP", | |
67 C.PI_PROTOCOLS: ["XEP-0184"], | |
68 C.PI_DEPENDENCIES: [], | |
69 C.PI_MAIN: "XEP_0184", | |
70 C.PI_HANDLER: "yes", | |
71 C.PI_DESCRIPTION: _("""Implementation of Message Delivery Receipts"""), | |
72 } | |
73 | |
74 | |
75 STATUS_MESSAGE_DELIVERY_RECEIVED = "delivered" | |
76 TEMPO_DELETE_WAITING_ACK_S = 300 # 5 min | |
77 | |
78 | |
79 class XEP_0184(object): | |
80 """ | |
81 Implementation for XEP 0184. | |
82 """ | |
83 | |
84 params = """ | |
85 <params> | |
86 <individual> | |
87 <category name="%(category_name)s" label="%(category_label)s"> | |
88 <param name="%(param_name)s" label="%(param_label)s" value="true" type="bool" security="0"/> | |
89 </category> | |
90 </individual> | |
91 </params> | |
92 """ % { | |
93 "category_name": PARAM_KEY, | |
94 "category_label": _(PARAM_KEY), | |
95 "param_name": PARAM_NAME, | |
96 "param_label": _("Enable message delivery receipts"), | |
97 } | |
98 | |
99 def __init__(self, host): | |
100 log.info(_("Plugin XEP_0184 (message delivery receipts) initialization")) | |
101 self.host = host | |
102 self._dictRequest = dict() | |
103 | |
104 # parameter value is retrieved before each use | |
105 host.memory.update_params(self.params) | |
106 | |
107 host.trigger.add("sendMessage", self.send_message_trigger) | |
108 host.bridge.add_signal( | |
109 "message_state", ".plugin", signature="sss" | |
110 ) # message_uid, status, profile | |
111 | |
112 def get_handler(self, client): | |
113 return XEP_0184_handler(self, client.profile) | |
114 | |
115 def send_message_trigger( | |
116 self, client, mess_data, pre_xml_treatments, post_xml_treatments | |
117 ): | |
118 """Install SendMessage command hook """ | |
119 | |
120 def treatment(mess_data): | |
121 message = mess_data["xml"] | |
122 message_type = message.getAttribute("type") | |
123 | |
124 if self._is_actif(client.profile) and ( | |
125 message_type == "chat" or message_type == "normal" | |
126 ): | |
127 message.addElement("request", NS_MESSAGE_DELIVERY_RECEIPTS) | |
128 uid = mess_data["uid"] | |
129 msg_id = message.getAttribute("id") | |
130 self._dictRequest[msg_id] = uid | |
131 reactor.callLater( | |
132 TEMPO_DELETE_WAITING_ACK_S, self._clear_dict_request, msg_id | |
133 ) | |
134 log.debug( | |
135 _( | |
136 "[XEP-0184] Request acknowledgment for message id {}".format( | |
137 msg_id | |
138 ) | |
139 ) | |
140 ) | |
141 | |
142 return mess_data | |
143 | |
144 post_xml_treatments.addCallback(treatment) | |
145 return True | |
146 | |
147 def on_message_delivery_receipts_request(self, msg_elt, client): | |
148 """This method is called on message delivery receipts **request** (XEP-0184 #7) | |
149 @param msg_elt: message element | |
150 @param client: %(doc_client)s""" | |
151 from_jid = jid.JID(msg_elt["from"]) | |
152 | |
153 if self._is_actif(client.profile) and client.roster.is_subscribed_from(from_jid): | |
154 received_elt_ret = domish.Element((NS_MESSAGE_DELIVERY_RECEIPTS, "received")) | |
155 try: | |
156 received_elt_ret["id"] = msg_elt["id"] | |
157 except KeyError: | |
158 log.warning(f"missing id for message element: {msg_elt.toXml}") | |
159 return | |
160 | |
161 msg_result_elt = xmlstream.toResponse(msg_elt, "result") | |
162 msg_result_elt.addChild(received_elt_ret) | |
163 client.send(msg_result_elt) | |
164 | |
165 def on_message_delivery_receipts_received(self, msg_elt, client): | |
166 """This method is called on message delivery receipts **received** (XEP-0184 #7) | |
167 @param msg_elt: message element | |
168 @param client: %(doc_client)s""" | |
169 msg_elt.handled = True | |
170 rcv_elt = next(msg_elt.elements(NS_MESSAGE_DELIVERY_RECEIPTS, "received")) | |
171 msg_id = rcv_elt["id"] | |
172 | |
173 try: | |
174 uid = self._dictRequest[msg_id] | |
175 del self._dictRequest[msg_id] | |
176 self.host.bridge.message_state( | |
177 uid, STATUS_MESSAGE_DELIVERY_RECEIVED, client.profile | |
178 ) | |
179 log.debug( | |
180 _("[XEP-0184] Receive acknowledgment for message id {}".format(msg_id)) | |
181 ) | |
182 except KeyError: | |
183 pass | |
184 | |
185 def _clear_dict_request(self, msg_id): | |
186 try: | |
187 del self._dictRequest[msg_id] | |
188 log.debug( | |
189 _( | |
190 "[XEP-0184] Delete waiting acknowledgment for message id {}".format( | |
191 msg_id | |
192 ) | |
193 ) | |
194 ) | |
195 except KeyError: | |
196 pass | |
197 | |
198 def _is_actif(self, profile): | |
199 return self.host.memory.param_get_a(PARAM_NAME, PARAM_KEY, profile_key=profile) | |
200 | |
201 | |
202 @implementer(iwokkel.IDisco) | |
203 class XEP_0184_handler(XMPPHandler): | |
204 | |
205 def __init__(self, plugin_parent, profile): | |
206 self.plugin_parent = plugin_parent | |
207 self.host = plugin_parent.host | |
208 self.profile = profile | |
209 | |
210 def connectionInitialized(self): | |
211 self.xmlstream.addObserver( | |
212 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_REQUEST, | |
213 self.plugin_parent.on_message_delivery_receipts_request, | |
214 client=self.parent, | |
215 ) | |
216 self.xmlstream.addObserver( | |
217 MSG_CHAT_MESSAGE_DELIVERY_RECEIPTS_RECEIVED, | |
218 self.plugin_parent.on_message_delivery_receipts_received, | |
219 client=self.parent, | |
220 ) | |
221 | |
222 self.xmlstream.addObserver( | |
223 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_REQUEST, | |
224 self.plugin_parent.on_message_delivery_receipts_request, | |
225 client=self.parent, | |
226 ) | |
227 self.xmlstream.addObserver( | |
228 MSG_NORMAL_MESSAGE_DELIVERY_RECEIPTS_RECEIVED, | |
229 self.plugin_parent.on_message_delivery_receipts_received, | |
230 client=self.parent, | |
231 ) | |
232 | |
233 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): | |
234 return [disco.DiscoFeature(NS_MESSAGE_DELIVERY_RECEIPTS)] | |
235 | |
236 def getDiscoItems(self, requestor, target, nodeIdentifier=""): | |
237 return [] |