diff --git a/lib/message/mu-contact.cc b/lib/message/mu-contact.cc index 4c164735..b31f5fd5 100644 --- a/lib/message/mu-contact.cc +++ b/lib/message/mu-contact.cc @@ -20,19 +20,33 @@ #include "mu-contact.hh" #include "mu-message.hh" #include "utils/mu-utils.hh" +#include "mu-mime-object.hh" #include #include +#include using namespace Mu; -std::string -Contact::display_name() const +static bool +needs_quoting(const std::string& name) { + for (auto& c: name) + if (c == ',' || c == '"') + return true; + return false; +} + +std::string +Contact::display_name(bool quote) const +{ + if (name.empty()) return email; - else + else if (!quote || !needs_quoting(name)) return name + " <" + email + '>'; + else + return address_rfc2047(*this); } std::string @@ -131,6 +145,29 @@ test_ctor_cleanup() assert_equal(c.display_name(), "Bli ky "); } +static void +test_encode() +{ + Contact c{ + "cassius@example.com", + "Ali, Muhammad \"The Greatest\"", + 345, + false, /* personal */ + 333, /*freq*/ + 768 /* tstamp */ + }; + + assert_equal(c.email, "cassius@example.com"); + assert_equal(c.name, "Ali, Muhammad \"The Greatest\""); + g_assert_false(c.personal); + g_assert_cmpuint(c.frequency,==,333); + g_assert_cmpuint(c.tstamp,==,768); + g_assert_cmpuint(c.message_date,==,345); + + assert_equal(c.display_name(true), + "\"Ali, Muhammad \\\"The Greatest\\\"\" "); +} + int main(int argc, char* argv[]) @@ -141,6 +178,7 @@ main(int argc, char* argv[]) g_test_add_func("/message/contact/ctor-foo", test_ctor_foo); g_test_add_func("/message/contact/ctor-blinky", test_ctor_blinky); g_test_add_func("/message/contact/ctor-cleanup", test_ctor_cleanup); + g_test_add_func("/message/contact/encode", test_encode); return g_test_run(); } diff --git a/lib/message/mu-contact.hh b/lib/message/mu-contact.hh index 4644f091..6b305f3b 100644 --- a/lib/message/mu-contact.hh +++ b/lib/message/mu-contact.hh @@ -88,9 +88,13 @@ struct Contact { * Jane Doe * otherwise it's just the e-mail address. * + * @param quote_if_needed if true, handle quoting of the name-part as well. This + * is useful when the address is to be used directly in emails. + * * @return the display name */ - std::string display_name() const; + std::string display_name(bool quote_if_needed=false) const; + /** * Operator==; based on the hash values (ie. lowercase e-mail address) @@ -160,7 +164,6 @@ private: } }; - constexpr Option contact_type_from_field_id(Field::Id id) noexcept { diff --git a/lib/mu-server.cc b/lib/mu-server.cc index c8e5cdd2..f5d196a0 100644 --- a/lib/mu-server.cc +++ b/lib/mu-server.cc @@ -547,7 +547,7 @@ Server::Private::contacts_handler(const Parameters& params) Sexp::List contact; contact.add_prop(":address", - Sexp::make_string(ci.display_name())); + Sexp::make_string(ci.display_name(true/*encode-if-needed*/))); contact.add_prop(":rank", Sexp::make_number(rank)); contacts.add(Sexp::make_list(std::move(contact)));