fts-lucene-plugin.c revision 57a712a4998e4bb0073080232225380cd2cea625
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2006-2011 Dovecot authors, see the included COPYING file */
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "lib.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "crc32.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "mail-storage-hooks.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "lucene-wrapper.h"
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#include "fts-lucene-plugin.h"
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainenconst char *fts_lucene_plugin_version = DOVECOT_VERSION;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenstruct fts_lucene_user_module fts_lucene_user_module =
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen MODULE_CONTEXT_INIT(&mail_user_module_register);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenstatic int
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenfts_lucene_plugin_init_settings(struct mail_user *user,
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen struct fts_lucene_settings *set,
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen const char *str)
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen{
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen const char *const *tmp;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen for (tmp = t_strsplit_spaces(str, " "); *tmp != NULL; tmp++) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (strncmp(*tmp, "default_language=", 17) == 0) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen set->default_language =
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen p_strdup(user->pool, *tmp + 17);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen } else if (strncmp(*tmp, "textcat_conf=", 13) == 0) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen set->textcat_conf = p_strdup(user->pool, *tmp + 13);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen } else if (strncmp(*tmp, "textcat_dir=", 12) == 0) {
4e8e7a93628b4ed60aaaa47c6f72c1433f21e81dTimo Sirainen set->textcat_dir = p_strdup(user->pool, *tmp + 12);
2d6c6f1d06d6d2e7b2ab0d46f7decdcf1cc80cd6Timo Sirainen } else {
2d6c6f1d06d6d2e7b2ab0d46f7decdcf1cc80cd6Timo Sirainen i_error("fts_lucene: Invalid setting: %s", *tmp);
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen return -1;
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen }
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen }
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (set->textcat_conf != NULL && set->textcat_dir == NULL) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen i_error("fts_lucene: textcat_conf set, but textcat_dir unset");
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen return -1;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen }
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (set->textcat_conf == NULL && set->textcat_dir != NULL) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen i_error("fts_lucene: textcat_dir set, but textcat_conf unset");
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen return -1;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen }
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#ifndef HAVE_LUCENE_STEMMER
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (set->default_language != NULL) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen i_error("fts_lucene: default_language set, "
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen "but Dovecot built without stemmer support");
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen return -1;
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen }
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen#else
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen if (set->default_language == NULL)
2eccb2637d0153bb7f9ad39a70f254cece74342cTimo Sirainen set->default_language = "english";
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#endif
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#ifndef HAVE_LUCENE_TEXTCAT
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen if (set->textcat_conf != NULL) {
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen i_error("fts_lucene: textcat_dir set, "
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen "but Dovecot built without textcat support");
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen return -1;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen }
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen#endif
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen return 0;
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen}
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenuint32_t fts_lucene_settings_checksum(const struct fts_lucene_settings *set)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen{
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen /* only the default language change matters */
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen return set->default_language == NULL ? 0 :
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen crc32_str(set->default_language);
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen}
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen
4e8e7a93628b4ed60aaaa47c6f72c1433f21e81dTimo Sirainenstatic void fts_lucene_mail_user_created(struct mail_user *user)
4e8e7a93628b4ed60aaaa47c6f72c1433f21e81dTimo Sirainen{
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen struct fts_lucene_user *fuser;
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen const char *env;
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen fuser = p_new(user->pool, struct fts_lucene_user, 1);
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen env = mail_user_plugin_getenv(user, "fts_lucene");
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen if (env == NULL)
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen env = "";
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen if (fts_lucene_plugin_init_settings(user, &fuser->set, env) < 0) {
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen /* invalid settings, disabling */
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen return;
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen }
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen MODULE_CONTEXT_SET(user, fts_lucene_user_module, fuser);
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen}
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainenstatic struct mail_storage_hooks fts_lucene_mail_storage_hooks = {
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen .mail_user_created = fts_lucene_mail_user_created
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen};
86bdb644d147a73df85abce4325254d694217a5fTimo Sirainen
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainenvoid fts_lucene_plugin_init(struct module *module ATTR_UNUSED)
ae9691f7ef36d5272d72c90fa51393dfea5dd126Timo Sirainen{
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen fts_backend_register(&fts_backend_lucene);
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen mail_storage_hooks_add(module, &fts_lucene_mail_storage_hooks);
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen}
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainenvoid fts_lucene_plugin_deinit(void)
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen{
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen fts_backend_unregister(fts_backend_lucene.name);
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen mail_storage_hooks_remove(&fts_lucene_mail_storage_hooks);
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen lucene_shutdown();
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen}
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainenconst char *fts_lucene_plugin_dependencies[] = { "fts", NULL };
7877db7b5daad125b6cb3e015574f33871c9a51bTimo Sirainen