2 * Another stupid program, this one parsing the headers of an
3 * email to figure out authorship and subject
14 extern char *gitstrcasestr(const char *haystack, const char *needle);
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];
28 TE_DONTCARE, TE_QP, TE_BASE64,
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)
41 if (strchr(name, '@') || strchr(name, '<') || strchr(name, '>'))
46 static int handle_from(char *line)
48 char *at = strchr(line, '@');
55 * If we already have one email, don't take any confusing lines
57 if (*email && strchr(at+1, '@'))
60 /* Pick up the string around '@', possibly delimited with <>
61 * pair; that is the email part. White them out while copying.
75 unsigned char c = *at;
76 if (!c || c == '>' || isspace(c)) {
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);
93 unsigned char c = *--at;
95 at[(c == ')') ? 0 : 1] = 0;
102 unsigned char c = *at;
103 if (!c || !isspace(c)) {
110 at = sanity_check(at, email);
115 static int handle_date(char *line)
121 static int handle_subject(char *line)
123 strcpy(subject, line);
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);
149 sz = strcspn(ap, ends);
150 memcpy(attr, ap, sz);
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");
164 slurp_attr(line, "charset=", charset);
167 for (i = 0; (c = charset[i]) != 0; i++)
168 charset[i] = tolower(c);
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);
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;
191 transfer_encoding = TE_DONTCARE;
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]))
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)) {
221 if (!memcmp("Date:", line, 5) && isspace(line[5])) {
222 if (!(*seen & SEEN_DATE)) {
228 if (!memcmp("Subject:", line, 8) && isspace(line[8])) {
229 if (!(*seen & SEEN_SUBJECT)) {
230 handle_subject(line+9);
231 *seen |= SEEN_SUBJECT;
235 if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) {
236 if (!(*seen & SEEN_SUBJECT)) {
237 handle_subject(line);
238 *seen |= SEEN_SUBJECT;
245 static char *cleanup_subject(char *subject)
254 if (!memcmp("e:", subject+1, 2)) {
259 case ' ': case '\t': case ':':
264 p = strchr(subject, ']');
270 remove = p - subject;
271 if (remove <= len *2) {
281 static void cleanup_space(char *buf)
284 while ((c = *buf) != 0) {
290 int len = strlen(buf);
291 memmove(buf, buf+1, len);
298 typedef int (*header_fn_t)(char *);
305 static void check_header(char *line, int len, struct header_def *header)
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);
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 },
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 },
344 check_header(line, len, header);
347 static int read_one_header_line(char *line, int sz, FILE *in)
352 if (fgets(line + ofs, sz - ofs, in) == NULL)
354 len = eatspace(line + ofs);
357 peek = fgetc(in); ungetc(peek, in);
358 if (peek == ' ' || peek == '\t') {
359 /* Yuck, 2822 header "folding" */
368 static unsigned hexval(int c)
370 if (c >= '0' && c <= '9')
372 if (c >= 'a' && c <= 'f')
374 if (c >= 'A' && c <= 'F')
379 static int decode_q_segment(char *in, char *ot, char *ep)
382 while ((c = *in++) != 0 && (in <= ep)) {
386 break; /* drop trailing newline */
387 *ot++ = ((hexval(d) << 4) | hexval(*in++));
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)) {
406 else if ('A' <= c && c <= 'Z')
408 else if ('a' <= c && c <= 'z')
410 else if ('0' <= c && c <= '9')
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.
420 continue; /* garbage */
426 *ot++ = (acc | (c >> 4));
430 *ot++ = (acc | (c >> 2));
443 static void convert_to_utf8(char *line, char *charset)
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);
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);
470 outsize = sizeof(outbuf);
471 nrc = iconv(conv, &in, &insize, &out, &outsize);
473 if (nrc == (size_t) -1)
476 strcpy(line, outbuf);
479 static void decode_header_bq(char *it)
481 char *in, *out, *ep, *cp, *sp;
486 while ((ep = strstr(in, "=?")) != NULL) {
488 char charset_q[256], piecebuf[256];
496 * ep : "=?iso-2022-jp?B?GyR...?= foo"
497 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
500 cp = strchr(ep, '?');
502 return; /* no munging */
503 for (sp = ep; sp < cp; sp++)
504 charset_q[sp - ep] = tolower(*sp);
505 charset_q[cp - ep] = 0;
507 if (!encoding || cp[2] != '?')
508 return; /* no munging */
509 ep = strstr(cp + 3, "?=");
511 return; /* no munging */
512 switch (tolower(encoding)) {
514 return; /* no munging */
516 sz = decode_b_segment(cp + 3, piecebuf, ep);
519 sz = decode_q_segment(cp + 3, piecebuf, ep);
524 if (metainfo_charset)
525 convert_to_utf8(piecebuf, charset_q);
526 strcpy(out, piecebuf);
534 static void decode_transfer_encoding(char *line)
538 switch (transfer_encoding) {
540 ep = line + strlen(line);
541 decode_q_segment(line, line, ep);
544 ep = line + strlen(line);
545 decode_b_segment(line, line, ep);
552 static void handle_info(void)
555 static int done_info = 0;
561 sub = cleanup_subject(subject);
564 cleanup_space(email);
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)
586 if (!memcmp("diff -", line, 6) ||
587 !memcmp("---", line, 3) ||
588 !memcmp("Index: ", line, 7))
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.
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);
612 /* We have done the commit message and have the first
613 * line of the patch in line[].
615 static void handle_patch(void)
618 if (multipart_boundary[0] && is_multipart_boundary(line))
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);
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)
642 while (fgets(line, sizeof(line), stdin) != NULL) {
644 len = eatspace(line);
648 if (is_multipart_boundary(line))
650 if (0 <= seen && handle_inbody_header(&seen, line))
652 seen = -1; /* no more inbody headers */
655 if (handle_commit_msg())
665 static void handle_multipart_body(void)
669 /* Skip up to the first boundary */
670 while (fgets(line, sizeof(line), stdin) != NULL)
671 if (is_multipart_boundary(line)) {
677 /* We are on boundary line. Start slurping the subhead. */
679 int len = read_one_header_line(line, sizeof(line), stdin);
681 if (handle_multipart_one_part() < 0)
685 check_subheader_line(line, len);
689 fprintf(stderr, "No patch found\n");
694 /* Non multipart message */
695 static void handle_body(void)
699 while (fgets(line, sizeof(line), stdin) != NULL) {
700 int len = eatspace(line);
703 if (0 <= seen && handle_inbody_header(&seen, line))
705 seen = -1; /* no more inbody headers */
714 fprintf(stderr, "No patch found\n");
719 static const char mailinfo_usage[] =
720 "git-mailinfo [-k] [-u] msg patch <mail >info";
722 int main(int argc, char **argv)
724 /* NEEDSWORK: might want to do the optional .git/ directory
727 git_config(git_default_config);
729 while (1 < argc && argv[1][0] == '-') {
730 if (!strcmp(argv[1], "-k"))
732 else if (!strcmp(argv[1], "-u"))
733 metainfo_charset = git_commit_encoding;
734 else if (!strncmp(argv[1], "-u=", 3))
735 metainfo_charset = argv[1] + 3;
737 usage(mailinfo_usage);
742 usage(mailinfo_usage);
743 cmitmsg = fopen(argv[1], "w");
748 patchfile = fopen(argv[2], "w");
754 int len = read_one_header_line(line, sizeof(line), stdin);
756 if (multipart_boundary[0])
757 handle_multipart_body();
762 check_header_line(line, len);