2 * Another stupid program, this one parsing the headers of an
3 * email to figure out authorship and subject
11 #include "git-compat-util.h"
14 static FILE *cmitmsg
, *patchfile
;
16 static int keep_subject
= 0;
17 static char *metainfo_charset
= NULL
;
18 static char line
[1000];
19 static char date
[1000];
20 static char name
[1000];
21 static char email
[1000];
22 static char subject
[1000];
25 TE_DONTCARE
, TE_QP
, TE_BASE64
,
27 static char charset
[256];
29 static char multipart_boundary
[1000];
30 static int multipart_boundary_len
;
31 static int patch_lines
= 0;
33 static char *sanity_check(char *name
, char *email
)
35 int len
= strlen(name
);
36 if (len
< 3 || len
> 60)
38 if (strchr(name
, '@') || strchr(name
, '<') || strchr(name
, '>'))
43 static int handle_from(char *line
)
45 char *at
= strchr(line
, '@');
52 * If we already have one email, don't take any confusing lines
54 if (*email
&& strchr(at
+1, '@'))
57 /* Pick up the string around '@', possibly delimited with <>
58 * pair; that is the email part. White them out while copying.
72 unsigned char c
= *at
;
73 if (!c
|| c
== '>' || isspace(c
)) {
83 /* The remainder is name. It could be "John Doe <john.doe@xz>"
84 * or "john.doe@xz (John Doe)", but we have whited out the
85 * email part, so trim from both ends, possibly removing
86 * the () pair at the end.
88 at
= line
+ strlen(line
);
90 unsigned char c
= *--at
;
92 at
[(c
== ')') ? 0 : 1] = 0;
99 unsigned char c
= *at
;
100 if (!c
|| !isspace(c
)) {
107 at
= sanity_check(at
, email
);
112 static int handle_date(char *line
)
118 static int handle_subject(char *line
)
120 strcpy(subject
, line
);
124 /* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
125 * to have enough heuristics to grok MIME encoded patches often found
126 * on our mailing lists. For example, we do not even treat header lines
127 * case insensitively.
130 static int slurp_attr(const char *line
, const char *name
, char *attr
)
132 char *ends
, *ap
= strcasestr(line
, name
);
146 sz
= strcspn(ap
, ends
);
147 memcpy(attr
, ap
, sz
);
152 static int handle_subcontent_type(char *line
)
154 /* We do not want to mess with boundary. Note that we do not
155 * handle nested multipart.
157 if (strcasestr(line
, "boundary=")) {
158 fprintf(stderr
, "Not handling nested multipart message.\n");
161 slurp_attr(line
, "charset=", charset
);
164 for (i
= 0; (c
= charset
[i
]) != 0; i
++)
165 charset
[i
] = tolower(c
);
170 static int handle_content_type(char *line
)
172 *multipart_boundary
= 0;
173 if (slurp_attr(line
, "boundary=", multipart_boundary
+ 2)) {
174 memcpy(multipart_boundary
, "--", 2);
175 multipart_boundary_len
= strlen(multipart_boundary
);
177 slurp_attr(line
, "charset=", charset
);
181 static int handle_content_transfer_encoding(char *line
)
183 if (strcasestr(line
, "base64"))
184 transfer_encoding
= TE_BASE64
;
185 else if (strcasestr(line
, "quoted-printable"))
186 transfer_encoding
= TE_QP
;
188 transfer_encoding
= TE_DONTCARE
;
192 static int is_multipart_boundary(const char *line
)
194 return (!memcmp(line
, multipart_boundary
, multipart_boundary_len
));
197 static int eatspace(char *line
)
199 int len
= strlen(line
);
200 while (len
> 0 && isspace(line
[len
-1]))
207 #define SEEN_SUBJECT 04
209 /* First lines of body can have From:, Date:, and Subject: */
210 static int handle_inbody_header(int *seen
, char *line
)
212 if (!memcmp("From:", line
, 5) && isspace(line
[5])) {
213 if (!(*seen
& SEEN_FROM
) && handle_from(line
+6)) {
218 if (!memcmp("Date:", line
, 5) && isspace(line
[5])) {
219 if (!(*seen
& SEEN_DATE
)) {
225 if (!memcmp("Subject:", line
, 8) && isspace(line
[8])) {
226 if (!(*seen
& SEEN_SUBJECT
)) {
227 handle_subject(line
+9);
228 *seen
|= SEEN_SUBJECT
;
232 if (!memcmp("[PATCH]", line
, 7) && isspace(line
[7])) {
233 if (!(*seen
& SEEN_SUBJECT
)) {
234 handle_subject(line
);
235 *seen
|= SEEN_SUBJECT
;
242 static char *cleanup_subject(char *subject
)
251 if (!memcmp("e:", subject
+1, 2)) {
256 case ' ': case '\t': case ':':
261 p
= strchr(subject
, ']');
267 remove
= p
- subject
;
268 if (remove
<= len
*2) {
278 static void cleanup_space(char *buf
)
281 while ((c
= *buf
) != 0) {
287 int len
= strlen(buf
);
288 memmove(buf
, buf
+1, len
);
295 typedef int (*header_fn_t
)(char *);
302 static void check_header(char *line
, int len
, struct header_def
*header
)
306 if (header
[0].namelen
<= 0) {
307 for (i
= 0; header
[i
].name
; i
++)
308 header
[i
].namelen
= strlen(header
[i
].name
);
310 for (i
= 0; header
[i
].name
; i
++) {
311 int len
= header
[i
].namelen
;
312 if (!strncasecmp(line
, header
[i
].name
, len
) &&
313 line
[len
] == ':' && isspace(line
[len
+ 1])) {
314 header
[i
].func(line
+ len
+ 2);
320 static void check_subheader_line(char *line
, int len
)
322 static struct header_def header
[] = {
323 { "Content-Type", handle_subcontent_type
},
324 { "Content-Transfer-Encoding",
325 handle_content_transfer_encoding
},
328 check_header(line
, len
, header
);
330 static void check_header_line(char *line
, int len
)
332 static struct header_def header
[] = {
333 { "From", handle_from
},
334 { "Date", handle_date
},
335 { "Subject", handle_subject
},
336 { "Content-Type", handle_content_type
},
337 { "Content-Transfer-Encoding",
338 handle_content_transfer_encoding
},
341 check_header(line
, len
, header
);
344 static int read_one_header_line(char *line
, int sz
, FILE *in
)
349 if (fgets(line
+ ofs
, sz
- ofs
, in
) == NULL
)
351 len
= eatspace(line
+ ofs
);
354 peek
= fgetc(in
); ungetc(peek
, in
);
355 if (peek
== ' ' || peek
== '\t') {
356 /* Yuck, 2822 header "folding" */
365 static unsigned hexval(int c
)
367 if (c
>= '0' && c
<= '9')
369 if (c
>= 'a' && c
<= 'f')
371 if (c
>= 'A' && c
<= 'F')
376 static int decode_q_segment(char *in
, char *ot
, char *ep
)
379 while ((c
= *in
++) != 0 && (in
<= ep
)) {
383 break; /* drop trailing newline */
384 *ot
++ = ((hexval(d
) << 4) | hexval(*in
++));
393 static int decode_b_segment(char *in
, char *ot
, char *ep
)
395 /* Decode in..ep, possibly in-place to ot */
396 int c
, pos
= 0, acc
= 0;
398 while ((c
= *in
++) != 0 && (in
<= ep
)) {
403 else if ('A' <= c
&& c
<= 'Z')
405 else if ('a' <= c
&& c
<= 'z')
407 else if ('0' <= c
&& c
<= '9')
410 /* padding is almost like (c == 0), except we do
411 * not output NUL resulting only from it;
412 * for now we just trust the data.
417 continue; /* garbage */
423 *ot
++ = (acc
| (c
>> 4));
427 *ot
++ = (acc
| (c
>> 2));
440 static void convert_to_utf8(char *line
, char *charset
)
443 size_t insize
, outsize
, nrc
;
444 char outbuf
[4096]; /* cheat */
445 static char latin_one
[] = "latin-1";
446 char *input_charset
= *charset
? charset
: latin_one
;
447 iconv_t conv
= iconv_open(metainfo_charset
, input_charset
);
449 if (conv
== (iconv_t
) -1) {
450 static int warned_latin1_once
= 0;
451 if (input_charset
!= latin_one
) {
452 fprintf(stderr
, "cannot convert from %s to %s\n",
453 input_charset
, metainfo_charset
);
456 else if (!warned_latin1_once
) {
457 warned_latin1_once
= 1;
458 fprintf(stderr
, "tried to convert from %s to %s, "
459 "but your iconv does not work with it.\n",
460 input_charset
, metainfo_charset
);
467 outsize
= sizeof(outbuf
);
468 nrc
= iconv(conv
, &in
, &insize
, &out
, &outsize
);
470 if (nrc
== (size_t) -1)
473 strcpy(line
, outbuf
);
476 static void decode_header_bq(char *it
)
478 char *in
, *out
, *ep
, *cp
, *sp
;
483 while ((ep
= strstr(in
, "=?")) != NULL
) {
485 char charset_q
[256], piecebuf
[256];
493 * ep : "=?iso-2022-jp?B?GyR...?= foo"
494 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
497 cp
= strchr(ep
, '?');
499 return; /* no munging */
500 for (sp
= ep
; sp
< cp
; sp
++)
501 charset_q
[sp
- ep
] = tolower(*sp
);
502 charset_q
[cp
- ep
] = 0;
504 if (!encoding
|| cp
[2] != '?')
505 return; /* no munging */
506 ep
= strstr(cp
+ 3, "?=");
508 return; /* no munging */
509 switch (tolower(encoding
)) {
511 return; /* no munging */
513 sz
= decode_b_segment(cp
+ 3, piecebuf
, ep
);
516 sz
= decode_q_segment(cp
+ 3, piecebuf
, ep
);
521 if (metainfo_charset
)
522 convert_to_utf8(piecebuf
, charset_q
);
523 strcpy(out
, piecebuf
);
531 static void decode_transfer_encoding(char *line
)
535 switch (transfer_encoding
) {
537 ep
= line
+ strlen(line
);
538 decode_q_segment(line
, line
, ep
);
541 ep
= line
+ strlen(line
);
542 decode_b_segment(line
, line
, ep
);
549 static void handle_info(void)
552 static int done_info
= 0;
558 sub
= cleanup_subject(subject
);
561 cleanup_space(email
);
564 /* Unwrap inline B and Q encoding, and optionally
565 * normalize the meta information to utf8.
567 decode_header_bq(name
);
568 decode_header_bq(date
);
569 decode_header_bq(email
);
570 decode_header_bq(sub
);
571 printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
572 name
, email
, sub
, date
);
575 /* We are inside message body and have read line[] already.
576 * Spit out the commit log.
578 static int handle_commit_msg(void)
583 if (!memcmp("diff -", line
, 6) ||
584 !memcmp("---", line
, 3) ||
585 !memcmp("Index: ", line
, 7))
587 if ((multipart_boundary
[0] && is_multipart_boundary(line
))) {
588 /* We come here when the first part had only
589 * the commit message without any patch. We
590 * pretend we have not seen this line yet, and
591 * go back to the loop.
596 /* Unwrap transfer encoding and optionally
597 * normalize the log message to UTF-8.
599 decode_transfer_encoding(line
);
600 if (metainfo_charset
)
601 convert_to_utf8(line
, charset
);
602 fputs(line
, cmitmsg
);
603 } while (fgets(line
, sizeof(line
), stdin
) != NULL
);
609 /* We have done the commit message and have the first
610 * line of the patch in line[].
612 static void handle_patch(void)
615 if (multipart_boundary
[0] && is_multipart_boundary(line
))
617 /* Only unwrap transfer encoding but otherwise do not
618 * do anything. We do *NOT* want UTF-8 conversion
619 * here; we are dealing with the user payload.
621 decode_transfer_encoding(line
);
622 fputs(line
, patchfile
);
624 } while (fgets(line
, sizeof(line
), stdin
) != NULL
);
627 /* multipart boundary and transfer encoding are set up for us, and we
628 * are at the end of the sub header. do equivalent of handle_body up
629 * to the next boundary without closing patchfile --- we will expect
630 * that the first part to contain commit message and a patch, and
631 * handle other parts as pure patches.
633 static int handle_multipart_one_part(void)
639 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
641 len
= eatspace(line
);
645 if (is_multipart_boundary(line
))
647 if (0 <= seen
&& handle_inbody_header(&seen
, line
))
649 seen
= -1; /* no more inbody headers */
652 if (handle_commit_msg())
662 static void handle_multipart_body(void)
666 /* Skip up to the first boundary */
667 while (fgets(line
, sizeof(line
), stdin
) != NULL
)
668 if (is_multipart_boundary(line
)) {
674 /* We are on boundary line. Start slurping the subhead. */
676 int len
= read_one_header_line(line
, sizeof(line
), stdin
);
678 if (handle_multipart_one_part() < 0)
682 check_subheader_line(line
, len
);
686 fprintf(stderr
, "No patch found\n");
691 /* Non multipart message */
692 static void handle_body(void)
696 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
697 int len
= eatspace(line
);
700 if (0 <= seen
&& handle_inbody_header(&seen
, line
))
702 seen
= -1; /* no more inbody headers */
711 fprintf(stderr
, "No patch found\n");
716 static const char mailinfo_usage
[] =
717 "git-mailinfo [-k] [-u | --encoding=<encoding>] msg patch <mail >info";
719 int main(int argc
, char **argv
)
721 /* NEEDSWORK: might want to do the optional .git/ directory
724 git_config(git_default_config
);
726 while (1 < argc
&& argv
[1][0] == '-') {
727 if (!strcmp(argv
[1], "-k"))
729 else if (!strcmp(argv
[1], "-u"))
730 metainfo_charset
= git_commit_encoding
;
731 else if (!strncmp(argv
[1], "--encoding=", 11))
732 metainfo_charset
= argv
[1] + 11;
734 usage(mailinfo_usage
);
739 usage(mailinfo_usage
);
740 cmitmsg
= fopen(argv
[1], "w");
745 patchfile
= fopen(argv
[2], "w");
751 int len
= read_one_header_line(line
, sizeof(line
), stdin
);
753 if (multipart_boundary
[0])
754 handle_multipart_body();
759 check_header_line(line
, len
);