bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2011-2018 Dovecot authors, see the included COPYING file */
bd63b5b860658b01b1f46f26d406e1e4a9dc019aTimo Sirainenconst char *doveadm_fts_plugin_version = DOVECOT_ABI_VERSION;
2e3e24888f9ce3da3116ee1d6a60f42394b6026cTimo Sirainencmd_search_box(struct doveadm_mail_cmd_context *ctx,
2e3e24888f9ce3da3116ee1d6a60f42394b6026cTimo Sirainen i_error("fts not enabled for %s", info->vname);
2e3e24888f9ce3da3116ee1d6a60f42394b6026cTimo Sirainen box = mailbox_alloc(info->ns->list, info->vname, 0);
2e3e24888f9ce3da3116ee1d6a60f42394b6026cTimo Sirainen if (fts_backend_lookup(backend, box, ctx->search_args->args,
2e3e24888f9ce3da3116ee1d6a60f42394b6026cTimo Sirainen doveadm_mail_failed_error(ctx, MAIL_ERROR_TEMP);
2e3e24888f9ce3da3116ee1d6a60f42394b6026cTimo Sirainen imap_write_seq_range(str, &result.definite_uids);
2e3e24888f9ce3da3116ee1d6a60f42394b6026cTimo Sirainen if (array_count(&result.maybe_uids) > 0) T_BEGIN {
2e3e24888f9ce3da3116ee1d6a60f42394b6026cTimo Sirainen imap_write_seq_range(str, &result.maybe_uids);
2e3e24888f9ce3da3116ee1d6a60f42394b6026cTimo Sirainencmd_fts_lookup_run(struct doveadm_mail_cmd_context *ctx,
2e3e24888f9ce3da3116ee1d6a60f42394b6026cTimo Sirainen const enum mailbox_list_iter_flags iter_flags =
2e3e24888f9ce3da3116ee1d6a60f42394b6026cTimo Sirainen iter = doveadm_mailbox_list_iter_init(ctx, user, ctx->search_args,
2e3e24888f9ce3da3116ee1d6a60f42394b6026cTimo Sirainen while ((info = doveadm_mailbox_list_iter_next(iter)) != NULL) T_BEGIN {
2e3e24888f9ce3da3116ee1d6a60f42394b6026cTimo Sirainen if (doveadm_mailbox_list_iter_deinit(&iter) < 0)
2e3e24888f9ce3da3116ee1d6a60f42394b6026cTimo Sirainencmd_fts_lookup_init(struct doveadm_mail_cmd_context *ctx,
2e3e24888f9ce3da3116ee1d6a60f42394b6026cTimo Sirainen const char *const args[])
2e3e24888f9ce3da3116ee1d6a60f42394b6026cTimo Sirainen ctx->search_args = doveadm_mail_build_search_args(args);
2e3e24888f9ce3da3116ee1d6a60f42394b6026cTimo Sirainen ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
be1126eef34743095ddbee3fecbafe0574d54eacTimo Sirainencmd_fts_expand_run(struct doveadm_mail_cmd_context *ctx,
be1126eef34743095ddbee3fecbafe0574d54eacTimo Sirainen struct mail_namespace *ns = mail_namespace_find_inbox(user->namespaces);
be1126eef34743095ddbee3fecbafe0574d54eacTimo Sirainen mail_search_args_init(ctx->search_args, box, FALSE, NULL);
be1126eef34743095ddbee3fecbafe0574d54eacTimo Sirainen if (fts_search_args_expand(backend, ctx->search_args) < 0)
be1126eef34743095ddbee3fecbafe0574d54eacTimo Sirainen mail_search_args_to_cmdline(str, ctx->search_args->args);
be1126eef34743095ddbee3fecbafe0574d54eacTimo Sirainencmd_fts_expand_init(struct doveadm_mail_cmd_context *ctx,
be1126eef34743095ddbee3fecbafe0574d54eacTimo Sirainen const char *const args[])
be1126eef34743095ddbee3fecbafe0574d54eacTimo Sirainen ctx->search_args = doveadm_mail_build_search_args(args);
be1126eef34743095ddbee3fecbafe0574d54eacTimo Sirainen ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainencmd_fts_tokenize_run(struct doveadm_mail_cmd_context *_ctx,
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen struct mail_namespace *ns = mail_namespace_find_inbox(user->namespaces);
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen (const unsigned char *)ctx->tokens, strlen(ctx->tokens),
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen lang = fts_language_list_get_first(lang_list);
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen i_warning("Text too short, can't detect its language - assuming %s", lang->name);
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen i_warning("Can't detect its language - assuming %s", lang->name);
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen i_error("Language detection library initialization failed");
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen i_error("Unknown language: %s", ctx->language);
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen user_lang = fts_user_language_find(user, lang);
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen i_error("Language not enabled for user: %s", ctx->language);
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen fts_tokenizer_reset(user_lang->index_tokenizer);
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen ret = fts_tokenizer_next(user_lang->index_tokenizer,
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen (const unsigned char *)ctx->tokens, strlen(ctx->tokens),
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen ret = fts_tokenizer_final(user_lang->index_tokenizer,
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen ret2 = fts_filter_filter(user_lang->filter, &token, &error);
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen else if (ret2 < 0)
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen i_error("Couldn't create indexable tokens: %s", error);
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainencmd_fts_tokenize_init(struct doveadm_mail_cmd_context *_ctx,
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen const char *const args[])
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen ctx->tokens = p_strdup(_ctx->pool, t_strarray_join(args, " "));
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen doveadm_print_header("token", "token", DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE);
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainencmd_fts_tokenize_parse_arg(struct doveadm_mail_cmd_context *_ctx, int c)
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen ctx = doveadm_mail_cmd_alloc(struct fts_tokenize_cmd_context);
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen ctx->ctx.v.parse_arg = cmd_fts_tokenize_parse_arg;
d3380f13cb3e398bb5e7acbd4d04582397f62ec2Timo Sirainenfts_namespace_find(struct mail_user *user, const char *ns_prefix,
d3380f13cb3e398bb5e7acbd4d04582397f62ec2Timo Sirainen ns = mail_namespace_find_inbox(user->namespaces);
d3380f13cb3e398bb5e7acbd4d04582397f62ec2Timo Sirainen ns = mail_namespace_find_prefix(user->namespaces, ns_prefix);
d3380f13cb3e398bb5e7acbd4d04582397f62ec2Timo Sirainen i_error("Namespace prefix not found: %s", ns_prefix);
0226f0f9198bf7d5897e8473e58aa72ae1a72e39Timo Sirainen i_error("fts not enabled for user's namespace %s",
d3380f13cb3e398bb5e7acbd4d04582397f62ec2Timo Sirainencmd_fts_optimize_run(struct doveadm_mail_cmd_context *ctx,
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen if (fts_namespace_find(user, ns_prefix, &ns) < 0) {
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen doveadm_mail_failed_error(ctx, MAIL_ERROR_NOTFOUND);
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen doveadm_mail_failed_error(ctx, MAIL_ERROR_TEMP);
d3380f13cb3e398bb5e7acbd4d04582397f62ec2Timo Sirainencmd_fts_optimize_init(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED,
d3380f13cb3e398bb5e7acbd4d04582397f62ec2Timo Sirainen const char *const args[])
d3380f13cb3e398bb5e7acbd4d04582397f62ec2Timo Sirainen ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
3dfa8c707590b8a37a99095dc617d0b4eac3cf87Timo Sirainencmd_fts_rescan_run(struct doveadm_mail_cmd_context *ctx, struct mail_user *user)
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen if (fts_namespace_find(user, ns_prefix, &ns) < 0) {
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen doveadm_mail_failed_error(ctx, MAIL_ERROR_NOTFOUND);
5fbccc935e3f7b916aa7c6e302a212821072e83aTimo Sirainen doveadm_mail_failed_error(ctx, MAIL_ERROR_TEMP);
3dfa8c707590b8a37a99095dc617d0b4eac3cf87Timo Sirainencmd_fts_rescan_init(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED,
3dfa8c707590b8a37a99095dc617d0b4eac3cf87Timo Sirainen const char *const args[])
3dfa8c707590b8a37a99095dc617d0b4eac3cf87Timo Sirainen ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
e47f08b87b409b42acf353c3156c21e1ccc35ac4Aki Tuomi .usage = DOVEADM_CMD_MAIL_USAGE_PREFIX "<search query>",
e47f08b87b409b42acf353c3156c21e1ccc35ac4Aki TuomiDOVEADM_CMD_PARAM('\0', "query", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
e47f08b87b409b42acf353c3156c21e1ccc35ac4Aki Tuomi .usage = DOVEADM_CMD_MAIL_USAGE_PREFIX "<search query>",
e47f08b87b409b42acf353c3156c21e1ccc35ac4Aki TuomiDOVEADM_CMD_PARAM('\0', "query", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo Sirainen .usage = DOVEADM_CMD_MAIL_USAGE_PREFIX "<text>",
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo SirainenDOVEADM_CMD_PARAM('l', "language", CMD_PARAM_STR, 0)
78d33b52c5b738bd9f20fe5ffcd05dd754284ba6Timo SirainenDOVEADM_CMD_PARAM('\0', "text", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
e47f08b87b409b42acf353c3156c21e1ccc35ac4Aki Tuomi .usage = DOVEADM_CMD_MAIL_USAGE_PREFIX "[<namespace>]",
e47f08b87b409b42acf353c3156c21e1ccc35ac4Aki TuomiDOVEADM_CMD_PARAM('\0', "namespace", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
e47f08b87b409b42acf353c3156c21e1ccc35ac4Aki Tuomi .usage = DOVEADM_CMD_MAIL_USAGE_PREFIX "[<namespace>]",
e47f08b87b409b42acf353c3156c21e1ccc35ac4Aki TuomiDOVEADM_CMD_PARAM('\0', "namespace", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
d3380f13cb3e398bb5e7acbd4d04582397f62ec2Timo Sirainenvoid doveadm_fts_plugin_init(struct module *module ATTR_UNUSED)
d3380f13cb3e398bb5e7acbd4d04582397f62ec2Timo Sirainen unsigned int i;
d3380f13cb3e398bb5e7acbd4d04582397f62ec2Timo Sirainen for (i = 0; i < N_ELEMENTS(fts_commands); i++)