io-imap 0.4.0

IMAP client library for Rust
Documentation
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! IMAP CAPABILITY coroutine returning the advertised capability list.
//!
//! # Example
//!
//! ```rust,no_run
//! use std::{
//!     io::{Read, Write},
//!     net::TcpStream,
//! };
//!
//! use io_imap::{
//!     codec::fragmentizer::Fragmentizer,
//!     coroutine::{ImapCoroutine, ImapCoroutineState, ImapYield},
//!     rfc3501::capability::ImapCapabilityGet,
//! };
//!
//! // Ready stream needed (TCP-connected, TLS-negotiated, IMAP-authenticated)
//! let mut stream = TcpStream::connect("localhost:143").unwrap();
//!
//! let mut fragmentizer = Fragmentizer::new(50 * 1024 * 1024);
//! let mut buf = [0u8; 4096];
//!
//! let mut coroutine = ImapCapabilityGet::new();
//! let mut arg = None;
//!
//! let capability = loop {
//!     match coroutine.resume(&mut fragmentizer, arg.take()) {
//!         ImapCoroutineState::Yielded(ImapYield::WantsWrite(bytes)) => {
//!             stream.write_all(&bytes).unwrap();
//!         }
//!         ImapCoroutineState::Yielded(ImapYield::WantsRead) => {
//!             let n = stream.read(&mut buf).unwrap();
//!             arg = Some(&buf[..n]);
//!         }
//!         ImapCoroutineState::Complete(Ok(capability)) => break capability,
//!         ImapCoroutineState::Complete(Err(err)) => panic!("{err}"),
//!     }
//! };
//!
//! println!("{capability:?}");
//! ```

use core::fmt;

use alloc::{string::String, string::ToString, vec::Vec};

use imap_codec::{
    CommandCodec,
    fragmentizer::Fragmentizer,
    imap_types::{
        auth::AuthMechanism,
        command::{Command, CommandBody},
        core::TagGenerator,
        response::{Capability, Code, Data, StatusBody, StatusKind, Tagged},
    },
};
use log::trace;
use pimalaya_stream::sasl::SaslMechanism;
use thiserror::Error;

use crate::{coroutine::*, imap_try, send::*};

/// Failure causes during the IMAP CAPABILITY flow.
#[derive(Clone, Debug, Error)]
pub enum ImapCapabilityGetError {
    /// The server rejected the command with a NO response.
    #[error("IMAP CAPABILITY failed: NO {0}")]
    No(String),
    /// The server rejected the command with a BAD response.
    #[error("IMAP CAPABILITY failed: BAD {0}")]
    Bad(String),
    /// The server closed the session with an untagged BYE.
    #[error("IMAP CAPABILITY failed: BYE {0}")]
    Bye(String),
    /// The exchange ended without a tagged response from the server.
    #[error("IMAP CAPABILITY failed: server did not return a tagged response")]
    MissingTagged,
    /// The response carried no capability list, neither as untagged
    /// data nor as a response code.
    #[error("IMAP CAPABILITY failed: server did not advertise any capability")]
    MissingCapability,
    /// The underlying send/receive exchange failed (EOF, decode, framing).
    #[error("IMAP CAPABILITY failed: {0}")]
    Send(#[from] ImapSendError),
}

/// I/O-free IMAP CAPABILITY coroutine.
pub struct ImapCapabilityGet {
    state: State,
}

impl ImapCapabilityGet {
    /// Builds a CAPABILITY coroutine requesting the advertised
    /// capability list.
    pub fn new() -> Self {
        let command = Command {
            tag: TagGenerator::new().generate(),
            body: CommandBody::Capability,
        };

        trace!("send IMAP command {command:?}");

        let state = State::Send(ImapSend::new(CommandCodec::new(), command));

        Self { state }
    }
}

impl Default for ImapCapabilityGet {
    fn default() -> Self {
        Self::new()
    }
}

impl ImapCoroutine for ImapCapabilityGet {
    type Yield = ImapYield;
    type Return = Result<Vec<Capability<'static>>, ImapCapabilityGetError>;

