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