message-contact: remove control characters from names

I.e., issue #2216.
This commit is contained in:
Dirk-Jan C. Binnema
2022-02-25 22:53:07 +02:00
parent 285185aac7
commit cfc68732e7
2 changed files with 46 additions and 20 deletions

View File

@ -139,12 +139,33 @@ test_ctor_02()
g_assert_true(c.personal); g_assert_true(c.personal);
g_assert_cmpuint(c.frequency,==,13); g_assert_cmpuint(c.frequency,==,13);
g_assert_cmpuint(c.tstamp,==,12345); g_assert_cmpuint(c.tstamp,==,12345);
assert_equal(c.name, "Blinky");
g_assert_cmpuint(c.message_date,==,1645215014); g_assert_cmpuint(c.message_date,==,1645215014);
assert_equal(c.display_name(), "Blinky <bar@example.com>"); assert_equal(c.display_name(), "Blinky <bar@example.com>");
} }
static void
test_ctor_03()
{
MessageContact c{
"bar@example.com",
"Bli\nky",
1645215014,
true, /* personal */
13, /*freq*/
12345 /* tstamp */
};
assert_equal(c.email, "bar@example.com");
assert_equal(c.name, "Bli ky");
g_assert_true(c.personal);
g_assert_cmpuint(c.frequency,==,13);
g_assert_cmpuint(c.tstamp,==,12345);
g_assert_cmpuint(c.message_date,==,1645215014);
assert_equal(c.display_name(), "Bli ky <bar@example.com>");
}
static void static void
@ -173,6 +194,7 @@ main(int argc, char* argv[])
g_test_add_func("/lib/message-contacts/ctor-01", test_ctor_01); g_test_add_func("/lib/message-contacts/ctor-01", test_ctor_01);
g_test_add_func("/lib/message-contacts/ctor-02", test_ctor_02); g_test_add_func("/lib/message-contacts/ctor-02", test_ctor_02);
g_test_add_func("/lib/message-contacts/ctor-cleanup", test_ctor_cleanup);
g_test_add_func("/lib/message-contacts/make-contacts", test_make_contacts); g_test_add_func("/lib/message-contacts/make-contacts", test_make_contacts);
return g_test_run(); return g_test_run();

View File

@ -34,11 +34,11 @@ struct _InternetAddressList;
namespace Mu { namespace Mu {
/** /**
* Get the hash value for a lowercase value of s; useful for email-addresses * Get the hash value for a lowercase value of s; useful for email-addresses
* *
* @param s a string * @param s a string
* *
* @return a hash value. * @return a hash value.
*/ */
size_t lowercase_hash(const std::string& s); size_t lowercase_hash(const std::string& s);
@ -66,11 +66,10 @@ struct MessageContact {
* @param message_date_ data for the message for this contact * @param message_date_ data for the message for this contact
*/ */
MessageContact(const std::string& email_, const std::string& name_ = "", MessageContact(const std::string& email_, const std::string& name_ = "",
Type type_ = Type::Unknown, time_t message_date_ = 0) Type type_ = Type::Unknown, time_t message_date_ = 0)
: email{email_}, name{name_}, type{type_}, : email{email_}, name{name_}, type{type_},
message_date{message_date_}, personal{}, frequency{1}, tstamp{} message_date{message_date_}, personal{}, frequency{1}, tstamp{}
{ { cleanup_name(); }
}
/** /**
* Construct a new MessageContact * Construct a new MessageContact
@ -83,13 +82,12 @@ struct MessageContact {
* @param tstamp_ timestamp for last change * @param tstamp_ timestamp for last change
*/ */
MessageContact(const std::string& email_, const std::string& name_, MessageContact(const std::string& email_, const std::string& name_,
time_t message_date_, bool personal_, size_t freq_, time_t message_date_, bool personal_, size_t freq_,
int64_t tstamp_) int64_t tstamp_)
: email{email_}, name{name_}, type{Type::Unknown}, : email{email_}, name{name_}, type{Type::Unknown},
message_date{message_date_}, personal{personal_}, frequency{freq_}, message_date{message_date_}, personal{personal_}, frequency{freq_},
tstamp{tstamp_} tstamp{tstamp_}
{ { cleanup_name();}
}
/** /**
* Get the "display name" for this contact; basically, if there's a * Get the "display name" for this contact; basically, if there's a
@ -100,19 +98,19 @@ struct MessageContact {
* @return the display name * @return the display name
*/ */
std::string display_name() const; std::string display_name() const;
/** /**
* Operator==; based on the hash values (ie. lowercase e-mail address) * Operator==; based on the hash values (ie. lowercase e-mail address)
* *
* @param rhs some other MessageContact * @param rhs some other MessageContact
* *
* @return true orf false. * @return true orf false.
*/ */
bool operator== (const MessageContact& rhs) const noexcept { bool operator== (const MessageContact& rhs) const noexcept {
return hash() == rhs.hash(); return hash() == rhs.hash();
} }
/** /**
* Get a hash-value for this contact, which gets lazily calculated. This * Get a hash-value for this contact, which gets lazily calculated. This
* is for use with container classes. This uses the _lowercase_ email * is for use with container classes. This uses the _lowercase_ email
* address. * address.
@ -126,7 +124,7 @@ struct MessageContact {
} }
return cached_hash; return cached_hash;
} }
/* /*
* data members * data members
*/ */
@ -135,11 +133,17 @@ struct MessageContact {
std::string name; /**< Name for this contact; can be empty. */ std::string name; /**< Name for this contact; can be empty. */
Type type{Type::Unknown}; /**< Type of contact */ Type type{Type::Unknown}; /**< Type of contact */
::time_t message_date; /**< date of the message from which the ::time_t message_date; /**< date of the message from which the
* contact originates */ * contact originates */
bool personal; /**< A personal message? */ bool personal; /**< A personal message? */
size_t frequency; /**< Frequency of this contact */ size_t frequency; /**< Frequency of this contact */
int64_t tstamp; /**< Timestamp for this contact */ int64_t tstamp; /**< Timestamp for this contact */
private:
void cleanup_name() { // replace control characters by spaces.
for (auto& c: name)
if (iscntrl(c))
c = ' ';
}
}; };
using MessageContacts = std::vector<MessageContact>; using MessageContacts = std::vector<MessageContact>;
@ -156,7 +160,7 @@ using MessageContacts = std::vector<MessageContact>;
*/ */
MessageContacts MessageContacts
make_message_contacts(/*const*/ struct _InternetAddressList* addr_lst, make_message_contacts(/*const*/ struct _InternetAddressList* addr_lst,
MessageContact::Type type, ::time_t message_date); MessageContact::Type type, ::time_t message_date);
/** /**
* Create a sequence of MessageContact objects from an InternetAddressList * Create a sequence of MessageContact objects from an InternetAddressList
@ -172,7 +176,7 @@ make_message_contacts(const std::string& addrs,
MessageContact::Type type, ::time_t message_date); MessageContact::Type type, ::time_t message_date);
} // namespace Mu } // namespace Mu
/** /**
* Implement our hash int std:: * Implement our hash int std::
*/ */
template<> struct std::hash<Mu::MessageContact> { template<> struct std::hash<Mu::MessageContact> {