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
//! Internet Message Format (RFC 5322)
//!
//! TODO: replace this with an IMF library, e.g. rustyknife?

/// 3.2.1.  Quoted characters
pub mod quoted_characters {
    use super::obsolete::obs_qp;
    use abnf_core::streaming::{is_VCHAR, WSP};
    use nom::{
        branch::alt,
        bytes::streaming::{tag, take_while_m_n},
        combinator::recognize,
        sequence::tuple,
        IResult,
    };

    /// quoted-pair = ("\" (VCHAR / WSP)) / obs-qp
    pub fn quoted_pair(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = alt((
            recognize(tuple((
                tag(b"\\"),
                alt((take_while_m_n(1, 1, is_VCHAR), WSP)),
            ))),
            obs_qp,
        ));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }
}

/// 3.2.2.  Folding White Space and Comments
pub mod folding_ws_and_comment {
    use nom::IResult;

    /// Folding white space
    ///
    /// FWS = ([*WSP CRLF] 1*WSP) / obs-FWS
    pub fn FWS(_input: &[u8]) -> IResult<&[u8], &[u8]> {
        unimplemented!()
    }

    // Printable US-ASCII characters not including "(", ")", or "\"
    //
    // ctext = %d33-39 / %d42-91 / %d93-126 / obs-ctext

    // ccontent = ctext / quoted-pair / comment

    // comment = "(" *([FWS] ccontent) [FWS] ")"

    /// CFWS = (1*([FWS] comment) [FWS]) / FWS
    pub fn CFWS(_input: &[u8]) -> IResult<&[u8], &[u8]> {
        unimplemented!()
    }
}

/// 3.2.3.  Atom
pub mod atom {
    use super::folding_ws_and_comment::CFWS;
    use abnf_core::streaming::{is_ALPHA, is_DIGIT};
    use nom::{
        bytes::streaming::{tag, take_while1},
        combinator::{opt, recognize},
        multi::many0,
        sequence::tuple,
        IResult,
    };

    /// Printable US-ASCII characters not including specials.
    /// Used for atoms.
    ///
    /// atext = ALPHA / DIGIT /
    ///          "!" / "#" /
    ///          "$" / "%" /
    ///          "&" / "'" /
    ///          "*" / "+" /
    ///          "-" / "/" /
    ///          "=" / "?" /
    ///          "^" / "_" /
    ///          "`" / "{" /
    ///          "|" / "}" /
    ///          "~"
    pub fn is_atext(byte: u8) -> bool {
        let allowed = b"!#$%&'*+-/=?^_`{|}~";

        is_ALPHA(byte) || is_DIGIT(byte) || allowed.contains(&byte)
    }

