rfc1939 0.1.0

Parsing RFC 1939 (i.e. POP3) data streams
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
use crate::common::{
    one_line_response_two_parts_parser, parse_u8_slice_to_usize_or_0, retr_message_parser,
    take_until_crlf, StatusIndicator,
};
use crate::types::response::{Dele, List, Noop, OneLineTwoParts, Retr, Rset, Stat, Top, Uidl};
use nom::{
    branch::alt,
    bytes::complete::{tag, tag_no_case},
    character::complete::digit1,
    combinator::map,
    combinator::opt,
    multi::many1,
    sequence::tuple,
    sequence::{preceded, separated_pair, terminated},
    IResult,
};

// ################################################################################
/// STAT
/// Restrictions:
///     may only be given in the TRANSACTION state
/// Discussion:
///     The positive response consists of "+OK" followed by a single
///     space, the number of messages in the maildrop, a single
///     space, and the size of the maildrop in octets.
// ################################################################################
pub fn stat(s: &[u8]) -> Option<Stat> {
    match stat_parser(s) {
        Ok((_, x)) => Some(x),
        Err(_) => None,
    }
}

pub(crate) fn stat_parser(s: &[u8]) -> IResult<&[u8], Stat> {
    alt((
        map(
            tuple((
                map(tag_no_case(b"+OK"), |_| StatusIndicator::OK),
                tag(b" "),
                digit1,
                tag(b" "),
                digit1,
                tag(b"\r\n"),
            )),
            |(si, _, num, _, size, _): (StatusIndicator, _, &[u8], _, &[u8], _)| Stat {
                status_indicator: si,
                number_of_messages: parse_u8_slice_to_usize_or_0(num),
                size_in_octets: parse_u8_slice_to_usize_or_0(size),
                information: &[],
            },
        ),
        map(one_line_response_two_parts_parser::<OneLineTwoParts>, |x| {
            Stat {
                status_indicator: x.left,
                number_of_messages: 0,
                size_in_octets: 0,
                information: x.right,
            }
        }),
    ))(s)
}

// ################################################################################
/// LIST [msg]
/// Restrictions:
///     may only be given in the TRANSACTION state
/// Discussion:
///     If an argument was given and the POP3 server issues a
///     positive response with a line containing information for
///     that message.  This line is called a "scan listing" for
///     that message.

///     If no argument was given and the POP3 server issues a
///     positive response, then the response given is multi-line.
///     After the initial +OK, for each message in the maildrop,
///     the POP3 server responds with a line containing
///     information for that message.  This line is also called a
///     "scan listing" for that message.  If there are no
///     messages in the maildrop, then the POP3 server responds
///     with no scan listings--it issues a positive response
///     followed by a line containing a termination octet and a
///     CRLF pair.

///     In order to simplify parsing, all POP3 servers are
///     required to use a certain format for scan listings.  A
///     scan listing consists of the message-number of the
///     message, followed by a single space and the exact size of
///     the message in octets.  Methods for calculating the exact
///     size of the message are described in the "Message Format"
///     section below.  This memo makes no requirement on what
///     follows the message size in the scan listing.  Minimal
///     implementations should just end that line of the response
///     with a CRLF pair.  More advanced implementations may
///     include other information, as parsed from the message.
// ################################################################################
pub fn list(s: &[u8]) -> Option<List> {
    match list_parser(s) {
        Ok((_, x)) => Some(x),
        Err(_) => None,
    }
}

pub(crate) fn list_parser(s: &[u8]) -> IResult<&[u8], List> {
    alt((list_multi_line_parser, list_one_line_parser))(s)
}

fn list_multi_line_parser(s: &[u8]) -> IResult<&[u8], List> {
    map(
        terminated(
            tuple((
                map(tag_no_case(b"+OK"), |_| StatusIndicator::OK),
                map(opt(preceded(tag(b" "), take_until_crlf)), |x| {
                    if let Some(msg) = x {
                        msg
                    } else {
                        &[]
                    }
                }),
                many1(preceded(
                    tag(b"\r\n"),
                    separated_pair(
                        map(digit1, parse_u8_slice_to_usize_or_0),
                        tag(b" "),
                        map(digit1, parse_u8_slice_to_usize_or_0),
                    ),
                )),
            )),
            tag(b"\r\n.\r\n"),
        ),
        |(si, msg, infos)| List {
            status_indicator: si,
            informations: infos,
            information: msg,
        },
    )(s)
}

