lib: clean up mu_msg_to_sexp API

move out the QueryMatch
This commit is contained in:
Dirk-Jan C. Binnema
2021-01-29 22:39:21 +02:00
parent e5a52e45fd
commit 72fdfec3eb
5 changed files with 89 additions and 68 deletions

View File

@ -283,13 +283,6 @@ make_part_types (MuMsgPartType ptype)
return Sexp::make_list(std::move(list)); return Sexp::make_list(std::move(list));
} }
static Sexp
symbol_t()
{
return Sexp::make_symbol("t");
}
static void static void
each_part (MuMsg *msg, MuMsgPart *part, PartInfo *pinfo) each_part (MuMsg *msg, MuMsgPart *part, PartInfo *pinfo)
{ {
@ -316,7 +309,7 @@ each_part (MuMsg *msg, MuMsgPart *part, PartInfo *pinfo)
g_free (fname); g_free (fname);
if (mu_msg_part_maybe_attachment (part)) if (mu_msg_part_maybe_attachment (part))
partlist.add_prop(":attachment", symbol_t()); partlist.add_prop(":attachment", Sexp::make_symbol("t"));
const auto cid{ mu_msg_part_get_content_id(part)}; const auto cid{ mu_msg_part_get_content_id(part)};
if (cid) if (cid)
partlist.add_prop(":cid", Sexp::make_string(cid)); partlist.add_prop(":cid", Sexp::make_string(cid));
@ -341,32 +334,6 @@ add_parts (Sexp::List& items, MuMsg *msg, MuMsgOptions opts)
items.add_prop(":parts", Sexp::make_list(std::move(pinfo.parts))); items.add_prop(":parts", Sexp::make_list(std::move(pinfo.parts)));
} }
static void
add_thread_info (Sexp::List& items, const QueryMatch& qmatch)
{
Sexp::List info;
info.add_prop(":path", Sexp::make_string(qmatch.thread_path));
info.add_prop(":level", Sexp::make_number(qmatch.thread_level));
if (any_of(qmatch.flags & QueryMatch::Flags::Root))
info.add_prop( ":root", symbol_t());
if (any_of(qmatch.flags & QueryMatch::Flags::Related))
info.add_prop( ":related", symbol_t());
if (any_of(qmatch.flags & QueryMatch::Flags::First))
info.add_prop( ":first-child", symbol_t());
if (any_of(qmatch.flags & QueryMatch::Flags::Last))
info.add_prop( ":last-child", symbol_t());
if (any_of(qmatch.flags & QueryMatch::Flags::Orphan))
info.add_prop( ":empty-parent", symbol_t());
if (any_of(qmatch.flags & QueryMatch::Flags::Duplicate))
info.add_prop( ":duplicate", symbol_t());
if (any_of(qmatch.flags & QueryMatch::Flags::HasChild))
info.add_prop( ":has-child", symbol_t());
items.add_prop(":thread", Sexp::make_list(std::move(info)));
}
static void static void
add_message_file_parts (Sexp::List& items, MuMsg *msg, MuMsgOptions opts) add_message_file_parts (Sexp::List& items, MuMsg *msg, MuMsgOptions opts)
@ -429,22 +396,18 @@ add_tags (Sexp::List& items, MuMsg *msg)
} }
Mu::Sexp Mu::Sexp::List
Mu::msg_to_sexp (MuMsg *msg, unsigned docid, Mu::msg_to_sexp_list (MuMsg *msg, unsigned docid, MuMsgOptions opts)
const Option<QueryMatch&> qm, MuMsgOptions opts)
{ {
g_return_val_if_fail (msg, Sexp::make_symbol("error")); g_return_val_if_fail (msg, Sexp::List());
g_return_val_if_fail (!((opts & MU_MSG_OPTION_HEADERS_ONLY) && g_return_val_if_fail (!((opts & MU_MSG_OPTION_HEADERS_ONLY) &&
(opts & MU_MSG_OPTION_EXTRACT_IMAGES)), (opts & MU_MSG_OPTION_EXTRACT_IMAGES)),
Sexp::make_symbol("error")); Sexp::List());
Sexp::List items; Sexp::List items;
if (docid != 0) if (docid != 0)
items.add_prop(":docid", Sexp::make_number(docid)); items.add_prop(":docid", Sexp::make_number(docid));
if (qm)
add_thread_info (items, *qm);
add_prop_nonempty (items, ":subject", mu_msg_get_subject (msg)); add_prop_nonempty (items, ":subject", mu_msg_get_subject (msg));
add_prop_nonempty (items, ":message-id", mu_msg_get_msgid (msg)); add_prop_nonempty (items, ":message-id", mu_msg_get_msgid (msg));
add_prop_nonempty (items, ":mailing-list", mu_msg_get_mailing_list (msg)); add_prop_nonempty (items, ":mailing-list", mu_msg_get_mailing_list (msg));
@ -471,5 +434,12 @@ Mu::msg_to_sexp (MuMsg *msg, unsigned docid,
if (!(opts & MU_MSG_OPTION_HEADERS_ONLY)) if (!(opts & MU_MSG_OPTION_HEADERS_ONLY))
add_message_file_parts (items, msg, opts); add_message_file_parts (items, msg, opts);
return Sexp::make_list(std::move(items)); return items;
}
Mu::Sexp
Mu::msg_to_sexp (MuMsg *msg, unsigned docid, MuMsgOptions opts)
{
return Sexp::make_list(msg_to_sexp_list(msg, docid, opts));
} }

View File

@ -601,7 +601,6 @@ struct QueryMatch;
* *
* @param msg a valid message * @param msg a valid message
* @param docid the docid for this message, or 0 * @param docid the docid for this message, or 0
* @param qm information about this match
* @param opts, bitwise OR'ed; * @param opts, bitwise OR'ed;
* - MU_MSG_OPTION_HEADERS_ONLY: only include message fields which can be * - MU_MSG_OPTION_HEADERS_ONLY: only include message fields which can be
* obtained from the database (this is much faster if the MuMsg is * obtained from the database (this is much faster if the MuMsg is
@ -614,11 +613,10 @@ struct QueryMatch;
* MU_MSG_OPTION_USE_AGENT: attempt to use GPG-agent * MU_MSG_OPTION_USE_AGENT: attempt to use GPG-agent
* MU_MSG_OPTION_USE_PKCS7: attempt to use PKCS (instead of gpg) * MU_MSG_OPTION_USE_PKCS7: attempt to use PKCS (instead of gpg)
* *
* @return a Sexp::Node representing the message * @return a Mu::Sexp or a Mu::Sexp::List representing the message.
*/ */
Mu::Sexp msg_to_sexp (MuMsg *msg, unsigned docid, Mu::Sexp::List msg_to_sexp_list(MuMsg *msg, unsigned docid, MuMsgOptions ops);
const Option<QueryMatch&> qm, Mu::Sexp msg_to_sexp(MuMsg *msg, unsigned docid, MuMsgOptions ops);
MuMsgOptions ops);
} }
#endif /*MU_MSG_HH__*/ #endif /*MU_MSG_HH__*/

View File

@ -109,7 +109,19 @@ struct Server::Private {
void sent_handler (const Parameters& params); void sent_handler (const Parameters& params);
void view_handler (const Parameters& params); void view_handler (const Parameters& params);
private: private:
// helpers
Sexp build_message_sexp(MuMsg *msg, unsigned docid,
const Option<QueryMatch&> qm,
MuMsgOptions opts);
Sexp::List move_docid (DocId docid, const std::string& flagstr,
bool new_name, bool no_view);
Sexp::List perform_move (DocId docid, MuMsg *msg,
const std::string& maildirarg,
MuFlags flags, bool new_name, bool no_view);
Store& store_; Store& store_;
Server::Output output_; Server::Output output_;
const CommandMap command_map_; const CommandMap command_map_;
@ -118,7 +130,48 @@ private:
std::atomic<bool> keep_going_{}; std::atomic<bool> keep_going_{};
}; };
static void
add_thread_info (Sexp::List& items, const QueryMatch& qmatch)
{
Sexp::List info;
auto symbol_t = []{return Sexp::make_symbol("t");};
info.add_prop(":path", Sexp::make_string(qmatch.thread_path));
info.add_prop(":level", Sexp::make_number(qmatch.thread_level));
if (qmatch.has_flag(QueryMatch::Flags::Root))
info.add_prop( ":root", symbol_t());
if (qmatch.has_flag(QueryMatch::Flags::Related))
info.add_prop( ":related", symbol_t());
if (qmatch.has_flag(QueryMatch::Flags::First))
info.add_prop( ":first-child", symbol_t());
if (qmatch.has_flag(QueryMatch::Flags::Last))
info.add_prop( ":last-child", symbol_t());
if (qmatch.has_flag(QueryMatch::Flags::Orphan))
info.add_prop( ":empty-parent", symbol_t());
if (qmatch.has_flag(QueryMatch::Flags::Duplicate))
info.add_prop( ":duplicate", symbol_t());
if (qmatch.has_flag(QueryMatch::Flags::HasChild))
info.add_prop( ":has-child", symbol_t());
if (qmatch.has_flag(QueryMatch::Flags::ThreadSubject))
info.add_prop( ":thread_subject", symbol_t());
items.add_prop(":thread", Sexp::make_list(std::move(info)));
}
Sexp
Server::Private::build_message_sexp (MuMsg *msg, unsigned docid,
const Option<QueryMatch&> qm,
MuMsgOptions opts)
{
auto lst{Mu::msg_to_sexp_list(msg, docid, opts)};
if (qm)
add_thread_info(lst, *qm);
return Sexp::make_list(std::move(lst));
}
CommandMap CommandMap
Server::Private::make_command_map () Server::Private::make_command_map ()
{ {
@ -358,7 +411,7 @@ Server::Private::add_handler (const Parameters& params)
path.c_str(), docid); path.c_str(), docid);
Sexp::List update; Sexp::List update;
update.add_prop(":update", Mu::msg_to_sexp(msg, docid, {}, MU_MSG_OPTION_VERIFY)); update.add_prop(":update", build_message_sexp(msg, docid, {}, MU_MSG_OPTION_VERIFY));
output_sexp(Sexp::make_list(std::move(update))); output_sexp(Sexp::make_list(std::move(update)));
mu_msg_unref(msg); mu_msg_unref(msg);
} }
@ -420,7 +473,7 @@ Server::Private::compose_handler (const Parameters& params)
throw Error{Error::Code::Store, &gerr, "failed to get message %u", docid}; throw Error{Error::Code::Store, &gerr, "failed to get message %u", docid};
const auto opts{message_options(params)}; const auto opts{message_options(params)};
comp_lst.add_prop(":original", Mu::msg_to_sexp(msg, docid, {}, opts)); comp_lst.add_prop(":original", build_message_sexp(msg, docid, {}, opts));
if (ctype == "forward") { if (ctype == "forward") {
PartInfo pinfo{}; PartInfo pinfo{};
@ -686,8 +739,8 @@ Server::Private::output_sexp (const QueryResults& qres, unsigned maxnum)
continue; continue;
auto qm{mi.query_match()}; auto qm{mi.query_match()};
output_sexp(Mu::msg_to_sexp(msg, mi.doc_id(), output_sexp(build_message_sexp(msg, mi.doc_id(),
qm, MU_MSG_OPTION_HEADERS_ONLY)); qm, MU_MSG_OPTION_HEADERS_ONLY));
} }
return n; return n;
@ -856,9 +909,9 @@ get_flags (const std::string& path, const std::string& flagstr)
} }
} }
static Sexp::List Sexp::List
perform_move (Store& store, DocId docid, MuMsg *msg, const std::string& maildirarg, Server::Private::perform_move (DocId docid, MuMsg *msg, const std::string& maildirarg,
MuFlags flags, bool new_name, bool no_view) MuFlags flags, bool new_name, bool no_view)
{ {
bool different_mdir{}; bool different_mdir{};
auto maildir{maildirarg}; auto maildir{maildirarg};
@ -874,11 +927,11 @@ perform_move (Store& store, DocId docid, MuMsg *msg, const std::string& maildira
/* after mu_msg_move_to_maildir, path will be the *new* path, and flags and maildir fields /* after mu_msg_move_to_maildir, path will be the *new* path, and flags and maildir fields
* will be updated as wel */ * will be updated as wel */
if (!store.update_message (msg, docid)) if (!store_.update_message (msg, docid))
throw Error{Error::Code::Store, "failed to store updated message"}; throw Error{Error::Code::Store, "failed to store updated message"};
Sexp::List seq; Sexp::List seq;
seq.add_prop(":update", msg_to_sexp (msg, docid, {}, MU_MSG_OPTION_VERIFY)); seq.add_prop(":update", build_message_sexp (msg, docid, {}, MU_MSG_OPTION_VERIFY));
/* note, the :move t thing is a hint to the frontend that it /* note, the :move t thing is a hint to the frontend that it
* could remove the particular header */ * could remove the particular header */
if (different_mdir) if (different_mdir)
@ -889,14 +942,14 @@ perform_move (Store& store, DocId docid, MuMsg *msg, const std::string& maildira
return seq; return seq;
} }
static Sexp::List Sexp::List
move_docid (Store& store, DocId docid, const std::string& flagstr, Server::Private::move_docid (DocId docid, const std::string& flagstr,
bool new_name, bool no_view) bool new_name, bool no_view)
{ {
if (docid == MU_STORE_INVALID_DOCID) if (docid == MU_STORE_INVALID_DOCID)
throw Error{Error::Code::InvalidArgument, "invalid docid"}; throw Error{Error::Code::InvalidArgument, "invalid docid"};
auto msg{store.find_message(docid)}; auto msg{store_.find_message(docid)};
try { try {
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"};
@ -907,7 +960,7 @@ move_docid (Store& store, DocId docid, const std::string& flagstr,
throw Error{Error::Code::InvalidArgument, "invalid flags '%s'", throw Error{Error::Code::InvalidArgument, "invalid flags '%s'",
flagstr.c_str()}; flagstr.c_str()};
auto lst = perform_move(store, 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;
@ -942,7 +995,7 @@ 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(store(), docid, flagstr, rename, no_view)); output_sexp(move_docid(docid, flagstr, rename, no_view));
return; return;
} }
auto docid{docids.at(0)}; auto docid{docids.at(0)};
@ -972,7 +1025,7 @@ Server::Private::move_handler (const Parameters& params)
} }
try { try {
output_sexp(perform_move(store(), docid, msg, maildir, flags, output_sexp(perform_move(docid, msg, maildir, flags,
rename, no_view)); rename, no_view));
} catch (...) { } catch (...) {
mu_msg_unref(msg); mu_msg_unref(msg);
@ -1128,7 +1181,7 @@ Server::Private::view_handler (const Parameters& params)
maybe_mark_as_read (store(), msg, docid); maybe_mark_as_read (store(), msg, docid);
Sexp::List seq; Sexp::List seq;
seq.add_prop(":view", msg_to_sexp(msg, docid, {}, message_options(params))); seq.add_prop(":view", build_message_sexp(msg, docid, {}, message_options(params)));
mu_msg_unref(msg); mu_msg_unref(msg);

View File

@ -496,7 +496,7 @@ output_sexp (MuMsg *msg, const OutputInfo& info, const MuConfig *opts, GError **
if (!msg) if (!msg)
return true; return true;
fputs(msg_to_sexp(msg, 0, {}, MU_MSG_OPTION_HEADERS_ONLY) fputs(msg_to_sexp(msg, 0, MU_MSG_OPTION_HEADERS_ONLY)
.to_sexp_string().c_str(), stdout); .to_sexp_string().c_str(), stdout);
fputs ("\n", stdout); fputs ("\n", stdout);
@ -516,7 +516,7 @@ output_json (MuMsg *msg, const OutputInfo& info, const MuConfig *opts, GError **
return true; return true;
} }
g_print("%s\n", msg_to_sexp(msg, info.docid, {}, MU_MSG_OPTION_HEADERS_ONLY) g_print("%s\n", msg_to_sexp(msg, info.docid, MU_MSG_OPTION_HEADERS_ONLY)
.to_sexp_string().c_str()); .to_sexp_string().c_str());
return true; return true;

View File

@ -50,7 +50,7 @@ using namespace Mu;
static gboolean static gboolean
view_msg_sexp (MuMsg *msg, const MuConfig *opts) view_msg_sexp (MuMsg *msg, const MuConfig *opts)
{ {
::fputs(msg_to_sexp(msg,0,{}, mu_config_get_msg_options(opts)) ::fputs(msg_to_sexp(msg,0, mu_config_get_msg_options(opts))
.to_sexp_string(). c_str(), stdout); .to_sexp_string(). c_str(), stdout);
return TRUE; return TRUE;
} }