2005-04-12 10:46:50 +04:00
|
|
|
/*
|
|
|
|
* Another stupid program, this one parsing the headers of an
|
|
|
|
* email to figure out authorship and subject
|
|
|
|
*/
|
2005-11-28 03:29:38 +03:00
|
|
|
#include "cache.h"
|
2006-06-14 00:21:50 +04:00
|
|
|
#include "builtin.h"
|
2006-12-24 10:36:55 +03:00
|
|
|
#include "utf8.h"
|
2008-07-13 22:30:12 +04:00
|
|
|
#include "strbuf.h"
|
2005-04-12 10:46:50 +04:00
|
|
|
|
2006-06-14 00:21:50 +04:00
|
|
|
static FILE *cmitmsg, *patchfile, *fin, *fout;
|
2005-04-12 10:46:50 +04:00
|
|
|
|
2006-08-15 21:23:48 +04:00
|
|
|
static int keep_subject;
|
|
|
|
static const char *metainfo_charset;
|
2008-07-13 22:30:12 +04:00
|
|
|
static struct strbuf line = STRBUF_INIT;
|
|
|
|
static struct strbuf name = STRBUF_INIT;
|
|
|
|
static struct strbuf email = STRBUF_INIT;
|
2005-04-12 10:46:50 +04:00
|
|
|
|
2005-08-28 23:33:16 +04:00
|
|
|
static enum {
|
|
|
|
TE_DONTCARE, TE_QP, TE_BASE64,
|
|
|
|
} transfer_encoding;
|
2007-03-12 22:52:04 +03:00
|
|
|
static enum {
|
|
|
|
TYPE_TEXT, TYPE_OTHER,
|
|
|
|
} message_type;
|
2005-08-28 23:33:16 +04:00
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static struct strbuf charset = STRBUF_INIT;
|
2006-08-15 21:23:48 +04:00
|
|
|
static int patch_lines;
|
2008-07-13 22:30:12 +04:00
|
|
|
static struct strbuf **p_hdr_data, **s_hdr_data;
|
2007-03-12 22:52:04 +03:00
|
|
|
|
|
|
|
#define MAX_HDR_PARSED 10
|
|
|
|
#define MAX_BOUNDARIES 5
|
2005-08-28 23:33:16 +04:00
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static void get_sane_name(struct strbuf *out, struct strbuf *name, struct strbuf *email)
|
2005-04-12 10:46:50 +04:00
|
|
|
{
|
2008-07-13 22:30:12 +04: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 10:46:50 +04:00
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static void parse_bogus_from(const struct strbuf *line)
|
2005-12-15 03:31:06 +03:00
|
|
|
{
|
|
|
|
/* John Doe <johndoe> */
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
char *bra, *ket;
|
2005-12-15 03:31:06 +03:00
|
|
|
/* This is fallback, so do not bother if we already have an
|
|
|
|
* e-mail address.
|
2006-06-14 00:21:50 +04:00
|
|
|
*/
|
2008-07-13 22:30:12 +04:00
|
|
|
if (email.len)
|
|
|
|
return;
|
2005-12-15 03:31:06 +03:00
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
bra = strchr(line->buf, '<');
|
2005-12-15 03:31:06 +03:00
|
|
|
if (!bra)
|
2008-07-13 22:30:12 +04:00
|
|
|
return;
|
2005-12-15 03:31:06 +03:00
|
|
|
ket = strchr(bra, '>');
|
|
|
|
if (!ket)
|
2008-07-13 22:30:12 +04:00
|
|
|
return;
|
2005-12-15 03:31:06 +03:00
|
|
|
|
2008-07-13 22:30:12 +04: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 03:31:06 +03:00
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static void handle_from(const struct strbuf *from)
|
2005-04-12 10:46:50 +04:00
|
|
|
{
|
2006-05-23 23:58:36 +04:00
|
|
|
char *at;
|
2008-07-13 22:30:12 +04:00
|
|
|
size_t el;
|
|
|
|
struct strbuf f;
|
2005-04-12 10:46:50 +04:00
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_init(&f, from->len);
|
|
|
|
strbuf_addbuf(&f, from);
|
|
|
|
|
|
|
|
at = strchr(f.buf, '@');
|
|
|
|
if (!at) {
|
|
|
|
parse_bogus_from(from);
|
|
|
|
return;
|
|
|
|
}
|
2005-04-12 10:46:50 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we already have one email, don't take any confusing lines
|
|
|
|
*/
|
2008-07-13 22:30:12 +04:00
|
|
|
if (email.len && strchr(at + 1, '@')) {
|
|
|
|
strbuf_release(&f);
|
|
|
|
return;
|
|
|
|
}
|
2005-04-12 10:46:50 +04:00
|
|
|
|
2005-08-28 23:33:16 +04:00
|
|
|
/* Pick up the string around '@', possibly delimited with <>
|
2008-07-13 22:30:12 +04:00
|
|
|
* pair; that is the email part.
|
2005-08-28 23:33:16 +04:00
|
|
|
*/
|
2008-07-13 22:30:12 +04:00
|
|
|
while (at > f.buf) {
|
2005-04-12 10:46:50 +04:00
|
|
|
char c = at[-1];
|
2005-08-28 23:33:16 +04:00
|
|
|
if (isspace(c))
|
|
|
|
break;
|
|
|
|
if (c == '<') {
|
|
|
|
at[-1] = ' ';
|
2005-04-12 10:46:50 +04:00
|
|
|
break;
|
2005-08-28 23:33:16 +04:00
|
|
|
}
|
2005-04-12 10:46:50 +04:00
|
|
|
at--;
|
|
|
|
}
|
2008-07-13 22:30:12 +04: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 21:28:24 +04:00
|
|
|
strbuf_remove(&f, at - f.buf, el + (at[el] ? 1 : 0));
|
2005-04-12 10:46:50 +04:00
|
|
|
|
2005-08-28 23:33:16 +04:00
|
|
|
/* The remainder is name. It could be "John Doe <john.doe@xz>"
|
2008-07-13 22:30:12 +04:00
|
|
|
* or "john.doe@xz (John Doe)", but we have removed the
|
2005-08-28 23:33:16 +04:00
|
|
|
* email part, so trim from both ends, possibly removing
|
|
|
|
* the () pair at the end.
|
|
|
|
*/
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_trim(&f);
|
2008-07-21 17:34:29 +04:00
|
|
|
if (f.buf[0] == '(' && f.len && f.buf[f.len - 1] == ')') {
|
|
|
|
strbuf_remove(&f, 0, 1);
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_setlen(&f, f.len - 1);
|
2008-07-21 17:34:29 +04:00
|
|
|
}
|
2005-04-12 10:46:50 +04:00
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
get_sane_name(&name, &f, &email);
|
|
|
|
strbuf_release(&f);
|
2005-04-12 10:46:50 +04:00
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static void handle_header(struct strbuf **out, const struct strbuf *line)
|
2005-05-02 08:42:53 +04:00
|
|
|
{
|
2008-07-13 22:30:12 +04:00
|
|
|
if (!*out) {
|
|
|
|
*out = xmalloc(sizeof(struct strbuf));
|
|
|
|
strbuf_init(*out, line->len);
|
|
|
|
} else
|
|
|
|
strbuf_reset(*out);
|
2007-03-12 22:52:04 +03:00
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_addbuf(*out, line);
|
2005-08-28 23:33:16 +04: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 22:30:12 +04:00
|
|
|
static int slurp_attr(const char *line, const char *name, struct strbuf *attr)
|
2005-08-28 23:33:16 +04:00
|
|
|
{
|
2006-06-28 13:04:39 +04:00
|
|
|
const char *ends, *ap = strcasestr(line, name);
|
2005-08-28 23:33:16 +04:00
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
if (!ap) {
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_setlen(attr, 0);
|
2005-08-28 23:33:16 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ap += strlen(name);
|
|
|
|
if (*ap == '"') {
|
|
|
|
ap++;
|
|
|
|
ends = "\"";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ends = "; \t";
|
|
|
|
sz = strcspn(ap, ends);
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_add(attr, ap, sz);
|
2005-08-28 23:33:16 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static struct strbuf *content[MAX_BOUNDARIES];
|
2007-03-12 22:52:04 +03:00
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static struct strbuf **content_top = content;
|
2007-03-12 22:52:04 +03:00
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static void handle_content_type(struct strbuf *line)
|
2005-08-28 23:33:16 +04:00
|
|
|
{
|
2008-07-13 22:30:12 +04:00
|
|
|
struct strbuf *boundary = xmalloc(sizeof(struct strbuf));
|
|
|
|
strbuf_init(boundary, line->len);
|
2007-03-12 22:52:04 +03:00
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
if (!strcasestr(line->buf, "text/"))
|
2007-03-12 22:52:04 +03:00
|
|
|
message_type = TYPE_OTHER;
|
2008-07-13 22:30:12 +04:00
|
|
|
if (slurp_attr(line->buf, "boundary=", boundary)) {
|
|
|
|
strbuf_insert(boundary, 0, "--", 2);
|
2008-08-14 19:35:42 +04:00
|
|
|
if (++content_top > &content[MAX_BOUNDARIES]) {
|
2007-03-12 22:52:04 +03:00
|
|
|
fprintf(stderr, "Too many boundaries to handle\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-07-13 22:30:12 +04:00
|
|
|
*content_top = boundary;
|
|
|
|
boundary = NULL;
|
2005-09-07 03:46:34 +04:00
|
|
|
}
|
2008-07-13 22:30:12 +04:00
|
|
|
if (slurp_attr(line->buf, "charset=", &charset))
|
|
|
|
strbuf_tolower(&charset);
|
|
|
|
|
|
|
|
if (boundary) {
|
|
|
|
strbuf_release(boundary);
|
|
|
|
free(boundary);
|
2005-08-28 23:33:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static void handle_content_transfer_encoding(const struct strbuf *line)
|
2005-08-28 23:33:16 +04:00
|
|
|
{
|
2008-07-13 22:30:12 +04:00
|
|
|
if (strcasestr(line->buf, "base64"))
|
2005-08-28 23:33:16 +04:00
|
|
|
transfer_encoding = TE_BASE64;
|
2008-07-13 22:30:12 +04:00
|
|
|
else if (strcasestr(line->buf, "quoted-printable"))
|
2005-08-28 23:33:16 +04:00
|
|
|
transfer_encoding = TE_QP;
|
|
|
|
else
|
|
|
|
transfer_encoding = TE_DONTCARE;
|
2005-04-12 10:46:50 +04:00
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static int is_multipart_boundary(const struct strbuf *line)
|
2005-08-28 23:33:16 +04:00
|
|
|
{
|
2008-08-09 12:17:24 +04:00
|
|
|
return (((*content_top)->len <= line->len) &&
|
|
|
|
!memcmp(line->buf, (*content_top)->buf, (*content_top)->len));
|
2005-08-28 23:33:16 +04:00
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static void cleanup_subject(struct strbuf *subject)
|
2005-04-12 10:46:50 +04:00
|
|
|
{
|
2008-07-13 22:30:12 +04:00
|
|
|
char *pos;
|
|
|
|
size_t remove;
|
|
|
|
while (subject->len) {
|
|
|
|
switch (*subject->buf) {
|
2005-04-12 10:46:50 +04:00
|
|
|
case 'r': case 'R':
|
2008-07-13 22:30:12 +04:00
|
|
|
if (subject->len <= 3)
|
|
|
|
break;
|
|
|
|
if (!memcmp(subject->buf + 1, "e:", 2)) {
|
|
|
|
strbuf_remove(subject, 0, 3);
|
2005-04-12 10:46:50 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ' ': case '\t': case ':':
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_remove(subject, 0, 1);
|
2005-04-12 10:46:50 +04:00
|
|
|
continue;
|
|
|
|
case '[':
|
2008-07-13 22:30:12 +04:00
|
|
|
if ((pos = strchr(subject->buf, ']'))) {
|
2008-07-17 09:42:04 +04:00
|
|
|
remove = pos - subject->buf;
|
|
|
|
if (remove <= (subject->len - remove) * 2) {
|
|
|
|
strbuf_remove(subject, 0, remove + 1);
|
2008-07-13 22:30:12 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
strbuf_remove(subject, 0, 1);
|
2005-04-12 10:46:50 +04:00
|
|
|
break;
|
|
|
|
}
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_trim(subject);
|
|
|
|
return;
|
2005-04-12 10:46:50 +04:00
|
|
|
}
|
2006-06-14 00:21:50 +04:00
|
|
|
}
|
2005-04-12 10:46:50 +04:00
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static void cleanup_space(struct strbuf *sb)
|
2005-04-12 10:46:50 +04:00
|
|
|
{
|
2008-07-13 22:30:12 +04: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 10:46:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static void decode_header(struct strbuf *line);
|
2007-10-21 08:12:12 +04:00
|
|
|
static const char *header[MAX_HDR_PARSED] = {
|
2007-03-12 22:52:04 +03:00
|
|
|
"From","Subject","Date",
|
2005-08-28 23:33:16 +04:00
|
|
|
};
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static inline int cmp_header(const struct strbuf *line, const char *hdr)
|
2005-08-28 23:33:16 +04:00
|
|
|
{
|
2008-07-13 22:30:12 +04: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 23:33:16 +04:00
|
|
|
|
2008-07-13 22:30:12 +04: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 22:52:04 +03:00
|
|
|
/* search for the interesting parts */
|
|
|
|
for (i = 0; header[i]; i++) {
|
|
|
|
int len = strlen(header[i]);
|
2008-07-13 22:30:12 +04:00
|
|
|
if ((!hdr_data[i] || overwrite) && cmp_header(line, header[i])) {
|
2006-05-23 23:45:37 +04:00
|
|
|
/* Unwrap inline B and Q encoding, and optionally
|
|
|
|
* normalize the meta information to utf8.
|
|
|
|
*/
|
2008-07-13 22:30:12 +04: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 23:33:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-12 22:52:04 +03:00
|
|
|
/* Content stuff */
|
2008-07-13 22:30:12 +04: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 22:52:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* for inbody stuff */
|
2008-07-13 22:30:12 +04:00
|
|
|
if (!prefixcmp(line->buf, ">From") && isspace(line->buf[5])) {
|
|
|
|
ret = 1; /* Should this return 0? */
|
|
|
|
goto check_header_out;
|
|
|
|
}
|
|
|
|
if (!prefixcmp(line->buf, "[PATCH]") && isspace(line->buf[7])) {
|
2007-03-12 22:52:04 +03:00
|
|
|
for (i = 0; header[i]; i++) {
|
2008-07-11 01:41:33 +04:00
|
|
|
if (!memcmp("Subject", header[i], 7)) {
|
2008-07-13 22:30:12 +04:00
|
|
|
handle_header(&hdr_data[i], line);
|
|
|
|
ret = 1;
|
|
|
|
goto check_header_out;
|
2007-03-12 22:52:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
check_header_out:
|
|
|
|
strbuf_release(&sb);
|
|
|
|
return ret;
|
2005-08-28 23:33:16 +04:00
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static int is_rfc2822_header(const struct strbuf *line)
|
2006-05-26 11:46:58 +04: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 22:30:12 +04:00
|
|
|
char *cp = line->buf;
|
2007-02-26 22:10:59 +03:00
|
|
|
|
|
|
|
/* Count mbox From headers as headers */
|
2008-07-13 22:30:12 +04:00
|
|
|
if (!prefixcmp(cp, "From ") || !prefixcmp(cp, ">From "))
|
2007-02-26 22:10:59 +03:00
|
|
|
return 1;
|
|
|
|
|
2006-05-26 11:46:58 +04:00
|
|
|
while ((ch = *cp++)) {
|
|
|
|
if (ch == ':')
|
2008-07-13 22:30:12 +04:00
|
|
|
return 1;
|
2006-05-26 11:46:58 +04:00
|
|
|
if ((33 <= ch && ch <= 57) ||
|
|
|
|
(59 <= ch && ch <= 126))
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static int read_one_header_line(struct strbuf *line, FILE *in)
|
2005-08-28 23:33:16 +04:00
|
|
|
{
|
2007-02-26 22:10:59 +03:00
|
|
|
/* Get the first part of the line. */
|
2008-07-13 22:30:12 +04:00
|
|
|
if (strbuf_getline(line, in, '\n'))
|
2007-02-26 22:10:59 +03: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 22:30:12 +04:00
|
|
|
strbuf_rtrim(line);
|
|
|
|
if (!line->len || !is_rfc2822_header(line)) {
|
2007-02-26 22:10:59 +03:00
|
|
|
/* Re-add the newline */
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_addch(line, '\n');
|
2007-02-26 22:10:59 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now we need to eat all the continuation lines..
|
|
|
|
* Yuck, 2822 header "folding"
|
|
|
|
*/
|
|
|
|
for (;;) {
|
2008-07-13 22:30:12 +04:00
|
|
|
int peek;
|
|
|
|
struct strbuf continuation = STRBUF_INIT;
|
2007-02-26 22:10:59 +03:00
|
|
|
|
2006-05-23 23:53:20 +04:00
|
|
|
peek = fgetc(in); ungetc(peek, in);
|
|
|
|
if (peek != ' ' && peek != '\t')
|
|
|
|
break;
|
2008-07-13 22:30:12 +04:00
|
|
|
if (strbuf_getline(&continuation, in, '\n'))
|
2007-02-26 22:10:59 +03:00
|
|
|
break;
|
2008-07-13 22:30:12 +04:00
|
|
|
continuation.buf[0] = '\n';
|
|
|
|
strbuf_rtrim(&continuation);
|
|
|
|
strbuf_addbuf(line, &continuation);
|
2005-08-28 23:33:16 +04:00
|
|
|
}
|
2007-02-26 22:10:59 +03:00
|
|
|
|
|
|
|
return 1;
|
2005-08-28 23:33:16 +04:00
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047)
|
2005-08-28 23:33:16 +04:00
|
|
|
{
|
2008-07-13 22:30:12 +04:00
|
|
|
const char *in = q_seg->buf;
|
2005-08-28 23:33:16 +04:00
|
|
|
int c;
|
2008-07-13 22:30:12 +04:00
|
|
|
struct strbuf *out = xmalloc(sizeof(struct strbuf));
|
|
|
|
strbuf_init(out, q_seg->len);
|
|
|
|
|
|
|
|
while ((c = *in++) != 0) {
|
2005-08-28 23:33:16 +04:00
|
|
|
if (c == '=') {
|
|
|
|
int d = *in++;
|
|
|
|
if (d == '\n' || !d)
|
|
|
|
break; /* drop trailing newline */
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_addch(out, (hexval(d) << 4) | hexval(*in++));
|
2006-04-21 11:06:58 +04:00
|
|
|
continue;
|
2005-08-28 23:33:16 +04:00
|
|
|
}
|
2006-04-21 11:06:58 +04:00
|
|
|
if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
|
|
|
|
c = 0x20;
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_addch(out, c);
|
2005-08-28 23:33:16 +04:00
|
|
|
}
|
2008-07-13 22:30:12 +04:00
|
|
|
return out;
|
2005-08-28 23:33:16 +04:00
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static struct strbuf *decode_b_segment(const struct strbuf *b_seg)
|
2005-08-28 23:33:16 +04:00
|
|
|
{
|
|
|
|
/* Decode in..ep, possibly in-place to ot */
|
|
|
|
int c, pos = 0, acc = 0;
|
2008-07-13 22:30:12 +04:00
|
|
|
const char *in = b_seg->buf;
|
|
|
|
struct strbuf *out = xmalloc(sizeof(struct strbuf));
|
|
|
|
strbuf_init(out, b_seg->len);
|
2005-08-28 23:33:16 +04:00
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
while ((c = *in++) != 0) {
|
2005-08-28 23:33:16 +04: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 if (c == '=') {
|
|
|
|
/* padding is almost like (c == 0), except we do
|
|
|
|
* not output NUL resulting only from it;
|
|
|
|
* for now we just trust the data.
|
|
|
|
*/
|
|
|
|
c = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
continue; /* garbage */
|
|
|
|
switch (pos++) {
|
|
|
|
case 0:
|
|
|
|
acc = (c << 2);
|
|
|
|
break;
|
|
|
|
case 1:
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_addch(out, (acc | (c >> 4)));
|
2005-08-28 23:33:16 +04:00
|
|
|
acc = (c & 15) << 4;
|
|
|
|
break;
|
|
|
|
case 2:
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_addch(out, (acc | (c >> 2)));
|
2005-08-28 23:33:16 +04:00
|
|
|
acc = (c & 3) << 6;
|
|
|
|
break;
|
|
|
|
case 3:
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_addch(out, (acc | c));
|
2005-08-28 23:33:16 +04:00
|
|
|
acc = pos = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-07-13 22:30:12 +04:00
|
|
|
return out;
|
2005-08-28 23:33:16 +04: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 21:34:44 +04:00
|
|
|
/*
|
|
|
|
* When there is no known charset, guess.
|
|
|
|
*
|
|
|
|
* Right now we assume that if the target is UTF-8 (the default),
|
|
|
|
* and it already looks like UTF-8 (which includes US-ASCII as its
|
|
|
|
* subset, of course) then that is what it is and there is nothing
|
|
|
|
* to do.
|
|
|
|
*
|
|
|
|
* Otherwise, we default to assuming it is Latin1 for historical
|
|
|
|
* reasons.
|
|
|
|
*/
|
2008-07-13 22:30:12 +04:00
|
|
|
static const char *guess_charset(const struct strbuf *line, const char *target_charset)
|
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 21:34:44 +04:00
|
|
|
{
|
|
|
|
if (is_encoding_utf8(target_charset)) {
|
2008-07-13 22:30:12 +04:00
|
|
|
if (is_utf8(line->buf))
|
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 21:34:44 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return "latin1";
|
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static void convert_to_utf8(struct strbuf *line, const char *charset)
|
2005-08-28 23:33:16 +04: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 21:34:44 +04:00
|
|
|
char *out;
|
|
|
|
|
|
|
|
if (!charset || !*charset) {
|
|
|
|
charset = guess_charset(line, metainfo_charset);
|
|
|
|
if (!charset)
|
|
|
|
return;
|
|
|
|
}
|
2006-12-24 10:36:55 +03:00
|
|
|
|
2007-07-24 04:03:26 +04:00
|
|
|
if (!strcmp(metainfo_charset, charset))
|
|
|
|
return;
|
2008-07-13 22:30:12 +04:00
|
|
|
out = reencode_string(line->buf, metainfo_charset, charset);
|
2007-01-10 08:31:36 +03:00
|
|
|
if (!out)
|
|
|
|
die("cannot convert from %s to %s\n",
|
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 21:34:44 +04:00
|
|
|
charset, metainfo_charset);
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_attach(line, out, strlen(out), strlen(out));
|
2005-08-28 23:33:16 +04:00
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static int decode_header_bq(struct strbuf *it)
|
2005-08-28 23:33:16 +04:00
|
|
|
{
|
2008-07-13 22:30:12 +04:00
|
|
|
char *in, *ep, *cp;
|
|
|
|
struct strbuf outbuf = STRBUF_INIT, *dec;
|
|
|
|
struct strbuf charset_q = STRBUF_INIT, piecebuf = STRBUF_INIT;
|
2006-07-06 01:17:49 +04:00
|
|
|
int rfc2047 = 0;
|
2005-08-28 23:33:16 +04:00
|
|
|
|
2008-07-13 22:30:12 +04: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-06 01:17:49 +04:00
|
|
|
rfc2047 = 1;
|
|
|
|
|
2005-08-28 23:33:16 +04:00
|
|
|
if (in != ep) {
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_add(&outbuf, in, ep - in);
|
|
|
|
in = ep;
|
2005-08-28 23:33:16 +04:00
|
|
|
}
|
|
|
|
/* E.g.
|
|
|
|
* ep : "=?iso-2022-jp?B?GyR...?= foo"
|
|
|
|
* ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
|
|
|
|
*/
|
|
|
|
ep += 2;
|
2008-07-13 22:30:12 +04: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);
|
|
|
|
strbuf_tolower(&charset_q);
|
|
|
|
|
2005-08-28 23:33:16 +04:00
|
|
|
encoding = cp[1];
|
|
|
|
if (!encoding || cp[2] != '?')
|
2008-07-13 22:30:12 +04:00
|
|
|
goto decode_header_bq_out;
|
2005-08-28 23:33:16 +04:00
|
|
|
ep = strstr(cp + 3, "?=");
|
|
|
|
if (!ep)
|
2008-07-13 22:30:12 +04:00
|
|
|
goto decode_header_bq_out;
|
|
|
|
strbuf_add(&piecebuf, cp + 3, ep - cp - 3);
|
2005-08-28 23:33:16 +04:00
|
|
|
switch (tolower(encoding)) {
|
|
|
|
default:
|
2008-07-13 22:30:12 +04:00
|
|
|
goto decode_header_bq_out;
|
2005-08-28 23:33:16 +04:00
|
|
|
case 'b':
|
2008-07-13 22:30:12 +04:00
|
|
|
dec = decode_b_segment(&piecebuf);
|
2005-08-28 23:33:16 +04:00
|
|
|
break;
|
|
|
|
case 'q':
|
2008-07-13 22:30:12 +04:00
|
|
|
dec = decode_q_segment(&piecebuf, 1);
|
2005-08-28 23:33:16 +04:00
|
|
|
break;
|
|
|
|
}
|
2005-11-28 03:22:16 +03:00
|
|
|
if (metainfo_charset)
|
2008-07-13 22:30:12 +04:00
|
|
|
convert_to_utf8(dec, charset_q.buf);
|
2007-08-31 01:48:24 +04:00
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_addbuf(&outbuf, dec);
|
|
|
|
strbuf_release(dec);
|
|
|
|
free(dec);
|
2005-08-28 23:33:16 +04:00
|
|
|
in = ep + 2;
|
|
|
|
}
|
2008-07-13 22:30:12 +04: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-06 01:17:49 +04:00
|
|
|
return rfc2047;
|
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static void decode_header(struct strbuf *it)
|
2006-07-06 01:17:49 +04:00
|
|
|
{
|
2008-07-13 22:30:12 +04:00
|
|
|
if (decode_header_bq(it))
|
2006-07-06 01:17:49 +04: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 22:30:12 +04:00
|
|
|
convert_to_utf8(it, "");
|
2005-08-28 23:33:16 +04:00
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static void decode_transfer_encoding(struct strbuf *line)
|
2005-08-28 23:33:16 +04:00
|
|
|
{
|
2008-07-13 22:30:12 +04:00
|
|
|
struct strbuf *ret;
|
2005-08-28 23:33:16 +04:00
|
|
|
|
|
|
|
switch (transfer_encoding) {
|
|
|
|
case TE_QP:
|
2008-07-13 22:30:12 +04:00
|
|
|
ret = decode_q_segment(line, 0);
|
|
|
|
break;
|
2005-08-28 23:33:16 +04:00
|
|
|
case TE_BASE64:
|
2008-07-13 22:30:12 +04:00
|
|
|
ret = decode_b_segment(line);
|
|
|
|
break;
|
2005-08-28 23:33:16 +04:00
|
|
|
case TE_DONTCARE:
|
2008-05-25 12:16:05 +04:00
|
|
|
default:
|
2008-07-13 22:30:12 +04:00
|
|
|
return;
|
2005-08-28 23:33:16 +04:00
|
|
|
}
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_reset(line);
|
|
|
|
strbuf_addbuf(line, ret);
|
|
|
|
strbuf_release(ret);
|
|
|
|
free(ret);
|
2005-08-28 23:33:16 +04:00
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static void handle_filter(struct strbuf *line);
|
2007-03-12 22:52:04 +03:00
|
|
|
|
|
|
|
static int find_boundary(void)
|
2005-04-12 10:46:50 +04:00
|
|
|
{
|
2008-07-13 22:30:12 +04:00
|
|
|
while (!strbuf_getline(&line, fin, '\n')) {
|
2008-08-14 19:35:42 +04:00
|
|
|
if (*content_top && is_multipart_boundary(&line))
|
2007-03-12 22:52:04 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int handle_boundary(void)
|
|
|
|
{
|
2008-07-13 22:30:12 +04:00
|
|
|
struct strbuf newline = STRBUF_INIT;
|
|
|
|
|
|
|
|
strbuf_addch(&newline, '\n');
|
2007-03-12 22:52:04 +03:00
|
|
|
again:
|
2008-07-13 22:30:12 +04:00
|
|
|
if (line.len >= (*content_top)->len + 2 &&
|
|
|
|
!memcmp(line.buf + (*content_top)->len, "--", 2)) {
|
2007-03-12 22:52:04 +03:00
|
|
|
/* we hit an end boundary */
|
|
|
|
/* pop the current boundary off the stack */
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_release(*content_top);
|
|
|
|
free(*content_top);
|
|
|
|
*content_top = NULL;
|
2007-03-12 22:52:04 +03:00
|
|
|
|
|
|
|
/* technically won't happen as is_multipart_boundary()
|
|
|
|
will fail first. But just in case..
|
|
|
|
*/
|
2008-08-14 19:35:42 +04:00
|
|
|
if (--content_top < content) {
|
2007-03-12 22:52:04 +03:00
|
|
|
fprintf(stderr, "Detected mismatched boundaries, "
|
|
|
|
"can't recover\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-07-13 22:30:12 +04:00
|
|
|
handle_filter(&newline);
|
|
|
|
strbuf_release(&newline);
|
2007-03-12 22:52:04 +03:00
|
|
|
|
|
|
|
/* skip to the next boundary */
|
|
|
|
if (!find_boundary())
|
|
|
|
return 0;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set some defaults */
|
|
|
|
transfer_encoding = TE_DONTCARE;
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_reset(&charset);
|
2007-03-12 22:52:04 +03:00
|
|
|
message_type = TYPE_TEXT;
|
2005-08-28 23:33:16 +04:00
|
|
|
|
2007-03-12 22:52:04 +03:00
|
|
|
/* slurp in this section's info */
|
2008-07-13 22:30:12 +04:00
|
|
|
while (read_one_header_line(&line, fin))
|
|
|
|
check_header(&line, p_hdr_data, 0);
|
2005-04-12 10:46:50 +04:00
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_release(&newline);
|
2008-08-09 12:17:24 +04:00
|
|
|
/* replenish line */
|
|
|
|
if (strbuf_getline(&line, fin, '\n'))
|
|
|
|
return 0;
|
|
|
|
strbuf_addch(&line, '\n');
|
|
|
|
return 1;
|
2005-08-28 23:33:16 +04:00
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static inline int patchbreak(const struct strbuf *line)
|
2007-03-12 22:52:06 +03:00
|
|
|
{
|
2008-07-13 22:30:12 +04:00
|
|
|
size_t i;
|
|
|
|
|
2007-03-12 22:52:06 +03:00
|
|
|
/* Beginning of a "diff -" header? */
|
2008-07-13 22:30:12 +04:00
|
|
|
if (!prefixcmp(line->buf, "diff -"))
|
2007-03-12 22:52:06 +03:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* CVS "Index: " line? */
|
2008-07-13 22:30:12 +04:00
|
|
|
if (!prefixcmp(line->buf, "Index: "))
|
2007-03-12 22:52:06 +03:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "--- <filename>" starts patches without headers
|
|
|
|
* "---<sp>*" is a manual separator
|
|
|
|
*/
|
2008-07-13 22:30:12 +04:00
|
|
|
if (line->len < 4)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!prefixcmp(line->buf, "---")) {
|
2007-03-12 22:52:06 +03:00
|
|
|
/* space followed by a filename? */
|
2008-07-13 22:30:12 +04:00
|
|
|
if (line->buf[3] == ' ' && !isspace(line->buf[4]))
|
2007-03-12 22:52:06 +03:00
|
|
|
return 1;
|
|
|
|
/* Just whitespace? */
|
2008-07-13 22:30:12 +04:00
|
|
|
for (i = 3; i < line->len; i++) {
|
|
|
|
unsigned char c = line->buf[i];
|
2007-03-12 22:52:06 +03:00
|
|
|
if (c == '\n')
|
|
|
|
return 1;
|
|
|
|
if (!isspace(c))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static int handle_commit_msg(struct strbuf *line)
|
2005-08-28 23:33:16 +04:00
|
|
|
{
|
2007-03-12 22:52:04 +03:00
|
|
|
static int still_looking = 1;
|
|
|
|
|
2005-08-28 23:33:16 +04:00
|
|
|
if (!cmitmsg)
|
|
|
|
return 0;
|
2005-04-12 10:46:50 +04:00
|
|
|
|
2007-03-12 22:52:04 +03:00
|
|
|
if (still_looking) {
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_ltrim(line);
|
|
|
|
if (!line->len)
|
|
|
|
return 0;
|
|
|
|
if ((still_looking = check_header(line, s_hdr_data, 0)) != 0)
|
2007-03-12 22:52:04 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2006-05-23 23:47:28 +04:00
|
|
|
|
2007-03-30 20:18:45 +04:00
|
|
|
/* normalize the log message to UTF-8. */
|
|
|
|
if (metainfo_charset)
|
2008-07-13 22:30:12 +04:00
|
|
|
convert_to_utf8(line, charset.buf);
|
2007-03-30 20:18:45 +04:00
|
|
|
|
2007-03-12 22:52:06 +03:00
|
|
|
if (patchbreak(line)) {
|
2007-03-12 22:52:04 +03:00
|
|
|
fclose(cmitmsg);
|
|
|
|
cmitmsg = NULL;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-05-23 23:47:28 +04:00
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
fputs(line->buf, cmitmsg);
|
2005-08-28 23:33:16 +04:00
|
|
|
return 0;
|
2005-04-12 10:46:50 +04:00
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static void handle_patch(const struct strbuf *line)
|
2005-04-12 10:46:50 +04:00
|
|
|
{
|
2008-07-13 22:30:12 +04:00
|
|
|
fwrite(line->buf, 1, line->len, patchfile);
|
2007-03-12 22:52:04 +03:00
|
|
|
patch_lines++;
|
2005-04-12 10:46:50 +04:00
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static void handle_filter(struct strbuf *line)
|
2005-04-12 10:46:50 +04:00
|
|
|
{
|
2007-03-12 22:52:04 +03:00
|
|
|
static int filter = 0;
|
2005-04-12 10:46:50 +04:00
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
/* filter tells us which part we left off on */
|
2007-03-12 22:52:04 +03:00
|
|
|
switch (filter) {
|
|
|
|
case 0:
|
2008-07-13 22:30:12 +04:00
|
|
|
if (!handle_commit_msg(line))
|
2005-08-28 23:33:16 +04:00
|
|
|
break;
|
2007-03-12 22:52:04 +03:00
|
|
|
filter++;
|
|
|
|
case 1:
|
2008-07-13 22:30:12 +04:00
|
|
|
handle_patch(line);
|
|
|
|
break;
|
2005-04-12 10:46:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-12 22:52:04 +03:00
|
|
|
static void handle_body(void)
|
2005-07-23 13:10:31 +04:00
|
|
|
{
|
2008-07-13 22:30:12 +04:00
|
|
|
int len = 0;
|
|
|
|
struct strbuf prev = STRBUF_INIT;
|
2005-08-28 23:33:16 +04:00
|
|
|
|
|
|
|
/* Skip up to the first boundary */
|
2008-07-13 22:30:12 +04:00
|
|
|
if (*content_top) {
|
2007-03-12 22:52:04 +03:00
|
|
|
if (!find_boundary())
|
2008-07-13 22:30:12 +04:00
|
|
|
goto handle_body_out;
|
2007-03-12 22:52:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_setlen(&line, line.len + len);
|
|
|
|
|
2007-03-12 22:52:04 +03:00
|
|
|
/* process any boundary lines */
|
2008-07-13 22:30:12 +04:00
|
|
|
if (*content_top && is_multipart_boundary(&line)) {
|
2007-03-12 22:52:04 +03:00
|
|
|
/* flush any leftover */
|
2008-08-09 12:17:24 +04:00
|
|
|
if (prev.len) {
|
|
|
|
handle_filter(&prev);
|
|
|
|
strbuf_reset(&prev);
|
|
|
|
}
|
2007-03-12 22:52:04 +03:00
|
|
|
if (!handle_boundary())
|
2008-07-13 22:30:12 +04:00
|
|
|
goto handle_body_out;
|
2007-03-12 22:52:04 +03:00
|
|
|
}
|
|
|
|
|
2007-03-30 20:18:45 +04:00
|
|
|
/* Unwrap transfer encoding */
|
2008-07-13 22:30:12 +04:00
|
|
|
decode_transfer_encoding(&line);
|
2007-03-12 22:52:04 +03:00
|
|
|
|
|
|
|
switch (transfer_encoding) {
|
|
|
|
case TE_BASE64:
|
2008-02-16 00:53:36 +03:00
|
|
|
case TE_QP:
|
2007-03-12 22:52:04 +03:00
|
|
|
{
|
2008-07-13 22:30:12 +04: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 22:52:04 +03:00
|
|
|
|
|
|
|
/* binary data most likely doesn't have newlines */
|
|
|
|
if (message_type != TYPE_TEXT) {
|
2008-07-13 22:30:12 +04:00
|
|
|
handle_filter(&line);
|
2007-03-12 22:52:04 +03:00
|
|
|
break;
|
|
|
|
}
|
2008-05-25 12:16:05 +04:00
|
|
|
/*
|
|
|
|
* This is a decoded line that may contain
|
2007-03-12 22:52:04 +03:00
|
|
|
* multiple new lines. Pass only one chunk
|
|
|
|
* at a time to handle_filter()
|
|
|
|
*/
|
2008-07-13 22:30:12 +04: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 12:16:05 +04:00
|
|
|
/*
|
2008-07-13 22:30:12 +04:00
|
|
|
* The partial chunk is saved in "prev" and will be
|
2008-05-25 12:16:05 +04:00
|
|
|
* appended by the next iteration of read_line_with_nul().
|
2007-03-12 22:52:04 +03:00
|
|
|
*/
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_list_free(lines);
|
2005-08-28 23:33:16 +04:00
|
|
|
break;
|
2005-07-23 13:10:31 +04:00
|
|
|
}
|
2007-03-12 22:52:04 +03:00
|
|
|
default:
|
2008-07-13 22:30:12 +04:00
|
|
|
handle_filter(&line);
|
2005-08-28 23:33:16 +04:00
|
|
|
}
|
2007-03-12 22:52:04 +03:00
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
strbuf_reset(&line);
|
|
|
|
if (strbuf_avail(&line) < 100)
|
|
|
|
strbuf_grow(&line, 100);
|
|
|
|
} while ((len = read_line_with_nul(line.buf, strbuf_avail(&line), fin)));
|
|
|
|
|
|
|
|
handle_body_out:
|
|
|
|
strbuf_release(&prev);
|
2005-07-23 13:10:31 +04:00
|
|
|
}
|
|
|
|
|
2008-07-13 22:30:12 +04:00
|
|
|
static void output_header_lines(FILE *fout, const char *hdr, const struct strbuf *data)
|
2007-07-29 04:57:25 +04:00
|
|
|
{
|
2008-07-13 22:30:12 +04:00
|
|
|
const char *sp = data->buf;
|
2007-07-29 04:57:25 +04:00
|
|
|
while (1) {
|
2008-07-13 22:30:12 +04:00
|
|
|
char *ep = strchr(sp, '\n');
|
2007-07-29 04:57:25 +04:00
|
|
|
int len;
|
|
|
|
if (!ep)
|
2008-07-13 22:30:12 +04:00
|
|
|
len = strlen(sp);
|
2007-07-29 04:57:25 +04:00
|
|
|
else
|
2008-07-13 22:30:12 +04:00
|
|
|
len = ep - sp;
|
|
|
|
fprintf(fout, "%s: %.*s\n", hdr, len, sp);
|
2007-07-29 04:57:25 +04:00
|
|
|
if (!ep)
|
|
|
|
break;
|
2008-07-13 22:30:12 +04:00
|
|
|
sp = ep + 1;
|
2007-07-29 04:57:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-12 22:52:04 +03:00
|
|
|
static void handle_info(void)
|
2005-04-12 10:46:50 +04:00
|
|
|
{
|
2008-07-13 22:30:12 +04:00
|
|
|
struct strbuf *hdr;
|
2007-03-12 22:52:04 +03: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;
|
|
|
|
|
|
|
|
if (!memcmp(header[i], "Subject", 7)) {
|
2008-07-13 22:30:12 +04:00
|
|
|
if (!keep_subject) {
|
|
|
|
cleanup_subject(hdr);
|
|
|
|
cleanup_space(hdr);
|
2007-07-29 04:57:25 +04:00
|
|
|
}
|
2008-07-13 22:30:12 +04:00
|
|
|
output_header_lines(fout, "Subject", hdr);
|
2007-03-12 22:52:04 +03:00
|
|
|
} else if (!memcmp(header[i], "From", 4)) {
|
|
|
|
handle_from(hdr);
|
2008-07-13 22:30:12 +04:00
|
|
|
fprintf(fout, "Author: %s\n", name.buf);
|
|
|
|
fprintf(fout, "Email: %s\n", email.buf);
|
2007-03-12 22:52:04 +03:00
|
|
|
} else {
|
|
|
|
cleanup_space(hdr);
|
2008-07-13 22:30:12 +04:00
|
|
|
fprintf(fout, "%s: %s\n", header[i], hdr->buf);
|
2007-03-12 22:52:04 +03:00
|
|
|
}
|
2005-08-28 23:33:16 +04:00
|
|
|
}
|
2007-03-12 22:52:04 +03:00
|
|
|
fprintf(fout, "\n");
|
2005-04-12 10:46:50 +04:00
|
|
|
}
|
|
|
|
|
2007-06-08 13:22:56 +04:00
|
|
|
static int mailinfo(FILE *in, FILE *out, int ks, const char *encoding,
|
|
|
|
const char *msg, const char *patch)
|
2006-06-14 00:21:50 +04:00
|
|
|
{
|
2007-11-02 01:57:45 +03:00
|
|
|
int peek;
|
2006-06-14 00:21:50 +04:00
|
|
|
keep_subject = ks;
|
|
|
|
metainfo_charset = encoding;
|
|
|
|
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 22:52:04 +03:00
|
|
|
|
2008-07-13 22:30:12 +04: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 22:52:04 +03:00
|
|
|
|
2007-11-02 01:57:45 +03:00
|
|
|
do {
|
|
|
|
peek = fgetc(in);
|
|
|
|
} while (isspace(peek));
|
|
|
|
ungetc(peek, in);
|
|
|
|
|
2007-03-12 22:52:04 +03:00
|
|
|
/* process the email header */
|
2008-07-13 22:30:12 +04:00
|
|
|
while (read_one_header_line(&line, fin))
|
|
|
|
check_header(&line, p_hdr_data, 1);
|
2007-03-12 22:52:04 +03:00
|
|
|
|
|
|
|
handle_body();
|
|
|
|
handle_info();
|
2006-06-14 00:21:50 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-17 09:18:27 +04:00
|
|
|
static const char mailinfo_usage[] =
|
2008-07-17 04:22:50 +04:00
|
|
|
"git mailinfo [-k] [-u | --encoding=<encoding> | -n] msg patch <mail >info";
|
2005-08-28 23:33:16 +04:00
|
|
|
|
2006-07-29 09:44:25 +04:00
|
|
|
int cmd_mailinfo(int argc, const char **argv, const char *prefix)
|
2005-04-12 10:46:50 +04:00
|
|
|
{
|
2007-01-10 08:31:36 +03:00
|
|
|
const char *def_charset;
|
|
|
|
|
2005-11-28 03:29:38 +03:00
|
|
|
/* NEEDSWORK: might want to do the optional .git/ directory
|
|
|
|
* discovery
|
|
|
|
*/
|
2008-05-14 21:46:53 +04:00
|
|
|
git_config(git_default_config, NULL);
|
2005-11-28 03:29:38 +03:00
|
|
|
|
2007-01-10 08:31:36 +03:00
|
|
|
def_charset = (git_commit_encoding ? git_commit_encoding : "utf-8");
|
|
|
|
metainfo_charset = def_charset;
|
|
|
|
|
2005-08-17 09:18:27 +04:00
|
|
|
while (1 < argc && argv[1][0] == '-') {
|
|
|
|
if (!strcmp(argv[1], "-k"))
|
|
|
|
keep_subject = 1;
|
2005-08-28 23:33:16 +04:00
|
|
|
else if (!strcmp(argv[1], "-u"))
|
2007-01-10 08:31:36 +03:00
|
|
|
metainfo_charset = def_charset;
|
|
|
|
else if (!strcmp(argv[1], "-n"))
|
|
|
|
metainfo_charset = NULL;
|
Mechanical conversion to use prefixcmp()
This mechanically converts strncmp() to use prefixcmp(), but only when
the parameters match specific patterns, so that they can be verified
easily. Leftover from this will be fixed in a separate step, including
idiotic conversions like
if (!strncmp("foo", arg, 3))
=>
if (!(-prefixcmp(arg, "foo")))
This was done by using this script in px.perl
#!/usr/bin/perl -i.bak -p
if (/strncmp\(([^,]+), "([^\\"]*)", (\d+)\)/ && (length($2) == $3)) {
s|strncmp\(([^,]+), "([^\\"]*)", (\d+)\)|prefixcmp($1, "$2")|;
}
if (/strncmp\("([^\\"]*)", ([^,]+), (\d+)\)/ && (length($1) == $3)) {
s|strncmp\("([^\\"]*)", ([^,]+), (\d+)\)|(-prefixcmp($2, "$1"))|;
}
and running:
$ git grep -l strncmp -- '*.c' | xargs perl px.perl
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-02-20 12:53:29 +03:00
|
|
|
else if (!prefixcmp(argv[1], "--encoding="))
|
2005-11-28 12:29:52 +03:00
|
|
|
metainfo_charset = argv[1] + 11;
|
2005-08-28 23:33:16 +04:00
|
|
|
else
|
2005-11-28 03:29:38 +03:00
|
|
|
usage(mailinfo_usage);
|
2005-08-17 09:18:27 +04:00
|
|
|
argc--; argv++;
|
|
|
|
}
|
|
|
|
|
2005-06-23 20:40:23 +04:00
|
|
|
if (argc != 3)
|
2005-11-28 03:29:38 +03:00
|
|
|
usage(mailinfo_usage);
|
2006-06-14 00:21:50 +04:00
|
|
|
|
|
|
|
return !!mailinfo(stdin, stdout, keep_subject, metainfo_charset, argv[1], argv[2]);
|
2005-04-12 10:46:50 +04:00
|
|
|
}
|