-
Notifications
You must be signed in to change notification settings - Fork 271
/
doveadm-fts.c
312 lines (275 loc) · 7.9 KB
/
doveadm-fts.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* Copyright (c) 2011-2016 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "str.h"
#include "imap-util.h"
#include "mail-namespace.h"
#include "mail-search.h"
#include "mailbox-list-iter.h"
#include "fts-storage.h"
#include "fts-search-args.h"
#include "doveadm-mail.h"
#include "doveadm-mailbox-list-iter.h"
#include "doveadm-fts.h"
const char *doveadm_fts_plugin_version = DOVECOT_ABI_VERSION;
static int
cmd_search_box(struct doveadm_mail_cmd_context *ctx,
const struct mailbox_info *info)
{
struct mailbox *box;
struct fts_backend *backend;
struct fts_result result;
int ret = 0;
backend = fts_list_backend(info->ns->list);
if (backend == NULL) {
i_error("fts not enabled for %s", info->vname);
ctx->exit_code = EX_CONFIG;
return -1;
}
memset(&result, 0, sizeof(result));
i_array_init(&result.definite_uids, 16);
i_array_init(&result.maybe_uids, 16);
i_array_init(&result.scores, 16);
box = mailbox_alloc(info->ns->list, info->vname, 0);
if (fts_backend_lookup(backend, box, ctx->search_args->args,
FTS_LOOKUP_FLAG_AND_ARGS, &result) < 0) {
i_error("fts lookup failed");
doveadm_mail_failed_error(ctx, MAIL_ERROR_TEMP);
ret = -1;
} else {
printf("%s: ", info->vname);
if (array_count(&result.definite_uids) == 0)
printf("no results\n");
else T_BEGIN {
string_t *str = t_str_new(128);
imap_write_seq_range(str, &result.definite_uids);
printf("%s\n", str_c(str));
} T_END;
if (array_count(&result.maybe_uids) > 0) T_BEGIN {
string_t *str = t_str_new(128);
imap_write_seq_range(str, &result.maybe_uids);
printf(" - maybe: %s\n", str_c(str));
} T_END;
fts_backend_lookup_done(backend);
}
mailbox_free(&box);
array_free(&result.definite_uids);
array_free(&result.maybe_uids);
array_free(&result.scores);
return ret;
}
static int
cmd_fts_lookup_run(struct doveadm_mail_cmd_context *ctx,
struct mail_user *user)
{
const enum mailbox_list_iter_flags iter_flags =
MAILBOX_LIST_ITER_NO_AUTO_BOXES |
MAILBOX_LIST_ITER_RETURN_NO_FLAGS;
struct doveadm_mailbox_list_iter *iter;
const struct mailbox_info *info;
int ret = 0;
iter = doveadm_mailbox_list_iter_init(ctx, user, ctx->search_args,
iter_flags);
while ((info = doveadm_mailbox_list_iter_next(iter)) != NULL) T_BEGIN {
if (cmd_search_box(ctx, info) < 0)
ret = -1;
} T_END;
if (doveadm_mailbox_list_iter_deinit(&iter) < 0)
ret = -1;
return ret;
}
static void
cmd_fts_lookup_init(struct doveadm_mail_cmd_context *ctx,
const char *const args[])
{
if (args[0] == NULL)
doveadm_mail_help_name("fts lookup");
ctx->search_args = doveadm_mail_build_search_args(args);
}
static struct doveadm_mail_cmd_context *
cmd_fts_lookup_alloc(void)
{
struct doveadm_mail_cmd_context *ctx;
ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
ctx->v.run = cmd_fts_lookup_run;
ctx->v.init = cmd_fts_lookup_init;
return ctx;
}
static int
cmd_fts_expand_run(struct doveadm_mail_cmd_context *ctx,
struct mail_user *user)
{
struct mail_namespace *ns = mail_namespace_find_inbox(user->namespaces);
struct mailbox *box;
struct fts_backend *backend;
string_t *str = t_str_new(128);
backend = fts_list_backend(ns->list);
if (backend == NULL) {
i_error("fts not enabled for INBOX");
ctx->exit_code = EX_CONFIG;
return -1;
}
box = mailbox_alloc(ns->list, "INBOX", 0);
mail_search_args_init(ctx->search_args, box, FALSE, NULL);
if (fts_search_args_expand(backend, ctx->search_args) < 0)
i_fatal("Couldn't expand search args");
mail_search_args_to_cmdline(str, ctx->search_args->args);
printf("%s\n", str_c(str));
mailbox_free(&box);
return 0;
}
static void
cmd_fts_expand_init(struct doveadm_mail_cmd_context *ctx,
const char *const args[])
{
if (args[0] == NULL)
doveadm_mail_help_name("fts expand");
ctx->search_args = doveadm_mail_build_search_args(args);
}
static struct doveadm_mail_cmd_context *
cmd_fts_expand_alloc(void)
{
struct doveadm_mail_cmd_context *ctx;
ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
ctx->v.run = cmd_fts_expand_run;
ctx->v.init = cmd_fts_expand_init;
return ctx;
}
static int
fts_namespace_find(struct mail_user *user, const char *ns_prefix,
struct mail_namespace **ns_r)
{
struct mail_namespace *ns;
if (ns_prefix == NULL)
ns = mail_namespace_find_inbox(user->namespaces);
else {
ns = mail_namespace_find_prefix(user->namespaces, ns_prefix);
if (ns == NULL) {
i_error("Namespace prefix not found: %s", ns_prefix);
return -1;
}
}
if (fts_list_backend(ns->list) == NULL) {
i_error("fts not enabled for user's namespace %s",
ns_prefix != NULL ? ns_prefix : "INBOX");
return -1;
}
*ns_r = ns;
return 0;
}
static int
cmd_fts_optimize_run(struct doveadm_mail_cmd_context *ctx,
struct mail_user *user)
{
const char *ns_prefix = ctx->args[0];
struct mail_namespace *ns;
struct fts_backend *backend;
if (fts_namespace_find(user, ns_prefix, &ns) < 0) {
doveadm_mail_failed_error(ctx, MAIL_ERROR_NOTFOUND);
return -1;
}
backend = fts_list_backend(ns->list);
if (fts_backend_optimize(backend) < 0) {
i_error("fts optimize failed");
doveadm_mail_failed_error(ctx, MAIL_ERROR_TEMP);
return -1;
}
return 0;
}
static void
cmd_fts_optimize_init(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED,
const char *const args[])
{
if (str_array_length(args) > 1)
doveadm_mail_help_name("fts optimize");
}
static struct doveadm_mail_cmd_context *
cmd_fts_optimize_alloc(void)
{
struct doveadm_mail_cmd_context *ctx;
ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
ctx->v.run = cmd_fts_optimize_run;
ctx->v.init = cmd_fts_optimize_init;
return ctx;
}
static int
cmd_fts_rescan_run(struct doveadm_mail_cmd_context *ctx, struct mail_user *user)
{
const char *ns_prefix = ctx->args[0];
struct mail_namespace *ns;
struct fts_backend *backend;
if (fts_namespace_find(user, ns_prefix, &ns) < 0) {
doveadm_mail_failed_error(ctx, MAIL_ERROR_NOTFOUND);
return -1;
}
backend = fts_list_backend(ns->list);
if (fts_backend_rescan(backend) < 0) {
i_error("fts rescan failed");
doveadm_mail_failed_error(ctx, MAIL_ERROR_TEMP);
return -1;
}
return 0;
}
static void
cmd_fts_rescan_init(struct doveadm_mail_cmd_context *ctx ATTR_UNUSED,
const char *const args[])
{
if (str_array_length(args) > 1)
doveadm_mail_help_name("fts rescan");
}
static struct doveadm_mail_cmd_context *
cmd_fts_rescan_alloc(void)
{
struct doveadm_mail_cmd_context *ctx;
ctx = doveadm_mail_cmd_alloc(struct doveadm_mail_cmd_context);
ctx->v.run = cmd_fts_rescan_run;
ctx->v.init = cmd_fts_rescan_init;
return ctx;
}
static struct doveadm_cmd_ver2 fts_commands[] = {
{
.name = "fts lookup",
.mail_cmd = cmd_fts_lookup_alloc,
.usage = DOVEADM_CMD_MAIL_USAGE_PREFIX "<search query>",
DOVEADM_CMD_PARAMS_START
DOVEADM_CMD_MAIL_COMMON
DOVEADM_CMD_PARAM('\0', "query", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
DOVEADM_CMD_PARAMS_END
},
{
.name = "fts expand",
.mail_cmd = cmd_fts_expand_alloc,
.usage = DOVEADM_CMD_MAIL_USAGE_PREFIX "<search query>",
DOVEADM_CMD_PARAMS_START
DOVEADM_CMD_MAIL_COMMON
DOVEADM_CMD_PARAM('\0', "query", CMD_PARAM_ARRAY, CMD_PARAM_FLAG_POSITIONAL)
DOVEADM_CMD_PARAMS_END
},
{
.name = "fts optimize",
.mail_cmd = cmd_fts_optimize_alloc,
.usage = DOVEADM_CMD_MAIL_USAGE_PREFIX "[<namespace>]",
DOVEADM_CMD_PARAMS_START
DOVEADM_CMD_MAIL_COMMON
DOVEADM_CMD_PARAM('\0', "namespace", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
DOVEADM_CMD_PARAMS_END
},
{
.name = "fts rescan",
.mail_cmd = cmd_fts_rescan_alloc,
.usage = DOVEADM_CMD_MAIL_USAGE_PREFIX "[<namespace>]",
DOVEADM_CMD_PARAMS_START
DOVEADM_CMD_MAIL_COMMON
DOVEADM_CMD_PARAM('\0', "namespace", CMD_PARAM_STR, CMD_PARAM_FLAG_POSITIONAL)
DOVEADM_CMD_PARAMS_END
},
};
void doveadm_fts_plugin_init(struct module *module ATTR_UNUSED)
{
unsigned int i;
for (i = 0; i < N_ELEMENTS(fts_commands); i++)
doveadm_cmd_register_ver2(&fts_commands[i]);
doveadm_dump_fts_expunge_log_init();
}
void doveadm_fts_plugin_deinit(void)
{
}