aboutsummaryrefslogtreecommitdiffstats
path: root/antispam-plugin.c
blob: 407b909a3554760643f3ac7931062d5e487714da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * antispam plugin for dovecot
 *
 * Copyright (C) 2004-2007  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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 * 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>

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


/* defined by imap, pop3, lda */
extern void (*hook_mail_storage_created)(struct mail_storage *storage);

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

static pool_t global_pool;
static char **trash_folders = NULL;
static char *default_spam_folders[] = {
	"SPAM",
	NULL
};
static char **spam_folders = default_spam_folders;
static char **unsure_folders = NULL;
bool antispam_can_append_to_spam = FALSE;

static bool mailbox_in_list(struct mailbox *box, char **list)
{
	if (!list)
		return FALSE;

	while (*list) {
		if (mailbox_equals(box, box->storage, *list))
			return TRUE;
		list++;
	}

	return FALSE;
}

bool mailbox_is_spam(struct mailbox *box)
{
	return mailbox_in_list(box, spam_folders);
}

bool mailbox_is_trash(struct mailbox *box)
{
	return mailbox_in_list(box, trash_folders);
}

bool mailbox_is_unsure(struct mailbox *box)
{
	return mailbox_in_list(box, unsure_folders);
}

const char *get_setting(const char *name)
{
	const char *env;

	t_push();
	env = t_strconcat(t_str_ucase(stringify(PLUGINNAME)),
			  "_",
			  name,
			  NULL);
	env = getenv(env);
	t_pop();

	return env;
}

#define __PLUGIN_FUNCTION(name, ioe) \
	name ## _plugin_ ## ioe
#define _PLUGIN_FUNCTION(name, ioe) \
	__PLUGIN_FUNCTION(name, ioe)
#define PLUGIN_FUNCTION(ioe)	\
	_PLUGIN_FUNCTION(PLUGINNAME, ioe)

void PLUGIN_FUNCTION(init)(void)
{
	const char *tmp;
	char * const *iter;

	debug("plugin initialising\n");

	global_pool = pool_alloconly_create("antispam-pool", 1024);

	tmp = get_setting("TRASH");
	if (tmp)
		trash_folders = p_strsplit(global_pool, tmp, ";");

	if (trash_folders) {
		iter = trash_folders;
		while (*iter) {
			debug("\"%s\" is trash folder\n", *iter);
			iter++;
		}
	} else
		debug("no trash folders\n");

	tmp = get_setting("SPAM");
	if (tmp)
		spam_folders = p_strsplit(global_pool, tmp, ";");

	tmp = get_setting("ALLOW_APPEND_TO_SPAM");
	if (tmp && strcasecmp(tmp, "yes") == 0) {
		antispam_can_append_to_spam = TRUE;
		debug("allowing APPEND to spam folders");
	}

	if (spam_folders) {
		iter = spam_folders;
		while (*iter) {
			debug("\"%s\" is spam folder\n", *iter);
			iter++;
		}
	} else
		debug("no spam folders\n");

	tmp = get_setting("UNSURE");
	if (tmp)
		unsure_folders = p_strsplit(global_pool, tmp, ";");

	if (unsure_folders) {
		iter = unsure_folders;
		while (*iter) {
			debug("\"%s\" is unsure folder\n", *iter);
			iter++;
		}
	} else
		debug("no unsure folders\n");

	backend_init(global_pool);

	antispam_next_hook_mail_storage_created = hook_mail_storage_created;
	hook_mail_storage_created = antispam_mail_storage_created;
}

void PLUGIN_FUNCTION(deinit)(void)
{
	hook_mail_storage_created = antispam_next_hook_mail_storage_created;
	backend_exit();
	mempool_unref(&global_pool);
}

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