forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.c
2168 lines (1945 loc) · 68.8 KB
/
functions.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
* Compose functions
*
* @authors
* Copyright (C) 2021 Pietro Cerutti <[email protected]>
* Copyright (C) 2021-2023 Richard Russon <[email protected]>
* Copyright (C) 2022 David Purton <[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 compose_functions Compose functions
*
* Compose functions
*/
#include "config.h"
#ifdef _MAKEDOC
#include "docs/makedoc_defs.h"
#else
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "private.h"
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "conn/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "lib.h"
#include "attach/lib.h"
#include "browser/lib.h"
#include "editor/lib.h"
#include "history/lib.h"
#include "imap/lib.h"
#include "index/lib.h"
#include "key/lib.h"
#include "menu/lib.h"
#include "ncrypt/lib.h"
#include "nntp/lib.h"
#include "pop/lib.h"
#include "question/lib.h"
#include "send/lib.h"
#include "attach_data.h"
#include "external.h"
#include "functions.h"
#include "globals.h"
#include "hook.h"
#include "mutt_header.h"
#include "mutt_logging.h"
#include "muttlib.h"
#include "mview.h"
#include "mx.h"
#include "nntp/adata.h" // IWYU pragma: keep
#include "protos.h"
#include "rfc3676.h"
#include "shared_data.h"
#ifdef ENABLE_NLS
#include <libintl.h>
#endif
#ifdef MIXMASTER
#include "mixmaster/lib.h"
#endif
#endif
// clang-format off
/**
* OpCompose - Functions for the Compose Menu
*/
const struct MenuFuncOp OpCompose[] = { /* map: compose */
{ "attach-file", OP_ATTACHMENT_ATTACH_FILE },
{ "attach-key", OP_ATTACHMENT_ATTACH_KEY },
{ "attach-message", OP_ATTACHMENT_ATTACH_MESSAGE },
{ "attach-news-message", OP_ATTACHMENT_ATTACH_NEWS_MESSAGE },
#ifdef USE_AUTOCRYPT
{ "autocrypt-menu", OP_COMPOSE_AUTOCRYPT_MENU },
#endif
{ "copy-file", OP_ATTACHMENT_SAVE },
{ "detach-file", OP_ATTACHMENT_DETACH },
{ "display-toggle-weed", OP_DISPLAY_HEADERS },
{ "edit-bcc", OP_ENVELOPE_EDIT_BCC },
{ "edit-cc", OP_ENVELOPE_EDIT_CC },
{ "edit-content-id", OP_ATTACHMENT_EDIT_CONTENT_ID },
{ "edit-description", OP_ATTACHMENT_EDIT_DESCRIPTION },
{ "edit-encoding", OP_ATTACHMENT_EDIT_ENCODING },
{ "edit-fcc", OP_ENVELOPE_EDIT_FCC },
{ "edit-file", OP_COMPOSE_EDIT_FILE },
{ "edit-followup-to", OP_ENVELOPE_EDIT_FOLLOWUP_TO },
{ "edit-from", OP_ENVELOPE_EDIT_FROM },
{ "edit-headers", OP_ENVELOPE_EDIT_HEADERS },
{ "edit-language", OP_ATTACHMENT_EDIT_LANGUAGE },
{ "edit-message", OP_COMPOSE_EDIT_MESSAGE },
{ "edit-mime", OP_ATTACHMENT_EDIT_MIME },
{ "edit-newsgroups", OP_ENVELOPE_EDIT_NEWSGROUPS },
{ "edit-reply-to", OP_ENVELOPE_EDIT_REPLY_TO },
{ "edit-subject", OP_ENVELOPE_EDIT_SUBJECT },
{ "edit-to", OP_ENVELOPE_EDIT_TO },
{ "edit-type", OP_ATTACHMENT_EDIT_TYPE },
{ "edit-x-comment-to", OP_ENVELOPE_EDIT_X_COMMENT_TO },
{ "exit", OP_EXIT },
{ "filter-entry", OP_ATTACHMENT_FILTER },
{ "forget-passphrase", OP_FORGET_PASSPHRASE },
{ "get-attachment", OP_ATTACHMENT_GET_ATTACHMENT },
{ "group-alternatives", OP_ATTACHMENT_GROUP_ALTS },
{ "group-multilingual", OP_ATTACHMENT_GROUP_LINGUAL },
{ "group-related", OP_ATTACHMENT_GROUP_RELATED },
{ "ispell", OP_COMPOSE_ISPELL },
#ifdef MIXMASTER
{ "mix", OP_COMPOSE_MIX },
#endif
{ "move-down", OP_ATTACHMENT_MOVE_DOWN },
{ "move-up", OP_ATTACHMENT_MOVE_UP },
{ "new-mime", OP_ATTACHMENT_NEW_MIME },
{ "pgp-menu", OP_COMPOSE_PGP_MENU },
{ "pipe-entry", OP_PIPE },
{ "pipe-message", OP_PIPE },
{ "postpone-message", OP_COMPOSE_POSTPONE_MESSAGE },
{ "print-entry", OP_ATTACHMENT_PRINT },
{ "rename-attachment", OP_ATTACHMENT_RENAME_ATTACHMENT },
{ "rename-file", OP_COMPOSE_RENAME_FILE },
{ "send-message", OP_COMPOSE_SEND_MESSAGE },
{ "smime-menu", OP_COMPOSE_SMIME_MENU },
{ "toggle-disposition", OP_ATTACHMENT_TOGGLE_DISPOSITION },
{ "toggle-recode", OP_ATTACHMENT_TOGGLE_RECODE },
{ "toggle-unlink", OP_ATTACHMENT_TOGGLE_UNLINK },
{ "ungroup-attachment", OP_ATTACHMENT_UNGROUP },
{ "update-encoding", OP_ATTACHMENT_UPDATE_ENCODING },
{ "view-attach", OP_ATTACHMENT_VIEW },
{ "view-mailcap", OP_ATTACHMENT_VIEW_MAILCAP },
{ "view-pager", OP_ATTACHMENT_VIEW_PAGER },
{ "view-text", OP_ATTACHMENT_VIEW_TEXT },
{ "write-fcc", OP_COMPOSE_WRITE_MESSAGE },
{ NULL, 0 },
};
/**
* ComposeDefaultBindings - Key bindings for the Compose Menu
*/
const struct MenuOpSeq ComposeDefaultBindings[] = { /* map: compose */
{ OP_ATTACHMENT_ATTACH_FILE, "a" },
{ OP_ATTACHMENT_ATTACH_KEY, "\033k" }, // <Alt-k>
{ OP_ATTACHMENT_ATTACH_MESSAGE, "A" },
{ OP_ATTACHMENT_DETACH, "D" },
{ OP_ATTACHMENT_EDIT_CONTENT_ID, "\033i" }, // <Alt-i>
{ OP_ATTACHMENT_EDIT_DESCRIPTION, "d" },
{ OP_ATTACHMENT_EDIT_ENCODING, "\005" }, // <Ctrl-E>
{ OP_ATTACHMENT_EDIT_LANGUAGE, "\014" }, // <Ctrl-L>
{ OP_ATTACHMENT_EDIT_MIME, "m" },
{ OP_ATTACHMENT_EDIT_TYPE, "\024" }, // <Ctrl-T>
{ OP_ATTACHMENT_FILTER, "F" },
{ OP_ATTACHMENT_GET_ATTACHMENT, "G" },
{ OP_ATTACHMENT_GROUP_ALTS, "&" },
{ OP_ATTACHMENT_GROUP_LINGUAL, "^" },
{ OP_ATTACHMENT_GROUP_RELATED, "%" },
{ OP_ATTACHMENT_MOVE_DOWN, "+" },
{ OP_ATTACHMENT_MOVE_UP, "-" },
{ OP_ATTACHMENT_NEW_MIME, "n" },
{ OP_EXIT, "q" },
{ OP_PIPE, "|" },
{ OP_ATTACHMENT_PRINT, "l" },
{ OP_ATTACHMENT_RENAME_ATTACHMENT, "\017" }, // <Ctrl-O>
{ OP_ATTACHMENT_SAVE, "C" },
{ OP_ATTACHMENT_TOGGLE_DISPOSITION, "\004" }, // <Ctrl-D>
{ OP_ATTACHMENT_TOGGLE_UNLINK, "u" },
{ OP_ATTACHMENT_UNGROUP, "#" },
{ OP_ATTACHMENT_UPDATE_ENCODING, "U" },
{ OP_ATTACHMENT_VIEW, "<keypadenter>" },
{ OP_ATTACHMENT_VIEW, "\n" }, // <Enter>
{ OP_ATTACHMENT_VIEW, "\r" }, // <Return>
#ifdef USE_AUTOCRYPT
{ OP_COMPOSE_AUTOCRYPT_MENU, "o" },
#endif
{ OP_COMPOSE_EDIT_FILE, "\033e" }, // <Alt-e>
{ OP_COMPOSE_EDIT_MESSAGE, "e" },
{ OP_COMPOSE_ISPELL, "i" },
#ifdef MIXMASTER
{ OP_COMPOSE_MIX, "M" },
#endif
{ OP_COMPOSE_PGP_MENU, "p" },
{ OP_COMPOSE_POSTPONE_MESSAGE, "P" },
{ OP_COMPOSE_RENAME_FILE, "R" },
{ OP_COMPOSE_SEND_MESSAGE, "y" },
{ OP_COMPOSE_SMIME_MENU, "S" },
{ OP_COMPOSE_WRITE_MESSAGE, "w" },
{ OP_DISPLAY_HEADERS, "h" },
{ OP_ENVELOPE_EDIT_BCC, "b" },
{ OP_ENVELOPE_EDIT_CC, "c" },
{ OP_ENVELOPE_EDIT_FCC, "f" },
{ OP_ENVELOPE_EDIT_FROM, "\033f" }, // <Alt-f>
{ OP_ENVELOPE_EDIT_HEADERS, "E" },
{ OP_ENVELOPE_EDIT_REPLY_TO, "r" },
{ OP_ENVELOPE_EDIT_SUBJECT, "s" },
{ OP_ENVELOPE_EDIT_TO, "t" },
{ OP_FORGET_PASSPHRASE, "\006" }, // <Ctrl-F>
{ OP_TAG, "T" },
{ 0, NULL },
};
// clang-format on
/**
* check_count - Check if there are any attachments
* @param actx Attachment context
* @retval true There are attachments
*/
static bool check_count(struct AttachCtx *actx)
{
if (actx->idxlen == 0)
{
mutt_error(_("There are no attachments"));
return false;
}
return true;
}
/**
* gen_cid - Generate a random Content ID
* @retval ptr Content ID
*
* @note The caller should free the string
*/
static char *gen_cid(void)
{
char rndid[MUTT_RANDTAG_LEN + 1];
mutt_rand_base32(rndid, sizeof(rndid) - 1);
rndid[MUTT_RANDTAG_LEN] = 0;
return mutt_str_dup(rndid);
}
/**
* check_cid - Check if a Content-ID is valid
* @param cid Content-ID to check
* @retval true Content-ID is valid
* @retval false Content-ID is not valid
*/
static bool check_cid(const char *cid)
{
static const char *check = "^[-\\.0-9@A-Z_a-z]+$";
struct Regex *check_cid_regex = mutt_regex_new(check, 0, NULL);
const bool valid = mutt_regex_match(check_cid_regex, cid);
mutt_regex_free(&check_cid_regex);
return valid;
}
/**
* check_attachments - Check if any attachments have changed or been deleted
* @param actx Attachment context
* @param sub ConfigSubset
* @retval 0 Success
* @retval -1 Error
*/
static int check_attachments(struct AttachCtx *actx, struct ConfigSubset *sub)
{
int rc = -1;
struct stat st = { 0 };
struct Buffer *pretty = NULL, *msg = NULL;
for (int i = 0; i < actx->idxlen; i++)
{
if (actx->idx[i]->body->type == TYPE_MULTIPART)
continue;
if (stat(actx->idx[i]->body->filename, &st) != 0)
{
if (!pretty)
pretty = buf_pool_get();
buf_strcpy(pretty, actx->idx[i]->body->filename);
buf_pretty_mailbox(pretty);
/* L10N: This message is displayed in the compose menu when an attachment
doesn't stat. %d is the attachment number and %s is the attachment
filename. The filename is located last to avoid a long path hiding
the error message. */
mutt_error(_("Attachment #%d no longer exists: %s"), i + 1, buf_string(pretty));
goto cleanup;
}
if (actx->idx[i]->body->stamp < st.st_mtime)
{
if (!pretty)
pretty = buf_pool_get();
buf_strcpy(pretty, actx->idx[i]->body->filename);
buf_pretty_mailbox(pretty);
if (!msg)
msg = buf_pool_get();
/* L10N: This message is displayed in the compose menu when an attachment
is modified behind the scenes. %d is the attachment number and %s is
the attachment filename. The filename is located last to avoid a long
path hiding the prompt question. */
buf_printf(msg, _("Attachment #%d modified. Update encoding for %s?"),
i + 1, buf_string(pretty));
enum QuadOption ans = query_yesorno(buf_string(msg), MUTT_YES);
if (ans == MUTT_YES)
mutt_update_encoding(actx->idx[i]->body, sub);
else if (ans == MUTT_ABORT)
goto cleanup;
}
}
rc = 0;
cleanup:
buf_pool_release(&pretty);
buf_pool_release(&msg);
return rc;
}
/**
* delete_attachment - Delete an attachment
* @param actx Attachment context
* @param aidx Index number of attachment to delete
* @retval 0 Success
* @retval -1 Error
*/
static int delete_attachment(struct AttachCtx *actx, int aidx)
{
if (!actx || (aidx < 0) || (aidx >= actx->idxlen))
return -1;
struct AttachPtr **idx = actx->idx;
struct Body *b_previous = NULL;
struct Body *b_parent = NULL;
if (aidx == 0)
{
struct Body *b = actx->idx[0]->body;
if (!b->next) // There's only one attachment left
{
mutt_error(_("You may not delete the only attachment"));
return -1;
}
}
if (idx[aidx]->level > 0)
{
if (attach_body_parent(idx[0]->body, NULL, idx[aidx]->body, &b_parent))
{
if (attach_body_count(b_parent->parts, false) < 3)
{
mutt_error(_("Can't leave group with only one attachment"));
return -1;
}
}
}
// reorder body pointers
if (aidx > 0)
{
if (attach_body_previous(idx[0]->body, idx[aidx]->body, &b_previous))
b_previous->next = idx[aidx]->body->next;
else if (attach_body_parent(idx[0]->body, NULL, idx[aidx]->body, &b_parent))
b_parent->parts = idx[aidx]->body->next;
}
// free memory
int part_count = 1;
if (aidx < (actx->idxlen - 1))
{
if ((idx[aidx]->body->type == TYPE_MULTIPART) &&
(idx[aidx + 1]->level > idx[aidx]->level))
{
part_count += attach_body_count(idx[aidx]->body->parts, true);
}
}
idx[aidx]->body->next = NULL;
mutt_body_free(&(idx[aidx]->body));
for (int i = 0; i < part_count; i++)
{
FREE(&idx[aidx + i]->tree);
FREE(&idx[aidx + i]);
}
// reorder attachment list
for (int i = aidx; i < (actx->idxlen - part_count); i++)
idx[i] = idx[i + part_count];
for (int i = 0; i < part_count; i++)
idx[actx->idxlen - i - 1] = NULL;
actx->idxlen -= part_count;
return 0;
}
/**
* update_idx - Add a new attachment to the message
* @param menu Current menu
* @param actx Attachment context
* @param ap Attachment to add
*/
static void update_idx(struct Menu *menu, struct AttachCtx *actx, struct AttachPtr *ap)
{
ap->level = 0;
for (int i = actx->idxlen; i > 0; i--)
{
if (ap->level == actx->idx[i - 1]->level)
{
actx->idx[i - 1]->body->next = ap->body;
break;
}
}
ap->body->aptr = ap;
mutt_actx_add_attach(actx, ap);
update_menu(actx, menu, false);
menu_set_index(menu, actx->vcount - 1);
}
/**
* compose_attach_swap - Swap two adjacent entries in the attachment list
* @param e Email
* @param actx Attachment information
* @param first Index of first attachment to swap
* @param second Index of second attachment to swap
*/
static void compose_attach_swap(struct Email *e, struct AttachCtx *actx, int first, int second)
{
struct AttachPtr **idx = actx->idx;
// check that attachments really are adjacent
if (idx[first]->body->next != idx[second]->body)
return;
// reorder Body pointers
if (first == 0)
{
// first attachment is the fundamental part
idx[first]->body->next = idx[second]->body->next;
idx[second]->body->next = idx[first]->body;
e->body = idx[second]->body;
}
else
{
// find previous attachment
struct Body *b_previous = NULL;
struct Body *b_parent = NULL;
if (attach_body_previous(e->body, idx[first]->body, &b_previous))
{
idx[first]->body->next = idx[second]->body->next;
idx[second]->body->next = idx[first]->body;
b_previous->next = idx[second]->body;
}
else if (attach_body_parent(e->body, NULL, idx[first]->body, &b_parent))
{
idx[first]->body->next = idx[second]->body->next;
idx[second]->body->next = idx[first]->body;
b_parent->parts = idx[second]->body;
}
}
// reorder attachment list
struct AttachPtr *saved = idx[second];
for (int i = second; i > first; i--)
idx[i] = idx[i - 1];
idx[first] = saved;
// if moved attachment is a group then move subparts too
if ((idx[first]->body->type == TYPE_MULTIPART) && (second < actx->idxlen - 1))
{
int i = second + 1;
while (idx[i]->level > idx[first]->level)
{
saved = idx[i];
int destidx = i - second + first;
for (int j = i; j > destidx; j--)
idx[j] = idx[j - 1];
idx[destidx] = saved;
i++;
if (i >= actx->idxlen)
break;
}
}
}
/**
* group_attachments - Group tagged attachments into a multipart group
* @param shared Shared compose data
* @param subtype MIME subtype
* @retval FR_SUCCESS Success
* @retval FR_ERROR Failure
*/
static int group_attachments(struct ComposeSharedData *shared, char *subtype)
{
struct AttachCtx *actx = shared->adata->actx;
int group_level = -1;
struct Body *bptr_parent = NULL;
// Attachments to be grouped must have the same parent
for (int i = 0; i < actx->idxlen; i++)
{
// check if all tagged attachments are at same level
if (actx->idx[i]->body->tagged)
{
if (group_level == -1)
{
group_level = actx->idx[i]->level;
}
else
{
if (group_level != actx->idx[i]->level)
{
mutt_error(_("Attachments to be grouped must have the same parent"));
return FR_ERROR;
}
}
// if not at top level check if all tagged attachments have same parent
if (group_level > 0)
{
if (bptr_parent)
{
struct Body *bptr_test = NULL;
if (!attach_body_parent(actx->idx[0]->body, NULL, actx->idx[i]->body, &bptr_test))
mutt_debug(LL_DEBUG5, "can't find parent\n");
if (bptr_test != bptr_parent)
{
mutt_error(_("Attachments to be grouped must have the same parent"));
return FR_ERROR;
}
}
else
{
if (!attach_body_parent(actx->idx[0]->body, NULL, actx->idx[i]->body, &bptr_parent))
mutt_debug(LL_DEBUG5, "can't find parent\n");
}
}
}
}
// Can't group all attachments unless at top level
if (bptr_parent)
{
if (shared->adata->menu->num_tagged == attach_body_count(bptr_parent->parts, false))
{
mutt_error(_("Can't leave group with only one attachment"));
return FR_ERROR;
}
}
struct Body *group = mutt_body_new();
group->type = TYPE_MULTIPART;
group->subtype = mutt_str_dup(subtype);
group->encoding = ENC_7BIT;
struct Body *bptr_first = NULL; // first tagged attachment
struct Body *bptr = NULL; // current tagged attachment
struct Body *group_parent = NULL; // parent of group
struct Body *group_previous = NULL; // previous body to group
struct Body *group_part = NULL; // current attachment in group
int group_idx = 0; // index in attachment list where group will be inserted
int group_last_idx = 0; // index of last part of previous found group
int group_parent_type = TYPE_OTHER;
for (int i = 0; i < actx->idxlen; i++)
{
bptr = actx->idx[i]->body;
if (bptr->tagged)
{
// set group properties based on first tagged attachment
if (!bptr_first)
{
group->disposition = bptr->disposition;
if (bptr->language && !mutt_str_equal(subtype, "multilingual"))
group->language = mutt_str_dup(bptr->language);
group_parent_type = bptr->aptr->parent_type;
bptr_first = bptr;
if (i > 0)
{
if (!attach_body_previous(shared->email->body, bptr, &group_previous))
{
mutt_debug(LL_DEBUG5, "couldn't find previous\n");
}
if (!attach_body_parent(shared->email->body, NULL, bptr, &group_parent))
{
mutt_debug(LL_DEBUG5, "couldn't find parent\n");
}
}
}
shared->adata->menu->num_tagged--;
bptr->tagged = false;
bptr->aptr->level++;
bptr->aptr->parent_type = TYPE_MULTIPART;
// append bptr to the group parts list and remove from email body list
struct Body *bptr_previous = NULL;
if (attach_body_previous(shared->email->body, bptr, &bptr_previous))
bptr_previous->next = bptr->next;
else if (attach_body_parent(shared->email->body, NULL, bptr, &bptr_parent))
bptr_parent->parts = bptr->next;
else
shared->email->body = bptr->next;
if (group_part)
{
// add bptr to group parts list
group_part->next = bptr;
group_part = group_part->next;
group_part->next = NULL;
// reorder attachments and set levels
int bptr_attachments = attach_body_count(bptr, true);
for (int j = i + 1; j < (i + bptr_attachments); j++)
actx->idx[j]->level++;
if (i > (group_last_idx + 1))
{
for (int j = 0; j < bptr_attachments; j++)
{
struct AttachPtr *saved = actx->idx[i + bptr_attachments - 1];
for (int k = i + bptr_attachments - 1; k > (group_last_idx + 1); k--)
actx->idx[k] = actx->idx[k - 1];
actx->idx[group_last_idx + 1] = saved;
}
}
i += bptr_attachments - 1;
group_last_idx += bptr_attachments;
}
else
{
group_idx = i;
group->parts = bptr;
group_part = bptr;
group_part->next = NULL;
int bptr_attachments = attach_body_count(bptr, true);
for (int j = i + 1; j < (i + bptr_attachments); j++)
actx->idx[j]->level++;
i += bptr_attachments - 1;
group_last_idx = i;
}
}
}
if (!bptr_first)
{
mutt_body_free(&group);
return FR_ERROR;
}
// set group->next
int next_aidx = group_idx + attach_body_count(group->parts, true);
if (group_parent)
{
// find next attachment with the same parent as the group
struct Body *b = NULL;
struct Body *b_parent = NULL;
while (next_aidx < actx->idxlen)
{
b = actx->idx[next_aidx]->body;
b_parent = NULL;
if (attach_body_parent(shared->email->body, NULL, b, &b_parent))
{
if (group_parent == b_parent)
{
group->next = b;
break;
}
}
next_aidx++;
}
}
else if (next_aidx < actx->idxlen)
{
// group is at top level
group->next = actx->idx[next_aidx]->body;
}
// set previous or parent for group
if (group_previous)
group_previous->next = group;
else if (group_parent)
group_parent->parts = group;
mutt_generate_boundary(&group->parameter);
struct AttachPtr *group_ap = mutt_aptr_new();
group_ap->body = group;
group_ap->body->aptr = group_ap;
group_ap->level = group_level;
group_ap->parent_type = group_parent_type;
// insert group into attachment list
mutt_actx_ins_attach(actx, group_ap, group_idx);
// update email body and last attachment pointers
shared->email->body = actx->idx[0]->body;
actx->idx[actx->idxlen - 1]->body->next = NULL;
update_menu(actx, shared->adata->menu, false);
shared->adata->menu->current = group_idx;
menu_queue_redraw(shared->adata->menu, MENU_REDRAW_INDEX);
mutt_message_hook(NULL, shared->email, MUTT_SEND2_HOOK);
return FR_SUCCESS;
}
// -----------------------------------------------------------------------------
/**
* op_attachment_attach_file - Attach files to this message - Implements ::compose_function_t - @ingroup compose_function_api
*/
static int op_attachment_attach_file(struct ComposeSharedData *shared, int op)
{
char *prompt = _("Attach file");
int numfiles = 0;
char **files = NULL;
struct Buffer *fname = buf_pool_get();
if ((mw_enter_fname(prompt, fname, false, NULL, true, &files, &numfiles,
MUTT_SEL_MULTI) == -1) ||
buf_is_empty(fname))
{
for (int i = 0; i < numfiles; i++)
FREE(&files[i]);
FREE(&files);
buf_pool_release(&fname);
return FR_NO_ACTION;
}
bool error = false;
bool added_attachment = false;
if (numfiles > 1)
{
mutt_message(ngettext("Attaching selected file...",
"Attaching selected files...", numfiles));
}
for (int i = 0; i < numfiles; i++)
{
char *att = files[i];
if (!att)
continue;
struct AttachPtr *ap = mutt_aptr_new();
ap->unowned = true;
ap->body = mutt_make_file_attach(att, shared->sub);
if (ap->body)
{
added_attachment = true;
update_idx(shared->adata->menu, shared->adata->actx, ap);
}
else
{
error = true;
mutt_error(_("Unable to attach %s"), att);
mutt_aptr_free(&ap);
}
FREE(&files[i]);
}
FREE(&files);
buf_pool_release(&fname);
if (!error)
mutt_clear_error();
menu_queue_redraw(shared->adata->menu, MENU_REDRAW_INDEX);
notify_send(shared->email->notify, NT_EMAIL, NT_EMAIL_CHANGE_ATTACH, NULL);
if (added_attachment)
mutt_message_hook(NULL, shared->email, MUTT_SEND2_HOOK);
return FR_SUCCESS;
}
/**
* op_attachment_attach_key - Attach a PGP public key - Implements ::compose_function_t - @ingroup compose_function_api
*/
static int op_attachment_attach_key(struct ComposeSharedData *shared, int op)
{
if (!(WithCrypto & APPLICATION_PGP))
return FR_NOT_IMPL;
struct AttachPtr *ap = mutt_aptr_new();
ap->body = crypt_pgp_make_key_attachment();
if (ap->body)
{
update_idx(shared->adata->menu, shared->adata->actx, ap);
menu_queue_redraw(shared->adata->menu, MENU_REDRAW_INDEX);
mutt_message_hook(NULL, shared->email, MUTT_SEND2_HOOK);
}
else
{
mutt_aptr_free(&ap);
}
notify_send(shared->email->notify, NT_EMAIL, NT_EMAIL_CHANGE_ATTACH, NULL);
return FR_SUCCESS;
}
/**
* op_attachment_attach_message - Attach messages to this message - Implements ::compose_function_t - @ingroup compose_function_api
*
* This function handles:
* - OP_ATTACHMENT_ATTACH_MESSAGE
* - OP_ATTACHMENT_ATTACH_NEWS_MESSAGE
*/
static int op_attachment_attach_message(struct ComposeSharedData *shared, int op)
{
char *prompt = _("Open mailbox to attach message from");
OptNews = false;
if (shared->mailbox && (op == OP_ATTACHMENT_ATTACH_NEWS_MESSAGE))
{
const char *const c_news_server = cs_subset_string(shared->sub, "news_server");
CurrentNewsSrv = nntp_select_server(shared->mailbox, c_news_server, false);
if (!CurrentNewsSrv)
return FR_NO_ACTION;
prompt = _("Open newsgroup to attach message from");
OptNews = true;
}
struct Buffer *fname = buf_pool_get();
if (shared->mailbox)
{
if ((op == OP_ATTACHMENT_ATTACH_MESSAGE) ^ (shared->mailbox->type == MUTT_NNTP))
{
buf_strcpy(fname, mailbox_path(shared->mailbox));
buf_pretty_mailbox(fname);
}
}
if ((mw_enter_fname(prompt, fname, true, shared->mailbox, false, NULL, NULL,
MUTT_SEL_NO_FLAGS) == -1) ||
buf_is_empty(fname))
{
buf_pool_release(&fname);
return FR_NO_ACTION;
}
if (OptNews)
nntp_expand_path(fname->data, fname->dsize, &CurrentNewsSrv->conn->account);
else
buf_expand_path(fname);
if (imap_path_probe(buf_string(fname), NULL) != MUTT_IMAP)
{
if (pop_path_probe(buf_string(fname), NULL) != MUTT_POP)
{
if (!OptNews && (nntp_path_probe(buf_string(fname), NULL) != MUTT_NNTP))
{
if (mx_path_probe(buf_string(fname)) != MUTT_NOTMUCH)
{
/* check to make sure the file exists and is readable */
if (access(buf_string(fname), R_OK) == -1)
{
mutt_perror("%s", buf_string(fname));
buf_pool_release(&fname);
return FR_ERROR;
}
}
}
}
}
menu_queue_redraw(shared->adata->menu, MENU_REDRAW_FULL);
struct Mailbox *m_attach = mx_path_resolve(buf_string(fname));
const bool old_readonly = m_attach->readonly;
if (!mx_mbox_open(m_attach, MUTT_READONLY))
{
mutt_error(_("Unable to open mailbox %s"), buf_string(fname));
mx_fastclose_mailbox(m_attach, false);
m_attach = NULL;
buf_pool_release(&fname);
return FR_ERROR;
}
buf_pool_release(&fname);
if (m_attach->msg_count == 0)
{
mx_mbox_close(m_attach);
mutt_error(_("No messages in that folder"));
return FR_NO_ACTION;
}
/* `$sort`, `$sort_aux`, `$use_threads` could be changed in dlg_index() */
const enum SortType old_sort = cs_subset_sort(shared->sub, "sort");
const enum SortType old_sort_aux = cs_subset_sort(shared->sub, "sort_aux");
const unsigned char old_use_threads = cs_subset_enum(shared->sub, "use_threads");
mutt_message(_("Tag the messages you want to attach"));
struct MuttWindow *dlg = index_pager_init();
struct IndexSharedData *index_shared = dlg->wdata;
index_shared->attach_msg = true;
dialog_push(dlg);
struct Mailbox *m_attach_new = dlg_index(dlg, m_attach);
dialog_pop();
mutt_window_free(&dlg);
if (!shared->mailbox)
{
/* Restore old $sort variables */
cs_subset_str_native_set(shared->sub, "sort", old_sort, NULL);
cs_subset_str_native_set(shared->sub, "sort_aux", old_sort_aux, NULL);
cs_subset_str_native_set(shared->sub, "use_threads", old_use_threads, NULL);
menu_queue_redraw(shared->adata->menu, MENU_REDRAW_INDEX);
notify_send(shared->email->notify, NT_EMAIL, NT_EMAIL_CHANGE_ATTACH, NULL);
return FR_SUCCESS;
}
bool added_attachment = false;
for (int i = 0; i < m_attach_new->msg_count; i++)
{
if (!m_attach_new->emails[i])
break;
if (!message_is_tagged(m_attach_new->emails[i]))
continue;
struct AttachPtr *ap = mutt_aptr_new();
ap->body = mutt_make_message_attach(m_attach_new, m_attach_new->emails[i],
true, shared->sub);
if (ap->body)
{
added_attachment = true;
update_idx(shared->adata->menu, shared->adata->actx, ap);
}
else
{
mutt_error(_("Unable to attach"));
mutt_aptr_free(&ap);
}
}
menu_queue_redraw(shared->adata->menu, MENU_REDRAW_FULL);
if (m_attach_new == m_attach)
{
m_attach->readonly = old_readonly;
}
mx_fastclose_mailbox(m_attach_new, false);
/* Restore old $sort variables */
cs_subset_str_native_set(shared->sub, "sort", old_sort, NULL);
cs_subset_str_native_set(shared->sub, "sort_aux", old_sort_aux, NULL);
cs_subset_str_native_set(shared->sub, "use_threads", old_use_threads, NULL);
notify_send(shared->email->notify, NT_EMAIL, NT_EMAIL_CHANGE_ATTACH, NULL);
if (added_attachment)
mutt_message_hook(NULL, shared->email, MUTT_SEND2_HOOK);
return FR_SUCCESS;
}
/**
* op_attachment_detach - Delete the current entry - Implements ::compose_function_t - @ingroup compose_function_api
*/
static int op_attachment_detach(struct ComposeSharedData *shared, int op)
{
struct AttachCtx *actx = shared->adata->actx;
if (!check_count(actx))
return FR_NO_ACTION;
struct Menu *menu = shared->adata->menu;
struct AttachPtr *cur_att = current_attachment(actx, menu);
if (cur_att->unowned)
cur_att->body->unlink = false;
int index = menu_get_index(menu);
if (delete_attachment(actx, index) == -1)
return FR_ERROR;
menu->num_tagged = 0;
for (int i = 0; i < actx->idxlen; i++)
{
if (actx->idx[i]->body->tagged)
menu->num_tagged++;
}
update_menu(actx, menu, false);
notify_send(shared->email->notify, NT_EMAIL, NT_EMAIL_CHANGE_ATTACH, NULL);
index = menu_get_index(menu);
if (index == 0)
shared->email->body = actx->idx[0]->body;
mutt_message_hook(NULL, shared->email, MUTT_SEND2_HOOK);
return FR_SUCCESS;
}
/**
* op_attachment_edit_content_id - Edit the 'Content-ID' of the attachment - Implements ::compose_function_t - @ingroup compose_function_api
*/