]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-storage: Add mailbox exclusion plugin API
authorMichael M Slusarz <michael.slusarz@open-xchange.com>
Thu, 22 Jul 2021 20:20:25 +0000 (14:20 -0600)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Thu, 5 Aug 2021 12:34:46 +0000 (12:34 +0000)
Allows mailbox exclusion configuration to be easily added to any
plugin.

src/lib-storage/Makefile.am
src/lib-storage/mailbox-match-plugin.c [new file with mode: 0644]
src/lib-storage/mailbox-match-plugin.h [new file with mode: 0644]

index e8cac8869b37c17c6b22a0917662d7645cdedc94..ddf4c9a83b6fce35aadf86f6eefdac0250bfa141 100644 (file)
@@ -60,6 +60,7 @@ libstorage_la_SOURCES = \
        mailbox-list.c \
        mailbox-list-notify.c \
        mailbox-list-register.c \
+       mailbox-match-plugin.c \
        mailbox-recent-flags.c \
        mailbox-search-result.c \
        mailbox-tree.c \
@@ -97,6 +98,7 @@ headers = \
        mailbox-list-iter.h \
        mailbox-list-private.h \
        mailbox-list-notify.h \
+       mailbox-match-plugin.h \
        mailbox-recent-flags.h \
        mailbox-search-result-private.h \
        mailbox-tree.h \
diff --git a/src/lib-storage/mailbox-match-plugin.c b/src/lib-storage/mailbox-match-plugin.c
new file mode 100644 (file)
index 0000000..276d134
--- /dev/null
@@ -0,0 +1,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);
+}
diff --git a/src/lib-storage/mailbox-match-plugin.h b/src/lib-storage/mailbox-match-plugin.h
new file mode 100644 (file)
index 0000000..c6dbfe9
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef MAILBOX_MATCH_PLUGIN_H
+#define MAILBOX_MATCH_PLUGIN_H
+
+struct mailbox;
+
+/* Utility library to allow a Dovecot plugin an easy way to configure a list
+   of mailbox patterns and special-use flags that can be matched against. */
+
+struct mailbox_match_plugin *
+mailbox_match_plugin_init(struct mail_user *user, const char *set_prefix);
+void mailbox_match_plugin_deinit(struct mailbox_match_plugin **match);
+
+bool mailbox_match_plugin_exclude(struct mailbox_match_plugin *match,
+                                 struct mailbox *box);
+
+#endif