/* Core mail-sending module.
 *
 * IRC Services is copyright (c) 1996-2002 Andrew Church.
 *     E-mail: <achurch@achurch.org>
 * Parts written by Andrew Kempe and others.
 * This program is free but copyrighted software; see the file COPYING for
 * details.
 */

#include "services.h"
#include "modules.h"
#include "conffile.h"
#include "language.h"
#include "mail.h"
#include "mail-local.h"

/*************************************************************************/

static Module *module;

static char *FromAddress;
static char *FromName;

EXPORT_VAR(void *,low_send)
int (*low_send)(const char *from, const char *fromname, const char *to,
		const char *subject, const char *body) = NULL;

/*************************************************************************/
/***************************** Mail sending ******************************/
/*************************************************************************/

/* The function to actually send mail.  All parameters must be filled in,
 * `to' must be a valid E-mail address, and only the body may contain
 * newlines.  Return value is 0 for success, 1 for temporary failure (no
 * resources), and -1 for permanent failure (invalid parameter, no such
 * user).
 *
 * NOTE: A return value of 0 does not guarantee that the mail was
 *       successfully sent!  It only indicates that the mail subsystem
 *       accepted the mail and will attempt to deliver it.
 */

EXPORT_FUNC(sendmail)
int sendmail(const char *to, const char *subject, const char *body)
{
    if (!low_send) {
	module_log("sendmail(): No low-level mail module installed!");
	return -1;
    }
    if (!to || !subject || !body) {
	module_log("sendmail(): Got a NULL parameter!");
	return -1;
    }
    if (!valid_email(to)) {
	module_log("sendmail(): Destination address is invalid: %s", to);
	return -1;
    }
    if (strchr(subject, '\n')) {
	module_log("sendmail(): Subject contains newlines (invalid)");
	return -1;
    }
    if (debug)
	module_log("debug: sendmail: from=%s to=%s subject=[%s]",
		   FromAddress, to, subject);
    if (debug >= 2)
	module_log("debug: sendmail: body=[%s]", body);
    return low_send(FromAddress, FromName, to, subject, body);
}

/*************************************************************************/
/***************************** Module stuff ******************************/
/*************************************************************************/

const int32 module_version = MODULE_VERSION_CODE;

ConfigDirective module_config[] = {
    { "FromAddress",      { { CD_STRING, CF_DIRREQ, &FromAddress } } },
    { "FromName",         { { CD_STRING, 0, &FromName } } },
    { NULL }
};

/*************************************************************************/

static int check_config(void)
{
    if (!valid_email(FromAddress)) {
	module_log("FromAddress requires a valid E-mail address");
	return 0;
    }
    if (!FromName)
	FromName = sstrdup("");
    if (strchr(FromName, '\n')) {
	module_log("FromName may not contain newlines");
	return 0;
    }
    return 1;
}

/*************************************************************************/

int init_module(Module *module_)
{
    module = module_;

    if (!check_config())
	return 0;

    return 1;
}

/*************************************************************************/

int exit_module(int shutdown_unused)
{
#ifdef CLEAN_COMPILE
    shutdown_unused = shutdown_unused;
#endif

    return 1;
}

/*************************************************************************/
