io_imap/rfc3501/
logout.rs1use core::fmt;
42
43use alloc::string::{String, ToString};
44
45use imap_codec::{
46 CommandCodec,
47 fragmentizer::Fragmentizer,
48 imap_types::{
49 command::{Command, CommandBody},
50 core::TagGenerator,
51 response::{StatusKind, Tagged},
52 },
53};
54use log::trace;
55use thiserror::Error;
56
57use crate::{coroutine::*, imap_try, send::*};
58
59#[derive(Clone, Debug, Error)]
61pub enum ImapLogoutError {
62 #[error("IMAP LOGOUT failed: NO {0}")]
63 No(String),
64 #[error("IMAP LOGOUT failed: BAD {0}")]
65 Bad(String),
66
67 #[error("IMAP LOGOUT failed: server did not return a tagged response")]
68 MissingTagged,
69 #[error("IMAP LOGOUT failed: server did not send the expected BYE")]
70 MissingBye,
71
72 #[error("IMAP LOGOUT failed: {0}")]
73 Send(#[from] SendImapCommandError),
74}
75
76pub struct ImapLogout {
78 state: State,
79}
80
81impl ImapLogout {
82 pub fn new() -> Self {
83 let command = Command {
84 tag: TagGenerator::new().generate(),
85 body: CommandBody::Logout,
86 };
87
88 trace!("send IMAP command {command:?}");
89
90 let state = State::Send(SendImapCommand::new(CommandCodec::new(), command));
91
92 Self { state }
93 }
94}
95
96impl Default for ImapLogout {
97 fn default() -> Self {
98 Self::new()
99 }
100}
101
102impl ImapCoroutine for ImapLogout {
103 type Yield = ImapYield;
104 type Return = Result<(), ImapLogoutError>;
105
106 fn resume(
107 &mut self,
108 fragmentizer: &mut Fragmentizer,
109 arg: Option<&[u8]>,
110 ) -> ImapCoroutineState<Self::Yield, Self::Return> {
111 loop {
112 trace!("logout: {}", self.state);
113
114 match &mut self.state {
115 State::Send(send) => {
116 let out = imap_try!(send, fragmentizer, arg);
117
118 if out.bye.is_none() {
119 return ImapCoroutineState::Complete(Err(ImapLogoutError::MissingBye));
120 }
121
122 let Some(Tagged { body, .. }) = out.tagged else {
123 return ImapCoroutineState::Complete(Err(ImapLogoutError::MissingTagged));
124 };
125
126 return match body.kind {
127 StatusKind::Ok => ImapCoroutineState::Complete(Ok(())),
128 StatusKind::No => {
129 let err = ImapLogoutError::No(body.text.to_string());
130 ImapCoroutineState::Complete(Err(err))
131 }
132 StatusKind::Bad => {
133 let err = ImapLogoutError::Bad(body.text.to_string());
134 ImapCoroutineState::Complete(Err(err))
135 }
136 };
137 }
138 }
139 }
140 }
141}
142
143enum State {
144 Send(SendImapCommand<CommandCodec>),
145}
146
147impl fmt::Display for State {
148 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149 match self {
150 Self::Send(_) => f.write_str("send logout"),
151 }
152 }
153}
154
155#[cfg(test)]
156mod tests {
157 use core::str;
158
159 use alloc::{borrow::ToOwned, vec::Vec};
160
161 use super::*;
162
163 #[test]
164 fn success_returns_ok() {
165 let mut logout = ImapLogout::new();
166 let mut frag = Fragmentizer::new(50 * 1024 * 1024);
167
168 let bytes = expect_wants_write(&mut logout, &mut frag, None);
169 let line = str::from_utf8(&bytes).expect("utf8 command");
170 let tag = first_word(line).to_owned();
171 assert!(line.trim_end().ends_with("LOGOUT"));
172
173 expect_wants_read(&mut logout, &mut frag);
174
175 let reply = format!("* BYE bye\r\n{tag} OK LOGOUT completed\r\n");
176 expect_complete_ok(&mut logout, &mut frag, reply.as_bytes());
177 }
178
179 #[test]
180 fn missing_bye_returns_missing_bye_error() {
181 let mut logout = ImapLogout::new();
182 let mut frag = Fragmentizer::new(50 * 1024 * 1024);
183
184 let bytes = expect_wants_write(&mut logout, &mut frag, None);
185 let tag = first_word(str::from_utf8(&bytes).expect("utf8 command")).to_owned();
186
187 expect_wants_read(&mut logout, &mut frag);
188
189 let reply = format!("{tag} OK LOGOUT completed\r\n");
190 let err = expect_complete_err(&mut logout, &mut frag, reply.as_bytes());
191 assert!(matches!(err, ImapLogoutError::MissingBye));
192 }
193
194 #[test]
195 fn tagged_bad_returns_bad_error() {
196 let mut logout = ImapLogout::new();
197 let mut frag = Fragmentizer::new(50 * 1024 * 1024);
198
199 let bytes = expect_wants_write(&mut logout, &mut frag, None);
200 let tag = first_word(str::from_utf8(&bytes).expect("utf8 command")).to_owned();
201
202 expect_wants_read(&mut logout, &mut frag);
203
204 let reply = format!("* BYE bye\r\n{tag} BAD LOGOUT not allowed\r\n");
205 let err = expect_complete_err(&mut logout, &mut frag, reply.as_bytes());
206 let ImapLogoutError::Bad(text) = err else {
207 panic!("expected ImapLogoutError::Bad, got {err:?}");
208 };
209 assert_eq!(text, "LOGOUT not allowed");
210 }
211
212 fn expect_wants_write(
215 cor: &mut ImapLogout,
216 frag: &mut Fragmentizer,
217 arg: Option<&[u8]>,
218 ) -> Vec<u8> {
219 match cor.resume(frag, arg) {
220 ImapCoroutineState::Yielded(ImapYield::WantsWrite(bytes)) => bytes,
221 state => panic!("expected WantsWrite, got {state:?}"),
222 }
223 }
224
225 fn expect_wants_read(cor: &mut ImapLogout, frag: &mut Fragmentizer) {
226 match cor.resume(frag, None) {
227 ImapCoroutineState::Yielded(ImapYield::WantsRead) => {}
228 state => panic!("expected WantsRead, got {state:?}"),
229 }
230 }
231
232 fn expect_complete_ok(cor: &mut ImapLogout, frag: &mut Fragmentizer, reply: &[u8]) {
233 match cor.resume(frag, Some(reply)) {
234 ImapCoroutineState::Complete(Ok(())) => {}
235 state => panic!("expected Complete(Ok), got {state:?}"),
236 }
237 }
238
239 fn expect_complete_err(
240 cor: &mut ImapLogout,
241 frag: &mut Fragmentizer,
242 reply: &[u8],
243 ) -> ImapLogoutError {
244 match cor.resume(frag, Some(reply)) {
245 ImapCoroutineState::Complete(Err(err)) => err,
246 state => panic!("expected Complete(Err), got {state:?}"),
247 }
248 }
249
250 fn first_word(line: &str) -> &str {
251 line.split_whitespace()
252 .next()
253 .expect("first whitespace-separated token")
254 }
255}