Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_xep_0424.py @ 4165:81faa85c9cfa
template: add a `bare_jid` filter
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 28 Nov 2023 17:41:49 +0100 |
parents | 4b842c1fb686 |
children | a1f7040b5a15 |
rev | line source |
---|---|
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # Copyright (C) 2009-2022 Jérôme Poisson (goffi@goffi.org) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 # This program is free software: you can redistribute it and/or modify |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # it under the terms of the GNU Affero General Public License as published by |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # the Free Software Foundation, either version 3 of the License, or |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # (at your option) any later version. |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # This program is distributed in the hope that it will be useful, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # GNU Affero General Public License for more details. |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # You should have received a copy of the GNU Affero General Public License |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 from typing import Dict, Any |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 import time |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from copy import deepcopy |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from twisted.words.protocols.jabber import xmlstream, jid |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from twisted.words.xish import domish |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from twisted.internet import defer |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from wokkel import disco |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from zope.interface import implementer |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
28 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
|
29 from libervia.backend.core.i18n import _, D_ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
30 from libervia.backend.core import exceptions |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
31 from libervia.backend.core.core_types import SatXMPPEntity |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
32 from libervia.backend.core.log import getLogger |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
33 from libervia.backend.memory.sqla_mapping import History |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 log = getLogger(__name__) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 PLUGIN_INFO = { |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 C.PI_NAME: "Message Retraction", |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 C.PI_IMPORT_NAME: "XEP-0424", |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 C.PI_TYPE: "XEP", |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 C.PI_MODES: C.PLUG_MODE_BOTH, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 C.PI_PROTOCOLS: ["XEP-0334", "XEP-0424", "XEP-0428"], |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 C.PI_DEPENDENCIES: ["XEP-0422"], |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 C.PI_MAIN: "XEP_0424", |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 C.PI_HANDLER: "yes", |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 C.PI_DESCRIPTION: _("""Implementation Message Retraction"""), |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 } |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 NS_MESSAGE_RETRACT = "urn:xmpp:message-retract:0" |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 CATEGORY = "Privacy" |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 NAME = "retract_history" |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 LABEL = D_("Keep History of Retracted Messages") |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 PARAMS = """ |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 <params> |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 <individual> |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 <category name="{category_name}"> |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 <param name="{name}" label="{label}" type="bool" value="false" /> |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 </category> |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 </individual> |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 </params> |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 """.format( |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 category_name=CATEGORY, name=NAME, label=_(LABEL) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 ) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 class XEP_0424(object): |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 def __init__(self, host): |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 log.info(_("XEP-0424 (Message Retraction) plugin initialization")) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 self.host = host |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
73 host.memory.update_params(PARAMS) |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 self._h = host.plugins["XEP-0334"] |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 self._f = host.plugins["XEP-0422"] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
76 host.register_namespace("message-retract", NS_MESSAGE_RETRACT) |
4051
c23cad65ae99
core: renamed `messageReceived` trigger to `message_received`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
77 host.trigger.add("message_received", self._message_received_trigger, 100) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
78 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
79 "message_retract", |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 ".plugin", |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 in_sign="ss", |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 out_sign="", |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 method=self._retract, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 async_=True, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 ) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
87 def get_handler(self, __): |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 return XEP_0424_handler() |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 def _retract(self, message_id: str, profile: str) -> None: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
91 client = self.host.get_client(profile) |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 return defer.ensureDeferred( |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 self.retract(client, message_id) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 ) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
96 def retract_by_origin_id( |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 self, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 client: SatXMPPEntity, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 dest_jid: jid.JID, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 origin_id: str |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 ) -> None: |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 """Send a message retraction using origin-id |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 [retract] should be prefered: internal ID should be used as it is independant of |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 XEPs changes. However, in some case messages may not be stored in database |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 (notably for some components), and then this method can be used |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 @param origin_id: origin-id as specified in XEP-0359 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 """ |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 message_elt = domish.Element((None, "message")) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 message_elt["from"] = client.jid.full() |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 message_elt["to"] = dest_jid.full() |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
112 apply_to_elt = self._f.apply_to_elt(message_elt, origin_id) |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 apply_to_elt.addElement((NS_MESSAGE_RETRACT, "retract")) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
114 self.host.plugins["XEP-0428"].add_fallback_elt( |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 message_elt, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 "[A message retraction has been requested, but your client doesn't support " |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 "it]" |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
119 self._h.add_hint_elements(message_elt, [self._h.HINT_STORE]) |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 client.send(message_elt) |
6952a002abc7
plugin XEP-424: Message Retractation 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:
3887
diff
changeset
|
122 async def retract_by_history( |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 self, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 client: SatXMPPEntity, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 history: History |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 ) -> None: |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 """Send a message retraction using History instance |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 This method is to use instead of [retract] when the history instance is already |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 retrieved. Note that the instance must have messages and subjets loaded |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 @param history: history instance of the message to retract |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 """ |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 try: |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 origin_id = history.origin_id |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 except KeyError: |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 raise exceptions.FeatureNotFound( |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 f"message to retract doesn't have the necessary origin-id, the sending " |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 "client is probably not supporting message retraction." |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 ) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
141 self.retract_by_origin_id(client, history.dest_jid, origin_id) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
142 await self.retract_db_history(client, history) |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 async def retract( |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 self, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 client: SatXMPPEntity, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 message_id: str, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 ) -> None: |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 """Send a message retraction request |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 @param message_id: ID of the message |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 This ID is the Libervia internal ID of the message. It will be retrieve from |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 database to find the ID used by XMPP (i.e. XEP-0359's "origin ID"). If the |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 message is not found in database, an exception will be raised |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 """ |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 if not message_id: |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 raise ValueError("message_id can't be empty") |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 history = await self.host.memory.storage.get( |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 client, History, History.uid, message_id, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 joined_loads=[History.messages, History.subjects] |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 ) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 if history is None: |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 raise exceptions.NotFound( |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 f"message to retract not found in database ({message_id})" |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
166 await self.retract_by_history(client, history) |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
168 async def retract_db_history(self, client, history: History) -> None: |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 """Mark an history instance in database as retracted |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 @param history: history instance |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 "messages" and "subjects" must be loaded too |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 """ |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 # FIXME: should be keep history? This is useful to check why a message has been |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 # retracted, but if may be bad if the user think it's really deleted |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 # we assign a new object to be sure to trigger an update |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 history.extra = deepcopy(history.extra) if history.extra else {} |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 history.extra["retracted"] = True |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
179 keep_history = self.host.memory.param_get_a( |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 NAME, CATEGORY, profile_key=client.profile |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 ) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 old_version: Dict[str, Any] = { |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 "timestamp": time.time() |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 } |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 if keep_history: |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 old_version.update({ |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 "messages": [m.serialise() for m in history.messages], |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 "subjects": [s.serialise() for s in history.subjects] |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 }) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 history.extra.setdefault("old_versions", []).append(old_version) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 await self.host.memory.storage.delete( |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 history.messages + history.subjects, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 session_add=[history] |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 ) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
197 async def _message_received_trigger( |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 self, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 client: SatXMPPEntity, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 message_elt: domish.Element, |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 post_treat: defer.Deferred |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 ) -> bool: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
203 fastened_elts = await self._f.get_fastened_elts(client, message_elt) |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 if fastened_elts is None: |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 return True |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 for elt in fastened_elts.elements: |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 if elt.name == "retract" and elt.uri == NS_MESSAGE_RETRACT: |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 if fastened_elts.history is not None: |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 source_jid = fastened_elts.history.source_jid |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 from_jid = jid.JID(message_elt["from"]) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 if source_jid.userhostJID() != from_jid.userhostJID(): |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 log.warning( |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 f"Received message retraction from {from_jid.full()}, but " |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 f"the message to retract is from {source_jid.full()}. This " |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 f"maybe a hack attempt.\n{message_elt.toXml()}" |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 ) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 return False |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 break |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 else: |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 return True |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
221 if not await self.host.trigger.async_point( |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 "XEP-0424_retractReceived", client, message_elt, elt, fastened_elts |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 ): |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 return False |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 if fastened_elts.history is None: |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 # we check history after the trigger because we may be in a component which |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 # doesn't store messages in database. |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 log.warning( |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 f"No message found with given origin-id: {message_elt.toXml()}" |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 ) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 return False |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 log.info(f"[{client.profile}] retracting message {fastened_elts.id!r}") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3887
diff
changeset
|
233 await self.retract_db_history(client, fastened_elts.history) |
3801
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 # TODO: send bridge signal |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 return False |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 @implementer(disco.IDisco) |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 class XEP_0424_handler(xmlstream.XMPPHandler): |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 def getDiscoInfo(self, __, target, nodeIdentifier=""): |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 return [disco.DiscoFeature(NS_MESSAGE_RETRACT)] |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
244 |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
6952a002abc7
plugin XEP-424: Message Retractation implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
246 return [] |