annotate libervia/backend/plugins/plugin_xep_0033.py @ 4151:18026ce0819c

core (xmpp): message reception workflow refactoring: - Call methods from a root async one instead of using Deferred callbacks chain. - Use a queue to be sure to process messages in order.
author Goffi <goffi@goffi.org>
date Wed, 22 Nov 2023 14:50:35 +0100
parents 4b842c1fb686
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: 2765
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
2
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
3
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
4 # SAT plugin for Extended Stanza Addressing (xep-0033)
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1552
diff changeset
5 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
6
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
10 # (at your option) any later version.
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
11
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
16
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
19
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
20 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
21 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
22 from libervia.backend.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
23
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
24 log = getLogger(__name__)
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
25 from libervia.backend.core import exceptions
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
26 from wokkel import disco, iwokkel
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
27 from zope.interface import implementer
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
28 from twisted.words.protocols.jabber.jid import JID
1052
e88bff4c8b77 core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents: 999
diff changeset
29 from twisted.python import failure
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
30 import copy
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
31
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
32 try:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
33 from twisted.words.protocols.xmlstream import XMPPHandler
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
34 except ImportError:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
35 from wokkel.subprotocols import XMPPHandler
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
36 from twisted.words.xish import domish
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
37 from twisted.internet import defer
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
38
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4051
diff changeset
39 from libervia.backend.tools import trigger
749
192b804ee446 plugin XEP-0033: bug fix for sending messages with the addressing feature to several servers
souliane <souliane@mailoo.org>
parents: 742
diff changeset
40 from time import time
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
41
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
42 # TODO: fix Prosody "addressing" plugin to leave the concerned bcc according to the spec:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
43 #
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
44 # http://xmpp.org/extensions/xep-0033.html#addr-type-bcc
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
45 # "This means that the server MUST remove these addresses before the stanza is delivered to anyone other than the given bcc addressee or the multicast service of the bcc addressee."
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
46 #
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
47 # http://xmpp.org/extensions/xep-0033.html#multicast
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
48 # "Each 'bcc' recipient MUST receive only the <address type='bcc'/> associated with that addressee."
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
49
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
50 # TODO: fix Prosody "addressing" plugin to determine itself if remote servers supports this XEP
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
51
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
52
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
53 NS_XMPP_CLIENT = "jabber:client"
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
54 NS_ADDRESS = "http://jabber.org/protocol/address"
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
55 ATTRIBUTES = ["jid", "uri", "node", "desc", "delivered", "type"]
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
56 ADDRESS_TYPES = ["to", "cc", "bcc", "replyto", "replyroom", "noreply"]
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
57
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
58 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
59 C.PI_NAME: "Extended Stanza Addressing Protocol Plugin",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
60 C.PI_IMPORT_NAME: "XEP-0033",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
61 C.PI_TYPE: "XEP",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
62 C.PI_PROTOCOLS: ["XEP-0033"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
63 C.PI_DEPENDENCIES: [],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
64 C.PI_MAIN: "XEP_0033",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
65 C.PI_HANDLER: "yes",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
66 C.PI_DESCRIPTION: _("""Implementation of Extended Stanza Addressing"""),
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
67 }
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
68
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
69
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
70 class XEP_0033(object):
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
71 """
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
72 Implementation for XEP 0033
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
73 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
74
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
75 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 922
diff changeset
76 log.info(_("Extended Stanza Addressing plugin initialization"))
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
77 self.host = host
749
192b804ee446 plugin XEP-0033: bug fix for sending messages with the addressing feature to several servers
souliane <souliane@mailoo.org>
parents: 742
diff changeset
78 self.internal_data = {}
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
79 host.trigger.add(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
80 "sendMessage", self.send_message_trigger, trigger.TriggerManager.MIN_PRIORITY
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
81 )
4051
c23cad65ae99 core: renamed `messageReceived` trigger to `message_received`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
82 host.trigger.add("message_received", self.message_received_trigger)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
83
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
84 def send_message_trigger(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
85 self, client, mess_data, pre_xml_treatments, post_xml_treatments
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
86 ):
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
87 """Process the XEP-0033 related data to be sent"""
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
88 profile = client.profile
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
89
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
90 def treatment(mess_data):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
91 if not "address" in mess_data["extra"]:
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
92 return mess_data
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
93
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
94 def disco_callback(entities):
999
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
95 if not entities:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
96 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
97 _("XEP-0033 is being used but the server doesn't support it!")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
98 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
99 raise failure.Failure(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
100 exceptions.CancelError("Cancelled by XEP-0033")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
101 )
999
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
102 if mess_data["to"] not in entities:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
103 expected = _(" or ").join([entity.userhost() for entity in entities])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
104 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
105 _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
106 "Stanzas using XEP-0033 should be addressed to %(expected)s, not %(current)s!"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
107 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
108 % {"expected": expected, "current": mess_data["to"]}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
109 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
110 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
111 _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
112 "TODO: addressing has been fixed by the backend... fix it in the frontend!"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
113 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
114 )
999
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
115 mess_data["to"] = list(entities)[0].userhostJID()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
116 element = mess_data["xml"].addElement("addresses", NS_ADDRESS)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
117 entries = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
118 entry.split(":")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
119 for entry in mess_data["extra"]["address"].split("\n")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
120 if entry != ""
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
121 ]
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
122 for type_, jid_ in entries:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
123 element.addChild(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
124 domish.Element(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
125 (None, "address"), None, {"type": type_, "jid": jid_}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
126 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
127 )
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
128 # when the prosody plugin is completed, we can immediately return mess_data from here
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
129 self.send_and_store_message(mess_data, entries, profile)
1052
e88bff4c8b77 core (XMPP): sendMessage refactoring:
Goffi <goffi@goffi.org>
parents: 999
diff changeset
130 log.debug("XEP-0033 took over")
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
131 raise failure.Failure(exceptions.CancelError("Cancelled by XEP-0033"))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
132
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
133 d = self.host.find_features_set(client, [NS_ADDRESS])
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
134 d.addCallbacks(disco_callback, lambda __: disco_callback(None))
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
135 return d
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
136
922
c897c8d321b3 core: sendMessageTrigger now manage pre and post treatments, which happen before or after XML generation
Goffi <goffi@goffi.org>
parents: 844
diff changeset
137 post_xml_treatments.addCallback(treatment)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
138 return True
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
139
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
140 def send_and_store_message(self, mess_data, entries, profile):
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
141 """Check if target servers support XEP-0033, send and store the messages
750
c8b9f675ac17 plugin XEP-0033: avoid the controlled error to explode (use return Failure(...) instead of raise)
souliane <souliane@mailoo.org>
parents: 749
diff changeset
142 @return: a friendly failure to let the core know that we sent the message already
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
143
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
144 Later we should be able to remove this method because:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
145 # XXX: sending the messages should be done by the local server
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
146 # FIXME: for now we duplicate the messages in the history for each recipient, this should change
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
147 # FIXME: for now we duplicate the echoes to the sender, this should also change
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
148 Ideas:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
149 - fix Prosody plugin to check if target server support the feature
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
150 - redesign the database to save only one entry to the database
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
151 - change the message_new signal to eventually pass more than one recipient
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
152 """
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
153 client = self.host.get_client(profile)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
154
1205
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
155 def send(mess_data, skip_send=False):
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
156 d = defer.Deferred()
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
157 if not skip_send:
3913
944f51f9c2b4 core (xmpp): make `send` a blocking method, fix `sendMessageData` calls:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
158 d.addCallback(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
159 lambda ret: defer.ensureDeferred(client.send_message_data(ret))
3913
944f51f9c2b4 core (xmpp): make `send` a blocking method, fix `sendMessageData` calls:
Goffi <goffi@goffi.org>
parents: 3541
diff changeset
160 )
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
161 d.addCallback(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
162 lambda ret: defer.ensureDeferred(client.message_add_to_history(ret))
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3172
diff changeset
163 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
164 d.addCallback(client.message_send_to_bridge)
1205
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
165 d.addErrback(lambda failure: failure.trap(exceptions.CancelError))
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
166 return d.callback(mess_data)
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
167
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
168 def disco_callback(entities, to_jid_s):
999
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
169 history_data = copy.deepcopy(mess_data)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
170 history_data["to"] = JID(to_jid_s)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
171 history_data["xml"]["to"] = to_jid_s
999
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
172 if entities:
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
173 if entities not in self.internal_data[timestamp]:
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
174 sent_data = copy.deepcopy(mess_data)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
175 sent_data["to"] = JID(JID(to_jid_s).host)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
176 sent_data["xml"]["to"] = JID(to_jid_s).host
1205
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
177 send(sent_data)
999
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
178 self.internal_data[timestamp].append(entities)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
179 # we still need to fill the history and signal the echo...
1205
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
180 send(history_data, skip_send=True)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
181 else:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
182 # target server misses the addressing feature
1205
3e234902177a plugin XEP-0033: fixes sending the message after the changes made in rev 1052 (e88bff4c8b77)
souliane <souliane@mailoo.org>
parents: 1052
diff changeset
183 send(history_data)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
184
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
185 def errback(failure, to_jid):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
186 disco_callback(None, to_jid)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
187
749
192b804ee446 plugin XEP-0033: bug fix for sending messages with the addressing feature to several servers
souliane <souliane@mailoo.org>
parents: 742
diff changeset
188 timestamp = time()
192b804ee446 plugin XEP-0033: bug fix for sending messages with the addressing feature to several servers
souliane <souliane@mailoo.org>
parents: 742
diff changeset
189 self.internal_data[timestamp] = []
192b804ee446 plugin XEP-0033: bug fix for sending messages with the addressing feature to several servers
souliane <souliane@mailoo.org>
parents: 742
diff changeset
190 defer_list = []
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
191 for type_, jid_ in entries:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
192 d = defer.Deferred()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
193 d.addCallback(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
194 self.host.find_features_set, client=client, jid_=JID(JID(jid_).host)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
195 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
196 d.addCallbacks(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
197 disco_callback, errback, callbackArgs=[jid_], errbackArgs=[jid_]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
198 )
999
c37a24922f27 plugin XEP_0033: fixes the server part and the tests
souliane <souliane@mailoo.org>
parents: 993
diff changeset
199 d.callback([NS_ADDRESS])
749
192b804ee446 plugin XEP-0033: bug fix for sending messages with the addressing feature to several servers
souliane <souliane@mailoo.org>
parents: 742
diff changeset
200 defer_list.append(d)
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
201 d = defer.Deferred().addCallback(lambda __: self.internal_data.pop(timestamp))
749
192b804ee446 plugin XEP-0033: bug fix for sending messages with the addressing feature to several servers
souliane <souliane@mailoo.org>
parents: 742
diff changeset
202 defer.DeferredList(defer_list).chainDeferred(d)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
203
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
204 def message_received_trigger(self, client, message, post_treat):
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
205 """In order to save the addressing information in the history"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
206
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
207 def post_treat_addr(data, addresses):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
208 data["extra"]["addresses"] = ""
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
209 for address in addresses:
787
dd656d745d6a test: added tests for XEP-0033
souliane <souliane@mailoo.org>
parents: 785
diff changeset
210 # Depending how message has been constructed, we could get here
dd656d745d6a test: added tests for XEP-0033
souliane <souliane@mailoo.org>
parents: 785
diff changeset
211 # some noise like "\n " instead of an address element.
dd656d745d6a test: added tests for XEP-0033
souliane <souliane@mailoo.org>
parents: 785
diff changeset
212 if isinstance(address, domish.Element):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
213 data["extra"]["addresses"] += "%s:%s\n" % (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
214 address["type"],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
215 address["jid"],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
216 )
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
217 return data
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
218
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
219 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
220 addresses = next(message.elements(NS_ADDRESS, "addresses"))
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
221 except StopIteration:
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
222 pass # no addresses
1955
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
223 else:
633b5c21aefd backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
224 post_treat.addCallback(post_treat_addr, addresses.children)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
225 return True
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
226
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3913
diff changeset
227 def get_handler(self, client):
2144
1d3f73e065e1 core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents: 2138
diff changeset
228 return XEP_0033_handler(self, client.profile)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
229
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
230
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
231 @implementer(iwokkel.IDisco)
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
232 class XEP_0033_handler(XMPPHandler):
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
233
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
234 def __init__(self, plugin_parent, profile):
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
235 self.plugin_parent = plugin_parent
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
236 self.host = plugin_parent.host
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
237 self.profile = profile
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
238
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
239 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
240 return [disco.DiscoFeature(NS_ADDRESS)]
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
241
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
242 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
742
03744d9ebc13 plugin XEP-0033: implementation of the addressing feature:
souliane <souliane@mailoo.org>
parents:
diff changeset
243 return []