send-email: --smtp-domain improvements
cover send-email: --smtp-domain improvements
The first patch keeps send-email from using a bogus FQDN for my laptop. Oddly enough, my MTA doesn't mind "localhost.localdomain" but complains about "My-Computer".
The second adds documentation for --smtp-domain to the git-send-email man page. The only documentation for it was in the code and the usage message.
The last is just an enhancement, but I was honestly surprised that --smtp-domain didn't have a configuration.
Brian Gernhardt (3):
send-email: Don't use FQDNs without a '.'
Document send-email --smtp-domain
send-email: Add sendemail.smtpdomain
Documentation/git-send-email.txt | 7 +++++++ git-send-email.perl | 26 ++++++++++++++------------ 2 files changed, 21 insertions(+), 12 deletions(-)
PATCH 1/3 send-email: Don't use FQDNs without a '.'
Although Net::Domain::domainname attempts to be very thorough, the host's configuration can still refuse to give a FQDN. Check to see if what we receive contains a dot as a basic sanity check.
---
My OS X machine doesn't add ".local" to it's hostname for some reason. Since a FQDN requires at least one . between the TLD and hostname, we can check for it to avoid nonsense results like "My-Computer".
git-send-email.perl | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-)
git-send-email.perl +4 -2
@@ -870,7 +870,8 @@ sub maildomain_net if (eval { require Net::Domain; 1 }) { my $domain = Net::Domain::domainname(); $maildomain = $domain - unless $^O eq 'darwin' && $domain =~ /\.local$/; + unless $^O eq 'darwin' && $domain =~ /\.local$/ + or $domain !~ /\./; } return $maildomain;
@@ -888,7 +889,8 @@ sub maildomain_mta $smtp->quit; $maildomain = $domain - unless $^O eq 'darwin' && $domain =~ /\.local$/; + unless $^O eq 'darwin' && $domain =~ /\.local$/ + or $domain !~ /\./; last if $maildomain; }
Signature · 1 line
1.7.1.rc0.210.ge6da
Re: [PATCH 1/3] send-email: Don't use FQDNs without a '.'
git-send-email.perl sub maildomain_net · line 874
Quoted 4 lines · Brian Gernhardt
git-send-email.perl +2
-1$maildomain = $domain - unless $^O eq 'darwin' && $domain =~ /\.local$/; + unless $^O eq 'darwin' && $domain =~ /\.local$/ + or $domain !~ /\./;
It would become *much* easier to read if we stop using the statement modifier, and write it in a more straightforward way:
unless (($^O eq 'darwin' && $domain =~ /\.local$/)
|| $domain !~ /\./) {
$maildomain = $domain;
}as the condition seems to have grown large enough to exceed "by the way don't do this under this narrow condition", which is what statement modifiers are designed to be used. Also mixing && and or that have different precedence taxes readers' brainpower unnecessarily.
I also think that this particular exception logic should probably be in a separate helper function that is called from the two places.
Jari, you are the last person who touched code around this area. What do you think?
PATCH 2/3 Document send-email --smtp-domain
In reply to Brian Gernhardt---
--smtp-debug is still undocumented. It also appears to be a boolean that takes 0 or 1 instead of being of the form --[no-]smtp-debug.
Documentation/git-send-email.txt | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-)
Documentation/git-send-email.txt +6 -0
@@ -119,6 +119,12 @@ Sending value reverts to plain SMTP. Default is the value of 'sendemail.smtpencryption'. +--smtp-domain=<FQDN>:: + Specifies the Fully Qualified Domain Name (FQDN) used in the + HELO/EHLO command to the SMTP server. Some servers require the + FQDN to match your IP address. If not set, git send-email attempts + to determine your FQDN automatically. + --smtp-pass[=<password>]:: Password for SMTP-AUTH. The argument is optional: If no argument is specified, then the empty string is used as
Signature · 1 line
1.7.1.rc0.210.ge6da
PATCH 3/3 send-email: Add sendemail.smtpdomain
In reply to Brian Gernhardt--smtp-domain is an option that if you need once, you probably will need again. To help with that, allow the user to set it in their .gitconfig
---
Seemed like a natural thing to add. Was surprised it wasn't there when I went poking around.
Documentation/git-send-email.txt | 3 ++- git-send-email.perl | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-)
Documentation/git-send-email.txt +2 -1
@@ -123,7 +123,8 @@ Sending Specifies the Fully Qualified Domain Name (FQDN) used in the HELO/EHLO command to the SMTP server. Some servers require the FQDN to match your IP address. If not set, git send-email attempts - to determine your FQDN automatically. + to determine your FQDN automatically. Default is the value of + 'sendemail.smtpdomain'. --smtp-pass[=<password>]:: Password for SMTP-AUTH. The argument is optional: If no
git-send-email.perl +10 -10
@@ -132,8 +132,7 @@ my $have_email_valid = eval { require Email::Valid; 1 }; my $have_mail_address = eval { require Mail::Address; 1 }; my $smtp; my $auth; -my $mail_domain_default = "localhost.localdomain"; -my $mail_domain; +my $smtp_domain_default = "localhost.localdomain"; sub unique_email_list(@); sub cleanup_compose_files();
@@ -190,7 +189,7 @@ sub do_edit { # Variables with corresponding config settings my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc, $cc_cmd); my ($smtp_server, $smtp_server_port, $smtp_authuser, $smtp_encryption); -my ($identity, $aliasfiletype, @alias_files, @smtp_host_parts); +my ($identity, $aliasfiletype, @alias_files, @smtp_host_parts, $smtp_domain); my ($validate, $confirm); my (@suppress_cc);
@@ -212,6 +211,7 @@ my %config_settings = ( "smtpserverport" => \$smtp_server_port, "smtpuser" => \$smtp_authuser, "smtppass" => \$smtp_authpass, + "smtpdomain" => \$smtp_domain, "to" => \@to, "cc" => \@initial_cc, "cccmd" => \$cc_cmd,
@@ -283,7 +283,7 @@ my $rc = GetOptions("sender|from=s" => \$sender, "smtp-ssl" => sub { $smtp_encryption = 'ssl' }, "smtp-encryption=s" => \$smtp_encryption, "smtp-debug:i" => \$debug_net_smtp, - "smtp-domain:s" => \$mail_domain, + "smtp-domain:s" => \$smtp_domain, "identity=s" => \$identity, "annotate" => \$annotate, "compose" => \$compose,
@@ -902,7 +902,7 @@ sub maildomain_mta sub maildomain { - return maildomain_net() || maildomain_mta() || $mail_domain_default; + return maildomain_net() || maildomain_mta() || $smtp_domain_default; } # Returns 1 if the message was sent, and 0 otherwise.
@@ -1007,18 +1007,18 @@ X-Mailer: git-send-email $gitversion if ($smtp_encryption eq 'ssl') { $smtp_server_port ||= 465; # ssmtp require Net::SMTP::SSL; - $mail_domain ||= maildomain(); + $smtp_domain ||= maildomain(); $smtp ||= Net::SMTP::SSL->new($smtp_server, - Hello => $mail_domain, + Hello => $smtp_domain, Port => $smtp_server_port); } else { require Net::SMTP; - $mail_domain ||= maildomain(); + $smtp_domain ||= maildomain(); $smtp ||= Net::SMTP->new((defined $smtp_server_port) ? "$smtp_server:$smtp_server_port" : $smtp_server, - Hello => $mail_domain, + Hello => $smtp_domain, Debug => $debug_net_smtp); if ($smtp_encryption eq 'tls' && $smtp) { require Net::SMTP::SSL;
@@ -1041,7 +1041,7 @@ X-Mailer: git-send-email $gitversion die "Unable to initialize SMTP properly. Check config and use --smtp-debug. ", "VALUES: server=$smtp_server ", "encryption=$smtp_encryption ", - "maildomain=$mail_domain", + "maildomain=$smtp_domain", defined $smtp_server_port ? "port=$smtp_server_port" : ""; }
Signature · 1 line
1.7.1.rc0.210.ge6da
Re: [PATCH 3/3] send-email: Add sendemail.smtpdomain
Quoted 3 lines · Brian Gernhardt
--smtp-domain is an option that if you need once, you probably will need again. To help with that, allow the user to set it in their .gitconfig
via 'sendemail.smtpdomain' ?
Quoted 10 lines
Signed-off-by Brian Gernhardt <[email protected]>---
Seemed like a natural thing to add. Was surprised it wasn't there when I went poking around.
Documentation/git-send-email.txt | 3 ++- git-send-email.perl | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-)
You should also add one-liner to Documentation/config.txt, mentioning 'sendemail.smtpdomain' configuration variable (the part referring reader to git-send-email(1) documentation).
P.S. Some of git-send-email configuration variables: sendemail.identity, sendemail.smtpencryption, sendemail.smtpssl (deprecated), and the concept of sendemail.<identity>.* are described in Documentation/config.txt, which means that they are present in git-config(1) manpage. Some of them are described in "Configuration" section of Documentation/git-send-email.txt (which means git-send-email(1) manpage): sendemail.aliasesfile, sendemail.aliasfiletype, sendemail.multiedit, sendemail.confirm; I think those are config variables which do not have corresponding command line option. The rest: sendemail.bcc, sendemail.cc, sendemail.from, sendemail.to, sendemail.envelopesender, sendemail.smtpencryption (which is described in git-config(1)), sendemail.smtppass, sendemail.smtpserver, sendemail.smtpserverport, sendemail.smtpuser, sendemail.cccmd, sendemail.chainreplyto, sendemail.identity and 'sendemail.<identity>' subsection (described in git-config(1)), sendemail.signedoffbycc, sendemail.suppresscc, sendemail.suppressfrom, sendemail.thread, and sendemail.validate are described only with corresponding command-line option.
Huh...
Signature · 3 lines
Jakub Narebski Poland ShadeHawk on #git
PATCH 1/1 git-send-email.perl: Add sub maildomain_sanitize
In reply to Junio C HamanoMove duplicate maildomain checks to a single subroutine. Require that a FQDN contains at least one period.
--- git-send-email.perl | 21 +++++++++++++++++---- 1 files changed, 17 insertions(+), 4 deletions(-)
Here is improved version.
-- Jari
>Re: [PATCH 1/3] send-email: Don't use FQDNs without a '.'
>Junio C Hamano <[email protected]> writes:
>> Brian Gernhardt <[email protected]> writes:
>
>> $maildomain = $domain
>> - unless $^O eq 'darwin' && $domain =~ /\.local$/;
>> + unless $^O eq 'darwin' && $domain =~ /\.local$/
>> + or $domain !~ /\./;
>
> [Junio] It would become *much* easier to read if we stop using the statement
> modifier, and write it in a more straightforward way:
>
> unless (($^O eq 'darwin' && $domain =~ /\.local$/)
> || $domain !~ /\./) {
> $maildomain = $domain;
> }
>
> as the condition seems to have grown large enough to exceed "by the way
> don't do this under this narrow condition", which is what statement
> modifiers are designed to be used. Also mixing && and or that have
> different precedence taxes readers' brainpower unnecessarily.
>
> I also think that this particular exception logic should probably be in a
> separate helper function that is called from the two places.
>
> Jari, you are the last person who touched code around this area. What do
> you think?git-send-email.perl +17 -4
@@ -863,14 +863,28 @@ sub sanitize_address # This maildomain*() code is based on ideas in Perl library Test::Reporter # /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain () +sub maildomain_sanitize +{ + local $_ = shift; + + # On Mac, the the domain name is not necessarily in FQDN format + # Require a period in the string + + if ( $^O eq 'darwin' and /\.local$/ ) { + # Nope, pass this one. + } + elsif ( /\./ ) { + $_; + } +} + sub maildomain_net { my $maildomain; if (eval { require Net::Domain; 1 }) { my $domain = Net::Domain::domainname(); - $maildomain = $domain - unless $^O eq 'darwin' && $domain =~ /\.local$/; + $maildomain = maildomain_sanitize($domain); } return $maildomain;
@@ -887,8 +901,7 @@ sub maildomain_mta my $domain = $smtp->domain; $smtp->quit; - $maildomain = $domain - unless $^O eq 'darwin' && $domain =~ /\.local$/; + $maildomain = maildomain_sanitize($domain); last if $maildomain; }
Signature · 1 line
1.7.0
PATCH 1/1 git-send-email.perl: add maildomain_sanitize()
Move duplicate maildomain checks to a single subroutine. Require that a FQDN contains at least one period.
--- git-send-email.perl | 21 +++++++++++++++++---- 1 files changed, 17 insertions(+), 4 deletions(-)
Here is improved version 2 (use "tab" for indentation).
-- Jari
>Re: [PATCH 1/3] send-email: Don't use FQDNs without a '.'
>Junio C Hamano <[email protected]> writes:
>> Brian Gernhardt <[email protected]> writes:
>
>> $maildomain = $domain
>> - unless $^O eq 'darwin' && $domain =~ /\.local$/;
>> + unless $^O eq 'darwin' && $domain =~ /\.local$/
>> + or $domain !~ /\./;
>
> [Junio] It would become *much* easier to read if we stop using the statement
> modifier, and write it in a more straightforward way:
>
> unless (($^O eq 'darwin' && $domain =~ /\.local$/)
> || $domain !~ /\./) {
> $maildomain = $domain;
> }
>
> as the condition seems to have grown large enough to exceed "by the way
> don't do this under this narrow condition", which is what statement
> modifiers are designed to be used. Also mixing && and or that have
> different precedence taxes readers' brainpower unnecessarily.
>
> I also think that this particular exception logic should probably be in a
> separate helper function that is called from the two places.
>
> Jari, you are the last person who touched code around this area. What do
> you think?git-send-email.perl +17 -4
@@ -863,14 +863,28 @@ sub sanitize_address # This maildomain*() code is based on ideas in Perl library Test::Reporter # /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain () +sub maildomain_sanitize +{ + local $_ = shift; + + # On Mac, the the domain name is not necessarily in FQDN format + # Require a period in the string + + if ( $^O eq 'darwin' and /\.local$/ ) { + # Nope, pass this one. + } + elsif ( /\./ ) { + $_; + } +} + sub maildomain_net { my $maildomain; if (eval { require Net::Domain; 1 }) { my $domain = Net::Domain::domainname(); - $maildomain = $domain - unless $^O eq 'darwin' && $domain =~ /\.local$/; + $maildomain = maildomain_sanitize($domain); } return $maildomain;
@@ -887,8 +901,7 @@ sub maildomain_mta my $domain = $smtp->domain; $smtp->quit; - $maildomain = $domain - unless $^O eq 'darwin' && $domain =~ /\.local$/; + $maildomain = maildomain_sanitize($domain); last if $maildomain; }
Signature · 1 line
1.7.0
Re: [PATCH] git-send-email.perl: add maildomain_sanitize()
Quoted 5 lines · Jari Aalto
Move duplicate maildomain checks to a single subroutine. Require that a FQDN contains at least one period.
Signed-off-by Jari Aalto <[email protected]>---
Good idea.
[...]
Quoted 12 lines
git-send-email.perl +4
-0@@ -863,14 +863,28 @@ sub sanitize_address # This maildomain*() code is based on ideas in Perl library Test::Reporter # /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain () +sub maildomain_sanitize +{ + local $_ = shift; +
git-send-email.perl sub sanitize_address · line 879
Quoted 10 lines
git-send-email.perl +10
-0+ # On Mac, the the domain name is not necessarily in FQDN format + # Require a period in the string + + if ( $^O eq 'darwin' and /\.local$/ ) { + # Nope, pass this one. + } + elsif ( /\./ ) { + $_; + } +}
Style:
+sub maildomain_sanitize {
+ local $domain = shift;
+
+ # On Mac, the the domain name is not necessarily in FQDN format
+ # Require a period in the string
+
+ if ($^O eq 'darwin' && $domain =~ /\.local$/) {
+ # Nope, pass this one.
+ } elsif ($domain =~ /\./) {
+ return $domain;
+ }
+}(probably fixed in new version of this series).
Signature · 3 lines
Jakub Narebski Poland ShadeHawk on #git
PATCH v2 1/1 git-send-email.perl: Add sub maildomain_sanitize
Move duplicate maildomain checks to a single subroutine. Require that a FQDN contains at least one period.
--- git-send-email.perl | 19 +++++++++++++++---- 1 files changed, 15 insertions(+), 4 deletions(-)
Jakub Narebski <[email protected]> writes:
> +sub maildomain_sanitize {
> + local $domain = shift;
> +
> + # On Mac, the the domain name is not necessarily in FQDN format
> + # Require a period in the string
> +
> + if ($^O eq 'darwin' && $domain =~ /\.local$/) {
> + # Nope, pass this one.
> + } elsif ($domain =~ /\./) {
> + return $domain;
> + }
> +}
Thanks for 2nd eye. Changed these in above:
* Starting brace placement at function start
* Placement of else
* Maybe: use or "return" (debatable).
But not these. Motivation:
* The "$_" simplifies ussage everywhere in the function. XP: less is more.
* The "and" is more readable than "&&". The "and" is also safer due to its lower precedence.
Jari
git-send-email.perl +15 -4
@@ -863,14 +863,26 @@ sub sanitize_address # This maildomain*() code is based on ideas in Perl library Test::Reporter # /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain () +sub maildomain_sanitize { + local $_ = shift; + + # On Mac, the the domain name is not necessarily in FQDN format. + # Require a period in the string. + + if ( $^O eq 'darwin' and /\.local$/ ) { + # Nope, pass this one. + } elsif ( /\./ ) { + return $_; + } +} + sub maildomain_net { my $maildomain; if (eval { require Net::Domain; 1 }) { my $domain = Net::Domain::domainname(); - $maildomain = $domain - unless $^O eq 'darwin' && $domain =~ /\.local$/; + $maildomain = maildomain_sanitize($domain); } return $maildomain;
@@ -887,8 +899,7 @@ sub maildomain_mta my $domain = $smtp->domain; $smtp->quit; - $maildomain = $domain - unless $^O eq 'darwin' && $domain =~ /\.local$/; + $maildomain = maildomain_sanitize($domain); last if $maildomain; }
Signature · 1 line
1.7.0
Re: [PATCH v2] git-send-email.perl: Add sub maildomain_sanitize
Quoted 20 lines · Jari Aalto
Jakub Narebski <[email protected]> writes:
> +submaildomain_sanitize{
> + local $domain = shift;
> +
> + # On Mac, the the domain name is not necessarily in FQDN format
> + # Require a period in the string
> +
> + if ($^O eq 'darwin' && $domain =~ /\.local$/) {
> + # Nope, pass this one.
> + } elsif ($domain =~ /\./) {
> + return $domain;
> + }
> +}Thanks for 2nd eye. Changed these in above:
* Starting brace placement at function start
* Placement of else
* Maybe: use or "return" (debatable).
Quoted 5 lines
But not these. Motivation:
* The "$_" simplifies usage everywhere in the function. XP: less is more.
* The "and" is more readable than "&&". The "and" is also safer due to its lower precedence.
O.K.
Note however that while setting $_ ($ARG with English) simplifies regexp matching, you have to take care to use
local $_ = shift;
and not
my $_ = shift;
And to use 'local'.
A bit of "difficulty conservation" at work.
[...]
Quoted 12 lines
git-send-email.perl +12
-0+sub maildomain_sanitize { + local $_ = shift; + + # On Mac, the the domain name is not necessarily in FQDN format. + # Require a period in the string. + + if ( $^O eq 'darwin' and /\.local$/ ) { + # Nope, pass this one. + } elsif ( /\./ ) { + return $_; + } +}
Signature · 2 lines
Jakub Narebski Poland
Re: Start encouraging English.pm (Was: [PATCH v2] git-send-email.perl: Add sub maildomain_sanitize)
Quoted 5 lines · Jakub Narebski
O.K.
Note however that while setting $_ ($ARG with English) simplifies regexp matching
Speaking of which...
Could we please move to using this everywhere:
use English qw( -no_match_vars );
That would give all the benefits of readability without performance penalties. The load time seems to be neglible:
$ perl --version | grep v5
This is perl, v5.10.1 (*) built for x86_64-linux-gnu-thread-multi
$ time perl -e 'print'
real 0m0.005s
user 0m0.000s
sys 0m0.004s
$ time perl -e 'use English qw( -no_match_vars ); print'
real 0m0.017s
user 0m0.008s
sys 0m0.008sQuoted 7 lines
you have to take care to use
git-send-email.perl sub sanitize_address · line 867
Quoted 2 lines
git-send-email.perl +2
-0+sub maildomain_sanitize { + local $_ = shift;local $_ = shift;
Quoted 5 lines
and not
my $_ = shift;And to use 'local'.
As done.
The Perl 5.12 now finally allow my'ing of $ARG too. But maybe we don't switch to it just yet...
Jari