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