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 *in_line)
81 strcpy(line, in_line);
82 at = strchr(line, '@');
84 return bogus_from(line);
87 * If we already have one email, don't take any confusing lines
89 if (*email && strchr(at+1, '@'))
92 /* Pick up the string around '@', possibly delimited with <>
93 * pair; that is the email part. White them out while copying.
107 unsigned char c = *at;
108 if (!c || c == '>' || isspace(c)) {
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.
123 at = line + strlen(line);
125 unsigned char c = *--at;
127 at[(c == ')') ? 0 : 1] = 0;
134 unsigned char c = *at;
135 if (!c || !isspace(c)) {
142 at = sanity_check(at, email);
147 static int handle_date(char *line)
153 static int handle_subject(char *line)
155 strcpy(subject, line);
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.
165 static int slurp_attr(const char *line, const char *name, char *attr)
167 char *ends, *ap = strcasestr(line, name);
181 sz = strcspn(ap, ends);
182 memcpy(attr, ap, sz);
187 static int handle_subcontent_type(char *line)
189 /* We do not want to mess with boundary. Note that we do not
190 * handle nested multipart.
192 if (strcasestr(line, "boundary=")) {
193 fprintf(stderr, "Not handling nested multipart message.\n");
196 slurp_attr(line, "charset=", charset);
199 for (i = 0; (c = charset[i]) != 0; i++)
200 charset[i] = tolower(c);
205 static int handle_content_type(char *line)
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);
212 slurp_attr(line, "charset=", charset);
216 static int handle_content_transfer_encoding(char *line)
218 if (strcasestr(line, "base64"))
219 transfer_encoding = TE_BASE64;
220 else if (strcasestr(line, "quoted-printable"))
221 transfer_encoding = TE_QP;
223 transfer_encoding = TE_DONTCARE;
227 static int is_multipart_boundary(const char *line)
229 return (!memcmp(line, multipart_boundary, multipart_boundary_len));
232 static int eatspace(char *line)
234 int len = strlen(line);
235 while (len > 0 && isspace(line[len-1]))
242 #define SEEN_SUBJECT 04
243 #define SEEN_PREFIX 0x08
245 /* First lines of body can have From:, Date:, and Subject: */
246 static void handle_inbody_header(int *seen, char *line)
248 if (!memcmp("From:", line, 5) && isspace(line[5])) {
249 if (!(*seen & SEEN_FROM) && handle_from(line+6)) {
254 if (!memcmp("Date:", line, 5) && isspace(line[5])) {
255 if (!(*seen & SEEN_DATE)) {
261 if (!memcmp("Subject:", line, 8) && isspace(line[8])) {
262 if (!(*seen & SEEN_SUBJECT)) {
263 handle_subject(line+9);
264 *seen |= SEEN_SUBJECT;
268 if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) {
269 if (!(*seen & SEEN_SUBJECT)) {
270 handle_subject(line);
271 *seen |= SEEN_SUBJECT;
275 *seen |= SEEN_PREFIX;
278 static char *cleanup_subject(char *subject)
287 if (!memcmp("e:", subject+1, 2)) {
292 case ' ': case '\t': case ':':
297 p = strchr(subject, ']');
303 remove = p - subject;
304 if (remove <= len *2) {
314 static void cleanup_space(char *buf)
317 while ((c = *buf) != 0) {
323 int len = strlen(buf);
324 memmove(buf, buf+1, len);
331 static void decode_header_bq(char *it);
332 typedef int (*header_fn_t)(char *);
339 static void check_header(char *line, struct header_def *header)
343 if (header[0].namelen <= 0) {
344 for (i = 0; header[i].name; i++)
345 header[i].namelen = strlen(header[i].name);
347 for (i = 0; header[i].name; i++) {
348 int len = header[i].namelen;
349 if (!strncasecmp(line, header[i].name, len) &&
350 line[len] == ':' && isspace(line[len + 1])) {
351 /* Unwrap inline B and Q encoding, and optionally
352 * normalize the meta information to utf8.
354 decode_header_bq(line + len + 2);
355 header[i].func(line + len + 2);
361 static void check_subheader_line(char *line)
363 static struct header_def header[] = {
364 { "Content-Type", handle_subcontent_type },
365 { "Content-Transfer-Encoding",
366 handle_content_transfer_encoding },
369 check_header(line, header);
371 static void check_header_line(char *line)
373 static struct header_def header[] = {
374 { "From", handle_from },
375 { "Date", handle_date },
376 { "Subject", handle_subject },
377 { "Content-Type", handle_content_type },
378 { "Content-Transfer-Encoding",
379 handle_content_transfer_encoding },
382 check_header(line, header);
385 static int read_one_header_line(char *line, int sz, FILE *in)
391 if (fgets(line + ofs, sz - ofs, in) == NULL)
393 len = eatspace(line + ofs);
396 colon = strchr(line, ':');
397 if (!colon || !isspace(colon[1])) {
398 /* Re-add the newline */
399 line[ofs + len] = '\n';
400 line[ofs + len + 1] = '\0';
404 /* Yuck, 2822 header "folding" */
405 peek = fgetc(in); ungetc(peek, in);
406 if (peek != ' ' && peek != '\t')
409 /* Count mbox From headers as headers */
410 if (!ofs && !memcmp(line, "From ", 5))
415 static unsigned hexval(int c)
417 if (c >= '0' && c <= '9')
419 if (c >= 'a' && c <= 'f')
421 if (c >= 'A' && c <= 'F')
426 static int decode_q_segment(char *in, char *ot, char *ep, int rfc2047)
429 while ((c = *in++) != 0 && (in <= ep)) {
433 break; /* drop trailing newline */
434 *ot++ = ((hexval(d) << 4) | hexval(*in++));
437 if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
445 static int decode_b_segment(char *in, char *ot, char *ep)
447 /* Decode in..ep, possibly in-place to ot */
448 int c, pos = 0, acc = 0;
450 while ((c = *in++) != 0 && (in <= ep)) {
455 else if ('A' <= c && c <= 'Z')
457 else if ('a' <= c && c <= 'z')
459 else if ('0' <= c && c <= '9')
462 /* padding is almost like (c == 0), except we do
463 * not output NUL resulting only from it;
464 * for now we just trust the data.
469 continue; /* garbage */
475 *ot++ = (acc | (c >> 4));
479 *ot++ = (acc | (c >> 2));
492 static void convert_to_utf8(char *line, char *charset)
496 size_t insize, outsize, nrc;
497 char outbuf[4096]; /* cheat */
498 static char latin_one[] = "latin1";
499 char *input_charset = *charset ? charset : latin_one;
500 iconv_t conv = iconv_open(metainfo_charset, input_charset);
502 if (conv == (iconv_t) -1) {
503 static int warned_latin1_once = 0;
504 if (input_charset != latin_one) {
505 fprintf(stderr, "cannot convert from %s to %s\n",
506 input_charset, metainfo_charset);
509 else if (!warned_latin1_once) {
510 warned_latin1_once = 1;
511 fprintf(stderr, "tried to convert from %s to %s, "
512 "but your iconv does not work with it.\n",
513 input_charset, metainfo_charset);
520 outsize = sizeof(outbuf);
521 nrc = iconv(conv, &in, &insize, &out, &outsize);
523 if (nrc == (size_t) -1)
526 strcpy(line, outbuf);
530 static void decode_header_bq(char *it)
532 char *in, *out, *ep, *cp, *sp;
537 while ((ep = strstr(in, "=?")) != NULL) {
539 char charset_q[256], piecebuf[256];
547 * ep : "=?iso-2022-jp?B?GyR...?= foo"
548 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
551 cp = strchr(ep, '?');
553 return; /* no munging */
554 for (sp = ep; sp < cp; sp++)
555 charset_q[sp - ep] = tolower(*sp);
556 charset_q[cp - ep] = 0;
558 if (!encoding || cp[2] != '?')
559 return; /* no munging */
560 ep = strstr(cp + 3, "?=");
562 return; /* no munging */
563 switch (tolower(encoding)) {
565 return; /* no munging */
567 sz = decode_b_segment(cp + 3, piecebuf, ep);
570 sz = decode_q_segment(cp + 3, piecebuf, ep, 1);
575 if (metainfo_charset)
576 convert_to_utf8(piecebuf, charset_q);
577 strcpy(out, piecebuf);
585 static void decode_transfer_encoding(char *line)
589 switch (transfer_encoding) {
591 ep = line + strlen(line);
592 decode_q_segment(line, line, ep, 0);
595 ep = line + strlen(line);
596 decode_b_segment(line, line, ep);
603 static void handle_info(void)
607 sub = cleanup_subject(subject);
610 cleanup_space(email);
613 printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
614 name, email, sub, date);
617 /* We are inside message body and have read line[] already.
618 * Spit out the commit log.
620 static int handle_commit_msg(int *seen)
625 if (!memcmp("diff -", line, 6) ||
626 !memcmp("---", line, 3) ||
627 !memcmp("Index: ", line, 7))
629 if ((multipart_boundary[0] && is_multipart_boundary(line))) {
630 /* We come here when the first part had only
631 * the commit message without any patch. We
632 * pretend we have not seen this line yet, and
633 * go back to the loop.
638 /* Unwrap transfer encoding and optionally
639 * normalize the log message to UTF-8.
641 decode_transfer_encoding(line);
642 if (metainfo_charset)
643 convert_to_utf8(line, charset);
645 handle_inbody_header(seen, line);
646 if (!(*seen & SEEN_PREFIX))
649 fputs(line, cmitmsg);
650 } while (fgets(line, sizeof(line), stdin) != NULL);
656 /* We have done the commit message and have the first
657 * line of the patch in line[].
659 static void handle_patch(void)
662 if (multipart_boundary[0] && is_multipart_boundary(line))
664 /* Only unwrap transfer encoding but otherwise do not
665 * do anything. We do *NOT* want UTF-8 conversion
666 * here; we are dealing with the user payload.
668 decode_transfer_encoding(line);
669 fputs(line, patchfile);
671 } while (fgets(line, sizeof(line), stdin) != NULL);
674 /* multipart boundary and transfer encoding are set up for us, and we
675 * are at the end of the sub header. do equivalent of handle_body up
676 * to the next boundary without closing patchfile --- we will expect
677 * that the first part to contain commit message and a patch, and
678 * handle other parts as pure patches.
680 static int handle_multipart_one_part(int *seen)
684 while (fgets(line, sizeof(line), stdin) != NULL) {
687 if (is_multipart_boundary(line))
689 if (handle_commit_msg(seen))
699 static void handle_multipart_body(void)
704 /* Skip up to the first boundary */
705 while (fgets(line, sizeof(line), stdin) != NULL)
706 if (is_multipart_boundary(line)) {
712 /* We are on boundary line. Start slurping the subhead. */
714 int hdr = read_one_header_line(line, sizeof(line), stdin);
716 if (handle_multipart_one_part(&seen) < 0)
718 /* Reset per part headers */
719 transfer_encoding = TE_DONTCARE;
723 check_subheader_line(line);
727 fprintf(stderr, "No patch found\n");
732 /* Non multipart message */
733 static void handle_body(void)
737 if (line[0] || fgets(line, sizeof(line), stdin) != NULL) {
738 handle_commit_msg(&seen);
743 fprintf(stderr, "No patch found\n");
748 static const char mailinfo_usage[] =
749 "git-mailinfo [-k] [-u | --encoding=<encoding>] msg patch <mail >info";
751 int main(int argc, char **argv)
753 /* NEEDSWORK: might want to do the optional .git/ directory
756 git_config(git_default_config);
758 while (1 < argc && argv[1][0] == '-') {
759 if (!strcmp(argv[1], "-k"))
761 else if (!strcmp(argv[1], "-u"))
762 metainfo_charset = git_commit_encoding;
763 else if (!strncmp(argv[1], "--encoding=", 11))
764 metainfo_charset = argv[1] + 11;
766 usage(mailinfo_usage);
771 usage(mailinfo_usage);
772 cmitmsg = fopen(argv[1], "w");
777 patchfile = fopen(argv[2], "w");
783 int hdr = read_one_header_line(line, sizeof(line), stdin);
785 if (multipart_boundary[0])
786 handle_multipart_body();
792 check_header_line(line);