fn list_one_line_parser(s: &[u8]) -> IResult<&[u8], List> {
    alt((
        map(
            terminated(
                tuple((
                    map(tag_no_case(b"+OK"), |_| StatusIndicator::OK),
                    tag(b" "),
                    map(digit1, parse_u8_slice_to_usize_or_0),
                    tag(b" "),
                    map(digit1, parse_u8_slice_to_usize_or_0),
                )),
                tag(b"\r\n"),
            ),
            |(si, _, num, _, size)| List {
                status_indicator: si,
                informations: vec![(num, size)],
                information: &[],
            },
        ),
        map(one_line_response_two_parts_parser::<OneLineTwoParts>, |x| {
            List {
                status_indicator: x.left,
                informations: vec![],
                information: x.right,
            }
        }),
    ))(s)
}

// ################################################################################
/// RETR msg
/// Restrictions:
///     may only be given in the TRANSACTION state
/// Discussion:
///     If the POP3 server issues a positive response, then the
///     response given is multi-line.  After the initial +OK, the
///     POP3 server sends the message corresponding to the given
///     message-number, being careful to byte-stuff the termination
///     character (as with all multi-line responses).
/// Possible Responses:
///     +OK message follows
///     -ERR no such message
// ################################################################################
pub fn retr(s: &[u8]) -> Option<Retr> {
    match retr_parser(s) {
        Ok((_, x)) => Some(x),
        Err(_) => None,
    }
}

pub(crate) fn retr_parser(s: &[u8]) -> IResult<&[u8], Retr> {
    retr_message_parser::<Retr>(s)
}

// ################################################################################
/// TOP msg n
/// Restrictions:
///     may only be given in the TRANSACTION state
/// Discussion:
///     If the POP3 server issues a positive response, then the
///     response given is multi-line.  After the initial +OK, the
///     POP3 server sends the headers of the message, the blank
///     line separating the headers from the body, and then the
///     number of lines of the indicated message's body, being
///     careful to byte-stuff the termination character (as with
///     all multi-line responses).
///     Note that if the number of lines requested by the POP3
///     client is greater than than the number of lines in the
///     body, then the POP3 server sends the entire message.
/// Possible Responses:
///     +OK top of message follows
///     -ERR no such message
// ################################################################################
pub fn top(s: &[u8]) -> Option<Top> {
    match top_parser(s) {
        Ok((_, x)) => Some(x),
        Err(_) => None,
    }
}

pub(crate) fn top_parser(s: &[u8]) -> IResult<&[u8], Top> {
    retr_message_parser::<Top>(s)
}

// ################################################################################
/// DELE msg
/// Restrictions:
///     may only be given in the TRANSACTION state
/// Discussion:
///     The POP3 server marks the message as deleted.  Any future
///     reference to the message-number associated with the message
///     in a POP3 command generates an error.  The POP3 server does
///     not actually delete the message until the POP3 session
///     enters the UPDATE state.
/// Possible Responses:
///     +OK message deleted
///     -ERR no such message
/// Examples:
///     S: +OK message 1 deleted
///     S: -ERR message 2 already deleted
// ################################################################################
pub fn dele(s: &[u8]) -> Option<Dele> {
    match dele_parser(s) {
        Ok((_, x)) => Some(x),
        Err(_) => None,
    }
}

pub(crate) fn dele_parser(s: &[u8]) -> IResult<&[u8], Dele> {
    one_line_response_two_parts_parser::<Dele>(s)
}

// ################################################################################
/// NOOP
/// Restrictions:
///     may only be given in the TRANSACTION state
/// Discussion:
///     The POP3 server does nothing, it merely replies with a
///     positive response.
/// Possible Responses:
///     +OK
/// Examples:
///     S: +OK
// ################################################################################
pub fn noop(s: &[u8]) -> Option<Noop> {
    match noop_parser(s) {
        Ok((_, x)) => Some(x),
        Err(_) => None,
    }
}

pub(crate) fn noop_parser(s: &[u8]) -> IResult<&[u8], Noop> {
    one_line_response_two_parts_parser::<Noop>(s)
}

// ################################################################################
/// RSET
/// Restrictions:
///     may only be given in the TRANSACTION state
/// Discussion:
///     If any messages have been marked as deleted by the POP3
///     server, they are unmarked.  The POP3 server then replies
///     with a positive response.
/// Possible Responses:
///     +OK
/// Examples:
///     S: +OK maildrop has 2 messages (320 octets)
// ################################################################################
pub fn rset(s: &[u8]) -> Option<Rset> {
    match rset_parser(s) {
        Ok((_, x)) => Some(x),
        Err(_) => None,
    }
}

