]>
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" |
2744b234 | 8 | |
34488e3c | 9 | static FILE *cmitmsg, *patchfile, *fin, *fout; |
2744b234 | 10 | |
96f1e58f DR |
11 | static int keep_subject; |
12 | static const char *metainfo_charset; | |
2744b234 | 13 | static char line[1000]; |
62c1f6b4 | 14 | static char date[1000]; |
2744b234 LT |
15 | static char name[1000]; |
16 | static char email[1000]; | |
17 | static char subject[1000]; | |
18 | ||
d4a9ce78 JH |
19 | static enum { |
20 | TE_DONTCARE, TE_QP, TE_BASE64, | |
21 | } transfer_encoding; | |
22 | static char charset[256]; | |
23 | ||
24 | static char multipart_boundary[1000]; | |
25 | static int multipart_boundary_len; | |
96f1e58f | 26 | static int patch_lines; |
d4a9ce78 | 27 | |
2744b234 LT |
28 | static char *sanity_check(char *name, char *email) |
29 | { | |
30 | int len = strlen(name); | |
31 | if (len < 3 || len > 60) | |
32 | return email; | |
33 | if (strchr(name, '@') || strchr(name, '<') || strchr(name, '>')) | |
34 | return email; | |
35 | return name; | |
36 | } | |
37 | ||
e0e3ba20 JH |
38 | static int bogus_from(char *line) |
39 | { | |
40 | /* John Doe <johndoe> */ | |
41 | char *bra, *ket, *dst, *cp; | |
42 | ||
43 | /* This is fallback, so do not bother if we already have an | |
44 | * e-mail address. | |
34488e3c | 45 | */ |
e0e3ba20 JH |
46 | if (*email) |
47 | return 0; | |
48 | ||
49 | bra = strchr(line, '<'); | |
50 | if (!bra) | |
51 | return 0; | |
52 | ket = strchr(bra, '>'); | |
53 | if (!ket) | |
54 | return 0; | |
55 | ||
56 | for (dst = email, cp = bra+1; cp < ket; ) | |
57 | *dst++ = *cp++; | |
58 | *dst = 0; | |
59 | for (cp = line; isspace(*cp); cp++) | |
60 | ; | |
61 | for (bra--; isspace(*bra); bra--) | |
62 | *bra = 0; | |
63 | cp = sanity_check(cp, email); | |
64 | strcpy(name, cp); | |
65 | return 1; | |
66 | } | |
67 | ||
2dec02b1 | 68 | static int handle_from(char *in_line) |
2744b234 | 69 | { |
2dec02b1 EB |
70 | char line[1000]; |
71 | char *at; | |
2744b234 LT |
72 | char *dst; |
73 | ||
2dec02b1 EB |
74 | strcpy(line, in_line); |
75 | at = strchr(line, '@'); | |
2744b234 | 76 | if (!at) |
e0e3ba20 | 77 | return bogus_from(line); |
2744b234 LT |
78 | |
79 | /* | |
80 | * If we already have one email, don't take any confusing lines | |
81 | */ | |
82 | if (*email && strchr(at+1, '@')) | |
83 | return 0; | |
84 | ||
d4a9ce78 JH |
85 | /* Pick up the string around '@', possibly delimited with <> |
86 | * pair; that is the email part. White them out while copying. | |
87 | */ | |
2744b234 LT |
88 | while (at > line) { |
89 | char c = at[-1]; | |
d4a9ce78 JH |
90 | if (isspace(c)) |
91 | break; | |
92 | if (c == '<') { | |
93 | at[-1] = ' '; | |
2744b234 | 94 | break; |
d4a9ce78 | 95 | } |
2744b234 LT |
96 | at--; |
97 | } | |
98 | dst = email; | |
99 | for (;;) { | |
100 | unsigned char c = *at; | |
d4a9ce78 JH |
101 | if (!c || c == '>' || isspace(c)) { |
102 | if (c == '>') | |
103 | *at = ' '; | |
2744b234 | 104 | break; |
d4a9ce78 | 105 | } |
2744b234 LT |
106 | *at++ = ' '; |
107 | *dst++ = c; | |
108 | } | |
109 | *dst++ = 0; | |
110 | ||
d4a9ce78 JH |
111 | /* The remainder is name. It could be "John Doe <john.doe@xz>" |
112 | * or "john.doe@xz (John Doe)", but we have whited out the | |
113 | * email part, so trim from both ends, possibly removing | |
114 | * the () pair at the end. | |
115 | */ | |
2744b234 LT |
116 | at = line + strlen(line); |
117 | while (at > line) { | |
118 | unsigned char c = *--at; | |
d4a9ce78 JH |
119 | if (!isspace(c)) { |
120 | at[(c == ')') ? 0 : 1] = 0; | |
2744b234 | 121 | break; |
d4a9ce78 | 122 | } |
2744b234 LT |
123 | } |
124 | ||
125 | at = line; | |
126 | for (;;) { | |
127 | unsigned char c = *at; | |
d4a9ce78 JH |
128 | if (!c || !isspace(c)) { |
129 | if (c == '(') | |
130 | at++; | |
2744b234 | 131 | break; |
d4a9ce78 | 132 | } |
2744b234 LT |
133 | at++; |
134 | } | |
2744b234 | 135 | at = sanity_check(at, email); |
2744b234 LT |
136 | strcpy(name, at); |
137 | return 1; | |
138 | } | |
139 | ||
d4a9ce78 | 140 | static int handle_date(char *line) |
62c1f6b4 LT |
141 | { |
142 | strcpy(date, line); | |
d4a9ce78 | 143 | return 0; |
62c1f6b4 LT |
144 | } |
145 | ||
d4a9ce78 | 146 | static int handle_subject(char *line) |
2744b234 LT |
147 | { |
148 | strcpy(subject, line); | |
d4a9ce78 JH |
149 | return 0; |
150 | } | |
151 | ||
152 | /* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt | |
153 | * to have enough heuristics to grok MIME encoded patches often found | |
154 | * on our mailing lists. For example, we do not even treat header lines | |
155 | * case insensitively. | |
156 | */ | |
157 | ||
158 | static int slurp_attr(const char *line, const char *name, char *attr) | |
159 | { | |
554fe20d | 160 | const char *ends, *ap = strcasestr(line, name); |
d4a9ce78 JH |
161 | size_t sz; |
162 | ||
163 | if (!ap) { | |
164 | *attr = 0; | |
165 | return 0; | |
166 | } | |
167 | ap += strlen(name); | |
168 | if (*ap == '"') { | |
169 | ap++; | |
170 | ends = "\""; | |
171 | } | |
172 | else | |
173 | ends = "; \t"; | |
174 | sz = strcspn(ap, ends); | |
175 | memcpy(attr, ap, sz); | |
176 | attr[sz] = 0; | |
177 | return 1; | |
178 | } | |
179 | ||
180 | static int handle_subcontent_type(char *line) | |
181 | { | |
182 | /* We do not want to mess with boundary. Note that we do not | |
183 | * handle nested multipart. | |
184 | */ | |
b893f091 JH |
185 | if (strcasestr(line, "boundary=")) { |
186 | fprintf(stderr, "Not handling nested multipart message.\n"); | |
187 | exit(1); | |
188 | } | |
d4a9ce78 JH |
189 | slurp_attr(line, "charset=", charset); |
190 | if (*charset) { | |
191 | int i, c; | |
192 | for (i = 0; (c = charset[i]) != 0; i++) | |
193 | charset[i] = tolower(c); | |
194 | } | |
195 | return 0; | |
196 | } | |
197 | ||
198 | static int handle_content_type(char *line) | |
199 | { | |
200 | *multipart_boundary = 0; | |
201 | if (slurp_attr(line, "boundary=", multipart_boundary + 2)) { | |
202 | memcpy(multipart_boundary, "--", 2); | |
203 | multipart_boundary_len = strlen(multipart_boundary); | |
204 | } | |
205 | slurp_attr(line, "charset=", charset); | |
206 | return 0; | |
207 | } | |
208 | ||
209 | static int handle_content_transfer_encoding(char *line) | |
210 | { | |
211 | if (strcasestr(line, "base64")) | |
212 | transfer_encoding = TE_BASE64; | |
213 | else if (strcasestr(line, "quoted-printable")) | |
214 | transfer_encoding = TE_QP; | |
215 | else | |
216 | transfer_encoding = TE_DONTCARE; | |
217 | return 0; | |
2744b234 LT |
218 | } |
219 | ||
d4a9ce78 JH |
220 | static int is_multipart_boundary(const char *line) |
221 | { | |
222 | return (!memcmp(line, multipart_boundary, multipart_boundary_len)); | |
223 | } | |
224 | ||
225 | static int eatspace(char *line) | |
2744b234 | 226 | { |
d4a9ce78 JH |
227 | int len = strlen(line); |
228 | while (len > 0 && isspace(line[len-1])) | |
229 | line[--len] = 0; | |
230 | return len; | |
2744b234 LT |
231 | } |
232 | ||
d4a9ce78 JH |
233 | #define SEEN_FROM 01 |
234 | #define SEEN_DATE 02 | |
235 | #define SEEN_SUBJECT 04 | |
81c5cf78 | 236 | #define SEEN_BOGUS_UNIX_FROM 010 |
d177e584 | 237 | #define SEEN_PREFIX 020 |
d4a9ce78 | 238 | |
ae448e38 | 239 | /* First lines of body can have From:, Date:, and Subject: or empty */ |
8b4525fb | 240 | static void handle_inbody_header(int *seen, char *line) |
d4a9ce78 | 241 | { |
2662dbfa EB |
242 | if (*seen & SEEN_PREFIX) |
243 | return; | |
ae448e38 JH |
244 | if (isspace(*line)) { |
245 | char *cp; | |
246 | for (cp = line + 1; *cp; cp++) { | |
247 | if (!isspace(*cp)) | |
248 | break; | |
249 | } | |
250 | if (!*cp) | |
251 | return; | |
252 | } | |
81c5cf78 JH |
253 | if (!memcmp(">From", line, 5) && isspace(line[5])) { |
254 | if (!(*seen & SEEN_BOGUS_UNIX_FROM)) { | |
255 | *seen |= SEEN_BOGUS_UNIX_FROM; | |
d177e584 | 256 | return; |
81c5cf78 JH |
257 | } |
258 | } | |
d4a9ce78 JH |
259 | if (!memcmp("From:", line, 5) && isspace(line[5])) { |
260 | if (!(*seen & SEEN_FROM) && handle_from(line+6)) { | |
261 | *seen |= SEEN_FROM; | |
8b4525fb | 262 | return; |
d4a9ce78 JH |
263 | } |
264 | } | |
265 | if (!memcmp("Date:", line, 5) && isspace(line[5])) { | |
266 | if (!(*seen & SEEN_DATE)) { | |
267 | handle_date(line+6); | |
268 | *seen |= SEEN_DATE; | |
8b4525fb | 269 | return; |
d4a9ce78 JH |
270 | } |
271 | } | |
272 | if (!memcmp("Subject:", line, 8) && isspace(line[8])) { | |
273 | if (!(*seen & SEEN_SUBJECT)) { | |
274 | handle_subject(line+9); | |
275 | *seen |= SEEN_SUBJECT; | |
8b4525fb | 276 | return; |
d4a9ce78 JH |
277 | } |
278 | } | |
279 | if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) { | |
280 | if (!(*seen & SEEN_SUBJECT)) { | |
281 | handle_subject(line); | |
282 | *seen |= SEEN_SUBJECT; | |
8b4525fb | 283 | return; |
d4a9ce78 JH |
284 | } |
285 | } | |
8b4525fb | 286 | *seen |= SEEN_PREFIX; |
d4a9ce78 JH |
287 | } |
288 | ||
289 | static char *cleanup_subject(char *subject) | |
2744b234 | 290 | { |
6bff6a60 JH |
291 | if (keep_subject) |
292 | return subject; | |
2744b234 LT |
293 | for (;;) { |
294 | char *p; | |
295 | int len, remove; | |
296 | switch (*subject) { | |
297 | case 'r': case 'R': | |
298 | if (!memcmp("e:", subject+1, 2)) { | |
299 | subject +=3; | |
300 | continue; | |
301 | } | |
302 | break; | |
303 | case ' ': case '\t': case ':': | |
304 | subject++; | |
305 | continue; | |
306 | ||
307 | case '[': | |
308 | p = strchr(subject, ']'); | |
309 | if (!p) { | |
310 | subject++; | |
311 | continue; | |
312 | } | |
313 | len = strlen(p); | |
314 | remove = p - subject; | |
315 | if (remove <= len *2) { | |
316 | subject = p+1; | |
317 | continue; | |
34488e3c | 318 | } |
2744b234 LT |
319 | break; |
320 | } | |
ae448e38 | 321 | eatspace(subject); |
2744b234 LT |
322 | return subject; |
323 | } | |
34488e3c | 324 | } |
2744b234 LT |
325 | |
326 | static void cleanup_space(char *buf) | |
327 | { | |
328 | unsigned char c; | |
329 | while ((c = *buf) != 0) { | |
330 | buf++; | |
331 | if (isspace(c)) { | |
332 | buf[-1] = ' '; | |
333 | c = *buf; | |
334 | while (isspace(c)) { | |
335 | int len = strlen(buf); | |
336 | memmove(buf, buf+1, len); | |
337 | c = *buf; | |
338 | } | |
339 | } | |
340 | } | |
341 | } | |
342 | ||
b75bf2c3 | 343 | static void decode_header(char *it); |
d4a9ce78 JH |
344 | typedef int (*header_fn_t)(char *); |
345 | struct header_def { | |
346 | const char *name; | |
347 | header_fn_t func; | |
348 | int namelen; | |
349 | }; | |
350 | ||
f8128cfb | 351 | static void check_header(char *line, struct header_def *header) |
d4a9ce78 JH |
352 | { |
353 | int i; | |
354 | ||
355 | if (header[0].namelen <= 0) { | |
356 | for (i = 0; header[i].name; i++) | |
357 | header[i].namelen = strlen(header[i].name); | |
358 | } | |
359 | for (i = 0; header[i].name; i++) { | |
360 | int len = header[i].namelen; | |
361 | if (!strncasecmp(line, header[i].name, len) && | |
362 | line[len] == ':' && isspace(line[len + 1])) { | |
33504530 EB |
363 | /* Unwrap inline B and Q encoding, and optionally |
364 | * normalize the meta information to utf8. | |
365 | */ | |
b75bf2c3 | 366 | decode_header(line + len + 2); |
d4a9ce78 JH |
367 | header[i].func(line + len + 2); |
368 | break; | |
369 | } | |
370 | } | |
371 | } | |
372 | ||
f8128cfb | 373 | static void check_subheader_line(char *line) |
d4a9ce78 JH |
374 | { |
375 | static struct header_def header[] = { | |
376 | { "Content-Type", handle_subcontent_type }, | |
377 | { "Content-Transfer-Encoding", | |
378 | handle_content_transfer_encoding }, | |
379 | { NULL }, | |
380 | }; | |
f8128cfb | 381 | check_header(line, header); |
d4a9ce78 | 382 | } |
f8128cfb | 383 | static void check_header_line(char *line) |
d4a9ce78 JH |
384 | { |
385 | static struct header_def header[] = { | |
386 | { "From", handle_from }, | |
387 | { "Date", handle_date }, | |
388 | { "Subject", handle_subject }, | |
389 | { "Content-Type", handle_content_type }, | |
390 | { "Content-Transfer-Encoding", | |
391 | handle_content_transfer_encoding }, | |
392 | { NULL }, | |
393 | }; | |
f8128cfb | 394 | check_header(line, header); |
d4a9ce78 JH |
395 | } |
396 | ||
ef29c117 JH |
397 | static int is_rfc2822_header(char *line) |
398 | { | |
399 | /* | |
400 | * The section that defines the loosest possible | |
401 | * field name is "3.6.8 Optional fields". | |
402 | * | |
403 | * optional-field = field-name ":" unstructured CRLF | |
404 | * field-name = 1*ftext | |
405 | * ftext = %d33-57 / %59-126 | |
406 | */ | |
407 | int ch; | |
408 | char *cp = line; | |
409 | while ((ch = *cp++)) { | |
410 | if (ch == ':') | |
411 | return cp != line; | |
412 | if ((33 <= ch && ch <= 57) || | |
413 | (59 <= ch && ch <= 126)) | |
414 | continue; | |
415 | break; | |
416 | } | |
417 | return 0; | |
418 | } | |
419 | ||
d4a9ce78 JH |
420 | static int read_one_header_line(char *line, int sz, FILE *in) |
421 | { | |
422 | int ofs = 0; | |
423 | while (ofs < sz) { | |
424 | int peek, len; | |
425 | if (fgets(line + ofs, sz - ofs, in) == NULL) | |
f30b2028 | 426 | break; |
d4a9ce78 | 427 | len = eatspace(line + ofs); |
ae448e38 | 428 | if ((len == 0) || !is_rfc2822_header(line)) { |
f30b2028 EB |
429 | /* Re-add the newline */ |
430 | line[ofs + len] = '\n'; | |
431 | line[ofs + len + 1] = '\0'; | |
432 | break; | |
d4a9ce78 | 433 | } |
f30b2028 EB |
434 | ofs += len; |
435 | /* Yuck, 2822 header "folding" */ | |
436 | peek = fgetc(in); ungetc(peek, in); | |
437 | if (peek != ' ' && peek != '\t') | |
438 | break; | |
d4a9ce78 | 439 | } |
f30b2028 | 440 | /* Count mbox From headers as headers */ |
c2c487cf | 441 | if (!ofs && (!memcmp(line, "From ", 5) || !memcmp(line, ">From ", 6))) |
f30b2028 | 442 | ofs = 1; |
d4a9ce78 JH |
443 | return ofs; |
444 | } | |
445 | ||
75731930 | 446 | static int decode_q_segment(char *in, char *ot, char *ep, int rfc2047) |
d4a9ce78 JH |
447 | { |
448 | int c; | |
449 | while ((c = *in++) != 0 && (in <= ep)) { | |
450 | if (c == '=') { | |
451 | int d = *in++; | |
452 | if (d == '\n' || !d) | |
453 | break; /* drop trailing newline */ | |
454 | *ot++ = ((hexval(d) << 4) | hexval(*in++)); | |
75731930 | 455 | continue; |
d4a9ce78 | 456 | } |
75731930 JH |
457 | if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */ |
458 | c = 0x20; | |
459 | *ot++ = c; | |
d4a9ce78 JH |
460 | } |
461 | *ot = 0; | |
462 | return 0; | |
463 | } | |
464 | ||
465 | static int decode_b_segment(char *in, char *ot, char *ep) | |
466 | { | |
467 | /* Decode in..ep, possibly in-place to ot */ | |
468 | int c, pos = 0, acc = 0; | |
469 | ||
470 | while ((c = *in++) != 0 && (in <= ep)) { | |
471 | if (c == '+') | |
472 | c = 62; | |
473 | else if (c == '/') | |
474 | c = 63; | |
475 | else if ('A' <= c && c <= 'Z') | |
476 | c -= 'A'; | |
477 | else if ('a' <= c && c <= 'z') | |
478 | c -= 'a' - 26; | |
479 | else if ('0' <= c && c <= '9') | |
480 | c -= '0' - 52; | |
481 | else if (c == '=') { | |
482 | /* padding is almost like (c == 0), except we do | |
483 | * not output NUL resulting only from it; | |
484 | * for now we just trust the data. | |
485 | */ | |
486 | c = 0; | |
487 | } | |
488 | else | |
489 | continue; /* garbage */ | |
490 | switch (pos++) { | |
491 | case 0: | |
492 | acc = (c << 2); | |
493 | break; | |
494 | case 1: | |
495 | *ot++ = (acc | (c >> 4)); | |
496 | acc = (c & 15) << 4; | |
497 | break; | |
498 | case 2: | |
499 | *ot++ = (acc | (c >> 2)); | |
500 | acc = (c & 3) << 6; | |
501 | break; | |
502 | case 3: | |
503 | *ot++ = (acc | c); | |
504 | acc = pos = 0; | |
505 | break; | |
506 | } | |
507 | } | |
508 | *ot = 0; | |
509 | return 0; | |
510 | } | |
511 | ||
512 | static void convert_to_utf8(char *line, char *charset) | |
513 | { | |
ac44f3e7 | 514 | static char latin_one[] = "latin1"; |
650e4be5 | 515 | char *input_charset = *charset ? charset : latin_one; |
b45974a6 JH |
516 | char *out = reencode_string(line, metainfo_charset, input_charset); |
517 | ||
518 | if (!out) { | |
519 | fprintf(stderr, "cannot convert from %s to %s\n", | |
520 | input_charset, metainfo_charset); | |
521 | *charset = 0; | |
650e4be5 | 522 | return; |
d4a9ce78 | 523 | } |
b45974a6 JH |
524 | strcpy(line, out); |
525 | free(out); | |
d4a9ce78 JH |
526 | } |
527 | ||
b75bf2c3 | 528 | static int decode_header_bq(char *it) |
d4a9ce78 JH |
529 | { |
530 | char *in, *out, *ep, *cp, *sp; | |
531 | char outbuf[1000]; | |
b75bf2c3 | 532 | int rfc2047 = 0; |
d4a9ce78 JH |
533 | |
534 | in = it; | |
535 | out = outbuf; | |
536 | while ((ep = strstr(in, "=?")) != NULL) { | |
537 | int sz, encoding; | |
538 | char charset_q[256], piecebuf[256]; | |
b75bf2c3 JH |
539 | rfc2047 = 1; |
540 | ||
d4a9ce78 JH |
541 | if (in != ep) { |
542 | sz = ep - in; | |
543 | memcpy(out, in, sz); | |
544 | out += sz; | |
545 | in += sz; | |
546 | } | |
547 | /* E.g. | |
548 | * ep : "=?iso-2022-jp?B?GyR...?= foo" | |
549 | * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz" | |
550 | */ | |
551 | ep += 2; | |
552 | cp = strchr(ep, '?'); | |
553 | if (!cp) | |
b75bf2c3 | 554 | return rfc2047; /* no munging */ |
d4a9ce78 JH |
555 | for (sp = ep; sp < cp; sp++) |
556 | charset_q[sp - ep] = tolower(*sp); | |
557 | charset_q[cp - ep] = 0; | |
558 | encoding = cp[1]; | |
559 | if (!encoding || cp[2] != '?') | |
b75bf2c3 | 560 | return rfc2047; /* no munging */ |
d4a9ce78 JH |
561 | ep = strstr(cp + 3, "?="); |
562 | if (!ep) | |
b75bf2c3 | 563 | return rfc2047; /* no munging */ |
d4a9ce78 JH |
564 | switch (tolower(encoding)) { |
565 | default: | |
b75bf2c3 | 566 | return rfc2047; /* no munging */ |
d4a9ce78 JH |
567 | case 'b': |
568 | sz = decode_b_segment(cp + 3, piecebuf, ep); | |
569 | break; | |
570 | case 'q': | |
75731930 | 571 | sz = decode_q_segment(cp + 3, piecebuf, ep, 1); |
d4a9ce78 JH |
572 | break; |
573 | } | |
574 | if (sz < 0) | |
b75bf2c3 | 575 | return rfc2047; |
650e4be5 | 576 | if (metainfo_charset) |
d4a9ce78 JH |
577 | convert_to_utf8(piecebuf, charset_q); |
578 | strcpy(out, piecebuf); | |
579 | out += strlen(out); | |
580 | in = ep + 2; | |
581 | } | |
582 | strcpy(out, in); | |
583 | strcpy(it, outbuf); | |
b75bf2c3 JH |
584 | return rfc2047; |
585 | } | |
586 | ||
587 | static void decode_header(char *it) | |
588 | { | |
589 | ||
590 | if (decode_header_bq(it)) | |
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) | |
596 | convert_to_utf8(it, ""); | |
d4a9ce78 JH |
597 | } |
598 | ||
599 | static void decode_transfer_encoding(char *line) | |
600 | { | |
601 | char *ep; | |
602 | ||
603 | switch (transfer_encoding) { | |
604 | case TE_QP: | |
605 | ep = line + strlen(line); | |
75731930 | 606 | decode_q_segment(line, line, ep, 0); |
d4a9ce78 JH |
607 | break; |
608 | case TE_BASE64: | |
609 | ep = line + strlen(line); | |
610 | decode_b_segment(line, line, ep); | |
611 | break; | |
612 | case TE_DONTCARE: | |
613 | break; | |
614 | } | |
615 | } | |
616 | ||
617 | static void handle_info(void) | |
2744b234 | 618 | { |
d4a9ce78 | 619 | char *sub; |
d4a9ce78 | 620 | |
d4a9ce78 | 621 | sub = cleanup_subject(subject); |
2744b234 | 622 | cleanup_space(name); |
62c1f6b4 | 623 | cleanup_space(date); |
2744b234 LT |
624 | cleanup_space(email); |
625 | cleanup_space(sub); | |
2744b234 | 626 | |
34488e3c | 627 | fprintf(fout, "Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n", |
d4a9ce78 JH |
628 | name, email, sub, date); |
629 | } | |
630 | ||
631 | /* We are inside message body and have read line[] already. | |
632 | * Spit out the commit log. | |
633 | */ | |
8b4525fb | 634 | static int handle_commit_msg(int *seen) |
d4a9ce78 JH |
635 | { |
636 | if (!cmitmsg) | |
637 | return 0; | |
2744b234 | 638 | do { |
a196d8d4 | 639 | if (!memcmp("diff -", line, 6) || |
fc7ef1e8 LT |
640 | !memcmp("---", line, 3) || |
641 | !memcmp("Index: ", line, 7)) | |
d4a9ce78 JH |
642 | break; |
643 | if ((multipart_boundary[0] && is_multipart_boundary(line))) { | |
644 | /* We come here when the first part had only | |
645 | * the commit message without any patch. We | |
646 | * pretend we have not seen this line yet, and | |
647 | * go back to the loop. | |
648 | */ | |
649 | return 1; | |
650 | } | |
2744b234 | 651 | |
d4a9ce78 JH |
652 | /* Unwrap transfer encoding and optionally |
653 | * normalize the log message to UTF-8. | |
654 | */ | |
655 | decode_transfer_encoding(line); | |
650e4be5 | 656 | if (metainfo_charset) |
d4a9ce78 | 657 | convert_to_utf8(line, charset); |
8b4525fb EB |
658 | |
659 | handle_inbody_header(seen, line); | |
660 | if (!(*seen & SEEN_PREFIX)) | |
661 | continue; | |
662 | ||
d4a9ce78 | 663 | fputs(line, cmitmsg); |
34488e3c | 664 | } while (fgets(line, sizeof(line), fin) != NULL); |
2744b234 | 665 | fclose(cmitmsg); |
d4a9ce78 JH |
666 | cmitmsg = NULL; |
667 | return 0; | |
2744b234 LT |
668 | } |
669 | ||
d4a9ce78 JH |
670 | /* We have done the commit message and have the first |
671 | * line of the patch in line[]. | |
672 | */ | |
673 | static void handle_patch(void) | |
2744b234 | 674 | { |
d4a9ce78 JH |
675 | do { |
676 | if (multipart_boundary[0] && is_multipart_boundary(line)) | |
677 | break; | |
678 | /* Only unwrap transfer encoding but otherwise do not | |
679 | * do anything. We do *NOT* want UTF-8 conversion | |
680 | * here; we are dealing with the user payload. | |
681 | */ | |
682 | decode_transfer_encoding(line); | |
683 | fputs(line, patchfile); | |
684 | patch_lines++; | |
34488e3c | 685 | } while (fgets(line, sizeof(line), fin) != NULL); |
2744b234 LT |
686 | } |
687 | ||
d4a9ce78 JH |
688 | /* multipart boundary and transfer encoding are set up for us, and we |
689 | * are at the end of the sub header. do equivalent of handle_body up | |
690 | * to the next boundary without closing patchfile --- we will expect | |
691 | * that the first part to contain commit message and a patch, and | |
692 | * handle other parts as pure patches. | |
693 | */ | |
8b4525fb | 694 | static int handle_multipart_one_part(int *seen) |
2744b234 | 695 | { |
d4a9ce78 | 696 | int n = 0; |
2744b234 | 697 | |
34488e3c | 698 | while (fgets(line, sizeof(line), fin) != NULL) { |
d4a9ce78 | 699 | again: |
d4a9ce78 | 700 | n++; |
d4a9ce78 JH |
701 | if (is_multipart_boundary(line)) |
702 | break; | |
8b4525fb | 703 | if (handle_commit_msg(seen)) |
d4a9ce78 JH |
704 | goto again; |
705 | handle_patch(); | |
2744b234 LT |
706 | break; |
707 | } | |
d4a9ce78 JH |
708 | if (n == 0) |
709 | return -1; | |
710 | return 0; | |
2744b234 LT |
711 | } |
712 | ||
d4a9ce78 | 713 | static void handle_multipart_body(void) |
1d8fa411 | 714 | { |
8b4525fb | 715 | int seen = 0; |
d4a9ce78 JH |
716 | int part_num = 0; |
717 | ||
718 | /* Skip up to the first boundary */ | |
34488e3c | 719 | while (fgets(line, sizeof(line), fin) != NULL) |
d4a9ce78 JH |
720 | if (is_multipart_boundary(line)) { |
721 | part_num = 1; | |
722 | break; | |
1d8fa411 | 723 | } |
d4a9ce78 JH |
724 | if (!part_num) |
725 | return; | |
726 | /* We are on boundary line. Start slurping the subhead. */ | |
727 | while (1) { | |
34488e3c | 728 | int hdr = read_one_header_line(line, sizeof(line), fin); |
f8128cfb | 729 | if (!hdr) { |
8b4525fb | 730 | if (handle_multipart_one_part(&seen) < 0) |
d4a9ce78 | 731 | return; |
8bc5c04a JH |
732 | /* Reset per part headers */ |
733 | transfer_encoding = TE_DONTCARE; | |
734 | charset[0] = 0; | |
d4a9ce78 JH |
735 | } |
736 | else | |
f8128cfb | 737 | check_subheader_line(line); |
d4a9ce78 JH |
738 | } |
739 | fclose(patchfile); | |
740 | if (!patch_lines) { | |
741 | fprintf(stderr, "No patch found\n"); | |
742 | exit(1); | |
1d8fa411 | 743 | } |
1d8fa411 JH |
744 | } |
745 | ||
d4a9ce78 JH |
746 | /* Non multipart message */ |
747 | static void handle_body(void) | |
2744b234 | 748 | { |
d4a9ce78 JH |
749 | int seen = 0; |
750 | ||
ae448e38 JH |
751 | handle_commit_msg(&seen); |
752 | handle_patch(); | |
d4a9ce78 JH |
753 | fclose(patchfile); |
754 | if (!patch_lines) { | |
755 | fprintf(stderr, "No patch found\n"); | |
756 | exit(1); | |
757 | } | |
2744b234 LT |
758 | } |
759 | ||
34488e3c LS |
760 | int mailinfo(FILE *in, FILE *out, int ks, const char *encoding, |
761 | const char *msg, const char *patch) | |
762 | { | |
763 | keep_subject = ks; | |
764 | metainfo_charset = encoding; | |
765 | fin = in; | |
766 | fout = out; | |
767 | ||
768 | cmitmsg = fopen(msg, "w"); | |
769 | if (!cmitmsg) { | |
770 | perror(msg); | |
771 | return -1; | |
772 | } | |
773 | patchfile = fopen(patch, "w"); | |
774 | if (!patchfile) { | |
775 | perror(patch); | |
776 | fclose(cmitmsg); | |
777 | return -1; | |
778 | } | |
779 | while (1) { | |
780 | int hdr = read_one_header_line(line, sizeof(line), fin); | |
781 | if (!hdr) { | |
782 | if (multipart_boundary[0]) | |
783 | handle_multipart_body(); | |
784 | else | |
785 | handle_body(); | |
786 | handle_info(); | |
787 | break; | |
788 | } | |
789 | check_header_line(line); | |
790 | } | |
791 | ||
792 | return 0; | |
793 | } | |
794 | ||
6bff6a60 | 795 | static const char mailinfo_usage[] = |
9f63892b | 796 | "git-mailinfo [-k] [-u | --encoding=<encoding>] msg patch <mail >info"; |
d4a9ce78 | 797 | |
a633fca0 | 798 | int cmd_mailinfo(int argc, const char **argv, const char *prefix) |
2744b234 | 799 | { |
f1f909e3 JH |
800 | /* NEEDSWORK: might want to do the optional .git/ directory |
801 | * discovery | |
802 | */ | |
803 | git_config(git_default_config); | |
804 | ||
6bff6a60 JH |
805 | while (1 < argc && argv[1][0] == '-') { |
806 | if (!strcmp(argv[1], "-k")) | |
807 | keep_subject = 1; | |
d4a9ce78 | 808 | else if (!strcmp(argv[1], "-u")) |
d2c11a38 JH |
809 | metainfo_charset = (git_commit_encoding |
810 | ? git_commit_encoding : "utf-8"); | |
9f63892b JH |
811 | else if (!strncmp(argv[1], "--encoding=", 11)) |
812 | metainfo_charset = argv[1] + 11; | |
d4a9ce78 | 813 | else |
f1f909e3 | 814 | usage(mailinfo_usage); |
6bff6a60 JH |
815 | argc--; argv++; |
816 | } | |
817 | ||
a196d8d4 | 818 | if (argc != 3) |
f1f909e3 | 819 | usage(mailinfo_usage); |
34488e3c LS |
820 | |
821 | return !!mailinfo(stdin, stdout, keep_subject, metainfo_charset, argv[1], argv[2]); | |
2744b234 | 822 | } |