forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendlib.c
3107 lines (2724 loc) · 75.2 KB
/
sendlib.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
* Miscellaneous functions for sending an email
*
* @authors
* Copyright (C) 1996-2002,2009-2012 Michael R. Elkins <[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/>.
*/
#include "config.h"
#include <stddef.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <iconv.h>
#include <inttypes.h>
#include <limits.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <wchar.h>
#include "mutt.h"
#include "address.h"
#include "body.h"
#include "buffy.h"
#include "charset.h"
#include "content.h"
#include "context.h"
#include "copy.h"
#include "envelope.h"
#include "format_flags.h"
#include "globals.h"
#include "header.h"
#include "lib/lib.h"
#include "list.h"
#include "mailbox.h"
#include "mime.h"
#include "mutt_curses.h"
#include "mutt_idna.h"
#include "mx.h"
#include "ncrypt/ncrypt.h"
#include "options.h"
#include "pager.h"
#include "parameter.h"
#include "protos.h"
#include "rfc2047.h"
#include "rfc2231.h"
#include "rfc822.h"
#include "state.h"
#ifdef USE_NNTP
#include "nntp.h"
#endif
#ifdef HAVE_SYSEXITS_H
#include <sysexits.h>
#else /* Make sure EX_OK is defined <[email protected]> */
#define EX_OK 0
#endif
/* If you are debugging this file, comment out the following line. */
#define NDEBUG
#ifdef NDEBUG
#define assert(x)
#else
#include <assert.h>
#endif
extern char RFC822Specials[];
const char MimeSpecials[] = "@.,;:<>[]\\\"()?/= \t";
static void encode_quoted(FGETCONV *fc, FILE *fout, int istext)
{
int c, linelen = 0;
char line[77], savechar;
while ((c = fgetconv(fc)) != EOF)
{
/* Wrap the line if needed. */
if (linelen == 76 && ((istext && c != '\n') || !istext))
{
/* If the last character is "quoted", then be sure to move all three
* characters to the next line. Otherwise, just move the last
* character...
*/
if (line[linelen - 3] == '=')
{
line[linelen - 3] = 0;
fputs(line, fout);
fputs("=\n", fout);
line[linelen] = 0;
line[0] = '=';
line[1] = line[linelen - 2];
line[2] = line[linelen - 1];
linelen = 3;
}
else
{
savechar = line[linelen - 1];
line[linelen - 1] = '=';
line[linelen] = 0;
fputs(line, fout);
fputc('\n', fout);
line[0] = savechar;
linelen = 1;
}
}
/* Escape lines that begin with/only contain "the message separator". */
if (linelen == 4 && (mutt_strncmp("From", line, 4) == 0))
{
strfcpy(line, "=46rom", sizeof(line));
linelen = 6;
}
else if (linelen == 4 && (mutt_strncmp("from", line, 4) == 0))
{
strfcpy(line, "=66rom", sizeof(line));
linelen = 6;
}
else if (linelen == 1 && line[0] == '.')
{
strfcpy(line, "=2E", sizeof(line));
linelen = 3;
}
if (c == '\n' && istext)
{
/* Check to make sure there is no trailing space on this line. */
if (linelen > 0 && (line[linelen - 1] == ' ' || line[linelen - 1] == '\t'))
{
if (linelen < 74)
{
sprintf(line + linelen - 1, "=%2.2X", (unsigned char) line[linelen - 1]);
fputs(line, fout);
}
else
{
int savechar2 = line[linelen - 1];
line[linelen - 1] = '=';
line[linelen] = 0;
fputs(line, fout);
fprintf(fout, "\n=%2.2X", (unsigned char) savechar2);
}
}
else
{
line[linelen] = 0;
fputs(line, fout);
}
fputc('\n', fout);
linelen = 0;
}
else if (c != 9 && (c < 32 || c > 126 || c == '='))
{
/* Check to make sure there is enough room for the quoted character.
* If not, wrap to the next line.
*/
if (linelen > 73)
{
line[linelen++] = '=';
line[linelen] = 0;
fputs(line, fout);
fputc('\n', fout);
linelen = 0;
}
sprintf(line + linelen, "=%2.2X", (unsigned char) c);
linelen += 3;
}
else
{
/* Don't worry about wrapping the line here. That will happen during
* the next iteration when I'll also know what the next character is.
*/
line[linelen++] = c;
}
}
/* Take care of anything left in the buffer */
if (linelen > 0)
{
if (line[linelen - 1] == ' ' || line[linelen - 1] == '\t')
{
/* take care of trailing whitespace */
if (linelen < 74)
sprintf(line + linelen - 1, "=%2.2X", (unsigned char) line[linelen - 1]);
else
{
savechar = line[linelen - 1];
line[linelen - 1] = '=';
line[linelen] = 0;
fputs(line, fout);
fputc('\n', fout);
sprintf(line, "=%2.2X", (unsigned char) savechar);
}
}
else
line[linelen] = 0;
fputs(line, fout);
}
}
/**
* struct B64Context - Cursor for the Base64 conversion
*/
struct B64Context
{
char buffer[3];
short size;
short linelen;
};
static int b64_init(struct B64Context *ctx)
{
memset(ctx->buffer, '\0', sizeof(ctx->buffer));
ctx->size = 0;
ctx->linelen = 0;
return 0;
}
static void b64_flush(struct B64Context *ctx, FILE *fout)
{
/* for some reasons, mutt_to_base64 expects the
* output buffer to be larger than 10B */
char encoded[11];
size_t ret;
if (!ctx->size)
return;
if (ctx->linelen >= 72)
{
fputc('\n', fout);
ctx->linelen = 0;
}
/* ret should always be equal to 4 here, because ctx->size
* is a value between 1 and 3 (included), but let's not hardcode it
* and prefer the return value of the function */
ret = mutt_to_base64(encoded, ctx->buffer, ctx->size, sizeof(encoded));
for (size_t i = 0; i < ret; i++)
{
fputc(encoded[i], fout);
ctx->linelen++;
}
ctx->size = 0;
}
static void b64_putc(struct B64Context *ctx, char c, FILE *fout)
{
if (ctx->size == 3)
b64_flush(ctx, fout);
ctx->buffer[ctx->size++] = c;
}
static void encode_base64(FGETCONV *fc, FILE *fout, int istext)
{
struct B64Context ctx;
int ch, ch1 = EOF;
b64_init(&ctx);
while ((ch = fgetconv(fc)) != EOF)
{
if (SigInt == 1)
{
SigInt = 0;
return;
}
if (istext && ch == '\n' && ch1 != '\r')
b64_putc(&ctx, '\r', fout);
b64_putc(&ctx, ch, fout);
ch1 = ch;
}
b64_flush(&ctx, fout);
fputc('\n', fout);
}
static void encode_8bit(FGETCONV *fc, FILE *fout, int istext)
{
int ch;
while ((ch = fgetconv(fc)) != EOF)
{
if (SigInt == 1)
{
SigInt = 0;
return;
}
fputc(ch, fout);
}
}
int mutt_write_mime_header(struct Body *a, FILE *f)
{
struct Parameter *p = NULL;
char buffer[STRING];
char *t = NULL;
char *fn = NULL;
int len;
int tmplen;
int encode;
fprintf(f, "Content-Type: %s/%s", TYPE(a), a->subtype);
if (a->parameter)
{
len = 25 + mutt_strlen(a->subtype); /* approximate len. of content-type */
for (p = a->parameter; p; p = p->next)
{
char *tmp = NULL;
if (!p->value)
continue;
fputc(';', f);
buffer[0] = 0;
tmp = safe_strdup(p->value);
encode = rfc2231_encode_string(&tmp);
rfc822_cat(buffer, sizeof(buffer), tmp, 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_strcasecmp(p->attribute, "boundary") == 0) && (strcmp(buffer, tmp) == 0))
snprintf(buffer, sizeof(buffer), "\"%s\"", tmp);
FREE(&tmp);
tmplen = mutt_strlen(buffer) + mutt_strlen(p->attribute) + 1;
if (len + tmplen + 2 > 76)
{
fputs("\n\t", f);
len = tmplen + 8;
}
else
{
fputc(' ', f);
len += tmplen + 1;
}
fprintf(f, "%s%s=%s", p->attribute, encode ? "*" : "", buffer);
}
}
fputc('\n', f);
if (a->description)
fprintf(f, "Content-Description: %s\n", a->description);
if (a->disposition != DISPNONE)
{
const char *dispstr[] = { "inline", "attachment", "form-data" };
if (a->disposition < sizeof(dispstr) / sizeof(char *))
{
fprintf(f, "Content-Disposition: %s", dispstr[a->disposition]);
if (a->use_disp)
{
if (!(fn = a->d_filename))
fn = a->filename;
if (fn)
{
char *tmp = NULL;
/* Strip off the leading path... */
if ((t = strrchr(fn, '/')))
t++;
else
t = fn;
buffer[0] = 0;
tmp = safe_strdup(t);
encode = rfc2231_encode_string(&tmp);
rfc822_cat(buffer, sizeof(buffer), tmp, MimeSpecials);
FREE(&tmp);
fprintf(f, "; filename%s=%s", encode ? "*" : "", buffer);
}
}
fputc('\n', f);
}
else
{
mutt_debug(1, "ERROR: invalid content-disposition %d\n", a->disposition);
}
}
if (a->encoding != ENC7BIT)
fprintf(f, "Content-Transfer-Encoding: %s\n", ENCODING(a->encoding));
/* Do NOT add the terminator here!!! */
return (ferror(f) ? -1 : 0);
}
#define write_as_text_part(a) \
(mutt_is_text_part(a) || ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp(a)))
int mutt_write_mime_body(struct Body *a, FILE *f)
{
char *p, boundary[SHORT_STRING];
char send_charset[SHORT_STRING];
FILE *fpin = NULL;
struct Body *t = NULL;
FGETCONV *fc = NULL;
if (a->type == TYPEMULTIPART)
{
/* First, find the boundary to use */
if (!(p = mutt_get_parameter("boundary", a->parameter)))
{
mutt_debug(1, "mutt_write_mime_body(): no boundary parameter found!\n");
mutt_error(_("No boundary parameter found! [report this error]"));
return -1;
}
strfcpy(boundary, p, sizeof(boundary));
for (t = a->parts; t; t = t->next)
{
fprintf(f, "\n--%s\n", boundary);
if (mutt_write_mime_header(t, f) == -1)
return -1;
fputc('\n', f);
if (mutt_write_mime_body(t, f) == -1)
return -1;
}
fprintf(f, "\n--%s--\n", boundary);
return (ferror(f) ? -1 : 0);
}
/* This is pretty gross, but it's the best solution for now... */
if ((WithCrypto & APPLICATION_PGP) && a->type == TYPEAPPLICATION &&
(mutt_strcmp(a->subtype, "pgp-encrypted") == 0))
{
fputs("Version: 1\n", f);
return 0;
}
if ((fpin = fopen(a->filename, "r")) == NULL)
{
mutt_debug(1, "write_mime_body: %s no longer exists!\n", a->filename);
mutt_error(_("%s no longer exists!"), a->filename);
return -1;
}
if (a->type == TYPETEXT && (!a->noconv))
fc = fgetconv_open(fpin, a->charset,
mutt_get_body_charset(send_charset, sizeof(send_charset), a), 0);
else
fc = fgetconv_open(fpin, 0, 0, 0);
mutt_allow_interrupt(1);
if (a->encoding == ENCQUOTEDPRINTABLE)
encode_quoted(fc, f, write_as_text_part(a));
else if (a->encoding == ENCBASE64)
encode_base64(fc, f, write_as_text_part(a));
else if (a->type == TYPETEXT && (!a->noconv))
encode_8bit(fc, f, write_as_text_part(a));
else
mutt_copy_stream(fpin, f);
mutt_allow_interrupt(0);
fgetconv_close(&fc);
safe_fclose(&fpin);
if (SigInt == 1)
{
SigInt = 0;
return -1;
}
return (ferror(f) ? -1 : 0);
}
#undef write_as_text_part
void mutt_generate_boundary(struct Parameter **parm)
{
char rs[MUTT_RANDTAG_LEN + 1];
mutt_rand_base32(rs, sizeof(rs) - 1);
rs[MUTT_RANDTAG_LEN] = 0;
mutt_set_parameter("boundary", rs, parm);
}
/**
* struct ContentState - Info about the body of an email
*/
struct ContentState
{
int from;
int whitespace;
int dot;
int linelen;
int was_cr;
};
static void update_content_info(struct Content *info, struct ContentState *s,
char *d, size_t dlen)
{
int from = s->from;
int whitespace = s->whitespace;
int dot = s->dot;
int linelen = s->linelen;
int was_cr = s->was_cr;
if (!d) /* This signals EOF */
{
if (was_cr)
info->binary = true;
if (linelen > info->linemax)
info->linemax = linelen;
return;
}
for (; dlen; d++, dlen--)
{
char ch = *d;
if (was_cr)
{
was_cr = 0;
if (ch != '\n')
{
info->binary = true;
}
else
{
if (whitespace)
info->space = true;
if (dot)
info->dot = true;
if (linelen > info->linemax)
info->linemax = linelen;
whitespace = 0;
dot = 0;
linelen = 0;
continue;
}
}
linelen++;
if (ch == '\n')
{
info->crlf++;
if (whitespace)
info->space = true;
if (dot)
info->dot = true;
if (linelen > info->linemax)
info->linemax = linelen;
whitespace = 0;
linelen = 0;
dot = 0;
}
else if (ch == '\r')
{
info->crlf++;
info->cr = true;
was_cr = 1;
continue;
}
else if (ch & 0x80)
info->hibin++;
else if (ch == '\t' || ch == '\f')
{
info->ascii++;
whitespace++;
}
else if (ch == 0)
{
info->nulbin++;
info->lobin++;
}
else if (ch < 32 || ch == 127)
info->lobin++;
else
{
if (linelen == 1)
{
if ((ch == 'F') || (ch == 'f'))
from = 1;
else
from = 0;
if (ch == '.')
dot = 1;
else
dot = 0;
}
else if (from)
{
if (linelen == 2 && ch != 'r')
from = 0;
else if (linelen == 3 && ch != 'o')
from = 0;
else if (linelen == 4)
{
if (ch == 'm')
info->from = true;
from = 0;
}
}
if (ch == ' ')
whitespace++;
info->ascii++;
}
if (linelen > 1)
dot = 0;
if (ch != ' ' && ch != '\t')
whitespace = 0;
}
s->from = from;
s->whitespace = whitespace;
s->dot = dot;
s->linelen = linelen;
s->was_cr = was_cr;
}
/* Define as 1 if iconv sometimes returns -1(EILSEQ) instead of transcribing. */
#define BUGGY_ICONV 1
/**
* convert_file_to - Change the encoding of a file
* @param[in] file File to convert
* @param[in] fromcode Original encoding
* @param[in] ncodes Number of target encodings
* @param[in] tocodes List of target encodings
* @param[out] tocode Chosen encoding
* @param[in] info Encoding information
* @retval -1 Error, no conversion was possible
* @retval >0 Success, number of bytes converted
*
* Find the best charset conversion of the file from fromcode into one
* of the tocodes. If successful, set *tocode and Content *info and
* return the number of characters converted inexactly.
*
* We convert via UTF-8 in order to avoid the condition -1(EINVAL),
* which would otherwise prevent us from knowing the number of inexact
* conversions. Where the candidate target charset is UTF-8 we avoid
* doing the second conversion because iconv_open("UTF-8", "UTF-8")
* fails with some libraries.
*
* We assume that the output from iconv is never more than 4 times as
* long as the input for any pair of charsets we might be interested
* in.
*/
static size_t convert_file_to(FILE *file, const char *fromcode, int ncodes,
const char **tocodes, int *tocode, struct Content *info)
{
iconv_t cd1, *cd = NULL;
char bufi[256], bufu[512], bufo[4 * sizeof(bufi)];
ICONV_CONST char *ib = NULL, *ub = NULL;
char *ob = NULL;
size_t ibl, obl, ubl, ubl1, n, ret;
int i;
struct Content *infos = NULL;
struct ContentState *states = NULL;
size_t *score = NULL;
cd1 = mutt_iconv_open("utf-8", fromcode, 0);
if (cd1 == (iconv_t)(-1))
return -1;
cd = safe_calloc(ncodes, sizeof(iconv_t));
score = safe_calloc(ncodes, sizeof(size_t));
states = safe_calloc(ncodes, sizeof(struct ContentState));
infos = safe_calloc(ncodes, sizeof(struct Content));
for (i = 0; i < ncodes; i++)
if (mutt_strcasecmp(tocodes[i], "utf-8") != 0)
cd[i] = mutt_iconv_open(tocodes[i], "utf-8", 0);
else
{
/* Special case for conversion to UTF-8 */
cd[i] = (iconv_t)(-1);
score[i] = (size_t)(-1);
}
rewind(file);
ibl = 0;
for (;;)
{
/* Try to fill input buffer */
n = fread(bufi + ibl, 1, sizeof(bufi) - ibl, file);
ibl += n;
/* Convert to UTF-8 */
ib = bufi;
ob = bufu;
obl = sizeof(bufu);
n = iconv(cd1, ibl ? &ib : 0, &ibl, &ob, &obl);
assert(n == (size_t)(-1) || !n);
if (n == (size_t)(-1) && ((errno != EINVAL && errno != E2BIG) || ib == bufi))
{
assert(errno == EILSEQ || (errno == EINVAL && ib == bufi && ibl < sizeof(bufi)));
ret = (size_t)(-1);
break;
}
ubl1 = ob - bufu;
/* Convert from UTF-8 */
for (i = 0; i < ncodes; i++)
if (cd[i] != (iconv_t)(-1) && score[i] != (size_t)(-1))
{
ub = bufu;
ubl = ubl1;
ob = bufo;
obl = sizeof(bufo);
n = iconv(cd[i], (ibl || ubl) ? &ub : 0, &ubl, &ob, &obl);
if (n == (size_t)(-1))
{
assert(errno == E2BIG || (BUGGY_ICONV && (errno == EILSEQ || errno == ENOENT)));
score[i] = (size_t)(-1);
}
else
{
score[i] += n;
update_content_info(&infos[i], &states[i], bufo, ob - bufo);
}
}
else if (cd[i] == (iconv_t)(-1) && score[i] == (size_t)(-1))
/* Special case for conversion to UTF-8 */
update_content_info(&infos[i], &states[i], bufu, ubl1);
if (ibl)
/* Save unused input */
memmove(bufi, ib, ibl);
else if (!ubl1 && ib < bufi + sizeof(bufi))
{
ret = 0;
break;
}
}
if (!ret)
{
/* Find best score */
ret = (size_t)(-1);
for (i = 0; i < ncodes; i++)
{
if (cd[i] == (iconv_t)(-1) && score[i] == (size_t)(-1))
{
/* Special case for conversion to UTF-8 */
*tocode = i;
ret = 0;
break;
}
else if (cd[i] == (iconv_t)(-1) || score[i] == (size_t)(-1))
continue;
else if (ret == (size_t)(-1) || score[i] < ret)
{
*tocode = i;
ret = score[i];
if (!ret)
break;
}
}
if (ret != (size_t)(-1))
{
memcpy(info, &infos[*tocode], sizeof(struct Content));
update_content_info(info, &states[*tocode], 0, 0); /* EOF */
}
}
for (i = 0; i < ncodes; i++)
if (cd[i] != (iconv_t)(-1))
iconv_close(cd[i]);
iconv_close(cd1);
FREE(&cd);
FREE(&infos);
FREE(&score);
FREE(&states);
return ret;
}
/**
* convert_file_from_to - Convert a file between encodings
*
* Find the first of the fromcodes that gives a valid conversion and the best
* charset conversion of the file into one of the tocodes. If successful, set
* *fromcode and *tocode to dynamically allocated strings, set Content *info,
* and return the number of characters converted inexactly. If no conversion
* was possible, return -1.
*
* Both fromcodes and tocodes may be colon-separated lists of charsets.
* However, if fromcode is zero then fromcodes is assumed to be the name of a
* single charset even if it contains a colon.
*/
static size_t convert_file_from_to(FILE *file, const char *fromcodes, const char *tocodes,
char **fromcode, char **tocode, struct Content *info)
{
char *fcode = NULL;
char **tcode = NULL;
const char *c = NULL, *c1 = NULL;
size_t ret;
int ncodes, i, cn;
/* Count the tocodes */
ncodes = 0;
for (c = tocodes; c; c = c1 ? c1 + 1 : 0)
{
if ((c1 = strchr(c, ':')) == c)
continue;
ncodes++;
}
/* Copy them */
tcode = safe_malloc(ncodes * sizeof(char *));
for (c = tocodes, i = 0; c; c = c1 ? c1 + 1 : 0, i++)
{
if ((c1 = strchr(c, ':')) == c)
continue;
tcode[i] = mutt_substrdup(c, c1);
}
ret = (size_t)(-1);
if (fromcode)
{
/* Try each fromcode in turn */
for (c = fromcodes; c; c = c1 ? c1 + 1 : 0)
{
if ((c1 = strchr(c, ':')) == c)
continue;
fcode = mutt_substrdup(c, c1);
ret = convert_file_to(file, fcode, ncodes, (const char **) tcode, &cn, info);
if (ret != (size_t)(-1))
{
*fromcode = fcode;
*tocode = tcode[cn];
tcode[cn] = 0;
break;
}
FREE(&fcode);
}
}
else
{
/* There is only one fromcode */
ret = convert_file_to(file, fromcodes, ncodes, (const char **) tcode, &cn, info);
if (ret != (size_t)(-1))
{
*tocode = tcode[cn];
tcode[cn] = 0;
}
}
/* Free memory */
for (i = 0; i < ncodes; i++)
FREE(&tcode[i]);
FREE(&tcode);
return ret;
}
/**
* mutt_get_content_info - Analyze file to determine MIME encoding to use
*
* Also set the body charset, sometimes, or not.
*/
struct Content *mutt_get_content_info(const char *fname, struct Body *b)
{
struct Content *info = NULL;
struct ContentState state;
FILE *fp = NULL;
char *fromcode = NULL;
char *tocode = NULL;
char buffer[100];
char chsbuf[STRING];
size_t r;
struct stat sb;
if (b && !fname)
fname = b->filename;
if (stat(fname, &sb) == -1)
{
mutt_error(_("Can't stat %s: %s"), fname, strerror(errno));
return NULL;
}
if (!S_ISREG(sb.st_mode))
{
mutt_error(_("%s isn't a regular file."), fname);
return NULL;
}
if ((fp = fopen(fname, "r")) == NULL)
{
mutt_debug(1, "mutt_get_content_info: %s: %s (errno %d).\n", fname,
strerror(errno), errno);
return NULL;
}
info = safe_calloc(1, sizeof(struct Content));
memset(&state, 0, sizeof(state));
if (b != NULL && b->type == TYPETEXT && (!b->noconv && !b->force_charset))
{
char *chs = mutt_get_parameter("charset", b->parameter);
char *fchs = b->use_disp ?
((AttachCharset && *AttachCharset) ? AttachCharset : Charset) :
Charset;
if (Charset && (chs || SendCharset) &&
convert_file_from_to(fp, fchs, chs ? chs : SendCharset, &fromcode,
&tocode, info) != (size_t)(-1))
{
if (!chs)
{
mutt_canonical_charset(chsbuf, sizeof(chsbuf), tocode);
mutt_set_parameter("charset", chsbuf, &b->parameter);
}
FREE(&b->charset);
b->charset = fromcode;
FREE(&tocode);
safe_fclose(&fp);
return info;
}
}
rewind(fp);
while ((r = fread(buffer, 1, sizeof(buffer), fp)))
update_content_info(info, &state, buffer, r);
update_content_info(info, &state, 0, 0);
safe_fclose(&fp);
if (b != NULL && b->type == TYPETEXT && (!b->noconv && !b->force_charset))
mutt_set_parameter(
"charset",
(!info->hibin ? "us-ascii" : Charset && !mutt_is_us_ascii(Charset) ? Charset : "unknown-8bit"),
&b->parameter);
return info;
}
/**
* mutt_lookup_mime_type - Find the MIME type for an attachment
* @param att Email with attachment
* @param path Path to attachment
* @retval n MIME type, e.g. #TYPEIMAGE
*
* Given a file at `path`, see if there is a registered MIME type.
* Returns the major MIME type, and copies the subtype to ``d''. First look
* for ~/.mime.types, then look in a system mime.types if we can find one.
* The longest match is used so that we can match `ps.gz' when `gz' also
* exists.
*/
int mutt_lookup_mime_type(struct Body *att, const char *path)
{
FILE *f = NULL;
char *p = NULL, *q = NULL, *ct = NULL;
char buf[LONG_STRING];
char subtype[STRING], xtype[STRING];
int szf, sze, cur_sze;
int type;
bool found_mimetypes = false;
*subtype = '\0';
*xtype = '\0';
type = TYPEOTHER;
cur_sze = 0;
szf = mutt_strlen(path);
for (int count = 0; count < 4; count++)