2 * Another stupid program, this one parsing the headers of an
3 * email to figure out authorship and subject
13 extern char *gitstrcasestr(const char *haystack
, const char *needle
);
16 static FILE *cmitmsg
, *patchfile
;
18 static int keep_subject
= 0;
19 static int metainfo_utf8
= 0;
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 handle_from(char *line
)
47 char *at
= strchr(line
, '@');
54 * If we already have one email, don't take any confusing lines
56 if (*email
&& strchr(at
+1, '@'))
59 /* Pick up the string around '@', possibly delimited with <>
60 * pair; that is the email part. White them out while copying.
74 unsigned char c
= *at
;
75 if (!c
|| c
== '>' || isspace(c
)) {
85 /* The remainder is name. It could be "John Doe <john.doe@xz>"
86 * or "john.doe@xz (John Doe)", but we have whited out the
87 * email part, so trim from both ends, possibly removing
88 * the () pair at the end.
90 at
= line
+ strlen(line
);
92 unsigned char c
= *--at
;
94 at
[(c
== ')') ? 0 : 1] = 0;
101 unsigned char c
= *at
;
102 if (!c
|| !isspace(c
)) {
109 at
= sanity_check(at
, email
);
114 static int handle_date(char *line
)
120 static int handle_subject(char *line
)
122 strcpy(subject
, line
);
126 /* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
127 * to have enough heuristics to grok MIME encoded patches often found
128 * on our mailing lists. For example, we do not even treat header lines
129 * case insensitively.
132 static int slurp_attr(const char *line
, const char *name
, char *attr
)
134 char *ends
, *ap
= strcasestr(line
, name
);
148 sz
= strcspn(ap
, ends
);
149 memcpy(attr
, ap
, sz
);
154 static int handle_subcontent_type(char *line
)
156 /* We do not want to mess with boundary. Note that we do not
157 * handle nested multipart.
159 if (strcasestr(line
, "boundary=")) {
160 fprintf(stderr
, "Not handling nested multipart message.\n");
163 slurp_attr(line
, "charset=", charset
);
166 for (i
= 0; (c
= charset
[i
]) != 0; i
++)
167 charset
[i
] = tolower(c
);
172 static int handle_content_type(char *line
)
174 *multipart_boundary
= 0;
175 if (slurp_attr(line
, "boundary=", multipart_boundary
+ 2)) {
176 memcpy(multipart_boundary
, "--", 2);
177 multipart_boundary_len
= strlen(multipart_boundary
);
179 slurp_attr(line
, "charset=", charset
);
183 static int handle_content_transfer_encoding(char *line
)
185 if (strcasestr(line
, "base64"))
186 transfer_encoding
= TE_BASE64
;
187 else if (strcasestr(line
, "quoted-printable"))
188 transfer_encoding
= TE_QP
;
190 transfer_encoding
= TE_DONTCARE
;
194 static int is_multipart_boundary(const char *line
)
196 return (!memcmp(line
, multipart_boundary
, multipart_boundary_len
));
199 static int eatspace(char *line
)
201 int len
= strlen(line
);
202 while (len
> 0 && isspace(line
[len
-1]))
209 #define SEEN_SUBJECT 04
211 /* First lines of body can have From:, Date:, and Subject: */
212 static int handle_inbody_header(int *seen
, char *line
)
214 if (!memcmp("From:", line
, 5) && isspace(line
[5])) {
215 if (!(*seen
& SEEN_FROM
) && handle_from(line
+6)) {
220 if (!memcmp("Date:", line
, 5) && isspace(line
[5])) {
221 if (!(*seen
& SEEN_DATE
)) {
227 if (!memcmp("Subject:", line
, 8) && isspace(line
[8])) {
228 if (!(*seen
& SEEN_SUBJECT
)) {
229 handle_subject(line
+9);
230 *seen
|= SEEN_SUBJECT
;
234 if (!memcmp("[PATCH]", line
, 7) && isspace(line
[7])) {
235 if (!(*seen
& SEEN_SUBJECT
)) {
236 handle_subject(line
);
237 *seen
|= SEEN_SUBJECT
;
244 static char *cleanup_subject(char *subject
)
253 if (!memcmp("e:", subject
+1, 2)) {
258 case ' ': case '\t': case ':':
263 p
= strchr(subject
, ']');
269 remove
= p
- subject
;
270 if (remove
<= len
*2) {
280 static void cleanup_space(char *buf
)
283 while ((c
= *buf
) != 0) {
289 int len
= strlen(buf
);
290 memmove(buf
, buf
+1, len
);
297 typedef int (*header_fn_t
)(char *);
304 static void check_header(char *line
, int len
, struct header_def
*header
)
308 if (header
[0].namelen
<= 0) {
309 for (i
= 0; header
[i
].name
; i
++)
310 header
[i
].namelen
= strlen(header
[i
].name
);
312 for (i
= 0; header
[i
].name
; i
++) {
313 int len
= header
[i
].namelen
;
314 if (!strncasecmp(line
, header
[i
].name
, len
) &&
315 line
[len
] == ':' && isspace(line
[len
+ 1])) {
316 header
[i
].func(line
+ len
+ 2);
322 static void check_subheader_line(char *line
, int len
)
324 static struct header_def header
[] = {
325 { "Content-Type", handle_subcontent_type
},
326 { "Content-Transfer-Encoding",
327 handle_content_transfer_encoding
},
330 check_header(line
, len
, header
);
332 static void check_header_line(char *line
, int len
)
334 static struct header_def header
[] = {
335 { "From", handle_from
},
336 { "Date", handle_date
},
337 { "Subject", handle_subject
},
338 { "Content-Type", handle_content_type
},
339 { "Content-Transfer-Encoding",
340 handle_content_transfer_encoding
},
343 check_header(line
, len
, header
);
346 static int read_one_header_line(char *line
, int sz
, FILE *in
)
351 if (fgets(line
+ ofs
, sz
- ofs
, in
) == NULL
)
353 len
= eatspace(line
+ ofs
);
356 peek
= fgetc(in
); ungetc(peek
, in
);
357 if (peek
== ' ' || peek
== '\t') {
358 /* Yuck, 2822 header "folding" */
367 static unsigned hexval(int c
)
369 if (c
>= '0' && c
<= '9')
371 if (c
>= 'a' && c
<= 'f')
373 if (c
>= 'A' && c
<= 'F')
378 static int decode_q_segment(char *in
, char *ot
, char *ep
)
381 while ((c
= *in
++) != 0 && (in
<= ep
)) {
385 break; /* drop trailing newline */
386 *ot
++ = ((hexval(d
) << 4) | hexval(*in
++));
395 static int decode_b_segment(char *in
, char *ot
, char *ep
)
397 /* Decode in..ep, possibly in-place to ot */
398 int c
, pos
= 0, acc
= 0;
400 while ((c
= *in
++) != 0 && (in
<= ep
)) {
405 else if ('A' <= c
&& c
<= 'Z')
407 else if ('a' <= c
&& c
<= 'z')
409 else if ('0' <= c
&& c
<= '9')
412 /* padding is almost like (c == 0), except we do
413 * not output NUL resulting only from it;
414 * for now we just trust the data.
419 continue; /* garbage */
425 *ot
++ = (acc
| (c
>> 4));
429 *ot
++ = (acc
| (c
>> 2));
442 static void convert_to_utf8(char *line
, char *charset
)
446 size_t insize
, outsize
, nrc
;
447 char outbuf
[4096]; /* cheat */
448 iconv_t conv
= iconv_open("utf-8", charset
);
450 if (conv
== (iconv_t
) -1) {
451 fprintf(stderr
, "cannot convert from %s to utf-8\n",
459 outsize
= sizeof(outbuf
);
460 nrc
= iconv(conv
, &in
, &insize
, &out
, &outsize
);
462 if (nrc
== (size_t) -1)
465 strcpy(line
, outbuf
);
469 static void decode_header_bq(char *it
)
471 char *in
, *out
, *ep
, *cp
, *sp
;
476 while ((ep
= strstr(in
, "=?")) != NULL
) {
478 char charset_q
[256], piecebuf
[256];
486 * ep : "=?iso-2022-jp?B?GyR...?= foo"
487 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
490 cp
= strchr(ep
, '?');
492 return; /* no munging */
493 for (sp
= ep
; sp
< cp
; sp
++)
494 charset_q
[sp
- ep
] = tolower(*sp
);
495 charset_q
[cp
- ep
] = 0;
497 if (!encoding
|| cp
[2] != '?')
498 return; /* no munging */
499 ep
= strstr(cp
+ 3, "?=");
501 return; /* no munging */
502 switch (tolower(encoding
)) {
504 return; /* no munging */
506 sz
= decode_b_segment(cp
+ 3, piecebuf
, ep
);
509 sz
= decode_q_segment(cp
+ 3, piecebuf
, ep
);
515 convert_to_utf8(piecebuf
, charset_q
);
516 strcpy(out
, piecebuf
);
524 static void decode_transfer_encoding(char *line
)
528 switch (transfer_encoding
) {
530 ep
= line
+ strlen(line
);
531 decode_q_segment(line
, line
, ep
);
534 ep
= line
+ strlen(line
);
535 decode_b_segment(line
, line
, ep
);
542 static void handle_info(void)
545 static int done_info
= 0;
551 sub
= cleanup_subject(subject
);
554 cleanup_space(email
);
557 /* Unwrap inline B and Q encoding, and optionally
558 * normalize the meta information to utf8.
560 decode_header_bq(name
);
561 decode_header_bq(date
);
562 decode_header_bq(email
);
563 decode_header_bq(sub
);
564 printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
565 name
, email
, sub
, date
);
568 /* We are inside message body and have read line[] already.
569 * Spit out the commit log.
571 static int handle_commit_msg(void)
576 if (!memcmp("diff -", line
, 6) ||
577 !memcmp("---", line
, 3) ||
578 !memcmp("Index: ", line
, 7))
580 if ((multipart_boundary
[0] && is_multipart_boundary(line
))) {
581 /* We come here when the first part had only
582 * the commit message without any patch. We
583 * pretend we have not seen this line yet, and
584 * go back to the loop.
589 /* Unwrap transfer encoding and optionally
590 * normalize the log message to UTF-8.
592 decode_transfer_encoding(line
);
594 convert_to_utf8(line
, charset
);
595 fputs(line
, cmitmsg
);
596 } while (fgets(line
, sizeof(line
), stdin
) != NULL
);
602 /* We have done the commit message and have the first
603 * line of the patch in line[].
605 static void handle_patch(void)
608 if (multipart_boundary
[0] && is_multipart_boundary(line
))
610 /* Only unwrap transfer encoding but otherwise do not
611 * do anything. We do *NOT* want UTF-8 conversion
612 * here; we are dealing with the user payload.
614 decode_transfer_encoding(line
);
615 fputs(line
, patchfile
);
617 } while (fgets(line
, sizeof(line
), stdin
) != NULL
);
620 /* multipart boundary and transfer encoding are set up for us, and we
621 * are at the end of the sub header. do equivalent of handle_body up
622 * to the next boundary without closing patchfile --- we will expect
623 * that the first part to contain commit message and a patch, and
624 * handle other parts as pure patches.
626 static int handle_multipart_one_part(void)
632 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
634 len
= eatspace(line
);
638 if (is_multipart_boundary(line
))
640 if (0 <= seen
&& handle_inbody_header(&seen
, line
))
642 seen
= -1; /* no more inbody headers */
645 if (handle_commit_msg())
655 static void handle_multipart_body(void)
659 /* Skip up to the first boundary */
660 while (fgets(line
, sizeof(line
), stdin
) != NULL
)
661 if (is_multipart_boundary(line
)) {
667 /* We are on boundary line. Start slurping the subhead. */
669 int len
= read_one_header_line(line
, sizeof(line
), stdin
);
671 if (handle_multipart_one_part() < 0)
675 check_subheader_line(line
, len
);
679 fprintf(stderr
, "No patch found\n");
684 /* Non multipart message */
685 static void handle_body(void)
689 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
690 int len
= eatspace(line
);
693 if (0 <= seen
&& handle_inbody_header(&seen
, line
))
695 seen
= -1; /* no more inbody headers */
704 fprintf(stderr
, "No patch found\n");
709 static const char mailinfo_usage
[] =
710 "git-mailinfo [-k] [-u] msg patch <mail >info";
712 static void usage(void) {
713 fprintf(stderr
, "%s\n", mailinfo_usage
);
717 int main(int argc
, char **argv
)
719 while (1 < argc
&& argv
[1][0] == '-') {
720 if (!strcmp(argv
[1], "-k"))
722 else if (!strcmp(argv
[1], "-u"))
731 cmitmsg
= fopen(argv
[1], "w");
736 patchfile
= fopen(argv
[2], "w");
742 int len
= read_one_header_line(line
, sizeof(line
), stdin
);
744 if (multipart_boundary
[0])
745 handle_multipart_body();
750 check_header_line(line
, len
);