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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
/*
* 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>
#include <ctype.h>
/* dovecot headers we need */
#include "lib.h"
#include "str.h"
#include "client.h"
#include "mail-storage-private.h"
#include "antispam-version.h"
/* defined by imap, pop3, lda */
extern void (*hook_mail_storage_created)(struct mail_storage *storage);
/* internal stuff we need */
#include "antispam-plugin.h"
/* macro since only needed for dovecot 1.1 */
PLUGIN_ID;
static pool_t global_pool;
static char *default_spam_folders[] = {
"SPAM",
NULL
};
enum match_type {
MT_REG,
MT_PATTERN,
MT_PATTERN_IGNCASE,
/* keep last */
NUM_MT
};
/*
* There are three different matches for each folder type
*
* type usage
* ----------------------------
* MT_REG plain strcmp()
* MT_PATTERN case sensitive match with possible wildcards
* MT_PATTERN_IGNCASE case insensitive match with possible wildcards
*/
static char **trash_folders[] = { NULL, NULL, NULL };
static char **spam_folders[] = { default_spam_folders,NULL, NULL };
static char **unsure_folders[] = { NULL, NULL, NULL };
/* display name of kind of pattern match'ing same order as above */
static const struct {
const char *human, *suffix;
} pmatch_name[NUM_MT] = {
[MT_REG] = { .human = "exact match",
.suffix = "" },
[MT_PATTERN] = { .human = "wildcard match",
.suffix = "_PATTERN" },
[MT_PATTERN_IGNCASE] = { .human = "case-insensitive wildcard match",
.suffix = "_PATTERN_IGNORECASE" },
};
bool antispam_can_append_to_spam = FALSE;
static char **spam_keywords = NULL;
bool need_keyword_hook;
bool need_folder_hook;
/* lower-case string, but keep modified UTF7 unchanged */
void lowercase_string(const char *in, char *out)
{
char ch;
while ((ch = *out++ = i_tolower(*in++))) {
/* lower case */
if (ch == '&') {
/* modified UTF7 -- find end of sequence ('-') */
while ((ch = *out++ = *in++)) {
if (ch == '-')
break;
}
}
}
}
static bool mailbox_patternmatch(struct mailbox *box,
struct mail_storage *storage,
const char *name,
bool lowercase)
{
const char *boxname;
char *lowerboxname;
int len;
int rc;
if (storage && mailbox_get_storage(box) != storage)
return FALSE;
t_push();
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_pop();
return rc;
}
static bool mailbox_patternmatch_case(struct mailbox *box,
struct mail_storage *storage,
const char *name)
{
return mailbox_patternmatch(box, storage, name, FALSE);
}
static bool mailbox_patternmatch_icase(struct mailbox *box,
struct mail_storage *storage,
const char *name)
{
return mailbox_patternmatch(box, storage, name, TRUE);
}
typedef bool (*match_fn_t)(struct mailbox *, struct mail_storage *,
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 ***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, box->storage, *list))
return TRUE;
list++;
}
}
return FALSE;
}
bool mailbox_is_spam(struct mailbox *box)
{
bool ret;
ret = mailbox_in_list(box, spam_folders);
debug_verbose("mailbox_is_spam(%s): %d\n", mailbox_get_name(box), ret);
return ret;
}
bool mailbox_is_trash(struct mailbox *box)
{
bool ret;
ret = mailbox_in_list(box, trash_folders);
debug_verbose("mailbox_is_trash(%s): %d\n", mailbox_get_name(box), ret);
return ret;
}
bool mailbox_is_unsure(struct mailbox *box)
{
bool ret;
ret = mailbox_in_list(box, unsure_folders);
debug_verbose("mailbox_is_unsure(%s): %d\n", mailbox_get_name(box), ret);
return ret;
}
bool keyword_is_spam(const char *keyword)
{
char **k = spam_keywords;
if (!spam_keywords)
return FALSE;
while (*k) {
if (strcmp(*k, keyword) == 0)
return TRUE;
k++;
}
return FALSE;
}
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;
}
int parse_folder_setting(const char *setting, char ***strings,
const char *display_name)
{
const char *tmp;
int cnt = 0;
enum match_type i;
t_push();
for (i = 0; i <= NUM_MT; ++i) {
tmp = get_setting(t_strconcat(setting, pmatch_name[i].suffix,
NULL));
if (tmp) {
strings[i] = p_strsplit(global_pool, tmp, ";");
if (i == 2) {
/* lower case the string */
char **list = strings[i];
while (*list) {
lowercase_string(*list, *list);
++list;
}
}
}
if (strings[i]) {
char **iter = strings[i];
while (*iter) {
++cnt;
debug("\"%s\" is %s %s folder\n", *iter,
pmatch_name[i].human, display_name);
iter++;
}
}
}
t_pop();
if (!cnt)
debug("no %s folders\n", display_name);
return cnt;
}
void PLUGIN_FUNCTION(init)(void)
{
const char *tmp;
char * const *iter;
int spam_folder_count;
debug("plugin initialising (%s)\n", ANTISPAM_VERSION);
global_pool = pool_alloconly_create("antispam-pool", 1024);
parse_folder_setting("TRASH", trash_folders, "trash");
spam_folder_count = parse_folder_setting("SPAM", spam_folders, "spam");
parse_folder_setting("UNSURE", unsure_folders, "unsure");
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");
}
tmp = get_setting("SPAM_KEYWORDS");
if (tmp)
spam_keywords = p_strsplit(global_pool, tmp, ";");
if (spam_keywords) {
iter = spam_keywords;
while (*iter) {
debug("\"%s\" is spam keyword\n", *iter);
iter++;
}
}
/* set spam_folders to empty to only allow keywords */
need_folder_hook = spam_folder_count > 0;
need_keyword_hook = !!spam_keywords;
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;
|