aboutsummaryrefslogtreecommitdiff
path: root/src/imap/capability.rs
blob: c76b51c0b5548d345218dc6764d4cdebbc620f70 (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
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
use imap_codec::imap_types::command::{FetchModifier, SelectExamineModifier, StoreModifier};
use imap_codec::imap_types::core::Vec1;
use imap_codec::imap_types::extensions::enable::{CapabilityEnable, Utf8Kind};
use imap_codec::imap_types::response::Capability;
use std::collections::HashSet;

use crate::imap::attributes::AttributesProxy;

fn capability_unselect() -> Capability<'static> {
    Capability::try_from("UNSELECT").unwrap()
}

fn capability_condstore() -> Capability<'static> {
    Capability::try_from("CONDSTORE").unwrap()
}

fn capability_uidplus() -> Capability<'static> {
    Capability::try_from("UIDPLUS").unwrap()
}

fn capability_liststatus() -> Capability<'static> {
    Capability::try_from("LIST-STATUS").unwrap()
}

/*
fn capability_qresync() -> Capability<'static> {
    Capability::try_from("QRESYNC").unwrap()
}
*/

#[derive(Debug, Clone)]
pub struct ServerCapability(HashSet<Capability<'static>>);

impl Default for ServerCapability {
    fn default() -> Self {
        Self(HashSet::from([
            Capability::Imap4Rev1,
            Capability::Enable,
            Capability::Move,
            Capability::LiteralPlus,
            Capability::Idle,
            capability_unselect(),
            capability_condstore(),
            capability_uidplus(),
            capability_liststatus(),
            //capability_qresync(),
        ]))
    }
}

impl ServerCapability {
    pub fn to_vec(&self) -> Vec1<Capability<'static>> {
        self.0
            .iter()
            .map(|v| v.clone())
            .collect::<Vec<_>>()
            .try_into()
            .unwrap()
    }

    #[allow(dead_code)]
    pub fn support(&self, cap: &Capability<'static>) -> bool {
        self.0.contains(cap)
    }
}

#[derive(Clone)]
pub enum ClientStatus {
    NotSupportedByServer,
    Disabled,
    Enabled,
}
impl ClientStatus {
    pub fn is_enabled(&self) -> bool {
        matches!(self, Self::Enabled)
    }

    pub fn enable(&self) -> Self {
        match self {
            Self::Disabled => Self::Enabled,
            other => other.clone(),
        }
    }
}

pub struct ClientCapability {
    pub condstore: ClientStatus,
    pub utf8kind: Option<Utf8Kind>,
}

impl ClientCapability {
    pub fn new(sc: &ServerCapability) -> Self {
        Self {
            condstore: match sc.0.contains(&capability_condstore()) {
                true => ClientStatus::Disabled,
                _ => ClientStatus::NotSupportedByServer,
            },
            utf8kind: None,
        }
    }

    pub fn enable_condstore(&mut self) {
        self.condstore = self.condstore.enable();
    }

    pub fn attributes_enable(&mut self, ap: &AttributesProxy) {
        if ap.is_enabling_condstore() {
            self.enable_condstore()
        }
    }

    pub fn fetch_modifiers_enable(&mut self, mods: &[FetchModifier]) {
        if mods
            .iter()
            .any(|x| matches!(x, FetchModifier::ChangedSince(..)))
        {
            self.enable_condstore()
        }
    }

    pub fn store_modifiers_enable(&mut self, mods: &[StoreModifier]) {
        if mods
            .iter()
            .any(|x| matches!(x, StoreModifier::UnchangedSince(..)))
        {
            self.enable_condstore()
        }
    }

    pub fn select_enable(&mut self, mods: &[SelectExamineModifier]) {
        for m in mods.iter() {
            match m {
                SelectExamineModifier::Condstore => self.enable_condstore(),
            }
        }
    }

    pub fn try_enable(
        &mut self,
        caps: &[CapabilityEnable<'static>],
    ) -> Vec<CapabilityEnable<'static>> {
        let mut enabled = vec![];
        for cap in caps {
            match cap {
                CapabilityEnable::CondStore if matches!(self.condstore, ClientStatus::Disabled) => {
                    self.condstore = ClientStatus::Enabled;
                    enabled.push(cap.clone());
                }
                CapabilityEnable::Utf8(kind) if Some(kind) != self.utf8kind.as_ref() => {
                    self.utf8kind = Some(kind.clone());
                    enabled.push(cap.clone());
                }
                _ => (),
            }
        }

        enabled
    }
}