2010-01-31 11:12:04 +01:00
|
|
|
/*
|
2010-08-24 23:57:16 +02:00
|
|
|
** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2010-01-31 11:12:04 +01:00
|
|
|
**
|
|
|
|
** This program is free software; you can redistribute it and/or modify it
|
|
|
|
** under the terms of the GNU General Public License as published by the
|
|
|
|
** Free Software Foundation; either version 3, or (at your option) any
|
|
|
|
** later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2010-08-24 23:57:16 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-01-31 11:12:04 +01:00
|
|
|
#include "config.h"
|
2010-08-24 23:57:16 +02:00
|
|
|
#endif /*HAVE_CONFIG_H*/
|
2010-01-31 11:12:04 +01:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2010-08-24 23:57:16 +02:00
|
|
|
#include "mu-msg.h"
|
2010-01-31 11:12:04 +01:00
|
|
|
#include "mu-maildir.h"
|
|
|
|
#include "mu-index.h"
|
2010-08-25 20:46:16 +02:00
|
|
|
#include "mu-query.h"
|
2010-08-25 20:29:53 +02:00
|
|
|
#include "mu-msg-iter.h"
|
2010-11-13 13:16:38 +01:00
|
|
|
#include "mu-bookmarks.h"
|
2010-11-14 12:55:04 +01:00
|
|
|
#include "mu-runtime.h"
|
|
|
|
|
2010-01-31 11:12:04 +01:00
|
|
|
#include "mu-util.h"
|
2010-08-25 20:40:07 +02:00
|
|
|
#include "mu-util-db.h"
|
2010-08-20 20:07:36 +02:00
|
|
|
#include "mu-cmd.h"
|
2010-10-31 10:34:01 +01:00
|
|
|
#include "mu-output-plain.h"
|
|
|
|
#include "mu-output-link.h"
|
2010-01-31 11:12:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
update_warning (void)
|
|
|
|
{
|
2010-11-15 07:43:30 +01:00
|
|
|
g_warning ("the database needs to be updated to version %s\n",
|
2010-01-31 11:12:04 +01:00
|
|
|
MU_XAPIAN_DB_VERSION);
|
2010-02-20 13:36:43 +01:00
|
|
|
g_message ("please run 'mu index --rebuild' (see the man page)");
|
2010-01-31 11:12:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2010-08-25 20:46:16 +02:00
|
|
|
print_xapian_query (MuQuery *xapian, const gchar *query)
|
2010-01-31 11:12:04 +01:00
|
|
|
{
|
|
|
|
char *querystr;
|
2010-11-23 23:48:28 +01:00
|
|
|
GError *err;
|
2010-01-31 11:12:04 +01:00
|
|
|
|
2010-11-23 23:48:28 +01:00
|
|
|
MU_WRITE_LOG ("query: '%s' (xquery)", query);
|
|
|
|
|
|
|
|
err = NULL;
|
|
|
|
querystr = mu_query_as_string (xapian, query, &err);
|
|
|
|
if (!querystr) {
|
2010-11-30 08:02:29 +01:00
|
|
|
g_warning ("error: %s", err->message);
|
2010-11-23 23:48:28 +01:00
|
|
|
g_error_free (err);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-01-31 11:12:04 +01:00
|
|
|
g_print ("%s\n", querystr);
|
|
|
|
g_free (querystr);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns NULL if there is an error */
|
2010-11-20 13:43:04 +01:00
|
|
|
static MuMsgFieldId
|
2010-01-31 11:12:04 +01:00
|
|
|
sort_field_from_string (const char* fieldstr)
|
|
|
|
{
|
2010-11-20 13:43:04 +01:00
|
|
|
MuMsgFieldId mfid;
|
2010-01-31 11:12:04 +01:00
|
|
|
|
2010-11-20 13:43:04 +01:00
|
|
|
mfid = mu_msg_field_id_from_name (fieldstr, FALSE);
|
2010-01-31 11:12:04 +01:00
|
|
|
|
2010-11-20 13:43:04 +01:00
|
|
|
/* not found? try a shortcut */
|
|
|
|
if (mfid == MU_MSG_FIELD_ID_NONE &&
|
|
|
|
strlen(fieldstr) == 1)
|
|
|
|
mfid = mu_msg_field_id_from_shortcut(fieldstr[0],
|
2010-11-23 22:52:37 +01:00
|
|
|
FALSE);
|
2010-11-20 13:43:04 +01:00
|
|
|
if (mfid == MU_MSG_FIELD_ID_NONE)
|
2010-11-30 21:20:27 +01:00
|
|
|
g_warning ("not a valid sort field: '%s'\n",
|
2010-11-20 13:43:04 +01:00
|
|
|
fieldstr);
|
|
|
|
return mfid;
|
2010-01-31 11:12:04 +01:00
|
|
|
}
|
|
|
|
|
2010-08-15 13:59:55 +02:00
|
|
|
|
2010-01-31 19:36:56 +01:00
|
|
|
static size_t
|
2010-08-25 20:29:53 +02:00
|
|
|
print_rows (MuMsgIter *iter, const char *fields, size_t summary_len)
|
2010-01-31 11:12:04 +01:00
|
|
|
{
|
2010-01-31 19:36:56 +01:00
|
|
|
size_t count = 0;
|
2010-02-02 07:46:55 +01:00
|
|
|
|
2010-09-09 07:17:00 +02:00
|
|
|
if (mu_msg_iter_is_done (iter))
|
2010-02-02 07:46:55 +01:00
|
|
|
return 0;
|
2010-01-31 11:12:04 +01:00
|
|
|
|
2010-01-31 19:36:56 +01:00
|
|
|
do {
|
2010-10-31 10:34:01 +01:00
|
|
|
if (mu_output_plain_row (iter, fields, summary_len))
|
|
|
|
++count;
|
2010-01-31 11:12:04 +01:00
|
|
|
|
2010-08-25 20:29:53 +02:00
|
|
|
} while (mu_msg_iter_next (iter));
|
2010-01-31 11:12:04 +01:00
|
|
|
|
2010-01-31 19:36:56 +01:00
|
|
|
return count;
|
2010-01-31 11:12:04 +01:00
|
|
|
}
|
|
|
|
|
2010-01-31 19:36:56 +01:00
|
|
|
|
|
|
|
static size_t
|
2010-08-25 20:29:53 +02:00
|
|
|
make_links (MuMsgIter *iter, const char* linksdir, gboolean clearlinks)
|
2010-01-31 11:12:04 +01:00
|
|
|
{
|
2010-01-31 19:36:56 +01:00
|
|
|
size_t count = 0;
|
2010-10-31 10:34:01 +01:00
|
|
|
|
|
|
|
if (!mu_output_link_create_dir (linksdir, clearlinks))
|
2010-01-31 19:36:56 +01:00
|
|
|
return 0;
|
2010-02-02 07:46:55 +01:00
|
|
|
|
2010-09-09 07:17:00 +02:00
|
|
|
if (mu_msg_iter_is_done (iter))
|
2010-02-02 07:46:55 +01:00
|
|
|
return 0;
|
2010-01-31 11:12:04 +01:00
|
|
|
|
|
|
|
/* iterate over the found iters */
|
2010-01-31 19:36:56 +01:00
|
|
|
do {
|
2010-10-31 10:34:01 +01:00
|
|
|
/* ignore errors...*/
|
|
|
|
if (mu_output_link_row (iter, linksdir))
|
|
|
|
++count;
|
2010-02-02 07:46:55 +01:00
|
|
|
|
2010-08-25 20:29:53 +02:00
|
|
|
} while (mu_msg_iter_next (iter));
|
2010-01-31 19:36:56 +01:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2010-08-25 20:46:16 +02:00
|
|
|
run_query (MuQuery *xapian, const gchar *query, MuConfigOptions *opts)
|
2010-01-31 19:36:56 +01:00
|
|
|
{
|
2010-11-23 23:48:28 +01:00
|
|
|
GError *err;
|
2010-08-25 20:29:53 +02:00
|
|
|
MuMsgIter *iter;
|
2010-11-20 13:43:04 +01:00
|
|
|
MuMsgFieldId sortid;
|
2010-01-31 19:36:56 +01:00
|
|
|
size_t matches;
|
|
|
|
|
2010-11-20 13:43:04 +01:00
|
|
|
sortid = MU_MSG_FIELD_ID_NONE;
|
2010-01-31 19:36:56 +01:00
|
|
|
if (opts->sortfield) {
|
2010-11-20 13:43:04 +01:00
|
|
|
sortid = sort_field_from_string (opts->sortfield);
|
|
|
|
if (sortid == MU_MSG_FIELD_ID_NONE) /* error occured? */
|
2010-01-31 19:36:56 +01:00
|
|
|
return FALSE;
|
2010-01-31 11:12:04 +01:00
|
|
|
}
|
2010-11-23 23:48:28 +01:00
|
|
|
|
|
|
|
err = NULL;
|
2010-11-23 22:52:37 +01:00
|
|
|
iter = mu_query_run (xapian, query, sortid,
|
2010-11-23 23:48:28 +01:00
|
|
|
opts->descending ? FALSE : TRUE, 0, &err);
|
2010-01-31 19:36:56 +01:00
|
|
|
if (!iter) {
|
2010-11-30 08:02:29 +01:00
|
|
|
g_warning ("error: %s", err->message);
|
2010-11-23 23:48:28 +01:00
|
|
|
g_error_free (err);
|
2010-01-31 19:36:56 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts->linksdir)
|
2010-11-20 13:43:04 +01:00
|
|
|
matches = make_links (iter, opts->linksdir,
|
|
|
|
opts->clearlinks);
|
2010-01-31 19:36:56 +01:00
|
|
|
else
|
2010-11-20 13:43:04 +01:00
|
|
|
matches = print_rows (iter, opts->fields,
|
|
|
|
opts->summary_len);
|
2010-01-31 19:36:56 +01:00
|
|
|
|
|
|
|
if (matches == 0)
|
2010-11-30 21:20:27 +01:00
|
|
|
g_warning ("no matches found");
|
2010-02-02 07:46:55 +01:00
|
|
|
|
2010-08-25 20:29:53 +02:00
|
|
|
mu_msg_iter_destroy (iter);
|
2010-01-31 19:36:56 +01:00
|
|
|
|
|
|
|
return matches > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-31 11:12:04 +01:00
|
|
|
static gboolean
|
|
|
|
query_params_valid (MuConfigOptions *opts)
|
|
|
|
{
|
2010-11-14 12:55:04 +01:00
|
|
|
const gchar *xpath;
|
|
|
|
|
2010-01-31 11:12:04 +01:00
|
|
|
if (opts->linksdir)
|
|
|
|
if (opts->xquery) {
|
2010-11-30 08:02:29 +01:00
|
|
|
g_warning ("invalid option for --linksdir");
|
2010-01-31 11:12:04 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2010-11-14 12:55:04 +01:00
|
|
|
|
|
|
|
xpath = mu_runtime_xapian_dir();
|
2010-11-13 13:16:38 +01:00
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
if (mu_util_check_dir (xpath, TRUE, FALSE))
|
2010-01-31 11:12:04 +01:00
|
|
|
return TRUE;
|
2010-11-14 12:55:04 +01:00
|
|
|
|
|
|
|
g_warning ("'%s' is not a readable Xapian directory\n", xpath);
|
2010-11-30 21:20:27 +01:00
|
|
|
g_message ("did you run 'mu index'?");
|
2010-01-31 11:12:04 +01:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-11-13 13:16:38 +01:00
|
|
|
static gchar*
|
|
|
|
resolve_bookmark (MuConfigOptions *opts)
|
|
|
|
{
|
|
|
|
MuBookmarks *bm;
|
|
|
|
char* val;
|
2010-11-14 12:55:04 +01:00
|
|
|
const gchar *bmfile;
|
2010-11-13 13:16:38 +01:00
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
bmfile = mu_runtime_bookmarks_file();
|
|
|
|
bm = mu_bookmarks_new (bmfile);
|
2010-11-13 13:16:38 +01:00
|
|
|
if (!bm) {
|
2010-11-30 08:02:29 +01:00
|
|
|
g_warning ("failed to open bookmarks file '%s'", bmfile);
|
2010-11-13 13:16:38 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = (gchar*)mu_bookmarks_lookup (bm, opts->bookmark);
|
|
|
|
if (!val)
|
2010-11-30 08:02:29 +01:00
|
|
|
g_warning ("bookmark '%s' not found", opts->bookmark);
|
2010-11-13 13:16:38 +01:00
|
|
|
else
|
|
|
|
val = g_strdup (val);
|
|
|
|
|
|
|
|
mu_bookmarks_destroy (bm);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gchar*
|
|
|
|
get_query (MuConfigOptions *opts)
|
|
|
|
{
|
|
|
|
gchar *query, *bookmarkval;
|
|
|
|
|
|
|
|
/* params[0] is 'find', actual search params start with [1] */
|
|
|
|
if (!opts->bookmark && !opts->params[1]) {
|
2010-11-30 08:02:29 +01:00
|
|
|
g_warning ("usage: mu find [options] search-expression");
|
2010-11-13 13:16:38 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2010-11-23 23:48:28 +01:00
|
|
|
|
|
|
|
bookmarkval = NULL;
|
|
|
|
if (opts->bookmark) {
|
|
|
|
bookmarkval = resolve_bookmark (opts);
|
|
|
|
if (!bookmarkval)
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-11-13 13:16:38 +01:00
|
|
|
|
|
|
|
query = mu_util_str_from_strv ((const gchar**)&opts->params[1]);
|
|
|
|
if (bookmarkval) {
|
|
|
|
gchar *tmp;
|
|
|
|
tmp = g_strdup_printf ("%s %s", bookmarkval, query);
|
|
|
|
g_free (query);
|
|
|
|
query = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (bookmarkval);
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2010-11-20 16:27:17 +01:00
|
|
|
static gboolean
|
|
|
|
db_is_ready (const char *xpath)
|
|
|
|
{
|
|
|
|
if (mu_util_db_is_empty (xpath)) {
|
2010-11-30 08:02:29 +01:00
|
|
|
g_warning ("database is empty; use 'mu index' to "
|
2010-11-20 16:27:17 +01:00
|
|
|
"add messages");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mu_util_db_version_up_to_date (xpath)) {
|
|
|
|
update_warning ();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-11-13 13:16:38 +01:00
|
|
|
|
2010-01-31 11:12:04 +01:00
|
|
|
gboolean
|
|
|
|
mu_cmd_find (MuConfigOptions *opts)
|
|
|
|
{
|
2010-11-23 23:48:28 +01:00
|
|
|
GError *err;
|
2010-08-25 20:46:16 +02:00
|
|
|
MuQuery *xapian;
|
2010-01-31 11:12:04 +01:00
|
|
|
gboolean rv;
|
2010-11-13 13:16:38 +01:00
|
|
|
gchar *query;
|
2010-11-14 12:55:04 +01:00
|
|
|
const gchar *xpath;
|
2010-11-13 13:16:38 +01:00
|
|
|
|
2010-01-31 11:12:04 +01:00
|
|
|
g_return_val_if_fail (opts, FALSE);
|
2010-08-29 15:39:27 +02:00
|
|
|
g_return_val_if_fail (mu_cmd_equals (opts, "find"), FALSE);
|
2010-01-31 11:12:04 +01:00
|
|
|
|
|
|
|
if (!query_params_valid (opts))
|
|
|
|
return FALSE;
|
|
|
|
|
2010-11-14 12:55:04 +01:00
|
|
|
xpath = mu_runtime_xapian_dir ();
|
2010-11-20 16:27:17 +01:00
|
|
|
if (!db_is_ready(xpath))
|
2010-01-31 11:12:04 +01:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* first param is 'query', search params are after that */
|
2010-11-13 13:16:38 +01:00
|
|
|
query = get_query (opts);
|
|
|
|
if (!query)
|
|
|
|
return FALSE;
|
2010-11-23 23:48:28 +01:00
|
|
|
|
|
|
|
err = NULL;
|
|
|
|
xapian = mu_query_new (xpath, &err);
|
2010-01-31 11:12:04 +01:00
|
|
|
if (!xapian) {
|
2010-11-30 08:02:29 +01:00
|
|
|
g_warning ("error: %s", err->message);
|
2010-11-23 23:48:28 +01:00
|
|
|
g_error_free (err);
|
2010-01-31 11:12:04 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-11-13 13:16:38 +01:00
|
|
|
if (opts->xquery)
|
|
|
|
rv = print_xapian_query (xapian, query);
|
|
|
|
else
|
|
|
|
rv = run_query (xapian, query, opts);
|
|
|
|
|
2010-08-25 20:46:16 +02:00
|
|
|
mu_query_destroy (xapian);
|
2010-11-13 13:16:38 +01:00
|
|
|
g_free (query);
|
2010-01-31 11:12:04 +01:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2010-01-31 13:14:56 +01:00
|
|
|
|