From 72fdfec3ebaa34e895c7fa20cc1fdd52c260cccc Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Fri, 29 Jan 2021 22:39:21 +0200 Subject: [PATCH] lib: clean up mu_msg_to_sexp API move out the QueryMatch --- lib/mu-msg-sexp.cc | 56 +++++++---------------------- lib/mu-msg.hh | 8 ++--- lib/mu-server.cc | 87 +++++++++++++++++++++++++++++++++++++--------- mu/mu-cmd-find.cc | 4 +-- mu/mu-cmd.cc | 2 +- 5 files changed, 89 insertions(+), 68 deletions(-) diff --git a/lib/mu-msg-sexp.cc b/lib/mu-msg-sexp.cc index 45a92597..330341a7 100644 --- a/lib/mu-msg-sexp.cc +++ b/lib/mu-msg-sexp.cc @@ -283,13 +283,6 @@ make_part_types (MuMsgPartType ptype) return Sexp::make_list(std::move(list)); } -static Sexp -symbol_t() -{ - return Sexp::make_symbol("t"); -} - - static void each_part (MuMsg *msg, MuMsgPart *part, PartInfo *pinfo) { @@ -316,7 +309,7 @@ each_part (MuMsg *msg, MuMsgPart *part, PartInfo *pinfo) g_free (fname); 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)}; if (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))); } -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 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::msg_to_sexp (MuMsg *msg, unsigned docid, - const Option qm, MuMsgOptions opts) +Mu::Sexp::List +Mu::msg_to_sexp_list (MuMsg *msg, unsigned docid, 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) && (opts & MU_MSG_OPTION_EXTRACT_IMAGES)), - Sexp::make_symbol("error")); + Sexp::List()); Sexp::List items; if (docid != 0) 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, ":message-id", mu_msg_get_msgid (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)) 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)); } diff --git a/lib/mu-msg.hh b/lib/mu-msg.hh index 6befc9f6..454a21f4 100644 --- a/lib/mu-msg.hh +++ b/lib/mu-msg.hh @@ -601,7 +601,6 @@ struct QueryMatch; * * @param msg a valid message * @param docid the docid for this message, or 0 - * @param qm information about this match * @param opts, bitwise OR'ed; * - MU_MSG_OPTION_HEADERS_ONLY: only include message fields which can be * 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_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, - const Option qm, - MuMsgOptions ops); +Mu::Sexp::List msg_to_sexp_list(MuMsg *msg, unsigned docid, MuMsgOptions ops); +Mu::Sexp msg_to_sexp(MuMsg *msg, unsigned docid, MuMsgOptions ops); } #endif /*MU_MSG_HH__*/ diff --git a/lib/mu-server.cc b/lib/mu-server.cc index 7a610791..d1fe4a15 100644 --- a/lib/mu-server.cc +++ b/lib/mu-server.cc @@ -109,7 +109,19 @@ struct Server::Private { void sent_handler (const Parameters& params); void view_handler (const Parameters& params); + private: + // helpers + Sexp build_message_sexp(MuMsg *msg, unsigned docid, + const Option 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_; Server::Output output_; const CommandMap command_map_; @@ -118,7 +130,48 @@ private: std::atomic 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 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 Server::Private::make_command_map () { @@ -358,7 +411,7 @@ Server::Private::add_handler (const Parameters& params) path.c_str(), docid); 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))); 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}; 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") { PartInfo pinfo{}; @@ -686,8 +739,8 @@ Server::Private::output_sexp (const QueryResults& qres, unsigned maxnum) continue; auto qm{mi.query_match()}; - output_sexp(Mu::msg_to_sexp(msg, mi.doc_id(), - qm, MU_MSG_OPTION_HEADERS_ONLY)); + output_sexp(build_message_sexp(msg, mi.doc_id(), + qm, MU_MSG_OPTION_HEADERS_ONLY)); } return n; @@ -856,9 +909,9 @@ get_flags (const std::string& path, const std::string& flagstr) } } -static Sexp::List -perform_move (Store& store, DocId docid, MuMsg *msg, const std::string& maildirarg, - MuFlags flags, bool new_name, bool no_view) +Sexp::List +Server::Private::perform_move (DocId docid, MuMsg *msg, const std::string& maildirarg, + MuFlags flags, bool new_name, bool no_view) { bool different_mdir{}; 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 * 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"}; 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 * could remove the particular header */ if (different_mdir) @@ -889,14 +942,14 @@ perform_move (Store& store, DocId docid, MuMsg *msg, const std::string& maildira return seq; } -static Sexp::List -move_docid (Store& store, DocId docid, const std::string& flagstr, - bool new_name, bool no_view) +Sexp::List +Server::Private::move_docid (DocId docid, const std::string& flagstr, + bool new_name, bool no_view) { if (docid == MU_STORE_INVALID_DOCID) throw Error{Error::Code::InvalidArgument, "invalid docid"}; - auto msg{store.find_message(docid)}; + auto msg{store_.find_message(docid)}; try { if (!msg) 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'", 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); return lst; @@ -942,7 +995,7 @@ Server::Private::move_handler (const Parameters& params) "can't move multiple messages at the same time"}; // multi. for (auto&& docid: docids) - output_sexp(move_docid(store(), docid, flagstr, rename, no_view)); + output_sexp(move_docid(docid, flagstr, rename, no_view)); return; } auto docid{docids.at(0)}; @@ -972,7 +1025,7 @@ Server::Private::move_handler (const Parameters& params) } try { - output_sexp(perform_move(store(), docid, msg, maildir, flags, + output_sexp(perform_move(docid, msg, maildir, flags, rename, no_view)); } catch (...) { mu_msg_unref(msg); @@ -1128,7 +1181,7 @@ Server::Private::view_handler (const Parameters& params) maybe_mark_as_read (store(), msg, docid); 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); diff --git a/mu/mu-cmd-find.cc b/mu/mu-cmd-find.cc index 11001785..8cb366d8 100644 --- a/mu/mu-cmd-find.cc +++ b/mu/mu-cmd-find.cc @@ -496,7 +496,7 @@ output_sexp (MuMsg *msg, const OutputInfo& info, const MuConfig *opts, GError ** if (!msg) 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); fputs ("\n", stdout); @@ -516,7 +516,7 @@ output_json (MuMsg *msg, const OutputInfo& info, const MuConfig *opts, GError ** 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()); return true; diff --git a/mu/mu-cmd.cc b/mu/mu-cmd.cc index 65457887..b0054596 100644 --- a/mu/mu-cmd.cc +++ b/mu/mu-cmd.cc @@ -50,7 +50,7 @@ using namespace Mu; static gboolean 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); return TRUE; }