imap 3.0.0-alpha.13

IMAP client 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
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
use imap_proto::{MailboxDatum, Response, ResponseCode, StatusAttribute};
use lazy_static::lazy_static;
use regex::Regex;
use std::collections::HashSet;
use std::sync::mpsc;

use super::error::{Error, ParseError, Result};
use super::types::*;

lazy_static! {
    static ref AUTH_RESP_REGEX: Regex = Regex::new("^\\+ (.*)\r\n").unwrap();
}

pub fn parse_authenticate_response(line: &str) -> Result<&str> {
    if let Some(cap) = AUTH_RESP_REGEX.captures_iter(line).next() {
        let data = cap.get(1).map(|x| x.as_str()).unwrap_or("");
        return Ok(data);
    }
    Err(Error::Parse(ParseError::Authentication(
        line.to_string(),
        None,
    )))
}

pub(crate) enum MapOrNot<'a, T> {
    Map(T),
    MapVec(Vec<T>),
    Not(Response<'a>),
    #[allow(dead_code)]
    Ignore,
}

/// Parse many `T` Responses with `F` and extend `into` with them.
/// Responses other than `T` go into the `unsolicited` channel.
pub(crate) fn parse_many_into<'input, T, F>(
    input: &'input [u8],
    into: &mut impl Extend<T>,
    unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
    mut map: F,
) -> Result<()>
where
    F: FnMut(Response<'input>) -> Result<MapOrNot<'input, T>>,
{
    let mut other = Vec::new();

    parse_many_into2::<_, (), _, _, _>(
        input,
        into,
        &mut other,
        unsolicited,
        |response| match map(response)? {
            MapOrNot::Map(t) => Ok(MapOrNot2::Map1(t)),
            MapOrNot::MapVec(t) => Ok(MapOrNot2::MapVec1(t)),
            MapOrNot::Not(t) => Ok(MapOrNot2::Not(t)),
            MapOrNot::Ignore => Ok(MapOrNot2::Ignore),
        },
    )?;

    assert_eq!(other.len(), 0);

    Ok(())
}

pub(crate) enum MapOrNot2<'a, T, U> {
    Map1(T),
    Map2(U),
    MapVec1(Vec<T>),
    #[allow(dead_code)]
    MapVec2(Vec<U>),
    Not(Response<'a>),
    Ignore,
}

/// Parse many `T` or `U` Responses with `F` and extend `into1` or `into2` with them.
/// Responses other than `T` or `U` go into the `unsolicited` channel.
pub(crate) fn parse_many_into2<'input, T, U, F, IU, IT>(
    input: &'input [u8],
    into1: &mut IT,
    into2: &mut IU,
    unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
    mut map: F,
) -> Result<()>
where
    IT: Extend<T>,
    IU: Extend<U>,
    F: FnMut(Response<'input>) -> Result<MapOrNot2<'input, T, U>>,
{
    let mut lines = input;
    loop {
        if lines.is_empty() {
            break Ok(());
        }

        match imap_proto::parser::parse_response(lines) {
            Ok((rest, resp)) => {
                lines = rest;

                match map(resp)? {
                    MapOrNot2::Map1(t) => into1.extend(std::iter::once(t)),
                    MapOrNot2::Map2(t) => into2.extend(std::iter::once(t)),
                    MapOrNot2::MapVec1(t) => into1.extend(t),
                    MapOrNot2::MapVec2(t) => into2.extend(t),
                    MapOrNot2::Not(resp) => match try_handle_unilateral(resp, unsolicited) {
                        Some(Response::Fetch(..)) => continue,
                        Some(resp) => break Err(resp.into()),
                        None => {}
                    },
                    MapOrNot2::Ignore => continue,
                }
            }
            _ => {
                break Err(Error::Parse(ParseError::Invalid(lines.to_vec())));
            }
        }
    }
}

fn parse_until_done_internal<'input, T, F>(
    input: &'input [u8],
    optional: bool,
    unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
    map: F,
) -> Result<Option<T>>
where
    F: FnMut(Response<'input>) -> Result<MapOrNot<'input, T>>,
{
    let mut temp_output = Vec::<T>::new();

    parse_many_into(input, &mut temp_output, unsolicited, map)?;

    match temp_output.len() {
        1 => Ok(Some(temp_output.remove(0))),
        0 if optional => Ok(None),
        _ => Err(Error::Parse(ParseError::Invalid(input.to_vec()))),
    }
}

/// Parse and return an optional single `T` Response with `F`.
/// Responses other than `T` go into the `unsolicited` channel.
///
/// If more than one `T` are found then [`Error::Parse`] is returned
pub(crate) fn parse_until_done_optional<'input, T, F>(
    input: &'input [u8],
    unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
    map: F,
) -> Result<Option<T>>
where
    F: FnMut(Response<'input>) -> Result<MapOrNot<'input, T>>,
{
    parse_until_done_internal(input, true, unsolicited, map)
}

/// Parse and return an expected single `T` Response with `F`.
/// Responses other than `T` go into the `unsolicited` channel.
///
/// If zero or more than one `T` are found then [`Error::Parse`] is returned.
pub(crate) fn parse_until_done<'input, T, F>(
    input: &'input [u8],
    unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
    map: F,
) -> Result<T>
where
    F: FnMut(Response<'input>) -> Result<MapOrNot<'input, T>>,
{
    parse_until_done_internal(input, false, unsolicited, map).map(|e| {
        e.expect("optional = false, so Err(Invalid) would be returned instead of Ok(None)")
    })
}

pub fn parse_expunge(
    lines: Vec<u8>,
    unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
) -> Result<Deleted> {
    let mut lines: &[u8] = &lines;
    let mut expunged = Vec::new();
    let mut vanished = Vec::new();
    let mut mod_seq: Option<u64> = None;

    loop {
        if lines.is_empty() {
            break;
        }

        match imap_proto::parser::parse_response(lines) {
            Ok((rest, Response::Done { status, code, .. })) => {
                assert_eq!(status, imap_proto::Status::Ok);
                lines = rest;
                if let Some(ResponseCode::HighestModSeq(ms)) = code {
                    mod_seq = Some(ms);
                };
            }
            Ok((rest, Response::Expunge(seq))) => {
                lines = rest;
                expunged.push(seq);
            }
            Ok((rest, Response::Vanished { earlier: _, uids })) => {
                lines = rest;
                vanished.extend(uids);
            }
            Ok((rest, data)) => {
                lines = rest;
                if let Some(resp) = try_handle_unilateral(data, unsolicited) {
                    return Err(resp.into());
                }
            }
            _ => {
                return Err(Error::Parse(ParseError::Invalid(lines.to_vec())));
            }
        }
    }

    // If the server sends a VANISHED response then they must only send VANISHED
    // in lieu of EXPUNGE responses for the rest of this connection, so it is
    // always one or the other.
    // https://tools.ietf.org/html/rfc7162#section-3.2.10
    if !vanished.is_empty() {
        Ok(Deleted::from_vanished(vanished, mod_seq))
    } else {
        Ok(Deleted::from_expunged(expunged, mod_seq))
    }
}

pub fn parse_append(
    mut lines: &[u8],
    unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
) -> Result<Appended> {
    let mut appended = Appended::default();

    loop {
        match imap_proto::parser::parse_response(lines) {
            Ok((rest, Response::Done { status, code, .. })) => {
                lines = rest;
                assert_eq!(status, imap_proto::Status::Ok);

                if let Some(ResponseCode::AppendUid(validity, uids)) = code {
                    appended.uid_validity = Some(validity);
                    appended.uids = Some(uids);
                }
            }
            Ok((rest, data)) => {
                lines = rest;
                if let Some(resp) = try_handle_unilateral(data, unsolicited) {
                    break Err(resp.into());
                }
            }
            _ => {
                return Err(Error::Parse(ParseError::Invalid(lines.to_vec())));
            }
        }

        if lines.is_empty() {
            break Ok(appended);
        }
    }
}

pub fn parse_noop(
    lines: Vec<u8>,
    unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
) -> Result<()> {
    let mut lines: &[u8] = &lines;

    loop {
        if lines.is_empty() {
            break Ok(());
        }

        match imap_proto::parser::parse_response(lines) {
            Ok((rest, data)) => {
                lines = rest;
                if let Some(resp) = try_handle_unilateral(data, unsolicited) {
                    break Err(resp.into());
                }
            }
            _ => {
                break Err(Error::Parse(ParseError::Invalid(lines.to_vec())));
            }
        }
    }
}

pub fn parse_mailbox(
    mut lines: &[u8],
    unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
) -> Result<Mailbox> {
    let mut mailbox = Mailbox::default();

    loop {
        match imap_proto::parser::parse_response(lines) {
            Ok((rest, Response::Done { status, code, .. })) => {
                assert!(rest.is_empty());
                lines = rest;

                // We wouldn't get to parsing if this wasn't an Ok response.
                assert_eq!(status, imap_proto::Status::Ok);

                if let Some(ResponseCode::ReadOnly) = code {
                    mailbox.is_read_only = true;
                }
            }
            Ok((rest, Response::Data { status, code, .. })) => {
                lines = rest;

                if let imap_proto::Status::Ok = status {
                } else {
                    // how can this happen for a Response::Data?
                    unreachable!();
                }

                match code {
                    Some(ResponseCode::HighestModSeq(seq)) => {
                        mailbox.highest_mod_seq = Some(seq);
                    }
                    Some(ResponseCode::UidValidity(uid)) => {
                        mailbox.uid_validity = Some(uid);
                    }
                    Some(ResponseCode::UidNext(unext)) => {
                        mailbox.uid_next = Some(unext);
                    }
                    Some(ResponseCode::Unseen(n)) => {
                        mailbox.unseen = Some(n);
                    }
                    Some(ResponseCode::PermanentFlags(flags)) => {
                        mailbox.permanent_flags.extend(Flag::from_strs(flags));
                    }
                    _ => {}
                }
            }
            Ok((rest, Response::MailboxData(m))) => {
                lines = rest;

                match m {
                    MailboxDatum::Status { mailbox, status } => {
                        unsolicited
                            .send(UnsolicitedResponse::Status {
                                mailbox: mailbox.into(),
                                attributes: status,
                            })
                            .unwrap();
                    }
                    MailboxDatum::Exists(e) => {
                        mailbox.exists = e;
                    }
                    MailboxDatum::Recent(r) => {
                        mailbox.recent = r;
                    }
                    MailboxDatum::Flags(flags) => {
                        mailbox.flags.extend(Flag::from_strs(flags));
                    }
                    _ => {}
                }
            }
            Ok((rest, Response::Expunge(n))) => {
                lines = rest;
                unsolicited.send(UnsolicitedResponse::Expunge(n)).unwrap();
            }
            Ok((_, resp)) => {
                break Err(resp.into());
            }
            _ => {
                break Err(Error::Parse(ParseError::Invalid(lines.to_vec())));
            }
        }

        if lines.is_empty() {
            break Ok(mailbox);
        }
    }
}

pub fn parse_status(
    mut lines: &[u8],
    mailbox_name: &str,
    unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
) -> Result<Mailbox> {
    let mut mailbox = Mailbox::default();
    let mut got_anything = false;
    while !lines.is_empty() {
        match imap_proto::parser::parse_response(lines) {
            Ok((
                rest,
                Response::MailboxData(MailboxDatum::Status {
                    mailbox: their_mailbox_name,
                    status,
                }),
            )) if their_mailbox_name == mailbox_name => {
                lines = rest;
                got_anything = true;
                for attr in status {
                    match attr {
                        StatusAttribute::HighestModSeq(v) => mailbox.highest_mod_seq = Some(v),
                        StatusAttribute::Messages(v) => mailbox.exists = v,
                        StatusAttribute::Recent(v) => mailbox.recent = v,
                        StatusAttribute::UidNext(v) => mailbox.uid_next = Some(v),
                        StatusAttribute::UidValidity(v) => mailbox.uid_validity = Some(v),
                        StatusAttribute::Unseen(v) => mailbox.unseen = Some(v),
                        _ => {} // needed because StatusAttribute is #[non_exhaustive]
                    }
                }
            }
            Ok((rest, data)) => {
                lines = rest;
                if let Some(resp) = try_handle_unilateral(data, unsolicited) {
                    return Err(resp.into());
                }
            }
            _ => {
                return Err(Error::Parse(ParseError::Invalid(lines.to_vec())));
            }
        }
    }
    if !got_anything {
        return Err(Error::MissingStatusResponse);
    }
    Ok(mailbox)
}

fn parse_ids_with<T: Extend<u32>>(
    lines: &[u8],
    unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
    mut collection: T,
) -> Result<T> {
    let mut lines = lines;
    loop {
        if lines.is_empty() {
            break Ok(collection);
        }

        match imap_proto::parser::parse_response(lines) {
            Ok((rest, Response::MailboxData(MailboxDatum::Search(c)))) => {
                lines = rest;
                collection.extend(c);
            }
            Ok((rest, Response::MailboxData(MailboxDatum::Sort(c)))) => {
                lines = rest;
                collection.extend(c);
            }
            Ok((rest, data)) => {
                lines = rest;
                if let Some(resp) = try_handle_unilateral(data, unsolicited) {
                    break Err(resp.into());
                }
            }
            _ => {
                break Err(Error::Parse(ParseError::Invalid(lines.to_vec())));
            }
        }
    }
}

pub fn parse_id_set(
    lines: &[u8],
    unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
) -> Result<HashSet<u32>> {
    parse_ids_with(lines, unsolicited, HashSet::new())
}

pub fn parse_id_seq(
    lines: &[u8],
    unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
) -> Result<Vec<u32>> {
    parse_ids_with(lines, unsolicited, Vec::new())
}

/// Parse a single unsolicited response from IDLE responses.
pub fn parse_idle(lines: &[u8]) -> (&[u8], Option<Result<UnsolicitedResponse>>) {
    match imap_proto::parser::parse_response(lines) {
        Ok((rest, response)) => match UnsolicitedResponse::try_from(response) {
            Ok(unsolicited) => (rest, Some(Ok(unsolicited))),
            Err(res) => (rest, Some(Err(res.into()))),
        },
        Err(nom::Err::Incomplete(_)) => (lines, None),
        Err(_) => (
            lines,
            Some(Err(Error::Parse(ParseError::Invalid(lines.to_vec())))),
        ),
    }
}

// Check if this is simply a unilateral server response (see Section 7 of RFC 3501).
//
// Returns `None` if the response was handled, `Some(res)` if not.
pub(crate) fn try_handle_unilateral<'a>(
    res: Response<'a>,
    unsolicited: &mut mpsc::Sender<UnsolicitedResponse>,
) -> Option<Response<'a>> {
    match UnsolicitedResponse::try_from(res) {
        Ok(response) => {
            unsolicited.send(response).ok();
            None
        }
        Err(unhandled) => Some(unhandled),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use imap_proto::types::*;
    use std::borrow::Cow;

    #[test]
    fn parse_capability_test() {
        let expected_capabilities = vec![
            Capability::Imap4rev1,
            Capability::Atom(Cow::Borrowed("STARTTLS")),
            Capability::Auth(Cow::Borrowed("GSSAPI")),
            Capability::Atom(Cow::Borrowed("LOGINDISABLED")),
        ];
        let lines = b"* CAPABILITY IMAP4rev1 STARTTLS AUTH=GSSAPI LOGINDISABLED\r\n";
        let (mut send, recv) = mpsc::channel();
        let capabilities = Capabilities::parse(lines.to_vec(), &mut send).unwrap();
        // shouldn't be any unexpected responses parsed
        assert!(recv.try_recv().is_err());
        assert_eq!(capabilities.len(), 4);
        for e in expected_capabilities {
            assert!(capabilities.has(&e));
        }
    }

    #[test]
    fn parse_capability_case_insensitive_test() {
        // Test that "IMAP4REV1" (instead of "IMAP4rev1") is accepted
        let expected_capabilities = vec![
            Capability::Imap4rev1,
            Capability::Atom(Cow::Borrowed("STARTTLS")),
        ];
        let lines = b"* CAPABILITY IMAP4REV1 STARTTLS\r\n";
        let (mut send, recv) = mpsc::channel();
        let capabilities = Capabilities::parse(lines.to_vec(), &mut send).unwrap();
        // shouldn't be any unexpected responses parsed
        assert!(recv.try_recv().is_err());
        assert_eq!(capabilities.len(), 2);
        for e in expected_capabilities {
            assert!(capabilities.has(&e));
        }
    }

    #[test]
    #[should_panic]
    fn parse_capability_invalid_test() {
        let (mut send, recv) = mpsc::channel();
        let lines = b"* JUNK IMAP4rev1 STARTTLS AUTH=GSSAPI LOGINDISABLED\r\n";
        Capabilities::parse(lines.to_vec(), &mut send).unwrap();
        assert!(recv.try_recv().is_err());
    }

    #[test]
    fn parse_names_test() {
        let lines = b"* LIST (\\HasNoChildren) \".\" \"INBOX\"\r\n";
        let (mut send, recv) = mpsc::channel();
        let names = Names::parse(lines.to_vec(), &mut send).unwrap();
        assert!(recv.try_recv().is_err());
        assert_eq!(names.len(), 1);
        let first = names.get(0).unwrap();
        assert_eq!(
            first.attributes(),
            &[NameAttribute::Extension(Cow::Borrowed("\\HasNoChildren"))]
        );
        assert_eq!(first.delimiter(), Some("."));
        assert_eq!(first.name(), "INBOX");
    }

    #[test]
    fn parse_fetches_empty() {
        let lines = b"";
        let (mut send, recv) = mpsc::channel();
        let fetches = Fetches::parse(lines.to_vec(), &mut send).unwrap();
        assert!(recv.try_recv().is_err());
        assert!(fetches.is_empty());
    }

    #[test]
    fn parse_fetches_test() {
        let lines = b"\
                    * 24 FETCH (FLAGS (\\Seen) UID 4827943)\r\n\
                    * 25 FETCH (FLAGS (\\Seen))\r\n";
        let (mut send, recv) = mpsc::channel();
        let fetches = Fetches::parse(lines.to_vec(), &mut send).unwrap();
        assert!(recv.try_recv().is_err());
        assert_eq!(fetches.len(), 2);
        let first = fetches.get(0).unwrap();
        assert_eq!(first.message, 24);
        assert_eq!(first.flags(), &[Flag::Seen]);
        assert_eq!(first.uid, Some(4827943));
        assert_eq!(first.body(), None);
        assert_eq!(first.header(), None);
        let second = fetches.get(1).unwrap();
        assert_eq!(second.message, 25);
        assert_eq!(second.flags(), &[Flag::Seen]);
        assert_eq!(second.uid, None);
        assert_eq!(second.body(), None);
        assert_eq!(second.header(), None);
    }

    #[test]
    fn parse_fetches_w_unilateral() {
        // https://github.com/mattnenterprise/rust-imap/issues/81
        let lines = b"\
            * 37 FETCH (UID 74)\r\n\
            * 1 RECENT\r\n";
        let (mut send, recv) = mpsc::channel();
        let fetches = Fetches::parse(lines.to_vec(), &mut send).unwrap();
        assert_eq!(recv.try_recv(), Ok(UnsolicitedResponse::Recent(1)));
        assert_eq!(fetches.len(), 1);
        let first = fetches.get(0).unwrap();
        assert_eq!(first.message, 37);
        assert_eq!(first.uid, Some(74));
    }

    #[test]
    fn parse_fetches_w_dovecot_info() {
        // Dovecot will sometimes send informational unsolicited
        // OK messages while performing long operations.
        // * OK Searched 91% of the mailbox, ETA 0:01
        let lines = b"\
            * OK Searched 91% of the mailbox, ETA 0:01\r\n\
            * 37 FETCH (UID 74)\r\n";
        let (mut send, recv) = mpsc::channel();
        let fetches = Fetches::parse(lines.to_vec(), &mut send).unwrap();
        assert_eq!(
            recv.try_recv(),
            Ok(UnsolicitedResponse::Ok {
                code: None,
                information: Some(String::from("Searched 91% of the mailbox, ETA 0:01")),
            })
        );
        assert_eq!(fetches.len(), 1);
        let first = fetches.get(0).unwrap();
        assert_eq!(first.message, 37);
        assert_eq!(first.uid, Some(74));
    }

    #[test]
    fn parse_names_w_unilateral() {
        let lines = b"\
                    * LIST (\\HasNoChildren) \".\" \"INBOX\"\r\n\
                    * 4 EXPUNGE\r\n";
        let (mut send, recv) = mpsc::channel();
        let names = Names::parse(lines.to_vec(), &mut send).unwrap();

        assert_eq!(recv.try_recv().unwrap(), UnsolicitedResponse::Expunge(4));

        assert_eq!(names.len(), 1);
        let first = names.get(0).unwrap();
        assert_eq!(
            first.attributes(),
            &[NameAttribute::Extension(Cow::Borrowed("\\HasNoChildren"))]
        );
        assert_eq!(first.delimiter(), Some("."));
        assert_eq!(first.name(), "INBOX");
    }

    #[test]
    fn parse_capabilities_w_unilateral() {
        let expected_capabilities = vec![
            Capability::Imap4rev1,
            Capability::Atom(Cow::Borrowed("STARTTLS")),
            Capability::Auth(Cow::Borrowed("GSSAPI")),
            Capability::Atom(Cow::Borrowed("LOGINDISABLED")),
        ];
        let lines = b"\
                    * CAPABILITY IMAP4rev1 STARTTLS AUTH=GSSAPI LOGINDISABLED\r\n\
                    * STATUS dev.github (MESSAGES 10 UIDNEXT 11 UIDVALIDITY 1408806928 UNSEEN 0)\r\n\
                    * 4 EXISTS\r\n";
        let (mut send, recv) = mpsc::channel();
        let capabilities = Capabilities::parse(lines.to_vec(), &mut send).unwrap();

        assert_eq!(capabilities.len(), 4);
        for e in expected_capabilities {
            assert!(capabilities.has(&e));
        }

        assert_eq!(
            recv.try_recv().unwrap(),
            UnsolicitedResponse::Status {
                mailbox: "dev.github".to_string(),
                attributes: vec![
                    StatusAttribute::Messages(10),
                    StatusAttribute::UidNext(11),
                    StatusAttribute::UidValidity(1408806928),
                    StatusAttribute::Unseen(0)
                ]
            }
        );
        assert_eq!(recv.try_recv().unwrap(), UnsolicitedResponse::Exists(4));
    }

    #[test]
    fn parse_ids_w_unilateral() {
        let lines = b"\
            * SEARCH 23 42 4711\r\n\
            * 1 RECENT\r\n\
            * STATUS INBOX (MESSAGES 10 UIDNEXT 11 UIDVALIDITY 1408806928 UNSEEN 0)\r\n";
        let (mut send, recv) = mpsc::channel();
        let ids = parse_id_set(lines, &mut send).unwrap();

        assert_eq!(ids, [23, 42, 4711].iter().cloned().collect());

        assert_eq!(recv.try_recv().unwrap(), UnsolicitedResponse::Recent(1));
        assert_eq!(
            recv.try_recv().unwrap(),
            UnsolicitedResponse::Status {
                mailbox: "INBOX".to_string(),
                attributes: vec![
                    StatusAttribute::Messages(10),
                    StatusAttribute::UidNext(11),
                    StatusAttribute::UidValidity(1408806928),
                    StatusAttribute::Unseen(0)
                ]
            }
        );
    }

    #[test]
    fn parse_ids_test() {
        let lines = b"* SEARCH 1600 1698 1739 1781 1795 1885 1891 1892 1893 1898 1899 1901 1911 1926 1932 1933 1993 1994 2007 2032 2033 2041 2053 2062 2063 2065 2066 2072 2078 2079 2082 2084 2095 2100 2101 2102 2103 2104 2107 2116 2120 2135 2138 2154 2163 2168 2172 2189 2193 2198 2199 2205 2212 2213 2221 2227 2267 2275 2276 2295 2300 2328 2330 2332 2333 2334\r\n\
            * SEARCH 2335 2336 2337 2338 2339 2341 2342 2347 2349 2350 2358 2359 2362 2369 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2390 2392 2397 2400 2401 2403 2405 2409 2411 2414 2417 2419 2420 2424 2426 2428 2439 2454 2456 2467 2468 2469 2490 2515 2519 2520 2521\r\n";
        let (mut send, recv) = mpsc::channel();
        let ids = parse_id_set(lines, &mut send).unwrap();
        assert!(recv.try_recv().is_err());
        let ids: HashSet<u32> = ids.iter().cloned().collect();
        assert_eq!(
            ids,
            [
                1600, 1698, 1739, 1781, 1795, 1885, 1891, 1892, 1893, 1898, 1899, 1901, 1911, 1926,
                1932, 1933, 1993, 1994, 2007, 2032, 2033, 2041, 2053, 2062, 2063, 2065, 2066, 2072,
                2078, 2079, 2082, 2084, 2095, 2100, 2101, 2102, 2103, 2104, 2107, 2116, 2120, 2135,
                2138, 2154, 2163, 2168, 2172, 2189, 2193, 2198, 2199, 2205, 2212, 2213, 2221, 2227,
                2267, 2275, 2276, 2295, 2300, 2328, 2330, 2332, 2333, 2334, 2335, 2336, 2337, 2338,
                2339, 2341, 2342, 2347, 2349, 2350, 2358, 2359, 2362, 2369, 2371, 2372, 2373, 2374,
                2375, 2376, 2377, 2378, 2379, 2380, 2381, 2382, 2383, 2384, 2385, 2386, 2390, 2392,
                2397, 2400, 2401, 2403, 2405, 2409, 2411, 2414, 2417, 2419, 2420, 2424, 2426, 2428,
                2439, 2454, 2456, 2467, 2468, 2469, 2490, 2515, 2519, 2520, 2521
            ]
            .iter()
            .cloned()
            .collect()
        );

        let lines = b"* SEARCH\r\n";
        let (mut send, recv) = mpsc::channel();
        let ids = parse_id_set(lines, &mut send).unwrap();
        assert!(recv.try_recv().is_err());
        let ids: HashSet<u32> = ids.iter().cloned().collect();
        assert_eq!(ids, HashSet::<u32>::new());

        let lines = b"* SORT\r\n";
        let (mut send, recv) = mpsc::channel();
        let ids = parse_id_seq(lines, &mut send).unwrap();
        assert!(recv.try_recv().is_err());
        let ids: Vec<u32> = ids.iter().cloned().collect();
        assert_eq!(ids, Vec::<u32>::new());
    }

    #[test]
    fn parse_vanished_test() {
        // VANISHED can appear if the user has enabled QRESYNC (RFC 7162), in response to
        // SELECT/EXAMINE (QRESYNC); UID FETCH (VANISHED); or EXPUNGE commands. In the first
        // two cases the VANISHED response will be a different type than expected
        // and so goes into the unsolicited responses channel.
        let lines = b"* VANISHED 3\r\n";
        let (mut send, recv) = mpsc::channel();
        let resp = parse_expunge(lines.to_vec(), &mut send).unwrap();

        // Should be not empty, and have no seqs
        assert!(!resp.is_empty());
        assert_eq!(None, resp.seqs().next());

        // Should have one UID response
        let mut uids = resp.uids();
        assert_eq!(Some(3), uids.next());
        assert_eq!(None, uids.next());

        // Should be nothing in the unsolicited responses channel
        assert!(recv.try_recv().is_err());

        // Test VANISHED mixed with FETCH
        let lines = b"* VANISHED (EARLIER) 3:8,12,50:60\r\n\
                      * 49 FETCH (UID 117 FLAGS (\\Seen \\Answered) MODSEQ (90060115194045001))\r\n";

        let fetches = Fetches::parse(lines.to_vec(), &mut send).unwrap();
        match recv.try_recv().unwrap() {
            UnsolicitedResponse::Vanished { earlier, uids } => {
                assert!(earlier);
                assert_eq!(uids.len(), 3);
                assert_eq!(*uids[0].start(), 3);
                assert_eq!(*uids[0].end(), 8);
                assert_eq!(*uids[1].start(), 12);
                assert_eq!(*uids[1].end(), 12);
                assert_eq!(*uids[2].start(), 50);
                assert_eq!(*uids[2].end(), 60);
            }
            what => panic!("Unexpected response in unsolicited responses: {:?}", what),
        }
        assert!(recv.try_recv().is_err());
        assert_eq!(fetches.len(), 1);
        let first = fetches.get(0).unwrap();
        assert_eq!(first.message, 49);
        assert_eq!(first.flags(), &[Flag::Seen, Flag::Answered]);
        assert_eq!(first.uid, Some(117));
        assert_eq!(first.body(), None);
        assert_eq!(first.header(), None);
    }

    #[test]
    fn parse_expunged_mod_seq_test() {
        // VANISHED can appear if the user has enabled QRESYNC (RFC 7162), in response to
        // SELECT/EXAMINE (QRESYNC); UID FETCH (VANISHED); or EXPUNGE commands. In the latter
        // case, the VANISHED responses will be parsed with the response and the list of
        // expunged message is included in the returned struct.
        let (mut send, recv) = mpsc::channel();

        // Test VANISHED mixed with FETCH
        let lines = b"* VANISHED 3:5,12\r\n\
                      B202 OK [HIGHESTMODSEQ 20010715194045319] expunged\r\n";

        let deleted = parse_expunge(lines.to_vec(), &mut send).unwrap();

        // No unsolicited responses, they are aggregated in the returned type
        assert!(recv.try_recv().is_err());

        assert_eq!(deleted.mod_seq, Some(20010715194045319));
        let mut del = deleted.uids();
        assert_eq!(del.next(), Some(3));
        assert_eq!(del.next(), Some(4));
        assert_eq!(del.next(), Some(5));
        assert_eq!(del.next(), Some(12));
        assert_eq!(del.next(), None);
    }

    #[test]
    fn parse_append_uid() {
        // If the user has enabled UIDPLUS (RFC 4315), the response contains an APPENDUID
        // response code followed by the UIDVALIDITY of the destination mailbox and the
        // UID assigned to the appended message in the destination mailbox.
        // If the MULTIAPPEND extension is also used, there can be multiple UIDs.
        let lines = b"A003 OK [APPENDUID 38505 3955] APPEND completed\r\n";
        let (mut send, recv) = mpsc::channel();
        let resp = parse_append(lines, &mut send).unwrap();

        assert!(recv.try_recv().is_err());
        assert_eq!(resp.uid_validity, Some(38505));
        match resp.uids {
            Some(uid_list) => {
                let mut it = uid_list.iter();
                assert_eq!(it.next(), Some(&UidSetMember::Uid(3955)));
                assert_eq!(it.next(), None);
            }
            None => panic!("Missing UIDs in APPEND response"),
        };
    }

    #[test]
    fn parse_multiappend_uid() {
        // If the user has enabled UIDPLUS (RFC 4315), the response contains an APPENDUID
        // response code followed by the UIDVALIDITY of the destination mailbox and the
        // UID assigned to the appended message in the destination mailbox.
        // If the MULTIAPPEND extension is also used, there can be multiple UIDs.
        let lines = b"A003 OK [APPENDUID 38505 3955:3957] APPEND completed\r\n";
        let (mut send, recv) = mpsc::channel();
        let resp = parse_append(lines, &mut send).unwrap();

        assert!(recv.try_recv().is_err());
        assert_eq!(resp.uid_validity, Some(38505));
        match resp.uids {
            Some(uid_list) => {
                let mut it = uid_list.iter();
                assert_eq!(it.next(), Some(&UidSetMember::UidRange(3955..=3957)));
                assert_eq!(it.next(), None);
            }
            None => panic!("Missing UIDs in APPEND response"),
        };
    }
}