]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-mailinfo.c
Do not create $GIT_DIR/remotes/ directory anymore.
[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 */
d4a9ce78 5#define _GNU_SOURCE
2744b234
LT
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <ctype.h>
b6e56eca 10#ifndef NO_ICONV
d4a9ce78 11#include <iconv.h>
b6e56eca 12#endif
a6da9395 13#include "git-compat-util.h"
f1f909e3 14#include "cache.h"
34488e3c 15#include "builtin.h"
2744b234 16
34488e3c 17static FILE *cmitmsg, *patchfile, *fin, *fout;
2744b234 18
96f1e58f
DR
19static int keep_subject;
20static const char *metainfo_charset;
2744b234 21static char line[1000];
62c1f6b4 22static char date[1000];
2744b234
LT
23static char name[1000];
24static char email[1000];
25static char subject[1000];
26
d4a9ce78
JH
27static enum {
28 TE_DONTCARE, TE_QP, TE_BASE64,
29} transfer_encoding;
30static char charset[256];
31
32static char multipart_boundary[1000];
33static int multipart_boundary_len;
96f1e58f 34static int patch_lines;
d4a9ce78 35
2744b234
LT
36static char *sanity_check(char *name, char *email)
37{
38 int len = strlen(name);
39 if (len < 3 || len > 60)
40 return email;
41 if (strchr(name, '@') || strchr(name, '<') || strchr(name, '>'))
42 return email;
43 return name;
44}
45
e0e3ba20
JH
46static int bogus_from(char *line)
47{
48 /* John Doe <johndoe> */
49 char *bra, *ket, *dst, *cp;
50
51 /* This is fallback, so do not bother if we already have an
52 * e-mail address.
34488e3c 53 */
e0e3ba20
JH
54 if (*email)
55 return 0;
56
57 bra = strchr(line, '<');
58 if (!bra)
59 return 0;
60 ket = strchr(bra, '>');
61 if (!ket)
62 return 0;
63
64 for (dst = email, cp = bra+1; cp < ket; )
65 *dst++ = *cp++;
66 *dst = 0;
67 for (cp = line; isspace(*cp); cp++)
68 ;
69 for (bra--; isspace(*bra); bra--)
70 *bra = 0;
71 cp = sanity_check(cp, email);
72 strcpy(name, cp);
73 return 1;
74}
75
2dec02b1 76static int handle_from(char *in_line)
2744b234 77{
2dec02b1
EB
78 char line[1000];
79 char *at;
2744b234
LT
80 char *dst;
81
2dec02b1
EB
82 strcpy(line, in_line);
83 at = strchr(line, '@');
2744b234 84 if (!at)
e0e3ba20 85 return bogus_from(line);
2744b234
LT
86
87 /*
88 * If we already have one email, don't take any confusing lines
89 */
90 if (*email && strchr(at+1, '@'))
91 return 0;
92
d4a9ce78
JH
93 /* Pick up the string around '@', possibly delimited with <>
94 * pair; that is the email part. White them out while copying.
95 */
2744b234
LT
96 while (at > line) {
97 char c = at[-1];
d4a9ce78
JH
98 if (isspace(c))
99 break;
100 if (c == '<') {
101 at[-1] = ' ';
2744b234 102 break;
d4a9ce78 103 }
2744b234
LT
104 at--;
105 }
106 dst = email;
107 for (;;) {
108 unsigned char c = *at;
d4a9ce78
JH
109 if (!c || c == '>' || isspace(c)) {
110 if (c == '>')
111 *at = ' ';
2744b234 112 break;
d4a9ce78 113 }
2744b234
LT
114 *at++ = ' ';
115 *dst++ = c;
116 }
117 *dst++ = 0;
118
d4a9ce78
JH
119 /* The remainder is name. It could be "John Doe <john.doe@xz>"
120 * or "john.doe@xz (John Doe)", but we have whited out the
121 * email part, so trim from both ends, possibly removing
122 * the () pair at the end.
123 */
2744b234
LT
124 at = line + strlen(line);
125 while (at > line) {
126 unsigned char c = *--at;
d4a9ce78
JH
127 if (!isspace(c)) {
128 at[(c == ')') ? 0 : 1] = 0;
2744b234 129 break;
d4a9ce78 130 }
2744b234
LT
131 }
132
133 at = line;
134 for (;;) {
135 unsigned char c = *at;
d4a9ce78
JH
136 if (!c || !isspace(c)) {
137 if (c == '(')
138 at++;
2744b234 139 break;
d4a9ce78 140 }
2744b234
LT
141 at++;
142 }
2744b234 143 at = sanity_check(at, email);
2744b234
LT
144 strcpy(name, at);
145 return 1;
146}
147
d4a9ce78 148static int handle_date(char *line)
62c1f6b4
LT
149{
150 strcpy(date, line);
d4a9ce78 151 return 0;
62c1f6b4
LT
152}
153
d4a9ce78 154static int handle_subject(char *line)
2744b234
LT
155{
156 strcpy(subject, line);
d4a9ce78
JH
157 return 0;
158}
159
160/* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
161 * to have enough heuristics to grok MIME encoded patches often found
162 * on our mailing lists. For example, we do not even treat header lines
163 * case insensitively.
164 */
165
166static int slurp_attr(const char *line, const char *name, char *attr)
167{
554fe20d 168 const char *ends, *ap = strcasestr(line, name);
d4a9ce78
JH
169 size_t sz;
170
171 if (!ap) {
172 *attr = 0;
173 return 0;
174 }
175 ap += strlen(name);
176 if (*ap == '"') {
177 ap++;
178 ends = "\"";
179 }
180 else
181 ends = "; \t";
182 sz = strcspn(ap, ends);
183 memcpy(attr, ap, sz);
184 attr[sz] = 0;
185 return 1;
186}
187
188static int handle_subcontent_type(char *line)
189{
190 /* We do not want to mess with boundary. Note that we do not
191 * handle nested multipart.
192 */
b893f091
JH
193 if (strcasestr(line, "boundary=")) {
194 fprintf(stderr, "Not handling nested multipart message.\n");
195 exit(1);
196 }
d4a9ce78
JH
197 slurp_attr(line, "charset=", charset);
198 if (*charset) {
199 int i, c;
200 for (i = 0; (c = charset[i]) != 0; i++)
201 charset[i] = tolower(c);
202 }
203 return 0;
204}
205
206static int handle_content_type(char *line)
207{
208 *multipart_boundary = 0;
209 if (slurp_attr(line, "boundary=", multipart_boundary + 2)) {
210 memcpy(multipart_boundary, "--", 2);
211 multipart_boundary_len = strlen(multipart_boundary);
212 }
213 slurp_attr(line, "charset=", charset);
214 return 0;
215}
216
217static int handle_content_transfer_encoding(char *line)
218{
219 if (strcasestr(line, "base64"))
220 transfer_encoding = TE_BASE64;
221 else if (strcasestr(line, "quoted-printable"))
222 transfer_encoding = TE_QP;
223 else
224 transfer_encoding = TE_DONTCARE;
225 return 0;
2744b234
LT
226}
227
d4a9ce78
JH
228static int is_multipart_boundary(const char *line)
229{
230 return (!memcmp(line, multipart_boundary, multipart_boundary_len));
231}
232
233static int eatspace(char *line)
2744b234 234{
d4a9ce78
JH
235 int len = strlen(line);
236 while (len > 0 && isspace(line[len-1]))
237 line[--len] = 0;
238 return len;
2744b234
LT
239}
240
d4a9ce78
JH
241#define SEEN_FROM 01
242#define SEEN_DATE 02
243#define SEEN_SUBJECT 04
81c5cf78 244#define SEEN_BOGUS_UNIX_FROM 010
d177e584 245#define SEEN_PREFIX 020
d4a9ce78 246
ae448e38 247/* First lines of body can have From:, Date:, and Subject: or empty */
8b4525fb 248static void handle_inbody_header(int *seen, char *line)
d4a9ce78 249{
2662dbfa
EB
250 if (*seen & SEEN_PREFIX)
251 return;
ae448e38
JH
252 if (isspace(*line)) {
253 char *cp;
254 for (cp = line + 1; *cp; cp++) {
255 if (!isspace(*cp))
256 break;
257 }
258 if (!*cp)
259 return;
260 }
81c5cf78
JH
261 if (!memcmp(">From", line, 5) && isspace(line[5])) {
262 if (!(*seen & SEEN_BOGUS_UNIX_FROM)) {
263 *seen |= SEEN_BOGUS_UNIX_FROM;
d177e584 264 return;
81c5cf78
JH
265 }
266 }
d4a9ce78
JH
267 if (!memcmp("From:", line, 5) && isspace(line[5])) {
268 if (!(*seen & SEEN_FROM) && handle_from(line+6)) {
269 *seen |= SEEN_FROM;
8b4525fb 270 return;
d4a9ce78
JH
271 }
272 }
273 if (!memcmp("Date:", line, 5) && isspace(line[5])) {
274 if (!(*seen & SEEN_DATE)) {
275 handle_date(line+6);
276 *seen |= SEEN_DATE;
8b4525fb 277 return;
d4a9ce78
JH
278 }
279 }
280 if (!memcmp("Subject:", line, 8) && isspace(line[8])) {
281 if (!(*seen & SEEN_SUBJECT)) {
282 handle_subject(line+9);
283 *seen |= SEEN_SUBJECT;
8b4525fb 284 return;
d4a9ce78
JH
285 }
286 }
287 if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) {
288 if (!(*seen & SEEN_SUBJECT)) {
289 handle_subject(line);
290 *seen |= SEEN_SUBJECT;
8b4525fb 291 return;
d4a9ce78
JH
292 }
293 }
8b4525fb 294 *seen |= SEEN_PREFIX;
d4a9ce78
JH
295}
296
297static char *cleanup_subject(char *subject)
2744b234 298{
6bff6a60
JH
299 if (keep_subject)
300 return subject;
2744b234
LT
301 for (;;) {
302 char *p;
303 int len, remove;
304 switch (*subject) {
305 case 'r': case 'R':
306 if (!memcmp("e:", subject+1, 2)) {
307 subject +=3;
308 continue;
309 }
310 break;
311 case ' ': case '\t': case ':':
312 subject++;
313 continue;
314
315 case '[':
316 p = strchr(subject, ']');
317 if (!p) {
318 subject++;
319 continue;
320 }
321 len = strlen(p);
322 remove = p - subject;
323 if (remove <= len *2) {
324 subject = p+1;
325 continue;
34488e3c 326 }
2744b234
LT
327 break;
328 }
ae448e38 329 eatspace(subject);
2744b234
LT
330 return subject;
331 }
34488e3c 332}
2744b234
LT
333
334static void cleanup_space(char *buf)
335{
336 unsigned char c;
337 while ((c = *buf) != 0) {
338 buf++;
339 if (isspace(c)) {
340 buf[-1] = ' ';
341 c = *buf;
342 while (isspace(c)) {
343 int len = strlen(buf);
344 memmove(buf, buf+1, len);
345 c = *buf;
346 }
347 }
348 }
349}
350
b75bf2c3 351static void decode_header(char *it);
d4a9ce78
JH
352typedef int (*header_fn_t)(char *);
353struct header_def {
354 const char *name;
355 header_fn_t func;
356 int namelen;
357};
358
f8128cfb 359static void check_header(char *line, struct header_def *header)
d4a9ce78
JH
360{
361 int i;
362
363 if (header[0].namelen <= 0) {
364 for (i = 0; header[i].name; i++)
365 header[i].namelen = strlen(header[i].name);
366 }
367 for (i = 0; header[i].name; i++) {
368 int len = header[i].namelen;
369 if (!strncasecmp(line, header[i].name, len) &&
370 line[len] == ':' && isspace(line[len + 1])) {
33504530
EB
371 /* Unwrap inline B and Q encoding, and optionally
372 * normalize the meta information to utf8.
373 */
b75bf2c3 374 decode_header(line + len + 2);
d4a9ce78
JH
375 header[i].func(line + len + 2);
376 break;
377 }
378 }
379}
380
f8128cfb 381static void check_subheader_line(char *line)
d4a9ce78
JH
382{
383 static struct header_def header[] = {
384 { "Content-Type", handle_subcontent_type },
385 { "Content-Transfer-Encoding",
386 handle_content_transfer_encoding },
387 { NULL },
388 };
f8128cfb 389 check_header(line, header);
d4a9ce78 390}
f8128cfb 391static void check_header_line(char *line)
d4a9ce78
JH
392{
393 static struct header_def header[] = {
394 { "From", handle_from },
395 { "Date", handle_date },
396 { "Subject", handle_subject },
397 { "Content-Type", handle_content_type },
398 { "Content-Transfer-Encoding",
399 handle_content_transfer_encoding },
400 { NULL },
401 };
f8128cfb 402 check_header(line, header);
d4a9ce78
JH
403}
404
ef29c117
JH
405static int is_rfc2822_header(char *line)
406{
407 /*
408 * The section that defines the loosest possible
409 * field name is "3.6.8 Optional fields".
410 *
411 * optional-field = field-name ":" unstructured CRLF
412 * field-name = 1*ftext
413 * ftext = %d33-57 / %59-126
414 */
415 int ch;
416 char *cp = line;
417 while ((ch = *cp++)) {
418 if (ch == ':')
419 return cp != line;
420 if ((33 <= ch && ch <= 57) ||
421 (59 <= ch && ch <= 126))
422 continue;
423 break;
424 }
425 return 0;
426}
427
d4a9ce78
JH
428static int read_one_header_line(char *line, int sz, FILE *in)
429{
430 int ofs = 0;
431 while (ofs < sz) {
432 int peek, len;
433 if (fgets(line + ofs, sz - ofs, in) == NULL)
f30b2028 434 break;
d4a9ce78 435 len = eatspace(line + ofs);
ae448e38 436 if ((len == 0) || !is_rfc2822_header(line)) {
f30b2028
EB
437 /* Re-add the newline */
438 line[ofs + len] = '\n';
439 line[ofs + len + 1] = '\0';
440 break;
d4a9ce78 441 }
f30b2028
EB
442 ofs += len;
443 /* Yuck, 2822 header "folding" */
444 peek = fgetc(in); ungetc(peek, in);
445 if (peek != ' ' && peek != '\t')
446 break;
d4a9ce78 447 }
f30b2028 448 /* Count mbox From headers as headers */
c2c487cf 449 if (!ofs && (!memcmp(line, "From ", 5) || !memcmp(line, ">From ", 6)))
f30b2028 450 ofs = 1;
d4a9ce78
JH
451 return ofs;
452}
453
75731930 454static int decode_q_segment(char *in, char *ot, char *ep, int rfc2047)
d4a9ce78
JH
455{
456 int c;
457 while ((c = *in++) != 0 && (in <= ep)) {
458 if (c == '=') {
459 int d = *in++;
460 if (d == '\n' || !d)
461 break; /* drop trailing newline */
462 *ot++ = ((hexval(d) << 4) | hexval(*in++));
75731930 463 continue;
d4a9ce78 464 }
75731930
JH
465 if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
466 c = 0x20;
467 *ot++ = c;
d4a9ce78
JH
468 }
469 *ot = 0;
470 return 0;
471}
472
473static int decode_b_segment(char *in, char *ot, char *ep)
474{
475 /* Decode in..ep, possibly in-place to ot */
476 int c, pos = 0, acc = 0;
477
478 while ((c = *in++) != 0 && (in <= ep)) {
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;
489 else if (c == '=') {
490 /* padding is almost like (c == 0), except we do
491 * not output NUL resulting only from it;
492 * for now we just trust the data.
493 */
494 c = 0;
495 }
496 else
497 continue; /* garbage */
498 switch (pos++) {
499 case 0:
500 acc = (c << 2);
501 break;
502 case 1:
503 *ot++ = (acc | (c >> 4));
504 acc = (c & 15) << 4;
505 break;
506 case 2:
507 *ot++ = (acc | (c >> 2));
508 acc = (c & 3) << 6;
509 break;
510 case 3:
511 *ot++ = (acc | c);
512 acc = pos = 0;
513 break;
514 }
515 }
516 *ot = 0;
517 return 0;
518}
519
520static void convert_to_utf8(char *line, char *charset)
521{
b6e56eca 522#ifndef NO_ICONV
650e4be5
JH
523 char *in, *out;
524 size_t insize, outsize, nrc;
525 char outbuf[4096]; /* cheat */
ac44f3e7 526 static char latin_one[] = "latin1";
650e4be5
JH
527 char *input_charset = *charset ? charset : latin_one;
528 iconv_t conv = iconv_open(metainfo_charset, input_charset);
529
530 if (conv == (iconv_t) -1) {
531 static int warned_latin1_once = 0;
532 if (input_charset != latin_one) {
533 fprintf(stderr, "cannot convert from %s to %s\n",
534 input_charset, metainfo_charset);
d4a9ce78 535 *charset = 0;
d4a9ce78 536 }
650e4be5
JH
537 else if (!warned_latin1_once) {
538 warned_latin1_once = 1;
539 fprintf(stderr, "tried to convert from %s to %s, "
540 "but your iconv does not work with it.\n",
541 input_charset, metainfo_charset);
542 }
543 return;
d4a9ce78 544 }
650e4be5
JH
545 in = line;
546 insize = strlen(in);
547 out = outbuf;
548 outsize = sizeof(outbuf);
549 nrc = iconv(conv, &in, &insize, &out, &outsize);
550 iconv_close(conv);
551 if (nrc == (size_t) -1)
552 return;
553 *out = 0;
554 strcpy(line, outbuf);
b6e56eca 555#endif
d4a9ce78
JH
556}
557
b75bf2c3 558static int decode_header_bq(char *it)
d4a9ce78
JH
559{
560 char *in, *out, *ep, *cp, *sp;
561 char outbuf[1000];
b75bf2c3 562 int rfc2047 = 0;
d4a9ce78
JH
563
564 in = it;
565 out = outbuf;
566 while ((ep = strstr(in, "=?")) != NULL) {
567 int sz, encoding;
568 char charset_q[256], piecebuf[256];
b75bf2c3
JH
569 rfc2047 = 1;
570
d4a9ce78
JH
571 if (in != ep) {
572 sz = ep - in;
573 memcpy(out, in, sz);
574 out += sz;
575 in += sz;
576 }
577 /* E.g.
578 * ep : "=?iso-2022-jp?B?GyR...?= foo"
579 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
580 */
581 ep += 2;
582 cp = strchr(ep, '?');
583 if (!cp)
b75bf2c3 584 return rfc2047; /* no munging */
d4a9ce78
JH
585 for (sp = ep; sp < cp; sp++)
586 charset_q[sp - ep] = tolower(*sp);
587 charset_q[cp - ep] = 0;
588 encoding = cp[1];
589 if (!encoding || cp[2] != '?')
b75bf2c3 590 return rfc2047; /* no munging */
d4a9ce78
JH
591 ep = strstr(cp + 3, "?=");
592 if (!ep)
b75bf2c3 593 return rfc2047; /* no munging */
d4a9ce78
JH
594 switch (tolower(encoding)) {
595 default:
b75bf2c3 596 return rfc2047; /* no munging */
d4a9ce78
JH
597 case 'b':
598 sz = decode_b_segment(cp + 3, piecebuf, ep);
599 break;
600 case 'q':
75731930 601 sz = decode_q_segment(cp + 3, piecebuf, ep, 1);
d4a9ce78
JH
602 break;
603 }
604 if (sz < 0)
b75bf2c3 605 return rfc2047;
650e4be5 606 if (metainfo_charset)
d4a9ce78
JH
607 convert_to_utf8(piecebuf, charset_q);
608 strcpy(out, piecebuf);
609 out += strlen(out);
610 in = ep + 2;
611 }
612 strcpy(out, in);
613 strcpy(it, outbuf);
b75bf2c3
JH
614 return rfc2047;
615}
616
617static void decode_header(char *it)
618{
619
620 if (decode_header_bq(it))
621 return;
622 /* otherwise "it" is a straight copy of the input.
623 * This can be binary guck but there is no charset specified.
624 */
625 if (metainfo_charset)
626 convert_to_utf8(it, "");
d4a9ce78
JH
627}
628
629static void decode_transfer_encoding(char *line)
630{
631 char *ep;
632
633 switch (transfer_encoding) {
634 case TE_QP:
635 ep = line + strlen(line);
75731930 636 decode_q_segment(line, line, ep, 0);
d4a9ce78
JH
637 break;
638 case TE_BASE64:
639 ep = line + strlen(line);
640 decode_b_segment(line, line, ep);
641 break;
642 case TE_DONTCARE:
643 break;
644 }
645}
646
647static void handle_info(void)
2744b234 648{
d4a9ce78 649 char *sub;
d4a9ce78 650
d4a9ce78 651 sub = cleanup_subject(subject);
2744b234 652 cleanup_space(name);
62c1f6b4 653 cleanup_space(date);
2744b234
LT
654 cleanup_space(email);
655 cleanup_space(sub);
2744b234 656
34488e3c 657 fprintf(fout, "Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
d4a9ce78
JH
658 name, email, sub, date);
659}
660
661/* We are inside message body and have read line[] already.
662 * Spit out the commit log.
663 */
8b4525fb 664static int handle_commit_msg(int *seen)
d4a9ce78
JH
665{
666 if (!cmitmsg)
667 return 0;
2744b234 668 do {
a196d8d4 669 if (!memcmp("diff -", line, 6) ||
fc7ef1e8
LT
670 !memcmp("---", line, 3) ||
671 !memcmp("Index: ", line, 7))
d4a9ce78
JH
672 break;
673 if ((multipart_boundary[0] && is_multipart_boundary(line))) {
674 /* We come here when the first part had only
675 * the commit message without any patch. We
676 * pretend we have not seen this line yet, and
677 * go back to the loop.
678 */
679 return 1;
680 }
2744b234 681
d4a9ce78
JH
682 /* Unwrap transfer encoding and optionally
683 * normalize the log message to UTF-8.
684 */
685 decode_transfer_encoding(line);
650e4be5 686 if (metainfo_charset)
d4a9ce78 687 convert_to_utf8(line, charset);
8b4525fb
EB
688
689 handle_inbody_header(seen, line);
690 if (!(*seen & SEEN_PREFIX))
691 continue;
692
d4a9ce78 693 fputs(line, cmitmsg);
34488e3c 694 } while (fgets(line, sizeof(line), fin) != NULL);
2744b234 695 fclose(cmitmsg);
d4a9ce78
JH
696 cmitmsg = NULL;
697 return 0;
2744b234
LT
698}
699
d4a9ce78
JH
700/* We have done the commit message and have the first
701 * line of the patch in line[].
702 */
703static void handle_patch(void)
2744b234 704{
d4a9ce78
JH
705 do {
706 if (multipart_boundary[0] && is_multipart_boundary(line))
707 break;
708 /* Only unwrap transfer encoding but otherwise do not
709 * do anything. We do *NOT* want UTF-8 conversion
710 * here; we are dealing with the user payload.
711 */
712 decode_transfer_encoding(line);
713 fputs(line, patchfile);
714 patch_lines++;
34488e3c 715 } while (fgets(line, sizeof(line), fin) != NULL);
2744b234
LT
716}
717
d4a9ce78
JH
718/* multipart boundary and transfer encoding are set up for us, and we
719 * are at the end of the sub header. do equivalent of handle_body up
720 * to the next boundary without closing patchfile --- we will expect
721 * that the first part to contain commit message and a patch, and
722 * handle other parts as pure patches.
723 */
8b4525fb 724static int handle_multipart_one_part(int *seen)
2744b234 725{
d4a9ce78 726 int n = 0;
2744b234 727
34488e3c 728 while (fgets(line, sizeof(line), fin) != NULL) {
d4a9ce78 729 again:
d4a9ce78 730 n++;
d4a9ce78
JH
731 if (is_multipart_boundary(line))
732 break;
8b4525fb 733 if (handle_commit_msg(seen))
d4a9ce78
JH
734 goto again;
735 handle_patch();
2744b234
LT
736 break;
737 }
d4a9ce78
JH
738 if (n == 0)
739 return -1;
740 return 0;
2744b234
LT
741}
742
d4a9ce78 743static void handle_multipart_body(void)
1d8fa411 744{
8b4525fb 745 int seen = 0;
d4a9ce78
JH
746 int part_num = 0;
747
748 /* Skip up to the first boundary */
34488e3c 749 while (fgets(line, sizeof(line), fin) != NULL)
d4a9ce78
JH
750 if (is_multipart_boundary(line)) {
751 part_num = 1;
752 break;
1d8fa411 753 }
d4a9ce78
JH
754 if (!part_num)
755 return;
756 /* We are on boundary line. Start slurping the subhead. */
757 while (1) {
34488e3c 758 int hdr = read_one_header_line(line, sizeof(line), fin);
f8128cfb 759 if (!hdr) {
8b4525fb 760 if (handle_multipart_one_part(&seen) < 0)
d4a9ce78 761 return;
8bc5c04a
JH
762 /* Reset per part headers */
763 transfer_encoding = TE_DONTCARE;
764 charset[0] = 0;
d4a9ce78
JH
765 }
766 else
f8128cfb 767 check_subheader_line(line);
d4a9ce78
JH
768 }
769 fclose(patchfile);
770 if (!patch_lines) {
771 fprintf(stderr, "No patch found\n");
772 exit(1);
1d8fa411 773 }
1d8fa411
JH
774}
775
d4a9ce78
JH
776/* Non multipart message */
777static void handle_body(void)
2744b234 778{
d4a9ce78
JH
779 int seen = 0;
780
ae448e38
JH
781 handle_commit_msg(&seen);
782 handle_patch();
d4a9ce78
JH
783 fclose(patchfile);
784 if (!patch_lines) {
785 fprintf(stderr, "No patch found\n");
786 exit(1);
787 }
2744b234
LT
788}
789
34488e3c
LS
790int mailinfo(FILE *in, FILE *out, int ks, const char *encoding,
791 const char *msg, const char *patch)
792{
793 keep_subject = ks;
794 metainfo_charset = encoding;
795 fin = in;
796 fout = out;
797
798 cmitmsg = fopen(msg, "w");
799 if (!cmitmsg) {
800 perror(msg);
801 return -1;
802 }
803 patchfile = fopen(patch, "w");
804 if (!patchfile) {
805 perror(patch);
806 fclose(cmitmsg);
807 return -1;
808 }
809 while (1) {
810 int hdr = read_one_header_line(line, sizeof(line), fin);
811 if (!hdr) {
812 if (multipart_boundary[0])
813 handle_multipart_body();
814 else
815 handle_body();
816 handle_info();
817 break;
818 }
819 check_header_line(line);
820 }
821
822 return 0;
823}
824
6bff6a60 825static const char mailinfo_usage[] =
9f63892b 826 "git-mailinfo [-k] [-u | --encoding=<encoding>] msg patch <mail >info";
d4a9ce78 827
a633fca0 828int cmd_mailinfo(int argc, const char **argv, const char *prefix)
2744b234 829{
f1f909e3
JH
830 /* NEEDSWORK: might want to do the optional .git/ directory
831 * discovery
832 */
833 git_config(git_default_config);
834
6bff6a60
JH
835 while (1 < argc && argv[1][0] == '-') {
836 if (!strcmp(argv[1], "-k"))
837 keep_subject = 1;
d4a9ce78 838 else if (!strcmp(argv[1], "-u"))
f1f909e3 839 metainfo_charset = git_commit_encoding;
9f63892b
JH
840 else if (!strncmp(argv[1], "--encoding=", 11))
841 metainfo_charset = argv[1] + 11;
d4a9ce78 842 else
f1f909e3 843 usage(mailinfo_usage);
6bff6a60
JH
844 argc--; argv++;
845 }
846
a196d8d4 847 if (argc != 3)
f1f909e3 848 usage(mailinfo_usage);
34488e3c
LS
849
850 return !!mailinfo(stdin, stdout, keep_subject, metainfo_charset, argv[1], argv[2]);
2744b234 851}