/*
 * antispam plugin for dovecot
 *
 * Copyright (C) 2004-2007, 2025  Johannes Berg <johannes@sipsolutions.net>
 *                    2006  Frank Cusack
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * based on the original framework http://www.dovecot.org/patches/1.0/copy_plugin.c
 *
 * Please see http://johannes.sipsolutions.net/wiki/Projects/dovecot-dspam-integration
 * for more information on this code.
 *
 * Install the plugin in the usual dovecot module location.
 */

#include <stdlib.h>
#include <ctype.h>

/* dovecot headers we need */
#include "lib.h"
#include "str.h"
#include "mail-storage-private.h"
#include "antispam-version.h"
#include "settings.h"

/* internal stuff we need */
#include "antispam-plugin.h"


static bool mailbox_patternmatch(struct mailbox *box,
				 const struct mail_namespace *ns,
				 const char *name, bool lowercase)
{
	const char *boxname;
	char *lowerboxname;
	int len;
	int rc;

	if (ns && mailbox_get_namespace(box) != ns)
		return FALSE;

	T_BEGIN {

	boxname = mailbox_get_name(box);
	if (lowercase) {
		lowerboxname = t_buffer_get(strlen(boxname) + 1);
		lowercase_string(boxname, lowerboxname);
		boxname = lowerboxname;
	}

	len = strlen(name);
	if (len && name[len - 1] == '*') {
		/* any wildcard */
		--len;
	} else {
		/* compare EOS too */
		++len;
	}

	rc = memcmp(name, boxname, len) == 0;

	} T_END;

	return rc;
}

static bool mailbox_patternmatch_case(struct mailbox *box,
				      const struct mail_namespace *ns,
				      const char *name)
{
	return mailbox_patternmatch(box, ns, name, FALSE);
}

static bool mailbox_patternmatch_icase(struct mailbox *box,
				       const struct mail_namespace *ns,
				       const char *name)
{
	return mailbox_patternmatch(box, ns, name, TRUE);
}

static bool _mailbox_equals(struct mailbox *box,
			    const struct mail_namespace *ns,
			    const char *name)
{
	return mailbox_equals(box, ns, name);
}

typedef bool (*match_fn_t)(struct mailbox *,
			   const struct mail_namespace *,
			   const char *);

static const match_fn_t match_fns[NUM_MT] = {
	[MT_REG] = _mailbox_equals,
	[MT_PATTERN] = mailbox_patternmatch_case,
	[MT_PATTERN_IGNCASE] = mailbox_patternmatch_icase,
};

static bool mailbox_in_list(struct mailbox *box, char ** const * patterns)
{
	enum match_type i;
	char **list;

	if (!patterns)
		return FALSE;

	for (i = 0; i < NUM_MT; i++) {
		list = patterns[i];
		if (!list)
			continue;

		while (*list) {
			if (match_fns[i](box, mailbox_get_namespace(box), *list))
				return TRUE;
			list++;
		}
	}

	return FALSE;
}

bool mailbox_is_spam(const struct antispam_settings *cfg, struct mailbox *box)
{
	bool ret;

	ret = mailbox_in_list(box, cfg->spam.folders);
	debug_verbose(cfg, "mailbox_is_spam(%s): %d\n",
		      mailbox_get_name(box), ret);
	return ret;
}

bool mailbox_is_trash(const struct antispam_settings *cfg, struct mailbox *box)
{
	bool ret;

	ret = mailbox_in_list(box, cfg->trash.folders);
	debug_verbose(cfg, "mailbox_is_trash(%s): %d\n",
		      mailbox_get_name(box), ret);
	return ret;
}

bool mailbox_is_unsure(const struct antispam_settings *cfg, struct mailbox *box)
{
	bool ret;

	ret = mailbox_in_list(box, cfg->unsure.folders);
	debug_verbose(cfg, "mailbox_is_unsure(%s): %d\n",
		      mailbox_get_name(box), ret);
	return ret;
}

bool keyword_is_spam(const struct antispam_settings *cfg, const char *keyword)
{
	char **k = cfg->spam_keywords;

	if (!cfg->spam_keywords)
		return FALSE;

	while (*k) {
		if (strcmp(*k, keyword) == 0)
			return TRUE;
		k++;
	}

	return FALSE;
}

void antispam_free_config(struct antispam_settings *cfg)
{
	settings_free(cfg);
}

void PLUGIN_FUNCTION(init)(struct module *module)
{
	antispam_storage_init(module);
}

void PLUGIN_FUNCTION(deinit)(void)
{
	antispam_storage_deinit();
}

/* put dovecot version we built against into plugin for checking */
const char *PLUGIN_FUNCTION(version) = DOVECOT_ABI_VERSION;
