vomit-sync 0.10.1

A library for IMAP to maildir synchronization
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
use imap_proto::types::UidSetMember;
use log::{error, info, trace, warn};
use maildir::Maildir;
use std::collections::BTreeSet;
use std::fs;
use std::io;

use crate::flags;
use crate::mailboxes;
use crate::seqset::SeqSet;
use crate::state;
use crate::SyncError;

fn imap_delete_uids<T: io::Read + io::Write>(
    session: &mut imap::Session<T>,
    uids: &SeqSet,
) -> Result<Option<u64>, SyncError> {
    trace!("deleting remote UIDs {}", uids);
    session.uid_store(format!("{}", uids), "+FLAGS.SILENT (\\Deleted)")?;
    let d = session.uid_expunge(format!("{}", uids))?;
    if d.mod_seq.is_none() {
        warn!("EXPUNGE response without MODSEQ");
    }
    Ok(d.mod_seq)
}

fn imap_apply_local_changes<T: io::Read + io::Write>(
    session: &mut imap::Session<T>,
    mailbox: &String,
    state: &mut state::SyncState,
    maildir: &maildir::Maildir,
) -> Result<(), SyncError> {
    trace!("pushing local changes (if any)");
    while let Some((uid, local_modification)) = state.local_changes.modified.pop_first() {
        let imap_flags =
            flags::to_imap_list(&flags::maildir_to_imap(local_modification.new_flags()));
        trace!("updating remote flags for UID {}", uid);
        let store = session.uid_store(
            format!("{}", uid),
            format!(
                "(UNCHANGEDSINCE {}) FLAGS ({})",
                state.last_seen.highest_mod_seq, imap_flags
            ),
        )?;
        for s in store.iter() {
            if let Some(modseq) = s.mod_seq() {
                // Not sure if these always come in order, so use max()
                state.last_seen.highest_mod_seq =
                    std::cmp::max(state.last_seen.highest_mod_seq, modseq);
            } else {
                warn!("{}: store response without MODSEQ", mailbox);
            }
        }
        state.apply(uid, state::LocalChange::Modification(local_modification));
    }
    if !state.local_changes.deleted.is_empty() {
        let set = SeqSet::from_iter(state.local_changes.deleted.keys().cloned());
        if let Some(mod_seq) = imap_delete_uids(session, &set)? {
            state.last_seen.highest_mod_seq = mod_seq;
        }
        while let Some((uid, local_deletion)) = state.local_changes.deleted.pop_first() {
            state.apply(uid, state::LocalChange::Deletion(local_deletion));
        }
    }
    while let Some(local_addition) = state.local_changes.added.pop() {
        trace!("uploading local mail {}", local_addition.id());
        let mail = maildir
            .find(local_addition.id())
            .expect("new mail not found on disk, inconsistent state, giving up");
        let data = fs::read(mail.path())?;
        let flags = flags::maildir_to_imap(mail.flags());
        let a = session.append(mailbox, &data).flags(flags).finish()?;
        match a.uids {
            Some(uids) => {
                for set in uids {
                    match set {
                        UidSetMember::UidRange(_) => {
                            // TODO support MULTIAPPEND
                            warn!("Unexpected UID range in APPEND response");
                        }
                        UidSetMember::Uid(uid) => {
                            // state.insert(uid, (local_addition.id.clone(), String::from(mail.flags())));
                            state.apply(uid, state::LocalChange::Addition(local_addition));
                            break;
                        }
                    }
                }
            }
            None => {
                return Err(SyncError::Error("APPEND returned no UIDs"));
            }
        }
        // E.g. Dovecot sends the new highest mod seq in an untagged OK. Cyrus does not.
        let mut need_mod_seq = true;
        for unsol in session.unsolicited_responses.try_iter() {
            match unsol {
                imap::types::UnsolicitedResponse::Ok { code, .. } => {
                    if let Some(imap_proto::types::ResponseCode::HighestModSeq(seq)) = code {
                        state.last_seen.highest_mod_seq = seq;
                        need_mod_seq = false;
                    }
                }
                imap::types::UnsolicitedResponse::Exists(_)
                | imap::types::UnsolicitedResponse::Recent(_) => (),
                _ => trace!("{}: unhandled unsolicited response: {:?}", mailbox, unsol),
            }
        }
        if need_mod_seq {
            // Send examine to get the final mod seq
            trace!(
                "{}: sending final EXAMINE for highest mod sequence",
                mailbox
            );
            let e = session.examine(mailbox)?;
            match e.highest_mod_seq {
                Some(mod_seq) => state.last_seen.highest_mod_seq = mod_seq,
                None => warn!("Failed to get highest mod sequence after uploads!"),
            }
        }
    }

    Ok(())
}

