|
24 | 24 | from email.mime.multipart import MIMEMultipart |
25 | 25 | from email.mime.text import MIMEText |
26 | 26 | from io import BytesIO |
27 | | -from typing import TYPE_CHECKING, Any, Dict, Optional |
| 27 | +from typing import TYPE_CHECKING, Dict, Optional |
28 | 28 |
|
29 | | -from pkg_resources import parse_version |
30 | | - |
31 | | -import twisted |
32 | 29 | from twisted.internet.defer import Deferred |
33 | 30 | from twisted.internet.endpoints import HostnameEndpoint |
34 | | -from twisted.internet.interfaces import IOpenSSLContextFactory, IProtocolFactory |
| 31 | +from twisted.internet.interfaces import IProtocolFactory |
35 | 32 | from twisted.internet.ssl import optionsForClientTLS |
36 | | -from twisted.mail.smtp import ESMTPSender, ESMTPSenderFactory |
| 33 | +from twisted.mail.smtp import ESMTPSenderFactory |
37 | 34 | from twisted.protocols.tls import TLSMemoryBIOFactory |
38 | 35 |
|
39 | 36 | from synapse.logging.context import make_deferred_yieldable |
|
44 | 41 |
|
45 | 42 | logger = logging.getLogger(__name__) |
46 | 43 |
|
47 | | -_is_old_twisted = parse_version(twisted.__version__) < parse_version("21") |
48 | | - |
49 | | - |
50 | | -class _BackportESMTPSender(ESMTPSender): |
51 | | - """Extend old versions of ESMTPSender to configure TLS. |
52 | | -
|
53 | | - Unfortunately, before Twisted 21.2, ESMTPSender doesn't give an easy way to |
54 | | - disable TLS, or to configure the hostname used for TLS certificate validation. |
55 | | - This backports the `hostname` parameter for that functionality. |
56 | | - """ |
57 | | - |
58 | | - __hostname: Optional[str] |
59 | | - |
60 | | - def __init__(self, *args: Any, **kwargs: Any) -> None: |
61 | | - """""" |
62 | | - self.__hostname = kwargs.pop("hostname", None) |
63 | | - super().__init__(*args, **kwargs) |
64 | | - |
65 | | - def _getContextFactory(self) -> Optional[IOpenSSLContextFactory]: |
66 | | - if self.context is not None: |
67 | | - return self.context |
68 | | - elif self.__hostname is None: |
69 | | - return None # disable TLS if hostname is None |
70 | | - return optionsForClientTLS(self.__hostname) |
71 | | - |
72 | | - |
73 | | -class _BackportESMTPSenderFactory(ESMTPSenderFactory): |
74 | | - """An ESMTPSenderFactory for _BackportESMTPSender. |
75 | | -
|
76 | | - This backports the `hostname` parameter, to disable or configure TLS. |
77 | | - """ |
78 | | - |
79 | | - __hostname: Optional[str] |
80 | | - |
81 | | - def __init__(self, *args: Any, **kwargs: Any) -> None: |
82 | | - self.__hostname = kwargs.pop("hostname", None) |
83 | | - super().__init__(*args, **kwargs) |
84 | | - |
85 | | - def protocol(self, *args: Any, **kwargs: Any) -> ESMTPSender: # type: ignore |
86 | | - # this overrides ESMTPSenderFactory's `protocol` attribute, with a Callable |
87 | | - # instantiating our _BackportESMTPSender, providing the hostname parameter |
88 | | - return _BackportESMTPSender(*args, **kwargs, hostname=self.__hostname) |
89 | | - |
90 | 44 |
|
91 | 45 | async def _sendmail( |
92 | 46 | reactor: ISynapseReactor, |
@@ -129,9 +83,7 @@ async def _sendmail( |
129 | 83 | elif tlsname is None: |
130 | 84 | tlsname = smtphost |
131 | 85 |
|
132 | | - factory: IProtocolFactory = ( |
133 | | - _BackportESMTPSenderFactory if _is_old_twisted else ESMTPSenderFactory |
134 | | - )( |
| 86 | + factory: IProtocolFactory = ESMTPSenderFactory( |
135 | 87 | username, |
136 | 88 | password, |
137 | 89 | from_addr, |
|
0 commit comments