2 * Another stupid program, this one parsing the headers of an
3 * email to figure out authorship and subject
13 static FILE *cmitmsg
, *patchfile
;
15 static int keep_subject
= 0;
16 static char *metainfo_charset
= NULL
;
17 static char line
[1000];
18 static char date
[1000];
19 static char name
[1000];
20 static char email
[1000];
21 static char subject
[1000];
24 TE_DONTCARE
, TE_QP
, TE_BASE64
,
26 static char charset
[256];
28 static char multipart_boundary
[1000];
29 static int multipart_boundary_len
;
30 static int patch_lines
= 0;
32 static char *sanity_check(char *name
, char *email
)
34 int len
= strlen(name
);
35 if (len
< 3 || len
> 60)
37 if (strchr(name
, '@') || strchr(name
, '<') || strchr(name
, '>'))
42 static int handle_from(char *line
)
44 char *at
= strchr(line
, '@');
51 * If we already have one email, don't take any confusing lines
53 if (*email
&& strchr(at
+1, '@'))
56 /* Pick up the string around '@', possibly delimited with <>
57 * pair; that is the email part. White them out while copying.
71 unsigned char c
= *at
;
72 if (!c
|| c
== '>' || isspace(c
)) {
82 /* The remainder is name. It could be "John Doe <john.doe@xz>"
83 * or "john.doe@xz (John Doe)", but we have whited out the
84 * email part, so trim from both ends, possibly removing
85 * the () pair at the end.
87 at
= line
+ strlen(line
);
89 unsigned char c
= *--at
;
91 at
[(c
== ')') ? 0 : 1] = 0;
98 unsigned char c
= *at
;
99 if (!c
|| !isspace(c
)) {
106 at
= sanity_check(at
, email
);
111 static int handle_date(char *line
)
117 static int handle_subject(char *line
)
119 strcpy(subject
, line
);
123 /* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
124 * to have enough heuristics to grok MIME encoded patches often found
125 * on our mailing lists. For example, we do not even treat header lines
126 * case insensitively.
129 static int slurp_attr(const char *line
, const char *name
, char *attr
)
131 char *ends
, *ap
= strcasestr(line
, name
);
145 sz
= strcspn(ap
, ends
);
146 memcpy(attr
, ap
, sz
);
151 static int handle_subcontent_type(char *line
)
153 /* We do not want to mess with boundary. Note that we do not
154 * handle nested multipart.
156 if (strcasestr(line
, "boundary=")) {
157 fprintf(stderr
, "Not handling nested multipart message.\n");
160 slurp_attr(line
, "charset=", charset
);
163 for (i
= 0; (c
= charset
[i
]) != 0; i
++)
164 charset
[i
] = tolower(c
);
169 static int handle_content_type(char *line
)
171 *multipart_boundary
= 0;
172 if (slurp_attr(line
, "boundary=", multipart_boundary
+ 2)) {
173 memcpy(multipart_boundary
, "--", 2);
174 multipart_boundary_len
= strlen(multipart_boundary
);
176 slurp_attr(line
, "charset=", charset
);
180 static int handle_content_transfer_encoding(char *line
)
182 if (strcasestr(line
, "base64"))
183 transfer_encoding
= TE_BASE64
;
184 else if (strcasestr(line
, "quoted-printable"))
185 transfer_encoding
= TE_QP
;
187 transfer_encoding
= TE_DONTCARE
;
191 static int is_multipart_boundary(const char *line
)
193 return (!memcmp(line
, multipart_boundary
, multipart_boundary_len
));
196 static int eatspace(char *line
)
198 int len
= strlen(line
);
199 while (len
> 0 && isspace(line
[len
-1]))
206 #define SEEN_SUBJECT 04
208 /* First lines of body can have From:, Date:, and Subject: */
209 static int handle_inbody_header(int *seen
, char *line
)
211 if (!memcmp("From:", line
, 5) && isspace(line
[5])) {
212 if (!(*seen
& SEEN_FROM
) && handle_from(line
+6)) {
217 if (!memcmp("Date:", line
, 5) && isspace(line
[5])) {
218 if (!(*seen
& SEEN_DATE
)) {
224 if (!memcmp("Subject:", line
, 8) && isspace(line
[8])) {
225 if (!(*seen
& SEEN_SUBJECT
)) {
226 handle_subject(line
+9);
227 *seen
|= SEEN_SUBJECT
;
231 if (!memcmp("[PATCH]", line
, 7) && isspace(line
[7])) {
232 if (!(*seen
& SEEN_SUBJECT
)) {
233 handle_subject(line
);
234 *seen
|= SEEN_SUBJECT
;
241 static char *cleanup_subject(char *subject
)
250 if (!memcmp("e:", subject
+1, 2)) {
255 case ' ': case '\t': case ':':
260 p
= strchr(subject
, ']');
266 remove
= p
- subject
;
267 if (remove
<= len
*2) {
277 static void cleanup_space(char *buf
)
280 while ((c
= *buf
) != 0) {
286 int len
= strlen(buf
);
287 memmove(buf
, buf
+1, len
);
294 typedef int (*header_fn_t
)(char *);
301 static void check_header(char *line
, int len
, struct header_def
*header
)
305 if (header
[0].namelen
<= 0) {
306 for (i
= 0; header
[i
].name
; i
++)
307 header
[i
].namelen
= strlen(header
[i
].name
);
309 for (i
= 0; header
[i
].name
; i
++) {
310 int len
= header
[i
].namelen
;
311 if (!strncasecmp(line
, header
[i
].name
, len
) &&
312 line
[len
] == ':' && isspace(line
[len
+ 1])) {
313 header
[i
].func(line
+ len
+ 2);
319 static void check_subheader_line(char *line
, int len
)
321 static struct header_def header
[] = {
322 { "Content-Type", handle_subcontent_type
},
323 { "Content-Transfer-Encoding",
324 handle_content_transfer_encoding
},
327 check_header(line
, len
, header
);
329 static void check_header_line(char *line
, int len
)
331 static struct header_def header
[] = {
332 { "From", handle_from
},
333 { "Date", handle_date
},
334 { "Subject", handle_subject
},
335 { "Content-Type", handle_content_type
},
336 { "Content-Transfer-Encoding",
337 handle_content_transfer_encoding
},
340 check_header(line
, len
, header
);
343 static int read_one_header_line(char *line
, int sz
, FILE *in
)
348 if (fgets(line
+ ofs
, sz
- ofs
, in
) == NULL
)
350 len
= eatspace(line
+ ofs
);
353 peek
= fgetc(in
); ungetc(peek
, in
);
354 if (peek
== ' ' || peek
== '\t') {
355 /* Yuck, 2822 header "folding" */
364 static unsigned hexval(int c
)
366 if (c
>= '0' && c
<= '9')
368 if (c
>= 'a' && c
<= 'f')
370 if (c
>= 'A' && c
<= 'F')
375 static int decode_q_segment(char *in
, char *ot
, char *ep
)
378 while ((c
= *in
++) != 0 && (in
<= ep
)) {
382 break; /* drop trailing newline */
383 *ot
++ = ((hexval(d
) << 4) | hexval(*in
++));
392 static int decode_b_segment(char *in
, char *ot
, char *ep
)
394 /* Decode in..ep, possibly in-place to ot */
395 int c
, pos
= 0, acc
= 0;
397 while ((c
= *in
++) != 0 && (in
<= ep
)) {
402 else if ('A' <= c
&& c
<= 'Z')
404 else if ('a' <= c
&& c
<= 'z')
406 else if ('0' <= c
&& c
<= '9')
409 /* padding is almost like (c == 0), except we do
410 * not output NUL resulting only from it;
411 * for now we just trust the data.
416 continue; /* garbage */
422 *ot
++ = (acc
| (c
>> 4));
426 *ot
++ = (acc
| (c
>> 2));
439 static void convert_to_utf8(char *line
, char *charset
)
442 size_t insize
, outsize
, nrc
;
443 char outbuf
[4096]; /* cheat */
444 static char latin_one
[] = "latin-1";
445 char *input_charset
= *charset
? charset
: latin_one
;
446 iconv_t conv
= iconv_open(metainfo_charset
, input_charset
);
448 if (conv
== (iconv_t
) -1) {
449 static int warned_latin1_once
= 0;
450 if (input_charset
!= latin_one
) {
451 fprintf(stderr
, "cannot convert from %s to %s\n",
452 input_charset
, metainfo_charset
);
455 else if (!warned_latin1_once
) {
456 warned_latin1_once
= 1;
457 fprintf(stderr
, "tried to convert from %s to %s, "
458 "but your iconv does not work with it.\n",
459 input_charset
, metainfo_charset
);
466 outsize
= sizeof(outbuf
);
467 nrc
= iconv(conv
, &in
, &insize
, &out
, &outsize
);
469 if (nrc
== (size_t) -1)
472 strcpy(line
, outbuf
);
475 static void decode_header_bq(char *it
)
477 char *in
, *out
, *ep
, *cp
, *sp
;
482 while ((ep
= strstr(in
, "=?")) != NULL
) {
484 char charset_q
[256], piecebuf
[256];
492 * ep : "=?iso-2022-jp?B?GyR...?= foo"
493 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
496 cp
= strchr(ep
, '?');
498 return; /* no munging */
499 for (sp
= ep
; sp
< cp
; sp
++)
500 charset_q
[sp
- ep
] = tolower(*sp
);
501 charset_q
[cp
- ep
] = 0;
503 if (!encoding
|| cp
[2] != '?')
504 return; /* no munging */
505 ep
= strstr(cp
+ 3, "?=");
507 return; /* no munging */
508 switch (tolower(encoding
)) {
510 return; /* no munging */
512 sz
= decode_b_segment(cp
+ 3, piecebuf
, ep
);
515 sz
= decode_q_segment(cp
+ 3, piecebuf
, ep
);
520 if (metainfo_charset
)
521 convert_to_utf8(piecebuf
, charset_q
);
522 strcpy(out
, piecebuf
);
530 static void decode_transfer_encoding(char *line
)
534 switch (transfer_encoding
) {
536 ep
= line
+ strlen(line
);
537 decode_q_segment(line
, line
, ep
);
540 ep
= line
+ strlen(line
);
541 decode_b_segment(line
, line
, ep
);
548 static void handle_info(void)
551 static int done_info
= 0;
557 sub
= cleanup_subject(subject
);
560 cleanup_space(email
);
563 /* Unwrap inline B and Q encoding, and optionally
564 * normalize the meta information to utf8.
566 decode_header_bq(name
);
567 decode_header_bq(date
);
568 decode_header_bq(email
);
569 decode_header_bq(sub
);
570 printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
571 name
, email
, sub
, date
);
574 /* We are inside message body and have read line[] already.
575 * Spit out the commit log.
577 static int handle_commit_msg(void)
582 if (!memcmp("diff -", line
, 6) ||
583 !memcmp("---", line
, 3) ||
584 !memcmp("Index: ", line
, 7))
586 if ((multipart_boundary
[0] && is_multipart_boundary(line
))) {
587 /* We come here when the first part had only
588 * the commit message without any patch. We
589 * pretend we have not seen this line yet, and
590 * go back to the loop.
595 /* Unwrap transfer encoding and optionally
596 * normalize the log message to UTF-8.
598 decode_transfer_encoding(line
);
599 if (metainfo_charset
)
600 convert_to_utf8(line
, charset
);
601 fputs(line
, cmitmsg
);
602 } while (fgets(line
, sizeof(line
), stdin
) != NULL
);
608 /* We have done the commit message and have the first
609 * line of the patch in line[].
611 static void handle_patch(void)
614 if (multipart_boundary
[0] && is_multipart_boundary(line
))
616 /* Only unwrap transfer encoding but otherwise do not
617 * do anything. We do *NOT* want UTF-8 conversion
618 * here; we are dealing with the user payload.
620 decode_transfer_encoding(line
);
621 fputs(line
, patchfile
);
623 } while (fgets(line
, sizeof(line
), stdin
) != NULL
);
626 /* multipart boundary and transfer encoding are set up for us, and we
627 * are at the end of the sub header. do equivalent of handle_body up
628 * to the next boundary without closing patchfile --- we will expect
629 * that the first part to contain commit message and a patch, and
630 * handle other parts as pure patches.
632 static int handle_multipart_one_part(void)
638 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
640 len
= eatspace(line
);
644 if (is_multipart_boundary(line
))
646 if (0 <= seen
&& handle_inbody_header(&seen
, line
))
648 seen
= -1; /* no more inbody headers */
651 if (handle_commit_msg())
661 static void handle_multipart_body(void)
665 /* Skip up to the first boundary */
666 while (fgets(line
, sizeof(line
), stdin
) != NULL
)
667 if (is_multipart_boundary(line
)) {
673 /* We are on boundary line. Start slurping the subhead. */
675 int len
= read_one_header_line(line
, sizeof(line
), stdin
);
677 if (handle_multipart_one_part() < 0)
681 check_subheader_line(line
, len
);
685 fprintf(stderr
, "No patch found\n");
690 /* Non multipart message */
691 static void handle_body(void)
695 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
696 int len
= eatspace(line
);
699 if (0 <= seen
&& handle_inbody_header(&seen
, line
))
701 seen
= -1; /* no more inbody headers */
710 fprintf(stderr
, "No patch found\n");
715 static const char mailinfo_usage
[] =
716 "git-mailinfo [-k] [-u | --encoding=<encoding>] msg patch <mail >info";
718 int main(int argc
, char **argv
)
720 /* NEEDSWORK: might want to do the optional .git/ directory
723 git_config(git_default_config
);
725 while (1 < argc
&& argv
[1][0] == '-') {
726 if (!strcmp(argv
[1], "-k"))
728 else if (!strcmp(argv
[1], "-u"))
729 metainfo_charset
= git_commit_encoding
;
730 else if (!strncmp(argv
[1], "--encoding=", 11))
731 metainfo_charset
= argv
[1] + 11;
733 usage(mailinfo_usage
);
738 usage(mailinfo_usage
);
739 cmitmsg
= fopen(argv
[1], "w");
744 patchfile
= fopen(argv
[2], "w");
750 int len
= read_one_header_line(line
, sizeof(line
), stdin
);
752 if (multipart_boundary
[0])
753 handle_multipart_body();
758 check_header_line(line
, len
);