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