2010-08-20 20:38:54 +02:00
|
|
|
/*
|
2023-01-14 16:11:36 +01:00
|
|
|
** Copyright (C) 2010-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2010-08-20 20:38:54 +02: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-06-08 22:04:05 +02:00
|
|
|
#include <iostream>
|
2020-06-27 10:50:02 +02:00
|
|
|
#include <iomanip>
|
2020-06-08 22:04:05 +02:00
|
|
|
|
2010-08-22 21:50:19 +02:00
|
|
|
#include <stdlib.h>
|
2011-05-25 21:02:24 +02:00
|
|
|
#include <stdio.h>
|
2011-08-02 07:14:19 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2010-08-22 21:50:19 +02:00
|
|
|
|
2022-11-16 21:51:15 +01:00
|
|
|
#include "mu-options.hh"
|
2020-06-08 22:04:05 +02:00
|
|
|
#include "mu-cmd.hh"
|
2020-11-28 09:16:43 +01:00
|
|
|
#include "mu-maildir.hh"
|
2022-02-19 21:08:54 +01:00
|
|
|
#include "mu-contacts-cache.hh"
|
2022-03-20 13:12:41 +01:00
|
|
|
#include "message/mu-message.hh"
|
2022-04-09 18:23:32 +02:00
|
|
|
#include "message/mu-mime-object.hh"
|
2019-12-16 20:44:35 +01:00
|
|
|
|
2020-06-08 22:04:05 +02:00
|
|
|
#include "utils/mu-error.hh"
|
2023-04-29 21:58:55 +02:00
|
|
|
#include "utils/mu-utils-file.hh"
|
2022-04-09 18:23:02 +02:00
|
|
|
#include "utils/mu-utils.hh"
|
2020-06-08 22:04:05 +02:00
|
|
|
|
2022-04-30 00:10:31 +02:00
|
|
|
#include <thirdparty/tabulate.hpp>
|
|
|
|
|
2020-11-28 09:16:43 +01:00
|
|
|
using namespace Mu;
|
|
|
|
|
2023-07-03 17:44:13 +02:00
|
|
|
|
|
|
|
static Result<void>
|
|
|
|
cmd_fields(const Options& opts)
|
|
|
|
{
|
2023-07-05 22:10:13 +02:00
|
|
|
mu_printerrln("the 'mu fields' command has been superseded by 'mu info'; try:\n"
|
2023-07-06 06:39:55 +02:00
|
|
|
" mu info fields\n");
|
2023-07-03 17:44:13 +02:00
|
|
|
return Ok();
|
|
|
|
}
|
|
|
|
|
2023-07-05 22:10:13 +02:00
|
|
|
|
2022-04-28 21:47:00 +02:00
|
|
|
static Result<void>
|
2022-11-16 21:51:15 +01:00
|
|
|
cmd_find(const Options& opts)
|
2020-06-08 22:04:05 +02:00
|
|
|
{
|
2022-11-16 21:51:15 +01:00
|
|
|
auto store{Store::make(opts.runtime_path(RuntimePath::XapianDb))};
|
2022-05-09 19:58:35 +02:00
|
|
|
if (!store)
|
|
|
|
return Err(store.error());
|
|
|
|
else
|
|
|
|
return mu_cmd_find(*store, opts);
|
2020-06-08 22:04:05 +02:00
|
|
|
}
|
2012-07-17 18:17:49 +02:00
|
|
|
|
2023-07-05 22:10:13 +02:00
|
|
|
|
2011-08-30 20:57:59 +02:00
|
|
|
static void
|
2021-10-20 11:18:15 +02:00
|
|
|
show_usage(void)
|
2011-08-30 20:57:59 +02:00
|
|
|
{
|
2023-07-05 22:10:13 +02:00
|
|
|
mu_println("usage: mu command [options] [parameters]");
|
|
|
|
mu_println("where command is one of index, find, cfind, view, mkdir, "
|
|
|
|
"extract, add, remove, script, verify or server");
|
|
|
|
mu_println("see the mu, mu-<command> or mu-easy manpages for "
|
|
|
|
"more information");
|
2011-08-30 20:57:59 +02:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2019-07-28 13:25:19 +02:00
|
|
|
|
2022-11-16 21:51:15 +01:00
|
|
|
using ReadOnlyStoreFunc = std::function<Result<void>(const Store&, const Options&)>;
|
|
|
|
using WritableStoreFunc = std::function<Result<void>(Store&, const Options&)>;
|
2022-05-12 07:48:28 +02:00
|
|
|
|
|
|
|
static Result<void>
|
2022-11-16 21:51:15 +01:00
|
|
|
with_readonly_store(const ReadOnlyStoreFunc& func, const Options& opts)
|
2020-02-04 20:14:30 +01:00
|
|
|
{
|
2022-11-16 21:51:15 +01:00
|
|
|
auto store{Store::make(opts.runtime_path(RuntimePath::XapianDb))};
|
2022-05-09 19:58:35 +02:00
|
|
|
if (!store)
|
2022-05-12 07:48:28 +02:00
|
|
|
return Err(store.error());
|
2022-05-09 19:58:35 +02:00
|
|
|
|
2022-05-12 07:48:28 +02:00
|
|
|
return func(store.value(), opts);
|
2020-02-04 20:14:30 +01:00
|
|
|
}
|
|
|
|
|
2022-05-12 07:48:28 +02:00
|
|
|
static Result<void>
|
2022-11-16 21:51:15 +01:00
|
|
|
with_writable_store(const WritableStoreFunc func, const Options& opts)
|
2011-08-30 20:57:59 +02:00
|
|
|
{
|
2022-11-16 21:51:15 +01:00
|
|
|
auto store{Store::make(opts.runtime_path(RuntimePath::XapianDb),
|
2022-05-09 19:58:35 +02:00
|
|
|
Store::Options::Writable)};
|
|
|
|
if (!store)
|
2022-05-12 07:48:28 +02:00
|
|
|
return Err(store.error());
|
2011-09-03 09:44:36 +02:00
|
|
|
|
2022-05-12 07:48:28 +02:00
|
|
|
return func(store.value(), opts);
|
2011-09-03 09:44:36 +02:00
|
|
|
}
|
|
|
|
|
2022-05-12 07:48:28 +02:00
|
|
|
Result<void>
|
2022-11-16 21:51:15 +01:00
|
|
|
Mu::mu_cmd_execute(const Options& opts) try {
|
2012-09-14 09:06:14 +02:00
|
|
|
|
2022-11-16 21:51:15 +01:00
|
|
|
if (!opts.sub_command)
|
|
|
|
return Err(Error::Code::Internal, "missing subcommand");
|
2012-07-19 19:45:06 +02:00
|
|
|
|
2022-11-16 21:51:15 +01:00
|
|
|
switch (*opts.sub_command) {
|
|
|
|
case Options::SubCommand::Help:
|
|
|
|
return Ok(); /* already handled in mu-options.cc */
|
2022-04-09 18:23:32 +02:00
|
|
|
/*
|
|
|
|
* no store needed
|
|
|
|
*/
|
2022-11-16 21:51:15 +01:00
|
|
|
case Options::SubCommand::Fields:
|
2023-07-03 17:44:13 +02:00
|
|
|
return cmd_fields(opts);
|
2022-11-16 21:51:15 +01:00
|
|
|
case Options::SubCommand::Mkdir:
|
2023-07-01 17:21:41 +02:00
|
|
|
return mu_cmd_mkdir(opts);
|
2022-11-16 21:51:15 +01:00
|
|
|
case Options::SubCommand::Script:
|
2022-05-12 07:48:28 +02:00
|
|
|
return mu_cmd_script(opts);
|
2022-11-16 21:51:15 +01:00
|
|
|
case Options::SubCommand::View:
|
2023-07-01 17:21:41 +02:00
|
|
|
return mu_cmd_view(opts);
|
2022-11-16 21:51:15 +01:00
|
|
|
case Options::SubCommand::Verify:
|
2023-07-01 17:21:41 +02:00
|
|
|
return mu_cmd_verify(opts);
|
2022-11-16 21:51:15 +01:00
|
|
|
case Options::SubCommand::Extract:
|
2022-05-12 07:48:28 +02:00
|
|
|
return mu_cmd_extract(opts);
|
2022-04-09 18:23:02 +02:00
|
|
|
/*
|
|
|
|
* read-only store
|
|
|
|
*/
|
2020-02-06 19:22:43 +01:00
|
|
|
|
2022-11-16 21:51:15 +01:00
|
|
|
case Options::SubCommand::Cfind:
|
2022-05-12 07:48:28 +02:00
|
|
|
return with_readonly_store(mu_cmd_cfind, opts);
|
2022-11-16 21:51:15 +01:00
|
|
|
case Options::SubCommand::Find:
|
2022-05-12 07:48:28 +02:00
|
|
|
return cmd_find(opts);
|
2022-11-16 21:51:15 +01:00
|
|
|
case Options::SubCommand::Info:
|
2023-07-01 17:21:41 +02:00
|
|
|
return with_readonly_store(mu_cmd_info, opts);
|
2020-02-06 19:22:43 +01:00
|
|
|
|
2022-04-09 18:23:32 +02:00
|
|
|
/* writable store */
|
2020-02-04 20:14:30 +01:00
|
|
|
|
2022-11-16 21:51:15 +01:00
|
|
|
case Options::SubCommand::Add:
|
2023-07-01 17:21:41 +02:00
|
|
|
return with_writable_store(mu_cmd_add, opts);
|
2022-11-16 21:51:15 +01:00
|
|
|
case Options::SubCommand::Remove:
|
2023-07-01 17:21:41 +02:00
|
|
|
return with_writable_store(mu_cmd_remove, opts);
|
2023-09-13 22:54:45 +02:00
|
|
|
case Options::SubCommand::Move:
|
|
|
|
return with_writable_store(mu_cmd_move, opts);
|
2022-11-16 21:51:15 +01:00
|
|
|
case Options::SubCommand::Index:
|
2022-05-12 07:48:28 +02:00
|
|
|
return with_writable_store(mu_cmd_index, opts);
|
2020-02-06 19:22:43 +01:00
|
|
|
|
|
|
|
/* commands instantiate store themselves */
|
2022-11-16 21:51:15 +01:00
|
|
|
case Options::SubCommand::Init:
|
2023-07-01 17:21:41 +02:00
|
|
|
return mu_cmd_init(opts);
|
2022-11-16 21:51:15 +01:00
|
|
|
case Options::SubCommand::Server:
|
2022-05-12 07:48:28 +02:00
|
|
|
return mu_cmd_server(opts);
|
2020-02-06 19:22:43 +01:00
|
|
|
|
2022-05-12 07:48:28 +02:00
|
|
|
default:
|
|
|
|
show_usage();
|
|
|
|
return Ok();
|
2011-08-30 20:57:59 +02:00
|
|
|
}
|
2012-09-09 13:07:34 +02:00
|
|
|
|
2020-06-08 22:04:05 +02:00
|
|
|
} catch (const Mu::Error& er) {
|
2022-05-12 07:48:28 +02:00
|
|
|
return Err(er);
|
|
|
|
} catch (const std::runtime_error& re) {
|
2023-07-05 22:10:13 +02:00
|
|
|
return Err(Error::Code::Internal, "runtime-error: {}", re.what());
|
2022-05-12 07:48:28 +02:00
|
|
|
} catch (const std::exception& ex) {
|
2023-07-05 22:10:13 +02:00
|
|
|
return Err(Error::Code::Internal, "error: {}", ex.what());
|
2020-06-08 22:04:05 +02:00
|
|
|
} catch (...) {
|
2022-05-12 07:48:28 +02:00
|
|
|
return Err(Error::Code::Internal, "caught exception");
|
2011-08-30 20:57:59 +02:00
|
|
|
}
|