]>
Commit | Line | Data |
---|---|---|
c6905e45 | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
c6905e45 JH |
3 | #include "utf8.h" |
4 | #include "strbuf.h" | |
5 | #include "mailinfo.h" | |
6 | ||
7 | static void cleanup_space(struct strbuf *sb) | |
8 | { | |
9 | size_t pos, cnt; | |
10 | for (pos = 0; pos < sb->len; pos++) { | |
11 | if (isspace(sb->buf[pos])) { | |
12 | sb->buf[pos] = ' '; | |
13 | for (cnt = 0; isspace(sb->buf[pos + cnt + 1]); cnt++); | |
14 | strbuf_remove(sb, pos + 1, cnt); | |
15 | } | |
16 | } | |
17 | } | |
18 | ||
19 | static void get_sane_name(struct strbuf *out, struct strbuf *name, struct strbuf *email) | |
20 | { | |
21 | struct strbuf *src = name; | |
72ee47ce | 22 | if (!name->len || 60 < name->len || strpbrk(name->buf, "@<>")) |
c6905e45 JH |
23 | src = email; |
24 | else if (name == out) | |
25 | return; | |
26 | strbuf_reset(out); | |
27 | strbuf_addbuf(out, src); | |
28 | } | |
29 | ||
30 | static void parse_bogus_from(struct mailinfo *mi, const struct strbuf *line) | |
31 | { | |
32 | /* John Doe <johndoe> */ | |
33 | ||
34 | char *bra, *ket; | |
35 | /* This is fallback, so do not bother if we already have an | |
36 | * e-mail address. | |
37 | */ | |
38 | if (mi->email.len) | |
39 | return; | |
40 | ||
41 | bra = strchr(line->buf, '<'); | |
42 | if (!bra) | |
43 | return; | |
44 | ket = strchr(bra, '>'); | |
45 | if (!ket) | |
46 | return; | |
47 | ||
48 | strbuf_reset(&mi->email); | |
49 | strbuf_add(&mi->email, bra + 1, ket - bra - 1); | |
50 | ||
51 | strbuf_reset(&mi->name); | |
52 | strbuf_add(&mi->name, line->buf, bra - line->buf); | |
53 | strbuf_trim(&mi->name); | |
54 | get_sane_name(&mi->name, &mi->name, &mi->email); | |
55 | } | |
56 | ||
f357e5de KD |
57 | static const char *unquote_comment(struct strbuf *outbuf, const char *in) |
58 | { | |
64127575 | 59 | int take_next_literally = 0; |
dee18294 | 60 | int depth = 1; |
f357e5de KD |
61 | |
62 | strbuf_addch(outbuf, '('); | |
63 | ||
d1bd3a8c JK |
64 | while (*in) { |
65 | int c = *in++; | |
64127575 VS |
66 | if (take_next_literally == 1) { |
67 | take_next_literally = 0; | |
f357e5de KD |
68 | } else { |
69 | switch (c) { | |
70 | case '\\': | |
64127575 | 71 | take_next_literally = 1; |
f357e5de KD |
72 | continue; |
73 | case '(': | |
dee18294 JK |
74 | strbuf_addch(outbuf, '('); |
75 | depth++; | |
f357e5de KD |
76 | continue; |
77 | case ')': | |
78 | strbuf_addch(outbuf, ')'); | |
dee18294 JK |
79 | if (!--depth) |
80 | return in; | |
81 | continue; | |
f357e5de KD |
82 | } |
83 | } | |
84 | ||
85 | strbuf_addch(outbuf, c); | |
86 | } | |
87 | ||
88 | return in; | |
89 | } | |
90 | ||
91 | static const char *unquote_quoted_string(struct strbuf *outbuf, const char *in) | |
92 | { | |
64127575 | 93 | int take_next_literally = 0; |
f357e5de | 94 | |
d1bd3a8c JK |
95 | while (*in) { |
96 | int c = *in++; | |
64127575 VS |
97 | if (take_next_literally == 1) { |
98 | take_next_literally = 0; | |
f357e5de KD |
99 | } else { |
100 | switch (c) { | |
101 | case '\\': | |
64127575 | 102 | take_next_literally = 1; |
f357e5de KD |
103 | continue; |
104 | case '"': | |
105 | return in; | |
106 | } | |
107 | } | |
108 | ||
109 | strbuf_addch(outbuf, c); | |
110 | } | |
111 | ||
112 | return in; | |
113 | } | |
114 | ||
115 | static void unquote_quoted_pair(struct strbuf *line) | |
116 | { | |
117 | struct strbuf outbuf; | |
118 | const char *in = line->buf; | |
119 | int c; | |
120 | ||
121 | strbuf_init(&outbuf, line->len); | |
122 | ||
123 | while ((c = *in++) != 0) { | |
124 | switch (c) { | |
125 | case '"': | |
126 | in = unquote_quoted_string(&outbuf, in); | |
127 | continue; | |
128 | case '(': | |
129 | in = unquote_comment(&outbuf, in); | |
130 | continue; | |
131 | } | |
132 | ||
133 | strbuf_addch(&outbuf, c); | |
134 | } | |
135 | ||
136 | strbuf_swap(&outbuf, line); | |
137 | strbuf_release(&outbuf); | |
138 | ||
139 | } | |
140 | ||
c6905e45 JH |
141 | static void handle_from(struct mailinfo *mi, const struct strbuf *from) |
142 | { | |
143 | char *at; | |
144 | size_t el; | |
145 | struct strbuf f; | |
146 | ||
147 | strbuf_init(&f, from->len); | |
148 | strbuf_addbuf(&f, from); | |
149 | ||
f357e5de KD |
150 | unquote_quoted_pair(&f); |
151 | ||
c6905e45 JH |
152 | at = strchr(f.buf, '@'); |
153 | if (!at) { | |
154 | parse_bogus_from(mi, from); | |
11fa5e2a | 155 | goto out; |
c6905e45 JH |
156 | } |
157 | ||
158 | /* | |
159 | * If we already have one email, don't take any confusing lines | |
160 | */ | |
11fa5e2a RS |
161 | if (mi->email.len && strchr(at + 1, '@')) |
162 | goto out; | |
c6905e45 JH |
163 | |
164 | /* Pick up the string around '@', possibly delimited with <> | |
165 | * pair; that is the email part. | |
166 | */ | |
167 | while (at > f.buf) { | |
168 | char c = at[-1]; | |
169 | if (isspace(c)) | |
170 | break; | |
171 | if (c == '<') { | |
172 | at[-1] = ' '; | |
173 | break; | |
174 | } | |
175 | at--; | |
176 | } | |
177 | el = strcspn(at, " \n\t\r\v\f>"); | |
178 | strbuf_reset(&mi->email); | |
179 | strbuf_add(&mi->email, at, el); | |
180 | strbuf_remove(&f, at - f.buf, el + (at[el] ? 1 : 0)); | |
181 | ||
182 | /* The remainder is name. It could be | |
183 | * | |
184 | * - "John Doe <john.doe@xz>" (a), or | |
185 | * - "john.doe@xz (John Doe)" (b), or | |
186 | * - "John (zzz) Doe <john.doe@xz> (Comment)" (c) | |
187 | * | |
188 | * but we have removed the email part, so | |
189 | * | |
190 | * - remove extra spaces which could stay after email (case 'c'), and | |
191 | * - trim from both ends, possibly removing the () pair at the end | |
192 | * (cases 'a' and 'b'). | |
193 | */ | |
194 | cleanup_space(&f); | |
195 | strbuf_trim(&f); | |
196 | if (f.buf[0] == '(' && f.len && f.buf[f.len - 1] == ')') { | |
197 | strbuf_remove(&f, 0, 1); | |
198 | strbuf_setlen(&f, f.len - 1); | |
199 | } | |
200 | ||
201 | get_sane_name(&mi->name, &f, &mi->email); | |
11fa5e2a | 202 | out: |
c6905e45 JH |
203 | strbuf_release(&f); |
204 | } | |
205 | ||
206 | static void handle_header(struct strbuf **out, const struct strbuf *line) | |
207 | { | |
208 | if (!*out) { | |
209 | *out = xmalloc(sizeof(struct strbuf)); | |
210 | strbuf_init(*out, line->len); | |
211 | } else | |
212 | strbuf_reset(*out); | |
213 | ||
214 | strbuf_addbuf(*out, line); | |
215 | } | |
216 | ||
217 | /* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt | |
218 | * to have enough heuristics to grok MIME encoded patches often found | |
219 | * on our mailing lists. For example, we do not even treat header lines | |
220 | * case insensitively. | |
221 | */ | |
222 | ||
223 | static int slurp_attr(const char *line, const char *name, struct strbuf *attr) | |
224 | { | |
225 | const char *ends, *ap = strcasestr(line, name); | |
226 | size_t sz; | |
227 | ||
228 | strbuf_setlen(attr, 0); | |
229 | if (!ap) | |
230 | return 0; | |
231 | ap += strlen(name); | |
232 | if (*ap == '"') { | |
233 | ap++; | |
234 | ends = "\""; | |
235 | } | |
236 | else | |
237 | ends = "; \t"; | |
238 | sz = strcspn(ap, ends); | |
239 | strbuf_add(attr, ap, sz); | |
240 | return 1; | |
241 | } | |
242 | ||
3aa4d81f RS |
243 | static int has_attr_value(const char *line, const char *name, const char *value) |
244 | { | |
245 | struct strbuf sb = STRBUF_INIT; | |
246 | int rc = slurp_attr(line, name, &sb) && !strcasecmp(sb.buf, value); | |
247 | strbuf_release(&sb); | |
248 | return rc; | |
249 | } | |
250 | ||
c6905e45 JH |
251 | static void handle_content_type(struct mailinfo *mi, struct strbuf *line) |
252 | { | |
253 | struct strbuf *boundary = xmalloc(sizeof(struct strbuf)); | |
254 | strbuf_init(boundary, line->len); | |
255 | ||
3aa4d81f RS |
256 | mi->format_flowed = has_attr_value(line->buf, "format=", "flowed"); |
257 | mi->delsp = has_attr_value(line->buf, "delsp=", "yes"); | |
258 | ||
c6905e45 | 259 | if (slurp_attr(line->buf, "boundary=", boundary)) { |
a91cc7fa | 260 | strbuf_insertstr(boundary, 0, "--"); |
c6905e45 | 261 | if (++mi->content_top >= &mi->content[MAX_BOUNDARIES]) { |
6ac617a3 JH |
262 | error("Too many boundaries to handle"); |
263 | mi->input_error = -1; | |
264 | mi->content_top = &mi->content[MAX_BOUNDARIES] - 1; | |
265 | return; | |
c6905e45 JH |
266 | } |
267 | *(mi->content_top) = boundary; | |
268 | boundary = NULL; | |
269 | } | |
270 | slurp_attr(line->buf, "charset=", &mi->charset); | |
271 | ||
272 | if (boundary) { | |
273 | strbuf_release(boundary); | |
274 | free(boundary); | |
275 | } | |
276 | } | |
277 | ||
c6905e45 JH |
278 | static void handle_content_transfer_encoding(struct mailinfo *mi, |
279 | const struct strbuf *line) | |
280 | { | |
281 | if (strcasestr(line->buf, "base64")) | |
282 | mi->transfer_encoding = TE_BASE64; | |
283 | else if (strcasestr(line->buf, "quoted-printable")) | |
284 | mi->transfer_encoding = TE_QP; | |
285 | else | |
286 | mi->transfer_encoding = TE_DONTCARE; | |
287 | } | |
288 | ||
289 | static int is_multipart_boundary(struct mailinfo *mi, const struct strbuf *line) | |
290 | { | |
291 | struct strbuf *content_top = *(mi->content_top); | |
292 | ||
293 | return ((content_top->len <= line->len) && | |
294 | !memcmp(line->buf, content_top->buf, content_top->len)); | |
295 | } | |
296 | ||
297 | static void cleanup_subject(struct mailinfo *mi, struct strbuf *subject) | |
298 | { | |
299 | size_t at = 0; | |
300 | ||
301 | while (at < subject->len) { | |
302 | char *pos; | |
303 | size_t remove; | |
304 | ||
305 | switch (subject->buf[at]) { | |
306 | case 'r': case 'R': | |
307 | if (subject->len <= at + 3) | |
308 | break; | |
309 | if ((subject->buf[at + 1] == 'e' || | |
310 | subject->buf[at + 1] == 'E') && | |
311 | subject->buf[at + 2] == ':') { | |
312 | strbuf_remove(subject, at, 3); | |
313 | continue; | |
314 | } | |
315 | at++; | |
316 | break; | |
317 | case ' ': case '\t': case ':': | |
318 | strbuf_remove(subject, at, 1); | |
319 | continue; | |
320 | case '[': | |
321 | pos = strchr(subject->buf + at, ']'); | |
322 | if (!pos) | |
323 | break; | |
3ef14946 | 324 | remove = pos - (subject->buf + at) + 1; |
c6905e45 JH |
325 | if (!mi->keep_non_patch_brackets_in_subject || |
326 | (7 <= remove && | |
327 | memmem(subject->buf + at, remove, "PATCH", 5))) | |
328 | strbuf_remove(subject, at, remove); | |
329 | else { | |
330 | at += remove; | |
331 | /* | |
332 | * If the input had a space after the ], keep | |
333 | * it. We don't bother with finding the end of | |
334 | * the space, since we later normalize it | |
335 | * anyway. | |
336 | */ | |
337 | if (isspace(subject->buf[at])) | |
338 | at += 1; | |
339 | } | |
340 | continue; | |
341 | } | |
342 | break; | |
343 | } | |
344 | strbuf_trim(subject); | |
345 | } | |
346 | ||
347 | #define MAX_HDR_PARSED 10 | |
348 | static const char *header[MAX_HDR_PARSED] = { | |
349 | "From","Subject","Date", | |
350 | }; | |
351 | ||
f447d029 JK |
352 | static inline int skip_header(const struct strbuf *line, const char *hdr, |
353 | const char **outval) | |
c6905e45 | 354 | { |
f447d029 JK |
355 | const char *val; |
356 | if (!skip_iprefix(line->buf, hdr, &val) || | |
ffbea181 | 357 | *val++ != ':') |
f447d029 | 358 | return 0; |
ffbea181 JK |
359 | while (isspace(*val)) |
360 | val++; | |
f447d029 JK |
361 | *outval = val; |
362 | return 1; | |
c6905e45 JH |
363 | } |
364 | ||
365 | static int is_format_patch_separator(const char *line, int len) | |
366 | { | |
367 | static const char SAMPLE[] = | |
368 | "From e6807f3efca28b30decfecb1732a56c7db1137ee Mon Sep 17 00:00:00 2001\n"; | |
369 | const char *cp; | |
370 | ||
371 | if (len != strlen(SAMPLE)) | |
372 | return 0; | |
373 | if (!skip_prefix(line, "From ", &cp)) | |
374 | return 0; | |
375 | if (strspn(cp, "0123456789abcdef") != 40) | |
376 | return 0; | |
377 | cp += 40; | |
378 | return !memcmp(SAMPLE + (cp - line), cp, strlen(SAMPLE) - (cp - line)); | |
379 | } | |
380 | ||
381 | static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047) | |
382 | { | |
383 | const char *in = q_seg->buf; | |
384 | int c; | |
385 | struct strbuf *out = xmalloc(sizeof(struct strbuf)); | |
386 | strbuf_init(out, q_seg->len); | |
387 | ||
388 | while ((c = *in++) != 0) { | |
389 | if (c == '=') { | |
c8cf423e | 390 | int ch, d = *in; |
c6905e45 JH |
391 | if (d == '\n' || !d) |
392 | break; /* drop trailing newline */ | |
c8cf423e RS |
393 | ch = hex2chr(in); |
394 | if (ch >= 0) { | |
395 | strbuf_addch(out, ch); | |
396 | in += 2; | |
397 | continue; | |
398 | } | |
399 | /* garbage -- fall through */ | |
c6905e45 JH |
400 | } |
401 | if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */ | |
402 | c = 0x20; | |
403 | strbuf_addch(out, c); | |
404 | } | |
405 | return out; | |
406 | } | |
407 | ||
408 | static struct strbuf *decode_b_segment(const struct strbuf *b_seg) | |
409 | { | |
410 | /* Decode in..ep, possibly in-place to ot */ | |
411 | int c, pos = 0, acc = 0; | |
412 | const char *in = b_seg->buf; | |
413 | struct strbuf *out = xmalloc(sizeof(struct strbuf)); | |
414 | strbuf_init(out, b_seg->len); | |
415 | ||
416 | while ((c = *in++) != 0) { | |
417 | if (c == '+') | |
418 | c = 62; | |
419 | else if (c == '/') | |
420 | c = 63; | |
421 | else if ('A' <= c && c <= 'Z') | |
422 | c -= 'A'; | |
423 | else if ('a' <= c && c <= 'z') | |
424 | c -= 'a' - 26; | |
425 | else if ('0' <= c && c <= '9') | |
426 | c -= '0' - 52; | |
427 | else | |
428 | continue; /* garbage */ | |
429 | switch (pos++) { | |
430 | case 0: | |
431 | acc = (c << 2); | |
432 | break; | |
433 | case 1: | |
434 | strbuf_addch(out, (acc | (c >> 4))); | |
435 | acc = (c & 15) << 4; | |
436 | break; | |
437 | case 2: | |
438 | strbuf_addch(out, (acc | (c >> 2))); | |
439 | acc = (c & 3) << 6; | |
440 | break; | |
441 | case 3: | |
442 | strbuf_addch(out, (acc | c)); | |
443 | acc = pos = 0; | |
444 | break; | |
445 | } | |
446 | } | |
447 | return out; | |
448 | } | |
449 | ||
669b963a JH |
450 | static int convert_to_utf8(struct mailinfo *mi, |
451 | struct strbuf *line, const char *charset) | |
c6905e45 JH |
452 | { |
453 | char *out; | |
2a2ff603 | 454 | size_t out_len; |
c6905e45 JH |
455 | |
456 | if (!mi->metainfo_charset || !charset || !*charset) | |
669b963a | 457 | return 0; |
c6905e45 JH |
458 | |
459 | if (same_encoding(mi->metainfo_charset, charset)) | |
669b963a | 460 | return 0; |
2a2ff603 ĐTCD |
461 | out = reencode_string_len(line->buf, line->len, |
462 | mi->metainfo_charset, charset, &out_len); | |
6ac617a3 JH |
463 | if (!out) { |
464 | mi->input_error = -1; | |
669b963a JH |
465 | return error("cannot convert from %s to %s", |
466 | charset, mi->metainfo_charset); | |
6ac617a3 | 467 | } |
2a2ff603 | 468 | strbuf_attach(line, out, out_len, out_len); |
669b963a | 469 | return 0; |
c6905e45 JH |
470 | } |
471 | ||
472 | static void decode_header(struct mailinfo *mi, struct strbuf *it) | |
473 | { | |
474 | char *in, *ep, *cp; | |
475 | struct strbuf outbuf = STRBUF_INIT, *dec; | |
476 | struct strbuf charset_q = STRBUF_INIT, piecebuf = STRBUF_INIT; | |
6ac617a3 | 477 | int found_error = 1; /* pessimism */ |
c6905e45 JH |
478 | |
479 | in = it->buf; | |
480 | while (in - it->buf <= it->len && (ep = strstr(in, "=?")) != NULL) { | |
481 | int encoding; | |
482 | strbuf_reset(&charset_q); | |
483 | strbuf_reset(&piecebuf); | |
484 | ||
485 | if (in != ep) { | |
486 | /* | |
487 | * We are about to process an encoded-word | |
488 | * that begins at ep, but there is something | |
489 | * before the encoded word. | |
490 | */ | |
491 | char *scan; | |
492 | for (scan = in; scan < ep; scan++) | |
493 | if (!isspace(*scan)) | |
494 | break; | |
495 | ||
496 | if (scan != ep || in == it->buf) { | |
497 | /* | |
498 | * We should not lose that "something", | |
499 | * unless we have just processed an | |
500 | * encoded-word, and there is only LWS | |
501 | * before the one we are about to process. | |
502 | */ | |
503 | strbuf_add(&outbuf, in, ep - in); | |
504 | } | |
505 | } | |
506 | /* E.g. | |
507 | * ep : "=?iso-2022-jp?B?GyR...?= foo" | |
508 | * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz" | |
509 | */ | |
510 | ep += 2; | |
511 | ||
512 | if (ep - it->buf >= it->len || !(cp = strchr(ep, '?'))) | |
513 | goto release_return; | |
514 | ||
515 | if (cp + 3 - it->buf > it->len) | |
516 | goto release_return; | |
517 | strbuf_add(&charset_q, ep, cp - ep); | |
518 | ||
519 | encoding = cp[1]; | |
520 | if (!encoding || cp[2] != '?') | |
521 | goto release_return; | |
522 | ep = strstr(cp + 3, "?="); | |
523 | if (!ep) | |
524 | goto release_return; | |
525 | strbuf_add(&piecebuf, cp + 3, ep - cp - 3); | |
526 | switch (tolower(encoding)) { | |
527 | default: | |
528 | goto release_return; | |
529 | case 'b': | |
530 | dec = decode_b_segment(&piecebuf); | |
531 | break; | |
532 | case 'q': | |
533 | dec = decode_q_segment(&piecebuf, 1); | |
534 | break; | |
535 | } | |
669b963a JH |
536 | if (convert_to_utf8(mi, dec, charset_q.buf)) |
537 | goto release_return; | |
c6905e45 JH |
538 | |
539 | strbuf_addbuf(&outbuf, dec); | |
540 | strbuf_release(dec); | |
541 | free(dec); | |
542 | in = ep + 2; | |
543 | } | |
544 | strbuf_addstr(&outbuf, in); | |
545 | strbuf_reset(it); | |
546 | strbuf_addbuf(it, &outbuf); | |
6ac617a3 | 547 | found_error = 0; |
c6905e45 JH |
548 | release_return: |
549 | strbuf_release(&outbuf); | |
550 | strbuf_release(&charset_q); | |
551 | strbuf_release(&piecebuf); | |
6ac617a3 JH |
552 | |
553 | if (found_error) | |
554 | mi->input_error = -1; | |
c6905e45 JH |
555 | } |
556 | ||
f696a2b1 JK |
557 | /* |
558 | * Returns true if "line" contains a header matching "hdr", in which case "val" | |
559 | * will contain the value of the header with any RFC2047 B and Q encoding | |
560 | * unwrapped, and optionally normalize the meta information to utf8. | |
561 | */ | |
562 | static int parse_header(const struct strbuf *line, | |
563 | const char *hdr, | |
564 | struct mailinfo *mi, | |
565 | struct strbuf *val) | |
566 | { | |
567 | const char *val_str; | |
568 | ||
569 | if (!skip_header(line, hdr, &val_str)) | |
570 | return 0; | |
571 | strbuf_addstr(val, val_str); | |
572 | decode_header(mi, val); | |
573 | return 1; | |
574 | } | |
575 | ||
c6905e45 JH |
576 | static int check_header(struct mailinfo *mi, |
577 | const struct strbuf *line, | |
578 | struct strbuf *hdr_data[], int overwrite) | |
579 | { | |
f447d029 | 580 | int i, ret = 0; |
c6905e45 JH |
581 | struct strbuf sb = STRBUF_INIT; |
582 | ||
583 | /* search for the interesting parts */ | |
584 | for (i = 0; header[i]; i++) { | |
f447d029 | 585 | if ((!hdr_data[i] || overwrite) && |
f696a2b1 | 586 | parse_header(line, header[i], mi, &sb)) { |
c6905e45 JH |
587 | handle_header(&hdr_data[i], &sb); |
588 | ret = 1; | |
589 | goto check_header_out; | |
590 | } | |
591 | } | |
592 | ||
593 | /* Content stuff */ | |
f696a2b1 | 594 | if (parse_header(line, "Content-Type", mi, &sb)) { |
c6905e45 JH |
595 | handle_content_type(mi, &sb); |
596 | ret = 1; | |
597 | goto check_header_out; | |
598 | } | |
f696a2b1 | 599 | if (parse_header(line, "Content-Transfer-Encoding", mi, &sb)) { |
c6905e45 JH |
600 | handle_content_transfer_encoding(mi, &sb); |
601 | ret = 1; | |
602 | goto check_header_out; | |
603 | } | |
f696a2b1 | 604 | if (parse_header(line, "Message-Id", mi, &sb)) { |
ecf30b23 RS |
605 | if (mi->add_message_id) |
606 | mi->message_id = strbuf_detach(&sb, NULL); | |
c6905e45 JH |
607 | ret = 1; |
608 | goto check_header_out; | |
609 | } | |
610 | ||
c6905e45 JH |
611 | check_header_out: |
612 | strbuf_release(&sb); | |
613 | return ret; | |
614 | } | |
615 | ||
6b4b013f JT |
616 | /* |
617 | * Returns 1 if the given line or any line beginning with the given line is an | |
618 | * in-body header (that is, check_header will succeed when passed | |
619 | * mi->s_hdr_data). | |
620 | */ | |
621 | static int is_inbody_header(const struct mailinfo *mi, | |
622 | const struct strbuf *line) | |
623 | { | |
624 | int i; | |
f447d029 | 625 | const char *val; |
6b4b013f | 626 | for (i = 0; header[i]; i++) |
f447d029 | 627 | if (!mi->s_hdr_data[i] && skip_header(line, header[i], &val)) |
6b4b013f JT |
628 | return 1; |
629 | return 0; | |
630 | } | |
631 | ||
c6905e45 JH |
632 | static void decode_transfer_encoding(struct mailinfo *mi, struct strbuf *line) |
633 | { | |
634 | struct strbuf *ret; | |
635 | ||
636 | switch (mi->transfer_encoding) { | |
637 | case TE_QP: | |
638 | ret = decode_q_segment(line, 0); | |
639 | break; | |
640 | case TE_BASE64: | |
641 | ret = decode_b_segment(line); | |
642 | break; | |
643 | case TE_DONTCARE: | |
644 | default: | |
645 | return; | |
646 | } | |
647 | strbuf_reset(line); | |
648 | strbuf_addbuf(line, ret); | |
649 | strbuf_release(ret); | |
650 | free(ret); | |
651 | } | |
652 | ||
653 | static inline int patchbreak(const struct strbuf *line) | |
654 | { | |
655 | size_t i; | |
656 | ||
657 | /* Beginning of a "diff -" header? */ | |
658 | if (starts_with(line->buf, "diff -")) | |
659 | return 1; | |
660 | ||
661 | /* CVS "Index: " line? */ | |
662 | if (starts_with(line->buf, "Index: ")) | |
663 | return 1; | |
664 | ||
665 | /* | |
666 | * "--- <filename>" starts patches without headers | |
667 | * "---<sp>*" is a manual separator | |
668 | */ | |
669 | if (line->len < 4) | |
670 | return 0; | |
671 | ||
672 | if (starts_with(line->buf, "---")) { | |
673 | /* space followed by a filename? */ | |
674 | if (line->buf[3] == ' ' && !isspace(line->buf[4])) | |
675 | return 1; | |
676 | /* Just whitespace? */ | |
677 | for (i = 3; i < line->len; i++) { | |
678 | unsigned char c = line->buf[i]; | |
679 | if (c == '\n') | |
680 | return 1; | |
681 | if (!isspace(c)) | |
682 | break; | |
683 | } | |
684 | return 0; | |
685 | } | |
686 | return 0; | |
687 | } | |
688 | ||
9c5681da | 689 | static int is_scissors_line(const char *line) |
c6905e45 | 690 | { |
9c5681da | 691 | const char *c; |
c6905e45 | 692 | int scissors = 0, gap = 0; |
9c5681da JT |
693 | const char *first_nonblank = NULL, *last_nonblank = NULL; |
694 | int visible, perforation = 0, in_perforation = 0; | |
c6905e45 | 695 | |
9c5681da JT |
696 | for (c = line; *c; c++) { |
697 | if (isspace(*c)) { | |
c6905e45 JH |
698 | if (in_perforation) { |
699 | perforation++; | |
700 | gap++; | |
701 | } | |
702 | continue; | |
703 | } | |
9c5681da | 704 | last_nonblank = c; |
afe8a907 | 705 | if (!first_nonblank) |
9c5681da JT |
706 | first_nonblank = c; |
707 | if (*c == '-') { | |
c6905e45 JH |
708 | in_perforation = 1; |
709 | perforation++; | |
710 | continue; | |
711 | } | |
4184cbd6 AR |
712 | if (starts_with(c, ">8") || starts_with(c, "8<") || |
713 | starts_with(c, ">%") || starts_with(c, "%<")) { | |
c6905e45 JH |
714 | in_perforation = 1; |
715 | perforation += 2; | |
716 | scissors += 2; | |
9c5681da | 717 | c++; |
c6905e45 JH |
718 | continue; |
719 | } | |
720 | in_perforation = 0; | |
721 | } | |
722 | ||
723 | /* | |
724 | * The mark must be at least 8 bytes long (e.g. "-- >8 --"). | |
725 | * Even though there can be arbitrary cruft on the same line | |
726 | * (e.g. "cut here"), in order to avoid misidentification, the | |
727 | * perforation must occupy more than a third of the visible | |
728 | * width of the line, and dashes and scissors must occupy more | |
729 | * than half of the perforation. | |
730 | */ | |
731 | ||
9c5681da JT |
732 | if (first_nonblank && last_nonblank) |
733 | visible = last_nonblank - first_nonblank + 1; | |
734 | else | |
735 | visible = 0; | |
c6905e45 JH |
736 | return (scissors && 8 <= visible && |
737 | visible < perforation * 3 && | |
738 | gap * 2 < perforation); | |
739 | } | |
740 | ||
6b4b013f JT |
741 | static void flush_inbody_header_accum(struct mailinfo *mi) |
742 | { | |
743 | if (!mi->inbody_header_accum.len) | |
744 | return; | |
08414938 | 745 | if (!check_header(mi, &mi->inbody_header_accum, mi->s_hdr_data, 0)) |
033abf97 | 746 | BUG("inbody_header_accum, if not empty, must always contain a valid in-body header"); |
6b4b013f JT |
747 | strbuf_reset(&mi->inbody_header_accum); |
748 | } | |
749 | ||
334192b4 JT |
750 | static int check_inbody_header(struct mailinfo *mi, const struct strbuf *line) |
751 | { | |
6b4b013f JT |
752 | if (mi->inbody_header_accum.len && |
753 | (line->buf[0] == ' ' || line->buf[0] == '\t')) { | |
754 | if (mi->use_scissors && is_scissors_line(line->buf)) { | |
755 | /* | |
756 | * This is a scissors line; do not consider this line | |
757 | * as a header continuation line. | |
758 | */ | |
759 | flush_inbody_header_accum(mi); | |
760 | return 0; | |
761 | } | |
762 | strbuf_strip_suffix(&mi->inbody_header_accum, "\n"); | |
763 | strbuf_addbuf(&mi->inbody_header_accum, line); | |
764 | return 1; | |
765 | } | |
766 | ||
767 | flush_inbody_header_accum(mi); | |
768 | ||
334192b4 JT |
769 | if (starts_with(line->buf, ">From") && isspace(line->buf[5])) |
770 | return is_format_patch_separator(line->buf + 1, line->len - 1); | |
771 | if (starts_with(line->buf, "[PATCH]") && isspace(line->buf[7])) { | |
772 | int i; | |
773 | for (i = 0; header[i]; i++) | |
774 | if (!strcmp("Subject", header[i])) { | |
775 | handle_header(&mi->s_hdr_data[i], line); | |
776 | return 1; | |
777 | } | |
778 | return 0; | |
779 | } | |
6b4b013f JT |
780 | if (is_inbody_header(mi, line)) { |
781 | strbuf_addbuf(&mi->inbody_header_accum, line); | |
782 | return 1; | |
783 | } | |
784 | return 0; | |
334192b4 JT |
785 | } |
786 | ||
c6905e45 JH |
787 | static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line) |
788 | { | |
789 | assert(!mi->filter_stage); | |
790 | ||
791 | if (mi->header_stage) { | |
fd1062e5 LT |
792 | if (!line->len || (line->len == 1 && line->buf[0] == '\n')) { |
793 | if (mi->inbody_header_accum.len) { | |
794 | flush_inbody_header_accum(mi); | |
795 | mi->header_stage = 0; | |
796 | } | |
c6905e45 | 797 | return 0; |
fd1062e5 | 798 | } |
c6905e45 JH |
799 | } |
800 | ||
801 | if (mi->use_inbody_headers && mi->header_stage) { | |
334192b4 | 802 | mi->header_stage = check_inbody_header(mi, line); |
c6905e45 JH |
803 | if (mi->header_stage) |
804 | return 0; | |
805 | } else | |
806 | /* Only trim the first (blank) line of the commit message | |
807 | * when ignoring in-body headers. | |
808 | */ | |
809 | mi->header_stage = 0; | |
810 | ||
811 | /* normalize the log message to UTF-8. */ | |
669b963a | 812 | if (convert_to_utf8(mi, line, mi->charset.buf)) |
6ac617a3 | 813 | return 0; /* mi->input_error already set */ |
c6905e45 | 814 | |
9c5681da | 815 | if (mi->use_scissors && is_scissors_line(line->buf)) { |
c6905e45 JH |
816 | int i; |
817 | ||
818 | strbuf_setlen(&mi->log_message, 0); | |
819 | mi->header_stage = 1; | |
820 | ||
821 | /* | |
822 | * We may have already read "secondary headers"; purge | |
823 | * them to give ourselves a clean restart. | |
824 | */ | |
825 | for (i = 0; header[i]; i++) { | |
826 | if (mi->s_hdr_data[i]) | |
827 | strbuf_release(mi->s_hdr_data[i]); | |
f3a96807 | 828 | FREE_AND_NULL(mi->s_hdr_data[i]); |
c6905e45 JH |
829 | } |
830 | return 0; | |
831 | } | |
832 | ||
833 | if (patchbreak(line)) { | |
834 | if (mi->message_id) | |
835 | strbuf_addf(&mi->log_message, | |
836 | "Message-Id: %s\n", mi->message_id); | |
837 | return 1; | |
838 | } | |
839 | ||
840 | strbuf_addbuf(&mi->log_message, line); | |
841 | return 0; | |
842 | } | |
843 | ||
844 | static void handle_patch(struct mailinfo *mi, const struct strbuf *line) | |
845 | { | |
846 | fwrite(line->buf, 1, line->len, mi->patchfile); | |
847 | mi->patch_lines++; | |
848 | } | |
849 | ||
850 | static void handle_filter(struct mailinfo *mi, struct strbuf *line) | |
851 | { | |
852 | switch (mi->filter_stage) { | |
853 | case 0: | |
854 | if (!handle_commit_msg(mi, line)) | |
855 | break; | |
856 | mi->filter_stage++; | |
1cf01a34 | 857 | /* fallthrough */ |
c6905e45 JH |
858 | case 1: |
859 | handle_patch(mi, line); | |
860 | break; | |
861 | } | |
862 | } | |
863 | ||
864 | static int is_rfc2822_header(const struct strbuf *line) | |
865 | { | |
866 | /* | |
867 | * The section that defines the loosest possible | |
868 | * field name is "3.6.8 Optional fields". | |
869 | * | |
870 | * optional-field = field-name ":" unstructured CRLF | |
871 | * field-name = 1*ftext | |
872 | * ftext = %d33-57 / %59-126 | |
873 | */ | |
874 | int ch; | |
875 | char *cp = line->buf; | |
876 | ||
877 | /* Count mbox From headers as headers */ | |
878 | if (starts_with(cp, "From ") || starts_with(cp, ">From ")) | |
879 | return 1; | |
880 | ||
881 | while ((ch = *cp++)) { | |
882 | if (ch == ':') | |
883 | return 1; | |
884 | if ((33 <= ch && ch <= 57) || | |
885 | (59 <= ch && ch <= 126)) | |
886 | continue; | |
887 | break; | |
888 | } | |
889 | return 0; | |
890 | } | |
891 | ||
892 | static int read_one_header_line(struct strbuf *line, FILE *in) | |
893 | { | |
894 | struct strbuf continuation = STRBUF_INIT; | |
895 | ||
896 | /* Get the first part of the line. */ | |
8f309aeb | 897 | if (strbuf_getline_lf(line, in)) |
c6905e45 JH |
898 | return 0; |
899 | ||
900 | /* | |
901 | * Is it an empty line or not a valid rfc2822 header? | |
902 | * If so, stop here, and return false ("not a header") | |
903 | */ | |
904 | strbuf_rtrim(line); | |
905 | if (!line->len || !is_rfc2822_header(line)) { | |
906 | /* Re-add the newline */ | |
907 | strbuf_addch(line, '\n'); | |
908 | return 0; | |
909 | } | |
910 | ||
911 | /* | |
912 | * Now we need to eat all the continuation lines.. | |
913 | * Yuck, 2822 header "folding" | |
914 | */ | |
915 | for (;;) { | |
916 | int peek; | |
917 | ||
f0733c13 JS |
918 | peek = fgetc(in); |
919 | if (peek == EOF) | |
920 | break; | |
921 | ungetc(peek, in); | |
c6905e45 JH |
922 | if (peek != ' ' && peek != '\t') |
923 | break; | |
8f309aeb | 924 | if (strbuf_getline_lf(&continuation, in)) |
c6905e45 JH |
925 | break; |
926 | continuation.buf[0] = ' '; | |
927 | strbuf_rtrim(&continuation); | |
928 | strbuf_addbuf(line, &continuation); | |
929 | } | |
930 | strbuf_release(&continuation); | |
931 | ||
932 | return 1; | |
933 | } | |
934 | ||
935 | static int find_boundary(struct mailinfo *mi, struct strbuf *line) | |
936 | { | |
8f309aeb | 937 | while (!strbuf_getline_lf(line, mi->input)) { |
c6905e45 JH |
938 | if (*(mi->content_top) && is_multipart_boundary(mi, line)) |
939 | return 1; | |
940 | } | |
941 | return 0; | |
942 | } | |
943 | ||
944 | static int handle_boundary(struct mailinfo *mi, struct strbuf *line) | |
945 | { | |
946 | struct strbuf newline = STRBUF_INIT; | |
947 | ||
948 | strbuf_addch(&newline, '\n'); | |
949 | again: | |
950 | if (line->len >= (*(mi->content_top))->len + 2 && | |
951 | !memcmp(line->buf + (*(mi->content_top))->len, "--", 2)) { | |
952 | /* we hit an end boundary */ | |
953 | /* pop the current boundary off the stack */ | |
954 | strbuf_release(*(mi->content_top)); | |
6a83d902 | 955 | FREE_AND_NULL(*(mi->content_top)); |
c6905e45 JH |
956 | |
957 | /* technically won't happen as is_multipart_boundary() | |
958 | will fail first. But just in case.. | |
959 | */ | |
960 | if (--mi->content_top < mi->content) { | |
6ac617a3 JH |
961 | error("Detected mismatched boundaries, can't recover"); |
962 | mi->input_error = -1; | |
963 | mi->content_top = mi->content; | |
400cd6bf | 964 | strbuf_release(&newline); |
6ac617a3 | 965 | return 0; |
c6905e45 JH |
966 | } |
967 | handle_filter(mi, &newline); | |
968 | strbuf_release(&newline); | |
6ac617a3 JH |
969 | if (mi->input_error) |
970 | return 0; | |
c6905e45 JH |
971 | |
972 | /* skip to the next boundary */ | |
973 | if (!find_boundary(mi, line)) | |
974 | return 0; | |
975 | goto again; | |
976 | } | |
977 | ||
978 | /* set some defaults */ | |
979 | mi->transfer_encoding = TE_DONTCARE; | |
980 | strbuf_reset(&mi->charset); | |
981 | ||
982 | /* slurp in this section's info */ | |
983 | while (read_one_header_line(line, mi->input)) | |
984 | check_header(mi, line, mi->p_hdr_data, 0); | |
985 | ||
986 | strbuf_release(&newline); | |
987 | /* replenish line */ | |
8f309aeb | 988 | if (strbuf_getline_lf(line, mi->input)) |
c6905e45 JH |
989 | return 0; |
990 | strbuf_addch(line, '\n'); | |
991 | return 1; | |
992 | } | |
993 | ||
3aa4d81f RS |
994 | static void handle_filter_flowed(struct mailinfo *mi, struct strbuf *line, |
995 | struct strbuf *prev) | |
996 | { | |
997 | size_t len = line->len; | |
998 | const char *rest; | |
999 | ||
1000 | if (!mi->format_flowed) { | |
0b689562 ĐTCD |
1001 | if (len >= 2 && |
1002 | line->buf[len - 2] == '\r' && | |
1003 | line->buf[len - 1] == '\n') { | |
1004 | mi->have_quoted_cr = 1; | |
133a4fda ĐTCD |
1005 | if (mi->quoted_cr == quoted_cr_strip) { |
1006 | strbuf_setlen(line, len - 2); | |
1007 | strbuf_addch(line, '\n'); | |
1008 | len--; | |
1009 | } | |
0b689562 | 1010 | } |
3aa4d81f RS |
1011 | handle_filter(mi, line); |
1012 | return; | |
1013 | } | |
1014 | ||
1015 | if (line->buf[len - 1] == '\n') { | |
1016 | len--; | |
1017 | if (len && line->buf[len - 1] == '\r') | |
1018 | len--; | |
1019 | } | |
1020 | ||
1021 | /* Keep signature separator as-is. */ | |
1022 | if (skip_prefix(line->buf, "-- ", &rest) && rest - line->buf == len) { | |
1023 | if (prev->len) { | |
1024 | handle_filter(mi, prev); | |
1025 | strbuf_reset(prev); | |
1026 | } | |
1027 | handle_filter(mi, line); | |
1028 | return; | |
1029 | } | |
1030 | ||
1031 | /* Unstuff space-stuffed line. */ | |
1032 | if (len && line->buf[0] == ' ') { | |
1033 | strbuf_remove(line, 0, 1); | |
1034 | len--; | |
1035 | } | |
1036 | ||
1037 | /* Save flowed line for later, but without the soft line break. */ | |
1038 | if (len && line->buf[len - 1] == ' ') { | |
1039 | strbuf_add(prev, line->buf, len - !!mi->delsp); | |
1040 | return; | |
1041 | } | |
1042 | ||
1043 | /* Prepend any previous partial lines */ | |
1044 | strbuf_insert(line, 0, prev->buf, prev->len); | |
1045 | strbuf_reset(prev); | |
1046 | ||
1047 | handle_filter(mi, line); | |
1048 | } | |
1049 | ||
0b689562 ĐTCD |
1050 | static void summarize_quoted_cr(struct mailinfo *mi) |
1051 | { | |
f1aa2994 ĐTCD |
1052 | if (mi->have_quoted_cr && |
1053 | mi->quoted_cr == quoted_cr_warn) | |
0b689562 ĐTCD |
1054 | warning(_("quoted CRLF detected")); |
1055 | } | |
1056 | ||
c6905e45 JH |
1057 | static void handle_body(struct mailinfo *mi, struct strbuf *line) |
1058 | { | |
1059 | struct strbuf prev = STRBUF_INIT; | |
1060 | ||
1061 | /* Skip up to the first boundary */ | |
1062 | if (*(mi->content_top)) { | |
1063 | if (!find_boundary(mi, line)) | |
1064 | goto handle_body_out; | |
1065 | } | |
1066 | ||
1067 | do { | |
1068 | /* process any boundary lines */ | |
1069 | if (*(mi->content_top) && is_multipart_boundary(mi, line)) { | |
1070 | /* flush any leftover */ | |
1071 | if (prev.len) { | |
1072 | handle_filter(mi, &prev); | |
1073 | strbuf_reset(&prev); | |
1074 | } | |
0b689562 ĐTCD |
1075 | summarize_quoted_cr(mi); |
1076 | mi->have_quoted_cr = 0; | |
c6905e45 JH |
1077 | if (!handle_boundary(mi, line)) |
1078 | goto handle_body_out; | |
1079 | } | |
1080 | ||
1081 | /* Unwrap transfer encoding */ | |
1082 | decode_transfer_encoding(mi, line); | |
1083 | ||
1084 | switch (mi->transfer_encoding) { | |
1085 | case TE_BASE64: | |
1086 | case TE_QP: | |
1087 | { | |
1088 | struct strbuf **lines, **it, *sb; | |
1089 | ||
1090 | /* Prepend any previous partial lines */ | |
1091 | strbuf_insert(line, 0, prev.buf, prev.len); | |
1092 | strbuf_reset(&prev); | |
1093 | ||
1094 | /* | |
1095 | * This is a decoded line that may contain | |
1096 | * multiple new lines. Pass only one chunk | |
1097 | * at a time to handle_filter() | |
1098 | */ | |
1099 | lines = strbuf_split(line, '\n'); | |
1100 | for (it = lines; (sb = *it); it++) { | |
afe8a907 | 1101 | if (!*(it + 1)) /* The last line */ |
c6905e45 JH |
1102 | if (sb->buf[sb->len - 1] != '\n') { |
1103 | /* Partial line, save it for later. */ | |
1104 | strbuf_addbuf(&prev, sb); | |
1105 | break; | |
1106 | } | |
3aa4d81f | 1107 | handle_filter_flowed(mi, sb, &prev); |
c6905e45 JH |
1108 | } |
1109 | /* | |
1110 | * The partial chunk is saved in "prev" and will be | |
1111 | * appended by the next iteration of read_line_with_nul(). | |
1112 | */ | |
1113 | strbuf_list_free(lines); | |
1114 | break; | |
1115 | } | |
1116 | default: | |
3aa4d81f | 1117 | handle_filter_flowed(mi, line, &prev); |
c6905e45 JH |
1118 | } |
1119 | ||
6ac617a3 JH |
1120 | if (mi->input_error) |
1121 | break; | |
c6905e45 JH |
1122 | } while (!strbuf_getwholeline(line, mi->input, '\n')); |
1123 | ||
3aa4d81f RS |
1124 | if (prev.len) |
1125 | handle_filter(mi, &prev); | |
0b689562 | 1126 | summarize_quoted_cr(mi); |
3aa4d81f | 1127 | |
6b4b013f JT |
1128 | flush_inbody_header_accum(mi); |
1129 | ||
c6905e45 JH |
1130 | handle_body_out: |
1131 | strbuf_release(&prev); | |
1132 | } | |
1133 | ||
1134 | static void output_header_lines(FILE *fout, const char *hdr, const struct strbuf *data) | |
1135 | { | |
1136 | const char *sp = data->buf; | |
1137 | while (1) { | |
1138 | char *ep = strchr(sp, '\n'); | |
1139 | int len; | |
1140 | if (!ep) | |
1141 | len = strlen(sp); | |
1142 | else | |
1143 | len = ep - sp; | |
1144 | fprintf(fout, "%s: %.*s\n", hdr, len, sp); | |
1145 | if (!ep) | |
1146 | break; | |
1147 | sp = ep + 1; | |
1148 | } | |
1149 | } | |
1150 | ||
1151 | static void handle_info(struct mailinfo *mi) | |
1152 | { | |
1153 | struct strbuf *hdr; | |
1154 | int i; | |
1155 | ||
1156 | for (i = 0; header[i]; i++) { | |
1157 | /* only print inbody headers if we output a patch file */ | |
1158 | if (mi->patch_lines && mi->s_hdr_data[i]) | |
1159 | hdr = mi->s_hdr_data[i]; | |
1160 | else if (mi->p_hdr_data[i]) | |
1161 | hdr = mi->p_hdr_data[i]; | |
1162 | else | |
1163 | continue; | |
1164 | ||
39199974 ĐTCD |
1165 | if (memchr(hdr->buf, '\0', hdr->len)) { |
1166 | error("a NUL byte in '%s' is not allowed.", header[i]); | |
1167 | mi->input_error = -1; | |
1168 | } | |
1169 | ||
c6905e45 JH |
1170 | if (!strcmp(header[i], "Subject")) { |
1171 | if (!mi->keep_subject) { | |
1172 | cleanup_subject(mi, hdr); | |
1173 | cleanup_space(hdr); | |
1174 | } | |
1175 | output_header_lines(mi->output, "Subject", hdr); | |
1176 | } else if (!strcmp(header[i], "From")) { | |
1177 | cleanup_space(hdr); | |
1178 | handle_from(mi, hdr); | |
1179 | fprintf(mi->output, "Author: %s\n", mi->name.buf); | |
1180 | fprintf(mi->output, "Email: %s\n", mi->email.buf); | |
1181 | } else { | |
1182 | cleanup_space(hdr); | |
1183 | fprintf(mi->output, "%s: %s\n", header[i], hdr->buf); | |
1184 | } | |
1185 | } | |
1186 | fprintf(mi->output, "\n"); | |
1187 | } | |
1188 | ||
1189 | int mailinfo(struct mailinfo *mi, const char *msg, const char *patch) | |
1190 | { | |
1191 | FILE *cmitmsg; | |
1192 | int peek; | |
1193 | struct strbuf line = STRBUF_INIT; | |
1194 | ||
1195 | cmitmsg = fopen(msg, "w"); | |
1196 | if (!cmitmsg) { | |
1197 | perror(msg); | |
1198 | return -1; | |
1199 | } | |
1200 | mi->patchfile = fopen(patch, "w"); | |
1201 | if (!mi->patchfile) { | |
1202 | perror(patch); | |
1203 | fclose(cmitmsg); | |
1204 | return -1; | |
1205 | } | |
1206 | ||
1207 | mi->p_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*(mi->p_hdr_data))); | |
1208 | mi->s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*(mi->s_hdr_data))); | |
1209 | ||
1210 | do { | |
1211 | peek = fgetc(mi->input); | |
f0733c13 JS |
1212 | if (peek == EOF) { |
1213 | fclose(cmitmsg); | |
1214 | return error("empty patch: '%s'", patch); | |
1215 | } | |
c6905e45 JH |
1216 | } while (isspace(peek)); |
1217 | ungetc(peek, mi->input); | |
1218 | ||
1219 | /* process the email header */ | |
1220 | while (read_one_header_line(&line, mi->input)) | |
1221 | check_header(mi, &line, mi->p_hdr_data, 1); | |
1222 | ||
1223 | handle_body(mi, &line); | |
1224 | fwrite(mi->log_message.buf, 1, mi->log_message.len, cmitmsg); | |
1225 | fclose(cmitmsg); | |
1226 | fclose(mi->patchfile); | |
1227 | ||
1228 | handle_info(mi); | |
1229 | strbuf_release(&line); | |
6ac617a3 | 1230 | return mi->input_error; |
c6905e45 JH |
1231 | } |
1232 | ||
f1aa2994 ĐTCD |
1233 | int mailinfo_parse_quoted_cr_action(const char *actionstr, int *action) |
1234 | { | |
1235 | if (!strcmp(actionstr, "nowarn")) | |
1236 | *action = quoted_cr_nowarn; | |
1237 | else if (!strcmp(actionstr, "warn")) | |
1238 | *action = quoted_cr_warn; | |
133a4fda ĐTCD |
1239 | else if (!strcmp(actionstr, "strip")) |
1240 | *action = quoted_cr_strip; | |
f1aa2994 ĐTCD |
1241 | else |
1242 | return -1; | |
1243 | return 0; | |
1244 | } | |
1245 | ||
c6905e45 JH |
1246 | static int git_mailinfo_config(const char *var, const char *value, void *mi_) |
1247 | { | |
1248 | struct mailinfo *mi = mi_; | |
1249 | ||
1250 | if (!starts_with(var, "mailinfo.")) | |
1251 | return git_default_config(var, value, NULL); | |
1252 | if (!strcmp(var, "mailinfo.scissors")) { | |
1253 | mi->use_scissors = git_config_bool(var, value); | |
1254 | return 0; | |
1255 | } | |
f1aa2994 ĐTCD |
1256 | if (!strcmp(var, "mailinfo.quotedcr")) { |
1257 | if (mailinfo_parse_quoted_cr_action(value, &mi->quoted_cr) != 0) | |
1258 | return error(_("bad action '%s' for '%s'"), value, var); | |
1259 | return 0; | |
1260 | } | |
c6905e45 JH |
1261 | /* perhaps others here */ |
1262 | return 0; | |
1263 | } | |
1264 | ||
1265 | void setup_mailinfo(struct mailinfo *mi) | |
1266 | { | |
1267 | memset(mi, 0, sizeof(*mi)); | |
1268 | strbuf_init(&mi->name, 0); | |
1269 | strbuf_init(&mi->email, 0); | |
1270 | strbuf_init(&mi->charset, 0); | |
1271 | strbuf_init(&mi->log_message, 0); | |
6b4b013f | 1272 | strbuf_init(&mi->inbody_header_accum, 0); |
f1aa2994 | 1273 | mi->quoted_cr = quoted_cr_warn; |
c6905e45 JH |
1274 | mi->header_stage = 1; |
1275 | mi->use_inbody_headers = 1; | |
1276 | mi->content_top = mi->content; | |
85d9d9dd | 1277 | git_config(git_mailinfo_config, mi); |
c6905e45 JH |
1278 | } |
1279 | ||
1280 | void clear_mailinfo(struct mailinfo *mi) | |
1281 | { | |
c6905e45 JH |
1282 | strbuf_release(&mi->name); |
1283 | strbuf_release(&mi->email); | |
1284 | strbuf_release(&mi->charset); | |
6b4b013f | 1285 | strbuf_release(&mi->inbody_header_accum); |
c6905e45 JH |
1286 | free(mi->message_id); |
1287 | ||
f3a96807 AH |
1288 | strbuf_list_free(mi->p_hdr_data); |
1289 | strbuf_list_free(mi->s_hdr_data); | |
c6905e45 JH |
1290 | |
1291 | while (mi->content < mi->content_top) { | |
1292 | free(*(mi->content_top)); | |
1293 | mi->content_top--; | |
1294 | } | |
1295 | ||
1296 | strbuf_release(&mi->log_message); | |
1297 | } |