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;
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
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())
return email;
else if (!quote || !needs_quoting(name))
else if (!needs_quoting(name))
return name + " <" + email + '>';
else
return address_rfc2047(*this);
return Mu::quote(name) + " <" + email + '>';
}
static Regex email_rx;
@ -196,7 +194,7 @@ test_encode()
g_assert_cmpuint(c.tstamp,==,768);
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>");
}

View File

@ -83,18 +83,15 @@ struct Contact {
{ cleanup_name();}
/**
* Get the "display name" for this contact; basically, if there's a
* non-empty name, it's
* Jane Doe <email@example.com>
* otherwise it's just the e-mail address.
* Get the "display name" for this contact:
*
* @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.
* If there's a non-empty name, it's Jane Doe <email@example.com>
* otherwise it's just the e-mail address. Names with commas are quoted
* (with the quotes escaped).
*
* @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);
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).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());
assert_equal(message->subject(), "Purkutyöurakka");

View File

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