2 * Another stupid program, this one parsing the headers of an
3 * email to figure out authorship and subject
9 static FILE *cmitmsg
, *patchfile
, *fin
, *fout
;
11 static int keep_subject
;
12 static const char *metainfo_charset
;
13 static char line
[1000];
14 static char date
[1000];
15 static char name
[1000];
16 static char email
[1000];
17 static char subject
[1000];
20 TE_DONTCARE
, TE_QP
, TE_BASE64
,
22 static char charset
[256];
24 static char multipart_boundary
[1000];
25 static int multipart_boundary_len
;
26 static int patch_lines
;
28 static char *sanity_check(char *name
, char *email
)
30 int len
= strlen(name
);
31 if (len
< 3 || len
> 60)
33 if (strchr(name
, '@') || strchr(name
, '<') || strchr(name
, '>'))
38 static int bogus_from(char *line
)
40 /* John Doe <johndoe> */
41 char *bra
, *ket
, *dst
, *cp
;
43 /* This is fallback, so do not bother if we already have an
49 bra
= strchr(line
, '<');
52 ket
= strchr(bra
, '>');
56 for (dst
= email
, cp
= bra
+1; cp
< ket
; )
59 for (cp
= line
; isspace(*cp
); cp
++)
61 for (bra
--; isspace(*bra
); bra
--)
63 cp
= sanity_check(cp
, email
);
68 static int handle_from(char *in_line
)
74 strcpy(line
, in_line
);
75 at
= strchr(line
, '@');
77 return bogus_from(line
);
80 * If we already have one email, don't take any confusing lines
82 if (*email
&& strchr(at
+1, '@'))
85 /* Pick up the string around '@', possibly delimited with <>
86 * pair; that is the email part. White them out while copying.
100 unsigned char c
= *at
;
101 if (!c
|| c
== '>' || isspace(c
)) {
111 /* The remainder is name. It could be "John Doe <john.doe@xz>"
112 * or "john.doe@xz (John Doe)", but we have whited out the
113 * email part, so trim from both ends, possibly removing
114 * the () pair at the end.
116 at
= line
+ strlen(line
);
118 unsigned char c
= *--at
;
120 at
[(c
== ')') ? 0 : 1] = 0;
127 unsigned char c
= *at
;
128 if (!c
|| !isspace(c
)) {
135 at
= sanity_check(at
, email
);
140 static int handle_date(char *line
)
146 static int handle_subject(char *line
)
148 strcpy(subject
, line
);
152 /* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
153 * to have enough heuristics to grok MIME encoded patches often found
154 * on our mailing lists. For example, we do not even treat header lines
155 * case insensitively.
158 static int slurp_attr(const char *line
, const char *name
, char *attr
)
160 const char *ends
, *ap
= strcasestr(line
, name
);
174 sz
= strcspn(ap
, ends
);
175 memcpy(attr
, ap
, sz
);
180 static int handle_subcontent_type(char *line
)
182 /* We do not want to mess with boundary. Note that we do not
183 * handle nested multipart.
185 if (strcasestr(line
, "boundary=")) {
186 fprintf(stderr
, "Not handling nested multipart message.\n");
189 slurp_attr(line
, "charset=", charset
);
192 for (i
= 0; (c
= charset
[i
]) != 0; i
++)
193 charset
[i
] = tolower(c
);
198 static int handle_content_type(char *line
)
200 *multipart_boundary
= 0;
201 if (slurp_attr(line
, "boundary=", multipart_boundary
+ 2)) {
202 memcpy(multipart_boundary
, "--", 2);
203 multipart_boundary_len
= strlen(multipart_boundary
);
205 slurp_attr(line
, "charset=", charset
);
209 static int handle_content_transfer_encoding(char *line
)
211 if (strcasestr(line
, "base64"))
212 transfer_encoding
= TE_BASE64
;
213 else if (strcasestr(line
, "quoted-printable"))
214 transfer_encoding
= TE_QP
;
216 transfer_encoding
= TE_DONTCARE
;
220 static int is_multipart_boundary(const char *line
)
222 return (!memcmp(line
, multipart_boundary
, multipart_boundary_len
));
225 static int eatspace(char *line
)
227 int len
= strlen(line
);
228 while (len
> 0 && isspace(line
[len
-1]))
235 #define SEEN_SUBJECT 04
236 #define SEEN_BOGUS_UNIX_FROM 010
237 #define SEEN_PREFIX 020
239 /* First lines of body can have From:, Date:, and Subject: or empty */
240 static void handle_inbody_header(int *seen
, char *line
)
242 if (*seen
& SEEN_PREFIX
)
244 if (isspace(*line
)) {
246 for (cp
= line
+ 1; *cp
; cp
++) {
253 if (!memcmp(">From", line
, 5) && isspace(line
[5])) {
254 if (!(*seen
& SEEN_BOGUS_UNIX_FROM
)) {
255 *seen
|= SEEN_BOGUS_UNIX_FROM
;
259 if (!memcmp("From:", line
, 5) && isspace(line
[5])) {
260 if (!(*seen
& SEEN_FROM
) && handle_from(line
+6)) {
265 if (!memcmp("Date:", line
, 5) && isspace(line
[5])) {
266 if (!(*seen
& SEEN_DATE
)) {
272 if (!memcmp("Subject:", line
, 8) && isspace(line
[8])) {
273 if (!(*seen
& SEEN_SUBJECT
)) {
274 handle_subject(line
+9);
275 *seen
|= SEEN_SUBJECT
;
279 if (!memcmp("[PATCH]", line
, 7) && isspace(line
[7])) {
280 if (!(*seen
& SEEN_SUBJECT
)) {
281 handle_subject(line
);
282 *seen
|= SEEN_SUBJECT
;
286 *seen
|= SEEN_PREFIX
;
289 static char *cleanup_subject(char *subject
)
298 if (!memcmp("e:", subject
+1, 2)) {
303 case ' ': case '\t': case ':':
308 p
= strchr(subject
, ']');
314 remove
= p
- subject
;
315 if (remove
<= len
*2) {
326 static void cleanup_space(char *buf
)
329 while ((c
= *buf
) != 0) {
335 int len
= strlen(buf
);
336 memmove(buf
, buf
+1, len
);
343 static void decode_header(char *it
);
344 typedef int (*header_fn_t
)(char *);
351 static void check_header(char *line
, struct header_def
*header
)
355 if (header
[0].namelen
<= 0) {
356 for (i
= 0; header
[i
].name
; i
++)
357 header
[i
].namelen
= strlen(header
[i
].name
);
359 for (i
= 0; header
[i
].name
; i
++) {
360 int len
= header
[i
].namelen
;
361 if (!strncasecmp(line
, header
[i
].name
, len
) &&
362 line
[len
] == ':' && isspace(line
[len
+ 1])) {
363 /* Unwrap inline B and Q encoding, and optionally
364 * normalize the meta information to utf8.
366 decode_header(line
+ len
+ 2);
367 header
[i
].func(line
+ len
+ 2);
373 static void check_subheader_line(char *line
)
375 static struct header_def header
[] = {
376 { "Content-Type", handle_subcontent_type
},
377 { "Content-Transfer-Encoding",
378 handle_content_transfer_encoding
},
381 check_header(line
, header
);
383 static void check_header_line(char *line
)
385 static struct header_def header
[] = {
386 { "From", handle_from
},
387 { "Date", handle_date
},
388 { "Subject", handle_subject
},
389 { "Content-Type", handle_content_type
},
390 { "Content-Transfer-Encoding",
391 handle_content_transfer_encoding
},
394 check_header(line
, header
);
397 static int is_rfc2822_header(char *line
)
400 * The section that defines the loosest possible
401 * field name is "3.6.8 Optional fields".
403 * optional-field = field-name ":" unstructured CRLF
404 * field-name = 1*ftext
405 * ftext = %d33-57 / %59-126
410 /* Count mbox From headers as headers */
411 if (!memcmp(line
, "From ", 5) || !memcmp(line
, ">From ", 6))
414 while ((ch
= *cp
++)) {
417 if ((33 <= ch
&& ch
<= 57) ||
418 (59 <= ch
&& ch
<= 126))
426 * sz is size of 'line' buffer in bytes. Must be reasonably
427 * long enough to hold one physical real-world e-mail line.
429 static int read_one_header_line(char *line
, int sz
, FILE *in
)
434 * We will read at most (sz-1) bytes and then potentially
435 * re-add NUL after it. Accessing line[sz] after this is safe
436 * and we can allow len to grow up to and including sz.
440 /* Get the first part of the line. */
441 if (!fgets(line
, sz
, in
))
445 * Is it an empty line or not a valid rfc2822 header?
446 * If so, stop here, and return false ("not a header")
448 len
= eatspace(line
);
449 if (!len
|| !is_rfc2822_header(line
)) {
450 /* Re-add the newline */
452 line
[len
+ 1] = '\0';
457 * Now we need to eat all the continuation lines..
458 * Yuck, 2822 header "folding"
462 static char continuation
[1000];
464 peek
= fgetc(in
); ungetc(peek
, in
);
465 if (peek
!= ' ' && peek
!= '\t')
467 if (!fgets(continuation
, sizeof(continuation
), in
))
469 addlen
= eatspace(continuation
);
471 if (addlen
>= sz
- len
)
472 addlen
= sz
- len
- 1;
473 memcpy(line
+ len
, continuation
, addlen
);
482 static int decode_q_segment(char *in
, char *ot
, char *ep
, int rfc2047
)
485 while ((c
= *in
++) != 0 && (in
<= ep
)) {
489 break; /* drop trailing newline */
490 *ot
++ = ((hexval(d
) << 4) | hexval(*in
++));
493 if (rfc2047
&& c
== '_') /* rfc2047 4.2 (2) */
501 static int decode_b_segment(char *in
, char *ot
, char *ep
)
503 /* Decode in..ep, possibly in-place to ot */
504 int c
, pos
= 0, acc
= 0;
506 while ((c
= *in
++) != 0 && (in
<= ep
)) {
511 else if ('A' <= c
&& c
<= 'Z')
513 else if ('a' <= c
&& c
<= 'z')
515 else if ('0' <= c
&& c
<= '9')
518 /* padding is almost like (c == 0), except we do
519 * not output NUL resulting only from it;
520 * for now we just trust the data.
525 continue; /* garbage */
531 *ot
++ = (acc
| (c
>> 4));
535 *ot
++ = (acc
| (c
>> 2));
548 static void convert_to_utf8(char *line
, const char *charset
)
550 static const char latin_one
[] = "latin1";
551 const char *input_charset
= *charset
? charset
: latin_one
;
552 char *out
= reencode_string(line
, metainfo_charset
, input_charset
);
555 die("cannot convert from %s to %s\n",
556 input_charset
, metainfo_charset
);
561 static int decode_header_bq(char *it
)
563 char *in
, *out
, *ep
, *cp
, *sp
;
569 while ((ep
= strstr(in
, "=?")) != NULL
) {
571 char charset_q
[256], piecebuf
[256];
581 * ep : "=?iso-2022-jp?B?GyR...?= foo"
582 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
585 cp
= strchr(ep
, '?');
587 return rfc2047
; /* no munging */
588 for (sp
= ep
; sp
< cp
; sp
++)
589 charset_q
[sp
- ep
] = tolower(*sp
);
590 charset_q
[cp
- ep
] = 0;
592 if (!encoding
|| cp
[2] != '?')
593 return rfc2047
; /* no munging */
594 ep
= strstr(cp
+ 3, "?=");
596 return rfc2047
; /* no munging */
597 switch (tolower(encoding
)) {
599 return rfc2047
; /* no munging */
601 sz
= decode_b_segment(cp
+ 3, piecebuf
, ep
);
604 sz
= decode_q_segment(cp
+ 3, piecebuf
, ep
, 1);
609 if (metainfo_charset
)
610 convert_to_utf8(piecebuf
, charset_q
);
611 strcpy(out
, piecebuf
);
620 static void decode_header(char *it
)
623 if (decode_header_bq(it
))
625 /* otherwise "it" is a straight copy of the input.
626 * This can be binary guck but there is no charset specified.
628 if (metainfo_charset
)
629 convert_to_utf8(it
, "");
632 static void decode_transfer_encoding(char *line
)
636 switch (transfer_encoding
) {
638 ep
= line
+ strlen(line
);
639 decode_q_segment(line
, line
, ep
, 0);
642 ep
= line
+ strlen(line
);
643 decode_b_segment(line
, line
, ep
);
650 static void handle_info(void)
654 sub
= cleanup_subject(subject
);
657 cleanup_space(email
);
660 fprintf(fout
, "Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
661 name
, email
, sub
, date
);
664 /* We are inside message body and have read line[] already.
665 * Spit out the commit log.
667 static int handle_commit_msg(int *seen
)
672 if (!memcmp("diff -", line
, 6) ||
673 !memcmp("---", line
, 3) ||
674 !memcmp("Index: ", line
, 7))
676 if ((multipart_boundary
[0] && is_multipart_boundary(line
))) {
677 /* We come here when the first part had only
678 * the commit message without any patch. We
679 * pretend we have not seen this line yet, and
680 * go back to the loop.
685 /* Unwrap transfer encoding and optionally
686 * normalize the log message to UTF-8.
688 decode_transfer_encoding(line
);
689 if (metainfo_charset
)
690 convert_to_utf8(line
, charset
);
692 handle_inbody_header(seen
, line
);
693 if (!(*seen
& SEEN_PREFIX
))
696 fputs(line
, cmitmsg
);
697 } while (fgets(line
, sizeof(line
), fin
) != NULL
);
703 /* We have done the commit message and have the first
704 * line of the patch in line[].
706 static void handle_patch(void)
709 if (multipart_boundary
[0] && is_multipart_boundary(line
))
711 /* Only unwrap transfer encoding but otherwise do not
712 * do anything. We do *NOT* want UTF-8 conversion
713 * here; we are dealing with the user payload.
715 decode_transfer_encoding(line
);
716 fputs(line
, patchfile
);
718 } while (fgets(line
, sizeof(line
), fin
) != NULL
);
721 /* multipart boundary and transfer encoding are set up for us, and we
722 * are at the end of the sub header. do equivalent of handle_body up
723 * to the next boundary without closing patchfile --- we will expect
724 * that the first part to contain commit message and a patch, and
725 * handle other parts as pure patches.
727 static int handle_multipart_one_part(int *seen
)
731 while (fgets(line
, sizeof(line
), fin
) != NULL
) {
734 if (is_multipart_boundary(line
))
736 if (handle_commit_msg(seen
))
746 static void handle_multipart_body(void)
751 /* Skip up to the first boundary */
752 while (fgets(line
, sizeof(line
), fin
) != NULL
)
753 if (is_multipart_boundary(line
)) {
759 /* We are on boundary line. Start slurping the subhead. */
761 int hdr
= read_one_header_line(line
, sizeof(line
), fin
);
763 if (handle_multipart_one_part(&seen
) < 0)
765 /* Reset per part headers */
766 transfer_encoding
= TE_DONTCARE
;
770 check_subheader_line(line
);
774 fprintf(stderr
, "No patch found\n");
779 /* Non multipart message */
780 static void handle_body(void)
784 handle_commit_msg(&seen
);
788 fprintf(stderr
, "No patch found\n");
793 int mailinfo(FILE *in
, FILE *out
, int ks
, const char *encoding
,
794 const char *msg
, const char *patch
)
797 metainfo_charset
= encoding
;
801 cmitmsg
= fopen(msg
, "w");
806 patchfile
= fopen(patch
, "w");
813 int hdr
= read_one_header_line(line
, sizeof(line
), fin
);
815 if (multipart_boundary
[0])
816 handle_multipart_body();
822 check_header_line(line
);
828 static const char mailinfo_usage
[] =
829 "git-mailinfo [-k] [-u | --encoding=<encoding>] msg patch <mail >info";
831 int cmd_mailinfo(int argc
, const char **argv
, const char *prefix
)
833 const char *def_charset
;
835 /* NEEDSWORK: might want to do the optional .git/ directory
838 git_config(git_default_config
);
840 def_charset
= (git_commit_encoding
? git_commit_encoding
: "utf-8");
841 metainfo_charset
= def_charset
;
843 while (1 < argc
&& argv
[1][0] == '-') {
844 if (!strcmp(argv
[1], "-k"))
846 else if (!strcmp(argv
[1], "-u"))
847 metainfo_charset
= def_charset
;
848 else if (!strcmp(argv
[1], "-n"))
849 metainfo_charset
= NULL
;
850 else if (!prefixcmp(argv
[1], "--encoding="))
851 metainfo_charset
= argv
[1] + 11;
853 usage(mailinfo_usage
);
858 usage(mailinfo_usage
);
860 return !!mailinfo(stdin
, stdout
, keep_subject
, metainfo_charset
, argv
[1], argv
[2]);