git-apply: work from subdirectory.
[git/jnareb-git.git] / mailinfo.c
blob890e3487adf65b937f9a9d1ec82d59c746bf62de
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 #include <iconv.h>
11 #include "cache.h"
13 #ifdef NO_STRCASESTR
14 extern char *gitstrcasestr(const char *haystack, const char *needle);
15 #endif
17 static FILE *cmitmsg, *patchfile;
19 static int keep_subject = 0;
20 static char *metainfo_charset = NULL;
21 static char line[1000];
22 static char date[1000];
23 static char name[1000];
24 static char email[1000];
25 static char subject[1000];
27 static enum {
28 TE_DONTCARE, TE_QP, TE_BASE64,
29 } transfer_encoding;
30 static char charset[256];
32 static char multipart_boundary[1000];
33 static int multipart_boundary_len;
34 static int patch_lines = 0;
36 static char *sanity_check(char *name, char *email)
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;
46 static int handle_from(char *line)
48 char *at = strchr(line, '@');
49 char *dst;
51 if (!at)
52 return 0;
55 * If we already have one email, don't take any confusing lines
57 if (*email && strchr(at+1, '@'))
58 return 0;
60 /* Pick up the string around '@', possibly delimited with <>
61 * pair; that is the email part. White them out while copying.
63 while (at > line) {
64 char c = at[-1];
65 if (isspace(c))
66 break;
67 if (c == '<') {
68 at[-1] = ' ';
69 break;
71 at--;
73 dst = email;
74 for (;;) {
75 unsigned char c = *at;
76 if (!c || c == '>' || isspace(c)) {
77 if (c == '>')
78 *at = ' ';
79 break;
81 *at++ = ' ';
82 *dst++ = c;
84 *dst++ = 0;
86 /* The remainder is name. It could be "John Doe <john.doe@xz>"
87 * or "john.doe@xz (John Doe)", but we have whited out the
88 * email part, so trim from both ends, possibly removing
89 * the () pair at the end.
91 at = line + strlen(line);
92 while (at > line) {
93 unsigned char c = *--at;
94 if (!isspace(c)) {
95 at[(c == ')') ? 0 : 1] = 0;
96 break;
100 at = line;
101 for (;;) {
102 unsigned char c = *at;
103 if (!c || !isspace(c)) {
104 if (c == '(')
105 at++;
106 break;
108 at++;
110 at = sanity_check(at, email);
111 strcpy(name, at);
112 return 1;
115 static int handle_date(char *line)
117 strcpy(date, line);
118 return 0;
121 static int handle_subject(char *line)
123 strcpy(subject, line);
124 return 0;
127 /* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
128 * to have enough heuristics to grok MIME encoded patches often found
129 * on our mailing lists. For example, we do not even treat header lines
130 * case insensitively.
133 static int slurp_attr(const char *line, const char *name, char *attr)
135 char *ends, *ap = strcasestr(line, name);
136 size_t sz;
138 if (!ap) {
139 *attr = 0;
140 return 0;
142 ap += strlen(name);
143 if (*ap == '"') {
144 ap++;
145 ends = "\"";
147 else
148 ends = "; \t";
149 sz = strcspn(ap, ends);
150 memcpy(attr, ap, sz);
151 attr[sz] = 0;
152 return 1;
155 static int handle_subcontent_type(char *line)
157 /* We do not want to mess with boundary. Note that we do not
158 * handle nested multipart.
160 if (strcasestr(line, "boundary=")) {
161 fprintf(stderr, "Not handling nested multipart message.\n");
162 exit(1);
164 slurp_attr(line, "charset=", charset);
165 if (*charset) {
166 int i, c;
167 for (i = 0; (c = charset[i]) != 0; i++)
168 charset[i] = tolower(c);
170 return 0;
173 static int handle_content_type(char *line)
175 *multipart_boundary = 0;
176 if (slurp_attr(line, "boundary=", multipart_boundary + 2)) {
177 memcpy(multipart_boundary, "--", 2);
178 multipart_boundary_len = strlen(multipart_boundary);
180 slurp_attr(line, "charset=", charset);
181 return 0;
184 static int handle_content_transfer_encoding(char *line)
186 if (strcasestr(line, "base64"))
187 transfer_encoding = TE_BASE64;
188 else if (strcasestr(line, "quoted-printable"))
189 transfer_encoding = TE_QP;
190 else
191 transfer_encoding = TE_DONTCARE;
192 return 0;
195 static int is_multipart_boundary(const char *line)
197 return (!memcmp(line, multipart_boundary, multipart_boundary_len));
200 static int eatspace(char *line)
202 int len = strlen(line);
203 while (len > 0 && isspace(line[len-1]))
204 line[--len] = 0;
205 return len;
208 #define SEEN_FROM 01
209 #define SEEN_DATE 02
210 #define SEEN_SUBJECT 04
212 /* First lines of body can have From:, Date:, and Subject: */
213 static int handle_inbody_header(int *seen, char *line)
215 if (!memcmp("From:", line, 5) && isspace(line[5])) {
216 if (!(*seen & SEEN_FROM) && handle_from(line+6)) {
217 *seen |= SEEN_FROM;
218 return 1;
221 if (!memcmp("Date:", line, 5) && isspace(line[5])) {
222 if (!(*seen & SEEN_DATE)) {
223 handle_date(line+6);
224 *seen |= SEEN_DATE;
225 return 1;
228 if (!memcmp("Subject:", line, 8) && isspace(line[8])) {
229 if (!(*seen & SEEN_SUBJECT)) {
230 handle_subject(line+9);
231 *seen |= SEEN_SUBJECT;
232 return 1;
235 if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) {
236 if (!(*seen & SEEN_SUBJECT)) {
237 handle_subject(line);
238 *seen |= SEEN_SUBJECT;
239 return 1;
242 return 0;
245 static char *cleanup_subject(char *subject)
247 if (keep_subject)
248 return subject;
249 for (;;) {
250 char *p;
251 int len, remove;
252 switch (*subject) {
253 case 'r': case 'R':
254 if (!memcmp("e:", subject+1, 2)) {
255 subject +=3;
256 continue;
258 break;
259 case ' ': case '\t': case ':':
260 subject++;
261 continue;
263 case '[':
264 p = strchr(subject, ']');
265 if (!p) {
266 subject++;
267 continue;
269 len = strlen(p);
270 remove = p - subject;
271 if (remove <= len *2) {
272 subject = p+1;
273 continue;
275 break;
277 return subject;
281 static void cleanup_space(char *buf)
283 unsigned char c;
284 while ((c = *buf) != 0) {
285 buf++;
286 if (isspace(c)) {
287 buf[-1] = ' ';
288 c = *buf;
289 while (isspace(c)) {
290 int len = strlen(buf);
291 memmove(buf, buf+1, len);
292 c = *buf;
298 typedef int (*header_fn_t)(char *);
299 struct header_def {
300 const char *name;
301 header_fn_t func;
302 int namelen;
305 static void check_header(char *line, int len, struct header_def *header)
307 int i;
309 if (header[0].namelen <= 0) {
310 for (i = 0; header[i].name; i++)
311 header[i].namelen = strlen(header[i].name);
313 for (i = 0; header[i].name; i++) {
314 int len = header[i].namelen;
315 if (!strncasecmp(line, header[i].name, len) &&
316 line[len] == ':' && isspace(line[len + 1])) {
317 header[i].func(line + len + 2);
318 break;
323 static void check_subheader_line(char *line, int len)
325 static struct header_def header[] = {
326 { "Content-Type", handle_subcontent_type },
327 { "Content-Transfer-Encoding",
328 handle_content_transfer_encoding },
329 { NULL },
331 check_header(line, len, header);
333 static void check_header_line(char *line, int len)
335 static struct header_def header[] = {
336 { "From", handle_from },
337 { "Date", handle_date },
338 { "Subject", handle_subject },
339 { "Content-Type", handle_content_type },
340 { "Content-Transfer-Encoding",
341 handle_content_transfer_encoding },
342 { NULL },
344 check_header(line, len, header);
347 static int read_one_header_line(char *line, int sz, FILE *in)
349 int ofs = 0;
350 while (ofs < sz) {
351 int peek, len;
352 if (fgets(line + ofs, sz - ofs, in) == NULL)
353 return ofs;
354 len = eatspace(line + ofs);
355 if (len == 0)
356 return ofs;
357 peek = fgetc(in); ungetc(peek, in);
358 if (peek == ' ' || peek == '\t') {
359 /* Yuck, 2822 header "folding" */
360 ofs += len;
361 continue;
363 return ofs + len;
365 return ofs;
368 static unsigned hexval(int c)
370 if (c >= '0' && c <= '9')
371 return c - '0';
372 if (c >= 'a' && c <= 'f')
373 return c - 'a' + 10;
374 if (c >= 'A' && c <= 'F')
375 return c - 'A' + 10;
376 return ~0;
379 static int decode_q_segment(char *in, char *ot, char *ep)
381 int c;
382 while ((c = *in++) != 0 && (in <= ep)) {
383 if (c == '=') {
384 int d = *in++;
385 if (d == '\n' || !d)
386 break; /* drop trailing newline */
387 *ot++ = ((hexval(d) << 4) | hexval(*in++));
389 else
390 *ot++ = c;
392 *ot = 0;
393 return 0;
396 static int decode_b_segment(char *in, char *ot, char *ep)
398 /* Decode in..ep, possibly in-place to ot */
399 int c, pos = 0, acc = 0;
401 while ((c = *in++) != 0 && (in <= ep)) {
402 if (c == '+')
403 c = 62;
404 else if (c == '/')
405 c = 63;
406 else if ('A' <= c && c <= 'Z')
407 c -= 'A';
408 else if ('a' <= c && c <= 'z')
409 c -= 'a' - 26;
410 else if ('0' <= c && c <= '9')
411 c -= '0' - 52;
412 else if (c == '=') {
413 /* padding is almost like (c == 0), except we do
414 * not output NUL resulting only from it;
415 * for now we just trust the data.
417 c = 0;
419 else
420 continue; /* garbage */
421 switch (pos++) {
422 case 0:
423 acc = (c << 2);
424 break;
425 case 1:
426 *ot++ = (acc | (c >> 4));
427 acc = (c & 15) << 4;
428 break;
429 case 2:
430 *ot++ = (acc | (c >> 2));
431 acc = (c & 3) << 6;
432 break;
433 case 3:
434 *ot++ = (acc | c);
435 acc = pos = 0;
436 break;
439 *ot = 0;
440 return 0;
443 static void convert_to_utf8(char *line, char *charset)
445 char *in, *out;
446 size_t insize, outsize, nrc;
447 char outbuf[4096]; /* cheat */
448 static char latin_one[] = "latin-1";
449 char *input_charset = *charset ? charset : latin_one;
450 iconv_t conv = iconv_open(metainfo_charset, input_charset);
452 if (conv == (iconv_t) -1) {
453 static int warned_latin1_once = 0;
454 if (input_charset != latin_one) {
455 fprintf(stderr, "cannot convert from %s to %s\n",
456 input_charset, metainfo_charset);
457 *charset = 0;
459 else if (!warned_latin1_once) {
460 warned_latin1_once = 1;
461 fprintf(stderr, "tried to convert from %s to %s, "
462 "but your iconv does not work with it.\n",
463 input_charset, metainfo_charset);
465 return;
467 in = line;
468 insize = strlen(in);
469 out = outbuf;
470 outsize = sizeof(outbuf);
471 nrc = iconv(conv, &in, &insize, &out, &outsize);
472 iconv_close(conv);
473 if (nrc == (size_t) -1)
474 return;
475 *out = 0;
476 strcpy(line, outbuf);
479 static void decode_header_bq(char *it)
481 char *in, *out, *ep, *cp, *sp;
482 char outbuf[1000];
484 in = it;
485 out = outbuf;
486 while ((ep = strstr(in, "=?")) != NULL) {
487 int sz, encoding;
488 char charset_q[256], piecebuf[256];
489 if (in != ep) {
490 sz = ep - in;
491 memcpy(out, in, sz);
492 out += sz;
493 in += sz;
495 /* E.g.
496 * ep : "=?iso-2022-jp?B?GyR...?= foo"
497 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
499 ep += 2;
500 cp = strchr(ep, '?');
501 if (!cp)
502 return; /* no munging */
503 for (sp = ep; sp < cp; sp++)
504 charset_q[sp - ep] = tolower(*sp);
505 charset_q[cp - ep] = 0;
506 encoding = cp[1];
507 if (!encoding || cp[2] != '?')
508 return; /* no munging */
509 ep = strstr(cp + 3, "?=");
510 if (!ep)
511 return; /* no munging */
512 switch (tolower(encoding)) {
513 default:
514 return; /* no munging */
515 case 'b':
516 sz = decode_b_segment(cp + 3, piecebuf, ep);
517 break;
518 case 'q':
519 sz = decode_q_segment(cp + 3, piecebuf, ep);
520 break;
522 if (sz < 0)
523 return;
524 if (metainfo_charset)
525 convert_to_utf8(piecebuf, charset_q);
526 strcpy(out, piecebuf);
527 out += strlen(out);
528 in = ep + 2;
530 strcpy(out, in);
531 strcpy(it, outbuf);
534 static void decode_transfer_encoding(char *line)
536 char *ep;
538 switch (transfer_encoding) {
539 case TE_QP:
540 ep = line + strlen(line);
541 decode_q_segment(line, line, ep);
542 break;
543 case TE_BASE64:
544 ep = line + strlen(line);
545 decode_b_segment(line, line, ep);
546 break;
547 case TE_DONTCARE:
548 break;
552 static void handle_info(void)
554 char *sub;
555 static int done_info = 0;
557 if (done_info)
558 return;
560 done_info = 1;
561 sub = cleanup_subject(subject);
562 cleanup_space(name);
563 cleanup_space(date);
564 cleanup_space(email);
565 cleanup_space(sub);
567 /* Unwrap inline B and Q encoding, and optionally
568 * normalize the meta information to utf8.
570 decode_header_bq(name);
571 decode_header_bq(date);
572 decode_header_bq(email);
573 decode_header_bq(sub);
574 printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
575 name, email, sub, date);
578 /* We are inside message body and have read line[] already.
579 * Spit out the commit log.
581 static int handle_commit_msg(void)
583 if (!cmitmsg)
584 return 0;
585 do {
586 if (!memcmp("diff -", line, 6) ||
587 !memcmp("---", line, 3) ||
588 !memcmp("Index: ", line, 7))
589 break;
590 if ((multipart_boundary[0] && is_multipart_boundary(line))) {
591 /* We come here when the first part had only
592 * the commit message without any patch. We
593 * pretend we have not seen this line yet, and
594 * go back to the loop.
596 return 1;
599 /* Unwrap transfer encoding and optionally
600 * normalize the log message to UTF-8.
602 decode_transfer_encoding(line);
603 if (metainfo_charset)
604 convert_to_utf8(line, charset);
605 fputs(line, cmitmsg);
606 } while (fgets(line, sizeof(line), stdin) != NULL);
607 fclose(cmitmsg);
608 cmitmsg = NULL;
609 return 0;
612 /* We have done the commit message and have the first
613 * line of the patch in line[].
615 static void handle_patch(void)
617 do {
618 if (multipart_boundary[0] && is_multipart_boundary(line))
619 break;
620 /* Only unwrap transfer encoding but otherwise do not
621 * do anything. We do *NOT* want UTF-8 conversion
622 * here; we are dealing with the user payload.
624 decode_transfer_encoding(line);
625 fputs(line, patchfile);
626 patch_lines++;
627 } while (fgets(line, sizeof(line), stdin) != NULL);
630 /* multipart boundary and transfer encoding are set up for us, and we
631 * are at the end of the sub header. do equivalent of handle_body up
632 * to the next boundary without closing patchfile --- we will expect
633 * that the first part to contain commit message and a patch, and
634 * handle other parts as pure patches.
636 static int handle_multipart_one_part(void)
638 int seen = 0;
639 int n = 0;
640 int len;
642 while (fgets(line, sizeof(line), stdin) != NULL) {
643 again:
644 len = eatspace(line);
645 n++;
646 if (!len)
647 continue;
648 if (is_multipart_boundary(line))
649 break;
650 if (0 <= seen && handle_inbody_header(&seen, line))
651 continue;
652 seen = -1; /* no more inbody headers */
653 line[len] = '\n';
654 handle_info();
655 if (handle_commit_msg())
656 goto again;
657 handle_patch();
658 break;
660 if (n == 0)
661 return -1;
662 return 0;
665 static void handle_multipart_body(void)
667 int part_num = 0;
669 /* Skip up to the first boundary */
670 while (fgets(line, sizeof(line), stdin) != NULL)
671 if (is_multipart_boundary(line)) {
672 part_num = 1;
673 break;
675 if (!part_num)
676 return;
677 /* We are on boundary line. Start slurping the subhead. */
678 while (1) {
679 int len = read_one_header_line(line, sizeof(line), stdin);
680 if (!len) {
681 if (handle_multipart_one_part() < 0)
682 return;
684 else
685 check_subheader_line(line, len);
687 fclose(patchfile);
688 if (!patch_lines) {
689 fprintf(stderr, "No patch found\n");
690 exit(1);
694 /* Non multipart message */
695 static void handle_body(void)
697 int seen = 0;
699 while (fgets(line, sizeof(line), stdin) != NULL) {
700 int len = eatspace(line);
701 if (!len)
702 continue;
703 if (0 <= seen && handle_inbody_header(&seen, line))
704 continue;
705 seen = -1; /* no more inbody headers */
706 line[len] = '\n';
707 handle_info();
708 handle_commit_msg();
709 handle_patch();
710 break;
712 fclose(patchfile);
713 if (!patch_lines) {
714 fprintf(stderr, "No patch found\n");
715 exit(1);
719 static const char mailinfo_usage[] =
720 "git-mailinfo [-k] [-u | --encoding=<encoding>] msg patch <mail >info";
722 int main(int argc, char **argv)
724 /* NEEDSWORK: might want to do the optional .git/ directory
725 * discovery
727 git_config(git_default_config);
729 while (1 < argc && argv[1][0] == '-') {
730 if (!strcmp(argv[1], "-k"))
731 keep_subject = 1;
732 else if (!strcmp(argv[1], "-u"))
733 metainfo_charset = git_commit_encoding;
734 else if (!strncmp(argv[1], "--encoding=", 11))
735 metainfo_charset = argv[1] + 11;
736 else
737 usage(mailinfo_usage);
738 argc--; argv++;
741 if (argc != 3)
742 usage(mailinfo_usage);
743 cmitmsg = fopen(argv[1], "w");
744 if (!cmitmsg) {
745 perror(argv[1]);
746 exit(1);
748 patchfile = fopen(argv[2], "w");
749 if (!patchfile) {
750 perror(argv[2]);
751 exit(1);
753 while (1) {
754 int len = read_one_header_line(line, sizeof(line), stdin);
755 if (!len) {
756 if (multipart_boundary[0])
757 handle_multipart_body();
758 else
759 handle_body();
760 break;
762 check_header_line(line, len);
764 return 0;