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