rusmes-smtp 0.1.2

Async SMTP server for RusMES — RFC 5321 compliant with STARTTLS, AUTH (PLAIN/LOGIN/CRAM-MD5/SCRAM-SHA-256), PIPELINING, DSN, and BDAT/CHUNKING
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! SMTP command parser using nom

use crate::command::{MailParam, SmtpCommand};
use nom::{
    branch::alt,
    bytes::complete::{tag_no_case, take_while1},
    character::complete::{char, space0, space1},
    combinator::{map, opt, rest},
    sequence::{delimited, preceded},
    IResult, Parser,
};
use rusmes_proto::MailAddress;

/// Parse a complete SMTP command line (ASCII-only address mode).
///
/// This is the standard entry point used during HELO sessions or before the
/// SMTPUTF8 capability is confirmed. Non-ASCII characters in the address
/// local-part cause a parse error.
pub fn parse_command(input: &str) -> Result<SmtpCommand, String> {
    let input = input.trim();
    parse_command_inner(input, false)
}

/// Parse a complete SMTP command line with optional SMTPUTF8 support.
///
/// When `smtputf8_session_active` is `true` the parser accepts non-ASCII
/// UTF-8 bytes in the address local-part (per RFC 6531 §3.3). In ASCII mode
/// (`false`) the behaviour is identical to [`parse_command`].
pub fn parse_command_smtputf8(
    input: &str,
    smtputf8_session_active: bool,
) -> Result<SmtpCommand, String> {
    let input = input.trim();
    parse_command_inner(input, smtputf8_session_active)
}

/// Internal parser dispatch.
fn parse_command_inner(input: &str, smtputf8: bool) -> Result<SmtpCommand, String> {
    if let Ok((_, cmd)) = smtp_command(input, smtputf8) {
        Ok(cmd)
    } else {
        Err(format!("Failed to parse command: {}", input))
    }
}

/// Parse any SMTP command, threading the SMTPUTF8 capability flag through
/// address-bearing commands (`MAIL FROM`, `RCPT TO`).
fn smtp_command(input: &str, smtputf8: bool) -> IResult<&str, SmtpCommand> {
    // Commands that carry mail addresses need the smtputf8 flag.
    let mail = |i| mail_command(i, smtputf8);
    let rcpt = |i| rcpt_command(i, smtputf8);

    alt((
        helo_command,
        ehlo_command,
        mail,
        rcpt,
        data_command,
        bdat_command,
        rset_command,
        noop_command,
        quit_command,
        vrfy_command,
        expn_command,
        help_command,
        starttls_command,
        auth_command,
    ))
    .parse(input)
}

/// Parse HELO command
fn helo_command(input: &str) -> IResult<&str, SmtpCommand> {
    map(
        preceded(tag_no_case("HELO"), preceded(space1, domain)),
        SmtpCommand::Helo,
    )
    .parse(input)
}

/// Parse EHLO command
fn ehlo_command(input: &str) -> IResult<&str, SmtpCommand> {
    map(
        preceded(tag_no_case("EHLO"), preceded(space1, domain)),
        SmtpCommand::Ehlo,
    )
    .parse(input)
}

/// Parse MAIL FROM command.
///
/// When `smtputf8` is `true` the address local-part may contain non-ASCII
/// UTF-8 bytes (RFC 6531). The parsed `SMTPUTF8` parameter (if present) is
/// threaded through unchanged — the session layer is responsible for checking
/// that the client sent EHLO before using SMTPUTF8.
fn mail_command(input: &str, smtputf8: bool) -> IResult<&str, SmtpCommand> {
    let (input, _) = tag_no_case("MAIL FROM:").parse(input)?;
    let (input, _) = space0(input)?;
    let (input, from) = reverse_path(input, smtputf8)?;
    let (input, params) = opt(preceded(space1, mail_parameters)).parse(input)?;

    Ok((
        input,
        SmtpCommand::Mail {
            from,
            params: params.unwrap_or_default(),
        },
    ))
}

/// Parse RCPT TO command.
///
/// When `smtputf8` is `true` the address local-part may contain non-ASCII
/// UTF-8 bytes (RFC 6531).
fn rcpt_command(input: &str, smtputf8: bool) -> IResult<&str, SmtpCommand> {
    let (input, _) = tag_no_case("RCPT TO:").parse(input)?;
    let (input, _) = space0(input)?;
    let (input, to) = forward_path(input, smtputf8)?;
    let (input, params) = opt(preceded(space1, mail_parameters)).parse(input)?;

    Ok((
        input,
        SmtpCommand::Rcpt {
            to,
            params: params.unwrap_or_default(),
        },
    ))
}

