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