blob: fcb32c9818e6c8ce5fef612192b971a4ec2d99dd [file] [log] [blame]
Linus Torvalds2744b232005-04-11 23:46:50 -07001/*
2 * Another stupid program, this one parsing the headers of an
3 * email to figure out authorship and subject
4 */
Junio C Hamanof1f909e2005-11-27 16:29:38 -08005#include "cache.h"
Lukas Sandström34488e32006-06-13 22:21:50 +02006#include "builtin.h"
Junio C Hamanob45974a2006-12-23 23:36:55 -08007#include "utf8.h"
Lukas Sandström3b6121f2008-07-13 20:30:12 +02008#include "strbuf.h"
Linus Torvalds2744b232005-04-11 23:46:50 -07009
Lukas Sandström34488e32006-06-13 22:21:50 +020010static FILE *cmitmsg, *patchfile, *fin, *fout;
Linus Torvalds2744b232005-04-11 23:46:50 -070011
David Rientjes96f1e582006-08-15 10:23:48 -070012static int keep_subject;
13static const char *metainfo_charset;
Lukas Sandström3b6121f2008-07-13 20:30:12 +020014static struct strbuf line = STRBUF_INIT;
15static struct strbuf name = STRBUF_INIT;
16static struct strbuf email = STRBUF_INIT;
Linus Torvalds2744b232005-04-11 23:46:50 -070017
Junio C Hamanod4a9ce72005-08-28 12:33:16 -070018static enum {
19 TE_DONTCARE, TE_QP, TE_BASE64,
20} transfer_encoding;
Don Zickus87ab7992007-03-12 15:52:04 -040021static enum {
22 TYPE_TEXT, TYPE_OTHER,
23} message_type;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -070024
Lukas Sandström3b6121f2008-07-13 20:30:12 +020025static struct strbuf charset = STRBUF_INIT;
David Rientjes96f1e582006-08-15 10:23:48 -070026static int patch_lines;
Lukas Sandström3b6121f2008-07-13 20:30:12 +020027static struct strbuf **p_hdr_data, **s_hdr_data;
Don Zickus87ab7992007-03-12 15:52:04 -040028
29#define MAX_HDR_PARSED 10
30#define MAX_BOUNDARIES 5
Junio C Hamanod4a9ce72005-08-28 12:33:16 -070031
Lukas Sandström3b6121f2008-07-13 20:30:12 +020032static void get_sane_name(struct strbuf *out, struct strbuf *name, struct strbuf *email)
Linus Torvalds2744b232005-04-11 23:46:50 -070033{
Lukas Sandström3b6121f2008-07-13 20:30:12 +020034 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);
Linus Torvalds2744b232005-04-11 23:46:50 -070042}
43
Lukas Sandström3b6121f2008-07-13 20:30:12 +020044static void parse_bogus_from(const struct strbuf *line)
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -080045{
46 /* John Doe <johndoe> */
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -080047
Lukas Sandström3b6121f2008-07-13 20:30:12 +020048 char *bra, *ket;
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -080049 /* This is fallback, so do not bother if we already have an
50 * e-mail address.
Lukas Sandström34488e32006-06-13 22:21:50 +020051 */
Lukas Sandström3b6121f2008-07-13 20:30:12 +020052 if (email.len)
53 return;
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -080054
Lukas Sandström3b6121f2008-07-13 20:30:12 +020055 bra = strchr(line->buf, '<');
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -080056 if (!bra)
Lukas Sandström3b6121f2008-07-13 20:30:12 +020057 return;
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -080058 ket = strchr(bra, '>');
59 if (!ket)
Lukas Sandström3b6121f2008-07-13 20:30:12 +020060 return;
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -080061
Lukas Sandström3b6121f2008-07-13 20:30:12 +020062 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);
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -080069}
70
Lukas Sandström3b6121f2008-07-13 20:30:12 +020071static void handle_from(const struct strbuf *from)
Linus Torvalds2744b232005-04-11 23:46:50 -070072{
Eric W. Biederman2dec02b2006-05-23 13:58:36 -060073 char *at;
Lukas Sandström3b6121f2008-07-13 20:30:12 +020074 size_t el;
75 struct strbuf f;
Linus Torvalds2744b232005-04-11 23:46:50 -070076
Lukas Sandström3b6121f2008-07-13 20:30:12 +020077 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 }
Linus Torvalds2744b232005-04-11 23:46:50 -070085
86 /*
87 * If we already have one email, don't take any confusing lines
88 */
Lukas Sandström3b6121f2008-07-13 20:30:12 +020089 if (email.len && strchr(at + 1, '@')) {
90 strbuf_release(&f);
91 return;
92 }
Linus Torvalds2744b232005-04-11 23:46:50 -070093
Junio C Hamanod4a9ce72005-08-28 12:33:16 -070094 /* Pick up the string around '@', possibly delimited with <>
Lukas Sandström3b6121f2008-07-13 20:30:12 +020095 * pair; that is the email part.
Junio C Hamanod4a9ce72005-08-28 12:33:16 -070096 */
Lukas Sandström3b6121f2008-07-13 20:30:12 +020097 while (at > f.buf) {
Linus Torvalds2744b232005-04-11 23:46:50 -070098 char c = at[-1];
Junio C Hamanod4a9ce72005-08-28 12:33:16 -070099 if (isspace(c))
Linus Torvalds2744b232005-04-11 23:46:50 -0700100 break;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700101 if (c == '<') {
102 at[-1] = ' ';
103 break;
104 }
Linus Torvalds2744b232005-04-11 23:46:50 -0700105 at--;
106 }
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200107 el = strcspn(at, " \n\t\r\v\f>");
108 strbuf_reset(&email);
109 strbuf_add(&email, at, el);
Jeff Kinge9d7d102008-08-19 13:28:24 -0400110 strbuf_remove(&f, at - f.buf, el + (at[el] ? 1 : 0));
Linus Torvalds2744b232005-04-11 23:46:50 -0700111
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700112 /* The remainder is name. It could be "John Doe <john.doe@xz>"
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200113 * or "john.doe@xz (John Doe)", but we have removed the
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700114 * email part, so trim from both ends, possibly removing
115 * the () pair at the end.
116 */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200117 strbuf_trim(&f);
Philippe Bruhat (BooK)0d4ede92008-07-21 15:34:29 +0200118 if (f.buf[0] == '(' && f.len && f.buf[f.len - 1] == ')') {
119 strbuf_remove(&f, 0, 1);
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200120 strbuf_setlen(&f, f.len - 1);
Philippe Bruhat (BooK)0d4ede92008-07-21 15:34:29 +0200121 }
Linus Torvalds2744b232005-04-11 23:46:50 -0700122
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200123 get_sane_name(&name, &f, &email);
124 strbuf_release(&f);
Linus Torvalds2744b232005-04-11 23:46:50 -0700125}
126
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200127static void handle_header(struct strbuf **out, const struct strbuf *line)
Linus Torvalds62c1f6b2005-05-01 21:42:53 -0700128{
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200129 if (!*out) {
130 *out = xmalloc(sizeof(struct strbuf));
131 strbuf_init(*out, line->len);
132 } else
133 strbuf_reset(*out);
Linus Torvalds62c1f6b2005-05-01 21:42:53 -0700134
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200135 strbuf_addbuf(*out, line);
Linus Torvalds2744b232005-04-11 23:46:50 -0700136}
137
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700138/* 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
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200144static int slurp_attr(const char *line, const char *name, struct strbuf *attr)
Linus Torvalds2744b232005-04-11 23:46:50 -0700145{
Timo Hirvonen554fe202006-06-28 12:04:39 +0300146 const char *ends, *ap = strcasestr(line, name);
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700147 size_t sz;
148
149 if (!ap) {
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200150 strbuf_setlen(attr, 0);
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700151 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);
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200161 strbuf_add(attr, ap, sz);
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700162 return 1;
Linus Torvalds2744b232005-04-11 23:46:50 -0700163}
164
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200165static struct strbuf *content[MAX_BOUNDARIES];
Don Zickus87ab7992007-03-12 15:52:04 -0400166
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200167static struct strbuf **content_top = content;
Don Zickus87ab7992007-03-12 15:52:04 -0400168
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200169static void handle_content_type(struct strbuf *line)
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700170{
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200171 struct strbuf *boundary = xmalloc(sizeof(struct strbuf));
172 strbuf_init(boundary, line->len);
Don Zickus87ab7992007-03-12 15:52:04 -0400173
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200174 if (!strcasestr(line->buf, "text/"))
Don Zickus87ab7992007-03-12 15:52:04 -0400175 message_type = TYPE_OTHER;
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200176 if (slurp_attr(line->buf, "boundary=", boundary)) {
177 strbuf_insert(boundary, 0, "--", 2);
Don Zickus289796d2008-08-14 11:35:42 -0400178 if (++content_top > &content[MAX_BOUNDARIES]) {
Don Zickus87ab7992007-03-12 15:52:04 -0400179 fprintf(stderr, "Too many boundaries to handle\n");
180 exit(1);
181 }
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200182 *content_top = boundary;
183 boundary = NULL;
Junio C Hamanob893f092005-09-06 16:46:34 -0700184 }
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200185 if (slurp_attr(line->buf, "charset=", &charset))
186 strbuf_tolower(&charset);
187
188 if (boundary) {
189 strbuf_release(boundary);
190 free(boundary);
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700191 }
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700192}
193
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200194static void handle_content_transfer_encoding(const struct strbuf *line)
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700195{
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200196 if (strcasestr(line->buf, "base64"))
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700197 transfer_encoding = TE_BASE64;
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200198 else if (strcasestr(line->buf, "quoted-printable"))
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700199 transfer_encoding = TE_QP;
200 else
201 transfer_encoding = TE_DONTCARE;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700202}
203
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200204static int is_multipart_boundary(const struct strbuf *line)
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700205{
Junio C Hamanoa9fd1382008-08-09 01:17:24 -0700206 return (((*content_top)->len <= line->len) &&
207 !memcmp(line->buf, (*content_top)->buf, (*content_top)->len));
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700208}
209
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200210static void cleanup_subject(struct strbuf *subject)
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700211{
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200212 char *pos;
213 size_t remove;
214 while (subject->len) {
215 switch (*subject->buf) {
Linus Torvalds2744b232005-04-11 23:46:50 -0700216 case 'r': case 'R':
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200217 if (subject->len <= 3)
218 break;
219 if (!memcmp(subject->buf + 1, "e:", 2)) {
220 strbuf_remove(subject, 0, 3);
Linus Torvalds2744b232005-04-11 23:46:50 -0700221 continue;
222 }
223 break;
224 case ' ': case '\t': case ':':
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200225 strbuf_remove(subject, 0, 1);
Linus Torvalds2744b232005-04-11 23:46:50 -0700226 continue;
Linus Torvalds2744b232005-04-11 23:46:50 -0700227 case '[':
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200228 if ((pos = strchr(subject->buf, ']'))) {
Junio C Hamano1e102bf2008-07-16 22:42:04 -0700229 remove = pos - subject->buf;
230 if (remove <= (subject->len - remove) * 2) {
231 strbuf_remove(subject, 0, remove + 1);
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200232 continue;
233 }
234 } else
235 strbuf_remove(subject, 0, 1);
Linus Torvalds2744b232005-04-11 23:46:50 -0700236 break;
237 }
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200238 strbuf_trim(subject);
239 return;
Linus Torvalds2744b232005-04-11 23:46:50 -0700240 }
Lukas Sandström34488e32006-06-13 22:21:50 +0200241}
Linus Torvalds2744b232005-04-11 23:46:50 -0700242
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200243static void cleanup_space(struct strbuf *sb)
Linus Torvalds2744b232005-04-11 23:46:50 -0700244{
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200245 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);
Linus Torvalds2744b232005-04-11 23:46:50 -0700251 }
252 }
253}
254
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200255static void decode_header(struct strbuf *line);
Shawn O. Pearce538dfe72007-10-21 00:12:12 -0400256static const char *header[MAX_HDR_PARSED] = {
Don Zickus87ab7992007-03-12 15:52:04 -0400257 "From","Subject","Date",
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700258};
259
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200260static inline int cmp_header(const struct strbuf *line, const char *hdr)
Linus Torvalds2744b232005-04-11 23:46:50 -0700261{
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200262 int len = strlen(hdr);
263 return !strncasecmp(line->buf, hdr, len) && line->len > len &&
264 line->buf[len] == ':' && isspace(line->buf[len + 1]);
265}
Linus Torvalds2744b232005-04-11 23:46:50 -0700266
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200267static 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;
Don Zickus87ab7992007-03-12 15:52:04 -0400272 /* search for the interesting parts */
273 for (i = 0; header[i]; i++) {
274 int len = strlen(header[i]);
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200275 if ((!hdr_data[i] || overwrite) && cmp_header(line, header[i])) {
Eric W. Biederman33504532006-05-23 13:45:37 -0600276 /* Unwrap inline B and Q encoding, and optionally
277 * normalize the meta information to utf8.
278 */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200279 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;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700284 }
285 }
Linus Torvalds2744b232005-04-11 23:46:50 -0700286
Don Zickus87ab7992007-03-12 15:52:04 -0400287 /* Content stuff */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200288 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;
Don Zickus87ab7992007-03-12 15:52:04 -0400296 }
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200297 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;
Don Zickus87ab7992007-03-12 15:52:04 -0400304 }
305
306 /* for inbody stuff */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200307 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])) {
Don Zickus87ab7992007-03-12 15:52:04 -0400312 for (i = 0; header[i]; i++) {
Lukas Sandströme9fe8042008-07-10 23:41:33 +0200313 if (!memcmp("Subject", header[i], 7)) {
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200314 handle_header(&hdr_data[i], line);
315 ret = 1;
316 goto check_header_out;
Don Zickus87ab7992007-03-12 15:52:04 -0400317 }
318 }
319 }
320
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200321check_header_out:
322 strbuf_release(&sb);
323 return ret;
Linus Torvalds2744b232005-04-11 23:46:50 -0700324}
325
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200326static int is_rfc2822_header(const struct strbuf *line)
Junio C Hamanoef29c112006-05-26 00:46:58 -0700327{
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;
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200337 char *cp = line->buf;
Linus Torvalds34fc5ce2007-02-26 11:10:59 -0800338
339 /* Count mbox From headers as headers */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200340 if (!prefixcmp(cp, "From ") || !prefixcmp(cp, ">From "))
Linus Torvalds34fc5ce2007-02-26 11:10:59 -0800341 return 1;
342
Junio C Hamanoef29c112006-05-26 00:46:58 -0700343 while ((ch = *cp++)) {
344 if (ch == ':')
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200345 return 1;
Junio C Hamanoef29c112006-05-26 00:46:58 -0700346 if ((33 <= ch && ch <= 57) ||
347 (59 <= ch && ch <= 126))
348 continue;
349 break;
350 }
351 return 0;
352}
353
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200354static int read_one_header_line(struct strbuf *line, FILE *in)
Junio C Hamano1d8fa412005-07-23 02:10:31 -0700355{
Linus Torvalds34fc5ce2007-02-26 11:10:59 -0800356 /* Get the first part of the line. */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200357 if (strbuf_getline(line, in, '\n'))
Linus Torvalds34fc5ce2007-02-26 11:10:59 -0800358 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 */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200364 strbuf_rtrim(line);
365 if (!line->len || !is_rfc2822_header(line)) {
Linus Torvalds34fc5ce2007-02-26 11:10:59 -0800366 /* Re-add the newline */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200367 strbuf_addch(line, '\n');
Linus Torvalds34fc5ce2007-02-26 11:10:59 -0800368 return 0;
369 }
370
371 /*
372 * Now we need to eat all the continuation lines..
373 * Yuck, 2822 header "folding"
374 */
375 for (;;) {
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200376 int peek;
377 struct strbuf continuation = STRBUF_INIT;
Linus Torvalds34fc5ce2007-02-26 11:10:59 -0800378
Eric W. Biedermanf30b2022006-05-23 13:53:20 -0600379 peek = fgetc(in); ungetc(peek, in);
380 if (peek != ' ' && peek != '\t')
381 break;
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200382 if (strbuf_getline(&continuation, in, '\n'))
Linus Torvalds34fc5ce2007-02-26 11:10:59 -0800383 break;
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200384 continuation.buf[0] = '\n';
385 strbuf_rtrim(&continuation);
386 strbuf_addbuf(line, &continuation);
Junio C Hamano1d8fa412005-07-23 02:10:31 -0700387 }
Linus Torvalds34fc5ce2007-02-26 11:10:59 -0800388
389 return 1;
Junio C Hamano1d8fa412005-07-23 02:10:31 -0700390}
391
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200392static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047)
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700393{
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200394 const char *in = q_seg->buf;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700395 int c;
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200396 struct strbuf *out = xmalloc(sizeof(struct strbuf));
397 strbuf_init(out, q_seg->len);
398
399 while ((c = *in++) != 0) {
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700400 if (c == '=') {
401 int d = *in++;
402 if (d == '\n' || !d)
403 break; /* drop trailing newline */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200404 strbuf_addch(out, (hexval(d) << 4) | hexval(*in++));
Junio C Hamano75731932006-04-21 00:06:58 -0700405 continue;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700406 }
Junio C Hamano75731932006-04-21 00:06:58 -0700407 if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
408 c = 0x20;
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200409 strbuf_addch(out, c);
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700410 }
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200411 return out;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700412}
413
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200414static struct strbuf *decode_b_segment(const struct strbuf *b_seg)
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700415{
416 /* Decode in..ep, possibly in-place to ot */
417 int c, pos = 0, acc = 0;
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200418 const char *in = b_seg->buf;
419 struct strbuf *out = xmalloc(sizeof(struct strbuf));
420 strbuf_init(out, b_seg->len);
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700421
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200422 while ((c = *in++) != 0) {
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700423 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;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700433 else
434 continue; /* garbage */
435 switch (pos++) {
436 case 0:
437 acc = (c << 2);
438 break;
439 case 1:
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200440 strbuf_addch(out, (acc | (c >> 4)));
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700441 acc = (c & 15) << 4;
442 break;
443 case 2:
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200444 strbuf_addch(out, (acc | (c >> 2)));
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700445 acc = (c & 3) << 6;
446 break;
447 case 3:
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200448 strbuf_addch(out, (acc | c));
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700449 acc = pos = 0;
450 break;
451 }
452 }
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200453 return out;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700454}
455
Linus Torvaldsb59d3982007-07-17 10:34:44 -0700456/*
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 */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200467static const char *guess_charset(const struct strbuf *line, const char *target_charset)
Linus Torvaldsb59d3982007-07-17 10:34:44 -0700468{
469 if (is_encoding_utf8(target_charset)) {
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200470 if (is_utf8(line->buf))
Linus Torvaldsb59d3982007-07-17 10:34:44 -0700471 return NULL;
472 }
473 return "latin1";
474}
475
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200476static void convert_to_utf8(struct strbuf *line, const char *charset)
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700477{
Linus Torvaldsb59d3982007-07-17 10:34:44 -0700478 char *out;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700479
Linus Torvaldsb59d3982007-07-17 10:34:44 -0700480 if (!charset || !*charset) {
481 charset = guess_charset(line, metainfo_charset);
482 if (!charset)
483 return;
484 }
485
Johannes Schindelin72960962007-07-24 01:03:26 +0100486 if (!strcmp(metainfo_charset, charset))
487 return;
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200488 out = reencode_string(line->buf, metainfo_charset, charset);
Junio C Hamanobb1091a2007-01-09 21:31:36 -0800489 if (!out)
490 die("cannot convert from %s to %s\n",
Linus Torvaldsb59d3982007-07-17 10:34:44 -0700491 charset, metainfo_charset);
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200492 strbuf_attach(line, out, strlen(out), strlen(out));
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700493}
494
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200495static int decode_header_bq(struct strbuf *it)
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700496{
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200497 char *in, *ep, *cp;
498 struct strbuf outbuf = STRBUF_INIT, *dec;
499 struct strbuf charset_q = STRBUF_INIT, piecebuf = STRBUF_INIT;
Junio C Hamanob75bf2c2006-07-05 14:17:49 -0700500 int rfc2047 = 0;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700501
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200502 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);
Junio C Hamanob75bf2c2006-07-05 14:17:49 -0700507 rfc2047 = 1;
508
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700509 if (in != ep) {
Kirill Smelkov353aaf22009-01-08 01:43:42 +0300510 /*
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 }
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200529 in = ep;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700530 }
531 /* E.g.
532 * ep : "=?iso-2022-jp?B?GyR...?= foo"
533 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
534 */
535 ep += 2;
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200536
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
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700545 encoding = cp[1];
546 if (!encoding || cp[2] != '?')
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200547 goto decode_header_bq_out;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700548 ep = strstr(cp + 3, "?=");
549 if (!ep)
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200550 goto decode_header_bq_out;
551 strbuf_add(&piecebuf, cp + 3, ep - cp - 3);
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700552 switch (tolower(encoding)) {
553 default:
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200554 goto decode_header_bq_out;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700555 case 'b':
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200556 dec = decode_b_segment(&piecebuf);
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700557 break;
558 case 'q':
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200559 dec = decode_q_segment(&piecebuf, 1);
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700560 break;
561 }
Junio C Hamano650e4be2005-11-27 16:22:16 -0800562 if (metainfo_charset)
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200563 convert_to_utf8(dec, charset_q.buf);
Alex Riesen8dabdfc2007-08-30 23:48:24 +0200564
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200565 strbuf_addbuf(&outbuf, dec);
566 strbuf_release(dec);
567 free(dec);
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700568 in = ep + 2;
569 }
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200570 strbuf_addstr(&outbuf, in);
571 strbuf_reset(it);
572 strbuf_addbuf(it, &outbuf);
573decode_header_bq_out:
574 strbuf_release(&outbuf);
575 strbuf_release(&charset_q);
576 strbuf_release(&piecebuf);
Junio C Hamanob75bf2c2006-07-05 14:17:49 -0700577 return rfc2047;
578}
579
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200580static void decode_header(struct strbuf *it)
Junio C Hamanob75bf2c2006-07-05 14:17:49 -0700581{
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200582 if (decode_header_bq(it))
Junio C Hamanob75bf2c2006-07-05 14:17:49 -0700583 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)
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200588 convert_to_utf8(it, "");
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700589}
590
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200591static void decode_transfer_encoding(struct strbuf *line)
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700592{
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200593 struct strbuf *ret;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700594
595 switch (transfer_encoding) {
596 case TE_QP:
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200597 ret = decode_q_segment(line, 0);
598 break;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700599 case TE_BASE64:
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200600 ret = decode_b_segment(line);
601 break;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700602 case TE_DONTCARE:
Junio C Hamano9aa23092008-05-25 01:16:05 -0700603 default:
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200604 return;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700605 }
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200606 strbuf_reset(line);
607 strbuf_addbuf(line, ret);
608 strbuf_release(ret);
609 free(ret);
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700610}
611
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200612static void handle_filter(struct strbuf *line);
Don Zickus87ab7992007-03-12 15:52:04 -0400613
614static int find_boundary(void)
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700615{
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200616 while (!strbuf_getline(&line, fin, '\n')) {
Don Zickus289796d2008-08-14 11:35:42 -0400617 if (*content_top && is_multipart_boundary(&line))
Don Zickus87ab7992007-03-12 15:52:04 -0400618 return 1;
619 }
620 return 0;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700621}
622
Don Zickus87ab7992007-03-12 15:52:04 -0400623static int handle_boundary(void)
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700624{
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200625 struct strbuf newline = STRBUF_INIT;
626
627 strbuf_addch(&newline, '\n');
Don Zickus87ab7992007-03-12 15:52:04 -0400628again:
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200629 if (line.len >= (*content_top)->len + 2 &&
630 !memcmp(line.buf + (*content_top)->len, "--", 2)) {
Don Zickus87ab7992007-03-12 15:52:04 -0400631 /* we hit an end boundary */
632 /* pop the current boundary off the stack */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200633 strbuf_release(*content_top);
634 free(*content_top);
635 *content_top = NULL;
Don Zickus87ab7992007-03-12 15:52:04 -0400636
637 /* technically won't happen as is_multipart_boundary()
638 will fail first. But just in case..
639 */
Don Zickus289796d2008-08-14 11:35:42 -0400640 if (--content_top < content) {
Don Zickus87ab7992007-03-12 15:52:04 -0400641 fprintf(stderr, "Detected mismatched boundaries, "
642 "can't recover\n");
643 exit(1);
644 }
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200645 handle_filter(&newline);
646 strbuf_release(&newline);
Don Zickus87ab7992007-03-12 15:52:04 -0400647
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;
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200656 strbuf_reset(&charset);
Don Zickus87ab7992007-03-12 15:52:04 -0400657 message_type = TYPE_TEXT;
658
659 /* slurp in this section's info */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200660 while (read_one_header_line(&line, fin))
661 check_header(&line, p_hdr_data, 0);
Don Zickus87ab7992007-03-12 15:52:04 -0400662
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200663 strbuf_release(&newline);
Junio C Hamanoa9fd1382008-08-09 01:17:24 -0700664 /* replenish line */
665 if (strbuf_getline(&line, fin, '\n'))
666 return 0;
667 strbuf_addch(&line, '\n');
668 return 1;
Don Zickus87ab7992007-03-12 15:52:04 -0400669}
670
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200671static inline int patchbreak(const struct strbuf *line)
Don Zickusf0658cf2007-03-12 15:52:06 -0400672{
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200673 size_t i;
674
Don Zickusf0658cf2007-03-12 15:52:06 -0400675 /* Beginning of a "diff -" header? */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200676 if (!prefixcmp(line->buf, "diff -"))
Don Zickusf0658cf2007-03-12 15:52:06 -0400677 return 1;
678
679 /* CVS "Index: " line? */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200680 if (!prefixcmp(line->buf, "Index: "))
Don Zickusf0658cf2007-03-12 15:52:06 -0400681 return 1;
682
683 /*
684 * "--- <filename>" starts patches without headers
685 * "---<sp>*" is a manual separator
686 */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200687 if (line->len < 4)
688 return 0;
689
690 if (!prefixcmp(line->buf, "---")) {
Don Zickusf0658cf2007-03-12 15:52:06 -0400691 /* space followed by a filename? */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200692 if (line->buf[3] == ' ' && !isspace(line->buf[4]))
Don Zickusf0658cf2007-03-12 15:52:06 -0400693 return 1;
694 /* Just whitespace? */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200695 for (i = 3; i < line->len; i++) {
696 unsigned char c = line->buf[i];
Don Zickusf0658cf2007-03-12 15:52:06 -0400697 if (c == '\n')
698 return 1;
699 if (!isspace(c))
700 break;
701 }
702 return 0;
703 }
704 return 0;
705}
706
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200707static int handle_commit_msg(struct strbuf *line)
Don Zickus87ab7992007-03-12 15:52:04 -0400708{
709 static int still_looking = 1;
710
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700711 if (!cmitmsg)
712 return 0;
Don Zickus87ab7992007-03-12 15:52:04 -0400713
714 if (still_looking) {
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200715 strbuf_ltrim(line);
716 if (!line->len)
717 return 0;
718 if ((still_looking = check_header(line, s_hdr_data, 0)) != 0)
Don Zickus87ab7992007-03-12 15:52:04 -0400719 return 0;
720 }
721
Don Zickus86747c12007-03-30 12:18:45 -0400722 /* normalize the log message to UTF-8. */
723 if (metainfo_charset)
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200724 convert_to_utf8(line, charset.buf);
Don Zickus86747c12007-03-30 12:18:45 -0400725
Don Zickusf0658cf2007-03-12 15:52:06 -0400726 if (patchbreak(line)) {
Don Zickus87ab7992007-03-12 15:52:04 -0400727 fclose(cmitmsg);
728 cmitmsg = NULL;
729 return 1;
730 }
731
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200732 fputs(line->buf, cmitmsg);
Don Zickus87ab7992007-03-12 15:52:04 -0400733 return 0;
734}
735
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200736static void handle_patch(const struct strbuf *line)
Don Zickus87ab7992007-03-12 15:52:04 -0400737{
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200738 fwrite(line->buf, 1, line->len, patchfile);
Don Zickus87ab7992007-03-12 15:52:04 -0400739 patch_lines++;
Don Zickus87ab7992007-03-12 15:52:04 -0400740}
741
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200742static void handle_filter(struct strbuf *line)
Don Zickus87ab7992007-03-12 15:52:04 -0400743{
744 static int filter = 0;
745
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200746 /* filter tells us which part we left off on */
Don Zickus87ab7992007-03-12 15:52:04 -0400747 switch (filter) {
748 case 0:
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200749 if (!handle_commit_msg(line))
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700750 break;
Don Zickus87ab7992007-03-12 15:52:04 -0400751 filter++;
752 case 1:
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200753 handle_patch(line);
754 break;
Don Zickus87ab7992007-03-12 15:52:04 -0400755 }
Don Zickus87ab7992007-03-12 15:52:04 -0400756}
757
758static void handle_body(void)
759{
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200760 int len = 0;
761 struct strbuf prev = STRBUF_INIT;
Don Zickus87ab7992007-03-12 15:52:04 -0400762
763 /* Skip up to the first boundary */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200764 if (*content_top) {
Don Zickus87ab7992007-03-12 15:52:04 -0400765 if (!find_boundary())
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200766 goto handle_body_out;
Don Zickus87ab7992007-03-12 15:52:04 -0400767 }
768
769 do {
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200770 strbuf_setlen(&line, line.len + len);
771
Don Zickus87ab7992007-03-12 15:52:04 -0400772 /* process any boundary lines */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200773 if (*content_top && is_multipart_boundary(&line)) {
Don Zickus87ab7992007-03-12 15:52:04 -0400774 /* flush any leftover */
Junio C Hamanoa9fd1382008-08-09 01:17:24 -0700775 if (prev.len) {
776 handle_filter(&prev);
777 strbuf_reset(&prev);
778 }
Don Zickus87ab7992007-03-12 15:52:04 -0400779 if (!handle_boundary())
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200780 goto handle_body_out;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700781 }
782
Don Zickus86747c12007-03-30 12:18:45 -0400783 /* Unwrap transfer encoding */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200784 decode_transfer_encoding(&line);
Eric W. Biederman8b4525f2006-05-23 13:47:28 -0600785
Don Zickus87ab7992007-03-12 15:52:04 -0400786 switch (transfer_encoding) {
787 case TE_BASE64:
Jay Soffian87f1b882008-02-15 16:53:36 -0500788 case TE_QP:
Don Zickus87ab7992007-03-12 15:52:04 -0400789 {
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200790 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);
Don Zickus87ab7992007-03-12 15:52:04 -0400795
796 /* binary data most likely doesn't have newlines */
797 if (message_type != TYPE_TEXT) {
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200798 handle_filter(&line);
Don Zickus87ab7992007-03-12 15:52:04 -0400799 break;
800 }
Junio C Hamano9aa23092008-05-25 01:16:05 -0700801 /*
802 * This is a decoded line that may contain
Don Zickus87ab7992007-03-12 15:52:04 -0400803 * multiple new lines. Pass only one chunk
804 * at a time to handle_filter()
805 */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200806 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 }
Junio C Hamano9aa23092008-05-25 01:16:05 -0700816 /*
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200817 * The partial chunk is saved in "prev" and will be
Junio C Hamano9aa23092008-05-25 01:16:05 -0700818 * appended by the next iteration of read_line_with_nul().
Don Zickus87ab7992007-03-12 15:52:04 -0400819 */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200820 strbuf_list_free(lines);
Don Zickus87ab7992007-03-12 15:52:04 -0400821 break;
822 }
823 default:
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200824 handle_filter(&line);
Don Zickus87ab7992007-03-12 15:52:04 -0400825 }
Don Zickus87ab7992007-03-12 15:52:04 -0400826
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200827 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
832handle_body_out:
833 strbuf_release(&prev);
Don Zickus87ab7992007-03-12 15:52:04 -0400834}
835
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200836static void output_header_lines(FILE *fout, const char *hdr, const struct strbuf *data)
Junio C Hamanod7f6bae2007-07-28 17:57:25 -0700837{
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200838 const char *sp = data->buf;
Junio C Hamanod7f6bae2007-07-28 17:57:25 -0700839 while (1) {
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200840 char *ep = strchr(sp, '\n');
Junio C Hamanod7f6bae2007-07-28 17:57:25 -0700841 int len;
842 if (!ep)
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200843 len = strlen(sp);
Junio C Hamanod7f6bae2007-07-28 17:57:25 -0700844 else
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200845 len = ep - sp;
846 fprintf(fout, "%s: %.*s\n", hdr, len, sp);
Junio C Hamanod7f6bae2007-07-28 17:57:25 -0700847 if (!ep)
848 break;
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200849 sp = ep + 1;
Junio C Hamanod7f6bae2007-07-28 17:57:25 -0700850 }
851}
852
Don Zickus87ab7992007-03-12 15:52:04 -0400853static void handle_info(void)
854{
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200855 struct strbuf *hdr;
Don Zickus87ab7992007-03-12 15:52:04 -0400856 int i;
857
858 for (i = 0; header[i]; i++) {
Don Zickus87ab7992007-03-12 15:52:04 -0400859 /* 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
Eric W. Biederman8b4525f2006-05-23 13:47:28 -0600865 continue;
866
Don Zickus87ab7992007-03-12 15:52:04 -0400867 if (!memcmp(header[i], "Subject", 7)) {
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200868 if (!keep_subject) {
869 cleanup_subject(hdr);
870 cleanup_space(hdr);
Junio C Hamanod7f6bae2007-07-28 17:57:25 -0700871 }
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200872 output_header_lines(fout, "Subject", hdr);
Don Zickus87ab7992007-03-12 15:52:04 -0400873 } else if (!memcmp(header[i], "From", 4)) {
874 handle_from(hdr);
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200875 fprintf(fout, "Author: %s\n", name.buf);
876 fprintf(fout, "Email: %s\n", email.buf);
Don Zickus87ab7992007-03-12 15:52:04 -0400877 } else {
878 cleanup_space(hdr);
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200879 fprintf(fout, "%s: %s\n", header[i], hdr->buf);
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700880 }
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700881 }
Don Zickus87ab7992007-03-12 15:52:04 -0400882 fprintf(fout, "\n");
Linus Torvalds2744b232005-04-11 23:46:50 -0700883}
884
Junio C Hamanofcd056a2007-06-08 02:22:56 -0700885static int mailinfo(FILE *in, FILE *out, int ks, const char *encoding,
886 const char *msg, const char *patch)
Lukas Sandström34488e32006-06-13 22:21:50 +0200887{
Simon Sasburgf88a5452007-11-01 23:57:45 +0100888 int peek;
Lukas Sandström34488e32006-06-13 22:21:50 +0200889 keep_subject = ks;
890 metainfo_charset = encoding;
891 fin = in;
892 fout = out;
893
894 cmitmsg = fopen(msg, "w");
895 if (!cmitmsg) {
896 perror(msg);
897 return -1;
898 }
899 patchfile = fopen(patch, "w");
900 if (!patchfile) {
901 perror(patch);
902 fclose(cmitmsg);
903 return -1;
904 }
Don Zickus87ab7992007-03-12 15:52:04 -0400905
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200906 p_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*p_hdr_data));
907 s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*s_hdr_data));
Don Zickus87ab7992007-03-12 15:52:04 -0400908
Simon Sasburgf88a5452007-11-01 23:57:45 +0100909 do {
910 peek = fgetc(in);
911 } while (isspace(peek));
912 ungetc(peek, in);
913
Don Zickus87ab7992007-03-12 15:52:04 -0400914 /* process the email header */
Lukas Sandström3b6121f2008-07-13 20:30:12 +0200915 while (read_one_header_line(&line, fin))
916 check_header(&line, p_hdr_data, 1);
Don Zickus87ab7992007-03-12 15:52:04 -0400917
918 handle_body();
919 handle_info();
Lukas Sandström34488e32006-06-13 22:21:50 +0200920
921 return 0;
922}
923
Junio C Hamano6bff6a62005-08-16 22:18:27 -0700924static const char mailinfo_usage[] =
Junio C Hamano588c0382008-07-16 17:22:50 -0700925 "git mailinfo [-k] [-u | --encoding=<encoding> | -n] msg patch <mail >info";
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700926
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700927int cmd_mailinfo(int argc, const char **argv, const char *prefix)
Linus Torvalds2744b232005-04-11 23:46:50 -0700928{
Junio C Hamanobb1091a2007-01-09 21:31:36 -0800929 const char *def_charset;
930
Junio C Hamanof1f909e2005-11-27 16:29:38 -0800931 /* NEEDSWORK: might want to do the optional .git/ directory
932 * discovery
933 */
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100934 git_config(git_default_config, NULL);
Junio C Hamanof1f909e2005-11-27 16:29:38 -0800935
Junio C Hamanobb1091a2007-01-09 21:31:36 -0800936 def_charset = (git_commit_encoding ? git_commit_encoding : "utf-8");
937 metainfo_charset = def_charset;
938
Junio C Hamano6bff6a62005-08-16 22:18:27 -0700939 while (1 < argc && argv[1][0] == '-') {
940 if (!strcmp(argv[1], "-k"))
941 keep_subject = 1;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700942 else if (!strcmp(argv[1], "-u"))
Junio C Hamanobb1091a2007-01-09 21:31:36 -0800943 metainfo_charset = def_charset;
944 else if (!strcmp(argv[1], "-n"))
945 metainfo_charset = NULL;
Junio C Hamanocc44c762007-02-20 01:53:29 -0800946 else if (!prefixcmp(argv[1], "--encoding="))
Junio C Hamano9f638922005-11-28 01:29:52 -0800947 metainfo_charset = argv[1] + 11;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -0700948 else
Junio C Hamanof1f909e2005-11-27 16:29:38 -0800949 usage(mailinfo_usage);
Junio C Hamano6bff6a62005-08-16 22:18:27 -0700950 argc--; argv++;
951 }
952
Linus Torvaldsa196d8d2005-06-23 09:40:23 -0700953 if (argc != 3)
Junio C Hamanof1f909e2005-11-27 16:29:38 -0800954 usage(mailinfo_usage);
Lukas Sandström34488e32006-06-13 22:21:50 +0200955
956 return !!mailinfo(stdin, stdout, keep_subject, metainfo_charset, argv[1], argv[2]);
Linus Torvalds2744b232005-04-11 23:46:50 -0700957}