Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_comp_email_gateway/pubsub_service.py @ 4387:a6270030968d default tip
doc (components): Document the handling of mailing lists in Email Gateway:
fix 462
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 03 Aug 2025 23:45:48 +0200 |
parents | c055042c01e3 |
children |
rev | line source |
---|---|
4338
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # Libervia ActivityPub Gateway |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
4386
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
19 from typing import TYPE_CHECKING, cast |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
20 |
4338
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from twisted.internet import defer |
4386
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
22 from twisted.words.protocols.jabber import error, jid |
4338
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from twisted.words.xish import domish |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from wokkel import data_form, disco, pubsub, rsm |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 |
4386
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
26 from libervia.backend import G |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
27 from libervia.backend.core import exceptions |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
28 from libervia.backend.core.core_types import SatXMPPComponent |
4338
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 from libervia.backend.core.i18n import _ |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 from libervia.backend.core.log import getLogger |
4386
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
31 from libervia.backend.memory.sqla_mapping import AccessModel, Affiliation |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
32 from libervia.backend.plugins.plugin_pubsub_cache import PubsubCache |
4338
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 from libervia.backend.plugins.plugin_xep_0498 import NodeData |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 from libervia.backend.tools.utils import ensure_deferred |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 if TYPE_CHECKING: |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 from . import EmailGatewayComponent |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 log = getLogger(__name__) |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 # all nodes have the same config |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 NODE_CONFIG = [ |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 {"var": "pubsub#persist_items", "type": "boolean", "value": True}, |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 {"var": "pubsub#max_items", "value": "max"}, |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 {"var": "pubsub#access_model", "type": "list-single", "value": "open"}, |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 {"var": "pubsub#publish_model", "type": "list-single", "value": "open"}, |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 ] |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 NODE_CONFIG_VALUES = {c["var"]: c["value"] for c in NODE_CONFIG} |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 NODE_OPTIONS = {c["var"]: {} for c in NODE_CONFIG} |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 for c in NODE_CONFIG: |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 NODE_OPTIONS[c["var"]].update( |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 {k: v for k, v in c.items() if k not in ("var", "value")} |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 ) |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 class EmailGWPubsubResource(pubsub.PubSubResource): |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 def __init__(self, service: "EmailGWPubsubService") -> None: |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 self.gateway = service.gateway |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 self.host = self.gateway.host |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 self.service = service |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 self._pfs = service._pfs |
4386
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
64 self._ps_cache = cast(PubsubCache, G.host.plugins["PUBSUB_CACHE"]) |
4338
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 super().__init__() |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
4386
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
67 @property |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
68 def client(self) -> SatXMPPComponent: |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
69 client = self.gateway.client |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
70 assert client is not None |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
71 return client |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
72 |
4338
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 def getNodes( |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 self, requestor: jid.JID, service: jid.JID, nodeIdentifier: str |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 ) -> defer.Deferred[list[str]]: |
4386
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
76 return defer.ensureDeferred(self.get_nodes(requestor, service, nodeIdentifier)) |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
77 |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
78 async def get_nodes( |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
79 self, |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
80 requestor: jid.JID, |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
81 service: jid.JID, |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
82 node_id: str |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
83 ) -> list[str]: |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
84 nodes = await G.storage.get_pubsub_nodes(self.client, self.client.jid) |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
85 return [self._pfs.namespace] + [cast(str, node.name) for node in nodes] |
4338
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 @ensure_deferred |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 async def items( |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 self, |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 request: rsm.PubSubRequest, |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 ) -> tuple[list[domish.Element], rsm.RSMResponse | None]: |
4386
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
92 client = self.client |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
93 requestor_jid = request.sender.userhostJID() |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
94 if not client.is_local(requestor_jid): |
4338
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 raise error.StanzaError("forbidden") |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 if request.nodeIdentifier != self._pfs.namespace: |
4386
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
98 return await self.items_from_mailing_list(request, requestor_jid) |
4338
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 |
4386
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
100 files = await self.host.memory.get_files(client, requestor_jid) |
4338
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 node_data = NodeData.from_files_data(client.jid, files) |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 return node_data.to_elements(), None |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 |
4386
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
104 async def items_from_mailing_list( |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
105 self, |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
106 request: rsm.PubSubRequest, |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
107 requestor_jid: jid.JID |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
108 ) -> tuple[list[domish.Element], rsm.RSMResponse|None]: |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
109 """Handle items coming from mailing lists. |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
110 |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
111 @param request: Pubsub request. |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
112 @param requestor_jid: Bare jid of the requestor. |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
113 @return: Items matching request, if allowed, and RSM response. |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
114 @raise error.StanzaError: One of: |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
115 - ``item-not-found`` if no corresponding node or item if found |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
116 - ``forbidden`` if the requestor does not have sufficient privileges |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
117 """ |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
118 node = request.nodeIdentifier |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
119 node = await G.storage.get_pubsub_node( |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
120 self.client, |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
121 self.client.jid, |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
122 node, |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
123 with_affiliations=True |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
124 ) |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
125 if node is None: |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
126 raise error.StanzaError("item-not-found") |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
127 |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
128 match str(node.access_model): |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
129 case AccessModel.open: |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
130 pass |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
131 case AccessModel.whitelist: |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
132 for affiliation in node.affiliations: |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
133 if ( |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
134 affiliation.entity == requestor_jid |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
135 and affiliation.affiliation in { |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
136 Affiliation.owner, Affiliation.publisher, Affiliation.member |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
137 } |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
138 ): |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
139 break |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
140 else: |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
141 raise error.StanzaError("forbidden") |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
142 case _: |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
143 raise exceptions.InternalError( |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
144 f"Unmanaged access model: {node.access_model}" |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
145 ) |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
146 |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
147 pubsub_items, metadata = await self._ps_cache.get_items_from_cache( |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
148 self.client, |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
149 node, |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
150 request.maxItems, |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
151 request.itemIdentifiers, |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
152 request.subscriptionIdentifier, |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
153 request.rsm |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
154 ) |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
155 if rsm_data := metadata.get("rsm"): |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
156 rsm_response = rsm.RSMResponse(**rsm_data) |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
157 else: |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
158 rsm_response = None |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
159 return ( |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
160 [cast(domish.Element, ps_item.data) for ps_item in pubsub_items], |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
161 rsm_response |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
162 ) |
c055042c01e3
component Email gateway: Convert mailing list to pubsub nodes:
Goffi <goffi@goffi.org>
parents:
4339
diff
changeset
|
163 |
4338
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 @ensure_deferred |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 async def retract(self, request: rsm.PubSubRequest) -> None: |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 client = self.gateway.client |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 assert client is not None |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 sender = request.sender.userhostJID() |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 if not client.is_local(sender): |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 raise error.StanzaError("forbidden") |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 if request.nodeIdentifier != self._pfs.namespace: |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 raise error.StanzaError("bad-request") |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 for item_id in request.itemIdentifiers: |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 try: |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 # FIXME: item ID naming convention must be hanlded using dedicated methods |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 # in XEP-0498. |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 file_id = item_id.rsplit("_", 1)[1] |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 except IndexError: |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 file_id = "" |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 if not file_id: |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 raise error.StanzaError("bad-request") |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 # Ownership is checked by ``file_delete``, and payload deletion is done there |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 # too. |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 await self.host.memory.file_delete(client, sender.userhostJID(), file_id) |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 @ensure_deferred |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 async def subscribe(self, request: rsm.PubSubRequest): |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 raise rsm.Unsupported("subscribe") |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 @ensure_deferred |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 async def unsubscribe(self, request: rsm.PubSubRequest): |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 raise rsm.Unsupported("unsubscribe") |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 def getConfigurationOptions(self): |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 return NODE_OPTIONS |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 def getConfiguration( |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 self, requestor: jid.JID, service: jid.JID, nodeIdentifier: str |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 ) -> defer.Deferred: |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 return defer.succeed(NODE_CONFIG_VALUES) |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 def getNodeInfo( |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 self, |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 requestor: jid.JID, |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 service: jid.JID, |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 nodeIdentifier: str, |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 pep: bool = False, |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 recipient: jid.JID | None = None, |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 ) -> dict | None: |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 if not nodeIdentifier: |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 return None |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 info = {"type": "leaf", "meta-data": NODE_CONFIG} |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 return info |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 class EmailGWPubsubService(rsm.PubSubService): |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 """Pubsub service for XMPP requests""" |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 def __init__(self, gateway: "EmailGatewayComponent"): |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 self.gateway = gateway |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 self._pfs = gateway._pfs |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 resource = EmailGWPubsubResource(self) |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 super().__init__(resource) |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 self.host = gateway.host |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 self.discoIdentity = { |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 "category": "pubsub", |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 "type": "service", |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 "name": "Libervia Email Gateway", |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 } |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 @ensure_deferred |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 async def getDiscoInfo( |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 self, requestor: jid.JID, target: jid.JID, nodeIdentifier: str = "" |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 ) -> list[disco.DiscoFeature | disco.DiscoIdentity | data_form.Form]: |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 infos = await super().getDiscoInfo(requestor, target, nodeIdentifier) |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 infos.append(disco.DiscoFeature(self._pfs.namespace)) |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 return infos |