/// Parse DATA command
fn data_command(input: &str) -> IResult<&str, SmtpCommand> {
    map(tag_no_case("DATA"), |_| SmtpCommand::Data).parse(input)
}

/// Parse BDAT command
fn bdat_command(input: &str) -> IResult<&str, SmtpCommand> {
    use nom::character::complete::digit1;

    let (input, _) = tag_no_case("BDAT").parse(input)?;
    let (input, _) = space1(input)?;
    let (input, size_str) = digit1(input)?;
    let (input, last) = opt(preceded(space1, tag_no_case("LAST"))).parse(input)?;

    // Parse chunk size
    let chunk_size = size_str.parse::<usize>().map_err(|_| {
        nom::Err::Error(nom::error::Error::new(input, nom::error::ErrorKind::Digit))
    })?;

    Ok((
        input,
        SmtpCommand::Bdat {
            chunk_size,
            last: last.is_some(),
        },
    ))
}

/// Parse RSET command
fn rset_command(input: &str) -> IResult<&str, SmtpCommand> {
    map(tag_no_case("RSET"), |_| SmtpCommand::Rset).parse(input)
}

/// Parse NOOP command
fn noop_command(input: &str) -> IResult<&str, SmtpCommand> {
    map(tag_no_case("NOOP"), |_| SmtpCommand::Noop).parse(input)
}

/// Parse QUIT command
fn quit_command(input: &str) -> IResult<&str, SmtpCommand> {
    map(tag_no_case("QUIT"), |_| SmtpCommand::Quit).parse(input)
}

/// Parse VRFY command
fn vrfy_command(input: &str) -> IResult<&str, SmtpCommand> {
    map(
        preceded(tag_no_case("VRFY"), preceded(space1, rest)),
        |s: &str| SmtpCommand::Vrfy(s.to_string()),
    )
    .parse(input)
}

/// Parse EXPN command
fn expn_command(input: &str) -> IResult<&str, SmtpCommand> {
    map(
        preceded(tag_no_case("EXPN"), preceded(space1, rest)),
        |s: &str| SmtpCommand::Expn(s.to_string()),
    )
    .parse(input)
}

/// Parse HELP command
fn help_command(input: &str) -> IResult<&str, SmtpCommand> {
    map(
        preceded(tag_no_case("HELP"), opt(preceded(space1, rest))),
        |s: Option<&str>| SmtpCommand::Help(s.map(|x| x.to_string())),
    )
    .parse(input)
}

/// Parse STARTTLS command
fn starttls_command(input: &str) -> IResult<&str, SmtpCommand> {
    map(tag_no_case("STARTTLS"), |_| SmtpCommand::StartTls).parse(input)
}

/// Parse AUTH command
fn auth_command(input: &str) -> IResult<&str, SmtpCommand> {
    let (input, _) = tag_no_case("AUTH").parse(input)?;
    let (input, _) = space1(input)?;
    let (input, mechanism) =
        take_while1(|c: char| c.is_ascii_alphanumeric() || c == '-').parse(input)?;
    let (input, initial_response) = opt(preceded(space1, rest)).parse(input)?;

    Ok((
        input,
        SmtpCommand::Auth {
            mechanism: mechanism.to_string(),
            initial_response: initial_response.map(|s| s.to_string()),
        },
    ))
}

/// Parse reverse-path (MAIL FROM), with optional SMTPUTF8 support.
fn reverse_path(input: &str, smtputf8: bool) -> IResult<&str, MailAddress> {
    let inner = |i| mailbox(i, smtputf8);
    delimited(char('<'), inner, char('>')).parse(input)
}

/// Parse forward-path (RCPT TO), with optional SMTPUTF8 support.
fn forward_path(input: &str, smtputf8: bool) -> IResult<&str, MailAddress> {
    let inner = |i| mailbox(i, smtputf8);
    delimited(char('<'), inner, char('>')).parse(input)
}

