Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_xep_0359.py @ 4094:c3b68fdc2de7
component AP gateway: fix handling of XMPP comments authors:
the gateway was supposing that comments where emitted from PEP of author. While this is
the case for most blog posts, it's not for comments. Instead the component is now using
`author_jid` which is retrieved by XEP-0277 plugin, and reject the item if the auhor is
not verified (i.e. if `publisher` attribute is not set by XMPP service).
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 12 Jun 2023 14:50:43 +0200 |
parents | 4b842c1fb686 |
children | 2729d424dee7 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
2696
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT plugin for Message Archive Management (XEP-0359) |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
2696
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org) |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # This program is free software: you can redistribute it and/or modify |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # it under the terms of the GNU Affero General Public License as published by |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # the Free Software Foundation, either version 3 of the License, or |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # (at your option) any later version. |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # This program is distributed in the hope that it will be useful, |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # GNU Affero General Public License for more details. |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # You should have received a copy of the GNU Affero General Public License |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 |
3911
8289ac1b34f4
plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents:
3797
diff
changeset
|
21 from typing import Optional |
3103 | 22 import uuid |
23 from zope.interface import implementer | |
24 from twisted.words.protocols.jabber import xmlstream | |
25 from wokkel import disco | |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
26 from libervia.backend.core.constants import Const as C |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
27 from libervia.backend.core import exceptions |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
28 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
29 from libervia.backend.core.log import getLogger |
3911
8289ac1b34f4
plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents:
3797
diff
changeset
|
30 from twisted.words.xish import domish |
2696
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 log = getLogger(__name__) |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 PLUGIN_INFO = { |
3028 | 36 C.PI_NAME: "Unique and Stable Stanza IDs", |
37 C.PI_IMPORT_NAME: "XEP-0359", | |
38 C.PI_TYPE: "XEP", | |
39 C.PI_PROTOCOLS: ["XEP-0359"], | |
40 C.PI_MAIN: "XEP_0359", | |
41 C.PI_HANDLER: "yes", | |
42 C.PI_DESCRIPTION: _("""Implementation of Unique and Stable Stanza IDs"""), | |
2696
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 } |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 |
3028 | 45 NS_SID = "urn:xmpp:sid:0" |
2696
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 class XEP_0359(object): |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 def __init__(self, host): |
3028 | 51 log.info(_("Unique and Stable Stanza IDs plugin initialization")) |
2696
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 self.host = host |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
53 host.register_namespace("stanza_id", NS_SID) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
54 host.trigger.add("message_parse", self._message_parse_trigger) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
55 host.trigger.add("send_message_data", self._send_message_data_trigger) |
2696
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
57 def _message_parse_trigger(self, client, message_elt, mess_data): |
2696
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 """Check if message has a stanza-id""" |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
59 stanza_id = self.get_stanza_id(message_elt, client.jid.userhostJID()) |
2696
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 if stanza_id is not None: |
3028 | 61 mess_data['extra']['stanza_id'] = stanza_id |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
62 origin_id = self.get_origin_id(message_elt) |
3797
cc653b2685f0
core (memory/sqla), plugin XEP-0359: always add `origin-id`, and store:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
63 if origin_id is not None: |
cc653b2685f0
core (memory/sqla), plugin XEP-0359: always add `origin-id`, and store:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
64 mess_data['extra']['origin_id'] = origin_id |
2696
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 return True |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
67 def _send_message_data_trigger(self, client, mess_data): |
3797
cc653b2685f0
core (memory/sqla), plugin XEP-0359: always add `origin-id`, and store:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
68 origin_id = mess_data["extra"].get("origin_id") |
cc653b2685f0
core (memory/sqla), plugin XEP-0359: always add `origin-id`, and store:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
69 if not origin_id: |
cc653b2685f0
core (memory/sqla), plugin XEP-0359: always add `origin-id`, and store:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
70 origin_id = str(uuid.uuid4()) |
cc653b2685f0
core (memory/sqla), plugin XEP-0359: always add `origin-id`, and store:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
71 mess_data["extra"]["origin_id"] = origin_id |
cc653b2685f0
core (memory/sqla), plugin XEP-0359: always add `origin-id`, and store:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
72 message_elt = mess_data["xml"] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
73 self.add_origin_id(message_elt, origin_id) |
3797
cc653b2685f0
core (memory/sqla), plugin XEP-0359: always add `origin-id`, and store:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
74 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
75 def get_stanza_id(self, element, by): |
2696
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 """Return stanza-id if found in element |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 @param element(domish.Element): element to parse |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 @param by(jid.JID): entity which should have set a stanza-id |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 @return (unicode, None): stanza-id if found |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 """ |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 stanza_id = None |
3028 | 83 for stanza_elt in element.elements(NS_SID, "stanza-id"): |
84 if stanza_elt.getAttribute("by") == by.full(): | |
2696
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 if stanza_id is not None: |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 # we must not have more than one element (§3 #4) |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 raise exceptions.DataError( |
3028 | 88 "More than one corresponding stanza-id found!") |
89 stanza_id = stanza_elt.getAttribute("id") | |
2696
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 # we don't break to be sure that there is no more than one element |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 # with this "by" attribute |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 return stanza_id |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
95 def add_stanza_id(self, client, element, stanza_id, by=None): |
2696
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 """Add a <stanza-id/> to a stanza |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 @param element(domish.Element): stanza where the <stanza-id/> must be added |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 @param stanza_id(unicode): id to use |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 @param by(jid.JID, None): jid to use or None to use client.jid |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 """ |
3028 | 102 sid_elt = element.addElement((NS_SID, "stanza-id")) |
103 sid_elt["by"] = client.jid.userhost() if by is None else by.userhost() | |
104 sid_elt["id"] = stanza_id | |
2696
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
106 def get_origin_id(self, element: domish.Element) -> Optional[str]: |
3103 | 107 """Return origin-id if found in element |
108 | |
3911
8289ac1b34f4
plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents:
3797
diff
changeset
|
109 @param element: element to parse |
8289ac1b34f4
plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents:
3797
diff
changeset
|
110 @return: origin-id if found |
3103 | 111 """ |
112 try: | |
113 origin_elt = next(element.elements(NS_SID, "origin-id")) | |
114 except StopIteration: | |
115 return None | |
116 else: | |
117 return origin_elt.getAttribute("id") | |
118 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
119 def add_origin_id(self, element, origin_id=None): |
3103 | 120 """Add a <origin-id/> to a stanza |
121 | |
122 @param element(domish.Element): stanza where the <origin-id/> must be added | |
123 @param origin_id(str): id to use, None to automatically generate | |
124 @return (str): origin_id | |
125 """ | |
126 if origin_id is None: | |
127 origin_id = str(uuid.uuid4()) | |
128 sid_elt = element.addElement((NS_SID, "origin-id")) | |
129 sid_elt["id"] = origin_id | |
130 return origin_id | |
131 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3911
diff
changeset
|
132 def get_handler(self, client): |
2696
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 return XEP_0359_handler() |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 |
3028 | 136 @implementer(disco.IDisco) |
2696
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 class XEP_0359_handler(xmlstream.XMPPHandler): |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 return [disco.DiscoFeature(NS_SID)] |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
5849dcaab99d
plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 return [] |