2
** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
4
** This program is free software; you can redistribute it and/or modify it
5
** under the terms of the GNU General Public License as published by the
6
** Free Software Foundation; either version 3, or (at your option) any
9
** This program is distributed in the hope that it will be useful,
10
** but WITHOUT ANY WARRANTY; without even the implied warranty of
11
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
** GNU General Public License for more details.
14
** You should have received a copy of the GNU General Public License
15
** along with this program; if not, write to the Free Software Foundation,
16
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27
#include "mu-msg-gmime.h"
28
#include "mu-maildir.h"
30
#include "mu-query-xapian.h"
31
#include "mu-msg-xapian.h"
32
#include "mu-msg-str.h"
37
cmd_from_string (const char* cmd)
40
return MU_CMD_UNKNOWN;
42
if (strcmp (cmd, "index") == 0)
45
/* support some synonyms... */
46
if ((strcmp (cmd, "query") == 0) ||
47
(strcmp (cmd, "find") == 0) ||
48
(strcmp (cmd, "search") == 0))
51
if (strcmp (cmd, "cleanup") == 0)
52
return MU_CMD_CLEANUP;
54
if ((strcmp (cmd, "mkmdir") == 0) ||
55
(strcmp (cmd, "mkdir") == 0))
58
/* if (strcmp (cmd, "link") == 0) */
59
/* return MU_CMD_LINK; */
61
/* if ((strcmp (cmd, "help") == 0) || */
62
/* (strcmp (cmd, "info") == 0)) */
63
/* return MU_CMD_HELP; */
65
return MU_CMD_UNKNOWN;
70
print_query (MuQueryXapian *xapian, const gchar *query)
74
MU_WRITE_LOG ("query: '%s' (xquery)", query);
76
querystr = mu_query_xapian_as_string (xapian, query);
77
g_print ("%s\n", querystr);
85
display_field (MuMsgXapian *row, const MuMsgField* field)
89
switch (mu_msg_field_type(field)) {
90
case MU_MSG_FIELD_TYPE_STRING:
91
return mu_msg_xapian_get_field (row, field);
93
case MU_MSG_FIELD_TYPE_INT:
95
if (mu_msg_field_id(field) == MU_MSG_FIELD_ID_PRIORITY) {
96
val = mu_msg_xapian_get_field_numeric (row, field);
97
return mu_msg_str_prio ((MuMsgPriority)val);
100
if (mu_msg_field_id(field) == MU_MSG_FIELD_ID_FLAGS) {
101
val = mu_msg_xapian_get_field_numeric (row, field);
102
return mu_msg_str_flags_s ((MuMsgPriority)val);
105
return mu_msg_xapian_get_field (row, field); /* as string */
107
case MU_MSG_FIELD_TYPE_TIME_T:
108
val = mu_msg_xapian_get_field_numeric (row, field);
109
return mu_msg_str_date_s ((time_t)val);
111
case MU_MSG_FIELD_TYPE_BYTESIZE:
112
val = mu_msg_xapian_get_field_numeric (row, field);
113
return mu_msg_str_size_s ((time_t)val);
115
g_return_val_if_reached (NULL);
120
/* returns NULL if there is an error */
122
sort_field_from_string (const char* fieldstr)
124
const MuMsgField *field;
126
field = mu_msg_field_from_name (fieldstr);
128
if (!field && strlen(fieldstr) == 1)
129
field = mu_msg_field_from_shortcut(fieldstr[0]);
132
g_printerr ("not a valid sort field: '%s'\n",
141
print_rows (MuQueryXapian *xapian, const gchar *query, MuConfigOptions *opts)
144
const MuMsgField *sortfield;
146
MU_WRITE_LOG ("query: '%s' (rows)", query);
149
if (opts->sortfield) {
150
sortfield = sort_field_from_string (opts->sortfield);
151
if (!sortfield) /* error occured? */
155
row = mu_query_xapian_run (xapian, query, sortfield,
158
g_printerr ("error: running query failed\n");
160
} else if (mu_msg_xapian_is_done (row)) {
161
g_printerr ("No matches found\n");
162
mu_msg_xapian_destroy (row);
166
/* iterate over the found rows */
168
const char* fields = opts->fields;
173
const MuMsgField* field;
174
field = mu_msg_field_from_shortcut (*fields);
176
!mu_msg_field_is_xapian_enabled (field))
177
printlen += printf ("%c", *fields);
179
printlen += printf ("%s",
180
display_field(row, field));
187
mu_msg_xapian_next (row);
189
} while (!mu_msg_xapian_is_done (row));
191
mu_msg_xapian_destroy (row);
198
do_output_text (MuQueryXapian *xapian, MuConfigOptions* opts,
199
const gchar **params)
202
gboolean retval = TRUE;
204
query = mu_query_xapian_combine (params, FALSE);
206
/* if xquery is set, we print the xapian query instead of the
207
* output; this is for debugging purposes */
209
retval = print_query (xapian, query);
211
retval = print_rows (xapian, query, opts);
219
/* create a linksdir if it not exist yet; if it already existed,
220
* remove old links if opts->clearlinks was specified */
222
create_or_clear_linksdir_maybe (MuConfigOptions* opts)
224
if (access (opts->linksdir, F_OK) != 0) {
225
if (!mu_maildir_mkmdir (opts->linksdir, 0700, TRUE))
228
} else if (opts->clearlinks)
229
mu_maildir_clear_links (opts->linksdir);
235
do_output_links (MuQueryXapian *xapian, MuConfigOptions* opts,
236
const gchar **params)
239
gboolean retval = TRUE;
241
const MuMsgField *pathfield;
243
if (!create_or_clear_linksdir_maybe (opts))
246
query = mu_query_xapian_combine (params, FALSE);
248
MU_WRITE_LOG ("query: '%s' (links)", query);
249
row = mu_query_xapian_run (xapian, query, NULL, FALSE);
251
g_printerr ("error: running query failed\n");
253
} else if (mu_msg_xapian_is_done (row)) {
254
g_printerr ("No matches found\n");
255
mu_msg_xapian_destroy (row);
259
pathfield = mu_msg_field_from_id (MU_MSG_FIELD_ID_PATH);
261
/* iterate over the found rows */
262
for (; !mu_msg_xapian_is_done (row); mu_msg_xapian_next (row)) {
266
path = mu_msg_xapian_get_field (row, pathfield);
270
/* this might happen if the database is not up-to-date */
271
if (access (path, R_OK) != 0) {
272
g_warning ("Cannot read source message %s: %s",
273
path, strerror (errno));
277
if (!mu_maildir_link (path, opts->linksdir))
281
mu_msg_xapian_destroy (row);
289
query_params_valid (MuConfigOptions *opts)
293
g_warning ("Invalid option for '--linksdir'");
297
if (!opts->params[0] || !opts->params[1]) {
298
g_warning ("Missing search expression");
302
if (mu_util_check_dir (opts->xpath, TRUE, FALSE))
305
g_warning ("%s is not a readable Xapian directory", opts->xpath);
306
g_message ("Did you run 'mu index'?");
313
cmd_find (MuConfigOptions *opts)
315
MuQueryXapian *xapian;
317
const gchar **params;
319
if (!query_params_valid (opts))
322
/* first param is 'query', search params are after that */
323
params = (const gchar**)&opts->params[1];
327
xapian = mu_query_xapian_new (opts->xpath);
329
g_warning ("Failed to create Xapian query");
330
mu_msg_gmime_uninit ();
335
rv = do_output_links (xapian, opts, params);
337
rv = do_output_text (xapian, opts, params);
339
mu_query_xapian_destroy (xapian);
340
mu_msg_gmime_uninit();
347
check_index_params (MuConfigOptions *opts)
349
if (opts->linksdir || opts->xquery) {
350
g_warning ("Invalid option(s) for command");
355
if (!mu_util_check_dir (opts->maildir, TRUE, TRUE)) {
356
g_message ("Please provide a valid Maildir");
365
index_msg_cb (MuIndexStats* stats, void *user_data)
373
while (len --> 0) /* note the --> operator :-) */
376
len = snprintf (output, sizeof(output),
377
"%c indexing mail; processed: %d; "
378
"updated/new: %d, cleaned-up: %d",
379
kars[i % 4], stats->_processed,
380
stats->_updated, stats->_cleaned_up);
381
g_print ("%s", output);
389
cmd_index (MuConfigOptions *opts)
393
if (!check_index_params (opts))
396
mu_msg_gmime_init ();
401
mu_index_stats_clear (&stats);
402
midx = mu_index_new (opts->xpath);
405
g_warning ("Indexing failed");
409
g_message ("Indexing messages from %s", opts->maildir);
410
g_message ("Database: %s", opts->xpath);
412
rv = mu_index_run (midx, opts->maildir,
413
opts->reindex, &stats,
414
opts->quiet ? NULL : index_msg_cb,
416
if (!opts->nocleanup) {
417
stats._processed = 0; /* start over */
418
mu_index_cleanup (midx, &stats,
419
opts->quiet ? NULL : index_msg_cb,
424
index_msg_cb (&stats, NULL);
428
MU_WRITE_LOG ("processed: %d; updated/new: %d, cleaned-up: %d",
429
stats._processed, stats._updated,
432
mu_index_destroy (midx);
434
mu_msg_gmime_uninit ();
436
return rv == MU_OK ? TRUE : FALSE;
441
cleanup_cb (MuIndexStats *stats, void *user_data)
452
len = snprintf (output, sizeof(output),
453
"%c mu is cleaning up the message database; "
454
"processed: %d; cleaned-up: %d",
455
kars[i % 4], stats->_processed, stats->_cleaned_up);
456
g_print ("%s", output);
466
cmd_mkdir (MuConfigOptions *opts)
470
if (!opts->params[0])
471
return FALSE; /* shouldn't happen */
473
if (!opts->params[1]) {
474
g_printerr ("usage: mu mkdir <dir> [more dirs]\n");
479
while (opts->params[i]) {
480
MU_WRITE_LOG ("mu_maildir_mkdir (%s, 0755, FALSE)",
482
if (!mu_maildir_mkmdir (opts->params[i], 0755, FALSE))
492
#if 0 /* currently, turned off */
494
cmd_link (MuConfigOptions *opts)
496
if (!opts->params[0])
497
return FALSE; /* shouldn't happen */
499
if (!opts->params[1] || !opts->params[2]) {
500
g_printerr ("usage: mu link <src> <targetdir>\n");
504
return mu_maildir_link (opts->params[1], opts->params[2]);
509
cmd_help (MuConfigOptions *opts)
511
/* FIXME: get context-sensitive help */
513
return _show_usage (FALSE);
517
cmd_cleanup (MuConfigOptions *opts)
521
if (!_check_index_params (opts))
524
mu_msg_gmime_init ();
529
mu_index_stats_clear (&stats);
531
midx = mu_index_new (opts->xpath);
533
g_warning ("Cleanup failed");
537
g_message ("Cleaning up removed messages from %s",
539
mu_index_cleanup (midx, &stats,
540
opts->quiet ? NULL :_cleanup_cb,
542
mu_index_destroy (midx);
545
_cleanup_cb (&stats, NULL);
550
mu_msg_gmime_uninit ();
552
return rv == MU_OK ? TRUE : FALSE;
559
show_usage (gboolean noerror)
562
"usage: mu [options] command [parameters]\n"
563
"\twhere command is one of index, find, cleanup, mkdir\n"
564
"see mu(1) for more information\n";
567
g_print ("%s", usage);
569
g_printerr ("%s", usage);
578
"mu (mail indexer / searcher version) " VERSION "\n\n"
579
"Copyright (C) 2010 Dirk-Jan C. Binnema\n"
580
"License GPLv3+: GNU GPL version 3 or later "
581
"<http://gnu.org/licenses/gpl.html>.\n\n"
582
"This is free software: you are free to change "
583
"and redistribute it.\n"
584
"There is NO WARRANTY, to the extent permitted by law.";
586
g_print ("%s\n", msg);
593
mu_cmd_execute (MuConfigOptions *opts)
598
return show_version ();
600
if (!opts->params||!opts->params[0]) /* no command? */
601
return show_usage (FALSE);
603
cmd = cmd_from_string (opts->params[0]);
607
case MU_CMD_INDEX: return cmd_index (opts);
608
case MU_CMD_FIND: return cmd_find (opts);
610
case MU_CMD_MKDIR: return cmd_mkdir (opts);
612
/* case MU_CMD_CLEANUP: return _cmd_cleanup (opts); */
613
/* case MU_CMD_HELP: return _cmd_help (opts); */
614
/* case MU_CMD_LINK: return _cmd_link (opts); */
616
case MU_CMD_UNKNOWN: return show_usage (FALSE);
618
g_return_val_if_reached (FALSE);