<commit_before>/* Copyright (c) 2006-2011 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "crc32.h"
#include "mail-storage-hooks.h"
#include "lucene-wrapper.h"
#include "fts-lucene-plugin.h"

const char *fts_lucene_plugin_version = DOVECOT_VERSION;

struct fts_lucene_user_module fts_lucene_user_module =
	MODULE_CONTEXT_INIT(&mail_user_module_register);

static int
fts_lucene_plugin_init_settings(struct mail_user *user,
				struct fts_lucene_settings *set,
				const char *str)
{
	const char *const *tmp;

	set->default_language = "english";
	for (tmp = t_strsplit_spaces(str, " "); *tmp != NULL; tmp++) {
		if (strncmp(*tmp, "default_language=", 17) == 0) {
			set->default_language =
				p_strdup(user->pool, *tmp + 17);
		} else if (strncmp(*tmp, "textcat_conf=", 13) == 0) {
			set->textcat_conf = p_strdup(user->pool, *tmp + 13);
		} else if (strncmp(*tmp, "textcat_dir=", 12) == 0) {
			set->textcat_dir = p_strdup(user->pool, *tmp + 12);
		} else {
			i_error("fts_lucene: Invalid setting: %s", *tmp);
			return -1;
		}
	}
	if (set->textcat_conf != NULL && set->textcat_dir == NULL) {
		i_error("fts_lucene: textcat_conf set, but textcat_dir unset");
		return -1;
	}
	if (set->textcat_conf == NULL && set->textcat_dir != NULL) {
		i_error("fts_lucene: textcat_dir set, but textcat_conf unset");
		return -1;
	}
#ifndef HAVE_LUCENE_STEMMER
	if (set->default_language != NULL) {
		i_error("fts_lucene: default_language set, "
			"but Dovecot built without stemmer support");
		return -1;
	}
#endif
#ifndef HAVE_LUCENE_TEXTCAT
	if (set->textcat_conf != NULL) {
		i_error("fts_lucene: textcat_dir set, "
			"but Dovecot built without textcat support");
		return -1;
	}
#endif
	return 0;
}

uint32_t fts_lucene_settings_checksum(const struct fts_lucene_settings *set)
{
	/* only the default language change matters */
	return crc32_str(set->default_language);
}

static void fts_lucene_mail_user_created(struct mail_user *user)
{
	struct fts_lucene_user *fuser;
	const char *env;

	fuser = p_new(user->pool, struct fts_lucene_user, 1);
	env = mail_user_plugin_getenv(user, "fts_lucene");
	if (env == NULL)
		env = "";

	if (fts_lucene_plugin_init_settings(user, &fuser->set, env) < 0) {
		/* invalid settings, disabling */
		return;
	}
	MODULE_CONTEXT_SET(user, fts_lucene_user_module, fuser);
}

static struct mail_storage_hooks fts_lucene_mail_storage_hooks = {
	.mail_user_created = fts_lucene_mail_user_created
};

void fts_lucene_plugin_init(struct module *module ATTR_UNUSED)<commit_msg>fts-lucene: Fixed to work without stemmer support.<commit_after>/* Copyright (c) 2006-2011 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "crc32.h"
#include "mail-storage-hooks.h"
#include "lucene-wrapper.h"
#include "fts-lucene-plugin.h"

const char *fts_lucene_plugin_version = DOVECOT_VERSION;

struct fts_lucene_user_module fts_lucene_user_module =
	MODULE_CONTEXT_INIT(&mail_user_module_register);

static int
fts_lucene_plugin_init_settings(struct mail_user *user,
				struct fts_lucene_settings *set,
				const char *str)
{
	const char *const *tmp;

	for (tmp = t_strsplit_spaces(str, " "); *tmp != NULL; tmp++) {
		if (strncmp(*tmp, "default_language=", 17) == 0) {
			set->default_language =
				p_strdup(user->pool, *tmp + 17);
		} else if (strncmp(*tmp, "textcat_conf=", 13) == 0) {
			set->textcat_conf = p_strdup(user->pool, *tmp + 13);
		} else if (strncmp(*tmp, "textcat_dir=", 12) == 0) {
			set->textcat_dir = p_strdup(user->pool, *tmp + 12);
		} else {
			i_error("fts_lucene: Invalid setting: %s", *tmp);
			return -1;
		}
	}
	if (set->textcat_conf != NULL && set->textcat_dir == NULL) {
		i_error("fts_lucene: textcat_conf set, but textcat_dir unset");
		return -1;
	}
	if (set->textcat_conf == NULL && set->textcat_dir != NULL) {
		i_error("fts_lucene: textcat_dir set, but textcat_conf unset");
		return -1;
	}
#ifndef HAVE_LUCENE_STEMMER
	if (set->default_language != NULL) {
		i_error("fts_lucene: default_language set, "
			"but Dovecot built without stemmer support");
		return -1;
	}
#else
	if (set->default_language == NULL)
		set->default_language = "english";
#endif
#ifndef HAVE_LUCENE_TEXTCAT
	if (set->textcat_conf != NULL) {
		i_error("fts_lucene: textcat_dir set, "
			"but Dovecot built without textcat support");
		return -1;
	}
#endif
	return 0;
}

uint32_t fts_lucene_settings_checksum(const struct fts_lucene_settings *set)
{
	/* only the default language change matters */
	return set->default_language == NULL ? 0 :
		crc32_str(set->default_language);
}

static void fts_lucene_mail_user_created(struct mail_user *user)
{
	struct fts_lucene_user *fuser;
	const char *env;

	fuser = p_new(user->pool, struct fts_lucene_user, 1);
	env = mail_user_plugin_getenv(user, "fts_lucene");
	if (env == NULL)
		env = "";

	if (fts_lucene_plugin_init_settings(user, &fuser->set, env) < 0) {
		/* invalid settings, disabling */
		return;
	}
	MODULE_CONTEXT_SET(user, fts_lucene_user_module, fuser);
}

static struct mail_storage_hooks fts_lucene_mail_storage_hooks = {
	.mail_user_created = fts_lucene_mail_user_created
};

void fts_lucene_plugin_init(struct module *module ATTR_UNUSED)
{'fraction_non_alphanumeric': 0.1326372776488786, 'fraction_numerical': 0.014501160092807424, 'mean_word_length': 5.029137529137529, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3389255', 'n_tokens_mistral': 2195, 'n_tokens_neox': 1981, 'n_words': 432}