annotate libervia/backend/plugins/plugin_xep_0359.py @ 4212:5f2d496c633f

core: get rid of `pickle`: Use of `pickle` to serialise data was a technical legacy that was causing trouble to store in database, to update (if a class was serialised, a change could break update), and to security (pickle can lead to code execution). This patch remove all use of Pickle in favour in JSON, notably: - for caching data, a Pydantic model is now used instead - for SQLAlchemy model, the LegacyPickle is replaced by JSON serialisation - in XEP-0373 a class `PublicKeyMetadata` was serialised. New method `from_dict` and `to_dict` method have been implemented to do serialisation. - new methods to (de)serialise data can now be specified with Identity data types. It is notably used to (de)serialise `path` of avatars. A migration script has been created to convert data (for upgrade or downgrade), with special care for XEP-0373 case. Depending of size of database, this migration script can be long to run. rel 443
author Goffi <goffi@goffi.org>
date Fri, 23 Feb 2024 13:31:04 +0100
parents 9cda0347e0ac
children 0d7bb4df2343
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
4197
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
3 # Libervia plugin for Message Archive Management (XEP-0359)
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
4 # Copyright (C) 2009-2023 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
5 # 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
6
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # 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
8 # 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
9 # 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
10 # (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
11
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # 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
13 # 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
14 # 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
15 # 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
16
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # 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
18 # 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
19
3911
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3797
diff changeset
20 from typing import Optional
3103
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
21 import uuid
4197
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
22
3103
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
23 from twisted.words.protocols.jabber import xmlstream
4197
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
24 from twisted.words.xish import domish
3103
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
25 from wokkel import disco
4197
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
26 from zope.interface import implementer
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
27
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
28 from libervia.backend.core import exceptions
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
29 from libervia.backend.core.constants import Const as C
4197
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
30 from libervia.backend.core.core_types import SatXMPPEntity
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
31 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
32 from libervia.backend.core.log import getLogger
4197
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
33 from libervia.backend.models.core import MessageData
2696
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 log = getLogger(__name__)
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38 PLUGIN_INFO = {
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
39 C.PI_NAME: "Unique and Stable Stanza IDs",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
40 C.PI_IMPORT_NAME: "XEP-0359",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
41 C.PI_TYPE: "XEP",
4183
6784d07b99c8 plugin XEP-053, component AP gateway: use the new `trigger.add_with_check` method
Goffi <goffi@goffi.org>
parents: 4156
diff changeset
42 C.PI_MODES: C.PLUG_MODE_BOTH,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
43 C.PI_PROTOCOLS: ["XEP-0359"],
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
44 C.PI_MAIN: "XEP_0359",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
45 C.PI_HANDLER: "yes",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
46 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
47 }
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
49 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
50
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 class XEP_0359(object):
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def __init__(self, host):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
55 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
56 self.host = host
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
57 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
58 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
59 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
60
4197
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
61 def _message_parse_trigger(
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
62 self,
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
63 client: SatXMPPEntity,
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
64 message_elt: domish.Element,
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
65 mess_data: MessageData
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
66 ) -> bool:
2696
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 """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
68 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
69 if stanza_id is not None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
70 mess_data['extra']['stanza_id'] = stanza_id
4197
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
71 try:
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
72 origin_id = self.get_origin_id(message_elt) or message_elt["id"]
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
73 except KeyError:
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
74 pass
9cda0347e0ac plugin XEP-0359: if no origin ID is found, use <message> ID instead:
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
75 else:
3797
cc653b2685f0 core (memory/sqla), plugin XEP-0359: always add `origin-id`, and store:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
76 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
77 return True
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
79 def _send_message_data_trigger(self, client, mess_data):
4156
2729d424dee7 plugin XEP-0359: use same ID as <message> for `origin_id`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
80 origin_id = mess_data["extra"].setdefault("origin_id", mess_data.get("uid"))
3797
cc653b2685f0 core (memory/sqla), plugin XEP-0359: always add `origin-id`, and store:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
81 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
82 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
83 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
84 message_elt = mess_data["xml"]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
85 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
86
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
87 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
88 """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
89
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 @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
91 @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
92 @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
93 """
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
94 stanza_id = None
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
95 for stanza_elt in element.elements(NS_SID, "stanza-id"):
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
96 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
97 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
98 # 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
99 raise exceptions.DataError(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
100 "More than one corresponding stanza-id found!")
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
101 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
102 # 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
103 # with this "by" attribute
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
105 return stanza_id
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
106
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
107 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
108 """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
109
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
110 @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
111 @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
112 @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
113 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
114 sid_elt = element.addElement((NS_SID, "stanza-id"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
115 sid_elt["by"] = client.jid.userhost() if by is None else by.userhost()
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
116 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
117
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
118 def get_origin_id(self, element: domish.Element) -> Optional[str]:
3103
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
119 """Return origin-id if found in element
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
120
3911
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3797
diff changeset
121 @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
122 @return: origin-id if found
3103
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
123 """
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
124 try:
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
125 origin_elt = next(element.elements(NS_SID, "origin-id"))
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
126 except StopIteration:
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
127 return None
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
128 else:
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
129 return origin_elt.getAttribute("id")
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
130
4156
2729d424dee7 plugin XEP-0359: use same ID as <message> for `origin_id`:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
131 def add_origin_id(self, element: domish.Element, origin_id: str) -> None:
3103
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
132 """Add a <origin-id/> to a stanza
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
133
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
134 @param element(domish.Element): stanza where the <origin-id/> must be added
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
135 @param origin_id(str): id to use, None to automatically generate
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
136 @return (str): origin_id
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
137 """
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
138 sid_elt = element.addElement((NS_SID, "origin-id"))
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
139 sid_elt["id"] = origin_id
f0f48b4deb35 plugin XEP-0359: handle origin-id
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
140
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
141 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
142 return XEP_0359_handler()
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
143
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
144
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
145 @implementer(disco.IDisco)
2696
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
146 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
147
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
148 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
149 return [disco.DiscoFeature(NS_SID)]
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
150
5849dcaab99d plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
151 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
152 return []