    /// atom = [CFWS] 1*atext [CFWS]
    pub fn atom(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = tuple((opt(CFWS), take_while1(is_atext), opt(CFWS)));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    /// dot-atom-text = 1*atext *("." 1*atext)
    pub fn dot_atom_text(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = tuple((
            take_while1(is_atext),
            many0(tuple((tag(b"."), take_while1(is_atext)))),
        ));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    // dot-atom = [CFWS] dot-atom-text [CFWS]
    pub fn dot_atom(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = tuple((opt(CFWS), dot_atom_text, opt(CFWS)));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    // Special characters that do not appear in atext.
    //
    // specials = "(" / ")" /
    //            "<" / ">" /
    //            "[" / "]" /
    //            ":" / ";" /
    //            "@" / "\" /
    //            "," / "." /
    //            DQUOTE
    // ...
}

/// 3.2.4.  Quoted Strings
pub mod quoted_strings {
    use super::{
        folding_ws_and_comment::{CFWS, FWS},
        obsolete::is_obs_qtext,
        quoted_characters::quoted_pair,
    };
    use abnf_core::streaming::DQUOTE;
    use nom::{
        branch::alt,
        bytes::streaming::take_while_m_n,
        combinator::{opt, recognize},
        multi::many0,
        sequence::tuple,
        IResult,
    };

    /// Printable US-ASCII characters not including "\" or the quote character.
    ///
    /// qtext = %d33 / %d35-91 / %d93-126 / obs-qtext
    pub fn is_qtext(byte: u8) -> bool {
        match byte {
            33 | 35..=91 | 93..=126 => true,
            _ if is_obs_qtext(byte) => true,
            _ => false,
        }
    }

    /// qcontent = qtext / quoted-pair
    pub fn qcontent(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = alt((take_while_m_n(1, 1, is_qtext), quoted_pair));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    /// quoted-string = [CFWS] DQUOTE *([FWS] qcontent) [FWS] DQUOTE [CFWS]
    pub fn quoted_string(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = tuple((
            opt(CFWS),
            DQUOTE,
            many0(tuple((opt(FWS), qcontent))),
            opt(FWS),
            DQUOTE,
            opt(CFWS),
        ));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }
}

/// 3.2.5.  Miscellaneous Tokens
pub mod miscellaneous {
    use super::{atom::atom, quoted_strings::quoted_string};
    use nom::{branch::alt, IResult};

    /// word = atom / quoted-string
    pub fn word(input: &[u8]) -> IResult<&[u8], &[u8]> {
        alt((atom, quoted_string))(input)
    }

    // phrase = 1*word / obs-phrase
    // ...

    // unstructured = (*([FWS] VCHAR) *WSP) / obs-unstruct
    // ...
}

/// 3.3.  Date and Time Specification
pub mod datetime {
    use super::folding_ws_and_comment::{CFWS, FWS};
    use abnf_core::streaming::is_DIGIT;
    use nom::{
        branch::alt,
        bytes::streaming::{tag, tag_no_case, take_while_m_n},
        combinator::{opt, recognize},
        sequence::tuple,
        IResult,
    };

    // date-time = [ day-of-week "," ] date time [CFWS]
    pub fn date_time(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = tuple((opt(tuple((day_of_week, tag(b",")))), date, time, opt(CFWS)));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    // day-of-week = ([FWS] day-name) / obs-day-of-week
    pub fn day_of_week(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = tuple((opt(FWS), day_name));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    // day-name = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
    pub fn day_name(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = alt((
            tag_no_case(b"Mon"),
            tag_no_case(b"Tue"),
            tag_no_case(b"Wed"),
            tag_no_case(b"Thu"),
            tag_no_case(b"Fri"),
            tag_no_case(b"Sat"),
            tag_no_case(b"Sun"),
        ));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    // date = day month year
    pub fn date(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = tuple((day, month, year));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    // day = ([FWS] 1*2DIGIT FWS) / obs-day
    pub fn day(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = tuple((opt(FWS), take_while_m_n(1, 2, is_DIGIT), FWS));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    // month = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
    pub fn month(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = alt((
            tag_no_case(b"Jan"),
            tag_no_case(b"Feb"),
            tag_no_case(b"Mar"),
            tag_no_case(b"Apr"),
            tag_no_case(b"May"),
            tag_no_case(b"Jun"),
            tag_no_case(b"Jul"),
            tag_no_case(b"Aug"),
            tag_no_case(b"Sep"),
            tag_no_case(b"Oct"),
            tag_no_case(b"Nov"),
            tag_no_case(b"Dec"),
        ));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    // year = (FWS 4*DIGIT FWS) / obs-year
    pub fn year(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = tuple((FWS, take_while_m_n(4, 8, is_DIGIT), FWS)); // FIXME: 4*?!

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    // time = time-of-day zone
    pub fn time(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = tuple((time_of_day, zone));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    // time-of-day = hour ":" minute [ ":" second ]
    pub fn time_of_day(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = tuple((hour, tag(b":"), minute, opt(tuple((tag(b":"), second)))));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    // hour = 2DIGIT / obs-hour
    pub fn hour(input: &[u8]) -> IResult<&[u8], &[u8]> {
        // FIXME: obs- forms must not be used in SMTP. Never?

        let parser = take_while_m_n(2, 2, is_DIGIT);

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    // minute = 2DIGIT / obs-minute
    pub fn minute(input: &[u8]) -> IResult<&[u8], &[u8]> {
        // FIXME: obs- forms must not be used in SMTP. Never?

        let parser = take_while_m_n(2, 2, is_DIGIT);

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    // second = 2DIGIT / obs-second
    pub fn second(input: &[u8]) -> IResult<&[u8], &[u8]> {
        // FIXME: obs- forms must not be used in SMTP. Never?

        let parser = take_while_m_n(2, 2, is_DIGIT);

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    // zone = (FWS ( "+" / "-" ) 4DIGIT) / obs-zone
    pub fn zone(input: &[u8]) -> IResult<&[u8], &[u8]> {
        // FIXME: obs- forms must not be used in SMTP. Never?

        let parser = tuple((
            FWS,
            alt((tag(b"+"), tag(b"-"))),
            take_while_m_n(4, 4, is_DIGIT),
        ));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }
}

/// 3.4.1.  Addr-Spec Specification
pub mod addr_spec {
    use super::{
        atom::dot_atom,
        folding_ws_and_comment::{CFWS, FWS},
        obsolete::{obs_domain, obs_dtext, obs_local_part},
        quoted_strings::quoted_string,
    };
    use nom::{
        branch::alt,
        bytes::streaming::{tag, take_while_m_n},
        combinator::{opt, recognize},
        multi::many0,
        sequence::tuple,
        IResult,
    };

    // addr-spec = local-part "@" domain

    /// local-part = dot-atom / quoted-string / obs-local-part
    pub fn local_part(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = alt((dot_atom, quoted_string, obs_local_part));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    /// domain = dot-atom / domain-literal / obs-domain
    pub fn domain(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = alt((dot_atom, domain_literal, obs_domain));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    /// domain-literal = [CFWS] "[" *([FWS] dtext) [FWS] "]" [CFWS]
    pub fn domain_literal(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = tuple((
            opt(CFWS),
            tag(b"["),
            many0(tuple((opt(FWS), dtext))),
            opt(FWS),
            tag(b"]"),
            opt(CFWS),
        ));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    /// Printable US-ASCII characters not including "[", "]", or "\".
    ///
    /// dtext = %d33-90 / %d94-126 / obs-dtext
    pub fn dtext(input: &[u8]) -> IResult<&[u8], &[u8]> {
        fn is_a(byte: u8) -> bool {
            matches!(byte, 33..=90)
        }

        fn is_b(byte: u8) -> bool {
            matches!(byte, 94..=126)
        }

        let parser = alt((
            take_while_m_n(1, 1, is_a),
            take_while_m_n(1, 1, is_b),
            obs_dtext,
        ));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }
}

/// 3.6.4.  Identification Fields
pub mod identification {
    use super::{
        addr_spec::dtext,
        atom::dot_atom_text,
        folding_ws_and_comment::CFWS,
        obsolete::{obs_id_left, obs_id_right},
    };
    use nom::{
        branch::alt,
        bytes::streaming::tag,
        combinator::{opt, recognize},
        multi::many0,
        sequence::{delimited, tuple},
        IResult,
    };

    // message-id = "Message-ID:" msg-id CRLF
    // ...

    // in-reply-to = "In-Reply-To:" 1*msg-id CRLF
    // ...

    // references = "References:" 1*msg-id CRLF
    // ...

    /// msg-id = [CFWS] "<" id-left "@" id-right ">" [CFWS]
    pub fn msg_id(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = tuple((
            opt(CFWS),
            tag(b"<"),
            id_left,
            tag(b"@"),
            id_right,
            tag(b">"),
            opt(CFWS),
        ));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    /// id-left = dot-atom-text / obs-id-left
    pub fn id_left(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = alt((dot_atom_text, obs_id_left));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    /// id-right = dot-atom-text / no-fold-literal / obs-id-right
    pub fn id_right(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = alt((dot_atom_text, no_fold_literal, obs_id_right));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    // no-fold-literal = "[" *dtext "]"
    pub fn no_fold_literal(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = delimited(tag(b"["), many0(dtext), tag(b"]"));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }
}

/// 4.1.  Miscellaneous Obsolete Tokens
pub mod obsolete {
    use super::{
        addr_spec::{domain, local_part},
        atom::atom,
        miscellaneous::word,
        quoted_characters::quoted_pair,
    };
    use abnf_core::streaming::{CR, LF};
    use nom::{
        branch::alt,
        bytes::streaming::{tag, take_while_m_n},
        combinator::recognize,
        multi::many0,
        sequence::tuple,
        IResult,
    };

    /// US-ASCII control characters that do not include the carriage
    /// return, line feed, and white space characters
    ///
    /// obs-NO-WS-CTL = %d1-8 / %d11 / %d12 / %d14-31 / %d127
    pub fn is_obs_NO_WS_CTL(byte: u8) -> bool {
        matches!(byte, 1..=8 | 11 | 12 | 14..=31 | 127)
    }

    /// obs-qtext = obs-NO-WS-CTL
    pub fn is_obs_qtext(byte: u8) -> bool {
        is_obs_NO_WS_CTL(byte)
    }

    /// obs-qp = "\" (%d0 / obs-NO-WS-CTL / LF / CR)
    pub fn obs_qp(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = tuple((
            tag(b"\\"),
            alt((
                take_while_m_n(1, 1, |x| x == 0x00),
                take_while_m_n(1, 1, is_obs_NO_WS_CTL),
                LF,
                CR,
            )),
        ));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    // 4.4.  Obsolete Addressing (RFC 5322)

    /// obs-local-part = word *("." word)
    pub fn obs_local_part(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = tuple((word, many0(tuple((tag(b"."), word)))));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    /// obs-domain = atom *("." atom)
    pub fn obs_domain(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = tuple((atom, many0(tuple((tag(b"."), atom)))));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    /// obs-dtext = obs-NO-WS-CTL / quoted-pair
    pub fn obs_dtext(input: &[u8]) -> IResult<&[u8], &[u8]> {
        let parser = alt((take_while_m_n(1, 1, is_obs_NO_WS_CTL), quoted_pair));

        let (remaining, parsed) = recognize(parser)(input)?;

        Ok((remaining, parsed))
    }

    // 4.5.4.  Obsolete Identification Fields (RFC 5322)

    /// obs-id-left = local-part
    pub fn obs_id_left(input: &[u8]) -> IResult<&[u8], &[u8]> {
        local_part(input)
    }

    /// obs-id-right = domain
    pub fn obs_id_right(input: &[u8]) -> IResult<&[u8], &[u8]> {
        domain(input)
    }
}