/// Parse a mailbox (email address).
///
/// In ASCII mode (`smtputf8 = false`) only printable ASCII characters that are
/// legal in email addresses are accepted; the address is then validated via
/// [`MailAddress::new`] which enforces the ASCII-only rule.
///
/// In SMTPUTF8 mode (`smtputf8 = true`) the tokenizer additionally allows
/// non-ASCII bytes (UTF-8 multi-byte sequences) in the local-part, and the
/// address is validated via [`MailAddress::from_str_smtputf8`] (RFC 6531).
fn mailbox(input: &str, smtputf8: bool) -> IResult<&str, MailAddress> {
    if smtputf8 {
        // In SMTPUTF8 mode accept everything up to '>' (the closing angle bracket).
        // We take any char that is not '>' so multi-byte UTF-8 passes through.
        let (input, addr_str) = take_while1(|c: char| c != '>').parse(input)?;

        match rusmes_proto::MailAddress::from_str_smtputf8(addr_str) {
            Ok(addr) => Ok((input, addr)),
            Err(_) => Err(nom::Err::Error(nom::error::Error::new(
                input,
                nom::error::ErrorKind::Verify,
            ))),
        }
    } else {
        // ASCII-only mode: accept the characters that are valid in a standard
        // RFC 5321 mailbox (no quoted-string support needed for our use-case).
        let (input, addr_str) = take_while1(|c: char| {
            c.is_ascii_alphanumeric() || c == '@' || c == '.' || c == '-' || c == '_' || c == '+'
        })
        .parse(input)?;

        match addr_str.parse::<MailAddress>() {
            Ok(addr) => Ok((input, addr)),
            Err(_) => Err(nom::Err::Error(nom::error::Error::new(
                input,
                nom::error::ErrorKind::Verify,
            ))),
        }
    }
}

/// Parse domain name
fn domain(input: &str) -> IResult<&str, String> {
    map(
        take_while1(|c: char| c.is_ascii_alphanumeric() || c == '.' || c == '-'),
        |s: &str| s.to_string(),
    )
    .parse(input)
}

/// Parse mail parameters (ESMTP)
fn mail_parameters(input: &str) -> IResult<&str, Vec<MailParam>> {
    let mut params = Vec::new();
    let mut remaining = input;

    while let Ok((rest, param)) = mail_parameter(remaining) {
        params.push(param);
        remaining = rest;

        // Skip any spaces before checking for more parameters
        remaining = remaining.trim_start();

        // If we have more content, continue parsing
        if remaining.is_empty() {
            break;
        }
    }

    Ok((remaining, params))
}

/// Parse a single mail parameter
fn mail_parameter(input: &str) -> IResult<&str, MailParam> {
    let (input, keyword) =
        take_while1(|c: char| c.is_ascii_alphanumeric() || c == '-').parse(input)?;
    let (input, value) = opt(preceded(char('='), parameter_value)).parse(input)?;

    Ok((
        input,
        MailParam::new(keyword.to_string(), value.map(|s| s.to_string())),
    ))
}