pub(crate) fn pull<T: io::Read + io::Write>(
    session: &mut imap::Session<T>,
    sync_job: &mailboxes::SyncJob,
    maildir: Maildir,
    remote: imap::types::Mailbox,
    mut state: state::SyncState,
) -> Result<(), SyncError> {
    let mailbox = &sync_job.name;

    // refresh_needed indicates that a refresh is needed even if nothing changed on the
    // remote side, e.g. because changes were made locally that should be "undone".
    // TODO maybe return the actual UIDs that need to be refreshed instead of just boolean?
    trace!("Discarding local state during pull");
    let mut refresh_needed = state.discard_local_changes()?;

    // new_uid_validity contains the remote's current UIDVALIDITY value
    let new_uid_validity = remote.uid_validity.ok_or(SyncError::Error(
        "Server is incompatible: no UIDVALIDITY provided",
    ))?;

    // new_highest_mod_seq contains the remote's current highest mod sequence, or 0 if not provided
    let new_highest_mod_seq: u64 = remote.highest_mod_seq.unwrap_or(0);

    // If either the server or the local state cannot supply a highest mod sequence,
    // then the meta data of all messages has to be refreshed.
    if new_highest_mod_seq == 0 {
        refresh_needed = true;
        warn!("No highest_mod_seq for mailbox {}", mailbox);
    }
    if state.last_seen.highest_mod_seq == 0 {
        refresh_needed = true;
    }

    // If absolutelty nothing changed, we can take a shortcut...
    if new_highest_mod_seq == state.last_seen.highest_mod_seq
        && new_uid_validity == state.last_seen.uid_validity
        && !refresh_needed
    {
        trace!("Nothing to do for {}", mailbox);
        state.save()?;
        return Ok(());
    }

    // Check for UIDVALIDITY changes, which require resyncing everything, unless we are just creating this mailbox
    if new_uid_validity != state.last_seen.uid_validity
        && !matches!(sync_job.action, mailboxes::SyncAction::CreateLocal)
    {
        info!("UID validity change in {} forces full resync", mailbox);
        state.clear();
        state.save()?;
        // The reload and discard will cause the mails on disk to be purged
        state = state::SyncState::load(&maildir.path())?;
        state.discard_local_changes()?;
        state.last_seen.highest_mod_seq = 0;
    }

    trace!("In {}: {:?}", mailbox, state.local_changes);

    let fetch = if state.is_empty() {
        // Do a full fetch if either this is the first time fetching this mailbox
        // or if the state was cleared due to UIDVALIDITY change.
        trace!("Performing full fetch for {}", mailbox);
        session.uid_fetch("1:*", "(FLAGS UID BODY.PEEK[])")?
    } else {
        let f = if refresh_needed {
            trace!("Performing refresh fetch for {}", mailbox);
            session.uid_fetch("1:*", "(FLAGS UID)")?
        } else {
            trace!(
                "Performing refresh fetch for {} ({} -> {})",
                mailbox,
                state.last_seen.highest_mod_seq,
                new_highest_mod_seq
            );
            session.uid_fetch(
                "1:*",
                format!(
                    "(FLAGS UID) (CHANGEDSINCE {} VANISHED)",
                    state.last_seen.highest_mod_seq
                ),
            )?
        };

        let mut to_fetch = SeqSet::new();
        let mut all_remote_uids: BTreeSet<u32> = BTreeSet::new();
        let all_local_uids: BTreeSet<u32> = state.uids();

        for mail in f.iter() {
            let uid = mail
                .uid
                .ok_or(SyncError::Error("Server did not send UID, giving up"))?;

            // Collect mails deleted remotely based on the type of meta data fetch
            if refresh_needed {
                // For the full meta data refresh, simply collect all UIDs from the response,
                // then delete all local mails that are not in this set
                all_remote_uids.insert(uid);
            }

            match &state.get(&uid) {
                Some((id, local_flags)) => {
                    let id = id.clone();

                    // Compare flags etc..
                    let remote_flags = flags::imap_to_maildir(mail.flags());
                    if local_flags != &remote_flags {
                        // This errors out if there are conflicting local changes
                        state.safe_update(uid, &id, &remote_flags)?;
                    }

                    if maildir.set_flags(&id, &remote_flags).is_err() {
                        // Maybe the message is still in new?
                        if maildir
                            .move_new_to_cur_with_flags(&id, &remote_flags)
                            .is_err()
                        {
                            return Err(SyncError::E(format!(
                                "Failed to set flags for {} - inconsistent state?",
                                id
                            )));
                        }
                    }
                }
                None => {
                    // Fetch the full mail later on
                    to_fetch.insert(uid);
                }
            }
        }

        let mut delete_local = |uid: &u32| -> Result<(), SyncError> {
            trace!("{}: mail deleted on remote side: UID {}", mailbox, uid);
            // In a pull, we don't care if a remotely deleted mail does not exist locally
            if let Ok(Some(local)) = state.safe_delete(uid) {
                maildir.delete(&local).map_err(|_| {
                    SyncError::Error("Error deleting mail locally - inconsistent state?")
                })?;
            }
            Ok(())
        };

        // Handle mails that have been expunged on the remote side.
        // If QRESYNC is used, they are supplied in a VANISHED response
        for unsol in session.unsolicited_responses.try_iter() {
            if let imap::types::UnsolicitedResponse::Vanished { earlier: _, uids } = unsol {
                for uid in uids.into_iter().flatten() {
                    delete_local(&uid)?;
                }
            } else {
                trace!("{}: unhandled unsolicited response: {:?}", mailbox, unsol);
            }
        }
        if refresh_needed {
            let deleted = all_local_uids.difference(&all_remote_uids);
            for uid in deleted {
                delete_local(uid)?;
            }
        }

        // return the final fetch for full messages
        session.uid_fetch(format!("{}", to_fetch), "(FLAGS UID BODY.PEEK[])")?
    };

    // This loop fetches all mails that are as of yet unknown locally
    for mail in fetch.iter() {
        let body = mail
            .body()
            .ok_or(SyncError::Error("downloaded message without content"))?;
        let uid = mail
            .uid
            .ok_or(SyncError::Error("Server did not send UID, giving up"))?;

        if uid > state.last_seen.uid {
            state.last_seen.uid = uid;
        }
        let recent = (mail.flags().is_empty()) || (mail.flags()[0] == imap::types::Flag::Recent);
        if !recent {
            let flags = flags::imap_to_maildir(mail.flags());
            match maildir.store_cur_with_flags(body, &flags) {
                Ok(newid) => {
                    state.insert(uid, (newid, flags));
                }
                Err(e) => {
                    error!("{}: error saving new message: {}", mailbox, e);
                    state.save()?;
                    return Err(SyncError::MaildirError(e));
                }
            };
        } else {
            match maildir.store_new(body) {
                Ok(newid) => {
                    state.insert(uid, (newid, String::from("")));
                }
                Err(e) => {
                    error!("{}: error saving new message: {}", mailbox, e);
                    state.save()?;
                    return Err(SyncError::MaildirError(e));
                }
            };
        }
    }

    // All remote state has been applied locally
    state.last_seen.highest_mod_seq = new_highest_mod_seq;
    state.last_seen.uid_validity = new_uid_validity;
    state.save()?;
    Ok(())
}

