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