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