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