forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompose.c
2559 lines (2272 loc) · 74.4 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 <[email protected]>
* Copyright (C) 2004 g10 Code GmbH
* Copyright (C) 2019 Pietro Cerutti <[email protected]>
* Copyright (C) 2020 Richard Russon <[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 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/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "alias/lib.h"
#include "conn/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "compose.h"
#include "ncrypt/lib.h"
#include "send/lib.h"
#include "browser.h"
#include "commands.h"
#include "context.h"
#include "format_flags.h"
#include "hook.h"
#include "index.h"
#include "init.h"
#include "keymap.h"
#include "mutt_attach.h"
#include "mutt_globals.h"
#include "mutt_header.h"
#include "mutt_logging.h"
#include "mutt_menu.h"
#include "muttlib.h"
#include "mx.h"
#include "opcodes.h"
#include "options.h"
#include "protos.h"
#include "recvattach.h"
#include "rfc3676.h"
#include "sort.h"
#ifdef ENABLE_NLS
#include <libintl.h>
#endif
#ifdef MIXMASTER
#include "remailer.h"
#endif
#ifdef USE_NNTP
#include "nntp/lib.h"
#endif
#ifdef USE_POP
#include "pop/lib.h"
#endif
#ifdef USE_IMAP
#include "imap/lib.h"
#endif
#ifdef USE_AUTOCRYPT
#include "autocrypt/lib.h"
#endif
/// Maximum number of rows to use for the To:, Cc:, Bcc: fields
#define MAX_ADDR_ROWS 5
/* 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");
static void compose_status_line(char *buf, size_t buflen, size_t col, int cols,
struct Menu *menu, const char *p);
/**
* struct ComposeRedrawData - Keep track when the compose screen needs redrawing
*/
struct ComposeRedrawData
{
struct Email *email;
struct Buffer *fcc;
struct ListHead to_list;
struct ListHead cc_list;
struct ListHead bcc_list;
short to_rows;
short cc_rows;
short bcc_rows;
short sec_rows;
#ifdef USE_AUTOCRYPT
enum AutocryptRec autocrypt_rec;
int autocrypt_rec_override;
#endif
struct MuttWindow *win_envelope; ///< Envelope: From, To, etc
struct MuttWindow *win_abar; ///< Attachments label
struct MuttWindow *win_attach; ///< List of Attachments
struct MuttWindow *win_cbar; ///< Compose bar
};
#define CHECK_COUNT \
if (actx->idxlen == 0) \
{ \
mutt_error(_(There_are_no_attachments)); \
break; \
}
#define CUR_ATTACH 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, ///< "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_AUTOCRYPT
HDR_AUTOCRYPT, ///< "Autocrypt:" and "Recommendation:" fields
#endif
#ifdef USE_NNTP
HDR_NEWSGROUPS, ///< "Newsgroups:" field
HDR_FOLLOWUPTO, ///< "Followup-To:" field
HDR_XCOMMENTTO, ///< "X-Comment-To:" field
#endif
HDR_ATTACH_TITLE, ///< The "-- Attachments" line
};
int HeaderPadding[HDR_ATTACH_TITLE] = { 0 };
int MaxHeaderWidth = 0;
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_AUTOCRYPT
// L10N: The compose menu autocrypt line
N_("Autocrypt: "),
#endif
#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
};
/// Help Bar for the Compose dialog
static const struct Mapping ComposeHelp[] = {
// clang-format off
{ 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 },
// clang-format on
};
#ifdef USE_NNTP
/// Help Bar for the News Compose dialog
static const struct Mapping ComposeNewsHelp[] = {
// clang-format off
{ 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 },
// clang-format on
};
#endif
#ifdef USE_AUTOCRYPT
static const char *AutocryptRecUiFlags[] = {
/* L10N: Autocrypt recommendation flag: off.
* This is displayed when Autocrypt is turned off. */
N_("Off"),
/* L10N: Autocrypt recommendation flag: no.
* This is displayed when Autocrypt cannot encrypt to the recipients. */
N_("No"),
/* L10N: Autocrypt recommendation flag: discouraged.
* This is displayed when Autocrypt believes encryption should not be used.
* This might occur if one of the recipient Autocrypt Keys has not been
* used recently, or if the only key available is a Gossip Header key. */
N_("Discouraged"),
/* L10N: Autocrypt recommendation flag: available.
* This is displayed when Autocrypt believes encryption is possible, but
* leaves enabling it up to the sender. Probably because "prefer encrypt"
* is not set in both the sender and recipient keys. */
N_("Available"),
/* L10N: Autocrypt recommendation flag: yes.
* This is displayed when Autocrypt would normally enable encryption
* automatically. */
N_("Yes"),
};
#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_len(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_ATTACH_TITLE; i++)
{
if (i == HDR_CRYPTINFO)
continue;
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_ATTACH_TITLE; i++)
{
HeaderPadding[i] += MaxHeaderWidth;
if (HeaderPadding[i] < 0)
HeaderPadding[i] = 0;
}
}
/**
* snd_make_entry - Format a menu item for the attachment list - Implements Menu::make_entry()
*/
static void snd_make_entry(char *buf, size_t buflen, struct Menu *menu, int line)
{
struct AttachCtx *actx = menu->mdata;
mutt_expando_format(buf, buflen, 0, menu->win_index->state.cols, NONULL(C_AttachFormat),
attach_format_str, (intptr_t)(actx->idx[actx->v2r[line]]),
MUTT_FORMAT_STAT_FILE | MUTT_FORMAT_ARROWCURSOR);
}
#ifdef USE_AUTOCRYPT
/**
* autocrypt_compose_menu - Autocrypt compose settings
* @param e Email
*/
static void autocrypt_compose_menu(struct Email *e)
{
/* L10N: The compose menu autocrypt prompt.
(e)ncrypt enables encryption via autocrypt.
(c)lear sets cleartext.
(a)utomatic defers to the recommendation. */
const char *prompt = _("Autocrypt: (e)ncrypt, (c)lear, (a)utomatic?");
e->security |= APPLICATION_PGP;
/* L10N: The letter corresponding to the compose menu autocrypt prompt
(e)ncrypt, (c)lear, (a)utomatic */
const char *letters = _("eca");
int choice = mutt_multi_choice(prompt, letters);
switch (choice)
{
case 1:
e->security |= (SEC_AUTOCRYPT | SEC_AUTOCRYPT_OVERRIDE);
e->security &= ~(SEC_ENCRYPT | SEC_SIGN | SEC_OPPENCRYPT | SEC_INLINE);
break;
case 2:
e->security &= ~SEC_AUTOCRYPT;
e->security |= SEC_AUTOCRYPT_OVERRIDE;
break;
case 3:
e->security &= ~SEC_AUTOCRYPT_OVERRIDE;
if (C_CryptOpportunisticEncrypt)
e->security |= SEC_OPPENCRYPT;
break;
}
}
#endif
/**
* draw_floating - Draw a floating label
* @param win Window to draw on
* @param col Column to draw at
* @param row Row to draw at
* @param text Text to display
*/
static void draw_floating(struct MuttWindow *win, int col, int row, const char *text)
{
mutt_curses_set_color(MT_COLOR_COMPOSE_HEADER);
mutt_window_mvprintw(win, col, row, "%s", text);
mutt_curses_set_color(MT_COLOR_NORMAL);
}
/**
* draw_header - Draw an aligned label
* @param win Window to draw on
* @param row Row to draw at
* @param field Field to display, e.g. #HDR_FROM
*/
static void draw_header(struct MuttWindow *win, int row, enum HeaderField field)
{
mutt_curses_set_color(MT_COLOR_COMPOSE_HEADER);
mutt_window_mvprintw(win, 0, row, "%*s", HeaderPadding[field], _(Prompts[field]));
mutt_curses_set_color(MT_COLOR_NORMAL);
}
/**
* calc_address - Calculate how many rows an AddressList will need
* @param[in] al Address List
* @param[out] slist String list
* @param[in] cols Screen columns available
* @param[out] srows Rows needed
* @retval num Rows needed
*
* @note Number of rows is capped at #MAX_ADDR_ROWS
*/
static int calc_address(struct AddressList *al, struct ListHead *slist,
short cols, short *srows)
{
mutt_list_free(slist);
mutt_addrlist_write_list(al, slist);
int rows = 1;
int addr_len;
int width_left = cols;
struct ListNode *next = NULL;
struct ListNode *np = NULL;
STAILQ_FOREACH(np, slist, entries)
{
next = STAILQ_NEXT(np, entries);
addr_len = mutt_strwidth(np->data);
if (next)
addr_len += 2; // ", "
try_again:
if (addr_len >= width_left)
{
if (width_left == cols)
break;
rows++;
width_left = cols;
goto try_again;
}
if (addr_len < width_left)
width_left -= addr_len;
}
*srows = MIN(rows, MAX_ADDR_ROWS);
return *srows;
}
/**
* calc_security - Calculate how many rows the security info will need
* @param e Email
* @param rows Rows needed
* @retval num Rows needed
*/
static int calc_security(struct Email *e, short *rows)
{
if ((WithCrypto & (APPLICATION_PGP | APPLICATION_SMIME)) == 0)
*rows = 0; // Neither PGP nor SMIME are built into NeoMutt
else if ((e->security & (SEC_ENCRYPT | SEC_SIGN)) != 0)
*rows = 2; // 'Security:' and 'Sign as:'
else
*rows = 1; // Just 'Security:'
#ifdef USE_AUTOCRYPT
if (C_Autocrypt)
*rows += 1;
#endif
return *rows;
}
/**
* calc_envelope - Calculate how many rows the envelope will need
* @param rd Email and other compose data
* @retval num Rows needed
*/
static int calc_envelope(struct ComposeRedrawData *rd)
{
int rows = 4; // 'From:', 'Subject:', 'Reply-To:', 'Fcc:'
#ifdef MIXMASTER
rows++;
#endif
struct Email *e = rd->email;
struct Envelope *env = e->env;
const int cols = rd->win_envelope->state.cols - MaxHeaderWidth;
#ifdef USE_NNTP
if (OptNewsSend)
{
rows += 2; // 'Newsgroups:' and 'Followup-To:'
if (C_XCommentTo)
rows++;
}
else
#endif
{
rows += calc_address(&env->to, &rd->to_list, cols, &rd->to_rows);
rows += calc_address(&env->cc, &rd->cc_list, cols, &rd->cc_rows);
rows += calc_address(&env->bcc, &rd->bcc_list, cols, &rd->bcc_rows);
}
rows += calc_security(e, &rd->sec_rows);
return rows;
}
/**
* redraw_crypt_lines - Update the encryption info in the compose window
* @param rd Email and other compose data
* @param row Window row to start drawing
*/
static int redraw_crypt_lines(struct ComposeRedrawData *rd, int row)
{
struct Email *e = rd->email;
draw_header(rd->win_envelope, row++, HDR_CRYPT);
if ((WithCrypto & (APPLICATION_PGP | APPLICATION_SMIME)) == 0)
return 0;
// We'll probably need two lines for 'Security:' and 'Sign as:'
int used = 2;
if ((e->security & (SEC_ENCRYPT | SEC_SIGN)) == (SEC_ENCRYPT | SEC_SIGN))
{
mutt_curses_set_color(MT_COLOR_COMPOSE_SECURITY_BOTH);
mutt_window_addstr(_("Sign, Encrypt"));
}
else if (e->security & SEC_ENCRYPT)
{
mutt_curses_set_color(MT_COLOR_COMPOSE_SECURITY_ENCRYPT);
mutt_window_addstr(_("Encrypt"));
}
else if (e->security & SEC_SIGN)
{
mutt_curses_set_color(MT_COLOR_COMPOSE_SECURITY_SIGN);
mutt_window_addstr(_("Sign"));
}
else
{
/* L10N: This refers to the encryption of the email, e.g. "Security: None" */
mutt_curses_set_color(MT_COLOR_COMPOSE_SECURITY_NONE);
mutt_window_addstr(_("None"));
used = 1; // 'Sign as:' won't be needed
}
mutt_curses_set_color(MT_COLOR_NORMAL);
if ((e->security & (SEC_ENCRYPT | SEC_SIGN)))
{
if (((WithCrypto & APPLICATION_PGP) != 0) && (e->security & APPLICATION_PGP))
{
if ((e->security & SEC_INLINE))
mutt_window_addstr(_(" (inline PGP)"));
else
mutt_window_addstr(_(" (PGP/MIME)"));
}
else if (((WithCrypto & APPLICATION_SMIME) != 0) && (e->security & APPLICATION_SMIME))
mutt_window_addstr(_(" (S/MIME)"));
}
if (C_CryptOpportunisticEncrypt && (e->security & SEC_OPPENCRYPT))
mutt_window_addstr(_(" (OppEnc mode)"));
mutt_window_clrtoeol(rd->win_envelope);
if (((WithCrypto & APPLICATION_PGP) != 0) &&
(e->security & APPLICATION_PGP) && (e->security & SEC_SIGN))
{
draw_header(rd->win_envelope, row++, HDR_CRYPTINFO);
mutt_window_printf("%s", C_PgpSignAs ? C_PgpSignAs : _("<default>"));
}
if (((WithCrypto & APPLICATION_SMIME) != 0) &&
(e->security & APPLICATION_SMIME) && (e->security & SEC_SIGN))
{
draw_header(rd->win_envelope, row++, HDR_CRYPTINFO);
mutt_window_printf("%s", C_SmimeSignAs ? C_SmimeSignAs : _("<default>"));
}
if (((WithCrypto & APPLICATION_SMIME) != 0) && (e->security & APPLICATION_SMIME) &&
(e->security & SEC_ENCRYPT) && C_SmimeEncryptWith)
{
draw_floating(rd->win_envelope, 40, row - 1, _("Encrypt with: "));
mutt_window_printf("%s", NONULL(C_SmimeEncryptWith));
}
#ifdef USE_AUTOCRYPT
if (C_Autocrypt)
{
draw_header(rd->win_envelope, row, HDR_AUTOCRYPT);
if (e->security & SEC_AUTOCRYPT)
{
mutt_curses_set_color(MT_COLOR_COMPOSE_SECURITY_ENCRYPT);
mutt_window_addstr(_("Encrypt"));
}
else
{
mutt_curses_set_color(MT_COLOR_COMPOSE_SECURITY_NONE);
mutt_window_addstr(_("Off"));
}
/* L10N: The autocrypt compose menu Recommendation field.
Displays the output of the recommendation engine
(Off, No, Discouraged, Available, Yes) */
draw_floating(rd->win_envelope, 40, row, _("Recommendation: "));
mutt_window_printf("%s", _(AutocryptRecUiFlags[rd->autocrypt_rec]));
used++;
}
#endif
return used;
}
/**
* update_crypt_info - Update the crypto info
* @param rd Email and other compose data
*/
static void update_crypt_info(struct ComposeRedrawData *rd)
{
struct Email *e = rd->email;
if (C_CryptOpportunisticEncrypt)
crypt_opportunistic_encrypt(e);
#ifdef USE_AUTOCRYPT
if (C_Autocrypt)
{
rd->autocrypt_rec = mutt_autocrypt_ui_recommendation(e, NULL);
/* Anything that enables SEC_ENCRYPT or SEC_SIGN, or turns on SMIME
* overrides autocrypt, be it oppenc or the user having turned on
* those flags manually. */
if (e->security & (SEC_ENCRYPT | SEC_SIGN | APPLICATION_SMIME))
e->security &= ~(SEC_AUTOCRYPT | SEC_AUTOCRYPT_OVERRIDE);
else
{
if (!(e->security & SEC_AUTOCRYPT_OVERRIDE))
{
if (rd->autocrypt_rec == AUTOCRYPT_REC_YES)
{
e->security |= (SEC_AUTOCRYPT | APPLICATION_PGP);
e->security &= ~(SEC_INLINE | APPLICATION_SMIME);
}
else
e->security &= ~SEC_AUTOCRYPT;
}
}
}
#endif
}
#ifdef MIXMASTER
/**
* redraw_mix_line - Redraw the Mixmaster chain
* @param chain List of chain links
* @param rd Email and other compose data
* @param row Window row to start drawing
*/
static void redraw_mix_line(struct ListHead *chain, struct ComposeRedrawData *rd, int row)
{
char *t = NULL;
draw_header(rd->win_envelope, row, HDR_MIX);
if (STAILQ_EMPTY(chain))
{
mutt_window_addstr(_("<no chain defined>"));
mutt_window_clrtoeol(rd->win_envelope);
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_len(t) + 2 >= rd->win_envelope->state.cols)
break;
mutt_window_addstr(NONULL(t));
if (STAILQ_NEXT(np, entries))
mutt_window_addstr(", ");
c += mutt_str_len(t) + 2;
}
}
#endif
/**
* 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)
{
int rc = -1;
struct stat st;
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 = mutt_buffer_pool_get();
mutt_buffer_strcpy(pretty, actx->idx[i]->body->filename);
mutt_buffer_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, mutt_b2s(pretty));
goto cleanup;
}
if (actx->idx[i]->body->stamp < st.st_mtime)
{
if (!pretty)
pretty = mutt_buffer_pool_get();
mutt_buffer_strcpy(pretty, actx->idx[i]->body->filename);
mutt_buffer_pretty_mailbox(pretty);
if (!msg)
msg = mutt_buffer_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. */
mutt_buffer_printf(msg, _("Attachment #%d modified. Update encoding for %s?"),
i + 1, mutt_b2s(pretty));
enum QuadOption ans = mutt_yesorno(mutt_b2s(msg), MUTT_YES);
if (ans == MUTT_YES)
mutt_update_encoding(actx->idx[i]->body, NeoMutt->sub);
else if (ans == MUTT_ABORT)
goto cleanup;
}
}
rc = 0;
cleanup:
mutt_buffer_pool_release(&pretty);
mutt_buffer_pool_release(&msg);
return rc;
}
/**
* draw_envelope_addr - Write addresses to the compose window
* @param field Field to display, e.g. #HDR_FROM
* @param al Address list to write
* @param win Window
* @param row Window row to start drawing
* @param max_lines How many lines may be used
*/
static int draw_envelope_addr(int field, struct AddressList *al,
struct MuttWindow *win, int row, size_t max_lines)
{
draw_header(win, row, field);
struct ListHead list = STAILQ_HEAD_INITIALIZER(list);
int count = mutt_addrlist_write_list(al, &list);
int lines_used = 1;
int width_left = win->state.cols - MaxHeaderWidth;
char more[32];
int more_len = 0;
char *sep = NULL;
struct ListNode *next = NULL;
struct ListNode *np = NULL;
STAILQ_FOREACH(np, &list, entries)
{
next = STAILQ_NEXT(np, entries);
int addr_len = mutt_strwidth(np->data);
if (next)
{
sep = ", ";
addr_len += 2;
}
else
{
sep = "";
}
count--;
try_again:
more_len = snprintf(more, sizeof(more),
ngettext("(+%d more)", "(+%d more)", count), count);
mutt_debug(LL_DEBUG3, "text: '%s' len: %d\n", more, more_len);
int reserve = ((count > 0) && (lines_used == max_lines)) ? more_len : 0;
mutt_debug(LL_DEBUG3, "processing: %s (al:%ld, wl:%d, r:%d, lu:%ld)\n",
np->data, addr_len, width_left, reserve, lines_used);
if (addr_len >= (width_left - reserve))
{
mutt_debug(LL_DEBUG3, "not enough space\n");
if (lines_used == max_lines)
{
mutt_debug(LL_DEBUG3, "no more lines\n");
mutt_debug(LL_DEBUG3, "truncating: %s\n", np->data);
mutt_paddstr(width_left, np->data);
break;
}
if (width_left == (win->state.cols - MaxHeaderWidth))
{
mutt_debug(LL_DEBUG3, "couldn't print: %s\n", np->data);
mutt_paddstr(width_left, np->data);
break;
}
mutt_debug(LL_DEBUG3, "start a new line\n");
mutt_window_clrtoeol(win);
row++;
lines_used++;
width_left = win->state.cols - MaxHeaderWidth;
mutt_window_move(win, MaxHeaderWidth, row);
goto try_again;
}
if (addr_len < width_left)
{
mutt_debug(LL_DEBUG3, "space for: %s\n", np->data);
mutt_window_addstr(np->data);
mutt_window_addstr(sep);
width_left -= addr_len;
}
mutt_debug(LL_DEBUG3, "%ld addresses remaining\n", count);
mutt_debug(LL_DEBUG3, "%ld lines remaining\n", max_lines - lines_used);
}
mutt_list_free(&list);
if (count > 0)
{
mutt_window_move(win, win->state.cols - more_len, row);
mutt_curses_set_color(MT_COLOR_BOLD);
mutt_window_addstr(more);
mutt_curses_set_color(MT_COLOR_NORMAL);
mutt_debug(LL_DEBUG3, "%ld more (len %d)\n", count, more_len);
}
else
{
mutt_window_clrtoeol(win);
}
for (int i = lines_used; i < max_lines; i++)
{
mutt_window_move(win, 0, row + i);
mutt_window_clrtoeol(win);
}
mutt_debug(LL_DEBUG3, "used %d lines\n", lines_used);
return lines_used;
}
/**
* draw_envelope - Write the email headers to the compose window
* @param rd Email and other compose data
*/
static void draw_envelope(struct ComposeRedrawData *rd)
{
struct Email *e = rd->email;
const char *fcc = mutt_b2s(rd->fcc);
const int cols = rd->win_envelope->state.cols - MaxHeaderWidth;
int row = draw_envelope_addr(HDR_FROM, &e->env->from, rd->win_envelope, 0, 1);
#ifdef USE_NNTP
if (OptNewsSend)
{
draw_header(rd->win_envelope, row++, HDR_NEWSGROUPS);
mutt_paddstr(cols, NONULL(e->env->newsgroups));
draw_header(rd->win_envelope, row++, HDR_FOLLOWUPTO);
mutt_paddstr(cols, NONULL(e->env->followup_to));
if (C_XCommentTo)
{
draw_header(rd->win_envelope, row++, HDR_XCOMMENTTO);
mutt_paddstr(cols, NONULL(e->env->x_comment_to));
}
}
else
#endif
{
row += draw_envelope_addr(HDR_TO, &e->env->to, rd->win_envelope, row, rd->to_rows);
row += draw_envelope_addr(HDR_CC, &e->env->cc, rd->win_envelope, row, rd->cc_rows);
row += draw_envelope_addr(HDR_BCC, &e->env->bcc, rd->win_envelope, row, rd->bcc_rows);
}
draw_header(rd->win_envelope, row++, HDR_SUBJECT);
mutt_paddstr(cols, NONULL(e->env->subject));
row += draw_envelope_addr(HDR_REPLYTO, &e->env->reply_to, rd->win_envelope, row, 1);
draw_header(rd->win_envelope, row++, HDR_FCC);
mutt_paddstr(cols, fcc);
if (WithCrypto)
row += redraw_crypt_lines(rd, row);
#ifdef MIXMASTER
redraw_mix_line(&e->chain, rd, row++);
#endif
mutt_curses_set_color(MT_COLOR_STATUS);
mutt_window_mvaddstr(rd->win_abar, 0, 0, _("-- Attachments"));
mutt_window_clrtoeol(rd->win_abar);
mutt_curses_set_color(MT_COLOR_NORMAL);
}
/**
* edit_address_list - Let the user edit the address list
* @param[in] field Field to edit, e.g. #HDR_FROM
* @param[in,out] al AddressList to edit
*/
static void edit_address_list(int field, struct AddressList *al)
{
char buf[8192] = { 0 }; /* needs to be large for alias expansion */
mutt_addrlist_to_local(al);
mutt_addrlist_write(al, buf, sizeof(buf), false);
if (mutt_get_field(_(Prompts[field]), buf, sizeof(buf), MUTT_ALIAS) == 0)
{
mutt_addrlist_clear(al);
mutt_addrlist_parse2(al, buf);
mutt_expand_aliases(al);
}
char *err = NULL;
if (mutt_addrlist_to_intl(al, &err) != 0)
{
mutt_error(_("Bad IDN: '%s'"), err);
mutt_refresh();
FREE(&err);
}
}
/**
* 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]->body->tagged = false;
return -1;
}
for (int y = 0; y < actx->idxlen; y++)
{
if (idx[y]->body->next == idx[rindex]->body)
{
idx[y]->body->next = idx[rindex]->body->next;
break;
}
}
idx[rindex]->body->next = NULL;
/* mutt_make_message_attach() creates body->parts, shared by
* body->email->body. If we NULL out that, it creates a memory
* leak because mutt_free_body() frees body->parts, not
* body->email->body.
*
* Other mutt_send_message() message constructors are careful to free
* any body->parts, removing depth:
* - mutt_prepare_template() used by postponed, resent, and draft files
* - mutt_copy_body() used by the recvattach menu and $forward_attachments.
*
* I believe it is safe to completely remove the "body->parts =
* NULL" statement. But for safety, am doing so only for the case
* it must be avoided: message attachments.
*/
if (!idx[rindex]->body->email)
idx[rindex]->body->parts = NULL;
mutt_body_free(&(idx[rindex]->body));
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--;