pub(crate) fn push<T: io::Read + io::Write>(
    session: &mut imap::Session<T>,
    sync_job: &mailboxes::SyncJob,
    maildir: Maildir,
    remote: imap::types::Mailbox,
    mut state: state::SyncState,
) -> Result<(), SyncError> {
    let mailbox = &sync_job.name;

    // refresh_needed indicates that a refresh is needed even if nothing changed on the
    // remote side, e.g. because changes were made locally that should be "undone".
    let mut refresh_needed = false;

    // new_uid_validity contains the remote's current UIDVALIDITY value
    let new_uid_validity = remote.uid_validity.ok_or(SyncError::Error(
        "Server is incompatible: no UIDVALIDITY provided",
    ))?;

    // new_highest_mod_seq contains the remote's current highest mod sequence, or 0 if not provided
    let new_highest_mod_seq: u64 = remote.highest_mod_seq.unwrap_or(0);

    // If either the server or the local state cannot supply a highest mod sequence,
    // then the meta data of all messages has to be refreshed.
    if new_highest_mod_seq == 0 {
        refresh_needed = true;
        warn!("No highest_mod_seq for mailbox {}", mailbox);
    }
    if state.last_seen.highest_mod_seq == 0 {
        refresh_needed = true;
    }

    // If absolutelty nothing changed, we can take a shortcut...
    if new_highest_mod_seq == state.last_seen.highest_mod_seq
        && new_uid_validity == state.last_seen.uid_validity
        && !refresh_needed
        && !state.has_local_changes()
    {
        trace!(
            "Nothing to do for {} ({}/{})",
            mailbox,
            new_uid_validity,
            new_highest_mod_seq
        );
        state.save()?;
        return Ok(());
    }

    // Check for UIDVALIDITY changes, which require resyncing everything, unless we are just creating this mailbox
    if new_uid_validity != state.last_seen.uid_validity {
        info!("UID validity change in {} forces full resync", mailbox);
        state.clear();
        state.save()?;
        // The reload after clear will cause all local mails to be registered as added
        state = state::SyncState::load(&maildir.path())?;
        state.last_seen.highest_mod_seq = 0;
        refresh_needed = true;
        // If we are just creating this mailbox remotely, we can from now on assume that the UIDVALIDITY matches,
        // a simple refresh is good enough (no need to delete anything from an empty mailbox)
        if matches!(sync_job.action, mailboxes::SyncAction::CreateRemote) {
            state.last_seen.uid_validity = new_uid_validity;
        }
    }

    trace!("In {}: {:?}", mailbox, state.local_changes);

    // Find all remote mails that should be deleted.
    let deletion_seqset = if state.last_seen.uid_validity != new_uid_validity {
        // Clear everything if state was cleared due to UIDVALIDITY change.
        // All local mails are registered as locally added in this case and will
        // be re-uploaded when applying the local changes.
        trace!("Clearing entire mailbox {}", mailbox);
        SeqSet::from([1, u32::MAX])
    } else {
        let f = if refresh_needed {
            trace!("Performing refresh fetch for {}", mailbox);
            session.uid_fetch("1:*", "(FLAGS UID)")?
        } else {
            trace!(
                "Performing refresh fetch for {} ({} -> {})",
                mailbox,
                state.last_seen.highest_mod_seq,
                new_highest_mod_seq
            );
            session.uid_fetch(
                "1:*",
                format!(
                    "(FLAGS UID) (CHANGEDSINCE {} VANISHED)",
                    state.last_seen.highest_mod_seq
                ),
            )?
        };

        let mut all_remote_uids: BTreeSet<u32> = BTreeSet::new();
        let all_local_uids: BTreeSet<u32> = state.uids();
        let mut to_delete = SeqSet::new();

        for mail in f.iter() {
            let uid = mail
                .uid
                .ok_or(SyncError::Error("Server did not send UID, giving up"))?;

            // Collect mails deleted remotely based on the type of meta data fetch
            if refresh_needed {
                // For the full meta data refresh, simply collect all UIDs from the response,
                // then delete all local mails that are not in this set
                all_remote_uids.insert(uid);
            }

            match &state.get(&uid) {
                Some((_, local_flags)) => {
                    // Compare flags etc..
                    let remote_flags = flags::imap_to_maildir(mail.flags());
                    if local_flags != &remote_flags {
                        let store = session.uid_store(
                            format!("{}", uid),
                            format!(
                                "(UNCHANGEDSINCE {}) FLAGS ({})",
                                new_highest_mod_seq, local_flags
                            ),
                        )?;
                        for s in store.iter() {
                            if let Some(modseq) = s.mod_seq() {
                                // Not sure if these always come in order, so use max()
                                state.last_seen.highest_mod_seq =
                                    std::cmp::max(state.last_seen.highest_mod_seq, modseq);
                            } else {
                                warn!("store response without MODSEQ");
                            }
                        }
                    }
                }
                None => {
                    // Delete mail later on
                    to_delete.insert(uid);
                }
            }
        }

        // If a mail was deleted remotely it will be removed from the state and
        // added to local changes, to be uploaded again under a new UID.
        let restore_mail = |uid: &u32, state: &mut state::SyncState| -> Result<(), SyncError> {
            trace!(
                "{}: restoring mail deleted on remote side: UID {}",
                mailbox,
                uid
            );
            if let Some((id, flags)) = state.remove(uid) {
                state
                    .local_changes
                    .added
                    .push(state::LocalAddition::new(id, flags));
            }
            Ok(())
        };

        // Handle mails that have been expunged on the remote side.
        // If QRESYNC is used, they are supplied in a VANISHED response
        for unsol in session.unsolicited_responses.try_iter() {
            if let imap::types::UnsolicitedResponse::Vanished { earlier: _, uids } = unsol {
                for uid in uids.into_iter().flatten() {
                    restore_mail(&uid, &mut state)?;
                }
            } else {
                trace!("{}: unhandled unsolicited response: {:?}", mailbox, unsol);
            }
        }
        if refresh_needed {
            let deleted = all_local_uids.difference(&all_remote_uids);
            for uid in deleted {
                restore_mail(uid, &mut state)?;
            }
        };

        to_delete
    };

    if !deletion_seqset.is_empty() {
        if let Some(mod_seq) = imap_delete_uids(session, &deletion_seqset)? {
            state.last_seen.highest_mod_seq = mod_seq;
        }
    }

    // All remote changes have been undone
    state.last_seen.highest_mod_seq = new_highest_mod_seq;
    state.last_seen.uid_validity = new_uid_validity;

    // Upload local changes
    imap_apply_local_changes(session, mailbox, &mut state, &maildir)?;

    state.save()?;
    Ok(())
}

