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
< 3 || 60 < name
->len
|| strchr(name
->buf
, '@') ||
23 strchr(name
->buf
, '<') || strchr(name
->buf
, '>'))
28 strbuf_addbuf(out
, src
);
31 static void parse_bogus_from(struct mailinfo
*mi
, const struct strbuf
*line
)
33 /* John Doe <johndoe> */
36 /* This is fallback, so do not bother if we already have an
42 bra
= strchr(line
->buf
, '<');
45 ket
= strchr(bra
, '>');
49 strbuf_reset(&mi
->email
);
50 strbuf_add(&mi
->email
, bra
+ 1, ket
- bra
- 1);
52 strbuf_reset(&mi
->name
);
53 strbuf_add(&mi
->name
, line
->buf
, bra
- line
->buf
);
54 strbuf_trim(&mi
->name
);
55 get_sane_name(&mi
->name
, &mi
->name
, &mi
->email
);
58 static const char *unquote_comment(struct strbuf
*outbuf
, const char *in
)
61 int take_next_literally
= 0;
63 strbuf_addch(outbuf
, '(');
65 while ((c
= *in
++) != 0) {
66 if (take_next_literally
== 1) {
67 take_next_literally
= 0;
71 take_next_literally
= 1;
74 in
= unquote_comment(outbuf
, in
);
77 strbuf_addch(outbuf
, ')');
82 strbuf_addch(outbuf
, c
);
88 static const char *unquote_quoted_string(struct strbuf
*outbuf
, const char *in
)
91 int take_next_literally
= 0;
93 while ((c
= *in
++) != 0) {
94 if (take_next_literally
== 1) {
95 take_next_literally
= 0;
99 take_next_literally
= 1;
106 strbuf_addch(outbuf
, c
);
112 static void unquote_quoted_pair(struct strbuf
*line
)
114 struct strbuf outbuf
;
115 const char *in
= line
->buf
;
118 strbuf_init(&outbuf
, line
->len
);
120 while ((c
= *in
++) != 0) {
123 in
= unquote_quoted_string(&outbuf
, in
);
126 in
= unquote_comment(&outbuf
, in
);
130 strbuf_addch(&outbuf
, c
);
133 strbuf_swap(&outbuf
, line
);
134 strbuf_release(&outbuf
);
138 static void handle_from(struct mailinfo
*mi
, const struct strbuf
*from
)
144 strbuf_init(&f
, from
->len
);
145 strbuf_addbuf(&f
, from
);
147 unquote_quoted_pair(&f
);
149 at
= strchr(f
.buf
, '@');
151 parse_bogus_from(mi
, from
);
156 * If we already have one email, don't take any confusing lines
158 if (mi
->email
.len
&& strchr(at
+ 1, '@'))
161 /* Pick up the string around '@', possibly delimited with <>
162 * pair; that is the email part.
174 el
= strcspn(at
, " \n\t\r\v\f>");
175 strbuf_reset(&mi
->email
);
176 strbuf_add(&mi
->email
, at
, el
);
177 strbuf_remove(&f
, at
- f
.buf
, el
+ (at
[el
] ? 1 : 0));
179 /* The remainder is name. It could be
181 * - "John Doe <john.doe@xz>" (a), or
182 * - "john.doe@xz (John Doe)" (b), or
183 * - "John (zzz) Doe <john.doe@xz> (Comment)" (c)
185 * but we have removed the email part, so
187 * - remove extra spaces which could stay after email (case 'c'), and
188 * - trim from both ends, possibly removing the () pair at the end
189 * (cases 'a' and 'b').
193 if (f
.buf
[0] == '(' && f
.len
&& f
.buf
[f
.len
- 1] == ')') {
194 strbuf_remove(&f
, 0, 1);
195 strbuf_setlen(&f
, f
.len
- 1);
198 get_sane_name(&mi
->name
, &f
, &mi
->email
);
203 static void handle_header(struct strbuf
**out
, const struct strbuf
*line
)
206 *out
= xmalloc(sizeof(struct strbuf
));
207 strbuf_init(*out
, line
->len
);
211 strbuf_addbuf(*out
, line
);
214 /* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
215 * to have enough heuristics to grok MIME encoded patches often found
216 * on our mailing lists. For example, we do not even treat header lines
217 * case insensitively.
220 static int slurp_attr(const char *line
, const char *name
, struct strbuf
*attr
)
222 const char *ends
, *ap
= strcasestr(line
, name
);
225 strbuf_setlen(attr
, 0);
235 sz
= strcspn(ap
, ends
);
236 strbuf_add(attr
, ap
, sz
);
240 static int has_attr_value(const char *line
, const char *name
, const char *value
)
242 struct strbuf sb
= STRBUF_INIT
;
243 int rc
= slurp_attr(line
, name
, &sb
) && !strcasecmp(sb
.buf
, value
);
248 static void handle_content_type(struct mailinfo
*mi
, struct strbuf
*line
)
250 struct strbuf
*boundary
= xmalloc(sizeof(struct strbuf
));
251 strbuf_init(boundary
, line
->len
);
253 mi
->format_flowed
= has_attr_value(line
->buf
, "format=", "flowed");
254 mi
->delsp
= has_attr_value(line
->buf
, "delsp=", "yes");
256 if (slurp_attr(line
->buf
, "boundary=", boundary
)) {
257 strbuf_insertstr(boundary
, 0, "--");
258 if (++mi
->content_top
>= &mi
->content
[MAX_BOUNDARIES
]) {
259 error("Too many boundaries to handle");
260 mi
->input_error
= -1;
261 mi
->content_top
= &mi
->content
[MAX_BOUNDARIES
] - 1;
264 *(mi
->content_top
) = boundary
;
267 slurp_attr(line
->buf
, "charset=", &mi
->charset
);
270 strbuf_release(boundary
);
275 static void handle_content_transfer_encoding(struct mailinfo
*mi
,
276 const struct strbuf
*line
)
278 if (strcasestr(line
->buf
, "base64"))
279 mi
->transfer_encoding
= TE_BASE64
;
280 else if (strcasestr(line
->buf
, "quoted-printable"))
281 mi
->transfer_encoding
= TE_QP
;
283 mi
->transfer_encoding
= TE_DONTCARE
;
286 static int is_multipart_boundary(struct mailinfo
*mi
, const struct strbuf
*line
)
288 struct strbuf
*content_top
= *(mi
->content_top
);
290 return ((content_top
->len
<= line
->len
) &&
291 !memcmp(line
->buf
, content_top
->buf
, content_top
->len
));
294 static void cleanup_subject(struct mailinfo
*mi
, struct strbuf
*subject
)
298 while (at
< subject
->len
) {
302 switch (subject
->buf
[at
]) {
304 if (subject
->len
<= at
+ 3)
306 if ((subject
->buf
[at
+ 1] == 'e' ||
307 subject
->buf
[at
+ 1] == 'E') &&
308 subject
->buf
[at
+ 2] == ':') {
309 strbuf_remove(subject
, at
, 3);
314 case ' ': case '\t': case ':':
315 strbuf_remove(subject
, at
, 1);
318 pos
= strchr(subject
->buf
+ at
, ']');
321 remove
= pos
- subject
->buf
+ at
+ 1;
322 if (!mi
->keep_non_patch_brackets_in_subject
||
324 memmem(subject
->buf
+ at
, remove
, "PATCH", 5)))
325 strbuf_remove(subject
, at
, remove
);
329 * If the input had a space after the ], keep
330 * it. We don't bother with finding the end of
331 * the space, since we later normalize it
334 if (isspace(subject
->buf
[at
]))
341 strbuf_trim(subject
);
344 #define MAX_HDR_PARSED 10
345 static const char *header
[MAX_HDR_PARSED
] = {
346 "From","Subject","Date",
349 static inline int cmp_header(const struct strbuf
*line
, const char *hdr
)
351 int len
= strlen(hdr
);
352 return !strncasecmp(line
->buf
, hdr
, len
) && line
->len
> len
&&
353 line
->buf
[len
] == ':' && isspace(line
->buf
[len
+ 1]);
356 static int is_format_patch_separator(const char *line
, int len
)
358 static const char SAMPLE
[] =
359 "From e6807f3efca28b30decfecb1732a56c7db1137ee Mon Sep 17 00:00:00 2001\n";
362 if (len
!= strlen(SAMPLE
))
364 if (!skip_prefix(line
, "From ", &cp
))
366 if (strspn(cp
, "0123456789abcdef") != 40)
369 return !memcmp(SAMPLE
+ (cp
- line
), cp
, strlen(SAMPLE
) - (cp
- line
));
372 static struct strbuf
*decode_q_segment(const struct strbuf
*q_seg
, int rfc2047
)
374 const char *in
= q_seg
->buf
;
376 struct strbuf
*out
= xmalloc(sizeof(struct strbuf
));
377 strbuf_init(out
, q_seg
->len
);
379 while ((c
= *in
++) != 0) {
383 break; /* drop trailing newline */
386 strbuf_addch(out
, ch
);
390 /* garbage -- fall through */
392 if (rfc2047
&& c
== '_') /* rfc2047 4.2 (2) */
394 strbuf_addch(out
, c
);
399 static struct strbuf
*decode_b_segment(const struct strbuf
*b_seg
)
401 /* Decode in..ep, possibly in-place to ot */
402 int c
, pos
= 0, acc
= 0;
403 const char *in
= b_seg
->buf
;
404 struct strbuf
*out
= xmalloc(sizeof(struct strbuf
));
405 strbuf_init(out
, b_seg
->len
);
407 while ((c
= *in
++) != 0) {
412 else if ('A' <= c
&& c
<= 'Z')
414 else if ('a' <= c
&& c
<= 'z')
416 else if ('0' <= c
&& c
<= '9')
419 continue; /* garbage */
425 strbuf_addch(out
, (acc
| (c
>> 4)));
429 strbuf_addch(out
, (acc
| (c
>> 2)));
433 strbuf_addch(out
, (acc
| c
));
441 static int convert_to_utf8(struct mailinfo
*mi
,
442 struct strbuf
*line
, const char *charset
)
446 if (!mi
->metainfo_charset
|| !charset
|| !*charset
)
449 if (same_encoding(mi
->metainfo_charset
, charset
))
451 out
= reencode_string(line
->buf
, mi
->metainfo_charset
, charset
);
453 mi
->input_error
= -1;
454 return error("cannot convert from %s to %s",
455 charset
, mi
->metainfo_charset
);
457 strbuf_attach(line
, out
, strlen(out
), strlen(out
));
461 static void decode_header(struct mailinfo
*mi
, struct strbuf
*it
)
464 struct strbuf outbuf
= STRBUF_INIT
, *dec
;
465 struct strbuf charset_q
= STRBUF_INIT
, piecebuf
= STRBUF_INIT
;
466 int found_error
= 1; /* pessimism */
469 while (in
- it
->buf
<= it
->len
&& (ep
= strstr(in
, "=?")) != NULL
) {
471 strbuf_reset(&charset_q
);
472 strbuf_reset(&piecebuf
);
476 * We are about to process an encoded-word
477 * that begins at ep, but there is something
478 * before the encoded word.
481 for (scan
= in
; scan
< ep
; scan
++)
485 if (scan
!= ep
|| in
== it
->buf
) {
487 * We should not lose that "something",
488 * unless we have just processed an
489 * encoded-word, and there is only LWS
490 * before the one we are about to process.
492 strbuf_add(&outbuf
, in
, ep
- in
);
496 * ep : "=?iso-2022-jp?B?GyR...?= foo"
497 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
501 if (ep
- it
->buf
>= it
->len
|| !(cp
= strchr(ep
, '?')))
504 if (cp
+ 3 - it
->buf
> it
->len
)
506 strbuf_add(&charset_q
, ep
, cp
- ep
);
509 if (!encoding
|| cp
[2] != '?')
511 ep
= strstr(cp
+ 3, "?=");
514 strbuf_add(&piecebuf
, cp
+ 3, ep
- cp
- 3);
515 switch (tolower(encoding
)) {
519 dec
= decode_b_segment(&piecebuf
);
522 dec
= decode_q_segment(&piecebuf
, 1);
525 if (convert_to_utf8(mi
, dec
, charset_q
.buf
))
528 strbuf_addbuf(&outbuf
, dec
);
533 strbuf_addstr(&outbuf
, in
);
535 strbuf_addbuf(it
, &outbuf
);
538 strbuf_release(&outbuf
);
539 strbuf_release(&charset_q
);
540 strbuf_release(&piecebuf
);
543 mi
->input_error
= -1;
546 static int check_header(struct mailinfo
*mi
,
547 const struct strbuf
*line
,
548 struct strbuf
*hdr_data
[], int overwrite
)
551 struct strbuf sb
= STRBUF_INIT
;
553 /* search for the interesting parts */
554 for (i
= 0; header
[i
]; i
++) {
555 int len
= strlen(header
[i
]);
556 if ((!hdr_data
[i
] || overwrite
) && cmp_header(line
, header
[i
])) {
557 /* Unwrap inline B and Q encoding, and optionally
558 * normalize the meta information to utf8.
560 strbuf_add(&sb
, line
->buf
+ len
+ 2, line
->len
- len
- 2);
561 decode_header(mi
, &sb
);
562 handle_header(&hdr_data
[i
], &sb
);
564 goto check_header_out
;
569 if (cmp_header(line
, "Content-Type")) {
570 len
= strlen("Content-Type: ");
571 strbuf_add(&sb
, line
->buf
+ len
, line
->len
- len
);
572 decode_header(mi
, &sb
);
573 handle_content_type(mi
, &sb
);
575 goto check_header_out
;
577 if (cmp_header(line
, "Content-Transfer-Encoding")) {
578 len
= strlen("Content-Transfer-Encoding: ");
579 strbuf_add(&sb
, line
->buf
+ len
, line
->len
- len
);
580 decode_header(mi
, &sb
);
581 handle_content_transfer_encoding(mi
, &sb
);
583 goto check_header_out
;
585 if (cmp_header(line
, "Message-Id")) {
586 len
= strlen("Message-Id: ");
587 strbuf_add(&sb
, line
->buf
+ len
, line
->len
- len
);
588 decode_header(mi
, &sb
);
589 if (mi
->add_message_id
)
590 mi
->message_id
= strbuf_detach(&sb
, NULL
);
592 goto check_header_out
;
601 * Returns 1 if the given line or any line beginning with the given line is an
602 * in-body header (that is, check_header will succeed when passed
605 static int is_inbody_header(const struct mailinfo
*mi
,
606 const struct strbuf
*line
)
609 for (i
= 0; header
[i
]; i
++)
610 if (!mi
->s_hdr_data
[i
] && cmp_header(line
, header
[i
]))
615 static void decode_transfer_encoding(struct mailinfo
*mi
, struct strbuf
*line
)
619 switch (mi
->transfer_encoding
) {
621 ret
= decode_q_segment(line
, 0);
624 ret
= decode_b_segment(line
);
631 strbuf_addbuf(line
, ret
);
636 static inline int patchbreak(const struct strbuf
*line
)
640 /* Beginning of a "diff -" header? */
641 if (starts_with(line
->buf
, "diff -"))
644 /* CVS "Index: " line? */
645 if (starts_with(line
->buf
, "Index: "))
649 * "--- <filename>" starts patches without headers
650 * "---<sp>*" is a manual separator
655 if (starts_with(line
->buf
, "---")) {
656 /* space followed by a filename? */
657 if (line
->buf
[3] == ' ' && !isspace(line
->buf
[4]))
659 /* Just whitespace? */
660 for (i
= 3; i
< line
->len
; i
++) {
661 unsigned char c
= line
->buf
[i
];
672 static int is_scissors_line(const char *line
)
675 int scissors
= 0, gap
= 0;
676 const char *first_nonblank
= NULL
, *last_nonblank
= NULL
;
677 int visible
, perforation
= 0, in_perforation
= 0;
679 for (c
= line
; *c
; c
++) {
681 if (in_perforation
) {
688 if (first_nonblank
== NULL
)
695 if ((!memcmp(c
, ">8", 2) || !memcmp(c
, "8<", 2) ||
696 !memcmp(c
, ">%", 2) || !memcmp(c
, "%<", 2))) {
707 * The mark must be at least 8 bytes long (e.g. "-- >8 --").
708 * Even though there can be arbitrary cruft on the same line
709 * (e.g. "cut here"), in order to avoid misidentification, the
710 * perforation must occupy more than a third of the visible
711 * width of the line, and dashes and scissors must occupy more
712 * than half of the perforation.
715 if (first_nonblank
&& last_nonblank
)
716 visible
= last_nonblank
- first_nonblank
+ 1;
719 return (scissors
&& 8 <= visible
&&
720 visible
< perforation
* 3 &&
721 gap
* 2 < perforation
);
724 static void flush_inbody_header_accum(struct mailinfo
*mi
)
726 if (!mi
->inbody_header_accum
.len
)
728 if (!check_header(mi
, &mi
->inbody_header_accum
, mi
->s_hdr_data
, 0))
729 BUG("inbody_header_accum, if not empty, must always contain a valid in-body header");
730 strbuf_reset(&mi
->inbody_header_accum
);
733 static int check_inbody_header(struct mailinfo
*mi
, const struct strbuf
*line
)
735 if (mi
->inbody_header_accum
.len
&&
736 (line
->buf
[0] == ' ' || line
->buf
[0] == '\t')) {
737 if (mi
->use_scissors
&& is_scissors_line(line
->buf
)) {
739 * This is a scissors line; do not consider this line
740 * as a header continuation line.
742 flush_inbody_header_accum(mi
);
745 strbuf_strip_suffix(&mi
->inbody_header_accum
, "\n");
746 strbuf_addbuf(&mi
->inbody_header_accum
, line
);
750 flush_inbody_header_accum(mi
);
752 if (starts_with(line
->buf
, ">From") && isspace(line
->buf
[5]))
753 return is_format_patch_separator(line
->buf
+ 1, line
->len
- 1);
754 if (starts_with(line
->buf
, "[PATCH]") && isspace(line
->buf
[7])) {
756 for (i
= 0; header
[i
]; i
++)
757 if (!strcmp("Subject", header
[i
])) {
758 handle_header(&mi
->s_hdr_data
[i
], line
);
763 if (is_inbody_header(mi
, line
)) {
764 strbuf_addbuf(&mi
->inbody_header_accum
, line
);
770 static int handle_commit_msg(struct mailinfo
*mi
, struct strbuf
*line
)
772 assert(!mi
->filter_stage
);
774 if (mi
->header_stage
) {
775 if (!line
->len
|| (line
->len
== 1 && line
->buf
[0] == '\n')) {
776 if (mi
->inbody_header_accum
.len
) {
777 flush_inbody_header_accum(mi
);
778 mi
->header_stage
= 0;
784 if (mi
->use_inbody_headers
&& mi
->header_stage
) {
785 mi
->header_stage
= check_inbody_header(mi
, line
);
786 if (mi
->header_stage
)
789 /* Only trim the first (blank) line of the commit message
790 * when ignoring in-body headers.
792 mi
->header_stage
= 0;
794 /* normalize the log message to UTF-8. */
795 if (convert_to_utf8(mi
, line
, mi
->charset
.buf
))
796 return 0; /* mi->input_error already set */
798 if (mi
->use_scissors
&& is_scissors_line(line
->buf
)) {
801 strbuf_setlen(&mi
->log_message
, 0);
802 mi
->header_stage
= 1;
805 * We may have already read "secondary headers"; purge
806 * them to give ourselves a clean restart.
808 for (i
= 0; header
[i
]; i
++) {
809 if (mi
->s_hdr_data
[i
])
810 strbuf_release(mi
->s_hdr_data
[i
]);
811 mi
->s_hdr_data
[i
] = NULL
;
816 if (patchbreak(line
)) {
818 strbuf_addf(&mi
->log_message
,
819 "Message-Id: %s\n", mi
->message_id
);
823 strbuf_addbuf(&mi
->log_message
, line
);
827 static void handle_patch(struct mailinfo
*mi
, const struct strbuf
*line
)
829 fwrite(line
->buf
, 1, line
->len
, mi
->patchfile
);
833 static void handle_filter(struct mailinfo
*mi
, struct strbuf
*line
)
835 switch (mi
->filter_stage
) {
837 if (!handle_commit_msg(mi
, line
))
842 handle_patch(mi
, line
);
847 static int is_rfc2822_header(const struct strbuf
*line
)
850 * The section that defines the loosest possible
851 * field name is "3.6.8 Optional fields".
853 * optional-field = field-name ":" unstructured CRLF
854 * field-name = 1*ftext
855 * ftext = %d33-57 / %59-126
858 char *cp
= line
->buf
;
860 /* Count mbox From headers as headers */
861 if (starts_with(cp
, "From ") || starts_with(cp
, ">From "))
864 while ((ch
= *cp
++)) {
867 if ((33 <= ch
&& ch
<= 57) ||
868 (59 <= ch
&& ch
<= 126))
875 static int read_one_header_line(struct strbuf
*line
, FILE *in
)
877 struct strbuf continuation
= STRBUF_INIT
;
879 /* Get the first part of the line. */
880 if (strbuf_getline_lf(line
, in
))
884 * Is it an empty line or not a valid rfc2822 header?
885 * If so, stop here, and return false ("not a header")
888 if (!line
->len
|| !is_rfc2822_header(line
)) {
889 /* Re-add the newline */
890 strbuf_addch(line
, '\n');
895 * Now we need to eat all the continuation lines..
896 * Yuck, 2822 header "folding"
905 if (peek
!= ' ' && peek
!= '\t')
907 if (strbuf_getline_lf(&continuation
, in
))
909 continuation
.buf
[0] = ' ';
910 strbuf_rtrim(&continuation
);
911 strbuf_addbuf(line
, &continuation
);
913 strbuf_release(&continuation
);
918 static int find_boundary(struct mailinfo
*mi
, struct strbuf
*line
)
920 while (!strbuf_getline_lf(line
, mi
->input
)) {
921 if (*(mi
->content_top
) && is_multipart_boundary(mi
, line
))
927 static int handle_boundary(struct mailinfo
*mi
, struct strbuf
*line
)
929 struct strbuf newline
= STRBUF_INIT
;
931 strbuf_addch(&newline
, '\n');
933 if (line
->len
>= (*(mi
->content_top
))->len
+ 2 &&
934 !memcmp(line
->buf
+ (*(mi
->content_top
))->len
, "--", 2)) {
935 /* we hit an end boundary */
936 /* pop the current boundary off the stack */
937 strbuf_release(*(mi
->content_top
));
938 FREE_AND_NULL(*(mi
->content_top
));
940 /* technically won't happen as is_multipart_boundary()
941 will fail first. But just in case..
943 if (--mi
->content_top
< mi
->content
) {
944 error("Detected mismatched boundaries, can't recover");
945 mi
->input_error
= -1;
946 mi
->content_top
= mi
->content
;
947 strbuf_release(&newline
);
950 handle_filter(mi
, &newline
);
951 strbuf_release(&newline
);
955 /* skip to the next boundary */
956 if (!find_boundary(mi
, line
))
961 /* set some defaults */
962 mi
->transfer_encoding
= TE_DONTCARE
;
963 strbuf_reset(&mi
->charset
);
965 /* slurp in this section's info */
966 while (read_one_header_line(line
, mi
->input
))
967 check_header(mi
, line
, mi
->p_hdr_data
, 0);
969 strbuf_release(&newline
);
971 if (strbuf_getline_lf(line
, mi
->input
))
973 strbuf_addch(line
, '\n');
977 static void handle_filter_flowed(struct mailinfo
*mi
, struct strbuf
*line
,
980 size_t len
= line
->len
;
983 if (!mi
->format_flowed
) {
984 handle_filter(mi
, line
);
988 if (line
->buf
[len
- 1] == '\n') {
990 if (len
&& line
->buf
[len
- 1] == '\r')
994 /* Keep signature separator as-is. */
995 if (skip_prefix(line
->buf
, "-- ", &rest
) && rest
- line
->buf
== len
) {
997 handle_filter(mi
, prev
);
1000 handle_filter(mi
, line
);
1004 /* Unstuff space-stuffed line. */
1005 if (len
&& line
->buf
[0] == ' ') {
1006 strbuf_remove(line
, 0, 1);
1010 /* Save flowed line for later, but without the soft line break. */
1011 if (len
&& line
->buf
[len
- 1] == ' ') {
1012 strbuf_add(prev
, line
->buf
, len
- !!mi
->delsp
);
1016 /* Prepend any previous partial lines */
1017 strbuf_insert(line
, 0, prev
->buf
, prev
->len
);
1020 handle_filter(mi
, line
);
1023 static void handle_body(struct mailinfo
*mi
, struct strbuf
*line
)
1025 struct strbuf prev
= STRBUF_INIT
;
1027 /* Skip up to the first boundary */
1028 if (*(mi
->content_top
)) {
1029 if (!find_boundary(mi
, line
))
1030 goto handle_body_out
;
1034 /* process any boundary lines */
1035 if (*(mi
->content_top
) && is_multipart_boundary(mi
, line
)) {
1036 /* flush any leftover */
1038 handle_filter(mi
, &prev
);
1039 strbuf_reset(&prev
);
1041 if (!handle_boundary(mi
, line
))
1042 goto handle_body_out
;
1045 /* Unwrap transfer encoding */
1046 decode_transfer_encoding(mi
, line
);
1048 switch (mi
->transfer_encoding
) {
1052 struct strbuf
**lines
, **it
, *sb
;
1054 /* Prepend any previous partial lines */
1055 strbuf_insert(line
, 0, prev
.buf
, prev
.len
);
1056 strbuf_reset(&prev
);
1059 * This is a decoded line that may contain
1060 * multiple new lines. Pass only one chunk
1061 * at a time to handle_filter()
1063 lines
= strbuf_split(line
, '\n');
1064 for (it
= lines
; (sb
= *it
); it
++) {
1065 if (*(it
+ 1) == NULL
) /* The last line */
1066 if (sb
->buf
[sb
->len
- 1] != '\n') {
1067 /* Partial line, save it for later. */
1068 strbuf_addbuf(&prev
, sb
);
1071 handle_filter_flowed(mi
, sb
, &prev
);
1074 * The partial chunk is saved in "prev" and will be
1075 * appended by the next iteration of read_line_with_nul().
1077 strbuf_list_free(lines
);
1081 handle_filter_flowed(mi
, line
, &prev
);
1084 if (mi
->input_error
)
1086 } while (!strbuf_getwholeline(line
, mi
->input
, '\n'));
1089 handle_filter(mi
, &prev
);
1091 flush_inbody_header_accum(mi
);
1094 strbuf_release(&prev
);
1097 static void output_header_lines(FILE *fout
, const char *hdr
, const struct strbuf
*data
)
1099 const char *sp
= data
->buf
;
1101 char *ep
= strchr(sp
, '\n');
1107 fprintf(fout
, "%s: %.*s\n", hdr
, len
, sp
);
1114 static void handle_info(struct mailinfo
*mi
)
1119 for (i
= 0; header
[i
]; i
++) {
1120 /* only print inbody headers if we output a patch file */
1121 if (mi
->patch_lines
&& mi
->s_hdr_data
[i
])
1122 hdr
= mi
->s_hdr_data
[i
];
1123 else if (mi
->p_hdr_data
[i
])
1124 hdr
= mi
->p_hdr_data
[i
];
1128 if (!strcmp(header
[i
], "Subject")) {
1129 if (!mi
->keep_subject
) {
1130 cleanup_subject(mi
, hdr
);
1133 output_header_lines(mi
->output
, "Subject", hdr
);
1134 } else if (!strcmp(header
[i
], "From")) {
1136 handle_from(mi
, hdr
);
1137 fprintf(mi
->output
, "Author: %s\n", mi
->name
.buf
);
1138 fprintf(mi
->output
, "Email: %s\n", mi
->email
.buf
);
1141 fprintf(mi
->output
, "%s: %s\n", header
[i
], hdr
->buf
);
1144 fprintf(mi
->output
, "\n");
1147 int mailinfo(struct mailinfo
*mi
, const char *msg
, const char *patch
)
1151 struct strbuf line
= STRBUF_INIT
;
1153 cmitmsg
= fopen(msg
, "w");
1158 mi
->patchfile
= fopen(patch
, "w");
1159 if (!mi
->patchfile
) {
1165 mi
->p_hdr_data
= xcalloc(MAX_HDR_PARSED
, sizeof(*(mi
->p_hdr_data
)));
1166 mi
->s_hdr_data
= xcalloc(MAX_HDR_PARSED
, sizeof(*(mi
->s_hdr_data
)));
1169 peek
= fgetc(mi
->input
);
1172 return error("empty patch: '%s'", patch
);
1174 } while (isspace(peek
));
1175 ungetc(peek
, mi
->input
);
1177 /* process the email header */
1178 while (read_one_header_line(&line
, mi
->input
))
1179 check_header(mi
, &line
, mi
->p_hdr_data
, 1);
1181 handle_body(mi
, &line
);
1182 fwrite(mi
->log_message
.buf
, 1, mi
->log_message
.len
, cmitmsg
);
1184 fclose(mi
->patchfile
);
1187 strbuf_release(&line
);
1188 return mi
->input_error
;
1191 static int git_mailinfo_config(const char *var
, const char *value
, void *mi_
)
1193 struct mailinfo
*mi
= mi_
;
1195 if (!starts_with(var
, "mailinfo."))
1196 return git_default_config(var
, value
, NULL
);
1197 if (!strcmp(var
, "mailinfo.scissors")) {
1198 mi
->use_scissors
= git_config_bool(var
, value
);
1201 /* perhaps others here */
1205 void setup_mailinfo(struct mailinfo
*mi
)
1207 memset(mi
, 0, sizeof(*mi
));
1208 strbuf_init(&mi
->name
, 0);
1209 strbuf_init(&mi
->email
, 0);
1210 strbuf_init(&mi
->charset
, 0);
1211 strbuf_init(&mi
->log_message
, 0);
1212 strbuf_init(&mi
->inbody_header_accum
, 0);
1213 mi
->header_stage
= 1;
1214 mi
->use_inbody_headers
= 1;
1215 mi
->content_top
= mi
->content
;
1216 git_config(git_mailinfo_config
, mi
);
1219 void clear_mailinfo(struct mailinfo
*mi
)
1223 strbuf_release(&mi
->name
);
1224 strbuf_release(&mi
->email
);
1225 strbuf_release(&mi
->charset
);
1226 strbuf_release(&mi
->inbody_header_accum
);
1227 free(mi
->message_id
);
1230 for (i
= 0; mi
->p_hdr_data
[i
]; i
++)
1231 strbuf_release(mi
->p_hdr_data
[i
]);
1232 free(mi
->p_hdr_data
);
1234 for (i
= 0; mi
->s_hdr_data
[i
]; i
++)
1235 strbuf_release(mi
->s_hdr_data
[i
]);
1236 free(mi
->s_hdr_data
);
1238 while (mi
->content
< mi
->content_top
) {
1239 free(*(mi
->content_top
));
1243 strbuf_release(&mi
->log_message
);