forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_info.c
279 lines (252 loc) · 6.75 KB
/
content_info.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
/**
* @file
* Extracting content info from email body.
*
* @authors
* Copyright (C) 2022 Michal Siedlaczek <[email protected]>
* Copyright (C) 2022-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 convert_content_info Content Info Extraction
*
* Extracting content info from email body.
*/
#include "config.h"
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "lib.h"
/**
* mutt_update_content_info - Cache some info about an email
* @param info Info about an Attachment
* @param s Info about the Body of an email
* @param buf Buffer for the result
* @param buflen Length of the buffer
*/
void mutt_update_content_info(struct Content *info, struct ContentState *s,
char *buf, size_t buflen)
{
bool from = s->from;
int whitespace = s->whitespace;
bool dot = s->dot;
int linelen = s->linelen;
bool was_cr = s->was_cr;
if (!buf) /* This signals EOF */
{
if (was_cr)
info->binary = true;
if (linelen > info->linemax)
info->linemax = linelen;
return;
}
for (; buflen; buf++, buflen--)
{
char ch = *buf;
if (was_cr)
{
was_cr = false;
if (ch == '\n')
{
if (whitespace)
info->space = true;
if (dot)
info->dot = true;
if (linelen > info->linemax)
info->linemax = linelen;
whitespace = 0;
dot = false;
linelen = 0;
continue;
}
info->binary = true;
}
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 = false;
}
else if (ch == '\r')
{
info->crlf++;
info->cr = true;
was_cr = true;
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 = true;
else
from = false;
if (ch == '.')
dot = true;
else
dot = false;
}
else if (from)
{
if ((linelen == 2) && (ch != 'r'))
{
from = false;
}
else if ((linelen == 3) && (ch != 'o'))
{
from = false;
}
else if (linelen == 4)
{
if (ch == 'm')
info->from = true;
from = false;
}
}
if (ch == ' ')
whitespace++;
info->ascii++;
}
if (linelen > 1)
dot = false;
if ((ch != ' ') && (ch != '\t'))
whitespace = 0;
}
s->from = from;
s->whitespace = whitespace;
s->dot = dot;
s->linelen = linelen;
s->was_cr = was_cr;
}
/**
* mutt_get_content_info - Analyze file to determine MIME encoding to use
* @param fname File to examine
* @param b Body to update
* @param sub Config Subset
* @retval ptr Newly allocated Content
*
* Also set the body charset, sometimes, or not.
*/
struct Content *mutt_get_content_info(const char *fname, struct Body *b,
struct ConfigSubset *sub)
{
struct Content *info = NULL;
struct ContentState cstate = { 0 };
FILE *fp = NULL;
char *fromcode = NULL;
char *tocode = NULL;
char buf[100] = { 0 };
size_t r;
struct stat st = { 0 };
if (b && !fname)
fname = b->filename;
if (!fname)
return NULL;
if (stat(fname, &st) == -1)
{
mutt_error(_("Can't stat %s: %s"), fname, strerror(errno));
return NULL;
}
if (!S_ISREG(st.st_mode))
{
mutt_error(_("%s isn't a regular file"), fname);
return NULL;
}
fp = mutt_file_fopen(fname, "r");
if (!fp)
{
mutt_debug(LL_DEBUG1, "%s: %s (errno %d)\n", fname, strerror(errno), errno);
return NULL;
}
info = mutt_mem_calloc(1, sizeof(struct Content));
const char *const c_charset = cc_charset();
if (b && (b->type == TYPE_TEXT) && (!b->noconv && !b->force_charset))
{
const struct Slist *const c_attach_charset = cs_subset_slist(sub, "attach_charset");
const struct Slist *const c_send_charset = cs_subset_slist(sub, "send_charset");
struct Slist *c_charset_slist = slist_parse(c_charset, D_SLIST_SEP_COLON);
const struct Slist *fchs = b->use_disp ?
(c_attach_charset ? c_attach_charset : c_charset_slist) :
c_charset_slist;
struct Slist *chs = slist_parse(mutt_param_get(&b->parameter, "charset"), D_SLIST_SEP_COLON);
if (c_charset && (chs || c_send_charset) &&
(mutt_convert_file_from_to(fp, fchs, chs ? chs : c_send_charset, &fromcode,
&tocode, info) != ICONV_ILLEGAL_SEQ))
{
if (!chs)
{
char chsbuf[256] = { 0 };
mutt_ch_canonical_charset(chsbuf, sizeof(chsbuf), tocode);
mutt_param_set(&b->parameter, "charset", chsbuf);
}
FREE(&b->charset);
b->charset = mutt_str_dup(fromcode);
FREE(&tocode);
mutt_file_fclose(&fp);
slist_free(&c_charset_slist);
slist_free(&chs);
return info;
}
slist_free(&c_charset_slist);
slist_free(&chs);
}
rewind(fp);
while ((r = fread(buf, 1, sizeof(buf), fp)))
mutt_update_content_info(info, &cstate, buf, r);
mutt_update_content_info(info, &cstate, 0, 0);
mutt_file_fclose(&fp);
if (b && (b->type == TYPE_TEXT) && (!b->noconv && !b->force_charset))
{
mutt_param_set(&b->parameter, "charset",
(!info->hibin ? "us-ascii" :
c_charset && !mutt_ch_is_us_ascii(c_charset) ? c_charset :
"unknown-8bit"));
}
return info;
}