    fn resume(
        &mut self,
        fragmentizer: &mut Fragmentizer,
        arg: Option<&[u8]>,
    ) -> ImapCoroutineState<Self::Yield, Self::Return> {
        match &mut self.state {
            State::Send(send) => {
                let out = imap_try!(send, fragmentizer, arg);

                if let Some(bye) = out.bye {
                    let err = ImapCapabilityGetError::Bye(bye.text.to_string());
                    return ImapCoroutineState::Complete(Err(err));
                }

                let Some(Tagged { body, .. }) = out.tagged else {
                    let err = ImapCapabilityGetError::MissingTagged;
                    return ImapCoroutineState::Complete(Err(err));
                };

                let code = match body.kind {
                    StatusKind::Ok => body.code,
                    StatusKind::No => {
                        let err = ImapCapabilityGetError::No(body.text.to_string());
                        return ImapCoroutineState::Complete(Err(err));
                    }
                    StatusKind::Bad => {
                        let err = ImapCapabilityGetError::Bad(body.text.to_string());
                        return ImapCoroutineState::Complete(Err(err));
                    }
                };

                let mut new_capability = None;

                if let Some(Code::Capability(capability)) = code {
                    new_capability.replace(capability);
                }

                for data in out.data {
                    if let Data::Capability(capability) = data {
                        new_capability.replace(capability);
                    }
                }

                for StatusBody { code, .. } in out.untagged {
                    if let Some(Code::Capability(capability)) = code {
                        new_capability.replace(capability);
                    }
                }

                let Some(capability) = new_capability else {
                    let err = ImapCapabilityGetError::MissingCapability;
                    return ImapCoroutineState::Complete(Err(err));
                };

                ImapCoroutineState::Complete(Ok(capability.into_iter().collect()))
            }
        }
    }
}

/// Returns the authentication mechanisms available for `capabilities`,
/// most preferred first and the legacy `LOGIN` command last, as the
/// [`SaslMechanism`] tags a client authenticates with.
///
/// Every advertised `AUTH=` capability maps to its mechanism; unknown
/// ones are ignored. `SaslMechanism::Login` (the plain IMAP `LOGIN`
/// command, per this crate's SASL convention) is offered unless the
/// server advertises `LOGINDISABLED`. A perdition-style proxy
/// advertising a bare `IMAP4 IMAP4REV1`, with no `AUTH=` and no
/// `LOGINDISABLED`, therefore yields just `[SaslMechanism::Login]`.
pub fn available_auth_mechanisms(capabilities: &[Capability]) -> Vec<SaslMechanism> {
    let advertises = |name: &str| {
        capabilities.iter().any(|capability| match capability {
            Capability::Auth(mechanism) => {
                AuthMechanism::as_ref(mechanism).eq_ignore_ascii_case(name)
            }
            _ => false,
        })
    };

    let mut mechanisms = Vec::new();

    if advertises("SCRAM-SHA-256") {
        mechanisms.push(SaslMechanism::ScramSha256);
    }
    if advertises("PLAIN") {
        mechanisms.push(SaslMechanism::Plain);
    }
    if advertises("OAUTHBEARER") {
        mechanisms.push(SaslMechanism::OAuthBearer);
    }
    if advertises("XOAUTH2") {
        mechanisms.push(SaslMechanism::XOAuth2);
    }
    if advertises("ANONYMOUS") {
        mechanisms.push(SaslMechanism::Anonymous);
    }

    // The IMAP LOGIN command is the last-resort mechanism, available
    // unless the server explicitly disabled it.
    let login_disabled = capabilities
        .iter()
        .any(|capability| matches!(capability, Capability::LoginDisabled));
    if !login_disabled {
        mechanisms.push(SaslMechanism::Login);
    }

    mechanisms
}

enum State {
    Send(ImapSend<CommandCodec>),
}

impl fmt::Display for State {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Send(_) => f.write_str("send capability"),
        }
    }
}

#[cfg(test)]
mod tests {
    use core::str;

    use alloc::{borrow::ToOwned, format};

    use crate::rfc3501::capability::*;

    #[test]
    fn data_capability_returns_capabilities() {
        let mut cap = ImapCapabilityGet::new();
        let mut frag = Fragmentizer::new(50 * 1024 * 1024);

        let bytes = expect_wants_write(&mut cap, &mut frag, None);
        let line = str::from_utf8(&bytes).expect("utf8 command");
        let tag = first_word(line).to_owned();
        assert!(line.trim_end().ends_with("CAPABILITY"));

        expect_wants_read(&mut cap, &mut frag);

        let reply =
            format!("* CAPABILITY IMAP4REV1 STARTTLS IDLE\r\n{tag} OK CAPABILITY completed\r\n");
        let caps = expect_complete_ok(&mut cap, &mut frag, reply.as_bytes());
        assert_eq!(3, caps.len());
        assert!(caps.contains(&Capability::Imap4Rev1));
        assert!(caps.contains(&Capability::StartTls));
        assert!(caps.contains(&Capability::Idle));
    }

    #[test]
    fn tagged_code_capability_returns_capabilities() {
        let mut cap = ImapCapabilityGet::new();
        let mut frag = Fragmentizer::new(50 * 1024 * 1024);

        let bytes = expect_wants_write(&mut cap, &mut frag, None);
        let tag = first_word(str::from_utf8(&bytes).expect("utf8 command")).to_owned();

        expect_wants_read(&mut cap, &mut frag);

        let reply = format!("{tag} OK [CAPABILITY IMAP4REV1 IDLE] done\r\n");
        let caps = expect_complete_ok(&mut cap, &mut frag, reply.as_bytes());
        assert_eq!(2, caps.len());
    }

    #[test]
    fn no_capability_returns_missing_error() {
        let mut cap = ImapCapabilityGet::new();
        let mut frag = Fragmentizer::new(50 * 1024 * 1024);

        let bytes = expect_wants_write(&mut cap, &mut frag, None);
        let tag = first_word(str::from_utf8(&bytes).expect("utf8 command")).to_owned();

        expect_wants_read(&mut cap, &mut frag);

        let reply = format!("{tag} OK CAPABILITY completed\r\n");
        let err = expect_complete_err(&mut cap, &mut frag, reply.as_bytes());
        assert!(matches!(err, ImapCapabilityGetError::MissingCapability));
    }