/// Parse parameter value
fn parameter_value(input: &str) -> IResult<&str, String> {
    map(
        take_while1(|c: char| c.is_ascii_alphanumeric() || c == '-' || c == '.'),
        |s: &str| s.to_string(),
    )
    .parse(input)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_helo() {
        let cmd = parse_command("HELO example.com").expect("HELO command parse");
        assert!(matches!(cmd, SmtpCommand::Helo(domain) if domain == "example.com"));
    }

    #[test]
    fn test_parse_ehlo() {
        let cmd = parse_command("EHLO mail.example.com").expect("EHLO command parse");
        assert!(matches!(cmd, SmtpCommand::Ehlo(domain) if domain == "mail.example.com"));
    }

    #[test]
    fn test_parse_mail_from() {
        let cmd = parse_command("MAIL FROM:<user@example.com>").expect("MAIL FROM parse");
        match cmd {
            SmtpCommand::Mail { from, .. } => {
                assert_eq!(from.as_string(), "user@example.com");
            }
            _ => panic!("Expected Mail command"),
        }
    }

    #[test]
    fn test_parse_rcpt_to() {
        let cmd = parse_command("RCPT TO:<recipient@example.com>").expect("RCPT TO parse");
        match cmd {
            SmtpCommand::Rcpt { to, .. } => {
                assert_eq!(to.as_string(), "recipient@example.com");
            }
            _ => panic!("Expected Rcpt command"),
        }
    }

    #[test]
    fn test_parse_data() {
        let cmd = parse_command("DATA").expect("DATA command parse");
        assert!(matches!(cmd, SmtpCommand::Data));
    }

    #[test]
    fn test_parse_quit() {
        let cmd = parse_command("QUIT").expect("QUIT command parse");
        assert!(matches!(cmd, SmtpCommand::Quit));
    }

    #[test]
    fn test_parse_rset() {
        let cmd = parse_command("RSET").expect("RSET command parse");
        assert!(matches!(cmd, SmtpCommand::Rset));
    }

    #[test]
    fn test_parse_starttls() {
        let cmd = parse_command("STARTTLS").expect("STARTTLS command parse");
        assert!(matches!(cmd, SmtpCommand::StartTls));
    }

    #[test]
    fn test_parse_auth() {
        let cmd = parse_command("AUTH PLAIN dGVzdA==").expect("AUTH PLAIN command parse");
        match cmd {
            SmtpCommand::Auth {
                mechanism,
                initial_response,
            } => {
                assert_eq!(mechanism, "PLAIN");
                assert_eq!(initial_response, Some("dGVzdA==".to_string()));
            }
            _ => panic!("Expected Auth command"),
        }
    }

    #[test]
    fn test_parse_case_insensitive() {
        let cmd1 = parse_command("quit").expect("lowercase quit parse");
        let cmd2 = parse_command("QUIT").expect("uppercase QUIT parse");
        let cmd3 = parse_command("QuIt").expect("mixed-case QuIt parse");

        assert!(matches!(cmd1, SmtpCommand::Quit));
        assert!(matches!(cmd2, SmtpCommand::Quit));
        assert!(matches!(cmd3, SmtpCommand::Quit));
    }

    #[test]
    fn test_parse_mail_with_size() {
        let cmd = parse_command("MAIL FROM:<user@example.com> SIZE=12345")
            .expect("MAIL FROM with SIZE param parse");
        match cmd {
            SmtpCommand::Mail { from, params } => {
                assert_eq!(from.as_string(), "user@example.com");
                assert_eq!(params.len(), 1);
                assert_eq!(params[0].keyword, "SIZE");
                assert_eq!(params[0].value, Some("12345".to_string()));
            }
            _ => panic!("Expected Mail command"),
        }
    }

    #[test]
    fn test_parse_mail_with_body() {
        let cmd = parse_command("MAIL FROM:<user@example.com> BODY=8BITMIME")
            .expect("MAIL FROM with BODY param parse");
        match cmd {
            SmtpCommand::Mail { from, params } => {
                assert_eq!(from.as_string(), "user@example.com");
                assert_eq!(params.len(), 1);
                assert_eq!(params[0].keyword, "BODY");
                assert_eq!(params[0].value, Some("8BITMIME".to_string()));
            }
            _ => panic!("Expected Mail command"),
        }
    }

    #[test]
    fn test_parse_mail_with_smtputf8() {
        let cmd = parse_command("MAIL FROM:<user@example.com> SMTPUTF8")
            .expect("MAIL FROM with SMTPUTF8 param parse");
        match cmd {
            SmtpCommand::Mail { from, params } => {
                assert_eq!(from.as_string(), "user@example.com");
                assert_eq!(params.len(), 1);
                assert_eq!(params[0].keyword, "SMTPUTF8");
                assert_eq!(params[0].value, None);
            }
            _ => panic!("Expected Mail command"),
        }
    }

    #[test]
    fn test_parse_mail_with_multiple_params() {
        let cmd = parse_command("MAIL FROM:<user@example.com> SIZE=12345 BODY=8BITMIME SMTPUTF8")
            .expect("MAIL FROM with multiple params parse");
        match cmd {
            SmtpCommand::Mail { from, params } => {
                assert_eq!(from.as_string(), "user@example.com");
                assert_eq!(params.len(), 3);
                assert_eq!(params[0].keyword, "SIZE");
                assert_eq!(params[0].value, Some("12345".to_string()));
                assert_eq!(params[1].keyword, "BODY");
                assert_eq!(params[1].value, Some("8BITMIME".to_string()));
                assert_eq!(params[2].keyword, "SMTPUTF8");
                assert_eq!(params[2].value, None);
            }
            _ => panic!("Expected Mail command"),
        }
    }

    #[test]
    fn test_parse_bdat() {
        let cmd = parse_command("BDAT 1024").expect("BDAT without LAST parse");
        match cmd {
            SmtpCommand::Bdat { chunk_size, last } => {
                assert_eq!(chunk_size, 1024);
                assert!(!last);
            }
            _ => panic!("Expected Bdat command"),
        }
    }

    #[test]
    fn test_parse_bdat_last() {
        let cmd = parse_command("BDAT 512 LAST").expect("BDAT with LAST parse");
        match cmd {
            SmtpCommand::Bdat { chunk_size, last } => {
                assert_eq!(chunk_size, 512);
                assert!(last);
            }
            _ => panic!("Expected Bdat command"),
        }
    }

    #[test]
    fn test_parse_bdat_case_insensitive() {
        let cmd1 = parse_command("bdat 100").expect("lowercase bdat parse");
        let cmd2 = parse_command("BDAT 100").expect("uppercase BDAT parse");
        let cmd3 = parse_command("BdAt 100").expect("mixed-case BdAt parse");
        let cmd4 = parse_command("BDAT 256 last").expect("BDAT with lowercase last parse");
        let cmd5 = parse_command("bdat 256 LAST").expect("bdat with uppercase LAST parse");

        match (cmd1, cmd2, cmd3, cmd4, cmd5) {
            (
                SmtpCommand::Bdat {
                    chunk_size: s1,
                    last: l1,
                },
                SmtpCommand::Bdat {
                    chunk_size: s2,
                    last: l2,
                },
                SmtpCommand::Bdat {
                    chunk_size: s3,
                    last: l3,
                },
                SmtpCommand::Bdat {
                    chunk_size: s4,
                    last: l4,
                },
                SmtpCommand::Bdat {
                    chunk_size: s5,
                    last: l5,
                },
            ) => {
                assert_eq!(s1, 100);
                assert_eq!(s2, 100);
                assert_eq!(s3, 100);
                assert_eq!(s4, 256);
                assert_eq!(s5, 256);
                assert!(!l1);
                assert!(!l2);
                assert!(!l3);
                assert!(l4);
                assert!(l5);
            }
            _ => panic!("Expected Bdat commands"),
        }
    }

    // ── SMTPUTF8 / RFC 6531 tests ──────────────────────────────────────────

    /// ASCII-only parse_command must reject a non-ASCII local-part.
    #[test]
    fn test_parse_mail_from_ascii_rejects_unicode() {
        // "münchen" contains non-ASCII bytes; ASCII-mode parser must fail.
        let result = parse_command("MAIL FROM:<münchen@example.com>");
        assert!(
            result.is_err(),
            "ASCII-mode parser must reject non-ASCII local-part"
        );
    }

    /// parse_command_smtputf8 with smtputf8=true must accept a Unicode local-part.
    #[test]
    fn test_parse_mail_from_smtputf8_accepts_unicode() {
        let cmd = parse_command_smtputf8("MAIL FROM:<münchen@example.com>", true)
            .expect("SMTPUTF8-mode parser must accept non-ASCII local-part");
        match cmd {
            SmtpCommand::Mail { from, .. } => {
                assert_eq!(from.local_part(), "münchen");
                assert_eq!(from.as_string(), "münchen@example.com");
            }
            _ => panic!("Expected Mail command"),
        }
    }

    /// parse_command_smtputf8 with smtputf8=false must reject a Unicode local-part
    /// (same behaviour as parse_command).
    #[test]
    fn test_parse_mail_from_smtputf8_false_rejects_unicode() {
        let result = parse_command_smtputf8("MAIL FROM:<münchen@example.com>", false);
        assert!(
            result.is_err(),
            "smtputf8=false must reject non-ASCII local-part"
        );
    }

    /// SMTPUTF8 keyword parameter is captured correctly in SMTPUTF8-mode.
    #[test]
    fn test_parse_mail_from_smtputf8_with_param() {
        let cmd =
            parse_command_smtputf8("MAIL FROM:<münchen@example.com> SMTPUTF8 SIZE=12345", true)
                .expect("SMTPUTF8 with params must parse");
        match cmd {
            SmtpCommand::Mail { from, params } => {
                assert_eq!(from.local_part(), "münchen");
                // Params: SMTPUTF8 (no value) + SIZE=12345
                assert_eq!(params.len(), 2);
                assert_eq!(params[0].keyword, "SMTPUTF8");
                assert_eq!(params[0].value, None);
                assert_eq!(params[1].keyword, "SIZE");
                assert_eq!(params[1].value, Some("12345".to_string()));
            }
            _ => panic!("Expected Mail command"),
        }
    }

    /// RCPT TO with SMTPUTF8-mode must accept a Unicode local-part.
    #[test]
    fn test_parse_rcpt_to_smtputf8_accepts_unicode() {
        let cmd = parse_command_smtputf8("RCPT TO:<用户@example.com>", true)
            .expect("SMTPUTF8-mode RCPT TO must accept non-ASCII local-part");
        match cmd {
            SmtpCommand::Rcpt { to, .. } => {
                assert_eq!(to.local_part(), "用户");
            }
            _ => panic!("Expected Rcpt command"),
        }
    }
}