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