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