Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_comp_email_gateway/__init__.py @ 4348:35d41de5b2aa default tip @
doc (component): document use of Gateway Relayed Encryption:
fix 455
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 13 Jan 2025 01:23:22 +0100 |
parents | 54df67d5646c |
children |
rev | line source |
---|---|
4303 | 1 #!/usr/bin/env python3 |
2 | |
3 # Libervia Email Gateway Component | |
4 # Copyright (C) 2009-2024 Jérôme Poisson (goffi@goffi.org) | |
5 | |
6 # This program is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU Affero General Public License as published by | |
8 # the Free Software Foundation, either version 3 of the License, or | |
9 # (at your option) any later version. | |
10 | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU Affero General Public License for more details. | |
15 | |
16 # You should have received a copy of the GNU Affero General Public License | |
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | |
4347
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
19 import base64 |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
20 from email import encoders |
4303 | 21 from email.header import decode_header |
22 from email.message import EmailMessage | |
4347
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
23 from email.mime.application import MIMEApplication |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
24 from email.mime.multipart import MIMEMultipart |
4303 | 25 from email.mime.text import MIMEText |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
26 from email.utils import formataddr, getaddresses, parseaddr |
4303 | 27 from functools import partial |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
28 import hashlib |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
29 from pathlib import Path |
4303 | 30 import re |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
31 import shutil |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
32 import tempfile |
4347
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
33 from typing import NamedTuple, TYPE_CHECKING, cast |
4303 | 34 |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
35 from pydantic import BaseModel |
4303 | 36 from twisted.internet import defer, reactor |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
37 from twisted.internet.threads import deferToThread |
4303 | 38 from twisted.mail import smtp |
39 from twisted.words.protocols.jabber import jid | |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
40 from twisted.words.protocols.jabber import error as jabber_error |
4303 | 41 from twisted.words.protocols.jabber.error import StanzaError |
42 from twisted.words.protocols.jabber.xmlstream import XMPPHandler | |
43 from twisted.words.xish import domish | |
44 from wokkel import data_form, disco, iwokkel | |
45 from zope.interface import implementer | |
46 | |
47 from libervia.backend.core import exceptions | |
48 from libervia.backend.core.constants import Const as C | |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
49 from libervia.backend.core.core_types import SatXMPPComponent, SatXMPPEntity |
4303 | 50 from libervia.backend.core.i18n import D_, _ |
51 from libervia.backend.core.log import getLogger | |
52 from libervia.backend.memory.persistent import LazyPersistentBinaryDict | |
53 from libervia.backend.memory.sqla import select | |
54 from libervia.backend.memory.sqla_mapping import PrivateIndBin | |
55 from libervia.backend.models.core import MessageData | |
4338
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
4337
diff
changeset
|
56 from libervia.backend.plugins.plugin_comp_email_gateway.pubsub_service import ( |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
4337
diff
changeset
|
57 EmailGWPubsubService, |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
4337
diff
changeset
|
58 ) |
4347
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
59 from libervia.backend.plugins.plugin_exp_gre import GRE, GetDataHandler |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
60 from libervia.backend.plugins.plugin_sec_gre_encrypted_openpgp import NS_GRE_OPENPGP |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
61 from libervia.backend.plugins.plugin_sec_gre_formatter_mime import NS_GRE_MIME |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
62 from libervia.backend.plugins.plugin_xep_0033 import ( |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
63 AddressType, |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
64 AddressesData, |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
65 RECIPIENT_FIELDS, |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
66 ) |
4303 | 67 from libervia.backend.plugins.plugin_xep_0077 import XEP_0077 |
68 from libervia.backend.plugins.plugin_xep_0106 import XEP_0106 | |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
69 from libervia.backend.plugins.plugin_xep_0131 import HeadersData, Urgency, XEP_0131 |
4347
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
70 from libervia.backend.plugins.plugin_xep_0373 import binary_to_ascii_armor |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
71 from libervia.backend.plugins.plugin_xep_0498 import XEP_0498 |
4303 | 72 from libervia.backend.tools.utils import aio |
73 | |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
74 from .imap import IMAPClientFactory |
4303 | 75 from .models import Credentials, UserData |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
76 |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
77 if TYPE_CHECKING: |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
78 from libervia.backend.core.main import LiberviaBackend |
4303 | 79 |
80 | |
81 log = getLogger(__name__) | |
82 | |
83 IMPORT_NAME = "email-gateway" | |
84 NAME = "Libervia Email Gateway" | |
85 | |
86 PLUGIN_INFO = { | |
87 C.PI_NAME: "Email Gateway Component", | |
88 C.PI_IMPORT_NAME: IMPORT_NAME, | |
89 C.PI_MODES: [C.PLUG_MODE_COMPONENT], | |
90 C.PI_TYPE: C.PLUG_TYPE_ENTRY_POINT, | |
91 C.PI_PROTOCOLS: [], | |
4347
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
92 C.PI_DEPENDENCIES: [ |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
93 "XEP-0033", "XEP-0077", "XEP-0106", "XEP-0498", "GRE", "GRE-MIME", "GRE-OpenPGP" |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
94 ], |
4303 | 95 C.PI_RECOMMENDATIONS: [], |
96 C.PI_MAIN: "EmailGatewayComponent", | |
97 C.PI_HANDLER: C.BOOL_TRUE, | |
98 C.PI_DESCRIPTION: D_( | |
99 "Gateway to handle email. Usual emails are handled as message, while mailing " | |
100 "lists are converted to pubsub blogs." | |
101 ), | |
102 } | |
103 | |
104 CONF_SECTION = f"component {IMPORT_NAME}" | |
105 PREFIX_KEY_CREDENTIALS = "CREDENTIALS_" | |
106 KEY_CREDENTIALS = f"{PREFIX_KEY_CREDENTIALS}{{from_jid}}" | |
107 | |
108 email_pattern = re.compile(r"[^@]+@[^@]+\.[^@]+") | |
109 | |
110 | |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
111 class FileMetadata(NamedTuple): |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
112 path: Path |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
113 hash: str |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
114 size: int |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
115 |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
116 |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
117 class SendMailExtra(BaseModel): |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
118 addresses: AddressesData | None = None |
4317
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
119 headers: HeadersData | None = None |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
120 |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
121 |
4347
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
122 class EmailGatewayComponent(GetDataHandler): |
4303 | 123 IMPORT_NAME = IMPORT_NAME |
4347
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
124 gre_formatters = [NS_GRE_MIME] |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
125 gre_encrypters = [NS_GRE_OPENPGP] |
4303 | 126 verbose = 0 |
127 | |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
128 def __init__(self, host: "LiberviaBackend") -> None: |
4303 | 129 self.host = host |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
130 self.client: SatXMPPComponent | None = None |
4303 | 131 self.initalized = False |
132 self.storage: LazyPersistentBinaryDict | None = None | |
133 self._iq_register = cast(XEP_0077, host.plugins["XEP-0077"]) | |
134 self._iq_register.register_handler( | |
135 self._on_registration_form, self._on_registration_submit | |
136 ) | |
137 self._e = cast(XEP_0106, host.plugins["XEP-0106"]) | |
4317
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
138 self._shim = cast(XEP_0131, host.plugins["XEP-0131"]) |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
139 self._pfs = cast(XEP_0498, host.plugins["XEP-0498"]) |
4347
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
140 self._gre = cast(GRE, host.plugins["GRE"]) |
4303 | 141 # TODO: For the moment, all credentials are kept in cache; we should only keep the |
142 # X latest. | |
143 self.users_data: dict[jid.JID, UserData] = {} | |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
144 self.files_path = self.host.get_local_path(None, C.FILES_DIR) |
4303 | 145 host.trigger.add_with_check( |
146 "message_received", self, self._message_received_trigger, priority=-1000 | |
147 ) | |
148 | |
149 async def _init(self) -> None: | |
150 """Initialisation done after profile is connected""" | |
151 assert self.client is not None | |
152 self.client.identities.append(disco.DiscoIdentity("gateway", "smtp", NAME)) | |
153 self.storage = LazyPersistentBinaryDict(IMPORT_NAME, self.client.profile) | |
154 await self.connect_registered_users() | |
155 | |
156 @aio | |
157 async def get_registered_users(self) -> dict[jid.JID, Credentials]: | |
158 """Retrieve credentials for all registered users | |
159 | |
160 @return: a mapping from user JID to credentials data. | |
161 """ | |
162 assert self.client is not None | |
163 profile_id = self.host.memory.storage.profiles[self.client.profile] | |
164 async with self.host.memory.storage.session() as session: | |
165 query = select(PrivateIndBin).where( | |
166 PrivateIndBin.profile_id == profile_id, | |
167 PrivateIndBin.namespace == IMPORT_NAME, | |
168 PrivateIndBin.key.startswith(PREFIX_KEY_CREDENTIALS), | |
169 ) | |
170 result = await session.execute(query) | |
171 return { | |
172 jid.JID(p.key[len(PREFIX_KEY_CREDENTIALS) :]): p.value | |
173 for p in result.scalars() | |
174 } | |
175 | |
176 async def connect_registered_users(self) -> None: | |
177 """Connected users already registered to the gateway.""" | |
178 registered_data = await self.get_registered_users() | |
179 for user_jid, credentials in registered_data.items(): | |
180 user_data = self.users_data[user_jid] = UserData(credentials=credentials) | |
181 if not credentials["imap_success"]: | |
182 log.warning( | |
183 f"Ignoring unsuccessful IMAP credentials of {user_jid}. This user " | |
184 "won't receive message from this gateway." | |
185 ) | |
186 else: | |
187 try: | |
188 await self.connect_imap(user_jid, user_data) | |
189 except Exception as e: | |
190 log.warning(f"Can't connect {user_jid} to IMAP: {e}.") | |
191 else: | |
192 log.debug(f"Connection to IMAP server successful for {user_jid}.") | |
193 | |
4338
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
4337
diff
changeset
|
194 def get_handler(self, __) -> tuple[XMPPHandler, XMPPHandler]: |
7c0b7ecb816f
component email gateway: Add a pubsub service:
Goffi <goffi@goffi.org>
parents:
4337
diff
changeset
|
195 return EmailGatewayHandler(), EmailGWPubsubService(self) |
4303 | 196 |
197 async def profile_connecting(self, client: SatXMPPEntity) -> None: | |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
198 assert isinstance(client, SatXMPPComponent) |
4303 | 199 self.client = client |
4347
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
200 self._gre.register_get_data_handler(client, self) |
4303 | 201 if not self.initalized: |
202 await self._init() | |
203 self.initalized = True | |
204 | |
205 def _message_received_trigger( | |
206 self, | |
207 client: SatXMPPEntity, | |
208 message_elt: domish.Element, | |
209 post_treat: defer.Deferred, | |
210 ) -> bool: | |
211 """add the gateway workflow on post treatment""" | |
212 if client != self.client: | |
213 return True | |
214 post_treat.addCallback( | |
215 lambda mess_data: defer.ensureDeferred( | |
216 self.on_message(client, mess_data, message_elt) | |
217 ) | |
218 ) | |
219 return True | |
220 | |
221 async def on_message( | |
222 self, client: SatXMPPEntity, mess_data: MessageData, message_elt: domish.Element | |
223 ) -> dict: | |
224 """Called once message has been parsed | |
225 | |
226 @param client: Client session. | |
227 @param mess_data: Message data. | |
228 @return: Message data. | |
229 """ | |
230 if client != self.client: | |
231 return mess_data | |
232 from_jid = mess_data["from"].userhostJID() | |
4317
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
233 extra_kw = {} |
4303 | 234 if mess_data["type"] not in ("chat", "normal"): |
235 log.warning(f"ignoring message with unexpected type: {mess_data}") | |
236 return mess_data | |
237 if not client.is_local(from_jid): | |
238 log.warning(f"ignoring non local message: {mess_data}") | |
239 return mess_data | |
240 if not mess_data["to"].user: | |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
241 addresses = mess_data["extra"].get("addresses") |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
242 if not addresses: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
243 log.warning(f"ignoring message addressed to gateway itself: {mess_data}") |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
244 return mess_data |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
245 else: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
246 to_email = None |
4317
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
247 extra_kw["addresses"] = addresses |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
248 else: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
249 try: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
250 to_email = self._e.unescape(mess_data["to"].user) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
251 except ValueError: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
252 raise exceptions.DataError( |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
253 f'Invalid "to" JID, can\'t send message: {message_elt.toXml()}.' |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
254 ) |
4303 | 255 |
4347
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
256 encrypted_payload = self._gre.get_encrypted_payload(message_elt) |
4317
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
257 |
4303 | 258 try: |
4347
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
259 if encrypted_payload is not None: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
260 # We convert the base64 datat to ASCII Armor |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
261 encrypted_binary = base64.b64decode(encrypted_payload) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
262 encrypted_payload = binary_to_ascii_armor(encrypted_binary) |
4303 | 263 |
4347
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
264 assert to_email is not None |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
265 subject = "This is an encrypted message." |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
266 outer = MIMEMultipart('encrypted', protocol="application/pgp-encrypted") |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
267 outer["Subject"] = subject |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
268 # FIXME: use credentials here. |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
269 outer["From"] = from_jid.userhost() |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
270 outer["To"] = to_email |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
271 outer["Content-Type"] = "multipart/encrypted; protocol=\"application/pgp-encrypted\"" |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
272 version = MIMEApplication( |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
273 "Version: 1\n", |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
274 _subtype='pgp-encrypted', |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
275 _encoder=encoders.encode_7or8bit |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
276 ) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
277 version["Content-Description"] = "PGP/MIME version identification" |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
278 encrypted_part = MIMEApplication( |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
279 encrypted_payload, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
280 _subtype='octet-stream', |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
281 _encoder=encoders.encode_7or8bit |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
282 ) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
283 encrypted_part["Content-Description"] = "OpenPGP encrypted message" |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
284 encrypted_part["Content-Type"] = "application/octet-stream; name=\"encrypted.asc\"" |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
285 encrypted_part["Content-Disposition"] = "inline; filename=\"encrypted.asc\"" |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
286 outer.attach(version) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
287 outer.attach(encrypted_part) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
288 body = outer.as_bytes() |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
289 await self.send_encrypted_email( |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
290 from_jid=from_jid, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
291 to_email=to_email, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
292 body=body, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
293 extra=SendMailExtra(**extra_kw) if extra_kw else None, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
294 ) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
295 else: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
296 self._shim.move_keywords_to_headers(mess_data["extra"]) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
297 headers = mess_data["extra"].get("headers") |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
298 if headers: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
299 extra_kw["headers"] = headers |
4303 | 300 |
4347
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
301 try: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
302 body_lang, body = next(iter(mess_data["message"].items())) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
303 except (KeyError, StopIteration): |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
304 log.warning(f"No body found: {mess_data}") |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
305 body_lang, body = "", "" |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
306 try: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
307 subject_lang, subject = next(iter(mess_data["subject"].items())) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
308 except (KeyError, StopIteration): |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
309 subject_lang, subject = "", None |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
310 |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
311 if not body and not subject: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
312 log.warning(f"Ignoring empty message: {mess_data}") |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
313 return mess_data |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
314 |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
315 await self.send_email( |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
316 from_jid=from_jid, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
317 to_email=to_email, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
318 body=body, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
319 subject=subject, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
320 extra=SendMailExtra(**extra_kw) if extra_kw else None, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
321 ) |
4303 | 322 except exceptions.UnknownEntityError: |
323 log.warning(f"Can't send message, user {from_jid} is not registered.") | |
324 message_error_elt = StanzaError( | |
325 "subscription-required", | |
326 text="User need to register to the gateway before sending emails.", | |
327 ).toResponse(message_elt) | |
328 await client.a_send(message_error_elt) | |
329 raise exceptions.CancelError("User not registered.") | |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
330 except StanzaError as e: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
331 log.warning("Can't send message: {e}") |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
332 message_error_elt = e.toResponse(message_elt) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
333 await client.a_send(message_error_elt) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
334 raise exceptions.CancelError("Can't send message: {e}") |
4303 | 335 |
336 return mess_data | |
337 | |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
338 def jid_to_email( |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
339 self, client: SatXMPPEntity, address_jid: jid.JID, credentials: dict[str, str] |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
340 ) -> str: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
341 """Convert a JID to an email address. |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
342 |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
343 If JID is from the gateway, email address will be extracted. Otherwise, the |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
344 gateway email will be used, with XMPP address specified in name part. |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
345 |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
346 @param address_jid: JID of the recipient. |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
347 @param credentials: Sender credentials. |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
348 @return: Email address. |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
349 """ |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
350 if address_jid and address_jid.host.endswith(str(client.jid)): |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
351 return self._e.unescape(address_jid.user) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
352 else: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
353 email_address = credentials["user_email"] |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
354 if address_jid: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
355 email_address = formataddr((f"xmpp:{address_jid}", email_address)) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
356 return email_address |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
357 |
4347
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
358 async def get_credentials(self, from_jid: jid.JID) -> Credentials: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
359 """Retrieve user credentials from a bare JID. |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
360 |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
361 @param from_jid: Entity to retrieve credentials from. |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
362 @return: Credentials. |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
363 |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
364 @raise UnknownEntityError: If no credentials are found for the given JID. |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
365 """ |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
366 # We need a bare jid. |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
367 assert self.storage is not None |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
368 assert not from_jid.resource |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
369 try: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
370 user_data = self.users_data[from_jid] |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
371 except KeyError: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
372 key = KEY_CREDENTIALS.format(from_jid=from_jid) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
373 credentials = await self.storage.get(key) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
374 if credentials is None: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
375 raise exceptions.UnknownEntityError( |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
376 f"No credentials found for {from_jid}." |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
377 ) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
378 self.users_data[from_jid] = UserData(credentials) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
379 else: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
380 credentials = user_data.credentials |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
381 return credentials |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
382 |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
383 async def send_encrypted_email( |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
384 self, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
385 from_jid: jid.JID, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
386 to_email: str | None, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
387 body: bytes, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
388 extra: SendMailExtra | None = None, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
389 ) -> None: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
390 """Send an email using sender credentials. |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
391 |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
392 Credentials will be retrieved from cache or database. |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
393 |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
394 @param from_jid: Bare JID of the sender. |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
395 @param to_email: Email address of the recipient. |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
396 @param body: Encrypted body of the email. |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
397 @param extra: Extra data. |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
398 """ |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
399 assert self.client is not None |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
400 assert isinstance(body, bytes) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
401 credentials = await self.get_credentials(from_jid) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
402 |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
403 sender_domain = credentials["user_email"].split("@", 1)[-1] |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
404 recipients = [] |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
405 if to_email is not None: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
406 recipients.append(to_email.encode()) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
407 if extra is not None and extra.addresses is not None: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
408 for address in extra.addresses.addresses: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
409 recipient_jid = address.jid |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
410 if recipient_jid is None: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
411 continue |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
412 recipient_email = self.jid_to_email( |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
413 self.client, recipient_jid, credentials |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
414 ) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
415 recipients.append(recipient_email.encode()) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
416 |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
417 if not recipients: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
418 raise exceptions.InternalError("No recipient found.") |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
419 |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
420 await smtp.sendmail( |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
421 credentials["smtp_host"].encode(), |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
422 credentials["user_email"].encode(), |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
423 recipients, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
424 body, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
425 senderDomainName=sender_domain, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
426 port=int(credentials["smtp_port"]), |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
427 username=credentials["smtp_username"].encode(), |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
428 password=credentials["smtp_password"].encode(), |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
429 requireAuthentication=True, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
430 # TODO: only STARTTLS is supported right now, implicit TLS should be supported |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
431 # too. |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
432 requireTransportSecurity=True, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
433 ) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
434 |
4303 | 435 async def send_email( |
436 self, | |
437 from_jid: jid.JID, | |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
438 to_email: str | None, |
4303 | 439 body: str, |
440 subject: str | None, | |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
441 extra: SendMailExtra | None = None, |
4303 | 442 ) -> None: |
443 """Send an email using sender credentials. | |
444 | |
445 Credentials will be retrieve from cache, or database. | |
446 | |
447 @param from_jid: Bare JID of the sender. | |
448 @param to_email: Email address of the destinee. | |
449 @param body: Body of the email. | |
450 @param subject: Subject of the email. | |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
451 @param extra: Extra data. |
4303 | 452 |
453 @raise exceptions.UnknownEntityError: Credentials for "from_jid" can't be found. | |
454 """ | |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
455 assert self.client is not None |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
456 if extra is None: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
457 extra = SendMailExtra() |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
458 if to_email is None and (extra.addresses is None or not extra.addresses.to): |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
459 raise exceptions.InternalError( |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
460 '"to_email" can\'t be None if there is no "to" address!' |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
461 ) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
462 |
4347
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
463 credentials = await self.get_credentials(from_jid) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
464 |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
465 if isinstance(body, bytes): |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
466 assert to_email is not None |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
467 sender_domain = credentials["user_email"].split("@", 1)[-1] |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
468 await smtp.sendmail( |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
469 credentials["smtp_host"].encode(), |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
470 credentials["user_email"].encode(), |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
471 [to_email.encode()], |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
472 body, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
473 senderDomainName=sender_domain, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
474 port=int(credentials["smtp_port"]), |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
475 username=credentials["smtp_username"].encode(), |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
476 password=credentials["smtp_password"].encode(), |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
477 requireAuthentication=True, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
478 # TODO: only STARTTLS is supported right now, implicit TLS should be supported |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
479 # too. |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
480 requireTransportSecurity=True, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
481 ) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
482 return |
4303 | 483 |
484 msg = MIMEText(body, "plain", "UTF-8") | |
485 if subject is not None: | |
486 msg["Subject"] = subject | |
487 msg["From"] = formataddr( | |
488 (credentials["user_name"] or None, credentials["user_email"]) | |
489 ) | |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
490 if extra.addresses: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
491 assert extra.addresses.to |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
492 main_to_address = extra.addresses.to[0] |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
493 assert main_to_address.jid |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
494 to_email = self.jid_to_email(self.client, main_to_address.jid, credentials) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
495 for field in RECIPIENT_FIELDS: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
496 addresses = getattr(extra.addresses, field) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
497 if not addresses: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
498 continue |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
499 for address in addresses: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
500 if not address.delivered and ( |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
501 address.jid is None or address.jid.host != str(self.client.jid) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
502 ): |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
503 log.warning( |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
504 "Received undelivered message to external JID, this is not " |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
505 "allowed! Cancelling the message sending." |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
506 ) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
507 stanza_err = jabber_error.StanzaError( |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
508 "forbidden", |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
509 text="Multicasting (XEP-0033 addresses) can only be used " |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
510 "with JID from this gateway, not external ones. " |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
511 f" {address.jid} can't be delivered by this gateway and " |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
512 "should be delivered by server instead.", |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
513 ) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
514 raise stanza_err |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
515 email_addresses = [ |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
516 self.jid_to_email(self.client, address.jid, credentials) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
517 for address in addresses |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
518 if address.jid |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
519 ] |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
520 if email_addresses: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
521 msg[field.upper()] = ", ".join(email_addresses) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
522 else: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
523 assert to_email is not None |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
524 msg["To"] = to_email |
4303 | 525 |
526 sender_domain = credentials["user_email"].split("@", 1)[-1] | |
527 | |
4317
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
528 if extra.headers: |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
529 if extra.headers.keywords: |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
530 msg["Keywords"] = extra.headers.keywords |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
531 if extra.headers.urgency: |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
532 urgency = extra.headers.urgency |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
533 if urgency == Urgency.medium: |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
534 importance = "normal" |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
535 else: |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
536 importance = urgency |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
537 msg["Importance"] = importance |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
538 |
4303 | 539 await smtp.sendmail( |
540 credentials["smtp_host"].encode(), | |
541 credentials["user_email"].encode(), | |
542 [to_email.encode()], | |
543 msg.as_bytes(), | |
544 senderDomainName=sender_domain, | |
545 port=int(credentials["smtp_port"]), | |
546 username=credentials["smtp_username"].encode(), | |
547 password=credentials["smtp_password"].encode(), | |
548 requireAuthentication=True, | |
549 # TODO: only STARTTLS is supported right now, implicit TLS should be supported | |
550 # too. | |
551 requireTransportSecurity=True, | |
552 ) | |
553 | |
554 async def _on_registration_form( | |
555 self, client: SatXMPPEntity, iq_elt: domish.Element | |
556 ) -> tuple[bool, data_form.Form] | None: | |
557 if client != self.client: | |
558 return | |
559 assert self.storage is not None | |
560 from_jid = jid.JID(iq_elt["from"]) | |
561 key = KEY_CREDENTIALS.format(from_jid=from_jid.userhost()) | |
562 credentials = await self.storage.get(key) or {} | |
563 | |
564 form = data_form.Form(formType="form", title="IMAP/SMTP Credentials") | |
565 | |
566 # Add instructions | |
567 form.instructions = [ | |
568 D_( | |
569 "Please provide your IMAP and SMTP credentials to configure the " | |
570 "connection." | |
571 ) | |
572 ] | |
573 | |
574 # Add identity fields | |
575 form.addField( | |
576 data_form.Field( | |
577 fieldType="text-single", | |
578 var="user_name", | |
579 label="User Name", | |
580 desc=D_('The display name to use in the "From" field of sent emails.'), | |
581 value=credentials.get("user_name"), | |
582 required=True, | |
583 ) | |
584 ) | |
585 | |
586 form.addField( | |
587 data_form.Field( | |
588 fieldType="text-single", | |
589 var="user_email", | |
590 label="User Email", | |
591 desc=D_('The email address to use in the "From" field of sent emails.'), | |
592 value=credentials.get("user_email"), | |
593 required=True, | |
594 ) | |
595 ) | |
596 | |
597 # Add fields for IMAP credentials | |
598 form.addField( | |
599 data_form.Field( | |
600 fieldType="text-single", | |
601 var="imap_host", | |
602 label="IMAP Host", | |
603 desc=D_("IMAP server hostname or IP address"), | |
604 value=credentials.get("imap_host"), | |
605 required=True, | |
606 ) | |
607 ) | |
608 form.addField( | |
609 data_form.Field( | |
610 fieldType="text-single", | |
611 var="imap_port", | |
612 label="IMAP Port", | |
613 desc=D_("IMAP server port (default: 993)"), | |
614 value=credentials.get("imap_port", "993"), | |
615 ) | |
616 ) | |
617 form.addField( | |
618 data_form.Field( | |
619 fieldType="text-single", | |
620 var="imap_username", | |
621 label="IMAP Username", | |
622 desc=D_("Username for IMAP authentication"), | |
623 value=credentials.get("imap_username"), | |
624 required=True, | |
625 ) | |
626 ) | |
627 form.addField( | |
628 data_form.Field( | |
629 fieldType="text-private", | |
630 var="imap_password", | |
631 label="IMAP Password", | |
632 desc=D_("Password for IMAP authentication"), | |
633 value=credentials.get("imap_password"), | |
634 required=True, | |
635 ) | |
636 ) | |
637 | |
638 # Add fields for SMTP credentials | |
639 form.addField( | |
640 data_form.Field( | |
641 fieldType="text-single", | |
642 var="smtp_host", | |
643 label="SMTP Host", | |
644 desc=D_("SMTP server hostname or IP address"), | |
645 value=credentials.get("smtp_host"), | |
646 required=True, | |
647 ) | |
648 ) | |
649 form.addField( | |
650 data_form.Field( | |
651 fieldType="text-single", | |
652 var="smtp_port", | |
653 label="SMTP Port", | |
654 desc=D_("SMTP server port (default: 587)"), | |
655 value=credentials.get("smtp_port", "587"), | |
656 ) | |
657 ) | |
658 form.addField( | |
659 data_form.Field( | |
660 fieldType="text-single", | |
661 var="smtp_username", | |
662 label="SMTP Username", | |
663 desc=D_("Username for SMTP authentication"), | |
664 value=credentials.get("smtp_username"), | |
665 required=True, | |
666 ) | |
667 ) | |
668 form.addField( | |
669 data_form.Field( | |
670 fieldType="text-private", | |
671 var="smtp_password", | |
672 label="SMTP Password", | |
673 desc=D_("Password for SMTP authentication"), | |
674 value=credentials.get("smtp_password"), | |
675 required=True, | |
676 ) | |
677 ) | |
678 | |
679 return bool(credentials), form | |
680 | |
681 def validate_field( | |
682 self, | |
683 form: data_form.Form, | |
684 key: str, | |
685 field_type: str, | |
686 min_value: int | None = None, | |
687 max_value: int | None = None, | |
688 default: str | int | None = None, | |
689 ) -> None: | |
690 """Validate a single field. | |
691 | |
692 @param form: The form containing the fields. | |
693 @param key: The key of the field to validate. | |
694 @param field_type: The expected type of the field value. | |
695 @param min_value: Optional minimum value for integer fields. | |
696 @param max_value: Optional maximum value for integer fields. | |
697 @param default: Default value to use if the field is missing. | |
698 @raise StanzaError: If the field value is invalid or missing. | |
699 """ | |
700 field = form.fields.get(key) | |
701 if field is None: | |
702 if default is None: | |
703 raise StanzaError("bad-request", text=f"{key} is required") | |
704 field = data_form.Field(var=key, value=str(default)) | |
705 form.addField(field) | |
706 | |
707 value = field.value | |
708 if field_type == "int": | |
709 try: | |
710 value = int(value) | |
711 if (min_value is not None and value < min_value) or ( | |
712 max_value is not None and value > max_value | |
713 ): | |
714 raise ValueError | |
715 except (ValueError, TypeError): | |
716 raise StanzaError("bad-request", text=f"Invalid value for {key}: {value}") | |
717 elif field_type == "str": | |
718 if not isinstance(value, str): | |
719 raise StanzaError("bad-request", text=f"Invalid value for {key}: {value}") | |
720 | |
721 # Basic email validation for user_email field | |
722 if key == "user_email": | |
723 # XXX: This is a minimal check. A complete email validation is notoriously | |
724 # difficult. | |
725 if not email_pattern.match(value): | |
726 raise StanzaError( | |
727 "bad-request", text=f"Invalid email address: {value}" | |
728 ) | |
729 | |
730 def validate_imap_smtp_form(self, submit_form: data_form.Form) -> None: | |
731 """Validate the submitted IMAP/SMTP credentials form. | |
732 | |
733 @param submit_form: The submitted form containing IMAP/SMTP credentials. | |
734 @raise StanzaError: If any of the values are invalid. | |
735 """ | |
736 # Validate identity fields | |
737 self.validate_field(submit_form, "user_name", "str") | |
738 self.validate_field(submit_form, "user_email", "str") | |
739 | |
740 # Validate IMAP fields | |
741 self.validate_field(submit_form, "imap_host", "str") | |
742 self.validate_field( | |
743 submit_form, "imap_port", "int", min_value=1, max_value=65535, default=993 | |
744 ) | |
745 self.validate_field(submit_form, "imap_username", "str") | |
746 self.validate_field(submit_form, "imap_password", "str") | |
747 | |
748 # Validate SMTP fields | |
749 self.validate_field(submit_form, "smtp_host", "str") | |
750 self.validate_field( | |
751 submit_form, "smtp_port", "int", min_value=1, max_value=65535, default=587 | |
752 ) | |
753 self.validate_field(submit_form, "smtp_username", "str") | |
754 self.validate_field(submit_form, "smtp_password", "str") | |
755 | |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
756 def email_to_jid( |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
757 self, |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
758 client: SatXMPPEntity, |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
759 user_email: str, |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
760 user_jid: jid.JID, |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
761 email_name: str, |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
762 email_addr: str, |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
763 ) -> tuple[jid.JID, str | None]: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
764 """Convert an email address to a JID and extract the name if present. |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
765 |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
766 @param client: Client session. |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
767 @param user_email: Email address of the gateway user. |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
768 @param user_jid: JID of the gateway user. |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
769 @param email_name: Email associated name. |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
770 @param email_addr: Email address. |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
771 @return: Tuple of JID and name (if present). |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
772 """ |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
773 email_name = email_name.strip() |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
774 if email_name.startswith("xmpp:"): |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
775 return jid.JID(email_name[5:]), None |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
776 elif email_addr == user_email: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
777 return (user_jid, None) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
778 else: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
779 return ( |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
780 jid.JID(None, (self._e.escape(email_addr), client.jid.host, None)), |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
781 email_name or None, |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
782 ) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
783 |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
784 async def on_new_email( |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
785 self, user_data: UserData, user_jid: jid.JID, email: EmailMessage |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
786 ) -> None: |
4303 | 787 """Called when a new message has been received. |
788 | |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
789 @param user_data: user data, used to map registered user email to corresponding |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
790 jid. |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
791 @param user_jid: JID of the recipient. |
4303 | 792 @param email: Parsed email. |
793 """ | |
794 assert self.client is not None | |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
795 user_email = user_data.credentials["user_email"] |
4303 | 796 name, email_addr = parseaddr(email["from"]) |
797 email_addr = email_addr.lower() | |
798 from_jid = jid.JID(None, (self._e.escape(email_addr), self.client.jid.host, None)) | |
799 | |
800 # Get the email body | |
801 body_mime = email.get_body(("plain",)) | |
802 if body_mime is not None: | |
803 charset = body_mime.get_content_charset() or "utf-8" | |
804 body = body_mime.get_payload(decode=True).decode(charset, errors="replace") | |
805 else: | |
806 log.warning(f"No body found in email:\n{email}") | |
807 body = "" | |
808 | |
809 # Decode the subject | |
810 subject = email.get("subject") | |
811 if subject: | |
812 decoded_subject = decode_header(subject) | |
813 subject = "".join( | |
814 [ | |
815 part.decode(encoding or "utf-8") if isinstance(part, bytes) else part | |
816 for part, encoding in decoded_subject | |
817 ] | |
818 ).strip() | |
819 else: | |
820 subject = None | |
821 | |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
822 # Parse recipient fields |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
823 kwargs = {} |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
824 for field in RECIPIENT_FIELDS: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
825 email_addresses = email.get_all(field) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
826 if email_addresses: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
827 jids_and_names = [ |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
828 self.email_to_jid(self.client, user_email, user_jid, name, addr) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
829 for name, addr in getaddresses(email_addresses) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
830 ] |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
831 kwargs[field] = [ |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
832 AddressType(jid=jid, desc=name) for jid, name in jids_and_names |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
833 ] |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
834 |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
835 # At least "to" header should be set, so kwargs should never be empty |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
836 assert kwargs |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
837 addresses_data = AddressesData(**kwargs) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
838 |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
839 # Parse reply-to field |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
840 reply_to_addresses = email.get_all("reply-to") |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
841 if reply_to_addresses: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
842 jids_with_names = [ |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
843 self.email_to_jid(self.client, user_email, user_jid, name, addr) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
844 for name, addr in getaddresses(reply_to_addresses) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
845 ] |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
846 addresses_data.replyto = [ |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
847 AddressType(jid=jid, desc=name) for jid, name in jids_with_names |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
848 ] |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
849 |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
850 # Set noreply flag |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
851 # The is no flag to indicate a no-reply message, so we check common user parts in |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
852 # from and reply-to headers. |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
853 from_addresses = [email_addr] |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
854 if reply_to_addresses: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
855 from_addresses.extend( |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
856 addr for a in reply_to_addresses if (addr := parseaddr(a)[1]) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
857 ) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
858 for from_address in from_addresses: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
859 from_user_part = from_address.split("@", 1)[0].lower() |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
860 if from_user_part in ( |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
861 "no-reply", |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
862 "noreply", |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
863 "do-not-reply", |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
864 "donotreply", |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
865 "notification", |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
866 "notifications", |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
867 ): |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
868 addresses_data.noreply = True |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
869 break |
4317
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
870 extra = {} |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
871 |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
872 if ( |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
873 not addresses_data.replyto |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
874 and not addresses_data.noreply |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
875 and not addresses_data.cc |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
876 and not addresses_data.bcc |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
877 and addresses_data.to == [AddressType(jid=user_jid)] |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
878 ): |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
879 # The main recipient is the only one, and there is no other metadata: there is |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
880 # no need to add addresses metadata. |
4317
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
881 pass |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
882 else: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
883 for address in addresses_data.addresses: |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
884 if address.jid and ( |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
885 address.jid == user_jid or address.jid.host == str(self.client.jid) |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
886 ): |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
887 # Those are email address, and have been delivered by the sender, |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
888 # other JID addresses will have to be delivered by us. |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
889 address.delivered = True |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
890 |
4317
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
891 extra["addresses"] = addresses_data.model_dump(mode="json", exclude_none=True) |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
892 |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
893 # We look for interesting headers |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
894 headers = {} |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
895 keywords_headers = email.get_all("keywords") |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
896 if keywords_headers: |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
897 keywords = ",".join(keywords_headers) |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
898 headers["keywords"] = keywords |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
899 |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
900 importance = email["importance"] |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
901 if importance: |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
902 # We convert to urgency |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
903 if importance in ("low", "high"): |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
904 headers["urgency"] = importance |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
905 elif importance == "normal": |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
906 headers["urgency"] = "medium" |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
907 else: |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
908 log.warning("Ignoring invalid importance header: {importance!r}") |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
909 |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
910 if headers: |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
911 extra["headers"] = HeadersData(**headers).model_dump( |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
912 mode="json", exclude_none=True |
055930cc81f9
component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents:
4309
diff
changeset
|
913 ) |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
914 |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
915 # Handle attachments |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
916 for part in email.iter_attachments(): |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
917 await self.handle_attachment(part, user_jid) |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
918 |
4303 | 919 client = self.client.get_virtual_client(from_jid) |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
920 |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
921 await client.sendMessage( |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
922 user_jid, |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
923 {"": body}, |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
924 {"": subject} if subject else None, |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
925 extra=extra, |
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
926 ) |
4303 | 927 |
4337
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
928 async def handle_attachment(self, part: EmailMessage, recipient_jid: jid.JID) -> None: |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
929 """Handle an attachment from an email. |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
930 |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
931 @param part: The object representing the attachment. |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
932 @param recipient_jid: JID of the recipient to whom the attachment is being sent. |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
933 """ |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
934 assert self.client is not None |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
935 content_type = part.get_content_type() |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
936 filename = part.get_filename() or "attachment" |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
937 log.debug(f"Handling attachment: {filename} ({content_type})") |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
938 file_metadata = await deferToThread(self._save_attachment, part) |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
939 if file_metadata is not None: |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
940 log.debug(f"Attachment {filename!r} saved to {file_metadata.path}") |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
941 try: |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
942 await self.host.memory.set_file( |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
943 self.client, |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
944 filename, |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
945 file_hash=file_metadata.hash, |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
946 hash_algo="sha-256", |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
947 size=file_metadata.size, |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
948 namespace=PLUGIN_INFO[C.PI_IMPORT_NAME], |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
949 mime_type=content_type, |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
950 owner=recipient_jid, |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
951 ) |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
952 except Exception: |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
953 log.exception(f"Failed to register file {filename!r}") |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
954 |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
955 def _save_attachment(self, part: EmailMessage) -> FileMetadata | None: |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
956 """Save the attachment to files path. |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
957 |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
958 This method must be executed in a thread with deferToThread to avoid blocking the |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
959 reactor with IO operations if the attachment is large. |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
960 |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
961 @param part: The object representing the attachment. |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
962 @return: Attachment data, or None if an error occurs. |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
963 @raises IOError: Can't save the attachment. |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
964 """ |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
965 temp_file = None |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
966 try: |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
967 with tempfile.NamedTemporaryFile(delete=False) as temp_file: |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
968 payload = part.get_payload(decode=True) |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
969 if isinstance(payload, bytes): |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
970 temp_file.write(payload) |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
971 file_hash = hashlib.sha256(payload).hexdigest() |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
972 file_path = self.files_path / file_hash |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
973 shutil.move(temp_file.name, file_path) |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
974 file_size = len(payload) |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
975 return FileMetadata(path=file_path, hash=file_hash, size=file_size) |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
976 else: |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
977 log.warning(f"Can't write payload of type {type(payload)}.") |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
978 return None |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
979 except Exception as e: |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
980 raise IOError(f"Failed to save attachment: {e}") |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
981 finally: |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
982 if temp_file is not None and Path(temp_file.name).exists(): |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
983 Path(temp_file.name).unlink() |
95792a1f26c7
component email gateway: attachments handling:
Goffi <goffi@goffi.org>
parents:
4317
diff
changeset
|
984 |
4303 | 985 async def connect_imap(self, from_jid: jid.JID, user_data: UserData) -> None: |
986 """Connect to IMAP service. | |
987 | |
988 [self.on_new_email] will be used as callback on new messages. | |
989 | |
990 @param from_jid: JID of the user associated with given credentials. | |
991 @param credentials: Email credentials. | |
992 """ | |
993 credentials = user_data.credentials | |
994 | |
995 connected = defer.Deferred() | |
996 factory = IMAPClientFactory( | |
997 user_data, | |
4309
b56b1eae7994
component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents:
4303
diff
changeset
|
998 partial(self.on_new_email, user_data, from_jid.userhostJID()), |
4303 | 999 connected, |
1000 ) | |
1001 reactor.connectTCP( | |
1002 credentials["imap_host"], int(credentials["imap_port"]), factory | |
1003 ) | |
1004 await connected | |
1005 | |
1006 async def _on_registration_submit( | |
1007 self, | |
1008 client: SatXMPPEntity, | |
1009 iq_elt: domish.Element, | |
1010 submit_form: data_form.Form | None, | |
1011 ) -> bool | None: | |
1012 """Handle registration submit request. | |
1013 | |
1014 Submit form is validated, and credentials are stored. | |
1015 @param client: client session. | |
1016 iq_elt: IQ stanza of the submission request. | |
1017 submit_form: submit form. | |
1018 @return: True if successful. | |
1019 None if the callback is not relevant for this request. | |
1020 """ | |
1021 if client != self.client: | |
1022 return | |
1023 assert self.storage is not None | |
1024 from_jid = jid.JID(iq_elt["from"]).userhostJID() | |
1025 | |
1026 if submit_form is None: | |
1027 # This is an unregistration request. | |
1028 try: | |
1029 user_data = self.users_data[from_jid] | |
1030 except KeyError: | |
1031 pass | |
1032 else: | |
1033 if user_data.imap_client is not None: | |
1034 try: | |
1035 await user_data.imap_client.logout() | |
1036 except Exception: | |
1037 log.exception(f"Can't log out {from_jid} from IMAP server.") | |
1038 key = KEY_CREDENTIALS.format(from_jid=from_jid) | |
1039 await self.storage.adel(key) | |
1040 log.info(f"{from_jid} unregistered from this gateway.") | |
1041 return True | |
1042 | |
1043 self.validate_imap_smtp_form(submit_form) | |
1044 credentials = {key: field.value for key, field in submit_form.fields.items()} | |
1045 user_data = self.users_data.get(from_jid) | |
1046 if user_data is None: | |
1047 # The user is not in cache, we cache current credentials. | |
1048 user_data = self.users_data[from_jid] = UserData(credentials=credentials) | |
1049 else: | |
1050 # The user is known, we update credentials. | |
1051 user_data.credentials = credentials | |
1052 key = KEY_CREDENTIALS.format(from_jid=from_jid) | |
1053 try: | |
1054 await self.connect_imap(from_jid, user_data) | |
1055 except Exception as e: | |
1056 log.warning(f"Can't connect to IMAP server for {from_jid}") | |
1057 credentials["imap_success"] = False | |
1058 await self.storage.aset(key, credentials) | |
1059 raise e | |
1060 else: | |
1061 log.debug(f"Connection successful to IMAP server for {from_jid}") | |
1062 credentials["imap_success"] = True | |
1063 await self.storage.aset(key, credentials) | |
1064 return True | |
1065 | |
1066 | |
4347
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
1067 async def on_relayed_encryption_data( |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
1068 self, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
1069 client: SatXMPPEntity, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
1070 iq_elt: domish.Element, |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
1071 form: data_form.Form |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
1072 ) -> None: |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
1073 from_jid = jid.JID(iq_elt["from"]).userhostJID() |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
1074 credentials = await self.get_credentials(from_jid) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
1075 form.addField(data_form.Field(var="sender_id", value=credentials["user_email"])) |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
1076 |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
1077 |
54df67d5646c
component email gateway: implement Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
4338
diff
changeset
|
1078 |
4303 | 1079 @implementer(iwokkel.IDisco) |
1080 class EmailGatewayHandler(XMPPHandler): | |
1081 | |
1082 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): | |
1083 return [] | |
1084 | |
1085 def getDiscoItems(self, requestor, target, nodeIdentifier=""): | |
1086 return [] |