send-email: do not barf when Term::ReadLine does not like your terminal
[gitweb.git] / git-send-email.perl
index a7a77977787d1d1fcc98d9fa4dc745162d6fde04..b04b8f40e92f55ecc76dd8c2f239e4960096d2dc 100755 (executable)
 use Getopt::Long;
 use Data::Dumper;
 
+package FakeTerm;
+sub new {
+       my ($class, $reason) = @_;
+       return bless \$reason, shift;
+}
+sub readline {
+       my $self = shift;
+       die "Cannot use readline on FakeTerm: $$self";
+}
+package main;
+
 # most mail servers generate the Date: header, but not all...
 $ENV{LC_ALL} = 'C';
 use POSIX qw/strftime/;
 # Example reply to:
 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
 
-my $term = new Term::ReadLine 'git-send-email';
+my $term = eval {
+       new Term::ReadLine 'git-send-email';
+};
+if ($@) {
+       $term = new FakeTerm "$@: going non-interactive";
+}
 
 # Begin by accumulating all the variables (defined above), that we will end up
 # needing, first, from the command line:
                    "no-signed-off-cc|no-signed-off-by-cc" => \$no_signed_off_cc,
         );
 
+# Verify the user input
+
+foreach my $entry (@to) {
+       die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
+}
+
+foreach my $entry (@initial_cc) {
+       die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
+}
+
+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:
 
 sub gitvar {
@@ -312,16 +342,18 @@ sub expand_aliases {
 
 sub extract_valid_address {
        my $address = shift;
+       my $local_part_regexp = '[^<>"\s@]+';
+       my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
 
        # check for a local address:
-       return $address if ($address =~ /^([\w\-.]+)$/);
+       return $address if ($address =~ /^($local_part_regexp)$/);
 
        if ($have_email_valid) {
-               return Email::Valid->address($address);
+               return scalar Email::Valid->address($address);
        } else {
                # less robust/correct than the monster regexp in Email::Valid,
                # but still does a 99% job, and one less dependency
-               $address =~ /([\w\-.]+@[\w\-.]+)/;
+               $address =~ /($local_part_regexp\@$domain_regexp)/;
                return $1;
        }
 }
@@ -384,7 +416,7 @@ sub send_message
                defined $pid or die $!;
                if (!$pid) {
                        exec($smtp_server,'-i',
-                            map { scalar extract_valid_address($_) }
+                            map { extract_valid_address($_) }
                             @recipients) or die $!;
                }
                print $sm "$header\n$message";