annotate libervia/backend/plugins/plugin_comp_email_gateway/__init__.py @ 4357:f43cbceba2a0

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