]> git.ipfire.org Git - thirdparty/git.git/blame - mailinfo.c
mailinfo: make is_scissors_line take plain char *
[thirdparty/git.git] / mailinfo.c
CommitLineData
c6905e45
JH
1#include "cache.h"
2#include "utf8.h"
3#include "strbuf.h"
4#include "mailinfo.h"
5
6static void cleanup_space(struct strbuf *sb)
7{
8 size_t pos, cnt;
9 for (pos = 0; pos < sb->len; pos++) {
10 if (isspace(sb->buf[pos])) {
11 sb->buf[pos] = ' ';
12 for (cnt = 0; isspace(sb->buf[pos + cnt + 1]); cnt++);
13 strbuf_remove(sb, pos + 1, cnt);
14 }
15 }
16}
17
18static void get_sane_name(struct strbuf *out, struct strbuf *name, struct strbuf *email)
19{
20 struct strbuf *src = name;
21 if (name->len < 3 || 60 < name->len || strchr(name->buf, '@') ||
22 strchr(name->buf, '<') || strchr(name->buf, '>'))
23 src = email;
24 else if (name == out)
25 return;
26 strbuf_reset(out);
27 strbuf_addbuf(out, src);
28}
29
30static void parse_bogus_from(struct mailinfo *mi, const struct strbuf *line)
31{
32 /* John Doe <johndoe> */
33
34 char *bra, *ket;
35 /* This is fallback, so do not bother if we already have an
36 * e-mail address.
37 */
38 if (mi->email.len)
39 return;
40
41 bra = strchr(line->buf, '<');
42 if (!bra)
43 return;
44 ket = strchr(bra, '>');
45 if (!ket)
46 return;
47
48 strbuf_reset(&mi->email);
49 strbuf_add(&mi->email, bra + 1, ket - bra - 1);
50
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);
55}
56
57static void handle_from(struct mailinfo *mi, const struct strbuf *from)
58{
59 char *at;
60 size_t el;
61 struct strbuf f;
62
63 strbuf_init(&f, from->len);
64 strbuf_addbuf(&f, from);
65
66 at = strchr(f.buf, '@');
67 if (!at) {
68 parse_bogus_from(mi, from);
69 return;
70 }
71
72 /*
73 * If we already have one email, don't take any confusing lines
74 */
75 if (mi->email.len && strchr(at + 1, '@')) {
76 strbuf_release(&f);
77 return;
78 }
79
80 /* Pick up the string around '@', possibly delimited with <>
81 * pair; that is the email part.
82 */
83 while (at > f.buf) {
84 char c = at[-1];
85 if (isspace(c))
86 break;
87 if (c == '<') {
88 at[-1] = ' ';
89 break;
90 }
91 at--;
92 }
93 el = strcspn(at, " \n\t\r\v\f>");
94 strbuf_reset(&mi->email);
95 strbuf_add(&mi->email, at, el);
96 strbuf_remove(&f, at - f.buf, el + (at[el] ? 1 : 0));
97
98 /* The remainder is name. It could be
99 *
100 * - "John Doe <john.doe@xz>" (a), or
101 * - "john.doe@xz (John Doe)" (b), or
102 * - "John (zzz) Doe <john.doe@xz> (Comment)" (c)
103 *
104 * but we have removed the email part, so
105 *
106 * - remove extra spaces which could stay after email (case 'c'), and
107 * - trim from both ends, possibly removing the () pair at the end
108 * (cases 'a' and 'b').
109 */
110 cleanup_space(&f);
111 strbuf_trim(&f);
112 if (f.buf[0] == '(' && f.len && f.buf[f.len - 1] == ')') {
113 strbuf_remove(&f, 0, 1);
114 strbuf_setlen(&f, f.len - 1);
115 }
116
117 get_sane_name(&mi->name, &f, &mi->email);
118 strbuf_release(&f);
119}
120
121static void handle_header(struct strbuf **out, const struct strbuf *line)
122{
123 if (!*out) {
124 *out = xmalloc(sizeof(struct strbuf));
125 strbuf_init(*out, line->len);
126 } else
127 strbuf_reset(*out);
128
129 strbuf_addbuf(*out, line);
130}
131
132/* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
133 * to have enough heuristics to grok MIME encoded patches often found
134 * on our mailing lists. For example, we do not even treat header lines
135 * case insensitively.
136 */
137
138static int slurp_attr(const char *line, const char *name, struct strbuf *attr)
139{
140 const char *ends, *ap = strcasestr(line, name);
141 size_t sz;
142
143 strbuf_setlen(attr, 0);
144 if (!ap)
145 return 0;
146 ap += strlen(name);
147 if (*ap == '"') {
148 ap++;
149 ends = "\"";
150 }
151 else
152 ends = "; \t";
153 sz = strcspn(ap, ends);
154 strbuf_add(attr, ap, sz);
155 return 1;
156}
157
158static void handle_content_type(struct mailinfo *mi, struct strbuf *line)
159{
160 struct strbuf *boundary = xmalloc(sizeof(struct strbuf));
161 strbuf_init(boundary, line->len);
162
163 if (slurp_attr(line->buf, "boundary=", boundary)) {
164 strbuf_insert(boundary, 0, "--", 2);
165 if (++mi->content_top >= &mi->content[MAX_BOUNDARIES]) {
6ac617a3
JH
166 error("Too many boundaries to handle");
167 mi->input_error = -1;
168 mi->content_top = &mi->content[MAX_BOUNDARIES] - 1;
169 return;
c6905e45
JH
170 }
171 *(mi->content_top) = boundary;
172 boundary = NULL;
173 }
174 slurp_attr(line->buf, "charset=", &mi->charset);
175
176 if (boundary) {
177 strbuf_release(boundary);
178 free(boundary);
179 }
180}
181
c6905e45
JH
182static void handle_content_transfer_encoding(struct mailinfo *mi,
183 const struct strbuf *line)
184{
185 if (strcasestr(line->buf, "base64"))
186 mi->transfer_encoding = TE_BASE64;
187 else if (strcasestr(line->buf, "quoted-printable"))
188 mi->transfer_encoding = TE_QP;
189 else
190 mi->transfer_encoding = TE_DONTCARE;
191}
192
193static int is_multipart_boundary(struct mailinfo *mi, const struct strbuf *line)
194{
195 struct strbuf *content_top = *(mi->content_top);
196
197 return ((content_top->len <= line->len) &&
198 !memcmp(line->buf, content_top->buf, content_top->len));
199}
200
201static void cleanup_subject(struct mailinfo *mi, struct strbuf *subject)
202{
203 size_t at = 0;
204
205 while (at < subject->len) {
206 char *pos;
207 size_t remove;
208
209 switch (subject->buf[at]) {
210 case 'r': case 'R':
211 if (subject->len <= at + 3)
212 break;
213 if ((subject->buf[at + 1] == 'e' ||
214 subject->buf[at + 1] == 'E') &&
215 subject->buf[at + 2] == ':') {
216 strbuf_remove(subject, at, 3);
217 continue;
218 }
219 at++;
220 break;
221 case ' ': case '\t': case ':':
222 strbuf_remove(subject, at, 1);
223 continue;
224 case '[':
225 pos = strchr(subject->buf + at, ']');
226 if (!pos)
227 break;
228 remove = pos - subject->buf + at + 1;
229 if (!mi->keep_non_patch_brackets_in_subject ||
230 (7 <= remove &&
231 memmem(subject->buf + at, remove, "PATCH", 5)))
232 strbuf_remove(subject, at, remove);
233 else {
234 at += remove;
235 /*
236 * If the input had a space after the ], keep
237 * it. We don't bother with finding the end of
238 * the space, since we later normalize it
239 * anyway.
240 */
241 if (isspace(subject->buf[at]))
242 at += 1;
243 }
244 continue;
245 }
246 break;
247 }
248 strbuf_trim(subject);
249}
250
251#define MAX_HDR_PARSED 10
252static const char *header[MAX_HDR_PARSED] = {
253 "From","Subject","Date",
254};
255
256static inline int cmp_header(const struct strbuf *line, const char *hdr)
257{
258 int len = strlen(hdr);
259 return !strncasecmp(line->buf, hdr, len) && line->len > len &&
260 line->buf[len] == ':' && isspace(line->buf[len + 1]);
261}
262
263static int is_format_patch_separator(const char *line, int len)
264{
265 static const char SAMPLE[] =
266 "From e6807f3efca28b30decfecb1732a56c7db1137ee Mon Sep 17 00:00:00 2001\n";
267 const char *cp;
268
269 if (len != strlen(SAMPLE))
270 return 0;
271 if (!skip_prefix(line, "From ", &cp))
272 return 0;
273 if (strspn(cp, "0123456789abcdef") != 40)
274 return 0;
275 cp += 40;
276 return !memcmp(SAMPLE + (cp - line), cp, strlen(SAMPLE) - (cp - line));
277}
278
279static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047)
280{
281 const char *in = q_seg->buf;
282 int c;
283 struct strbuf *out = xmalloc(sizeof(struct strbuf));
284 strbuf_init(out, q_seg->len);
285
286 while ((c = *in++) != 0) {
287 if (c == '=') {
288 int d = *in++;
289 if (d == '\n' || !d)
290 break; /* drop trailing newline */
291 strbuf_addch(out, (hexval(d) << 4) | hexval(*in++));
292 continue;
293 }
294 if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
295 c = 0x20;
296 strbuf_addch(out, c);
297 }
298 return out;
299}
300
301static struct strbuf *decode_b_segment(const struct strbuf *b_seg)
302{
303 /* Decode in..ep, possibly in-place to ot */
304 int c, pos = 0, acc = 0;
305 const char *in = b_seg->buf;
306 struct strbuf *out = xmalloc(sizeof(struct strbuf));
307 strbuf_init(out, b_seg->len);
308
309 while ((c = *in++) != 0) {
310 if (c == '+')
311 c = 62;
312 else if (c == '/')
313 c = 63;
314 else if ('A' <= c && c <= 'Z')
315 c -= 'A';
316 else if ('a' <= c && c <= 'z')
317 c -= 'a' - 26;
318 else if ('0' <= c && c <= '9')
319 c -= '0' - 52;
320 else
321 continue; /* garbage */
322 switch (pos++) {
323 case 0:
324 acc = (c << 2);
325 break;
326 case 1:
327 strbuf_addch(out, (acc | (c >> 4)));
328 acc = (c & 15) << 4;
329 break;
330 case 2:
331 strbuf_addch(out, (acc | (c >> 2)));
332 acc = (c & 3) << 6;
333 break;
334 case 3:
335 strbuf_addch(out, (acc | c));
336 acc = pos = 0;
337 break;
338 }
339 }
340 return out;
341}
342
669b963a
JH
343static int convert_to_utf8(struct mailinfo *mi,
344 struct strbuf *line, const char *charset)
c6905e45
JH
345{
346 char *out;
347
348 if (!mi->metainfo_charset || !charset || !*charset)
669b963a 349 return 0;
c6905e45
JH
350
351 if (same_encoding(mi->metainfo_charset, charset))
669b963a 352 return 0;
c6905e45 353 out = reencode_string(line->buf, mi->metainfo_charset, charset);
6ac617a3
JH
354 if (!out) {
355 mi->input_error = -1;
669b963a
JH
356 return error("cannot convert from %s to %s",
357 charset, mi->metainfo_charset);
6ac617a3 358 }
c6905e45 359 strbuf_attach(line, out, strlen(out), strlen(out));
669b963a 360 return 0;
c6905e45
JH
361}
362
363static void decode_header(struct mailinfo *mi, struct strbuf *it)
364{
365 char *in, *ep, *cp;
366 struct strbuf outbuf = STRBUF_INIT, *dec;
367 struct strbuf charset_q = STRBUF_INIT, piecebuf = STRBUF_INIT;
6ac617a3 368 int found_error = 1; /* pessimism */
c6905e45
JH
369
370 in = it->buf;
371 while (in - it->buf <= it->len && (ep = strstr(in, "=?")) != NULL) {
372 int encoding;
373 strbuf_reset(&charset_q);
374 strbuf_reset(&piecebuf);
375
376 if (in != ep) {
377 /*
378 * We are about to process an encoded-word
379 * that begins at ep, but there is something
380 * before the encoded word.
381 */
382 char *scan;
383 for (scan = in; scan < ep; scan++)
384 if (!isspace(*scan))
385 break;
386
387 if (scan != ep || in == it->buf) {
388 /*
389 * We should not lose that "something",
390 * unless we have just processed an
391 * encoded-word, and there is only LWS
392 * before the one we are about to process.
393 */
394 strbuf_add(&outbuf, in, ep - in);
395 }
396 }
397 /* E.g.
398 * ep : "=?iso-2022-jp?B?GyR...?= foo"
399 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
400 */
401 ep += 2;
402
403 if (ep - it->buf >= it->len || !(cp = strchr(ep, '?')))
404 goto release_return;
405
406 if (cp + 3 - it->buf > it->len)
407 goto release_return;
408 strbuf_add(&charset_q, ep, cp - ep);
409
410 encoding = cp[1];
411 if (!encoding || cp[2] != '?')
412 goto release_return;
413 ep = strstr(cp + 3, "?=");
414 if (!ep)
415 goto release_return;
416 strbuf_add(&piecebuf, cp + 3, ep - cp - 3);
417 switch (tolower(encoding)) {
418 default:
419 goto release_return;
420 case 'b':
421 dec = decode_b_segment(&piecebuf);
422 break;
423 case 'q':
424 dec = decode_q_segment(&piecebuf, 1);
425 break;
426 }
669b963a
JH
427 if (convert_to_utf8(mi, dec, charset_q.buf))
428 goto release_return;
c6905e45
JH
429
430 strbuf_addbuf(&outbuf, dec);
431 strbuf_release(dec);
432 free(dec);
433 in = ep + 2;
434 }
435 strbuf_addstr(&outbuf, in);
436 strbuf_reset(it);
437 strbuf_addbuf(it, &outbuf);
6ac617a3 438 found_error = 0;
c6905e45
JH
439release_return:
440 strbuf_release(&outbuf);
441 strbuf_release(&charset_q);
442 strbuf_release(&piecebuf);
6ac617a3
JH
443
444 if (found_error)
445 mi->input_error = -1;
c6905e45
JH
446}
447
448static int check_header(struct mailinfo *mi,
449 const struct strbuf *line,
450 struct strbuf *hdr_data[], int overwrite)
451{
452 int i, ret = 0, len;
453 struct strbuf sb = STRBUF_INIT;
454
455 /* search for the interesting parts */
456 for (i = 0; header[i]; i++) {
457 int len = strlen(header[i]);
458 if ((!hdr_data[i] || overwrite) && cmp_header(line, header[i])) {
459 /* Unwrap inline B and Q encoding, and optionally
460 * normalize the meta information to utf8.
461 */
462 strbuf_add(&sb, line->buf + len + 2, line->len - len - 2);
463 decode_header(mi, &sb);
464 handle_header(&hdr_data[i], &sb);
465 ret = 1;
466 goto check_header_out;
467 }
468 }
469
470 /* Content stuff */
471 if (cmp_header(line, "Content-Type")) {
472 len = strlen("Content-Type: ");
473 strbuf_add(&sb, line->buf + len, line->len - len);
474 decode_header(mi, &sb);
475 strbuf_insert(&sb, 0, "Content-Type: ", len);
476 handle_content_type(mi, &sb);
477 ret = 1;
478 goto check_header_out;
479 }
480 if (cmp_header(line, "Content-Transfer-Encoding")) {
481 len = strlen("Content-Transfer-Encoding: ");
482 strbuf_add(&sb, line->buf + len, line->len - len);
483 decode_header(mi, &sb);
484 handle_content_transfer_encoding(mi, &sb);
485 ret = 1;
486 goto check_header_out;
487 }
488 if (cmp_header(line, "Message-Id")) {
489 len = strlen("Message-Id: ");
490 strbuf_add(&sb, line->buf + len, line->len - len);
491 decode_header(mi, &sb);
ecf30b23
RS
492 if (mi->add_message_id)
493 mi->message_id = strbuf_detach(&sb, NULL);
c6905e45
JH
494 ret = 1;
495 goto check_header_out;
496 }
497
c6905e45
JH
498check_header_out:
499 strbuf_release(&sb);
500 return ret;
501}
502
503static void decode_transfer_encoding(struct mailinfo *mi, struct strbuf *line)
504{
505 struct strbuf *ret;
506
507 switch (mi->transfer_encoding) {
508 case TE_QP:
509 ret = decode_q_segment(line, 0);
510 break;
511 case TE_BASE64:
512 ret = decode_b_segment(line);
513 break;
514 case TE_DONTCARE:
515 default:
516 return;
517 }
518 strbuf_reset(line);
519 strbuf_addbuf(line, ret);
520 strbuf_release(ret);
521 free(ret);
522}
523
524static inline int patchbreak(const struct strbuf *line)
525{
526 size_t i;
527
528 /* Beginning of a "diff -" header? */
529 if (starts_with(line->buf, "diff -"))
530 return 1;
531
532 /* CVS "Index: " line? */
533 if (starts_with(line->buf, "Index: "))
534 return 1;
535
536 /*
537 * "--- <filename>" starts patches without headers
538 * "---<sp>*" is a manual separator
539 */
540 if (line->len < 4)
541 return 0;
542
543 if (starts_with(line->buf, "---")) {
544 /* space followed by a filename? */
545 if (line->buf[3] == ' ' && !isspace(line->buf[4]))
546 return 1;
547 /* Just whitespace? */
548 for (i = 3; i < line->len; i++) {
549 unsigned char c = line->buf[i];
550 if (c == '\n')
551 return 1;
552 if (!isspace(c))
553 break;
554 }
555 return 0;
556 }
557 return 0;
558}
559
9c5681da 560static int is_scissors_line(const char *line)
c6905e45 561{
9c5681da 562 const char *c;
c6905e45 563 int scissors = 0, gap = 0;
9c5681da
JT
564 const char *first_nonblank = NULL, *last_nonblank = NULL;
565 int visible, perforation = 0, in_perforation = 0;
c6905e45 566
9c5681da
JT
567 for (c = line; *c; c++) {
568 if (isspace(*c)) {
c6905e45
JH
569 if (in_perforation) {
570 perforation++;
571 gap++;
572 }
573 continue;
574 }
9c5681da
JT
575 last_nonblank = c;
576 if (first_nonblank == NULL)
577 first_nonblank = c;
578 if (*c == '-') {
c6905e45
JH
579 in_perforation = 1;
580 perforation++;
581 continue;
582 }
9c5681da
JT
583 if ((!memcmp(c, ">8", 2) || !memcmp(c, "8<", 2) ||
584 !memcmp(c, ">%", 2) || !memcmp(c, "%<", 2))) {
c6905e45
JH
585 in_perforation = 1;
586 perforation += 2;
587 scissors += 2;
9c5681da 588 c++;
c6905e45
JH
589 continue;
590 }
591 in_perforation = 0;
592 }
593
594 /*
595 * The mark must be at least 8 bytes long (e.g. "-- >8 --").
596 * Even though there can be arbitrary cruft on the same line
597 * (e.g. "cut here"), in order to avoid misidentification, the
598 * perforation must occupy more than a third of the visible
599 * width of the line, and dashes and scissors must occupy more
600 * than half of the perforation.
601 */
602
9c5681da
JT
603 if (first_nonblank && last_nonblank)
604 visible = last_nonblank - first_nonblank + 1;
605 else
606 visible = 0;
c6905e45
JH
607 return (scissors && 8 <= visible &&
608 visible < perforation * 3 &&
609 gap * 2 < perforation);
610}
611
334192b4
JT
612static int check_inbody_header(struct mailinfo *mi, const struct strbuf *line)
613{
614 if (starts_with(line->buf, ">From") && isspace(line->buf[5]))
615 return is_format_patch_separator(line->buf + 1, line->len - 1);
616 if (starts_with(line->buf, "[PATCH]") && isspace(line->buf[7])) {
617 int i;
618 for (i = 0; header[i]; i++)
619 if (!strcmp("Subject", header[i])) {
620 handle_header(&mi->s_hdr_data[i], line);
621 return 1;
622 }
623 return 0;
624 }
625 return check_header(mi, line, mi->s_hdr_data, 0);
626}
627
c6905e45
JH
628static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
629{
630 assert(!mi->filter_stage);
631
632 if (mi->header_stage) {
633 if (!line->len || (line->len == 1 && line->buf[0] == '\n'))
634 return 0;
635 }
636
637 if (mi->use_inbody_headers && mi->header_stage) {
334192b4 638 mi->header_stage = check_inbody_header(mi, line);
c6905e45
JH
639 if (mi->header_stage)
640 return 0;
641 } else
642 /* Only trim the first (blank) line of the commit message
643 * when ignoring in-body headers.
644 */
645 mi->header_stage = 0;
646
647 /* normalize the log message to UTF-8. */
669b963a 648 if (convert_to_utf8(mi, line, mi->charset.buf))
6ac617a3 649 return 0; /* mi->input_error already set */
c6905e45 650
9c5681da 651 if (mi->use_scissors && is_scissors_line(line->buf)) {
c6905e45
JH
652 int i;
653
654 strbuf_setlen(&mi->log_message, 0);
655 mi->header_stage = 1;
656
657 /*
658 * We may have already read "secondary headers"; purge
659 * them to give ourselves a clean restart.
660 */
661 for (i = 0; header[i]; i++) {
662 if (mi->s_hdr_data[i])
663 strbuf_release(mi->s_hdr_data[i]);
664 mi->s_hdr_data[i] = NULL;
665 }
666 return 0;
667 }
668
669 if (patchbreak(line)) {
670 if (mi->message_id)
671 strbuf_addf(&mi->log_message,
672 "Message-Id: %s\n", mi->message_id);
673 return 1;
674 }
675
676 strbuf_addbuf(&mi->log_message, line);
677 return 0;
678}
679
680static void handle_patch(struct mailinfo *mi, const struct strbuf *line)
681{
682 fwrite(line->buf, 1, line->len, mi->patchfile);
683 mi->patch_lines++;
684}
685
686static void handle_filter(struct mailinfo *mi, struct strbuf *line)
687{
688 switch (mi->filter_stage) {
689 case 0:
690 if (!handle_commit_msg(mi, line))
691 break;
692 mi->filter_stage++;
693 case 1:
694 handle_patch(mi, line);
695 break;
696 }
697}
698
699static int is_rfc2822_header(const struct strbuf *line)
700{
701 /*
702 * The section that defines the loosest possible
703 * field name is "3.6.8 Optional fields".
704 *
705 * optional-field = field-name ":" unstructured CRLF
706 * field-name = 1*ftext
707 * ftext = %d33-57 / %59-126
708 */
709 int ch;
710 char *cp = line->buf;
711
712 /* Count mbox From headers as headers */
713 if (starts_with(cp, "From ") || starts_with(cp, ">From "))
714 return 1;
715
716 while ((ch = *cp++)) {
717 if (ch == ':')
718 return 1;
719 if ((33 <= ch && ch <= 57) ||
720 (59 <= ch && ch <= 126))
721 continue;
722 break;
723 }
724 return 0;
725}
726
727static int read_one_header_line(struct strbuf *line, FILE *in)
728{
729 struct strbuf continuation = STRBUF_INIT;
730
731 /* Get the first part of the line. */
8f309aeb 732 if (strbuf_getline_lf(line, in))
c6905e45
JH
733 return 0;
734
735 /*
736 * Is it an empty line or not a valid rfc2822 header?
737 * If so, stop here, and return false ("not a header")
738 */
739 strbuf_rtrim(line);
740 if (!line->len || !is_rfc2822_header(line)) {
741 /* Re-add the newline */
742 strbuf_addch(line, '\n');
743 return 0;
744 }
745
746 /*
747 * Now we need to eat all the continuation lines..
748 * Yuck, 2822 header "folding"
749 */
750 for (;;) {
751 int peek;
752
753 peek = fgetc(in); ungetc(peek, in);
754 if (peek != ' ' && peek != '\t')
755 break;
8f309aeb 756 if (strbuf_getline_lf(&continuation, in))
c6905e45
JH
757 break;
758 continuation.buf[0] = ' ';
759 strbuf_rtrim(&continuation);
760 strbuf_addbuf(line, &continuation);
761 }
762 strbuf_release(&continuation);
763
764 return 1;
765}
766
767static int find_boundary(struct mailinfo *mi, struct strbuf *line)
768{
8f309aeb 769 while (!strbuf_getline_lf(line, mi->input)) {
c6905e45
JH
770 if (*(mi->content_top) && is_multipart_boundary(mi, line))
771 return 1;
772 }
773 return 0;
774}
775
776static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
777{
778 struct strbuf newline = STRBUF_INIT;
779
780 strbuf_addch(&newline, '\n');
781again:
782 if (line->len >= (*(mi->content_top))->len + 2 &&
783 !memcmp(line->buf + (*(mi->content_top))->len, "--", 2)) {
784 /* we hit an end boundary */
785 /* pop the current boundary off the stack */
786 strbuf_release(*(mi->content_top));
787 free(*(mi->content_top));
788 *(mi->content_top) = NULL;
789
790 /* technically won't happen as is_multipart_boundary()
791 will fail first. But just in case..
792 */
793 if (--mi->content_top < mi->content) {
6ac617a3
JH
794 error("Detected mismatched boundaries, can't recover");
795 mi->input_error = -1;
796 mi->content_top = mi->content;
797 return 0;
c6905e45
JH
798 }
799 handle_filter(mi, &newline);
800 strbuf_release(&newline);
6ac617a3
JH
801 if (mi->input_error)
802 return 0;
c6905e45
JH
803
804 /* skip to the next boundary */
805 if (!find_boundary(mi, line))
806 return 0;
807 goto again;
808 }
809
810 /* set some defaults */
811 mi->transfer_encoding = TE_DONTCARE;
812 strbuf_reset(&mi->charset);
813
814 /* slurp in this section's info */
815 while (read_one_header_line(line, mi->input))
816 check_header(mi, line, mi->p_hdr_data, 0);
817
818 strbuf_release(&newline);
819 /* replenish line */
8f309aeb 820 if (strbuf_getline_lf(line, mi->input))
c6905e45
JH
821 return 0;
822 strbuf_addch(line, '\n');
823 return 1;
824}
825
826static void handle_body(struct mailinfo *mi, struct strbuf *line)
827{
828 struct strbuf prev = STRBUF_INIT;
829
830 /* Skip up to the first boundary */
831 if (*(mi->content_top)) {
832 if (!find_boundary(mi, line))
833 goto handle_body_out;
834 }
835
836 do {
837 /* process any boundary lines */
838 if (*(mi->content_top) && is_multipart_boundary(mi, line)) {
839 /* flush any leftover */
840 if (prev.len) {
841 handle_filter(mi, &prev);
842 strbuf_reset(&prev);
843 }
844 if (!handle_boundary(mi, line))
845 goto handle_body_out;
846 }
847
848 /* Unwrap transfer encoding */
849 decode_transfer_encoding(mi, line);
850
851 switch (mi->transfer_encoding) {
852 case TE_BASE64:
853 case TE_QP:
854 {
855 struct strbuf **lines, **it, *sb;
856
857 /* Prepend any previous partial lines */
858 strbuf_insert(line, 0, prev.buf, prev.len);
859 strbuf_reset(&prev);
860
861 /*
862 * This is a decoded line that may contain
863 * multiple new lines. Pass only one chunk
864 * at a time to handle_filter()
865 */
866 lines = strbuf_split(line, '\n');
867 for (it = lines; (sb = *it); it++) {
868 if (*(it + 1) == NULL) /* The last line */
869 if (sb->buf[sb->len - 1] != '\n') {
870 /* Partial line, save it for later. */
871 strbuf_addbuf(&prev, sb);
872 break;
873 }
874 handle_filter(mi, sb);
875 }
876 /*
877 * The partial chunk is saved in "prev" and will be
878 * appended by the next iteration of read_line_with_nul().
879 */
880 strbuf_list_free(lines);
881 break;
882 }
883 default:
884 handle_filter(mi, line);
885 }
886
6ac617a3
JH
887 if (mi->input_error)
888 break;
c6905e45
JH
889 } while (!strbuf_getwholeline(line, mi->input, '\n'));
890
891handle_body_out:
892 strbuf_release(&prev);
893}
894
895static void output_header_lines(FILE *fout, const char *hdr, const struct strbuf *data)
896{
897 const char *sp = data->buf;
898 while (1) {
899 char *ep = strchr(sp, '\n');
900 int len;
901 if (!ep)
902 len = strlen(sp);
903 else
904 len = ep - sp;
905 fprintf(fout, "%s: %.*s\n", hdr, len, sp);
906 if (!ep)
907 break;
908 sp = ep + 1;
909 }
910}
911
912static void handle_info(struct mailinfo *mi)
913{
914 struct strbuf *hdr;
915 int i;
916
917 for (i = 0; header[i]; i++) {
918 /* only print inbody headers if we output a patch file */
919 if (mi->patch_lines && mi->s_hdr_data[i])
920 hdr = mi->s_hdr_data[i];
921 else if (mi->p_hdr_data[i])
922 hdr = mi->p_hdr_data[i];
923 else
924 continue;
925
926 if (!strcmp(header[i], "Subject")) {
927 if (!mi->keep_subject) {
928 cleanup_subject(mi, hdr);
929 cleanup_space(hdr);
930 }
931 output_header_lines(mi->output, "Subject", hdr);
932 } else if (!strcmp(header[i], "From")) {
933 cleanup_space(hdr);
934 handle_from(mi, hdr);
935 fprintf(mi->output, "Author: %s\n", mi->name.buf);
936 fprintf(mi->output, "Email: %s\n", mi->email.buf);
937 } else {
938 cleanup_space(hdr);
939 fprintf(mi->output, "%s: %s\n", header[i], hdr->buf);
940 }
941 }
942 fprintf(mi->output, "\n");
943}
944
945int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
946{
947 FILE *cmitmsg;
948 int peek;
949 struct strbuf line = STRBUF_INIT;
950
951 cmitmsg = fopen(msg, "w");
952 if (!cmitmsg) {
953 perror(msg);
954 return -1;
955 }
956 mi->patchfile = fopen(patch, "w");
957 if (!mi->patchfile) {
958 perror(patch);
959 fclose(cmitmsg);
960 return -1;
961 }
962
963 mi->p_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*(mi->p_hdr_data)));
964 mi->s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*(mi->s_hdr_data)));
965
966 do {
967 peek = fgetc(mi->input);
968 } while (isspace(peek));
969 ungetc(peek, mi->input);
970
971 /* process the email header */
972 while (read_one_header_line(&line, mi->input))
973 check_header(mi, &line, mi->p_hdr_data, 1);
974
975 handle_body(mi, &line);
976 fwrite(mi->log_message.buf, 1, mi->log_message.len, cmitmsg);
977 fclose(cmitmsg);
978 fclose(mi->patchfile);
979
980 handle_info(mi);
981 strbuf_release(&line);
6ac617a3 982 return mi->input_error;
c6905e45
JH
983}
984
985static int git_mailinfo_config(const char *var, const char *value, void *mi_)
986{
987 struct mailinfo *mi = mi_;
988
989 if (!starts_with(var, "mailinfo."))
990 return git_default_config(var, value, NULL);
991 if (!strcmp(var, "mailinfo.scissors")) {
992 mi->use_scissors = git_config_bool(var, value);
993 return 0;
994 }
995 /* perhaps others here */
996 return 0;
997}
998
999void setup_mailinfo(struct mailinfo *mi)
1000{
1001 memset(mi, 0, sizeof(*mi));
1002 strbuf_init(&mi->name, 0);
1003 strbuf_init(&mi->email, 0);
1004 strbuf_init(&mi->charset, 0);
1005 strbuf_init(&mi->log_message, 0);
1006 mi->header_stage = 1;
1007 mi->use_inbody_headers = 1;
1008 mi->content_top = mi->content;
85d9d9dd 1009 git_config(git_mailinfo_config, mi);
c6905e45
JH
1010}
1011
1012void clear_mailinfo(struct mailinfo *mi)
1013{
1014 int i;
1015
1016 strbuf_release(&mi->name);
1017 strbuf_release(&mi->email);
1018 strbuf_release(&mi->charset);
1019 free(mi->message_id);
1020
1021 for (i = 0; mi->p_hdr_data[i]; i++)
1022 strbuf_release(mi->p_hdr_data[i]);
1023 free(mi->p_hdr_data);
1024 for (i = 0; mi->s_hdr_data[i]; i++)
1025 strbuf_release(mi->s_hdr_data[i]);
1026 free(mi->s_hdr_data);
1027
1028 while (mi->content < mi->content_top) {
1029 free(*(mi->content_top));
1030 mi->content_top--;
1031 }
1032
1033 strbuf_release(&mi->log_message);
1034}