37babb1d5d86c3d0c3a4c7700ae6b6d253152f20 — Philippe Pepiot 6 months ago 50ebb75
Allow to serve lmtp on tcp and smtp on unix socket

We previously used "sock" parameter to determine if protocol should be
lmtp (unix socket) or smtp (tcp).
Since it make sense to serve both protocols on tcp or unix socket, introduce a
new "protocol" parameter to switch between lmtp and smtp.
Keep backward compat when protocol is not set by using lmtp over unix
socket and smtp over tcp socket.

Also evaluate "sock-group" option only when using unix socket, so the
option is no longer mandatory for tcp where it's unused.
2 files changed, 15 insertions(+), 8 deletions(-)

M config.example.ini
M listssrht-lmtp
M config.example.ini => config.example.ini +4 -1
@@ 90,9 90,12 @@ msgauth-server=mail.sr.ht.local

# Protocol used by the daemon. Either lmtp or smtp. By default ltmp if using
# unix socket and smtp if using tcp socket.
# Path for the lmtp daemon's unix socket. Direct incoming mail to this socket.
# Alternatively, specify IP:PORT and an SMTP server will be run instead.
# Alternatively, specify IP:PORT will run the server using tcp.
# The lmtp daemon will make the unix socket group-read/write for users in this

M listssrht-lmtp => listssrht-lmtp +11 -7
@@ 289,21 289,25 @@ class MailHandler:
        return "250 Message accepted for delivery"

async def create_server():
    sock_gid = getgrnam(cfg("lists.sr.ht::worker", "sock-group")).gr_gid
    pg = await asyncpg.create_pool(dsn=cfg("lists.sr.ht", "connection-string"))
    handler = MailHandler(pg)
    sock = cfg("lists.sr.ht::worker", "sock")
    protocol = cfg("lists.sr.ht::worker", "protocol",
            default="lmtp" if "/" in sock else "smtp")
    if protocol == "smtp":
        def serve():
            return SMTP(handler, enable_SMTPUTF8=True)
        def serve():
            return LMTP(handler, enable_SMTPUTF8=True)
    if "/" in sock:
        await loop.create_unix_server(
                lambda: LMTP(handler, enable_SMTPUTF8=True),
        sock_gid = getgrnam(cfg("lists.sr.ht::worker", "sock-group")).gr_gid
        await loop.create_unix_server(serve, path=sock)
        os.chmod(sock, 0o775)
        os.chown(sock, os.getuid(), sock_gid)
        host, port = sock.split(":")
        await loop.create_server(
                lambda: SMTP(handler, enable_SMTPUTF8=True),
                host=host, port=int(port))
        await loop.create_server(serve, host=host, port=int(port))

def sigint_handler():
    print("Exiting due to SIGINT")