forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmx.c
1870 lines (1640 loc) · 43.6 KB
/
mx.c
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
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
* Mailbox multiplexor
*
* @authors
* Copyright (C) 1996-2002,2010,2013 Michael R. Elkins <[email protected]>
* Copyright (C) 1999-2003 Thomas Roessler <[email protected]>
* Copyright (C) 2016-2018 Richard Russon <[email protected]>
* Copyright (C) 2019 Pietro Cerutti <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page neo_mx Mailbox multiplexor
*
* Mailbox multiplexor
*/
#include "config.h"
#include <errno.h>
#include <limits.h>
#include <locale.h>
#include <stdbool.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "alias/lib.h"
#include "mutt.h"
#include "mx.h"
#include "maildir/lib.h"
#include "mbox/lib.h"
#include "menu/lib.h"
#include "question/lib.h"
#include "commands.h"
#include "copy.h"
#include "hook.h"
#include "keymap.h"
#include "mutt_globals.h"
#include "mutt_header.h"
#include "mutt_logging.h"
#include "mutt_mailbox.h"
#include "muttlib.h"
#include "opcodes.h"
#include "options.h"
#include "protos.h"
#ifdef USE_COMP_MBOX
#include "compmbox/lib.h"
#endif
#ifdef USE_IMAP
#include "imap/lib.h"
#endif
#ifdef USE_POP
#include "pop/lib.h"
#endif
#ifdef USE_NNTP
#include "nntp/lib.h"
#include "nntp/adata.h" // IWYU pragma: keep
#include "nntp/mdata.h" // IWYU pragma: keep
#endif
#ifdef USE_NOTMUCH
#include "notmuch/lib.h"
#endif
#ifdef ENABLE_NLS
#include <libintl.h>
#endif
#ifdef __APPLE__
#include <xlocale.h>
#endif
static const struct Mapping MboxTypeMap[] = {
// clang-format off
{ "mbox", MUTT_MBOX, },
{ "MMDF", MUTT_MMDF, },
{ "MH", MUTT_MH, },
{ "Maildir", MUTT_MAILDIR, },
{ NULL, 0, },
// clang-format on
};
struct EnumDef MboxTypeDef = {
"mbox_type",
4,
(struct Mapping *) &MboxTypeMap,
};
/**
* MxOps - All the Mailbox backends
*/
const struct MxOps *MxOps[] = {
/* These mailboxes can be recognised by their Url scheme */
#ifdef USE_IMAP
&MxImapOps,
#endif
#ifdef USE_NOTMUCH
&MxNotmuchOps,
#endif
#ifdef USE_POP
&MxPopOps,
#endif
#ifdef USE_NNTP
&MxNntpOps,
#endif
/* Local mailboxes */
&MxMaildirOps,
&MxMboxOps,
&MxMhOps,
&MxMmdfOps,
/* If everything else fails... */
#ifdef USE_COMP_MBOX
&MxCompOps,
#endif
NULL,
};
/**
* mx_get_ops - Get mailbox operations
* @param type Mailbox type
* @retval ptr Mailbox function
* @retval NULL Error
*/
const struct MxOps *mx_get_ops(enum MailboxType type)
{
for (const struct MxOps **ops = MxOps; *ops; ops++)
if ((*ops)->type == type)
return *ops;
return NULL;
}
/**
* mutt_is_spool - Is this the spool_file?
* @param str Name to check
* @retval true It is the spool_file
*/
static bool mutt_is_spool(const char *str)
{
const char *const c_spool_file = cs_subset_string(NeoMutt->sub, "spool_file");
if (mutt_str_equal(str, c_spool_file))
return true;
struct Url *ua = url_parse(str);
struct Url *ub = url_parse(c_spool_file);
const bool is_spool =
ua && ub && (ua->scheme == ub->scheme) &&
mutt_istr_equal(ua->host, ub->host) && mutt_istr_equal(ua->path, ub->path) &&
(!ua->user || !ub->user || mutt_str_equal(ua->user, ub->user));
url_free(&ua);
url_free(&ub);
return is_spool;
}
/**
* mx_access - Wrapper for access, checks permissions on a given mailbox
* @param path Path of mailbox
* @param flags Flags, e.g. W_OK
* @retval 0 Success, allowed
* @retval <0 Failure, not allowed
*
* We may be interested in using ACL-style flags at some point, currently we
* use the normal access() flags.
*/
int mx_access(const char *path, int flags)
{
#ifdef USE_IMAP
if (imap_path_probe(path, NULL) == MUTT_IMAP)
return imap_access(path);
#endif
return access(path, flags);
}
/**
* mx_open_mailbox_append - Open a mailbox for appending
* @param m Mailbox
* @param flags Flags, see #OpenMailboxFlags
* @retval true Success
* @retval false Failure
*/
static bool mx_open_mailbox_append(struct Mailbox *m, OpenMailboxFlags flags)
{
if (!m)
return false;
struct stat st = { 0 };
m->append = true;
if ((m->type == MUTT_UNKNOWN) || (m->type == MUTT_MAILBOX_ERROR))
{
m->type = mx_path_probe(mailbox_path(m));
if (m->type == MUTT_UNKNOWN)
{
if (flags & (MUTT_APPEND | MUTT_NEWFOLDER))
{
m->type = MUTT_MAILBOX_ERROR;
}
else
{
mutt_error(_("%s is not a mailbox"), mailbox_path(m));
return false;
}
}
if (m->type == MUTT_MAILBOX_ERROR)
{
if (stat(mailbox_path(m), &st) == -1)
{
if (errno == ENOENT)
{
#ifdef USE_COMP_MBOX
if (mutt_comp_can_append(m))
m->type = MUTT_COMPRESSED;
else
#endif
m->type = cs_subset_enum(NeoMutt->sub, "mbox_type");
flags |= MUTT_APPENDNEW;
}
else
{
mutt_perror(mailbox_path(m));
return false;
}
}
else
return false;
}
m->mx_ops = mx_get_ops(m->type);
}
if (!m->mx_ops || !m->mx_ops->mbox_open_append)
return false;
const bool rc = m->mx_ops->mbox_open_append(m, flags);
m->opened++;
return rc;
}
/**
* mx_mbox_ac_link - Link a Mailbox to an existing or new Account
* @param m Mailbox to link
* @retval true Success
* @retval false Failure
*/
bool mx_mbox_ac_link(struct Mailbox *m)
{
if (!m)
return false;
if (m->account)
return true;
struct Account *a = mx_ac_find(m);
const bool new_account = !a;
if (new_account)
{
a = account_new(NULL, NeoMutt->sub);
a->type = m->type;
}
if (!mx_ac_add(a, m))
{
if (new_account)
{
account_free(&a);
}
return false;
}
if (new_account)
{
neomutt_account_add(NeoMutt, a);
}
return true;
}
/**
* mx_mbox_open - Open a mailbox and parse it
* @param m Mailbox to open
* @param flags Flags, see #OpenMailboxFlags
* @retval true Success
* @retval false Error
*/
bool mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
{
if (!m)
return false;
if ((m->type == MUTT_UNKNOWN) && (flags & (MUTT_NEWFOLDER | MUTT_APPEND)))
{
m->type = cs_subset_enum(NeoMutt->sub, "mbox_type");
m->mx_ops = mx_get_ops(m->type);
}
const bool newly_linked_account = !m->account;
if (newly_linked_account)
{
if (!mx_mbox_ac_link(m))
{
return false;
}
}
m->verbose = !(flags & MUTT_QUIET);
m->readonly = (flags & MUTT_READONLY);
m->peekonly = (flags & MUTT_PEEK);
if (flags & (MUTT_APPEND | MUTT_NEWFOLDER))
{
if (!mx_open_mailbox_append(m, flags))
{
goto error;
}
return true;
}
if (m->opened > 0)
{
m->opened++;
return true;
}
m->size = 0;
m->msg_unread = 0;
m->msg_flagged = 0;
m->rights = MUTT_ACL_ALL;
if (m->type == MUTT_UNKNOWN)
{
m->type = mx_path_probe(mailbox_path(m));
m->mx_ops = mx_get_ops(m->type);
}
if ((m->type == MUTT_UNKNOWN) || (m->type == MUTT_MAILBOX_ERROR) || !m->mx_ops)
{
if (m->type == MUTT_MAILBOX_ERROR)
mutt_perror(mailbox_path(m));
else if ((m->type == MUTT_UNKNOWN) || !m->mx_ops)
mutt_error(_("%s is not a mailbox"), mailbox_path(m));
goto error;
}
mutt_make_label_hash(m);
/* if the user has a 'push' command in their .neomuttrc, or in a folder-hook,
* it will cause the progress messages not to be displayed because
* mutt_refresh() will think we are in the middle of a macro. so set a
* flag to indicate that we should really refresh the screen. */
OptForceRefresh = true;
if (m->verbose)
mutt_message(_("Reading %s..."), mailbox_path(m));
// Clear out any existing emails
for (int i = 0; i < m->email_max; i++)
{
email_free(&m->emails[i]);
}
m->msg_count = 0;
m->msg_unread = 0;
m->msg_flagged = 0;
m->msg_new = 0;
m->msg_deleted = 0;
m->msg_tagged = 0;
m->vcount = 0;
enum MxOpenReturns rc = m->mx_ops->mbox_open(m);
m->opened++;
if ((rc == MX_OPEN_OK) || (rc == MX_OPEN_ABORT))
{
if ((flags & MUTT_NOSORT) == 0)
{
/* avoid unnecessary work since the mailbox is completely unthreaded
* to begin with */
OptSortSubthreads = false;
OptNeedRescore = false;
}
if (m->verbose)
mutt_clear_error();
if (rc == MX_OPEN_ABORT)
{
mutt_error(_("Reading from %s interrupted..."), mailbox_path(m));
}
}
else
{
goto error;
}
if (!m->peekonly)
m->has_new = false;
OptForceRefresh = false;
return true;
error:
mx_fastclose_mailbox(m, newly_linked_account);
if (newly_linked_account)
account_mailbox_remove(m->account, m);
return false;
}
/**
* mx_fastclose_mailbox - Free up memory associated with the Mailbox
* @param m Mailbox
* @param keep_account Make sure not to remove the mailbox's account
*/
void mx_fastclose_mailbox(struct Mailbox *m, bool keep_account)
{
if (!m)
return;
m->opened--;
if (m->opened != 0)
return;
/* never announce that a mailbox we've just left has new mail.
* TODO: really belongs in mx_mbox_close, but this is a nice hook point */
if (!m->peekonly)
mutt_mailbox_set_notified(m);
if (m->mx_ops)
m->mx_ops->mbox_close(m);
mutt_hash_free(&m->subj_hash);
mutt_hash_free(&m->id_hash);
mutt_hash_free(&m->label_hash);
if (m->emails)
{
for (int i = 0; i < m->msg_count; i++)
{
if (!m->emails[i])
break;
email_free(&m->emails[i]);
}
}
if (m->flags & MB_HIDDEN)
{
mx_ac_remove(m, keep_account);
}
}
/**
* sync_mailbox - Save changes to disk
* @param m Mailbox
* @retval enum #MxStatus
*/
static enum MxStatus sync_mailbox(struct Mailbox *m)
{
if (!m || !m->mx_ops || !m->mx_ops->mbox_sync)
return MX_STATUS_ERROR;
if (m->verbose)
{
/* L10N: Displayed before/as a mailbox is being synced */
mutt_message(_("Writing %s..."), mailbox_path(m));
}
enum MxStatus rc = m->mx_ops->mbox_sync(m);
if (rc != MX_STATUS_OK)
{
mutt_debug(LL_DEBUG2, "mbox_sync returned: %d\n", rc);
if ((rc == MX_STATUS_ERROR) && m->verbose)
{
/* L10N: Displayed if a mailbox sync fails */
mutt_error(_("Unable to write %s"), mailbox_path(m));
}
}
return rc;
}
/**
* trash_append - Move deleted mails to the trash folder
* @param m Mailbox
* @retval 0 Success
* @retval -1 Failure
*/
static int trash_append(struct Mailbox *m)
{
if (!m)
return -1;
struct stat st = { 0 };
struct stat stc = { 0 };
int rc;
const bool c_maildir_trash = cs_subset_bool(NeoMutt->sub, "maildir_trash");
const char *const c_trash = cs_subset_string(NeoMutt->sub, "trash");
if (!c_trash || (m->msg_deleted == 0) || ((m->type == MUTT_MAILDIR) && c_maildir_trash))
{
return 0;
}
int delmsgcount = 0;
int first_del = -1;
for (int i = 0; i < m->msg_count; i++)
{
struct Email *e = m->emails[i];
if (!e)
break;
if (e->deleted && !e->purge)
{
if (first_del < 0)
first_del = i;
delmsgcount++;
}
}
if (delmsgcount == 0)
return 0; /* nothing to be done */
/* avoid the "append messages" prompt */
const bool c_confirm_append = cs_subset_bool(NeoMutt->sub, "confirm_append");
cs_subset_str_native_set(NeoMutt->sub, "confirm_append", false, NULL);
rc = mutt_save_confirm(c_trash, &st);
cs_subset_str_native_set(NeoMutt->sub, "confirm_append", c_confirm_append, NULL);
if (rc != 0)
{
/* L10N: Although we know the precise number of messages, we do not show it to the user.
So feel free to use a "generic plural" as plural translation if your language has one. */
mutt_error(ngettext("message not deleted", "messages not deleted", delmsgcount));
return -1;
}
if ((lstat(mailbox_path(m), &stc) == 0) && (stc.st_ino == st.st_ino) &&
(stc.st_dev == st.st_dev) && (stc.st_rdev == st.st_rdev))
{
return 0; /* we are in the trash folder: simple sync */
}
#ifdef USE_IMAP
if ((m->type == MUTT_IMAP) && (imap_path_probe(c_trash, NULL) == MUTT_IMAP))
{
if (imap_fast_trash(m, c_trash) == 0)
return 0;
}
#endif
struct Mailbox *m_trash = mx_path_resolve(c_trash);
const bool old_append = m_trash->append;
if (!mx_mbox_open(m_trash, MUTT_APPEND))
{
mutt_error(_("Can't open trash folder"));
mailbox_free(&m_trash);
return -1;
}
/* continue from initial scan above */
for (int i = first_del; i < m->msg_count; i++)
{
struct Email *e = m->emails[i];
if (!e)
break;
if (e->deleted && !e->purge)
{
if (mutt_append_message(m_trash, m, e, NULL, MUTT_CM_NO_FLAGS, CH_NO_FLAGS) == -1)
{
mx_mbox_close(m_trash);
// L10N: Displayed if appending to $trash fails when syncing or closing a mailbox
mutt_error(_("Unable to append to trash folder"));
m_trash->append = old_append;
return -1;
}
}
}
mx_mbox_close(m_trash);
m_trash->append = old_append;
if (m_trash->flags == MB_HIDDEN)
mailbox_free(&m_trash);
return 0;
}
/**
* mx_mbox_close - Save changes and close mailbox
* @param m Mailbox
* @retval enum #MxStatus
*
* @note The flag retvals come from a call to a backend sync function
*
* @note It's very important to ensure the mailbox is properly closed before
* free'ing the context. For selected mailboxes, IMAP will cache the
* context inside connection->adata until imap_close_mailbox() removes
* it. Readonly, dontwrite, and append mailboxes are guaranteed to call
* mx_fastclose_mailbox(), so for most of NeoMutt's code you won't see
* return value checks for temporary contexts.
*/
enum MxStatus mx_mbox_close(struct Mailbox *m)
{
if (!m)
return MX_STATUS_ERROR;
const bool c_mail_check_recent =
cs_subset_bool(NeoMutt->sub, "mail_check_recent");
if (c_mail_check_recent && !m->peekonly)
m->has_new = false;
if (m->readonly || m->dontwrite || m->append || m->peekonly)
{
mx_fastclose_mailbox(m, false);
return 0;
}
int i, read_msgs = 0;
enum MxStatus rc = MX_STATUS_ERROR;
enum QuadOption move_messages = MUTT_NO;
enum QuadOption purge = MUTT_YES;
struct Buffer *mbox = NULL;
struct Buffer *buf = mutt_buffer_pool_get();
#ifdef USE_NNTP
if ((m->msg_unread != 0) && (m->type == MUTT_NNTP))
{
struct NntpMboxData *mdata = m->mdata;
if (mdata && mdata->adata && mdata->group)
{
const enum QuadOption c_catchup_newsgroup =
cs_subset_quad(NeoMutt->sub, "catchup_newsgroup");
enum QuadOption ans =
query_quadoption(c_catchup_newsgroup, _("Mark all articles read?"));
if (ans == MUTT_ABORT)
goto cleanup;
if (ans == MUTT_YES)
mutt_newsgroup_catchup(m, mdata->adata, mdata->group);
}
}
#endif
const bool c_keep_flagged = cs_subset_bool(NeoMutt->sub, "keep_flagged");
for (i = 0; i < m->msg_count; i++)
{
struct Email *e = m->emails[i];
if (!e)
break;
if (!e->deleted && e->read && !(e->flagged && c_keep_flagged))
read_msgs++;
}
#ifdef USE_NNTP
/* don't need to move articles from newsgroup */
if (m->type == MUTT_NNTP)
read_msgs = 0;
#endif
const enum QuadOption c_move = cs_subset_quad(NeoMutt->sub, "move");
if ((read_msgs != 0) && (c_move != MUTT_NO))
{
bool is_spool;
mbox = mutt_buffer_pool_get();
char *p = mutt_find_hook(MUTT_MBOX_HOOK, mailbox_path(m));
if (p)
{
is_spool = true;
mutt_buffer_strcpy(mbox, p);
}
else
{
const char *const c_mbox = cs_subset_string(NeoMutt->sub, "mbox");
mutt_buffer_strcpy(mbox, c_mbox);
is_spool = mutt_is_spool(mailbox_path(m)) &&
!mutt_is_spool(mutt_buffer_string(mbox));
}
if (is_spool && !mutt_buffer_is_empty(mbox))
{
mutt_buffer_expand_path(mbox);
mutt_buffer_printf(buf,
/* L10N: The first argument is the number of read messages to be
moved, the second argument is the target mailbox. */
ngettext("Move %d read message to %s?",
"Move %d read messages to %s?", read_msgs),
read_msgs, mutt_buffer_string(mbox));
move_messages = query_quadoption(c_move, mutt_buffer_string(buf));
if (move_messages == MUTT_ABORT)
goto cleanup;
}
}
/* There is no point in asking whether or not to purge if we are
* just marking messages as "trash". */
const bool c_maildir_trash = cs_subset_bool(NeoMutt->sub, "maildir_trash");
if ((m->msg_deleted != 0) && !((m->type == MUTT_MAILDIR) && c_maildir_trash))
{
mutt_buffer_printf(buf,
ngettext("Purge %d deleted message?",
"Purge %d deleted messages?", m->msg_deleted),
m->msg_deleted);
const enum QuadOption c_delete = cs_subset_quad(NeoMutt->sub, "delete");
purge = query_quadoption(c_delete, mutt_buffer_string(buf));
if (purge == MUTT_ABORT)
goto cleanup;
}
const bool c_mark_old = cs_subset_bool(NeoMutt->sub, "mark_old");
if (c_mark_old && !m->peekonly)
{
for (i = 0; i < m->msg_count; i++)
{
struct Email *e = m->emails[i];
if (!e)
break;
if (!e->deleted && !e->old && !e->read)
mutt_set_flag(m, e, MUTT_OLD, true);
}
}
if (move_messages)
{
if (m->verbose)
mutt_message(_("Moving read messages to %s..."), mutt_buffer_string(mbox));
#ifdef USE_IMAP
/* try to use server-side copy first */
i = 1;
if ((m->type == MUTT_IMAP) && (imap_path_probe(mutt_buffer_string(mbox), NULL) == MUTT_IMAP))
{
/* add messages for moving, and clear old tags, if any */
struct EmailList el = STAILQ_HEAD_INITIALIZER(el);
for (i = 0; i < m->msg_count; i++)
{
struct Email *e = m->emails[i];
if (!e)
break;
if (e->read && !e->deleted && !(e->flagged && c_keep_flagged))
{
e->tagged = true;
emaillist_add_email(&el, e);
}
else
e->tagged = false;
}
i = imap_copy_messages(m, &el, mutt_buffer_string(mbox), SAVE_MOVE);
emaillist_clear(&el);
}
if (i == 0) /* success */
mutt_clear_error();
else if (i == -1) /* horrible error, bail */
goto cleanup;
else /* use regular append-copy mode */
#endif
{
struct Mailbox *m_read = mx_path_resolve(mutt_buffer_string(mbox));
if (!mx_mbox_open(m_read, MUTT_APPEND))
{
mailbox_free(&m_read);
goto cleanup;
}
for (i = 0; i < m->msg_count; i++)
{
struct Email *e = m->emails[i];
if (!e)
break;
if (e->read && !e->deleted && !(e->flagged && c_keep_flagged))
{
if (mutt_append_message(m_read, m, e, NULL, MUTT_CM_NO_FLAGS, CH_UPDATE_LEN) == 0)
{
mutt_set_flag(m, e, MUTT_DELETE, true);
mutt_set_flag(m, e, MUTT_PURGE, true);
}
else
{
mx_mbox_close(m_read);
goto cleanup;
}
}
}
mx_mbox_close(m_read);
}
}
else if (!m->changed && (m->msg_deleted == 0))
{
if (m->verbose)
mutt_message(_("Mailbox is unchanged"));
if ((m->type == MUTT_MBOX) || (m->type == MUTT_MMDF))
mbox_reset_atime(m, NULL);
mx_fastclose_mailbox(m, false);
rc = MX_STATUS_OK;
goto cleanup;
}
/* copy mails to the trash before expunging */
const char *const c_trash = cs_subset_string(NeoMutt->sub, "trash");
const struct Mailbox *m_trash = mx_mbox_find(m->account, c_trash);
if (purge && (m->msg_deleted != 0) && (m != m_trash))
{
if (trash_append(m) != 0)
goto cleanup;
}
#ifdef USE_IMAP
/* allow IMAP to preserve the deleted flag across sessions */
if (m->type == MUTT_IMAP)
{
const enum MxStatus check = imap_sync_mailbox(m, (purge != MUTT_NO), true);
if (check == MX_STATUS_ERROR)
{
rc = check;
goto cleanup;
}
}
else
#endif
{
if (purge == MUTT_NO)
{
for (i = 0; i < m->msg_count; i++)
{
struct Email *e = m->emails[i];
if (!e)
break;
e->deleted = false;
e->purge = false;
}
m->msg_deleted = 0;
}
if (m->changed || (m->msg_deleted != 0))
{
enum MxStatus check = sync_mailbox(m);
if (check != MX_STATUS_OK)
{
rc = check;
goto cleanup;
}
}
}
if (m->verbose)
{
if (move_messages)
{
mutt_message(_("%d kept, %d moved, %d deleted"),
m->msg_count - m->msg_deleted, read_msgs, m->msg_deleted);
}
else
mutt_message(_("%d kept, %d deleted"), m->msg_count - m->msg_deleted, m->msg_deleted);
}
const bool c_save_empty = cs_subset_bool(NeoMutt->sub, "save_empty");
if ((m->msg_count == m->msg_deleted) &&
((m->type == MUTT_MMDF) || (m->type == MUTT_MBOX)) &&
!mutt_is_spool(mailbox_path(m)) && !c_save_empty)
{
mutt_file_unlink_empty(mailbox_path(m));
}
#ifdef USE_SIDEBAR
if ((purge == MUTT_YES) && (m->msg_deleted != 0))
{
for (i = 0; i < m->msg_count; i++)
{
struct Email *e = m->emails[i];
if (!e)
break;
if (e->deleted && !e->read)
{
m->msg_unread--;
if (!e->old)
m->msg_new--;
}
if (e->deleted && e->flagged)
m->msg_flagged--;
}
}
#endif
mx_fastclose_mailbox(m, false);
rc = MX_STATUS_OK;
cleanup:
mutt_buffer_pool_release(&mbox);
mutt_buffer_pool_release(&buf);
return rc;
}
/**
* mx_mbox_sync - Save changes to mailbox
* @param[in] m Mailbox
* @retval enum #MxStatus
*
* @note The flag retvals come from a call to a backend sync function
*/
enum MxStatus mx_mbox_sync(struct Mailbox *m)
{
if (!m)
return MX_STATUS_ERROR;
enum MxStatus rc = MX_STATUS_OK;
int purge = 1;
int msgcount, deleted;
if (m->dontwrite)
{
char buf[256], tmp[256];
if (km_expand_key(buf, sizeof(buf), km_find_func(MENU_MAIN, OP_TOGGLE_WRITE)))
snprintf(tmp, sizeof(tmp), _(" Press '%s' to toggle write"), buf);
else
mutt_str_copy(tmp, _("Use 'toggle-write' to re-enable write"), sizeof(tmp));
mutt_error(_("Mailbox is marked unwritable. %s"), tmp);
return MX_STATUS_ERROR;
}
else if (m->readonly)
{
mutt_error(_("Mailbox is read-only"));
return MX_STATUS_ERROR;
}
if (!m->changed && (m->msg_deleted == 0))
{
if (m->verbose)
mutt_message(_("Mailbox is unchanged"));
return MX_STATUS_OK;
}
if (m->msg_deleted != 0)
{
char buf[128];
snprintf(buf, sizeof(buf),
ngettext("Purge %d deleted message?", "Purge %d deleted messages?", m->msg_deleted),
m->msg_deleted);
const enum QuadOption c_delete = cs_subset_quad(NeoMutt->sub, "delete");
purge = query_quadoption(c_delete, buf);
if (purge == MUTT_ABORT)
return MX_STATUS_ERROR;
if (purge == MUTT_NO)
{
if (!m->changed)
return MX_STATUS_OK; /* nothing to do! */
/* let IMAP servers hold on to D flags */
if (m->type != MUTT_IMAP)
{
for (int i = 0; i < m->msg_count; i++)
{
struct Email *e = m->emails[i];
if (!e)
break;
e->deleted = false;
e->purge = false;
}
m->msg_deleted = 0;
}
}
mailbox_changed(m, NT_MAILBOX_UNTAG);
}
/* really only for IMAP - imap_sync_mailbox results in a call to
* ctx_update_tables, so m->msg_deleted is 0 when it comes back */
msgcount = m->msg_count;
deleted = m->msg_deleted;
const char *const c_trash = cs_subset_string(NeoMutt->sub, "trash");
const struct Mailbox *m_trash = mx_mbox_find(m->account, c_trash);
if (purge && (m->msg_deleted != 0) && (m != m_trash))
{
if (trash_append(m) != 0)
return MX_STATUS_OK;
}
#ifdef USE_IMAP