pub(crate) fn sync<T: io::Read + io::Write>(
    session: &mut imap::Session<T>,
    sync_job: &mailboxes::SyncJob,
    maildir: Maildir,
    remote: imap::types::Mailbox,
    mut state: state::SyncState,
) -> Result<(), SyncError> {
    let mailbox = &sync_job.name;

    // refresh_needed indicates that a refresh is needed even if nothing changed on the
    // remote side, e.g. because changes were made locally that should be "undone".
    let mut refresh_needed = false;

    // new_uid_validity contains the remote's current UIDVALIDITY value
    let new_uid_validity = remote.uid_validity.ok_or(SyncError::Error(
        "Server is incompatible: no UIDVALIDITY provided",
    ))?;

    // new_highest_mod_seq contains the remote's current highest mod sequence, or 0 if not provided
    let new_highest_mod_seq: u64 = remote.highest_mod_seq.unwrap_or(0);

    // If either the server or the local state cannot supply a highest mod sequence,
    // then the meta data of all messages has to be refreshed.
    if new_highest_mod_seq == 0 {
        refresh_needed = true;
        warn!("No highest_mod_seq for mailbox {}", mailbox);
    }
    if state.last_seen.highest_mod_seq == 0 {
        refresh_needed = true;
    }

    // If absolutelty nothing changed, we can take a shortcut...
    if new_highest_mod_seq == state.last_seen.highest_mod_seq
        && new_uid_validity == state.last_seen.uid_validity
        && !refresh_needed
        && !state.has_local_changes()
    {
        trace!("Nothing to do for {}", mailbox);
        state.save()?;
        return Ok(());
    }

    // Check for UIDVALIDITY changes, which require resyncing everything, unless we are just creating this mailbox
    if new_uid_validity != state.last_seen.uid_validity {
        // If we are just creating this mailbox remotely, we can from now on assume that the UIDVALIDITY matches,
        // a simple refresh is good enough (no need to delete anything from an empty mailbox)
        if matches!(sync_job.action, mailboxes::SyncAction::CreateRemote) {
            state.last_seen.uid_validity = new_uid_validity;
        } else {
            if state.has_local_changes() {
                let msg = format!("UID validity change in {} with pending local changes makes resynchronization impossible", mailbox);
                error!("{}", &msg);
                return Err(SyncError::E(msg));
            }
            info!("UID validity change in {} forces full resync", mailbox);
            state.clear();
            state.save()?;
            // The reload and discard will cause the mails on disk to be purged
            state = state::SyncState::load(&maildir.path())?;
            state.discard_local_changes()?;
            state.last_seen.highest_mod_seq = 0;
        }
    }

    trace!("In {}: {:?}", mailbox, state.local_changes);

    let fetch = if state.is_empty() {
        // Do a full fetch if either this is the first time fetching this mailbox
        // or if the state was cleared due to UIDVALIDITY change.
        trace!("Performing full fetch for {}", mailbox);
        session.uid_fetch("1:*", "(FLAGS UID BODY.PEEK[])")?
    } else {
        let f = if refresh_needed {
            trace!("Performing refresh fetch for {}", mailbox);
            session.uid_fetch("1:*", "(FLAGS UID)")?
        } else {
            trace!(
                "Performing refresh fetch for {} ({} -> {})",
                mailbox,
                state.last_seen.highest_mod_seq,
                new_highest_mod_seq
            );
            session.uid_fetch(
                "1:*",
                format!(
                    "(FLAGS UID) (CHANGEDSINCE {} VANISHED)",
                    state.last_seen.highest_mod_seq
                ),
            )?
        };

        let mut to_fetch = SeqSet::new();
        let mut all_remote_uids: BTreeSet<u32> = BTreeSet::new();
        let all_local_uids: BTreeSet<u32> = state.uids();

        for mail in f.iter() {
            let uid = mail
                .uid
                .ok_or(SyncError::Error("Server did not send UID, giving up"))?;

            // Collect mails deleted remotely based on the type of meta data fetch
            if refresh_needed {
                // For the full meta data refresh, simply collect all UIDs from the response,
                // then delete all local mails that are not in this set
                all_remote_uids.insert(uid);
            }

            match &state.get(&uid) {
                Some((id, local_flags)) => {
                    let id = id.clone();

                    // Compare flags etc..
                    let remote_flags = flags::imap_to_maildir(mail.flags());
                    if local_flags != &remote_flags {
                        // This errors out if there are conflicting local changes
                        state.safe_update(uid, &id, &remote_flags)?;
                    }

                    if maildir.set_flags(&id, &remote_flags).is_err() {
                        // Maybe the message is still in new?
                        if maildir
                            .move_new_to_cur_with_flags(&id, &remote_flags)
                            .is_err()
                        {
                            return Err(SyncError::E(format!(
                                "Failed to set flags for {} - inconsistent state?",
                                id
                            )));
                        }
                    }
                }
                None => {
                    // Fetch the full mail later on
                    to_fetch.insert(uid);
                }
            }
        }

        let mut delete_mail = |uid: &u32| -> Result<(), SyncError> {
            trace!("{}: mail deleted on remote side: UID {}", mailbox, uid);
            // This errors out if there are conflicting local changes
            if let Some(local) = state.safe_delete(uid)? {
                maildir.delete(&local).map_err(|_| {
                    SyncError::Error("Error deleting mail locally - inconsistent state?")
                })?;
            }
            Ok(())
        };

        // Handle mails that have been expunged on the remote side.
        // If QRESYNC is used, they are supplied in a VANISHED response
        for unsol in session.unsolicited_responses.try_iter() {
            if let imap::types::UnsolicitedResponse::Vanished { earlier: _, uids } = unsol {
                for uid in uids.into_iter().flatten() {
                    delete_mail(&uid)?;
                }
            } else {
                trace!("{}: unhandled unsolicited response: {:?}", mailbox, unsol);
            }
        }
        if refresh_needed {
            let deleted = all_local_uids.difference(&all_remote_uids);
            for uid in deleted {
                delete_mail(uid)?;
            }
        }

        // return the final fetch for full messages
        session.uid_fetch(format!("{}", to_fetch), "(FLAGS UID BODY.PEEK[])")?
    };

    // This loop fetches all mails that are as of yet unknown locally
    for mail in fetch.iter() {
        let body = mail
            .body()
            .ok_or(SyncError::Error("downloaded message without content"))?;
        let uid = mail
            .uid
            .ok_or(SyncError::Error("Server did not send UID, giving up"))?;

        if uid > state.last_seen.uid {
            state.last_seen.uid = uid;
        }
        let recent = (mail.flags().is_empty()) || (mail.flags()[0] == imap::types::Flag::Recent);
        if !recent {
            let flags = flags::imap_to_maildir(mail.flags());
            match maildir.store_cur_with_flags(body, &flags) {
                Ok(newid) => {
                    state.insert(uid, (newid, flags));
                }
                Err(e) => {
                    error!("{}: error saving new message: {}", mailbox, e);
                    state.save()?;
                    return Err(SyncError::MaildirError(e));
                }
            };
        } else {
            match maildir.store_new(body) {
                Ok(newid) => {
                    state.insert(uid, (newid, String::from("")));
                }
                Err(e) => {
                    error!("{}: error saving new message: {}", mailbox, e);
                    state.save()?;
                    return Err(SyncError::MaildirError(e));
                }
            };
        }
    }

    // All remote state has been applied locally
    state.last_seen.highest_mod_seq = new_highest_mod_seq;
    state.last_seen.uid_validity = new_uid_validity;

    // Upload local changes
    imap_apply_local_changes(session, mailbox, &mut state, &maildir)?;

    state.save()?;

    Ok(())
}

