annotate libervia/backend/plugins/plugin_comp_email_gateway/__init__.py @ 4332:71c939e34ca6

XEP-0373 (OX): Adjust to gpgme updates: generate with explicit algorithm and subkeys
author Syndace <me@syndace.dev>
date Sat, 13 Jul 2024 18:28:28 +0200
parents 055930cc81f9
children 95792a1f26c7
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
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from email.header import decode_header
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from email.message import EmailMessage
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from email.mime.text import MIMEText
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
22 from email.utils import formataddr, getaddresses, parseaddr
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from functools import partial
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 import re
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
25 from typing import cast
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
27 from pydantic import BaseModel
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from twisted.internet import defer, reactor
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from twisted.mail import smtp
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from twisted.words.protocols.jabber import jid
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
31 from twisted.words.protocols.jabber import error as jabber_error
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from twisted.words.protocols.jabber.error import StanzaError
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from twisted.words.xish import domish
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from wokkel import data_form, disco, iwokkel
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from zope.interface import implementer
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 from libervia.backend.core import exceptions
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 from libervia.backend.core.constants import Const as C
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 from libervia.backend.core.core_types import SatXMPPEntity
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 from libervia.backend.core.i18n import D_, _
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 from libervia.backend.core.log import getLogger
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 from libervia.backend.memory.persistent import LazyPersistentBinaryDict
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 from libervia.backend.memory.sqla import select
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 from libervia.backend.memory.sqla_mapping import PrivateIndBin
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 from libervia.backend.models.core import MessageData
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
47 from libervia.backend.plugins.plugin_xep_0033 import (
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
48 RECIPIENT_FIELDS,
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
49 AddressType,
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
50 AddressesData,
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
51 )
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 from libervia.backend.plugins.plugin_xep_0077 import XEP_0077
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 from libervia.backend.plugins.plugin_xep_0106 import XEP_0106
4317
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
54 from libervia.backend.plugins.plugin_xep_0131 import XEP_0131, HeadersData, Urgency
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 from libervia.backend.tools.utils import aio
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 from .models import Credentials, UserData
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 from .imap import IMAPClientFactory
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 log = getLogger(__name__)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 IMPORT_NAME = "email-gateway"
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 NAME = "Libervia Email Gateway"
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 PLUGIN_INFO = {
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 C.PI_NAME: "Email Gateway Component",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 C.PI_IMPORT_NAME: IMPORT_NAME,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 C.PI_MODES: [C.PLUG_MODE_COMPONENT],
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 C.PI_TYPE: C.PLUG_TYPE_ENTRY_POINT,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 C.PI_PROTOCOLS: [],
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
72 C.PI_DEPENDENCIES: ["XEP-0033", "XEP-0077", "XEP-0106"],
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 C.PI_RECOMMENDATIONS: [],
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 C.PI_MAIN: "EmailGatewayComponent",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 C.PI_HANDLER: C.BOOL_TRUE,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 C.PI_DESCRIPTION: D_(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 "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
78 "lists are converted to pubsub blogs."
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 ),
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 CONF_SECTION = f"component {IMPORT_NAME}"
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 PREFIX_KEY_CREDENTIALS = "CREDENTIALS_"
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 KEY_CREDENTIALS = f"{PREFIX_KEY_CREDENTIALS}{{from_jid}}"
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 email_pattern = re.compile(r"[^@]+@[^@]+\.[^@]+")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
89 class SendMailExtra(BaseModel):
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
90 addresses: AddressesData | None = None
4317
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
91 headers: HeadersData | None = None
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
92
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
93
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 class EmailGatewayComponent:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 IMPORT_NAME = IMPORT_NAME
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 verbose = 0
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 def __init__(self, host):
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 self.host = host
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 self.client: SatXMPPEntity | None = None
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 self.initalized = False
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 self.storage: LazyPersistentBinaryDict | None = None
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 self._iq_register = cast(XEP_0077, host.plugins["XEP-0077"])
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 self._iq_register.register_handler(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 self._on_registration_form, self._on_registration_submit
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 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
108 self._shim = cast(XEP_0131, host.plugins["XEP-0131"])
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 # 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
110 # X latest.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 self.users_data: dict[jid.JID, UserData] = {}
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 host.trigger.add_with_check(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 "message_received", self, self._message_received_trigger, priority=-1000
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
115
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 async def _init(self) -> None:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 """Initialisation done after profile is connected"""
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 assert self.client is not None
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 self.client.identities.append(disco.DiscoIdentity("gateway", "smtp", NAME))
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 self.storage = LazyPersistentBinaryDict(IMPORT_NAME, self.client.profile)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 await self.connect_registered_users()
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 @aio
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 async def get_registered_users(self) -> dict[jid.JID, Credentials]:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 """Retrieve credentials for all registered users
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
126
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 @return: a mapping from user JID to credentials data.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 """
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 assert self.client is not None
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 profile_id = self.host.memory.storage.profiles[self.client.profile]
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 async with self.host.memory.storage.session() as session:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 query = select(PrivateIndBin).where(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 PrivateIndBin.profile_id == profile_id,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 PrivateIndBin.namespace == IMPORT_NAME,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 PrivateIndBin.key.startswith(PREFIX_KEY_CREDENTIALS),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 result = await session.execute(query)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 return {
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 jid.JID(p.key[len(PREFIX_KEY_CREDENTIALS) :]): p.value
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 for p in result.scalars()
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 }
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 async def connect_registered_users(self) -> None:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 """Connected users already registered to the gateway."""
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 registered_data = await self.get_registered_users()
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 for user_jid, credentials in registered_data.items():
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 user_data = self.users_data[user_jid] = UserData(credentials=credentials)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 if not credentials["imap_success"]:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 log.warning(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 f"Ignoring unsuccessful IMAP credentials of {user_jid}. This user "
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 "won't receive message from this gateway."
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 else:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 try:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 await self.connect_imap(user_jid, user_data)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 except Exception as e:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 log.warning(f"Can't connect {user_jid} to IMAP: {e}.")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 else:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 log.debug(f"Connection to IMAP server successful for {user_jid}.")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 def get_handler(self, __) -> XMPPHandler:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 return EmailGatewayHandler()
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 async def profile_connecting(self, client: SatXMPPEntity) -> None:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 self.client = client
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 if not self.initalized:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 await self._init()
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 self.initalized = True
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
169
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 def _message_received_trigger(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 self,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 client: SatXMPPEntity,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 message_elt: domish.Element,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 post_treat: defer.Deferred,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 ) -> bool:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 """add the gateway workflow on post treatment"""
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 if client != self.client:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 return True
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 post_treat.addCallback(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 lambda mess_data: defer.ensureDeferred(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 self.on_message(client, mess_data, message_elt)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 return True
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
185
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 async def on_message(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 self, client: SatXMPPEntity, mess_data: MessageData, message_elt: domish.Element
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 ) -> dict:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 """Called once message has been parsed
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
190
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 @param client: Client session.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 @param mess_data: Message data.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 @return: Message data.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 """
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 if client != self.client:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 return mess_data
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 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
198 extra_kw = {}
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 if mess_data["type"] not in ("chat", "normal"):
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 log.warning(f"ignoring message with unexpected type: {mess_data}")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 return mess_data
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 if not client.is_local(from_jid):
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 log.warning(f"ignoring non local message: {mess_data}")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 return mess_data
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 if not mess_data["to"].user:
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
206 addresses = mess_data["extra"].get("addresses")
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
207 if not addresses:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
208 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
209 return mess_data
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
210 else:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
211 to_email = None
4317
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
212 extra_kw["addresses"] = addresses
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
213 else:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
214 try:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
215 to_email = self._e.unescape(mess_data["to"].user)
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
216 except ValueError:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
217 raise exceptions.DataError(
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
218 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
219 )
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
220
4317
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
221 self._shim.move_keywords_to_headers(mess_data["extra"])
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
222 headers = mess_data["extra"].get("headers")
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
223 if headers:
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
224 extra_kw["headers"] = headers
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
225
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 try:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 body_lang, body = next(iter(mess_data["message"].items()))
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 except (KeyError, StopIteration):
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 log.warning(f"No body found: {mess_data}")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 body_lang, body = "", ""
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 try:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 subject_lang, subject = next(iter(mess_data["subject"].items()))
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 except (KeyError, StopIteration):
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 subject_lang, subject = "", None
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
235
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 if not body and not subject:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 log.warning(f"Ignoring empty message: {mess_data}")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 return mess_data
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
239
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 try:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 await self.send_email(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 from_jid=from_jid,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 to_email=to_email,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 body=body,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 subject=subject,
4317
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
246 extra=SendMailExtra(**extra_kw) if extra_kw else None,
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 except exceptions.UnknownEntityError:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 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
250 message_error_elt = StanzaError(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 "subscription-required",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 text="User need to register to the gateway before sending emails.",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 ).toResponse(message_elt)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 await client.a_send(message_error_elt)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 raise exceptions.CancelError("User not registered.")
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
256 except StanzaError as e:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
257 log.warning("Can't send message: {e}")
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
258 message_error_elt = e.toResponse(message_elt)
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
259 await client.a_send(message_error_elt)
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
260 raise exceptions.CancelError("Can't send message: {e}")
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
261
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 return mess_data
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
263
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
264 def jid_to_email(
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
265 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
266 ) -> str:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
267 """Convert a JID to an email address.
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
268
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
269 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
270 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
271
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
272 @param address_jid: JID of the recipient.
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
273 @param credentials: Sender credentials.
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
274 @return: Email address.
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
275 """
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
276 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
277 return self._e.unescape(address_jid.user)
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
278 else:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
279 email_address = credentials["user_email"]
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
280 if address_jid:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
281 email_address = formataddr((f"xmpp:{address_jid}", email_address))
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
282 return email_address
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
283
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 async def send_email(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 self,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 from_jid: jid.JID,
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
287 to_email: str | None,
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 body: str,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
289 subject: str | None,
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
290 extra: SendMailExtra | None = None,
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 ) -> None:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 """Send an email using sender credentials.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
293
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 Credentials will be retrieve from cache, or database.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
295
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
296 @param from_jid: Bare JID of the sender.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
297 @param to_email: Email address of the destinee.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
298 @param body: Body of the email.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 @param subject: Subject of the email.
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
300 @param extra: Extra data.
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
301
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
302 @raise exceptions.UnknownEntityError: Credentials for "from_jid" can't be found.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
303 """
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
304 assert self.client is not None
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
305 if extra is None:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
306 extra = SendMailExtra()
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
307 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
308 raise exceptions.InternalError(
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
309 '"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
310 )
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
311
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
312 # We need a bare jid.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
313 assert self.storage is not None
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
314 assert not from_jid.resource
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
315 try:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
316 user_data = self.users_data[from_jid]
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
317 except KeyError:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
318 key = KEY_CREDENTIALS.format(from_jid=from_jid)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
319 credentials = await self.storage.get(key)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
320 if credentials is None:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 raise exceptions.UnknownEntityError(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 f"No credentials found for {from_jid}."
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
323 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
324 self.users_data[from_jid] = UserData(credentials)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
325 else:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
326 credentials = user_data.credentials
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
327
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
328 msg = MIMEText(body, "plain", "UTF-8")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
329 if subject is not None:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
330 msg["Subject"] = subject
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
331 msg["From"] = formataddr(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
332 (credentials["user_name"] or None, credentials["user_email"])
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
333 )
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
334 if extra.addresses:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
335 assert extra.addresses.to
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
336 main_to_address = extra.addresses.to[0]
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
337 assert main_to_address.jid
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
338 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
339 for field in RECIPIENT_FIELDS:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
340 addresses = getattr(extra.addresses, field)
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
341 if not addresses:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
342 continue
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
343 for address in addresses:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
344 if not address.delivered and (
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
345 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
346 ):
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
347 log.warning(
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
348 "Received undelivered message to external JID, this is not "
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
349 "allowed! Cancelling the message sending."
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
350 )
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
351 stanza_err = jabber_error.StanzaError(
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
352 "forbidden",
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
353 text="Multicasting (XEP-0033 addresses) can only be used "
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
354 "with JID from this gateway, not external ones. "
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
355 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
356 "should be delivered by server instead.",
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
357 )
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
358 raise stanza_err
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
359 email_addresses = [
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
360 self.jid_to_email(self.client, address.jid, credentials)
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
361 for address in addresses
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
362 if address.jid
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
363 ]
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
364 if email_addresses:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
365 msg[field.upper()] = ", ".join(email_addresses)
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
366 else:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
367 assert to_email is not None
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
368 msg["To"] = to_email
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
369
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
370 sender_domain = credentials["user_email"].split("@", 1)[-1]
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
371
4317
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
372 if extra.headers:
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
373 if extra.headers.keywords:
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
374 msg["Keywords"] = extra.headers.keywords
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
375 if extra.headers.urgency:
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
376 urgency = extra.headers.urgency
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
377 if urgency == Urgency.medium:
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
378 importance = "normal"
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
379 else:
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
380 importance = urgency
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
381 msg["Importance"] = importance
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
382
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
383 await smtp.sendmail(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
384 credentials["smtp_host"].encode(),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
385 credentials["user_email"].encode(),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
386 [to_email.encode()],
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
387 msg.as_bytes(),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
388 senderDomainName=sender_domain,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
389 port=int(credentials["smtp_port"]),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
390 username=credentials["smtp_username"].encode(),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
391 password=credentials["smtp_password"].encode(),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
392 requireAuthentication=True,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
393 # 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
394 # too.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
395 requireTransportSecurity=True,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
396 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
397
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
398 async def _on_registration_form(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
399 self, client: SatXMPPEntity, iq_elt: domish.Element
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
400 ) -> tuple[bool, data_form.Form] | None:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
401 if client != self.client:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
402 return
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
403 assert self.storage is not None
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
404 from_jid = jid.JID(iq_elt["from"])
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
405 key = KEY_CREDENTIALS.format(from_jid=from_jid.userhost())
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
406 credentials = await self.storage.get(key) or {}
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
407
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
408 form = data_form.Form(formType="form", title="IMAP/SMTP Credentials")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
409
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
410 # Add instructions
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
411 form.instructions = [
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
412 D_(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
413 "Please provide your IMAP and SMTP credentials to configure the "
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
414 "connection."
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
415 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
416 ]
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
417
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
418 # Add identity fields
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
419 form.addField(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
420 data_form.Field(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
421 fieldType="text-single",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
422 var="user_name",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
423 label="User Name",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
424 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
425 value=credentials.get("user_name"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
426 required=True,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
427 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
428 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
429
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
430 form.addField(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
431 data_form.Field(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
432 fieldType="text-single",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
433 var="user_email",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
434 label="User Email",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
435 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
436 value=credentials.get("user_email"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
437 required=True,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
438 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
439 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
440
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
441 # Add fields for IMAP credentials
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
442 form.addField(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
443 data_form.Field(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
444 fieldType="text-single",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
445 var="imap_host",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
446 label="IMAP Host",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
447 desc=D_("IMAP server hostname or IP address"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
448 value=credentials.get("imap_host"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
449 required=True,
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 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
452 form.addField(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
453 data_form.Field(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
454 fieldType="text-single",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
455 var="imap_port",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
456 label="IMAP Port",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
457 desc=D_("IMAP server port (default: 993)"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
458 value=credentials.get("imap_port", "993"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
459 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
460 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
461 form.addField(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
462 data_form.Field(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
463 fieldType="text-single",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
464 var="imap_username",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
465 label="IMAP Username",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
466 desc=D_("Username for IMAP authentication"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
467 value=credentials.get("imap_username"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
468 required=True,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
469 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
470 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
471 form.addField(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
472 data_form.Field(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
473 fieldType="text-private",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
474 var="imap_password",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
475 label="IMAP Password",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
476 desc=D_("Password for IMAP authentication"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
477 value=credentials.get("imap_password"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
478 required=True,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
479 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
480 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
481
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
482 # Add fields for SMTP credentials
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
483 form.addField(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
484 data_form.Field(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
485 fieldType="text-single",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
486 var="smtp_host",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
487 label="SMTP Host",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
488 desc=D_("SMTP server hostname or IP address"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
489 value=credentials.get("smtp_host"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
490 required=True,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
491 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
492 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
493 form.addField(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
494 data_form.Field(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
495 fieldType="text-single",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
496 var="smtp_port",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
497 label="SMTP Port",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
498 desc=D_("SMTP server port (default: 587)"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
499 value=credentials.get("smtp_port", "587"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
500 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
501 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
502 form.addField(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
503 data_form.Field(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
504 fieldType="text-single",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
505 var="smtp_username",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
506 label="SMTP Username",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
507 desc=D_("Username for SMTP authentication"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
508 value=credentials.get("smtp_username"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
509 required=True,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
510 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
511 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
512 form.addField(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
513 data_form.Field(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
514 fieldType="text-private",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
515 var="smtp_password",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
516 label="SMTP Password",
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
517 desc=D_("Password for SMTP authentication"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
518 value=credentials.get("smtp_password"),
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
519 required=True,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
520 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
521 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
522
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
523 return bool(credentials), form
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
524
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
525 def validate_field(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
526 self,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
527 form: data_form.Form,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
528 key: str,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
529 field_type: str,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
530 min_value: int | None = None,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
531 max_value: int | None = None,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
532 default: str | int | None = None,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
533 ) -> None:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
534 """Validate a single field.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
535
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
536 @param form: The form containing the fields.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
537 @param key: The key of the field to validate.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
538 @param field_type: The expected type of the field value.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
539 @param min_value: Optional minimum value for integer fields.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
540 @param max_value: Optional maximum value for integer fields.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
541 @param default: Default value to use if the field is missing.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
542 @raise StanzaError: If the field value is invalid or missing.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
543 """
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
544 field = form.fields.get(key)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
545 if field is None:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
546 if default is None:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
547 raise StanzaError("bad-request", text=f"{key} is required")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
548 field = data_form.Field(var=key, value=str(default))
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
549 form.addField(field)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
550
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
551 value = field.value
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
552 if field_type == "int":
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
553 try:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
554 value = int(value)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
555 if (min_value is not None and value < min_value) or (
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
556 max_value is not None and value > max_value
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
557 ):
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
558 raise ValueError
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
559 except (ValueError, TypeError):
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
560 raise StanzaError("bad-request", text=f"Invalid value for {key}: {value}")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
561 elif field_type == "str":
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
562 if not isinstance(value, str):
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
563 raise StanzaError("bad-request", text=f"Invalid value for {key}: {value}")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
564
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
565 # Basic email validation for user_email field
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
566 if key == "user_email":
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
567 # 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
568 # difficult.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
569 if not email_pattern.match(value):
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
570 raise StanzaError(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
571 "bad-request", text=f"Invalid email address: {value}"
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
572 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
573
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
574 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
575 """Validate the submitted IMAP/SMTP credentials form.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
576
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
577 @param submit_form: The submitted form containing IMAP/SMTP credentials.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
578 @raise StanzaError: If any of the values are invalid.
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 # Validate identity fields
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
581 self.validate_field(submit_form, "user_name", "str")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
582 self.validate_field(submit_form, "user_email", "str")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
583
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
584 # Validate IMAP fields
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
585 self.validate_field(submit_form, "imap_host", "str")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
586 self.validate_field(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
587 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
588 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
589 self.validate_field(submit_form, "imap_username", "str")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
590 self.validate_field(submit_form, "imap_password", "str")
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 # Validate SMTP fields
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
593 self.validate_field(submit_form, "smtp_host", "str")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
594 self.validate_field(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
595 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
596 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
597 self.validate_field(submit_form, "smtp_username", "str")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
598 self.validate_field(submit_form, "smtp_password", "str")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
599
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
600 def email_to_jid(
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
601 self,
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
602 client: SatXMPPEntity,
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
603 user_email: str,
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
604 user_jid: jid.JID,
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
605 email_name: str,
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
606 email_addr: str,
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
607 ) -> tuple[jid.JID, str | None]:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
608 """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
609
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
610 @param client: Client session.
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
611 @param user_email: Email address of the gateway user.
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
612 @param user_jid: JID of the gateway user.
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
613 @param email_name: Email associated name.
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
614 @param email_addr: Email address.
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
615 @return: Tuple of JID and name (if present).
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
616 """
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
617 email_name = email_name.strip()
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
618 if email_name.startswith("xmpp:"):
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
619 return jid.JID(email_name[5:]), None
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
620 elif email_addr == user_email:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
621 return (user_jid, None)
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
622 else:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
623 return (
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
624 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
625 email_name or None,
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
626 )
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
627
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
628 async def on_new_email(
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
629 self, user_data: UserData, user_jid: jid.JID, email: EmailMessage
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
630 ) -> None:
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
631 """Called when a new message has been received.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
632
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
633 @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
634 jid.
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
635 @param user_jid: JID of the recipient.
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
636 @param email: Parsed email.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
637 """
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
638 assert self.client is not None
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
639 user_email = user_data.credentials["user_email"]
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
640 name, email_addr = parseaddr(email["from"])
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
641 email_addr = email_addr.lower()
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
642 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
643
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
644 # Get the email body
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
645 body_mime = email.get_body(("plain",))
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
646 if body_mime is not None:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
647 charset = body_mime.get_content_charset() or "utf-8"
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
648 body = body_mime.get_payload(decode=True).decode(charset, errors="replace")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
649 else:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
650 log.warning(f"No body found in email:\n{email}")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
651 body = ""
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
652
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
653 # Decode the subject
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
654 subject = email.get("subject")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
655 if subject:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
656 decoded_subject = decode_header(subject)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
657 subject = "".join(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
658 [
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
659 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
660 for part, encoding in decoded_subject
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
661 ]
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
662 ).strip()
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
663 else:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
664 subject = None
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
665
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
666 # Parse recipient fields
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
667 kwargs = {}
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
668 for field in RECIPIENT_FIELDS:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
669 email_addresses = email.get_all(field)
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
670 if email_addresses:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
671 jids_and_names = [
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
672 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
673 for name, addr in getaddresses(email_addresses)
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
674 ]
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
675 kwargs[field] = [
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
676 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
677 ]
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
678
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
679 # 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
680 assert kwargs
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
681 addresses_data = AddressesData(**kwargs)
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
682
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
683 # Parse reply-to field
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
684 reply_to_addresses = email.get_all("reply-to")
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
685 if reply_to_addresses:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
686 jids_with_names = [
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
687 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
688 for name, addr in getaddresses(reply_to_addresses)
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
689 ]
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
690 addresses_data.replyto = [
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
691 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
692 ]
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
693
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
694 # Set noreply flag
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
695 # 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
696 # from and reply-to headers.
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
697 from_addresses = [email_addr]
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
698 if reply_to_addresses:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
699 from_addresses.extend(
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
700 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
701 )
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
702 for from_address in from_addresses:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
703 from_user_part = from_address.split("@", 1)[0].lower()
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
704 if from_user_part in (
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
705 "no-reply",
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
706 "noreply",
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
707 "do-not-reply",
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
708 "donotreply",
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
709 "notification",
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
710 "notifications",
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
711 ):
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
712 addresses_data.noreply = True
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
713 break
4317
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
714 extra = {}
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
715
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
716 if (
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
717 not addresses_data.replyto
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
718 and not addresses_data.noreply
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
719 and not addresses_data.cc
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
720 and not addresses_data.bcc
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
721 and addresses_data.to == [AddressType(jid=user_jid)]
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
722 ):
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
723 # 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
724 # 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
725 pass
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
726 else:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
727 for address in addresses_data.addresses:
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
728 if address.jid and (
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
729 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
730 ):
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
731 # 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
732 # other JID addresses will have to be delivered by us.
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
733 address.delivered = True
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
734
4317
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
735 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
736
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
737 # We look for interesting headers
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
738 headers = {}
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
739 keywords_headers = email.get_all("keywords")
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
740 if keywords_headers:
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
741 keywords = ",".join(keywords_headers)
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
742 headers["keywords"] = keywords
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
743
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
744 importance = email["importance"]
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
745 if importance:
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
746 # We convert to urgency
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
747 if importance in ("low", "high"):
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
748 headers["urgency"] = importance
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
749 elif importance == "normal":
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
750 headers["urgency"] = "medium"
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
751 else:
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
752 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
753
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
754 if headers:
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
755 extra["headers"] = HeadersData(**headers).model_dump(
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
756 mode="json", exclude_none=True
055930cc81f9 component email gateway: Add support for XEP-0131 headers:
Goffi <goffi@goffi.org>
parents: 4309
diff changeset
757 )
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
758
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
759 client = self.client.get_virtual_client(from_jid)
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
760 await client.sendMessage(
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
761 user_jid,
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
762 {"": body},
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
763 {"": subject} if subject else None,
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
764 extra=extra,
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
765 )
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
766
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
767 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
768 """Connect to IMAP service.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
769
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
770 [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
771
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
772 @param from_jid: JID of the user associated with given credentials.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
773 @param credentials: Email credentials.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
774 """
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
775 credentials = user_data.credentials
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
776
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
777 connected = defer.Deferred()
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
778 factory = IMAPClientFactory(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
779 user_data,
4309
b56b1eae7994 component email gateway: add multicasting:
Goffi <goffi@goffi.org>
parents: 4303
diff changeset
780 partial(self.on_new_email, user_data, from_jid.userhostJID()),
4303
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
781 connected,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
782 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
783 reactor.connectTCP(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
784 credentials["imap_host"], int(credentials["imap_port"]), factory
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
785 )
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
786 await connected
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
787
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
788 async def _on_registration_submit(
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
789 self,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
790 client: SatXMPPEntity,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
791 iq_elt: domish.Element,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
792 submit_form: data_form.Form | None,
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
793 ) -> bool | None:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
794 """Handle registration submit request.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
795
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
796 Submit form is validated, and credentials are stored.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
797 @param client: client session.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
798 iq_elt: IQ stanza of the submission request.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
799 submit_form: submit form.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
800 @return: True if successful.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
801 None if the callback is not relevant for this request.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
802 """
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
803 if client != self.client:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
804 return
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
805 assert self.storage is not None
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
806 from_jid = jid.JID(iq_elt["from"]).userhostJID()
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
807
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
808 if submit_form is None:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
809 # This is an unregistration request.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
810 try:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
811 user_data = self.users_data[from_jid]
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
812 except KeyError:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
813 pass
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
814 else:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
815 if user_data.imap_client is not None:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
816 try:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
817 await user_data.imap_client.logout()
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
818 except Exception:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
819 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
820 key = KEY_CREDENTIALS.format(from_jid=from_jid)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
821 await self.storage.adel(key)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
822 log.info(f"{from_jid} unregistered from this gateway.")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
823 return True
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
824
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
825 self.validate_imap_smtp_form(submit_form)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
826 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
827 user_data = self.users_data.get(from_jid)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
828 if user_data is None:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
829 # The user is not in cache, we cache current credentials.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
830 user_data = self.users_data[from_jid] = UserData(credentials=credentials)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
831 else:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
832 # The user is known, we update credentials.
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
833 user_data.credentials = credentials
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
834 key = KEY_CREDENTIALS.format(from_jid=from_jid)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
835 try:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
836 await self.connect_imap(from_jid, user_data)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
837 except Exception as e:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
838 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
839 credentials["imap_success"] = False
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
840 await self.storage.aset(key, credentials)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
841 raise e
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
842 else:
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
843 log.debug(f"Connection successful to IMAP server for {from_jid}")
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
844 credentials["imap_success"] = True
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
845 await self.storage.aset(key, credentials)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
846 return True
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
847
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
848
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
849 @implementer(iwokkel.IDisco)
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
850 class EmailGatewayHandler(XMPPHandler):
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
851
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
852 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
853 return []
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
854
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
855 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
a7ec325246fb component email-gateway: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
856 return []