annotate libervia/backend/plugins/plugin_xep_0498.py @ 4336:6e0918e638ee

plugin XEP-0498: "Pubsub File Sharing" implementation: Partial implementation of XEP-0498, necessary to implement the service part in email gateway. rel 453
author Goffi <goffi@goffi.org>
date Tue, 03 Dec 2024 00:13:23 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4336
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia plugin to jingle session publishing.
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2024 Jérôme Poisson (goffi@goffi.org)
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from typing import TYPE_CHECKING, Final, Self
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from pydantic import BaseModel
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.words.protocols.jabber import jid
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.xish import domish
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from wokkel import disco, iwokkel, pubsub
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from zope.interface import implementer
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from libervia.backend.core.constants import Const as C
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from libervia.backend.core.core_types import SatXMPPEntity
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from libervia.backend.core.i18n import _
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from libervia.backend.core.log import getLogger
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from libervia.backend.plugins.plugin_xep_0234 import NS_JINGLE_FT
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from libervia.backend.plugins.plugin_xep_0358 import JinglePub
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from libervia.backend.plugins.plugin_xep_0446 import FileMetadata
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from libervia.backend.plugins.plugin_xep_0447 import FileSharing, JinglePubSource
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 if TYPE_CHECKING:
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 from libervia.backend.core.main import LiberviaBackend
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 log = getLogger(__name__)
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 PLUGIN_INFO = {
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 C.PI_NAME: "Pubsub File Sharing",
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 C.PI_IMPORT_NAME: "XEP-0498",
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_TYPE: "XEP",
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 C.PI_MODES: C.PLUG_MODE_BOTH,
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 C.PI_PROTOCOLS: [],
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 C.PI_DEPENDENCIES: [
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 "XEP-0060",
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 "XEP-0447",
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 ],
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 C.PI_RECOMMENDATIONS: [],
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 C.PI_MAIN: "XEP_0498",
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 C.PI_HANDLER: "yes",
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 C.PI_DESCRIPTION: _("""Share and retrieve files via Pubsub."""),
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 }
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
58
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 NS_PUBSUB_FILE_SHARING: Final = "urn:xmpp:pubsub-file-sharing:0"
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
60
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 class NodeData(BaseModel):
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 """Model for JinglePub element."""
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 files: dict[str, FileSharing]
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
66
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 def to_elements(self) -> list[domish.Element]:
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 """Return the list of item elements corresponding to this model"""
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 items = []
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 for item_id, file_sharing in self.files.items():
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 item_elt = pubsub.Item(id=item_id, payload=file_sharing.to_element())
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 items.append(item_elt)
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
73
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 return items
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
75
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 @classmethod
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 def from_files_data(cls, source_jid: jid.JID, files_data: list[dict]) -> Self:
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 """Generate from list of file data as returned by ``memory.get_files``.
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
79
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 @param files_data: list of files data as returned by ``memory.get_files``.
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 @return: Instance of ``NodeData``.
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 """
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 kw = {}
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 for file_data in files_data:
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 file_metadata = FileMetadata.from_filedata_dict(file_data)
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 source = JinglePubSource(
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 from_jid=source_jid,
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 id=file_data["id"],
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 descriptions=[domish.Element((NS_JINGLE_FT, "description"))],
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 )
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 # We don't know if names are unique, so we add ID to be sure.
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 key = f'{file_data["name"]}_{file_data["id"]}'
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 kw[key] = FileSharing(file=file_metadata, sources=[source])
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 return cls(files=kw)
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
96
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
97
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 class XEP_0498:
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 namespace = NS_PUBSUB_FILE_SHARING
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
100
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 def __init__(self, host: "LiberviaBackend") -> None:
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 log.info(f"plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization")
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 self.host = host
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 host.register_namespace("pubsub-file-sharing", NS_PUBSUB_FILE_SHARING)
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
105
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 def get_handler(self, client: SatXMPPEntity) -> XMPPHandler:
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 return PubsubFileSharingHandler(self)
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
108
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
109
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 @implementer(iwokkel.IDisco)
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 class PubsubFileSharingHandler(XMPPHandler):
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
112
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 def __init__(self, plugin_parent):
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 self.plugin_parent = plugin_parent
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
115
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 def getDiscoInfo(
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 self, requestor: jid.JID, target: jid.JID, nodeIdentifier: str = ""
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 ) -> list[disco.DiscoFeature]:
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 return [
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 disco.DiscoFeature(NS_PUBSUB_FILE_SHARING),
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 ]
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
122
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 def getDiscoItems(
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 self, requestor: jid.JID, target: jid.JID, nodeIdentifier: str = ""
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 ) -> list[disco.DiscoItems]:
6e0918e638ee plugin XEP-0498: "Pubsub File Sharing" implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 return []