# HG changeset patch # User Timo Sirainen # Date 1291606891 0 # Node ID 8d27401008c9af414b07a752f7150d7d3ea3b029 # Parent 5815ff80f1982d3caa791d84d88d9a84ef2b1980 lib-storage: Added missing mailbox-keywords.c for recent commit. diff -r 5815ff80f198 -r 8d27401008c9 src/lib-storage/mailbox-keywords.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lib-storage/mailbox-keywords.c Mon Dec 06 03:41:31 2010 +0000 @@ -0,0 +1,130 @@ +/* Copyright (c) 2002-2010 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "imap-arg.h" +#include "mail-storage-private.h" + +static struct mail_keywords * +mailbox_keywords_create_skip(struct mailbox *box, + const char *const keywords[]) +{ + struct mail_keywords *kw; + + T_BEGIN { + ARRAY_DEFINE(valid_keywords, const char *); + const char *error; + + t_array_init(&valid_keywords, 32); + for (; *keywords != NULL; keywords++) { + if (mailbox_keyword_is_valid(box, *keywords, &error)) + array_append(&valid_keywords, keywords, 1); + } + (void)array_append_space(&valid_keywords); /* NULL-terminate */ + kw = mail_index_keywords_create(box->index, keywords); + } T_END; + return kw; +} + +static bool +mailbox_keywords_are_valid(struct mailbox *box, const char *const keywords[], + const char **error_r) +{ + unsigned int i; + + for (i = 0; keywords[i] != NULL; i++) { + if (!mailbox_keyword_is_valid(box, keywords[i], error_r)) + return FALSE; + } + return TRUE; +} + +int mailbox_keywords_create(struct mailbox *box, const char *const keywords[], + struct mail_keywords **keywords_r) +{ + const char *error, *empty_keyword_list = NULL; + + i_assert(box->opened); + + if (keywords == NULL) + keywords = &empty_keyword_list; + if (!mailbox_keywords_are_valid(box, keywords, &error)) { + mail_storage_set_error(box->storage, MAIL_ERROR_PARAMS, error); + return -1; + } + + *keywords_r = mail_index_keywords_create(box->index, keywords); + return 0; +} + +struct mail_keywords * +mailbox_keywords_create_valid(struct mailbox *box, + const char *const keywords[]) +{ + const char *empty_keyword_list = NULL; + const char *error; + + i_assert(box->opened); + + if (keywords == NULL) + keywords = &empty_keyword_list; + if (mailbox_keywords_are_valid(box, keywords, &error)) + return mail_index_keywords_create(box->index, keywords); + else { + /* found invalid keywords, do this the slow way */ + return mailbox_keywords_create_skip(box, keywords); + } +} + +struct mail_keywords * +mailbox_keywords_create_from_indexes(struct mailbox *box, + const ARRAY_TYPE(keyword_indexes) *idx) +{ + i_assert(box->opened); + + return mail_index_keywords_create_from_indexes(box->index, idx); +} + +void mailbox_keywords_ref(struct mail_keywords *keywords) +{ + mail_index_keywords_ref(keywords); +} + +void mailbox_keywords_unref(struct mail_keywords **keywords) +{ + mail_index_keywords_unref(keywords); +} + +bool mailbox_keyword_is_valid(struct mailbox *box, const char *keyword, + const char **error_r) +{ + unsigned int i, idx; + + i_assert(box->opened); + + /* if it already exists, skip validity checks */ + if (mail_index_keyword_lookup(box->index, keyword, &idx)) + return TRUE; + + if (*keyword == '\0') { + *error_r = "Empty keywords not allowed"; + return FALSE; + } + + /* these are IMAP-specific restrictions, but for now IMAP is all we + care about */ + for (i = 0; keyword[i] != '\0'; i++) { + if (IS_ATOM_SPECIAL((unsigned char)keyword[i])) { + *error_r = "Invalid characters in keyword"; + return FALSE; + } + if ((unsigned char)keyword[i] >= 0x80) { + *error_r = "8bit characters in keyword"; + return FALSE; + } + } + if (i > box->storage->set->mail_max_keyword_length) { + *error_r = "Keyword length too long"; + return FALSE; + } + return TRUE; +}