#!/usr/bin/env python3
#
# Written by Thomas Schneider <qsuscs@qsuscs.de>
# This script is placed in public domain.  If this is not applicable, consider
# it licensed under the CC-0:
# <https://creativecommons.org/publicdomain/zero/1.0/>

try:
    import smtplib
    import sys
    import os

    lmtp_host = sys.argv[3] if len(sys.argv) > 3 else 'localhost'
    lmtp = smtplib.LMTP(lmtp_host, int(sys.argv[1]))

    try:
        # See <http://www.qmail.org/man/man8/qmail-command.html> for qmail command
        # docs and supplied environment variables. We need to replace "1" with an 
        # empty string, as qmail only supports EXT, EXT2, EXT3, EXT4.
        arg_ext = sys.argv[2] if sys.argv[2] != "1" else ""
        lmtp.sendmail(
            os.environ['SENDER'],
            os.environ['EXT' + arg_ext] + "@" + os.environ['HOST'],
            sys.stdin.buffer.read()
        )
    except smtplib.SMTPResponseException as e:
        if 400 <= e.smtp_code < 500:
            exit(111)
        # otherwise, it's either a 5xx aka permanent error or something else
        # is already b0rked, thus raise -> exit(100) -> have qmail return a
        # 5xx error
        else:
            raise
except:
    exit(100)
