annotate libervia/backend/plugins/plugin_sec_gre_formatter_mime.py @ 4346:62746042e6d9

plugin gre encrypter: implement GRE Encrypter: OpenPGP: rel 455
author Goffi <goffi@goffi.org>
date Mon, 13 Jan 2025 01:23:22 +0100
parents 07e87adb2f65
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4345
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia plugin
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2025 Jérôme Poisson (goffi@goffi.org)
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from typing import Final, TYPE_CHECKING, cast
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from twisted.words.protocols.jabber import jid
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.xish import domish
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from wokkel import data_form, disco, iwokkel
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from zope.interface import implementer
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from libervia.backend.core import exceptions
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from libervia.backend.core.constants import Const as C
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from libervia.backend.core.core_types import SatXMPPEntity
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from libervia.backend.core.i18n import _
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from libervia.backend.core.log import getLogger
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from libervia.backend.plugins.plugin_xep_0106 import XEP_0106
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from .plugin_exp_gre import Formatter
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from email.mime.multipart import MIMEMultipart
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from email.mime.text import MIMEText
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 if TYPE_CHECKING:
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 from libervia.backend.core.main import LiberviaBackend
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 log = getLogger(__name__)
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 PLUGIN_INFO = {
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 C.PI_NAME: "GRE Formatter: MIME",
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_IMPORT_NAME: "GRE-MIME",
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 C.PI_TYPE: "XEP",
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 C.PI_MODES: C.PLUG_MODE_BOTH,
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 C.PI_PROTOCOLS: [],
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 C.PI_DEPENDENCIES: [
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 "GRE",
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 ],
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 C.PI_RECOMMENDATIONS: [],
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 C.PI_MAIN: "GREFormatterMime",
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 C.PI_HANDLER: "yes",
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 C.PI_DESCRIPTION: _("Handle MIME formatting for Gateway Relayed Encryption."),
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 }
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
58
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 NS_GRE_MIME: Final = "urn:xmpp:gre:formatter:mime:0"
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
60
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
61
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 class GREFormatterMime(Formatter):
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 name = "mime"
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 namespace = NS_GRE_MIME
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 def __init__(self, host: "LiberviaBackend") -> None:
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 log.info(f"plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization")
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 super().__init__(host)
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 host.register_namespace("gre-mime", NS_GRE_MIME)
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
70
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 def get_handler(self, client: SatXMPPEntity) -> XMPPHandler:
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 return GREMIMEHandler(self)
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
73
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 async def format(
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self,
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 client: SatXMPPEntity,
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 recipient_id: str,
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 message_elt: domish.Element,
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 encryption_data_form: data_form.Form,
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 ) -> bytes:
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 """Format the sent stanza as multipart MIME payload.
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
82
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 @param client: Client session.
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 @param message_elt: <message> element being sent.
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 @return: MIME formatted payload.
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 """
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 if message_elt.name != "message":
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 raise exceptions.InternalError("Only <message> stanza should be received.")
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
89
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 try:
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 body = str(next(message_elt.elements(None, "body")))
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 except StopIteration as e:
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 msg = f"Cancelling message sending due to missing body: {message_elt.toXml()}"
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 log.warning(msg)
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 raise exceptions.CancelError(msg) from e
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
96
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 msg = MIMEMultipart()
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 # "sender_id" is the real email address used by the gateway for this user.
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 msg["From"] = encryption_data_form["sender_id"]
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 msg["To"] = recipient_id
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
101
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 subject_elt = next(message_elt.elements(None, "subject"), None)
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 if subject_elt:
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 msg["Subject"] = str(subject_elt)
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
105
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 msg.attach(MIMEText(body))
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
107
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 return msg.as_bytes()
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
109
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
110
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 @implementer(iwokkel.IDisco)
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 class GREMIMEHandler(XMPPHandler):
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
113
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 def __init__(self, plugin_parent):
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 self.plugin_parent = plugin_parent
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
116
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 def getDiscoInfo(
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 self, requestor: jid.JID, target: jid.JID, nodeIdentifier: str = ""
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 ) -> list[disco.DiscoFeature]:
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 return [
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 disco.DiscoFeature(NS_GRE_MIME),
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 ]
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
123
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 def getDiscoItems(
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 self, requestor: jid.JID, target: jid.JID, nodeIdentifier: str = ""
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 ) -> list[disco.DiscoItems]:
07e87adb2f65 plugin GRE formatter MIME: implements GRE Formatter: MIME:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 return []