2 * Another stupid program, this one parsing the headers of an
3 * email to figure out authorship and subject
11 #include "git-compat-util.h"
14 static FILE *cmitmsg, *patchfile;
16 static int keep_subject = 0;
17 static char *metainfo_charset = NULL;
18 static char line[1000];
19 static char date[1000];
20 static char name[1000];
21 static char email[1000];
22 static char subject[1000];
25 TE_DONTCARE, TE_QP, TE_BASE64,
27 static char charset[256];
29 static char multipart_boundary[1000];
30 static int multipart_boundary_len;
31 static int patch_lines = 0;
33 static char *sanity_check(char *name, char *email)
35 int len = strlen(name);
36 if (len < 3 || len > 60)
38 if (strchr(name, '@') || strchr(name, '<') || strchr(name, '>'))
43 static int bogus_from(char *line)
45 /* John Doe <johndoe> */
46 char *bra, *ket, *dst, *cp;
48 /* This is fallback, so do not bother if we already have an
54 bra = strchr(line, '<');
57 ket = strchr(bra, '>');
61 for (dst = email, cp = bra+1; cp < ket; )
64 for (cp = line; isspace(*cp); cp++)
66 for (bra--; isspace(*bra); bra--)
68 cp = sanity_check(cp, email);
73 static int handle_from(char *line)
75 char *at = strchr(line, '@');
79 return bogus_from(line);
82 * If we already have one email, don't take any confusing lines
84 if (*email && strchr(at+1, '@'))
87 /* Pick up the string around '@', possibly delimited with <>
88 * pair; that is the email part. White them out while copying.
102 unsigned char c = *at;
103 if (!c || c == '>' || isspace(c)) {
113 /* The remainder is name. It could be "John Doe <john.doe@xz>"
114 * or "john.doe@xz (John Doe)", but we have whited out the
115 * email part, so trim from both ends, possibly removing
116 * the () pair at the end.
118 at = line + strlen(line);
120 unsigned char c = *--at;
122 at[(c == ')') ? 0 : 1] = 0;
129 unsigned char c = *at;
130 if (!c || !isspace(c)) {
137 at = sanity_check(at, email);
142 static int handle_date(char *line)
148 static int handle_subject(char *line)
150 strcpy(subject, line);
154 /* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
155 * to have enough heuristics to grok MIME encoded patches often found
156 * on our mailing lists. For example, we do not even treat header lines
157 * case insensitively.
160 static int slurp_attr(const char *line, const char *name, char *attr)
162 char *ends, *ap = strcasestr(line, name);
176 sz = strcspn(ap, ends);
177 memcpy(attr, ap, sz);
182 static int handle_subcontent_type(char *line)
184 /* We do not want to mess with boundary. Note that we do not
185 * handle nested multipart.
187 if (strcasestr(line, "boundary=")) {
188 fprintf(stderr, "Not handling nested multipart message.\n");
191 slurp_attr(line, "charset=", charset);
194 for (i = 0; (c = charset[i]) != 0; i++)
195 charset[i] = tolower(c);
200 static int handle_content_type(char *line)
202 *multipart_boundary = 0;
203 if (slurp_attr(line, "boundary=", multipart_boundary + 2)) {
204 memcpy(multipart_boundary, "--", 2);
205 multipart_boundary_len = strlen(multipart_boundary);
207 slurp_attr(line, "charset=", charset);
211 static int handle_content_transfer_encoding(char *line)
213 if (strcasestr(line, "base64"))
214 transfer_encoding = TE_BASE64;
215 else if (strcasestr(line, "quoted-printable"))
216 transfer_encoding = TE_QP;
218 transfer_encoding = TE_DONTCARE;
222 static int is_multipart_boundary(const char *line)
224 return (!memcmp(line, multipart_boundary, multipart_boundary_len));
227 static int eatspace(char *line)
229 int len = strlen(line);
230 while (len > 0 && isspace(line[len-1]))
237 #define SEEN_SUBJECT 04
239 /* First lines of body can have From:, Date:, and Subject: */
240 static int handle_inbody_header(int *seen, char *line)
242 if (!memcmp("From:", line, 5) && isspace(line[5])) {
243 if (!(*seen & SEEN_FROM) && handle_from(line+6)) {
248 if (!memcmp("Date:", line, 5) && isspace(line[5])) {
249 if (!(*seen & SEEN_DATE)) {
255 if (!memcmp("Subject:", line, 8) && isspace(line[8])) {
256 if (!(*seen & SEEN_SUBJECT)) {
257 handle_subject(line+9);
258 *seen |= SEEN_SUBJECT;
262 if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) {
263 if (!(*seen & SEEN_SUBJECT)) {
264 handle_subject(line);
265 *seen |= SEEN_SUBJECT;
272 static char *cleanup_subject(char *subject)
281 if (!memcmp("e:", subject+1, 2)) {
286 case ' ': case '\t': case ':':
291 p = strchr(subject, ']');
297 remove = p - subject;
298 if (remove <= len *2) {
308 static void cleanup_space(char *buf)
311 while ((c = *buf) != 0) {
317 int len = strlen(buf);
318 memmove(buf, buf+1, len);
325 typedef int (*header_fn_t)(char *);
332 static void check_header(char *line, int len, struct header_def *header)
336 if (header[0].namelen <= 0) {
337 for (i = 0; header[i].name; i++)
338 header[i].namelen = strlen(header[i].name);
340 for (i = 0; header[i].name; i++) {
341 int len = header[i].namelen;
342 if (!strncasecmp(line, header[i].name, len) &&
343 line[len] == ':' && isspace(line[len + 1])) {
344 header[i].func(line + len + 2);
350 static void check_subheader_line(char *line, int len)
352 static struct header_def header[] = {
353 { "Content-Type", handle_subcontent_type },
354 { "Content-Transfer-Encoding",
355 handle_content_transfer_encoding },
358 check_header(line, len, header);
360 static void check_header_line(char *line, int len)
362 static struct header_def header[] = {
363 { "From", handle_from },
364 { "Date", handle_date },
365 { "Subject", handle_subject },
366 { "Content-Type", handle_content_type },
367 { "Content-Transfer-Encoding",
368 handle_content_transfer_encoding },
371 check_header(line, len, header);
374 static int read_one_header_line(char *line, int sz, FILE *in)
379 if (fgets(line + ofs, sz - ofs, in) == NULL)
381 len = eatspace(line + ofs);
384 peek = fgetc(in); ungetc(peek, in);
385 if (peek == ' ' || peek == '\t') {
386 /* Yuck, 2822 header "folding" */
395 static unsigned hexval(int c)
397 if (c >= '0' && c <= '9')
399 if (c >= 'a' && c <= 'f')
401 if (c >= 'A' && c <= 'F')
406 static int decode_q_segment(char *in, char *ot, char *ep)
409 while ((c = *in++) != 0 && (in <= ep)) {
413 break; /* drop trailing newline */
414 *ot++ = ((hexval(d) << 4) | hexval(*in++));
423 static int decode_b_segment(char *in, char *ot, char *ep)
425 /* Decode in..ep, possibly in-place to ot */
426 int c, pos = 0, acc = 0;
428 while ((c = *in++) != 0 && (in <= ep)) {
433 else if ('A' <= c && c <= 'Z')
435 else if ('a' <= c && c <= 'z')
437 else if ('0' <= c && c <= '9')
440 /* padding is almost like (c == 0), except we do
441 * not output NUL resulting only from it;
442 * for now we just trust the data.
447 continue; /* garbage */
453 *ot++ = (acc | (c >> 4));
457 *ot++ = (acc | (c >> 2));
470 static void convert_to_utf8(char *line, char *charset)
473 size_t insize, outsize, nrc;
474 char outbuf[4096]; /* cheat */
475 static char latin_one[] = "latin1";
476 char *input_charset = *charset ? charset : latin_one;
477 iconv_t conv = iconv_open(metainfo_charset, input_charset);
479 if (conv == (iconv_t) -1) {
480 static int warned_latin1_once = 0;
481 if (input_charset != latin_one) {
482 fprintf(stderr, "cannot convert from %s to %s\n",
483 input_charset, metainfo_charset);
486 else if (!warned_latin1_once) {
487 warned_latin1_once = 1;
488 fprintf(stderr, "tried to convert from %s to %s, "
489 "but your iconv does not work with it.\n",
490 input_charset, metainfo_charset);
497 outsize = sizeof(outbuf);
498 nrc = iconv(conv, &in, &insize, &out, &outsize);
500 if (nrc == (size_t) -1)
503 strcpy(line, outbuf);
506 static void decode_header_bq(char *it)
508 char *in, *out, *ep, *cp, *sp;
513 while ((ep = strstr(in, "=?")) != NULL) {
515 char charset_q[256], piecebuf[256];
523 * ep : "=?iso-2022-jp?B?GyR...?= foo"
524 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
527 cp = strchr(ep, '?');
529 return; /* no munging */
530 for (sp = ep; sp < cp; sp++)
531 charset_q[sp - ep] = tolower(*sp);
532 charset_q[cp - ep] = 0;
534 if (!encoding || cp[2] != '?')
535 return; /* no munging */
536 ep = strstr(cp + 3, "?=");
538 return; /* no munging */
539 switch (tolower(encoding)) {
541 return; /* no munging */
543 sz = decode_b_segment(cp + 3, piecebuf, ep);
546 sz = decode_q_segment(cp + 3, piecebuf, ep);
551 if (metainfo_charset)
552 convert_to_utf8(piecebuf, charset_q);
553 strcpy(out, piecebuf);
561 static void decode_transfer_encoding(char *line)
565 switch (transfer_encoding) {
567 ep = line + strlen(line);
568 decode_q_segment(line, line, ep);
571 ep = line + strlen(line);
572 decode_b_segment(line, line, ep);
579 static void handle_info(void)
582 static int done_info = 0;
588 sub = cleanup_subject(subject);
591 cleanup_space(email);
594 /* Unwrap inline B and Q encoding, and optionally
595 * normalize the meta information to utf8.
597 decode_header_bq(name);
598 decode_header_bq(date);
599 decode_header_bq(email);
600 decode_header_bq(sub);
601 printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
602 name, email, sub, date);
605 /* We are inside message body and have read line[] already.
606 * Spit out the commit log.
608 static int handle_commit_msg(void)
613 if (!memcmp("diff -", line, 6) ||
614 !memcmp("---", line, 3) ||
615 !memcmp("Index: ", line, 7))
617 if ((multipart_boundary[0] && is_multipart_boundary(line))) {
618 /* We come here when the first part had only
619 * the commit message without any patch. We
620 * pretend we have not seen this line yet, and
621 * go back to the loop.
626 /* Unwrap transfer encoding and optionally
627 * normalize the log message to UTF-8.
629 decode_transfer_encoding(line);
630 if (metainfo_charset)
631 convert_to_utf8(line, charset);
632 fputs(line, cmitmsg);
633 } while (fgets(line, sizeof(line), stdin) != NULL);
639 /* We have done the commit message and have the first
640 * line of the patch in line[].
642 static void handle_patch(void)
645 if (multipart_boundary[0] && is_multipart_boundary(line))
647 /* Only unwrap transfer encoding but otherwise do not
648 * do anything. We do *NOT* want UTF-8 conversion
649 * here; we are dealing with the user payload.
651 decode_transfer_encoding(line);
652 fputs(line, patchfile);
654 } while (fgets(line, sizeof(line), stdin) != NULL);
657 /* multipart boundary and transfer encoding are set up for us, and we
658 * are at the end of the sub header. do equivalent of handle_body up
659 * to the next boundary without closing patchfile --- we will expect
660 * that the first part to contain commit message and a patch, and
661 * handle other parts as pure patches.
663 static int handle_multipart_one_part(void)
669 while (fgets(line, sizeof(line), stdin) != NULL) {
671 len = eatspace(line);
675 if (is_multipart_boundary(line))
677 if (0 <= seen && handle_inbody_header(&seen, line))
679 seen = -1; /* no more inbody headers */
682 if (handle_commit_msg())
692 static void handle_multipart_body(void)
696 /* Skip up to the first boundary */
697 while (fgets(line, sizeof(line), stdin) != NULL)
698 if (is_multipart_boundary(line)) {
704 /* We are on boundary line. Start slurping the subhead. */
706 int len = read_one_header_line(line, sizeof(line), stdin);
708 if (handle_multipart_one_part() < 0)
712 check_subheader_line(line, len);
716 fprintf(stderr, "No patch found\n");
721 /* Non multipart message */
722 static void handle_body(void)
726 while (fgets(line, sizeof(line), stdin) != NULL) {
727 int len = eatspace(line);
730 if (0 <= seen && handle_inbody_header(&seen, line))
732 seen = -1; /* no more inbody headers */
741 fprintf(stderr, "No patch found\n");
746 static const char mailinfo_usage[] =
747 "git-mailinfo [-k] [-u | --encoding=<encoding>] msg patch <mail >info";
749 int main(int argc, char **argv)
751 /* NEEDSWORK: might want to do the optional .git/ directory
754 git_config(git_default_config);
756 while (1 < argc && argv[1][0] == '-') {
757 if (!strcmp(argv[1], "-k"))
759 else if (!strcmp(argv[1], "-u"))
760 metainfo_charset = git_commit_encoding;
761 else if (!strncmp(argv[1], "--encoding=", 11))
762 metainfo_charset = argv[1] + 11;
764 usage(mailinfo_usage);
769 usage(mailinfo_usage);
770 cmitmsg = fopen(argv[1], "w");
775 patchfile = fopen(argv[2], "w");
781 int len = read_one_header_line(line, sizeof(line), stdin);
783 if (multipart_boundary[0])
784 handle_multipart_body();
789 check_header_line(line, len);