]>
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 DR |
12 | static int keep_subject; |
13 | static const char *metainfo_charset; | |
3b6121f6 LS |
14 | static struct strbuf line = STRBUF_INIT; |
15 | static struct strbuf name = STRBUF_INIT; | |
16 | static struct strbuf email = STRBUF_INIT; | |
2744b234 | 17 | |
d4a9ce78 JH |
18 | static enum { |
19 | TE_DONTCARE, TE_QP, TE_BASE64, | |
20 | } transfer_encoding; | |
87ab7992 DZ |
21 | static enum { |
22 | TYPE_TEXT, TYPE_OTHER, | |
23 | } message_type; | |
d4a9ce78 | 24 | |
3b6121f6 | 25 | static struct strbuf charset = STRBUF_INIT; |
96f1e58f | 26 | static int patch_lines; |
3b6121f6 | 27 | static struct strbuf **p_hdr_data, **s_hdr_data; |
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 | ||
160 | if (!ap) { | |
3b6121f6 | 161 | strbuf_setlen(attr, 0); |
d4a9ce78 JH |
162 | return 0; |
163 | } | |
164 | ap += strlen(name); | |
165 | if (*ap == '"') { | |
166 | ap++; | |
167 | ends = "\""; | |
168 | } | |
169 | else | |
170 | ends = "; \t"; | |
171 | sz = strcspn(ap, ends); | |
3b6121f6 | 172 | strbuf_add(attr, ap, sz); |
d4a9ce78 JH |
173 | return 1; |
174 | } | |
175 | ||
3b6121f6 | 176 | static struct strbuf *content[MAX_BOUNDARIES]; |
87ab7992 | 177 | |
3b6121f6 | 178 | static struct strbuf **content_top = content; |
87ab7992 | 179 | |
3b6121f6 | 180 | static void handle_content_type(struct strbuf *line) |
d4a9ce78 | 181 | { |
3b6121f6 LS |
182 | struct strbuf *boundary = xmalloc(sizeof(struct strbuf)); |
183 | strbuf_init(boundary, line->len); | |
87ab7992 | 184 | |
3b6121f6 | 185 | if (!strcasestr(line->buf, "text/")) |
87ab7992 | 186 | message_type = TYPE_OTHER; |
3b6121f6 LS |
187 | if (slurp_attr(line->buf, "boundary=", boundary)) { |
188 | strbuf_insert(boundary, 0, "--", 2); | |
289796dd | 189 | if (++content_top > &content[MAX_BOUNDARIES]) { |
87ab7992 DZ |
190 | fprintf(stderr, "Too many boundaries to handle\n"); |
191 | exit(1); | |
192 | } | |
3b6121f6 LS |
193 | *content_top = boundary; |
194 | boundary = NULL; | |
b893f091 | 195 | } |
ed1e3985 | 196 | slurp_attr(line->buf, "charset=", &charset); |
3b6121f6 LS |
197 | |
198 | if (boundary) { | |
199 | strbuf_release(boundary); | |
200 | free(boundary); | |
d4a9ce78 | 201 | } |
d4a9ce78 JH |
202 | } |
203 | ||
3b6121f6 | 204 | static void handle_content_transfer_encoding(const struct strbuf *line) |
d4a9ce78 | 205 | { |
3b6121f6 | 206 | if (strcasestr(line->buf, "base64")) |
d4a9ce78 | 207 | transfer_encoding = TE_BASE64; |
3b6121f6 | 208 | else if (strcasestr(line->buf, "quoted-printable")) |
d4a9ce78 JH |
209 | transfer_encoding = TE_QP; |
210 | else | |
211 | transfer_encoding = TE_DONTCARE; | |
2744b234 LT |
212 | } |
213 | ||
3b6121f6 | 214 | static int is_multipart_boundary(const struct strbuf *line) |
d4a9ce78 | 215 | { |
a9fd1383 JH |
216 | return (((*content_top)->len <= line->len) && |
217 | !memcmp(line->buf, (*content_top)->buf, (*content_top)->len)); | |
d4a9ce78 JH |
218 | } |
219 | ||
3b6121f6 | 220 | static void cleanup_subject(struct strbuf *subject) |
2744b234 | 221 | { |
3b6121f6 LS |
222 | char *pos; |
223 | size_t remove; | |
224 | while (subject->len) { | |
225 | switch (*subject->buf) { | |
2744b234 | 226 | case 'r': case 'R': |
3b6121f6 LS |
227 | if (subject->len <= 3) |
228 | break; | |
229 | if (!memcmp(subject->buf + 1, "e:", 2)) { | |
230 | strbuf_remove(subject, 0, 3); | |
2744b234 LT |
231 | continue; |
232 | } | |
233 | break; | |
234 | case ' ': case '\t': case ':': | |
3b6121f6 | 235 | strbuf_remove(subject, 0, 1); |
2744b234 | 236 | continue; |
2744b234 | 237 | case '[': |
3b6121f6 | 238 | if ((pos = strchr(subject->buf, ']'))) { |
1e102bf7 JH |
239 | remove = pos - subject->buf; |
240 | if (remove <= (subject->len - remove) * 2) { | |
241 | strbuf_remove(subject, 0, remove + 1); | |
3b6121f6 LS |
242 | continue; |
243 | } | |
244 | } else | |
245 | strbuf_remove(subject, 0, 1); | |
2744b234 LT |
246 | break; |
247 | } | |
3b6121f6 LS |
248 | strbuf_trim(subject); |
249 | return; | |
2744b234 | 250 | } |
34488e3c | 251 | } |
2744b234 | 252 | |
3b6121f6 | 253 | static void cleanup_space(struct strbuf *sb) |
2744b234 | 254 | { |
3b6121f6 LS |
255 | size_t pos, cnt; |
256 | for (pos = 0; pos < sb->len; pos++) { | |
257 | if (isspace(sb->buf[pos])) { | |
258 | sb->buf[pos] = ' '; | |
259 | for (cnt = 0; isspace(sb->buf[pos + cnt + 1]); cnt++); | |
260 | strbuf_remove(sb, pos + 1, cnt); | |
2744b234 LT |
261 | } |
262 | } | |
263 | } | |
264 | ||
3b6121f6 | 265 | static void decode_header(struct strbuf *line); |
538dfe73 | 266 | static const char *header[MAX_HDR_PARSED] = { |
87ab7992 | 267 | "From","Subject","Date", |
d4a9ce78 JH |
268 | }; |
269 | ||
3b6121f6 | 270 | static inline int cmp_header(const struct strbuf *line, const char *hdr) |
d4a9ce78 | 271 | { |
3b6121f6 LS |
272 | int len = strlen(hdr); |
273 | return !strncasecmp(line->buf, hdr, len) && line->len > len && | |
274 | line->buf[len] == ':' && isspace(line->buf[len + 1]); | |
275 | } | |
d4a9ce78 | 276 | |
3b6121f6 LS |
277 | static int check_header(const struct strbuf *line, |
278 | struct strbuf *hdr_data[], int overwrite) | |
279 | { | |
280 | int i, ret = 0, len; | |
281 | struct strbuf sb = STRBUF_INIT; | |
87ab7992 DZ |
282 | /* search for the interesting parts */ |
283 | for (i = 0; header[i]; i++) { | |
284 | int len = strlen(header[i]); | |
3b6121f6 | 285 | if ((!hdr_data[i] || overwrite) && cmp_header(line, header[i])) { |
33504530 EB |
286 | /* Unwrap inline B and Q encoding, and optionally |
287 | * normalize the meta information to utf8. | |
288 | */ | |
3b6121f6 LS |
289 | strbuf_add(&sb, line->buf + len + 2, line->len - len - 2); |
290 | decode_header(&sb); | |
291 | handle_header(&hdr_data[i], &sb); | |
292 | ret = 1; | |
293 | goto check_header_out; | |
d4a9ce78 JH |
294 | } |
295 | } | |
d4a9ce78 | 296 | |
87ab7992 | 297 | /* Content stuff */ |
3b6121f6 LS |
298 | if (cmp_header(line, "Content-Type")) { |
299 | len = strlen("Content-Type: "); | |
300 | strbuf_add(&sb, line->buf + len, line->len - len); | |
301 | decode_header(&sb); | |
302 | strbuf_insert(&sb, 0, "Content-Type: ", len); | |
303 | handle_content_type(&sb); | |
304 | ret = 1; | |
305 | goto check_header_out; | |
306 | } | |
307 | if (cmp_header(line, "Content-Transfer-Encoding")) { | |
308 | len = strlen("Content-Transfer-Encoding: "); | |
309 | strbuf_add(&sb, line->buf + len, line->len - len); | |
310 | decode_header(&sb); | |
311 | handle_content_transfer_encoding(&sb); | |
312 | ret = 1; | |
313 | goto check_header_out; | |
87ab7992 DZ |
314 | } |
315 | ||
316 | /* for inbody stuff */ | |
3b6121f6 LS |
317 | if (!prefixcmp(line->buf, ">From") && isspace(line->buf[5])) { |
318 | ret = 1; /* Should this return 0? */ | |
319 | goto check_header_out; | |
320 | } | |
321 | if (!prefixcmp(line->buf, "[PATCH]") && isspace(line->buf[7])) { | |
87ab7992 | 322 | for (i = 0; header[i]; i++) { |
e9fe804a | 323 | if (!memcmp("Subject", header[i], 7)) { |
3b6121f6 LS |
324 | handle_header(&hdr_data[i], line); |
325 | ret = 1; | |
326 | goto check_header_out; | |
87ab7992 DZ |
327 | } |
328 | } | |
329 | } | |
330 | ||
3b6121f6 LS |
331 | check_header_out: |
332 | strbuf_release(&sb); | |
333 | return ret; | |
d4a9ce78 JH |
334 | } |
335 | ||
3b6121f6 | 336 | static int is_rfc2822_header(const struct strbuf *line) |
ef29c117 JH |
337 | { |
338 | /* | |
339 | * The section that defines the loosest possible | |
340 | * field name is "3.6.8 Optional fields". | |
341 | * | |
342 | * optional-field = field-name ":" unstructured CRLF | |
343 | * field-name = 1*ftext | |
344 | * ftext = %d33-57 / %59-126 | |
345 | */ | |
346 | int ch; | |
3b6121f6 | 347 | char *cp = line->buf; |
34fc5cef LT |
348 | |
349 | /* Count mbox From headers as headers */ | |
3b6121f6 | 350 | if (!prefixcmp(cp, "From ") || !prefixcmp(cp, ">From ")) |
34fc5cef LT |
351 | return 1; |
352 | ||
ef29c117 JH |
353 | while ((ch = *cp++)) { |
354 | if (ch == ':') | |
3b6121f6 | 355 | return 1; |
ef29c117 JH |
356 | if ((33 <= ch && ch <= 57) || |
357 | (59 <= ch && ch <= 126)) | |
358 | continue; | |
359 | break; | |
360 | } | |
361 | return 0; | |
362 | } | |
363 | ||
3b6121f6 | 364 | static int read_one_header_line(struct strbuf *line, FILE *in) |
d4a9ce78 | 365 | { |
34fc5cef | 366 | /* Get the first part of the line. */ |
3b6121f6 | 367 | if (strbuf_getline(line, in, '\n')) |
34fc5cef LT |
368 | return 0; |
369 | ||
370 | /* | |
371 | * Is it an empty line or not a valid rfc2822 header? | |
372 | * If so, stop here, and return false ("not a header") | |
373 | */ | |
3b6121f6 LS |
374 | strbuf_rtrim(line); |
375 | if (!line->len || !is_rfc2822_header(line)) { | |
34fc5cef | 376 | /* Re-add the newline */ |
3b6121f6 | 377 | strbuf_addch(line, '\n'); |
34fc5cef LT |
378 | return 0; |
379 | } | |
380 | ||
381 | /* | |
382 | * Now we need to eat all the continuation lines.. | |
383 | * Yuck, 2822 header "folding" | |
384 | */ | |
385 | for (;;) { | |
3b6121f6 LS |
386 | int peek; |
387 | struct strbuf continuation = STRBUF_INIT; | |
34fc5cef | 388 | |
f30b2028 EB |
389 | peek = fgetc(in); ungetc(peek, in); |
390 | if (peek != ' ' && peek != '\t') | |
391 | break; | |
3b6121f6 | 392 | if (strbuf_getline(&continuation, in, '\n')) |
34fc5cef | 393 | break; |
3b6121f6 LS |
394 | continuation.buf[0] = '\n'; |
395 | strbuf_rtrim(&continuation); | |
396 | strbuf_addbuf(line, &continuation); | |
d4a9ce78 | 397 | } |
34fc5cef LT |
398 | |
399 | return 1; | |
d4a9ce78 JH |
400 | } |
401 | ||
3b6121f6 | 402 | static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047) |
d4a9ce78 | 403 | { |
3b6121f6 | 404 | const char *in = q_seg->buf; |
d4a9ce78 | 405 | int c; |
3b6121f6 LS |
406 | struct strbuf *out = xmalloc(sizeof(struct strbuf)); |
407 | strbuf_init(out, q_seg->len); | |
408 | ||
409 | while ((c = *in++) != 0) { | |
d4a9ce78 JH |
410 | if (c == '=') { |
411 | int d = *in++; | |
412 | if (d == '\n' || !d) | |
413 | break; /* drop trailing newline */ | |
3b6121f6 | 414 | strbuf_addch(out, (hexval(d) << 4) | hexval(*in++)); |
75731930 | 415 | continue; |
d4a9ce78 | 416 | } |
75731930 JH |
417 | if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */ |
418 | c = 0x20; | |
3b6121f6 | 419 | strbuf_addch(out, c); |
d4a9ce78 | 420 | } |
3b6121f6 | 421 | return out; |
d4a9ce78 JH |
422 | } |
423 | ||
3b6121f6 | 424 | static struct strbuf *decode_b_segment(const struct strbuf *b_seg) |
d4a9ce78 JH |
425 | { |
426 | /* Decode in..ep, possibly in-place to ot */ | |
427 | int c, pos = 0, acc = 0; | |
3b6121f6 LS |
428 | const char *in = b_seg->buf; |
429 | struct strbuf *out = xmalloc(sizeof(struct strbuf)); | |
430 | strbuf_init(out, b_seg->len); | |
d4a9ce78 | 431 | |
3b6121f6 | 432 | while ((c = *in++) != 0) { |
d4a9ce78 JH |
433 | if (c == '+') |
434 | c = 62; | |
435 | else if (c == '/') | |
436 | c = 63; | |
437 | else if ('A' <= c && c <= 'Z') | |
438 | c -= 'A'; | |
439 | else if ('a' <= c && c <= 'z') | |
440 | c -= 'a' - 26; | |
441 | else if ('0' <= c && c <= '9') | |
442 | c -= '0' - 52; | |
d4a9ce78 JH |
443 | else |
444 | continue; /* garbage */ | |
445 | switch (pos++) { | |
446 | case 0: | |
447 | acc = (c << 2); | |
448 | break; | |
449 | case 1: | |
3b6121f6 | 450 | strbuf_addch(out, (acc | (c >> 4))); |
d4a9ce78 JH |
451 | acc = (c & 15) << 4; |
452 | break; | |
453 | case 2: | |
3b6121f6 | 454 | strbuf_addch(out, (acc | (c >> 2))); |
d4a9ce78 JH |
455 | acc = (c & 3) << 6; |
456 | break; | |
457 | case 3: | |
3b6121f6 | 458 | strbuf_addch(out, (acc | c)); |
d4a9ce78 JH |
459 | acc = pos = 0; |
460 | break; | |
461 | } | |
462 | } | |
3b6121f6 | 463 | return out; |
d4a9ce78 JH |
464 | } |
465 | ||
b59d398b LT |
466 | /* |
467 | * When there is no known charset, guess. | |
468 | * | |
469 | * Right now we assume that if the target is UTF-8 (the default), | |
470 | * and it already looks like UTF-8 (which includes US-ASCII as its | |
471 | * subset, of course) then that is what it is and there is nothing | |
472 | * to do. | |
473 | * | |
474 | * Otherwise, we default to assuming it is Latin1 for historical | |
475 | * reasons. | |
476 | */ | |
3b6121f6 | 477 | static const char *guess_charset(const struct strbuf *line, const char *target_charset) |
b59d398b LT |
478 | { |
479 | if (is_encoding_utf8(target_charset)) { | |
3b6121f6 | 480 | if (is_utf8(line->buf)) |
b59d398b LT |
481 | return NULL; |
482 | } | |
62645006 | 483 | return "ISO8859-1"; |
b59d398b LT |
484 | } |
485 | ||
3b6121f6 | 486 | static void convert_to_utf8(struct strbuf *line, const char *charset) |
d4a9ce78 | 487 | { |
b59d398b LT |
488 | char *out; |
489 | ||
490 | if (!charset || !*charset) { | |
491 | charset = guess_charset(line, metainfo_charset); | |
492 | if (!charset) | |
493 | return; | |
494 | } | |
b45974a6 | 495 | |
ed1e3985 | 496 | if (!strcasecmp(metainfo_charset, charset)) |
7296096c | 497 | return; |
3b6121f6 | 498 | out = reencode_string(line->buf, metainfo_charset, charset); |
bb1091a4 | 499 | if (!out) |
d7530708 | 500 | die("cannot convert from %s to %s", |
b59d398b | 501 | charset, metainfo_charset); |
3b6121f6 | 502 | strbuf_attach(line, out, strlen(out), strlen(out)); |
d4a9ce78 JH |
503 | } |
504 | ||
3b6121f6 | 505 | static int decode_header_bq(struct strbuf *it) |
d4a9ce78 | 506 | { |
3b6121f6 LS |
507 | char *in, *ep, *cp; |
508 | struct strbuf outbuf = STRBUF_INIT, *dec; | |
509 | struct strbuf charset_q = STRBUF_INIT, piecebuf = STRBUF_INIT; | |
b75bf2c3 | 510 | int rfc2047 = 0; |
d4a9ce78 | 511 | |
3b6121f6 LS |
512 | in = it->buf; |
513 | while (in - it->buf <= it->len && (ep = strstr(in, "=?")) != NULL) { | |
514 | int encoding; | |
515 | strbuf_reset(&charset_q); | |
516 | strbuf_reset(&piecebuf); | |
b75bf2c3 JH |
517 | rfc2047 = 1; |
518 | ||
d4a9ce78 | 519 | if (in != ep) { |
353aaf2f KS |
520 | /* |
521 | * We are about to process an encoded-word | |
522 | * that begins at ep, but there is something | |
523 | * before the encoded word. | |
524 | */ | |
525 | char *scan; | |
526 | for (scan = in; scan < ep; scan++) | |
527 | if (!isspace(*scan)) | |
528 | break; | |
529 | ||
530 | if (scan != ep || in == it->buf) { | |
531 | /* | |
532 | * We should not lose that "something", | |
533 | * unless we have just processed an | |
534 | * encoded-word, and there is only LWS | |
535 | * before the one we are about to process. | |
536 | */ | |
537 | strbuf_add(&outbuf, in, ep - in); | |
538 | } | |
d4a9ce78 JH |
539 | } |
540 | /* E.g. | |
541 | * ep : "=?iso-2022-jp?B?GyR...?= foo" | |
542 | * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz" | |
543 | */ | |
544 | ep += 2; | |
3b6121f6 LS |
545 | |
546 | if (ep - it->buf >= it->len || !(cp = strchr(ep, '?'))) | |
547 | goto decode_header_bq_out; | |
548 | ||
549 | if (cp + 3 - it->buf > it->len) | |
550 | goto decode_header_bq_out; | |
551 | strbuf_add(&charset_q, ep, cp - ep); | |
3b6121f6 | 552 | |
d4a9ce78 JH |
553 | encoding = cp[1]; |
554 | if (!encoding || cp[2] != '?') | |
3b6121f6 | 555 | goto decode_header_bq_out; |
d4a9ce78 JH |
556 | ep = strstr(cp + 3, "?="); |
557 | if (!ep) | |
3b6121f6 LS |
558 | goto decode_header_bq_out; |
559 | strbuf_add(&piecebuf, cp + 3, ep - cp - 3); | |
d4a9ce78 JH |
560 | switch (tolower(encoding)) { |
561 | default: | |
3b6121f6 | 562 | goto decode_header_bq_out; |
d4a9ce78 | 563 | case 'b': |
3b6121f6 | 564 | dec = decode_b_segment(&piecebuf); |
d4a9ce78 JH |
565 | break; |
566 | case 'q': | |
3b6121f6 | 567 | dec = decode_q_segment(&piecebuf, 1); |
d4a9ce78 JH |
568 | break; |
569 | } | |
650e4be5 | 570 | if (metainfo_charset) |
3b6121f6 | 571 | convert_to_utf8(dec, charset_q.buf); |
8dabdfcc | 572 | |
3b6121f6 LS |
573 | strbuf_addbuf(&outbuf, dec); |
574 | strbuf_release(dec); | |
575 | free(dec); | |
d4a9ce78 JH |
576 | in = ep + 2; |
577 | } | |
3b6121f6 LS |
578 | strbuf_addstr(&outbuf, in); |
579 | strbuf_reset(it); | |
580 | strbuf_addbuf(it, &outbuf); | |
581 | decode_header_bq_out: | |
582 | strbuf_release(&outbuf); | |
583 | strbuf_release(&charset_q); | |
584 | strbuf_release(&piecebuf); | |
b75bf2c3 JH |
585 | return rfc2047; |
586 | } | |
587 | ||
3b6121f6 | 588 | static void decode_header(struct strbuf *it) |
b75bf2c3 | 589 | { |
3b6121f6 | 590 | if (decode_header_bq(it)) |
b75bf2c3 JH |
591 | return; |
592 | /* otherwise "it" is a straight copy of the input. | |
593 | * This can be binary guck but there is no charset specified. | |
594 | */ | |
595 | if (metainfo_charset) | |
3b6121f6 | 596 | convert_to_utf8(it, ""); |
d4a9ce78 JH |
597 | } |
598 | ||
3b6121f6 | 599 | static void decode_transfer_encoding(struct strbuf *line) |
d4a9ce78 | 600 | { |
3b6121f6 | 601 | struct strbuf *ret; |
d4a9ce78 JH |
602 | |
603 | switch (transfer_encoding) { | |
604 | case TE_QP: | |
3b6121f6 LS |
605 | ret = decode_q_segment(line, 0); |
606 | break; | |
d4a9ce78 | 607 | case TE_BASE64: |
3b6121f6 LS |
608 | ret = decode_b_segment(line); |
609 | break; | |
d4a9ce78 | 610 | case TE_DONTCARE: |
9aa23094 | 611 | default: |
3b6121f6 | 612 | return; |
d4a9ce78 | 613 | } |
3b6121f6 LS |
614 | strbuf_reset(line); |
615 | strbuf_addbuf(line, ret); | |
616 | strbuf_release(ret); | |
617 | free(ret); | |
d4a9ce78 JH |
618 | } |
619 | ||
3b6121f6 | 620 | static void handle_filter(struct strbuf *line); |
87ab7992 DZ |
621 | |
622 | static int find_boundary(void) | |
2744b234 | 623 | { |
3b6121f6 | 624 | while (!strbuf_getline(&line, fin, '\n')) { |
289796dd | 625 | if (*content_top && is_multipart_boundary(&line)) |
87ab7992 DZ |
626 | return 1; |
627 | } | |
628 | return 0; | |
629 | } | |
630 | ||
631 | static int handle_boundary(void) | |
632 | { | |
3b6121f6 LS |
633 | struct strbuf newline = STRBUF_INIT; |
634 | ||
635 | strbuf_addch(&newline, '\n'); | |
87ab7992 | 636 | again: |
3b6121f6 LS |
637 | if (line.len >= (*content_top)->len + 2 && |
638 | !memcmp(line.buf + (*content_top)->len, "--", 2)) { | |
87ab7992 DZ |
639 | /* we hit an end boundary */ |
640 | /* pop the current boundary off the stack */ | |
3b6121f6 LS |
641 | strbuf_release(*content_top); |
642 | free(*content_top); | |
643 | *content_top = NULL; | |
87ab7992 DZ |
644 | |
645 | /* technically won't happen as is_multipart_boundary() | |
646 | will fail first. But just in case.. | |
647 | */ | |
289796dd | 648 | if (--content_top < content) { |
87ab7992 DZ |
649 | fprintf(stderr, "Detected mismatched boundaries, " |
650 | "can't recover\n"); | |
651 | exit(1); | |
652 | } | |
3b6121f6 LS |
653 | handle_filter(&newline); |
654 | strbuf_release(&newline); | |
87ab7992 DZ |
655 | |
656 | /* skip to the next boundary */ | |
657 | if (!find_boundary()) | |
658 | return 0; | |
659 | goto again; | |
660 | } | |
661 | ||
662 | /* set some defaults */ | |
663 | transfer_encoding = TE_DONTCARE; | |
3b6121f6 | 664 | strbuf_reset(&charset); |
87ab7992 | 665 | message_type = TYPE_TEXT; |
d4a9ce78 | 666 | |
87ab7992 | 667 | /* slurp in this section's info */ |
3b6121f6 LS |
668 | while (read_one_header_line(&line, fin)) |
669 | check_header(&line, p_hdr_data, 0); | |
2744b234 | 670 | |
3b6121f6 | 671 | strbuf_release(&newline); |
a9fd1383 JH |
672 | /* replenish line */ |
673 | if (strbuf_getline(&line, fin, '\n')) | |
674 | return 0; | |
675 | strbuf_addch(&line, '\n'); | |
676 | return 1; | |
d4a9ce78 JH |
677 | } |
678 | ||
3b6121f6 | 679 | static inline int patchbreak(const struct strbuf *line) |
f0658cf2 | 680 | { |
3b6121f6 LS |
681 | size_t i; |
682 | ||
f0658cf2 | 683 | /* Beginning of a "diff -" header? */ |
3b6121f6 | 684 | if (!prefixcmp(line->buf, "diff -")) |
f0658cf2 DZ |
685 | return 1; |
686 | ||
687 | /* CVS "Index: " line? */ | |
3b6121f6 | 688 | if (!prefixcmp(line->buf, "Index: ")) |
f0658cf2 DZ |
689 | return 1; |
690 | ||
691 | /* | |
692 | * "--- <filename>" starts patches without headers | |
693 | * "---<sp>*" is a manual separator | |
694 | */ | |
3b6121f6 LS |
695 | if (line->len < 4) |
696 | return 0; | |
697 | ||
698 | if (!prefixcmp(line->buf, "---")) { | |
f0658cf2 | 699 | /* space followed by a filename? */ |
3b6121f6 | 700 | if (line->buf[3] == ' ' && !isspace(line->buf[4])) |
f0658cf2 DZ |
701 | return 1; |
702 | /* Just whitespace? */ | |
3b6121f6 LS |
703 | for (i = 3; i < line->len; i++) { |
704 | unsigned char c = line->buf[i]; | |
f0658cf2 DZ |
705 | if (c == '\n') |
706 | return 1; | |
707 | if (!isspace(c)) | |
708 | break; | |
709 | } | |
710 | return 0; | |
711 | } | |
712 | return 0; | |
713 | } | |
714 | ||
3b6121f6 | 715 | static int handle_commit_msg(struct strbuf *line) |
d4a9ce78 | 716 | { |
87ab7992 DZ |
717 | static int still_looking = 1; |
718 | ||
d4a9ce78 JH |
719 | if (!cmitmsg) |
720 | return 0; | |
2744b234 | 721 | |
87ab7992 | 722 | if (still_looking) { |
3b6121f6 LS |
723 | strbuf_ltrim(line); |
724 | if (!line->len) | |
725 | return 0; | |
726 | if ((still_looking = check_header(line, s_hdr_data, 0)) != 0) | |
87ab7992 DZ |
727 | return 0; |
728 | } | |
8b4525fb | 729 | |
86747c13 DZ |
730 | /* normalize the log message to UTF-8. */ |
731 | if (metainfo_charset) | |
3b6121f6 | 732 | convert_to_utf8(line, charset.buf); |
86747c13 | 733 | |
f0658cf2 | 734 | if (patchbreak(line)) { |
87ab7992 DZ |
735 | fclose(cmitmsg); |
736 | cmitmsg = NULL; | |
737 | return 1; | |
738 | } | |
8b4525fb | 739 | |
3b6121f6 | 740 | fputs(line->buf, cmitmsg); |
d4a9ce78 | 741 | return 0; |
2744b234 LT |
742 | } |
743 | ||
3b6121f6 | 744 | static void handle_patch(const struct strbuf *line) |
2744b234 | 745 | { |
3b6121f6 | 746 | fwrite(line->buf, 1, line->len, patchfile); |
87ab7992 | 747 | patch_lines++; |
2744b234 LT |
748 | } |
749 | ||
3b6121f6 | 750 | static void handle_filter(struct strbuf *line) |
2744b234 | 751 | { |
87ab7992 | 752 | static int filter = 0; |
2744b234 | 753 | |
3b6121f6 | 754 | /* filter tells us which part we left off on */ |
87ab7992 DZ |
755 | switch (filter) { |
756 | case 0: | |
3b6121f6 | 757 | if (!handle_commit_msg(line)) |
d4a9ce78 | 758 | break; |
87ab7992 DZ |
759 | filter++; |
760 | case 1: | |
3b6121f6 LS |
761 | handle_patch(line); |
762 | break; | |
2744b234 LT |
763 | } |
764 | } | |
765 | ||
87ab7992 | 766 | static void handle_body(void) |
1d8fa411 | 767 | { |
3b6121f6 LS |
768 | int len = 0; |
769 | struct strbuf prev = STRBUF_INIT; | |
d4a9ce78 JH |
770 | |
771 | /* Skip up to the first boundary */ | |
3b6121f6 | 772 | if (*content_top) { |
87ab7992 | 773 | if (!find_boundary()) |
3b6121f6 | 774 | goto handle_body_out; |
87ab7992 DZ |
775 | } |
776 | ||
777 | do { | |
3b6121f6 LS |
778 | strbuf_setlen(&line, line.len + len); |
779 | ||
87ab7992 | 780 | /* process any boundary lines */ |
3b6121f6 | 781 | if (*content_top && is_multipart_boundary(&line)) { |
87ab7992 | 782 | /* flush any leftover */ |
a9fd1383 JH |
783 | if (prev.len) { |
784 | handle_filter(&prev); | |
785 | strbuf_reset(&prev); | |
786 | } | |
87ab7992 | 787 | if (!handle_boundary()) |
3b6121f6 | 788 | goto handle_body_out; |
87ab7992 DZ |
789 | } |
790 | ||
86747c13 | 791 | /* Unwrap transfer encoding */ |
3b6121f6 | 792 | decode_transfer_encoding(&line); |
87ab7992 DZ |
793 | |
794 | switch (transfer_encoding) { | |
795 | case TE_BASE64: | |
87f1b884 | 796 | case TE_QP: |
87ab7992 | 797 | { |
3b6121f6 LS |
798 | struct strbuf **lines, **it, *sb; |
799 | ||
800 | /* Prepend any previous partial lines */ | |
801 | strbuf_insert(&line, 0, prev.buf, prev.len); | |
802 | strbuf_reset(&prev); | |
87ab7992 DZ |
803 | |
804 | /* binary data most likely doesn't have newlines */ | |
805 | if (message_type != TYPE_TEXT) { | |
3b6121f6 | 806 | handle_filter(&line); |
87ab7992 DZ |
807 | break; |
808 | } | |
9aa23094 JH |
809 | /* |
810 | * This is a decoded line that may contain | |
87ab7992 DZ |
811 | * multiple new lines. Pass only one chunk |
812 | * at a time to handle_filter() | |
813 | */ | |
3b6121f6 LS |
814 | lines = strbuf_split(&line, '\n'); |
815 | for (it = lines; (sb = *it); it++) { | |
816 | if (*(it + 1) == NULL) /* The last line */ | |
817 | if (sb->buf[sb->len - 1] != '\n') { | |
818 | /* Partial line, save it for later. */ | |
819 | strbuf_addbuf(&prev, sb); | |
820 | break; | |
821 | } | |
822 | handle_filter(sb); | |
823 | } | |
9aa23094 | 824 | /* |
3b6121f6 | 825 | * The partial chunk is saved in "prev" and will be |
9aa23094 | 826 | * appended by the next iteration of read_line_with_nul(). |
87ab7992 | 827 | */ |
3b6121f6 | 828 | strbuf_list_free(lines); |
d4a9ce78 | 829 | break; |
1d8fa411 | 830 | } |
87ab7992 | 831 | default: |
3b6121f6 | 832 | handle_filter(&line); |
d4a9ce78 | 833 | } |
87ab7992 | 834 | |
3b6121f6 LS |
835 | strbuf_reset(&line); |
836 | if (strbuf_avail(&line) < 100) | |
837 | strbuf_grow(&line, 100); | |
838 | } while ((len = read_line_with_nul(line.buf, strbuf_avail(&line), fin))); | |
839 | ||
840 | handle_body_out: | |
841 | strbuf_release(&prev); | |
1d8fa411 JH |
842 | } |
843 | ||
3b6121f6 | 844 | static void output_header_lines(FILE *fout, const char *hdr, const struct strbuf *data) |
d7f6bae2 | 845 | { |
3b6121f6 | 846 | const char *sp = data->buf; |
d7f6bae2 | 847 | while (1) { |
3b6121f6 | 848 | char *ep = strchr(sp, '\n'); |
d7f6bae2 JH |
849 | int len; |
850 | if (!ep) | |
3b6121f6 | 851 | len = strlen(sp); |
d7f6bae2 | 852 | else |
3b6121f6 LS |
853 | len = ep - sp; |
854 | fprintf(fout, "%s: %.*s\n", hdr, len, sp); | |
d7f6bae2 JH |
855 | if (!ep) |
856 | break; | |
3b6121f6 | 857 | sp = ep + 1; |
d7f6bae2 JH |
858 | } |
859 | } | |
860 | ||
87ab7992 | 861 | static void handle_info(void) |
2744b234 | 862 | { |
3b6121f6 | 863 | struct strbuf *hdr; |
87ab7992 DZ |
864 | int i; |
865 | ||
866 | for (i = 0; header[i]; i++) { | |
87ab7992 DZ |
867 | /* only print inbody headers if we output a patch file */ |
868 | if (patch_lines && s_hdr_data[i]) | |
869 | hdr = s_hdr_data[i]; | |
870 | else if (p_hdr_data[i]) | |
871 | hdr = p_hdr_data[i]; | |
872 | else | |
873 | continue; | |
874 | ||
875 | if (!memcmp(header[i], "Subject", 7)) { | |
3b6121f6 LS |
876 | if (!keep_subject) { |
877 | cleanup_subject(hdr); | |
878 | cleanup_space(hdr); | |
d7f6bae2 | 879 | } |
3b6121f6 | 880 | output_header_lines(fout, "Subject", hdr); |
87ab7992 | 881 | } else if (!memcmp(header[i], "From", 4)) { |
ddfb3696 | 882 | cleanup_space(hdr); |
87ab7992 | 883 | handle_from(hdr); |
3b6121f6 LS |
884 | fprintf(fout, "Author: %s\n", name.buf); |
885 | fprintf(fout, "Email: %s\n", email.buf); | |
87ab7992 DZ |
886 | } else { |
887 | cleanup_space(hdr); | |
3b6121f6 | 888 | fprintf(fout, "%s: %s\n", header[i], hdr->buf); |
87ab7992 | 889 | } |
d4a9ce78 | 890 | } |
87ab7992 | 891 | fprintf(fout, "\n"); |
2744b234 LT |
892 | } |
893 | ||
fcd056a6 JH |
894 | static int mailinfo(FILE *in, FILE *out, int ks, const char *encoding, |
895 | const char *msg, const char *patch) | |
34488e3c | 896 | { |
f88a545a | 897 | int peek; |
34488e3c LS |
898 | keep_subject = ks; |
899 | metainfo_charset = encoding; | |
900 | fin = in; | |
901 | fout = out; | |
902 | ||
903 | cmitmsg = fopen(msg, "w"); | |
904 | if (!cmitmsg) { | |
905 | perror(msg); | |
906 | return -1; | |
907 | } | |
908 | patchfile = fopen(patch, "w"); | |
909 | if (!patchfile) { | |
910 | perror(patch); | |
911 | fclose(cmitmsg); | |
912 | return -1; | |
913 | } | |
87ab7992 | 914 | |
3b6121f6 LS |
915 | p_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*p_hdr_data)); |
916 | s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*s_hdr_data)); | |
87ab7992 | 917 | |
f88a545a SS |
918 | do { |
919 | peek = fgetc(in); | |
920 | } while (isspace(peek)); | |
921 | ungetc(peek, in); | |
922 | ||
87ab7992 | 923 | /* process the email header */ |
3b6121f6 LS |
924 | while (read_one_header_line(&line, fin)) |
925 | check_header(&line, p_hdr_data, 1); | |
87ab7992 DZ |
926 | |
927 | handle_body(); | |
928 | handle_info(); | |
34488e3c LS |
929 | |
930 | return 0; | |
931 | } | |
932 | ||
6bff6a60 | 933 | static const char mailinfo_usage[] = |
588c038a | 934 | "git mailinfo [-k] [-u | --encoding=<encoding> | -n] msg patch <mail >info"; |
d4a9ce78 | 935 | |
a633fca0 | 936 | int cmd_mailinfo(int argc, const char **argv, const char *prefix) |
2744b234 | 937 | { |
bb1091a4 JH |
938 | const char *def_charset; |
939 | ||
f1f909e3 JH |
940 | /* NEEDSWORK: might want to do the optional .git/ directory |
941 | * discovery | |
942 | */ | |
ef90d6d4 | 943 | git_config(git_default_config, NULL); |
f1f909e3 | 944 | |
330db18c | 945 | def_charset = (git_commit_encoding ? git_commit_encoding : "UTF-8"); |
bb1091a4 JH |
946 | metainfo_charset = def_charset; |
947 | ||
6bff6a60 JH |
948 | while (1 < argc && argv[1][0] == '-') { |
949 | if (!strcmp(argv[1], "-k")) | |
950 | keep_subject = 1; | |
d4a9ce78 | 951 | else if (!strcmp(argv[1], "-u")) |
bb1091a4 JH |
952 | metainfo_charset = def_charset; |
953 | else if (!strcmp(argv[1], "-n")) | |
954 | metainfo_charset = NULL; | |
cc44c765 | 955 | else if (!prefixcmp(argv[1], "--encoding=")) |
9f63892b | 956 | metainfo_charset = argv[1] + 11; |
d4a9ce78 | 957 | else |
f1f909e3 | 958 | usage(mailinfo_usage); |
6bff6a60 JH |
959 | argc--; argv++; |
960 | } | |
961 | ||
a196d8d4 | 962 | if (argc != 3) |
f1f909e3 | 963 | usage(mailinfo_usage); |
34488e3c LS |
964 | |
965 | return !!mailinfo(stdin, stdout, keep_subject, metainfo_charset, argv[1], argv[2]); | |
2744b234 | 966 | } |