2022-11-16 21:51:15 +01:00
|
|
|
/*
|
2024-04-21 21:08:10 +02:00
|
|
|
** Copyright (C) 2008-2024 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>
|
2024-04-21 21:08:10 +02:00
|
|
|
#include <sys/wait.h>
|
2010-01-31 11:12:04 +01:00
|
|
|
|
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"
|
2023-09-11 22:52:38 +02:00
|
|
|
#include "mu-query-macros.hh"
|
2023-09-09 10:49:42 +02:00
|
|
|
#include "mu-query-parser.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
|
|
|
|
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;
|
|
|
|
|
2022-11-16 21:51:15 +01:00
|
|
|
using Format = Options::Find::Format;
|
|
|
|
|
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
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
using OutputFunc = std::function<Result<void>(const Option<Message>& msg, const OutputInfo&,
|
|
|
|
const Options&)>;
|
2022-11-16 21:51:15 +01:00
|
|
|
|
|
|
|
using Format = Options::Find::Format;
|
2010-01-31 11:12:04 +01:00
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
static Result<void>
|
2023-09-09 10:49:42 +02:00
|
|
|
analyze_query_expr(const Store& store, const std::string& expr, const Options& opts)
|
2010-01-31 11:12:04 +01:00
|
|
|
{
|
2023-09-09 10:49:42 +02:00
|
|
|
auto print_item=[&](auto&&title, auto&&val) {
|
|
|
|
const auto blue{opts.nocolor ? "" : MU_COLOR_BLUE};
|
|
|
|
const auto green{opts.nocolor ? "" : MU_COLOR_GREEN};
|
|
|
|
const auto reset{opts.nocolor ? "" : MU_COLOR_DEFAULT};
|
|
|
|
mu_println("* {}{}{}:\n {}{}{}", blue, title, reset, green, val, reset);
|
|
|
|
};
|
|
|
|
|
|
|
|
print_item("query", expr);
|
|
|
|
|
|
|
|
const auto pq{parse_query(expr, false/*don't expand*/).to_string()};
|
|
|
|
const auto pqx{parse_query(expr, true/*do expand*/).to_string()};
|
|
|
|
|
|
|
|
print_item("parsed query", pq);
|
|
|
|
if (pq != pqx)
|
|
|
|
print_item("parsed query (expanded)", pqx);
|
|
|
|
|
|
|
|
auto xq{make_xapian_query(store, expr)};
|
|
|
|
if (!xq)
|
|
|
|
return Err(std::move(xq.error()));
|
|
|
|
|
|
|
|
print_item("Xapian query", xq->get_description());
|
|
|
|
|
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>
|
2022-11-16 21:51:15 +01:00
|
|
|
run_query(const Store& store, const std::string& expr, const Options& opts)
|
2010-01-31 19:36:56 +01:00
|
|
|
{
|
2022-07-19 20:54:26 +02:00
|
|
|
Mu::QueryFlags qflags{QueryFlags::SkipUnreadable};
|
2022-11-16 21:51:15 +01:00
|
|
|
if (opts.find.reverse)
|
2021-10-20 11:18:15 +02:00
|
|
|
qflags |= QueryFlags::Descending;
|
2022-11-16 21:51:15 +01:00
|
|
|
if (opts.find.skip_dups)
|
2021-10-20 11:18:15 +02:00
|
|
|
qflags |= QueryFlags::SkipDuplicates;
|
2022-11-16 21:51:15 +01:00
|
|
|
if (opts.find.include_related)
|
2021-10-20 11:18:15 +02:00
|
|
|
qflags |= QueryFlags::IncludeRelated;
|
2022-11-16 21:51:15 +01:00
|
|
|
if (opts.find.threads)
|
2021-10-20 11:18:15 +02:00
|
|
|
qflags |= QueryFlags::Threading;
|
|
|
|
|
2022-11-16 21:51:15 +01:00
|
|
|
return store.run_query(expr,
|
|
|
|
opts.find.sortfield,
|
|
|
|
qflags, opts.find.maxnum.value_or(0));
|
2011-07-06 01:11:55 +02:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
static Result<void>
|
|
|
|
exec_cmd(const Option<Message>& msg, const OutputInfo& info, const Options& opts)
|
2011-07-06 22:35:52 +02:00
|
|
|
{
|
2022-04-22 07:02:12 +02:00
|
|
|
if (!msg)
|
2023-07-25 20:23:30 +02:00
|
|
|
return Ok();
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
int wait_status{};
|
|
|
|
GError *err{};
|
|
|
|
auto cmdline{mu_format("{} {}", opts.find.exec,
|
|
|
|
to_string_gchar(g_shell_quote(msg->path().c_str())))};
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
if (!g_spawn_command_line_sync(cmdline.c_str(), {}, {}, &wait_status, &err))
|
|
|
|
return Err(Error::Code::File, &err/*consumed*/,
|
|
|
|
"failed to execute shell command");
|
|
|
|
else if (WEXITSTATUS(wait_status) != 0)
|
|
|
|
return Err(Error::Code::File,
|
|
|
|
"shell command exited with exit-code {}",
|
|
|
|
WEXITSTATUS(wait_status));
|
|
|
|
return Ok();
|
2011-07-06 22:35:52 +02:00
|
|
|
}
|
|
|
|
|
2022-11-16 21:51:15 +01:00
|
|
|
static Result<std::string>
|
2023-09-11 22:52:38 +02:00
|
|
|
resolve_bookmark(const Store& store, const Options& opts)
|
2010-11-13 13:16:38 +01:00
|
|
|
{
|
2023-09-11 22:52:38 +02:00
|
|
|
QueryMacros macros{store.config()};
|
|
|
|
if (auto&& res{macros.load_bookmarks(opts.runtime_path(RuntimePath::Bookmarks))}; !res)
|
|
|
|
return Err(res.error());
|
|
|
|
else if (auto&& bm{macros.find_macro(opts.find.bookmark)}; !bm)
|
|
|
|
return Err(Error::Code::InvalidArgument, "bookmark '{}' not found",
|
|
|
|
opts.find.bookmark);
|
|
|
|
else
|
|
|
|
return Ok(std::move(*bm));
|
2010-11-13 13:16:38 +01:00
|
|
|
}
|
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
static Result<std::string>
|
2023-09-11 22:52:38 +02:00
|
|
|
get_query(const Store& store, const Options& opts)
|
2010-11-13 13:16:38 +01:00
|
|
|
{
|
2022-11-16 21:51:15 +01:00
|
|
|
if (opts.find.bookmark.empty() && opts.find.query.empty())
|
|
|
|
return Err(Error::Code::InvalidArgument,
|
|
|
|
"neither bookmark nor query");
|
|
|
|
|
|
|
|
std::string bookmark;
|
|
|
|
if (!opts.find.bookmark.empty()) {
|
2023-09-11 22:52:38 +02:00
|
|
|
const auto res = resolve_bookmark(store, opts);
|
2022-11-16 21:51:15 +01:00
|
|
|
if (!res)
|
|
|
|
return Err(std::move(res.error()));
|
|
|
|
bookmark = res.value() + " ";
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
|
|
|
|
2022-11-16 21:51:15 +01:00
|
|
|
auto&& query{join(opts.find.query, " ")};
|
|
|
|
return Ok(bookmark + query);
|
2010-11-13 13:16:38 +01:00
|
|
|
}
|
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
static Result<void>
|
|
|
|
prepare_links(const Options& opts)
|
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 */
|
2023-07-25 20:23:30 +02:00
|
|
|
if (auto&& res = maildir_mkdir(opts.find.linksdir, 0700, true); !res)
|
|
|
|
return Err(std::move(res.error()));
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-11-16 21:51:15 +01:00
|
|
|
if (!opts.find.clearlinks)
|
2023-07-25 20:23:30 +02:00
|
|
|
return Ok();
|
2022-02-16 22:03:48 +01:00
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
if (auto&& res = maildir_clear_links(opts.find.linksdir); !res)
|
|
|
|
return Err(std::move(res.error()));
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
return Ok();
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
static Result<void>
|
|
|
|
output_link(const Option<Message>& msg, const OutputInfo& info, const Options& opts)
|
2011-06-02 10:18:47 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
if (info.header)
|
2023-07-25 20:23:30 +02:00
|
|
|
return prepare_links(opts);
|
2021-10-20 11:18:15 +02:00
|
|
|
else if (info.footer)
|
2023-07-25 20:23:30 +02:00
|
|
|
return Ok();
|
2022-06-29 06:48:01 +02:00
|
|
|
|
2023-04-09 10:02:58 +02:00
|
|
|
/* during test, do not create "unique names" (i.e., names with path
|
|
|
|
* hashes), so we get a predictable result */
|
|
|
|
const auto unique_names{!g_getenv("MU_TEST")&&!g_test_initialized()};
|
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
if (auto&& res = maildir_link(msg->path(), opts.find.linksdir, unique_names); !res)
|
|
|
|
return Err(std::move(res.error()));
|
2022-06-29 06:48:01 +02:00
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
return Ok();
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-11-16 21:51:15 +01:00
|
|
|
ansi_color_maybe(Field::Id field_id, bool 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-11-16 21:51:15 +01:00
|
|
|
ansi_reset_maybe(Field::Id field_id, bool 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:
|
2023-08-05 09:53:11 +02:00
|
|
|
return mu_format("{:%c}",
|
|
|
|
mu_time(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-11-16 21:51:15 +01:00
|
|
|
print_summary(const Message& msg, const Options& 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
|
|
|
|
2023-01-14 16:11:36 +01:00
|
|
|
const auto summ{summarize(body->c_str(), opts.find.summary_len.value_or(0))};
|
2012-05-04 08:40:38 +02:00
|
|
|
|
2023-07-05 22:10:13 +02:00
|
|
|
mu_print("Summary: ");
|
2023-01-14 16:11:36 +01:00
|
|
|
fputs_encoded(summ, stdout);
|
2023-07-05 22:10:13 +02:00
|
|
|
mu_println("");
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2011-06-18 17:49:33 +02:00
|
|
|
static void
|
2022-11-16 21:51:15 +01:00
|
|
|
thread_indent(const QueryMatch& info, const Options& 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 */
|
2022-11-16 21:51:15 +01:00
|
|
|
if (opts.debug) {
|
2021-10-20 11:18:15 +02:00
|
|
|
::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-11-16 21:51:15 +01:00
|
|
|
output_plain_fields(const Message& msg, const std::string& fields,
|
|
|
|
bool color, bool threads)
|
2011-06-19 20:24:08 +02:00
|
|
|
{
|
2022-11-16 21:51:15 +01:00
|
|
|
size_t nonempty{};
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2022-11-16 21:51:15 +01:00
|
|
|
for (auto&& k: fields) {
|
|
|
|
const auto field_opt{field_from_shortcut(k)};
|
2022-03-20 13:12:41 +01:00
|
|
|
if (!field_opt || (!field_opt->is_value() && !field_opt->is_contact()))
|
2022-11-16 21:51:15 +01:00
|
|
|
nonempty += printf("%c", k);
|
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);
|
2023-01-14 16:11:36 +01:00
|
|
|
nonempty += fputs_encoded(
|
|
|
|
display_field(msg, field_opt->id), stdout);
|
2022-03-20 13:12:41 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
static Result<void>
|
2022-04-22 07:02:12 +02:00
|
|
|
output_plain(const Option<Message>& msg, const OutputInfo& info,
|
2023-07-25 20:23:30 +02:00
|
|
|
const Options& opts)
|
2011-06-02 10:18:47 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!msg)
|
2023-07-25 20:23:30 +02:00
|
|
|
return Ok();
|
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-11-16 21:51:15 +01:00
|
|
|
ansi_color_maybe(Field::Id::Priority, !opts.nocolor);
|
|
|
|
if (opts.find.threads && info.match_info)
|
2021-10-20 11:18:15 +02:00
|
|
|
thread_indent(*info.match_info, opts);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2022-11-16 21:51:15 +01:00
|
|
|
output_plain_fields(*msg, opts.find.fields, !opts.nocolor, opts.find.threads);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2022-11-16 21:51:15 +01:00
|
|
|
if (opts.view.summary_len)
|
2022-04-22 07:02:12 +02:00
|
|
|
print_summary(*msg, opts);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
return Ok();
|
2012-07-12 14:53:36 +02:00
|
|
|
}
|
2011-08-03 22:06:18 +02:00
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
static Result<void>
|
|
|
|
output_sexp(const Option<Message>& msg, const OutputInfo& info, const Options& opts)
|
2012-07-12 14:53:36 +02:00
|
|
|
{
|
2022-04-28 21:47:00 +02:00
|
|
|
if (msg) {
|
2022-11-07 17:36:33 +01:00
|
|
|
if (const auto sexp{msg->sexp()}; !sexp.empty())
|
|
|
|
fputs(sexp.to_string().c_str(), stdout);
|
2022-05-05 00:27:08 +02:00
|
|
|
else
|
2022-11-07 17:36:33 +01:00
|
|
|
fputs(msg->sexp().to_string().c_str(), stdout);
|
2022-04-28 21:47:00 +02:00
|
|
|
fputs("\n", stdout);
|
|
|
|
}
|
2021-01-27 17:58:00 +01:00
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
return Ok();
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
static Result<void>
|
|
|
|
output_json(const Option<Message>& msg, const OutputInfo& info, const Options& opts)
|
2018-11-11 11:16:49 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
if (info.header) {
|
2023-07-05 22:10:13 +02:00
|
|
|
mu_println("[");
|
2023-07-25 20:23:30 +02:00
|
|
|
return Ok();
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
2018-11-11 11:16:49 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (info.footer) {
|
2023-07-05 22:10:13 +02:00
|
|
|
mu_println("]");
|
2023-07-25 20:23:30 +02:00
|
|
|
return Ok();
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
2018-11-11 11:16:49 +01:00
|
|
|
|
2022-06-29 06:48:01 +02:00
|
|
|
if (!msg)
|
2023-07-25 20:23:30 +02:00
|
|
|
return Ok();
|
2022-06-29 06:48:01 +02:00
|
|
|
|
2023-07-05 22:10:13 +02:00
|
|
|
mu_println("{}{}", msg->sexp().to_json_string(), info.last ? "" : ",");
|
2018-11-11 11:16:49 +01:00
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
return Ok();
|
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))};
|
2023-07-05 22:10:13 +02:00
|
|
|
mu_println("\t\t<{}>{}</{}>", elm, esc.value_or(""), elm);
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
static Result<void>
|
|
|
|
output_xml(const Option<Message>& msg, const OutputInfo& info, const Options& opts)
|
2011-08-10 22:56:38 +02:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
if (info.header) {
|
2023-07-05 22:10:13 +02:00
|
|
|
mu_println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
|
|
|
|
mu_println("<messages>");
|
2023-07-25 20:23:30 +02:00
|
|
|
return Ok();
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (info.footer) {
|
2023-07-05 22:10:13 +02:00
|
|
|
mu_println("</messages>");
|
2023-07-25 20:23:30 +02:00
|
|
|
return Ok();
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
|
|
|
|
2023-07-05 22:10:13 +02:00
|
|
|
mu_println("\t<message>");
|
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());
|
2023-07-05 22:10:13 +02:00
|
|
|
mu_println("\t\t<date>{}</date>", (unsigned)msg->date());
|
|
|
|
mu_println("\t\t<size>{}</size>", (unsigned)msg->size());
|
2022-04-22 07:02:12 +02:00
|
|
|
print_attr_xml("msgid", msg->message_id());
|
|
|
|
print_attr_xml("path", msg->path());
|
|
|
|
print_attr_xml("maildir", msg->maildir());
|
2023-07-05 22:10:13 +02:00
|
|
|
mu_println("\t</message>");
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
return Ok();
|
2011-08-10 22:56:38 +02:00
|
|
|
}
|
|
|
|
|
2020-11-28 09:16:43 +01:00
|
|
|
static OutputFunc
|
2023-07-25 20:23:30 +02:00
|
|
|
get_output_func(const Options& opts)
|
2011-06-02 10:18:47 +02:00
|
|
|
{
|
2022-11-16 21:51:15 +01:00
|
|
|
if (!opts.find.exec.empty())
|
|
|
|
return exec_cmd;
|
|
|
|
|
|
|
|
switch (opts.find.format) {
|
2023-07-25 20:23:30 +02:00
|
|
|
case Format::Links:
|
|
|
|
return output_link;
|
|
|
|
case Format::Plain:
|
|
|
|
return output_plain;
|
|
|
|
case Format::Xml:
|
|
|
|
return output_xml;
|
|
|
|
case Format::Sexp:
|
|
|
|
return output_sexp;
|
|
|
|
case Format::Json:
|
|
|
|
return output_json;
|
|
|
|
default:
|
|
|
|
throw Error(Error::Code::Internal,
|
|
|
|
"invalid format {}",
|
|
|
|
static_cast<size_t>(opts.find.format));
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
2012-07-20 10:54:37 +02:00
|
|
|
}
|
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
static Result<void>
|
2022-11-16 21:51:15 +01:00
|
|
|
output_query_results(const QueryResults& qres, const Options& opts)
|
2012-07-20 10:54:37 +02:00
|
|
|
{
|
2022-04-28 21:47:00 +02:00
|
|
|
GError* err{};
|
2023-07-25 20:23:30 +02:00
|
|
|
const auto output_func{get_output_func(opts)};
|
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
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
if (auto&& res = output_func(Nothing, FirstOutput, opts); !res)
|
|
|
|
return Err(std::move(res.error()));
|
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-11-16 21:51:15 +01:00
|
|
|
if (msg->changed() < opts.find.after.value_or(0))
|
2021-10-20 11:18:15 +02:00
|
|
|
continue;
|
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
if (auto&& res = output_func(msg,
|
|
|
|
{item.doc_id(),
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
n == qres.size(), /* last? */
|
|
|
|
item.query_match()},
|
|
|
|
opts); !res)
|
|
|
|
return Err(std::move(res.error()));
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
2022-04-28 21:47:00 +02:00
|
|
|
|
2023-07-25 20:23:30 +02:00
|
|
|
if (auto&& res{output_func(Nothing, LastOutput, opts)}; !res)
|
|
|
|
return Err(std::move(res.error()));
|
2022-04-28 21:47:00 +02:00
|
|
|
else
|
2023-07-25 20:23:30 +02:00
|
|
|
return Ok();
|
2011-06-02 10:18:47 +02:00
|
|
|
}
|
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
static Result<void>
|
2023-09-09 10:49:42 +02:00
|
|
|
process_store_query(const Store& store, const std::string& expr, const Options& 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-11-16 21:51:15 +01:00
|
|
|
Result<void>
|
|
|
|
Mu::mu_cmd_find(const Store& store, const Options& opts)
|
2010-01-31 11:12:04 +01:00
|
|
|
{
|
2023-09-11 22:52:38 +02:00
|
|
|
auto expr{get_query(store, 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
|
|
|
|
2023-09-09 10:49:42 +02:00
|
|
|
if (opts.find.analyze)
|
|
|
|
return analyze_query_expr(store, *expr, opts);
|
2021-10-20 11:18:15 +02:00
|
|
|
else
|
2023-09-09 10:49:42 +02:00
|
|
|
return process_store_query(store, *expr, opts);
|
2011-09-03 09:43:08 +02:00
|
|
|
}
|
2023-08-26 12:20:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef BUILD_TESTS
|
|
|
|
/*
|
|
|
|
* Tests.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "utils/mu-test-utils.hh"
|
|
|
|
|
|
|
|
|
|
|
|
/* tests for the command line interface, uses testdir2 */
|
|
|
|
|
|
|
|
static std::string test_mu_home;
|
|
|
|
|
|
|
|
auto count_nl(const std::string& s)->size_t {
|
|
|
|
size_t n{};
|
|
|
|
for (auto&& c: s)
|
|
|
|
if (c == '\n')
|
|
|
|
++n;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
search_func(const std::string& expr, size_t expected)
|
|
|
|
{
|
|
|
|
auto res = run_command({MU_PROGRAM, "find", "--muhome", test_mu_home, expr});
|
|
|
|
assert_valid_result(res);
|
|
|
|
|
|
|
|
/* we expect zero lines of error output if there is a match; otherwise
|
|
|
|
* there should be one line 'No matches found' */
|
|
|
|
if (res->exit_code != 0) {
|
|
|
|
g_assert_cmpuint(res->exit_code, ==, 2); // no match
|
|
|
|
g_assert_true(res->standard_out.empty());
|
|
|
|
g_assert_cmpuint(count_nl(res->standard_err), ==, 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count_nl(res->standard_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define search(Q,EXP) do { \
|
|
|
|
g_assert_cmpuint(search_func(Q, EXP), ==, EXP); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_mu_find_empty_query(void)
|
|
|
|
{
|
2023-09-12 18:30:18 +02:00
|
|
|
search("\"\"", 14);
|
2023-08-26 12:20:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_mu_find_01(void)
|
|
|
|
{
|
|
|
|
search("f:john fruit", 1);
|
|
|
|
search("f:soc@example.com", 1);
|
|
|
|
search("t:alki@example.com", 1);
|
|
|
|
search("t:alcibiades", 1);
|
|
|
|
search("http emacs", 1);
|
|
|
|
search("f:soc@example.com OR f:john", 2);
|
|
|
|
search("f:soc@example.com OR f:john OR t:edmond", 3);
|
|
|
|
search("t:julius", 1);
|
|
|
|
search("s:dude", 1);
|
|
|
|
search("t:dantès", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* index testdir2, and make sure it adds two documents */
|
|
|
|
static void
|
|
|
|
test_mu_find_02(void)
|
|
|
|
{
|
|
|
|
search("bull", 1);
|
|
|
|
search("g:x", 0);
|
|
|
|
search("flag:encrypted", 0);
|
|
|
|
search("flag:attach", 1);
|
|
|
|
|
|
|
|
search("i:3BE9E6535E0D852173@emss35m06.us.lmco.com", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_mu_find_file(void)
|
|
|
|
{
|
|
|
|
search("file:sittingbull.jpg", 1);
|
|
|
|
search("file:custer.jpg", 1);
|
|
|
|
search("file:custer.*", 1);
|
|
|
|
search("j:sit*", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_mu_find_mime(void)
|
|
|
|
{
|
|
|
|
search("mime:image/jpeg", 1);
|
2023-09-12 18:30:18 +02:00
|
|
|
search("mime:text/plain", 14);
|
|
|
|
search("y:text*", 14);
|
2023-08-26 12:20:51 +02:00
|
|
|
search("y:image*", 1);
|
|
|
|
search("mime:message/rfc822", 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_mu_find_text_in_rfc822(void)
|
|
|
|
{
|
|
|
|
search("embed:dancing", 1);
|
|
|
|
search("e:curious", 1);
|
|
|
|
search("embed:with", 2);
|
|
|
|
search("e:karjala", 0);
|
|
|
|
search("embed:navigation", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_mu_find_maildir_special(void)
|
|
|
|
{
|
|
|
|
search("\"maildir:/wOm_bàT\"", 3);
|
|
|
|
search("\"maildir:/wOm*\"", 3);
|
|
|
|
search("\"maildir:/wOm_*\"", 3);
|
|
|
|
search("\"maildir:wom_bat\"", 0);
|
|
|
|
search("\"maildir:/wombat\"", 0);
|
|
|
|
search("subject:atoms", 1);
|
|
|
|
search("\"maildir:/wom_bat\" subject:atoms", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* some more tests */
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_mu_find_wrong_muhome()
|
|
|
|
{
|
|
|
|
auto res = run_command({MU_PROGRAM, "find", "--muhome",
|
|
|
|
join_paths("/foo", "bar", "nonexistent"), "f:socrates"});
|
|
|
|
assert_valid_result(res);
|
|
|
|
g_assert_cmpuint(res->exit_code,==,1); // general error
|
|
|
|
g_assert_cmpuint(count_nl(res->standard_err), >, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_mu_find_links(void)
|
|
|
|
{
|
|
|
|
TempDir temp_dir;
|
|
|
|
|
|
|
|
{
|
|
|
|
auto res = run_command({MU_PROGRAM, "find", "--muhome", test_mu_home,
|
|
|
|
"--format", "links", "--linksdir", temp_dir.path(),
|
|
|
|
"mime:message/rfc822"});
|
|
|
|
assert_valid_result(res);
|
|
|
|
g_assert_cmpuint(res->exit_code,==,0);
|
|
|
|
g_assert_cmpuint(count_nl(res->standard_out),==,0);
|
|
|
|
g_assert_cmpuint(count_nl(res->standard_err),==,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* furthermore, two symlinks should be there */
|
|
|
|
const auto f1{mu_format("{}/cur/rfc822.1", temp_dir)};
|
|
|
|
const auto f2{mu_format("{}/cur/rfc822.2", temp_dir)};
|
|
|
|
|
|
|
|
g_assert_cmpuint(determine_dtype(f1.c_str(), true), ==, DT_LNK);
|
|
|
|
g_assert_cmpuint(determine_dtype(f2.c_str(), true), ==, DT_LNK);
|
|
|
|
|
|
|
|
/* now we try again, we should get a line of error output,
|
|
|
|
* when we find the first target file already exists */
|
|
|
|
{
|
|
|
|
auto res = run_command({MU_PROGRAM, "find", "--muhome", test_mu_home,
|
|
|
|
"--format", "links", "--linksdir", temp_dir.path(),
|
|
|
|
"mime:message/rfc822"});
|
|
|
|
assert_valid_result(res);
|
|
|
|
g_assert_cmpuint(res->exit_code,==,1);
|
|
|
|
g_assert_cmpuint(count_nl(res->standard_out),==,0);
|
|
|
|
g_assert_cmpuint(count_nl(res->standard_err),==,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now we try again with --clearlinks, and the we should be
|
|
|
|
* back to 0 errors */
|
|
|
|
{
|
|
|
|
auto res = run_command({MU_PROGRAM, "find", "--muhome", test_mu_home,
|
|
|
|
"--format", "links", "--clearlinks", "--linksdir", temp_dir.path(),
|
|
|
|
"mime:message/rfc822"});
|
|
|
|
assert_valid_result(res);
|
|
|
|
g_assert_cmpuint(res->exit_code,==,0);
|
|
|
|
g_assert_cmpuint(count_nl(res->standard_out),==,0);
|
|
|
|
g_assert_cmpuint(count_nl(res->standard_err),==,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert_cmpuint(determine_dtype(f1.c_str(), true), ==, DT_LNK);
|
|
|
|
g_assert_cmpuint(determine_dtype(f2.c_str(), true), ==, DT_LNK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* some more tests */
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
mu_test_init(&argc, &argv);
|
|
|
|
|
|
|
|
if (!set_en_us_utf8_locale())
|
|
|
|
return 0; /* don't error out... */
|
|
|
|
|
|
|
|
TempDir temp_dir{};
|
|
|
|
{
|
|
|
|
test_mu_home = temp_dir.path();
|
|
|
|
|
|
|
|
auto res1 = run_command({MU_PROGRAM, "--quiet", "init",
|
|
|
|
"--muhome", test_mu_home, "--maildir" , MU_TESTMAILDIR2});
|
|
|
|
assert_valid_result(res1);
|
|
|
|
|
|
|
|
auto res2 = run_command({MU_PROGRAM, "--quiet", "index",
|
|
|
|
"--muhome", test_mu_home});
|
|
|
|
assert_valid_result(res2);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_test_add_func("/cmd/find/empty-query", test_mu_find_empty_query);
|
|
|
|
g_test_add_func("/cmd/find/01", test_mu_find_01);
|
|
|
|
g_test_add_func("/cmd/find/02", test_mu_find_02);
|
|
|
|
g_test_add_func("/cmd/find/file", test_mu_find_file);
|
|
|
|
g_test_add_func("/cmd/find/mime", test_mu_find_mime);
|
|
|
|
g_test_add_func("/cmd/find/links", test_mu_find_links);
|
|
|
|
g_test_add_func("/cmd/find/text-in-rfc822", test_mu_find_text_in_rfc822);
|
|
|
|
g_test_add_func("/cmd/find/wrong-muhome", test_mu_find_wrong_muhome);
|
|
|
|
g_test_add_func("/cmd/find/maildir-special", test_mu_find_maildir_special);
|
|
|
|
|
|
|
|
return g_test_run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /*BUILD_TESTS*/
|