2 * Another stupid program, this one parsing the headers of an
3 * email to figure out authorship and subject
13 #include "git-compat-util.h"
16 static FILE *cmitmsg, *patchfile;
18 static int keep_subject = 0;
19 static char *metainfo_charset = NULL;
20 static char line[1000];
21 static char date[1000];
22 static char name[1000];
23 static char email[1000];
24 static char subject[1000];
27 TE_DONTCARE, TE_QP, TE_BASE64,
29 static char charset[256];
31 static char multipart_boundary[1000];
32 static int multipart_boundary_len;
33 static int patch_lines = 0;
35 static char *sanity_check(char *name, char *email)
37 int len = strlen(name);
38 if (len < 3 || len > 60)
40 if (strchr(name, '@') || strchr(name, '<') || strchr(name, '>'))
45 static int bogus_from(char *line)
47 /* John Doe <johndoe> */
48 char *bra, *ket, *dst, *cp;
50 /* This is fallback, so do not bother if we already have an
56 bra = strchr(line, '<');
59 ket = strchr(bra, '>');
63 for (dst = email, cp = bra+1; cp < ket; )
66 for (cp = line; isspace(*cp); cp++)
68 for (bra--; isspace(*bra); bra--)
70 cp = sanity_check(cp, email);
75 static int handle_from(char *line)
77 char *at = strchr(line, '@');
81 return bogus_from(line);
84 * If we already have one email, don't take any confusing lines
86 if (*email && strchr(at+1, '@'))
89 /* Pick up the string around '@', possibly delimited with <>
90 * pair; that is the email part. White them out while copying.
104 unsigned char c = *at;
105 if (!c || c == '>' || isspace(c)) {
115 /* The remainder is name. It could be "John Doe <john.doe@xz>"
116 * or "john.doe@xz (John Doe)", but we have whited out the
117 * email part, so trim from both ends, possibly removing
118 * the () pair at the end.
120 at = line + strlen(line);
122 unsigned char c = *--at;
124 at[(c == ')') ? 0 : 1] = 0;
131 unsigned char c = *at;
132 if (!c || !isspace(c)) {
139 at = sanity_check(at, email);
144 static int handle_date(char *line)
150 static int handle_subject(char *line)
152 strcpy(subject, line);
156 /* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
157 * to have enough heuristics to grok MIME encoded patches often found
158 * on our mailing lists. For example, we do not even treat header lines
159 * case insensitively.
162 static int slurp_attr(const char *line, const char *name, char *attr)
164 char *ends, *ap = strcasestr(line, name);
178 sz = strcspn(ap, ends);
179 memcpy(attr, ap, sz);
184 static int handle_subcontent_type(char *line)
186 /* We do not want to mess with boundary. Note that we do not
187 * handle nested multipart.
189 if (strcasestr(line, "boundary=")) {
190 fprintf(stderr, "Not handling nested multipart message.\n");
193 slurp_attr(line, "charset=", charset);
196 for (i = 0; (c = charset[i]) != 0; i++)
197 charset[i] = tolower(c);
202 static int handle_content_type(char *line)
204 *multipart_boundary = 0;
205 if (slurp_attr(line, "boundary=", multipart_boundary + 2)) {
206 memcpy(multipart_boundary, "--", 2);
207 multipart_boundary_len = strlen(multipart_boundary);
209 slurp_attr(line, "charset=", charset);
213 static int handle_content_transfer_encoding(char *line)
215 if (strcasestr(line, "base64"))
216 transfer_encoding = TE_BASE64;
217 else if (strcasestr(line, "quoted-printable"))
218 transfer_encoding = TE_QP;
220 transfer_encoding = TE_DONTCARE;
224 static int is_multipart_boundary(const char *line)
226 return (!memcmp(line, multipart_boundary, multipart_boundary_len));
229 static int eatspace(char *line)
231 int len = strlen(line);
232 while (len > 0 && isspace(line[len-1]))
239 #define SEEN_SUBJECT 04
240 #define SEEN_PREFIX 0x08
242 /* First lines of body can have From:, Date:, and Subject: */
243 static void handle_inbody_header(int *seen, char *line)
245 if (*seen & SEEN_PREFIX)
247 if (!memcmp("From:", line, 5) && isspace(line[5])) {
248 if (!(*seen & SEEN_FROM) && handle_from(line+6)) {
253 if (!memcmp("Date:", line, 5) && isspace(line[5])) {
254 if (!(*seen & SEEN_DATE)) {
260 if (!memcmp("Subject:", line, 8) && isspace(line[8])) {
261 if (!(*seen & SEEN_SUBJECT)) {
262 handle_subject(line+9);
263 *seen |= SEEN_SUBJECT;
267 if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) {
268 if (!(*seen & SEEN_SUBJECT)) {
269 handle_subject(line);
270 *seen |= SEEN_SUBJECT;
274 *seen |= SEEN_PREFIX;
277 static char *cleanup_subject(char *subject)
286 if (!memcmp("e:", subject+1, 2)) {
291 case ' ': case '\t': case ':':
296 p = strchr(subject, ']');
302 remove = p - subject;
303 if (remove <= len *2) {
313 static void cleanup_space(char *buf)
316 while ((c = *buf) != 0) {
322 int len = strlen(buf);
323 memmove(buf, buf+1, len);
330 static void decode_header_bq(char *it);
331 typedef int (*header_fn_t)(char *);
338 static void check_header(char *line, struct header_def *header)
342 if (header[0].namelen <= 0) {
343 for (i = 0; header[i].name; i++)
344 header[i].namelen = strlen(header[i].name);
346 for (i = 0; header[i].name; i++) {
347 int len = header[i].namelen;
348 if (!strncasecmp(line, header[i].name, len) &&
349 line[len] == ':' && isspace(line[len + 1])) {
350 /* Unwrap inline B and Q encoding, and optionally
351 * normalize the meta information to utf8.
353 decode_header_bq(line + len + 2);
354 header[i].func(line + len + 2);
360 static void check_subheader_line(char *line)
362 static struct header_def header[] = {
363 { "Content-Type", handle_subcontent_type },
364 { "Content-Transfer-Encoding",
365 handle_content_transfer_encoding },
368 check_header(line, header);
370 static void check_header_line(char *line)
372 static struct header_def header[] = {
373 { "From", handle_from },
374 { "Date", handle_date },
375 { "Subject", handle_subject },
376 { "Content-Type", handle_content_type },
377 { "Content-Transfer-Encoding",
378 handle_content_transfer_encoding },
381 check_header(line, header);
384 static int read_one_header_line(char *line, int sz, FILE *in)
390 if (fgets(line + ofs, sz - ofs, in) == NULL)
392 len = eatspace(line + ofs);
395 colon = strchr(line, ':');
396 if (!colon || !isspace(colon[1])) {
397 /* Re-add the newline */
398 line[ofs + len] = '\n';
399 line[ofs + len + 1] = '\0';
403 /* Yuck, 2822 header "folding" */
404 peek = fgetc(in); ungetc(peek, in);
405 if (peek != ' ' && peek != '\t')
408 /* Count mbox From headers as headers */
409 if (!ofs && !memcmp(line, "From ", 5))
414 static unsigned hexval(int c)
416 if (c >= '0' && c <= '9')
418 if (c >= 'a' && c <= 'f')
420 if (c >= 'A' && c <= 'F')
425 static int decode_q_segment(char *in, char *ot, char *ep, int rfc2047)
428 while ((c = *in++) != 0 && (in <= ep)) {
432 break; /* drop trailing newline */
433 *ot++ = ((hexval(d) << 4) | hexval(*in++));
436 if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
444 static int decode_b_segment(char *in, char *ot, char *ep)
446 /* Decode in..ep, possibly in-place to ot */
447 int c, pos = 0, acc = 0;
449 while ((c = *in++) != 0 && (in <= ep)) {
454 else if ('A' <= c && c <= 'Z')
456 else if ('a' <= c && c <= 'z')
458 else if ('0' <= c && c <= '9')
461 /* padding is almost like (c == 0), except we do
462 * not output NUL resulting only from it;
463 * for now we just trust the data.
468 continue; /* garbage */
474 *ot++ = (acc | (c >> 4));
478 *ot++ = (acc | (c >> 2));
491 static void convert_to_utf8(char *line, char *charset)
495 size_t insize, outsize, nrc;
496 char outbuf[4096]; /* cheat */
497 static char latin_one[] = "latin1";
498 char *input_charset = *charset ? charset : latin_one;
499 iconv_t conv = iconv_open(metainfo_charset, input_charset);
501 if (conv == (iconv_t) -1) {
502 static int warned_latin1_once = 0;
503 if (input_charset != latin_one) {
504 fprintf(stderr, "cannot convert from %s to %s\n",
505 input_charset, metainfo_charset);
508 else if (!warned_latin1_once) {
509 warned_latin1_once = 1;
510 fprintf(stderr, "tried to convert from %s to %s, "
511 "but your iconv does not work with it.\n",
512 input_charset, metainfo_charset);
519 outsize = sizeof(outbuf);
520 nrc = iconv(conv, &in, &insize, &out, &outsize);
522 if (nrc == (size_t) -1)
525 strcpy(line, outbuf);
529 static void decode_header_bq(char *it)
531 char *in, *out, *ep, *cp, *sp;
536 while ((ep = strstr(in, "=?")) != NULL) {
538 char charset_q[256], piecebuf[256];
546 * ep : "=?iso-2022-jp?B?GyR...?= foo"
547 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
550 cp = strchr(ep, '?');
552 return; /* no munging */
553 for (sp = ep; sp < cp; sp++)
554 charset_q[sp - ep] = tolower(*sp);
555 charset_q[cp - ep] = 0;
557 if (!encoding || cp[2] != '?')
558 return; /* no munging */
559 ep = strstr(cp + 3, "?=");
561 return; /* no munging */
562 switch (tolower(encoding)) {
564 return; /* no munging */
566 sz = decode_b_segment(cp + 3, piecebuf, ep);
569 sz = decode_q_segment(cp + 3, piecebuf, ep, 1);
574 if (metainfo_charset)
575 convert_to_utf8(piecebuf, charset_q);
576 strcpy(out, piecebuf);
584 static void decode_transfer_encoding(char *line)
588 switch (transfer_encoding) {
590 ep = line + strlen(line);
591 decode_q_segment(line, line, ep, 0);
594 ep = line + strlen(line);
595 decode_b_segment(line, line, ep);
602 static void handle_info(void)
606 sub = cleanup_subject(subject);
609 cleanup_space(email);
612 printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
613 name, email, sub, date);
616 /* We are inside message body and have read line[] already.
617 * Spit out the commit log.
619 static int handle_commit_msg(int *seen)
624 if (!memcmp("diff -", line, 6) ||
625 !memcmp("---", line, 3) ||
626 !memcmp("Index: ", line, 7))
628 if ((multipart_boundary[0] && is_multipart_boundary(line))) {
629 /* We come here when the first part had only
630 * the commit message without any patch. We
631 * pretend we have not seen this line yet, and
632 * go back to the loop.
637 /* Unwrap transfer encoding and optionally
638 * normalize the log message to UTF-8.
640 decode_transfer_encoding(line);
641 if (metainfo_charset)
642 convert_to_utf8(line, charset);
644 handle_inbody_header(seen, line);
645 if (!(*seen & SEEN_PREFIX))
648 fputs(line, cmitmsg);
649 } while (fgets(line, sizeof(line), stdin) != NULL);
655 /* We have done the commit message and have the first
656 * line of the patch in line[].
658 static void handle_patch(void)
661 if (multipart_boundary[0] && is_multipart_boundary(line))
663 /* Only unwrap transfer encoding but otherwise do not
664 * do anything. We do *NOT* want UTF-8 conversion
665 * here; we are dealing with the user payload.
667 decode_transfer_encoding(line);
668 fputs(line, patchfile);
670 } while (fgets(line, sizeof(line), stdin) != NULL);
673 /* multipart boundary and transfer encoding are set up for us, and we
674 * are at the end of the sub header. do equivalent of handle_body up
675 * to the next boundary without closing patchfile --- we will expect
676 * that the first part to contain commit message and a patch, and
677 * handle other parts as pure patches.
679 static int handle_multipart_one_part(int *seen)
683 while (fgets(line, sizeof(line), stdin) != NULL) {
686 if (is_multipart_boundary(line))
688 if (handle_commit_msg(seen))
698 static void handle_multipart_body(void)
703 /* Skip up to the first boundary */
704 while (fgets(line, sizeof(line), stdin) != NULL)
705 if (is_multipart_boundary(line)) {
711 /* We are on boundary line. Start slurping the subhead. */
713 int hdr = read_one_header_line(line, sizeof(line), stdin);
715 if (handle_multipart_one_part(&seen) < 0)
717 /* Reset per part headers */
718 transfer_encoding = TE_DONTCARE;
722 check_subheader_line(line);
726 fprintf(stderr, "No patch found\n");
731 /* Non multipart message */
732 static void handle_body(void)
736 if (line[0] || fgets(line, sizeof(line), stdin) != NULL) {
737 handle_commit_msg(&seen);
742 fprintf(stderr, "No patch found\n");
747 static const char mailinfo_usage[] =
748 "git-mailinfo [-k] [-u | --encoding=<encoding>] msg patch <mail >info";
750 int main(int argc, char **argv)
752 /* NEEDSWORK: might want to do the optional .git/ directory
755 git_config(git_default_config);
757 while (1 < argc && argv[1][0] == '-') {
758 if (!strcmp(argv[1], "-k"))
760 else if (!strcmp(argv[1], "-u"))
761 metainfo_charset = git_commit_encoding;
762 else if (!strncmp(argv[1], "--encoding=", 11))
763 metainfo_charset = argv[1] + 11;
765 usage(mailinfo_usage);
770 usage(mailinfo_usage);
771 cmitmsg = fopen(argv[1], "w");
776 patchfile = fopen(argv[2], "w");
782 int hdr = read_one_header_line(line, sizeof(line), stdin);
784 if (multipart_boundary[0])
785 handle_multipart_body();
791 check_header_line(line);