2010-01-31 11:12:04 +01:00
|
|
|
/*
|
2021-09-30 14:18:50 +02:00
|
|
|
** Copyright (C) 2008-2021 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-03-20 13:12:41 +01:00
|
|
|
#include "message/mu-fields.hh"
|
2020-11-28 09:16:43 +01:00
|
|
|
#include "mu-msg.hh"
|
|
|
|
#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
|
|
|
|
2019-12-16 20:44:35 +01:00
|
|
|
#include "utils/mu-util.h"
|
|
|
|
#include "utils/mu-str.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
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
using OutputFunc = std::function<bool(MuMsg*, const OutputInfo&, const MuConfig*, GError**)>;
|
2010-01-31 11:12:04 +01:00
|
|
|
|
|
|
|
static gboolean
|
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,
|
|
|
|
gboolean warn,
|
|
|
|
GError** err)
|
2010-01-31 11:12:04 +01:00
|
|
|
{
|
2022-01-30 13:33:25 +01:00
|
|
|
std::cout << store.parse_query(expr, xapian) << "\n";
|
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
return TRUE;
|
2010-01-31 11:12:04 +01:00
|
|
|
}
|
|
|
|
|
2020-11-28 09:16:43 +01:00
|
|
|
static Option<QueryResults>
|
2022-03-04 23:36:37 +01:00
|
|
|
run_query(const Store& store, const std::string& expr, const MuConfig* opts,
|
|
|
|
GError** err)
|
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 : "")};
|
|
|
|
if (!sortfield && opts->sortfield) {
|
2022-03-04 23:36:37 +01:00
|
|
|
g_set_error(err, MU_ERROR_DOMAIN, MU_ERROR_IN_PARAMETERS,
|
|
|
|
"invvalid sort field: '%s'\n", opts->sortfield);
|
|
|
|
return Nothing;
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Mu::QueryFlags qflags{QueryFlags::None};
|
|
|
|
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-03-20 13:12:41 +01:00
|
|
|
return store.run_query(expr, sortfield->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
|
2021-10-20 11:18:15 +02:00
|
|
|
exec_cmd(MuMsg* msg, const OutputInfo& info, const MuConfig* opts, GError** err)
|
2011-07-06 22:35:52 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
gint status;
|
|
|
|
char * cmdline, *escpath;
|
|
|
|
gboolean rv;
|
2011-08-30 20:57:59 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
escpath = g_shell_quote(mu_msg_get_path(msg));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-28 09:16:43 +01:00
|
|
|
static Option<std::string>
|
2021-10-20 11:18:15 +02:00
|
|
|
get_query(const MuConfig* opts, GError** err)
|
2010-11-13 13:16:38 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
gchar *query, *bookmarkval;
|
|
|
|
|
|
|
|
/* params[0] is 'find', actual search params start with [1] */
|
|
|
|
if (!opts->bookmark && !opts->params[1]) {
|
2022-03-04 23:36:37 +01:00
|
|
|
g_set_error(err, MU_ERROR_DOMAIN, MU_ERROR_IN_PARAMETERS,
|
|
|
|
"error in parameters");
|
2021-10-20 11:18:15 +02:00
|
|
|
return Nothing;
|
|
|
|
}
|
|
|
|
|
|
|
|
bookmarkval = NULL;
|
|
|
|
if (opts->bookmark) {
|
|
|
|
bookmarkval = resolve_bookmark(opts, err);
|
|
|
|
if (!bookmarkval)
|
|
|
|
return Nothing;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
std::string q{query};
|
|
|
|
g_free(query);
|
|
|
|
|
|
|
|
return q;
|
2010-11-13 13:16:38 +01:00
|
|
|
}
|
|
|
|
|
2011-06-02 10:18:47 +02:00
|
|
|
static gboolean
|
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-02-16 22:03:48 +01:00
|
|
|
if (auto&& res = mu_maildir_mkdir(opts->linksdir, 0700, true); !res) {
|
|
|
|
res.error().fill_g_error(err);
|
2021-10-20 11:18:15 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2022-02-16 22:03:48 +01:00
|
|
|
if (!opts->clearlinks)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (auto&& res = mu_maildir_clear_links(opts->linksdir); !res) {
|
|
|
|
res.error().fill_g_error(err);
|
2021-10-20 11:18:15 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2020-11-28 09:16:43 +01:00
|
|
|
static bool
|
2021-10-20 11:18:15 +02:00
|
|
|
output_link(MuMsg* 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-02-16 22:03:48 +01:00
|
|
|
if (auto&& res = mu_maildir_link(
|
|
|
|
mu_msg_get_path(msg), opts->linksdir); !res) {
|
|
|
|
res.error().fill_g_error(err);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-04-18 19:30:47 +02:00
|
|
|
static const char*
|
2022-03-04 23:36:37 +01:00
|
|
|
field_string_list(MuMsg* msg, Field::Id field_id)
|
2013-04-18 19:30:47 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
char* str;
|
|
|
|
const GSList* lst;
|
|
|
|
static char buf[80];
|
|
|
|
|
2022-03-04 23:36:37 +01:00
|
|
|
lst = mu_msg_get_field_string_list(msg, field_id);
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!lst)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
str = mu_str_from_list(lst, ',');
|
|
|
|
if (str) {
|
|
|
|
strncpy(buf, str, sizeof(buf) - 1);
|
|
|
|
buf[sizeof(buf) - 1] = '\0';
|
|
|
|
g_free(str);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2013-04-18 19:30:47 +02:00
|
|
|
}
|
|
|
|
|
2022-02-16 22:03:48 +01:00
|
|
|
/* ugly... for backward compat */
|
|
|
|
static const char*
|
2022-03-20 13:12:41 +01:00
|
|
|
flags_s(Flags flags)
|
2022-02-16 22:03:48 +01:00
|
|
|
{
|
|
|
|
static char buf[64];
|
2022-04-10 11:30:59 +02:00
|
|
|
const auto flagstr{to_string(flags)};
|
2022-02-16 22:03:48 +01:00
|
|
|
|
|
|
|
::strncpy(buf, flagstr.c_str(), sizeof(buf) - 1);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2022-02-22 21:48:29 +01:00
|
|
|
static std::string
|
2022-03-04 23:36:37 +01:00
|
|
|
display_field(MuMsg* msg, Field::Id field_id)
|
2011-06-02 10:18:47 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
gint64 val;
|
|
|
|
|
2022-03-20 13:12:41 +01:00
|
|
|
switch (field_from_id(field_id).type) {
|
2022-03-04 23:36:37 +01:00
|
|
|
case Field::Type::String: {
|
2021-10-20 11:18:15 +02:00
|
|
|
const gchar* str;
|
2022-03-04 23:36:37 +01:00
|
|
|
str = mu_msg_get_field_string(msg, field_id);
|
2021-10-20 11:18:15 +02:00
|
|
|
return str ? str : "";
|
|
|
|
}
|
2022-03-04 23:36:37 +01:00
|
|
|
case Field::Type::Integer:
|
|
|
|
if (field_id == Field::Id::Priority) {
|
|
|
|
const auto val = static_cast<char>(mu_msg_get_field_numeric(msg, field_id));
|
2022-03-20 13:12:41 +01:00
|
|
|
const auto prio = priority_from_char(val);
|
|
|
|
return priority_name_c_str(prio);
|
2022-03-04 23:36:37 +01:00
|
|
|
} else if (field_id == Field::Id::Flags) {
|
|
|
|
val = mu_msg_get_field_numeric(msg, field_id);
|
2022-03-20 13:12:41 +01:00
|
|
|
return flags_s(static_cast<Flags>(val));
|
2021-10-20 11:18:15 +02:00
|
|
|
} else /* as string */
|
2022-03-04 23:36:37 +01:00
|
|
|
return mu_msg_get_field_string(msg, field_id);
|
2021-10-20 11:18:15 +02:00
|
|
|
|
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-03-04 23:36:37 +01:00
|
|
|
"%c", static_cast<::time_t>(mu_msg_get_field_numeric(msg, field_id)));
|
|
|
|
case Field::Type::ByteSize:
|
|
|
|
val = mu_msg_get_field_numeric(msg, field_id);
|
2021-10-20 11:18:15 +02:00
|
|
|
return mu_str_size_s((unsigned)val);
|
2022-03-04 23:36:37 +01:00
|
|
|
case Field::Type::StringList: {
|
2021-10-20 11:18:15 +02:00
|
|
|
const char* str;
|
2022-03-04 23:36:37 +01:00
|
|
|
str = field_string_list(msg, field_id);
|
2021-10-20 11:18:15 +02:00
|
|
|
return str ? str : "";
|
|
|
|
}
|
|
|
|
default: g_return_val_if_reached(NULL);
|
|
|
|
}
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-10-20 11:18:15 +02:00
|
|
|
print_summary(MuMsg* msg, const MuConfig* opts)
|
2011-06-02 10:18:47 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
const char* body;
|
|
|
|
char* summ;
|
|
|
|
MuMsgOptions msgopts;
|
2012-05-04 08:40:38 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
msgopts = mu_config_get_msg_options(opts);
|
|
|
|
body = mu_msg_get_body_text(msg, msgopts);
|
2012-05-04 08:40:38 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (body)
|
|
|
|
summ = mu_str_summarize(body, (unsigned)opts->summary_len);
|
|
|
|
else
|
|
|
|
summ = NULL;
|
2012-05-04 08:40:38 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
g_print("Summary: ");
|
|
|
|
mu_util_fputs_encoded(summ ? summ : "<none>", stdout);
|
|
|
|
g_print("\n");
|
2012-05-04 08:40:38 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
g_free(summ);
|
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
|
2021-10-20 11:18:15 +02:00
|
|
|
output_plain_fields(MuMsg* 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
|
2021-10-20 11:18:15 +02:00
|
|
|
output_plain(MuMsg* 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
|
|
|
|
2021-10-20 11:18:15 +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)
|
|
|
|
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
|
|
|
|
2021-01-27 17:58:00 +01:00
|
|
|
G_GNUC_UNUSED static std::string
|
2021-10-20 11:18:15 +02:00
|
|
|
to_string(const Mu::Sexp& sexp, bool color, size_t level = 0)
|
2020-07-12 14:10:35 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
Mu::MaybeAnsi col{color};
|
|
|
|
using Color = Mu::MaybeAnsi::Color;
|
|
|
|
|
|
|
|
// clang/libc++ don't allow constexpr here
|
|
|
|
const std::array<Color, 6> rainbow = {
|
|
|
|
Color::BrightBlue,
|
|
|
|
Color::Green,
|
|
|
|
Color::Yellow,
|
|
|
|
Color::Magenta,
|
|
|
|
Color::Cyan,
|
|
|
|
Color::BrightGreen,
|
|
|
|
};
|
|
|
|
|
|
|
|
std::stringstream sstrm;
|
|
|
|
|
|
|
|
switch (sexp.type()) {
|
|
|
|
case Sexp::Type::List: {
|
|
|
|
const auto bracecol{col.fg(rainbow[level % rainbow.size()])};
|
|
|
|
sstrm << bracecol << "(";
|
|
|
|
|
|
|
|
bool first{true};
|
|
|
|
for (auto&& child : sexp.list()) {
|
|
|
|
sstrm << (first ? "" : " ") << to_string(child, color, level + 1);
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
sstrm << bracecol << ")";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Sexp::Type::String:
|
|
|
|
sstrm << col.fg(Color::BrightCyan) << Mu::quote(sexp.value()) << col.reset();
|
|
|
|
break;
|
|
|
|
case Sexp::Type::Number:
|
|
|
|
sstrm << col.fg(Color::BrightMagenta) << sexp.value() << col.reset();
|
|
|
|
break;
|
|
|
|
case Sexp::Type::Symbol:
|
|
|
|
sstrm << (col.fg(sexp.value().at(0) == ':' ? Color::BrightGreen
|
2022-02-22 21:48:29 +01:00
|
|
|
: Color::BrightBlue))
|
2021-10-20 11:18:15 +02:00
|
|
|
<< sexp.value() << col.reset();
|
|
|
|
break;
|
|
|
|
default: throw std::logic_error("invalid type");
|
|
|
|
}
|
|
|
|
|
|
|
|
return sstrm.str();
|
2020-07-12 14:10:35 +02:00
|
|
|
}
|
|
|
|
|
2020-11-28 09:16:43 +01:00
|
|
|
static bool
|
2021-10-20 11:18:15 +02:00
|
|
|
output_sexp(MuMsg* msg, const OutputInfo& info, const MuConfig* opts, GError** err)
|
2012-07-12 14:53:36 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!msg)
|
|
|
|
return true;
|
2021-01-27 17:58:00 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
fputs(msg_to_sexp(msg, 0, MU_MSG_OPTION_HEADERS_ONLY).to_sexp_string().c_str(), stdout);
|
|
|
|
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
|
2021-10-20 11:18:15 +02:00
|
|
|
output_json(MuMsg* 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
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
g_print("%s%s\n",
|
2022-02-22 21:48:29 +01:00
|
|
|
msg_to_sexp(msg, info.docid, MU_MSG_OPTION_HEADERS_ONLY).to_json_string().c_str(),
|
|
|
|
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
|
2021-10-20 11:18:15 +02:00
|
|
|
print_attr_xml(const char* elm, const char* str)
|
2011-06-02 10:18:47 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
gchar* esc;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (mu_str_is_empty(str))
|
|
|
|
return; /* empty: don't include */
|
2011-06-02 10:18:47 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
esc = g_markup_escape_text(str, -1);
|
|
|
|
g_print("\t\t<%s>%s</%s>\n", elm, esc, elm);
|
|
|
|
g_free(esc);
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2020-11-28 09:16:43 +01:00
|
|
|
static bool
|
2021-10-20 11:18:15 +02:00
|
|
|
output_xml(MuMsg* 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");
|
|
|
|
print_attr_xml("from", mu_msg_get_from(msg));
|
|
|
|
print_attr_xml("to", mu_msg_get_to(msg));
|
|
|
|
print_attr_xml("cc", mu_msg_get_cc(msg));
|
|
|
|
print_attr_xml("subject", mu_msg_get_subject(msg));
|
|
|
|
g_print("\t\t<date>%u</date>\n", (unsigned)mu_msg_get_date(msg));
|
|
|
|
g_print("\t\t<size>%u</size>\n", (unsigned)mu_msg_get_size(msg));
|
|
|
|
print_attr_xml("msgid", mu_msg_get_msgid(msg));
|
|
|
|
print_attr_xml("path", mu_msg_get_path(msg));
|
|
|
|
print_attr_xml("maildir", mu_msg_get_maildir(msg));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-28 09:16:43 +01:00
|
|
|
static bool
|
2021-10-20 11:18:15 +02:00
|
|
|
output_query_results(const QueryResults& qres, const MuConfig* opts, GError** err)
|
2012-07-20 10:54:37 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
const auto output_func{get_output_func(opts, err)};
|
|
|
|
if (!output_func)
|
|
|
|
return false;
|
2020-11-28 09:16:43 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
gboolean rv{true};
|
|
|
|
output_func(NULL, FirstOutput, opts, NULL);
|
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++;
|
2021-10-20 11:18:15 +02:00
|
|
|
auto msg{item.floating_msg()};
|
|
|
|
if (!msg)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (opts->after != 0 && mu_msg_get_timestamp(msg) < opts->after)
|
|
|
|
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,
|
|
|
|
err);
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!rv)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
output_func(NULL, LastOutput, opts, NULL);
|
|
|
|
|
|
|
|
return rv;
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2012-07-12 17:09:42 +02:00
|
|
|
static gboolean
|
2022-01-30 13:33:25 +01:00
|
|
|
process_query(const Store& store, const std::string& expr, const MuConfig* opts, GError** err)
|
2012-07-12 17:09:42 +02:00
|
|
|
{
|
2022-01-30 13:33:25 +01:00
|
|
|
auto qres{run_query(store, expr, opts, err)};
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!qres)
|
|
|
|
return FALSE;
|
2012-07-12 17:09:42 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (qres->empty()) {
|
|
|
|
mu_util_g_set_error(err, MU_ERROR_NO_MATCHES, "no matches for search expression");
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-12 17:09:42 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
return output_query_results(*qres, opts, err);
|
2012-07-12 17:09:42 +02:00
|
|
|
}
|
|
|
|
|
2011-09-03 09:43:08 +02:00
|
|
|
static gboolean
|
2021-10-20 11:18:15 +02:00
|
|
|
execute_find(const Store& store, const MuConfig* opts, GError** err)
|
2010-01-31 11:12:04 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
auto expr{get_query(opts, err)};
|
|
|
|
if (!expr)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (opts->format == MU_CONFIG_FORMAT_XQUERY)
|
2022-01-30 13:33:25 +01:00
|
|
|
return print_internal(store, *expr, TRUE, FALSE, err);
|
2021-10-20 11:18:15 +02:00
|
|
|
else if (opts->format == MU_CONFIG_FORMAT_MQUERY)
|
2022-01-30 13:33:25 +01:00
|
|
|
return print_internal(store, *expr, FALSE, opts->verbose, err);
|
2021-10-20 11:18:15 +02:00
|
|
|
else
|
2022-01-30 13:33:25 +01:00
|
|
|
return process_query(store, *expr, opts, err);
|
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
|
|
|
}
|
|
|
|
|
2011-09-03 09:43:08 +02:00
|
|
|
MuError
|
2021-10-20 11:18:15 +02:00
|
|
|
Mu::mu_cmd_find(const Store& store, const MuConfig* opts, GError** err)
|
2011-09-03 09:43:08 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
g_return_val_if_fail(opts, MU_ERROR_INTERNAL);
|
|
|
|
g_return_val_if_fail(opts->cmd == MU_CONFIG_CMD_FIND, MU_ERROR_INTERNAL);
|
2011-09-03 09:43:08 +02:00
|
|
|
|
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
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!query_params_valid(&myopts, err) || !format_params_valid(&myopts, err))
|
|
|
|
return MU_G_ERROR_CODE(err);
|
2011-09-03 09:43:08 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!execute_find(store, &myopts, err))
|
|
|
|
return MU_G_ERROR_CODE(err);
|
|
|
|
else
|
|
|
|
return MU_OK;
|
2010-01-31 11:12:04 +01:00
|
|
|
}
|