fts-user.c revision f5e35763912e29c420a6977a211ae215ece3a8ab
bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
ebcd7cf40e53c2bbc98f7f686e206cda5c0e3111Timo Sirainen ARRAY_TYPE(fts_user_language) languages, data_languages;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenstatic MODULE_CONTEXT_DEFINE_INIT(fts_user_module,
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainenstatic const char *const *str_keyvalues_to_array(const char *str)
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen unsigned int i;
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainenfts_user_init_languages(struct mail_user *user, struct fts_user *fuser,
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainen const char **error_r)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen const char *lang_config[3] = {NULL, NULL, NULL};
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen languages = mail_user_plugin_getenv(user, "fts_languages");
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainen *error_r = "fts_languages setting is missing";
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen lang_config[1] = mail_user_plugin_getenv(user, "fts_language_config");
a9b3887f4d9ed75a76fed964c1930432bf84f4f5Timo Sirainen if (fts_language_list_init(lang_config, &fuser->lang_list, error_r) < 0)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (!fts_language_list_add_names(fuser->lang_list, languages, &unknown)) {
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainen "fts_languages: Unknown language '%s'", unknown);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (array_count(fts_language_list_get_all(fuser->lang_list)) == 0) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenfts_user_create_filters(struct mail_user *user, const struct fts_language *lang,
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen struct fts_filter **filter_r, const char **error_r)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen struct fts_filter *filter = NULL, *parent = NULL;
4c61d6b6ad6173f06563c2cee9bd813c59277dd2Timo Sirainen const char *filters_key, *const *filters, *filter_set_name;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen unsigned int i;
57dc9669d34de7d08a44be9d7d3c9f6a0c34cc87Timo Sirainen /* try to get the language-specific filters first */
57dc9669d34de7d08a44be9d7d3c9f6a0c34cc87Timo Sirainen filters_key = t_strconcat("fts_filters_", lang->name, NULL);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen str = mail_user_plugin_getenv(user, filters_key);
57dc9669d34de7d08a44be9d7d3c9f6a0c34cc87Timo Sirainen /* fallback to global filters */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen str = mail_user_plugin_getenv(user, filters_key);
5455a917515f774fcaad35558f149536b796b531Teemu Huovila /* No filters */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen *error_r = t_strdup_printf("%s: Unknown filter '%s'",
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen /* try the language-specific setting first */
4c61d6b6ad6173f06563c2cee9bd813c59277dd2Timo Sirainen filter_set_name = t_str_replace(filters[i], '-', '_');
fe0dff63d6b53d22ae16ac53ab183e9355a64a32Timo Sirainen set_key = t_strdup_printf("fts_filter_%s", filter_set_name);
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen if (fts_filter_create(filter_class, parent, lang,
fac865bad1ba10e85d80b63dedfd3493a65510d4Timo Sirainen *error_r = t_strdup_printf("%s: %s", set_key, error);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovilafts_user_create_tokenizer(struct mail_user *user,
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila struct fts_tokenizer **tokenizer_r, bool search,
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila const char **error_r)
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila struct fts_tokenizer *tokenizer = NULL, *parent = NULL;
957b0b4c9aeff7153bb9ebf91d8aea550bd07865Teemu Huovila const char *tokenizers_key, *const *tokenizers, *tokenizer_set_name;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila unsigned int i;
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen tokenizers_key = t_strconcat("fts_tokenizers_", lang->name, NULL);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila str = mail_user_plugin_getenv(user, tokenizers_key);
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen str = mail_user_plugin_getenv(user, "fts_tokenizers");
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen *error_r = t_strdup_printf("%s or fts_tokenizers setting must exist", tokenizers_key);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila tokenizer_class = fts_tokenizer_find(tokenizers[i]);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila *error_r = t_strdup_printf("%s: Unknown tokenizer '%s'",
957b0b4c9aeff7153bb9ebf91d8aea550bd07865Teemu Huovila tokenizer_set_name = t_str_replace(tokenizers[i], '-', '_');
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen set_key = t_strdup_printf("fts_tokenizer_%s_%s", tokenizer_set_name, lang->name);
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen set_key = t_strdup_printf("fts_tokenizer_%s", tokenizer_set_name);
0c827d2094e80ede4c089fc00260d7ffcc764636Timo Sirainen /* tell the tokenizers that we're tokenizing a search string
0c827d2094e80ede4c089fc00260d7ffcc764636Timo Sirainen (instead of tokenizing indexed data) */
0ebeb1831a56e020b0958ed1ced50e86ee9347ecTimo Sirainen if (fts_tokenizer_create(tokenizer_class, parent,
fac865bad1ba10e85d80b63dedfd3493a65510d4Timo Sirainen *error_r = t_strdup_printf("%s: %s", set_key, error);
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainenfts_user_language_init_tokenizers(struct mail_user *user,
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen const char **error_r)
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen if (fts_user_create_tokenizer(user, user_lang->lang,
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen if (fts_user_create_tokenizer(user, user_lang->lang,
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila struct fts_user *fuser = FTS_USER_CONTEXT(user);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen array_foreach(&fuser->languages, user_langp) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (strcmp((*user_langp)->lang->name, lang->name) == 0)
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovilastatic int fts_user_language_create(struct mail_user *user,
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila const char **error_r)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen user_lang = p_new(user->pool, struct fts_user_language, 1);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen array_append(&fuser->languages, &user_lang, 1);
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen if (fts_user_language_init_tokenizers(user, user_lang, error_r) < 0)
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen if (fts_user_create_filters(user, lang, &user_lang->filter, error_r) < 0)
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovilastatic int fts_user_languages_fill_all(struct mail_user *user,
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila const char **error_r)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen array_foreach(fts_language_list_get_all(fuser->lang_list), langp) {
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila if (fts_user_language_create(user, fuser, *langp, error_r) < 0)
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainenfts_user_init_data_language(struct mail_user *user, struct fts_user *fuser,
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen const char **error_r)
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen user_lang = p_new(user->pool, struct fts_user_language, 1);
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen if (fts_user_language_init_tokenizers(user, user_lang, error_r) < 0)
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen if (fts_filter_create(fts_filter_lowercase, NULL, user_lang->lang, NULL,
ebcd7cf40e53c2bbc98f7f686e206cda5c0e3111Timo Sirainen p_array_init(&fuser->data_languages, user->pool, 1);
ebcd7cf40e53c2bbc98f7f686e206cda5c0e3111Timo Sirainen array_append(&fuser->data_languages, &user_lang, 1);
f5e35763912e29c420a6977a211ae215ece3a8abTimo Sirainen array_append(&fuser->languages, &user_lang, 1);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenstruct fts_language_list *fts_user_get_language_list(struct mail_user *user)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen struct fts_user *fuser = FTS_USER_CONTEXT(user);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenfts_user_get_all_languages(struct mail_user *user)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen struct fts_user *fuser = FTS_USER_CONTEXT(user);
ebcd7cf40e53c2bbc98f7f686e206cda5c0e3111Timo Sirainenfts_user_get_data_languages(struct mail_user *user)
ebcd7cf40e53c2bbc98f7f686e206cda5c0e3111Timo Sirainen struct fts_user *fuser = FTS_USER_CONTEXT(user);
60ba197d17023594231d9805d889817782e41859Timo Sirainenstruct fts_user_language *fts_user_get_data_lang(struct mail_user *user)
60ba197d17023594231d9805d889817782e41859Timo Sirainen struct fts_user *fuser = FTS_USER_CONTEXT(user);
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainenstatic void fts_user_language_free(struct fts_user_language *user_lang)
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen fts_tokenizer_unref(&user_lang->index_tokenizer);
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen fts_tokenizer_unref(&user_lang->search_tokenizer);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenstatic void fts_user_free(struct fts_user *fuser)
eeb03434472130f9631f164440566fd8d09e6380Timo Sirainenint fts_mail_user_init(struct mail_user *user, const char **error_r)
6634e45500e81cfa36932203ee69c81745efd3e6Timo Sirainen struct fts_user *fuser = FTS_USER_CONTEXT(user);
6634e45500e81cfa36932203ee69c81745efd3e6Timo Sirainen /* multiple fts plugins are loaded */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen fuser = p_new(user->pool, struct fts_user, 1);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen p_array_init(&fuser->languages, user->pool, 4);
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen if (fts_user_init_languages(user, fuser, error_r) < 0 ||
d9a7e950a9cd21f2b4a90ec7759fca9e8fcc7995Timo Sirainen fts_user_init_data_language(user, fuser, error_r) < 0) {
dbf26a3ea43cd79fe88f01ec99c7d9440679b996Timo Sirainen if (fts_user_languages_fill_all(user, fuser, error_r) < 0) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen MODULE_CONTEXT_SET(user, fts_user_module, fuser);
eeb03434472130f9631f164440566fd8d09e6380Timo Sirainenvoid fts_mail_user_deinit(struct mail_user *user)