mirror of
https://github.com/element-hq/synapse.git
synced 2025-11-30 00:00:40 -05:00
Remove some obsolete Twisted version checks. (#18729)
Follows: #18727 --------- Signed-off-by: Olivier 'reivilibre <oliverw@matrix.org>
This commit is contained in:
parent
a2ba909ded
commit
16a639e0fe
1
changelog.d/18729.misc
Normal file
1
changelog.d/18729.misc
Normal file
@ -0,0 +1 @@
|
|||||||
|
Bump minimum version bound on Twisted to 21.2.0.
|
||||||
@ -45,16 +45,6 @@ if py_version < (3, 9):
|
|||||||
|
|
||||||
# Allow using the asyncio reactor via env var.
|
# Allow using the asyncio reactor via env var.
|
||||||
if strtobool(os.environ.get("SYNAPSE_ASYNC_IO_REACTOR", "0")):
|
if strtobool(os.environ.get("SYNAPSE_ASYNC_IO_REACTOR", "0")):
|
||||||
from incremental import Version
|
|
||||||
|
|
||||||
import twisted
|
|
||||||
|
|
||||||
# We need a bugfix that is included in Twisted 21.2.0:
|
|
||||||
# https://twistedmatrix.com/trac/ticket/9787
|
|
||||||
if twisted.version < Version("Twisted", 21, 2, 0):
|
|
||||||
print("Using asyncio reactor requires Twisted>=21.2.0")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from twisted.internet import asyncioreactor
|
from twisted.internet import asyncioreactor
|
||||||
|
|||||||
@ -24,16 +24,13 @@ import logging
|
|||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from typing import TYPE_CHECKING, Any, Dict, Optional
|
from typing import TYPE_CHECKING, Dict, Optional
|
||||||
|
|
||||||
from pkg_resources import parse_version
|
|
||||||
|
|
||||||
import twisted
|
|
||||||
from twisted.internet.defer import Deferred
|
from twisted.internet.defer import Deferred
|
||||||
from twisted.internet.endpoints import HostnameEndpoint
|
from twisted.internet.endpoints import HostnameEndpoint
|
||||||
from twisted.internet.interfaces import IOpenSSLContextFactory, IProtocolFactory
|
from twisted.internet.interfaces import IProtocolFactory
|
||||||
from twisted.internet.ssl import optionsForClientTLS
|
from twisted.internet.ssl import optionsForClientTLS
|
||||||
from twisted.mail.smtp import ESMTPSender, ESMTPSenderFactory
|
from twisted.mail.smtp import ESMTPSenderFactory
|
||||||
from twisted.protocols.tls import TLSMemoryBIOFactory
|
from twisted.protocols.tls import TLSMemoryBIOFactory
|
||||||
|
|
||||||
from synapse.logging.context import make_deferred_yieldable
|
from synapse.logging.context import make_deferred_yieldable
|
||||||
@ -44,49 +41,6 @@ if TYPE_CHECKING:
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
_is_old_twisted = parse_version(twisted.__version__) < parse_version("21")
|
|
||||||
|
|
||||||
|
|
||||||
class _BackportESMTPSender(ESMTPSender):
|
|
||||||
"""Extend old versions of ESMTPSender to configure TLS.
|
|
||||||
|
|
||||||
Unfortunately, before Twisted 21.2, ESMTPSender doesn't give an easy way to
|
|
||||||
disable TLS, or to configure the hostname used for TLS certificate validation.
|
|
||||||
This backports the `hostname` parameter for that functionality.
|
|
||||||
"""
|
|
||||||
|
|
||||||
__hostname: Optional[str]
|
|
||||||
|
|
||||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
||||||
""""""
|
|
||||||
self.__hostname = kwargs.pop("hostname", None)
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
|
|
||||||
def _getContextFactory(self) -> Optional[IOpenSSLContextFactory]:
|
|
||||||
if self.context is not None:
|
|
||||||
return self.context
|
|
||||||
elif self.__hostname is None:
|
|
||||||
return None # disable TLS if hostname is None
|
|
||||||
return optionsForClientTLS(self.__hostname)
|
|
||||||
|
|
||||||
|
|
||||||
class _BackportESMTPSenderFactory(ESMTPSenderFactory):
|
|
||||||
"""An ESMTPSenderFactory for _BackportESMTPSender.
|
|
||||||
|
|
||||||
This backports the `hostname` parameter, to disable or configure TLS.
|
|
||||||
"""
|
|
||||||
|
|
||||||
__hostname: Optional[str]
|
|
||||||
|
|
||||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
||||||
self.__hostname = kwargs.pop("hostname", None)
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
|
|
||||||
def protocol(self, *args: Any, **kwargs: Any) -> ESMTPSender: # type: ignore
|
|
||||||
# this overrides ESMTPSenderFactory's `protocol` attribute, with a Callable
|
|
||||||
# instantiating our _BackportESMTPSender, providing the hostname parameter
|
|
||||||
return _BackportESMTPSender(*args, **kwargs, hostname=self.__hostname)
|
|
||||||
|
|
||||||
|
|
||||||
async def _sendmail(
|
async def _sendmail(
|
||||||
reactor: ISynapseReactor,
|
reactor: ISynapseReactor,
|
||||||
@ -129,9 +83,7 @@ async def _sendmail(
|
|||||||
elif tlsname is None:
|
elif tlsname is None:
|
||||||
tlsname = smtphost
|
tlsname = smtphost
|
||||||
|
|
||||||
factory: IProtocolFactory = (
|
factory: IProtocolFactory = ESMTPSenderFactory(
|
||||||
_BackportESMTPSenderFactory if _is_old_twisted else ESMTPSenderFactory
|
|
||||||
)(
|
|
||||||
username,
|
username,
|
||||||
password,
|
password,
|
||||||
from_addr,
|
from_addr,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user