forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compose.c
1668 lines (1478 loc) · 48 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
* 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>
#ifdef ENABLE_NLS
#include <libintl.h>
#endif
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mutt.h"
#include "alias.h"
#include "attach.h"
#include "body.h"
#include "content.h"
#include "context.h"
#include "envelope.h"
#include "format_flags.h"
#include "globals.h"
#include "header.h"
#include "keymap.h"
#include "lib/lib.h"
#include "list.h"
#include "mailbox.h"
#include "mapping.h"
#include "mime.h"
#include "mutt_curses.h"
#include "mutt_idna.h"
#include "mutt_menu.h"
#include "mutt_socket.h"
#include "mx.h"
#include "ncrypt/ncrypt.h"
#include "opcodes.h"
#include "options.h"
#include "protos.h"
#include "rfc1524.h"
#include "rfc822.h"
#include "sort.h"
#ifdef MIXMASTER
#include "remailer.h"
#endif
#ifdef USE_NNTP
#include "nntp.h"
#endif
struct Address;
static const char *There_are_no_attachments = N_("There are no attachments.");
#define CHECK_COUNT \
if (idxlen == 0) \
{ \
mutt_error(_(There_are_no_attachments)); \
break; \
}
/**
* enum HeaderField - Ordered list of headers for the compose screen
*/
enum HeaderField
{
HDR_FROM = 0,
HDR_TO,
HDR_CC,
HDR_BCC,
HDR_SUBJECT,
HDR_REPLYTO,
HDR_FCC,
#ifdef MIXMASTER
HDR_MIX,
#endif
HDR_CRYPT,
HDR_CRYPTINFO,
#ifdef USE_NNTP
HDR_NEWSGROUPS,
HDR_FOLLOWUPTO,
HDR_XCOMMENTTO,
#endif
HDR_ATTACH = (HDR_FCC + 5) /* where 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
static void calc_header_width_padding(int idx, const char *header, int calc_max)
{
int width;
HeaderPadding[idx] = mutt_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 short done = 0;
int i;
if (done)
return;
done = 1;
for (i = 0; i <= HDR_XCOMMENTTO; i++)
calc_header_width_padding(i, _(Prompts[i]), 1);
/* 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]), 0);
for (i = 0; i <= HDR_XCOMMENTTO; i++)
{
HeaderPadding[i] += MaxHeaderWidth;
if (HeaderPadding[i] < 0)
HeaderPadding[i] = 0;
}
}
static void snd_entry(char *b, size_t blen, struct Menu *menu, int num)
{
mutt_expando_format(b, blen, 0, MuttIndexWindow->cols, NONULL(AttachFormat), mutt_attach_fmt,
(unsigned long) (((struct AttachPtr **) menu->data)[num]),
MUTT_FORMAT_STAT_FILE | MUTT_FORMAT_ARROWCURSOR);
}
static void redraw_crypt_lines(struct Header *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 & (ENCRYPT | SIGN)) == (ENCRYPT | SIGN))
{
SETCOLOR(MT_COLOR_COMPOSE_SECURITY_BOTH);
addstr(_("Sign, Encrypt"));
}
else if (msg->security & ENCRYPT)
{
SETCOLOR(MT_COLOR_COMPOSE_SECURITY_ENCRYPT);
addstr(_("Encrypt"));
}
else if (msg->security & 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 & (ENCRYPT | SIGN)))
{
if ((WithCrypto & APPLICATION_PGP) && (msg->security & APPLICATION_PGP))
{
if ((msg->security & INLINE))
addstr(_(" (inline PGP)"));
else
addstr(_(" (PGP/MIME)"));
}
else if ((WithCrypto & APPLICATION_SMIME) && (msg->security & APPLICATION_SMIME))
addstr(_(" (S/MIME)"));
}
if (option(OPT_CRYPT_OPPORTUNISTIC_ENCRYPT) && (msg->security & OPPENCRYPT))
addstr(_(" (OppEnc mode)"));
mutt_window_clrtoeol(MuttIndexWindow);
mutt_window_move(MuttIndexWindow, HDR_CRYPTINFO, 0);
mutt_window_clrtoeol(MuttIndexWindow);
if ((WithCrypto & APPLICATION_PGP) && (msg->security & APPLICATION_PGP) &&
(msg->security & SIGN))
{
SETCOLOR(MT_COLOR_COMPOSE_HEADER);
printw("%*s", HeaderPadding[HDR_CRYPTINFO], _(Prompts[HDR_CRYPTINFO]));
NORMAL_COLOR;
printw("%s", PgpSignAs ? PgpSignAs : _("<default>"));
}
if ((WithCrypto & APPLICATION_SMIME) && (msg->security & APPLICATION_SMIME) &&
(msg->security & SIGN))
{
SETCOLOR(MT_COLOR_COMPOSE_HEADER);
printw("%*s", HeaderPadding[HDR_CRYPTINFO], _(Prompts[HDR_CRYPTINFO]));
NORMAL_COLOR;
printw("%s", SmimeDefaultKey ? SmimeDefaultKey : _("<default>"));
}
if ((WithCrypto & APPLICATION_SMIME) && (msg->security & APPLICATION_SMIME) &&
(msg->security & ENCRYPT) && SmimeCryptAlg && *SmimeCryptAlg)
{
SETCOLOR(MT_COLOR_COMPOSE_HEADER);
mutt_window_mvprintw(MuttIndexWindow, HDR_CRYPTINFO, 40, "%s",
_("Encrypt with: "));
NORMAL_COLOR;
printw("%s", NONULL(SmimeCryptAlg));
}
}
#ifdef MIXMASTER
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;
STAILQ_FOREACH(np, chain, entries)
{
t = np->data;
if (t && t[0] == '0' && t[1] == '\0')
t = "<random>";
if (c + mutt_strlen(t) + 2 >= MuttIndexWindow->cols)
break;
addstr(NONULL(t));
if (STAILQ_NEXT(np, entries))
addstr(", ");
c += mutt_strlen(t) + 2;
}
}
#endif /* MIXMASTER */
static int check_attachments(struct AttachPtr **idx, short idxlen)
{
int r;
struct stat st;
char pretty[_POSIX_PATH_MAX], msg[_POSIX_PATH_MAX + SHORT_STRING];
for (int i = 0; i < idxlen; i++)
{
strfcpy(pretty, idx[i]->content->filename, sizeof(pretty));
if (stat(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 (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);
if ((r = mutt_yesorno(msg, MUTT_YES)) == MUTT_YES)
mutt_update_encoding(idx[i]->content);
else if (r == MUTT_ABORT)
return -1;
}
}
return 0;
}
static void draw_envelope_addr(int line, struct Address *addr)
{
char buf[LONG_STRING];
buf[0] = 0;
rfc822_write_address(buf, sizeof(buf), addr, 1);
SETCOLOR(MT_COLOR_COMPOSE_HEADER);
mutt_window_mvprintw(MuttIndexWindow, line, 0, "%*s", HeaderPadding[line],
_(Prompts[line]));
NORMAL_COLOR;
mutt_paddstr(W, buf);
}
static void draw_envelope(struct Header *msg, char *fcc)
{
draw_envelope_addr(HDR_FROM, msg->env->from);
#ifdef USE_NNTP
if (!option(OPT_NEWS_SEND))
{
#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 (option(OPT_XCOMMENT_TO))
{
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;
}
static void edit_address_list(int line, struct Address **addr)
{
char buf[HUGE_STRING] = ""; /* needs to be large for alias expansion */
char *err = NULL;
mutt_addrlist_to_local(*addr);
rfc822_write_address(buf, sizeof(buf), *addr, 0);
if (mutt_get_field(_(Prompts[line]), buf, sizeof(buf), MUTT_ALIAS) == 0)
{
rfc822_free_address(addr);
*addr = mutt_parse_adrlist(*addr, buf);
*addr = mutt_expand_aliases(*addr);
}
if (mutt_addrlist_to_intl(*addr, &err) != 0)
{
mutt_error(_("Warning: '%s' is a bad IDN."), err);
mutt_refresh();
FREE(&err);
}
/* redraw the expanded list so the user can see the result */
buf[0] = 0;
rfc822_write_address(buf, sizeof(buf), *addr, 1);
mutt_window_move(MuttIndexWindow, line, HDR_XOFFSET);
mutt_paddstr(W, buf);
}
static int delete_attachment(struct Menu *menu, short *idxlen, int x)
{
struct AttachPtr **idx = (struct AttachPtr **) menu->data;
menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
if (x == 0 && menu->max == 1)
{
mutt_error(_("You may not delete the only attachment."));
idx[x]->content->tagged = false;
return -1;
}
for (int y = 0; y < *idxlen; y++)
{
if (idx[y]->content->next == idx[x]->content)
{
idx[y]->content->next = idx[x]->content->next;
break;
}
}
idx[x]->content->next = NULL;
idx[x]->content->parts = NULL;
mutt_free_body(&(idx[x]->content));
FREE(&idx[x]->tree);
FREE(&idx[x]);
for (; x < *idxlen - 1; x++)
idx[x] = idx[x + 1];
idx[*idxlen - 1] = NULL;
menu->max = --(*idxlen);
return 0;
}
static void update_idx(struct Menu *menu, struct AttachPtr **idx, short idxlen)
{
idx[idxlen]->level = (idxlen > 0) ? idx[idxlen - 1]->level : 0;
if (idxlen)
idx[idxlen - 1]->content->next = idx[idxlen]->content;
idx[idxlen]->content->aptr = idx[idxlen];
menu->current = idxlen++;
mutt_update_tree(idx, idxlen);
menu->max = idxlen;
return;
}
/**
* struct ComposeRedrawData - Keep track when the compose screen needs redrawing
*/
struct ComposeRedrawData
{
struct Header *msg;
char *fcc;
};
/* prototype for use below */
static void compose_status_line(char *buf, size_t buflen, size_t col, int cols,
struct Menu *menu, const char *p);
static void compose_menu_redraw(struct Menu *menu)
{
char buf[LONG_STRING];
struct ComposeRedrawData *rd = menu->redraw_data;
if (!rd)
return;
if (menu->redraw & REDRAW_FULL)
{
menu_redraw_full(menu);
draw_envelope(rd->msg, rd->fcc);
menu->offset = HDR_ATTACH;
menu->pagelen = MuttIndexWindow->rows - HDR_ATTACH;
}
menu_check_recenter(menu);
if (menu->redraw & REDRAW_STATUS)
{
compose_status_line(buf, sizeof(buf), 0, MuttStatusWindow->cols, menu,
NONULL(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_RESYNCH))
menu_redraw_motion(menu);
else if (menu->redraw == REDRAW_CURRENT)
menu_redraw_current(menu);
}
/**
* cum_attachs_size - Cumulative Attachments Size
* @param menu Menu listing attachments
* @retval n Number of 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;
unsigned short i;
struct AttachPtr **idx = menu->data;
struct Content *info = NULL;
struct Body *b = NULL;
for (i = 0, s = 0; i < menu->max; i++)
{
b = idx[i]->content;
if (!b->content)
b->content = mutt_get_content_info(b->filename, b);
if ((info = b->content))
{
switch (b->encoding)
{
case ENCQUOTEDPRINTABLE:
s += 3 * (info->lobin + info->hibin) + info->ascii + info->crlf;
break;
case ENCBASE64:
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 - Format strings like printf()
*
* * \%a Total number of attachments
* * \%h Hostname [option]
* * \%l Approx. length of current message (in bytes)
* * \%v Mutt version
*
* This function is similar to status_format_str(). Look at that function for
* help when modifying this function.
*/
static const char *compose_format_str(char *buf, size_t buflen, size_t col, int cols,
char op, const char *src, const char *prefix,
const char *ifstring, const char *elsestring,
unsigned long data, enum FormatFlag flags)
{
char fmt[SHORT_STRING], tmp[SHORT_STRING];
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", prefix);
snprintf(buf, buflen, fmt, menu->max);
break;
case 'h': /* hostname */
snprintf(fmt, sizeof(fmt), "%%%ss", prefix);
snprintf(buf, buflen, fmt, NONULL(Hostname));
break;
case 'l': /* approx length of current message in bytes */
snprintf(fmt, sizeof(fmt), "%%%ss", prefix);
mutt_pretty_size(tmp, sizeof(tmp), menu ? cum_attachs_size(menu) : 0);
snprintf(buf, buflen, fmt, tmp);
break;
case 'v':
snprintf(fmt, sizeof(fmt), "NeoMutt %%s%%s");
snprintf(buf, buflen, fmt, PACKAGE_VERSION, GitVer);
break;
case 0:
*buf = 0;
return src;
default:
snprintf(buf, buflen, "%%%s%c", prefix, op);
break;
}
if (optional)
compose_status_line(buf, buflen, col, cols, menu, ifstring);
else if (flags & MUTT_FORMAT_OPTIONAL)
compose_status_line(buf, buflen, col, cols, menu, elsestring);
return src;
}
static void compose_status_line(char *buf, size_t buflen, size_t col, int cols,
struct Menu *menu, const char *p)
{
mutt_expando_format(buf, buflen, col, cols, p, compose_format_str, (unsigned long) menu, 0);
}
/**
* mutt_compose_menu - Allow the user to edit the message envelope
* @retval 1 Message should be postponed
* @retval 0 Normal exit
* @retval -1 Abort message
*/
int mutt_compose_menu(struct Header *msg, /* structure for new message */
char *fcc, /* where to save a copy of the message */
size_t fcclen, struct Header *cur, /* current message */
int flags)
{
char helpstr[LONG_STRING];
char buf[LONG_STRING];
char fname[_POSIX_PATH_MAX];
struct Menu *menu = NULL;
struct AttachPtr **idx = NULL;
short idxlen = 0;
short idxmax = 0;
int i, close = 0;
int r = -1; /* return value */
int op = 0;
int loop = 1;
int fccSet = 0; /* has the user edited the Fcc: field ? */
struct Context *ctx = NULL, *this = NULL;
/* Sort, SortAux could be changed in mutt_index_menu() */
int oldSort, oldSortAux;
struct stat st;
struct ComposeRedrawData rd;
#ifdef USE_NNTP
int news = 0; /* is it a news article ? */
if (option(OPT_NEWS_SEND))
news++;
#endif
init_header_padding();
rd.msg = msg;
rd.fcc = fcc;
mutt_attach_init(msg->content);
idx = mutt_gen_attach_list(msg->content, -1, idx, &idxlen, &idxmax, 0, 1);
menu = mutt_new_menu(MENU_COMPOSE);
menu->offset = HDR_ATTACH;
menu->max = idxlen;
menu->make_entry = snd_entry;
menu->tag = mutt_tag_attach;
menu->data = idx;
#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->custom_menu_redraw = compose_menu_redraw;
menu->redraw_data = &rd;
mutt_push_current_menu(menu);
while (loop)
{
#ifdef USE_NNTP
unset_option(OPT_NEWS); /* for any case */
#endif
switch (op = mutt_menu_loop(menu))
{
case OP_COMPOSE_EDIT_FROM:
edit_address_list(HDR_FROM, &msg->env->from);
mutt_message_hook(NULL, msg, MUTT_SEND2HOOK);
break;
case OP_COMPOSE_EDIT_TO:
#ifdef USE_NNTP
if (news)
break;
#endif
edit_address_list(HDR_TO, &msg->env->to);
if (option(OPT_CRYPT_OPPORTUNISTIC_ENCRYPT))
{
crypt_opportunistic_encrypt(msg);
redraw_crypt_lines(msg);
}
mutt_message_hook(NULL, msg, MUTT_SEND2HOOK);
break;
case OP_COMPOSE_EDIT_BCC:
#ifdef USE_NNTP
if (news)
break;
#endif
edit_address_list(HDR_BCC, &msg->env->bcc);
if (option(OPT_CRYPT_OPPORTUNISTIC_ENCRYPT))
{
crypt_opportunistic_encrypt(msg);
redraw_crypt_lines(msg);
}
mutt_message_hook(NULL, msg, MUTT_SEND2HOOK);
break;
case OP_COMPOSE_EDIT_CC:
#ifdef USE_NNTP
if (news)
break;
#endif
edit_address_list(HDR_CC, &msg->env->cc);
if (option(OPT_CRYPT_OPPORTUNISTIC_ENCRYPT))
{
crypt_opportunistic_encrypt(msg);
redraw_crypt_lines(msg);
}
mutt_message_hook(NULL, msg, MUTT_SEND2HOOK);
break;
#ifdef USE_NNTP
case OP_COMPOSE_EDIT_NEWSGROUPS:
if (news)
{
if (msg->env->newsgroups)
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)
{
if (msg->env->followup_to)
strfcpy(buf, msg->env->followup_to, sizeof(buf));
else
buf[0] = 0;
if (mutt_get_field("Followup-To: ", buf, sizeof(buf), 0) == 0)
{
mutt_str_replace(&msg->env->followup_to, buf);
mutt_window_move(MuttIndexWindow, HDR_CC, HDR_XOFFSET);
if (msg->env->followup_to)
mutt_paddstr(W, msg->env->followup_to);
else
clrtoeol();
}
}
break;
case OP_COMPOSE_EDIT_X_COMMENT_TO:
if (news && option(OPT_XCOMMENT_TO))
{
if (msg->env->x_comment_to)
strfcpy(buf, msg->env->x_comment_to, sizeof(buf));
else
buf[0] = 0;
if (mutt_get_field("X-Comment-To: ", buf, sizeof(buf), 0) == 0)
{
mutt_str_replace(&msg->env->x_comment_to, buf);
mutt_window_move(MuttIndexWindow, HDR_BCC, HDR_XOFFSET);
if (msg->env->x_comment_to)
mutt_paddstr(W, msg->env->x_comment_to);
else
clrtoeol();
}
}
break;
#endif
case OP_COMPOSE_EDIT_SUBJECT:
if (msg->env->subject)
strfcpy(buf, msg->env->subject, sizeof(buf));
else
buf[0] = 0;
if (mutt_get_field(_("Subject: "), buf, sizeof(buf), 0) == 0)
{
mutt_str_replace(&msg->env->subject, buf);
mutt_window_move(MuttIndexWindow, HDR_SUBJECT, HDR_XOFFSET);
if (msg->env->subject)
mutt_paddstr(W, msg->env->subject);
else
mutt_window_clrtoeol(MuttIndexWindow);
}
mutt_message_hook(NULL, msg, MUTT_SEND2HOOK);
break;
case OP_COMPOSE_EDIT_REPLY_TO:
edit_address_list(HDR_REPLYTO, &msg->env->reply_to);
mutt_message_hook(NULL, msg, MUTT_SEND2HOOK);
break;
case OP_COMPOSE_EDIT_FCC:
strfcpy(buf, fcc, sizeof(buf));
if (mutt_get_field(_("Fcc: "), buf, sizeof(buf), MUTT_FILE | MUTT_CLEAR) == 0)
{
strfcpy(fcc, buf, fcclen);
mutt_pretty_mailbox(fcc, fcclen);
mutt_window_move(MuttIndexWindow, HDR_FCC, HDR_XOFFSET);
mutt_paddstr(W, fcc);
fccSet = 1;
}
mutt_message_hook(NULL, msg, MUTT_SEND2HOOK);
break;
case OP_COMPOSE_EDIT_MESSAGE:
if (Editor && (mutt_strcmp("builtin", Editor) != 0) && !option(OPT_EDIT_HDRS))
{
mutt_edit_file(Editor, msg->content->filename);
mutt_update_encoding(msg->content);
menu->redraw = REDRAW_FULL;
mutt_message_hook(NULL, msg, MUTT_SEND2HOOK);
break;
}
/* fall through */
case OP_COMPOSE_EDIT_HEADERS:
if ((mutt_strcmp("builtin", Editor) != 0) &&
(op == OP_COMPOSE_EDIT_HEADERS ||
(op == OP_COMPOSE_EDIT_MESSAGE && option(OPT_EDIT_HDRS))))
{
char *tag = NULL, *err = NULL;
mutt_env_to_local(msg->env);
mutt_edit_headers(NONULL(Editor), msg->content->filename, msg, fcc, fcclen);
if (mutt_env_to_intl(msg->env, &tag, &err))
{
mutt_error(_("Bad IDN in \"%s\": '%s'"), tag, err);
FREE(&err);
}
if (option(OPT_CRYPT_OPPORTUNISTIC_ENCRYPT))
crypt_opportunistic_encrypt(msg);
}
else
{
/* this is grouped with OP_COMPOSE_EDIT_HEADERS because the
attachment list could change if the user invokes ~v to edit
the message with headers, in which we need to execute the
code below to regenerate the index array */
mutt_builtin_editor(msg->content->filename, msg, cur);
}
mutt_update_encoding(msg->content);
/* attachments may have been added */
if (idxlen && idx[idxlen - 1]->content->next)
{
for (i = 0; i < idxlen; i++)
{
FREE(&idx[i]->tree);
FREE(&idx[i]);
}
idxlen = 0;
idx = mutt_gen_attach_list(msg->content, -1, idx, &idxlen, &idxmax, 0, 1);
menu->data = idx;
menu->max = idxlen;
}
menu->redraw = REDRAW_FULL;
mutt_message_hook(NULL, msg, MUTT_SEND2HOOK);
break;
case OP_COMPOSE_ATTACH_KEY:
if (!(WithCrypto & APPLICATION_PGP))
break;
if (idxlen == idxmax)
{
safe_realloc(&idx, sizeof(struct AttachPtr *) * (idxmax += 5));
menu->data = idx;
}
idx[idxlen] = safe_calloc(1, sizeof(struct AttachPtr));
if ((idx[idxlen]->content = crypt_pgp_make_key_attachment(NULL)) != NULL)
{
update_idx(menu, idx, idxlen++);
menu->redraw |= REDRAW_INDEX;
}
else
FREE(&idx[idxlen]);
menu->redraw |= REDRAW_STATUS;
mutt_message_hook(NULL, msg, MUTT_SEND2HOOK);
break;
case OP_COMPOSE_ATTACH_FILE:
{
char *prompt = NULL, **files = NULL;
int error, numfiles;
fname[0] = 0;
prompt = _("Attach file");
numfiles = 0;
files = NULL;
if (_mutt_enter_fname(prompt, fname, sizeof(fname), 0, 1, &files,
&numfiles, MUTT_SEL_MULTI) == -1 ||
*fname == '\0')
break;
if (idxlen + numfiles >= idxmax)
{
safe_realloc(&idx, sizeof(struct AttachPtr *) * (idxmax += 5 + numfiles));
menu->data = idx;
}
error = 0;
if (numfiles > 1)
mutt_message(_("Attaching selected files..."));
for (i = 0; i < numfiles; i++)
{