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