threads / patch / 9397

patchsend-email: let sanitize_address_rfc822 do rfc2047 quoting

Subject: [PATCH] send-email: let sanitize_address_rfc822 do rfc2047 quoting

## tl;dr

5 messages between Aug 5, 2007 and Aug 9, 2007. Diffs are folded; open one to read it.

replies: 4people: 2as markdown or json

Uwe Kleine-König· Aug 5, 2007, 20:09 UTC · lore

Without this patch I'm not able to properly send emails as I have a non-ascii character in my name.

The former version tried to fix-up the real name part with double quotes if it includes a '.'. I removed this as rfc2047 can handle a dot, too.

Signed-off-by: Uwe Kleine-König <ukleinek@informatik.uni-freiburg.de>
---
Hello,
I already sent a similar patch that was discussed a bit[1], but was not taken.
The list of allowed character was taken from Python's email package.

Comparing with the former version I removed the quoting as described in the 2nd paragraph of the log and now I only test $recipient_name once.

I will try to send this patch with next + this patch. I think it should work in principle, but --suppress-from is not honored. I will take a look on this issue later.

Best regards Uwe

[1] http://thread.gmane.org/gmane.comp.version-control.git/52093
 git-send-email.perl |   23 +++++++++++++++++------
 1 files changed, 17 insertions(+), 6 deletions(-)
Show changes to git-send-email.perl +17 −6
diff --git a/git-send-email.perl b/git-send-email.perl
index f43f92f..5785e29 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -459,16 +459,27 @@ sub unquote_rfc2047 {
 	return "$_";
 }
 
-# If an address contains a . in the name portion, the name must be quoted.
+# The name part of an address must consist only of alnum chars, space and a few
+# others.
+# If it contains a "forbidden" char in the name port, quote it according to
+# rfc2047.
 sub sanitize_address_rfc822
 {
 	my ($recipient) = @_;
-	my ($recipient_name) = ($recipient =~ /^(.*?)\s+</);
-	if ($recipient_name && $recipient_name =~ /\./ && $recipient_name !~ /^".*"$/) {
-		my ($name, $addr) = ($recipient =~ /^(.*?)(\s+<.*)/);
-		$recipient = "\"$name\"$addr";
+	my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)(\s+<.*)/);
+
+	if ($recipient_name) {
+		if ($recipient_name =~ /[^-a-zA-Z0-9!*+\/ ]/ && $recipient_name !~ /=\?utf-8\?q?.*\?=/) {
+			$recipient_name =~ s/([^-a-zA-Z0-9!*+\/ ])/sprintf("=%02X", ord($1))/eg;
+			$recipient_name =~ s/ /_/;
+			$recipient_name =~ s/(.*)/=\?utf-8\?q\?$1\?=/;
+		}
+
+		return "$recipient_name$recipient_addr";
+
+	} else {
+		return "$recipient";
 	}
-	return $recipient;
 }
 
 sub send_message
-- 
1.5.3.rc3.13.g7ab3
Jakub Narebski· Aug 6, 2007, 00:21 UTC · re: Uwe Kleine-König · lore

Re: [PATCH] send-email: let sanitize_address_rfc822 do rfc2047 quoting

[Uwe Kleine-König <ukleinek@informatik.uni-freiburg.de>,
 git@vger.kernel.org]
Uwe Kleine-König wrote:
> Without this patch I'm not able to properly send emails as I have a
> non-ascii character in my name.
Nice.
> The former version tried to fix-up the real name part with double quotes
> if it includes a '.'.  I removed this as rfc2047 can handle a dot, too.
Not nice. I'd rather use double quotes if rfc2047 is not needed.
It means:
 - no quotes for us-ascii name, without '.'
 - quotes for us-ascii name with '.' and perhaps other forbidden characters
   like '"' or something
 - full rfc2047 quoting if name contains characters outside us-ascii.
-- 
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git
Uwe Kleine-König· Aug 6, 2007, 08:33 UTC · re: Jakub Narebski · lore

Re: [PATCH] send-email: let sanitize_address_rfc822 do rfc2047 quoting

Hello Jakub,
Jakub Narebski wrote:
Show 9 quoted lines
> > The former version tried to fix-up the real name part with double quotes
> > if it includes a '.'.  I removed this as rfc2047 can handle a dot, too.
> 
> Not nice. I'd rather use double quotes if rfc2047 is not needed.
> It means:
>  - no quotes for us-ascii name, without '.'
>  - quotes for us-ascii name with '.' and perhaps other forbidden characters
>    like '"' or something
>  - full rfc2047 quoting if name contains characters outside us-ascii.

Well, that is OK for me, too. I just thought to do just one thing that is able to handle all cases. So the code is easier to read and most of the time you don't see the quoted result anyhow.

