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) = @_;
}
package main;
+
+sub usage {
+ print <<EOT;
+git-send-email [options] <file | directory>...
+Options:
+ --from Specify the "From:" line of the email to be sent.
+
+ --to Specify the primary "To:" line of the email.
+
+ --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 \$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
+ is not set, this will be prompted for.
+
+ --in-reply-to Specify the first "In-Reply-To:" header line.
+ Only used if --compose is also set. If --compose is not
+ set, this will be prompted for.
+
+ --chain-reply-to If set, the replies will all be to the previous
+ email sent, rather than to the first email sent.
+ Defaults to on.
+
+ --signed-off-cc Automatically add email addresses that appear in
+ Signed-off-by: or Cc: lines to the cc: list. Defaults to on.
+
+ --smtp-server If set, specifies the outgoing SMTP server to use.
+ Defaults to localhost.
+
+ --suppress-from Suppress sending emails to yourself if your address
+ appears in a From: line. Defaults to off.
+
+ --thread Specify that the "In-Reply-To:" header should be set on all
+ emails. Defaults to on.
+
+ --quiet Make git-send-email less verbose. One line per email
+ should be all that is output.
+
+ --dry-run Do everything except actually send the emails.
+
+ --envelope-sender Specify the envelope sender used to send the emails.
+
+EOT
+ exit(1);
+}
+
# most mail servers generate the Date: header, but not all...
sub format_2822_time {
my ($time) = @_;
# 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);
-# Behavior modification variables
-my ($chain_reply_to, $quiet, $suppress_from, $no_signed_off_cc,
- $dry_run) = (1, 0, 0, 0, 0);
my $smtp_server;
+my $envelope_sender;
# Example reply to:
#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
$term = new FakeTerm "$@: going non-interactive";
}
+# Behavior modification variables
+my ($quiet, $dry_run) = (0, 0);
+
+# Variables with corresponding config settings
+my ($thread, $chain_reply_to, $suppress_from, $signed_off_cc, $cc_cmd);
+
+my %config_settings = (
+ "thread" => [\$thread, 1],
+ "chainreplyto" => [\$chain_reply_to, 1],
+ "suppressfrom" => [\$suppress_from, 0],
+ "signedoffcc" => [\$signed_off_cc, 1],
+ "cccmd" => [\$cc_cmd, ""],
+);
+
+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 = ();
+}
+
# 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,
"smtp-server=s" => \$smtp_server,
"compose" => \$compose,
"quiet" => \$quiet,
- "suppress-from" => \$suppress_from,
- "no-signed-off-cc|no-signed-off-by-cc" => \$no_signed_off_cc,
+ "cc-cmd=s" => \$cc_cmd,
+ "suppress-from!" => \$suppress_from,
+ "signed-off-cc|signed-off-by-cc!" => \$signed_off_cc,
"dry-run" => \$dry_run,
+ "envelope-sender=s" => \$envelope_sender,
+ "thread!" => \$thread,
);
+unless ($rc) {
+ usage();
+}
+
# Verify the user input
foreach my $entry (@to) {
# 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 ($repoauthor) = $repo->ident_person('author');
+my ($repocommitter) = $repo->ident_person('committer');
my %aliases;
my @alias_files = $repo->config('sendemail.aliasesfile');
my %parse_alias = (
# multiline formats can be supported in the future
mutt => sub { my $fh = shift; while (<$fh>) {
- if (/^alias\s+(\S+)\s+(.*)$/) {
+ if (/^\s*alias\s+(\S+)\s+(.*)$/) {
my ($alias, $addr) = ($1, $2);
$addr =~ s/#.*$//; # mutt allows # comments
# commas delimit multiple addresses
$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>) {
}
}
+($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++;
}
}
@to = expand_aliases(@to);
+@to = (map { sanitize_address($_) } @to);
@initial_cc = expand_aliases(@initial_cc);
@bcclist = expand_aliases(@bcclist);
$prompting++;
}
-if (!defined $initial_reply_to && $prompting) {
+if ($thread && !defined $initial_reply_to && $prompting) {
do {
$_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
$initial_reply_to);
} while (!defined $_);
$initial_reply_to = $_;
- $initial_reply_to =~ s/(^\s+|\s+$)//g;
+ $initial_reply_to =~ s/^\s+<?/</;
+ $initial_reply_to =~ s/>?\s+$/>/;
}
if (!$smtp_server) {
# 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 <<EOT;
GIT: Please enter your email below.
EOT
close(C);
- my $editor = $ENV{EDITOR};
- $editor = 'vi' unless defined $editor;
+ my $editor = $ENV{GIT_EDITOR} || $repo->config("core.editor") || $ENV{VISUAL} || $ENV{EDITOR} || "vi";
system($editor, $compose_filename);
open(C2,">",$compose_filename . ".final")
print $_,"\n" for (@files);
}
} else {
- print <<EOT;
-git-send-email [options] <file | directory> [... file | directory ]
-Options:
- --from Specify the "From:" line of the email to be sent.
-
- --to Specify the primary "To:" line of the email.
-
- --cc Specify an initial "Cc:" list for the entire series
- of emails.
-
- --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.
-
- --subject Specify the initial "Subject:" line.
- Only necessary if --compose is also set. If --compose
- is not set, this will be prompted for.
-
- --in-reply-to Specify the first "In-Reply-To:" header line.
- Only used if --compose is also set. If --compose is not
- set, this will be prompted for.
-
- --chain-reply-to If set, the replies will all be to the previous
- email sent, rather than to the first email sent.
- Defaults to on.
-
- --no-signed-off-cc Suppress the automatic addition of email addresses
- that appear in a Signed-off-by: line, to the cc: list.
- Note: Using this option is not recommended.
-
- --smtp-server If set, specifies the outgoing SMTP server to use.
- Defaults to localhost.
-
- --suppress-from Suppress sending emails to yourself if your address
- appears in a From: line.
-
- --quiet Make git-send-email less verbose. One line per email should be
- all that is output.
-
-Error: Please specify a file or a directory on the command line.
-EOT
- exit(1);
+ print STDERR "\nNo patch files specified!\n\n";
+ usage();
}
# Variables we set as part of the loop over files
-our ($message_id, $cc, %mail, $subject, $reply_to, $references, $message);
+our ($message_id, %mail, $subject, $reply_to, $references, $message);
sub extract_valid_address {
my $address = shift;
# check for a local address:
return $address if ($address =~ /^($local_part_regexp)$/);
+ $address =~ s/^\s*<(.*)>\s*$/$1/;
if ($have_email_valid) {
return scalar Email::Valid->address($address);
} else {
# 1 second since the last time we were called.
# We'll setup a template for the message id, using the "from" address:
-my $message_id_from = extract_valid_address($from);
-my $message_id_template = "<%s-git-send-email-$message_id_from>";
+my ($message_id_stamp, $message_id_serial);
sub make_message_id
{
- my $date = time;
- my $pseudo_rand = int (rand(4200));
- $message_id = sprintf $message_id_template, "$date$pseudo_rand";
+ 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 ($sender, $repocommitter, $repoauthor) {
+ $du_part = extract_valid_address(sanitize_address($_));
+ last if (defined $du_part and $du_part ne '');
+ }
+ 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-%s>";
+ $message_id = sprintf($message_id_template, $uniq, $du_part);
#print "new message id = $message_id\n"; # Was useful for debugging
}
-$cc = "";
$time = time - scalar $#files;
sub unquote_rfc2047 {
return "$_";
}
+# use the simplest quoting being able to handle the recipient
+sub sanitize_address
+{
+ my ($recipient) = @_;
+ 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\?=/;
+ }
+
+ # 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($_) } @cc);
my $to = join (",\n\t", @recipients);
@recipients = unique_email_list(@recipients,@cc,@bcclist);
+ @recipients = (map { extract_valid_address($_) } @recipients);
my $date = format_2822_time($time++);
my $gitversion = '@@GIT_VERSION@@';
if ($gitversion =~ m/..GIT_VERSION../) {
$gitversion = Git::version();
}
- my ($author_name) = ($from =~ /^(.*?)\s+</);
- if ($author_name && $author_name =~ /\./ && $author_name !~ /^".*"$/) {
- my ($name, $addr) = ($from =~ /^(.*?)(\s+<.*)/);
- $from = "\"$name\"$addr";
+ my $cc = join(", ", unique_email_list(@cc));
+ my $ccline = "";
+ if ($cc ne '') {
+ $ccline = "\nCc: $cc";
}
- my $header = "From: $from
-To: $to
-Cc: $cc
+ my $sanitized_sender = sanitize_address($sender);
+ make_message_id();
+
+ my $header = "From: $sanitized_sender
+To: $to${ccline}
Subject: $subject
Date: $date
Message-Id: $message_id
X-Mailer: git-send-email $gitversion
";
- if ($reply_to) {
+ if ($thread && $reply_to) {
$header .= "In-Reply-To: $reply_to\n";
$header .= "References: $references\n";
$header .= join("\n", @xh) . "\n";
}
+ my @sendmail_parameters = ('-i', @recipients);
+ my $raw_from = $sanitized_sender;
+ $raw_from = $envelope_sender if (defined $envelope_sender);
+ $raw_from = extract_valid_address($raw_from);
+ unshift (@sendmail_parameters,
+ '-f', $raw_from) if(defined $envelope_sender);
+
if ($dry_run) {
# We don't want to send the email.
} elsif ($smtp_server =~ m#^/#) {
my $pid = open my $sm, '|-';
defined $pid or die $!;
if (!$pid) {
- exec($smtp_server,'-i',
- map { extract_valid_address($_) }
- @recipients) or die $!;
+ exec($smtp_server, @sendmail_parameters) or die $!;
}
print $sm "$header\n$message";
close $sm or die $?;
} else {
require Net::SMTP;
$smtp ||= Net::SMTP->new( $smtp_server );
- $smtp->mail( $from ) 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;
$smtp->datasend("$header\n$message") or die $smtp->message;
$smtp->ok or die "Failed to send $subject\n".$smtp->message;
}
if ($quiet) {
- printf "Sent %s\n", $subject;
+ printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
} else {
- print "OK. Log says:\nDate: $date\n";
- if ($smtp) {
+ print (($dry_run ? "Dry-" : "")."OK. Log says:\nDate: $date\n");
+ if ($smtp_server !~ m#^/#) {
print "Server: $smtp_server\n";
+ print "MAIL FROM:<$raw_from>\n";
+ print "RCPT TO:".join(',',(map { "<$_>" } @recipients))."\n";
} else {
- print "Sendmail: $smtp_server\n";
+ 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";
$reply_to = $initial_reply_to;
$references = $initial_reply_to || '';
-make_message_id();
$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;
$subject = $1;
} elsif (/^(Cc|From):\s+(.*)$/) {
- if ($2 eq $from) {
+ 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;
}
} else {
$message .= $_;
- if (/^Signed-off-by: (.*)$/i && !$no_signed_off_cc) {
- my $c = $1;
+ if (/^(Signed-off-by|Cc): (.*)$/i && $signed_off_cc) {
+ my $c = $2;
chomp $c;
push @cc, $c;
printf("(sob) Adding cc: %s from line '%s'\n",
}
}
close F;
- if (defined $author_not_sender) {
- $author_not_sender = unquote_rfc2047($author_not_sender);
- $message = "From: $author_not_sender\n\n$message";
+
+ if ($cc_cmd ne "") {
+ open(F, "$cc_cmd $t |")
+ or die "(cc-cmd) Could not execute '$cc_cmd'";
+ while(<F>) {
+ 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'";
}
- $cc = join(", ", unique_email_list(@cc));
+ if (defined $author) {
+ $message = "From: $author\n\n$message";
+ }
send_message();
if ($chain_reply_to || !defined $reply_to || length($reply_to) == 0) {
$reply_to = $message_id;
if (length $references > 0) {
- $references .= " $message_id";
+ $references .= "\n $message_id";
} else {
$references = "$message_id";
}
}
- make_message_id();
}
if ($compose) {