#[cfg(test)]
#[cfg(feature = "test-full")]
mod tests {
    extern crate imap;
    extern crate lettre;

    use super::*;
    use tempfile::tempdir;

    // For debugging tests, see comment below
    // use simplelog::*;

    const TO: &str = "vsync@localhost";

    fn test_host() -> String {
        std::env::var("TEST_HOST").unwrap_or("127.0.0.1".to_string())
    }

    fn test_imaps_port() -> u16 {
        std::env::var("TEST_IMAPS_PORT")
            .unwrap_or("3143".to_string())
            .parse()
            .unwrap_or(3143)
    }

    fn clean_mailbox<T: io::Read + io::Write>(session: &mut imap::Session<T>) {
        session.select("INBOX").unwrap();
        let inbox = session.search("ALL").unwrap();
        if !inbox.is_empty() {
            session.uid_store("1:*", "+FLAGS (\\Deleted)").unwrap();
        }
        session.expunge().unwrap();
    }

    fn session(user: &str) -> imap::Session<imap::Connection> {
        let host = test_host();
        let port = test_imaps_port();
        let mut c = imap::ClientBuilder::new(&host, port)
            .mode(imap::ConnectionMode::Plaintext)
            .connect()
            .unwrap();
        c.debug = true;
        let mut s = c.login(user, user).unwrap();
        s.debug = true;
        clean_mailbox(&mut s);
        s
    }

