changeset 20418:567fcc09fd80

fts plugin: Added fts_autoindex_exclude settings. fts_autoindex_exclude setting specifies special-use flag or mailbox name to be excluded. - If a name starts with '\', it's treated as a case-insensitive special-use flag. - Multiple names can be specified with serial numbers, for example: plugin { fts_autoindex_exclude = \Junk fts_autoindex_exclude2 = \Trash fts_autoindex_exclude3 = DUMPSTER fts_autoindex_exclude4 = New folder }
author Baofeng Wang <baofeng.wang@dovecot.fi>
date Fri, 10 Jun 2016 14:31:00 +0300
parents 4da83dd8b95c
children 0dc214cf2e30
files src/plugins/fts/fts-storage.c
diffstat 1 files changed, 58 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/fts/fts-storage.c	Mon Jun 27 16:37:18 2016 +0300
+++ b/src/plugins/fts/fts-storage.c	Fri Jun 10 14:31:00 2016 +0300
@@ -6,6 +6,7 @@
 #include "str.h"
 #include "strescape.h"
 #include "write-full.h"
+#include "wildcard-match.h"
 #include "mail-search-build.h"
 #include "mail-storage-private.h"
 #include "mailbox-list-private.h"
@@ -39,6 +40,7 @@
 struct fts_mailbox {
 	union mailbox_module_context module_ctx;
 	struct fts_backend_update_context *sync_update_ctx;
+	bool fts_mailbox_excluded;
 };
 
 struct fts_transaction_context {
@@ -629,7 +631,7 @@
 	bool autoindex;
 	int ret = 0;
 
-	autoindex = ft->mails_saved &&
+	autoindex = ft->mails_saved && !fbox->fts_mailbox_excluded &&
 		mail_user_plugin_getenv(box->storage->user,
 					"fts_autoindex") != NULL;
 
@@ -726,6 +728,60 @@
 	return 0;
 }
 
+static const char *const *fts_exclude_get_patterns(struct mail_user *user)
+{
+	ARRAY_TYPE(const_string) patterns;
+	const char *str;
+	char set_name[21+MAX_INT_STRLEN+1];
+	unsigned int i;
+
+	str = mail_user_plugin_getenv(user, "fts_autoindex_exclude");
+	if (str == NULL)
+		return NULL;
+
+	t_array_init(&patterns, 16);
+	for (i = 2; str != NULL; i++) {
+		array_append(&patterns, &str, 1);
+
+		if (i_snprintf(set_name, sizeof(set_name),
+			       "fts_autoindex_exclude%u", i) < 0)
+			i_unreached();
+		str = mail_user_plugin_getenv(user, set_name);
+	}
+	array_append_zero(&patterns);
+	return array_idx(&patterns, 0);
+}
+
+static bool fts_autoindex_exclude_match(struct mailbox *box)
+{
+	const char *const *exclude_list;
+	unsigned int i;
+	const struct mailbox_settings *set;
+	const char *const *special_use;
+	struct mail_user *user = box->storage->user;
+
+	exclude_list = fts_exclude_get_patterns(user);
+	if (exclude_list == NULL)
+		return TRUE;
+
+	set = mailbox_settings_find(mailbox_get_namespace(box),
+				    mailbox_get_vname(box));
+	special_use = set == NULL ? NULL :
+		t_strsplit_spaces(set->special_use, " ");
+	for (i = 0; exclude_list[i] != NULL; i++) {
+		if (exclude_list[i][0] == '\\') {
+			/* \Special-use flag */
+			if (str_array_icase_find(special_use, exclude_list[i]))
+				return TRUE;
+		} else {
+			/* mailbox name with wildcards */
+			if (wildcard_match(box->name, exclude_list[i]))
+				return TRUE;
+		}
+	}
+	return FALSE;
+}
+
 void fts_mailbox_allocated(struct mailbox *box)
 {
 	struct fts_mailbox_list *flist = FTS_LIST_CONTEXT(box->list);
@@ -738,6 +794,7 @@
 	fbox = p_new(box->pool, struct fts_mailbox, 1);
 	fbox->module_ctx.super = *v;
 	box->vlast = &fbox->module_ctx.super;
+	fbox->fts_mailbox_excluded = fts_autoindex_exclude_match(box);
 
 	v->get_status = fts_mailbox_get_status;
 	v->search_init = fts_mailbox_search_init;