*: update for for mu-maildir changes

Update the dependencies.
This commit is contained in:
Dirk-Jan C. Binnema
2022-02-16 23:03:48 +02:00
parent abd2a4a8f1
commit 4eabf1a64a
7 changed files with 424 additions and 401 deletions

View File

@ -413,11 +413,11 @@ Mu::mu_msg_get_date(MuMsg* self)
return (time_t)get_num_field(self, MU_MSG_FIELD_ID_DATE); return (time_t)get_num_field(self, MU_MSG_FIELD_ID_DATE);
} }
MuFlags MessageFlags
Mu::mu_msg_get_flags(MuMsg* self) Mu::mu_msg_get_flags(MuMsg* self)
{ {
g_return_val_if_fail(self, MU_FLAG_NONE); g_return_val_if_fail(self, MessageFlags::None);
return (MuFlags)get_num_field(self, MU_MSG_FIELD_ID_FLAGS); return static_cast<MessageFlags>(get_num_field(self, MU_MSG_FIELD_ID_FLAGS));
} }
size_t size_t
@ -742,140 +742,47 @@ Mu::mu_msg_is_readable(MuMsg* self)
return access(mu_msg_get_path(self), R_OK) == 0 ? TRUE : FALSE; return access(mu_msg_get_path(self), R_OK) == 0 ? TRUE : FALSE;
} }
/* we need do to determine the
* /home/foo/Maildir/bar
* from the /bar
* that we got
*/
static char*
get_target_mdir(MuMsg* msg, const char* target_maildir, GError** err)
{
char * rootmaildir, *rv;
const char* maildir;
gboolean not_top_level;
/* maildir is the maildir stored in the message, e.g. '/foo' */
maildir = mu_msg_get_maildir(msg);
if (!maildir) {
mu_util_g_set_error(err, MU_ERROR_GMIME, "message without maildir");
return NULL;
}
/* the 'rootmaildir' is the filesystem path from root to
* maildir, ie. /home/user/Maildir/foo */
rootmaildir = mu_maildir_get_maildir_from_path(mu_msg_get_path(msg));
if (!rootmaildir) {
mu_util_g_set_error(err, MU_ERROR_GMIME, "cannot determine maildir");
return NULL;
}
/* we do a sanity check: verify that that maildir is a suffix of
* rootmaildir;*/
not_top_level = TRUE;
if (!g_str_has_suffix(rootmaildir, maildir) &&
/* special case for the top-level '/' maildir, and
* remember not_top_level */
(not_top_level = (g_strcmp0(maildir, "/") != 0))) {
g_set_error(err,
MU_ERROR_DOMAIN,
MU_ERROR_FILE,
"path is '%s', but maildir is '%s' ('%s')",
rootmaildir,
mu_msg_get_maildir(msg),
mu_msg_get_path(msg));
g_free(rootmaildir);
return NULL;
}
/* if we're not at the top-level, remove the final '/' from
* the rootmaildir */
if (not_top_level)
rootmaildir[strlen(rootmaildir) - strlen(mu_msg_get_maildir(msg))] = '\0';
rv = g_strconcat(rootmaildir, target_maildir, NULL);
g_free(rootmaildir);
return rv;
}
/* /*
* move a msg to another maildir, trying to maintain 'integrity', * move a msg to another maildir, trying to maintain 'integrity',
* ie. msg in 'new/' will go to new/, one in cur/ goes to cur/. be * ie. msg in 'new/' will go to new/, one in cur/ goes to cur/. be
* super-paranoid here... * super-paranoid here...
*/ */
gboolean bool
Mu::mu_msg_move_to_maildir(MuMsg* self, Mu::mu_msg_move_to_maildir(MuMsg* self,
const char* maildir, const std::string& root_maildir_path,
MuFlags flags, const std::string& target_maildir,
gboolean ignore_dups, MessageFlags flags,
gboolean new_name, bool ignore_dups,
GError** err) bool new_name,
GError** err)
{ {
char* newfullpath; g_return_val_if_fail(self, false);
char* targetmdir;
g_return_val_if_fail(self, FALSE);
g_return_val_if_fail(maildir, FALSE); /* i.e. "/inbox" */
/* targetmdir is the full path to maildir, i.e., const auto srcpath{mu_msg_get_path(self)};
* /home/foo/Maildir/inbox */ const auto dstpath{mu_maildir_determine_target(srcpath,
targetmdir = get_target_mdir(self, maildir, err); root_maildir_path,
if (!targetmdir) target_maildir,
return FALSE; flags,
new_name)};
if (!dstpath)
return false;
newfullpath = mu_maildir_move_message(mu_msg_get_path(self), if (!mu_maildir_move_message(srcpath, *dstpath, ignore_dups))
targetmdir, return false;
flags,
ignore_dups,
new_name,
err);
if (!newfullpath) {
g_free(targetmdir);
return FALSE;
}
/* clear the old backends */ /* clear the old backends */
mu_msg_doc_destroy(self->_doc); mu_msg_doc_destroy(self->_doc);
self->_doc = NULL; self->_doc = NULL;
mu_msg_file_destroy(self->_file); mu_msg_file_destroy(self->_file);
/* and create a new one */ /* and create a new one */
self->_file = mu_msg_file_new(newfullpath, maildir, err); self->_file = mu_msg_file_new(dstpath->c_str(), target_maildir.c_str(), err);
g_free(targetmdir);
g_free(newfullpath);
return self->_file ? TRUE : FALSE; return !!self->_file;
} }
/*
* Rename a message-file, keeping the same flags. This is useful for tricking
* some 3rd party progs such as mbsync
*/
gboolean
Mu::mu_msg_tickle(MuMsg* self, GError** err)
{
g_return_val_if_fail(self, FALSE);
return mu_msg_move_to_maildir(self,
mu_msg_get_maildir(self),
mu_msg_get_flags(self),
FALSE,
TRUE,
err);
}
const char*
Mu::mu_str_flags_s(MuFlags flags)
{
return mu_flags_to_str_s(flags, MU_FLAG_TYPE_ANY);
}
char*
Mu::mu_str_flags(MuFlags flags)
{
return g_strdup(mu_str_flags_s(flags));
}
static void static void
cleanup_contact(char* contact) cleanup_contact(char* contact)

View File

