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