2010-01-31 11:12:04 +01:00
|
|
|
/*
|
2022-04-28 21:47:00 +02:00
|
|
|
** Copyright (C) 2008-2022 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.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2020-08-15 10:04:36 +02:00
|
|
|
#include <array>
|
|
|
|
|
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>
|
|
|
|
|
2022-04-22 07:02:12 +02:00
|
|
|
#include "message/mu-message.hh"
|
2020-11-28 09:16:43 +01:00
|
|
|
#include "mu-maildir.hh"
|
|
|
|
#include "mu-query-match-deciders.hh"
|
2020-11-03 09:01:18 +01:00
|
|
|
#include "mu-query.hh"
|
2020-11-07 13:06:23 +01:00
|
|
|
#include "mu-bookmarks.hh"
|
|
|
|
#include "mu-runtime.hh"
|
2022-03-20 13:12:41 +01:00
|
|
|
#include "message/mu-message.hh"
|
2010-11-14 12:55:04 +01:00
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
#include "utils/mu-option.hh"
|
2019-12-16 20:44:35 +01:00
|
|
|
#include "utils/mu-util.h"
|
|
|
|
|
2020-06-08 22:04:05 +02:00
|
|
|
#include "mu-cmd.hh"
|
2022-02-22 21:48:29 +01:00
|
|
|
#include "utils/mu-utils.hh"
|
2011-01-05 19:35:50 +01:00
|
|
|
|
2020-07-12 14:10:35 +02:00
|
|
|
using namespace Mu;
|
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
struct OutputInfo {
|
|
|
|
Xapian::docid docid{};
|
|
|
|
bool header{};
|
|
|
|
bool footer{};
|
|
|
|
bool last{};
|
|
|
|
Option<QueryMatch&> match_info;
|
2020-11-28 09:16:43 +01:00
|
|
|
};
|
|
|
|
|
2022-02-18 09:49:56 +01:00
|
|
|
constexpr auto FirstOutput{OutputInfo{0, true, false, {}, {}}};
|
|
|
|
constexpr auto LastOutput{OutputInfo{0, false, true, {}, {}}};
|
2020-11-28 09:16:43 +01:00
|
|
|
|
2022-04-22 07:02:12 +02:00
|
|
|
using OutputFunc = std::function<bool(const Option<Message>& msg, const OutputInfo&,
|
|
|
|
const MuConfig*, GError**)>;
|
2010-01-31 11:12:04 +01:00
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
static Result<void>
|
2022-01-30 13:33:25 +01:00
|
|
|
print_internal(const Store& store,
|
2022-02-22 21:48:29 +01:00
|
|
|
const std::string& expr,
|
|
|
|
gboolean xapian,
|
2022-04-28 21:47:00 +02:00
|
|
|
gboolean warn)
|
2010-01-31 11:12:04 +01:00
|
|
|
{
|
2022-01-30 13:33:25 +01:00
|
|
|
std::cout << store.parse_query(expr, xapian) << "\n";
|
2022-04-28 21:47:00 +02:00
|
|
|
return Ok();
|
2010-01-31 11:12:04 +01:00
|
|
|
}
|
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
static Result<QueryResults>
|
|
|
|
run_query(const Store& store, const std::string& expr, const MuConfig* opts)
|
2010-01-31 19:36:56 +01:00
|
|
|
{
|
2022-03-20 13:12:41 +01:00
|
|
|
const auto sortfield{field_from_name(opts->sortfield ? opts->sortfield : "")};
|
2022-04-28 21:47:00 +02:00
|
|
|
if (!sortfield && opts->sortfield)
|
|
|
|
return Err(Error::Code::InvalidArgument,
|
|
|
|
"invalid sort field: '%s'", opts->sortfield);
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-07-19 20:54:26 +02:00
|
|
|
Mu::QueryFlags qflags{QueryFlags::SkipUnreadable};
|
2021-10-20 11:18:15 +02:00
|
|
|
if (opts->reverse)
|
|
|
|
qflags |= QueryFlags::Descending;
|
|
|
|
if (opts->skip_dups)
|
|
|
|
qflags |= QueryFlags::SkipDuplicates;
|
|
|
|
if (opts->include_related)
|
|
|
|
qflags |= QueryFlags::IncludeRelated;
|
|
|
|
if (opts->threads)
|
|
|
|
qflags |= QueryFlags::Threading;
|
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
return store.run_query(expr, sortfield.value_or(field_from_id(Field::Id::Date)).id,
|
|
|
|
qflags, opts->maxnum);
|
2011-07-06 01:11:55 +02:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-07-10 13:48:45 +02:00
|
|
|
static gboolean
|
2022-04-22 07:02:12 +02:00
|
|
|
exec_cmd(const Option<Message>& msg, const OutputInfo& info, const MuConfig* opts, GError** err)
|
2011-07-06 22:35:52 +02:00
|
|
|
{
|
2022-04-22 07:02:12 +02:00
|
|
|
if (!msg)
|
|
|
|
return TRUE;
|
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
gint status;
|
|
|
|
char * cmdline, *escpath;
|
|
|
|
gboolean rv;
|
2011-08-30 20:57:59 +02:00
|
|
|
|
2022-04-22 07:02:12 +02:00
|
|
|
escpath = g_shell_quote(msg->path().c_str());
|
2021-10-20 11:18:15 +02:00
|
|
|
cmdline = g_strdup_printf("%s %s", opts->exec, escpath);
|
2011-08-30 20:57:59 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
rv = g_spawn_command_line_sync(cmdline, NULL, NULL, &status, err);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
g_free(cmdline);
|
|
|
|
g_free(escpath);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
return rv;
|
2011-07-06 22:35:52 +02:00
|
|
|
}
|
|
|
|
|
2010-11-13 13:16:38 +01:00
|
|
|
static gchar*
|
2021-10-20 11:18:15 +02:00
|
|
|
resolve_bookmark(const MuConfig* opts, GError** err)
|
2010-11-13 13:16:38 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
MuBookmarks* bm;
|
|
|
|
char* val;
|
|
|
|
const gchar* bmfile;
|
|
|
|
|
|
|
|
bmfile = mu_runtime_path(MU_RUNTIME_PATH_BOOKMARKS);
|
|
|
|
bm = mu_bookmarks_new(bmfile);
|
|
|
|
if (!bm) {
|
|
|
|
g_set_error(err,
|
2022-02-22 21:48:29 +01:00
|
|
|
MU_ERROR_DOMAIN,
|
|
|
|
MU_ERROR_FILE_CANNOT_OPEN,
|
|
|
|
"failed to open bookmarks file '%s'",
|
|
|
|
bmfile);
|
2021-10-20 11:18:15 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = (gchar*)mu_bookmarks_lookup(bm, opts->bookmark);
|
|
|
|
if (!val)
|
|
|
|
g_set_error(err,
|
2022-02-22 21:48:29 +01:00
|
|
|
MU_ERROR_DOMAIN,
|
|
|
|
MU_ERROR_NO_MATCHES,
|
|
|
|
"bookmark '%s' not found",
|
|
|
|
opts->bookmark);
|
2021-10-20 11:18:15 +02:00
|
|
|
else
|
|
|
|
val = g_strdup(val);
|
|
|
|
|
|
|
|
mu_bookmarks_destroy(bm);
|
|
|
|
return val;
|
2010-11-13 13:16:38 +01:00
|
|
|
}
|
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
static Result<std::string>
|
|
|
|
get_query(const MuConfig* opts)
|
2010-11-13 13:16:38 +01:00
|
|
|
{
|
2022-04-28 21:47:00 +02:00
|
|
|
GError *err{};
|
2021-10-20 11:18:15 +02:00
|
|
|
gchar *query, *bookmarkval;
|
|
|
|
|
|
|
|
/* params[0] is 'find', actual search params start with [1] */
|
2022-04-28 21:47:00 +02:00
|
|
|
if (!opts->bookmark && !opts->params[1])
|
|
|
|
return Err(Error::Code::InvalidArgument, "error in parameters");
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
bookmarkval = {};
|
2021-10-20 11:18:15 +02:00
|
|
|
if (opts->bookmark) {
|
2022-04-28 21:47:00 +02:00
|
|
|
bookmarkval = resolve_bookmark(opts, &err);
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!bookmarkval)
|
2022-04-28 21:47:00 +02:00
|
|
|
return Err(Error::Code::Command, &err,
|
|
|
|
"failed to resolve bookmark");
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
query = g_strjoinv(" ", &opts->params[1]);
|
|
|
|
if (bookmarkval) {
|
|
|
|
gchar* tmp;
|
|
|
|
tmp = g_strdup_printf("%s %s", bookmarkval, query);
|
|
|
|
g_free(query);
|
|
|
|
query = tmp;
|
|
|
|
}
|
|
|
|
g_free(bookmarkval);
|
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
return Ok(to_string_gchar(std::move(query)));
|
2010-11-13 13:16:38 +01:00
|
|
|
}
|
|
|
|
|
2022-06-29 06:48:01 +02:00
|
|
|
static bool
|
2021-10-20 11:18:15 +02:00
|
|
|
prepare_links(const MuConfig* opts, GError** err)
|
2011-06-02 10:18:47 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
/* note, mu_maildir_mkdir simply ignores whatever part of the
|
|
|
|
* mail dir already exists */
|
2022-05-09 18:34:07 +02:00
|
|
|
if (auto&& res = maildir_mkdir(opts->linksdir, 0700, true); !res) {
|
2022-02-16 22:03:48 +01:00
|
|
|
res.error().fill_g_error(err);
|
2022-06-29 06:48:01 +02:00
|
|
|
return false;
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
|
|
|
|
2022-02-16 22:03:48 +01:00
|
|
|
if (!opts->clearlinks)
|
2022-06-29 06:48:01 +02:00
|
|
|
return false;
|
2022-02-16 22:03:48 +01:00
|
|
|
|
2022-05-09 18:34:07 +02:00
|
|
|
if (auto&& res = maildir_clear_links(opts->linksdir); !res) {
|
2022-02-16 22:03:48 +01:00
|
|
|
res.error().fill_g_error(err);
|
2022-06-29 06:48:01 +02:00
|
|
|
return false;
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
|
|
|
|
2022-06-29 06:48:01 +02:00
|
|
|
return true;
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2020-11-28 09:16:43 +01:00
|
|
|
static bool
|
2022-04-22 07:02:12 +02:00
|
|
|
output_link(const Option<Message>& msg, const OutputInfo& info, const MuConfig* opts, GError** err)
|
2011-06-02 10:18:47 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
if (info.header)
|
|
|
|
return prepare_links(opts, err);
|
|
|
|
else if (info.footer)
|
|
|
|
return true;
|
2022-06-29 06:48:01 +02:00
|
|
|
|
2022-05-09 18:34:07 +02:00
|
|
|
if (auto&& res = maildir_link(msg->path(), opts->linksdir); !res) {
|
2022-02-16 22:03:48 +01:00
|
|
|
res.error().fill_g_error(err);
|
2022-06-29 06:48:01 +02:00
|
|
|
return false;
|
2022-02-16 22:03:48 +01:00
|
|
|
}
|
2022-06-29 06:48:01 +02:00
|
|
|
|
|
|
|
return true;
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-03-04 23:36:37 +01:00
|
|
|
ansi_color_maybe(Field::Id field_id, gboolean color)
|
2011-06-02 10:18:47 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
const char* ansi;
|
2011-06-02 10:18:47 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!color)
|
|
|
|
return; /* nothing to do */
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2022-03-04 23:36:37 +01:00
|
|
|
switch (field_id) {
|
|
|
|
case Field::Id::From: ansi = MU_COLOR_CYAN; break;
|
2011-06-02 10:18:47 +02:00
|
|
|
|
2022-03-04 23:36:37 +01:00
|
|
|
case Field::Id::To:
|
|
|
|
case Field::Id::Cc:
|
|
|
|
case Field::Id::Bcc: ansi = MU_COLOR_BLUE; break;
|
|
|
|
case Field::Id::Subject: ansi = MU_COLOR_GREEN; break;
|
|
|
|
case Field::Id::Date: ansi = MU_COLOR_MAGENTA; break;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
default:
|
2022-03-20 13:12:41 +01:00
|
|
|
if (field_from_id(field_id).type != Field::Type::String)
|
2021-10-20 11:18:15 +02:00
|
|
|
ansi = MU_COLOR_YELLOW;
|
|
|
|
else
|
|
|
|
ansi = MU_COLOR_RED;
|
|
|
|
}
|
2011-06-02 10:18:47 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
fputs(ansi, stdout);
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-03-04 23:36:37 +01:00
|
|
|
ansi_reset_maybe(Field::Id field_id, gboolean color)
|
2011-06-02 10:18:47 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!color)
|
|
|
|
return; /* nothing to do */
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
fputs(MU_COLOR_DEFAULT, stdout);
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2022-02-22 21:48:29 +01:00
|
|
|
static std::string
|
2022-04-22 07:02:12 +02:00
|
|
|
display_field(const Message& msg, Field::Id field_id)
|
2011-06-02 10:18:47 +02:00
|
|
|
{
|
2022-03-20 13:12:41 +01:00
|
|
|
switch (field_from_id(field_id).type) {
|
2022-04-22 07:02:12 +02:00
|
|
|
case Field::Type::String:
|
|
|
|
return msg.document().string_value(field_id);
|
2022-03-04 23:36:37 +01:00
|
|
|
case Field::Type::Integer:
|
|
|
|
if (field_id == Field::Id::Priority) {
|
2022-04-22 07:02:12 +02:00
|
|
|
return to_string(msg.priority());
|
2022-03-04 23:36:37 +01:00
|
|
|
} else if (field_id == Field::Id::Flags) {
|
2022-04-22 07:02:12 +02:00
|
|
|
return to_string(msg.flags());
|
2021-10-20 11:18:15 +02:00
|
|
|
} else /* as string */
|
2022-04-22 07:02:12 +02:00
|
|
|
return msg.document().string_value(field_id);
|
2022-03-04 23:36:37 +01:00
|
|
|
case Field::Type::TimeT:
|
2022-02-22 21:48:29 +01:00
|
|
|
return time_to_string(
|
2022-04-22 07:02:12 +02:00
|
|
|
"%c", static_cast<::time_t>(msg.document().integer_value(field_id)));
|
2022-03-04 23:36:37 +01:00
|
|
|
case Field::Type::ByteSize:
|
2022-04-22 07:02:12 +02:00
|
|
|
return to_string(msg.document().integer_value(field_id));
|
|
|
|
case Field::Type::StringList:
|
|
|
|
return join(msg.document().string_vec_value(field_id), ',');
|
2022-04-28 21:47:00 +02:00
|
|
|
case Field::Type::ContactList:
|
|
|
|
return to_string(msg.document().contacts_value(field_id));
|
2022-04-22 07:02:12 +02:00
|
|
|
default:
|
|
|
|
g_return_val_if_reached("");
|
|
|
|
return "";
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-04-22 07:02:12 +02:00
|
|
|
print_summary(const Message& msg, const MuConfig* opts)
|
2011-06-02 10:18:47 +02:00
|
|
|
{
|
2022-04-22 07:02:12 +02:00
|
|
|
const auto body{msg.body_text()};
|
|
|
|
if (!body)
|
|
|
|
return;
|
2012-05-04 08:40:38 +02:00
|
|
|
|
2022-04-22 07:02:12 +02:00
|
|
|
const auto summ{to_string_opt_gchar(
|
|
|
|
mu_str_summarize(body->c_str(),
|
|
|
|
opts->summary_len))};
|
2012-05-04 08:40:38 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
g_print("Summary: ");
|
2022-04-22 07:02:12 +02:00
|
|
|
mu_util_fputs_encoded(summ ? summ->c_str() : "<none>", stdout);
|
2021-10-20 11:18:15 +02:00
|
|
|
g_print("\n");
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2011-06-18 17:49:33 +02:00
|
|
|
static void
|
2021-10-20 11:18:15 +02:00
|
|
|
thread_indent(const QueryMatch& info, const MuConfig* opts)
|
2011-06-18 17:49:33 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
const auto is_root{any_of(info.flags & QueryMatch::Flags::Root)};
|
|
|
|
const auto first_child{any_of(info.flags & QueryMatch::Flags::First)};
|
|
|
|
const auto last_child{any_of(info.flags & QueryMatch::Flags::Last)};
|
|
|
|
const auto empty_parent{any_of(info.flags & QueryMatch::Flags::Orphan)};
|
|
|
|
const auto is_dup{any_of(info.flags & QueryMatch::Flags::Duplicate)};
|
|
|
|
// const auto is_related{any_of(info.flags & QueryMatch::Flags::Related)};
|
|
|
|
|
|
|
|
/* indent */
|
|
|
|
if (opts->debug) {
|
|
|
|
::fputs(info.thread_path.c_str(), stdout);
|
|
|
|
::fputs(" ", stdout);
|
|
|
|
} else
|
|
|
|
for (auto i = info.thread_level; i > 1; --i)
|
|
|
|
::fputs(" ", stdout);
|
|
|
|
|
|
|
|
if (!is_root) {
|
|
|
|
if (first_child)
|
|
|
|
::fputs("\\", stdout);
|
|
|
|
else if (last_child)
|
|
|
|
::fputs("/", stdout);
|
|
|
|
else
|
|
|
|
::fputs(" ", stdout);
|
2022-01-30 13:33:25 +01:00
|
|
|
::fputs(empty_parent ? "*> " : is_dup ? "=> "
|
2022-02-22 21:48:29 +01:00
|
|
|
: "-> ",
|
|
|
|
stdout);
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
2011-06-18 17:49:33 +02:00
|
|
|
}
|
2011-06-02 10:18:47 +02:00
|
|
|
|
2011-09-03 09:43:08 +02:00
|
|
|
static void
|
2022-04-22 07:02:12 +02:00
|
|
|
output_plain_fields(const Message& msg, const char* fields,
|
|
|
|
gboolean color, gboolean threads)
|
2011-06-19 20:24:08 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
const char* myfields;
|
|
|
|
int nonempty;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
g_return_if_fail(fields);
|
2016-10-25 19:34:08 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
for (myfields = fields, nonempty = 0; *myfields; ++myfields) {
|
2022-03-20 13:12:41 +01:00
|
|
|
const auto field_opt{field_from_shortcut(*myfields)};
|
|
|
|
if (!field_opt || (!field_opt->is_value() && !field_opt->is_contact()))
|
2021-10-20 11:18:15 +02:00
|
|
|
nonempty += printf("%c", *myfields);
|
2011-06-19 20:24:08 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
else {
|
2022-03-20 13:12:41 +01:00
|
|
|
ansi_color_maybe(field_opt->id, color);
|
|
|
|
nonempty += mu_util_fputs_encoded(
|
|
|
|
display_field(msg, field_opt->id).c_str(), stdout);
|
|
|
|
ansi_reset_maybe(field_opt->id, color);
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
|
|
|
}
|
2011-06-19 20:24:08 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (nonempty)
|
|
|
|
fputs("\n", stdout);
|
2011-06-19 20:24:08 +02:00
|
|
|
}
|
|
|
|
|
2011-06-02 10:18:47 +02:00
|
|
|
static gboolean
|
2022-04-22 07:02:12 +02:00
|
|
|
output_plain(const Option<Message>& msg, const OutputInfo& info,
|
|
|
|
const MuConfig* opts, GError** err)
|
2011-06-02 10:18:47 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!msg)
|
|
|
|
return true;
|
2020-11-28 09:16:43 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
/* we reuse the color (whatever that may be)
|
|
|
|
* for message-priority for threads, too */
|
2022-03-04 23:36:37 +01:00
|
|
|
ansi_color_maybe(Field::Id::Priority, !opts->nocolor);
|
2021-10-20 11:18:15 +02:00
|
|
|
if (opts->threads && info.match_info)
|
|
|
|
thread_indent(*info.match_info, opts);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2022-04-22 07:02:12 +02:00
|
|
|
output_plain_fields(*msg, opts->fields, !opts->nocolor, opts->threads);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (opts->summary_len > 0)
|
2022-04-22 07:02:12 +02:00
|
|
|
print_summary(*msg, opts);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
return TRUE;
|
2012-07-12 14:53:36 +02:00
|
|
|
}
|
2011-08-03 22:06:18 +02:00
|
|
|
|
2020-11-28 09:16:43 +01:00
|
|
|
static bool
|
2022-04-22 07:02:12 +02:00
|
|
|
output_sexp(const Option<Message>& msg, const OutputInfo& info, const MuConfig* opts, GError** err)
|
2012-07-12 14:53:36 +02:00
|
|
|
{
|
2022-04-28 21:47:00 +02:00
|
|
|
if (msg) {
|
2022-05-05 00:27:08 +02:00
|
|
|
|
|
|
|
if (const auto sexp{msg->cached_sexp()}; !sexp.empty())
|
|
|
|
fputs(sexp.c_str(), stdout);
|
|
|
|
else
|
|
|
|
fputs(msg->to_sexp().to_sexp_string().c_str(), stdout);
|
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
fputs("\n", stdout);
|
|
|
|
}
|
2021-01-27 17:58:00 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
return true;
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2020-11-28 09:16:43 +01:00
|
|
|
static bool
|
2022-04-22 07:02:12 +02:00
|
|
|
output_json(const Option<Message>& msg, const OutputInfo& info, const MuConfig* opts, GError** err)
|
2018-11-11 11:16:49 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
if (info.header) {
|
|
|
|
g_print("[\n");
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-11 11:16:49 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (info.footer) {
|
|
|
|
g_print("]\n");
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-11 11:16:49 +01:00
|
|
|
|
2022-06-29 06:48:01 +02:00
|
|
|
if (!msg)
|
|
|
|
return true;
|
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
g_print("%s%s\n",
|
2022-04-22 07:02:12 +02:00
|
|
|
msg->to_sexp().to_json_string().c_str(),
|
2022-02-22 21:48:29 +01:00
|
|
|
info.last ? "" : ",");
|
2018-11-11 11:16:49 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
return true;
|
2018-11-11 11:16:49 +01:00
|
|
|
}
|
|
|
|
|
2011-06-02 10:18:47 +02:00
|
|
|
static void
|
2022-04-22 07:02:12 +02:00
|
|
|
print_attr_xml(const std::string& elm, const std::string& str)
|
2011-06-02 10:18:47 +02:00
|
|
|
{
|
2022-04-22 07:02:12 +02:00
|
|
|
if (str.empty())
|
2021-10-20 11:18:15 +02:00
|
|
|
return; /* empty: don't include */
|
2011-06-02 10:18:47 +02:00
|
|
|
|
2022-04-22 07:02:12 +02:00
|
|
|
auto&& esc{to_string_opt_gchar(g_markup_escape_text(str.c_str(), -1))};
|
|
|
|
g_print("\t\t<%s>%s</%s>\n", elm.c_str(), esc.value_or("").c_str(), elm.c_str());
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2020-11-28 09:16:43 +01:00
|
|
|
static bool
|
2022-04-22 07:02:12 +02:00
|
|
|
output_xml(const Option<Message>& msg, const OutputInfo& info, const MuConfig* opts, GError** err)
|
2011-08-10 22:56:38 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
if (info.header) {
|
|
|
|
g_print("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
|
|
|
|
g_print("<messages>\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.footer) {
|
|
|
|
g_print("</messages>\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_print("\t<message>\n");
|
2022-04-22 07:02:12 +02:00
|
|
|
print_attr_xml("from", to_string(msg->from()));
|
|
|
|
print_attr_xml("to", to_string(msg->to()));
|
|
|
|
print_attr_xml("cc", to_string(msg->cc()));
|
|
|
|
print_attr_xml("subject", msg->subject());
|
|
|
|
g_print("\t\t<date>%u</date>\n", (unsigned)msg->date());
|
|
|
|
g_print("\t\t<size>%u</size>\n", (unsigned)msg->size());
|
|
|
|
print_attr_xml("msgid", msg->message_id());
|
|
|
|
print_attr_xml("path", msg->path());
|
|
|
|
print_attr_xml("maildir", msg->maildir());
|
2021-10-20 11:18:15 +02:00
|
|
|
g_print("\t</message>\n");
|
|
|
|
|
|
|
|
return true;
|
2011-08-10 22:56:38 +02:00
|
|
|
}
|
|
|
|
|
2020-11-28 09:16:43 +01:00
|
|
|
static OutputFunc
|
2021-10-20 11:18:15 +02:00
|
|
|
get_output_func(const MuConfig* opts, GError** err)
|
2011-06-02 10:18:47 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
switch (opts->format) {
|
|
|
|
case MU_CONFIG_FORMAT_LINKS: return output_link;
|
|
|
|
case MU_CONFIG_FORMAT_EXEC: return exec_cmd;
|
|
|
|
case MU_CONFIG_FORMAT_PLAIN: return output_plain;
|
|
|
|
case MU_CONFIG_FORMAT_XML: return output_xml;
|
|
|
|
case MU_CONFIG_FORMAT_SEXP: return output_sexp;
|
|
|
|
case MU_CONFIG_FORMAT_JSON: return output_json;
|
|
|
|
|
|
|
|
default: g_return_val_if_reached(NULL); return NULL;
|
|
|
|
}
|
2012-07-20 10:54:37 +02:00
|
|
|
}
|
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
static Result<void>
|
|
|
|
output_query_results(const QueryResults& qres, const MuConfig* opts)
|
2012-07-20 10:54:37 +02:00
|
|
|
{
|
2022-04-28 21:47:00 +02:00
|
|
|
GError* err{};
|
|
|
|
const auto output_func{get_output_func(opts, &err)};
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!output_func)
|
2022-04-28 21:47:00 +02:00
|
|
|
return Err(Error::Code::Query, &err, "failed to find output function");
|
2020-11-28 09:16:43 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
gboolean rv{true};
|
2022-04-22 07:02:12 +02:00
|
|
|
output_func(Nothing, FirstOutput, opts, {});
|
2020-11-28 09:16:43 +01:00
|
|
|
|
2021-09-30 14:18:50 +02:00
|
|
|
size_t n{0};
|
2021-10-20 11:18:15 +02:00
|
|
|
for (auto&& item : qres) {
|
2021-09-30 14:18:50 +02:00
|
|
|
n++;
|
2022-04-22 07:02:12 +02:00
|
|
|
auto msg{item.message()};
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!msg)
|
|
|
|
continue;
|
|
|
|
|
2022-05-05 00:27:08 +02:00
|
|
|
if (opts->after != 0 && msg->changed() < opts->after)
|
2021-10-20 11:18:15 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
rv = output_func(msg,
|
2022-02-22 21:48:29 +01:00
|
|
|
{item.doc_id(),
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
n == qres.size(), /* last? */
|
|
|
|
item.query_match()},
|
|
|
|
opts,
|
2022-04-28 21:47:00 +02:00
|
|
|
&err);
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!rv)
|
|
|
|
break;
|
|
|
|
}
|
2022-04-22 07:02:12 +02:00
|
|
|
output_func(Nothing, LastOutput, opts, {});
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
|
|
|
|
if (rv)
|
|
|
|
return Ok();
|
|
|
|
else
|
|
|
|
return Err(Error::Code::Query, &err, "error in query results output");
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
static Result<void>
|
|
|
|
process_query(const Store& store, const std::string& expr, const MuConfig* opts)
|
2012-07-12 17:09:42 +02:00
|
|
|
{
|
2022-04-28 21:47:00 +02:00
|
|
|
auto qres{run_query(store, expr, opts)};
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!qres)
|
2022-04-28 21:47:00 +02:00
|
|
|
return Err(qres.error());
|
2012-07-12 17:09:42 +02:00
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
if (qres->empty())
|
|
|
|
return Err(Error::Code::NoMatches, "no matches for search expression");
|
2012-07-12 17:09:42 +02:00
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
return output_query_results(*qres, opts);
|
2012-07-12 17:09:42 +02:00
|
|
|
}
|
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
static Result<void>
|
|
|
|
execute_find(const Store& store, const MuConfig* opts)
|
2010-01-31 11:12:04 +01:00
|
|
|
{
|
2022-04-28 21:47:00 +02:00
|
|
|
auto expr{get_query(opts)};
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!expr)
|
2022-04-28 21:47:00 +02:00
|
|
|
return Err(expr.error());
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
if (opts->format == MU_CONFIG_FORMAT_XQUERY)
|
2022-04-28 21:47:00 +02:00
|
|
|
return print_internal(store, *expr, TRUE, FALSE);
|
2021-10-20 11:18:15 +02:00
|
|
|
else if (opts->format == MU_CONFIG_FORMAT_MQUERY)
|
2022-04-28 21:47:00 +02:00
|
|
|
return print_internal(store, *expr, FALSE, opts->verbose);
|
2021-10-20 11:18:15 +02:00
|
|
|
else
|
2022-04-28 21:47:00 +02:00
|
|
|
return process_query(store, *expr, opts);
|
2011-09-03 09:43:08 +02:00
|
|
|
}
|
|
|
|
|
2012-07-12 17:09:42 +02:00
|
|
|
static gboolean
|
2021-10-20 11:18:15 +02:00
|
|
|
format_params_valid(const MuConfig* opts, GError** err)
|
2012-07-12 17:09:42 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
switch (opts->format) {
|
|
|
|
case MU_CONFIG_FORMAT_EXEC: break;
|
|
|
|
case MU_CONFIG_FORMAT_PLAIN:
|
|
|
|
case MU_CONFIG_FORMAT_SEXP:
|
|
|
|
case MU_CONFIG_FORMAT_JSON:
|
|
|
|
case MU_CONFIG_FORMAT_LINKS:
|
|
|
|
case MU_CONFIG_FORMAT_XML:
|
|
|
|
case MU_CONFIG_FORMAT_XQUERY:
|
|
|
|
case MU_CONFIG_FORMAT_MQUERY:
|
|
|
|
if (opts->exec) {
|
|
|
|
mu_util_g_set_error(err,
|
2022-02-22 21:48:29 +01:00
|
|
|
MU_ERROR_IN_PARAMETERS,
|
|
|
|
"--exec and --format cannot be combined");
|
2021-10-20 11:18:15 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
mu_util_g_set_error(err,
|
2022-02-22 21:48:29 +01:00
|
|
|
MU_ERROR_IN_PARAMETERS,
|
|
|
|
"invalid output format %s",
|
|
|
|
opts->formatstr ? opts->formatstr : "<none>");
|
2021-10-20 11:18:15 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts->format == MU_CONFIG_FORMAT_LINKS && !opts->linksdir) {
|
|
|
|
mu_util_g_set_error(err, MU_ERROR_IN_PARAMETERS, "missing --linksdir argument");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts->linksdir && opts->format != MU_CONFIG_FORMAT_LINKS) {
|
|
|
|
mu_util_g_set_error(err,
|
2022-02-22 21:48:29 +01:00
|
|
|
MU_ERROR_IN_PARAMETERS,
|
|
|
|
"--linksdir is only valid with --format=links");
|
2021-10-20 11:18:15 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2012-07-12 17:09:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2021-10-20 11:18:15 +02:00
|
|
|
query_params_valid(const MuConfig* opts, GError** err)
|
2012-07-12 17:09:42 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
const gchar* xpath;
|
|
|
|
|
|
|
|
if (!opts->params[1]) {
|
|
|
|
mu_util_g_set_error(err, MU_ERROR_IN_PARAMETERS, "missing query");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
xpath = mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB);
|
|
|
|
if (mu_util_check_dir(xpath, TRUE, FALSE))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
mu_util_g_set_error(err,
|
2022-02-22 21:48:29 +01:00
|
|
|
MU_ERROR_FILE_CANNOT_READ,
|
|
|
|
"'%s' is not a readable Xapian directory",
|
|
|
|
xpath);
|
2021-10-20 11:18:15 +02:00
|
|
|
return FALSE;
|
2012-07-12 17:09:42 +02:00
|
|
|
}
|
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
Result<void>
|
|
|
|
Mu::mu_cmd_find(const Store& store, const MuConfig* opts)
|
2011-09-03 09:43:08 +02:00
|
|
|
{
|
2022-04-28 21:47:00 +02:00
|
|
|
g_return_val_if_fail(opts, Err(Error::Code::Internal, "no opts"));
|
|
|
|
g_return_val_if_fail(opts->cmd == MU_CONFIG_CMD_FIND, Err(Error::Code::Internal,
|
|
|
|
"wrong command"));
|
2021-10-20 11:18:15 +02:00
|
|
|
MuConfig myopts{*opts};
|
2020-06-08 22:04:05 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (myopts.exec)
|
|
|
|
myopts.format = MU_CONFIG_FORMAT_EXEC; /* pseudo format */
|
2012-07-12 14:53:36 +02:00
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
GError *err{};
|
|
|
|
if (!query_params_valid(&myopts, &err) || !format_params_valid(&myopts, &err))
|
|
|
|
return Err(Error::Code::InvalidArgument, &err, "invalid argument");
|
2021-10-20 11:18:15 +02:00
|
|
|
else
|
2022-04-28 21:47:00 +02:00
|
|
|
return execute_find(store, &myopts);
|
2010-01-31 11:12:04 +01:00
|
|
|
}
|