summaryrefslogtreecommitdiff
path: root/lib/message/mu-contact.hh
blob: 90052938e11015d241bd53f982f2929828846ef5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
** Copyright (C) 2022-2025 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>

#include <utils/mu-option.hh>
#include "mu-fields.hh"

namespace Mu {
struct Contact {
	enum struct Type:char {
		None='\0', Sender='s', From='f',
		ReplyTo='r', To='t', Cc='c', Bcc='b'
	};

	/**
	 * Construct a new Contact
	 *
	 * @param email email address
	 * @param name name or empty
	 * @param type contact field type
	 * @param message_date data for the message for this contact
	 */
	Contact(const std::string& email, const std::string& name = {},
		Type type = {}, int64_t message_date ={})
		: email{email}, name{name}, type{type},
		  message_date{message_date}, personal{}, frequency{1}, tstamp{}
		{ cleanup_name(); }

	/**
	 * Construct a new Contact
	 *
	 * @param email email address
	 * @param name name or empty
	 * @param message_date date of message this contact originate from
	 * @param personal is this a personal contact?
	 * @param freq how often was this contact seen?
	 * @param tstamp timestamp for last change
	 */
	Contact(const std::string& email, const std::string& name,
		int64_t message_date, bool personal, size_t freq,
		int64_t tstamp)
	    : email{email}, name{name}, type{},
	      message_date{message_date}, personal{personal}, frequency{freq},
	      tstamp{tstamp}
	{ cleanup_name(); }

	/**
	 * Get the "display name" for this contact:
	 *
	 * If there is a non-empty name, it is of the form
	 *   Jane Doe <email@example.com>
	 * Otherwise it is just the e-mail address. Names with commas are quoted
	 * (with the quotes escaped).
	 *
	 * @return the display name
	 */
	std::string display_name() const;

	/**
	 * Does the contact contain a valid email address as per
	 *   https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
	 * ?
	 * @return true or false
	 */
	bool has_valid_email() const;

	/**
	 * Operator==; based on the e-mail address only
	 *
	 * @param rhs some other Contact
	 *
	 * @return true or false.
	 */
	bool operator== (const Contact& rhs) const noexcept {
		return email == rhs.email;
	}
	/**
	 * Operator!=
	 *
	 * @param rhs some other Contact
	 *
	 * @return true or false.
	 */
	bool operator!= (const Contact& rhs) const noexcept {
		return !(*this == rhs);
	}

	static constexpr int64_t RecentOffset{15 * 24 * 3600};
	/**< Contacts seen after now - RecentOffset seconds are considered
	 * "recent" */

	/**
	 * operator<
	 *
	 * Less "relevant" contacts are smaller than more relevant.
	 *
	 * Used as a somewhat over-engineered way to sort by relevance
	 *
	 * This is currently used for the ordering in mu-cfind and
	 * auto-completion in mu4e, if the various completion methods don't
	 * override it...
	 *
	 * @param rhs some other contact
	 *
	 * @return true of false
	 */
	bool operator<(const Contact& rhs) const noexcept {
		// non-personal is less relevant.
		if (personal != rhs.personal)
			return personal < rhs.personal;
		// older is less relevant for recent messages
		if (message_date != rhs.message_date &&
		    (message_date > recently() || rhs.message_date > recently()))
		    return message_date < rhs.message_date;
		// less frequent is less relevant
		if (frequency != rhs.frequency)
			return frequency < rhs.frequency;
		// if all else fails, alphabetically
		return email < rhs.email;
	}

	/**
	 * Get the corresponding Field::Id (if any)
	 * for this contact.
	 *
	 * @return the field-id or Nothing.
	 */
	constexpr Option<Field::Id> field_id() const noexcept {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch-enum"
		switch(type) {
		case Type::Bcc:
			return Field::Id::Bcc;
		case Type::Cc:
			return Field::Id::Cc;
		case Type::From:
			return Field::Id::From;
		case Type::To:
			return Field::Id::To;
		default:
			return Nothing;
		}
#pragma GCC diagnostic pop
	}

	/*
	 * data members
	 */
	std::string	email{};	/**< Email address for this contact.Not empty */
	std::string	name{};		 /**< Name for this contact; can be empty. */
	Type		type{Type::None};/**< Type of contact */
	int64_t		message_date{}; /**< Date of the contact's message */
	bool		personal{};	/**<  A personal message? */
	size_t		frequency{};	/**< Frequency of this contact */
	int64_t		tstamp{};	/**< Timestamp for this contact (internal use) */

private:
	void cleanup_name() { // replace control characters by spaces.
		for (auto& c: name)
			if (iscntrl(c))
				c = ' ';
	}

	/**
	 * Oldest timestamp considered "recent"
	 *
	 * This is arbitrary of course
	 *
	 * @return timestamp
	 */
	static int64_t recently() {
		static const auto recent{::time({}) - RecentOffset};
		return recent;
	}
};

constexpr Option<Contact::Type>
contact_type_from_field_id(Field::Id id) noexcept {

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch-enum"
	switch(id) {
	case Field::Id::Bcc:
		return Contact::Type::Bcc;
	case Field::Id::Cc:
		return Contact::Type::Cc;
	case Field::Id::From:
		return Contact::Type::From;
	case Field::Id::To:
		return Contact::Type::To;
	default:
		return Nothing;
	}
#pragma GCC diagnostic pop
}

using Contacts = std::vector<Contact>;

/**
 * Get contacts as a comma-separated list.
 *
 * @param contacts contacs
 *
 * @return string with contacts.
 */
std::string to_string(const Contacts& contacts);

} // namespace Mu

/**
 * Implement our hash int std::
 */
template<> struct std::hash<Mu::Contact> {
	std::size_t operator()(const Mu::Contact& c) const noexcept {
		return std::hash<std::string>{}(c.email);
	}
};

#endif /* MU_CONTACT_HH__ */