summaryrefslogtreecommitdiffstats
path: root/src/lib-storage/mailbox-match-plugin.c
blob: 276d134084e1e7d31a78025c77ad0dcf1f79fdb5 (plain)
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
/* Copyright (c) 2021 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "array.h"
#include "str.h"
#include "wildcard-match.h"
#include "mail-storage-private.h"
#include "mailbox-match-plugin.h"

struct mailbox_match_plugin {
	ARRAY_TYPE(const_string) patterns;
};

struct mailbox_match_plugin *
mailbox_match_plugin_init(struct mail_user *user, const char *set_prefix)
{
	struct mailbox_match_plugin *match;
	string_t *str;
	const char *value;

	match = i_new(struct mailbox_match_plugin, 1);

	value = mail_user_plugin_getenv(user, set_prefix);
	if (value == NULL)
		return match;

	i_array_init(&match->patterns, 16);
	str = t_str_new(128);
	for (unsigned int i = 2; value != NULL; i++) {
		/* value points to user's settings, so there's no need to
		   strdup() it. */
		array_push_back(&match->patterns, &value);

		str_truncate(str, 0);
		str_printfa(str, "%s%u", set_prefix, i);

		value = mail_user_plugin_getenv(user, str_c(str));
	}

	return match;
}

bool mailbox_match_plugin_exclude(struct mailbox_match_plugin *match,
				  struct mailbox *box)
{
	const struct mailbox_settings *set;
	const char *const *special_use;
	const char *str;

	if (!array_is_created(&match->patterns))
		return FALSE;

	set = mailbox_settings_find(mailbox_get_namespace(box),
				    mailbox_get_vname(box));
	special_use = set == NULL ? NULL :
		t_strsplit_spaces(set->special_use, " ");

	array_foreach_elem(&match->patterns, str) {
		if (str[0] == '\\') {
			/* \Special-use flag */
			if (special_use != NULL &&
			    str_array_icase_find(special_use, str))
				return TRUE;
		} else {
			/* mailbox name with wildcards */
			if (wildcard_match(box->name, str))
				return TRUE;
		}
	}
	return FALSE;
}

void mailbox_match_plugin_deinit(struct mailbox_match_plugin **_match)
{
	struct mailbox_match_plugin *match = *_match;

	if (match == NULL)
		return;
	*_match = NULL;

	array_free(&match->patterns);
	i_free(match);
}