annotate libervia/backend/plugins/plugin_xep_0334.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 3b3cd9453d9b
children
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
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
3
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # SAT plugin for Delayed Delivery (XEP-0334)
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
7
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # This program is free software: you can redistribute it and/or modify
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # it under the terms of the GNU Affero General Public License as published by
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # the Free Software Foundation, either version 3 of the License, or
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
11 # (at your option) any later version.
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
12
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # This program is distributed in the hope that it will be useful,
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
16 # GNU Affero General Public License for more details.
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
17
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # You should have received a copy of the GNU Affero General Public License
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
20
3911
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3830
diff changeset
21 from typing import Iterable
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
22 from libervia.backend.core.i18n import _, D_
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
23 from libervia.backend.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
24
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
25 log = getLogger(__name__)
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
26 from libervia.backend.core.constants import Const as C
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.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.tools.common import data_format
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
29
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
30 from wokkel import disco, iwokkel
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
31
2698
5060cbeec01e core: minor style/typos fixes
Goffi <goffi@goffi.org>
parents: 2652
diff changeset
32 from twisted.words.protocols.jabber import xmlstream
3911
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3830
diff changeset
33 from twisted.words.xish import domish
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
34 from zope.interface import implementer
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
35 from textwrap import dedent
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
36
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
37
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
38 PLUGIN_INFO = {
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
39 C.PI_NAME: "Message Processing Hints",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
40 C.PI_IMPORT_NAME: "XEP-0334",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
41 C.PI_TYPE: "XEP",
3830
68a11b95a7d3 plugin XEP-0372: References implementation:
Goffi <goffi@goffi.org>
parents: 3479
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-0334"],
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
44 C.PI_MAIN: "XEP_0334",
3028
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: D_("""Implementation of Message Processing Hints"""),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
47 C.PI_USAGE: dedent(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
48 D_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
49 """\
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
50 Frontends can use HINT_* constants in mess_data['extra'] in a serialized 'hints' dict.
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3923
diff changeset
51 Internal plugins can use directly add_hint([HINT_* constant]).
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
52 Will set mess_data['extra']['history'] to 'skipped' when no store is requested and message is not saved in history."""
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
53 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
54 ),
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
55 }
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
56
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
57 NS_HINTS = "urn:xmpp:hints"
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
58
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
59
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
60 class XEP_0334(object):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
61 HINT_NO_PERMANENT_STORE = "no-permanent-store"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
62 HINT_NO_STORE = "no-store"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
63 HINT_NO_COPY = "no-copy"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
64 HINT_STORE = "store"
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
65 HINTS = (HINT_NO_PERMANENT_STORE, HINT_NO_STORE, HINT_NO_COPY, HINT_STORE)
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
66
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
67 def __init__(self, host):
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
68 log.info(_("Message Processing Hints plugin initialization"))
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
69 self.host = host
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3923
diff changeset
70 host.trigger.add("sendMessage", self.send_message_trigger)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
71 host.trigger.add(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
72 "message_received", self.message_received_trigger, priority=-1000
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
73 )
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
74
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3923
diff changeset
75 def get_handler(self, client):
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
76 return XEP_0334_handler()
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
77
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3923
diff changeset
78 def add_hint(self, mess_data, hint):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
79 if hint == self.HINT_NO_COPY and not mess_data["to"].resource:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
80 log.error(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
81 "{hint} can only be used with full jids! Ignoring it.".format(hint=hint)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
82 )
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
83 return
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
84 hints = mess_data.setdefault("hints", set())
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
85 if hint in self.HINTS:
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
86 hints.add(hint)
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
87 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
88 log.error("Unknown hint: {}".format(hint))
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
89
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
90 def add_hint_elements(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
91 self, message_elt: domish.Element, hints: Iterable[str]
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
92 ) -> None:
2652
baccc27d5c5c plugin XEP-0334: added a method to add hint directly to elements (i.e. on domish.Element instead of message data)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
93 """Add hints elements to message stanza
baccc27d5c5c plugin XEP-0334: added a method to add hint directly to elements (i.e. on domish.Element instead of message data)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
94
3911
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3830
diff changeset
95 @param message_elt: stanza where hints must be added
8289ac1b34f4 plugin XEP-0384: Fully reworked to adjust to the reworked python-omemo:
Syndace <me@syndace.dev>
parents: 3830
diff changeset
96 @param hints: hints to add
2652
baccc27d5c5c plugin XEP-0334: added a method to add hint directly to elements (i.e. on domish.Element instead of message data)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
97 """
baccc27d5c5c plugin XEP-0334: added a method to add hint directly to elements (i.e. on domish.Element instead of message data)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
98 for hint in hints:
3923
13a2403774d4 plugin XEP-0334: avoid adding twice the same hint:
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
99 if not list(message_elt.elements(NS_HINTS, hint)):
13a2403774d4 plugin XEP-0334: avoid adding twice the same hint:
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
100 message_elt.addElement((NS_HINTS, hint))
13a2403774d4 plugin XEP-0334: avoid adding twice the same hint:
Goffi <goffi@goffi.org>
parents: 3911
diff changeset
101 else:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4163
diff changeset
102 log.debug(f"Not adding {hint!r} hint: it is already present in <message>")
2652
baccc27d5c5c plugin XEP-0334: added a method to add hint directly to elements (i.e. on domish.Element instead of message data)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
103
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3923
diff changeset
104 def _send_post_xml_treatment(self, mess_data):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
105 if "hints" in mess_data:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3923
diff changeset
106 self.add_hint_elements(mess_data["xml"], mess_data["hints"])
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
107 return mess_data
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
108
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3923
diff changeset
109 def send_message_trigger(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
110 self, client, mess_data, pre_xml_treatments, post_xml_treatments
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
111 ):
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
112 """Add the hints element to the message to be sent"""
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
113 if "hints" in mess_data["extra"]:
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
114 for hint in data_format.dict2iter("hints", mess_data["extra"], pop=True):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3923
diff changeset
115 self.add_hint(hint)
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
116
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3923
diff changeset
117 post_xml_treatments.addCallback(self._send_post_xml_treatment)
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
118 return True
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
119
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3923
diff changeset
120 def _received_skip_history(self, mess_data):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
121 mess_data["history"] = C.HISTORY_SKIP
2132
c0577837680a core: replaced SkipHistory exception by a key in mess_data:
Goffi <goffi@goffi.org>
parents: 2131
diff changeset
122 return mess_data
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
123
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3923
diff changeset
124 def message_received_trigger(self, client, message_elt, post_treat):
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
125 """Check for hints in the received message"""
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
126 for elt in message_elt.elements():
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
127 if elt.uri == NS_HINTS and elt.name in (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
128 self.HINT_NO_PERMANENT_STORE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
129 self.HINT_NO_STORE,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
130 ):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
131 log.debug("history will be skipped for this message, as requested")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3923
diff changeset
132 post_treat.addCallback(self._received_skip_history)
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
133 break
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
134 return True
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
135
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
136
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
137 @implementer(iwokkel.IDisco)
2698
5060cbeec01e core: minor style/typos fixes
Goffi <goffi@goffi.org>
parents: 2652
diff changeset
138 class XEP_0334_handler(xmlstream.XMPPHandler):
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
139
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
140 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
2131
628c1c95f442 plugin XEP-0334: fixed and improved message processing hints:
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
141 return [disco.DiscoFeature(NS_HINTS)]
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
142
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
143 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
1279
d84905c3e124 plugin XEP-0334: first draft
souliane <souliane@mailoo.org>
parents:
diff changeset
144 return []