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_BOGUS_UNIX_FROM 010
242 /* First lines of body can have From:, Date:, and Subject: */
243 static int handle_inbody_header(int *seen
, char *line
)
245 if (!memcmp(">From", line
, 5) && isspace(line
[5])) {
246 if (!(*seen
& SEEN_BOGUS_UNIX_FROM
)) {
247 *seen
|= SEEN_BOGUS_UNIX_FROM
;
251 if (!memcmp("From:", line
, 5) && isspace(line
[5])) {
252 if (!(*seen
& SEEN_FROM
) && handle_from(line
+6)) {
257 if (!memcmp("Date:", line
, 5) && isspace(line
[5])) {
258 if (!(*seen
& SEEN_DATE
)) {
264 if (!memcmp("Subject:", line
, 8) && isspace(line
[8])) {
265 if (!(*seen
& SEEN_SUBJECT
)) {
266 handle_subject(line
+9);
267 *seen
|= SEEN_SUBJECT
;
271 if (!memcmp("[PATCH]", line
, 7) && isspace(line
[7])) {
272 if (!(*seen
& SEEN_SUBJECT
)) {
273 handle_subject(line
);
274 *seen
|= SEEN_SUBJECT
;
281 static char *cleanup_subject(char *subject
)
290 if (!memcmp("e:", subject
+1, 2)) {
295 case ' ': case '\t': case ':':
300 p
= strchr(subject
, ']');
306 remove
= p
- subject
;
307 if (remove
<= len
*2) {
317 static void cleanup_space(char *buf
)
320 while ((c
= *buf
) != 0) {
326 int len
= strlen(buf
);
327 memmove(buf
, buf
+1, len
);
334 typedef int (*header_fn_t
)(char *);
341 static void check_header(char *line
, int len
, struct header_def
*header
)
345 if (header
[0].namelen
<= 0) {
346 for (i
= 0; header
[i
].name
; i
++)
347 header
[i
].namelen
= strlen(header
[i
].name
);
349 for (i
= 0; header
[i
].name
; i
++) {
350 int len
= header
[i
].namelen
;
351 if (!strncasecmp(line
, header
[i
].name
, len
) &&
352 line
[len
] == ':' && isspace(line
[len
+ 1])) {
353 header
[i
].func(line
+ len
+ 2);
359 static void check_subheader_line(char *line
, int len
)
361 static struct header_def header
[] = {
362 { "Content-Type", handle_subcontent_type
},
363 { "Content-Transfer-Encoding",
364 handle_content_transfer_encoding
},
367 check_header(line
, len
, header
);
369 static void check_header_line(char *line
, int len
)
371 static struct header_def header
[] = {
372 { "From", handle_from
},
373 { "Date", handle_date
},
374 { "Subject", handle_subject
},
375 { "Content-Type", handle_content_type
},
376 { "Content-Transfer-Encoding",
377 handle_content_transfer_encoding
},
380 check_header(line
, len
, header
);
383 static int read_one_header_line(char *line
, int sz
, FILE *in
)
388 if (fgets(line
+ ofs
, sz
- ofs
, in
) == NULL
)
390 len
= eatspace(line
+ ofs
);
393 peek
= fgetc(in
); ungetc(peek
, in
);
394 if (peek
== ' ' || peek
== '\t') {
395 /* Yuck, 2822 header "folding" */
404 static unsigned hexval(int c
)
406 if (c
>= '0' && c
<= '9')
408 if (c
>= 'a' && c
<= 'f')
410 if (c
>= 'A' && c
<= 'F')
415 static int decode_q_segment(char *in
, char *ot
, char *ep
, int rfc2047
)
418 while ((c
= *in
++) != 0 && (in
<= ep
)) {
422 break; /* drop trailing newline */
423 *ot
++ = ((hexval(d
) << 4) | hexval(*in
++));
426 if (rfc2047
&& c
== '_') /* rfc2047 4.2 (2) */
434 static int decode_b_segment(char *in
, char *ot
, char *ep
)
436 /* Decode in..ep, possibly in-place to ot */
437 int c
, pos
= 0, acc
= 0;
439 while ((c
= *in
++) != 0 && (in
<= ep
)) {
444 else if ('A' <= c
&& c
<= 'Z')
446 else if ('a' <= c
&& c
<= 'z')
448 else if ('0' <= c
&& c
<= '9')
451 /* padding is almost like (c == 0), except we do
452 * not output NUL resulting only from it;
453 * for now we just trust the data.
458 continue; /* garbage */
464 *ot
++ = (acc
| (c
>> 4));
468 *ot
++ = (acc
| (c
>> 2));
481 static void convert_to_utf8(char *line
, char *charset
)
485 size_t insize
, outsize
, nrc
;
486 char outbuf
[4096]; /* cheat */
487 static char latin_one
[] = "latin1";
488 char *input_charset
= *charset
? charset
: latin_one
;
489 iconv_t conv
= iconv_open(metainfo_charset
, input_charset
);
491 if (conv
== (iconv_t
) -1) {
492 static int warned_latin1_once
= 0;
493 if (input_charset
!= latin_one
) {
494 fprintf(stderr
, "cannot convert from %s to %s\n",
495 input_charset
, metainfo_charset
);
498 else if (!warned_latin1_once
) {
499 warned_latin1_once
= 1;
500 fprintf(stderr
, "tried to convert from %s to %s, "
501 "but your iconv does not work with it.\n",
502 input_charset
, metainfo_charset
);
509 outsize
= sizeof(outbuf
);
510 nrc
= iconv(conv
, &in
, &insize
, &out
, &outsize
);
512 if (nrc
== (size_t) -1)
515 strcpy(line
, outbuf
);
519 static void decode_header_bq(char *it
)
521 char *in
, *out
, *ep
, *cp
, *sp
;
526 while ((ep
= strstr(in
, "=?")) != NULL
) {
528 char charset_q
[256], piecebuf
[256];
536 * ep : "=?iso-2022-jp?B?GyR...?= foo"
537 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
540 cp
= strchr(ep
, '?');
542 return; /* no munging */
543 for (sp
= ep
; sp
< cp
; sp
++)
544 charset_q
[sp
- ep
] = tolower(*sp
);
545 charset_q
[cp
- ep
] = 0;
547 if (!encoding
|| cp
[2] != '?')
548 return; /* no munging */
549 ep
= strstr(cp
+ 3, "?=");
551 return; /* no munging */
552 switch (tolower(encoding
)) {
554 return; /* no munging */
556 sz
= decode_b_segment(cp
+ 3, piecebuf
, ep
);
559 sz
= decode_q_segment(cp
+ 3, piecebuf
, ep
, 1);
564 if (metainfo_charset
)
565 convert_to_utf8(piecebuf
, charset_q
);
566 strcpy(out
, piecebuf
);
574 static void decode_transfer_encoding(char *line
)
578 switch (transfer_encoding
) {
580 ep
= line
+ strlen(line
);
581 decode_q_segment(line
, line
, ep
, 0);
584 ep
= line
+ strlen(line
);
585 decode_b_segment(line
, line
, ep
);
592 static void handle_info(void)
595 static int done_info
= 0;
601 sub
= cleanup_subject(subject
);
604 cleanup_space(email
);
607 /* Unwrap inline B and Q encoding, and optionally
608 * normalize the meta information to utf8.
610 decode_header_bq(name
);
611 decode_header_bq(date
);
612 decode_header_bq(email
);
613 decode_header_bq(sub
);
614 printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
615 name
, email
, sub
, date
);
618 /* We are inside message body and have read line[] already.
619 * Spit out the commit log.
621 static int handle_commit_msg(void)
626 if (!memcmp("diff -", line
, 6) ||
627 !memcmp("---", line
, 3) ||
628 !memcmp("Index: ", line
, 7))
630 if ((multipart_boundary
[0] && is_multipart_boundary(line
))) {
631 /* We come here when the first part had only
632 * the commit message without any patch. We
633 * pretend we have not seen this line yet, and
634 * go back to the loop.
639 /* Unwrap transfer encoding and optionally
640 * normalize the log message to UTF-8.
642 decode_transfer_encoding(line
);
643 if (metainfo_charset
)
644 convert_to_utf8(line
, charset
);
645 fputs(line
, cmitmsg
);
646 } while (fgets(line
, sizeof(line
), stdin
) != NULL
);
652 /* We have done the commit message and have the first
653 * line of the patch in line[].
655 static void handle_patch(void)
658 if (multipart_boundary
[0] && is_multipart_boundary(line
))
660 /* Only unwrap transfer encoding but otherwise do not
661 * do anything. We do *NOT* want UTF-8 conversion
662 * here; we are dealing with the user payload.
664 decode_transfer_encoding(line
);
665 fputs(line
, patchfile
);
667 } while (fgets(line
, sizeof(line
), stdin
) != NULL
);
670 /* multipart boundary and transfer encoding are set up for us, and we
671 * are at the end of the sub header. do equivalent of handle_body up
672 * to the next boundary without closing patchfile --- we will expect
673 * that the first part to contain commit message and a patch, and
674 * handle other parts as pure patches.
676 static int handle_multipart_one_part(void)
682 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
684 len
= eatspace(line
);
688 if (is_multipart_boundary(line
))
690 if (0 <= seen
&& handle_inbody_header(&seen
, line
))
692 seen
= -1; /* no more inbody headers */
695 if (handle_commit_msg())
705 static void handle_multipart_body(void)
709 /* Skip up to the first boundary */
710 while (fgets(line
, sizeof(line
), stdin
) != NULL
)
711 if (is_multipart_boundary(line
)) {
717 /* We are on boundary line. Start slurping the subhead. */
719 int len
= read_one_header_line(line
, sizeof(line
), stdin
);
721 if (handle_multipart_one_part() < 0)
723 /* Reset per part headers */
724 transfer_encoding
= TE_DONTCARE
;
728 check_subheader_line(line
, len
);
732 fprintf(stderr
, "No patch found\n");
737 /* Non multipart message */
738 static void handle_body(void)
742 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
743 int len
= eatspace(line
);
746 if (0 <= seen
&& handle_inbody_header(&seen
, line
))
748 seen
= -1; /* no more inbody headers */
757 fprintf(stderr
, "No patch found\n");
762 static const char mailinfo_usage
[] =
763 "git-mailinfo [-k] [-u | --encoding=<encoding>] msg patch <mail >info";
765 int main(int argc
, char **argv
)
767 /* NEEDSWORK: might want to do the optional .git/ directory
770 git_config(git_default_config
);
772 while (1 < argc
&& argv
[1][0] == '-') {
773 if (!strcmp(argv
[1], "-k"))
775 else if (!strcmp(argv
[1], "-u"))
776 metainfo_charset
= git_commit_encoding
;
777 else if (!strncmp(argv
[1], "--encoding=", 11))
778 metainfo_charset
= argv
[1] + 11;
780 usage(mailinfo_usage
);
785 usage(mailinfo_usage
);
786 cmitmsg
= fopen(argv
[1], "w");
791 patchfile
= fopen(argv
[2], "w");
797 int len
= read_one_header_line(line
, sizeof(line
), stdin
);
799 if (multipart_boundary
[0])
800 handle_multipart_body();
805 check_header_line(line
, len
);