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 if (strcasestr(line
, "boundary=")) {
156 fprintf(stderr
, "Not handling nested multipart message.\n");
159 slurp_attr(line
, "charset=", charset
);
162 for (i
= 0; (c
= charset
[i
]) != 0; i
++)
163 charset
[i
] = tolower(c
);
168 static int handle_content_type(char *line
)
170 *multipart_boundary
= 0;
171 if (slurp_attr(line
, "boundary=", multipart_boundary
+ 2)) {
172 memcpy(multipart_boundary
, "--", 2);
173 multipart_boundary_len
= strlen(multipart_boundary
);
175 slurp_attr(line
, "charset=", charset
);
179 static int handle_content_transfer_encoding(char *line
)
181 if (strcasestr(line
, "base64"))
182 transfer_encoding
= TE_BASE64
;
183 else if (strcasestr(line
, "quoted-printable"))
184 transfer_encoding
= TE_QP
;
186 transfer_encoding
= TE_DONTCARE
;
190 static int is_multipart_boundary(const char *line
)
192 return (!memcmp(line
, multipart_boundary
, multipart_boundary_len
));
195 static int eatspace(char *line
)
197 int len
= strlen(line
);
198 while (len
> 0 && isspace(line
[len
-1]))
205 #define SEEN_SUBJECT 04
207 /* First lines of body can have From:, Date:, and Subject: */
208 static int handle_inbody_header(int *seen
, char *line
)
210 if (!memcmp("From:", line
, 5) && isspace(line
[5])) {
211 if (!(*seen
& SEEN_FROM
) && handle_from(line
+6)) {
216 if (!memcmp("Date:", line
, 5) && isspace(line
[5])) {
217 if (!(*seen
& SEEN_DATE
)) {
223 if (!memcmp("Subject:", line
, 8) && isspace(line
[8])) {
224 if (!(*seen
& SEEN_SUBJECT
)) {
225 handle_subject(line
+9);
226 *seen
|= SEEN_SUBJECT
;
230 if (!memcmp("[PATCH]", line
, 7) && isspace(line
[7])) {
231 if (!(*seen
& SEEN_SUBJECT
)) {
232 handle_subject(line
);
233 *seen
|= SEEN_SUBJECT
;
240 static char *cleanup_subject(char *subject
)
249 if (!memcmp("e:", subject
+1, 2)) {
254 case ' ': case '\t': case ':':
259 p
= strchr(subject
, ']');
265 remove
= p
- subject
;
266 if (remove
<= len
*2) {
276 static void cleanup_space(char *buf
)
279 while ((c
= *buf
) != 0) {
285 int len
= strlen(buf
);
286 memmove(buf
, buf
+1, len
);
293 typedef int (*header_fn_t
)(char *);
300 static void check_header(char *line
, int len
, struct header_def
*header
)
304 if (header
[0].namelen
<= 0) {
305 for (i
= 0; header
[i
].name
; i
++)
306 header
[i
].namelen
= strlen(header
[i
].name
);
308 for (i
= 0; header
[i
].name
; i
++) {
309 int len
= header
[i
].namelen
;
310 if (!strncasecmp(line
, header
[i
].name
, len
) &&
311 line
[len
] == ':' && isspace(line
[len
+ 1])) {
312 header
[i
].func(line
+ len
+ 2);
318 static void check_subheader_line(char *line
, int len
)
320 static struct header_def header
[] = {
321 { "Content-Type", handle_subcontent_type
},
322 { "Content-Transfer-Encoding",
323 handle_content_transfer_encoding
},
326 check_header(line
, len
, header
);
328 static void check_header_line(char *line
, int len
)
330 static struct header_def header
[] = {
331 { "From", handle_from
},
332 { "Date", handle_date
},
333 { "Subject", handle_subject
},
334 { "Content-Type", handle_content_type
},
335 { "Content-Transfer-Encoding",
336 handle_content_transfer_encoding
},
339 check_header(line
, len
, header
);
342 static int read_one_header_line(char *line
, int sz
, FILE *in
)
347 if (fgets(line
+ ofs
, sz
- ofs
, in
) == NULL
)
349 len
= eatspace(line
+ ofs
);
352 peek
= fgetc(in
); ungetc(peek
, in
);
353 if (peek
== ' ' || peek
== '\t') {
354 /* Yuck, 2822 header "folding" */
363 static unsigned hexval(int c
)
365 if (c
>= '0' && c
<= '9')
367 if (c
>= 'a' && c
<= 'f')
369 if (c
>= 'A' && c
<= 'F')
374 static int decode_q_segment(char *in
, char *ot
, char *ep
)
377 while ((c
= *in
++) != 0 && (in
<= ep
)) {
381 break; /* drop trailing newline */
382 *ot
++ = ((hexval(d
) << 4) | hexval(*in
++));
391 static int decode_b_segment(char *in
, char *ot
, char *ep
)
393 /* Decode in..ep, possibly in-place to ot */
394 int c
, pos
= 0, acc
= 0;
396 while ((c
= *in
++) != 0 && (in
<= ep
)) {
401 else if ('A' <= c
&& c
<= 'Z')
403 else if ('a' <= c
&& c
<= 'z')
405 else if ('0' <= c
&& c
<= '9')
408 /* padding is almost like (c == 0), except we do
409 * not output NUL resulting only from it;
410 * for now we just trust the data.
415 continue; /* garbage */
421 *ot
++ = (acc
| (c
>> 4));
425 *ot
++ = (acc
| (c
>> 2));
438 static void convert_to_utf8(char *line
, char *charset
)
442 size_t insize
, outsize
, nrc
;
443 char outbuf
[4096]; /* cheat */
444 iconv_t conv
= iconv_open("utf-8", charset
);
446 if (conv
== (iconv_t
) -1) {
447 fprintf(stderr
, "cannot convert from %s to utf-8\n",
455 outsize
= sizeof(outbuf
);
456 nrc
= iconv(conv
, &in
, &insize
, &out
, &outsize
);
458 if (nrc
== (size_t) -1)
461 strcpy(line
, outbuf
);
465 static void decode_header_bq(char *it
)
467 char *in
, *out
, *ep
, *cp
, *sp
;
472 while ((ep
= strstr(in
, "=?")) != NULL
) {
474 char charset_q
[256], piecebuf
[256];
482 * ep : "=?iso-2022-jp?B?GyR...?= foo"
483 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
486 cp
= strchr(ep
, '?');
488 return; /* no munging */
489 for (sp
= ep
; sp
< cp
; sp
++)
490 charset_q
[sp
- ep
] = tolower(*sp
);
491 charset_q
[cp
- ep
] = 0;
493 if (!encoding
|| cp
[2] != '?')
494 return; /* no munging */
495 ep
= strstr(cp
+ 3, "?=");
497 return; /* no munging */
498 switch (tolower(encoding
)) {
500 return; /* no munging */
502 sz
= decode_b_segment(cp
+ 3, piecebuf
, ep
);
505 sz
= decode_q_segment(cp
+ 3, piecebuf
, ep
);
511 convert_to_utf8(piecebuf
, charset_q
);
512 strcpy(out
, piecebuf
);
520 static void decode_transfer_encoding(char *line
)
524 switch (transfer_encoding
) {
526 ep
= line
+ strlen(line
);
527 decode_q_segment(line
, line
, ep
);
530 ep
= line
+ strlen(line
);
531 decode_b_segment(line
, line
, ep
);
538 static void handle_info(void)
541 static int done_info
= 0;
547 sub
= cleanup_subject(subject
);
550 cleanup_space(email
);
553 /* Unwrap inline B and Q encoding, and optionally
554 * normalize the meta information to utf8.
556 decode_header_bq(name
);
557 decode_header_bq(date
);
558 decode_header_bq(email
);
559 decode_header_bq(sub
);
560 printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
561 name
, email
, sub
, date
);
564 /* We are inside message body and have read line[] already.
565 * Spit out the commit log.
567 static int handle_commit_msg(void)
572 if (!memcmp("diff -", line
, 6) ||
573 !memcmp("---", line
, 3) ||
574 !memcmp("Index: ", line
, 7))
576 if ((multipart_boundary
[0] && is_multipart_boundary(line
))) {
577 /* We come here when the first part had only
578 * the commit message without any patch. We
579 * pretend we have not seen this line yet, and
580 * go back to the loop.
585 /* Unwrap transfer encoding and optionally
586 * normalize the log message to UTF-8.
588 decode_transfer_encoding(line
);
590 convert_to_utf8(line
, charset
);
591 fputs(line
, cmitmsg
);
592 } while (fgets(line
, sizeof(line
), stdin
) != NULL
);
598 /* We have done the commit message and have the first
599 * line of the patch in line[].
601 static void handle_patch(void)
604 if (multipart_boundary
[0] && is_multipart_boundary(line
))
606 /* Only unwrap transfer encoding but otherwise do not
607 * do anything. We do *NOT* want UTF-8 conversion
608 * here; we are dealing with the user payload.
610 decode_transfer_encoding(line
);
611 fputs(line
, patchfile
);
613 } while (fgets(line
, sizeof(line
), stdin
) != NULL
);
616 /* multipart boundary and transfer encoding are set up for us, and we
617 * are at the end of the sub header. do equivalent of handle_body up
618 * to the next boundary without closing patchfile --- we will expect
619 * that the first part to contain commit message and a patch, and
620 * handle other parts as pure patches.
622 static int handle_multipart_one_part(void)
628 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
630 len
= eatspace(line
);
634 if (is_multipart_boundary(line
))
636 if (0 <= seen
&& handle_inbody_header(&seen
, line
))
638 seen
= -1; /* no more inbody headers */
641 if (handle_commit_msg())
651 static void handle_multipart_body(void)
655 /* Skip up to the first boundary */
656 while (fgets(line
, sizeof(line
), stdin
) != NULL
)
657 if (is_multipart_boundary(line
)) {
663 /* We are on boundary line. Start slurping the subhead. */
665 int len
= read_one_header_line(line
, sizeof(line
), stdin
);
667 if (handle_multipart_one_part() < 0)
671 check_subheader_line(line
, len
);
675 fprintf(stderr
, "No patch found\n");
680 /* Non multipart message */
681 static void handle_body(void)
685 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
686 int len
= eatspace(line
);
689 if (0 <= seen
&& handle_inbody_header(&seen
, line
))
691 seen
= -1; /* no more inbody headers */
700 fprintf(stderr
, "No patch found\n");
705 static const char mailinfo_usage
[] =
706 "git-mailinfo [-k] [-u] msg patch <mail >info";
708 static void usage(void) {
709 fprintf(stderr
, "%s\n", mailinfo_usage
);
713 int main(int argc
, char **argv
)
715 while (1 < argc
&& argv
[1][0] == '-') {
716 if (!strcmp(argv
[1], "-k"))
718 else if (!strcmp(argv
[1], "-u"))
727 cmitmsg
= fopen(argv
[1], "w");
732 patchfile
= fopen(argv
[2], "w");
738 int len
= read_one_header_line(line
, sizeof(line
), stdin
);
740 if (multipart_boundary
[0])
741 handle_multipart_body();
746 check_header_line(line
, len
);