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