aboutsummaryrefslogtreecommitdiff
path: root/src/imap/attributes.rs
blob: 6cf52b559dfbd29c9a744c67e46024d796e5e2c4 (plain) (blame)
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
use imap_codec::imap_types::fetch::{MacroOrMessageDataItemNames, MessageDataItemName, Section};

/// Internal decisions based on fetched attributes
/// passed by the client

pub struct AttributesProxy {
    pub attrs: Vec<MessageDataItemName<'static>>,
}
impl AttributesProxy {
    pub fn new(attrs: &MacroOrMessageDataItemNames<'static>, is_uid_fetch: bool) -> Self {
        // Expand macros
        let mut fetch_attrs = match attrs {
            MacroOrMessageDataItemNames::Macro(m) => {
                use imap_codec::imap_types::fetch::Macro;
                use MessageDataItemName::*;
                match m {
                    Macro::All => vec![Flags, InternalDate, Rfc822Size, Envelope],
                    Macro::Fast => vec![Flags, InternalDate, Rfc822Size],
                    Macro::Full => vec![Flags, InternalDate, Rfc822Size, Envelope, Body],
                    _ => {
                        tracing::error!("unimplemented macro");
                        vec![]
                    }
                }
            }
            MacroOrMessageDataItemNames::MessageDataItemNames(a) => a.clone(),
        };

        // Handle uids
        if is_uid_fetch && !fetch_attrs.contains(&MessageDataItemName::Uid) {
            fetch_attrs.push(MessageDataItemName::Uid);
        }

        Self { attrs: fetch_attrs }
    }

    pub fn is_enabling_condstore(&self) -> bool {
        self.attrs.iter().any(|x| {
            matches!(x, MessageDataItemName::ModSeq)
        })
    }

    pub fn need_body(&self) -> bool {
        self.attrs.iter().any(|x| {
            match x {
                MessageDataItemName::Body
                | MessageDataItemName::Rfc822
                | MessageDataItemName::Rfc822Text
                | MessageDataItemName::BodyStructure => true,

                MessageDataItemName::BodyExt {
                    section: Some(section),
                    partial: _,
                    peek: _,
                } => match section {
                    Section::Header(None)
                    | Section::HeaderFields(None, _)
                    | Section::HeaderFieldsNot(None, _) => false,
                    _ => true,
                },
                MessageDataItemName::BodyExt { .. } => true,
                _ => false,
            }
        })
    }
}