7 static void cleanup_space(struct strbuf
*sb
)
10 for (pos
= 0; pos
< sb
->len
; pos
++) {
11 if (isspace(sb
->buf
[pos
])) {
13 for (cnt
= 0; isspace(sb
->buf
[pos
+ cnt
+ 1]); cnt
++);
14 strbuf_remove(sb
, pos
+ 1, cnt
);
19 static void get_sane_name(struct strbuf
*out
, struct strbuf
*name
, struct strbuf
*email
)
21 struct strbuf
*src
= name
;
22 if (!name
->len
|| 60 < name
->len
|| strpbrk(name
->buf
, "@<>"))
27 strbuf_addbuf(out
, src
);
30 static void parse_bogus_from(struct mailinfo
*mi
, const struct strbuf
*line
)
32 /* John Doe <johndoe> */
35 /* This is fallback, so do not bother if we already have an
41 bra
= strchr(line
->buf
, '<');
44 ket
= strchr(bra
, '>');
48 strbuf_reset(&mi
->email
);
49 strbuf_add(&mi
->email
, bra
+ 1, ket
- bra
- 1);
51 strbuf_reset(&mi
->name
);
52 strbuf_add(&mi
->name
, line
->buf
, bra
- line
->buf
);
53 strbuf_trim(&mi
->name
);
54 get_sane_name(&mi
->name
, &mi
->name
, &mi
->email
);
57 static const char *unquote_comment(struct strbuf
*outbuf
, const char *in
)
60 int take_next_literally
= 0;
62 strbuf_addch(outbuf
, '(');
64 while ((c
= *in
++) != 0) {
65 if (take_next_literally
== 1) {
66 take_next_literally
= 0;
70 take_next_literally
= 1;
73 in
= unquote_comment(outbuf
, in
);
76 strbuf_addch(outbuf
, ')');
81 strbuf_addch(outbuf
, c
);
87 static const char *unquote_quoted_string(struct strbuf
*outbuf
, const char *in
)
90 int take_next_literally
= 0;
92 while ((c
= *in
++) != 0) {
93 if (take_next_literally
== 1) {
94 take_next_literally
= 0;
98 take_next_literally
= 1;
105 strbuf_addch(outbuf
, c
);
111 static void unquote_quoted_pair(struct strbuf
*line
)
113 struct strbuf outbuf
;
114 const char *in
= line
->buf
;
117 strbuf_init(&outbuf
, line
->len
);
119 while ((c
= *in
++) != 0) {
122 in
= unquote_quoted_string(&outbuf
, in
);
125 in
= unquote_comment(&outbuf
, in
);
129 strbuf_addch(&outbuf
, c
);
132 strbuf_swap(&outbuf
, line
);
133 strbuf_release(&outbuf
);
137 static void handle_from(struct mailinfo
*mi
, const struct strbuf
*from
)
143 strbuf_init(&f
, from
->len
);
144 strbuf_addbuf(&f
, from
);
146 unquote_quoted_pair(&f
);
148 at
= strchr(f
.buf
, '@');
150 parse_bogus_from(mi
, from
);
155 * If we already have one email, don't take any confusing lines
157 if (mi
->email
.len
&& strchr(at
+ 1, '@'))
160 /* Pick up the string around '@', possibly delimited with <>
161 * pair; that is the email part.
173 el
= strcspn(at
, " \n\t\r\v\f>");
174 strbuf_reset(&mi
->email
);
175 strbuf_add(&mi
->email
, at
, el
);
176 strbuf_remove(&f
, at
- f
.buf
, el
+ (at
[el
] ? 1 : 0));
178 /* The remainder is name. It could be
180 * - "John Doe <john.doe@xz>" (a), or
181 * - "john.doe@xz (John Doe)" (b), or
182 * - "John (zzz) Doe <john.doe@xz> (Comment)" (c)
184 * but we have removed the email part, so
186 * - remove extra spaces which could stay after email (case 'c'), and
187 * - trim from both ends, possibly removing the () pair at the end
188 * (cases 'a' and 'b').
192 if (f
.buf
[0] == '(' && f
.len
&& f
.buf
[f
.len
- 1] == ')') {
193 strbuf_remove(&f
, 0, 1);
194 strbuf_setlen(&f
, f
.len
- 1);
197 get_sane_name(&mi
->name
, &f
, &mi
->email
);
202 static void handle_header(struct strbuf
**out
, const struct strbuf
*line
)
205 *out
= xmalloc(sizeof(struct strbuf
));
206 strbuf_init(*out
, line
->len
);
210 strbuf_addbuf(*out
, line
);
213 /* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
214 * to have enough heuristics to grok MIME encoded patches often found
215 * on our mailing lists. For example, we do not even treat header lines
216 * case insensitively.
219 static int slurp_attr(const char *line
, const char *name
, struct strbuf
*attr
)
221 const char *ends
, *ap
= strcasestr(line
, name
);
224 strbuf_setlen(attr
, 0);
234 sz
= strcspn(ap
, ends
);
235 strbuf_add(attr
, ap
, sz
);
239 static int has_attr_value(const char *line
, const char *name
, const char *value
)
241 struct strbuf sb
= STRBUF_INIT
;
242 int rc
= slurp_attr(line
, name
, &sb
) && !strcasecmp(sb
.buf
, value
);
247 static void handle_content_type(struct mailinfo
*mi
, struct strbuf
*line
)
249 struct strbuf
*boundary
= xmalloc(sizeof(struct strbuf
));
250 strbuf_init(boundary
, line
->len
);
252 mi
->format_flowed
= has_attr_value(line
->buf
, "format=", "flowed");
253 mi
->delsp
= has_attr_value(line
->buf
, "delsp=", "yes");
255 if (slurp_attr(line
->buf
, "boundary=", boundary
)) {
256 strbuf_insertstr(boundary
, 0, "--");
257 if (++mi
->content_top
>= &mi
->content
[MAX_BOUNDARIES
]) {
258 error("Too many boundaries to handle");
259 mi
->input_error
= -1;
260 mi
->content_top
= &mi
->content
[MAX_BOUNDARIES
] - 1;
263 *(mi
->content_top
) = boundary
;
266 slurp_attr(line
->buf
, "charset=", &mi
->charset
);
269 strbuf_release(boundary
);
274 static void handle_content_transfer_encoding(struct mailinfo
*mi
,
275 const struct strbuf
*line
)
277 if (strcasestr(line
->buf
, "base64"))
278 mi
->transfer_encoding
= TE_BASE64
;
279 else if (strcasestr(line
->buf
, "quoted-printable"))
280 mi
->transfer_encoding
= TE_QP
;
282 mi
->transfer_encoding
= TE_DONTCARE
;
285 static int is_multipart_boundary(struct mailinfo
*mi
, const struct strbuf
*line
)
287 struct strbuf
*content_top
= *(mi
->content_top
);
289 return ((content_top
->len
<= line
->len
) &&
290 !memcmp(line
->buf
, content_top
->buf
, content_top
->len
));
293 static void cleanup_subject(struct mailinfo
*mi
, struct strbuf
*subject
)
297 while (at
< subject
->len
) {
301 switch (subject
->buf
[at
]) {
303 if (subject
->len
<= at
+ 3)
305 if ((subject
->buf
[at
+ 1] == 'e' ||
306 subject
->buf
[at
+ 1] == 'E') &&
307 subject
->buf
[at
+ 2] == ':') {
308 strbuf_remove(subject
, at
, 3);
313 case ' ': case '\t': case ':':
314 strbuf_remove(subject
, at
, 1);
317 pos
= strchr(subject
->buf
+ at
, ']');
320 remove
= pos
- subject
->buf
+ at
+ 1;
321 if (!mi
->keep_non_patch_brackets_in_subject
||
323 memmem(subject
->buf
+ at
, remove
, "PATCH", 5)))
324 strbuf_remove(subject
, at
, remove
);
328 * If the input had a space after the ], keep
329 * it. We don't bother with finding the end of
330 * the space, since we later normalize it
333 if (isspace(subject
->buf
[at
]))
340 strbuf_trim(subject
);
343 #define MAX_HDR_PARSED 10
344 static const char *header
[MAX_HDR_PARSED
] = {
345 "From","Subject","Date",
348 static inline int skip_header(const struct strbuf
*line
, const char *hdr
,
352 if (!skip_iprefix(line
->buf
, hdr
, &val
) ||
355 while (isspace(*val
))
361 static int is_format_patch_separator(const char *line
, int len
)
363 static const char SAMPLE
[] =
364 "From e6807f3efca28b30decfecb1732a56c7db1137ee Mon Sep 17 00:00:00 2001\n";
367 if (len
!= strlen(SAMPLE
))
369 if (!skip_prefix(line
, "From ", &cp
))
371 if (strspn(cp
, "0123456789abcdef") != 40)
374 return !memcmp(SAMPLE
+ (cp
- line
), cp
, strlen(SAMPLE
) - (cp
- line
));
377 static struct strbuf
*decode_q_segment(const struct strbuf
*q_seg
, int rfc2047
)
379 const char *in
= q_seg
->buf
;
381 struct strbuf
*out
= xmalloc(sizeof(struct strbuf
));
382 strbuf_init(out
, q_seg
->len
);
384 while ((c
= *in
++) != 0) {
388 break; /* drop trailing newline */
391 strbuf_addch(out
, ch
);
395 /* garbage -- fall through */
397 if (rfc2047
&& c
== '_') /* rfc2047 4.2 (2) */
399 strbuf_addch(out
, c
);
404 static struct strbuf
*decode_b_segment(const struct strbuf
*b_seg
)
406 /* Decode in..ep, possibly in-place to ot */
407 int c
, pos
= 0, acc
= 0;
408 const char *in
= b_seg
->buf
;
409 struct strbuf
*out
= xmalloc(sizeof(struct strbuf
));
410 strbuf_init(out
, b_seg
->len
);
412 while ((c
= *in
++) != 0) {
417 else if ('A' <= c
&& c
<= 'Z')
419 else if ('a' <= c
&& c
<= 'z')
421 else if ('0' <= c
&& c
<= '9')
424 continue; /* garbage */
430 strbuf_addch(out
, (acc
| (c
>> 4)));
434 strbuf_addch(out
, (acc
| (c
>> 2)));
438 strbuf_addch(out
, (acc
| c
));
446 static int convert_to_utf8(struct mailinfo
*mi
,
447 struct strbuf
*line
, const char *charset
)
452 if (!mi
->metainfo_charset
|| !charset
|| !*charset
)
455 if (same_encoding(mi
->metainfo_charset
, charset
))
457 out
= reencode_string_len(line
->buf
, line
->len
,
458 mi
->metainfo_charset
, charset
, &out_len
);
460 mi
->input_error
= -1;
461 return error("cannot convert from %s to %s",
462 charset
, mi
->metainfo_charset
);
464 strbuf_attach(line
, out
, out_len
, out_len
);
468 static void decode_header(struct mailinfo
*mi
, struct strbuf
*it
)
471 struct strbuf outbuf
= STRBUF_INIT
, *dec
;
472 struct strbuf charset_q
= STRBUF_INIT
, piecebuf
= STRBUF_INIT
;
473 int found_error
= 1; /* pessimism */
476 while (in
- it
->buf
<= it
->len
&& (ep
= strstr(in
, "=?")) != NULL
) {
478 strbuf_reset(&charset_q
);
479 strbuf_reset(&piecebuf
);
483 * We are about to process an encoded-word
484 * that begins at ep, but there is something
485 * before the encoded word.
488 for (scan
= in
; scan
< ep
; scan
++)
492 if (scan
!= ep
|| in
== it
->buf
) {
494 * We should not lose that "something",
495 * unless we have just processed an
496 * encoded-word, and there is only LWS
497 * before the one we are about to process.
499 strbuf_add(&outbuf
, in
, ep
- in
);
503 * ep : "=?iso-2022-jp?B?GyR...?= foo"
504 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
508 if (ep
- it
->buf
>= it
->len
|| !(cp
= strchr(ep
, '?')))
511 if (cp
+ 3 - it
->buf
> it
->len
)
513 strbuf_add(&charset_q
, ep
, cp
- ep
);
516 if (!encoding
|| cp
[2] != '?')
518 ep
= strstr(cp
+ 3, "?=");
521 strbuf_add(&piecebuf
, cp
+ 3, ep
- cp
- 3);
522 switch (tolower(encoding
)) {
526 dec
= decode_b_segment(&piecebuf
);
529 dec
= decode_q_segment(&piecebuf
, 1);
532 if (convert_to_utf8(mi
, dec
, charset_q
.buf
))
535 strbuf_addbuf(&outbuf
, dec
);
540 strbuf_addstr(&outbuf
, in
);
542 strbuf_addbuf(it
, &outbuf
);
545 strbuf_release(&outbuf
);
546 strbuf_release(&charset_q
);
547 strbuf_release(&piecebuf
);
550 mi
->input_error
= -1;
554 * Returns true if "line" contains a header matching "hdr", in which case "val"
555 * will contain the value of the header with any RFC2047 B and Q encoding
556 * unwrapped, and optionally normalize the meta information to utf8.
558 static int parse_header(const struct strbuf
*line
,
565 if (!skip_header(line
, hdr
, &val_str
))
567 strbuf_addstr(val
, val_str
);
568 decode_header(mi
, val
);
572 static int check_header(struct mailinfo
*mi
,
573 const struct strbuf
*line
,
574 struct strbuf
*hdr_data
[], int overwrite
)
577 struct strbuf sb
= STRBUF_INIT
;
579 /* search for the interesting parts */
580 for (i
= 0; header
[i
]; i
++) {
581 if ((!hdr_data
[i
] || overwrite
) &&
582 parse_header(line
, header
[i
], mi
, &sb
)) {
583 handle_header(&hdr_data
[i
], &sb
);
585 goto check_header_out
;
590 if (parse_header(line
, "Content-Type", mi
, &sb
)) {
591 handle_content_type(mi
, &sb
);
593 goto check_header_out
;
595 if (parse_header(line
, "Content-Transfer-Encoding", mi
, &sb
)) {
596 handle_content_transfer_encoding(mi
, &sb
);
598 goto check_header_out
;
600 if (parse_header(line
, "Message-Id", mi
, &sb
)) {
601 if (mi
->add_message_id
)
602 mi
->message_id
= strbuf_detach(&sb
, NULL
);
604 goto check_header_out
;
613 * Returns 1 if the given line or any line beginning with the given line is an
614 * in-body header (that is, check_header will succeed when passed
617 static int is_inbody_header(const struct mailinfo
*mi
,
618 const struct strbuf
*line
)
622 for (i
= 0; header
[i
]; i
++)
623 if (!mi
->s_hdr_data
[i
] && skip_header(line
, header
[i
], &val
))
628 static void decode_transfer_encoding(struct mailinfo
*mi
, struct strbuf
*line
)
632 switch (mi
->transfer_encoding
) {
634 ret
= decode_q_segment(line
, 0);
637 ret
= decode_b_segment(line
);
644 strbuf_addbuf(line
, ret
);
649 static inline int patchbreak(const struct strbuf
*line
)
653 /* Beginning of a "diff -" header? */
654 if (starts_with(line
->buf
, "diff -"))
657 /* CVS "Index: " line? */
658 if (starts_with(line
->buf
, "Index: "))
662 * "--- <filename>" starts patches without headers
663 * "---<sp>*" is a manual separator
668 if (starts_with(line
->buf
, "---")) {
669 /* space followed by a filename? */
670 if (line
->buf
[3] == ' ' && !isspace(line
->buf
[4]))
672 /* Just whitespace? */
673 for (i
= 3; i
< line
->len
; i
++) {
674 unsigned char c
= line
->buf
[i
];
685 static int is_scissors_line(const char *line
)
688 int scissors
= 0, gap
= 0;
689 const char *first_nonblank
= NULL
, *last_nonblank
= NULL
;
690 int visible
, perforation
= 0, in_perforation
= 0;
692 for (c
= line
; *c
; c
++) {
694 if (in_perforation
) {
701 if (first_nonblank
== NULL
)
708 if (starts_with(c
, ">8") || starts_with(c
, "8<") ||
709 starts_with(c
, ">%") || starts_with(c
, "%<")) {
720 * The mark must be at least 8 bytes long (e.g. "-- >8 --").
721 * Even though there can be arbitrary cruft on the same line
722 * (e.g. "cut here"), in order to avoid misidentification, the
723 * perforation must occupy more than a third of the visible
724 * width of the line, and dashes and scissors must occupy more
725 * than half of the perforation.
728 if (first_nonblank
&& last_nonblank
)
729 visible
= last_nonblank
- first_nonblank
+ 1;
732 return (scissors
&& 8 <= visible
&&
733 visible
< perforation
* 3 &&
734 gap
* 2 < perforation
);
737 static void flush_inbody_header_accum(struct mailinfo
*mi
)
739 if (!mi
->inbody_header_accum
.len
)
741 if (!check_header(mi
, &mi
->inbody_header_accum
, mi
->s_hdr_data
, 0))
742 BUG("inbody_header_accum, if not empty, must always contain a valid in-body header");
743 strbuf_reset(&mi
->inbody_header_accum
);
746 static int check_inbody_header(struct mailinfo
*mi
, const struct strbuf
*line
)
748 if (mi
->inbody_header_accum
.len
&&
749 (line
->buf
[0] == ' ' || line
->buf
[0] == '\t')) {
750 if (mi
->use_scissors
&& is_scissors_line(line
->buf
)) {
752 * This is a scissors line; do not consider this line
753 * as a header continuation line.
755 flush_inbody_header_accum(mi
);
758 strbuf_strip_suffix(&mi
->inbody_header_accum
, "\n");
759 strbuf_addbuf(&mi
->inbody_header_accum
, line
);
763 flush_inbody_header_accum(mi
);
765 if (starts_with(line
->buf
, ">From") && isspace(line
->buf
[5]))
766 return is_format_patch_separator(line
->buf
+ 1, line
->len
- 1);
767 if (starts_with(line
->buf
, "[PATCH]") && isspace(line
->buf
[7])) {
769 for (i
= 0; header
[i
]; i
++)
770 if (!strcmp("Subject", header
[i
])) {
771 handle_header(&mi
->s_hdr_data
[i
], line
);
776 if (is_inbody_header(mi
, line
)) {
777 strbuf_addbuf(&mi
->inbody_header_accum
, line
);
783 static int handle_commit_msg(struct mailinfo
*mi
, struct strbuf
*line
)
785 assert(!mi
->filter_stage
);
787 if (mi
->header_stage
) {
788 if (!line
->len
|| (line
->len
== 1 && line
->buf
[0] == '\n')) {
789 if (mi
->inbody_header_accum
.len
) {
790 flush_inbody_header_accum(mi
);
791 mi
->header_stage
= 0;
797 if (mi
->use_inbody_headers
&& mi
->header_stage
) {
798 mi
->header_stage
= check_inbody_header(mi
, line
);
799 if (mi
->header_stage
)
802 /* Only trim the first (blank) line of the commit message
803 * when ignoring in-body headers.
805 mi
->header_stage
= 0;
807 /* normalize the log message to UTF-8. */
808 if (convert_to_utf8(mi
, line
, mi
->charset
.buf
))
809 return 0; /* mi->input_error already set */
811 if (mi
->use_scissors
&& is_scissors_line(line
->buf
)) {
814 strbuf_setlen(&mi
->log_message
, 0);
815 mi
->header_stage
= 1;
818 * We may have already read "secondary headers"; purge
819 * them to give ourselves a clean restart.
821 for (i
= 0; header
[i
]; i
++) {
822 if (mi
->s_hdr_data
[i
])
823 strbuf_release(mi
->s_hdr_data
[i
]);
824 FREE_AND_NULL(mi
->s_hdr_data
[i
]);
829 if (patchbreak(line
)) {
831 strbuf_addf(&mi
->log_message
,
832 "Message-Id: %s\n", mi
->message_id
);
836 strbuf_addbuf(&mi
->log_message
, line
);
840 static void handle_patch(struct mailinfo
*mi
, const struct strbuf
*line
)
842 fwrite(line
->buf
, 1, line
->len
, mi
->patchfile
);
846 static void handle_filter(struct mailinfo
*mi
, struct strbuf
*line
)
848 switch (mi
->filter_stage
) {
850 if (!handle_commit_msg(mi
, line
))
855 handle_patch(mi
, line
);
860 static int is_rfc2822_header(const struct strbuf
*line
)
863 * The section that defines the loosest possible
864 * field name is "3.6.8 Optional fields".
866 * optional-field = field-name ":" unstructured CRLF
867 * field-name = 1*ftext
868 * ftext = %d33-57 / %59-126
871 char *cp
= line
->buf
;
873 /* Count mbox From headers as headers */
874 if (starts_with(cp
, "From ") || starts_with(cp
, ">From "))
877 while ((ch
= *cp
++)) {
880 if ((33 <= ch
&& ch
<= 57) ||
881 (59 <= ch
&& ch
<= 126))
888 static int read_one_header_line(struct strbuf
*line
, FILE *in
)
890 struct strbuf continuation
= STRBUF_INIT
;
892 /* Get the first part of the line. */
893 if (strbuf_getline_lf(line
, in
))
897 * Is it an empty line or not a valid rfc2822 header?
898 * If so, stop here, and return false ("not a header")
901 if (!line
->len
|| !is_rfc2822_header(line
)) {
902 /* Re-add the newline */
903 strbuf_addch(line
, '\n');
908 * Now we need to eat all the continuation lines..
909 * Yuck, 2822 header "folding"
918 if (peek
!= ' ' && peek
!= '\t')
920 if (strbuf_getline_lf(&continuation
, in
))
922 continuation
.buf
[0] = ' ';
923 strbuf_rtrim(&continuation
);
924 strbuf_addbuf(line
, &continuation
);
926 strbuf_release(&continuation
);
931 static int find_boundary(struct mailinfo
*mi
, struct strbuf
*line
)
933 while (!strbuf_getline_lf(line
, mi
->input
)) {
934 if (*(mi
->content_top
) && is_multipart_boundary(mi
, line
))
940 static int handle_boundary(struct mailinfo
*mi
, struct strbuf
*line
)
942 struct strbuf newline
= STRBUF_INIT
;
944 strbuf_addch(&newline
, '\n');
946 if (line
->len
>= (*(mi
->content_top
))->len
+ 2 &&
947 !memcmp(line
->buf
+ (*(mi
->content_top
))->len
, "--", 2)) {
948 /* we hit an end boundary */
949 /* pop the current boundary off the stack */
950 strbuf_release(*(mi
->content_top
));
951 FREE_AND_NULL(*(mi
->content_top
));
953 /* technically won't happen as is_multipart_boundary()
954 will fail first. But just in case..
956 if (--mi
->content_top
< mi
->content
) {
957 error("Detected mismatched boundaries, can't recover");
958 mi
->input_error
= -1;
959 mi
->content_top
= mi
->content
;
960 strbuf_release(&newline
);
963 handle_filter(mi
, &newline
);
964 strbuf_release(&newline
);
968 /* skip to the next boundary */
969 if (!find_boundary(mi
, line
))
974 /* set some defaults */
975 mi
->transfer_encoding
= TE_DONTCARE
;
976 strbuf_reset(&mi
->charset
);
978 /* slurp in this section's info */
979 while (read_one_header_line(line
, mi
->input
))
980 check_header(mi
, line
, mi
->p_hdr_data
, 0);
982 strbuf_release(&newline
);
984 if (strbuf_getline_lf(line
, mi
->input
))
986 strbuf_addch(line
, '\n');
990 static void handle_filter_flowed(struct mailinfo
*mi
, struct strbuf
*line
,
993 size_t len
= line
->len
;
996 if (!mi
->format_flowed
) {
998 line
->buf
[len
- 2] == '\r' &&
999 line
->buf
[len
- 1] == '\n') {
1000 mi
->have_quoted_cr
= 1;
1001 if (mi
->quoted_cr
== quoted_cr_strip
) {
1002 strbuf_setlen(line
, len
- 2);
1003 strbuf_addch(line
, '\n');
1007 handle_filter(mi
, line
);
1011 if (line
->buf
[len
- 1] == '\n') {
1013 if (len
&& line
->buf
[len
- 1] == '\r')
1017 /* Keep signature separator as-is. */
1018 if (skip_prefix(line
->buf
, "-- ", &rest
) && rest
- line
->buf
== len
) {
1020 handle_filter(mi
, prev
);
1023 handle_filter(mi
, line
);
1027 /* Unstuff space-stuffed line. */
1028 if (len
&& line
->buf
[0] == ' ') {
1029 strbuf_remove(line
, 0, 1);
1033 /* Save flowed line for later, but without the soft line break. */
1034 if (len
&& line
->buf
[len
- 1] == ' ') {
1035 strbuf_add(prev
, line
->buf
, len
- !!mi
->delsp
);
1039 /* Prepend any previous partial lines */
1040 strbuf_insert(line
, 0, prev
->buf
, prev
->len
);
1043 handle_filter(mi
, line
);
1046 static void summarize_quoted_cr(struct mailinfo
*mi
)
1048 if (mi
->have_quoted_cr
&&
1049 mi
->quoted_cr
== quoted_cr_warn
)
1050 warning(_("quoted CRLF detected"));
1053 static void handle_body(struct mailinfo
*mi
, struct strbuf
*line
)
1055 struct strbuf prev
= STRBUF_INIT
;
1057 /* Skip up to the first boundary */
1058 if (*(mi
->content_top
)) {
1059 if (!find_boundary(mi
, line
))
1060 goto handle_body_out
;
1064 /* process any boundary lines */
1065 if (*(mi
->content_top
) && is_multipart_boundary(mi
, line
)) {
1066 /* flush any leftover */
1068 handle_filter(mi
, &prev
);
1069 strbuf_reset(&prev
);
1071 summarize_quoted_cr(mi
);
1072 mi
->have_quoted_cr
= 0;
1073 if (!handle_boundary(mi
, line
))
1074 goto handle_body_out
;
1077 /* Unwrap transfer encoding */
1078 decode_transfer_encoding(mi
, line
);
1080 switch (mi
->transfer_encoding
) {
1084 struct strbuf
**lines
, **it
, *sb
;
1086 /* Prepend any previous partial lines */
1087 strbuf_insert(line
, 0, prev
.buf
, prev
.len
);
1088 strbuf_reset(&prev
);
1091 * This is a decoded line that may contain
1092 * multiple new lines. Pass only one chunk
1093 * at a time to handle_filter()
1095 lines
= strbuf_split(line
, '\n');
1096 for (it
= lines
; (sb
= *it
); it
++) {
1097 if (*(it
+ 1) == NULL
) /* The last line */
1098 if (sb
->buf
[sb
->len
- 1] != '\n') {
1099 /* Partial line, save it for later. */
1100 strbuf_addbuf(&prev
, sb
);
1103 handle_filter_flowed(mi
, sb
, &prev
);
1106 * The partial chunk is saved in "prev" and will be
1107 * appended by the next iteration of read_line_with_nul().
1109 strbuf_list_free(lines
);
1113 handle_filter_flowed(mi
, line
, &prev
);
1116 if (mi
->input_error
)
1118 } while (!strbuf_getwholeline(line
, mi
->input
, '\n'));
1121 handle_filter(mi
, &prev
);
1122 summarize_quoted_cr(mi
);
1124 flush_inbody_header_accum(mi
);
1127 strbuf_release(&prev
);
1130 static void output_header_lines(FILE *fout
, const char *hdr
, const struct strbuf
*data
)
1132 const char *sp
= data
->buf
;
1134 char *ep
= strchr(sp
, '\n');
1140 fprintf(fout
, "%s: %.*s\n", hdr
, len
, sp
);
1147 static void handle_info(struct mailinfo
*mi
)
1152 for (i
= 0; header
[i
]; i
++) {
1153 /* only print inbody headers if we output a patch file */
1154 if (mi
->patch_lines
&& mi
->s_hdr_data
[i
])
1155 hdr
= mi
->s_hdr_data
[i
];
1156 else if (mi
->p_hdr_data
[i
])
1157 hdr
= mi
->p_hdr_data
[i
];
1161 if (memchr(hdr
->buf
, '\0', hdr
->len
)) {
1162 error("a NUL byte in '%s' is not allowed.", header
[i
]);
1163 mi
->input_error
= -1;
1166 if (!strcmp(header
[i
], "Subject")) {
1167 if (!mi
->keep_subject
) {
1168 cleanup_subject(mi
, hdr
);
1171 output_header_lines(mi
->output
, "Subject", hdr
);
1172 } else if (!strcmp(header
[i
], "From")) {
1174 handle_from(mi
, hdr
);
1175 fprintf(mi
->output
, "Author: %s\n", mi
->name
.buf
);
1176 fprintf(mi
->output
, "Email: %s\n", mi
->email
.buf
);
1179 fprintf(mi
->output
, "%s: %s\n", header
[i
], hdr
->buf
);
1182 fprintf(mi
->output
, "\n");
1185 int mailinfo(struct mailinfo
*mi
, const char *msg
, const char *patch
)
1189 struct strbuf line
= STRBUF_INIT
;
1191 cmitmsg
= fopen(msg
, "w");
1196 mi
->patchfile
= fopen(patch
, "w");
1197 if (!mi
->patchfile
) {
1203 mi
->p_hdr_data
= xcalloc(MAX_HDR_PARSED
, sizeof(*(mi
->p_hdr_data
)));
1204 mi
->s_hdr_data
= xcalloc(MAX_HDR_PARSED
, sizeof(*(mi
->s_hdr_data
)));
1207 peek
= fgetc(mi
->input
);
1210 return error("empty patch: '%s'", patch
);
1212 } while (isspace(peek
));
1213 ungetc(peek
, mi
->input
);
1215 /* process the email header */
1216 while (read_one_header_line(&line
, mi
->input
))
1217 check_header(mi
, &line
, mi
->p_hdr_data
, 1);
1219 handle_body(mi
, &line
);
1220 fwrite(mi
->log_message
.buf
, 1, mi
->log_message
.len
, cmitmsg
);
1222 fclose(mi
->patchfile
);
1225 strbuf_release(&line
);
1226 return mi
->input_error
;
1229 int mailinfo_parse_quoted_cr_action(const char *actionstr
, int *action
)
1231 if (!strcmp(actionstr
, "nowarn"))
1232 *action
= quoted_cr_nowarn
;
1233 else if (!strcmp(actionstr
, "warn"))
1234 *action
= quoted_cr_warn
;
1235 else if (!strcmp(actionstr
, "strip"))
1236 *action
= quoted_cr_strip
;
1242 static int git_mailinfo_config(const char *var
, const char *value
, void *mi_
)
1244 struct mailinfo
*mi
= mi_
;
1246 if (!starts_with(var
, "mailinfo."))
1247 return git_default_config(var
, value
, NULL
);
1248 if (!strcmp(var
, "mailinfo.scissors")) {
1249 mi
->use_scissors
= git_config_bool(var
, value
);
1252 if (!strcmp(var
, "mailinfo.quotedcr")) {
1253 if (mailinfo_parse_quoted_cr_action(value
, &mi
->quoted_cr
) != 0)
1254 return error(_("bad action '%s' for '%s'"), value
, var
);
1257 /* perhaps others here */
1261 void setup_mailinfo(struct mailinfo
*mi
)
1263 memset(mi
, 0, sizeof(*mi
));
1264 strbuf_init(&mi
->name
, 0);
1265 strbuf_init(&mi
->email
, 0);
1266 strbuf_init(&mi
->charset
, 0);
1267 strbuf_init(&mi
->log_message
, 0);
1268 strbuf_init(&mi
->inbody_header_accum
, 0);
1269 mi
->quoted_cr
= quoted_cr_warn
;
1270 mi
->header_stage
= 1;
1271 mi
->use_inbody_headers
= 1;
1272 mi
->content_top
= mi
->content
;
1273 git_config(git_mailinfo_config
, mi
);
1276 void clear_mailinfo(struct mailinfo
*mi
)
1278 strbuf_release(&mi
->name
);
1279 strbuf_release(&mi
->email
);
1280 strbuf_release(&mi
->charset
);
1281 strbuf_release(&mi
->inbody_header_accum
);
1282 free(mi
->message_id
);
1284 strbuf_list_free(mi
->p_hdr_data
);
1285 strbuf_list_free(mi
->s_hdr_data
);
1287 while (mi
->content
< mi
->content_top
) {
1288 free(*(mi
->content_top
));
1292 strbuf_release(&mi
->log_message
);