bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen#include "lib.h"
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen#include "str.h"
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen#include "fts-language.h"
c8b49c07999af06d368ea4b37c54919f46d94844Timo Sirainen#include "fts-filter-private.h"
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen
b5440e0ae7f0827154998efae81bbf4a07475ccbTimo Sirainen#ifdef HAVE_LIBICU
b5440e0ae7f0827154998efae81bbf4a07475ccbTimo Sirainen# include "fts-icu.h"
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila# include "fts-filter-common.h"
b5440e0ae7f0827154998efae81bbf4a07475ccbTimo Sirainen#endif
b5440e0ae7f0827154998efae81bbf4a07475ccbTimo Sirainen
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainenstatic int
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainenfts_filter_lowercase_create(const struct fts_language *lang ATTR_UNUSED,
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila const char *const *settings,
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila struct fts_filter **filter_r,
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila const char **error_r)
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen{
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen struct fts_filter *filter;
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila unsigned int i, max_length = 250;
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila for (i = 0; settings[i] != NULL; i += 2) {
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila const char *key = settings[i], *value = settings[i+1];
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila if (strcmp(key, "maxlen") == 0) {
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila if (str_to_uint(value, &max_length) < 0 ||
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila max_length == 0) {
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila *error_r = t_strdup_printf("Invalid lowercase filter maxlen setting: %s", value);
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila return -1;
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila }
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila }
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila else {
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila *error_r = t_strdup_printf("Unknown setting: %s", key);
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila return -1;
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila }
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen }
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen filter = i_new(struct fts_filter, 1);
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen *filter = *fts_filter_lowercase;
c8b49c07999af06d368ea4b37c54919f46d94844Timo Sirainen filter->token = str_new(default_pool, 64);
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila filter->max_length = max_length;
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen *filter_r = filter;
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen return 0;
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen}
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainenstatic int
266c879b43bce714aa5cd6a59b7f709d74451ccbTimo Sirainenfts_filter_lowercase_filter(struct fts_filter *filter ATTR_UNUSED,
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila const char **token,
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila const char **error_r ATTR_UNUSED)
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen{
c8b49c07999af06d368ea4b37c54919f46d94844Timo Sirainen#ifdef HAVE_LIBICU
c8b49c07999af06d368ea4b37c54919f46d94844Timo Sirainen str_truncate(filter->token, 0);
c8b49c07999af06d368ea4b37c54919f46d94844Timo Sirainen fts_icu_lcase(filter->token, *token);
5fcd30add8dcf4d883978cce3e39f3a89184f1e5Teemu Huovila fts_filter_truncate_token(filter->token, filter->max_length);
c8b49c07999af06d368ea4b37c54919f46d94844Timo Sirainen *token = str_c(filter->token);
c8b49c07999af06d368ea4b37c54919f46d94844Timo Sirainen#else
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen *token = t_str_lcase(*token);
c8b49c07999af06d368ea4b37c54919f46d94844Timo Sirainen#endif
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen return 1;
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen}
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainenstatic const struct fts_filter fts_filter_lowercase_real = {
a7d8afaadae968db20eb979052111d76a3086cd7Timo Sirainen .class_name = "lowercase",
be7085921228e1c1333dbf7aa2daca41115c3f23Timo Sirainen .v = {
be7085921228e1c1333dbf7aa2daca41115c3f23Timo Sirainen fts_filter_lowercase_create,
be7085921228e1c1333dbf7aa2daca41115c3f23Timo Sirainen fts_filter_lowercase_filter,
be7085921228e1c1333dbf7aa2daca41115c3f23Timo Sirainen NULL
be7085921228e1c1333dbf7aa2daca41115c3f23Timo Sirainen }
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen};
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainen
acfcf88e4dd529e4b2409f43bc9713cbc0169347Timo Sirainenconst struct fts_filter *fts_filter_lowercase = &fts_filter_lowercase_real;