imap_client/tasks/tasks/
select.rs

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
use std::num::NonZeroU32;

use imap_next::imap_types::{
    command::CommandBody,
    flag::{Flag, FlagPerm},
    mailbox::Mailbox,
    response::{Code, Data, StatusBody, StatusKind},
};
use tracing::warn;

use super::TaskError;
use crate::tasks::Task;

#[derive(Clone, Debug, Default)]
pub struct SelectDataUnvalidated {
    // required untagged responses
    pub flags: Option<Vec<Flag<'static>>>,
    pub exists: Option<u32>,
    pub recent: Option<u32>,

    // required OK untagged responses
    pub unseen: Option<NonZeroU32>,
    pub permanent_flags: Option<Vec<FlagPerm<'static>>>,
    pub uid_next: Option<NonZeroU32>,
    pub uid_validity: Option<NonZeroU32>,
}

impl SelectDataUnvalidated {
    pub fn validate(self) -> Result<Self, TaskError> {
        if self.flags.is_none() {
            warn!("missing required FLAGS untagged response");
        }

        if self.exists.is_none() {
            warn!("missing required EXISTS untagged response");
        }

        if self.recent.is_none() {
            warn!("missing required RECENT untagged response");
        }

        if self.unseen.is_none() {
            warn!("missing required UNSEEN OK untagged response");
        }

        if self.permanent_flags.is_none() {
            warn!("missing required PERMANENTFLAGS OK untagged response");
        }

        if self.uid_next.is_none() {
            warn!("missing required UIDNEXT OK untagged response");
        }

        if self.uid_validity.is_none() {
            warn!("missing required UIDVALIDITY OK untagged response");
        }

        Ok(self)
    }
}

#[derive(Clone, Debug)]
pub struct SelectTask {
    mailbox: Mailbox<'static>,
    read_only: bool,
    output: SelectDataUnvalidated,
}

impl SelectTask {
    pub fn new(mailbox: Mailbox<'static>) -> Self {
        Self {
            mailbox,
            read_only: false,
            output: Default::default(),
        }
    }

    pub fn read_only(mailbox: Mailbox<'static>) -> Self {
        Self {
            mailbox,
            read_only: true,
            output: Default::default(),
        }
    }
}

impl Task for SelectTask {
    type Output = Result<SelectDataUnvalidated, TaskError>;

    fn command_body(&self) -> CommandBody<'static> {
        let mailbox = self.mailbox.clone();

        if self.read_only {
            CommandBody::Examine { mailbox }
        } else {
            CommandBody::Select { mailbox }
        }
    }

    fn process_data(&mut self, data: Data<'static>) -> Option<Data<'static>> {
        match data {
            Data::Flags(flags) => {
                self.output.flags = Some(flags);
                None
            }
            Data::Exists(count) => {
                self.output.exists = Some(count);
                None
            }
            Data::Recent(count) => {
                self.output.recent = Some(count);
                None
            }
            data => Some(data),
        }
    }

    fn process_untagged(
        &mut self,
        status_body: StatusBody<'static>,
    ) -> Option<StatusBody<'static>> {
        if let StatusKind::Ok = status_body.kind {
            match status_body.code {
                Some(Code::Unseen(seq)) => {
                    self.output.unseen = Some(seq);
                    None
                }
                Some(Code::PermanentFlags(flags)) => {
                    self.output.permanent_flags = Some(flags);
                    None
                }
                Some(Code::UidNext(uid)) => {
                    self.output.uid_next = Some(uid);
                    None
                }
                Some(Code::UidValidity(uid)) => {
                    self.output.uid_validity = Some(uid);
                    None
                }
                _ => Some(status_body),
            }
        } else {
            Some(status_body)
        }
    }

    fn process_tagged(self, status_body: StatusBody<'static>) -> Self::Output {
        match status_body.kind {
            StatusKind::Ok => self.output.validate(),
            StatusKind::No => Err(TaskError::UnexpectedNoResponse(status_body)),
            StatusKind::Bad => Err(TaskError::UnexpectedBadResponse(status_body)),
        }
    }
}