pub(crate) fn rset_parser(s: &[u8]) -> IResult<&[u8], Rset> {
    one_line_response_two_parts_parser::<Rset>(s)
}

// ################################################################################
/// UIDL [msg]
/// Restrictions:
///     may only be given in the TRANSACTION state
/// Discussion:
///     If an argument was given and the POP3 server issues a positive
///     response with a line containing information for that message.
///     This line is called a "unique-id listing" for that message.

///     If no argument was given and the POP3 server issues a positive
///     response, then the response given is multi-line.  After the
///     initial +OK, for each message in the maildrop, the POP3 server
///     responds with a line containing information for that message.
///     This line is called a "unique-id listing" for that message.

///     In order to simplify parsing, all POP3 servers are required to
///     use a certain format for unique-id listings.  A unique-id
///     listing consists of the message-number of the message,
///     followed by a single space and the unique-id of the message.
///     No information follows the unique-id in the unique-id listing.

///     The unique-id of a message is an arbitrary server-determined
///     string, consisting of one to 70 characters in the range 0x21
///     to 0x7E, which uniquely identifies a message within a
///     maildrop and which persists across sessions.  This
///     persistence is required even if a session ends without
///     entering the UPDATE state.  The server should never reuse an
///     unique-id in a given maildrop, for as long as the entity
///     using the unique-id exists.

///     Note that messages marked as deleted are not listed.

///     While it is generally preferable for server implementations
///     to store arbitrarily assigned unique-ids in the maildrop,
///     this specification is intended to permit unique-ids to be
///     calculated as a hash of the message.  Clients should be able
///     to handle a situation where two identical copies of a
///     message in a maildrop have the same unique-id.
// ################################################################################
pub fn uidl(s: &[u8]) -> Option<Uidl> {
    match uidl_parser(s) {
        Ok((_, x)) => Some(x),
        Err(_) => None,
    }
}

pub(crate) fn uidl_parser(s: &[u8]) -> IResult<&[u8], Uidl> {
    alt((uidl_multi_line_parser, uidl_one_line_parser))(s)
}

fn uidl_multi_line_parser(s: &[u8]) -> IResult<&[u8], Uidl> {
    map(
        terminated(
            tuple((
                map(tag_no_case(b"+OK"), |_| StatusIndicator::OK),
                map(opt(preceded(tag(b" "), take_until_crlf)), |x| {
                    if let Some(msg) = x {
                        msg
                    } else {
                        &[]
                    }
                }),
                many1(preceded(
                    tag(b"\r\n"),
                    separated_pair(
                        map(digit1, parse_u8_slice_to_usize_or_0),
                        tag(b" "),
                        take_until_crlf,
                    ),
                )),
            )),
            tag(b"\r\n.\r\n"),
        ),
        |(si, information, infos)| Uidl {
            status_indicator: si,
            informations: infos,
            information: information,
        },
    )(s)
}

fn uidl_one_line_parser(s: &[u8]) -> IResult<&[u8], Uidl> {
    alt((
        map(
            terminated(
                tuple((
                    map(tag_no_case(b"+OK"), |_| StatusIndicator::OK),
                    tag(b" "),
                    map(digit1, parse_u8_slice_to_usize_or_0),
                    tag(b" "),
                    take_until_crlf,
                )),
                tag(b"\r\n"),
            ),
            |(si, _, num, _, size)| Uidl {
                status_indicator: si,
                informations: vec![(num, size)],
                information: &[],
            },
        ),
        map(one_line_response_two_parts_parser::<OneLineTwoParts>, |x| {
            Uidl {
                status_indicator: x.left,
                informations: vec![],
                information: x.right,
            }
        }),
    ))(s)
}

