X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=git-send-email.perl;h=96051bc01e1585a69a3e0746bbef7016ec01663e;hb=7ae4dd05725e1613375e03f206077959853d6b51;hp=7552caca4bbbb38b161cfb47f94d4577627ccbb3;hpb=d60a6a662fac58f833efde93c962314fd5d83541;p=git.git diff --git a/git-send-email.perl b/git-send-email.perl index 7552caca4..96051bc01 100755 --- a/git-send-email.perl +++ b/git-send-email.perl @@ -21,8 +21,11 @@ use warnings; use Term::ReadLine; use Getopt::Long; use Data::Dumper; +use Term::ANSIColor; use Git; +$SIG{INT} = sub { print color("reset"), "\n"; exit }; + package FakeTerm; sub new { my ($class, $reason) = @_; @@ -46,11 +49,14 @@ Options: --cc Specify an initial "Cc:" list for the entire series of emails. + --cc-cmd Specify a command to execute per file which adds + per file specific cc address entries + --bcc Specify a list of email addresses that should be Bcc: on all the emails. - --compose Use \$EDITOR to edit an introductory message for the - patch series. + --compose Use \$GIT_EDITOR, core.editor, \$EDITOR, or \$VISUAL to edit + an introductory message for the patch series. --subject Specify the initial "Subject:" line. Only necessary if --compose is also set. If --compose @@ -67,8 +73,20 @@ Options: --signed-off-cc Automatically add email addresses that appear in Signed-off-by: or Cc: lines to the cc: list. Defaults to on. + --identity The configuration identity, a subsection to prioritise over + the default section. + --smtp-server If set, specifies the outgoing SMTP server to use. - Defaults to localhost. + Defaults to localhost. Port number can be specified here with + hostname:port format or by using --smtp-server-port option. + + --smtp-server-port Specify a port on the outgoing SMTP server to connect to. + + --smtp-user The username for SMTP-AUTH. + + --smtp-pass The password for SMTP-AUTH. + + --smtp-ssl If set, connects to the SMTP server using SSL. --suppress-from Suppress sending emails to yourself if your address appears in a From: line. Defaults to off. @@ -137,9 +155,8 @@ my $compose_filename = ".msg.$$"; # Variables we fill in automatically, or via prompting: my (@to,@cc,@initial_cc,@bcclist,@xh, - $initial_reply_to,$initial_subject,@files,$from,$compose,$time); + $initial_reply_to,$initial_subject,@files,$author,$sender,$compose,$time); -my $smtp_server; my $envelope_sender; # Example reply to: @@ -157,29 +174,34 @@ if ($@) { my ($quiet, $dry_run) = (0, 0); # Variables with corresponding config settings -my ($thread, $chain_reply_to, $suppress_from, $signed_off_cc); +my ($thread, $chain_reply_to, $suppress_from, $signed_off_cc, $cc_cmd); +my ($smtp_server, $smtp_server_port, $smtp_authuser, $smtp_authpass, $smtp_ssl); +my ($identity, $aliasfiletype, @alias_files, @smtp_host_parts); -my %config_settings = ( +my %config_bool_settings = ( "thread" => [\$thread, 1], "chainreplyto" => [\$chain_reply_to, 1], "suppressfrom" => [\$suppress_from, 0], "signedoffcc" => [\$signed_off_cc, 1], + "smtpssl" => [\$smtp_ssl, 0], ); -foreach my $setting (keys %config_settings) { - my $config = $repo->config_bool("sendemail.$setting"); - ${$config_settings{$setting}->[0]} = (defined $config) ? $config : $config_settings{$setting}->[1]; -} - -@bcclist = $repo->config('sendemail.bcc'); -if (!@bcclist or !$bcclist[0]) { - @bcclist = (); -} +my %config_settings = ( + "smtpserver" => \$smtp_server, + "smtpserverport" => \$smtp_server_port, + "smtpuser" => \$smtp_authuser, + "smtppass" => \$smtp_authpass, + "to" => \@to, + "cccmd" => \$cc_cmd, + "aliasfiletype" => \$aliasfiletype, + "bcc" => \@bcclist, + "aliasesfile" => \@alias_files, +); # Begin by accumulating all the variables (defined above), that we will end up # needing, first, from the command line: -my $rc = GetOptions("from=s" => \$from, +my $rc = GetOptions("sender|from=s" => \$sender, "in-reply-to=s" => \$initial_reply_to, "subject=s" => \$initial_subject, "to=s" => \@to, @@ -187,8 +209,14 @@ my $rc = GetOptions("from=s" => \$from, "bcc=s" => \@bcclist, "chain-reply-to!" => \$chain_reply_to, "smtp-server=s" => \$smtp_server, + "smtp-server-port=s" => \$smtp_server_port, + "smtp-user=s" => \$smtp_authuser, + "smtp-pass=s" => \$smtp_authpass, + "smtp-ssl!" => \$smtp_ssl, + "identity=s" => \$identity, "compose" => \$compose, "quiet" => \$quiet, + "cc-cmd=s" => \$cc_cmd, "suppress-from!" => \$suppress_from, "signed-off-cc|signed-off-by-cc!" => \$signed_off_cc, "dry-run" => \$dry_run, @@ -200,6 +228,43 @@ unless ($rc) { usage(); } +# Now, let's fill any that aren't set in with defaults: + +sub read_config { + my ($prefix) = @_; + + foreach my $setting (keys %config_bool_settings) { + my $target = $config_bool_settings{$setting}->[0]; + $$target = $repo->config_bool("$prefix.$setting") unless (defined $$target); + } + + foreach my $setting (keys %config_settings) { + my $target = $config_settings{$setting}; + if (ref($target) eq "ARRAY") { + unless (@$target) { + my @values = $repo->config("$prefix.$setting"); + @$target = @values if (@values && defined $values[0]); + } + } + else { + $$target = $repo->config("$prefix.$setting") unless (defined $$target); + } + } +} + +# read configuration from [sendemail "$identity"], fall back on [sendemail] +$identity = $repo->config("sendemail.identity") unless (defined $identity); +read_config("sendemail.$identity") if (defined $identity); +read_config("sendemail"); + +# fall back on builtin bool defaults +foreach my $setting (values %config_bool_settings) { + ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]})); +} + +my ($repoauthor) = $repo->ident_person('author'); +my ($repocommitter) = $repo->ident_person('committer'); + # Verify the user input foreach my $entry (@to) { @@ -214,14 +279,7 @@ foreach my $entry (@bcclist) { die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/; } -# Now, let's fill any that aren't set in with defaults: - -my ($author) = $repo->ident_person('author'); -my ($committer) = $repo->ident_person('committer'); - my %aliases; -my @alias_files = $repo->config('sendemail.aliasesfile'); -my $aliasfiletype = $repo->config('sendemail.aliasfiletype'); my %parse_alias = ( # multiline formats can be supported in the future mutt => sub { my $fh = shift; while (<$fh>) { @@ -237,7 +295,7 @@ my %parse_alias = ( $aliases{$1} = [ split(/\s+/, $2) ]; }}}, pine => sub { my $fh = shift; while (<$fh>) { - if (/^(\S+)\s+(.*)$/) { + if (/^(\S+)\t.*\t(.*)$/) { $aliases{$1} = [ split(/\s*,\s*/, $2) ]; }}}, gnus => sub { my $fh = shift; while (<$fh>) { @@ -254,17 +312,17 @@ if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) { } } -($from) = expand_aliases($from) if defined $from; +($sender) = expand_aliases($sender) if defined $sender; my $prompting = 0; -if (!defined $from) { - $from = $author || $committer; +if (!defined $sender) { + $sender = $repoauthor || $repocommitter; do { - $_ = $term->readline("Who should the emails appear to be from? [$from] "); + $_ = $term->readline("Who should the emails appear to be from? [$sender] "); } while (!defined $_); - $from = $_ if ($_); - print "Emails will be sent from: ", $from, "\n"; + $sender = $_ if ($_); + print "Emails will be sent from: ", $sender, "\n"; $prompting++; } @@ -289,7 +347,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); @@ -309,13 +367,11 @@ if ($thread && !defined $initial_reply_to && $prompting) { } while (!defined $_); $initial_reply_to = $_; - $initial_reply_to =~ s/(^\s+|\s+$)//g; + $initial_reply_to =~ s/^\s+?\s+$/>/; } -if (!$smtp_server) { - $smtp_server = $repo->config('sendemail.smtpserver'); -} -if (!$smtp_server) { +if (!defined $smtp_server) { foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) { if (-x $_) { $smtp_server = $_; @@ -330,7 +386,7 @@ if ($compose) { # effort to have it be unique open(C,">",$compose_filename) or die "Failed to open for writing $compose_filename: $!"; - print C "From $from # This line is ignored.\n"; + print C "From $sender # This line is ignored.\n"; printf C "Subject: %s\n\n", $initial_subject; printf C <config("core.editor") || $ENV{VISUAL} || $ENV{EDITOR} || "vi"; system($editor, $compose_filename); open(C2,">",$compose_filename . ".final") @@ -409,8 +464,8 @@ sub extract_valid_address { # check for a local address: return $address if ($address =~ /^($local_part_regexp)$/); + $address =~ s/^\s*<(.*)>\s*$/$1/; if ($have_email_valid) { - $address =~ s/^\s*<(.*)>\s*$/$1/; return scalar Email::Valid->address($address); } else { # less robust/correct than the monster regexp in Email::Valid, @@ -429,21 +484,28 @@ sub extract_valid_address { # We'll setup a template for the message id, using the "from" address: +my ($message_id_stamp, $message_id_serial); sub make_message_id { - my $date = time; - my $pseudo_rand = int (rand(4200)); + my $uniq; + if (!defined $message_id_stamp) { + $message_id_stamp = sprintf("%s-%s", time, $$); + $message_id_serial = 0; + } + $message_id_serial++; + $uniq = "$message_id_stamp-$message_id_serial"; + my $du_part; - for ($from, $committer, $author) { - $du_part = extract_valid_address($_); - last if ($du_part ne ''); + for ($sender, $repocommitter, $repoauthor) { + $du_part = extract_valid_address(sanitize_address($_)); + last if (defined $du_part and $du_part ne ''); } - if ($du_part eq '') { + if (not defined $du_part or $du_part eq '') { use Sys::Hostname qw(); $du_part = 'user@' . Sys::Hostname::hostname(); } - my $message_id_template = "<%s-git-send-email-$du_part>"; - $message_id = sprintf $message_id_template, "$date$pseudo_rand"; + my $message_id_template = "<%s-git-send-email-%s>"; + $message_id = sprintf($message_id_template, $uniq, $du_part); #print "new message id = $message_id\n"; # Was useful for debugging } @@ -460,22 +522,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+@,;:\\".\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); @@ -490,10 +571,10 @@ sub send_message if ($cc ne '') { $ccline = "\nCc: $cc"; } - $from = sanitize_address_rfc822($from); + my $sanitized_sender = sanitize_address($sender); make_message_id(); - my $header = "From: $from + my $header = "From: $sanitized_sender To: $to${ccline} Subject: $subject Date: $date @@ -510,7 +591,7 @@ X-Mailer: git-send-email $gitversion } my @sendmail_parameters = ('-i', @recipients); - my $raw_from = $from; + my $raw_from = $sanitized_sender; $raw_from = $envelope_sender if (defined $envelope_sender); $raw_from = extract_valid_address($raw_from); unshift (@sendmail_parameters, @@ -527,8 +608,30 @@ X-Mailer: git-send-email $gitversion print $sm "$header\n$message"; close $sm or die $?; } else { - require Net::SMTP; - $smtp ||= Net::SMTP->new( $smtp_server ); + + if (!defined $smtp_server) { + die "The required SMTP server is not properly defined." + } + + if ($smtp_ssl) { + $smtp_server_port ||= 465; # ssmtp + require Net::SMTP::SSL; + $smtp ||= Net::SMTP::SSL->new($smtp_server, Port => $smtp_server_port); + } + else { + require Net::SMTP; + $smtp ||= Net::SMTP->new((defined $smtp_server_port) + ? "$smtp_server:$smtp_server_port" + : $smtp_server); + } + + if (!$smtp) { + die "Unable to initialize SMTP properly. Is there something wrong with your config?"; + } + + if ((defined $smtp_authuser) && (defined $smtp_authpass)) { + $smtp->auth( $smtp_authuser, $smtp_authpass ) or die $smtp->message; + } $smtp->mail( $raw_from ) or die $smtp->message; $smtp->to( @recipients ) or die $smtp->message; $smtp->data or die $smtp->message; @@ -547,7 +650,7 @@ X-Mailer: git-send-email $gitversion } else { print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n"; } - print "From: $from\nSubject: $subject\nCc: $cc\nTo: $to\n\n"; + print "From: $sanitized_sender\nSubject: $subject\nCc: $cc\nTo: $to\n\n"; if ($smtp) { print "Result: ", $smtp->code, ' ', ($smtp->message =~ /\n([^\n]+\n)$/s), "\n"; @@ -564,7 +667,7 @@ $subject = $initial_subject; foreach my $t (@files) { open(F,"<",$t) or die "can't open file $t"; - my $author_not_sender = undef; + my $author = undef; @cc = @initial_cc; @xh = (); my $input_format = undef; @@ -586,12 +689,11 @@ foreach my $t (@files) { $subject = $1; } elsif (/^(Cc|From):\s+(.*)$/) { - if (unquote_rfc2047($2) eq $from) { - $from = $2; + if (unquote_rfc2047($2) eq $sender) { next if ($suppress_from); } elsif ($1 eq 'From') { - $author_not_sender = $2; + $author = unquote_rfc2047($2); } printf("(mbox) Adding cc: %s from line '%s'\n", $2, $_) unless $quiet; @@ -635,11 +737,25 @@ foreach my $t (@files) { } } close F; - if (defined $author_not_sender) { - $author_not_sender = unquote_rfc2047($author_not_sender); - $message = "From: $author_not_sender\n\n$message"; + + if (defined $cc_cmd) { + open(F, "$cc_cmd $t |") + or die "(cc-cmd) Could not execute '$cc_cmd'"; + while() { + my $c = $_; + $c =~ s/^\s*//g; + $c =~ s/\n$//g; + push @cc, $c; + printf("(cc-cmd) Adding cc: %s from: '%s'\n", + $c, $cc_cmd) unless $quiet; + } + close F + or die "(cc-cmd) failed to close pipe to '$cc_cmd'"; } + if (defined $author) { + $message = "From: $author\n\n$message"; + } send_message();