forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compose.c
2017 lines (1804 loc) · 59.2 KB
/
compose.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
* GUI editor for an email's headers
*
* @authors
* Copyright (C) 1996-2000,2002,2007,2010,2012 Michael R. Elkins <me@mutt.org>
* Copyright (C) 2004 g10 Code GmbH
*
* @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 GUI editor for an email's headers
*
* GUI editor for an email's headers
*/
#include "config.h"
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mutt/mutt.h"
#include "config/lib.h"
#include "email/lib.h"
#include "conn/conn.h"
#include "mutt.h"
#include "compose.h"
#include "alias.h"
#include "browser.h"
#include "commands.h"
#include "context.h"
#include "curs_lib.h"
#include "edit.h"
#include "format_flags.h"
#include "globals.h"
#include "hook.h"
#include "index.h"
#include "keymap.h"
#include "mailbox.h"
#include "mutt_attach.h"
#include "mutt_curses.h"
#include "mutt_header.h"
#include "mutt_logging.h"
#include "mutt_menu.h"
#include "mutt_window.h"
#include "muttlib.h"
#include "mx.h"
#include "ncrypt/ncrypt.h"
#include "opcodes.h"
#include "options.h"
#include "protos.h"
#include "recvattach.h"
#include "sendlib.h"
#include "sort.h"
#ifdef ENABLE_NLS
#include <libintl.h>
#endif
#ifdef MIXMASTER
#include "remailer.h"
#endif
#ifdef USE_NNTP
#include "nntp/nntp.h"
#endif
#ifdef USE_POP
#include "pop/pop.h"
#endif
#ifdef USE_IMAP
#include "imap/imap.h"
#endif
/* These Config Variables are only used in compose.c */
char *C_ComposeFormat; ///< Config: printf-like format string for the Compose panel's status bar
char *C_Ispell; ///< Config: External command to perform spell-checking
unsigned char C_Postpone; ///< Config: Save messages to the #C_Postponed folder
static const char *There_are_no_attachments = N_("There are no attachments");
#define CHECK_COUNT \
if (actx->idxlen == 0) \
{ \
mutt_error(_(There_are_no_attachments)); \
break; \
}
#define CURATTACH actx->idx[actx->v2r[menu->current]]
/**
* enum HeaderField - Ordered list of headers for the compose screen
*
* The position of various fields on the compose screen.
*/
enum HeaderField
{
HDR_FROM = 0, ///< "From:" field
HDR_TO, ///< "To:" field
HDR_CC, ///< "Cc:" field
HDR_BCC, ///< "Bcc:" field
HDR_SUBJECT, ///< "Subject:" field
HDR_REPLYTO, ///< "Reply-To:" field
HDR_FCC, ///< "Fcc:" (save folder) field
#ifdef MIXMASTER
HDR_MIX, ///< "Mix:" field (Mixmaster chain)
#endif
HDR_CRYPT, ///< "Security:" field (encryption/signing info)
HDR_CRYPTINFO, ///< "Sign as:" field (encryption/signing info)
#ifdef USE_NNTP
HDR_NEWSGROUPS, ///< "Newsgroups:" field
HDR_FOLLOWUPTO, ///< "Followup-To:" field
HDR_XCOMMENTTO, ///< "X-Comment-To:" field
#endif
HDR_ATTACH = (HDR_FCC + 5), ///< Position to start printing the attachments
};
int HeaderPadding[HDR_XCOMMENTTO + 1] = { 0 };
int MaxHeaderWidth = 0;
#define HDR_XOFFSET MaxHeaderWidth
#define W (MuttIndexWindow->cols - MaxHeaderWidth)
static const char *const Prompts[] = {
/* L10N: Compose menu field. May not want to translate. */
N_("From: "),
/* L10N: Compose menu field. May not want to translate. */
N_("To: "),
/* L10N: Compose menu field. May not want to translate. */
N_("Cc: "),
/* L10N: Compose menu field. May not want to translate. */
N_("Bcc: "),
/* L10N: Compose menu field. May not want to translate. */
N_("Subject: "),
/* L10N: Compose menu field. May not want to translate. */
N_("Reply-To: "),
/* L10N: Compose menu field. May not want to translate. */
N_("Fcc: "),
#ifdef MIXMASTER
/* L10N: "Mix" refers to the MixMaster chain for anonymous email */
N_("Mix: "),
#endif
/* L10N: Compose menu field. Holds "Encrypt", "Sign" related information */
N_("Security: "),
/* L10N:
This string is used by the compose menu.
Since it is hidden by default, it does not increase the
indentation of other compose menu fields. However, if possible,
it should not be longer than the other compose menu fields.
Since it shares the row with "Encrypt with:", it should not be longer
than 15-20 character cells. */
N_("Sign as: "),
#ifdef USE_NNTP
/* L10N: Compose menu field. May not want to translate. */
N_("Newsgroups: "),
/* L10N: Compose menu field. May not want to translate. */
N_("Followup-To: "),
/* L10N: Compose menu field. May not want to translate. */
N_("X-Comment-To: "),
#endif
};
static const struct Mapping ComposeHelp[] = {
{ N_("Send"), OP_COMPOSE_SEND_MESSAGE },
{ N_("Abort"), OP_EXIT },
/* L10N: compose menu help line entry */
{ N_("To"), OP_COMPOSE_EDIT_TO },
/* L10N: compose menu help line entry */
{ N_("CC"), OP_COMPOSE_EDIT_CC },
/* L10N: compose menu help line entry */
{ N_("Subj"), OP_COMPOSE_EDIT_SUBJECT },
{ N_("Attach file"), OP_COMPOSE_ATTACH_FILE },
{ N_("Descrip"), OP_COMPOSE_EDIT_DESCRIPTION },
{ N_("Help"), OP_HELP },
{ NULL, 0 },
};
#ifdef USE_NNTP
static struct Mapping ComposeNewsHelp[] = {
{ N_("Send"), OP_COMPOSE_SEND_MESSAGE },
{ N_("Abort"), OP_EXIT },
{ N_("Newsgroups"), OP_COMPOSE_EDIT_NEWSGROUPS },
{ N_("Subj"), OP_COMPOSE_EDIT_SUBJECT },
{ N_("Attach file"), OP_COMPOSE_ATTACH_FILE },
{ N_("Descrip"), OP_COMPOSE_EDIT_DESCRIPTION },
{ N_("Help"), OP_HELP },
{ NULL, 0 },
};
#endif
/**
* calc_header_width_padding - Calculate the width needed for the compose labels
* @param idx Store the result at this index of HeaderPadding
* @param header Header string
* @param calc_max If true, calculate the maximum width
*/
static void calc_header_width_padding(int idx, const char *header, bool calc_max)
{
int width;
HeaderPadding[idx] = mutt_str_strlen(header);
width = mutt_strwidth(header);
if (calc_max && (MaxHeaderWidth < width))
MaxHeaderWidth = width;
HeaderPadding[idx] -= width;
}
/**
* init_header_padding - Calculate how much padding the compose table will need
*
* The padding needed for each header is strlen() + max_width - strwidth().
*
* calc_header_width_padding sets each entry in HeaderPadding to strlen -
* width. Then, afterwards, we go through and add max_width to each entry.
*/
static void init_header_padding(void)
{
static bool done = false;
if (done)
return;
done = true;
for (int i = 0; i <= HDR_XCOMMENTTO; i++)
calc_header_width_padding(i, _(Prompts[i]), true);
/* Don't include "Sign as: " in the MaxHeaderWidth calculation. It
* doesn't show up by default, and so can make the indentation of
* the other fields look funny. */
calc_header_width_padding(HDR_CRYPTINFO, _(Prompts[HDR_CRYPTINFO]), false);
for (int i = 0; i <= HDR_XCOMMENTTO; i++)
{
HeaderPadding[i] += MaxHeaderWidth;
if (HeaderPadding[i] < 0)
HeaderPadding[i] = 0;
}
}
/**
* snd_make_entry - Format a menu item for the attachment list - Implements Menu::menu_make_entry()
*/
static void snd_make_entry(char *buf, size_t buflen, struct Menu *menu, int line)
{
struct AttachCtx *actx = menu->data;
mutt_expando_format(buf, buflen, 0, MuttIndexWindow->cols, NONULL(C_AttachFormat),
attach_format_str, (unsigned long) (actx->idx[actx->v2r[line]]),
MUTT_FORMAT_STAT_FILE | MUTT_FORMAT_ARROWCURSOR);
}
/**
* redraw_crypt_lines - Update the encryption info in the compose window
* @param msg Header of message
*/
static void redraw_crypt_lines(struct Email *msg)
{
SETCOLOR(MT_COLOR_COMPOSE_HEADER);
mutt_window_mvprintw(MuttIndexWindow, HDR_CRYPT, 0, "%*s",
HeaderPadding[HDR_CRYPT], _(Prompts[HDR_CRYPT]));
NORMAL_COLOR;
if ((WithCrypto & (APPLICATION_PGP | APPLICATION_SMIME)) == 0)
{
addstr(_("Not supported"));
return;
}
if ((msg->security & (SEC_ENCRYPT | SEC_SIGN)) == (SEC_ENCRYPT | SEC_SIGN))
{
SETCOLOR(MT_COLOR_COMPOSE_SECURITY_BOTH);
addstr(_("Sign, Encrypt"));
}
else if (msg->security & SEC_ENCRYPT)
{
SETCOLOR(MT_COLOR_COMPOSE_SECURITY_ENCRYPT);
addstr(_("Encrypt"));
}
else if (msg->security & SEC_SIGN)
{
SETCOLOR(MT_COLOR_COMPOSE_SECURITY_SIGN);
addstr(_("Sign"));
}
else
{
/* L10N: This refers to the encryption of the email, e.g. "Security: None" */
SETCOLOR(MT_COLOR_COMPOSE_SECURITY_NONE);
addstr(_("None"));
}
NORMAL_COLOR;
if ((msg->security & (SEC_ENCRYPT | SEC_SIGN)))
{
if (((WithCrypto & APPLICATION_PGP) != 0) && (msg->security & APPLICATION_PGP))
{
if ((msg->security & SEC_INLINE))
addstr(_(" (inline PGP)"));
else
addstr(_(" (PGP/MIME)"));
}
else if (((WithCrypto & APPLICATION_SMIME) != 0) && (msg->security & APPLICATION_SMIME))
addstr(_(" (S/MIME)"));
}
if (C_CryptOpportunisticEncrypt && (msg->security & SEC_OPPENCRYPT))
addstr(_(" (OppEnc mode)"));
mutt_window_clrtoeol(MuttIndexWindow);
mutt_window_move(MuttIndexWindow, HDR_CRYPTINFO, 0);
mutt_window_clrtoeol(MuttIndexWindow);
if (((WithCrypto & APPLICATION_PGP) != 0) &&
(msg->security & APPLICATION_PGP) && (msg->security & SEC_SIGN))
{
SETCOLOR(MT_COLOR_COMPOSE_HEADER);
printw("%*s", HeaderPadding[HDR_CRYPTINFO], _(Prompts[HDR_CRYPTINFO]));
NORMAL_COLOR;
printw("%s", C_PgpSignAs ? C_PgpSignAs : _("<default>"));
}
if (((WithCrypto & APPLICATION_SMIME) != 0) &&
(msg->security & APPLICATION_SMIME) && (msg->security & SEC_SIGN))
{
SETCOLOR(MT_COLOR_COMPOSE_HEADER);
printw("%*s", HeaderPadding[HDR_CRYPTINFO], _(Prompts[HDR_CRYPTINFO]));
NORMAL_COLOR;
printw("%s", C_SmimeSignAs ? C_SmimeSignAs : _("<default>"));
}
if (((WithCrypto & APPLICATION_SMIME) != 0) && (msg->security & APPLICATION_SMIME) &&
(msg->security & SEC_ENCRYPT) && C_SmimeEncryptWith && *C_SmimeEncryptWith)
{
SETCOLOR(MT_COLOR_COMPOSE_HEADER);
mutt_window_mvprintw(MuttIndexWindow, HDR_CRYPTINFO, 40, "%s", _("Encrypt with: "));
NORMAL_COLOR;
printw("%s", NONULL(C_SmimeEncryptWith));
}
}
#ifdef MIXMASTER
/**
* redraw_mix_line - Redraw the Mixmaster chain
* @param chain List of chain links
*/
static void redraw_mix_line(struct ListHead *chain)
{
char *t = NULL;
SETCOLOR(MT_COLOR_COMPOSE_HEADER);
mutt_window_mvprintw(MuttIndexWindow, HDR_MIX, 0, "%*s",
HeaderPadding[HDR_MIX], _(Prompts[HDR_MIX]));
NORMAL_COLOR;
if (STAILQ_EMPTY(chain))
{
addstr(_("<no chain defined>"));
mutt_window_clrtoeol(MuttIndexWindow);
return;
}
int c = 12;
struct ListNode *np = NULL;
STAILQ_FOREACH(np, chain, entries)
{
t = np->data;
if (t && (t[0] == '0') && (t[1] == '\0'))
t = "<random>";
if (c + mutt_str_strlen(t) + 2 >= MuttIndexWindow->cols)
break;
addstr(NONULL(t));
if (STAILQ_NEXT(np, entries))
addstr(", ");
c += mutt_str_strlen(t) + 2;
}
}
#endif /* MIXMASTER */
/**
* check_attachments - Check if any attachments have changed or been deleted
* @param actx Attachment context
* @retval 0 Success
* @retval -1 Error
*/
static int check_attachments(struct AttachCtx *actx)
{
struct stat st;
char pretty[PATH_MAX], msg[PATH_MAX + 128];
for (int i = 0; i < actx->idxlen; i++)
{
if (actx->idx[i]->content->type == TYPE_MULTIPART)
continue;
mutt_str_strfcpy(pretty, actx->idx[i]->content->filename, sizeof(pretty));
if (stat(actx->idx[i]->content->filename, &st) != 0)
{
mutt_pretty_mailbox(pretty, sizeof(pretty));
mutt_error(_("%s [#%d] no longer exists"), pretty, i + 1);
return -1;
}
if (actx->idx[i]->content->stamp < st.st_mtime)
{
mutt_pretty_mailbox(pretty, sizeof(pretty));
snprintf(msg, sizeof(msg), _("%s [#%d] modified. Update encoding?"), pretty, i + 1);
enum QuadOption ans = mutt_yesorno(msg, MUTT_YES);
if (ans == MUTT_YES)
mutt_update_encoding(actx->idx[i]->content);
else if (ans == MUTT_ABORT)
return -1;
}
}
return 0;
}
/**
* draw_envelope_addr - Write addresses to the compose window
* @param line Line to write to (index into Prompts)
* @param addr Address list to write
*/
static void draw_envelope_addr(int line, struct Address *addr)
{
char buf[1024];
buf[0] = '\0';
mutt_addr_write(buf, sizeof(buf), addr, true);
SETCOLOR(MT_COLOR_COMPOSE_HEADER);
mutt_window_mvprintw(MuttIndexWindow, line, 0, "%*s", HeaderPadding[line],
_(Prompts[line]));
NORMAL_COLOR;
mutt_paddstr(W, buf);
}
/**
* draw_envelope - Write the email headers to the compose window
* @param msg Header of the message
* @param fcc Fcc field
*/
static void draw_envelope(struct Email *msg, char *fcc)
{
draw_envelope_addr(HDR_FROM, msg->env->from);
#ifdef USE_NNTP
if (!OptNewsSend)
{
#endif
draw_envelope_addr(HDR_TO, msg->env->to);
draw_envelope_addr(HDR_CC, msg->env->cc);
draw_envelope_addr(HDR_BCC, msg->env->bcc);
#ifdef USE_NNTP
}
else
{
mutt_window_mvprintw(MuttIndexWindow, HDR_TO, 0, "%*s",
HeaderPadding[HDR_NEWSGROUPS], Prompts[HDR_NEWSGROUPS]);
mutt_paddstr(W, NONULL(msg->env->newsgroups));
mutt_window_mvprintw(MuttIndexWindow, HDR_CC, 0, "%*s",
HeaderPadding[HDR_FOLLOWUPTO], Prompts[HDR_FOLLOWUPTO]);
mutt_paddstr(W, NONULL(msg->env->followup_to));
if (C_XCommentTo)
{
mutt_window_mvprintw(MuttIndexWindow, HDR_BCC, 0, "%*s",
HeaderPadding[HDR_XCOMMENTTO], Prompts[HDR_XCOMMENTTO]);
mutt_paddstr(W, NONULL(msg->env->x_comment_to));
}
}
#endif
SETCOLOR(MT_COLOR_COMPOSE_HEADER);
mutt_window_mvprintw(MuttIndexWindow, HDR_SUBJECT, 0, "%*s",
HeaderPadding[HDR_SUBJECT], _(Prompts[HDR_SUBJECT]));
NORMAL_COLOR;
mutt_paddstr(W, NONULL(msg->env->subject));
draw_envelope_addr(HDR_REPLYTO, msg->env->reply_to);
SETCOLOR(MT_COLOR_COMPOSE_HEADER);
mutt_window_mvprintw(MuttIndexWindow, HDR_FCC, 0, "%*s",
HeaderPadding[HDR_FCC], _(Prompts[HDR_FCC]));
NORMAL_COLOR;
mutt_paddstr(W, fcc);
if (WithCrypto)
redraw_crypt_lines(msg);
#ifdef MIXMASTER
redraw_mix_line(&msg->chain);
#endif
SETCOLOR(MT_COLOR_STATUS);
mutt_window_mvaddstr(MuttIndexWindow, HDR_ATTACH - 1, 0, _("-- Attachments"));
mutt_window_clrtoeol(MuttIndexWindow);
NORMAL_COLOR;
}
/**
* edit_address_list - Let the user edit the address list
* @param[in] line Index into the Prompts lists
* @param[in,out] addr Address list to edit
*/
static void edit_address_list(int line, struct Address **addr)
{
char buf[8192] = ""; /* needs to be large for alias expansion */
char *err = NULL;
mutt_addrlist_to_local(*addr);
mutt_addr_write(buf, sizeof(buf), *addr, false);
if (mutt_get_field(_(Prompts[line]), buf, sizeof(buf), MUTT_ALIAS) == 0)
{
mutt_addr_free(addr);
*addr = mutt_addr_parse_list2(*addr, buf);
*addr = mutt_expand_aliases(*addr);
}
if (mutt_addrlist_to_intl(*addr, &err) != 0)
{
mutt_error(_("Bad IDN: '%s'"), err);
mutt_refresh();
FREE(&err);
}
/* redraw the expanded list so the user can see the result */
buf[0] = '\0';
mutt_addr_write(buf, sizeof(buf), *addr, true);
mutt_window_move(MuttIndexWindow, line, HDR_XOFFSET);
mutt_paddstr(W, buf);
}
/**
* delete_attachment - Delete an attachment
* @param actx Attachment context
* @param x Index number of attachment
* @retval 0 Success
* @retval -1 Error
*/
static int delete_attachment(struct AttachCtx *actx, int x)
{
struct AttachPtr **idx = actx->idx;
int rindex = actx->v2r[x];
if ((rindex == 0) && (actx->idxlen == 1))
{
mutt_error(_("You may not delete the only attachment"));
idx[rindex]->content->tagged = false;
return -1;
}
for (int y = 0; y < actx->idxlen; y++)
{
if (idx[y]->content->next == idx[rindex]->content)
{
idx[y]->content->next = idx[rindex]->content->next;
break;
}
}
idx[rindex]->content->next = NULL;
idx[rindex]->content->parts = NULL;
mutt_body_free(&(idx[rindex]->content));
FREE(&idx[rindex]->tree);
FREE(&idx[rindex]);
for (; rindex < actx->idxlen - 1; rindex++)
idx[rindex] = idx[rindex + 1];
idx[actx->idxlen - 1] = NULL;
actx->idxlen--;
return 0;
}
/**
* mutt_gen_compose_attach_list - Generate the attachment list for the compose screen
* @param actx Attachment context
* @param m Attachment
* @param parent_type Attachment type, e.g #TYPE_MULTIPART
* @param level Nesting depth of attachment
*/
static void mutt_gen_compose_attach_list(struct AttachCtx *actx, struct Body *m,
int parent_type, int level)
{
for (; m; m = m->next)
{
if ((m->type == TYPE_MULTIPART) && m->parts &&
(!(WithCrypto & APPLICATION_PGP) || !mutt_is_multipart_encrypted(m)))
{
mutt_gen_compose_attach_list(actx, m->parts, m->type, level);
}
else
{
struct AttachPtr *new = mutt_mem_calloc(1, sizeof(struct AttachPtr));
mutt_actx_add_attach(actx, new);
new->content = m;
m->aptr = new;
new->parent_type = parent_type;
new->level = level;
/* We don't support multipart messages in the compose menu yet */
}
}
}
/**
* mutt_update_compose_menu - Redraw the compose window
* @param actx Attachment context
* @param menu Current menu
* @param init If true, initialise the attachment list
*/
static void mutt_update_compose_menu(struct AttachCtx *actx, struct Menu *menu, bool init)
{
if (init)
{
mutt_gen_compose_attach_list(actx, actx->email->content, -1, 0);
mutt_attach_init(actx);
menu->data = actx;
}
mutt_update_tree(actx);
menu->max = actx->vcount;
if (menu->max)
{
if (menu->current >= menu->max)
menu->current = menu->max - 1;
}
else
menu->current = 0;
menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
}
/**
* update_idx - Add a new attchment to the message
* @param menu Current menu
* @param actx Attachment context
* @param new Attachment to add
*/
static void update_idx(struct Menu *menu, struct AttachCtx *actx, struct AttachPtr *new)
{
new->level = (actx->idxlen > 0) ? actx->idx[actx->idxlen - 1]->level : 0;
if (actx->idxlen)
actx->idx[actx->idxlen - 1]->content->next = new->content;
new->content->aptr = new;
mutt_actx_add_attach(actx, new);
mutt_update_compose_menu(actx, menu, false);
menu->current = actx->vcount - 1;
}
/**
* struct ComposeRedrawData - Keep track when the compose screen needs redrawing
*/
struct ComposeRedrawData
{
struct Email *email;
char *fcc;
};
static void compose_status_line(char *buf, size_t buflen, size_t col, int cols,
struct Menu *menu, const char *p);
/**
* compose_custom_redraw - Redraw the compose menu - Implements Menu::menu_custom_redraw()
*/
static void compose_custom_redraw(struct Menu *menu)
{
struct ComposeRedrawData *rd = menu->redraw_data;
if (!rd)
return;
if (menu->redraw & REDRAW_FULL)
{
menu_redraw_full(menu);
draw_envelope(rd->email, rd->fcc);
menu->offset = HDR_ATTACH;
menu->pagelen = MuttIndexWindow->rows - HDR_ATTACH;
}
menu_check_recenter(menu);
if (menu->redraw & REDRAW_STATUS)
{
char buf[1024];
compose_status_line(buf, sizeof(buf), 0, MuttStatusWindow->cols, menu,
NONULL(C_ComposeFormat));
mutt_window_move(MuttStatusWindow, 0, 0);
SETCOLOR(MT_COLOR_STATUS);
mutt_paddstr(MuttStatusWindow->cols, buf);
NORMAL_COLOR;
menu->redraw &= ~REDRAW_STATUS;
}
#ifdef USE_SIDEBAR
if (menu->redraw & REDRAW_SIDEBAR)
menu_redraw_sidebar(menu);
#endif
if (menu->redraw & REDRAW_INDEX)
menu_redraw_index(menu);
else if (menu->redraw & (REDRAW_MOTION | REDRAW_MOTION_RESYNC))
menu_redraw_motion(menu);
else if (menu->redraw == REDRAW_CURRENT)
menu_redraw_current(menu);
}
/**
* compose_attach_swap - Swap two adjacent entries in the attachment list
* @param[in] msg Body of email
* @param[out] idx Array of Attachments
* @param[in] first Index of first attachment to swap
*/
static void compose_attach_swap(struct Body *msg, struct AttachPtr **idx, short first)
{
/* Reorder Body pointers.
* Must traverse msg from top since Body has no previous ptr. */
for (struct Body *part = msg; part; part = part->next)
{
if (part->next == idx[first]->content)
{
idx[first]->content->next = idx[first + 1]->content->next;
idx[first + 1]->content->next = idx[first]->content;
part->next = idx[first + 1]->content;
break;
}
}
/* Reorder index */
struct AttachPtr *saved = idx[first];
idx[first] = idx[first + 1];
idx[first + 1] = saved;
/* Swap ptr->num */
int i = idx[first]->num;
idx[first]->num = idx[first + 1]->num;
idx[first + 1]->num = i;
}
/**
* cum_attachs_size - Cumulative Attachments Size
* @param menu Menu listing attachments
* @retval num Bytes in attachments
*
* Returns the total number of bytes used by the attachments in the attachment
* list _after_ content-transfer-encodings have been applied.
*/
static unsigned long cum_attachs_size(struct Menu *menu)
{
size_t s = 0;
struct AttachCtx *actx = menu->data;
struct AttachPtr **idx = actx->idx;
struct Content *info = NULL;
struct Body *b = NULL;
for (unsigned short i = 0; i < actx->idxlen; i++)
{
b = idx[i]->content;
if (!b->content)
b->content = mutt_get_content_info(b->filename, b);
info = b->content;
if (info)
{
switch (b->encoding)
{
case ENC_QUOTED_PRINTABLE:
s += 3 * (info->lobin + info->hibin) + info->ascii + info->crlf;
break;
case ENC_BASE64:
s += (4 * (info->lobin + info->hibin + info->ascii + info->crlf)) / 3;
break;
default:
s += info->lobin + info->hibin + info->ascii + info->crlf;
break;
}
}
}
return s;
}
/**
* compose_format_str - Create the status bar string for compose mode - Implements ::format_t
*
* | Expando | Description
* |:--------|:--------------------------------------------------------
* | \%a | Total number of attachments
* | \%h | Local hostname
* | \%l | Approximate size (in bytes) of the current message
* | \%v | NeoMutt version string
*/
static const char *compose_format_str(char *buf, size_t buflen, size_t col, int cols,
char op, const char *src, const char *prec,
const char *if_str, const char *else_str,
unsigned long data, MuttFormatFlags flags)
{
char fmt[128], tmp[128];
int optional = (flags & MUTT_FORMAT_OPTIONAL);
struct Menu *menu = (struct Menu *) data;
*buf = '\0';
switch (op)
{
case 'a': /* total number of attachments */
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt, menu->max);
break;
case 'h': /* hostname */
snprintf(fmt, sizeof(fmt), "%%%ss", prec);
snprintf(buf, buflen, fmt, NONULL(ShortHostname));
break;
case 'l': /* approx length of current message in bytes */
snprintf(fmt, sizeof(fmt), "%%%ss", prec);
mutt_str_pretty_size(tmp, sizeof(tmp), menu ? cum_attachs_size(menu) : 0);
snprintf(buf, buflen, fmt, tmp);
break;
case 'v':
snprintf(buf, buflen, "%s", mutt_make_version());
break;
case 0:
*buf = '\0';
return src;
default:
snprintf(buf, buflen, "%%%s%c", prec, op);
break;
}
if (optional)
compose_status_line(buf, buflen, col, cols, menu, if_str);
else if (flags & MUTT_FORMAT_OPTIONAL)
compose_status_line(buf, buflen, col, cols, menu, else_str);
return src;
}
/**
* compose_status_line - Compose the string for the status bar
* @param[out] buf Buffer in which to save string
* @param[in] buflen Buffer length
* @param[in] col Starting column
* @param[in] cols Number of screen columns
* @param[in] menu Current menu
* @param[in] src Printf-like format string
*/
static void compose_status_line(char *buf, size_t buflen, size_t col, int cols,
struct Menu *menu, const char *src)
{
mutt_expando_format(buf, buflen, col, cols, src, compose_format_str,
(unsigned long) menu, 0);
}
/**
* mutt_compose_menu - Allow the user to edit the message envelope
* @param msg Message to fill
* @param fcc Buffer to save FCC
* @param fcclen Length of FCC buffer
* @param cur Current message
* @param flags Flags, e.g. #MUTT_COMPOSE_NOFREEHEADER
* @retval 1 Message should be postponed
* @retval 0 Normal exit
* @retval -1 Abort message
*/
int mutt_compose_menu(struct Email *msg, char *fcc, size_t fcclen, struct Email *cur, int flags)
{
char helpstr[1024]; // This isn't copied by the help bar
char buf[PATH_MAX];
int op_close = OP_NULL;
int rc = -1;
bool loop = true;
bool fcc_set = false; /* has the user edited the Fcc: field ? */
struct ComposeRedrawData rd;
#ifdef USE_NNTP
bool news = OptNewsSend; /* is it a news article ? */
#endif
init_header_padding();
rd.email = msg;
rd.fcc = fcc;
struct Menu *menu = mutt_menu_new(MENU_COMPOSE);
menu->offset = HDR_ATTACH;
menu->menu_make_entry = snd_make_entry;
menu->menu_tag = attach_tag;
#ifdef USE_NNTP
if (news)
menu->help = mutt_compile_help(helpstr, sizeof(helpstr), MENU_COMPOSE, ComposeNewsHelp);
else
#endif
menu->help = mutt_compile_help(helpstr, sizeof(helpstr), MENU_COMPOSE, ComposeHelp);
menu->menu_custom_redraw = compose_custom_redraw;
menu->redraw_data = &rd;
mutt_menu_push_current(menu);
struct AttachCtx *actx = mutt_mem_calloc(sizeof(struct AttachCtx), 1);
actx->email = msg;
mutt_update_compose_menu(actx, menu, true);
while (loop)
{
#ifdef USE_NNTP
OptNews = false; /* for any case */
#endif
const int op = mutt_menu_loop(menu);
switch (op)
{
case OP_COMPOSE_EDIT_FROM:
edit_address_list(HDR_FROM, &msg->env->from);
mutt_message_hook(NULL, msg, MUTT_SEND2_HOOK);
break;
case OP_COMPOSE_EDIT_TO:
#ifdef USE_NNTP
if (news)
break;
#endif
edit_address_list(HDR_TO, &msg->env->to);
if (C_CryptOpportunisticEncrypt)
{
crypt_opportunistic_encrypt(msg);
redraw_crypt_lines(msg);
}
mutt_message_hook(NULL, msg, MUTT_SEND2_HOOK);
break;
case OP_COMPOSE_EDIT_BCC:
#ifdef USE_NNTP
if (news)
break;
#endif
edit_address_list(HDR_BCC, &msg->env->bcc);
if (C_CryptOpportunisticEncrypt)
{
crypt_opportunistic_encrypt(msg);
redraw_crypt_lines(msg);
}
mutt_message_hook(NULL, msg, MUTT_SEND2_HOOK);
break;
case OP_COMPOSE_EDIT_CC:
#ifdef USE_NNTP
if (news)
break;
#endif
edit_address_list(HDR_CC, &msg->env->cc);
if (C_CryptOpportunisticEncrypt)
{
crypt_opportunistic_encrypt(msg);
redraw_crypt_lines(msg);
}
mutt_message_hook(NULL, msg, MUTT_SEND2_HOOK);
break;
#ifdef USE_NNTP
case OP_COMPOSE_EDIT_NEWSGROUPS:
if (!news)
break;
if (msg->env->newsgroups)
mutt_str_strfcpy(buf, msg->env->newsgroups, sizeof(buf));
else
buf[0] = '\0';
if (mutt_get_field("Newsgroups: ", buf, sizeof(buf), 0) == 0)
{
mutt_str_replace(&msg->env->newsgroups, buf);
mutt_window_move(MuttIndexWindow, HDR_TO, HDR_XOFFSET);
if (msg->env->newsgroups)
mutt_paddstr(W, msg->env->newsgroups);
else
clrtoeol();
}
break;
case OP_COMPOSE_EDIT_FOLLOWUP_TO:
if (!news)
break;
if (msg->env->followup_to)
mutt_str_strfcpy(buf, msg->env->followup_to, sizeof(buf));
else
buf[0] = '\0';
if (mutt_get_field("Followup-To: ", buf, sizeof(buf), 0) == 0)
{