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
use std::collections::HashSet;
use chrono::{DateTime, Local};
use panorama_imap::response::*;
#[derive(Clone, Debug, Default)]
pub struct EmailMetadata {
pub uid: Option<u32>,
pub unread: bool,
pub date: Option<DateTime<Local>>,
pub from: String,
pub subject: String,
}
impl EmailMetadata {
pub fn from_attrs(attrs: Vec<AttributeValue>) -> Self {
let mut meta = EmailMetadata::default();
for attr in attrs {
match attr {
AttributeValue::Flags(flags) => {
let flags = flags.into_iter().collect::<HashSet<_>>();
if !flags.contains(&MailboxFlag::Seen) {
meta.unread = true;
}
}
AttributeValue::Uid(new_uid) => meta.uid = Some(new_uid),
AttributeValue::InternalDate(new_date) => {
meta.date = Some(new_date.with_timezone(&Local));
}
AttributeValue::Envelope(Envelope {
subject: new_subject,
from: new_from,
..
}) => {
if let Some(new_from) = new_from {
meta.from = new_from
.iter()
.filter_map(|addr| addr.name.to_owned())
.collect::<Vec<_>>()
.join(", ");
}
if let Some(new_subject) = new_subject {
use quoted_printable::ParseMode;
let new_subject =
quoted_printable::decode(new_subject.as_bytes(), ParseMode::Robust)
.unwrap();
let new_subject = String::from_utf8(new_subject).unwrap();
meta.subject = new_subject;
}
}
_ => {}
}
}
meta
}
}