email-lib 0.27.0

Cross-platform, asynchronous Rust library to manage emails
Documentation
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Module dedicated to email envelopes.
//!
//! The email envelope is composed of an identifier, some
//! [flags](self::Flags), and few headers taken from the email
//! [message](crate::Message).

pub mod address;
pub mod config;
pub mod flag;
pub mod get;
pub mod id;
#[cfg(feature = "imap")]
pub mod imap;
pub mod list;
#[cfg(feature = "maildir")]
pub mod maildir;
#[cfg(feature = "notmuch")]
pub mod notmuch;
#[cfg(feature = "sync")]
pub mod sync;
#[cfg(feature = "thread")]
pub mod thread;
#[cfg(feature = "watch")]
pub mod watch;

#[cfg(feature = "thread")]
use std::collections::HashMap;
use std::{
    hash::{DefaultHasher, Hash, Hasher},
    ops::{Deref, DerefMut},
    vec,
};

use chrono::{DateTime, FixedOffset, Local};
#[cfg(feature = "thread")]
use petgraph::graphmap::DiGraphMap;
use tracing::{debug, trace};

#[doc(inline)]
pub use self::{
    address::Address,
    flag::{Flag, Flags},
    id::{Id, MultipleIds, SingleId},
};
use crate::{
    account::config::AccountConfig, date::from_mail_parser_to_chrono_datetime, message::Message,
};

/// The email envelope.
///
/// The email envelope is composed of an identifier, some
/// [flags](self::Flags), and few headers taken from the email
/// [message](crate::Message).
#[derive(Clone, Debug, Default, Eq, Ord, PartialOrd)]
pub struct Envelope {
    /// The shape of the envelope identifier may vary depending on the backend.
    /// For IMAP backend, it is an stringified auto-incremented integer.
    /// For Notmuch backend it is a Git-like hash.
    // TODO: replace me with SingleId
    pub id: String,
    /// The Message-ID header from the email message.
    pub message_id: String,
    /// The In-Reply-To header from the email message.
    pub in_reply_to: Option<String>,
    /// The envelope flags.
    pub flags: Flags,
    /// The first address from the email message header From.
    pub from: Address,
    /// The first address from the email message header To.
    pub to: Address,
    /// The Subject header from the email message.
    pub subject: String,
    /// The Date header from the email message.
    pub date: DateTime<FixedOffset>,

    /// True if the current envelope contains at least one attachment.
    ///
    /// An attachment is defined here as a MIME part that is not a
    /// `text/*`.
    pub has_attachment: bool,
}

