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::*;

/// A record that describes the metadata of an email as it appears in the UI list
#[derive(Clone, Debug, Default)]
pub struct EmailMetadata {
    /// UID if the message has one
    pub uid: Option<u32>,

    /// Whether or not this message is unread
    pub unread: bool,

    /// Date
    pub date: Option<DateTime<Local>>,

    /// Sender
    pub from: String,

    /// Subject
    pub subject: String,
}

impl EmailMetadata {
    /// Construct an EmailMetadata from a list of attributes retrieved from the server
    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 {
                        // TODO: probably shouldn't insert quoted-printable here
                        // but this is just the most convenient for it to look right at the moment
                        // there's probably some header that indicates it's quoted-printable
                        // MIME?
                        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
    }
}