2005-04-12 08:46:50 +02:00
|
|
|
/*
|
|
|
|
* Another stupid program, this one parsing the headers of an
|
|
|
|
* email to figure out authorship and subject
|
|
|
|
*/
|
2005-11-28 01:29:38 +01:00
|
|
|
#include "cache.h"
|
2006-06-13 22:21:50 +02:00
|
|
|
#include "builtin.h"
|
2006-12-24 08:36:55 +01:00
|
|
|
#include "utf8.h"
|
2008-07-13 20:30:12 +02:00
|
|
|
#include "strbuf.h"
|
2005-04-12 08:46:50 +02:00
|
|
|
|
2006-06-13 22:21:50 +02:00
|
|
|
static FILE *cmitmsg, *patchfile, *fin, *fout;
|
2005-04-12 08:46:50 +02:00
|
|
|
|
2006-08-15 19:23:48 +02:00
|
|
|
static int keep_subject;
|
2009-07-16 00:31:12 +02:00
|
|
|
static int keep_non_patch_brackets_in_subject;
|
2006-08-15 19:23:48 +02:00
|
|
|
static const char *metainfo_charset;
|
2008-07-13 20:30:12 +02:00
|
|
|
static struct strbuf line = STRBUF_INIT;
|
|
|
|
static struct strbuf name = STRBUF_INIT;
|
|
|
|
static struct strbuf email = STRBUF_INIT;
|
2014-11-25 15:00:55 +01:00
|
|
|
static char *message_id;
|
2005-04-12 08:46:50 +02:00
|
|
|
|
2005-08-28 21:33:16 +02:00
|
|
|
static enum {
|
2010-05-14 11:31:35 +02:00
|
|
|
TE_DONTCARE, TE_QP, TE_BASE64
|
2005-08-28 21:33:16 +02:00
|
|
|
} transfer_encoding;
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static struct strbuf charset = STRBUF_INIT;
|
2006-08-15 19:23:48 +02:00
|
|
|
static int patch_lines;
|
2008-07-13 20:30:12 +02:00
|
|
|
static struct strbuf **p_hdr_data, **s_hdr_data;
|
2009-08-27 06:36:05 +02:00
|
|
|
static int use_scissors;
|
2014-11-25 15:00:55 +01:00
|
|
|
static int add_message_id;
|
2009-11-20 17:12:47 +01:00
|
|
|
static int use_inbody_headers = 1;
|
2007-03-12 20:52:04 +01:00
|
|
|
|
|
|
|
#define MAX_HDR_PARSED 10
|
|
|
|
#define MAX_BOUNDARIES 5
|
2005-08-28 21:33:16 +02:00
|
|
|
|
2009-02-01 18:45:05 +01:00
|
|
|
static void cleanup_space(struct strbuf *sb);
|
|
|
|
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static void get_sane_name(struct strbuf *out, struct strbuf *name, struct strbuf *email)
|
2005-04-12 08:46:50 +02:00
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
struct strbuf *src = name;
|
|
|
|
if (name->len < 3 || 60 < name->len || strchr(name->buf, '@') ||
|
|
|
|
strchr(name->buf, '<') || strchr(name->buf, '>'))
|
|
|
|
src = email;
|
|
|
|
else if (name == out)
|
|
|
|
return;
|
|
|
|
strbuf_reset(out);
|
|
|
|
strbuf_addbuf(out, src);
|
2005-04-12 08:46:50 +02:00
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static void parse_bogus_from(const struct strbuf *line)
|
2005-12-15 01:31:06 +01:00
|
|
|
{
|
|
|
|
/* John Doe <johndoe> */
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
char *bra, *ket;
|
2005-12-15 01:31:06 +01:00
|
|
|
/* This is fallback, so do not bother if we already have an
|
|
|
|
* e-mail address.
|
2006-06-13 22:21:50 +02:00
|
|
|
*/
|
2008-07-13 20:30:12 +02:00
|
|
|
if (email.len)
|
|
|
|
return;
|
2005-12-15 01:31:06 +01:00
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
bra = strchr(line->buf, '<');
|
2005-12-15 01:31:06 +01:00
|
|
|
if (!bra)
|
2008-07-13 20:30:12 +02:00
|
|
|
return;
|
2005-12-15 01:31:06 +01:00
|
|
|
ket = strchr(bra, '>');
|
|
|
|
if (!ket)
|
2008-07-13 20:30:12 +02:00
|
|
|
return;
|
2005-12-15 01:31:06 +01:00
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_reset(&email);
|
|
|
|
strbuf_add(&email, bra + 1, ket - bra - 1);
|
|
|
|
|
|
|
|
strbuf_reset(&name);
|
|
|
|
strbuf_add(&name, line->buf, bra - line->buf);
|
|
|
|
strbuf_trim(&name);
|
|
|
|
get_sane_name(&name, &name, &email);
|
2005-12-15 01:31:06 +01:00
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static void handle_from(const struct strbuf *from)
|
2005-04-12 08:46:50 +02:00
|
|
|
{
|
2006-05-23 21:58:36 +02:00
|
|
|
char *at;
|
2008-07-13 20:30:12 +02:00
|
|
|
size_t el;
|
|
|
|
struct strbuf f;
|
2005-04-12 08:46:50 +02:00
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_init(&f, from->len);
|
|
|
|
strbuf_addbuf(&f, from);
|
|
|
|
|
|
|
|
at = strchr(f.buf, '@');
|
|
|
|
if (!at) {
|
|
|
|
parse_bogus_from(from);
|
|
|
|
return;
|
|
|
|
}
|
2005-04-12 08:46:50 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we already have one email, don't take any confusing lines
|
|
|
|
*/
|
2008-07-13 20:30:12 +02:00
|
|
|
if (email.len && strchr(at + 1, '@')) {
|
|
|
|
strbuf_release(&f);
|
|
|
|
return;
|
|
|
|
}
|
2005-04-12 08:46:50 +02:00
|
|
|
|
2005-08-28 21:33:16 +02:00
|
|
|
/* Pick up the string around '@', possibly delimited with <>
|
2008-07-13 20:30:12 +02:00
|
|
|
* pair; that is the email part.
|
2005-08-28 21:33:16 +02:00
|
|
|
*/
|
2008-07-13 20:30:12 +02:00
|
|
|
while (at > f.buf) {
|
2005-04-12 08:46:50 +02:00
|
|
|
char c = at[-1];
|
2005-08-28 21:33:16 +02:00
|
|
|
if (isspace(c))
|
|
|
|
break;
|
|
|
|
if (c == '<') {
|
|
|
|
at[-1] = ' ';
|
2005-04-12 08:46:50 +02:00
|
|
|
break;
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
2005-04-12 08:46:50 +02:00
|
|
|
at--;
|
|
|
|
}
|
2008-07-13 20:30:12 +02:00
|
|
|
el = strcspn(at, " \n\t\r\v\f>");
|
|
|
|
strbuf_reset(&email);
|
|
|
|
strbuf_add(&email, at, el);
|
mailinfo: avoid violating strbuf assertion
In handle_from, we calculate the end boundary of a section
to remove from a strbuf using strcspn like this:
el = strcspn(buf, set_of_end_boundaries);
strbuf_remove(&sb, start, el + 1);
This works fine if "el" is the offset of the boundary
character, meaning we remove up to and including that
character. But if the end boundary didn't match (that is, we
hit the end of the string as the boundary instead) then we
want just "el". Asking for "el+1" caught an out-of-bounds
assertion in the strbuf library.
This manifested itself when we got a 'From' header that had
just an email address with nothing else in it (the end of
the string was the end of the address, rather than, e.g., a
trailing '>' character), causing git-mailinfo to barf.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2008-08-19 19:28:24 +02:00
|
|
|
strbuf_remove(&f, at - f.buf, el + (at[el] ? 1 : 0));
|
2005-04-12 08:46:50 +02:00
|
|
|
|
2009-02-01 18:45:05 +01:00
|
|
|
/* The remainder is name. It could be
|
|
|
|
*
|
|
|
|
* - "John Doe <john.doe@xz>" (a), or
|
|
|
|
* - "john.doe@xz (John Doe)" (b), or
|
|
|
|
* - "John (zzz) Doe <john.doe@xz> (Comment)" (c)
|
|
|
|
*
|
|
|
|
* but we have removed the email part, so
|
|
|
|
*
|
|
|
|
* - remove extra spaces which could stay after email (case 'c'), and
|
|
|
|
* - trim from both ends, possibly removing the () pair at the end
|
|
|
|
* (cases 'a' and 'b').
|
2005-08-28 21:33:16 +02:00
|
|
|
*/
|
2009-02-01 18:45:05 +01:00
|
|
|
cleanup_space(&f);
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_trim(&f);
|
2008-07-21 15:34:29 +02:00
|
|
|
if (f.buf[0] == '(' && f.len && f.buf[f.len - 1] == ')') {
|
|
|
|
strbuf_remove(&f, 0, 1);
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_setlen(&f, f.len - 1);
|
2008-07-21 15:34:29 +02:00
|
|
|
}
|
2005-04-12 08:46:50 +02:00
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
get_sane_name(&name, &f, &email);
|
|
|
|
strbuf_release(&f);
|
2005-04-12 08:46:50 +02:00
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static void handle_header(struct strbuf **out, const struct strbuf *line)
|
2005-05-02 06:42:53 +02:00
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
if (!*out) {
|
|
|
|
*out = xmalloc(sizeof(struct strbuf));
|
|
|
|
strbuf_init(*out, line->len);
|
|
|
|
} else
|
|
|
|
strbuf_reset(*out);
|
2007-03-12 20:52:04 +01:00
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_addbuf(*out, line);
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
|
|
|
|
* to have enough heuristics to grok MIME encoded patches often found
|
|
|
|
* on our mailing lists. For example, we do not even treat header lines
|
|
|
|
* case insensitively.
|
|
|
|
*/
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static int slurp_attr(const char *line, const char *name, struct strbuf *attr)
|
2005-08-28 21:33:16 +02:00
|
|
|
{
|
2006-06-28 11:04:39 +02:00
|
|
|
const char *ends, *ap = strcasestr(line, name);
|
2005-08-28 21:33:16 +02:00
|
|
|
size_t sz;
|
|
|
|
|
2012-09-17 23:17:58 +02:00
|
|
|
strbuf_setlen(attr, 0);
|
|
|
|
if (!ap)
|
2005-08-28 21:33:16 +02:00
|
|
|
return 0;
|
|
|
|
ap += strlen(name);
|
|
|
|
if (*ap == '"') {
|
|
|
|
ap++;
|
|
|
|
ends = "\"";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ends = "; \t";
|
|
|
|
sz = strcspn(ap, ends);
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_add(attr, ap, sz);
|
2005-08-28 21:33:16 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static struct strbuf *content[MAX_BOUNDARIES];
|
2007-03-12 20:52:04 +01:00
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static struct strbuf **content_top = content;
|
2007-03-12 20:52:04 +01:00
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static void handle_content_type(struct strbuf *line)
|
2005-08-28 21:33:16 +02:00
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
struct strbuf *boundary = xmalloc(sizeof(struct strbuf));
|
|
|
|
strbuf_init(boundary, line->len);
|
2007-03-12 20:52:04 +01:00
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
if (slurp_attr(line->buf, "boundary=", boundary)) {
|
|
|
|
strbuf_insert(boundary, 0, "--", 2);
|
2008-08-14 17:35:42 +02:00
|
|
|
if (++content_top > &content[MAX_BOUNDARIES]) {
|
2007-03-12 20:52:04 +01:00
|
|
|
fprintf(stderr, "Too many boundaries to handle\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-07-13 20:30:12 +02:00
|
|
|
*content_top = boundary;
|
|
|
|
boundary = NULL;
|
2005-09-07 01:46:34 +02:00
|
|
|
}
|
2009-05-19 01:44:40 +02:00
|
|
|
slurp_attr(line->buf, "charset=", &charset);
|
2008-07-13 20:30:12 +02:00
|
|
|
|
|
|
|
if (boundary) {
|
|
|
|
strbuf_release(boundary);
|
|
|
|
free(boundary);
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 15:00:55 +01:00
|
|
|
static void handle_message_id(const struct strbuf *line)
|
|
|
|
{
|
|
|
|
if (add_message_id)
|
|
|
|
message_id = strdup(line->buf);
|
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static void handle_content_transfer_encoding(const struct strbuf *line)
|
2005-08-28 21:33:16 +02:00
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
if (strcasestr(line->buf, "base64"))
|
2005-08-28 21:33:16 +02:00
|
|
|
transfer_encoding = TE_BASE64;
|
2008-07-13 20:30:12 +02:00
|
|
|
else if (strcasestr(line->buf, "quoted-printable"))
|
2005-08-28 21:33:16 +02:00
|
|
|
transfer_encoding = TE_QP;
|
|
|
|
else
|
|
|
|
transfer_encoding = TE_DONTCARE;
|
2005-04-12 08:46:50 +02:00
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static int is_multipart_boundary(const struct strbuf *line)
|
2005-08-28 21:33:16 +02:00
|
|
|
{
|
2008-08-09 10:17:24 +02:00
|
|
|
return (((*content_top)->len <= line->len) &&
|
|
|
|
!memcmp(line->buf, (*content_top)->buf, (*content_top)->len));
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static void cleanup_subject(struct strbuf *subject)
|
2005-04-12 08:46:50 +02:00
|
|
|
{
|
2009-07-16 00:31:12 +02:00
|
|
|
size_t at = 0;
|
|
|
|
|
|
|
|
while (at < subject->len) {
|
|
|
|
char *pos;
|
|
|
|
size_t remove;
|
|
|
|
|
|
|
|
switch (subject->buf[at]) {
|
2005-04-12 08:46:50 +02:00
|
|
|
case 'r': case 'R':
|
2009-07-16 00:31:12 +02:00
|
|
|
if (subject->len <= at + 3)
|
2008-07-13 20:30:12 +02:00
|
|
|
break;
|
2012-09-09 11:59:58 +02:00
|
|
|
if ((subject->buf[at + 1] == 'e' ||
|
|
|
|
subject->buf[at + 1] == 'E') &&
|
|
|
|
subject->buf[at + 2] == ':') {
|
2009-07-16 00:31:12 +02:00
|
|
|
strbuf_remove(subject, at, 3);
|
2005-04-12 08:46:50 +02:00
|
|
|
continue;
|
|
|
|
}
|
2009-07-16 00:31:12 +02:00
|
|
|
at++;
|
2005-04-12 08:46:50 +02:00
|
|
|
break;
|
|
|
|
case ' ': case '\t': case ':':
|
2009-07-16 00:31:12 +02:00
|
|
|
strbuf_remove(subject, at, 1);
|
2005-04-12 08:46:50 +02:00
|
|
|
continue;
|
|
|
|
case '[':
|
2009-07-16 00:31:12 +02:00
|
|
|
pos = strchr(subject->buf + at, ']');
|
|
|
|
if (!pos)
|
|
|
|
break;
|
|
|
|
remove = pos - subject->buf + at + 1;
|
|
|
|
if (!keep_non_patch_brackets_in_subject ||
|
|
|
|
(7 <= remove &&
|
|
|
|
memmem(subject->buf + at, remove, "PATCH", 5)))
|
|
|
|
strbuf_remove(subject, at, remove);
|
2012-01-16 11:53:01 +01:00
|
|
|
else {
|
2009-07-16 00:31:12 +02:00
|
|
|
at += remove;
|
2012-01-16 11:53:01 +01:00
|
|
|
/*
|
|
|
|
* If the input had a space after the ], keep
|
|
|
|
* it. We don't bother with finding the end of
|
|
|
|
* the space, since we later normalize it
|
|
|
|
* anyway.
|
|
|
|
*/
|
|
|
|
if (isspace(subject->buf[at]))
|
|
|
|
at += 1;
|
|
|
|
}
|
2009-07-16 00:31:12 +02:00
|
|
|
continue;
|
2005-04-12 08:46:50 +02:00
|
|
|
}
|
2009-07-16 00:31:12 +02:00
|
|
|
break;
|
2005-04-12 08:46:50 +02:00
|
|
|
}
|
2009-07-16 00:31:12 +02:00
|
|
|
strbuf_trim(subject);
|
2006-06-13 22:21:50 +02:00
|
|
|
}
|
2005-04-12 08:46:50 +02:00
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static void cleanup_space(struct strbuf *sb)
|
2005-04-12 08:46:50 +02:00
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
size_t pos, cnt;
|
|
|
|
for (pos = 0; pos < sb->len; pos++) {
|
|
|
|
if (isspace(sb->buf[pos])) {
|
|
|
|
sb->buf[pos] = ' ';
|
|
|
|
for (cnt = 0; isspace(sb->buf[pos + cnt + 1]); cnt++);
|
|
|
|
strbuf_remove(sb, pos + 1, cnt);
|
2005-04-12 08:46:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static void decode_header(struct strbuf *line);
|
2007-10-21 06:12:12 +02:00
|
|
|
static const char *header[MAX_HDR_PARSED] = {
|
2007-03-12 20:52:04 +01:00
|
|
|
"From","Subject","Date",
|
2005-08-28 21:33:16 +02:00
|
|
|
};
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static inline int cmp_header(const struct strbuf *line, const char *hdr)
|
2005-08-28 21:33:16 +02:00
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
int len = strlen(hdr);
|
|
|
|
return !strncasecmp(line->buf, hdr, len) && line->len > len &&
|
|
|
|
line->buf[len] == ':' && isspace(line->buf[len + 1]);
|
|
|
|
}
|
2005-08-28 21:33:16 +02:00
|
|
|
|
2014-09-14 03:30:38 +02:00
|
|
|
static int is_format_patch_separator(const char *line, int len)
|
|
|
|
{
|
2014-09-21 11:13:58 +02:00
|
|
|
static const char SAMPLE[] =
|
|
|
|
"From e6807f3efca28b30decfecb1732a56c7db1137ee Mon Sep 17 00:00:00 2001\n";
|
2014-09-14 03:30:38 +02:00
|
|
|
const char *cp;
|
|
|
|
|
|
|
|
if (len != strlen(SAMPLE))
|
|
|
|
return 0;
|
|
|
|
if (!skip_prefix(line, "From ", &cp))
|
|
|
|
return 0;
|
|
|
|
if (strspn(cp, "0123456789abcdef") != 40)
|
|
|
|
return 0;
|
|
|
|
cp += 40;
|
|
|
|
return !memcmp(SAMPLE + (cp - line), cp, strlen(SAMPLE) - (cp - line));
|
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static int check_header(const struct strbuf *line,
|
|
|
|
struct strbuf *hdr_data[], int overwrite)
|
|
|
|
{
|
|
|
|
int i, ret = 0, len;
|
|
|
|
struct strbuf sb = STRBUF_INIT;
|
2007-03-12 20:52:04 +01:00
|
|
|
/* search for the interesting parts */
|
|
|
|
for (i = 0; header[i]; i++) {
|
|
|
|
int len = strlen(header[i]);
|
2008-07-13 20:30:12 +02:00
|
|
|
if ((!hdr_data[i] || overwrite) && cmp_header(line, header[i])) {
|
2006-05-23 21:45:37 +02:00
|
|
|
/* Unwrap inline B and Q encoding, and optionally
|
|
|
|
* normalize the meta information to utf8.
|
|
|
|
*/
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_add(&sb, line->buf + len + 2, line->len - len - 2);
|
|
|
|
decode_header(&sb);
|
|
|
|
handle_header(&hdr_data[i], &sb);
|
|
|
|
ret = 1;
|
|
|
|
goto check_header_out;
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-12 20:52:04 +01:00
|
|
|
/* Content stuff */
|
2008-07-13 20:30:12 +02:00
|
|
|
if (cmp_header(line, "Content-Type")) {
|
|
|
|
len = strlen("Content-Type: ");
|
|
|
|
strbuf_add(&sb, line->buf + len, line->len - len);
|
|
|
|
decode_header(&sb);
|
|
|
|
strbuf_insert(&sb, 0, "Content-Type: ", len);
|
|
|
|
handle_content_type(&sb);
|
|
|
|
ret = 1;
|
|
|
|
goto check_header_out;
|
|
|
|
}
|
|
|
|
if (cmp_header(line, "Content-Transfer-Encoding")) {
|
|
|
|
len = strlen("Content-Transfer-Encoding: ");
|
|
|
|
strbuf_add(&sb, line->buf + len, line->len - len);
|
|
|
|
decode_header(&sb);
|
|
|
|
handle_content_transfer_encoding(&sb);
|
|
|
|
ret = 1;
|
|
|
|
goto check_header_out;
|
2007-03-12 20:52:04 +01:00
|
|
|
}
|
2014-11-25 15:00:55 +01:00
|
|
|
if (cmp_header(line, "Message-Id")) {
|
|
|
|
len = strlen("Message-Id: ");
|
|
|
|
strbuf_add(&sb, line->buf + len, line->len - len);
|
|
|
|
decode_header(&sb);
|
|
|
|
handle_message_id(&sb);
|
|
|
|
ret = 1;
|
|
|
|
goto check_header_out;
|
|
|
|
}
|
2007-03-12 20:52:04 +01:00
|
|
|
|
|
|
|
/* for inbody stuff */
|
2013-11-30 21:55:40 +01:00
|
|
|
if (starts_with(line->buf, ">From") && isspace(line->buf[5])) {
|
2014-09-14 03:30:38 +02:00
|
|
|
ret = is_format_patch_separator(line->buf + 1, line->len - 1);
|
2008-07-13 20:30:12 +02:00
|
|
|
goto check_header_out;
|
|
|
|
}
|
2013-11-30 21:55:40 +01:00
|
|
|
if (starts_with(line->buf, "[PATCH]") && isspace(line->buf[7])) {
|
2007-03-12 20:52:04 +01:00
|
|
|
for (i = 0; header[i]; i++) {
|
2014-06-01 11:00:40 +02:00
|
|
|
if (!strcmp("Subject", header[i])) {
|
2008-07-13 20:30:12 +02:00
|
|
|
handle_header(&hdr_data[i], line);
|
|
|
|
ret = 1;
|
|
|
|
goto check_header_out;
|
2007-03-12 20:52:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
check_header_out:
|
|
|
|
strbuf_release(&sb);
|
|
|
|
return ret;
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static int is_rfc2822_header(const struct strbuf *line)
|
2006-05-26 09:46:58 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The section that defines the loosest possible
|
|
|
|
* field name is "3.6.8 Optional fields".
|
|
|
|
*
|
|
|
|
* optional-field = field-name ":" unstructured CRLF
|
|
|
|
* field-name = 1*ftext
|
|
|
|
* ftext = %d33-57 / %59-126
|
|
|
|
*/
|
|
|
|
int ch;
|
2008-07-13 20:30:12 +02:00
|
|
|
char *cp = line->buf;
|
2007-02-26 20:10:59 +01:00
|
|
|
|
|
|
|
/* Count mbox From headers as headers */
|
2013-11-30 21:55:40 +01:00
|
|
|
if (starts_with(cp, "From ") || starts_with(cp, ">From "))
|
2007-02-26 20:10:59 +01:00
|
|
|
return 1;
|
|
|
|
|
2006-05-26 09:46:58 +02:00
|
|
|
while ((ch = *cp++)) {
|
|
|
|
if (ch == ':')
|
2008-07-13 20:30:12 +02:00
|
|
|
return 1;
|
2006-05-26 09:46:58 +02:00
|
|
|
if ((33 <= ch && ch <= 57) ||
|
|
|
|
(59 <= ch && ch <= 126))
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static int read_one_header_line(struct strbuf *line, FILE *in)
|
2005-08-28 21:33:16 +02:00
|
|
|
{
|
2007-02-26 20:10:59 +01:00
|
|
|
/* Get the first part of the line. */
|
2008-07-13 20:30:12 +02:00
|
|
|
if (strbuf_getline(line, in, '\n'))
|
2007-02-26 20:10:59 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Is it an empty line or not a valid rfc2822 header?
|
|
|
|
* If so, stop here, and return false ("not a header")
|
|
|
|
*/
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_rtrim(line);
|
|
|
|
if (!line->len || !is_rfc2822_header(line)) {
|
2007-02-26 20:10:59 +01:00
|
|
|
/* Re-add the newline */
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_addch(line, '\n');
|
2007-02-26 20:10:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now we need to eat all the continuation lines..
|
|
|
|
* Yuck, 2822 header "folding"
|
|
|
|
*/
|
|
|
|
for (;;) {
|
2008-07-13 20:30:12 +02:00
|
|
|
int peek;
|
|
|
|
struct strbuf continuation = STRBUF_INIT;
|
2007-02-26 20:10:59 +01:00
|
|
|
|
2006-05-23 21:53:20 +02:00
|
|
|
peek = fgetc(in); ungetc(peek, in);
|
|
|
|
if (peek != ' ' && peek != '\t')
|
|
|
|
break;
|
2008-07-13 20:30:12 +02:00
|
|
|
if (strbuf_getline(&continuation, in, '\n'))
|
2007-02-26 20:10:59 +01:00
|
|
|
break;
|
2011-05-26 22:53:38 +02:00
|
|
|
continuation.buf[0] = ' ';
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_rtrim(&continuation);
|
|
|
|
strbuf_addbuf(line, &continuation);
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
2007-02-26 20:10:59 +01:00
|
|
|
|
|
|
|
return 1;
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047)
|
2005-08-28 21:33:16 +02:00
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
const char *in = q_seg->buf;
|
2005-08-28 21:33:16 +02:00
|
|
|
int c;
|
2008-07-13 20:30:12 +02:00
|
|
|
struct strbuf *out = xmalloc(sizeof(struct strbuf));
|
|
|
|
strbuf_init(out, q_seg->len);
|
|
|
|
|
|
|
|
while ((c = *in++) != 0) {
|
2005-08-28 21:33:16 +02:00
|
|
|
if (c == '=') {
|
|
|
|
int d = *in++;
|
|
|
|
if (d == '\n' || !d)
|
|
|
|
break; /* drop trailing newline */
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_addch(out, (hexval(d) << 4) | hexval(*in++));
|
2006-04-21 09:06:58 +02:00
|
|
|
continue;
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
2006-04-21 09:06:58 +02:00
|
|
|
if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
|
|
|
|
c = 0x20;
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_addch(out, c);
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
2008-07-13 20:30:12 +02:00
|
|
|
return out;
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static struct strbuf *decode_b_segment(const struct strbuf *b_seg)
|
2005-08-28 21:33:16 +02:00
|
|
|
{
|
|
|
|
/* Decode in..ep, possibly in-place to ot */
|
|
|
|
int c, pos = 0, acc = 0;
|
2008-07-13 20:30:12 +02:00
|
|
|
const char *in = b_seg->buf;
|
|
|
|
struct strbuf *out = xmalloc(sizeof(struct strbuf));
|
|
|
|
strbuf_init(out, b_seg->len);
|
2005-08-28 21:33:16 +02:00
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
while ((c = *in++) != 0) {
|
2005-08-28 21:33:16 +02:00
|
|
|
if (c == '+')
|
|
|
|
c = 62;
|
|
|
|
else if (c == '/')
|
|
|
|
c = 63;
|
|
|
|
else if ('A' <= c && c <= 'Z')
|
|
|
|
c -= 'A';
|
|
|
|
else if ('a' <= c && c <= 'z')
|
|
|
|
c -= 'a' - 26;
|
|
|
|
else if ('0' <= c && c <= '9')
|
|
|
|
c -= '0' - 52;
|
|
|
|
else
|
|
|
|
continue; /* garbage */
|
|
|
|
switch (pos++) {
|
|
|
|
case 0:
|
|
|
|
acc = (c << 2);
|
|
|
|
break;
|
|
|
|
case 1:
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_addch(out, (acc | (c >> 4)));
|
2005-08-28 21:33:16 +02:00
|
|
|
acc = (c & 15) << 4;
|
|
|
|
break;
|
|
|
|
case 2:
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_addch(out, (acc | (c >> 2)));
|
2005-08-28 21:33:16 +02:00
|
|
|
acc = (c & 3) << 6;
|
|
|
|
break;
|
|
|
|
case 3:
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_addch(out, (acc | c));
|
2005-08-28 21:33:16 +02:00
|
|
|
acc = pos = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-07-13 20:30:12 +02:00
|
|
|
return out;
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static void convert_to_utf8(struct strbuf *line, const char *charset)
|
2005-08-28 21:33:16 +02:00
|
|
|
{
|
Do a better job at guessing unknown character sets
At least in the kernel development community, we're generally slowly
converting to UTF-8 everywhere, and the old default of Latin1 in emails is
being supplanted by UTF-8, and it doesn't necessarily show up as such in
the mail headers (because, quite frankly, when people send patches
around, they want the email client to do as little as humanly possible
about the patch)
Despite that, it's often the case that email addresses etc still have
Latin1, so I've seen emails where this is a mixed bag, with Signed-off
parts being copied from email (and containing Latin1 characters), and the
rest of the email being a patch in UTF-8.
So this suggests a very natural change: if the target character set is
utf-8 (the default), and if the source already looks like utf-8, just
assume that it doesn't need any conversion at all.
Only assume that it needs conversion if it isn't already valid utf-8, in
which case we (for historical reasons) will assume it's Latin1.
Basically no really _valid_ latin1 will ever look like utf-8, so while
this changes our historical behaviour, it doesn't do so in practice, and
makes the default behaviour saner for the case where the input was already
in proper format.
We could do a more fancy guess, of course, but this correctly handled a
series of patches I just got from Andrew that had a mixture of Latin1 and
UTF-8 (in different emails, but without any character set indication).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2007-07-17 19:34:44 +02:00
|
|
|
char *out;
|
|
|
|
|
2012-06-28 20:24:14 +02:00
|
|
|
if (!charset || !*charset)
|
|
|
|
return;
|
2006-12-24 08:36:55 +01:00
|
|
|
|
2012-10-19 07:41:56 +02:00
|
|
|
if (same_encoding(metainfo_charset, charset))
|
2007-07-24 02:03:26 +02:00
|
|
|
return;
|
2008-07-13 20:30:12 +02:00
|
|
|
out = reencode_string(line->buf, metainfo_charset, charset);
|
2007-01-10 06:31:36 +01:00
|
|
|
if (!out)
|
2009-01-04 19:38:41 +01:00
|
|
|
die("cannot convert from %s to %s",
|
Do a better job at guessing unknown character sets
At least in the kernel development community, we're generally slowly
converting to UTF-8 everywhere, and the old default of Latin1 in emails is
being supplanted by UTF-8, and it doesn't necessarily show up as such in
the mail headers (because, quite frankly, when people send patches
around, they want the email client to do as little as humanly possible
about the patch)
Despite that, it's often the case that email addresses etc still have
Latin1, so I've seen emails where this is a mixed bag, with Signed-off
parts being copied from email (and containing Latin1 characters), and the
rest of the email being a patch in UTF-8.
So this suggests a very natural change: if the target character set is
utf-8 (the default), and if the source already looks like utf-8, just
assume that it doesn't need any conversion at all.
Only assume that it needs conversion if it isn't already valid utf-8, in
which case we (for historical reasons) will assume it's Latin1.
Basically no really _valid_ latin1 will ever look like utf-8, so while
this changes our historical behaviour, it doesn't do so in practice, and
makes the default behaviour saner for the case where the input was already
in proper format.
We could do a more fancy guess, of course, but this correctly handled a
series of patches I just got from Andrew that had a mixture of Latin1 and
UTF-8 (in different emails, but without any character set indication).
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2007-07-17 19:34:44 +02:00
|
|
|
charset, metainfo_charset);
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_attach(line, out, strlen(out), strlen(out));
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static int decode_header_bq(struct strbuf *it)
|
2005-08-28 21:33:16 +02:00
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
char *in, *ep, *cp;
|
|
|
|
struct strbuf outbuf = STRBUF_INIT, *dec;
|
|
|
|
struct strbuf charset_q = STRBUF_INIT, piecebuf = STRBUF_INIT;
|
2006-07-05 23:17:49 +02:00
|
|
|
int rfc2047 = 0;
|
2005-08-28 21:33:16 +02:00
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
in = it->buf;
|
|
|
|
while (in - it->buf <= it->len && (ep = strstr(in, "=?")) != NULL) {
|
|
|
|
int encoding;
|
|
|
|
strbuf_reset(&charset_q);
|
|
|
|
strbuf_reset(&piecebuf);
|
2006-07-05 23:17:49 +02:00
|
|
|
rfc2047 = 1;
|
|
|
|
|
2005-08-28 21:33:16 +02:00
|
|
|
if (in != ep) {
|
2009-01-07 23:43:42 +01:00
|
|
|
/*
|
|
|
|
* We are about to process an encoded-word
|
|
|
|
* that begins at ep, but there is something
|
|
|
|
* before the encoded word.
|
|
|
|
*/
|
|
|
|
char *scan;
|
|
|
|
for (scan = in; scan < ep; scan++)
|
|
|
|
if (!isspace(*scan))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (scan != ep || in == it->buf) {
|
|
|
|
/*
|
|
|
|
* We should not lose that "something",
|
|
|
|
* unless we have just processed an
|
|
|
|
* encoded-word, and there is only LWS
|
|
|
|
* before the one we are about to process.
|
|
|
|
*/
|
|
|
|
strbuf_add(&outbuf, in, ep - in);
|
|
|
|
}
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
|
|
|
/* E.g.
|
|
|
|
* ep : "=?iso-2022-jp?B?GyR...?= foo"
|
|
|
|
* ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
|
|
|
|
*/
|
|
|
|
ep += 2;
|
2008-07-13 20:30:12 +02:00
|
|
|
|
|
|
|
if (ep - it->buf >= it->len || !(cp = strchr(ep, '?')))
|
|
|
|
goto decode_header_bq_out;
|
|
|
|
|
|
|
|
if (cp + 3 - it->buf > it->len)
|
|
|
|
goto decode_header_bq_out;
|
|
|
|
strbuf_add(&charset_q, ep, cp - ep);
|
|
|
|
|
2005-08-28 21:33:16 +02:00
|
|
|
encoding = cp[1];
|
|
|
|
if (!encoding || cp[2] != '?')
|
2008-07-13 20:30:12 +02:00
|
|
|
goto decode_header_bq_out;
|
2005-08-28 21:33:16 +02:00
|
|
|
ep = strstr(cp + 3, "?=");
|
|
|
|
if (!ep)
|
2008-07-13 20:30:12 +02:00
|
|
|
goto decode_header_bq_out;
|
|
|
|
strbuf_add(&piecebuf, cp + 3, ep - cp - 3);
|
2005-08-28 21:33:16 +02:00
|
|
|
switch (tolower(encoding)) {
|
|
|
|
default:
|
2008-07-13 20:30:12 +02:00
|
|
|
goto decode_header_bq_out;
|
2005-08-28 21:33:16 +02:00
|
|
|
case 'b':
|
2008-07-13 20:30:12 +02:00
|
|
|
dec = decode_b_segment(&piecebuf);
|
2005-08-28 21:33:16 +02:00
|
|
|
break;
|
|
|
|
case 'q':
|
2008-07-13 20:30:12 +02:00
|
|
|
dec = decode_q_segment(&piecebuf, 1);
|
2005-08-28 21:33:16 +02:00
|
|
|
break;
|
|
|
|
}
|
2005-11-28 01:22:16 +01:00
|
|
|
if (metainfo_charset)
|
2008-07-13 20:30:12 +02:00
|
|
|
convert_to_utf8(dec, charset_q.buf);
|
2007-08-30 23:48:24 +02:00
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_addbuf(&outbuf, dec);
|
|
|
|
strbuf_release(dec);
|
|
|
|
free(dec);
|
2005-08-28 21:33:16 +02:00
|
|
|
in = ep + 2;
|
|
|
|
}
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_addstr(&outbuf, in);
|
|
|
|
strbuf_reset(it);
|
|
|
|
strbuf_addbuf(it, &outbuf);
|
|
|
|
decode_header_bq_out:
|
|
|
|
strbuf_release(&outbuf);
|
|
|
|
strbuf_release(&charset_q);
|
|
|
|
strbuf_release(&piecebuf);
|
2006-07-05 23:17:49 +02:00
|
|
|
return rfc2047;
|
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static void decode_header(struct strbuf *it)
|
2006-07-05 23:17:49 +02:00
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
if (decode_header_bq(it))
|
2006-07-05 23:17:49 +02:00
|
|
|
return;
|
|
|
|
/* otherwise "it" is a straight copy of the input.
|
|
|
|
* This can be binary guck but there is no charset specified.
|
|
|
|
*/
|
|
|
|
if (metainfo_charset)
|
2008-07-13 20:30:12 +02:00
|
|
|
convert_to_utf8(it, "");
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static void decode_transfer_encoding(struct strbuf *line)
|
2005-08-28 21:33:16 +02:00
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
struct strbuf *ret;
|
2005-08-28 21:33:16 +02:00
|
|
|
|
|
|
|
switch (transfer_encoding) {
|
|
|
|
case TE_QP:
|
2008-07-13 20:30:12 +02:00
|
|
|
ret = decode_q_segment(line, 0);
|
|
|
|
break;
|
2005-08-28 21:33:16 +02:00
|
|
|
case TE_BASE64:
|
2008-07-13 20:30:12 +02:00
|
|
|
ret = decode_b_segment(line);
|
|
|
|
break;
|
2005-08-28 21:33:16 +02:00
|
|
|
case TE_DONTCARE:
|
2008-05-25 10:16:05 +02:00
|
|
|
default:
|
2008-07-13 20:30:12 +02:00
|
|
|
return;
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_reset(line);
|
|
|
|
strbuf_addbuf(line, ret);
|
|
|
|
strbuf_release(ret);
|
|
|
|
free(ret);
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static void handle_filter(struct strbuf *line);
|
2007-03-12 20:52:04 +01:00
|
|
|
|
|
|
|
static int find_boundary(void)
|
2005-04-12 08:46:50 +02:00
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
while (!strbuf_getline(&line, fin, '\n')) {
|
2008-08-14 17:35:42 +02:00
|
|
|
if (*content_top && is_multipart_boundary(&line))
|
2007-03-12 20:52:04 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int handle_boundary(void)
|
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
struct strbuf newline = STRBUF_INIT;
|
|
|
|
|
|
|
|
strbuf_addch(&newline, '\n');
|
2007-03-12 20:52:04 +01:00
|
|
|
again:
|
2008-07-13 20:30:12 +02:00
|
|
|
if (line.len >= (*content_top)->len + 2 &&
|
|
|
|
!memcmp(line.buf + (*content_top)->len, "--", 2)) {
|
2007-03-12 20:52:04 +01:00
|
|
|
/* we hit an end boundary */
|
|
|
|
/* pop the current boundary off the stack */
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_release(*content_top);
|
|
|
|
free(*content_top);
|
|
|
|
*content_top = NULL;
|
2007-03-12 20:52:04 +01:00
|
|
|
|
|
|
|
/* technically won't happen as is_multipart_boundary()
|
|
|
|
will fail first. But just in case..
|
|
|
|
*/
|
2008-08-14 17:35:42 +02:00
|
|
|
if (--content_top < content) {
|
2007-03-12 20:52:04 +01:00
|
|
|
fprintf(stderr, "Detected mismatched boundaries, "
|
|
|
|
"can't recover\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-07-13 20:30:12 +02:00
|
|
|
handle_filter(&newline);
|
|
|
|
strbuf_release(&newline);
|
2007-03-12 20:52:04 +01:00
|
|
|
|
|
|
|
/* skip to the next boundary */
|
|
|
|
if (!find_boundary())
|
|
|
|
return 0;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set some defaults */
|
|
|
|
transfer_encoding = TE_DONTCARE;
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_reset(&charset);
|
2005-08-28 21:33:16 +02:00
|
|
|
|
2007-03-12 20:52:04 +01:00
|
|
|
/* slurp in this section's info */
|
2008-07-13 20:30:12 +02:00
|
|
|
while (read_one_header_line(&line, fin))
|
|
|
|
check_header(&line, p_hdr_data, 0);
|
2005-04-12 08:46:50 +02:00
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_release(&newline);
|
2008-08-09 10:17:24 +02:00
|
|
|
/* replenish line */
|
|
|
|
if (strbuf_getline(&line, fin, '\n'))
|
|
|
|
return 0;
|
|
|
|
strbuf_addch(&line, '\n');
|
|
|
|
return 1;
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static inline int patchbreak(const struct strbuf *line)
|
2007-03-12 20:52:06 +01:00
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
size_t i;
|
|
|
|
|
2007-03-12 20:52:06 +01:00
|
|
|
/* Beginning of a "diff -" header? */
|
2013-11-30 21:55:40 +01:00
|
|
|
if (starts_with(line->buf, "diff -"))
|
2007-03-12 20:52:06 +01:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* CVS "Index: " line? */
|
2013-11-30 21:55:40 +01:00
|
|
|
if (starts_with(line->buf, "Index: "))
|
2007-03-12 20:52:06 +01:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "--- <filename>" starts patches without headers
|
|
|
|
* "---<sp>*" is a manual separator
|
|
|
|
*/
|
2008-07-13 20:30:12 +02:00
|
|
|
if (line->len < 4)
|
|
|
|
return 0;
|
|
|
|
|
2013-11-30 21:55:40 +01:00
|
|
|
if (starts_with(line->buf, "---")) {
|
2007-03-12 20:52:06 +01:00
|
|
|
/* space followed by a filename? */
|
2008-07-13 20:30:12 +02:00
|
|
|
if (line->buf[3] == ' ' && !isspace(line->buf[4]))
|
2007-03-12 20:52:06 +01:00
|
|
|
return 1;
|
|
|
|
/* Just whitespace? */
|
2008-07-13 20:30:12 +02:00
|
|
|
for (i = 3; i < line->len; i++) {
|
|
|
|
unsigned char c = line->buf[i];
|
2007-03-12 20:52:06 +01:00
|
|
|
if (c == '\n')
|
|
|
|
return 1;
|
|
|
|
if (!isspace(c))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-23 10:56:19 +02:00
|
|
|
static int is_scissors_line(const struct strbuf *line)
|
|
|
|
{
|
|
|
|
size_t i, len = line->len;
|
|
|
|
int scissors = 0, gap = 0;
|
|
|
|
int first_nonblank = -1;
|
|
|
|
int last_nonblank = 0, visible, perforation = 0, in_perforation = 0;
|
|
|
|
const char *buf = line->buf;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (isspace(buf[i])) {
|
|
|
|
if (in_perforation) {
|
|
|
|
perforation++;
|
|
|
|
gap++;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
last_nonblank = i;
|
|
|
|
if (first_nonblank < 0)
|
|
|
|
first_nonblank = i;
|
|
|
|
if (buf[i] == '-') {
|
|
|
|
in_perforation = 1;
|
|
|
|
perforation++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (i + 1 < len &&
|
2010-04-03 21:52:17 +02:00
|
|
|
(!memcmp(buf + i, ">8", 2) || !memcmp(buf + i, "8<", 2) ||
|
|
|
|
!memcmp(buf + i, ">%", 2) || !memcmp(buf + i, "%<", 2))) {
|
2009-08-23 10:56:19 +02:00
|
|
|
in_perforation = 1;
|
|
|
|
perforation += 2;
|
|
|
|
scissors += 2;
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
in_perforation = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The mark must be at least 8 bytes long (e.g. "-- >8 --").
|
|
|
|
* Even though there can be arbitrary cruft on the same line
|
|
|
|
* (e.g. "cut here"), in order to avoid misidentification, the
|
|
|
|
* perforation must occupy more than a third of the visible
|
|
|
|
* width of the line, and dashes and scissors must occupy more
|
|
|
|
* than half of the perforation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
visible = last_nonblank - first_nonblank + 1;
|
|
|
|
return (scissors && 8 <= visible &&
|
|
|
|
visible < perforation * 3 &&
|
|
|
|
gap * 2 < perforation);
|
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static int handle_commit_msg(struct strbuf *line)
|
2005-08-28 21:33:16 +02:00
|
|
|
{
|
2007-03-12 20:52:04 +01:00
|
|
|
static int still_looking = 1;
|
|
|
|
|
2005-08-28 21:33:16 +02:00
|
|
|
if (!cmitmsg)
|
|
|
|
return 0;
|
2005-04-12 08:46:50 +02:00
|
|
|
|
2007-03-12 20:52:04 +01:00
|
|
|
if (still_looking) {
|
2010-02-20 06:55:33 +01:00
|
|
|
if (!line->len || (line->len == 1 && line->buf[0] == '\n'))
|
2008-07-13 20:30:12 +02:00
|
|
|
return 0;
|
2009-11-20 17:12:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (use_inbody_headers && still_looking) {
|
2009-08-23 10:56:19 +02:00
|
|
|
still_looking = check_header(line, s_hdr_data, 0);
|
|
|
|
if (still_looking)
|
2007-03-12 20:52:04 +01:00
|
|
|
return 0;
|
2009-11-20 17:12:47 +01:00
|
|
|
} else
|
|
|
|
/* Only trim the first (blank) line of the commit message
|
|
|
|
* when ignoring in-body headers.
|
|
|
|
*/
|
|
|
|
still_looking = 0;
|
2006-05-23 21:47:28 +02:00
|
|
|
|
2007-03-30 18:18:45 +02:00
|
|
|
/* normalize the log message to UTF-8. */
|
|
|
|
if (metainfo_charset)
|
2008-07-13 20:30:12 +02:00
|
|
|
convert_to_utf8(line, charset.buf);
|
2007-03-30 18:18:45 +02:00
|
|
|
|
2009-08-27 06:36:05 +02:00
|
|
|
if (use_scissors && is_scissors_line(line)) {
|
2009-08-23 10:56:19 +02:00
|
|
|
int i;
|
2009-09-29 08:40:08 +02:00
|
|
|
if (fseek(cmitmsg, 0L, SEEK_SET))
|
|
|
|
die_errno("Could not rewind output message file");
|
|
|
|
if (ftruncate(fileno(cmitmsg), 0))
|
|
|
|
die_errno("Could not truncate output message file at scissors");
|
2009-08-23 10:56:19 +02:00
|
|
|
still_looking = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We may have already read "secondary headers"; purge
|
|
|
|
* them to give ourselves a clean restart.
|
|
|
|
*/
|
|
|
|
for (i = 0; header[i]; i++) {
|
|
|
|
if (s_hdr_data[i])
|
|
|
|
strbuf_release(s_hdr_data[i]);
|
|
|
|
s_hdr_data[i] = NULL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-12 20:52:06 +01:00
|
|
|
if (patchbreak(line)) {
|
2014-11-25 15:00:55 +01:00
|
|
|
if (message_id)
|
|
|
|
fprintf(cmitmsg, "Message-Id: %s\n", message_id);
|
2007-03-12 20:52:04 +01:00
|
|
|
fclose(cmitmsg);
|
|
|
|
cmitmsg = NULL;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-05-23 21:47:28 +02:00
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
fputs(line->buf, cmitmsg);
|
2005-08-28 21:33:16 +02:00
|
|
|
return 0;
|
2005-04-12 08:46:50 +02:00
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static void handle_patch(const struct strbuf *line)
|
2005-04-12 08:46:50 +02:00
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
fwrite(line->buf, 1, line->len, patchfile);
|
2007-03-12 20:52:04 +01:00
|
|
|
patch_lines++;
|
2005-04-12 08:46:50 +02:00
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static void handle_filter(struct strbuf *line)
|
2005-04-12 08:46:50 +02:00
|
|
|
{
|
2007-03-12 20:52:04 +01:00
|
|
|
static int filter = 0;
|
2005-04-12 08:46:50 +02:00
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
/* filter tells us which part we left off on */
|
2007-03-12 20:52:04 +01:00
|
|
|
switch (filter) {
|
|
|
|
case 0:
|
2008-07-13 20:30:12 +02:00
|
|
|
if (!handle_commit_msg(line))
|
2005-08-28 21:33:16 +02:00
|
|
|
break;
|
2007-03-12 20:52:04 +01:00
|
|
|
filter++;
|
|
|
|
case 1:
|
2008-07-13 20:30:12 +02:00
|
|
|
handle_patch(line);
|
|
|
|
break;
|
2005-04-12 08:46:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-12 20:52:04 +01:00
|
|
|
static void handle_body(void)
|
2005-07-23 11:10:31 +02:00
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
struct strbuf prev = STRBUF_INIT;
|
2005-08-28 21:33:16 +02:00
|
|
|
|
|
|
|
/* Skip up to the first boundary */
|
2008-07-13 20:30:12 +02:00
|
|
|
if (*content_top) {
|
2007-03-12 20:52:04 +01:00
|
|
|
if (!find_boundary())
|
2008-07-13 20:30:12 +02:00
|
|
|
goto handle_body_out;
|
2007-03-12 20:52:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* process any boundary lines */
|
2008-07-13 20:30:12 +02:00
|
|
|
if (*content_top && is_multipart_boundary(&line)) {
|
2007-03-12 20:52:04 +01:00
|
|
|
/* flush any leftover */
|
2008-08-09 10:17:24 +02:00
|
|
|
if (prev.len) {
|
|
|
|
handle_filter(&prev);
|
|
|
|
strbuf_reset(&prev);
|
|
|
|
}
|
2007-03-12 20:52:04 +01:00
|
|
|
if (!handle_boundary())
|
2008-07-13 20:30:12 +02:00
|
|
|
goto handle_body_out;
|
2007-03-12 20:52:04 +01:00
|
|
|
}
|
|
|
|
|
2007-03-30 18:18:45 +02:00
|
|
|
/* Unwrap transfer encoding */
|
2008-07-13 20:30:12 +02:00
|
|
|
decode_transfer_encoding(&line);
|
2007-03-12 20:52:04 +01:00
|
|
|
|
|
|
|
switch (transfer_encoding) {
|
|
|
|
case TE_BASE64:
|
2008-02-15 22:53:36 +01:00
|
|
|
case TE_QP:
|
2007-03-12 20:52:04 +01:00
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
struct strbuf **lines, **it, *sb;
|
|
|
|
|
|
|
|
/* Prepend any previous partial lines */
|
|
|
|
strbuf_insert(&line, 0, prev.buf, prev.len);
|
|
|
|
strbuf_reset(&prev);
|
2007-03-12 20:52:04 +01:00
|
|
|
|
2008-05-25 10:16:05 +02:00
|
|
|
/*
|
|
|
|
* This is a decoded line that may contain
|
2007-03-12 20:52:04 +01:00
|
|
|
* multiple new lines. Pass only one chunk
|
|
|
|
* at a time to handle_filter()
|
|
|
|
*/
|
2008-07-13 20:30:12 +02:00
|
|
|
lines = strbuf_split(&line, '\n');
|
|
|
|
for (it = lines; (sb = *it); it++) {
|
|
|
|
if (*(it + 1) == NULL) /* The last line */
|
|
|
|
if (sb->buf[sb->len - 1] != '\n') {
|
|
|
|
/* Partial line, save it for later. */
|
|
|
|
strbuf_addbuf(&prev, sb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
handle_filter(sb);
|
|
|
|
}
|
2008-05-25 10:16:05 +02:00
|
|
|
/*
|
2008-07-13 20:30:12 +02:00
|
|
|
* The partial chunk is saved in "prev" and will be
|
2008-05-25 10:16:05 +02:00
|
|
|
* appended by the next iteration of read_line_with_nul().
|
2007-03-12 20:52:04 +01:00
|
|
|
*/
|
2008-07-13 20:30:12 +02:00
|
|
|
strbuf_list_free(lines);
|
2005-08-28 21:33:16 +02:00
|
|
|
break;
|
2005-07-23 11:10:31 +02:00
|
|
|
}
|
2007-03-12 20:52:04 +01:00
|
|
|
default:
|
2008-07-13 20:30:12 +02:00
|
|
|
handle_filter(&line);
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
2007-03-12 20:52:04 +01:00
|
|
|
|
2009-08-05 05:31:57 +02:00
|
|
|
} while (!strbuf_getwholeline(&line, fin, '\n'));
|
2008-07-13 20:30:12 +02:00
|
|
|
|
|
|
|
handle_body_out:
|
|
|
|
strbuf_release(&prev);
|
2005-07-23 11:10:31 +02:00
|
|
|
}
|
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
static void output_header_lines(FILE *fout, const char *hdr, const struct strbuf *data)
|
2007-07-29 02:57:25 +02:00
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
const char *sp = data->buf;
|
2007-07-29 02:57:25 +02:00
|
|
|
while (1) {
|
2008-07-13 20:30:12 +02:00
|
|
|
char *ep = strchr(sp, '\n');
|
2007-07-29 02:57:25 +02:00
|
|
|
int len;
|
|
|
|
if (!ep)
|
2008-07-13 20:30:12 +02:00
|
|
|
len = strlen(sp);
|
2007-07-29 02:57:25 +02:00
|
|
|
else
|
2008-07-13 20:30:12 +02:00
|
|
|
len = ep - sp;
|
|
|
|
fprintf(fout, "%s: %.*s\n", hdr, len, sp);
|
2007-07-29 02:57:25 +02:00
|
|
|
if (!ep)
|
|
|
|
break;
|
2008-07-13 20:30:12 +02:00
|
|
|
sp = ep + 1;
|
2007-07-29 02:57:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-12 20:52:04 +01:00
|
|
|
static void handle_info(void)
|
2005-04-12 08:46:50 +02:00
|
|
|
{
|
2008-07-13 20:30:12 +02:00
|
|
|
struct strbuf *hdr;
|
2007-03-12 20:52:04 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; header[i]; i++) {
|
|
|
|
/* only print inbody headers if we output a patch file */
|
|
|
|
if (patch_lines && s_hdr_data[i])
|
|
|
|
hdr = s_hdr_data[i];
|
|
|
|
else if (p_hdr_data[i])
|
|
|
|
hdr = p_hdr_data[i];
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
2014-06-01 11:00:40 +02:00
|
|
|
if (!strcmp(header[i], "Subject")) {
|
2008-07-13 20:30:12 +02:00
|
|
|
if (!keep_subject) {
|
|
|
|
cleanup_subject(hdr);
|
|
|
|
cleanup_space(hdr);
|
2007-07-29 02:57:25 +02:00
|
|
|
}
|
2008-07-13 20:30:12 +02:00
|
|
|
output_header_lines(fout, "Subject", hdr);
|
2014-06-01 11:00:40 +02:00
|
|
|
} else if (!strcmp(header[i], "From")) {
|
2009-01-13 00:22:11 +01:00
|
|
|
cleanup_space(hdr);
|
2007-03-12 20:52:04 +01:00
|
|
|
handle_from(hdr);
|
2008-07-13 20:30:12 +02:00
|
|
|
fprintf(fout, "Author: %s\n", name.buf);
|
|
|
|
fprintf(fout, "Email: %s\n", email.buf);
|
2007-03-12 20:52:04 +01:00
|
|
|
} else {
|
|
|
|
cleanup_space(hdr);
|
2008-07-13 20:30:12 +02:00
|
|
|
fprintf(fout, "%s: %s\n", header[i], hdr->buf);
|
2007-03-12 20:52:04 +01:00
|
|
|
}
|
2005-08-28 21:33:16 +02:00
|
|
|
}
|
2007-03-12 20:52:04 +01:00
|
|
|
fprintf(fout, "\n");
|
2005-04-12 08:46:50 +02:00
|
|
|
}
|
|
|
|
|
2009-08-27 07:17:25 +02:00
|
|
|
static int mailinfo(FILE *in, FILE *out, const char *msg, const char *patch)
|
2006-06-13 22:21:50 +02:00
|
|
|
{
|
2007-11-01 23:57:45 +01:00
|
|
|
int peek;
|
2006-06-13 22:21:50 +02:00
|
|
|
fin = in;
|
|
|
|
fout = out;
|
|
|
|
|
|
|
|
cmitmsg = fopen(msg, "w");
|
|
|
|
if (!cmitmsg) {
|
|
|
|
perror(msg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
patchfile = fopen(patch, "w");
|
|
|
|
if (!patchfile) {
|
|
|
|
perror(patch);
|
|
|
|
fclose(cmitmsg);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-03-12 20:52:04 +01:00
|
|
|
|
2008-07-13 20:30:12 +02:00
|
|
|
p_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*p_hdr_data));
|
|
|
|
s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*s_hdr_data));
|
2007-03-12 20:52:04 +01:00
|
|
|
|
2007-11-01 23:57:45 +01:00
|
|
|
do {
|
|
|
|
peek = fgetc(in);
|
|
|
|
} while (isspace(peek));
|
|
|
|
ungetc(peek, in);
|
|
|
|
|
2007-03-12 20:52:04 +01:00
|
|
|
/* process the email header */
|
2008-07-13 20:30:12 +02:00
|
|
|
while (read_one_header_line(&line, fin))
|
|
|
|
check_header(&line, p_hdr_data, 1);
|
2007-03-12 20:52:04 +01:00
|
|
|
|
|
|
|
handle_body();
|
|
|
|
handle_info();
|
2006-06-13 22:21:50 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-27 07:30:33 +02:00
|
|
|
static int git_mailinfo_config(const char *var, const char *value, void *unused)
|
|
|
|
{
|
2013-11-30 21:55:40 +01:00
|
|
|
if (!starts_with(var, "mailinfo."))
|
2009-08-27 07:30:33 +02:00
|
|
|
return git_default_config(var, value, unused);
|
|
|
|
if (!strcmp(var, "mailinfo.scissors")) {
|
|
|
|
use_scissors = git_config_bool(var, value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* perhaps others here */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-17 07:18:27 +02:00
|
|
|
static const char mailinfo_usage[] =
|
2014-11-25 15:00:55 +01:00
|
|
|
"git mailinfo [-k|-b] [-m | --message-id] [-u | --encoding=<encoding> | -n] [--scissors | --no-scissors] msg patch < mail >info";
|
2005-08-28 21:33:16 +02:00
|
|
|
|
2006-07-29 07:44:25 +02:00
|
|
|
int cmd_mailinfo(int argc, const char **argv, const char *prefix)
|
2005-04-12 08:46:50 +02:00
|
|
|
{
|
2007-01-10 06:31:36 +01:00
|
|
|
const char *def_charset;
|
|
|
|
|
2005-11-28 01:29:38 +01:00
|
|
|
/* NEEDSWORK: might want to do the optional .git/ directory
|
|
|
|
* discovery
|
|
|
|
*/
|
2009-08-27 07:30:33 +02:00
|
|
|
git_config(git_mailinfo_config, NULL);
|
2005-11-28 01:29:38 +01:00
|
|
|
|
2010-11-02 20:59:07 +01:00
|
|
|
def_charset = get_commit_output_encoding();
|
2007-01-10 06:31:36 +01:00
|
|
|
metainfo_charset = def_charset;
|
|
|
|
|
2005-08-17 07:18:27 +02:00
|
|
|
while (1 < argc && argv[1][0] == '-') {
|
|
|
|
if (!strcmp(argv[1], "-k"))
|
|
|
|
keep_subject = 1;
|
2009-07-16 00:31:12 +02:00
|
|
|
else if (!strcmp(argv[1], "-b"))
|
|
|
|
keep_non_patch_brackets_in_subject = 1;
|
2014-11-25 15:00:55 +01:00
|
|
|
else if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--message-id"))
|
|
|
|
add_message_id = 1;
|
2005-08-28 21:33:16 +02:00
|
|
|
else if (!strcmp(argv[1], "-u"))
|
2007-01-10 06:31:36 +01:00
|
|
|
metainfo_charset = def_charset;
|
|
|
|
else if (!strcmp(argv[1], "-n"))
|
|
|
|
metainfo_charset = NULL;
|
2013-11-30 21:55:40 +01:00
|
|
|
else if (starts_with(argv[1], "--encoding="))
|
2005-11-28 10:29:52 +01:00
|
|
|
metainfo_charset = argv[1] + 11;
|
2009-08-27 06:36:05 +02:00
|
|
|
else if (!strcmp(argv[1], "--scissors"))
|
|
|
|
use_scissors = 1;
|
|
|
|
else if (!strcmp(argv[1], "--no-scissors"))
|
|
|
|
use_scissors = 0;
|
2009-11-20 17:12:47 +01:00
|
|
|
else if (!strcmp(argv[1], "--no-inbody-headers"))
|
|
|
|
use_inbody_headers = 0;
|
2005-08-28 21:33:16 +02:00
|
|
|
else
|
2005-11-28 01:29:38 +01:00
|
|
|
usage(mailinfo_usage);
|
2005-08-17 07:18:27 +02:00
|
|
|
argc--; argv++;
|
|
|
|
}
|
|
|
|
|
2005-06-23 18:40:23 +02:00
|
|
|
if (argc != 3)
|
2005-11-28 01:29:38 +01:00
|
|
|
usage(mailinfo_usage);
|
2006-06-13 22:21:50 +02:00
|
|
|
|
2009-08-27 07:17:25 +02:00
|
|
|
return !!mailinfo(stdin, stdout, argv[1], argv[2]);
|
2005-04-12 08:46:50 +02:00
|
|
|
}
|