impl Envelope {
    /// Build an envelope from an identifier, some
    /// [flags](self::Flags) and a [message](super::Message).
    pub fn from_msg(id: impl ToString, flags: Flags, msg: Message) -> Envelope {
        let mut envelope = Envelope {
            id: id.to_string(),
            flags,
            ..Default::default()
        };

        if let Ok(msg) = msg.parsed() {
            match msg.from() {
                Some(mail_parser::Address::List(addrs))
                    if !addrs.is_empty() && addrs[0].address.is_some() =>
                {
                    let name = addrs[0].name.as_ref().map(|name| name.to_string());
                    let email = addrs[0]
                        .address
                        .as_ref()
                        .map(|name| name.to_string())
                        .unwrap();
                    envelope.from = Address::new(name, email);
                }
                Some(mail_parser::Address::Group(groups))
                    if !groups.is_empty()
                        && !groups[0].addresses.is_empty()
                        && groups[0].addresses[0].address.is_some() =>
                {
                    let name = groups[0].name.as_ref().map(|name| name.to_string());
                    let email = groups[0].addresses[0]
                        .address
                        .as_ref()
                        .map(|name| name.to_string())
                        .unwrap();
                    envelope.from = Address::new(name, email)
                }
                _ => {
                    trace!("cannot extract envelope sender from message header, skipping it");
                }
            };

            match msg.to() {
                Some(mail_parser::Address::List(addrs))
                    if !addrs.is_empty() && addrs[0].address.is_some() =>
                {
                    let name = addrs[0].name.as_ref().map(|name| name.to_string());
                    let email = addrs[0]
                        .address
                        .as_ref()
                        .map(|name| name.to_string())
                        .unwrap();
                    envelope.to = Address::new(name, email);
                }
                Some(mail_parser::Address::Group(groups))
                    if !groups.is_empty()
                        && !groups[0].addresses.is_empty()
                        && groups[0].addresses[0].address.is_some() =>
                {
                    let name = groups[0].name.as_ref().map(|name| name.to_string());
                    let email = groups[0].addresses[0]
                        .address
                        .as_ref()
                        .map(|name| name.to_string())
                        .unwrap();
                    envelope.to = Address::new(name, email)
                }
                _ => {
                    trace!("cannot extract envelope recipient from message header, skipping it");
                }
            };

            envelope.subject = msg.subject().map(ToOwned::to_owned).unwrap_or_default();

            match msg.date() {
                Some(date) => envelope.set_date(date),
                None => {
                    trace!("cannot extract envelope date from message header, skipping it")
                }
            };

            envelope.message_id = msg
                .message_id()
                .map(|mid| format!("<{mid}>"))
                // NOTE: this is useful for the sync to prevent
                // messages without Message-ID to still being
                // synchronized.
                .unwrap_or_else(|| {
                    let mut hasher = DefaultHasher::new();
                    envelope.date.to_string().hash(&mut hasher);
                    format!("<{:x}@generated>", hasher.finish())
                });

            envelope.in_reply_to = msg.in_reply_to().as_text().map(|mid| format!("<{mid}>"));
        } else {
            trace!("cannot parse message header, skipping it");
        };

        envelope
    }

    pub fn set_some_from(&mut self, addr: Option<Address>) {
        if let Some(addr) = addr {
            self.from = addr;
        }
    }

    pub fn set_some_to(&mut self, addr: Option<Address>) {
        if let Some(addr) = addr {
            self.to = addr;
        }
    }

    pub fn set_some_date(&mut self, date: Option<&mail_parser::DateTime>) {
        if let Some(date) = date {
            self.set_date(date)
        }
    }

    /// Transform a [`mail_parser::DateTime`] into a fixed offset [`chrono::DateTime`]
    /// and add it to the current envelope.
    pub fn set_date(&mut self, date: &mail_parser::DateTime) {
        self.date = from_mail_parser_to_chrono_datetime(date).unwrap_or_else(|| {
            debug!("cannot parse envelope date {date}, skipping it");
            DateTime::default()
        });
    }

    /// Format the envelope date according to the datetime format and
    /// timezone from the [account configuration](crate::AccountConfig).
    pub fn format_date(&self, config: &AccountConfig) -> String {
        let fmt = config.get_envelope_list_datetime_fmt();

        let date = if config.has_envelope_list_datetime_local_tz() {
            self.date.with_timezone(&Local).format(&fmt)
        } else {
            self.date.format(&fmt)
        };

        date.to_string()
    }

    /// Build a message from the current envelope.
    ///
    /// The message is just composed of two headers and contains no
    /// content. It is mostly used by the synchronization to cache
    /// envelopes.
    #[cfg(feature = "sync")]
    pub fn to_sync_cache_msg(&self) -> String {
        let id = &self.message_id;
        let date = self.date.to_rfc2822();
        format!("Message-ID: {id}\nDate: {date}\n\n")
    }

    #[cfg(feature = "thread")]
    pub fn as_threaded(&self) -> ThreadedEnvelope<'_> {
        ThreadedEnvelope {
            id: self.id.as_str(),
            message_id: self.message_id.as_str(),
            subject: self.subject.as_str(),
            from: match self.from.name.as_ref() {
                Some(name) => name.as_str(),
                None => self.from.addr.as_str(),
            },
            date: self.date,
        }
    }
}