    #[test]
    fn tagged_no_returns_no_error() {
        let mut cap = ImapCapabilityGet::new();
        let mut frag = Fragmentizer::new(50 * 1024 * 1024);

        let bytes = expect_wants_write(&mut cap, &mut frag, None);
        let tag = first_word(str::from_utf8(&bytes).expect("utf8 command")).to_owned();

        expect_wants_read(&mut cap, &mut frag);

        let reply = format!("{tag} NO server is sulking\r\n");
        let err = expect_complete_err(&mut cap, &mut frag, reply.as_bytes());
        let ImapCapabilityGetError::No(text) = err else {
            panic!("expected ImapCapabilityGetError::No, got {err:?}");
        };
        assert_eq!(text, "server is sulking");
    }

    #[test]
    fn bye_returns_bye_error() {
        let mut cap = ImapCapabilityGet::new();
        let mut frag = Fragmentizer::new(50 * 1024 * 1024);

        let _ = expect_wants_write(&mut cap, &mut frag, None);
        expect_wants_read(&mut cap, &mut frag);

        let err = expect_complete_err(&mut cap, &mut frag, b"* BYE going down\r\n");
        let ImapCapabilityGetError::Bye(text) = err else {
            panic!("expected ImapCapabilityGetError::Bye, got {err:?}");
        };
        assert_eq!(text, "going down");
    }

    fn expect_wants_write(
        cor: &mut ImapCapabilityGet,
        frag: &mut Fragmentizer,
        arg: Option<&[u8]>,
    ) -> Vec<u8> {
        match cor.resume(frag, arg) {
            ImapCoroutineState::Yielded(ImapYield::WantsWrite(bytes)) => bytes,
            state => panic!("expected WantsWrite, got {state:?}"),
        }
    }

    fn expect_wants_read(cor: &mut ImapCapabilityGet, frag: &mut Fragmentizer) {
        match cor.resume(frag, None) {
            ImapCoroutineState::Yielded(ImapYield::WantsRead) => {}
            state => panic!("expected WantsRead, got {state:?}"),
        }
    }

    fn expect_complete_ok(
        cor: &mut ImapCapabilityGet,
        frag: &mut Fragmentizer,
        reply: &[u8],
    ) -> Vec<Capability<'static>> {
        match cor.resume(frag, Some(reply)) {
            ImapCoroutineState::Complete(Ok(value)) => value,
            state => panic!("expected Complete(Ok), got {state:?}"),
        }
    }

    fn expect_complete_err(
        cor: &mut ImapCapabilityGet,
        frag: &mut Fragmentizer,
        reply: &[u8],
    ) -> ImapCapabilityGetError {
        match cor.resume(frag, Some(reply)) {
            ImapCoroutineState::Complete(Err(err)) => err,
            state => panic!("expected Complete(Err), got {state:?}"),
        }
    }

    fn first_word(line: &str) -> &str {
        line.split_whitespace()
            .next()
            .expect("first whitespace-separated token")
    }

    fn auth(name: &str) -> Capability<'static> {
        Capability::Auth(AuthMechanism::try_from(name.to_owned()).expect("valid mechanism"))
    }

    #[test]
    fn bare_capabilities_offer_only_the_login_command() {
        // A perdition-style proxy: no AUTH=, no LOGINDISABLED.
        let caps = [Capability::Imap4Rev1];
        assert!(matches!(
            available_auth_mechanisms(&caps).as_slice(),
            [SaslMechanism::Login]
        ));
    }

    #[test]
    fn advertised_mechanisms_are_ordered_by_preference_with_login_last() {
        // Advertisement order is PLAIN then SCRAM, but SCRAM outranks it
        // and LOGIN always trails.
        let caps = [Capability::Imap4Rev1, auth("PLAIN"), auth("SCRAM-SHA-256")];
        assert!(matches!(
            available_auth_mechanisms(&caps).as_slice(),
            [
                SaslMechanism::ScramSha256,
                SaslMechanism::Plain,
                SaslMechanism::Login,
            ]
        ));
    }

    #[test]
    fn login_disabled_drops_the_login_command() {
        let caps = [auth("PLAIN"), Capability::LoginDisabled];
        assert!(matches!(
            available_auth_mechanisms(&caps).as_slice(),
            [SaslMechanism::Plain]
        ));
    }

    #[test]
    fn token_and_anonymous_mechanisms_are_recognised() {
        let caps = [auth("XOAUTH2"), auth("OAUTHBEARER"), auth("ANONYMOUS")];
        assert!(matches!(
            available_auth_mechanisms(&caps).as_slice(),
            [
                SaslMechanism::OAuthBearer,
                SaslMechanism::XOAuth2,
                SaslMechanism::Anonymous,
                SaslMechanism::Login,
            ]
        ));
    }
}