#[test]
fn test_stat_parser() {
    assert_eq!(
        stat_parser(b"+OK 2 320\r\n").unwrap().1,
        Stat {
            status_indicator: StatusIndicator::OK,
            number_of_messages: 2,
            size_in_octets: 320,
            information: &[]
        }
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_stat() {
        assert_eq!(
            stat(b"+OK 2 320\r\n").unwrap(),
            Stat {
                status_indicator: StatusIndicator::OK,
                number_of_messages: 2,
                size_in_octets: 320,
                information: &[]
            }
        );
        assert_eq!(
            stat(b"-ERR failed\r\n").unwrap(),
            Stat {
                status_indicator: StatusIndicator::ERR,
                number_of_messages: 0,
                size_in_octets: 0,
                information: b"failed"
            }
        )
    }

    #[test]
    fn test_list_parser() {
        assert_eq!(
            list_multi_line_parser(b"+OK 2 messages (320 octets)\r\n1 120\r\n2 200\r\n.\r\n")
                .unwrap()
                .1,
            List {
                status_indicator: StatusIndicator::OK,
                informations: vec![(1, 120), (2, 200)],
                information: b"2 messages (320 octets)"
            }
        );
        assert_eq!(
            list_multi_line_parser(b"+OK\r\n1 120\r\n2 200\r\n.\r\n")
                .unwrap()
                .1,
            List {
                status_indicator: StatusIndicator::OK,
                informations: vec![(1, 120), (2, 200)],
                information: b""
            }
        );
        assert_eq!(
            list_one_line_parser(b"+OK 1 60178\r\n").unwrap().1,
            List {
                status_indicator: StatusIndicator::OK,
                informations: vec![(1, 60178)],
                information: &[]
            }
        );
        assert_eq!(
            list_one_line_parser(b"-ERR Syntax error\r\n").unwrap().1,
            List {
                status_indicator: StatusIndicator::ERR,
                informations: vec![],
                information: b"Syntax error"
            }
        );
    }

    #[test]
    fn test_list() {
        assert_eq!(
            list(b"+OK 2 messages (320 octets)\r\n1 120\r\n2 200\r\n.\r\n").unwrap(),
            List {
                status_indicator: StatusIndicator::OK,
                informations: vec![(1, 120), (2, 200)],
                information: b"2 messages (320 octets)"
            }
        );
        assert_eq!(
            list(b"-ERR Syntax error\r\n").unwrap(),
            List {
                status_indicator: StatusIndicator::ERR,
                informations: vec![],
                information: b"Syntax error"
            }
        );
    }

    #[test]
    fn test_retr() {
        assert_eq!(
            retr(b"+OK 120 octets\r\n<the POP3 server sends the entire message here>\r\n.\r\n")
                .unwrap(),
            Retr {
                status_indicator: StatusIndicator::OK,
                message: Some(b"<the POP3 server sends the entire message here>"),
                information: b"120 octets"
            }
        );
    }

    #[test]
    fn test_dele() {
        assert_eq!(
            dele(b"+OK message 1 deleted\r\n").unwrap(),
            Dele {
                status_indicator: StatusIndicator::OK,
                information: b"message 1 deleted"
            }
        );
    }

    #[test]
    fn test_noop() {
        assert_eq!(
            noop(b"+OK\r\n").unwrap(),
            Noop {
                status_indicator: StatusIndicator::OK,
                information: b""
            }
        );
    }

    #[test]
    fn test_rset() {
        assert_eq!(
            rset(b"+OK core mail\r\n").unwrap(),
            Rset {
                status_indicator: StatusIndicator::OK,
                information: b"core mail"
            }
        );
    }
    #[test]
    fn test_top() {
        assert_eq!(
            top(b"+OK 1364\r\n58.87.109.78\r\n.\r\n").unwrap(),
            Top {
                status_indicator: StatusIndicator::OK,
                message: Some(b"58.87.109.78"),
                information: b"1364"
            }
        );
    }

    #[test]
    fn test_uidl() {
        assert_eq!(
            uidl(b"+OK\r\n1 whqtswO00WBw418f9t5JxYwZ\r\n2 QhdPYR:00WBw1Ph7x7\r\n.\r\n").unwrap(),
            Uidl {
                status_indicator: StatusIndicator::OK,
                informations: vec![(1, b"whqtswO00WBw418f9t5JxYwZ"), (2, b"QhdPYR:00WBw1Ph7x7")],
                information: &vec![]
            }
        );
        assert_eq!(
            uidl(b"+OK 1 whqtswO00WBw418f9t5JxYwZ%\r\n").unwrap(),
            Uidl {
                status_indicator: StatusIndicator::OK,
                informations: vec![(1, b"whqtswO00WBw418f9t5JxYwZ%")],
                information: &vec![]
            }
        );
        assert_eq!(
            uidl(b"-ERR Syntax error\r\n").unwrap(),
            Uidl {
                status_indicator: StatusIndicator::ERR,
                informations: vec![],
                information: b"Syntax error"
            }
        );
    }
}