// NOTE: this is useful for the sync, not sure how relevant it is for
// the rest.
impl PartialEq for Envelope {
    fn eq(&self, other: &Self) -> bool {
        self.message_id == other.message_id
    }
}

impl Hash for Envelope {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.message_id.hash(state);
    }
}

/// The list of email envelopes.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Envelopes(Vec<Envelope>);

impl IntoIterator for Envelopes {
    type IntoIter = vec::IntoIter<Self::Item>;
    type Item = Envelope;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl From<Envelopes> for Vec<Envelope> {
    fn from(val: Envelopes) -> Self {
        val.0
    }
}

impl Deref for Envelopes {
    type Target = Vec<Envelope>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Envelopes {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl FromIterator<Envelope> for Envelopes {
    fn from_iter<T: IntoIterator<Item = Envelope>>(iter: T) -> Self {
        Envelopes(iter.into_iter().collect())
    }
}

#[cfg(feature = "thread")]
#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialOrd)]
#[cfg_attr(
    feature = "derive",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "kebab-case")
)]
pub struct ThreadedEnvelope<'a> {
    pub id: &'a str,
    pub message_id: &'a str,
    pub from: &'a str,
    pub subject: &'a str,
    pub date: DateTime<FixedOffset>,
}

#[cfg(feature = "thread")]
impl ThreadedEnvelope<'_> {
    /// Format the envelope date according to the datetime format and
    /// timezone from the [account configuration](crate::AccountConfig).
    pub fn format_date(&self, config: &AccountConfig) -> String {
        let fmt = config.get_envelope_list_datetime_fmt();

        let date = if config.has_envelope_list_datetime_local_tz() {
            self.date.with_timezone(&Local).format(&fmt)
        } else {
            self.date.format(&fmt)
        };

        date.to_string()
    }
}

#[cfg(feature = "thread")]
impl PartialEq for ThreadedEnvelope<'_> {
    fn eq(&self, other: &Self) -> bool {
        self.message_id == other.message_id
    }
}

#[cfg(feature = "thread")]
impl Hash for ThreadedEnvelope<'_> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.message_id.hash(state);
    }
}

#[cfg_attr(feature = "thread", ouroboros::self_referencing)]
#[derive(Debug)]
pub struct ThreadedEnvelopes {
    #[cfg(feature = "thread")]
    inner: std::collections::HashMap<String, Envelope>,
    #[cfg(feature = "thread")]
    #[borrows(inner)]
    #[covariant]
    graph: DiGraphMap<ThreadedEnvelope<'this>, u8>,
}

#[cfg(feature = "thread")]
impl ThreadedEnvelopes {
    pub fn build(
        envelopes: HashMap<String, Envelope>,
        f: impl Fn(&HashMap<String, Envelope>) -> DiGraphMap<ThreadedEnvelope, u8>,
    ) -> Self {
        ThreadedEnvelopes::new(envelopes, f)
    }

    pub fn map(&self) -> &HashMap<String, Envelope> {
        self.borrow_inner()
    }

    pub fn graph(&self) -> &DiGraphMap<ThreadedEnvelope<'_>, u8> {
        self.borrow_graph()
    }
}

#[cfg(all(feature = "thread", feature = "derive"))]
impl serde::Serialize for ThreadedEnvelopes {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::ser::SerializeSeq;

        let count = self.graph().all_edges().count();
        let mut seq = serializer.serialize_seq(Some(count))?;

        for ref edge in self.graph().all_edges() {
            seq.serialize_element(edge)?;
        }

        serde::ser::SerializeSeq::end(seq)
    }
}

#[cfg(all(feature = "thread", feature = "derive"))]
impl<'de> serde::Deserialize<'de> for ThreadedEnvelopes {
    fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        todo!()
    }
}