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