mailinfo: support rfc3676 (format=flowed) text/plain messages
PATCH 1/2 mailinfo: support rfc3676 (format=flowed) text/plain messages
RFC 3676 establishes two parameters (Format and DelSP) to be used with the Text/Plain media type. In the presence of these parameters, trailing whitespace is used to indicate flowed lines and a canonical quote indicator is used to indicate quoted lines.
mailinfo now unfolds, unquotes, and un-space-stuffs such messages.
---
It's been a while since I hacked C, so mucho scrutiny appreciated. The mailinfo testsuite still passes, and this patch is followed by one which adds a new test for this code, which also passes.
This is based off next, but mailinfo hasn't changed in a while, so it should apply cleanly to master (didn't test that though).
builtin-mailinfo.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 40 insertions(+), 0 deletions(-)
builtin-mailinfo.c +40 -0
@@ -20,6 +20,13 @@ static enum {static enum {TYPE_TEXT ,TYPE_OTHER , } message_type; +/* RFC 3676 Text/Plain Format and DelSp Parameters */ +static enum { +FORMAT_NONE ,FORMAT_FIXED ,FORMAT_FLOWED , +} tp_format; +static enum { +DELSP_NONE ,DELSP_YES ,DELSP_NO , +} tp_delsp;static char charset[256 ];static int patch_lines;
@@ -193,6 +200,18 @@ static int handle_content_type(char *line)if (strcasestr (line,"text/" ) ==NULL ) message_type =TYPE_OTHER ; +else if (strcasestr (line,"text/plain" )) { +char attr[256 ]; +if (slurp_attr (line,"format=" , attr) && !strcasecmp (attr,"flowed" )) { + tp_format =FORMAT_FLOWED ; +if (slurp_attr (line,"delsp=" , attr) && !strcasecmp (attr,"yes" )) + tp_delsp =DELSP_YES ; +else + tp_delsp =DELSP_NO ; + } +else + tp_format =FORMAT_FIXED ; + }if (slurp_attr (line,"boundary=" , boundary +2 )) {memcpy (boundary,"--" ,2 );if (content_top++ >= &content[MAX_BOUNDARIES ]) {
@@ -681,6 +700,8 @@ again: transfer_encoding =TE_DONTCARE ; charset[0 ] =0 ; message_type =TYPE_TEXT ; + tp_format =FORMAT_NONE ; + tp_delsp =DELSP_NONE ;/* slurp in this section's info */ while (read_one_header_line (line,sizeof (line), fin))
@@ -770,6 +791,24 @@ static int handle_filter(char *line, unsigned linesize) {static int filter =0 ; +if (tp_format ==FORMAT_FLOWED && !!strcmp (line,"-- \n" )) { +char *cp = line; +while (*cp =='>' && *cp !=0 ) + cp++; +if (*cp ==' ' ) + cp++; + line = cp; +if (!!strcmp (line,"-- \n" )) { +while (*cp !='\n' && *cp !=0 ) + cp++; +if (cp > line && *cp =='\n' && *(cp-1 ) ==' ' ) { +if (tp_delsp ==DELSP_YES ) + *(cp-1 ) ='\0' ; +else + *cp ='\0' ; + } + } + }/* filter tells us which part we left off on * a non-zero return indicates we hit a filter point */
@@ -818,6 +857,7 @@ static void handle_body(void)switch (transfer_encoding) {case TE_BASE64 : +case TE_QP : {char *op = line;
Signature · 1 line
1.5.4.1.1281.g75df
PATCH 2/2 test mailinfo rfc3676 support
Adds a format=flowed message to the sample.mbox in order to test mailinfo's rfc3676 support.
--- t/t5100-mailinfo.sh | 2 +- t/t5100/info0009 | 5 ++ t/t5100/msg0009 | 8 +++ t/t5100/patch0009 | 98 ++++++++++++++++++++++++++++++++++++ t/t5100/sample.mbox | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 250 insertions(+), 1 deletions(-) create mode 100644 t/t5100/info0009 create mode 100644 t/t5100/msg0009 create mode 100644 t/t5100/patch0009
t/t5100-mailinfo.sh +1 -1
@@ -11,7 +11,7 @@ test_expect_success 'split sample box' \ 'git mailsplit -o. ../t5100/sample.mbox >last && last=`cat last` && echo total is $last && - test `cat last` = 8' + test `cat last` = 9' for mail in `echo 00*` do
t/t5100/info0009 new file +5 -0
@@ -0,0 +1,5 @@ +Author: A U Thor +Email: [email protected] +Subject: mailinfo: support rfc3676 (format=flowed) text/plain messages +Date: Thu, 14 Feb 2008 20:56:34 -0500 +
t/t5100/msg0009 new file +8 -0
@@ -0,0 +1,8 @@ +RFC 3676 establishes two parameters (Format and DelSP) to be used with +the Text/Plain media type. In the presence of these parameters, trailing +whitespace is used to indicate flowed lines and a canonical quote +indicator is used to indicate quoted lines. + +mailinfo now unfolds, unquotes, and un-space-stuffs such messages. + +Signed-off-by: A U Thor <[email protected]
t/t5100/patch0009 new file +98 -0
98 lines not on this page · raw message
t/t5100/sample.mbox +138 -0
141 lines not on this page · raw message
Signature · 1 line
1.5.4.1.1281.g75df
Re: [PATCH 2/2] test mailinfo rfc3676 support
Hi,
Quoted 4 lines · Jay Soffian
+@@ -20,6 +20,13 @@ static enum {
t/t5100/patch0009 new file +3
-0+static enum { + TYPE_TEXT, TYPE_OTHER, +} message_type;
t/t5100/patch0009 line 35
Quoted 10 lines
t/t5100/patch0009 new file +10
-0++/* RFC 3676 Text/Plain Format and DelSp Parameters */ ++static enum { ++ FORMAT_NONE, FORMAT_FIXED, FORMAT_FLOWED, ++} tp_format; ++static enum { ++ DELSP_NONE, DELSP_YES, DELSP_NO, ++} tp_delsp; + +static char charset[256]; +static int patch_lines;
Hmm. Such a corrupt patch (lacking spaces at the beginning of the line) would not be accepted by git-apply. I briefly thought about teaching git-apply to grok that, with a flag. But now I think that mailsplit should handle that, no?
Question is: can you "de-corruptify" such a patch? (Note: it would probably need a validating step, too, i.e. count the lines it added a space to, and match that up with the numbers in the @@ lines)
Ciao,
Dscho
Re: [PATCH 2/2] test mailinfo rfc3676 support
Quoted 8 lines · Johannes Schindelin
Hmm. Such a corrupt patch (lacking spaces at the beginning of the line) would not be accepted by git-apply. I briefly thought about teaching git-apply to grok that, with a flag. But now I think that mailsplit should handle that, no?
Question is: can you "de-corruptify" such a patch? (Note: it would probably need a validating step, too, i.e. count the lines it added a space to, and match that up with the numbers in the @@ lines)
Heh, I see this was a confusing patch. It is a lot clearer to read if you apply it and then take a look at what's added to sample.mbox, as well as look at the new files in t5100.
What I did to create this test was add the format=flowed code to mailinfo (my previous patch email), extract it with format-patch, then cut/paste it into my MUA and mail it to myself so I could have a sample format=flowed message. I then added that to the sample.mbox to see if the code I'd just added to mailinfo was working properly. Yes, it "de-corrupted" it just fine by removing the flowing.
j.
PATCH 1/3 mailinfo: ensure handle_filter gets only one line at a time
In reply to Jay Soffianhandle_filter() is only supposed to get one line at a time. handle_body() already ensures this after decoding BASE64 messages per this comment:
/* this is a decoded line that may contain
* multiple new lines. Pass only one chunk
* at a time to handle_filter()
*/
QP messages need to be similarly processed since decoding QP can also add newlines.
---
I broke this change out into it's own patch since it's independent of the format=flowed additions.
builtin-mailinfo.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
builtin-mailinfo.c +1 -0
@@ -818,6 +818,7 @@ static void handle_body(void)switch (transfer_encoding) {case TE_BASE64 : +case TE_QP : {char *op = line;
Signature · 1 line
1.5.4.1.1281.g75df
PATCH 2/3 mailinfo: support rfc3676 (format=flowed) text/plain messages
RFC 3676 establishes two parameters (Format and DelSP) to be used with the Text/Plain media type. In the presence of these parameters, trailing whitespace is used to indicate flowed lines and a canonical quote indicator is used to indicate quoted lines.
mailinfo now unfolds, unquotes, and un-space-stuffs such messages.
---
This is a bit simpler than the previous patch and incorporates most of Johannes' feedback. I also switched from enum's to int's since enum's were really overkill.
builtin-mailinfo.c | 31 +++++++++++++++++++++++++++++++ 1 files changed, 31 insertions(+), 0 deletions(-)
builtin-mailinfo.c +31 -0
@@ -21,6 +21,10 @@ static enum {TYPE_TEXT ,TYPE_OTHER , } message_type; +/* RFC 3676 Text/Plain Format and DelSp Parameters */ +static int message_format_is_flowed; +static int message_delsp_is_yes; +static char charset[256 ];static int patch_lines;static char **p_hdr_data, **s_hdr_data;
@@ -193,6 +197,15 @@ static int handle_content_type(char *line)if (strcasestr (line,"text/" ) ==NULL ) message_type =TYPE_OTHER ; +else if (strcasestr (line,"text/plain" )) { +char attr[256 ]; +if ((message_format_is_flowed = ( +slurp_attr (line,"format=" , attr) && + !strcasecmp (attr,"flowed" )))) + message_delsp_is_yes = ( +slurp_attr (line,"delsp=" , attr) && + !strcasecmp (attr,"yes" )); + }if (slurp_attr (line,"boundary=" , boundary +2 )) {memcpy (boundary,"--" ,2 );if (content_top++ >= &content[MAX_BOUNDARIES ]) {
@@ -681,6 +694,8 @@ again: transfer_encoding =TE_DONTCARE ; charset[0 ] =0 ; message_type =TYPE_TEXT ; + message_format_is_flowed =0 ; + message_delsp_is_yes =0 ;/* slurp in this section's info */ while (read_one_header_line (line,sizeof (line), fin))
@@ -770,6 +785,22 @@ static int handle_filter(char *line, unsigned linesize) {static int filter =0 ; +if (message_format_is_flowed &&strcmp (line,"-- \n" )) { +/* strip quote markers */ +while (*line && *line =='>' ) + line++; +/* undo space-stuffing */ +if (*line ==' ' ) + line++; +if (strcmp (line,"-- \n" )) { +char *cp =strchrnul (line,'\n' ); +if (cp > line && *(cp-1 ) ==' ' && *cp =='\n' ) +/* line is flowed (wrapped); remove +* the \n or <space>\n if delsp is yes +*/ + *(cp-(message_delsp_is_yes?1 :0 )) ='\0' ; + } + }/* filter tells us which part we left off on * a non-zero return indicates we hit a filter point */
Signature · 1 line
1.5.4.1.1281.g75df
PATCH 3/3 test mailinfo rfc3676 support
Adds format=flowed and format=flowed delsp=yes messages to the sample.mbox for testing rfc3676 support.
---
I added two tests. The previous patch only added one. This also I think does a better job of checking the new code.
t/t5100-mailinfo.sh | 2 +- t/t5100/info0009 | 5 ++ t/t5100/info0010 | 5 ++ t/t5100/msg0009 | 12 +++++ t/t5100/msg0010 | 12 +++++ t/t5100/patch0009 | 24 ++++++++++ t/t5100/patch0010 | 24 ++++++++++ t/t5100/sample.mbox | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 205 insertions(+), 1 deletions(-) create mode 100644 t/t5100/info0009 create mode 100644 t/t5100/info0010 create mode 100644 t/t5100/msg0009 create mode 100644 t/t5100/msg0010 create mode 100644 t/t5100/patch0009 create mode 100644 t/t5100/patch0010
t/t5100-mailinfo.sh +1 -1
@@ -11,7 +11,7 @@ test_expect_success 'split sample box' \ 'git mailsplit -o. ../t5100/sample.mbox >last && last=`cat last` && echo total is $last && - test `cat last` = 8' + test `cat last` = 10' for mail in `echo 00*` do
t/t5100/info0009 new file +5 -0
@@ -0,0 +1,5 @@ +Author: Jay Soffian +Email: [email protected] +Subject: Add lorum ipsum text +Date: Fri, 15 Feb 2008 21:16:18 -0000 +
t/t5100/info0010 new file +5 -0
@@ -0,0 +1,5 @@ +Author: Jay Soffian +Email: [email protected] +Subject: Add lorum ipsum text +Date: Fri, 15 Feb 2008 21:15:32 -0000 +
t/t5100/msg0009 new file +12 -0
@@ -0,0 +1,12 @@ +The text was added twice, once pre-wrapped, once as a single long line, +which will be flowed. + +The next two lines test for space stuffing: +From nobody +> + +Let's also add some non-ASCII characters: + +①② ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩ + +Signed-off-by: Jay Soffian <[email protected]>
t/t5100/msg0010 new file +12 -0
@@ -0,0 +1,12 @@ +The text was added twice, once pre-wrapped, once as a single long line, +which will be flowed. + +The next two lines test for space stuffing: +From nobody +> + +Let's also add some non-ASCII characters: + +①② ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩ + +Signed-off-by: Jay Soffian <[email protected]>
t/t5100/patch0009 new file +24 -0
@@ -0,0 +1,24 @@ +--- +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + + lorum.txt | 8 ++++++++ + 1 files changed, 8 insertions(+), 0 deletions(-) + +diff --git a/lorum.txt b/lorum.txt +index cbe5415..c04a705 100644 +--- a/lorum.txt ++++ b/lorum.txt +@@ -1,2 +1,10 @@ + From nobody ++Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ++Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod ++tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim ++veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea ++commodo consequat. Duis aute irure dolor in reprehenderit in voluptate ++velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint ++occaecat cupidatat non proident, sunt in culpa qui officia deserunt ++mollit anim id est laborum. + > +-- +1.5.4.1.1281.g75df +
t/t5100/patch0010 new file +24 -0
@@ -0,0 +1,24 @@ +--- +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + + lorum.txt | 8 ++++++++ + 1 files changed, 8 insertions(+), 0 deletions(-) + +diff --git a/lorum.txt b/lorum.txt +index cbe5415..c04a705 100644 +--- a/lorum.txt ++++ b/lorum.txt +@@ -1,2 +1,10 @@ + From nobody ++Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ++Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod ++tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim ++veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea ++commodo consequat. Duis aute irure dolor in reprehenderit in voluptate ++velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint ++occaecat cupidatat non proident, sunt in culpa qui officia deserunt ++mollit anim id est laborum. + > +-- +1.5.4.1.1281.g75df +
t/t5100/sample.mbox +122 -0
125 lines not on this page · raw message
Signature · 1 line
1.5.4.1.1281.g75df
Re: [PATCH 1/3] mailinfo: ensure handle_filter gets only one line at a time
In reply to Jay SoffianQuoted 8 lines · Jay Soffian
handle_filter()is only supposed to get one line at a time.handle_body()already ensures this after decoding BASE64 messages per this comment:/* this is a decoded line that may contain
* multiple new lines. Pass only one chunk
* at a time tohandle_filter()
Good eyes. Although I do not think we need to quote in-code comments here ;-)
Quoted 16 lines
QP messages need to be similarly processed since decoding QP can also add newlines.
Signed-off-by Jay Soffian <[email protected]>---
I broke this change out into it's own patch since it's independent of the format=flowed additions.builtin-mailinfo.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)builtin-mailinfo.c +0
-0
builtin-mailinfo.c static void handle_body(void) · line 824
Quoted 7 lines
builtin-mailinfo.c +1
-0@@ -818,6 +818,7 @@ static void handle_body(void)switch (transfer_encoding) {case TE_BASE64 : +case TE_QP : {char *op = line;
And it appears to be an obvious fix that should even go to 'maint' where no new features will land.
Care to provide a test case for this one?
Re: [PATCH 1/3] mailinfo: ensure handle_filter gets only one line at a time
Quoted 2 lines · Junio C Hamano
And it appears to be an obvious fix that should even go to 'maint' where no new features will land.
Yes.
Quoted 1 line
Care to provide a test case for this one?
I'll see what I can do. I'm not sure it makes a difference w/o my format=flowed additions, so there may not be a test case, but I'll poke it with a stick some more.
Signature · 1 line
j.
Re: [PATCH 1/3] mailinfo: ensure handle_filter gets only one line at a time
Quoted 9 lines · Jay Soffian
Quoted 2 lines · Junio C Hamano
And it appears to be an obvious fix that should even go to 'maint' where no new features will land.
Yes.
Quoted 1 line
Care to provide a test case for this one?
I'll see what I can do. I'm not sure it makes a difference...
Of course it does; otherwise I wouldn't have asked.
Thanks for the fix. The following will go to 'maint'.
-- >8 --
From: Jay Soffian <[email protected]>
Date: Fri, 15 Feb 2008 16:53:36 -0500
Subject: mailinfo: feed only one line to handle_filter() for QP input
The function is intended to be fed one logical line at a time to inspect, but a QP encoded raw input line can have more than one lines, just like BASE64 encoded one.
Quoting LF as =0A may be unusual but RFC2045 allows it.
The issue was noticed and fixed by Jay Soffian. JC added a test to protect the fix from regressing later.
--- builtin-mailinfo.c | 1 + t/t5100-mailinfo.sh | 2 +- t/t5100/info0009 | 5 +++++ t/t5100/msg0009 | 2 ++ t/t5100/patch0009 | 13 +++++++++++++ t/t5100/sample.mbox | 23 +++++++++++++++++++++++ 6 files changed, 45 insertions(+), 1 deletions(-)
builtin-mailinfo.c +1 -0
@@ -818,6 +818,7 @@ static void handle_body(void)switch (transfer_encoding) {case TE_BASE64 : +case TE_QP : {char *op = line;
t/t5100-mailinfo.sh +1 -1
@@ -11,7 +11,7 @@ test_expect_success 'split sample box' \ 'git mailsplit -o. ../t5100/sample.mbox >last && last=`cat last` && echo total is $last && - test `cat last` = 8' + test `cat last` = 9' for mail in `echo 00*` do
t/t5100/info0009 new file +5 -0
@@ -0,0 +1,5 @@ +Author: F U Bar +Email: [email protected] +Subject: updates +Date: Mon, 17 Sep 2001 00:00:00 +0900 +
t/t5100/msg0009 new file +2 -0
@@ -0,0 +1,2 @@ +This is to fix diff-format documentation. +
t/t5100/patch0009 new file +13 -0
@@ -0,0 +1,13 @@ +diff --git a/Documentation/diff-format.txt b/Documentation/diff-format.txt +index b426a14..97756ec 100644 +--- a/Documentation/diff-format.txt ++++ b/Documentation/diff-format.txt +@@ -81,7 +81,7 @@ The "diff" formatting options can be customized via the + environment variable 'GIT_DIFF_OPTS'. For example, if you + prefer context diff: + +- GIT_DIFF_OPTS=-c git-diff-index -p $(cat .git/HEAD) ++ GIT_DIFF_OPTS=-c git-diff-index -p HEAD + + + 2. When the environment variable 'GIT_EXTERNAL_DIFF' is set, the
t/t5100/sample.mbox +23 -0
@@ -407,3 +407,26 @@ Subject: [PATCH] another patch Hey you forgot the patch! +From nobody Mon Sep 17 00:00:00 2001 +From: A U Thor <[email protected]> +Date: Mon, 17 Sep 2001 00:00:00 +0900 +Mime-Version: 1.0 +Content-Type: Text/Plain; charset=us-ascii +Content-Transfer-Encoding: Quoted-Printable + +=0A=0AFrom: F U Bar <[email protected]> +Subject: [PATCH] updates=0A=0AThis is to fix diff-format documentation. + +diff --git a/Documentation/diff-format.txt b/Documentation/diff-format.txt +index b426a14..97756ec 100644 +--- a/Documentation/diff-format.txt ++++ b/Documentation/diff-format.txt +@@ -81,7 +81,7 @@ The "diff" formatting options can be customized via the + environment variable 'GIT_DIFF_OPTS'. For example, if you + prefer context diff: +=20 +- GIT_DIFF_OPTS=3D-c git-diff-index -p $(cat .git/HEAD) ++ GIT_DIFF_OPTS=3D-c git-diff-index -p HEAD +=20 +=20 + 2. When the environment variable 'GIT_EXTERNAL_DIFF' is set, the
Re: [PATCH 1/3] mailinfo: ensure handle_filter gets only one line at a time
Quoted 8 lines · Junio C Hamano
Quoted 4 lines · Jay Soffian
Quoted 1 line · Junio C Hamano
Care to provide a test case for this one?
I'll see what I can do. I'm not sure it makes a difference...
Of course it does; otherwise I wouldn't have asked.
What I meant was, I wasn't sure if the output of mailinfo would have been incorrect w/o the patch. I noticed it only when I added the format=flowed code which relied on handle_filter() getting a single line at a time. I hadn't looked at whether handle_commit_msg() and/or handle_patch() (downstream of handle_filter) would care.
I wasn't trying to shirk off adding a test case. :-)
j.
Re: [PATCH 1/3] mailinfo: ensure handle_filter gets only one line at a time
In reply to Junio C HamanoQuoted 5 lines · Junio C Hamano
The function is intended to be fed one logical line at a time to inspect, but a QP encoded raw input line can have more than one lines, just like BASE64 encoded one.
Quoting LF as =0A may be unusual but RFC2045 allows it.
Well, this is a bit dark-gray.
The canonical line ending CRLF should not be represented as =0A and the intention of RFC2045 seems to allow =0A only as part of binary contents. So strictly speaking this may not be needed, but there may always be not quite conforming MUA that would break the assumption and would bite us, so we'd better be safe than sorry.
I really do not like this.
format=flowed instructs the receivers MUA that it is Ok to reflow the text when the message is presented to the user. That is exactly what we do _NOT_ want to happen to patches. Your implementation may cleanly salvage what the sender intended to send, but salvaging when applying the patch is too late.
You review the patch, decide to apply or reject, and then finally apply. Unmangling of corrupt contents should be done before you review, not before you apply.
We have similar hacks to clean-up MIME attachments and CTE. They are useful when your mailpath is not clean and can corrupt contents even if you try to send it as text/plain in-line patch as a fallback measure to ensure the contents not to get corrupted. format=flowed is completely opposite --- you are giving your and recipients MUA freedom to reflow the text, but there is no valid reason to allow that when sending patches.
We do not even encourage MIME attachments and ask senders to try sending uncorrupt patch in-line, _even though_ MIME attachments is a way to try harder not to corrupt the payload. Why should we encourage format=flowed which is meant to do the opposite (i.e. we do not care about the exact content, we care more about how better the paragraph looks and easier to read on screens with different width)?
The format is not meant for the exact transmission of text (like "patch") but more for paragraphed prose that can be re-fit on narrower display like phones. Section 5. even goes to say "Hand-aligned text, such as ASCII tables or art, source code, etc., SHOULD be sent as fixed, not flowed lines."
Side note. I did not look at the patch very carefully, but can you salvage a deleted text in the patch that removes a line that consists of "- " (minus followed by a single space and then end-of-line), or any deleted or added text that ends with a SP without making them misinterpreted as "flowed" line for that matter?
I even suspect that the sending MUA client may misbehave for such a patch line. In fact, doesn't section 4.2 say "a generating agant should trim spaces before user-inserted hard line breaks."? It implies to me that you cannot have a fixed line that ends with SP.
So just reject the patch when somebody sends you format=flowed and ask them to re-send without =flowed, and the world will be a much better place.
Quoted 11 lines · Junio C Hamano
I really do not like this.
format=flowed instructs the receivers MUA that it is Ok to reflow the text when the message is presented to the user. That is exactly what we do _NOT_ want to happen to patches. Your implementation may cleanly salvage what the sender intended to send, but salvaging when applying the patch is too late.
You review the patch, decide to apply or reject, and then finally apply. Unmangling of corrupt contents should be done before you review, not before you apply.
I have to agree that this is a bad idea - for the reasons stated, and one additional:
There are MUA's which send 'format=fixed' email marked as 'format=flowed'.
At which point the receiving MUA performing the deflowing _will_ get a corrupt patch.
I saw this recently wrt to internal company code reviews. We have a variety of different MUAs in use on a variety of different OSs.
Some 'properly' implement format=flowed, some do not. Some always
send format=flowed for text, some can have it disabled to send format=fixed. Despite what the RFCs say, some of them also QP or base64 encode such flowed pieces of text. It is a nightmare.
However it seems thay all use fixed format for attachments, unfortunately some them always tag those as application/octet-stream, so one cannot automatically process them (in the absense of other clues).
I am able to work around some of these issues by configuing mutt to do some guesses when it sees application/x-patch, or application/octet-stream and a file with say as '.diff' extension.
But ultimately for some of these one is forced to contact the original author and get access to the actual patch file on the filesystem. While this can be done for a 'small' community, it is not something that would work in the wider world.
So - format=flowed is simply unsuitable for fixed format text and should not be used.
[Actually it rather turns out format=flowed is a bad idea all together,
you should see what it does to log files...]
DF
Quoted 1 line · Junio C Hamano
I really do not like this.
Ugh.
Quoted 2 lines
format=flowed instructs the receivers MUA that it is Ok to reflow the text when the message is presented to the user.
No, it indicates that the MUA sender mangled the message, but did so in a way that it can be unmanged* as long as the receiving MUA implements RFC 3676.
* Trailing whitespace removed by the MUA before sending is the one exception that cannot be unmangled; I'll address this further below.
Quoted 3 lines
You review the patch, decide to apply or reject, and then finally apply. Unmangling of corrupt contents should be done before you review, not before you apply.
As long as you're reviewing the patch in an MUA which implements RFC 3676, you'll be reviewing the unmangled patch. By your reasoning, mailinfo shouldn't decode QP or BASE64 either. If you want to make 100% sure you're reviewing *exactly* what you plan to apply, then you need to run it through git-am first and then review it via git-diff, no?
If you're reviewing in your MUA, your MUA may already be taking care of QP or BASE64 for you. If the MUA is RFC 3676 aware, it's un-flowing format=flowed too. mailinfo should do the same.
Quoted 3 lines
format=flowed is completely opposite --- you are giving your and recipients MUA freedom to reflow the text, but there is no valid reason to allow that when sending patches.
Not all MUA's can be configured to disable format=flowed. Sure, it's best if folks send 100% plain/text w/o any mangling, but their MUA may already be doing B64 or QP out of their control.
Quoted 4 lines
We do not even encourage MIME attachments and ask senders to try sending uncorrupt patch in-line,
_eventhough_MIME attachments is a way to try harder not to corrupt the payload. Why should we encourage format=flowed
We're not trying to encourage it, just accommodate it where the sender can't disable it.
Quoted 5 lines
The format is not meant for the exact transmission of text (like "patch") but more for paragraphed prose that can be re-fit on narrower display like phones. Section 5. even goes to say "Hand-aligned text, such as ASCII tables or art, source code, etc., SHOULD be sent as fixed, not flowed lines."
Again, some MUA's aren't configurable. They apply format=flowed to any text/plain message.
Quoted 7 lines
Side note. I did not look at the patch very carefully, but can you salvage a deleted text in the patch that removes a line that consists of "- " (minus followed by a single space and then end-of-line), or any deleted or added text that ends with a SP without making them misinterpreted as "flowed" line for that matter?
Quoted 5 lines
I even suspect that the sending MUA client may misbehave for such a patch line. In fact, doesn't section 4.2 say "a generating agant should trim spaces before user-inserted hard line breaks."? It implies to me that you cannot have a fixed line that ends with SP.
Yes, the sending MUA likely striped trailing whitespace and this cannot be recovered. Let's look at this in practice to see where it can be a problem.
Existing code generally shouldn't have trailing whitespace nor whitespace only lines. However, let's say that it does and that the patch refers to one of these lines (either as context or a subtraction). In this case that hunk will fail to apply, unless we teach git-apply to be lenient if the only difference between the line in the patch and the existing code is trailing whitespace.
In the case of an addition, the patch *shouldn't* contain trailing whitespace anyway. If it did, git-apply would in its default configuration flag it as a whitespace error. So arguably, the MUA is doing you a favor by stripping whitespace on such lines. :-)
Quoted 3 lines
So just reject the patch when somebody sends you format=flowed and ask them to re-send without =flowed, and the world will be a much better place.
I don't get it. Why not accommodate fascist RFC 3676 MUAs if we can for the same reasons we accommodate QP, BASE64, and attachments instead of inline? I guess I can only think of two reasons:
1) We need to accommodate QP, BASE64, etc since they may be due to the MTA. By contrast, format=flowed is always an MUA issue.
2) The other transformations are 100% safe in getting back exactly the patch the user intended. format=flowed isn't. But per above, I'm not sure the trailing-whitespace loss is a problem in practice.
j.
Quoted 6 lines · Jay Soffian
Quoted 4 lines · Junio C Hamano
I really do not like this.
format=flowed instructs the receivers MUA that it is Ok to reflow the text when the message is presented to the user.
Quoted 6 lines
No, it indicates that the MUA sender mangled the message, but did so in a way that it can be unmanged* as long as the receiving MUA implements RFC 3676.
* Trailing whitespace removed by the MUA before sending is the one exception that cannot be unmangled; I'll address this further below.
There is no way for you to address this other than brushing it aside, saying "it does not matter". The information lost is information lost.
Quoted 2 lines
Existing code generally shouldn't have trailing whitespace nor whitespace only lines. ...
Says who?
We and the kernel folks are probably one of the more whitespace strict projects, but I am sure both of us get our fair share of patches that fix whitespace breakages while updating the surrounding area. There are existing breakages, and also there are lines that have deliberate trailing whitespaces.
Also, remember that this is not May 2005. git is not about the kernel and git itself. We passed that point long time ago. Other communities have different whitespace policies.
Quoted 5 lines
... However, let's say that it does and that the
patch refers to one of these lines (either as context or a subtraction). In this case that hunk will fail to apply, unless we teach git-apply to be lenient if the only difference between the line in the patch and the existing code is trailing whitespace.
Failing to apply is not something I am worried about.
Silently applying bogosity is much more problematic, because it would be _silent_, and "git am" is often used to apply hundreds of patches in one go. We need to be able to trust the tool and the tool should error out when there is any ambiguity.
Quoted 3 lines
In the case of an addition, the patch *shouldn't* contain trailing whitespace anyway. If it did, git-apply would in its default configuration flag it as a whitespace error.
It just "warns". It does not strip by default. It should be a conscious user decision (done with "am --whitespace=fix") and the project policy.
Quoted 7 lines
Quoted 3 lines
So just reject the patch when somebody sends you format=flowed and ask them to re-send without =flowed, and the world will be a much better place.
I don't get it. Why not accommodate fascist RFC 3676 MUAs if we can for the same reasons we accommodate QP, BASE64, and attachments instead of inline?
I need to ask people listening from the sidelines on the list here. Was my explanation why MIME attachments and QP/BASE64 are _fundamentally different_ from format=flowed insufficient?
The point is, format=flowed is _not_ meant for precise transfer of the content matter. The design objective of format=flowed lies elsewhere. It achieves nicer looking line-wrapping by sacrificing the precise transfer of the content. It probably does a very good job, allowing you to communicate with pals on text-email enabled phones, although I do not have one so I cannot judge ;-).
Attachments, QP and BASE64 are all about getting the contents as intact as possible. Because they tend to make reviews harder, git and the kernel community frown upon them. But our tools tolerate them, because attachment is a fine or even preferred way to transfer the patches in other circles.
But format=flowed is different. It loses information. Not just one space at the end of the original line, but if you have very long line that has more than one spaces at an unfortunate place, the sending MUA can cut the line there and leave a single trailing space for the receiving end to reflow.
RFC3676 may be a good text communication medium. It is just not suitable for patch transfer. Just don't use it.
Quoted 5 lines · Junio C Hamano
But format=flowed is different. It loses information. Not just one space at the end of the original line, but if you have very long line that has more than one spaces at an unfortunate place, the sending MUA can cut the line there and leave a single trailing space for the receiving end to reflow.
Well, according to the RFC, that shouldn't be the case. The only lost information should be trailing whitespace. Embedded whitespace should not be altered.
Quoted 2 lines
RFC3676 may be a good text communication medium. It is just not suitable for patch transfer. Just don't use it.
Would you consider making this configurable. Something like:
applymail.allowed_flowed = true/false/warn
If you're completely opposed though, we should modify git-am (and/or mailinfo) to reject format=flowed messages entirely, no?
j.
Quoted 5 lines · Jay Soffian
On Feb 16, 2008 1:57 AM, Junio C Hamano <[email protected]> wrote:
Well, according to the RFC, that shouldn't be the case. The only lost information should be trailing whitespace. Embedded whitespace should not be altered.
Unfortunately, actual implementations and people's use matter more. I usually do not use graphical MUAs but when I tried to paste long lines to Thunderbird (or was it Evolution, I do not recall), it behaved as if I was typing the words, folded them at convenient inter word spaces. I would not blindly trust what RFC says.
There also seem to be a strong correlation between people who allow their MUA to send format=flowed when sending patches and people who cut and paste their patches to their MUA, corrupting whitespaces.
Quoted 9 lines
Quoted 2 lines
RFC3676 may be a good text communication medium. It is just not suitable for patch transfer. Just don't use it.
Would you consider making this configurable. Something like:
applymail.allowed_flowed = true/false/warn
If you're completely opposed though, we should modify git-am (and/or mailinfo) to reject format=flowed messages entirely, no?
I would not even consider applying flowed message these days (my workflow is to review in MUA, save perhaps worthy ones to a separate mailbox and after re-reviewing apply), so they will not hit "git am" and it would not personally affect me. Honestly, I do not care very much either way.
But for some projects (perhaps the ones that do not value their source as much as we do ;-)) accepting a subset of flowed contents might be reasonable and even useful. Maybe something like this would be reasonably safe and still useful?
- A format=flowed message that actually has a flowed line is always rejected;
- If there is no flowed line (i.e. lines that end with SP) in the message, we unstuff the initial space to produce the result (there won't be anything else that is funny, will there? In a patch we do not care much about the quotation of discussions):
- If apply.whitespace is set to nowarn, we do not warn even though we might have lost the trailing whitespaces.
- If apply.whitespace is set to warn, we warn about the flowed message.
- If apply.whitespace is set to error or fix, we error out, but still leaving the result for manual inspection.
I dunno.
By the way, I do not think a solution that only uses configuration is usable.
When you have to apply many patches, you set your configuration to the most strict (i.e. apply.whitespace=error), and when the processing errors out, you then inspect the situation and manually override with the command line --whitespace=fix (or "warn") to process that one message. You need both.
Since mailinfo now has only a very few users (quiltimport and "am"), we probably could add --whitespace option to mailinfo, and teach "am" to pass --whitespace command line option it was given (otherwise the value from apply.whitespace configuration) when running mailinfo, if we were to do a "safer subset" as outlined above.
Hi,
builtin-mailinfo.c static enum { · line 26
Quoted 12 lines · Jay Soffian
builtin-mailinfo.c +4
-0@@ -20,6 +20,13 @@ static enum {static enum {TYPE_TEXT ,TYPE_OTHER , } message_type; +/* RFC 3676 Text/Plain Format and DelSp Parameters */ +static enum { +FORMAT_NONE ,FORMAT_FIXED ,FORMAT_FLOWED , +} tp_format;
Why not call it "enum message_format"?
builtin-mailinfo.c static enum { · line 29
Quoted 3 lines
builtin-mailinfo.c +3
-0+static enum { +DELSP_NONE ,DELSP_YES ,DELSP_NO , +} tp_delsp;
Why not call it "enum message_is_delsp"?
Quoted 5 lines
builtin-mailinfo.c +1
-0@@ -193,6 +200,18 @@ static int handle_content_type(char *line)if (strcasestr (line,"text/" ) ==NULL ) message_type =TYPE_OTHER ; +else if (strcasestr (line,"text/plain" )) {
builtin-mailinfo.c static int handle_content_type(char *line) · line 213
Quoted 10 lines
builtin-mailinfo.c +10
-0+char attr[256 ]; +if (slurp_attr (line,"format=" , attr) && !strcasecmp (attr,"flowed" )) { + tp_format =FORMAT_FLOWED ; +if (slurp_attr (line,"delsp=" , attr) && !strcasecmp (attr,"yes" )) + tp_delsp =DELSP_YES ; +else + tp_delsp =DELSP_NO ; + } +else + tp_format =FORMAT_FIXED ;
Does that mean that the format is only set if the content type is "text/plain"?
Quoted 10 lines
builtin-mailinfo.c +2
-0@@ -681,6 +700,8 @@ again: transfer_encoding =TE_DONTCARE ; charset[0 ] =0 ; message_type =TYPE_TEXT ; + tp_format =FORMAT_NONE ; + tp_delsp =DELSP_NONE ;/* slurp in this section's info */ while (read_one_header_line (line,sizeof (line), fin))
builtin-mailinfo.c static int handle_filter(char *line, unsigned linesize) · line 794
Quoted 4 lines
builtin-mailinfo.c +1
-0@@ -770,6 +791,24 @@ static int handle_filter(char *line, unsigned linesize) {static int filter =0 ; +if (tp_format ==FORMAT_FLOWED && !!strcmp (line,"-- \n" )) {
The !! is unnecessary; please skip it.
builtin-mailinfo.c static int handle_filter(char *line, unsigned linesize) · line 800
Quoted 6 lines
builtin-mailinfo.c +6
-0+char *cp = line; +while (*cp =='>' && *cp !=0 ) + cp++; +if (*cp ==' ' ) + cp++; + line = cp;
How about using strchrnul()?
Note: I do not know enough about format=flawed to know why you should skip to "> ". I would have expected "\n", though.
builtin-mailinfo.c static int handle_filter(char *line, unsigned linesize) · line 801
Quoted 1 line
builtin-mailinfo.c +1
-0+if (!!strcmp (line,"-- \n" )) {
The !! is unnecessary; please skip it.
builtin-mailinfo.c static int handle_filter(char *line, unsigned linesize) · line 803
Quoted 2 lines
builtin-mailinfo.c +2
-0+while (*cp !='\n' && *cp !=0 ) + cp++;
Again, this is the job for strchrnul().
builtin-mailinfo.c static int handle_filter(char *line, unsigned linesize) · line 809
Quoted 6 lines
builtin-mailinfo.c +6
-0+if (cp > line && *cp =='\n' && *(cp-1 ) ==' ' ) { +if (tp_delsp ==DELSP_YES ) + *(cp-1 ) ='\0' ; +else + *cp ='\0' ; + }
Or maybe
cp[0 - (tp_delsp == DELSP_YES)] = '\0';
But maybe that is too cute.
But another thing struck me here: why setting *cp = '\0'; only if *(cp-1) == ' ', even if tp_delsp != DELSP_YES?
builtin-mailinfo.c static void handle_body(void) · line 862
Quoted 7 lines
builtin-mailinfo.c +1
-0@@ -818,6 +857,7 @@ static void handle_body(void)switch (transfer_encoding) {case TE_BASE64 : +case TE_QP : {char *op = line;
Did that just slip in, or was this intended. If the latter, is this related to format=flawed, or is it a bug fix in its own right?
Ciao,
Dscho
Quoted 1 line · Johannes Schindelin
Why not call it "enum
message_format"?
It's only applicable to text/plain messages. Then again, this will be set to FORMAT_NONE for non-text/plain messages so I guess that's just as good a name.
BTW, since all I care about is format=flowed, I could just as easily use an int and call it something like message_is_flowed. This would actually simplify the code a bit but I was trying to follow the existing code which uses an enum for message_type that could similarly have been an int called message_type_is_text.
Quoted 1 line
Why not call it "enum
message_is_delsp"?
Sure, my reasoning is same as above.
Quoted 18 lines
Quoted 5 lines
builtin-mailinfo.c +1
-0@@ -193,6 +200,18 @@ static int handle_content_type(char *line)if (strcasestr (line,"text/" ) ==NULL ) message_type =TYPE_OTHER ; +else if (strcasestr (line,"text/plain" )) {builtin-mailinfo.c static int handle_content_type(char *line) · line 213
Quoted 10 lines
builtin-mailinfo.c +10
-0+char attr[256 ]; +if (slurp_attr (line,"format=" , attr) && !strcasecmp (attr,"flowed" )) { + tp_format =FORMAT_FLOWED ; +if (slurp_attr (line,"delsp=" , attr) && !strcasecmp (attr,"yes" )) + tp_delsp =DELSP_YES ; +else + tp_delsp =DELSP_NO ; + } +else + tp_format =FORMAT_FIXED ;Does that mean that the format is only set if the content type is "text/plain"?
tp_format is initially set to FORMAT_NONE. Only for text/plain is it them set to FORMAT_FIXED or FORMAT_FLOWED. Again, though, all I care about is format=flowed so I could be using an int called message_is_flowed but was following the enum message_type example.
Quoted 3 lines
builtin-mailinfo.c static int handle_filter(char *line, unsigned linesize) · line 794
Quoted 1 line
builtin-mailinfo.c +1
-0+if (tp_format ==FORMAT_FLOWED && !!strcmp (line,"-- \n" )) {The !! is unnecessary; please skip it.
Heh. I'd never seen that before I started perusing the git code, I was just following along. But looking more carefully now I see that the other code only does that if it's assigning to an int. I see that it's not needed in a boolean context.
Quoted 1 line
How about using
strchrnul()?
Cool. I'd never heard of it.
Quoted 11 lines
builtin-mailinfo.c static int handle_filter(char *line, unsigned linesize) · line 809
Quoted 6 lines
builtin-mailinfo.c +6
-0+if (cp > line && *cp =='\n' && *(cp-1 ) ==' ' ) { +if (tp_delsp ==DELSP_YES ) + *(cp-1 ) ='\0' ; +else + *cp ='\0' ; + }Or maybe
cp[0 - (tp_delsp == DELSP_YES)] = '\0';But maybe that is too cute.
Heh, I think that's too clever by half.
Quoted 2 lines
But another thing struck me here: why setting *cp = '\0'; only if *(cp-1) == ' ', even if
tp_delsp!=DELSP_YES?
" \n$" indicates the line is flowed, meaning the MUA added the '\n' to the line and we need to remove it. The next question is whether the space before the '\n' was also inserted by the MUA. If DELSP_YES, it was, so we want to remove it, else it was an existing space that we want to keep.
Quoted 10 lines
builtin-mailinfo.c static void handle_body(void) · line 862
Quoted 7 lines
builtin-mailinfo.c +1
-0@@ -818,6 +857,7 @@ static void handle_body(void)switch (transfer_encoding) {case TE_BASE64 : +case TE_QP : {char *op = line;Did that just slip in, or was this intended. If the latter, is this related to format=flawed, or is it a bug fix in its own right?
It was intended. The patch doesn't have enough context to have included this comment inside the TE_BASE64 case:
/* this is a decoded line that may contain * multiple new lines. Pass only one chunk * at a time to handle_filter() */
It turns out that decoding QP can also cause a decoded line to contain extra newlines. So it's a bug fix, but I'm not sure it mattered before. I'll break it out into a separate patch though to make that clear. And I know I should add a test first for that fix, but ugh, figuring out the test case for a one-line code change is painful.
Thanks for the comments, I'll follow up with a revised patch.
j.
Quoted 5 lines · Johannes Schindelin
builtin-mailinfo.c static int handle_filter(char *line, unsigned linesize) · line 797
Quoted 3 lines
builtin-mailinfo.c +3
-0+char *cp = line; +while (*cp =='>' && *cp !=0 ) + cp++;How about using
strchrnul()?
Actually strchrnul() here isn't correct. I want to strip leading '>' only. strchrnul() will search for the first occurrence (skipping over non-'>' to do so), which is not what I want.
Quoted 4 lines
builtin-mailinfo.c static int handle_filter(char *line, unsigned linesize) · line 803
Quoted 2 lines
builtin-mailinfo.c +2
-0+while (*cp !='\n' && *cp !=0 ) + cp++;Again, this is the job for
strchrnul().
Yep, here strchrnul would be fine.
j.
Hi,
Quoted 11 lines · Jay Soffian
Quoted 5 lines · Johannes Schindelin
builtin-mailinfo.c static int handle_filter(char *line, unsigned linesize) · line 797
Quoted 3 lines
builtin-mailinfo.c +3
-0+char *cp = line; +while (*cp =='>' && *cp !=0 ) + cp++;How about using
strchrnul()?Actually
strchrnul()here isn't correct. I want to strip leading '>' only.strchrnul()will search for the first occurrence (skipping over non-'>' to do so), which is not what I want.
Oh, okay, I read again.
You just wanted to strip the leading ">". So I know even less what you tried to do. But then, I do not know anything about format=flawed either.
Ciao,
Dscho