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: or empty */
247 static void handle_inbody_header(int *seen, char *line)
249 if (*seen & SEEN_PREFIX)
251 if (isspace(*line)) {
253 for (cp = line + 1; *cp; cp++) {
260 if (!memcmp(">From", line, 5) && isspace(line[5])) {
261 if (!(*seen & SEEN_BOGUS_UNIX_FROM)) {
262 *seen |= SEEN_BOGUS_UNIX_FROM;
266 if (!memcmp("From:", line, 5) && isspace(line[5])) {
267 if (!(*seen & SEEN_FROM) && handle_from(line+6)) {
272 if (!memcmp("Date:", line, 5) && isspace(line[5])) {
273 if (!(*seen & SEEN_DATE)) {
279 if (!memcmp("Subject:", line, 8) && isspace(line[8])) {
280 if (!(*seen & SEEN_SUBJECT)) {
281 handle_subject(line+9);
282 *seen |= SEEN_SUBJECT;
286 if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) {
287 if (!(*seen & SEEN_SUBJECT)) {
288 handle_subject(line);
289 *seen |= SEEN_SUBJECT;
293 *seen |= SEEN_PREFIX;
296 static char *cleanup_subject(char *subject)
305 if (!memcmp("e:", subject+1, 2)) {
310 case ' ': case '\t': case ':':
315 p = strchr(subject, ']');
321 remove = p - subject;
322 if (remove <= len *2) {
333 static void cleanup_space(char *buf)
336 while ((c = *buf) != 0) {
342 int len = strlen(buf);
343 memmove(buf, buf+1, len);
350 static void decode_header_bq(char *it);
351 typedef int (*header_fn_t)(char *);
358 static void check_header(char *line, struct header_def *header)
362 if (header[0].namelen <= 0) {
363 for (i = 0; header[i].name; i++)
364 header[i].namelen = strlen(header[i].name);
366 for (i = 0; header[i].name; i++) {
367 int len = header[i].namelen;
368 if (!strncasecmp(line, header[i].name, len) &&
369 line[len] == ':' && isspace(line[len + 1])) {
370 /* Unwrap inline B and Q encoding, and optionally
371 * normalize the meta information to utf8.
373 decode_header_bq(line + len + 2);
374 header[i].func(line + len + 2);
380 static void check_subheader_line(char *line)
382 static struct header_def header[] = {
383 { "Content-Type", handle_subcontent_type },
384 { "Content-Transfer-Encoding",
385 handle_content_transfer_encoding },
388 check_header(line, header);
390 static void check_header_line(char *line)
392 static struct header_def header[] = {
393 { "From", handle_from },
394 { "Date", handle_date },
395 { "Subject", handle_subject },
396 { "Content-Type", handle_content_type },
397 { "Content-Transfer-Encoding",
398 handle_content_transfer_encoding },
401 check_header(line, header);
404 static int is_rfc2822_header(char *line)
407 * The section that defines the loosest possible
408 * field name is "3.6.8 Optional fields".
410 * optional-field = field-name ":" unstructured CRLF
411 * field-name = 1*ftext
412 * ftext = %d33-57 / %59-126
416 while ((ch = *cp++)) {
419 if ((33 <= ch && ch <= 57) ||
420 (59 <= ch && ch <= 126))
427 static int read_one_header_line(char *line, int sz, FILE *in)
432 if (fgets(line + ofs, sz - ofs, in) == NULL)
434 len = eatspace(line + ofs);
435 if ((len == 0) || !is_rfc2822_header(line)) {
436 /* Re-add the newline */
437 line[ofs + len] = '\n';
438 line[ofs + len + 1] = '\0';
442 /* Yuck, 2822 header "folding" */
443 peek = fgetc(in); ungetc(peek, in);
444 if (peek != ' ' && peek != '\t')
447 /* Count mbox From headers as headers */
448 if (!ofs && !memcmp(line, "From ", 5))
453 static unsigned hexval(int c)
455 if (c >= '0' && c <= '9')
457 if (c >= 'a' && c <= 'f')
459 if (c >= 'A' && c <= 'F')
464 static int decode_q_segment(char *in, char *ot, char *ep, int rfc2047)
467 while ((c = *in++) != 0 && (in <= ep)) {
471 break; /* drop trailing newline */
472 *ot++ = ((hexval(d) << 4) | hexval(*in++));
475 if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
483 static int decode_b_segment(char *in, char *ot, char *ep)
485 /* Decode in..ep, possibly in-place to ot */
486 int c, pos = 0, acc = 0;
488 while ((c = *in++) != 0 && (in <= ep)) {
493 else if ('A' <= c && c <= 'Z')
495 else if ('a' <= c && c <= 'z')
497 else if ('0' <= c && c <= '9')
500 /* padding is almost like (c == 0), except we do
501 * not output NUL resulting only from it;
502 * for now we just trust the data.
507 continue; /* garbage */
513 *ot++ = (acc | (c >> 4));
517 *ot++ = (acc | (c >> 2));
530 static void convert_to_utf8(char *line, char *charset)
534 size_t insize, outsize, nrc;
535 char outbuf[4096]; /* cheat */
536 static char latin_one[] = "latin1";
537 char *input_charset = *charset ? charset : latin_one;
538 iconv_t conv = iconv_open(metainfo_charset, input_charset);
540 if (conv == (iconv_t) -1) {
541 static int warned_latin1_once = 0;
542 if (input_charset != latin_one) {
543 fprintf(stderr, "cannot convert from %s to %s\n",
544 input_charset, metainfo_charset);
547 else if (!warned_latin1_once) {
548 warned_latin1_once = 1;
549 fprintf(stderr, "tried to convert from %s to %s, "
550 "but your iconv does not work with it.\n",
551 input_charset, metainfo_charset);
558 outsize = sizeof(outbuf);
559 nrc = iconv(conv, &in, &insize, &out, &outsize);
561 if (nrc == (size_t) -1)
564 strcpy(line, outbuf);
568 static void decode_header_bq(char *it)
570 char *in, *out, *ep, *cp, *sp;
575 while ((ep = strstr(in, "=?")) != NULL) {
577 char charset_q[256], piecebuf[256];
585 * ep : "=?iso-2022-jp?B?GyR...?= foo"
586 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
589 cp = strchr(ep, '?');
591 return; /* no munging */
592 for (sp = ep; sp < cp; sp++)
593 charset_q[sp - ep] = tolower(*sp);
594 charset_q[cp - ep] = 0;
596 if (!encoding || cp[2] != '?')
597 return; /* no munging */
598 ep = strstr(cp + 3, "?=");
600 return; /* no munging */
601 switch (tolower(encoding)) {
603 return; /* no munging */
605 sz = decode_b_segment(cp + 3, piecebuf, ep);
608 sz = decode_q_segment(cp + 3, piecebuf, ep, 1);
613 if (metainfo_charset)
614 convert_to_utf8(piecebuf, charset_q);
615 strcpy(out, piecebuf);
623 static void decode_transfer_encoding(char *line)
627 switch (transfer_encoding) {
629 ep = line + strlen(line);
630 decode_q_segment(line, line, ep, 0);
633 ep = line + strlen(line);
634 decode_b_segment(line, line, ep);
641 static void handle_info(void)
645 sub = cleanup_subject(subject);
648 cleanup_space(email);
651 printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
652 name, email, sub, date);
655 /* We are inside message body and have read line[] already.
656 * Spit out the commit log.
658 static int handle_commit_msg(int *seen)
663 if (!memcmp("diff -", line, 6) ||
664 !memcmp("---", line, 3) ||
665 !memcmp("Index: ", line, 7))
667 if ((multipart_boundary[0] && is_multipart_boundary(line))) {
668 /* We come here when the first part had only
669 * the commit message without any patch. We
670 * pretend we have not seen this line yet, and
671 * go back to the loop.
676 /* Unwrap transfer encoding and optionally
677 * normalize the log message to UTF-8.
679 decode_transfer_encoding(line);
680 if (metainfo_charset)
681 convert_to_utf8(line, charset);
683 handle_inbody_header(seen, line);
684 if (!(*seen & SEEN_PREFIX))
687 fputs(line, cmitmsg);
688 } while (fgets(line, sizeof(line), stdin) != NULL);
694 /* We have done the commit message and have the first
695 * line of the patch in line[].
697 static void handle_patch(void)
700 if (multipart_boundary[0] && is_multipart_boundary(line))
702 /* Only unwrap transfer encoding but otherwise do not
703 * do anything. We do *NOT* want UTF-8 conversion
704 * here; we are dealing with the user payload.
706 decode_transfer_encoding(line);
707 fputs(line, patchfile);
709 } while (fgets(line, sizeof(line), stdin) != NULL);
712 /* multipart boundary and transfer encoding are set up for us, and we
713 * are at the end of the sub header. do equivalent of handle_body up
714 * to the next boundary without closing patchfile --- we will expect
715 * that the first part to contain commit message and a patch, and
716 * handle other parts as pure patches.
718 static int handle_multipart_one_part(int *seen)
722 while (fgets(line, sizeof(line), stdin) != NULL) {
725 if (is_multipart_boundary(line))
727 if (handle_commit_msg(seen))
737 static void handle_multipart_body(void)
742 /* Skip up to the first boundary */
743 while (fgets(line, sizeof(line), stdin) != NULL)
744 if (is_multipart_boundary(line)) {
750 /* We are on boundary line. Start slurping the subhead. */
752 int hdr = read_one_header_line(line, sizeof(line), stdin);
754 if (handle_multipart_one_part(&seen) < 0)
756 /* Reset per part headers */
757 transfer_encoding = TE_DONTCARE;
761 check_subheader_line(line);
765 fprintf(stderr, "No patch found\n");
770 /* Non multipart message */
771 static void handle_body(void)
775 handle_commit_msg(&seen);
779 fprintf(stderr, "No patch found\n");
784 static const char mailinfo_usage[] =
785 "git-mailinfo [-k] [-u | --encoding=<encoding>] msg patch <mail >info";
787 int main(int argc, char **argv)
789 /* NEEDSWORK: might want to do the optional .git/ directory
792 git_config(git_default_config);
794 while (1 < argc && argv[1][0] == '-') {
795 if (!strcmp(argv[1], "-k"))
797 else if (!strcmp(argv[1], "-u"))
798 metainfo_charset = git_commit_encoding;
799 else if (!strncmp(argv[1], "--encoding=", 11))
800 metainfo_charset = argv[1] + 11;
802 usage(mailinfo_usage);
807 usage(mailinfo_usage);
808 cmitmsg = fopen(argv[1], "w");
813 patchfile = fopen(argv[2], "w");
819 int hdr = read_one_header_line(line, sizeof(line), stdin);
821 if (multipart_boundary[0])
822 handle_multipart_body();
828 check_header_line(line);