@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "mu-message-flags.hh"
#include "mu-msg-fields.h" #include "mu-msg-fields.h"
#include "mu-msg.hh" #include "mu-msg.hh"
#include "mu-server.hh" #include "mu-server.hh"
@ -120,13 +121,15 @@ private:
const Option<QueryMatch&> qm, const Option<QueryMatch&> qm,
MuMsgOptions opts) const; MuMsgOptions opts) const;
Sexp::List move_docid(Store::Id docid, const std::string& flagstr, bool new_name, bool no_view); Sexp::List move_docid(Store::Id docid, std::optional<std::string> flagstr,
Sexp::List perform_move(Store::Id docid, bool new_name, bool no_view);
MuMsg* msg,
const std::string& maildirarg, Sexp::List perform_move(Store::Id docid,
MuFlags flags, MuMsg* msg,
bool new_name, const std::string& maildirarg,
bool no_view); MessageFlags flags,
bool new_name,
bool no_view);
bool maybe_mark_as_read(MuMsg* msg, Store::Id docid, bool rename); bool maybe_mark_as_read(MuMsg* msg, Store::Id docid, bool rename);
bool maybe_mark_msgid_as_read(const char* msgid, bool rename); bool maybe_mark_msgid_as_read(const char* msgid, bool rename);
@ -799,10 +802,8 @@ void
Server::Private::mkdir_handler(const Parameters& params) Server::Private::mkdir_handler(const Parameters& params)
{ {
const auto path{get_string_or(params, ":path")}; const auto path{get_string_or(params, ":path")};
if (auto&& res = mu_maildir_mkdir(path, 0755, FALSE); !res)
GError* gerr{}; throw res.error();
if (!mu_maildir_mkdir(path.c_str(), 0755, FALSE, &gerr))
throw Error{Error::Code::File, &gerr, "failed to create maildir"};
Sexp::List lst; Sexp::List lst;
lst.add_prop(":info", Sexp::make_string("mkdir")); lst.add_prop(":info", Sexp::make_string("mkdir"));
@ -811,31 +812,13 @@ Server::Private::mkdir_handler(const Parameters& params)
output_sexp(std::move(lst)); output_sexp(std::move(lst));
} }
static MuFlags
get_flags(const std::string& path, const std::string& flagstr)
{
if (flagstr.empty())
return MU_FLAG_NONE; /* ie., ignore flags */
else {
/* if there's a '+' or '-' sign in the string, it must
* be a flag-delta */
if (strstr(flagstr.c_str(), "+") || strstr(flagstr.c_str(), "-")) {
auto oldflags = mu_maildir_get_flags_from_path(path.c_str());
return mu_flags_from_str_delta(flagstr.c_str(), oldflags, MU_FLAG_TYPE_ANY);
} else
return mu_flags_from_str(flagstr.c_str(),
MU_FLAG_TYPE_ANY,
TRUE /*ignore invalid*/);
}
}
Sexp::List Sexp::List
Server::Private::perform_move(Store::Id docid, Server::Private::perform_move(Store::Id docid,
MuMsg* msg, MuMsg* msg,
const std::string& maildirarg, const std::string& maildirarg,
MuFlags flags, MessageFlags flags,
bool new_name, bool new_name,
bool no_view) bool no_view)
{ {
bool different_mdir{}; bool different_mdir{};
auto maildir{maildirarg}; auto maildir{maildirarg};
@ -846,7 +829,10 @@ Server::Private::perform_move(Store::Id docid,
different_mdir = maildir != mu_msg_get_maildir(msg); different_mdir = maildir != mu_msg_get_maildir(msg);
GError* gerr{}; GError* gerr{};
if (!mu_msg_move_to_maildir(msg, maildir.c_str(), flags, TRUE, new_name, &gerr))
if (!mu_msg_move_to_maildir(msg,
store().properties().root_maildir,
maildir, flags, true, new_name, &gerr))
throw Error{Error::Code::File, &gerr, "failed to move message"}; throw Error{Error::Code::File, &gerr, "failed to move message"};
/* after mu_msg_move_to_maildir, path will be the *new* path, and flags and maildir /* after mu_msg_move_to_maildir, path will be the *new* path, and flags and maildir
@ -866,11 +852,30 @@ Server::Private::perform_move(Store::Id docid,
return seq; return seq;
} }
static MessageFlags
calculate_message_flags(MuMsg* msg, std::optional<std::string> flagopt)
{
const auto flags = std::invoke([&]()->std::optional<MessageFlags>{
auto msgflags{mu_msg_get_flags(msg)};
if (!flagopt)
return mu_msg_get_flags(msg);
else
return message_flags_from_expr(*flagopt, msgflags);
});
if (!flags)
throw Error{Error::Code::InvalidArgument,
"invalid flags '%s'", flagopt.value_or("").c_str()};
else
return flags.value();
}
Sexp::List Sexp::List
Server::Private::move_docid(Store::Id docid, Server::Private::move_docid(Store::Id docid,
const std::string& flagstr, std::optional<std::string> flagopt,
bool new_name, bool new_name,
bool no_view) bool no_view)
{ {
if (docid == Store::InvalidId) if (docid == Store::InvalidId)
throw Error{Error::Code::InvalidArgument, "invalid docid"}; throw Error{Error::Code::InvalidArgument, "invalid docid"};
@ -880,13 +885,7 @@ Server::Private::move_docid(Store::Id docid,
if (!msg) if (!msg)
throw Error{Error::Code::Store, "failed to get message from store"}; throw Error{Error::Code::Store, "failed to get message from store"};
const auto flags = flagstr.empty() ? mu_msg_get_flags(msg) const auto flags = calculate_message_flags(msg, flagopt);
: get_flags(mu_msg_get_path(msg), flagstr);
if (flags == MU_FLAG_INVALID)
throw Error{Error::Code::InvalidArgument,
"invalid flags '%s'",
flagstr.c_str()};
auto lst = perform_move(docid, msg, "", flags, new_name, no_view); auto lst = perform_move(docid, msg, "", flags, new_name, no_view);
mu_msg_unref(msg); mu_msg_unref(msg);
return lst; return lst;
@ -911,7 +910,7 @@ void
Server::Private::move_handler(const Parameters& params) Server::Private::move_handler(const Parameters& params)
{ {
auto maildir{get_string_or(params, ":maildir")}; auto maildir{get_string_or(params, ":maildir")};
const auto flagstr{get_string_or(params, ":flags")}; const auto flagopt{get_string(params, ":flags")};
const auto rename{get_bool_or(params, ":rename")}; const auto rename{get_bool_or(params, ":rename")};
const auto no_view{get_bool_or(params, ":noupdate")}; const auto no_view{get_bool_or(params, ":noupdate")};
const auto docids{determine_docids(store_, params)}; const auto docids{determine_docids(store_, params)};
@ -922,7 +921,8 @@ Server::Private::move_handler(const Parameters& params)
"can't move multiple messages at the same time"}; "can't move multiple messages at the same time"};
// multi. // multi.
for (auto&& docid : docids) for (auto&& docid : docids)
output_sexp(move_docid(docid, flagstr, rename, no_view)); output_sexp(move_docid(docid, flagopt,
rename, no_view));
return; return;
} }
auto docid{docids.at(0)}; auto docid{docids.at(0)};
@ -939,17 +939,7 @@ Server::Private::move_handler(const Parameters& params)
/* determine the real target flags, which come from the flags-parameter /* determine the real target flags, which come from the flags-parameter
* we received (ie., flagstr), if any, plus the existing message * we received (ie., flagstr), if any, plus the existing message
* flags. */ * flags. */
MuFlags flags{}; const auto flags = calculate_message_flags(msg, flagopt);
if (!flagstr.empty())
flags = get_flags(mu_msg_get_path(msg), flagstr.c_str());
else
flags = mu_msg_get_flags(msg);
if (flags == MU_FLAG_INVALID) {
mu_msg_unref(msg);
throw Error{Error::Code::InvalidArgument, "invalid flags"};
}
try { try {
output_sexp(perform_move(docid, msg, maildir, flags, rename, no_view)); output_sexp(perform_move(docid, msg, maildir, flags, rename, no_view));
} catch (...) { } catch (...) {
@ -967,7 +957,7 @@ Server::Private::ping_handler(const Parameters& params)
if (storecount == (unsigned)-1) if (storecount == (unsigned)-1)
throw Error{Error::Code::Store, "failed to read store"}; throw Error{Error::Code::Store, "failed to read store"};
const auto queries = get_string_vec(params, ":queries"); const auto queries{get_string_vec(params, ":queries")};
Sexp::List qresults; Sexp::List qresults;
for (auto&& q : queries) { for (auto&& q : queries) {
const auto count{store_.count_query(q)}; const auto count{store_.count_query(q)};
@ -1053,16 +1043,17 @@ Server::Private::maybe_mark_as_read(MuMsg* msg, Store::Id docid, bool rename)
throw Error{Error::Code::Store, "missing message"}; throw Error{Error::Code::Store, "missing message"};
const auto oldflags{mu_msg_get_flags(msg)}; const auto oldflags{mu_msg_get_flags(msg)};
const auto newflags{get_flags(mu_msg_get_path(msg), "+S-u-N")}; const auto newflags{message_flags_from_delta_expr("+S-u-N", oldflags)};
if (oldflags == newflags) if (!newflags || oldflags == *newflags)
return false; // nothing to do. return false; // nothing to do.
GError* gerr{}; GError* gerr{};
if (!mu_msg_move_to_maildir(msg, if (!mu_msg_move_to_maildir(msg,
store().properties().root_maildir,
mu_msg_get_maildir(msg), mu_msg_get_maildir(msg),
newflags, *newflags,
TRUE, true,
rename ? TRUE : FALSE, rename,
&gerr)) &gerr))
throw Error{Error::Code::File, &gerr, "failed to move message"}; throw Error{Error::Code::File, &gerr, "failed to move message"};

View File

@ -1,5 +1,5 @@
/* /*
** Copyright (C) 2008-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2008-2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
** This program is free software; you can redistribute it and/or modify it ** 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 ** under the terms of the GNU General Public License as published by the
@ -17,16 +17,13 @@
** **
*/ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /*HAVE_CONFIG_H*/
#include <glib.h> #include <glib.h>
#include <glib/gstdio.h> #include <glib/gstdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <vector>
#include "test-mu-common.hh" #include "test-mu-common.hh"
#include "mu-maildir.hh" #include "mu-maildir.hh"
@ -44,7 +41,7 @@ test_mu_maildir_mkdir_01(void)
tmpdir = test_mu_common_get_random_tmpdir(); tmpdir = test_mu_common_get_random_tmpdir();
mdir = g_strdup_printf("%s%c%s", tmpdir, G_DIR_SEPARATOR, "cuux"); mdir = g_strdup_printf("%s%c%s", tmpdir, G_DIR_SEPARATOR, "cuux");
g_assert_cmpuint(mu_maildir_mkdir(mdir, 0755, FALSE, NULL), ==, TRUE); g_assert_true(!!mu_maildir_mkdir(mdir, 0755, FALSE));
for (i = 0; i != G_N_ELEMENTS(subs); ++i) { for (i = 0; i != G_N_ELEMENTS(subs); ++i) {
gchar* dir; gchar* dir;
@ -73,7 +70,7 @@ test_mu_maildir_mkdir_02(void)
tmpdir = test_mu_common_get_random_tmpdir(); tmpdir = test_mu_common_get_random_tmpdir();
mdir = g_strdup_printf("%s%c%s", tmpdir, G_DIR_SEPARATOR, "cuux"); mdir = g_strdup_printf("%s%c%s", tmpdir, G_DIR_SEPARATOR, "cuux");
g_assert_cmpuint(mu_maildir_mkdir(mdir, 0755, TRUE, NULL), ==, TRUE); g_assert_true(!!mu_maildir_mkdir(mdir, 0755, TRUE));
for (i = 0; i != G_N_ELEMENTS(subs); ++i) { for (i = 0; i != G_N_ELEMENTS(subs); ++i) {
gchar* dir; gchar* dir;
@ -112,7 +109,7 @@ test_mu_maildir_mkdir_03(void)
} }
/* this should still work */ /* this should still work */
g_assert_cmpuint(mu_maildir_mkdir(mdir, 0755, FALSE, NULL), ==, TRUE); g_assert_true(!!mu_maildir_mkdir(mdir, 0755, FALSE));
for (i = 0; i != G_N_ELEMENTS(subs); ++i) { for (i = 0; i != G_N_ELEMENTS(subs); ++i) {
gchar* dir; gchar* dir;
@ -149,9 +146,9 @@ test_mu_maildir_mkdir_04(void)
} }
/* this should fail now, because cur is not read/writable */ /* this should fail now, because cur is not read/writable */
g_assert_cmpuint(mu_maildir_mkdir(mdir, 0755, FALSE, NULL), if (geteuid() != 0)
==, g_assert_false(!!mu_maildir_mkdir(mdir, 0755, false));
(geteuid() == 0 ? TRUE : FALSE));
g_free(tmpdir); g_free(tmpdir);
g_free(mdir); g_free(mdir);
} }
@ -168,30 +165,34 @@ test_mu_maildir_mkdir_05(void)
/* this must fail */ /* this must fail */
g_test_log_set_fatal_handler((GTestLogFatalFunc)ignore_error, NULL); g_test_log_set_fatal_handler((GTestLogFatalFunc)ignore_error, NULL);
g_assert_cmpuint(mu_maildir_mkdir(NULL, 0755, TRUE, NULL), ==, FALSE); g_assert_false(!!mu_maildir_mkdir({}, 0755, true));
} }
static void static void
test_mu_maildir_get_flags_from_path(void) test_mu_maildir_flags_from_path(void)
{ {
int i; int i;
struct { struct {
const char* path; const char* path;
MuFlags flags; MessageFlags flags;
} paths[] = {{"/home/foo/Maildir/test/cur/123456:2,FSR", } paths[] = {
(MuFlags)(MU_FLAG_REPLIED | MU_FLAG_SEEN | MU_FLAG_FLAGGED)}, {"/home/foo/Maildir/test/cur/123456:2,FSR",
{"/home/foo/Maildir/test/new/123456", MU_FLAG_NEW}, (MessageFlags::Replied | MessageFlags::Seen | MessageFlags::Flagged)},
{/* NOTE: when in new/, the :2,.. stuff is ignored */ {"/home/foo/Maildir/test/new/123456", MessageFlags::New},
"/home/foo/Maildir/test/new/123456:2,FR", {/* NOTE: when in new/, the :2,.. stuff is ignored */
MU_FLAG_NEW}, "/home/foo/Maildir/test/new/123456:2,FR",
{"/home/foo/Maildir/test/cur/123456:2,DTP", MessageFlags::New},
(MuFlags)(MU_FLAG_DRAFT | MU_FLAG_TRASHED | MU_FLAG_PASSED)}, {"/home/foo/Maildir/test/cur/123456:2,DTP",
{"/home/foo/Maildir/test/cur/123456:2,S", MU_FLAG_SEEN}}; (MessageFlags::Draft | MessageFlags::Trashed | MessageFlags::Passed)},
{"/home/foo/Maildir/test/cur/123456:2,S", MessageFlags::Seen}};
for (i = 0; i != G_N_ELEMENTS(paths); ++i) { for (i = 0; i != G_N_ELEMENTS(paths); ++i) {
MuFlags flags; auto res{mu_maildir_flags_from_path(paths[i].path)};
flags = mu_maildir_get_flags_from_path(paths[i].path); g_assert_true(!!res);
g_assert_cmpuint(flags, ==, paths[i].flags); if (g_test_verbose())
g_print("%s -> <%s>\n", paths[i].path,
message_flags_to_string(res.value()).c_str());
g_assert_true(res.value() == paths[i].flags);
} }
} }
@ -205,166 +206,242 @@ assert_matches_regexp(const char* str, const char* rx)
} }
} }
static void static void
test_mu_maildir_get_new_path_new(void) test_determine_target_ok(void)
{ {
int i; struct TestCase {
std::string old_path;
std::string root_maildir;
std::string target_maildir;
MessageFlags new_flags;
bool new_name;
std::string expected;
};
const std::vector<TestCase> testcases = {
TestCase{ /* change some flags */
"/home/foo/Maildir/test/cur/123456:2,FR",
"/home/foo/Maildir",
{},
MessageFlags::Seen | MessageFlags::Passed,
false,
"/home/foo/Maildir/test/cur/123456:2,PS"
},
struct { TestCase{ /* from cur -> new */
const char* oldpath; "/home/foo/Maildir/test/cur/123456:2,FR",
MuFlags flags; "/home/foo/Maildir",
const char* newpath; {},
} paths[] = {{"/home/foo/Maildir/test/cur/123456:2,FR", MessageFlags::New,
MU_FLAG_REPLIED, false,
"/home/foo/Maildir/test/cur/123456:2,R"}, "/home/foo/Maildir/test/new/123456"
{"/home/foo/Maildir/test/cur/123456:2,FR", },
MU_FLAG_NEW,
"/home/foo/Maildir/test/new/123456"},
{"/home/foo/Maildir/test/new/123456:2,FR",
(MuFlags)(MU_FLAG_SEEN | MU_FLAG_REPLIED),
"/home/foo/Maildir/test/cur/123456:2,RS"},
{"/home/foo/Maildir/test/new/1313038887_0.697:2,",
(MuFlags)(MU_FLAG_SEEN | MU_FLAG_FLAGGED | MU_FLAG_PASSED),
"/home/foo/Maildir/test/cur/1313038887_0.697:2,FPS"},
{"/home/djcb/Maildir/trash/new/1312920597.2206_16.cthulhu",
MU_FLAG_SEEN,
"/home/djcb/Maildir/trash/cur/1312920597.2206_16.cthulhu:2,S"}};
for (i = 0; i != G_N_ELEMENTS(paths); ++i) { TestCase{ /* from new->cur */
char *str, *newbase; "/home/foo/Maildir/test/cur/123456",
str = mu_maildir_get_new_path(paths[i].oldpath, NULL, paths[i].flags, TRUE); "/home/foo/Maildir",
newbase = g_path_get_basename(str); {},
assert_matches_regexp(newbase, MessageFlags::Seen | MessageFlags::Flagged,
"\\d+\\." false,
"[[:xdigit:]]{16}\\." "/home/foo/Maildir/test/cur/123456:2,FS"
"[[:alnum:]][[:alnum:]-]+(:2,.*)?"); },
g_free(newbase);
g_free(str); TestCase{ /* change maildir */
"/home/foo/Maildir/test/cur/123456:2,FR",
"/home/foo/Maildir",
"/test2",
MessageFlags::Flagged | MessageFlags::Replied,
false,
"/home/foo/Maildir/test2/cur/123456:2,FR"
},
TestCase{ /* remove all flags */
"/home/foo/Maildir/test/new/123456",
"/home/foo/Maildir",
{},
MessageFlags::None,
false,
"/home/foo/Maildir/test/cur/123456:2,"
},
};
for (auto&& testcase: testcases) {
const auto res = mu_maildir_determine_target(
testcase.old_path,
testcase.root_maildir,
testcase.target_maildir,
testcase.new_flags,
testcase.new_name);
g_assert_true(!!res);
g_assert_cmpstr(testcase.expected.c_str(), ==,
res.value().c_str());
} }
} }
static void
test_mu_maildir_get_new_path_01(void)
{
int i;
struct {
const char* oldpath;
MuFlags flags;
const char* newpath;
} paths[] = {{"/home/foo/Maildir/test/cur/123456:2,FR",
MU_FLAG_REPLIED,
"/home/foo/Maildir/test/cur/123456:2,R"},
{"/home/foo/Maildir/test/cur/123456:2,FR",
MU_FLAG_NEW,
"/home/foo/Maildir/test/new/123456"},
{"/home/foo/Maildir/test/new/123456:2,FR",
(MuFlags)(MU_FLAG_SEEN | MU_FLAG_REPLIED),
"/home/foo/Maildir/test/cur/123456:2,RS"},
{"/home/foo/Maildir/test/new/1313038887_0.697:2,",
(MuFlags)(MU_FLAG_SEEN | MU_FLAG_FLAGGED | MU_FLAG_PASSED),
"/home/foo/Maildir/test/cur/1313038887_0.697:2,FPS"},
{"/home/djcb/Maildir/trash/new/1312920597.2206_16.cthulhu",
MU_FLAG_SEEN,
"/home/djcb/Maildir/trash/cur/1312920597.2206_16.cthulhu:2,S"}};
for (i = 0; i != G_N_ELEMENTS(paths); ++i) {
gchar* str;
str = mu_maildir_get_new_path(paths[i].oldpath, NULL, paths[i].flags, FALSE);
g_assert_cmpstr(str, ==, paths[i].newpath);
g_free(str);
}
}
static void
test_mu_maildir_get_new_path_02(void)
{
int i;
struct {
const char* oldpath;
MuFlags flags;
const char* targetdir;
const char* newpath;
} paths[] = {{"/home/foo/Maildir/test/cur/123456:2,FR",
MU_FLAG_REPLIED,
"/home/foo/Maildir/blabla",
"/home/foo/Maildir/blabla/cur/123456:2,R"},
{"/home/foo/Maildir/test/cur/123456:2,FR",
MU_FLAG_NEW,
"/home/bar/Maildir/coffee",
"/home/bar/Maildir/coffee/new/123456"},
{"/home/foo/Maildir/test/new/123456",
(MuFlags)(MU_FLAG_SEEN | MU_FLAG_REPLIED),
"/home/cuux/Maildir/tea",
"/home/cuux/Maildir/tea/cur/123456:2,RS"},
{"/home/foo/Maildir/test/new/1313038887_0.697:2,",
(MuFlags)(MU_FLAG_SEEN | MU_FLAG_FLAGGED | MU_FLAG_PASSED),
"/home/boy/Maildir/stuff",
"/home/boy/Maildir/stuff/cur/1313038887_0.697:2,FPS"}};
for (i = 0; i != G_N_ELEMENTS(paths); ++i) {
gchar* str;
str = mu_maildir_get_new_path(paths[i].oldpath,
paths[i].targetdir,
paths[i].flags,
FALSE);
g_assert_cmpstr(str, ==, paths[i].newpath);
g_free(str);
}
}
static void
test_mu_maildir_get_new_path_custom(void)
{
int i;
struct {
const char* oldpath;
MuFlags flags;
const char* targetdir;
const char* newpath;
} paths[] = {{"/home/foo/Maildir/test/cur/123456:2,FR",
MU_FLAG_REPLIED,
"/home/foo/Maildir/blabla",
"/home/foo/Maildir/blabla/cur/123456:2,R"},
{"/home/foo/Maildir/test/cur/123456:2,hFeRllo123",
MU_FLAG_FLAGGED,
"/home/foo/Maildir/blabla",
"/home/foo/Maildir/blabla/cur/123456:2,Fhello123"},
{"/home/foo/Maildir/test/cur/123456:2,abc",
MU_FLAG_PASSED,
"/home/foo/Maildir/blabla",
"/home/foo/Maildir/blabla/cur/123456:2,Pabc"}};
for (i = 0; i != G_N_ELEMENTS(paths); ++i) {
gchar* str;
str = mu_maildir_get_new_path(paths[i].oldpath,
paths[i].targetdir,
paths[i].flags,
FALSE);
g_assert_cmpstr(str, ==, paths[i].newpath);
g_free(str);
}
}
static void
test_mu_maildir_get_maildir_from_path(void)
{
unsigned u;
struct { // static void
const char *path, *exp; // test_mu_maildir_determine_target(void)
} cases[] = {{"/home/foo/Maildir/test/cur/123456:2,FR", "/home/foo/Maildir/test"}, // {
{"/home/foo/Maildir/lala/new/1313038887_0.697:2,", "/home/foo/Maildir/lala"}}; // int i;
for (u = 0; u != G_N_ELEMENTS(cases); ++u) { // struct {
gchar* mdir; // std::string oldpath;
mdir = mu_maildir_get_maildir_from_path(cases[u].path); // MessageFlags flags;
g_assert_cmpstr(mdir, ==, cases[u].exp); // std::string newpath;
g_free(mdir); // } paths[] = {{"/home/foo/Maildir/test/cur/123456:2,FR",
} // MessageFlags::Replied,
} // "/home/foo/Maildir/test/cur/123456:2,R"},
// {"/home/foo/Maildir/test/cur/123456:2,FR",
// MessageFlags::New,
// "/home/foo/Maildir/test/new/123456"},
// {"/home/foo/Maildir/test/new/123456:2,FR",
// (MessageFlags::Seen | MessageFlags::Replied),
// "/home/foo/Maildir/test/cur/123456:2,RS"},
// {"/home/foo/Maildir/test/new/1313038887_0.697:2,",
// (MessageFlags::Seen | MessageFlags::Flagged | MessageFlags::Passed),
// "/home/foo/Maildir/test/cur/1313038887_0.697:2,FPS"},
// {"/home/djcb/Maildir/trash/new/1312920597.2206_16.cthulhu",
// MessageFlags::Seen,
// "/home/djcb/Maildir/trash/cur/1312920597.2206_16.cthulhu:2,S"}};
// for (i = 0; i != G_N_ELEMENTS(paths); ++i) {
// const auto res{mu_maildir_determine_target(paths[i].oldpath,
// "/home/foo/Maildir",
// {},
// paths[i].flags, false)};
// g_assert_true(res && res.value() == paths[i].newpath);
// char *newbase = g_path_get_basename(newpath->c_str());
// assert_matches_regexp(newbase,
// "\\d+\\."
// "[[:xdigit:]]{16}\\."
// "[[:alnum:]][[:alnum:]-]+(:2,.*)?");
// g_free(newbase);
// }
// }
// static void
// test_mu_maildir_get_new_path_01(void)
// {
// struct {
// std::string oldpath;
// MessageFlags flags;
// std::string newpath;
// } paths[] = {{"/home/foo/Maildir/test/cur/123456:2,FR",
// MessageFlags::Replied,
// "/home/foo/Maildir/test/cur/123456:2,R"},
// {"/home/foo/Maildir/test/cur/123456:2,FR",
// MessageFlags::New,
// "/home/foo/Maildir/test/new/123456"},
// {"/home/foo/Maildir/test/new/123456:2,FR",
// (MessageFlags::Seen | MessageFlags::Replied),
// "/home/foo/Maildir/test/cur/123456:2,RS"},
// {"/home/foo/Maildir/test/new/1313038887_0.697:2,",
// (MessageFlags::Seen | MessageFlags::Flagged | MessageFlags::Passed),
// "/home/foo/Maildir/test/cur/1313038887_0.697:2,FPS"},
// {"/home/djcb/Maildir/trash/new/1312920597.2206_16.cthulhu",
// MessageFlags::Seen,
// "/home/djcb/Maildir/trash/cur/1312920597.2206_16.cthulhu:2,S"}};
// for (int i = 0; i != G_N_ELEMENTS(paths); ++i) {
// const auto newpath{mu_maildir_determine_target(
// paths[i].oldpath,
// "/home/foo/maildir",
// {}, paths[i].flags, false)};
// g_assert_true(newpath.has_value());
// g_assert_true(*newpath == paths[i].newpath);
// }
// }
// static void
// test_mu_maildir_get_new_path_02(void)
// {
// struct {
// std::string oldpath;
// MessageFlags flags;
// std::string targetdir;
// std::string newpath;
// } paths[] = {{"/home/foo/Maildir/test/cur/123456:2,FR",
// MessageFlags::Replied,
// "/home/foo/Maildir/blabla",
// "/home/foo/Maildir/blabla/cur/123456:2,R"},
// {"/home/foo/Maildir/test/cur/123456:2,FR",
// MessageFlags::New,
// "/home/bar/Maildir/coffee",
// "/home/bar/Maildir/coffee/new/123456"},
// {"/home/foo/Maildir/test/new/123456",
// (MessageFlags::Seen | MessageFlags::Replied),
// "/home/cuux/Maildir/tea",
// "/home/cuux/Maildir/tea/cur/123456:2,RS"},
// {"/home/foo/Maildir/test/new/1313038887_0.697:2,",
// (MessageFlags::Seen | MessageFlags::Flagged | MessageFlags::Passed),
// "/home/boy/Maildir/stuff",
// "/home/boy/Maildir/stuff/cur/1313038887_0.697:2,FPS"}};
// for (int i = 0; i != G_N_ELEMENTS(paths); ++i) {
// auto newpath{mu_maildir_determine_target(paths[i].oldpath,
// paths[i].targetdir,
// paths[i].flags,
// false)};
// g_assert_true(newpath.has_value());
// g_assert_true(*newpath == paths[i].newpath);
// }
// }
// static void
// test_mu_maildir_get_new_path_custom(void)
// {
// struct {
// std::string oldpath;
// MessageFlags flags;
// std::string targetdir;
// std::string newpath;
// } paths[] = {{"/home/foo/Maildir/test/cur/123456:2,FR",
// MessageFlags::Replied,
// "/home/foo/Maildir/blabla",
// "/home/foo/Maildir/blabla/cur/123456:2,R"},
// {"/home/foo/Maildir/test/cur/123456:2,hFeRllo123",
// MessageFlags::Flagged,
// "/home/foo/Maildir/blabla",
// "/home/foo/Maildir/blabla/cur/123456:2,Fhello123"},
// {"/home/foo/Maildir/test/cur/123456:2,abc",
// MessageFlags::Passed,
// "/home/foo/Maildir/blabla",
// "/home/foo/Maildir/blabla/cur/123456:2,Pabc"}};
// for (int i = 0; i != G_N_ELEMENTS(paths); ++i) {
// auto newpath{mu_maildir_get_new_path(paths[i].oldpath,
// paths[i].targetdir,
// paths[i].flags,
// FALSE)};
// g_assert_true(newpath);
// g_assert_true(*newpath == paths[i].newpath);
// }
// }
// static void
// test_mu_maildir_from_path(void)
// {
// unsigned u;
// struct {
// std::string path, exp;
// } cases[] = {{"/home/foo/Maildir/test/cur/123456:2,FR", "/home/foo/Maildir/test"},
// {"/home/foo/Maildir/lala/new/1313038887_0.697:2,", "/home/foo/Maildir/lala"}};
// for (u = 0; u != G_N_ELEMENTS(cases); ++u) {
// auto mdir{mu_maildir_from_path(cases[u].path)};
// g_assert_true(mdir.has_value());
// g_assert_true(*mdir == cases[u].exp);
// }
// }
int int
main(int argc, char* argv[]) main(int argc, char* argv[])
@ -378,19 +455,27 @@ main(int argc, char* argv[])
g_test_add_func("/mu-maildir/mu-maildir-mkdir-04", test_mu_maildir_mkdir_04); g_test_add_func("/mu-maildir/mu-maildir-mkdir-04", test_mu_maildir_mkdir_04);
g_test_add_func("/mu-maildir/mu-maildir-mkdir-05", test_mu_maildir_mkdir_05); g_test_add_func("/mu-maildir/mu-maildir-mkdir-05", test_mu_maildir_mkdir_05);
/* get/set flags */ g_test_add_func("/mu-maildir/mu-maildir-flags-from-path",
g_test_add_func("/mu-maildir/mu-maildir-get-new-path-new", test_mu_maildir_flags_from_path);
test_mu_maildir_get_new_path_new);
g_test_add_func("/mu-maildir/mu-maildir-get-new-path-01", test_mu_maildir_get_new_path_01);
g_test_add_func("/mu-maildir/mu-maildir-get-new-path-02", test_mu_maildir_get_new_path_02);
g_test_add_func("/mu-maildir/mu-maildir-get-new-path-custom",
test_mu_maildir_get_new_path_custom);
g_test_add_func("/mu-maildir/mu-maildir-get-flags-from-path",
test_mu_maildir_get_flags_from_path);
g_test_add_func("/mu-maildir/mu-maildir-get-maildir-from-path", g_test_add_func("/mu-maildir/mu-maildir-determine-target-ok",
test_mu_maildir_get_maildir_from_path); test_determine_target_ok);
// /* get/set flags */
// g_test_add_func("/mu-maildir/mu-maildir-get-new-path-new",
// test_mu_maildir_get_new_path_new);
// g_test_add_func("/mu-maildir/mu-maildir-get-new-path-01", test_mu_maildir_get_new_path_01);
// g_test_add_func("/mu-maildir/mu-maildir-get-new-path-02", test_mu_maildir_get_new_path_02);
// g_test_add_func("/mu-maildir/mu-maildir-get-new-path-custom",
// test_mu_maildir_get_new_path_custom);
// g_test_add_func("/mu-maildir/mu-maildir-get-flags-from-path",
// test_mu_maildir_get_flags_from_path);
// g_test_add_func("/mu-maildir/mu-maildir-from-path",
// test_mu_maildir_from_path);
g_log_set_handler( g_log_set_handler(
NULL, NULL,

View File

@ -126,48 +126,48 @@ wrong_type(Sexp::Type expected, Sexp::Type got)
to_string(got).c_str()); to_string(got).c_str());
} }
const std::string& std::optional<std::string>
Command::get_string_or(const Parameters& params, const std::string& argname, const std::string& alt) Command::get_string(const Parameters& params, const std::string& argname)
{ {
const auto it = find_param_node(params, argname); const auto it = find_param_node(params, argname);
if (it == params.end() || it->is_nil()) if (it == params.end() || it->is_nil())
return alt; return std::nullopt;
else if (!it->is_string()) else if (!it->is_string())
throw wrong_type(Sexp::Type::String, it->type()); throw wrong_type(Sexp::Type::String, it->type());
else else
return it->value(); return it->value();
} }
const std::string& std::optional<std::string>
Command::get_symbol_or(const Parameters& params, const std::string& argname, const std::string& alt) Command::get_symbol(const Parameters& params, const std::string& argname)
{ {
const auto it = find_param_node(params, argname); const auto it = find_param_node(params, argname);
if (it == params.end() || it->is_nil()) if (it == params.end() || it->is_nil())
return alt; return std::nullopt;
else if (!it->is_symbol()) else if (!it->is_symbol())
throw wrong_type(Sexp::Type::Symbol, it->type()); throw wrong_type(Sexp::Type::Symbol, it->type());
else else
return it->value(); return it->value();
} }
int std::optional<int>
Command::get_int_or(const Parameters& params, const std::string& argname, int alt) Command::get_int(const Parameters& params, const std::string& argname)
{ {
const auto it = find_param_node(params, argname); const auto it = find_param_node(params, argname);
if (it == params.end() || it->is_nil()) if (it == params.end() || it->is_nil())
return alt; return std::nullopt;
else if (!it->is_number()) else if (!it->is_number())
throw wrong_type(Sexp::Type::Number, it->type()); throw wrong_type(Sexp::Type::Number, it->type());
else else
return ::atoi(it->value().c_str()); return ::atoi(it->value().c_str());
} }
bool std::optional<bool>
Command::get_bool_or(const Parameters& params, const std::string& argname, bool alt) Command::get_bool(const Parameters& params, const std::string& argname)
{ {
const auto it = find_param_node(params, argname); const auto it = find_param_node(params, argname);
if (it == params.end()) if (it == params.end())
return alt; return std::nullopt;
else if (!it->is_symbol()) else if (!it->is_symbol())
throw wrong_type(Sexp::Type::Symbol, it->type()); throw wrong_type(Sexp::Type::Symbol, it->type());
else else

View File

@ -26,6 +26,7 @@
#include <unordered_map> #include <unordered_map>
#include <functional> #include <functional>
#include <algorithm> #include <algorithm>
#include <optional>
#include "utils/mu-error.hh" #include "utils/mu-error.hh"
#include "utils/mu-sexp.hh" #include "utils/mu-sexp.hh"
@ -61,15 +62,37 @@ using ArgMap = std::unordered_map<std::string, ArgInfo>;
// The parameters to a Handler. // The parameters to a Handler.
using Parameters = Sexp::Seq; using Parameters = Sexp::Seq;
int get_int_or(const Parameters& parms, const std::string& argname, int alt = 0); std::optional<int> get_int(const Parameters& parms, const std::string& argname);
bool get_bool_or(const Parameters& parms, const std::string& argname, bool alt = false); std::optional<bool> get_bool(const Parameters& parms, const std::string& argname);
const std::string& std::optional<std::string> get_string(const Parameters& parms, const std::string& argname);
get_string_or(const Parameters& parms, const std::string& argname, const std::string& alt = ""); std::optional<std::string> get_symbol(const Parameters& parms, const std::string& argname);
const std::string&
get_symbol_or(const Parameters& parms, const std::string& argname, const std::string& alt = "nil");
std::vector<std::string> get_string_vec(const Parameters& params, const std::string& argname); std::vector<std::string> get_string_vec(const Parameters& params, const std::string& argname);
/*
* backward compat
*/
static inline int
get_int_or(const Parameters& parms, const std::string& arg, int alt = 0) {
return get_int(parms, arg).value_or(alt);
}
static inline bool
get_bool_or(const Parameters& parms, const std::string& arg, bool alt = false) {
return get_bool(parms, arg).value_or(alt);
}
static inline std::string
get_string_or(const Parameters& parms, const std::string& arg, const std::string& alt = ""){
return get_string(parms, arg).value_or(alt);
}
static inline std::string
get_symbol_or(const Parameters& parms, const std::string& arg, const std::string& alt = "nil") {
return get_symbol(parms, arg).value_or(alt);
}
// A handler function // A handler function
using Handler = std::function<void(const Parameters&)>; using Handler = std::function<void(const Parameters&)>;

View File

@ -202,20 +202,16 @@ prepare_links(const MuConfig* opts, GError** err)
{ {
/* note, mu_maildir_mkdir simply ignores whatever part of the /* note, mu_maildir_mkdir simply ignores whatever part of the
* mail dir already exists */ * mail dir already exists */
if (auto&& res = mu_maildir_mkdir(opts->linksdir, 0700, true); !res) {
if (!mu_maildir_mkdir(opts->linksdir, 0700, TRUE, err)) { res.error().fill_g_error(err);
mu_util_g_set_error(err,
MU_ERROR_FILE_CANNOT_MKDIR,
"error creating %s",
opts->linksdir);
return FALSE; return FALSE;
} }
if (opts->clearlinks && !mu_maildir_clear_links(opts->linksdir, err)) { if (!opts->clearlinks)
mu_util_g_set_error(err, return TRUE;
MU_ERROR_FILE,
"error clearing links under %s", if (auto&& res = mu_maildir_clear_links(opts->linksdir); !res) {
opts->linksdir); res.error().fill_g_error(err);
return FALSE; return FALSE;
} }
@ -229,8 +225,12 @@ output_link(MuMsg* msg, const OutputInfo& info, const MuConfig* opts, GError** e
return prepare_links(opts, err); return prepare_links(opts, err);
else if (info.footer) else if (info.footer)
return true; return true;
if (auto&& res = mu_maildir_link(
return mu_maildir_link(mu_msg_get_path(msg), opts->linksdir, err); mu_msg_get_path(msg), opts->linksdir); !res) {
res.error().fill_g_error(err);
return FALSE;
}
return TRUE;
} }
static void static void
@ -293,6 +293,18 @@ field_string_list(MuMsg* msg, MuMsgFieldId mfid)
return NULL; return NULL;
} }
/* ugly... for backward compat */
static const char*
flags_s(MessageFlags flags)
{
static char buf[64];
const auto flagstr{message_flags_to_string(flags)};
::strncpy(buf, flagstr.c_str(), sizeof(buf) - 1);
return buf;
}
static const char* static const char*
display_field(MuMsg* msg, MuMsgFieldId mfid) display_field(MuMsg* msg, MuMsgFieldId mfid)
{ {
@ -305,14 +317,13 @@ display_field(MuMsg* msg, MuMsgFieldId mfid)
return str ? str : ""; return str ? str : "";
} }
case MU_MSG_FIELD_TYPE_INT: case MU_MSG_FIELD_TYPE_INT:
if (mfid == MU_MSG_FIELD_ID_PRIO) { if (mfid == MU_MSG_FIELD_ID_PRIO) {
const auto val = static_cast<char>(mu_msg_get_field_numeric(msg, mfid)); const auto val = static_cast<char>(mu_msg_get_field_numeric(msg, mfid));
const auto prio = message_priority_from_char(val); const auto prio = message_priority_from_char(val);
return message_priority_name_c_str(prio); return message_priority_name_c_str(prio);
} else if (mfid == MU_MSG_FIELD_ID_FLAGS) { } else if (mfid == MU_MSG_FIELD_ID_FLAGS) {
val = mu_msg_get_field_numeric(msg, mfid); val = mu_msg_get_field_numeric(msg, mfid);
return mu_str_flags_s((MuFlags)val); return flags_s(static_cast<MessageFlags>(val));
} else /* as string */ } else /* as string */
return mu_msg_get_field_string(msg, mfid); return mu_msg_get_field_string(msg, mfid);

View File

@ -34,7 +34,7 @@
#include "mu-maildir.hh" #include "mu-maildir.hh"
#include "mu-contacts.hh" #include "mu-contacts.hh"
#include "mu-runtime.hh" #include "mu-runtime.hh"
#include "mu-flags.hh" #include "mu-message-flags.hh"
#include "utils/mu-util.h" #include "utils/mu-util.h"
#include "utils/mu-str.h" #include "utils/mu-str.h"
@ -122,7 +122,7 @@ body_or_summary(MuMsg* msg, const MuConfig* opts)
color = !opts->nocolor; color = !opts->nocolor;
body = mu_msg_get_body_text(msg, (MuMsgOptions)my_opts); body = mu_msg_get_body_text(msg, (MuMsgOptions)my_opts);
if (!body) { if (!body) {
if (mu_msg_get_flags(msg) & MU_FLAG_ENCRYPTED) { if (any_of(mu_msg_get_flags(msg) & MessageFlags::Encrypted)) {
color_maybe(MU_COLOR_CYAN); color_maybe(MU_COLOR_CYAN);
g_print("[No body found; " g_print("[No body found; "
"message has encrypted parts]\n"); "message has encrypted parts]\n");
@ -268,9 +268,15 @@ cmd_mkdir(const MuConfig* opts, GError** err)
return MU_ERROR_IN_PARAMETERS; return MU_ERROR_IN_PARAMETERS;
} }
for (i = 1; opts->params[i]; ++i) for (i = 1; opts->params[i]; ++i) {
if (!mu_maildir_mkdir(opts->params[i], opts->dirmode, FALSE, err)) if (auto&& res{mu_maildir_mkdir(opts->params[i],
return err && *err ? (MuError)(*err)->code : MU_ERROR_FILE_CANNOT_MKDIR; opts->dirmode, FALSE)}; !res) {
g_set_error(err, MU_ERROR_DOMAIN, MU_ERROR_FILE,
"%s", res.error().what());
return MU_ERROR_FILE_CANNOT_MKDIR;
}
}
return MU_OK; return MU_OK;
} }