1use core::fmt;
46
47use alloc::{string::String, string::ToString, vec::Vec};
48
49use imap_codec::{
50 CommandCodec,
51 fragmentizer::Fragmentizer,
52 imap_types::{
53 command::{Command, CommandBody},
54 core::{TagGenerator, Vec1},
55 extensions::enable::CapabilityEnable,
56 response::{Data, StatusKind, Tagged},
57 },
58};
59use log::trace;
60use thiserror::Error;
61
62use crate::{coroutine::*, imap_try, send::*};
63
64#[derive(Clone, Debug, Error)]
66pub enum ImapExtensionEnableError {
67 #[error("IMAP ENABLE failed: NO {0}")]
68 No(String),
69 #[error("IMAP ENABLE failed: BAD {0}")]
70 Bad(String),
71 #[error("IMAP ENABLE failed: BYE {0}")]
72 Bye(String),
73
74 #[error("IMAP ENABLE failed: server did not return a tagged response")]
75 MissingTagged,
76
77 #[error("IMAP ENABLE failed: {0}")]
78 Send(#[from] SendImapCommandError),
79}
80
81pub struct ImapExtensionEnable {
83 state: State,
84}
85
86impl ImapExtensionEnable {
87 pub fn new(capabilities: Vec1<CapabilityEnable<'static>>) -> Self {
88 let command = Command {
89 tag: TagGenerator::new().generate(),
90 body: CommandBody::Enable { capabilities },
91 };
92
93 trace!("send IMAP command {command:?}");
94
95 let state = State::Send(SendImapCommand::new(CommandCodec::new(), command));
96
97 Self { state }
98 }
99}
100
101impl ImapCoroutine for ImapExtensionEnable {
102 type Yield = ImapYield;
103 type Return = Result<Option<Vec<CapabilityEnable<'static>>>, ImapExtensionEnableError>;
104
105 fn resume(
106 &mut self,
107 fragmentizer: &mut Fragmentizer,
108 arg: Option<&[u8]>,
109 ) -> ImapCoroutineState<Self::Yield, Self::Return> {
110 loop {
111 trace!("enable: {}", self.state);
112
113 match &mut self.state {
114 State::Send(send) => {
115 let out = imap_try!(send, fragmentizer, arg);
116
117 if let Some(bye) = out.bye {
118 let err = ImapExtensionEnableError::Bye(bye.text.to_string());
119 return ImapCoroutineState::Complete(Err(err));
120 }
121
122 let Some(Tagged { body, .. }) = out.tagged else {
123 let err = ImapExtensionEnableError::MissingTagged;
124 return ImapCoroutineState::Complete(Err(err));
125 };
126
127 let mut enabled = None;
128 for data in out.data {
129 if let Data::Enabled { capabilities } = data {
130 enabled = Some(capabilities);
131 }
132 }
133
134 return match body.kind {
135 StatusKind::Ok => ImapCoroutineState::Complete(Ok(enabled)),
136 StatusKind::No => {
137 let err = ImapExtensionEnableError::No(body.text.to_string());
138 ImapCoroutineState::Complete(Err(err))
139 }
140 StatusKind::Bad => {
141 let err = ImapExtensionEnableError::Bad(body.text.to_string());
142 ImapCoroutineState::Complete(Err(err))
143 }
144 };
145 }
146 }
147 }
148 }
149}
150
151enum State {
152 Send(SendImapCommand<CommandCodec>),
153}
154
155impl fmt::Display for State {
156 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157 match self {
158 Self::Send(_) => f.write_str("send enable"),
159 }
160 }
161}
162
163#[cfg(test)]
164mod tests {
165 use core::str;
166
167 use alloc::{borrow::ToOwned, vec, vec::Vec};
168
169 use super::*;
170
171 fn caps() -> Vec1<CapabilityEnable<'static>> {
172 Vec1::try_from(vec![CapabilityEnable::CondStore]).expect("one cap")
173 }
174
175 #[test]
176 fn success_returns_enabled_list() {
177 let mut enable = ImapExtensionEnable::new(caps());
178 let mut frag = Fragmentizer::new(50 * 1024 * 1024);
179
180 let bytes = expect_wants_write(&mut enable, &mut frag, None);
181 let line = str::from_utf8(&bytes).expect("utf8 command");
182 let tag = first_word(line).to_owned();
183 assert!(line.contains("ENABLE CONDSTORE"));
184
185 expect_wants_read(&mut enable, &mut frag);
186
187 let reply = format!("* ENABLED CONDSTORE\r\n{tag} OK ENABLE completed\r\n");
188 let enabled = expect_complete_ok(&mut enable, &mut frag, reply.as_bytes())
189 .expect("server returned ENABLED");
190 assert_eq!(1, enabled.len());
191 }
192
193 #[test]
194 fn success_without_enabled_returns_none() {
195 let mut enable = ImapExtensionEnable::new(caps());
196 let mut frag = Fragmentizer::new(50 * 1024 * 1024);
197
198 let bytes = expect_wants_write(&mut enable, &mut frag, None);
199 let tag = first_word(str::from_utf8(&bytes).expect("utf8 command")).to_owned();
200
201 expect_wants_read(&mut enable, &mut frag);
202
203 let reply = format!("{tag} OK ENABLE completed\r\n");
204 let enabled = expect_complete_ok(&mut enable, &mut frag, reply.as_bytes());
205 assert!(enabled.is_none());
206 }
207
208 #[test]
209 fn tagged_no_returns_no_error() {
210 let mut enable = ImapExtensionEnable::new(caps());
211 let mut frag = Fragmentizer::new(50 * 1024 * 1024);
212
213 let bytes = expect_wants_write(&mut enable, &mut frag, None);
214 let tag = first_word(str::from_utf8(&bytes).expect("utf8 command")).to_owned();
215
216 expect_wants_read(&mut enable, &mut frag);
217
218 let reply = format!("{tag} NO CONDSTORE not supported\r\n");
219 let err = expect_complete_err(&mut enable, &mut frag, reply.as_bytes());
220 let ImapExtensionEnableError::No(text) = err else {
221 panic!("expected ImapExtensionEnableError::No, got {err:?}");
222 };
223 assert_eq!(text, "CONDSTORE not supported");
224 }
225
226 #[test]
227 fn bye_returns_bye_error() {
228 let mut enable = ImapExtensionEnable::new(caps());
229 let mut frag = Fragmentizer::new(50 * 1024 * 1024);
230
231 let _ = expect_wants_write(&mut enable, &mut frag, None);
232 expect_wants_read(&mut enable, &mut frag);
233
234 let err = expect_complete_err(&mut enable, &mut frag, b"* BYE going down\r\n");
235 let ImapExtensionEnableError::Bye(text) = err else {
236 panic!("expected ImapExtensionEnableError::Bye, got {err:?}");
237 };
238 assert_eq!(text, "going down");
239 }
240
241 fn expect_wants_write(
244 cor: &mut ImapExtensionEnable,
245 frag: &mut Fragmentizer,
246 arg: Option<&[u8]>,
247 ) -> Vec<u8> {
248 match cor.resume(frag, arg) {
249 ImapCoroutineState::Yielded(ImapYield::WantsWrite(bytes)) => bytes,
250 state => panic!("expected WantsWrite, got {state:?}"),
251 }
252 }
253
254 fn expect_wants_read(cor: &mut ImapExtensionEnable, frag: &mut Fragmentizer) {
255 match cor.resume(frag, None) {
256 ImapCoroutineState::Yielded(ImapYield::WantsRead) => {}
257 state => panic!("expected WantsRead, got {state:?}"),
258 }
259 }
260
261 fn expect_complete_ok(
262 cor: &mut ImapExtensionEnable,
263 frag: &mut Fragmentizer,
264 reply: &[u8],
265 ) -> Option<Vec<CapabilityEnable<'static>>> {
266 match cor.resume(frag, Some(reply)) {
267 ImapCoroutineState::Complete(Ok(value)) => value,
268 state => panic!("expected Complete(Ok), got {state:?}"),
269 }
270 }
271
272 fn expect_complete_err(
273 cor: &mut ImapExtensionEnable,
274 frag: &mut Fragmentizer,
275 reply: &[u8],
276 ) -> ImapExtensionEnableError {
277 match cor.resume(frag, Some(reply)) {
278 ImapCoroutineState::Complete(Err(err)) => err,
279 state => panic!("expected Complete(Err), got {state:?}"),
280 }
281 }
282
283 fn first_word(line: &str) -> &str {
284 line.split_whitespace()
285 .next()
286 .expect("first whitespace-separated token")
287 }
288}