#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.


def check_smtp_arguments(params):
    _description, settings = params
    args = []

    if "expect" in settings:
        args += ["-e", settings["expect"]]

    if "port" in settings:
        port = int(settings["port"])  # ValueSpec was broken, convert to int
        args += ["-p", port]

    # Be compatible to legacy option
    if "ip_version" in settings:
        settings["address_family"] = settings.pop("ip_version")

    # Use the address family of the monitored host by default
    address_family = settings.get("address_family")
    if address_family is None:
        address_family = "ipv6" if is_ipv6_primary(host_name()) else "ipv4"

    if address_family == "ipv6":
        args.append("-6")
        address = "$_HOSTADDRESS_6$"
    else:
        args.append("-4")
        address = "$_HOSTADDRESS_4$"

    for s in settings.get("commands", []):
        args += ["-C", s]

    for s in settings.get("command_responses", []):
        args += ["-R", s]

    if settings.get("from"):
        args += ["-f", settings["from"]]

    if "response_time" in settings:
        warn, crit = settings["response_time"]
        args += ["-w", "%0.4f" % warn]
        args += ["-c", "%0.4f" % crit]

    if "timeout" in settings:
        args += ["-t", settings["timeout"]]

    if "auth" in settings:
        username, password = settings["auth"]
        args += ["-A", "LOGIN", "-U", username, "-P", passwordstore_get_cmdline("%s", password)]

    if settings.get("starttls", False):
        args.append("-S")

    if "fqdn" in settings:
        args += ["-F", settings["fqdn"]]

    if "cert_days" in settings:
        # legacy behavior
        if isinstance(settings["cert_days"], int):
            args += ["-D", settings["cert_days"]]
        else:
            warn, crit = settings["cert_days"]
            args += ["-D", "%d,%d" % (warn, crit)]

    if "hostname" in settings:
        args += ["-H", settings["hostname"]]
    else:
        args += ["-H", address]

    return args


def check_smtp_desc(params):
    if params[0].startswith("^"):
        return params[0][1:]
    return "SMTP %s" % params[0]


active_check_info["smtp"] = {
    "command_line": "check_smtp $ARG1$",
    "argument_function": check_smtp_arguments,
    "service_description": check_smtp_desc,
    "has_perfdata": True,
}
