{"thread":{"id":"1319","subject":"[PATCH] mailinfo: handle folded header.","startedAt":"2005-07-23T09:10:31Z","lastAt":"2005-07-23T09:10:31Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"6375","messageId":"7vfyu5horr.fsf@totally-fudged-out-message-id","threadId":"1319","inReplyTo":null,"subject":"[PATCH] mailinfo: handle folded header.","fromName":"Junio C Hamano","fromEmail":"junio@twinsun.com","sentAt":"2005-07-23T09:10:31Z","receivedAt":"2005-07-23T09:10:31Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Some people split their long E-mail address over two lines\nusing the RFC2822 header \"folding\".  We can lose authorship\ninformation this way, so make a minimum effort to deal with it,\ninstead of special casing only the \"Subject:\" field.\n\nWe could teach mailsplit to unfold the folded header, but\nteaching mailinfo about folding would make more sense; a single\nmessage can be fed to mailinfo without going through mailsplit.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n\n  This was done primarily to help Yoshifuji-san ;-) but I am not\n  sure if it is worth this half-effort.  His address would be\n  parsed as:\n\n    Author: YOSHIFUJI Hideaki / =?iso-2022-jp?B?GyRCNUhGIzFRTEAbKEI\n    Email: yoshfuji@linux-ipv6.org\n\n  which, while it is certainly better than the emptiness we would\n  get without this patch, I suspect that it is not really what he\n  would want to see either.\n\n  We _might_ want to go all the way, decoding the mime encoded\n  strings and convert them to utf8 (not limited to the From: but\n  also the Subject: field).  I dunno.  I am submitting this\n  patch in this form because it is an improvement over the\n  current version without opening this can of worms.  I'll let\n  other people open it by submitting a separate patch on top of\n  this one if they want to.\n\n tools/mailinfo.c |   64 +++++++++++++++++++++++-------------------------------\n 1 files changed, 27 insertions(+), 37 deletions(-)\n\n25c8a3906c9f8645d7482abb2ef2de2b51155e45\ndiff --git a/tools/mailinfo.c b/tools/mailinfo.c\n--- a/tools/mailinfo.c\n+++ b/tools/mailinfo.c\n@@ -89,45 +89,14 @@ static void handle_subject(char *line)\n \tstrcpy(subject, line);\n }\n \n-static void add_subject_line(char *line)\n-{\n-\twhile (isspace(*line))\n-\t\tline++;\n-\t*--line = ' ';\n-\tstrcat(subject, line);\n-}\n-\n static void check_line(char *line, int len)\n {\n-\tstatic int cont = -1;\n-\tif (!memcmp(line, \"From:\", 5) && isspace(line[5])) {\n+\tif (!memcmp(line, \"From:\", 5) && isspace(line[5]))\n \t\thandle_from(line+6);\n-\t\tcont = 0;\n-\t\treturn;\n-\t}\n-\tif (!memcmp(line, \"Date:\", 5) && isspace(line[5])) {\n+\telse if (!memcmp(line, \"Date:\", 5) && isspace(line[5]))\n \t\thandle_date(line+6);\n-\t\tcont = 0;\n-\t\treturn;\n-\t}\n-\tif (!memcmp(line, \"Subject:\", 8) && isspace(line[8])) {\n+\telse if (!memcmp(line, \"Subject:\", 8) && isspace(line[8]))\n \t\thandle_subject(line+9);\n-\t\tcont = 1;\n-\t\treturn;\n-\t}\n-\tif (isspace(*line)) {\n-\t\tswitch (cont) {\n-\t\tcase 0:\n-\t\t\tfprintf(stderr, \"I don't do 'Date:' or 'From:' line continuations\\n\");\n-\t\t\tbreak;\n-\t\tcase 1:\n-\t\t\tadd_subject_line(line);\n-\t\t\treturn;\n-\t\tdefault:\n-\t\t\tbreak;\n-\t\t}\n-\t}\n-\tcont = -1;\n }\n \n static char * cleanup_subject(char *subject)\n@@ -246,9 +215,30 @@ static void handle_body(void)\n \t}\n }\n \n+static int read_one_header_line(char *line, int sz, FILE *in)\n+{\n+\tint ofs = 0;\n+\twhile (ofs < sz) {\n+\t\tint peek, len;\n+\t\tif (fgets(line + ofs, sz - ofs, in) == NULL)\n+\t\t\treturn ofs;\n+\t\tlen = eatspace(line + ofs);\n+\t\tif (len == 0)\n+\t\t\treturn ofs;\n+\t\tpeek = fgetc(in); ungetc(peek, in);\n+\t\tif (peek == ' ' || peek == '\\t') {\n+\t\t\t/* Yuck, 2822 header \"folding\" */\n+\t\t\tofs += len;\n+\t\t\tcontinue;\n+\t\t}\n+\t\treturn ofs + len;\n+\t}\n+\treturn ofs;\n+}\n+\n static void usage(void)\n {\n-\tfprintf(stderr, \"mailinfo msg-file path-file < email\\n\");\n+\tfprintf(stderr, \"mailinfo msg-file patch-file < email\\n\");\n \texit(1);\n }\n \n@@ -266,8 +256,8 @@ int main(int argc, char ** argv)\n \t\tperror(argv[2]);\n \t\texit(1);\n \t}\n-\twhile (fgets(line, sizeof(line), stdin) != NULL) {\n-\t\tint len = eatspace(line);\n+\twhile (1) {\n+\t\tint len = read_one_header_line(line, sizeof(line), stdin);\n \t\tif (!len) {\n \t\t\thandle_body();\n \t\t\tbreak;\n"}]}