1/*
2 * Another stupid program, this one parsing the headers of an
3 * email to figure out authorship and subject
4 */
5#define _GNU_SOURCE
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <ctype.h>
10#ifndef NO_ICONV
11#include <iconv.h>
12#endif
13#include "git-compat-util.h"
14#include "cache.h"
15
16static FILE *cmitmsg, *patchfile;
17
18static int keep_subject = 0;
19static char *metainfo_charset = NULL;
20static char line[1000];
21static char date[1000];
22static char name[1000];
23static char email[1000];
24static char subject[1000];
25
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
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
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
75static int handle_from(char *in_line)
76{
77 char line[1000];
78 char *at;
79 char *dst;
80
81 strcpy(line, in_line);
82 at = strchr(line, '@');
83 if (!at)
84 return bogus_from(line);
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
92 /* Pick up the string around '@', possibly delimited with <>
93 * pair; that is the email part. White them out while copying.
94 */
95 while (at > line) {
96 char c = at[-1];
97 if (isspace(c))
98 break;
99 if (c == '<') {
100 at[-1] = ' ';
101 break;
102 }
103 at--;
104 }
105 dst = email;
106 for (;;) {
107 unsigned char c = *at;
108 if (!c || c == '>' || isspace(c)) {
109 if (c == '>')
110 *at = ' ';
111 break;
112 }
113 *at++ = ' ';
114 *dst++ = c;
115 }
116 *dst++ = 0;
117
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 */
123 at = line + strlen(line);
124 while (at > line) {
125 unsigned char c = *--at;
126 if (!isspace(c)) {
127 at[(c == ')') ? 0 : 1] = 0;
128 break;
129 }
130 }
131
132 at = line;
133 for (;;) {
134 unsigned char c = *at;
135 if (!c || !isspace(c)) {
136 if (c == '(')
137 at++;
138 break;
139 }
140 at++;
141 }
142 at = sanity_check(at, email);
143 strcpy(name, at);
144 return 1;
145}
146
147static int handle_date(char *line)
148{
149 strcpy(date, line);
150 return 0;
151}
152
153static int handle_subject(char *line)
154{
155 strcpy(subject, line);
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 */
192 if (strcasestr(line, "boundary=")) {
193 fprintf(stderr, "Not handling nested multipart message.\n");
194 exit(1);
195 }
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;
225}
226
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)
233{
234 int len = strlen(line);
235 while (len > 0 && isspace(line[len-1]))
236 line[--len] = 0;
237 return len;
238}
239
240#define SEEN_FROM 01
241#define SEEN_DATE 02
242#define SEEN_SUBJECT 04
243#define SEEN_BOGUS_UNIX_FROM 010
244#define SEEN_PREFIX 020
245
246/* First lines of body can have From:, Date:, and Subject: or empty */
247static void handle_inbody_header(int *seen, char *line)
248{
249 if (*seen & SEEN_PREFIX)
250 return;
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 }
260 if (!memcmp(">From", line, 5) && isspace(line[5])) {
261 if (!(*seen & SEEN_BOGUS_UNIX_FROM)) {
262 *seen |= SEEN_BOGUS_UNIX_FROM;
263 return;
264 }
265 }
266 if (!memcmp("From:", line, 5) && isspace(line[5])) {
267 if (!(*seen & SEEN_FROM) && handle_from(line+6)) {
268 *seen |= SEEN_FROM;
269 return;
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;
276 return;
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;
283 return;
284 }
285 }
286 if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) {
287 if (!(*seen & SEEN_SUBJECT)) {
288 handle_subject(line);
289 *seen |= SEEN_SUBJECT;
290 return;
291 }
292 }
293 *seen |= SEEN_PREFIX;
294}
295
296static char *cleanup_subject(char *subject)
297{
298 if (keep_subject)
299 return subject;
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 }
328 eatspace(subject);
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
350static void decode_header_bq(char *it);
351typedef int (*header_fn_t)(char *);
352struct header_def {
353 const char *name;
354 header_fn_t func;
355 int namelen;
356};
357
358static void check_header(char *line, struct header_def *header)
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])) {
370 /* Unwrap inline B and Q encoding, and optionally
371 * normalize the meta information to utf8.
372 */
373 decode_header_bq(line + len + 2);
374 header[i].func(line + len + 2);
375 break;
376 }
377 }
378}
379
380static void check_subheader_line(char *line)
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 };
388 check_header(line, header);
389}
390static void check_header_line(char *line)
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 };
401 check_header(line, header);
402}
403
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
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)
433 break;
434 len = eatspace(line + ofs);
435 if ((len == 0) || !is_rfc2822_header(line)) {
436 /* Re-add the newline */
437 line[ofs + len] = '\n';
438 line[ofs + len + 1] = '\0';
439 break;
440 }
441 ofs += len;
442 /* Yuck, 2822 header "folding" */
443 peek = fgetc(in); ungetc(peek, in);
444 if (peek != ' ' && peek != '\t')
445 break;
446 }
447 /* Count mbox From headers as headers */
448 if (!ofs && !memcmp(line, "From ", 5))
449 ofs = 1;
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
464static int decode_q_segment(char *in, char *ot, char *ep, int rfc2047)
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++));
473 continue;
474 }
475 if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
476 c = 0x20;
477 *ot++ = c;
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{
532#ifndef NO_ICONV
533 char *in, *out;
534 size_t insize, outsize, nrc;
535 char outbuf[4096]; /* cheat */
536 static char latin_one[] = "latin1";
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);
545 *charset = 0;
546 }
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;
554 }
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);
565#endif
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':
608 sz = decode_q_segment(cp + 3, piecebuf, ep, 1);
609 break;
610 }
611 if (sz < 0)
612 return;
613 if (metainfo_charset)
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);
630 decode_q_segment(line, line, ep, 0);
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)
642{
643 char *sub;
644
645 sub = cleanup_subject(subject);
646 cleanup_space(name);
647 cleanup_space(date);
648 cleanup_space(email);
649 cleanup_space(sub);
650
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 */
658static int handle_commit_msg(int *seen)
659{
660 if (!cmitmsg)
661 return 0;
662 do {
663 if (!memcmp("diff -", line, 6) ||
664 !memcmp("---", line, 3) ||
665 !memcmp("Index: ", line, 7))
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 }
675
676 /* Unwrap transfer encoding and optionally
677 * normalize the log message to UTF-8.
678 */
679 decode_transfer_encoding(line);
680 if (metainfo_charset)
681 convert_to_utf8(line, charset);
682
683 handle_inbody_header(seen, line);
684 if (!(*seen & SEEN_PREFIX))
685 continue;
686
687 fputs(line, cmitmsg);
688 } while (fgets(line, sizeof(line), stdin) != NULL);
689 fclose(cmitmsg);
690 cmitmsg = NULL;
691 return 0;
692}
693
694/* We have done the commit message and have the first
695 * line of the patch in line[].
696 */
697static void handle_patch(void)
698{
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);
710}
711
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 */
718static int handle_multipart_one_part(int *seen)
719{
720 int n = 0;
721
722 while (fgets(line, sizeof(line), stdin) != NULL) {
723 again:
724 n++;
725 if (is_multipart_boundary(line))
726 break;
727 if (handle_commit_msg(seen))
728 goto again;
729 handle_patch();
730 break;
731 }
732 if (n == 0)
733 return -1;
734 return 0;
735}
736
737static void handle_multipart_body(void)
738{
739 int seen = 0;
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;
747 }
748 if (!part_num)
749 return;
750 /* We are on boundary line. Start slurping the subhead. */
751 while (1) {
752 int hdr = read_one_header_line(line, sizeof(line), stdin);
753 if (!hdr) {
754 if (handle_multipart_one_part(&seen) < 0)
755 return;
756 /* Reset per part headers */
757 transfer_encoding = TE_DONTCARE;
758 charset[0] = 0;
759 }
760 else
761 check_subheader_line(line);
762 }
763 fclose(patchfile);
764 if (!patch_lines) {
765 fprintf(stderr, "No patch found\n");
766 exit(1);
767 }
768}
769
770/* Non multipart message */
771static void handle_body(void)
772{
773 int seen = 0;
774
775 handle_commit_msg(&seen);
776 handle_patch();
777 fclose(patchfile);
778 if (!patch_lines) {
779 fprintf(stderr, "No patch found\n");
780 exit(1);
781 }
782}
783
784static const char mailinfo_usage[] =
785 "git-mailinfo [-k] [-u | --encoding=<encoding>] msg patch <mail >info";
786
787int main(int argc, char **argv)
788{
789 /* NEEDSWORK: might want to do the optional .git/ directory
790 * discovery
791 */
792 git_config(git_default_config);
793
794 while (1 < argc && argv[1][0] == '-') {
795 if (!strcmp(argv[1], "-k"))
796 keep_subject = 1;
797 else if (!strcmp(argv[1], "-u"))
798 metainfo_charset = git_commit_encoding;
799 else if (!strncmp(argv[1], "--encoding=", 11))
800 metainfo_charset = argv[1] + 11;
801 else
802 usage(mailinfo_usage);
803 argc--; argv++;
804 }
805
806 if (argc != 3)
807 usage(mailinfo_usage);
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 }
818 while (1) {
819 int hdr = read_one_header_line(line, sizeof(line), stdin);
820 if (!hdr) {
821 if (multipart_boundary[0])
822 handle_multipart_body();
823 else
824 handle_body();
825 handle_info();
826 break;
827 }
828 check_header_line(line);
829 }
830 return 0;
831}