2022-02-19 10:55:31 +01:00
|
|
|
/*
|
|
|
|
** Copyright (C) 2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
|
|
|
**
|
|
|
|
** This program is free software; you can redistribute it and/or modify it
|
|
|
|
** under the terms of the GNU General Public License as published by the
|
|
|
|
** Free Software Foundation; either version 3, or (at your option) any
|
|
|
|
** later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MU_MESSAGE_CONTACT_HH__
|
|
|
|
#define MU_MESSAGE_CONTACT_HH__
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <functional>
|
|
|
|
#include <cctype>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <ctime>
|
|
|
|
|
2022-03-03 23:02:52 +01:00
|
|
|
#include "mu-message-fields.hh"
|
|
|
|
|
2022-02-19 10:55:31 +01:00
|
|
|
struct _InternetAddressList;
|
|
|
|
|
|
|
|
namespace Mu {
|
|
|
|
|
2022-02-25 21:53:07 +01:00
|
|
|
/**
|
2022-02-19 10:55:31 +01:00
|
|
|
* Get the hash value for a lowercase value of s; useful for email-addresses
|
2022-02-25 21:53:07 +01:00
|
|
|
*
|
2022-02-19 10:55:31 +01:00
|
|
|
* @param s a string
|
2022-02-25 21:53:07 +01:00
|
|
|
*
|
2022-02-19 10:55:31 +01:00
|
|
|
* @return a hash value.
|
|
|
|
*/
|
|
|
|
size_t lowercase_hash(const std::string& s);
|
|
|
|
|
|
|
|
struct MessageContact {
|
|
|
|
/**
|
|
|
|
* Construct a new MessageContact
|
|
|
|
*
|
|
|
|
* @param email_ email address
|
|
|
|
* @param name_ name or empty
|
2022-03-19 17:38:30 +01:00
|
|
|
* @param field_id_ contact field id, or {}
|
2022-02-19 10:55:31 +01:00
|
|
|
* @param message_date_ data for the message for this contact
|
|
|
|
*/
|
|
|
|
MessageContact(const std::string& email_, const std::string& name_ = "",
|
2022-03-19 17:38:30 +01:00
|
|
|
std::optional<MessageField::Id> field_id_ = {},
|
|
|
|
time_t message_date_ = 0)
|
|
|
|
: email{email_}, name{name_}, field_id{field_id_},
|
2022-02-19 10:55:31 +01:00
|
|
|
message_date{message_date_}, personal{}, frequency{1}, tstamp{}
|
2022-02-25 21:53:07 +01:00
|
|
|
{ cleanup_name(); }
|
2022-02-19 10:55:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a new MessageContact
|
|
|
|
*
|
|
|
|
* @param email_ email address
|
|
|
|
* @param name_ name or empty
|
2022-02-19 17:57:50 +01:00
|
|
|
* @param message_date_ date of message this contact originate from
|
2022-02-19 10:55:31 +01:00
|
|
|
* @param personal_ is this a personal contact?
|
|
|
|
* @param freq_ how often was this contact seen?
|
2022-02-19 17:57:50 +01:00
|
|
|
* @param tstamp_ timestamp for last change
|
2022-02-19 10:55:31 +01:00
|
|
|
*/
|
|
|
|
MessageContact(const std::string& email_, const std::string& name_,
|
2022-02-25 21:53:07 +01:00
|
|
|
time_t message_date_, bool personal_, size_t freq_,
|
|
|
|
int64_t tstamp_)
|
2022-03-19 17:38:30 +01:00
|
|
|
: email{email_}, name{name_}, field_id{},
|
2022-02-19 10:55:31 +01:00
|
|
|
message_date{message_date_}, personal{personal_}, frequency{freq_},
|
|
|
|
tstamp{tstamp_}
|
2022-02-25 21:53:07 +01:00
|
|
|
{ cleanup_name();}
|
2022-02-19 10:55:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @return the display name
|
|
|
|
*/
|
|
|
|
std::string display_name() const;
|
2022-02-25 21:53:07 +01:00
|
|
|
|
|
|
|
/**
|
2022-02-19 10:55:31 +01:00
|
|
|
* Operator==; based on the hash values (ie. lowercase e-mail address)
|
2022-02-25 21:53:07 +01:00
|
|
|
*
|
2022-02-19 10:55:31 +01:00
|
|
|
* @param rhs some other MessageContact
|
2022-02-25 21:53:07 +01:00
|
|
|
*
|
2022-02-19 10:55:31 +01:00
|
|
|
* @return true orf false.
|
|
|
|
*/
|
|
|
|
bool operator== (const MessageContact& rhs) const noexcept {
|
|
|
|
return hash() == rhs.hash();
|
|
|
|
}
|
|
|
|
|
2022-02-25 21:53:07 +01:00
|
|
|
/**
|
2022-02-19 10:55:31 +01:00
|
|
|
* Get a hash-value for this contact, which gets lazily calculated. This
|
|
|
|
* is for use with container classes. This uses the _lowercase_ email
|
|
|
|
* address.
|
|
|
|
*
|
|
|
|
* @return the hash
|
|
|
|
*/
|
|
|
|
size_t hash() const {
|
|
|
|
static size_t cached_hash;
|
|
|
|
if (cached_hash == 0) {
|
|
|
|
cached_hash = lowercase_hash(email);
|
|
|
|
}
|
|
|
|
return cached_hash;
|
|
|
|
}
|
2022-02-25 21:53:07 +01:00
|
|
|
|
2022-02-19 10:55:31 +01:00
|
|
|
/*
|
|
|
|
* data members
|
|
|
|
*/
|
|
|
|
|
2022-03-19 17:38:30 +01:00
|
|
|
std::string email; /**< Email address for this contact.Not empty */
|
|
|
|
std::string name; /**< Name for this contact; can be empty. */
|
|
|
|
std::optional<MessageField::Id> field_id; /**< Field Id of contact or nullopt */
|
|
|
|
::time_t message_date; /**< date of the message from which the
|
|
|
|
* contact originates */
|
|
|
|
bool personal; /**< A personal message? */
|
|
|
|
size_t frequency; /**< Frequency of this contact */
|
|
|
|
int64_t tstamp; /**< Timestamp for this contact */
|
2022-02-19 10:55:31 +01:00
|
|
|
|
2022-02-25 21:53:07 +01:00
|
|
|
private:
|
|
|
|
void cleanup_name() { // replace control characters by spaces.
|
|
|
|
for (auto& c: name)
|
|
|
|
if (iscntrl(c))
|
|
|
|
c = ' ';
|
|
|
|
}
|
2022-02-19 10:55:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
using MessageContacts = std::vector<MessageContact>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a sequence of MessageContact objects from an InternetAddressList
|
|
|
|
*
|
|
|
|
* @param addr_lst an address list
|
2022-03-19 17:38:30 +01:00
|
|
|
* @param field_id the field_id for message field for these addresses
|
2022-02-19 10:55:31 +01:00
|
|
|
* @param message_date the date of the message from which the InternetAddressList
|
|
|
|
* originates.
|
|
|
|
*
|
|
|
|
* @return a sequence of MessageContact objects.
|
|
|
|
*/
|
|
|
|
MessageContacts
|
|
|
|
make_message_contacts(/*const*/ struct _InternetAddressList* addr_lst,
|
2022-03-19 17:38:30 +01:00
|
|
|
MessageField::Id field_id, ::time_t message_date);
|
2022-02-19 10:55:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a sequence of MessageContact objects from an InternetAddressList
|
|
|
|
*
|
|
|
|
* @param addrs a string with one more valid addresses (as per internet_address_list_parse())
|
2022-03-19 17:38:30 +01:00
|
|
|
* @param field_id the field_id for message field for these addresses
|
2022-02-19 10:55:31 +01:00
|
|
|
* @param message_date the date of the message from which the addresses originate
|
|
|
|
*
|
|
|
|
* @return a sequence of MessageContact objects.
|
|
|
|
*/
|
|
|
|
MessageContacts
|
|
|
|
make_message_contacts(const std::string& addrs,
|
2022-03-19 17:38:30 +01:00
|
|
|
MessageField::Id field_id, ::time_t message_date);
|
2022-02-19 10:55:31 +01:00
|
|
|
} // namespace Mu
|
|
|
|
|
2022-02-25 21:53:07 +01:00
|
|
|
/**
|
2022-02-19 10:55:31 +01:00
|
|
|
* Implement our hash int std::
|
|
|
|
*/
|
|
|
|
template<> struct std::hash<Mu::MessageContact> {
|
|
|
|
std::size_t operator()(const Mu::MessageContact& c) const noexcept {
|
|
|
|
return c.hash();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* MU_MESSAGE_CONTACT_HH__ */
|