fts-user.c revision fac865bad1ba10e85d80b63dedfd3493a65510d4
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2015 Dovecot authors, see the included COPYING file */
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen#define FTS_DEFAULT_TOKENIZERS "generic email-address"
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen#define FTS_DEFAULT_FILTERS "normalizer-icu snowball"
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen struct fts_tokenizer *index_tokenizer, *search_tokenizer;
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(fts_user_module,
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenstatic const char *const *str_keyvalues_to_array(const char *str)
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen unsigned int i;
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenfts_user_init_languages(struct mail_user *user, struct fts_user *fuser,
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen const char **error_r)
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen const char *lang_config[3] = {NULL, NULL, NULL};
091a2dea9d89734a7c1225eed511b3851693a757Timo Sirainen languages = mail_user_plugin_getenv(user, "fts_languages");
091a2dea9d89734a7c1225eed511b3851693a757Timo Sirainen *error_r = "fts_languages setting is missing";
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen lang_config[1] = mail_user_plugin_getenv(user, "fts_language_config");
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen fuser->lang_list = fts_language_list_init(lang_config);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen if (!fts_language_list_add_names(fuser->lang_list, languages, &unknown)) {
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen "fts_languages: Unknown language '%s'", unknown);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen if (array_count(fts_language_list_get_all(fuser->lang_list)) == 0) {
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenfts_user_create_filters(struct mail_user *user, const struct fts_language *lang,
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen struct fts_filter **filter_r, const char **error_r)
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen struct fts_filter *filter = NULL, *parent = NULL;
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen const char *filters_key, *const *filters, *filter_set_name;
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen unsigned int i;
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen /* try to get the language-specific filters first */
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen filters_key = t_strconcat("fts_filters_", lang->name, NULL);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen str = mail_user_plugin_getenv(user, filters_key);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen /* fallback to global filters */
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen str = mail_user_plugin_getenv(user, filters_key);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen filters_key = "fts_filters(built-in default)";
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen *error_r = t_strdup_printf("%s: Unknown filter '%s'",
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen /* try the language-specific setting first */
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen filter_set_name = t_str_replace(filters[i], '-', '_');
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen set_key = t_strdup_printf("fts_filters_%s_%s",
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen set_key = t_strdup_printf("fts_filters_%s", filter_set_name);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen if (fts_filter_create(filter_class, parent, lang,
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen *error_r = t_strdup_printf("%s: %s", set_key, error);
fa33df8230c2f27ae863ff83d4251923428d53c7Aki Tuomi const char **error_r)
d48e40d6c77d673ad402d96571198d1cce4da225Timo Sirainen struct fts_tokenizer *tokenizer = NULL, *parent = NULL;
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen const char *tokenizers_key, *const *tokenizers, *tokenizer_set_name;
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen unsigned int i;
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen str = mail_user_plugin_getenv(user, tokenizers_key);
7db7fbea5d8a07463b625f93d69166d56018dadfTimo Sirainen tokenizer_class = fts_tokenizer_find(tokenizers[i]);
7db7fbea5d8a07463b625f93d69166d56018dadfTimo Sirainen *error_r = t_strdup_printf("%s: Unknown tokenizer '%s'",
7db7fbea5d8a07463b625f93d69166d56018dadfTimo Sirainen tokenizer_set_name = t_str_replace(tokenizers[i], '-', '_');
7db7fbea5d8a07463b625f93d69166d56018dadfTimo Sirainen set_key = t_strdup_printf("fts_tokenizers_%s", tokenizer_set_name);
7db7fbea5d8a07463b625f93d69166d56018dadfTimo Sirainen /* tell the tokenizers that we're tokenizing a search string
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen (instead of tokenizing indexed data) */
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen if (fts_tokenizer_create(tokenizer_class, parent,
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen *error_r = t_strdup_printf("%s: %s", set_key, error);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenstatic int fts_user_init_tokenizers(struct mail_user *user,
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen const char **error_r)
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen if (fts_user_create_tokenizer(user, &fuser->index_tokenizer, FALSE,
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen if (fts_user_create_tokenizer(user, &fuser->search_tokenizer, TRUE,
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen array_foreach(&fuser->languages, user_langp) {
a618726eb3eb09a3866fe93208baf923d593f4d3Timo Sirainen if (strcmp((*user_langp)->lang->name, lang->name) == 0)
67e0afe62b26d222614b8d817155bf5c74bd7fe0Stephan Boschstruct fts_tokenizer *fts_user_get_index_tokenizer(struct mail_user *user)
67e0afe62b26d222614b8d817155bf5c74bd7fe0Stephan Bosch struct fts_user *fuser = FTS_USER_CONTEXT(user);
67e0afe62b26d222614b8d817155bf5c74bd7fe0Stephan Boschstruct fts_tokenizer *fts_user_get_search_tokenizer(struct mail_user *user)
67e0afe62b26d222614b8d817155bf5c74bd7fe0Stephan Bosch struct fts_user *fuser = FTS_USER_CONTEXT(user);
67e0afe62b26d222614b8d817155bf5c74bd7fe0Stephan Boschstatic int fts_user_language_create(struct mail_user *user,
67e0afe62b26d222614b8d817155bf5c74bd7fe0Stephan Bosch const char **error_r)
67e0afe62b26d222614b8d817155bf5c74bd7fe0Stephan Bosch if (fts_user_create_filters(user, lang, &filter, error_r) < 0)
4388ddff22681fc4385c963532b20c3560606667Stephan Bosch user_lang = p_new(user->pool, struct fts_user_language, 1);
67e0afe62b26d222614b8d817155bf5c74bd7fe0Stephan Bosch array_append(&fuser->languages, &user_lang, 1);
67e0afe62b26d222614b8d817155bf5c74bd7fe0Stephan Boschstatic int fts_user_languages_fill_all(struct mail_user *user,
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen const char **error_r)
7db7fbea5d8a07463b625f93d69166d56018dadfTimo Sirainen array_foreach(fts_language_list_get_all(fuser->lang_list), langp) {
7db7fbea5d8a07463b625f93d69166d56018dadfTimo Sirainen if (fts_user_language_create(user, fuser, *langp, error_r) < 0)
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenstruct fts_language_list *fts_user_get_language_list(struct mail_user *user)
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen struct fts_user *fuser = FTS_USER_CONTEXT(user);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenfts_user_get_all_languages(struct mail_user *user)
4487c66123ca4830f8afbf4efcd7a260848d0e05Timo Sirainen struct fts_user *fuser = FTS_USER_CONTEXT(user);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenstruct fts_user_language *fts_user_get_data_lang(struct mail_user *user)
cb3ab2fd5668700a89b274a43595cfbfa1616e4bTimo Sirainen struct fts_user *fuser = FTS_USER_CONTEXT(user);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen lang = p_new(user->pool, struct fts_user_language, 1);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen if (fts_filter_create(fts_filter_lowercase, NULL, lang->lang, NULL,
5f1d689131a75c39f064cbd4202373e7edf78f18Josef 'Jeff' Sipekstatic void fts_user_free(struct fts_user *fuser)
9a84b90d894a741ae6e090de104d31382a41d0aaJosef 'Jeff' Sipek struct fts_user_language *const *user_langp;
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen array_foreach(&fuser->languages, user_langp) {
a618726eb3eb09a3866fe93208baf923d593f4d3Timo Sirainen if (fuser->data_lang != NULL && fuser->data_lang->filter != NULL)
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen fts_tokenizer_unref(&fuser->search_tokenizer);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenint fts_mail_user_init(struct mail_user *user, const char **error_r)
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen fuser = p_new(user->pool, struct fts_user, 1);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen p_array_init(&fuser->languages, user->pool, 4);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen if (fts_user_init_languages(user, fuser, error_r) < 0) {
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen if (fts_user_languages_fill_all(user, fuser, error_r) < 0 ||
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen fts_user_init_tokenizers(user, fuser, error_r) < 0) {
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainen MODULE_CONTEXT_SET(user, fts_user_module, fuser);
96f89d51e8315f644f46804a9f0fc4f685ac48bfTimo Sirainenvoid fts_mail_user_deinit(struct mail_user *user)