forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbody.c
379 lines (349 loc) · 9.62 KB
/
body.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
/**
* @file
* Write a MIME Email Body to a file
*
* @authors
* Copyright (C) 2020-2023 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 send_body Write a MIME Email to a file
*
* Write a MIME Email Body to a file
*/
#include "config.h"
#include <stdbool.h>
#include "mutt/lib.h"
#include "email/lib.h"
#include "body.h"
#include "ncrypt/lib.h"
#include "header.h"
#include "muttlib.h"
/**
* struct B64Context - Cursor for the Base64 conversion
*/
struct B64Context
{
char buffer[3];
short size;
short linelen;
};
/**
* b64_flush - Save the bytes to the file
* @param bctx Cursor for the base64 conversion
* @param fp_out File to save the output
*/
static void b64_flush(struct B64Context *bctx, FILE *fp_out)
{
/* for some reasons, mutt_b64_encode expects the
* output buffer to be larger than 10B */
char encoded[11] = { 0 };
size_t rc;
if (bctx->size == 0)
return;
if (bctx->linelen >= 72)
{
fputc('\n', fp_out);
bctx->linelen = 0;
}
/* rc should always be equal to 4 here, because bctx->size
* is a value between 1 and 3 (included), but let's not hardcode it
* and prefer the return value of the function */
rc = mutt_b64_encode(bctx->buffer, bctx->size, encoded, sizeof(encoded));
for (size_t i = 0; i < rc; i++)
{
fputc(encoded[i], fp_out);
bctx->linelen++;
}
bctx->size = 0;
}
/**
* b64_putc - Base64-encode one character
* @param bctx Cursor for the base64 conversion
* @param c Character to encode
* @param fp_out File to save the output
*/
static void b64_putc(struct B64Context *bctx, char c, FILE *fp_out)
{
if (bctx->size == 3)
b64_flush(bctx, fp_out);
bctx->buffer[bctx->size++] = c;
}
/**
* encode_base64 - Base64-encode some data
* @param fc Cursor for converting a file's encoding
* @param fp_out File to store the result
* @param istext Is the input text?
*/
static void encode_base64(struct FgetConv *fc, FILE *fp_out, int istext)
{
struct B64Context bctx = { 0 };
int ch, ch1 = EOF;
while ((ch = mutt_ch_fgetconv(fc)) != EOF)
{
if (SigInt)
{
SigInt = false;
return;
}
if (istext && (ch == '\n') && (ch1 != '\r'))
b64_putc(&bctx, '\r', fp_out);
b64_putc(&bctx, ch, fp_out);
ch1 = ch;
}
b64_flush(&bctx, fp_out);
fputc('\n', fp_out);
}
/**
* encode_8bit - Write the data as raw 8-bit data
* @param fc Cursor for converting a file's encoding
* @param fp_out File to store the result
*/
static void encode_8bit(struct FgetConv *fc, FILE *fp_out)
{
int ch;
while ((ch = mutt_ch_fgetconv(fc)) != EOF)
{
if (SigInt)
{
SigInt = false;
return;
}
fputc(ch, fp_out);
}
}
/**
* encode_quoted - Encode text as quoted printable
* @param fc Cursor for converting a file's encoding
* @param fp_out File to store the result
* @param istext Is the input text?
*/
static void encode_quoted(struct FgetConv *fc, FILE *fp_out, bool istext)
{
int c, linelen = 0;
char line[77] = { 0 };
while ((c = mutt_ch_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, fp_out);
fputs("=\n", fp_out);
line[linelen] = 0;
line[0] = '=';
line[1] = line[linelen - 2];
line[2] = line[linelen - 1];
linelen = 3;
}
else
{
char savechar = line[linelen - 1];
line[linelen - 1] = '=';
line[linelen] = 0;
fputs(line, fp_out);
fputc('\n', fp_out);
line[0] = savechar;
linelen = 1;
}
}
/* Escape lines that begin with/only contain "the message separator". */
if ((linelen == 4) && mutt_str_startswith(line, "From"))
{
mutt_str_copy(line, "=46rom", sizeof(line));
linelen = 6;
}
else if ((linelen == 4) && mutt_str_startswith(line, "from"))
{
mutt_str_copy(line, "=66rom", sizeof(line));
linelen = 6;
}
else if ((linelen == 1) && (line[0] == '.'))
{
mutt_str_copy(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, fp_out);
}
else
{
int savechar2 = line[linelen - 1];
line[linelen - 1] = '=';
line[linelen] = 0;
fputs(line, fp_out);
fprintf(fp_out, "\n=%2.2X", (unsigned char) savechar2);
}
}
else
{
line[linelen] = 0;
fputs(line, fp_out);
}
fputc('\n', fp_out);
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, fp_out);
fputc('\n', fp_out);
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
{
char savechar = line[linelen - 1];
line[linelen - 1] = '=';
line[linelen] = 0;
fputs(line, fp_out);
fputc('\n', fp_out);
snprintf(line, sizeof(line), "=%2.2X", (unsigned char) savechar);
}
}
else
{
line[linelen] = 0;
}
fputs(line, fp_out);
}
}
/**
* write_as_text_part - Should the Body be written as a text MIME part
* @param b Email to examine
* @retval true The Body should be written as text
*/
static bool write_as_text_part(struct Body *b)
{
return mutt_is_text_part(b) ||
(((WithCrypto & APPLICATION_PGP) != 0) && mutt_is_application_pgp(b));
}
/**
* mutt_write_mime_body - Write a MIME part
* @param b Body to use
* @param fp File to write to
* @param sub Config Subset
* @retval 0 Success
* @retval -1 Failure
*/
int mutt_write_mime_body(struct Body *b, FILE *fp, struct ConfigSubset *sub)
{
FILE *fp_in = NULL;
struct FgetConv *fc = NULL;
if (b->type == TYPE_MULTIPART)
{
/* First, find the boundary to use */
const char *p = mutt_param_get(&b->parameter, "boundary");
if (!p)
{
mutt_debug(LL_DEBUG1, "no boundary parameter found\n");
mutt_error(_("No boundary parameter found [report this error]"));
return -1;
}
char boundary[128] = { 0 };
mutt_str_copy(boundary, p, sizeof(boundary));
for (struct Body *t = b->parts; t; t = t->next)
{
fprintf(fp, "\n--%s\n", boundary);
if (mutt_write_mime_header(t, fp, sub) == -1)
return -1;
fputc('\n', fp);
if (mutt_write_mime_body(t, fp, sub) == -1)
return -1;
}
fprintf(fp, "\n--%s--\n", boundary);
return ferror(fp) ? -1 : 0;
}
/* This is pretty gross, but it's the best solution for now... */
if (((WithCrypto & APPLICATION_PGP) != 0) && (b->type == TYPE_APPLICATION) &&
mutt_str_equal(b->subtype, "pgp-encrypted") && !b->filename)
{
fputs("Version: 1\n", fp);
return 0;
}
fp_in = mutt_file_fopen(b->filename, "r");
if (!fp_in)
{
mutt_debug(LL_DEBUG1, "%s no longer exists\n", b->filename);
mutt_error(_("%s no longer exists"), b->filename);
return -1;
}
if ((b->type == TYPE_TEXT) && (!b->noconv))
{
char send_charset[128] = { 0 };
fc = mutt_ch_fgetconv_open(fp_in, b->charset,
mutt_body_get_charset(b, send_charset, sizeof(send_charset)),
MUTT_ICONV_NO_FLAGS);
}
else
{
fc = mutt_ch_fgetconv_open(fp_in, 0, 0, MUTT_ICONV_NO_FLAGS);
}
mutt_sig_allow_interrupt(true);
if (b->encoding == ENC_QUOTED_PRINTABLE)
encode_quoted(fc, fp, write_as_text_part(b));
else if (b->encoding == ENC_BASE64)
encode_base64(fc, fp, write_as_text_part(b));
else if ((b->type == TYPE_TEXT) && (!b->noconv))
encode_8bit(fc, fp);
else
mutt_file_copy_stream(fp_in, fp);
mutt_sig_allow_interrupt(false);
mutt_ch_fgetconv_close(&fc);
mutt_file_fclose(&fp_in);
if (SigInt)
{
SigInt = false;
return -1;
}
return ferror(fp) ? -1 : 0;
}