-
-
Notifications
You must be signed in to change notification settings - Fork 306
/
header.c
918 lines (810 loc) · 25.4 KB
/
header.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
/**
* @file
* Write a MIME Email Header to a file
*
* @authors
* Copyright (C) 2020 Richard Russon <rich@flatcap.org>
*
* @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 send_header Write a MIME Email Header to a file
*
* Write a MIME Email Header to a file
*/
#include "config.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "gui/lib.h"
#include "header.h"
#include "mutt_globals.h"
#include "options.h"
#ifdef USE_AUTOCRYPT
#include "autocrypt/lib.h"
#endif
/**
* The next array/enum pair is used to to keep track of user headers that
* override pre-defined headers NeoMutt would emit. Keep the array sorted and
* in sync with the enum.
*/
static const char *const userhdrs_override_headers[] = {
"content-type:",
"user-agent:",
};
/**
* enum UserHdrsOverrideIdx - Headers that the user may override
*/
enum UserHdrsOverrideIdx
{
USERHDRS_OVERRIDE_CONTENT_TYPE, ///< Override the "Content-Type"
USERHDRS_OVERRIDE_USER_AGENT, ///< Override the "User-Agent"
};
/**
* struct UserHdrsOverride - Which headers have been overridden
*/
struct UserHdrsOverride
{
/// Which email headers have been overridden
bool is_overridden[mutt_array_size(userhdrs_override_headers)];
};
/**
* print_val - Add pieces to an email header, wrapping where necessary
* @param fp File to write to
* @param pfx Prefix for headers
* @param value Text to be added
* @param chflags Flags, see #CopyHeaderFlags
* @param col Column that this text starts at
* @retval 0 Success
* @retval -1 Failure
*/
static int print_val(FILE *fp, const char *pfx, const char *value,
CopyHeaderFlags chflags, size_t col)
{
while (value && (value[0] != '\0'))
{
if (fputc(*value, fp) == EOF)
return -1;
/* corner-case: break words longer than 998 chars by force,
* mandated by RFC5322 */
if (!(chflags & CH_DISPLAY) && (++col >= 998))
{
if (fputs("\n ", fp) < 0)
return -1;
col = 1;
}
if (*value == '\n')
{
if ((value[1] != '\0') && pfx && (pfx[0] != '\0') && (fputs(pfx, fp) == EOF))
return -1;
/* for display, turn folding spaces into folding tabs */
if ((chflags & CH_DISPLAY) && ((value[1] == ' ') || (value[1] == '\t')))
{
value++;
while ((value[0] != '\0') && ((value[0] == ' ') || (value[0] == '\t')))
value++;
if (fputc('\t', fp) == EOF)
return -1;
continue;
}
}
value++;
}
return 0;
}
/**
* fold_one_header - Fold one header line
* @param fp File to write to
* @param tag Header key, e.g. "From"
* @param value Header value
* @param vlen Length of the header value string
* @param pfx Prefix for header
* @param wraplen Column to wrap at
* @param chflags Flags, see #CopyHeaderFlags
* @retval 0 Success
* @retval -1 Failure
*/
static int fold_one_header(FILE *fp, const char *tag, const char *value, size_t vlen,
const char *pfx, int wraplen, CopyHeaderFlags chflags)
{
if (!value || (*value == '\0') || !vlen)
return 0;
const char *p = value;
char buf[8192] = { 0 };
int first = 1, col = 0, l = 0;
const bool display = (chflags & CH_DISPLAY);
mutt_debug(LL_DEBUG5, "pfx=[%s], tag=[%s], flags=%d value=[%.*s]\n", pfx, tag,
chflags, ((value[vlen - 1] == '\n') ? vlen - 1 : vlen), value);
if (tag && *tag && (fprintf(fp, "%s%s: ", NONULL(pfx), tag) < 0))
return -1;
col = mutt_str_len(tag) + ((tag && (tag[0] != '\0')) ? 2 : 0) + mutt_str_len(pfx);
while (p && (p[0] != '\0'))
{
int fold = 0;
/* find the next word and place it in 'buf'. it may start with
* whitespace we can fold before */
const char *next = mutt_str_find_word(p);
l = MIN(sizeof(buf) - 1, next - p);
memcpy(buf, p, l);
buf[l] = '\0';
/* determine width: character cells for display, bytes for sending
* (we get pure ascii only) */
const int w = mutt_mb_width(buf, col, display);
const int enc = mutt_str_startswith(buf, "=?");
mutt_debug(LL_DEBUG5, "word=[%s], col=%d, w=%d, next=[0x0%x]\n",
(buf[0] == '\n' ? "\\n" : buf), col, w, *next);
/* insert a folding \n before the current word's lwsp except for
* header name, first word on a line (word longer than wrap width)
* and encoded words */
if (!first && !enc && col && ((col + w) >= wraplen))
{
col = mutt_str_len(pfx);
fold = 1;
if (fprintf(fp, "\n%s", NONULL(pfx)) <= 0)
return -1;
}
/* print the actual word; for display, ignore leading ws for word
* and fold with tab for readability */
if (display && fold)
{
char *pc = buf;
while ((pc[0] != '\0') && ((pc[0] == ' ') || (pc[0] == '\t')))
{
pc++;
col--;
}
if (fputc('\t', fp) == EOF)
return -1;
if (print_val(fp, pfx, pc, chflags, col) < 0)
return -1;
col += 8;
}
else if (print_val(fp, pfx, buf, chflags, col) < 0)
return -1;
col += w;
/* if the current word ends in \n, ignore all its trailing spaces
* and reset column; this prevents us from putting only spaces (or
* even none) on a line if the trailing spaces are located at our
* current line width
* XXX this covers ASCII space only, for display we probably
* want something like iswspace() here */
const char *sp = next;
while ((sp[0] != '\0') && ((sp[0] == ' ') || (sp[0] == '\t')))
sp++;
if (sp[0] == '\n')
{
if (sp[1] == '\0')
break;
next = sp;
col = 0;
}
p = next;
first = 0;
}
/* if we have printed something but didn't \n-terminate it, do it
* except the last word we printed ended in \n already */
if (col && ((l == 0) || (buf[l - 1] != '\n')))
if (putc('\n', fp) == EOF)
return -1;
return 0;
}
/**
* unfold_header - Unfold a wrapped email header
* @param s String to process
* @retval ptr Unfolded string
*
* @note The string is altered in-place
*/
static char *unfold_header(char *s)
{
char *p = s;
char *q = s;
while (p && (p[0] != '\0'))
{
/* remove CRLF prior to FWSP, turn \t into ' ' */
if ((p[0] == '\r') && (p[1] == '\n') && ((p[2] == ' ') || (p[2] == '\t')))
{
*q++ = ' ';
p += 3;
continue;
}
/* remove LF prior to FWSP, turn \t into ' ' */
else if ((p[0] == '\n') && ((p[1] == ' ') || (p[1] == '\t')))
{
*q++ = ' ';
p += 2;
continue;
}
*q++ = *p++;
}
if (q)
q[0] = '\0';
return s;
}
/**
* userhdrs_override_cmp - Compare a user-defined header with an element of the userhdrs_override_headers list
* @param a Pointer to the string containing the user-defined header
* @param b Pointer to an element of the userhdrs_override_headers list
* @retval -1 a precedes b
* @retval 0 a and b are identical
* @retval 1 b precedes a
*/
static int userhdrs_override_cmp(const void *a, const void *b)
{
const char *ca = a;
const char *cb = *(const char **) b;
return mutt_istrn_cmp(ca, cb, strlen(cb));
}
/**
* write_one_header - Write out one header line
* @param fp File to write to
* @param pfxw Width of prefix string
* @param max Max width
* @param wraplen Column to wrap at
* @param pfx Prefix for header
* @param start Start of header line
* @param end End of header line
* @param chflags Flags, see #CopyHeaderFlags
* @retval 0 Success
* @retval -1 Failure
*/
static int write_one_header(FILE *fp, int pfxw, int max, int wraplen, const char *pfx,
const char *start, const char *end, CopyHeaderFlags chflags)
{
const char *t = strchr(start, ':');
if (!t || (t > end))
{
mutt_debug(LL_DEBUG1, "#2 warning: header not in 'key: value' format!\n");
return 0;
}
const size_t vallen = end - start;
const bool short_enough = (pfxw + max <= wraplen);
mutt_debug((short_enough ? LL_DEBUG2 : LL_DEBUG5), "buf[%s%.*s] %s, max width = %d %s %d\n",
NONULL(pfx), vallen - 1 /* skip newline */, start,
(short_enough ? "short enough" : "too long"), max,
(short_enough ? "<=" : ">"), wraplen);
int rc = 0;
const char *valbuf = NULL, *tagbuf = NULL;
const bool is_from = (vallen > 5) && mutt_istr_startswith(start, "from ");
/* only pass through folding machinery if necessary for sending,
* never wrap From_ headers on sending */
if (!(chflags & CH_DISPLAY) && (short_enough || is_from))
{
if (pfx && *pfx)
{
if (fputs(pfx, fp) == EOF)
{
return -1;
}
}
valbuf = mutt_strn_dup(start, end - start);
rc = print_val(fp, pfx, valbuf, chflags, mutt_str_len(pfx));
}
else
{
if (!is_from)
{
tagbuf = mutt_strn_dup(start, t - start);
/* skip over the colon separating the header field name and value */
t++;
/* skip over any leading whitespace (WSP, as defined in RFC5322)
* NOTE: mutt_str_skip_email_wsp() does the wrong thing here.
* See tickets 3609 and 3716. */
while ((*t == ' ') || (*t == '\t'))
t++;
}
const char *s = is_from ? start : t;
valbuf = mutt_strn_dup(s, end - s);
rc = fold_one_header(fp, tagbuf, valbuf, end - s, pfx, wraplen, chflags);
}
FREE(&tagbuf);
FREE(&valbuf);
return rc;
}
/**
* write_userhdrs - Write user-defined headers and keep track of the interesting ones
* @param fp FILE pointer where to write the headers
* @param userhdrs List of headers to write
* @param privacy Omit headers that could identify the user
* @param sub Config Subset
* @retval obj UserHdrsOverride struct containing a bitmask of which unique headers were written
*/
static struct UserHdrsOverride write_userhdrs(FILE *fp, const struct ListHead *userhdrs,
bool privacy, struct ConfigSubset *sub)
{
struct UserHdrsOverride overrides = { { 0 } };
struct ListNode *tmp = NULL;
STAILQ_FOREACH(tmp, userhdrs, entries)
{
char *const colon = strchr(tmp->data, ':');
if (!colon)
{
continue;
}
const char *const value = mutt_str_skip_email_wsp(colon + 1);
if (*value == '\0')
{
continue; /* don't emit empty fields. */
}
/* check whether the current user-header is an override */
size_t curr_override = (size_t) -1;
const char *const *idx = bsearch(tmp->data, userhdrs_override_headers,
mutt_array_size(userhdrs_override_headers),
sizeof(char *), userhdrs_override_cmp);
if (idx != NULL)
{
curr_override = idx - userhdrs_override_headers;
overrides.is_overridden[curr_override] = true;
}
if (privacy && (curr_override == USERHDRS_OVERRIDE_USER_AGENT))
{
continue;
}
*colon = '\0';
mutt_write_one_header(fp, tmp->data, value, NULL, 0, CH_NO_FLAGS, sub);
*colon = ':';
}
return overrides;
}
/**
* mutt_write_one_header - Write one header line to a file
* @param fp File to write to
* @param tag Header key, e.g. "From"
* @param value Header value
* @param pfx Prefix for header
* @param wraplen Column to wrap at
* @param chflags Flags, see #CopyHeaderFlags
* @param sub Config Subset
* @retval 0 Success
* @retval -1 Failure
*
* split several headers into individual ones and call write_one_header
* for each one
*/
int mutt_write_one_header(FILE *fp, const char *tag, const char *value,
const char *pfx, int wraplen, CopyHeaderFlags chflags,
struct ConfigSubset *sub)
{
char *last = NULL, *line = NULL;
int max = 0, w, rc = -1;
int pfxw = mutt_strwidth(pfx);
char *v = mutt_str_dup(value);
bool display = (chflags & CH_DISPLAY);
const bool c_weed = cs_subset_bool(sub, "weed");
if (!display || c_weed)
v = unfold_header(v);
/* when not displaying, use sane wrap value */
if (!display)
{
const short c_wrap_headers = cs_subset_number(sub, "wrap_headers");
if ((c_wrap_headers < 78) || (c_wrap_headers > 998))
wraplen = 78;
else
wraplen = c_wrap_headers;
}
else if (wraplen <= 0)
wraplen = 78;
const size_t vlen = mutt_str_len(v);
if (tag)
{
/* if header is short enough, simply print it */
if (!display && (mutt_strwidth(tag) + 2 + pfxw + mutt_strnwidth(v, vlen) <= wraplen))
{
mutt_debug(LL_DEBUG5, "buf[%s%s: %s] is short enough\n", NONULL(pfx), tag, v);
if (fprintf(fp, "%s%s: %s\n", NONULL(pfx), tag, v) <= 0)
goto out;
rc = 0;
goto out;
}
else
{
rc = fold_one_header(fp, tag, v, vlen, pfx, wraplen, chflags);
goto out;
}
}
char *p = v;
last = v;
line = v;
while (p && *p)
{
p = strchr(p, '\n');
/* find maximum line width in current header */
if (p)
*p = '\0';
w = mutt_mb_width(line, 0, display);
if (w > max)
max = w;
if (p)
*p = '\n';
if (!p)
break;
line = ++p;
if ((*p != ' ') && (*p != '\t'))
{
if (write_one_header(fp, pfxw, max, wraplen, pfx, last, p, chflags) < 0)
goto out;
last = p;
max = 0;
}
}
if (last && *last)
if (write_one_header(fp, pfxw, max, wraplen, pfx, last, p, chflags) < 0)
goto out;
rc = 0;
out:
FREE(&v);
return rc;
}
/**
* mutt_write_references - Add the message references to a list
* @param r String List of references
* @param fp File to write to
* @param trim Trim the list to at most this many items
*
* Write the list in reverse because they are stored in reverse order when
* parsed to speed up threading.
*/
void mutt_write_references(const struct ListHead *r, FILE *fp, size_t trim)
{
struct ListNode *np = NULL;
size_t length = 0;
STAILQ_FOREACH(np, r, entries)
{
if (++length == trim)
break;
}
struct ListNode **ref = mutt_mem_calloc(length, sizeof(struct ListNode *));
// store in reverse order
size_t tmp = length;
STAILQ_FOREACH(np, r, entries)
{
ref[--tmp] = np;
if (tmp == 0)
break;
}
for (size_t i = 0; i < length; i++)
{
fputc(' ', fp);
fputs(ref[i]->data, fp);
if (i != length - 1)
fputc('\n', fp);
}
FREE(&ref);
}
/**
* mutt_rfc822_write_header - Write out one RFC822 header line
* @param fp File to write to
* @param env Envelope of email
* @param attach Attachment
* @param mode Mode, see #MuttWriteHeaderMode
* @param privacy If true, remove headers that might identify the user
* @param hide_protected_subject If true, replace subject header
* @param sub Config Subset
* @retval 0 Success
* @retval -1 Failure
*
* @note All RFC2047 encoding should be done outside of this routine, except
* for the "real name." This will allow this routine to be used more than
* once, if necessary.
*
* Likewise, all IDN processing should happen outside of this routine.
*
* privacy true => will omit any headers which may identify the user.
* Output generated is suitable for being sent through
* anonymous remailer chains.
*
* hide_protected_subject: replaces the Subject header with
* $crypt_protected_headers_subject in NORMAL or POSTPONE mode.
*/
int mutt_rfc822_write_header(FILE *fp, struct Envelope *env, struct Body *attach,
enum MuttWriteHeaderMode mode, bool privacy,
bool hide_protected_subject, struct ConfigSubset *sub)
{
char buf[1024];
if (((mode == MUTT_WRITE_HEADER_NORMAL) || (mode == MUTT_WRITE_HEADER_FCC) ||
(mode == MUTT_WRITE_HEADER_POSTPONE)) &&
!privacy)
{
struct Buffer *date = mutt_buffer_pool_get();
mutt_date_make_date(date, cs_subset_bool(sub, "local_date_header"));
fprintf(fp, "Date: %s\n", mutt_buffer_string(date));
mutt_buffer_pool_release(&date);
}
/* UseFrom is not consulted here so that we can still write a From:
* field if the user sets it with the 'my_hdr' command */
if (!TAILQ_EMPTY(&env->from) && !privacy)
{
buf[0] = '\0';
mutt_addrlist_write(&env->from, buf, sizeof(buf), false);
fprintf(fp, "From: %s\n", buf);
}
if (!TAILQ_EMPTY(&env->sender) && !privacy)
{
buf[0] = '\0';
mutt_addrlist_write(&env->sender, buf, sizeof(buf), false);
fprintf(fp, "Sender: %s\n", buf);
}
if (!TAILQ_EMPTY(&env->to))
{
fputs("To: ", fp);
mutt_addrlist_write_file(&env->to, fp, 4, false);
}
else if (mode == MUTT_WRITE_HEADER_EDITHDRS)
#ifdef USE_NNTP
if (!OptNewsSend)
#endif
fputs("To:\n", fp);
if (!TAILQ_EMPTY(&env->cc))
{
fputs("Cc: ", fp);
mutt_addrlist_write_file(&env->cc, fp, 4, false);
}
else if (mode == MUTT_WRITE_HEADER_EDITHDRS)
#ifdef USE_NNTP
if (!OptNewsSend)
#endif
fputs("Cc:\n", fp);
if (!TAILQ_EMPTY(&env->bcc))
{
const bool c_write_bcc = cs_subset_bool(sub, "write_bcc");
if ((mode == MUTT_WRITE_HEADER_POSTPONE) ||
(mode == MUTT_WRITE_HEADER_EDITHDRS) || (mode == MUTT_WRITE_HEADER_FCC) ||
((mode == MUTT_WRITE_HEADER_NORMAL) && c_write_bcc))
{
fputs("Bcc: ", fp);
mutt_addrlist_write_file(&env->bcc, fp, 5, false);
}
}
else if (mode == MUTT_WRITE_HEADER_EDITHDRS)
#ifdef USE_NNTP
if (!OptNewsSend)
#endif
fputs("Bcc:\n", fp);
#ifdef USE_NNTP
if (env->newsgroups)
fprintf(fp, "Newsgroups: %s\n", env->newsgroups);
else if ((mode == MUTT_WRITE_HEADER_EDITHDRS) && OptNewsSend)
fputs("Newsgroups:\n", fp);
if (env->followup_to)
fprintf(fp, "Followup-To: %s\n", env->followup_to);
else if ((mode == MUTT_WRITE_HEADER_EDITHDRS) && OptNewsSend)
fputs("Followup-To:\n", fp);
const bool c_x_comment_to = cs_subset_bool(sub, "x_comment_to");
if (env->x_comment_to)
fprintf(fp, "X-Comment-To: %s\n", env->x_comment_to);
else if ((mode == MUTT_WRITE_HEADER_EDITHDRS) && OptNewsSend && c_x_comment_to)
fputs("X-Comment-To:\n", fp);
#endif
if (env->subject)
{
if (hide_protected_subject &&
((mode == MUTT_WRITE_HEADER_NORMAL) || (mode == MUTT_WRITE_HEADER_FCC) ||
(mode == MUTT_WRITE_HEADER_POSTPONE)))
{
const char *const c_crypt_protected_headers_subject =
cs_subset_string(sub, "crypt_protected_headers_subject");
mutt_write_one_header(fp, "Subject", c_crypt_protected_headers_subject,
NULL, 0, CH_NO_FLAGS, sub);
}
else
mutt_write_one_header(fp, "Subject", env->subject, NULL, 0, CH_NO_FLAGS, sub);
}
else if (mode == MUTT_WRITE_HEADER_EDITHDRS)
fputs("Subject:\n", fp);
/* save message id if the user has set it */
if (env->message_id && !privacy)
fprintf(fp, "Message-ID: %s\n", env->message_id);
if (!TAILQ_EMPTY(&env->reply_to))
{
fputs("Reply-To: ", fp);
mutt_addrlist_write_file(&env->reply_to, fp, 10, false);
}
else if (mode == MUTT_WRITE_HEADER_EDITHDRS)
fputs("Reply-To:\n", fp);
if (!TAILQ_EMPTY(&env->mail_followup_to))
{
#ifdef USE_NNTP
if (!OptNewsSend)
#endif
{
fputs("Mail-Followup-To: ", fp);
mutt_addrlist_write_file(&env->mail_followup_to, fp, 18, false);
}
}
/* Add any user defined headers */
struct UserHdrsOverride userhdrs_overrides =
write_userhdrs(fp, &env->userhdrs, privacy, sub);
if ((mode == MUTT_WRITE_HEADER_NORMAL) || (mode == MUTT_WRITE_HEADER_FCC) ||
(mode == MUTT_WRITE_HEADER_POSTPONE) || (mode == MUTT_WRITE_HEADER_MIME))
{
if (!STAILQ_EMPTY(&env->references))
{
fputs("References:", fp);
mutt_write_references(&env->references, fp, 10);
fputc('\n', fp);
}
/* Add the MIME headers */
if (!userhdrs_overrides.is_overridden[USERHDRS_OVERRIDE_CONTENT_TYPE])
{
fputs("MIME-Version: 1.0\n", fp);
mutt_write_mime_header(attach, fp, sub);
}
}
if (!STAILQ_EMPTY(&env->in_reply_to))
{
fputs("In-Reply-To:", fp);
mutt_write_references(&env->in_reply_to, fp, 0);
fputc('\n', fp);
}
#ifdef USE_AUTOCRYPT
const bool c_autocrypt = cs_subset_bool(sub, "autocrypt");
if (c_autocrypt)
{
if (mode == MUTT_WRITE_HEADER_NORMAL || mode == MUTT_WRITE_HEADER_FCC)
mutt_autocrypt_write_autocrypt_header(env, fp);
if (mode == MUTT_WRITE_HEADER_MIME)
mutt_autocrypt_write_gossip_headers(env, fp);
}
#endif
const bool c_user_agent = cs_subset_bool(sub, "user_agent");
if (((mode == MUTT_WRITE_HEADER_NORMAL) || (mode == MUTT_WRITE_HEADER_FCC)) && !privacy &&
c_user_agent && !userhdrs_overrides.is_overridden[USERHDRS_OVERRIDE_USER_AGENT])
{
/* Add a vanity header */
fprintf(fp, "User-Agent: NeoMutt/%s%s\n", PACKAGE_VERSION, GitVer);
}
return (ferror(fp) == 0) ? 0 : -1;
}
/**
* mutt_write_mime_header - Create a MIME header
* @param a Body part
* @param fp File to write to
* @param sub Config Subset
* @retval 0 Success
* @retval -1 Failure
*/
int mutt_write_mime_header(struct Body *a, FILE *fp, struct ConfigSubset *sub)
{
if (!a || !fp)
return -1;
int len;
int tmplen;
char buf[256] = { 0 };
char *id = NULL;
fprintf(fp, "Content-Type: %s/%s", TYPE(a), a->subtype);
if (!TAILQ_EMPTY(&a->parameter))
{
len = 25 + mutt_str_len(a->subtype); /* approximate len. of content-type */
struct Parameter *np = NULL;
TAILQ_FOREACH(np, &a->parameter, entries)
{
if (!np->attribute || !np->value)
continue;
struct ParameterList pl_conts = TAILQ_HEAD_INITIALIZER(pl_conts);
rfc2231_encode_string(&pl_conts, np->attribute, np->value);
struct Parameter *cont = NULL;
TAILQ_FOREACH(cont, &pl_conts, entries)
{
if (mutt_istr_equal(cont->attribute, "content-id"))
{
// Content-ID: gets its own header
id = mutt_str_dup(cont->value);
break;
}
fputc(';', fp);
buf[0] = 0;
mutt_addr_cat(buf, sizeof(buf), cont->value, MimeSpecials);
/* Dirty hack to make messages readable by Outlook Express
* for the Mac: force quotes around the boundary parameter
* even when they aren't needed. */
if (mutt_istr_equal(cont->attribute, "boundary") && mutt_str_equal(buf, cont->value))
snprintf(buf, sizeof(buf), "\"%s\"", cont->value);
tmplen = mutt_str_len(buf) + mutt_str_len(cont->attribute) + 1;
if (len + tmplen + 2 > 76)
{
fputs("\n\t", fp);
len = tmplen + 1;
}
else
{
fputc(' ', fp);
len += tmplen + 1;
}
fprintf(fp, "%s=%s", cont->attribute, buf);
}
mutt_param_free(&pl_conts);
}
}
fputc('\n', fp);
if (id)
{
fprintf(fp, "Content-ID: <%s>\n", id);
mutt_mem_free(&id);
}
if (a->language)
fprintf(fp, "Content-Language: %s\n", a->language);
if (a->description)
fprintf(fp, "Content-Description: %s\n", a->description);
if (a->disposition != DISP_NONE)
{
const char *dispstr[] = { "inline", "attachment", "form-data" };
if (a->disposition < sizeof(dispstr) / sizeof(char *))
{
fprintf(fp, "Content-Disposition: %s", dispstr[a->disposition]);
len = 21 + mutt_str_len(dispstr[a->disposition]);
if (a->use_disp && ((a->disposition != DISP_INLINE) || a->d_filename))
{
char *fn = a->d_filename;
if (!fn)
fn = a->filename;
if (fn)
{
/* Strip off the leading path... */
char *t = strrchr(fn, '/');
if (t)
t++;
else
t = fn;
struct ParameterList pl_conts = TAILQ_HEAD_INITIALIZER(pl_conts);
rfc2231_encode_string(&pl_conts, "filename", t);
struct Parameter *cont = NULL;
TAILQ_FOREACH(cont, &pl_conts, entries)
{
fputc(';', fp);
buf[0] = 0;
mutt_addr_cat(buf, sizeof(buf), cont->value, MimeSpecials);
tmplen = mutt_str_len(buf) + mutt_str_len(cont->attribute) + 1;
if (len + tmplen + 2 > 76)
{
fputs("\n\t", fp);
len = tmplen + 1;
}
else
{
fputc(' ', fp);
len += tmplen + 1;
}
fprintf(fp, "%s=%s", cont->attribute, buf);
}
mutt_param_free(&pl_conts);
}
}
fputc('\n', fp);
}
else
{
mutt_debug(LL_DEBUG1, "ERROR: invalid content-disposition %d\n", a->disposition);
}
}
if (a->encoding != ENC_7BIT)
fprintf(fp, "Content-Transfer-Encoding: %s\n", ENCODING(a->encoding));
const bool c_crypt_protected_headers_write =
cs_subset_bool(sub, "crypt_protected_headers_write");
bool autocrypt = false;
#ifdef USE_AUTOCRYPT
autocrypt = cs_subset_bool(sub, "autocrypt");
#endif
if ((c_crypt_protected_headers_write || autocrypt) && a->mime_headers)
{
mutt_rfc822_write_header(fp, a->mime_headers, NULL, MUTT_WRITE_HEADER_MIME,
false, false, sub);
}
/* Do NOT add the terminator here!!! */
return ferror(fp) ? -1 : 0;
}