forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmx.c
1368 lines (1207 loc) · 31.4 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
* 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/>.
*/
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <utime.h>
#include "mutt.h"
#include "mx.h"
#include "address.h"
#include "body.h"
#include "buffy.h"
#include "context.h"
#include "copy.h"
#include "envelope.h"
#include "globals.h"
#include "header.h"
#include "keymap.h"
#include "lib/lib.h"
#include "mailbox.h"
#include "ncrypt/ncrypt.h"
#include "opcodes.h"
#include "options.h"
#include "pattern.h"
#include "protos.h"
#include "sort.h"
#include "thread.h"
#include "url.h"
#ifdef USE_SIDEBAR
#include "sidebar.h"
#endif
#ifdef USE_COMPRESSED
#include "compress.h"
#endif
#ifdef USE_IMAP
#include "imap/imap.h"
#endif
#ifdef USE_POP
#include "pop.h"
#endif
#ifdef USE_NNTP
#include "nntp.h"
#endif
#ifdef USE_NOTMUCH
#include "mutt_notmuch.h"
#endif
struct MxOps *mx_get_ops(int magic)
{
switch (magic)
{
#ifdef USE_IMAP
case MUTT_IMAP:
return &mx_imap_ops;
#endif
case MUTT_MAILDIR:
return &mx_maildir_ops;
case MUTT_MBOX:
return &mx_mbox_ops;
case MUTT_MH:
return &mx_mh_ops;
case MUTT_MMDF:
return &mx_mmdf_ops;
#ifdef USE_POP
case MUTT_POP:
return &mx_pop_ops;
#endif
#ifdef USE_COMPRESSED
case MUTT_COMPRESSED:
return &mx_comp_ops;
#endif
#ifdef USE_NNTP
case MUTT_NNTP:
return &mx_nntp_ops;
#endif
#ifdef USE_NOTMUCH
case MUTT_NOTMUCH:
return &mx_notmuch_ops;
#endif
default:
return NULL;
}
}
#define mutt_is_spool(s) (mutt_strcmp(SpoolFile, s) == 0)
#ifdef USE_IMAP
/**
* mx_is_imap - Is this an IMAP mailbox
* @param p Mailbox string to test
* return boolean
*/
bool mx_is_imap(const char *p)
{
enum UrlScheme scheme;
if (!p)
return false;
if (*p == '{')
return true;
scheme = url_check_scheme(p);
if (scheme == U_IMAP || scheme == U_IMAPS)
return true;
return false;
}
#endif
#ifdef USE_POP
/**
* mx_is_pop - Is this a POP mailbox
* @param p Mailbox string to test
* return boolean
*/
bool mx_is_pop(const char *p)
{
enum UrlScheme scheme;
if (!p)
return false;
scheme = url_check_scheme(p);
if (scheme == U_POP || scheme == U_POPS)
return true;
return false;
}
#endif
#ifdef USE_NNTP
/**
* mx_is_nntp - Is this an NNTP mailbox
* @param p Mailbox string to test
* return boolean
*/
bool mx_is_nntp(const char *p)
{
enum UrlScheme scheme;
if (!p)
return false;
scheme = url_check_scheme(p);
if (scheme == U_NNTP || scheme == U_NNTPS)
return true;
return false;
}
#endif
#ifdef USE_NOTMUCH
/**
* mx_is_notmuch - Is this a notmuch mailbox
* @param p Mailbox string to test
* return boolean
*/
bool mx_is_notmuch(const char *p)
{
enum UrlScheme scheme;
if (!p)
return false;
scheme = url_check_scheme(p);
if (scheme == U_NOTMUCH)
return true;
return false;
}
#endif
/**
* mx_get_magic - Identify the type of mailbox
* @param path Mailbox path to test
* return
* * -1 Error, can't identify mailbox
* * >0 Success, e.g. #MUTT_IMAP
*/
int mx_get_magic(const char *path)
{
struct stat st;
int magic = 0;
char tmp[_POSIX_PATH_MAX];
FILE *f = NULL;
#ifdef USE_IMAP
if (mx_is_imap(path))
return MUTT_IMAP;
#endif /* USE_IMAP */
#ifdef USE_POP
if (mx_is_pop(path))
return MUTT_POP;
#endif /* USE_POP */
#ifdef USE_NNTP
if (mx_is_nntp(path))
return MUTT_NNTP;
#endif /* USE_NNTP */
#ifdef USE_NOTMUCH
if (mx_is_notmuch(path))
return MUTT_NOTMUCH;
#endif
if (stat(path, &st) == -1)
{
mutt_debug(1, "mx_get_magic(): unable to stat %s: %s (errno %d).\n", path,
strerror(errno), errno);
return -1;
}
if (S_ISDIR(st.st_mode))
{
/* check for maildir-style mailbox */
if (mx_is_maildir(path))
return MUTT_MAILDIR;
/* check for mh-style mailbox */
if (mx_is_mh(path))
return MUTT_MH;
}
else if (st.st_size == 0)
{
/* hard to tell what zero-length files are, so assume the default magic */
if (DefaultMagic == MUTT_MBOX || DefaultMagic == MUTT_MMDF)
return DefaultMagic;
else
return MUTT_MBOX;
}
else if ((f = fopen(path, "r")) != NULL)
{
struct utimbuf times;
int ch;
/* Some mailbox creation tools erroneously append a blank line to
* a file before appending a mail message. This allows mutt to
* detect magic for and thus open those files. */
while ((ch = fgetc(f)) != EOF)
{
if (ch != '\n' && ch != '\r')
{
ungetc(ch, f);
break;
}
}
if (fgets(tmp, sizeof(tmp), f))
{
if (mutt_strncmp("From ", tmp, 5) == 0)
magic = MUTT_MBOX;
else if (mutt_strcmp(MMDF_SEP, tmp) == 0)
magic = MUTT_MMDF;
}
safe_fclose(&f);
if (!option(OPT_CHECK_MBOX_SIZE))
{
/* need to restore the times here, the file was not really accessed,
* only the type was accessed. This is important, because detection
* of "new mail" depends on those times set correctly.
*/
times.actime = st.st_atime;
times.modtime = st.st_mtime;
utime(path, ×);
}
}
else
{
mutt_debug(1, "mx_get_magic(): unable to open file %s for reading.\n", path);
return -1;
}
#ifdef USE_COMPRESSED
/* If there are no other matches, see if there are any
* compress hooks that match */
if ((magic == 0) && mutt_comp_can_read(path))
return MUTT_COMPRESSED;
#endif
return magic;
}
/**
* mx_set_magic - set DefaultMagic to the given value
*/
int mx_set_magic(const char *s)
{
if (mutt_strcasecmp(s, "mbox") == 0)
DefaultMagic = MUTT_MBOX;
else if (mutt_strcasecmp(s, "mmdf") == 0)
DefaultMagic = MUTT_MMDF;
else if (mutt_strcasecmp(s, "mh") == 0)
DefaultMagic = MUTT_MH;
else if (mutt_strcasecmp(s, "maildir") == 0)
DefaultMagic = MUTT_MAILDIR;
else
return -1;
return 0;
}
/**
* mx_access - Wrapper for access, checks permissions on a given mailbox
*
* 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 (mx_is_imap(path))
return imap_access(path);
#endif
return access(path, flags);
}
static int mx_open_mailbox_append(struct Context *ctx, int flags)
{
struct stat sb;
ctx->append = true;
ctx->magic = mx_get_magic(ctx->path);
if (ctx->magic == 0)
{
mutt_error(_("%s is not a mailbox."), ctx->path);
return -1;
}
if (ctx->magic < 0)
{
if (stat(ctx->path, &sb) == -1)
{
if (errno == ENOENT)
{
#ifdef USE_COMPRESSED
if (mutt_comp_can_append(ctx))
ctx->magic = MUTT_COMPRESSED;
else
#endif
ctx->magic = DefaultMagic;
flags |= MUTT_APPENDNEW;
}
else
{
mutt_perror(ctx->path);
return -1;
}
}
else
return -1;
}
ctx->mx_ops = mx_get_ops(ctx->magic);
if (!ctx->mx_ops || !ctx->mx_ops->open_append)
return -1;
return ctx->mx_ops->open_append(ctx, flags);
}
/**
* mx_open_mailbox - Open a mailbox and parse it
* @param path Path to the mailbox
* @param flags See below
* @param pctx Reuse this Context (if supplied)
*
* flags:
* * #MUTT_NOSORT do not sort mailbox
* * #MUTT_APPEND open mailbox for appending
* * #MUTT_READONLY open mailbox in read-only mode
* * #MUTT_QUIET only print error messages
* * #MUTT_PEEK revert atime where applicable
*/
struct Context *mx_open_mailbox(const char *path, int flags, struct Context *pctx)
{
struct Context *ctx = pctx;
int rc;
if (!path || !path[0])
return NULL;
if (!ctx)
ctx = safe_malloc(sizeof(struct Context));
memset(ctx, 0, sizeof(struct Context));
ctx->path = safe_strdup(path);
if (!ctx->path)
{
if (!pctx)
FREE(&ctx);
return NULL;
}
if (!(ctx->realpath = realpath(ctx->path, NULL)))
ctx->realpath = safe_strdup(ctx->path);
ctx->msgnotreadyet = -1;
ctx->collapsed = false;
for (rc = 0; rc < RIGHTSMAX; rc++)
mutt_bit_set(ctx->rights, rc);
if (flags & MUTT_QUIET)
ctx->quiet = true;
if (flags & MUTT_READONLY)
ctx->readonly = true;
if (flags & MUTT_PEEK)
ctx->peekonly = true;
if (flags & (MUTT_APPEND | MUTT_NEWFOLDER))
{
if (mx_open_mailbox_append(ctx, flags) != 0)
{
mx_fastclose_mailbox(ctx);
if (!pctx)
FREE(&ctx);
return NULL;
}
return ctx;
}
ctx->magic = mx_get_magic(path);
ctx->mx_ops = mx_get_ops(ctx->magic);
if (ctx->magic <= 0 || !ctx->mx_ops)
{
if (ctx->magic == -1)
mutt_perror(path);
else if (ctx->magic == 0 || !ctx->mx_ops)
mutt_error(_("%s is not a mailbox."), path);
mx_fastclose_mailbox(ctx);
if (!pctx)
FREE(&ctx);
return NULL;
}
mutt_make_label_hash(ctx);
/* if the user has a `push' command in their .muttrc, 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.
*/
set_option(OPT_FORCE_REFRESH);
if (!ctx->quiet)
mutt_message(_("Reading %s..."), ctx->path);
rc = ctx->mx_ops->open(ctx);
if ((rc == 0) || (rc == -2))
{
if ((flags & MUTT_NOSORT) == 0)
{
/* avoid unnecessary work since the mailbox is completely unthreaded
to begin with */
unset_option(OPT_SORT_SUBTHREADS);
unset_option(OPT_NEED_RESCORE);
mutt_sort_headers(ctx, 1);
}
if (!ctx->quiet)
mutt_clear_error();
if (rc == -2)
mutt_error(_("Reading from %s interrupted..."), ctx->path);
}
else
{
mx_fastclose_mailbox(ctx);
if (!pctx)
FREE(&ctx);
}
unset_option(OPT_FORCE_REFRESH);
return ctx;
}
/**
* mx_fastclose_mailbox - free up memory associated with the mailbox context
*/
void mx_fastclose_mailbox(struct Context *ctx)
{
struct utimbuf ut;
if (!ctx)
return;
/* fix up the times so buffy won't get confused */
if (ctx->peekonly && ctx->path && (ctx->mtime > ctx->atime))
{
ut.actime = ctx->atime;
ut.modtime = ctx->mtime;
utime(ctx->path, &ut);
}
/* never announce that a mailbox we've just left has new mail. #3290
* XXX: really belongs in mx_close_mailbox, but this is a nice hook point */
if (!ctx->peekonly)
mutt_buffy_setnotified(ctx->path);
if (ctx->mx_ops)
ctx->mx_ops->close(ctx);
if (ctx->subj_hash)
hash_destroy(&ctx->subj_hash, NULL);
if (ctx->id_hash)
hash_destroy(&ctx->id_hash, NULL);
hash_destroy(&ctx->label_hash, NULL);
mutt_clear_threads(ctx);
for (int i = 0; i < ctx->msgcount; i++)
mutt_free_header(&ctx->hdrs[i]);
FREE(&ctx->hdrs);
FREE(&ctx->v2r);
FREE(&ctx->path);
FREE(&ctx->realpath);
FREE(&ctx->pattern);
if (ctx->limit_pattern)
mutt_pattern_free(&ctx->limit_pattern);
safe_fclose(&ctx->fp);
memset(ctx, 0, sizeof(struct Context));
}
/**
* sync_mailbox - save changes to disk
*/
static int sync_mailbox(struct Context *ctx, int *index_hint)
{
if (!ctx->mx_ops || !ctx->mx_ops->sync)
return -1;
if (!ctx->quiet)
mutt_message(_("Writing %s..."), ctx->path);
return ctx->mx_ops->sync(ctx, index_hint);
}
/**
* trash_append - move deleted mails to the trash folder
*/
static int trash_append(struct Context *ctx)
{
struct Context ctx_trash;
int i;
struct stat st, stc;
int opt_confappend, rc;
if (!TrashPath || !ctx->deleted || (ctx->magic == MUTT_MAILDIR && option(OPT_MAILDIR_TRASH)))
return 0;
for (i = 0; i < ctx->msgcount; i++)
if (ctx->hdrs[i]->deleted && (!ctx->hdrs[i]->purge))
break;
if (i == ctx->msgcount)
return 0; /* nothing to be done */
/* avoid the "append messages" prompt */
opt_confappend = option(OPT_CONFIRM_APPEND);
if (opt_confappend)
unset_option(OPT_CONFIRM_APPEND);
rc = mutt_save_confirm(TrashPath, &st);
if (opt_confappend)
set_option(OPT_CONFIRM_APPEND);
if (rc != 0)
{
mutt_error(_("message(s) not deleted"));
return -1;
}
if (lstat(ctx->path, &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 (Context->magic == MUTT_IMAP && mx_is_imap(TrashPath))
{
if (!imap_fast_trash(Context, TrashPath))
return 0;
}
#endif
if (mx_open_mailbox(TrashPath, MUTT_APPEND, &ctx_trash) != NULL)
{
/* continue from initial scan above */
for (; i < ctx->msgcount; i++)
if (ctx->hdrs[i]->deleted && (!ctx->hdrs[i]->purge))
{
if (mutt_append_message(&ctx_trash, ctx, ctx->hdrs[i], 0, 0) == -1)
{
mx_close_mailbox(&ctx_trash, NULL);
return -1;
}
}
mx_close_mailbox(&ctx_trash, NULL);
}
else
{
mutt_error(_("Can't open trash folder"));
return -1;
}
return 0;
}
/**
* mx_close_mailbox - save changes and close mailbox
*/
int mx_close_mailbox(struct Context *ctx, int *index_hint)
{
int i, move_messages = 0, purge = 1, read_msgs = 0;
int check;
int isSpool = 0;
struct Context f;
char mbox[_POSIX_PATH_MAX];
char buf[SHORT_STRING];
if (!ctx)
return 0;
ctx->closing = true;
if (ctx->readonly || ctx->dontwrite || ctx->append)
{
mx_fastclose_mailbox(ctx);
return 0;
}
#ifdef USE_NNTP
if (ctx->unread && ctx->magic == MUTT_NNTP)
{
struct NntpData *nntp_data = ctx->data;
if (nntp_data && nntp_data->nserv && nntp_data->group)
{
int rc = query_quadoption(OPT_CATCHUP, _("Mark all articles read?"));
if (rc == MUTT_ABORT)
{
ctx->closing = false;
return -1;
}
else if (rc == MUTT_YES)
mutt_newsgroup_catchup(nntp_data->nserv, nntp_data->group);
}
}
#endif
for (i = 0; i < ctx->msgcount; i++)
{
if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read &&
!(ctx->hdrs[i]->flagged && option(OPT_KEEP_FLAGGED)))
read_msgs++;
}
#ifdef USE_NNTP
/* don't need to move articles from newsgroup */
if (ctx->magic == MUTT_NNTP)
read_msgs = 0;
#endif
if (read_msgs && quadoption(OPT_MOVE) != MUTT_NO)
{
char *p = NULL;
if ((p = mutt_find_hook(MUTT_MBOXHOOK, ctx->path)))
{
isSpool = 1;
strfcpy(mbox, p, sizeof(mbox));
}
else
{
strfcpy(mbox, NONULL(Inbox), sizeof(mbox));
isSpool = mutt_is_spool(ctx->path) && !mutt_is_spool(mbox);
}
if (isSpool && *mbox)
{
mutt_expand_path(mbox, sizeof(mbox));
snprintf(buf, sizeof(buf), _("Move read messages to %s?"), mbox);
if ((move_messages = query_quadoption(OPT_MOVE, buf)) == MUTT_ABORT)
{
ctx->closing = false;
return -1;
}
}
}
/*
* There is no point in asking whether or not to purge if we are
* just marking messages as "trash".
*/
if (ctx->deleted && !(ctx->magic == MUTT_MAILDIR && option(OPT_MAILDIR_TRASH)))
{
snprintf(buf, sizeof(buf),
ctx->deleted == 1 ? _("Purge %d deleted message?") :
_("Purge %d deleted messages?"),
ctx->deleted);
if ((purge = query_quadoption(OPT_DELETE, buf)) == MUTT_ABORT)
{
ctx->closing = false;
return -1;
}
}
if (option(OPT_MARK_OLD))
{
for (i = 0; i < ctx->msgcount; i++)
{
if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->old && !ctx->hdrs[i]->read)
mutt_set_flag(ctx, ctx->hdrs[i], MUTT_OLD, 1);
}
}
if (move_messages)
{
if (!ctx->quiet)
mutt_message(_("Moving read messages to %s..."), mbox);
#ifdef USE_IMAP
/* try to use server-side copy first */
i = 1;
if (ctx->magic == MUTT_IMAP && mx_is_imap(mbox))
{
/* tag messages for moving, and clear old tags, if any */
for (i = 0; i < ctx->msgcount; i++)
if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted &&
!(ctx->hdrs[i]->flagged && option(OPT_KEEP_FLAGGED)))
ctx->hdrs[i]->tagged = true;
else
ctx->hdrs[i]->tagged = false;
i = imap_copy_messages(ctx, NULL, mbox, 1);
}
if (i == 0) /* success */
mutt_clear_error();
else if (i == -1) /* horrible error, bail */
{
ctx->closing = false;
return -1;
}
else /* use regular append-copy mode */
#endif
{
if (mx_open_mailbox(mbox, MUTT_APPEND, &f) == NULL)
{
ctx->closing = false;
return -1;
}
for (i = 0; i < ctx->msgcount; i++)
{
if (ctx->hdrs[i]->read && !ctx->hdrs[i]->deleted &&
!(ctx->hdrs[i]->flagged && option(OPT_KEEP_FLAGGED)))
{
if (mutt_append_message(&f, ctx, ctx->hdrs[i], 0, CH_UPDATE_LEN) == 0)
{
mutt_set_flag(ctx, ctx->hdrs[i], MUTT_DELETE, 1);
mutt_set_flag(ctx, ctx->hdrs[i], MUTT_PURGE, 1);
}
else
{
mx_close_mailbox(&f, NULL);
ctx->closing = false;
return -1;
}
}
}
mx_close_mailbox(&f, NULL);
}
}
else if (!ctx->changed && ctx->deleted == 0)
{
if (!ctx->quiet)
mutt_message(_("Mailbox is unchanged."));
if (ctx->magic == MUTT_MBOX || ctx->magic == MUTT_MMDF)
mbox_reset_atime(ctx, NULL);
mx_fastclose_mailbox(ctx);
return 0;
}
/* copy mails to the trash before expunging */
if (purge && ctx->deleted && (mutt_strcmp(ctx->path, TrashPath) != 0))
{
if (trash_append(ctx) != 0)
{
ctx->closing = false;
return -1;
}
}
#ifdef USE_IMAP
/* allow IMAP to preserve the deleted flag across sessions */
if (ctx->magic == MUTT_IMAP)
{
if ((check = imap_sync_mailbox(ctx, purge)) != 0)
{
ctx->closing = false;
return check;
}
}
else
#endif
{
if (!purge)
{
for (i = 0; i < ctx->msgcount; i++)
{
ctx->hdrs[i]->deleted = false;
ctx->hdrs[i]->purge = false;
}
ctx->deleted = 0;
}
if (ctx->changed || ctx->deleted)
{
if ((check = sync_mailbox(ctx, index_hint)) != 0)
{
ctx->closing = false;
return check;
}
}
}
if (!ctx->quiet)
{
if (move_messages)
mutt_message(_("%d kept, %d moved, %d deleted."),
ctx->msgcount - ctx->deleted, read_msgs, ctx->deleted);
else
mutt_message(_("%d kept, %d deleted."), ctx->msgcount - ctx->deleted, ctx->deleted);
}
if (ctx->msgcount == ctx->deleted && (ctx->magic == MUTT_MMDF || ctx->magic == MUTT_MBOX) &&
!mutt_is_spool(ctx->path) && !option(OPT_SAVE_EMPTY))
mutt_unlink_empty(ctx->path);
#ifdef USE_SIDEBAR
if (purge && ctx->deleted)
{
int orig_msgcount = ctx->msgcount;
for (i = 0; i < ctx->msgcount; i++)
{
if (ctx->hdrs[i]->deleted && !ctx->hdrs[i]->read)
ctx->unread--;
if (ctx->hdrs[i]->deleted && ctx->hdrs[i]->flagged)
ctx->flagged--;
}
ctx->msgcount -= ctx->deleted;
mutt_sb_set_buffystats(ctx);
ctx->msgcount = orig_msgcount;
}
#endif
mx_fastclose_mailbox(ctx);
return 0;
}
/**
* mx_update_tables - Update a Context structure's internal tables
*/
void mx_update_tables(struct Context *ctx, int committing)
{
int i, j;
/* update memory to reflect the new state of the mailbox */
ctx->vcount = 0;
ctx->vsize = 0;
ctx->tagged = 0;
ctx->deleted = 0;
ctx->new = 0;
ctx->unread = 0;
ctx->changed = false;
ctx->flagged = 0;
#define this_body ctx->hdrs[j]->content
for (i = 0, j = 0; i < ctx->msgcount; i++)
{
if (!ctx->hdrs[i]->quasi_deleted &&
((committing && (!ctx->hdrs[i]->deleted ||
(ctx->magic == MUTT_MAILDIR && option(OPT_MAILDIR_TRASH)))) ||
(!committing && ctx->hdrs[i]->active)))
{
if (i != j)
{
ctx->hdrs[j] = ctx->hdrs[i];
ctx->hdrs[i] = NULL;
}
ctx->hdrs[j]->msgno = j;
if (ctx->hdrs[j]->virtual != -1)
{
ctx->v2r[ctx->vcount] = j;
ctx->hdrs[j]->virtual = ctx->vcount++;
ctx->vsize += this_body->length + this_body->offset - this_body->hdr_offset;
}
if (committing)
ctx->hdrs[j]->changed = false;
else if (ctx->hdrs[j]->changed)
ctx->changed = true;
if (!committing || (ctx->magic == MUTT_MAILDIR && option(OPT_MAILDIR_TRASH)))
{
if (ctx->hdrs[j]->deleted)
ctx->deleted++;
}
if (ctx->hdrs[j]->tagged)
ctx->tagged++;
if (ctx->hdrs[j]->flagged)
ctx->flagged++;
if (!ctx->hdrs[j]->read)
{
ctx->unread++;
if (!ctx->hdrs[j]->old)
ctx->new++;
}
j++;
}
else
{
if (ctx->magic == MUTT_MH || ctx->magic == MUTT_MAILDIR)
ctx->size -= (ctx->hdrs[i]->content->length + ctx->hdrs[i]->content->offset -
ctx->hdrs[i]->content->hdr_offset);
/* remove message from the hash tables */
if (ctx->subj_hash && ctx->hdrs[i]->env->real_subj)
hash_delete(ctx->subj_hash, ctx->hdrs[i]->env->real_subj, ctx->hdrs[i], NULL);
if (ctx->id_hash && ctx->hdrs[i]->env->message_id)
hash_delete(ctx->id_hash, ctx->hdrs[i]->env->message_id, ctx->hdrs[i], NULL);
mutt_label_hash_remove(ctx, ctx->hdrs[i]);
/* The path mx_check_mailbox() -> imap_check_mailbox() ->
* imap_expunge_mailbox() -> mx_update_tables()
* can occur before a call to mx_sync_mailbox(), resulting in
* last_tag being stale if it's not reset here.
*/
if (ctx->last_tag == ctx->hdrs[i])
ctx->last_tag = NULL;
mutt_free_header(&ctx->hdrs[i]);
}
}
#undef this_body
ctx->msgcount = j;
}
/**
* mx_sync_mailbox - Save changes to mailbox
* @param[in] ctx Context
* @param[out] index_hint Currently selected mailbox
* @retval 0 on success
* @retval -1 on error
*/
int mx_sync_mailbox(struct Context *ctx, int *index_hint)
{
int rc, i;
int purge = 1;
int msgcount, deleted;
if (ctx->dontwrite)
{
char buf[STRING], tmp[STRING];
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
strfcpy(tmp, _("Use 'toggle-write' to re-enable write!"), sizeof(tmp));
mutt_error(_("Mailbox is marked unwritable. %s"), tmp);
return -1;