2 * Another stupid program, this one parsing the headers of an
3 * email to figure out authorship and subject
12 static FILE *cmitmsg
, *patchfile
;
14 static int keep_subject
= 0;
15 static int metainfo_utf8
= 0;
16 static char line
[1000];
17 static char date
[1000];
18 static char name
[1000];
19 static char email
[1000];
20 static char subject
[1000];
23 TE_DONTCARE
, TE_QP
, TE_BASE64
,
25 static char charset
[256];
27 static char multipart_boundary
[1000];
28 static int multipart_boundary_len
;
29 static int patch_lines
= 0;
31 static char *sanity_check(char *name
, char *email
)
33 int len
= strlen(name
);
34 if (len
< 3 || len
> 60)
36 if (strchr(name
, '@') || strchr(name
, '<') || strchr(name
, '>'))
41 static int handle_from(char *line
)
43 char *at
= strchr(line
, '@');
50 * If we already have one email, don't take any confusing lines
52 if (*email
&& strchr(at
+1, '@'))
55 /* Pick up the string around '@', possibly delimited with <>
56 * pair; that is the email part. White them out while copying.
70 unsigned char c
= *at
;
71 if (!c
|| c
== '>' || isspace(c
)) {
81 /* The remainder is name. It could be "John Doe <john.doe@xz>"
82 * or "john.doe@xz (John Doe)", but we have whited out the
83 * email part, so trim from both ends, possibly removing
84 * the () pair at the end.
86 at
= line
+ strlen(line
);
88 unsigned char c
= *--at
;
90 at
[(c
== ')') ? 0 : 1] = 0;
97 unsigned char c
= *at
;
98 if (!c
|| !isspace(c
)) {
105 at
= sanity_check(at
, email
);
110 static int handle_date(char *line
)
116 static int handle_subject(char *line
)
118 strcpy(subject
, line
);
122 /* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
123 * to have enough heuristics to grok MIME encoded patches often found
124 * on our mailing lists. For example, we do not even treat header lines
125 * case insensitively.
128 static int slurp_attr(const char *line
, const char *name
, char *attr
)
130 char *ends
, *ap
= strcasestr(line
, name
);
144 sz
= strcspn(ap
, ends
);
145 memcpy(attr
, ap
, sz
);
150 static int handle_subcontent_type(char *line
)
152 /* We do not want to mess with boundary. Note that we do not
153 * handle nested multipart.
155 slurp_attr(line
, "charset=", charset
);
158 for (i
= 0; (c
= charset
[i
]) != 0; i
++)
159 charset
[i
] = tolower(c
);
164 static int handle_content_type(char *line
)
166 *multipart_boundary
= 0;
167 if (slurp_attr(line
, "boundary=", multipart_boundary
+ 2)) {
168 memcpy(multipart_boundary
, "--", 2);
169 multipart_boundary_len
= strlen(multipart_boundary
);
171 slurp_attr(line
, "charset=", charset
);
175 static int handle_content_transfer_encoding(char *line
)
177 if (strcasestr(line
, "base64"))
178 transfer_encoding
= TE_BASE64
;
179 else if (strcasestr(line
, "quoted-printable"))
180 transfer_encoding
= TE_QP
;
182 transfer_encoding
= TE_DONTCARE
;
186 static int is_multipart_boundary(const char *line
)
188 return (!memcmp(line
, multipart_boundary
, multipart_boundary_len
));
191 static int eatspace(char *line
)
193 int len
= strlen(line
);
194 while (len
> 0 && isspace(line
[len
-1]))
201 #define SEEN_SUBJECT 04
203 /* First lines of body can have From:, Date:, and Subject: */
204 static int handle_inbody_header(int *seen
, char *line
)
206 if (!memcmp("From:", line
, 5) && isspace(line
[5])) {
207 if (!(*seen
& SEEN_FROM
) && handle_from(line
+6)) {
212 if (!memcmp("Date:", line
, 5) && isspace(line
[5])) {
213 if (!(*seen
& SEEN_DATE
)) {
219 if (!memcmp("Subject:", line
, 8) && isspace(line
[8])) {
220 if (!(*seen
& SEEN_SUBJECT
)) {
221 handle_subject(line
+9);
222 *seen
|= SEEN_SUBJECT
;
226 if (!memcmp("[PATCH]", line
, 7) && isspace(line
[7])) {
227 if (!(*seen
& SEEN_SUBJECT
)) {
228 handle_subject(line
);
229 *seen
|= SEEN_SUBJECT
;
236 static char *cleanup_subject(char *subject
)
245 if (!memcmp("e:", subject
+1, 2)) {
250 case ' ': case '\t': case ':':
255 p
= strchr(subject
, ']');
261 remove
= p
- subject
;
262 if (remove
<= len
*2) {
272 static void cleanup_space(char *buf
)
275 while ((c
= *buf
) != 0) {
281 int len
= strlen(buf
);
282 memmove(buf
, buf
+1, len
);
289 typedef int (*header_fn_t
)(char *);
296 static void check_header(char *line
, int len
, struct header_def
*header
)
300 if (header
[0].namelen
<= 0) {
301 for (i
= 0; header
[i
].name
; i
++)
302 header
[i
].namelen
= strlen(header
[i
].name
);
304 for (i
= 0; header
[i
].name
; i
++) {
305 int len
= header
[i
].namelen
;
306 if (!strncasecmp(line
, header
[i
].name
, len
) &&
307 line
[len
] == ':' && isspace(line
[len
+ 1])) {
308 header
[i
].func(line
+ len
+ 2);
314 static void check_subheader_line(char *line
, int len
)
316 static struct header_def header
[] = {
317 { "Content-Type", handle_subcontent_type
},
318 { "Content-Transfer-Encoding",
319 handle_content_transfer_encoding
},
322 check_header(line
, len
, header
);
324 static void check_header_line(char *line
, int len
)
326 static struct header_def header
[] = {
327 { "From", handle_from
},
328 { "Date", handle_date
},
329 { "Subject", handle_subject
},
330 { "Content-Type", handle_content_type
},
331 { "Content-Transfer-Encoding",
332 handle_content_transfer_encoding
},
335 check_header(line
, len
, header
);
338 static int read_one_header_line(char *line
, int sz
, FILE *in
)
343 if (fgets(line
+ ofs
, sz
- ofs
, in
) == NULL
)
345 len
= eatspace(line
+ ofs
);
348 peek
= fgetc(in
); ungetc(peek
, in
);
349 if (peek
== ' ' || peek
== '\t') {
350 /* Yuck, 2822 header "folding" */
359 static unsigned hexval(int c
)
361 if (c
>= '0' && c
<= '9')
363 if (c
>= 'a' && c
<= 'f')
365 if (c
>= 'A' && c
<= 'F')
370 static int decode_q_segment(char *in
, char *ot
, char *ep
)
373 while ((c
= *in
++) != 0 && (in
<= ep
)) {
377 break; /* drop trailing newline */
378 *ot
++ = ((hexval(d
) << 4) | hexval(*in
++));
387 static int decode_b_segment(char *in
, char *ot
, char *ep
)
389 /* Decode in..ep, possibly in-place to ot */
390 int c
, pos
= 0, acc
= 0;
392 while ((c
= *in
++) != 0 && (in
<= ep
)) {
397 else if ('A' <= c
&& c
<= 'Z')
399 else if ('a' <= c
&& c
<= 'z')
401 else if ('0' <= c
&& c
<= '9')
404 /* padding is almost like (c == 0), except we do
405 * not output NUL resulting only from it;
406 * for now we just trust the data.
411 continue; /* garbage */
417 *ot
++ = (acc
| (c
>> 4));
421 *ot
++ = (acc
| (c
>> 2));
434 static void convert_to_utf8(char *line
, char *charset
)
438 size_t insize
, outsize
, nrc
;
439 char outbuf
[4096]; /* cheat */
440 iconv_t conv
= iconv_open("utf-8", charset
);
442 if (conv
== (iconv_t
) -1) {
443 fprintf(stderr
, "cannot convert from %s to utf-8\n",
451 outsize
= sizeof(outbuf
);
452 nrc
= iconv(conv
, &in
, &insize
, &out
, &outsize
);
454 if (nrc
== (size_t) -1)
457 strcpy(line
, outbuf
);
461 static void decode_header_bq(char *it
)
463 char *in
, *out
, *ep
, *cp
, *sp
;
468 while ((ep
= strstr(in
, "=?")) != NULL
) {
470 char charset_q
[256], piecebuf
[256];
478 * ep : "=?iso-2022-jp?B?GyR...?= foo"
479 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
482 cp
= strchr(ep
, '?');
484 return; /* no munging */
485 for (sp
= ep
; sp
< cp
; sp
++)
486 charset_q
[sp
- ep
] = tolower(*sp
);
487 charset_q
[cp
- ep
] = 0;
489 if (!encoding
|| cp
[2] != '?')
490 return; /* no munging */
491 ep
= strstr(cp
+ 3, "?=");
493 return; /* no munging */
494 switch (tolower(encoding
)) {
496 return; /* no munging */
498 sz
= decode_b_segment(cp
+ 3, piecebuf
, ep
);
501 sz
= decode_q_segment(cp
+ 3, piecebuf
, ep
);
507 convert_to_utf8(piecebuf
, charset_q
);
508 strcpy(out
, piecebuf
);
516 static void decode_transfer_encoding(char *line
)
520 switch (transfer_encoding
) {
522 ep
= line
+ strlen(line
);
523 decode_q_segment(line
, line
, ep
);
526 ep
= line
+ strlen(line
);
527 decode_b_segment(line
, line
, ep
);
534 static void handle_info(void)
537 static int done_info
= 0;
543 sub
= cleanup_subject(subject
);
546 cleanup_space(email
);
549 /* Unwrap inline B and Q encoding, and optionally
550 * normalize the meta information to utf8.
552 decode_header_bq(name
);
553 decode_header_bq(date
);
554 decode_header_bq(email
);
555 decode_header_bq(sub
);
556 printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
557 name
, email
, sub
, date
);
560 /* We are inside message body and have read line[] already.
561 * Spit out the commit log.
563 static int handle_commit_msg(void)
568 if (!memcmp("diff -", line
, 6) ||
569 !memcmp("---", line
, 3) ||
570 !memcmp("Index: ", line
, 7))
572 if ((multipart_boundary
[0] && is_multipart_boundary(line
))) {
573 /* We come here when the first part had only
574 * the commit message without any patch. We
575 * pretend we have not seen this line yet, and
576 * go back to the loop.
581 /* Unwrap transfer encoding and optionally
582 * normalize the log message to UTF-8.
584 decode_transfer_encoding(line
);
586 convert_to_utf8(line
, charset
);
587 fputs(line
, cmitmsg
);
588 } while (fgets(line
, sizeof(line
), stdin
) != NULL
);
594 /* We have done the commit message and have the first
595 * line of the patch in line[].
597 static void handle_patch(void)
600 if (multipart_boundary
[0] && is_multipart_boundary(line
))
602 /* Only unwrap transfer encoding but otherwise do not
603 * do anything. We do *NOT* want UTF-8 conversion
604 * here; we are dealing with the user payload.
606 decode_transfer_encoding(line
);
607 fputs(line
, patchfile
);
609 } while (fgets(line
, sizeof(line
), stdin
) != NULL
);
612 /* multipart boundary and transfer encoding are set up for us, and we
613 * are at the end of the sub header. do equivalent of handle_body up
614 * to the next boundary without closing patchfile --- we will expect
615 * that the first part to contain commit message and a patch, and
616 * handle other parts as pure patches.
618 static int handle_multipart_one_part(void)
624 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
626 len
= eatspace(line
);
630 if (is_multipart_boundary(line
))
632 if (0 <= seen
&& handle_inbody_header(&seen
, line
))
634 seen
= -1; /* no more inbody headers */
637 if (handle_commit_msg())
647 static void handle_multipart_body(void)
651 /* Skip up to the first boundary */
652 while (fgets(line
, sizeof(line
), stdin
) != NULL
)
653 if (is_multipart_boundary(line
)) {
659 /* We are on boundary line. Start slurping the subhead. */
661 int len
= read_one_header_line(line
, sizeof(line
), stdin
);
663 if (handle_multipart_one_part() < 0)
667 check_subheader_line(line
, len
);
671 fprintf(stderr
, "No patch found\n");
676 /* Non multipart message */
677 static void handle_body(void)
681 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
682 int len
= eatspace(line
);
685 if (0 <= seen
&& handle_inbody_header(&seen
, line
))
687 seen
= -1; /* no more inbody headers */
696 fprintf(stderr
, "No patch found\n");
701 static const char mailinfo_usage
[] =
702 "git-mailinfo [-k] [-u] msg patch <mail >info";
704 static void usage(void) {
705 fprintf(stderr
, "%s\n", mailinfo_usage
);
709 int main(int argc
, char **argv
)
711 while (1 < argc
&& argv
[1][0] == '-') {
712 if (!strcmp(argv
[1], "-k"))
714 else if (!strcmp(argv
[1], "-u"))
723 cmitmsg
= fopen(argv
[1], "w");
728 patchfile
= fopen(argv
[2], "w");
734 int len
= read_one_header_line(line
, sizeof(line
), stdin
);
736 if (multipart_boundary
[0])
737 handle_multipart_body();
742 check_header_line(line
, len
);