    #[test]
    fn sync_actions() {
        // Can be run against
        // `docker run -it --rm -p 3025:25 -p 3110:110 -p 3143:143 -p 3465:465 -p 3993:993 outoforder/cyrus-imapd-tester:latest`

        // This can be used to debug failing tests
        // TermLogger::init(
        //     LevelFilter::Trace,
        //     Config::default(),
        //     TerminalMode::Mixed,
        //     ColorChoice::Auto,
        // )
        // .expect("Failed to initialize logger");

        let dir = tempdir().unwrap();
        let tmp_path = dir.path().to_owned();
        assert!(tmp_path.exists());

        let maildir = maildir::Maildir::from(tmp_path);
        maildir.create_dirs().unwrap();

        let mut c = session(TO);
        c.run_command_and_read_response("ENABLE QRESYNC").unwrap();

        // make a message to append
        let e: lettre::Message = lettre::message::Message::builder()
            .from("sender@localhost".parse().unwrap())
            .to(TO.parse().unwrap())
            .subject("My second e-mail")
            .body("Hello world".to_string())
            .unwrap()
            .into();

        // append message
        let mbox = "INBOX";
        let mailbox = c.select(mbox).unwrap();
        c.append(mbox, &e.formatted()).finish().unwrap();

        let sync_job = mailboxes::SyncJob {
            action: mailboxes::SyncAction::CreateLocal,
            name: String::from(mbox),
            delimiter: ".".to_string(),
        };
        let state = state::SyncState::load(&maildir.path()).unwrap();
        pull(&mut c, &sync_job, maildir, mailbox, state).unwrap();

        let maildir = maildir::Maildir::from(dir.path().to_owned());
        let state = state::SyncState::load(&maildir.path()).unwrap();
        assert_eq!(maildir.count_new() + maildir.count_cur(), 1);

        // Compare local to remote
        let inbox = c.uid_search("ALL").unwrap();
        assert_eq!(inbox, state.uids().into_iter().collect());

        // delete email remotely
        clean_mailbox(&mut c);

        // the e-mail should be gone now
        let inbox = c.search("ALL").unwrap();
        assert_eq!(inbox.len(), 0);

        // A push must bring it back
        let sync_job = mailboxes::SyncJob {
            action: mailboxes::SyncAction::Sync,
            name: String::from(mbox),
            delimiter: ".".to_string(),
        };
        let mailbox = c.select(mbox).unwrap();
        push(&mut c, &sync_job, maildir, mailbox, state).unwrap();

        let maildir = maildir::Maildir::from(dir.path().to_owned());
        let state = state::SyncState::load(&maildir.path()).unwrap();
        assert_eq!(maildir.count_new(), 1);

        // Get the (restored) emails UID
        let inbox = c.uid_search("ALL").unwrap();
        assert_eq!(inbox, state.uids().into_iter().collect());

        // Store a new mail locally
        maildir.store_cur_with_flags(&e.formatted(), "S").unwrap();
        // and reload the state
        let state = state::SyncState::load(&maildir.path()).unwrap();
        assert_eq!(state.local_changes.added.len(), 1);
        // and add "another" one remotely
        c.append(mbox, &e.formatted()).finish().unwrap();

        // sync both ways
        let mailbox = c.select(mbox).unwrap();
        sync(&mut c, &sync_job, maildir, mailbox, state).unwrap();

        let maildir = maildir::Maildir::from(dir.path().to_owned());
        let state = state::SyncState::load(&maildir.path()).unwrap();
        assert_eq!(state.local_changes.added.len(), 0);
        assert_eq!(maildir.count_new() + maildir.count_cur(), 3);
        let inbox = c.uid_search("ALL").unwrap();
        assert_eq!(inbox, state.uids().into_iter().collect());
    }
}