I will follow up with a new version later that will leave the second part out. I don't know which chars can be quoted by "...", is it more than .?

Best regards Uwe

-- 
Uwe Kleine-König

http://www.google.com/search?q=1+stone%3D
Uwe Kleine-König· Aug 6, 2007, 20:34 UTC · re: Uwe Kleine-König · lore

[PATCH] send-email: teach sanitize_address to do rfc2047 quoting

Without this patch I'm not able to properly send emails as I have a non-ascii character in my name.

I removed the _rfc822 suffix from the function name as it now does more than rfc822 quoting.

I dug through rfc822 to do the double quoting right. Only if that is not possible rfc2047 quoting is applied.

Signed-off-by: Uwe Kleine-König <ukleinek@informatik.uni-freiburg.de>
Cc: Jakub Narebski <jnareb@gmail.com>
---
As Jakub critizied now the "easiest" quoting is done.
 git-send-email.perl |   39 +++++++++++++++++++++++++++++----------
 1 files changed, 29 insertions(+), 10 deletions(-)
Show changes to git-send-email.perl +29 −10
diff --git a/git-send-email.perl b/git-send-email.perl
index f43f92f..39e433b 100755
--- a/git-send-email.perl
+++ b/git-send-email.perl
@@ -289,7 +289,7 @@ sub expand_aliases {
 }
 
 @to = expand_aliases(@to);
-@to = (map { sanitize_address_rfc822($_) } @to);
+@to = (map { sanitize_address($_) } @to);
 @initial_cc = expand_aliases(@initial_cc);
 @bcclist = expand_aliases(@bcclist);
 
@@ -459,22 +459,41 @@ sub unquote_rfc2047 {
 	return "$_";
 }
 
-# If an address contains a . in the name portion, the name must be quoted.
-sub sanitize_address_rfc822
+# use the simplest quoting being able to handle the recipient
+sub sanitize_address
 {
 	my ($recipient) = @_;
-	my ($recipient_name) = ($recipient =~ /^(.*?)\s+</);
-	if ($recipient_name && $recipient_name =~ /\./ && $recipient_name !~ /^".*"$/) {
-		my ($name, $addr) = ($recipient =~ /^(.*?)(\s+<.*)/);
-		$recipient = "\"$name\"$addr";
+	my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
+
+	if (not $recipient_name) {
+		return "$recipient";
+	}
+
+	# if recipient_name is already quoted, do nothing
+	if ($recipient_name =~ /^(".*"|=\?utf-8\?q\?.*\?=)$/) {
+		return $recipient;
+	}
+
+	# rfc2047 is needed if a non-ascii char is included
+	if ($recipient_name =~ /[^[:ascii:]]/) {
+		$recipient_name =~ s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
+		$recipient_name =~ s/(.*)/=\?utf-8\?q\?$1\?=/;
 	}
-	return $recipient;
+
+	# double quotes are needed if specials or CTLs are included
+	elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
+		$recipient_name =~ s/(["\\\r])/\\$1/;
+		$recipient_name = "\"$recipient_name\"";
+	}
+
+	return "$recipient_name $recipient_addr";
+
 }
 
 sub send_message
 {
 	my @recipients = unique_email_list(@to);
-	@cc = (map { sanitize_address_rfc822($_) } @cc);
+	@cc = (map { sanitize_address($_) } @cc);
 	my $to = join (",\n\t", @recipients);
 	@recipients = unique_email_list(@recipients,@cc,@bcclist);
 	@recipients = (map { extract_valid_address($_) } @recipients);
@@ -489,7 +508,7 @@ sub send_message
 	if ($cc ne '') {
 		$ccline = "\nCc: $cc";
 	}
-	$from = sanitize_address_rfc822($from);
+	$from = sanitize_address($from);
 	make_message_id();
 
 	my $header = "From: $from
-- 
1.5.3.rc3.13.g7ab3
Uwe Kleine-König· Aug 9, 2007, 12:10 UTC · re: Uwe Kleine-König · lore

Re: [PATCH] send-email: teach sanitize_address to do rfc2047 quoting

Hello Junio,

thanks for taking that patch in next (as 5b56aaa29e), but my name got scrambled once again in a strange way.

Please correct me if I'm wrong, but I think this time it happened completely on your side.

The mail I got returned from the list looks good and http://article.gmane.org/gmane.comp.version-control.git/55172 looks right, too.

Strange enough my name is right in your What's in git.git (stable) mail. (http://article.gmane.org/gmane.comp.version-control.git/55221)

I have still another problem with send-email. I will try to come up with a fix before 1.5.3.

Best regards Uwe

-- 
Uwe Kleine-König

exit vi, lesson II:
: w q ! <CR>

NB: write the current file

← back to recent threads