mu/src/mu-cmd-find.c

360 lines
7.4 KiB
C
Raw Normal View History

/*
2011-01-04 22:19:03 +01:00
** Copyright (C) 2008-2011 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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.
**
*/
2011-01-04 22:19:03 +01:00
#if HAVE_CONFIG_H
#include "config.h"
#endif /*HAVE_CONFIG_H*/
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <signal.h>
#include "mu-msg.h"
#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"
#include "mu-bookmarks.h"
#include "mu-runtime.h"
#include "mu-util.h"
2010-08-20 20:07:36 +02:00
#include "mu-cmd.h"
2011-01-04 22:19:03 +01:00
#include "mu-output.h"
enum _OutputFormat {
FORMAT_JSON,
FORMAT_LINKS,
FORMAT_PLAIN,
FORMAT_SEXP,
FORMAT_XML,
FORMAT_XQUERY,
FORMAT_NONE
};
typedef enum _OutputFormat OutputFormat;
static OutputFormat
get_output_format (const char *formatstr)
{
int i;
struct {
const char* name;
OutputFormat format;
} formats [] = {
{MU_CONFIG_FORMAT_JSON, FORMAT_JSON},
{MU_CONFIG_FORMAT_LINKS, FORMAT_LINKS},
{MU_CONFIG_FORMAT_PLAIN, FORMAT_PLAIN},
{MU_CONFIG_FORMAT_SEXP, FORMAT_SEXP},
{MU_CONFIG_FORMAT_XML, FORMAT_XML},
{MU_CONFIG_FORMAT_XQUERY, FORMAT_XQUERY}
};
for (i = 0; i != G_N_ELEMENTS(formats); i++)
if (strcmp (formats[i].name, formatstr) == 0)
return formats[i].format;
return FORMAT_NONE;
}
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",
MU_XAPIAN_DB_VERSION);
g_message ("please run 'mu index --rebuild' (see the man page)");
}
static gboolean
2010-08-25 20:46:16 +02:00
print_xapian_query (MuQuery *xapian, const gchar *query)
{
char *querystr;
GError *err;
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);
g_error_free (err);
return FALSE;
}
g_print ("%s\n", querystr);
g_free (querystr);
return TRUE;
}
/* returns NULL if there is an error */
static MuMsgFieldId
sort_field_from_string (const char* fieldstr)
{
MuMsgFieldId mfid;
mfid = mu_msg_field_id_from_name (fieldstr, FALSE);
/* 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);
if (mfid == MU_MSG_FIELD_ID_NONE)
g_warning ("not a valid sort field: '%s'\n",
fieldstr);
return mfid;
}
2010-01-31 19:36:56 +01:00
static gboolean
2011-01-04 22:19:03 +01:00
run_query (MuQuery *xapian, const gchar *query, MuConfig *opts,
OutputFormat format, size_t *count)
2010-01-31 19:36:56 +01:00
{
GError *err;
2010-08-25 20:29:53 +02:00
MuMsgIter *iter;
MuMsgFieldId sortid;
2011-01-04 22:19:03 +01:00
gboolean rv;
2010-01-31 19:36:56 +01:00
sortid = MU_MSG_FIELD_ID_NONE;
2010-01-31 19:36:56 +01:00
if (opts->sortfield) {
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;
}
err = NULL;
2010-11-23 22:52:37 +01:00
iter = mu_query_run (xapian, query, sortid,
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);
g_error_free (err);
2010-01-31 19:36:56 +01:00
return FALSE;
}
switch (format) {
case FORMAT_LINKS:
2011-01-04 22:19:03 +01:00
rv = mu_output_links (iter, opts->linksdir, opts->clearlinks,
count);
break;
case FORMAT_PLAIN:
2011-01-04 22:19:03 +01:00
rv = mu_output_plain (iter, opts->fields, opts->summary_len,
count);
break;
2011-01-05 23:09:26 +01:00
case FORMAT_XML: rv = mu_output_xml (iter, count); break;
case FORMAT_JSON: rv = mu_output_json (iter, count); break;
case FORMAT_SEXP: rv = mu_output_sexp (iter, count); break;
default:
g_assert_not_reached ();
return FALSE;
}
2011-01-04 22:19:03 +01:00
if (count && *count == 0)
g_warning ("no matches found");
2010-08-25 20:29:53 +02:00
mu_msg_iter_destroy (iter);
2010-01-31 19:36:56 +01:00
2011-01-04 22:19:03 +01:00
return rv;
2010-01-31 19:36:56 +01:00
}
static gboolean
2011-01-04 22:19:03 +01:00
query_params_valid (MuConfig *opts)
{
OutputFormat format;
const gchar *xpath;
if (opts->linksdir)
if (opts->xquery) {
2010-11-30 08:02:29 +01:00
g_warning ("invalid option for --linksdir");
return FALSE;
}
format = get_output_format (opts->formatstr);
if (format == FORMAT_NONE) {
g_warning ("invalid output format %s",
opts->formatstr ? opts->formatstr : "<none>");
return FALSE;
}
if (format == FORMAT_LINKS && !opts->linksdir) {
g_warning ("missing --linksdir argument");
return FALSE;
}
if (opts->linksdir && format != FORMAT_LINKS) {
g_warning ("--linksdir is only valid with --format=links");
return FALSE;
}
xpath = mu_runtime_xapian_dir();
if (mu_util_check_dir (xpath, TRUE, FALSE))
return TRUE;
g_warning ("'%s' is not a readable Xapian directory\n", xpath);
g_message ("did you run 'mu index'?");
return FALSE;
}
static gchar*
2011-01-04 22:19:03 +01:00
resolve_bookmark (MuConfig *opts)
{
MuBookmarks *bm;
char* val;
const gchar *bmfile;
bmfile = mu_runtime_bookmarks_file();
bm = mu_bookmarks_new (bmfile);
if (!bm) {
2010-11-30 08:02:29 +01:00
g_warning ("failed to open bookmarks file '%s'", bmfile);
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);
else
val = g_strdup (val);
mu_bookmarks_destroy (bm);
return val;
}
static gchar*
2011-01-04 22:19:03 +01:00
get_query (MuConfig *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");
return FALSE;
}
bookmarkval = NULL;
if (opts->bookmark) {
bookmarkval = resolve_bookmark (opts);
if (!bookmarkval)
return NULL;
}
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;
}
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 "
"add messages");
return FALSE;
}
if (!mu_util_db_version_up_to_date (xpath)) {
update_warning ();
return FALSE;
}
return TRUE;
}
static MuQuery*
get_query_obj (void)
{
GError *err;
const char* xpath;
MuQuery *mquery;
xpath = mu_runtime_xapian_dir ();
if (!db_is_ready(xpath)) {
g_warning ("database '%s' is not ready", xpath);
return NULL;
}
err = NULL;
mquery = mu_query_new (xpath, &err);
if (!mquery) {
g_warning ("error: %s", err->message);
g_error_free (err);
return NULL;
}
return mquery;
}
2011-01-04 22:19:03 +01:00
MuExitCode
mu_cmd_find (MuConfig *opts)
{
2010-08-25 20:46:16 +02:00
MuQuery *xapian;
gboolean rv;
gchar *query;
2011-01-04 22:19:03 +01:00
size_t count;
OutputFormat format;
g_return_val_if_fail (opts, FALSE);
2011-01-04 22:19:03 +01:00
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_FIND, FALSE);
if (!query_params_valid (opts))
2011-01-04 22:19:03 +01:00
return MU_EXITCODE_ERROR;
format = get_output_format (opts->formatstr);
2011-01-04 22:19:03 +01:00
xapian = get_query_obj ();
if (!xapian)
2011-01-04 22:19:03 +01:00
return MU_EXITCODE_ERROR;
/* first param is 'query', search params are after that */
query = get_query (opts);
if (!query)
2011-01-04 22:19:03 +01:00
return MU_EXITCODE_ERROR;
if (format == FORMAT_XQUERY)
rv = print_xapian_query (xapian, query);
else
rv = run_query (xapian, query, opts, format, &count);
2010-08-25 20:46:16 +02:00
mu_query_destroy (xapian);
g_free (query);
2011-01-04 22:19:03 +01:00
if (!rv)
return MU_EXITCODE_ERROR;
else
return (count == 0) ?
MU_EXITCODE_NO_MATCHES : MU_EXITCODE_OK;
}