2020-10-31 08:41:21 +01:00
|
|
|
/*
|
2022-06-29 21:18:15 +02:00
|
|
|
** Copyright (C) 2020-2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2020-10-31 08:41:21 +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"
|
|
|
|
|
2022-03-26 16:16:46 +01:00
|
|
|
#include "message/mu-message.hh"
|
2020-10-31 08:41:21 +01:00
|
|
|
#include "mu-server.hh"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <atomic>
|
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <glib.h>
|
|
|
|
#include <glib/gprintf.h>
|
|
|
|
|
2020-11-28 09:15:49 +01:00
|
|
|
#include "mu-maildir.hh"
|
2020-11-03 08:58:59 +01:00
|
|
|
#include "mu-query.hh"
|
2020-10-31 08:41:21 +01:00
|
|
|
#include "index/mu-indexer.hh"
|
|
|
|
#include "mu-store.hh"
|
|
|
|
|
|
|
|
#include "utils/mu-utils.hh"
|
2022-03-28 07:58:04 +02:00
|
|
|
#include "utils/mu-option.hh"
|
2022-11-07 17:33:01 +01:00
|
|
|
#include "utils/mu-command-handler.hh"
|
2020-10-31 08:41:21 +01:00
|
|
|
#include "utils/mu-readline.hh"
|
|
|
|
|
|
|
|
using namespace Mu;
|
|
|
|
|
|
|
|
/// @brief object to manage the server-context for all commands.
|
|
|
|
struct Server::Private {
|
2021-10-20 11:18:15 +02:00
|
|
|
Private(Store& store, Output output)
|
2022-11-07 17:33:01 +01:00
|
|
|
: store_{store}, output_{output},
|
|
|
|
command_handler_{make_command_map()},
|
2021-10-20 11:18:15 +02:00
|
|
|
keep_going_{true}
|
2022-06-29 21:18:15 +02:00
|
|
|
{}
|
2022-02-03 21:59:47 +01:00
|
|
|
|
2022-06-29 21:18:15 +02:00
|
|
|
~Private() {
|
2022-02-03 21:59:47 +01:00
|
|
|
indexer().stop();
|
|
|
|
if (index_thread_.joinable())
|
|
|
|
index_thread_.join();
|
|
|
|
}
|
2021-10-20 11:18:15 +02:00
|
|
|
//
|
|
|
|
// construction helpers
|
|
|
|
//
|
2022-11-07 17:33:01 +01:00
|
|
|
CommandHandler::CommandInfoMap make_command_map();
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// acccessors
|
|
|
|
Store& store() { return store_; }
|
|
|
|
const Store& store() const { return store_; }
|
|
|
|
Indexer& indexer() { return store().indexer(); }
|
2022-11-07 17:33:01 +01:00
|
|
|
//CommandMap& command_map() const { return command_map_; }
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// invoke
|
|
|
|
//
|
|
|
|
bool invoke(const std::string& expr) noexcept;
|
|
|
|
|
|
|
|
//
|
|
|
|
// output
|
|
|
|
//
|
2022-11-07 17:33:01 +01:00
|
|
|
void output_sexp(const Sexp& sexp, Server::OutputFlags flags = {}) const {
|
2021-10-20 11:18:15 +02:00
|
|
|
if (output_)
|
2022-11-07 17:33:01 +01:00
|
|
|
output_(sexp, flags);
|
2022-02-03 21:58:53 +01:00
|
|
|
}
|
2022-05-06 21:07:15 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
size_t output_results(const QueryResults& qres, size_t batch_size) const;
|
|
|
|
|
|
|
|
//
|
|
|
|
// handlers for various commands.
|
|
|
|
//
|
2022-11-07 17:33:01 +01:00
|
|
|
void add_handler(const Command& cmd);
|
|
|
|
void compose_handler(const Command& cmd);
|
|
|
|
void contacts_handler(const Command& cmd);
|
|
|
|
void find_handler(const Command& cmd);
|
|
|
|
void help_handler(const Command& cmd);
|
|
|
|
void index_handler(const Command& cmd);
|
|
|
|
void move_handler(const Command& cmd);
|
|
|
|
void mkdir_handler(const Command& cmd);
|
|
|
|
void ping_handler(const Command& cmd);
|
2023-01-07 09:29:57 +01:00
|
|
|
void queries_handler(const Command& cmd);
|
2022-11-07 17:33:01 +01:00
|
|
|
void quit_handler(const Command& cmd);
|
|
|
|
void remove_handler(const Command& cmd);
|
|
|
|
void sent_handler(const Command& cmd);
|
|
|
|
void view_handler(const Command& cmd);
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2021-11-10 20:50:43 +01:00
|
|
|
private:
|
2021-10-20 11:18:15 +02:00
|
|
|
// helpers
|
2022-04-22 07:07:06 +02:00
|
|
|
Sexp build_message_sexp(const Message& msg,
|
|
|
|
Store::Id docid,
|
|
|
|
const Option<QueryMatch&> qm) const;
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-12-03 15:45:41 +01:00
|
|
|
void move_docid(Store::Id docid, Option<std::string> flagstr,
|
2022-11-07 17:33:01 +01:00
|
|
|
bool new_name, bool no_view);
|
2022-02-16 22:03:48 +01:00
|
|
|
|
2022-12-03 15:45:41 +01:00
|
|
|
void perform_move(Store::Id docid,
|
|
|
|
const Message& msg,
|
2022-11-07 17:33:01 +01:00
|
|
|
const std::string& maildirarg,
|
|
|
|
Flags flags,
|
|
|
|
bool new_name,
|
|
|
|
bool no_view);
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-12-03 15:45:41 +01:00
|
|
|
void view_mark_as_read(Store::Id docid, Message&& msg, bool rename);
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
Store& store_;
|
|
|
|
Server::Output output_;
|
|
|
|
const CommandHandler command_handler_;
|
|
|
|
std::atomic<bool> keep_going_{};
|
|
|
|
std::thread index_thread_;
|
2020-10-31 08:41:21 +01:00
|
|
|
};
|
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
static Sexp
|
2021-10-21 18:19:21 +02:00
|
|
|
build_metadata(const QueryMatch& qmatch)
|
2021-01-29 21:39:21 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
const auto td{::atoi(qmatch.thread_date.c_str())};
|
2022-11-07 17:33:01 +01:00
|
|
|
auto mdata = Sexp().put_props(":path", qmatch.thread_path,
|
|
|
|
":level", qmatch.thread_level,
|
|
|
|
":date", qmatch.thread_date,
|
|
|
|
":data-tstamp", Sexp().add(static_cast<unsigned>(td >> 16),
|
|
|
|
static_cast<unsigned>(td & 0xffff),
|
|
|
|
0));
|
2021-10-20 11:18:15 +02:00
|
|
|
if (qmatch.has_flag(QueryMatch::Flags::Root))
|
2022-11-07 17:33:01 +01:00
|
|
|
mdata.put_props(":root", Sexp::t());
|
2021-10-20 11:18:15 +02:00
|
|
|
if (qmatch.has_flag(QueryMatch::Flags::Related))
|
2022-11-07 17:33:01 +01:00
|
|
|
mdata.put_props(":related", Sexp::t());
|
2021-10-20 11:18:15 +02:00
|
|
|
if (qmatch.has_flag(QueryMatch::Flags::First))
|
2022-11-07 17:33:01 +01:00
|
|
|
mdata.put_props(":first-child", Sexp::t());
|
2021-10-20 11:18:15 +02:00
|
|
|
if (qmatch.has_flag(QueryMatch::Flags::Last))
|
2022-11-07 17:33:01 +01:00
|
|
|
mdata.put_props(":last-child", Sexp::t());
|
2021-10-20 11:18:15 +02:00
|
|
|
if (qmatch.has_flag(QueryMatch::Flags::Orphan))
|
2022-11-07 17:33:01 +01:00
|
|
|
mdata.put_props(":orphan", Sexp::t());
|
2021-10-20 11:18:15 +02:00
|
|
|
if (qmatch.has_flag(QueryMatch::Flags::Duplicate))
|
2022-11-07 17:33:01 +01:00
|
|
|
mdata.put_props(":duplicate", Sexp::t());
|
2021-10-20 11:18:15 +02:00
|
|
|
if (qmatch.has_flag(QueryMatch::Flags::HasChild))
|
2022-11-07 17:33:01 +01:00
|
|
|
mdata.put_props(":has-child", Sexp::t());
|
2021-10-20 11:18:15 +02:00
|
|
|
if (qmatch.has_flag(QueryMatch::Flags::ThreadSubject))
|
2022-11-07 17:33:01 +01:00
|
|
|
mdata.put_props(":thread-subject", Sexp::t());
|
2021-01-29 21:39:21 +01:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
return mdata;
|
2021-01-29 21:39:21 +01:00
|
|
|
}
|
|
|
|
|
2021-10-21 18:19:21 +02:00
|
|
|
/*
|
2022-11-07 17:33:01 +01:00
|
|
|
* A message here consists of a message s-expression with optionally a :docid
|
|
|
|
* and/or :meta expression added.
|
2021-10-20 11:18:15 +02:00
|
|
|
*/
|
2021-01-29 21:39:21 +01:00
|
|
|
Sexp
|
2022-04-22 07:07:06 +02:00
|
|
|
Server::Private::build_message_sexp(const Message& msg,
|
2022-04-28 22:01:16 +02:00
|
|
|
Store::Id docid,
|
2022-04-22 07:07:06 +02:00
|
|
|
const Option<QueryMatch&> qm) const
|
2021-01-29 21:39:21 +01:00
|
|
|
{
|
2022-11-07 17:33:01 +01:00
|
|
|
Sexp sexp{msg.sexp()}; // copy
|
2022-05-05 00:36:01 +02:00
|
|
|
if (docid != 0)
|
2022-11-07 17:33:01 +01:00
|
|
|
sexp.put_props(":docid", docid);
|
2021-10-20 11:18:15 +02:00
|
|
|
if (qm)
|
2022-11-07 17:33:01 +01:00
|
|
|
sexp.put_props(":meta", build_metadata(*qm));
|
2020-10-31 08:41:21 +01:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
return sexp;
|
2021-01-29 21:39:21 +01:00
|
|
|
}
|
2021-10-20 21:41:48 +02:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
CommandHandler::CommandInfoMap
|
2021-10-20 11:18:15 +02:00
|
|
|
Server::Private::make_command_map()
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2022-11-07 17:33:01 +01:00
|
|
|
CommandHandler::CommandInfoMap cmap;
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
using CommandInfo = CommandHandler::CommandInfo;
|
|
|
|
using ArgMap = CommandHandler::ArgMap;
|
|
|
|
using ArgInfo = CommandHandler::ArgInfo;
|
|
|
|
using Type = Sexp::Type;
|
|
|
|
using Type = Sexp::Type;
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
cmap.emplace(
|
|
|
|
"add",
|
|
|
|
CommandInfo{
|
|
|
|
ArgMap{{":path", ArgInfo{Type::String, true, "file system path to the message"}}},
|
|
|
|
"add a message to the store",
|
|
|
|
[&](const auto& params) { add_handler(params); }});
|
|
|
|
|
|
|
|
cmap.emplace(
|
|
|
|
"compose",
|
|
|
|
CommandInfo{
|
|
|
|
ArgMap{
|
|
|
|
{":type",
|
2022-02-25 22:03:48 +01:00
|
|
|
ArgInfo{Type::Symbol,
|
|
|
|
true,
|
|
|
|
"type of composition: reply/forward/edit/resend/new"}},
|
2021-10-20 11:18:15 +02:00
|
|
|
{":docid",
|
2022-02-25 22:03:48 +01:00
|
|
|
ArgInfo{Type::Number, false, "document id of parent-message, if any"}},
|
2021-10-20 11:18:15 +02:00
|
|
|
{":decrypt",
|
2022-02-25 22:03:48 +01:00
|
|
|
ArgInfo{Type::Symbol, false, "whether to decrypt encrypted parts (if any)"}}},
|
2021-10-20 11:18:15 +02:00
|
|
|
"compose a new message",
|
|
|
|
[&](const auto& params) { compose_handler(params); }});
|
|
|
|
|
|
|
|
cmap.emplace(
|
|
|
|
"contacts",
|
|
|
|
CommandInfo{
|
|
|
|
ArgMap{{":personal", ArgInfo{Type::Symbol, false, "only personal contacts"}},
|
2022-02-25 22:03:48 +01:00
|
|
|
{":after",
|
|
|
|
ArgInfo{Type::String, false, "only contacts seen after time_t string"}},
|
2022-05-06 20:58:51 +02:00
|
|
|
{":tstamp", ArgInfo{Type::String, false, "return changes since tstamp"}},
|
|
|
|
{":maxnum", ArgInfo{Type::Number, false, "max number of contacts to return"}}},
|
2021-10-20 11:18:15 +02:00
|
|
|
"get contact information",
|
|
|
|
[&](const auto& params) { contacts_handler(params); }});
|
|
|
|
cmap.emplace(
|
|
|
|
"find",
|
|
|
|
CommandInfo{
|
|
|
|
ArgMap{{":query", ArgInfo{Type::String, true, "search expression"}},
|
2022-02-25 22:03:48 +01:00
|
|
|
{":threads",
|
|
|
|
ArgInfo{Type::Symbol, false, "whether to include threading information"}},
|
|
|
|
{":sortfield", ArgInfo{Type::Symbol, false, "the field to sort results by"}},
|
|
|
|
{":descending",
|
|
|
|
ArgInfo{Type::Symbol, false, "whether to sort in descending order"}},
|
|
|
|
{":batch-size", ArgInfo{Type::Number, false, "batch size for result"}},
|
|
|
|
{":maxnum", ArgInfo{Type::Number, false, "maximum number of result (hint)"}},
|
|
|
|
{":skip-dups",
|
|
|
|
ArgInfo{Type::Symbol,
|
|
|
|
false,
|
|
|
|
"whether to skip messages with duplicate message-ids"}},
|
|
|
|
{":include-related",
|
|
|
|
ArgInfo{Type::Symbol,
|
|
|
|
false,
|
|
|
|
"whether to include other message related to matching ones"}}},
|
2021-10-20 11:18:15 +02:00
|
|
|
"query the database for messages",
|
|
|
|
[&](const auto& params) { find_handler(params); }});
|
|
|
|
|
|
|
|
cmap.emplace(
|
|
|
|
"help",
|
|
|
|
CommandInfo{
|
|
|
|
ArgMap{{":command", ArgInfo{Type::Symbol, false, "command to get information for"}},
|
2022-02-25 22:03:48 +01:00
|
|
|
{":full", ArgInfo{Type::Symbol, false, "show full descriptions"}}},
|
2021-10-20 11:18:15 +02:00
|
|
|
"get information about one or all commands",
|
|
|
|
[&](const auto& params) { help_handler(params); }});
|
|
|
|
cmap.emplace(
|
|
|
|
"index",
|
|
|
|
CommandInfo{
|
|
|
|
ArgMap{{":my-addresses", ArgInfo{Type::List, false, "list of 'my' addresses"}},
|
2022-02-25 22:03:48 +01:00
|
|
|
{":cleanup",
|
|
|
|
ArgInfo{Type::Symbol,
|
|
|
|
false,
|
|
|
|
"whether to remove stale messages from the store"}},
|
|
|
|
{":lazy-check",
|
|
|
|
ArgInfo{Type::Symbol,
|
|
|
|
false,
|
|
|
|
"whether to avoid indexing up-to-date directories"}}},
|
2021-10-20 11:18:15 +02:00
|
|
|
"scan maildir for new/updated/removed messages",
|
|
|
|
[&](const auto& params) { index_handler(params); }});
|
|
|
|
|
|
|
|
cmap.emplace(
|
|
|
|
"move",
|
|
|
|
CommandInfo{
|
|
|
|
ArgMap{
|
|
|
|
{":docid", ArgInfo{Type::Number, false, "document-id"}},
|
|
|
|
{":msgid", ArgInfo{Type::String, false, "message-id"}},
|
|
|
|
{":flags", ArgInfo{Type::String, false, "new flags for the message"}},
|
|
|
|
{":maildir", ArgInfo{Type::String, false, "the target maildir"}},
|
|
|
|
{":rename", ArgInfo{Type::Symbol, false, "change filename when moving"}},
|
|
|
|
{":no-view",
|
2022-02-25 22:03:48 +01:00
|
|
|
ArgInfo{Type::Symbol, false, "if set, do not hint at updating the view"}},
|
2021-10-20 11:18:15 +02:00
|
|
|
},
|
|
|
|
"move messages and/or change their flags",
|
|
|
|
|
|
|
|
[&](const auto& params) { move_handler(params); }});
|
|
|
|
|
|
|
|
cmap.emplace(
|
|
|
|
"mkdir",
|
|
|
|
CommandInfo{
|
|
|
|
ArgMap{{":path", ArgInfo{Type::String, true, "location for the new maildir"}}},
|
|
|
|
"create a new maildir",
|
|
|
|
[&](const auto& params) { mkdir_handler(params); }});
|
|
|
|
cmap.emplace(
|
|
|
|
"ping",
|
2023-01-07 09:29:57 +01:00
|
|
|
CommandInfo{
|
|
|
|
ArgMap{},
|
|
|
|
"ping the mu-server and get server information in the response",
|
|
|
|
[&](const auto& params) { ping_handler(params); }});
|
|
|
|
|
|
|
|
cmap.emplace(
|
|
|
|
"queries",
|
2021-10-20 11:18:15 +02:00
|
|
|
CommandInfo{
|
|
|
|
ArgMap{
|
|
|
|
{":queries",
|
2022-02-25 22:03:48 +01:00
|
|
|
ArgInfo{Type::List, false, "queries for which to get read/unread numbers"}},
|
2021-10-20 11:18:15 +02:00
|
|
|
},
|
2023-01-07 09:29:57 +01:00
|
|
|
"get unread/totals information for a list of queries",
|
|
|
|
[&](const auto& params) { queries_handler(params); }});
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
cmap.emplace("quit", CommandInfo{{}, "quit the mu server", [&](const auto& params) {
|
|
|
|
quit_handler(params);
|
|
|
|
}});
|
|
|
|
|
|
|
|
cmap.emplace(
|
|
|
|
"remove",
|
|
|
|
CommandInfo{
|
|
|
|
ArgMap{{":docid",
|
2022-02-25 22:03:48 +01:00
|
|
|
ArgInfo{Type::Number, true, "document-id for the message to remove"}}},
|
2021-10-20 11:18:15 +02:00
|
|
|
"remove a message from filesystem and database",
|
|
|
|
[&](const auto& params) { remove_handler(params); }});
|
|
|
|
|
|
|
|
cmap.emplace(
|
|
|
|
"sent",
|
|
|
|
CommandInfo{ArgMap{{":path", ArgInfo{Type::String, true, "path to the message file"}}},
|
2022-02-25 22:03:48 +01:00
|
|
|
"tell mu about a message that was sent",
|
|
|
|
[&](const auto& params) { sent_handler(params); }});
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
cmap.emplace(
|
|
|
|
"view",
|
|
|
|
CommandInfo{ArgMap{
|
|
|
|
{":docid", ArgInfo{Type::Number, false, "document-id"}},
|
|
|
|
{":msgid", ArgInfo{Type::String, false, "message-id"}},
|
|
|
|
{":path", ArgInfo{Type::String, false, "message filesystem path"}},
|
|
|
|
{":mark-as-read",
|
2022-02-25 22:03:48 +01:00
|
|
|
ArgInfo{Type::Symbol, false, "mark message as read (if not already)"}},
|
2022-02-13 11:03:16 +01:00
|
|
|
{":rename", ArgInfo{Type::Symbol, false, "change filename when moving"}},
|
2021-10-20 11:18:15 +02:00
|
|
|
},
|
2022-02-25 22:03:48 +01:00
|
|
|
"view a message. exactly one of docid/msgid/path must be specified",
|
|
|
|
[&](const auto& params) { view_handler(params); }});
|
2021-10-20 11:18:15 +02:00
|
|
|
return cmap;
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
2022-01-30 13:33:25 +01:00
|
|
|
G_GNUC_PRINTF(2, 3)
|
|
|
|
static Sexp
|
|
|
|
make_error(Error::Code errcode, const char* frm, ...)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
char* msg{};
|
|
|
|
va_list ap;
|
2020-10-31 08:41:21 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
va_start(ap, frm);
|
|
|
|
g_vasprintf(&msg, frm, ap);
|
|
|
|
va_end(ap);
|
2020-10-31 08:41:21 +01:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
auto err = Sexp().put_props(":error", static_cast<int>(errcode),
|
|
|
|
":message", msg);
|
2021-10-20 11:18:15 +02:00
|
|
|
g_free(msg);
|
2022-11-07 17:33:01 +01:00
|
|
|
return err;
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2021-10-20 11:18:15 +02:00
|
|
|
Server::Private::invoke(const std::string& expr) noexcept
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!keep_going_)
|
|
|
|
return false;
|
|
|
|
try {
|
2022-11-07 17:33:01 +01:00
|
|
|
auto cmd{Command::make_parse(std::string{expr})};
|
|
|
|
if (!cmd)
|
|
|
|
throw cmd.error();
|
|
|
|
|
|
|
|
auto res = command_handler_.invoke(*cmd);
|
|
|
|
if (!res)
|
|
|
|
throw res.error();
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
} catch (const Mu::Error& me) {
|
|
|
|
output_sexp(make_error(me.code(), "%s", me.what()));
|
2022-05-05 00:36:01 +02:00
|
|
|
keep_going_ = true;
|
|
|
|
} catch (const Xapian::Error& xerr) {
|
|
|
|
output_sexp(make_error(Error::Code::Internal, "xapian error: %s: %s",
|
|
|
|
xerr.get_type(), xerr.get_description().c_str()));
|
|
|
|
keep_going_ = false;
|
2021-10-20 11:18:15 +02:00
|
|
|
} catch (const std::runtime_error& re) {
|
|
|
|
output_sexp(make_error(Error::Code::Internal, "caught exception: %s", re.what()));
|
|
|
|
keep_going_ = false;
|
|
|
|
} catch (...) {
|
|
|
|
output_sexp(make_error(Error::Code::Internal, "something went wrong: quiting"));
|
|
|
|
keep_going_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return keep_going_;
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 'add' adds a message to the database, and takes two parameters: 'path', which
|
|
|
|
* is the full path to the message, and 'maildir', which is the maildir this
|
|
|
|
* message lives in (e.g. "/inbox"). response with an (:info ...) message with
|
|
|
|
* information about the newly added message (details: see code below)
|
|
|
|
*/
|
|
|
|
void
|
2022-11-07 17:33:01 +01:00
|
|
|
Server::Private::add_handler(const Command& cmd)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2022-11-07 17:33:01 +01:00
|
|
|
auto path{cmd.string_arg(":path")};
|
|
|
|
const auto docid_res{store().add_message(*path)};
|
2022-04-22 07:07:06 +02:00
|
|
|
|
|
|
|
if (!docid_res)
|
2022-07-04 23:11:56 +02:00
|
|
|
throw docid_res.error();
|
2022-04-22 07:07:06 +02:00
|
|
|
|
|
|
|
const auto docid{docid_res.value()};
|
2022-11-07 17:33:01 +01:00
|
|
|
output_sexp(Sexp().put_props(":info", "add"_sym,
|
|
|
|
":path", *path,
|
|
|
|
":docid", docid));
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-04-22 07:07:06 +02:00
|
|
|
auto msg_res{store().find_message(docid)};
|
|
|
|
if (!msg_res)
|
2021-10-20 11:18:15 +02:00
|
|
|
throw Error(Error::Code::Store,
|
2022-11-01 17:37:31 +01:00
|
|
|
"failed to get message at %s (docid=%u): %s",
|
2022-11-07 17:33:01 +01:00
|
|
|
path->c_str(), docid);
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
output_sexp(Sexp().put_props(":update",
|
|
|
|
build_message_sexp(msg_res.value(), docid, {})));
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 'compose' produces the un-changed *original* message sexp (ie., the message
|
|
|
|
* to reply to, forward or edit) for a new message to compose). It takes two
|
|
|
|
* parameters: 'type' with the compose type (either reply, forward or
|
|
|
|
* edit/resend), and 'docid' for the message to reply to. Note, type:new does
|
|
|
|
* not have an original message, and therefore does not need a docid
|
|
|
|
*
|
|
|
|
* In returns a (:compose <type> [:original <original-msg>] [:include] )
|
|
|
|
* message (detals: see code below)
|
|
|
|
*
|
|
|
|
* Note ':include' t or nil determines whether to include attachments
|
|
|
|
*/
|
2022-05-05 00:36:01 +02:00
|
|
|
|
|
|
|
static Option<Sexp>
|
|
|
|
maybe_add_attachment(Message& message, const MessagePart& part, size_t index)
|
|
|
|
{
|
|
|
|
if (!part.is_attachment())
|
|
|
|
return Nothing;
|
|
|
|
|
2022-05-15 10:33:14 +02:00
|
|
|
const auto cache_path{message.cache_path(index)};
|
2022-05-05 00:36:01 +02:00
|
|
|
if (!cache_path)
|
|
|
|
throw cache_path.error();
|
|
|
|
|
2022-05-15 10:33:14 +02:00
|
|
|
const auto cooked_name{part.cooked_filename()};
|
|
|
|
const auto fname{format("%s/%s", cache_path->c_str(),
|
|
|
|
cooked_name.value_or("part").c_str())};
|
2022-05-05 00:36:01 +02:00
|
|
|
|
|
|
|
const auto res = part.to_file(fname, true);
|
|
|
|
if (!res)
|
|
|
|
throw res.error();
|
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
Sexp pi;
|
2022-05-05 22:45:11 +02:00
|
|
|
if (auto cdescr = part.content_description(); cdescr)
|
2022-11-07 17:33:01 +01:00
|
|
|
pi.put_props(":description", *cdescr);
|
2022-05-15 10:33:14 +02:00
|
|
|
else if (cooked_name)
|
2022-11-07 17:33:01 +01:00
|
|
|
pi.put_props(":description", cooked_name.value());
|
2022-05-05 22:45:11 +02:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
pi.put_props(":file-name", fname,
|
|
|
|
":mime-type",
|
|
|
|
part.mime_type().value_or("application/octet-stream"));
|
2022-05-05 22:45:11 +02:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
return Some(std::move(pi));
|
2022-05-05 00:36:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-31 08:41:21 +01:00
|
|
|
void
|
2022-11-07 17:33:01 +01:00
|
|
|
Server::Private::compose_handler(const Command& cmd)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2022-11-07 17:33:01 +01:00
|
|
|
const auto ctype = cmd.symbol_arg(":type").value_or("<error>");
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
auto comp_lst = Sexp().put_props(":compose", Sexp::Symbol(ctype));
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-04-22 07:07:06 +02:00
|
|
|
|
2022-05-05 00:36:01 +02:00
|
|
|
if (ctype == "reply" || ctype == "forward" ||
|
|
|
|
ctype == "edit" || ctype == "resend") {
|
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
const unsigned docid{static_cast<unsigned>(cmd.number_arg(":docid").value_or(0))};
|
2022-04-22 07:07:06 +02:00
|
|
|
auto msg{store().find_message(docid)};
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!msg)
|
2022-04-22 07:07:06 +02:00
|
|
|
throw Error{Error::Code::Store, "failed to get message %u", docid};
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
comp_lst.put_props(":original", build_message_sexp(msg.value(), docid, {}));
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
if (ctype == "forward") {
|
2022-05-05 00:36:01 +02:00
|
|
|
// when forwarding, attach any attachment in the orig
|
|
|
|
size_t index{};
|
2022-11-07 17:33:01 +01:00
|
|
|
Sexp attseq;
|
2022-05-05 00:36:01 +02:00
|
|
|
for (auto&& part: msg->parts()) {
|
|
|
|
if (auto attsexp = maybe_add_attachment(
|
|
|
|
*msg, part, index); attsexp) {
|
|
|
|
attseq.add(std::move(*attsexp));
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
}
|
2022-05-05 22:45:11 +02:00
|
|
|
if (!attseq.empty()) {
|
2022-11-07 17:33:01 +01:00
|
|
|
comp_lst.put_props(":include", std::move(attseq),
|
|
|
|
":cache-path", *msg->cache_path());
|
2022-05-05 22:45:11 +02:00
|
|
|
}
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (ctype != "new")
|
2022-04-22 07:07:06 +02:00
|
|
|
throw Error(Error::Code::InvalidArgument, "invalid compose type '%s'",
|
2022-02-25 22:03:48 +01:00
|
|
|
ctype.c_str());
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
output_sexp(comp_lst);
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-11-07 17:33:01 +01:00
|
|
|
Server::Private::contacts_handler(const Command& cmd)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2022-11-07 17:33:01 +01:00
|
|
|
const auto personal = cmd.boolean_arg(":personal");
|
|
|
|
const auto afterstr = cmd.string_arg(":after").value_or("");
|
|
|
|
const auto tstampstr = cmd.string_arg(":tstamp").value_or("");
|
|
|
|
const auto maxnum = cmd.number_arg(":maxnum").value_or(0 /*unlimited*/);
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-05-05 00:36:01 +02:00
|
|
|
const auto after{afterstr.empty() ? 0 :
|
|
|
|
parse_date_time(afterstr, true).value_or(0)};
|
2021-10-20 11:18:15 +02:00
|
|
|
const auto tstamp = g_ascii_strtoll(tstampstr.c_str(), NULL, 10);
|
|
|
|
|
2022-05-05 00:36:01 +02:00
|
|
|
g_debug("find %s contacts last seen >= %s (tstamp: %zu)",
|
|
|
|
personal ? "personal" : "any",
|
|
|
|
time_to_string("%c", after).c_str(),
|
|
|
|
static_cast<size_t>(tstamp));
|
|
|
|
|
2022-05-09 20:17:03 +02:00
|
|
|
auto n{0};
|
2022-11-07 17:33:01 +01:00
|
|
|
Sexp contacts;
|
2022-03-20 13:12:41 +01:00
|
|
|
store().contacts_cache().for_each([&](const Contact& ci) {
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
/* since the last time we got some contacts */
|
|
|
|
if (tstamp > ci.tstamp)
|
2022-05-09 20:17:03 +02:00
|
|
|
return true;
|
2021-10-20 11:18:15 +02:00
|
|
|
/* (maybe) only include 'personal' contacts */
|
|
|
|
if (personal && !ci.personal)
|
2022-05-09 20:17:03 +02:00
|
|
|
return true;
|
2021-10-20 11:18:15 +02:00
|
|
|
/* only include newer-than-x contacts */
|
2022-02-19 23:35:43 +01:00
|
|
|
if (after > ci.message_date)
|
2022-05-09 20:17:03 +02:00
|
|
|
return true;
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-05-09 20:17:03 +02:00
|
|
|
n++;
|
2022-05-05 00:36:01 +02:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
contacts.add(ci.display_name(true/*encode-if-needed*/));
|
2022-05-09 20:17:03 +02:00
|
|
|
return maxnum == 0 || n < maxnum;
|
|
|
|
});
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
Sexp seq;
|
|
|
|
seq.put_props(":contacts", contacts,
|
|
|
|
":tstamp", format("%" G_GINT64_FORMAT, g_get_monotonic_time()));
|
2022-05-06 21:07:15 +02:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
/* dump the contacts cache as a giant sexp */
|
2022-05-09 20:17:03 +02:00
|
|
|
g_debug("sending %d of %zu contact(s)", n, store().contacts_cache().size());
|
2022-11-07 17:33:01 +01:00
|
|
|
output_sexp(seq, Server::OutputFlags::SplitList);
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get a *list* of all messages with the given message id */
|
2021-02-11 17:54:25 +01:00
|
|
|
static std::vector<Store::Id>
|
2022-01-30 13:33:25 +01:00
|
|
|
docids_for_msgid(const Store& store, const std::string& msgid, size_t max = 100)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2022-05-09 19:10:37 +02:00
|
|
|
if (msgid.size() > MaxTermLength) {
|
2022-05-05 00:36:01 +02:00
|
|
|
throw Error(Error::Code::InvalidArgument,
|
|
|
|
"invalid message-id '%s'", msgid.c_str());
|
2022-06-29 07:59:40 +02:00
|
|
|
} else if (msgid.empty())
|
|
|
|
return {};
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-03-20 13:12:41 +01:00
|
|
|
const auto xprefix{field_from_id(Field::Id::MessageId).shortcut};
|
2021-10-20 11:18:15 +02:00
|
|
|
/*XXX this is a bit dodgy */
|
|
|
|
auto tmp{g_ascii_strdown(msgid.c_str(), -1)};
|
|
|
|
auto expr{g_strdup_printf("%c:%s", xprefix, tmp)};
|
|
|
|
g_free(tmp);
|
|
|
|
|
|
|
|
GError* gerr{};
|
2022-02-17 22:46:44 +01:00
|
|
|
std::lock_guard l{store.lock()};
|
2022-03-04 23:35:26 +01:00
|
|
|
const auto res{store.run_query(expr, {}, QueryFlags::None, max)};
|
2021-10-20 11:18:15 +02:00
|
|
|
g_free(expr);
|
|
|
|
if (!res)
|
2022-11-01 17:37:31 +01:00
|
|
|
throw Error(Error::Code::Store, &gerr,
|
|
|
|
"failed to run message-id-query: %s", res.error().what());
|
2021-10-20 11:18:15 +02:00
|
|
|
else if (res->empty())
|
|
|
|
throw Error(Error::Code::NotFound,
|
2022-05-12 07:50:20 +02:00
|
|
|
"could not find message(s) for msgid %s", msgid.c_str());
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
std::vector<Store::Id> docids{};
|
|
|
|
for (auto&& mi : *res)
|
|
|
|
docids.emplace_back(mi.doc_id());
|
|
|
|
|
|
|
|
return docids;
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* creating a message object just to get a path seems a bit excessive maybe
|
|
|
|
* mu_store_get_path could be added if this turns out to be a problem
|
|
|
|
*/
|
|
|
|
static std::string
|
2022-04-22 07:07:06 +02:00
|
|
|
path_from_docid(const Store& store, Store::Id docid)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
auto msg{store.find_message(docid)};
|
|
|
|
if (!msg)
|
|
|
|
throw Error(Error::Code::Store, "could not get message from store");
|
2020-10-31 08:41:21 +01:00
|
|
|
|
2022-04-22 07:07:06 +02:00
|
|
|
if (auto path{msg->path()}; path.empty())
|
|
|
|
throw Error(Error::Code::Store, "could not get path for message %u",
|
|
|
|
docid);
|
|
|
|
else
|
|
|
|
return path;
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
2021-02-11 17:54:25 +01:00
|
|
|
static std::vector<Store::Id>
|
2022-11-07 17:33:01 +01:00
|
|
|
determine_docids(const Store& store, const Command& cmd)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2022-11-07 17:33:01 +01:00
|
|
|
auto docid{cmd.number_arg(":docid").value_or(0)};
|
|
|
|
const auto msgid{cmd.string_arg(":msgid").value_or("")};
|
2020-10-31 08:41:21 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if ((docid == 0) == msgid.empty())
|
|
|
|
throw Error(Error::Code::InvalidArgument,
|
2022-02-25 22:03:48 +01:00
|
|
|
"precisely one of docid and msgid must be specified");
|
2020-10-31 08:41:21 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (docid != 0)
|
2022-02-13 11:03:16 +01:00
|
|
|
return {static_cast<Store::Id>(docid)};
|
2021-10-20 11:18:15 +02:00
|
|
|
else
|
2022-01-30 13:33:25 +01:00
|
|
|
return docids_for_msgid(store, msgid.c_str());
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2021-10-20 11:18:15 +02:00
|
|
|
Server::Private::output_results(const QueryResults& qres, size_t batch_size) const
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2021-10-21 18:19:21 +02:00
|
|
|
size_t n{};
|
2022-11-07 17:33:01 +01:00
|
|
|
Sexp headers;
|
2021-10-21 18:19:21 +02:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
const auto output_batch = [&](Sexp&& hdrs) {
|
|
|
|
Sexp batch;
|
|
|
|
batch.put_props(":headers", std::move(hdrs));
|
|
|
|
output_sexp(batch);
|
2021-10-21 18:19:21 +02:00
|
|
|
};
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
for (auto&& mi : qres) {
|
2022-04-22 07:07:06 +02:00
|
|
|
auto msg{mi.message()};
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!msg)
|
|
|
|
continue;
|
|
|
|
++n;
|
2021-10-21 18:19:21 +02:00
|
|
|
|
|
|
|
// construct sexp for a single header.
|
|
|
|
auto qm{mi.query_match()};
|
2022-05-06 21:07:15 +02:00
|
|
|
auto msgsexp{build_message_sexp(*msg, mi.doc_id(), qm)};
|
|
|
|
headers.add(std::move(msgsexp));
|
2021-10-21 18:19:21 +02:00
|
|
|
// we output up-to-batch-size lists of messages. It's much
|
|
|
|
// faster (on the emacs side) to handle such batches than single
|
|
|
|
// headers.
|
|
|
|
if (headers.size() % batch_size == 0) {
|
|
|
|
output_batch(std::move(headers));
|
|
|
|
headers.clear();
|
|
|
|
};
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
|
|
|
|
2021-10-21 18:19:21 +02:00
|
|
|
// remaining.
|
|
|
|
if (!headers.empty())
|
|
|
|
output_batch(std::move(headers));
|
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
return n;
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-11-07 17:33:01 +01:00
|
|
|
Server::Private::find_handler(const Command& cmd)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2022-11-07 17:33:01 +01:00
|
|
|
const auto q{cmd.string_arg(":query").value_or("")};
|
|
|
|
const auto threads{cmd.boolean_arg(":threads")};
|
2021-10-21 18:19:21 +02:00
|
|
|
// perhaps let mu4e set this as frame-lines of the appropriate frame.
|
2022-11-07 17:33:01 +01:00
|
|
|
const auto batch_size{cmd.number_arg(":batch-size").value_or(110)};
|
|
|
|
const auto sortfieldstr{cmd.symbol_arg(":sortfield").value_or("")};
|
|
|
|
const auto descending{cmd.boolean_arg(":descending")};
|
|
|
|
const auto maxnum{cmd.number_arg(":maxnum").value_or(-1) /*unlimited*/};
|
|
|
|
const auto skip_dups{cmd.boolean_arg(":skip-dups")};
|
|
|
|
const auto include_related{cmd.boolean_arg(":include-related")};
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-03-28 21:39:01 +02:00
|
|
|
auto sort_field = std::invoke([&]()->Option<Field>{
|
|
|
|
if (sortfieldstr.size() < 2)
|
|
|
|
return Nothing;
|
|
|
|
else
|
|
|
|
return field_from_name(sortfieldstr.substr(1));
|
|
|
|
});
|
|
|
|
if (!sort_field && !sortfieldstr.empty())
|
|
|
|
throw Error{Error::Code::InvalidArgument, "invalid sort field '%s'",
|
2022-03-04 23:35:26 +01:00
|
|
|
sortfieldstr.c_str()};
|
2021-10-20 11:18:15 +02:00
|
|
|
if (batch_size < 1)
|
|
|
|
throw Error{Error::Code::InvalidArgument, "invalid batch-size %d", batch_size};
|
|
|
|
|
2022-01-15 09:21:32 +01:00
|
|
|
auto qflags{QueryFlags::SkipUnreadable}; // don't show unreadables.
|
2021-10-20 11:18:15 +02:00
|
|
|
if (descending)
|
|
|
|
qflags |= QueryFlags::Descending;
|
|
|
|
if (skip_dups)
|
|
|
|
qflags |= QueryFlags::SkipDuplicates;
|
|
|
|
if (include_related)
|
|
|
|
qflags |= QueryFlags::IncludeRelated;
|
|
|
|
if (threads)
|
|
|
|
qflags |= QueryFlags::Threading;
|
|
|
|
|
2023-01-18 23:38:26 +01:00
|
|
|
StopWatch sw{format("%s (indexing: %s)", __func__,
|
|
|
|
indexer().is_running() ? "yes" : "no")};
|
|
|
|
|
2022-02-17 22:46:44 +01:00
|
|
|
std::lock_guard l{store_.lock()};
|
2022-03-20 13:12:41 +01:00
|
|
|
auto qres{store_.run_query(q, sort_field->id, qflags, maxnum)};
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!qres)
|
|
|
|
throw Error(Error::Code::Query, "failed to run query");
|
|
|
|
|
|
|
|
/* before sending new results, send an 'erase' message, so the frontend
|
|
|
|
* knows it should erase the headers buffer. this will ensure that the
|
|
|
|
* output of two finds will not be mixed. */
|
2022-11-07 17:33:01 +01:00
|
|
|
output_sexp(Sexp().put_props(":erase", Sexp::t()));
|
2021-10-20 11:18:15 +02:00
|
|
|
const auto foundnum{output_results(*qres, static_cast<size_t>(batch_size))};
|
2022-11-07 17:33:01 +01:00
|
|
|
output_sexp(Sexp().put_props(":found", foundnum));
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-11-07 17:33:01 +01:00
|
|
|
Server::Private::help_handler(const Command& cmd)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2022-11-07 17:33:01 +01:00
|
|
|
const auto command{cmd.symbol_arg(":command").value_or("")};
|
|
|
|
const auto full{cmd.bool_arg(":full").value_or(!command.empty())};
|
|
|
|
auto&& info_map{command_handler_.info_map()};
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
if (command.empty()) {
|
|
|
|
std::cout << ";; Commands are s-expressions of the form\n"
|
|
|
|
<< ";; (<command-name> :param1 val1 :param2 val2 ...)\n"
|
|
|
|
<< ";; For instance:\n;; (help :command quit)\n"
|
|
|
|
<< ";; to get detailed information about the 'quit'\n;;\n";
|
|
|
|
std::cout << ";; The following commands are available:\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> names;
|
2022-11-07 17:33:01 +01:00
|
|
|
for (auto&& name_cmd: info_map)
|
2021-10-20 11:18:15 +02:00
|
|
|
names.emplace_back(name_cmd.first);
|
2022-11-07 17:33:01 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
std::sort(names.begin(), names.end());
|
|
|
|
|
|
|
|
for (auto&& name : names) {
|
2022-11-07 17:33:01 +01:00
|
|
|
const auto& info{info_map.find(name)->second};
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
if (!command.empty() && name != command)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!command.empty())
|
|
|
|
std::cout << ";; "
|
|
|
|
<< format("%-10s -- %s\n", name.c_str(), info.docstring.c_str());
|
|
|
|
else
|
|
|
|
std::cout << ";; " << name.c_str() << " -- " << info.docstring.c_str()
|
|
|
|
<< '\n';
|
|
|
|
if (!full)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (auto&& argname : info.sorted_argnames()) {
|
|
|
|
const auto& arg{info.args.find(argname)};
|
|
|
|
std::cout << ";; "
|
|
|
|
<< format("%-17s : %-24s ",
|
2022-02-25 22:03:48 +01:00
|
|
|
arg->first.c_str(),
|
|
|
|
to_string(arg->second).c_str());
|
2021-10-20 11:18:15 +02:00
|
|
|
std::cout << " " << arg->second.docstring << "\n";
|
|
|
|
}
|
|
|
|
std::cout << ";;\n";
|
|
|
|
}
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
static Sexp
|
2021-10-20 11:18:15 +02:00
|
|
|
get_stats(const Indexer::Progress& stats, const std::string& state)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2022-11-07 17:33:01 +01:00
|
|
|
Sexp sexp;
|
2020-10-31 08:41:21 +01:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
sexp.put_props(
|
|
|
|
":info", "index"_sym,
|
|
|
|
":status", Sexp::Symbol(state),
|
|
|
|
":checked", static_cast<int>(stats.checked),
|
|
|
|
":updated", static_cast<int>(stats.updated),
|
|
|
|
":cleaned-up", static_cast<int>(stats.removed));
|
2020-10-31 08:41:21 +01:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
return sexp;
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-11-07 17:33:01 +01:00
|
|
|
Server::Private::index_handler(const Command& cmd)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
Mu::Indexer::Config conf{};
|
2022-11-07 17:33:01 +01:00
|
|
|
conf.cleanup = cmd.boolean_arg(":cleanup");
|
|
|
|
conf.lazy_check = cmd.boolean_arg(":lazy-check");
|
2021-11-11 20:58:23 +01:00
|
|
|
// ignore .noupdate with an empty store.
|
|
|
|
conf.ignore_noupdate = store().empty();
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
indexer().stop();
|
2022-02-03 21:59:47 +01:00
|
|
|
if (index_thread_.joinable())
|
|
|
|
index_thread_.join();
|
|
|
|
|
|
|
|
// start a background track.
|
|
|
|
index_thread_ = std::thread([this, conf = std::move(conf)] {
|
2023-01-18 23:38:26 +01:00
|
|
|
StopWatch sw{"indexing"};
|
2022-02-03 21:59:47 +01:00
|
|
|
indexer().start(conf);
|
|
|
|
while (indexer().is_running()) {
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
|
2022-05-06 21:07:15 +02:00
|
|
|
output_sexp(get_stats(indexer().progress(), "running"),
|
|
|
|
Server::OutputFlags::Flush);
|
2022-02-03 21:59:47 +01:00
|
|
|
}
|
2022-05-06 21:07:15 +02:00
|
|
|
output_sexp(get_stats(indexer().progress(), "complete"),
|
|
|
|
Server::OutputFlags::Flush);
|
2022-06-29 21:18:15 +02:00
|
|
|
store().commit(); /* ensure on-disk database is updated, too */
|
2022-02-03 21:59:47 +01:00
|
|
|
});
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-11-07 17:33:01 +01:00
|
|
|
Server::Private::mkdir_handler(const Command& cmd)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2022-11-07 17:33:01 +01:00
|
|
|
const auto path{cmd.string_arg(":path").value_or("<error>")};
|
|
|
|
if (auto&& res = maildir_mkdir(path, 0755, false); !res)
|
2022-02-16 22:03:48 +01:00
|
|
|
throw res.error();
|
2020-10-31 08:41:21 +01:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
output_sexp(Sexp().put_props(":info", "mkdir",
|
|
|
|
":message", format("%s has been created", path.c_str())));
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
2022-12-03 15:45:41 +01:00
|
|
|
void
|
2022-02-16 22:03:48 +01:00
|
|
|
Server::Private::perform_move(Store::Id docid,
|
2022-04-22 07:07:06 +02:00
|
|
|
const Message& msg,
|
2022-02-25 22:03:48 +01:00
|
|
|
const std::string& maildirarg,
|
2022-03-28 07:58:04 +02:00
|
|
|
Flags flags,
|
2022-02-25 22:03:48 +01:00
|
|
|
bool new_name,
|
|
|
|
bool no_view)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
bool different_mdir{};
|
|
|
|
auto maildir{maildirarg};
|
|
|
|
if (maildir.empty()) {
|
2022-04-22 07:07:06 +02:00
|
|
|
maildir = msg.maildir();
|
2021-10-20 11:18:15 +02:00
|
|
|
different_mdir = false;
|
|
|
|
} else /* are we moving to a different mdir, or is it just flags? */
|
2022-04-22 07:07:06 +02:00
|
|
|
different_mdir = maildir != msg.maildir();
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-12-03 15:45:41 +01:00
|
|
|
Store::MoveOptions move_opts{Store::MoveOptions::DupFlags};
|
|
|
|
if (new_name)
|
|
|
|
move_opts |= Store::MoveOptions::ChangeName;
|
|
|
|
|
|
|
|
/* note: we get back _all_ the messages that changed; the first is the
|
|
|
|
* primary mover; the rest (if present) are any dups affected */
|
|
|
|
const auto idmsgvec{store().move_message(docid, maildir, flags, move_opts)};
|
|
|
|
if (!idmsgvec)
|
|
|
|
throw idmsgvec.error();
|
|
|
|
|
|
|
|
for (auto&&[id, msg]: *idmsgvec) {
|
|
|
|
Sexp sexp{":update"_sym, build_message_sexp(idmsgvec->at(0).second, id, {})};
|
|
|
|
/* note, the :move t thing is a hint to the frontend that it
|
|
|
|
* could remove the particular header */
|
|
|
|
if (different_mdir)
|
|
|
|
sexp.put_props(":move", Sexp::t());
|
|
|
|
if (!no_view && id == docid)
|
|
|
|
sexp.put_props(":maybe-view", Sexp::t());
|
|
|
|
output_sexp(std::move(sexp));
|
|
|
|
}
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
2022-02-16 22:03:48 +01:00
|
|
|
|
2022-03-20 13:12:41 +01:00
|
|
|
static Flags
|
2022-04-22 07:07:06 +02:00
|
|
|
calculate_message_flags(const Message& msg, Option<std::string> flagopt)
|
2022-02-25 22:03:48 +01:00
|
|
|
{
|
2022-03-28 07:58:04 +02:00
|
|
|
const auto flags = std::invoke([&]()->Option<Flags>{
|
2022-02-16 22:03:48 +01:00
|
|
|
if (!flagopt)
|
2022-04-22 07:07:06 +02:00
|
|
|
return msg.flags();
|
2022-02-16 22:03:48 +01:00
|
|
|
else
|
2022-04-22 07:07:06 +02:00
|
|
|
return flags_from_expr(*flagopt, msg.flags());
|
2022-02-16 22:03:48 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!flags)
|
|
|
|
throw Error{Error::Code::InvalidArgument,
|
|
|
|
"invalid flags '%s'", flagopt.value_or("").c_str()};
|
|
|
|
else
|
|
|
|
return flags.value();
|
|
|
|
}
|
|
|
|
|
2022-12-03 15:45:41 +01:00
|
|
|
void
|
2022-03-28 07:58:04 +02:00
|
|
|
Server::Private::move_docid(Store::Id docid,
|
|
|
|
Option<std::string> flagopt,
|
|
|
|
bool new_name,
|
|
|
|
bool no_view)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
if (docid == Store::InvalidId)
|
|
|
|
throw Error{Error::Code::InvalidArgument, "invalid docid"};
|
|
|
|
|
|
|
|
auto msg{store_.find_message(docid)};
|
2022-04-22 07:07:06 +02:00
|
|
|
if (!msg)
|
|
|
|
throw Error{Error::Code::Store, "failed to get message from store"};
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-04-22 07:07:06 +02:00
|
|
|
const auto flags = calculate_message_flags(msg.value(), flagopt);
|
2022-12-03 15:45:41 +01:00
|
|
|
perform_move(docid, *msg, "", flags, new_name, no_view);
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 'move' moves a message to a different maildir and/or changes its
|
|
|
|
* flags. parameters are *either* a 'docid:' or 'msgid:' pointing to
|
|
|
|
* the message, a 'maildir:' for the target maildir, and a 'flags:'
|
|
|
|
* parameter for the new flags.
|
|
|
|
*/
|
|
|
|
void
|
2022-11-07 17:33:01 +01:00
|
|
|
Server::Private::move_handler(const Command& cmd)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2022-11-07 17:33:01 +01:00
|
|
|
auto maildir{cmd.string_arg(":maildir").value_or("")};
|
|
|
|
const auto flagopt{cmd.string_arg(":flags").value_or("")};
|
|
|
|
const auto rename{cmd.boolean_arg(":rename")};
|
|
|
|
const auto no_view{cmd.boolean_arg(":noupdate")};
|
|
|
|
const auto docids{determine_docids(store_, cmd)};
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
if (docids.size() > 1) {
|
|
|
|
if (!maildir.empty()) // ie. duplicate message-ids.
|
|
|
|
throw Mu::Error{Error::Code::Store,
|
2022-11-07 17:33:01 +01:00
|
|
|
"can't move multiple messages at the same time"};
|
2021-10-20 11:18:15 +02:00
|
|
|
// multi.
|
|
|
|
for (auto&& docid : docids)
|
2022-12-03 15:45:41 +01:00
|
|
|
move_docid(docid, flagopt, rename, no_view);
|
2021-10-20 11:18:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto docid{docids.at(0)};
|
2022-04-22 07:07:06 +02:00
|
|
|
auto msg = store().find_message(docid)
|
|
|
|
.or_else([]{throw Error{Error::Code::InvalidArgument,
|
|
|
|
"could not create message"};}).value();
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
/* if maildir was not specified, take the current one */
|
|
|
|
if (maildir.empty())
|
2022-04-22 07:07:06 +02:00
|
|
|
maildir = msg.maildir();
|
2021-10-20 11:18:15 +02:00
|
|
|
|
|
|
|
/* determine the real target flags, which come from the flags-parameter
|
|
|
|
* we received (ie., flagstr), if any, plus the existing message
|
|
|
|
* flags. */
|
2022-02-16 22:03:48 +01:00
|
|
|
const auto flags = calculate_message_flags(msg, flagopt);
|
2022-12-03 15:45:41 +01:00
|
|
|
perform_move(docid, msg, maildir, flags, rename, no_view);
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-11-07 17:33:01 +01:00
|
|
|
Server::Private::ping_handler(const Command& cmd)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
const auto storecount{store().size()};
|
|
|
|
if (storecount == (unsigned)-1)
|
|
|
|
throw Error{Error::Code::Store, "failed to read store"};
|
2023-01-07 09:29:57 +01:00
|
|
|
Sexp addrs;
|
|
|
|
for (auto&& addr : store().properties().personal_addresses)
|
|
|
|
addrs.add(addr);
|
|
|
|
|
|
|
|
output_sexp(Sexp()
|
|
|
|
.put_props(":pong", "mu")
|
|
|
|
.put_props(":props",
|
|
|
|
Sexp().put_props(
|
|
|
|
":version", VERSION,
|
|
|
|
":personal-addresses", std::move(addrs),
|
|
|
|
":database-path", store().properties().database_path,
|
|
|
|
":root-maildir", store().properties().root_maildir,
|
|
|
|
":doccount", storecount)));
|
|
|
|
}
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2023-01-07 09:29:57 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
Server::Private::queries_handler(const Command& cmd)
|
|
|
|
{
|
2022-11-07 17:33:01 +01:00
|
|
|
const auto queries{cmd.string_vec_arg(":queries")
|
|
|
|
.value_or(std::vector<std::string>{})};
|
2023-01-07 09:29:57 +01:00
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
Sexp qresults;
|
2021-10-20 11:18:15 +02:00
|
|
|
for (auto&& q : queries) {
|
2022-01-30 13:33:25 +01:00
|
|
|
const auto count{store_.count_query(q)};
|
2021-10-20 11:18:15 +02:00
|
|
|
const auto unreadq{format("flag:unread AND (%s)", q.c_str())};
|
2022-01-30 13:33:25 +01:00
|
|
|
const auto unread{store_.count_query(unreadq)};
|
2022-11-07 17:33:01 +01:00
|
|
|
qresults.add(Sexp().put_props(":query", q,
|
|
|
|
":count", count,
|
|
|
|
":unread", unread));
|
2021-10-20 11:18:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-07 09:29:57 +01:00
|
|
|
output_sexp(Sexp(":queries"_sym, std::move(qresults)));
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
2023-01-07 09:29:57 +01:00
|
|
|
|
2020-10-31 08:41:21 +01:00
|
|
|
void
|
2022-11-07 17:33:01 +01:00
|
|
|
Server::Private::quit_handler(const Command& cmd)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
keep_going_ = false;
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-11-07 17:33:01 +01:00
|
|
|
Server::Private::remove_handler(const Command& cmd)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2022-11-07 17:33:01 +01:00
|
|
|
const auto docid{cmd.number_arg(":docid").value_or(0)};
|
2021-10-20 11:18:15 +02:00
|
|
|
const auto path{path_from_docid(store(), docid)};
|
2020-10-31 08:41:21 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (::unlink(path.c_str()) != 0 && errno != ENOENT)
|
|
|
|
throw Error(Error::Code::File,
|
2022-02-25 22:03:48 +01:00
|
|
|
"could not delete %s: %s",
|
|
|
|
path.c_str(),
|
|
|
|
g_strerror(errno));
|
2020-10-31 08:41:21 +01:00
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
if (!store().remove_message(path))
|
|
|
|
g_warning("failed to remove message @ %s (%d) from store", path.c_str(), docid);
|
2022-12-03 15:45:41 +01:00
|
|
|
output_sexp(Sexp().put_props(":remove", docid)); // act as if it worked.
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-11-07 17:33:01 +01:00
|
|
|
Server::Private::sent_handler(const Command& cmd)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2022-11-07 17:33:01 +01:00
|
|
|
const auto path{cmd.string_arg(":path").value_or("")};
|
2022-04-22 07:07:06 +02:00
|
|
|
const auto docid = store().add_message(path);
|
|
|
|
if (!docid)
|
2022-11-01 17:37:31 +01:00
|
|
|
throw Error{Error::Code::Store, "failed to add path: %s",
|
|
|
|
docid.error().what()};
|
2022-11-07 17:33:01 +01:00
|
|
|
output_sexp(Sexp().put_props(
|
|
|
|
":sent", Sexp::t(),
|
|
|
|
":path", path,
|
|
|
|
":docid", docid.value()));
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
2022-12-03 15:45:41 +01:00
|
|
|
void
|
|
|
|
Server::Private::view_mark_as_read(Store::Id docid, Message&& msg, bool rename)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2022-04-28 22:01:16 +02:00
|
|
|
|
2022-12-03 15:45:41 +01:00
|
|
|
auto move_res = std::invoke([&]()->Result<Store::IdMessageVec> {
|
|
|
|
const auto newflags{flags_from_delta_expr("+S-u-N", msg.flags())};
|
|
|
|
if (!newflags || msg.flags() == *newflags) {
|
|
|
|
/* case 1: message was already read; do nothing */
|
|
|
|
Store::IdMessageVec idmvec;
|
|
|
|
idmvec.emplace_back(docid, std::move(msg));
|
|
|
|
return idmvec;
|
|
|
|
} else {
|
|
|
|
/* case 2: move message (and possibly dups) */
|
|
|
|
Store::MoveOptions move_opts{Store::MoveOptions::DupFlags};
|
|
|
|
if (rename)
|
|
|
|
move_opts |= Store::MoveOptions::ChangeName;
|
|
|
|
return store().move_message(docid, {}, newflags, move_opts);
|
|
|
|
}
|
|
|
|
});
|
2022-06-03 21:03:58 +02:00
|
|
|
|
2022-12-03 15:45:41 +01:00
|
|
|
if (!move_res)
|
|
|
|
throw move_res.error();
|
2022-06-29 07:59:40 +02:00
|
|
|
|
2022-12-03 15:45:41 +01:00
|
|
|
for (auto&& [id, msg]: move_res.value())
|
|
|
|
output_sexp(Sexp{id == docid ? ":view"_sym : ":update"_sym,
|
|
|
|
build_message_sexp(msg, id, {})});
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-11-07 17:33:01 +01:00
|
|
|
Server::Private::view_handler(const Command& cmd)
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2023-01-18 23:38:26 +01:00
|
|
|
StopWatch sw{format("%s (indexing: %s)", __func__,
|
|
|
|
indexer().is_running() ? "yes" : "no")};
|
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
const auto mark_as_read{cmd.boolean_arg(":mark-as-read")};
|
2022-02-25 22:03:48 +01:00
|
|
|
/* for now, do _not_ rename, as it seems to confuse mbsync */
|
|
|
|
const auto rename{false};
|
|
|
|
//const auto rename{get_bool_or(params, ":rename")};
|
|
|
|
|
2022-11-07 17:33:01 +01:00
|
|
|
const auto docids{determine_docids(store(), cmd)};
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-02-13 11:03:16 +01:00
|
|
|
if (docids.empty())
|
|
|
|
throw Error{Error::Code::Store, "failed to find message for view"};
|
|
|
|
const auto docid{docids.at(0)};
|
2022-04-22 07:07:06 +02:00
|
|
|
auto msg = store().find_message(docid)
|
|
|
|
.or_else([]{throw Error{Error::Code::Store,
|
|
|
|
"failed to find message for view"};}).value();
|
2021-10-20 11:18:15 +02:00
|
|
|
|
2022-12-03 15:45:41 +01:00
|
|
|
/* if the message should not be marked-as-read, we're done. */
|
|
|
|
if (!mark_as_read)
|
2022-11-07 17:33:01 +01:00
|
|
|
output_sexp(Sexp().put_props(":view", build_message_sexp(msg, docid, {})));
|
2022-12-03 15:45:41 +01:00
|
|
|
else
|
|
|
|
view_mark_as_read(docid, std::move(msg), rename);
|
|
|
|
/* otherwise, mark message and and possible dups as read */
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|
|
|
|
|
2021-10-20 11:18:15 +02:00
|
|
|
Server::Server(Store& store, Server::Output output)
|
|
|
|
: priv_{std::make_unique<Private>(store, output)}
|
2022-06-29 07:59:40 +02:00
|
|
|
{}
|
2020-10-31 08:41:21 +01:00
|
|
|
|
|
|
|
Server::~Server() = default;
|
|
|
|
|
|
|
|
bool
|
2021-10-20 11:18:15 +02:00
|
|
|
Server::invoke(const std::string& expr) noexcept
|
2020-10-31 08:41:21 +01:00
|
|
|
{
|
2021-10-20 11:18:15 +02:00
|
|
|
return priv_->invoke(expr);
|
2020-10-31 08:41:21 +01:00
|
|
|
}
|