]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-mailinfo.c
MSVC: Add support for building with NO_MMAP
[thirdparty/git.git] / builtin-mailinfo.c
CommitLineData
2744b234
LT
1/*
2 * Another stupid program, this one parsing the headers of an
3 * email to figure out authorship and subject
4 */
f1f909e3 5#include "cache.h"
34488e3c 6#include "builtin.h"
b45974a6 7#include "utf8.h"
3b6121f6 8#include "strbuf.h"
2744b234 9
34488e3c 10static FILE *cmitmsg, *patchfile, *fin, *fout;
2744b234 11
96f1e58f
DR
12static int keep_subject;
13static const char *metainfo_charset;
3b6121f6
LS
14static struct strbuf line = STRBUF_INIT;
15static struct strbuf name = STRBUF_INIT;
16static struct strbuf email = STRBUF_INIT;
2744b234 17
d4a9ce78
JH
18static enum {
19 TE_DONTCARE, TE_QP, TE_BASE64,
20} transfer_encoding;
87ab7992
DZ
21static enum {
22 TYPE_TEXT, TYPE_OTHER,
23} message_type;
d4a9ce78 24
3b6121f6 25static struct strbuf charset = STRBUF_INIT;
96f1e58f 26static int patch_lines;
3b6121f6 27static struct strbuf **p_hdr_data, **s_hdr_data;
017678b4 28static int use_scissors;
87ab7992
DZ
29
30#define MAX_HDR_PARSED 10
31#define MAX_BOUNDARIES 5
d4a9ce78 32
08e6710f
KS
33static void cleanup_space(struct strbuf *sb);
34
35
3b6121f6 36static void get_sane_name(struct strbuf *out, struct strbuf *name, struct strbuf *email)
2744b234 37{
3b6121f6
LS
38 struct strbuf *src = name;
39 if (name->len < 3 || 60 < name->len || strchr(name->buf, '@') ||
40 strchr(name->buf, '<') || strchr(name->buf, '>'))
41 src = email;
42 else if (name == out)
43 return;
44 strbuf_reset(out);
45 strbuf_addbuf(out, src);
2744b234
LT
46}
47
3b6121f6 48static void parse_bogus_from(const struct strbuf *line)
e0e3ba20
JH
49{
50 /* John Doe <johndoe> */
e0e3ba20 51
3b6121f6 52 char *bra, *ket;
e0e3ba20
JH
53 /* This is fallback, so do not bother if we already have an
54 * e-mail address.
34488e3c 55 */
3b6121f6
LS
56 if (email.len)
57 return;
e0e3ba20 58
3b6121f6 59 bra = strchr(line->buf, '<');
e0e3ba20 60 if (!bra)
3b6121f6 61 return;
e0e3ba20
JH
62 ket = strchr(bra, '>');
63 if (!ket)
3b6121f6 64 return;
e0e3ba20 65
3b6121f6
LS
66 strbuf_reset(&email);
67 strbuf_add(&email, bra + 1, ket - bra - 1);
68
69 strbuf_reset(&name);
70 strbuf_add(&name, line->buf, bra - line->buf);
71 strbuf_trim(&name);
72 get_sane_name(&name, &name, &email);
e0e3ba20
JH
73}
74
3b6121f6 75static void handle_from(const struct strbuf *from)
2744b234 76{
2dec02b1 77 char *at;
3b6121f6
LS
78 size_t el;
79 struct strbuf f;
2744b234 80
3b6121f6
LS
81 strbuf_init(&f, from->len);
82 strbuf_addbuf(&f, from);
83
84 at = strchr(f.buf, '@');
85 if (!at) {
86 parse_bogus_from(from);
87 return;
88 }
2744b234
LT
89
90 /*
91 * If we already have one email, don't take any confusing lines
92 */
3b6121f6
LS
93 if (email.len && strchr(at + 1, '@')) {
94 strbuf_release(&f);
95 return;
96 }
2744b234 97
d4a9ce78 98 /* Pick up the string around '@', possibly delimited with <>
3b6121f6 99 * pair; that is the email part.
d4a9ce78 100 */
3b6121f6 101 while (at > f.buf) {
2744b234 102 char c = at[-1];
d4a9ce78
JH
103 if (isspace(c))
104 break;
105 if (c == '<') {
106 at[-1] = ' ';
2744b234 107 break;
d4a9ce78 108 }
2744b234
LT
109 at--;
110 }
3b6121f6
LS
111 el = strcspn(at, " \n\t\r\v\f>");
112 strbuf_reset(&email);
113 strbuf_add(&email, at, el);
e9d7d10a 114 strbuf_remove(&f, at - f.buf, el + (at[el] ? 1 : 0));
2744b234 115
08e6710f
KS
116 /* The remainder is name. It could be
117 *
118 * - "John Doe <john.doe@xz>" (a), or
119 * - "john.doe@xz (John Doe)" (b), or
120 * - "John (zzz) Doe <john.doe@xz> (Comment)" (c)
121 *
122 * but we have removed the email part, so
123 *
124 * - remove extra spaces which could stay after email (case 'c'), and
125 * - trim from both ends, possibly removing the () pair at the end
126 * (cases 'a' and 'b').
d4a9ce78 127 */
08e6710f 128 cleanup_space(&f);
3b6121f6 129 strbuf_trim(&f);
0d4ede9f
PB
130 if (f.buf[0] == '(' && f.len && f.buf[f.len - 1] == ')') {
131 strbuf_remove(&f, 0, 1);
3b6121f6 132 strbuf_setlen(&f, f.len - 1);
0d4ede9f 133 }
2744b234 134
3b6121f6
LS
135 get_sane_name(&name, &f, &email);
136 strbuf_release(&f);
2744b234
LT
137}
138
3b6121f6 139static void handle_header(struct strbuf **out, const struct strbuf *line)
62c1f6b4 140{
3b6121f6
LS
141 if (!*out) {
142 *out = xmalloc(sizeof(struct strbuf));
143 strbuf_init(*out, line->len);
144 } else
145 strbuf_reset(*out);
87ab7992 146
3b6121f6 147 strbuf_addbuf(*out, line);
d4a9ce78
JH
148}
149
150/* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
151 * to have enough heuristics to grok MIME encoded patches often found
152 * on our mailing lists. For example, we do not even treat header lines
153 * case insensitively.
154 */
155
3b6121f6 156static int slurp_attr(const char *line, const char *name, struct strbuf *attr)
d4a9ce78 157{
554fe20d 158 const char *ends, *ap = strcasestr(line, name);
d4a9ce78
JH
159 size_t sz;
160
161 if (!ap) {
3b6121f6 162 strbuf_setlen(attr, 0);
d4a9ce78
JH
163 return 0;
164 }
165 ap += strlen(name);
166 if (*ap == '"') {
167 ap++;
168 ends = "\"";
169 }
170 else
171 ends = "; \t";
172 sz = strcspn(ap, ends);
3b6121f6 173 strbuf_add(attr, ap, sz);
d4a9ce78
JH
174 return 1;
175}
176
3b6121f6 177static struct strbuf *content[MAX_BOUNDARIES];
87ab7992 178
3b6121f6 179static struct strbuf **content_top = content;
87ab7992 180
3b6121f6 181static void handle_content_type(struct strbuf *line)
d4a9ce78 182{
3b6121f6
LS
183 struct strbuf *boundary = xmalloc(sizeof(struct strbuf));
184 strbuf_init(boundary, line->len);
87ab7992 185
3b6121f6 186 if (!strcasestr(line->buf, "text/"))
87ab7992 187 message_type = TYPE_OTHER;
3b6121f6
LS
188 if (slurp_attr(line->buf, "boundary=", boundary)) {
189 strbuf_insert(boundary, 0, "--", 2);
289796dd 190 if (++content_top > &content[MAX_BOUNDARIES]) {
87ab7992
DZ
191 fprintf(stderr, "Too many boundaries to handle\n");
192 exit(1);
193 }
3b6121f6
LS
194 *content_top = boundary;
195 boundary = NULL;
b893f091 196 }
ed1e3985 197 slurp_attr(line->buf, "charset=", &charset);
3b6121f6
LS
198
199 if (boundary) {
200 strbuf_release(boundary);
201 free(boundary);
d4a9ce78 202 }
d4a9ce78
JH
203}
204
3b6121f6 205static void handle_content_transfer_encoding(const struct strbuf *line)
d4a9ce78 206{
3b6121f6 207 if (strcasestr(line->buf, "base64"))
d4a9ce78 208 transfer_encoding = TE_BASE64;
3b6121f6 209 else if (strcasestr(line->buf, "quoted-printable"))
d4a9ce78
JH
210 transfer_encoding = TE_QP;
211 else
212 transfer_encoding = TE_DONTCARE;
2744b234
LT
213}
214
3b6121f6 215static int is_multipart_boundary(const struct strbuf *line)
d4a9ce78 216{
a9fd1383
JH
217 return (((*content_top)->len <= line->len) &&
218 !memcmp(line->buf, (*content_top)->buf, (*content_top)->len));
d4a9ce78
JH
219}
220
3b6121f6 221static void cleanup_subject(struct strbuf *subject)
2744b234 222{
3b6121f6
LS
223 char *pos;
224 size_t remove;
225 while (subject->len) {
226 switch (*subject->buf) {
2744b234 227 case 'r': case 'R':
3b6121f6
LS
228 if (subject->len <= 3)
229 break;
230 if (!memcmp(subject->buf + 1, "e:", 2)) {
231 strbuf_remove(subject, 0, 3);
2744b234
LT
232 continue;
233 }
234 break;
235 case ' ': case '\t': case ':':
3b6121f6 236 strbuf_remove(subject, 0, 1);
2744b234 237 continue;
2744b234 238 case '[':
3b6121f6 239 if ((pos = strchr(subject->buf, ']'))) {
1e102bf7
JH
240 remove = pos - subject->buf;
241 if (remove <= (subject->len - remove) * 2) {
242 strbuf_remove(subject, 0, remove + 1);
3b6121f6
LS
243 continue;
244 }
245 } else
246 strbuf_remove(subject, 0, 1);
2744b234
LT
247 break;
248 }
3b6121f6
LS
249 strbuf_trim(subject);
250 return;
2744b234 251 }
34488e3c 252}
2744b234 253
3b6121f6 254static void cleanup_space(struct strbuf *sb)
2744b234 255{
3b6121f6
LS
256 size_t pos, cnt;
257 for (pos = 0; pos < sb->len; pos++) {
258 if (isspace(sb->buf[pos])) {
259 sb->buf[pos] = ' ';
260 for (cnt = 0; isspace(sb->buf[pos + cnt + 1]); cnt++);
261 strbuf_remove(sb, pos + 1, cnt);
2744b234
LT
262 }
263 }
264}
265
3b6121f6 266static void decode_header(struct strbuf *line);
538dfe73 267static const char *header[MAX_HDR_PARSED] = {
87ab7992 268 "From","Subject","Date",
d4a9ce78
JH
269};
270
3b6121f6 271static inline int cmp_header(const struct strbuf *line, const char *hdr)
d4a9ce78 272{
3b6121f6
LS
273 int len = strlen(hdr);
274 return !strncasecmp(line->buf, hdr, len) && line->len > len &&
275 line->buf[len] == ':' && isspace(line->buf[len + 1]);
276}
d4a9ce78 277
3b6121f6
LS
278static int check_header(const struct strbuf *line,
279 struct strbuf *hdr_data[], int overwrite)
280{
281 int i, ret = 0, len;
282 struct strbuf sb = STRBUF_INIT;
87ab7992
DZ
283 /* search for the interesting parts */
284 for (i = 0; header[i]; i++) {
285 int len = strlen(header[i]);
3b6121f6 286 if ((!hdr_data[i] || overwrite) && cmp_header(line, header[i])) {
33504530
EB
287 /* Unwrap inline B and Q encoding, and optionally
288 * normalize the meta information to utf8.
289 */
3b6121f6
LS
290 strbuf_add(&sb, line->buf + len + 2, line->len - len - 2);
291 decode_header(&sb);
292 handle_header(&hdr_data[i], &sb);
293 ret = 1;
294 goto check_header_out;
d4a9ce78
JH
295 }
296 }
d4a9ce78 297
87ab7992 298 /* Content stuff */
3b6121f6
LS
299 if (cmp_header(line, "Content-Type")) {
300 len = strlen("Content-Type: ");
301 strbuf_add(&sb, line->buf + len, line->len - len);
302 decode_header(&sb);
303 strbuf_insert(&sb, 0, "Content-Type: ", len);
304 handle_content_type(&sb);
305 ret = 1;
306 goto check_header_out;
307 }
308 if (cmp_header(line, "Content-Transfer-Encoding")) {
309 len = strlen("Content-Transfer-Encoding: ");
310 strbuf_add(&sb, line->buf + len, line->len - len);
311 decode_header(&sb);
312 handle_content_transfer_encoding(&sb);
313 ret = 1;
314 goto check_header_out;
87ab7992
DZ
315 }
316
317 /* for inbody stuff */
3b6121f6
LS
318 if (!prefixcmp(line->buf, ">From") && isspace(line->buf[5])) {
319 ret = 1; /* Should this return 0? */
320 goto check_header_out;
321 }
322 if (!prefixcmp(line->buf, "[PATCH]") && isspace(line->buf[7])) {
87ab7992 323 for (i = 0; header[i]; i++) {
e9fe804a 324 if (!memcmp("Subject", header[i], 7)) {
3b6121f6
LS
325 handle_header(&hdr_data[i], line);
326 ret = 1;
327 goto check_header_out;
87ab7992
DZ
328 }
329 }
330 }
331
3b6121f6
LS
332check_header_out:
333 strbuf_release(&sb);
334 return ret;
d4a9ce78
JH
335}
336
3b6121f6 337static int is_rfc2822_header(const struct strbuf *line)
ef29c117
JH
338{
339 /*
340 * The section that defines the loosest possible
341 * field name is "3.6.8 Optional fields".
342 *
343 * optional-field = field-name ":" unstructured CRLF
344 * field-name = 1*ftext
345 * ftext = %d33-57 / %59-126
346 */
347 int ch;
3b6121f6 348 char *cp = line->buf;
34fc5cef
LT
349
350 /* Count mbox From headers as headers */
3b6121f6 351 if (!prefixcmp(cp, "From ") || !prefixcmp(cp, ">From "))
34fc5cef
LT
352 return 1;
353
ef29c117
JH
354 while ((ch = *cp++)) {
355 if (ch == ':')
3b6121f6 356 return 1;
ef29c117
JH
357 if ((33 <= ch && ch <= 57) ||
358 (59 <= ch && ch <= 126))
359 continue;
360 break;
361 }
362 return 0;
363}
364
3b6121f6 365static int read_one_header_line(struct strbuf *line, FILE *in)
d4a9ce78 366{
34fc5cef 367 /* Get the first part of the line. */
3b6121f6 368 if (strbuf_getline(line, in, '\n'))
34fc5cef
LT
369 return 0;
370
371 /*
372 * Is it an empty line or not a valid rfc2822 header?
373 * If so, stop here, and return false ("not a header")
374 */
3b6121f6
LS
375 strbuf_rtrim(line);
376 if (!line->len || !is_rfc2822_header(line)) {
34fc5cef 377 /* Re-add the newline */
3b6121f6 378 strbuf_addch(line, '\n');
34fc5cef
LT
379 return 0;
380 }
381
382 /*
383 * Now we need to eat all the continuation lines..
384 * Yuck, 2822 header "folding"
385 */
386 for (;;) {
3b6121f6
LS
387 int peek;
388 struct strbuf continuation = STRBUF_INIT;
34fc5cef 389
f30b2028
EB
390 peek = fgetc(in); ungetc(peek, in);
391 if (peek != ' ' && peek != '\t')
392 break;
3b6121f6 393 if (strbuf_getline(&continuation, in, '\n'))
34fc5cef 394 break;
3b6121f6
LS
395 continuation.buf[0] = '\n';
396 strbuf_rtrim(&continuation);
397 strbuf_addbuf(line, &continuation);
d4a9ce78 398 }
34fc5cef
LT
399
400 return 1;
d4a9ce78
JH
401}
402
3b6121f6 403static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047)
d4a9ce78 404{
3b6121f6 405 const char *in = q_seg->buf;
d4a9ce78 406 int c;
3b6121f6
LS
407 struct strbuf *out = xmalloc(sizeof(struct strbuf));
408 strbuf_init(out, q_seg->len);
409
410 while ((c = *in++) != 0) {
d4a9ce78
JH
411 if (c == '=') {
412 int d = *in++;
413 if (d == '\n' || !d)
414 break; /* drop trailing newline */
3b6121f6 415 strbuf_addch(out, (hexval(d) << 4) | hexval(*in++));
75731930 416 continue;
d4a9ce78 417 }
75731930
JH
418 if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
419 c = 0x20;
3b6121f6 420 strbuf_addch(out, c);
d4a9ce78 421 }
3b6121f6 422 return out;
d4a9ce78
JH
423}
424
3b6121f6 425static struct strbuf *decode_b_segment(const struct strbuf *b_seg)
d4a9ce78
JH
426{
427 /* Decode in..ep, possibly in-place to ot */
428 int c, pos = 0, acc = 0;
3b6121f6
LS
429 const char *in = b_seg->buf;
430 struct strbuf *out = xmalloc(sizeof(struct strbuf));
431 strbuf_init(out, b_seg->len);
d4a9ce78 432
3b6121f6 433 while ((c = *in++) != 0) {
d4a9ce78
JH
434 if (c == '+')
435 c = 62;
436 else if (c == '/')
437 c = 63;
438 else if ('A' <= c && c <= 'Z')
439 c -= 'A';
440 else if ('a' <= c && c <= 'z')
441 c -= 'a' - 26;
442 else if ('0' <= c && c <= '9')
443 c -= '0' - 52;
d4a9ce78
JH
444 else
445 continue; /* garbage */
446 switch (pos++) {
447 case 0:
448 acc = (c << 2);
449 break;
450 case 1:
3b6121f6 451 strbuf_addch(out, (acc | (c >> 4)));
d4a9ce78
JH
452 acc = (c & 15) << 4;
453 break;
454 case 2:
3b6121f6 455 strbuf_addch(out, (acc | (c >> 2)));
d4a9ce78
JH
456 acc = (c & 3) << 6;
457 break;
458 case 3:
3b6121f6 459 strbuf_addch(out, (acc | c));
d4a9ce78
JH
460 acc = pos = 0;
461 break;
462 }
463 }
3b6121f6 464 return out;
d4a9ce78
JH
465}
466
b59d398b
LT
467/*
468 * When there is no known charset, guess.
469 *
470 * Right now we assume that if the target is UTF-8 (the default),
471 * and it already looks like UTF-8 (which includes US-ASCII as its
472 * subset, of course) then that is what it is and there is nothing
473 * to do.
474 *
475 * Otherwise, we default to assuming it is Latin1 for historical
476 * reasons.
477 */
3b6121f6 478static const char *guess_charset(const struct strbuf *line, const char *target_charset)
b59d398b
LT
479{
480 if (is_encoding_utf8(target_charset)) {
3b6121f6 481 if (is_utf8(line->buf))
b59d398b
LT
482 return NULL;
483 }
62645006 484 return "ISO8859-1";
b59d398b
LT
485}
486
3b6121f6 487static void convert_to_utf8(struct strbuf *line, const char *charset)
d4a9ce78 488{
b59d398b
LT
489 char *out;
490
491 if (!charset || !*charset) {
492 charset = guess_charset(line, metainfo_charset);
493 if (!charset)
494 return;
495 }
b45974a6 496
ed1e3985 497 if (!strcasecmp(metainfo_charset, charset))
7296096c 498 return;
3b6121f6 499 out = reencode_string(line->buf, metainfo_charset, charset);
bb1091a4 500 if (!out)
d7530708 501 die("cannot convert from %s to %s",
b59d398b 502 charset, metainfo_charset);
3b6121f6 503 strbuf_attach(line, out, strlen(out), strlen(out));
d4a9ce78
JH
504}
505
3b6121f6 506static int decode_header_bq(struct strbuf *it)
d4a9ce78 507{
3b6121f6
LS
508 char *in, *ep, *cp;
509 struct strbuf outbuf = STRBUF_INIT, *dec;
510 struct strbuf charset_q = STRBUF_INIT, piecebuf = STRBUF_INIT;
b75bf2c3 511 int rfc2047 = 0;
d4a9ce78 512
3b6121f6
LS
513 in = it->buf;
514 while (in - it->buf <= it->len && (ep = strstr(in, "=?")) != NULL) {
515 int encoding;
516 strbuf_reset(&charset_q);
517 strbuf_reset(&piecebuf);
b75bf2c3
JH
518 rfc2047 = 1;
519
d4a9ce78 520 if (in != ep) {
353aaf2f
KS
521 /*
522 * We are about to process an encoded-word
523 * that begins at ep, but there is something
524 * before the encoded word.
525 */
526 char *scan;
527 for (scan = in; scan < ep; scan++)
528 if (!isspace(*scan))
529 break;
530
531 if (scan != ep || in == it->buf) {
532 /*
533 * We should not lose that "something",
534 * unless we have just processed an
535 * encoded-word, and there is only LWS
536 * before the one we are about to process.
537 */
538 strbuf_add(&outbuf, in, ep - in);
539 }
d4a9ce78
JH
540 }
541 /* E.g.
542 * ep : "=?iso-2022-jp?B?GyR...?= foo"
543 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
544 */
545 ep += 2;
3b6121f6
LS
546
547 if (ep - it->buf >= it->len || !(cp = strchr(ep, '?')))
548 goto decode_header_bq_out;
549
550 if (cp + 3 - it->buf > it->len)
551 goto decode_header_bq_out;
552 strbuf_add(&charset_q, ep, cp - ep);
3b6121f6 553
d4a9ce78
JH
554 encoding = cp[1];
555 if (!encoding || cp[2] != '?')
3b6121f6 556 goto decode_header_bq_out;
d4a9ce78
JH
557 ep = strstr(cp + 3, "?=");
558 if (!ep)
3b6121f6
LS
559 goto decode_header_bq_out;
560 strbuf_add(&piecebuf, cp + 3, ep - cp - 3);
d4a9ce78
JH
561 switch (tolower(encoding)) {
562 default:
3b6121f6 563 goto decode_header_bq_out;
d4a9ce78 564 case 'b':
3b6121f6 565 dec = decode_b_segment(&piecebuf);
d4a9ce78
JH
566 break;
567 case 'q':
3b6121f6 568 dec = decode_q_segment(&piecebuf, 1);
d4a9ce78
JH
569 break;
570 }
650e4be5 571 if (metainfo_charset)
3b6121f6 572 convert_to_utf8(dec, charset_q.buf);
8dabdfcc 573
3b6121f6
LS
574 strbuf_addbuf(&outbuf, dec);
575 strbuf_release(dec);
576 free(dec);
d4a9ce78
JH
577 in = ep + 2;
578 }
3b6121f6
LS
579 strbuf_addstr(&outbuf, in);
580 strbuf_reset(it);
581 strbuf_addbuf(it, &outbuf);
582decode_header_bq_out:
583 strbuf_release(&outbuf);
584 strbuf_release(&charset_q);
585 strbuf_release(&piecebuf);
b75bf2c3
JH
586 return rfc2047;
587}
588
3b6121f6 589static void decode_header(struct strbuf *it)
b75bf2c3 590{
3b6121f6 591 if (decode_header_bq(it))
b75bf2c3
JH
592 return;
593 /* otherwise "it" is a straight copy of the input.
594 * This can be binary guck but there is no charset specified.
595 */
596 if (metainfo_charset)
3b6121f6 597 convert_to_utf8(it, "");
d4a9ce78
JH
598}
599
3b6121f6 600static void decode_transfer_encoding(struct strbuf *line)
d4a9ce78 601{
3b6121f6 602 struct strbuf *ret;
d4a9ce78
JH
603
604 switch (transfer_encoding) {
605 case TE_QP:
3b6121f6
LS
606 ret = decode_q_segment(line, 0);
607 break;
d4a9ce78 608 case TE_BASE64:
3b6121f6
LS
609 ret = decode_b_segment(line);
610 break;
d4a9ce78 611 case TE_DONTCARE:
9aa23094 612 default:
3b6121f6 613 return;
d4a9ce78 614 }
3b6121f6
LS
615 strbuf_reset(line);
616 strbuf_addbuf(line, ret);
617 strbuf_release(ret);
618 free(ret);
d4a9ce78
JH
619}
620
3b6121f6 621static void handle_filter(struct strbuf *line);
87ab7992
DZ
622
623static int find_boundary(void)
2744b234 624{
3b6121f6 625 while (!strbuf_getline(&line, fin, '\n')) {
289796dd 626 if (*content_top && is_multipart_boundary(&line))
87ab7992
DZ
627 return 1;
628 }
629 return 0;
630}
631
632static int handle_boundary(void)
633{
3b6121f6
LS
634 struct strbuf newline = STRBUF_INIT;
635
636 strbuf_addch(&newline, '\n');
87ab7992 637again:
3b6121f6
LS
638 if (line.len >= (*content_top)->len + 2 &&
639 !memcmp(line.buf + (*content_top)->len, "--", 2)) {
87ab7992
DZ
640 /* we hit an end boundary */
641 /* pop the current boundary off the stack */
3b6121f6
LS
642 strbuf_release(*content_top);
643 free(*content_top);
644 *content_top = NULL;
87ab7992
DZ
645
646 /* technically won't happen as is_multipart_boundary()
647 will fail first. But just in case..
648 */
289796dd 649 if (--content_top < content) {
87ab7992
DZ
650 fprintf(stderr, "Detected mismatched boundaries, "
651 "can't recover\n");
652 exit(1);
653 }
3b6121f6
LS
654 handle_filter(&newline);
655 strbuf_release(&newline);
87ab7992
DZ
656
657 /* skip to the next boundary */
658 if (!find_boundary())
659 return 0;
660 goto again;
661 }
662
663 /* set some defaults */
664 transfer_encoding = TE_DONTCARE;
3b6121f6 665 strbuf_reset(&charset);
87ab7992 666 message_type = TYPE_TEXT;
d4a9ce78 667
87ab7992 668 /* slurp in this section's info */
3b6121f6
LS
669 while (read_one_header_line(&line, fin))
670 check_header(&line, p_hdr_data, 0);
2744b234 671
3b6121f6 672 strbuf_release(&newline);
a9fd1383
JH
673 /* replenish line */
674 if (strbuf_getline(&line, fin, '\n'))
675 return 0;
676 strbuf_addch(&line, '\n');
677 return 1;
d4a9ce78
JH
678}
679
3b6121f6 680static inline int patchbreak(const struct strbuf *line)
f0658cf2 681{
3b6121f6
LS
682 size_t i;
683
f0658cf2 684 /* Beginning of a "diff -" header? */
3b6121f6 685 if (!prefixcmp(line->buf, "diff -"))
f0658cf2
DZ
686 return 1;
687
688 /* CVS "Index: " line? */
3b6121f6 689 if (!prefixcmp(line->buf, "Index: "))
f0658cf2
DZ
690 return 1;
691
692 /*
693 * "--- <filename>" starts patches without headers
694 * "---<sp>*" is a manual separator
695 */
3b6121f6
LS
696 if (line->len < 4)
697 return 0;
698
699 if (!prefixcmp(line->buf, "---")) {
f0658cf2 700 /* space followed by a filename? */
3b6121f6 701 if (line->buf[3] == ' ' && !isspace(line->buf[4]))
f0658cf2
DZ
702 return 1;
703 /* Just whitespace? */
3b6121f6
LS
704 for (i = 3; i < line->len; i++) {
705 unsigned char c = line->buf[i];
f0658cf2
DZ
706 if (c == '\n')
707 return 1;
708 if (!isspace(c))
709 break;
710 }
711 return 0;
712 }
713 return 0;
714}
715
200c75f0
JH
716static int is_scissors_line(const struct strbuf *line)
717{
718 size_t i, len = line->len;
719 int scissors = 0, gap = 0;
720 int first_nonblank = -1;
721 int last_nonblank = 0, visible, perforation = 0, in_perforation = 0;
722 const char *buf = line->buf;
723
724 for (i = 0; i < len; i++) {
725 if (isspace(buf[i])) {
726 if (in_perforation) {
727 perforation++;
728 gap++;
729 }
730 continue;
731 }
732 last_nonblank = i;
733 if (first_nonblank < 0)
734 first_nonblank = i;
735 if (buf[i] == '-') {
736 in_perforation = 1;
737 perforation++;
738 continue;
739 }
740 if (i + 1 < len &&
741 (!memcmp(buf + i, ">8", 2) || !memcmp(buf + i, "8<", 2))) {
742 in_perforation = 1;
743 perforation += 2;
744 scissors += 2;
745 i++;
746 continue;
747 }
748 in_perforation = 0;
749 }
750
751 /*
752 * The mark must be at least 8 bytes long (e.g. "-- >8 --").
753 * Even though there can be arbitrary cruft on the same line
754 * (e.g. "cut here"), in order to avoid misidentification, the
755 * perforation must occupy more than a third of the visible
756 * width of the line, and dashes and scissors must occupy more
757 * than half of the perforation.
758 */
759
760 visible = last_nonblank - first_nonblank + 1;
761 return (scissors && 8 <= visible &&
762 visible < perforation * 3 &&
763 gap * 2 < perforation);
764}
765
3b6121f6 766static int handle_commit_msg(struct strbuf *line)
d4a9ce78 767{
87ab7992
DZ
768 static int still_looking = 1;
769
d4a9ce78
JH
770 if (!cmitmsg)
771 return 0;
2744b234 772
87ab7992 773 if (still_looking) {
3b6121f6
LS
774 strbuf_ltrim(line);
775 if (!line->len)
776 return 0;
200c75f0
JH
777 still_looking = check_header(line, s_hdr_data, 0);
778 if (still_looking)
87ab7992
DZ
779 return 0;
780 }
8b4525fb 781
86747c13
DZ
782 /* normalize the log message to UTF-8. */
783 if (metainfo_charset)
3b6121f6 784 convert_to_utf8(line, charset.buf);
86747c13 785
017678b4 786 if (use_scissors && is_scissors_line(line)) {
200c75f0 787 int i;
1be224ba
JH
788 if (fseek(cmitmsg, 0L, SEEK_SET))
789 die_errno("Could not rewind output message file");
790 if (ftruncate(fileno(cmitmsg), 0))
791 die_errno("Could not truncate output message file at scissors");
200c75f0
JH
792 still_looking = 1;
793
794 /*
795 * We may have already read "secondary headers"; purge
796 * them to give ourselves a clean restart.
797 */
798 for (i = 0; header[i]; i++) {
799 if (s_hdr_data[i])
800 strbuf_release(s_hdr_data[i]);
801 s_hdr_data[i] = NULL;
802 }
803 return 0;
804 }
805
f0658cf2 806 if (patchbreak(line)) {
87ab7992
DZ
807 fclose(cmitmsg);
808 cmitmsg = NULL;
809 return 1;
810 }
8b4525fb 811
3b6121f6 812 fputs(line->buf, cmitmsg);
d4a9ce78 813 return 0;
2744b234
LT
814}
815
3b6121f6 816static void handle_patch(const struct strbuf *line)
2744b234 817{
3b6121f6 818 fwrite(line->buf, 1, line->len, patchfile);
87ab7992 819 patch_lines++;
2744b234
LT
820}
821
3b6121f6 822static void handle_filter(struct strbuf *line)
2744b234 823{
87ab7992 824 static int filter = 0;
2744b234 825
3b6121f6 826 /* filter tells us which part we left off on */
87ab7992
DZ
827 switch (filter) {
828 case 0:
3b6121f6 829 if (!handle_commit_msg(line))
d4a9ce78 830 break;
87ab7992
DZ
831 filter++;
832 case 1:
3b6121f6
LS
833 handle_patch(line);
834 break;
2744b234
LT
835 }
836}
837
87ab7992 838static void handle_body(void)
1d8fa411 839{
3b6121f6 840 struct strbuf prev = STRBUF_INIT;
d4a9ce78
JH
841
842 /* Skip up to the first boundary */
3b6121f6 843 if (*content_top) {
87ab7992 844 if (!find_boundary())
3b6121f6 845 goto handle_body_out;
87ab7992
DZ
846 }
847
848 do {
849 /* process any boundary lines */
3b6121f6 850 if (*content_top && is_multipart_boundary(&line)) {
87ab7992 851 /* flush any leftover */
a9fd1383
JH
852 if (prev.len) {
853 handle_filter(&prev);
854 strbuf_reset(&prev);
855 }
87ab7992 856 if (!handle_boundary())
3b6121f6 857 goto handle_body_out;
87ab7992
DZ
858 }
859
86747c13 860 /* Unwrap transfer encoding */
3b6121f6 861 decode_transfer_encoding(&line);
87ab7992
DZ
862
863 switch (transfer_encoding) {
864 case TE_BASE64:
87f1b884 865 case TE_QP:
87ab7992 866 {
3b6121f6
LS
867 struct strbuf **lines, **it, *sb;
868
869 /* Prepend any previous partial lines */
870 strbuf_insert(&line, 0, prev.buf, prev.len);
871 strbuf_reset(&prev);
87ab7992
DZ
872
873 /* binary data most likely doesn't have newlines */
874 if (message_type != TYPE_TEXT) {
3b6121f6 875 handle_filter(&line);
87ab7992
DZ
876 break;
877 }
9aa23094
JH
878 /*
879 * This is a decoded line that may contain
87ab7992
DZ
880 * multiple new lines. Pass only one chunk
881 * at a time to handle_filter()
882 */
3b6121f6
LS
883 lines = strbuf_split(&line, '\n');
884 for (it = lines; (sb = *it); it++) {
885 if (*(it + 1) == NULL) /* The last line */
886 if (sb->buf[sb->len - 1] != '\n') {
887 /* Partial line, save it for later. */
888 strbuf_addbuf(&prev, sb);
889 break;
890 }
891 handle_filter(sb);
892 }
9aa23094 893 /*
3b6121f6 894 * The partial chunk is saved in "prev" and will be
9aa23094 895 * appended by the next iteration of read_line_with_nul().
87ab7992 896 */
3b6121f6 897 strbuf_list_free(lines);
d4a9ce78 898 break;
1d8fa411 899 }
87ab7992 900 default:
3b6121f6 901 handle_filter(&line);
d4a9ce78 902 }
87ab7992 903
c8f373a5 904 } while (!strbuf_getwholeline(&line, fin, '\n'));
3b6121f6
LS
905
906handle_body_out:
907 strbuf_release(&prev);
1d8fa411
JH
908}
909
3b6121f6 910static void output_header_lines(FILE *fout, const char *hdr, const struct strbuf *data)
d7f6bae2 911{
3b6121f6 912 const char *sp = data->buf;
d7f6bae2 913 while (1) {
3b6121f6 914 char *ep = strchr(sp, '\n');
d7f6bae2
JH
915 int len;
916 if (!ep)
3b6121f6 917 len = strlen(sp);
d7f6bae2 918 else
3b6121f6
LS
919 len = ep - sp;
920 fprintf(fout, "%s: %.*s\n", hdr, len, sp);
d7f6bae2
JH
921 if (!ep)
922 break;
3b6121f6 923 sp = ep + 1;
d7f6bae2
JH
924 }
925}
926
87ab7992 927static void handle_info(void)
2744b234 928{
3b6121f6 929 struct strbuf *hdr;
87ab7992
DZ
930 int i;
931
932 for (i = 0; header[i]; i++) {
87ab7992
DZ
933 /* only print inbody headers if we output a patch file */
934 if (patch_lines && s_hdr_data[i])
935 hdr = s_hdr_data[i];
936 else if (p_hdr_data[i])
937 hdr = p_hdr_data[i];
938 else
939 continue;
940
941 if (!memcmp(header[i], "Subject", 7)) {
3b6121f6
LS
942 if (!keep_subject) {
943 cleanup_subject(hdr);
944 cleanup_space(hdr);
d7f6bae2 945 }
3b6121f6 946 output_header_lines(fout, "Subject", hdr);
87ab7992 947 } else if (!memcmp(header[i], "From", 4)) {
ddfb3696 948 cleanup_space(hdr);
87ab7992 949 handle_from(hdr);
3b6121f6
LS
950 fprintf(fout, "Author: %s\n", name.buf);
951 fprintf(fout, "Email: %s\n", email.buf);
87ab7992
DZ
952 } else {
953 cleanup_space(hdr);
3b6121f6 954 fprintf(fout, "%s: %s\n", header[i], hdr->buf);
87ab7992 955 }
d4a9ce78 956 }
87ab7992 957 fprintf(fout, "\n");
2744b234
LT
958}
959
606417bc 960static int mailinfo(FILE *in, FILE *out, const char *msg, const char *patch)
34488e3c 961{
f88a545a 962 int peek;
34488e3c
LS
963 fin = in;
964 fout = out;
965
966 cmitmsg = fopen(msg, "w");
967 if (!cmitmsg) {
968 perror(msg);
969 return -1;
970 }
971 patchfile = fopen(patch, "w");
972 if (!patchfile) {
973 perror(patch);
974 fclose(cmitmsg);
975 return -1;
976 }
87ab7992 977
3b6121f6
LS
978 p_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*p_hdr_data));
979 s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*s_hdr_data));
87ab7992 980
f88a545a
SS
981 do {
982 peek = fgetc(in);
983 } while (isspace(peek));
984 ungetc(peek, in);
985
87ab7992 986 /* process the email header */
3b6121f6
LS
987 while (read_one_header_line(&line, fin))
988 check_header(&line, p_hdr_data, 1);
87ab7992
DZ
989
990 handle_body();
991 handle_info();
34488e3c
LS
992
993 return 0;
994}
995
43485d3d
JH
996static int git_mailinfo_config(const char *var, const char *value, void *unused)
997{
998 if (prefixcmp(var, "mailinfo."))
999 return git_default_config(var, value, unused);
1000 if (!strcmp(var, "mailinfo.scissors")) {
1001 use_scissors = git_config_bool(var, value);
1002 return 0;
1003 }
1004 /* perhaps others here */
1005 return 0;
1006}
1007
6bff6a60 1008static const char mailinfo_usage[] =
434a6db7 1009 "git mailinfo [-k] [-u | --encoding=<encoding> | -n] [--scissors | --no-scissors] msg patch < mail >info";
d4a9ce78 1010
a633fca0 1011int cmd_mailinfo(int argc, const char **argv, const char *prefix)
2744b234 1012{
bb1091a4
JH
1013 const char *def_charset;
1014
f1f909e3
JH
1015 /* NEEDSWORK: might want to do the optional .git/ directory
1016 * discovery
1017 */
43485d3d 1018 git_config(git_mailinfo_config, NULL);
f1f909e3 1019
330db18c 1020 def_charset = (git_commit_encoding ? git_commit_encoding : "UTF-8");
bb1091a4
JH
1021 metainfo_charset = def_charset;
1022
6bff6a60
JH
1023 while (1 < argc && argv[1][0] == '-') {
1024 if (!strcmp(argv[1], "-k"))
1025 keep_subject = 1;
d4a9ce78 1026 else if (!strcmp(argv[1], "-u"))
bb1091a4
JH
1027 metainfo_charset = def_charset;
1028 else if (!strcmp(argv[1], "-n"))
1029 metainfo_charset = NULL;
cc44c765 1030 else if (!prefixcmp(argv[1], "--encoding="))
9f63892b 1031 metainfo_charset = argv[1] + 11;
017678b4
JH
1032 else if (!strcmp(argv[1], "--scissors"))
1033 use_scissors = 1;
1034 else if (!strcmp(argv[1], "--no-scissors"))
1035 use_scissors = 0;
d4a9ce78 1036 else
f1f909e3 1037 usage(mailinfo_usage);
6bff6a60
JH
1038 argc--; argv++;
1039 }
1040
a196d8d4 1041 if (argc != 3)
f1f909e3 1042 usage(mailinfo_usage);
34488e3c 1043
606417bc 1044 return !!mailinfo(stdin, stdout, argv[1], argv[2]);
2744b234 1045}