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