contact: fix quoting

Ensure that addresses with commas are quoted. Don't 2047-encode though.

Update tests.
This commit is contained in:
Dirk-Jan C. Binnema 2023-03-14 06:18:56 +02:00
parent 46746bacf8
commit 96e16034c7
4 changed files with 18 additions and 23 deletions

View File

@ -29,24 +29,22 @@
using namespace Mu; using namespace Mu;
static bool
needs_quoting(const std::string& name)
{
for (auto& c: name)
if (c == ',' || c == '"')
return true;
return false;
}
std::string std::string
Contact::display_name(bool quote) const Contact::display_name() const
{ {
auto needs_quoting= [](const std::string& n) {
for (auto& c: n)
if (c == ',' || c == '"')
return true;
return false;
};
if (name.empty()) if (name.empty())
return email; return email;
else if (!quote || !needs_quoting(name)) else if (!needs_quoting(name))
return name + " <" + email + '>'; return name + " <" + email + '>';
else else
return address_rfc2047(*this); return Mu::quote(name) + " <" + email + '>';
} }
static Regex email_rx; static Regex email_rx;
@ -196,7 +194,7 @@ test_encode()
g_assert_cmpuint(c.tstamp,==,768); g_assert_cmpuint(c.tstamp,==,768);
g_assert_cmpuint(c.message_date,==,345); g_assert_cmpuint(c.message_date,==,345);
assert_equal(c.display_name(true), assert_equal(c.display_name(),
"\"Ali, Muhammad \\\"The Greatest\\\"\" <cassius@example.com>"); "\"Ali, Muhammad \\\"The Greatest\\\"\" <cassius@example.com>");
} }

View File

@ -83,18 +83,15 @@ struct Contact {
{ cleanup_name();} { cleanup_name();}
/** /**
* Get the "display name" for this contact; basically, if there's a * Get the "display name" for this contact:
* non-empty name, it's
* Jane Doe <email@example.com>
* otherwise it's just the e-mail address.
* *
* @param quote_if_needed if true, handle quoting of the name-part as * If there's a non-empty name, it's Jane Doe <email@example.com>
* well. This is useful when the address is to be used directly in * otherwise it's just the e-mail address. Names with commas are quoted
* emails. * (with the quotes escaped).
* *
* @return the display name * @return the display name
*/ */
std::string display_name(bool quote_if_needed=false) const; std::string display_name() const;
/** /**

View File

@ -546,7 +546,7 @@ cmVmCjM1NjE4CiUlRU9GCg==
g_assert_cmpuint(message->cc().size(),==, 1); g_assert_cmpuint(message->cc().size(),==, 1);
assert_equal(message->cc().at(0).email, "Mickey.Mueller@example.com"); assert_equal(message->cc().at(0).email, "Mickey.Mueller@example.com");
assert_equal(message->cc().at(0).name, "Müller, Mickey"); assert_equal(message->cc().at(0).name, "Müller, Mickey");
assert_equal(message->cc().at(0).display_name(), "Müller, Mickey <Mickey.Mueller@example.com>"); assert_equal(message->cc().at(0).display_name(), "\"Müller, Mickey\" <Mickey.Mueller@example.com>");
g_assert_true(message->bcc().empty()); g_assert_true(message->bcc().empty());
assert_equal(message->subject(), "Purkutyöurakka"); assert_equal(message->subject(), "Purkutyöurakka");

View File

@ -225,7 +225,7 @@ output_json(ItemType itype, OptContact contact, const Options& opts)
" \"frequency\" : %zu\n", " \"frequency\" : %zu\n",
contact->email.c_str(), contact->email.c_str(),
name.c_str(), name.c_str(),
Mu::quote(contact->display_name(true)).c_str(), Mu::quote(contact->display_name()).c_str(),
contact->message_date, contact->message_date,
time_to_string("%FT%TZ", contact->message_date, true/*utc*/).c_str(), time_to_string("%FT%TZ", contact->message_date, true/*utc*/).c_str(),
contact->personal ? "true" : "false", contact->personal ? "true" : "false",