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_BOGUS_UNIX_FROM 010
244 #define SEEN_PREFIX 020
246 /* First lines of body can have From:, Date:, and Subject: */
247 static void handle_inbody_header(int *seen, char *line)
249 if (!memcmp(">From", line, 5) && isspace(line[5])) {
250 if (!(*seen & SEEN_BOGUS_UNIX_FROM)) {
251 *seen |= SEEN_BOGUS_UNIX_FROM;
255 if (!memcmp("From:", line, 5) && isspace(line[5])) {
256 if (!(*seen & SEEN_FROM) && handle_from(line+6)) {
261 if (!memcmp("Date:", line, 5) && isspace(line[5])) {
262 if (!(*seen & SEEN_DATE)) {
268 if (!memcmp("Subject:", line, 8) && isspace(line[8])) {
269 if (!(*seen & SEEN_SUBJECT)) {
270 handle_subject(line+9);
271 *seen |= SEEN_SUBJECT;
275 if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) {
276 if (!(*seen & SEEN_SUBJECT)) {
277 handle_subject(line);
278 *seen |= SEEN_SUBJECT;
282 *seen |= SEEN_PREFIX;
285 static char *cleanup_subject(char *subject)
294 if (!memcmp("e:", subject+1, 2)) {
299 case ' ': case '\t': case ':':
304 p = strchr(subject, ']');
310 remove = p - subject;
311 if (remove <= len *2) {
321 static void cleanup_space(char *buf)
324 while ((c = *buf) != 0) {
330 int len = strlen(buf);
331 memmove(buf, buf+1, len);
338 static void decode_header_bq(char *it);
339 typedef int (*header_fn_t)(char *);
346 static void check_header(char *line, struct header_def *header)
350 if (header[0].namelen <= 0) {
351 for (i = 0; header[i].name; i++)
352 header[i].namelen = strlen(header[i].name);
354 for (i = 0; header[i].name; i++) {
355 int len = header[i].namelen;
356 if (!strncasecmp(line, header[i].name, len) &&
357 line[len] == ':' && isspace(line[len + 1])) {
358 /* Unwrap inline B and Q encoding, and optionally
359 * normalize the meta information to utf8.
361 decode_header_bq(line + len + 2);
362 header[i].func(line + len + 2);
368 static void check_subheader_line(char *line)
370 static struct header_def header[] = {
371 { "Content-Type", handle_subcontent_type },
372 { "Content-Transfer-Encoding",
373 handle_content_transfer_encoding },
376 check_header(line, header);
378 static void check_header_line(char *line)
380 static struct header_def header[] = {
381 { "From", handle_from },
382 { "Date", handle_date },
383 { "Subject", handle_subject },
384 { "Content-Type", handle_content_type },
385 { "Content-Transfer-Encoding",
386 handle_content_transfer_encoding },
389 check_header(line, header);
392 static int is_rfc2822_header(char *line)
395 * The section that defines the loosest possible
396 * field name is "3.6.8 Optional fields".
398 * optional-field = field-name ":" unstructured CRLF
399 * field-name = 1*ftext
400 * ftext = %d33-57 / %59-126
404 while ((ch = *cp++)) {
407 if ((33 <= ch && ch <= 57) ||
408 (59 <= ch && ch <= 126))
415 static int read_one_header_line(char *line, int sz, FILE *in)
420 if (fgets(line + ofs, sz - ofs, in) == NULL)
422 len = eatspace(line + ofs);
425 if (!is_rfc2822_header(line)) {
426 /* Re-add the newline */
427 line[ofs + len] = '\n';
428 line[ofs + len + 1] = '\0';
432 /* Yuck, 2822 header "folding" */
433 peek = fgetc(in); ungetc(peek, in);
434 if (peek != ' ' && peek != '\t')
437 /* Count mbox From headers as headers */
438 if (!ofs && !memcmp(line, "From ", 5))
443 static unsigned hexval(int c)
445 if (c >= '0' && c <= '9')
447 if (c >= 'a' && c <= 'f')
449 if (c >= 'A' && c <= 'F')
454 static int decode_q_segment(char *in, char *ot, char *ep, int rfc2047)
457 while ((c = *in++) != 0 && (in <= ep)) {
461 break; /* drop trailing newline */
462 *ot++ = ((hexval(d) << 4) | hexval(*in++));
465 if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
473 static int decode_b_segment(char *in, char *ot, char *ep)
475 /* Decode in..ep, possibly in-place to ot */
476 int c, pos = 0, acc = 0;
478 while ((c = *in++) != 0 && (in <= ep)) {
483 else if ('A' <= c && c <= 'Z')
485 else if ('a' <= c && c <= 'z')
487 else if ('0' <= c && c <= '9')
490 /* padding is almost like (c == 0), except we do
491 * not output NUL resulting only from it;
492 * for now we just trust the data.
497 continue; /* garbage */
503 *ot++ = (acc | (c >> 4));
507 *ot++ = (acc | (c >> 2));
520 static void convert_to_utf8(char *line, char *charset)
524 size_t insize, outsize, nrc;
525 char outbuf[4096]; /* cheat */
526 static char latin_one[] = "latin1";
527 char *input_charset = *charset ? charset : latin_one;
528 iconv_t conv = iconv_open(metainfo_charset, input_charset);
530 if (conv == (iconv_t) -1) {
531 static int warned_latin1_once = 0;
532 if (input_charset != latin_one) {
533 fprintf(stderr, "cannot convert from %s to %s\n",
534 input_charset, metainfo_charset);
537 else if (!warned_latin1_once) {
538 warned_latin1_once = 1;
539 fprintf(stderr, "tried to convert from %s to %s, "
540 "but your iconv does not work with it.\n",
541 input_charset, metainfo_charset);
548 outsize = sizeof(outbuf);
549 nrc = iconv(conv, &in, &insize, &out, &outsize);
551 if (nrc == (size_t) -1)
554 strcpy(line, outbuf);
558 static void decode_header_bq(char *it)
560 char *in, *out, *ep, *cp, *sp;
565 while ((ep = strstr(in, "=?")) != NULL) {
567 char charset_q[256], piecebuf[256];
575 * ep : "=?iso-2022-jp?B?GyR...?= foo"
576 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
579 cp = strchr(ep, '?');
581 return; /* no munging */
582 for (sp = ep; sp < cp; sp++)
583 charset_q[sp - ep] = tolower(*sp);
584 charset_q[cp - ep] = 0;
586 if (!encoding || cp[2] != '?')
587 return; /* no munging */
588 ep = strstr(cp + 3, "?=");
590 return; /* no munging */
591 switch (tolower(encoding)) {
593 return; /* no munging */
595 sz = decode_b_segment(cp + 3, piecebuf, ep);
598 sz = decode_q_segment(cp + 3, piecebuf, ep, 1);
603 if (metainfo_charset)
604 convert_to_utf8(piecebuf, charset_q);
605 strcpy(out, piecebuf);
613 static void decode_transfer_encoding(char *line)
617 switch (transfer_encoding) {
619 ep = line + strlen(line);
620 decode_q_segment(line, line, ep, 0);
623 ep = line + strlen(line);
624 decode_b_segment(line, line, ep);
631 static void handle_info(void)
635 sub = cleanup_subject(subject);
638 cleanup_space(email);
641 printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
642 name, email, sub, date);
645 /* We are inside message body and have read line[] already.
646 * Spit out the commit log.
648 static int handle_commit_msg(int *seen)
653 if (!memcmp("diff -", line, 6) ||
654 !memcmp("---", line, 3) ||
655 !memcmp("Index: ", line, 7))
657 if ((multipart_boundary[0] && is_multipart_boundary(line))) {
658 /* We come here when the first part had only
659 * the commit message without any patch. We
660 * pretend we have not seen this line yet, and
661 * go back to the loop.
666 /* Unwrap transfer encoding and optionally
667 * normalize the log message to UTF-8.
669 decode_transfer_encoding(line);
670 if (metainfo_charset)
671 convert_to_utf8(line, charset);
673 handle_inbody_header(seen, line);
674 if (!(*seen & SEEN_PREFIX))
677 fputs(line, cmitmsg);
678 } while (fgets(line, sizeof(line), stdin) != NULL);
684 /* We have done the commit message and have the first
685 * line of the patch in line[].
687 static void handle_patch(void)
690 if (multipart_boundary[0] && is_multipart_boundary(line))
692 /* Only unwrap transfer encoding but otherwise do not
693 * do anything. We do *NOT* want UTF-8 conversion
694 * here; we are dealing with the user payload.
696 decode_transfer_encoding(line);
697 fputs(line, patchfile);
699 } while (fgets(line, sizeof(line), stdin) != NULL);
702 /* multipart boundary and transfer encoding are set up for us, and we
703 * are at the end of the sub header. do equivalent of handle_body up
704 * to the next boundary without closing patchfile --- we will expect
705 * that the first part to contain commit message and a patch, and
706 * handle other parts as pure patches.
708 static int handle_multipart_one_part(int *seen)
712 while (fgets(line, sizeof(line), stdin) != NULL) {
715 if (is_multipart_boundary(line))
717 if (handle_commit_msg(seen))
727 static void handle_multipart_body(void)
732 /* Skip up to the first boundary */
733 while (fgets(line, sizeof(line), stdin) != NULL)
734 if (is_multipart_boundary(line)) {
740 /* We are on boundary line. Start slurping the subhead. */
742 int hdr = read_one_header_line(line, sizeof(line), stdin);
744 if (handle_multipart_one_part(&seen) < 0)
746 /* Reset per part headers */
747 transfer_encoding = TE_DONTCARE;
751 check_subheader_line(line);
755 fprintf(stderr, "No patch found\n");
760 /* Non multipart message */
761 static void handle_body(void)
765 if (line[0] || fgets(line, sizeof(line), stdin) != NULL) {
766 handle_commit_msg(&seen);
771 fprintf(stderr, "No patch found\n");
776 static const char mailinfo_usage[] =
777 "git-mailinfo [-k] [-u | --encoding=<encoding>] msg patch <mail >info";
779 int main(int argc, char **argv)
781 /* NEEDSWORK: might want to do the optional .git/ directory
784 git_config(git_default_config);
786 while (1 < argc && argv[1][0] == '-') {
787 if (!strcmp(argv[1], "-k"))
789 else if (!strcmp(argv[1], "-u"))
790 metainfo_charset = git_commit_encoding;
791 else if (!strncmp(argv[1], "--encoding=", 11))
792 metainfo_charset = argv[1] + 11;
794 usage(mailinfo_usage);
799 usage(mailinfo_usage);
800 cmitmsg = fopen(argv[1], "w");
805 patchfile = fopen(argv[2], "w");
811 int hdr = read_one_header_line(line, sizeof(line), stdin);
813 if (multipart_boundary[0])
814 handle_multipart_body();
820 check_header_line(line);