Ævar Arnfjörð Bjarmason | 3328ace | 2010-09-24 20:00:53 +0000 | [diff] [blame] | 1 | #!/usr/bin/perl |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 2 | # |
Ryan Anderson | f3d9f35 | 2005-07-31 20:04:24 -0400 | [diff] [blame] | 3 | # Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com> |
| 4 | # Copyright 2005 Ryan Anderson <ryan@michonline.com> |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 5 | # |
| 6 | # GPL v2 (See COPYING) |
Junio C Hamano | 5825e5b | 2005-07-31 23:05:16 -0700 | [diff] [blame] | 7 | # |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 8 | # Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com> |
| 9 | # |
Ryan Anderson | f3d9f35 | 2005-07-31 20:04:24 -0400 | [diff] [blame] | 10 | # Sends a collection of emails to the given email addresses, disturbingly fast. |
Junio C Hamano | 5825e5b | 2005-07-31 23:05:16 -0700 | [diff] [blame] | 11 | # |
Ryan Anderson | f3d9f35 | 2005-07-31 20:04:24 -0400 | [diff] [blame] | 12 | # Supports two formats: |
| 13 | # 1. mbox format files (ignoring most headers and MIME formatting - this is designed for sending patches) |
| 14 | # 2. The original format support by Greg's script: |
Junio C Hamano | 5825e5b | 2005-07-31 23:05:16 -0700 | [diff] [blame] | 15 | # first line of the message is who to CC, |
Ryan Anderson | f3d9f35 | 2005-07-31 20:04:24 -0400 | [diff] [blame] | 16 | # and second line is the subject of the message. |
Junio C Hamano | 5825e5b | 2005-07-31 23:05:16 -0700 | [diff] [blame] | 17 | # |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 18 | |
Ævar Arnfjörð Bjarmason | d48b284 | 2010-09-24 20:00:52 +0000 | [diff] [blame] | 19 | use 5.008; |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 20 | use strict; |
| 21 | use warnings; |
| 22 | use Term::ReadLine; |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 23 | use Getopt::Long; |
Wu Fengguang | 0e73b3e | 2008-12-19 16:10:10 +0800 | [diff] [blame] | 24 | use Text::ParseWords; |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 25 | use Data::Dumper; |
Sean Estabrooks | 412876d | 2007-08-17 17:38:25 -0400 | [diff] [blame] | 26 | use Term::ANSIColor; |
Jay Soffian | eed6ca7 | 2009-02-14 23:32:13 -0500 | [diff] [blame] | 27 | use File::Temp qw/ tempdir tempfile /; |
Ævar Arnfjörð Bjarmason | 89bf1ba | 2010-09-14 19:02:24 +0000 | [diff] [blame] | 28 | use File::Spec::Functions qw(catfile); |
Pierre Habouzit | 5df9fcf | 2008-11-11 00:54:00 +0100 | [diff] [blame] | 29 | use Error qw(:try); |
Petr Baudis | 3cb8caf | 2006-07-03 22:47:58 +0200 | [diff] [blame] | 30 | use Git; |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 31 | |
Pierre Habouzit | 5df9fcf | 2008-11-11 00:54:00 +0100 | [diff] [blame] | 32 | Getopt::Long::Configure qw/ pass_through /; |
| 33 | |
Junio C Hamano | 280242d | 2006-07-02 16:03:59 -0700 | [diff] [blame] | 34 | package FakeTerm; |
| 35 | sub new { |
| 36 | my ($class, $reason) = @_; |
| 37 | return bless \$reason, shift; |
| 38 | } |
| 39 | sub readline { |
| 40 | my $self = shift; |
| 41 | die "Cannot use readline on FakeTerm: $$self"; |
| 42 | } |
| 43 | package main; |
| 44 | |
Michael Coleman | 1b0baf1 | 2007-02-27 22:47:54 -0600 | [diff] [blame] | 45 | |
| 46 | sub usage { |
| 47 | print <<EOT; |
Pierre Habouzit | 5df9fcf | 2008-11-11 00:54:00 +0100 | [diff] [blame] | 48 | git send-email [options] <file | directory | rev-list options > |
Michael Witten | 4ed62b0 | 2008-09-30 07:58:30 -0500 | [diff] [blame] | 49 | |
| 50 | Composing: |
| 51 | --from <str> * Email From: |
Stephen Boyd | f434c08 | 2010-03-07 14:46:48 -0800 | [diff] [blame] | 52 | --[no-]to <str> * Email To: |
| 53 | --[no-]cc <str> * Email Cc: |
| 54 | --[no-]bcc <str> * Email Bcc: |
Michael Witten | 4ed62b0 | 2008-09-30 07:58:30 -0500 | [diff] [blame] | 55 | --subject <str> * Email "Subject:" |
| 56 | --in-reply-to <str> * Email "In-Reply-To:" |
Pierre Habouzit | 8fd5bb7 | 2008-11-11 00:54:01 +0100 | [diff] [blame] | 57 | --annotate * Review each patch that will be sent in an editor. |
Michael Witten | 4ed62b0 | 2008-09-30 07:58:30 -0500 | [diff] [blame] | 58 | --compose * Open an editor for introduction. |
Thomas Rast | 3cae7e5 | 2010-06-17 22:10:39 +0200 | [diff] [blame] | 59 | --8bit-encoding <str> * Encoding to assume 8bit mails if undeclared |
Michael Witten | 4ed62b0 | 2008-09-30 07:58:30 -0500 | [diff] [blame] | 60 | |
| 61 | Sending: |
| 62 | --envelope-sender <str> * Email envelope sender. |
| 63 | --smtp-server <str:int> * Outgoing SMTP server to use. The port |
| 64 | is optional. Default 'localhost'. |
Pascal Obry | 052fbea | 2010-09-06 20:12:11 +0200 | [diff] [blame] | 65 | --smtp-server-option <str> * Outgoing SMTP server option to use. |
Michael Witten | 4ed62b0 | 2008-09-30 07:58:30 -0500 | [diff] [blame] | 66 | --smtp-server-port <int> * Outgoing SMTP server port. |
| 67 | --smtp-user <str> * Username for SMTP-AUTH. |
| 68 | --smtp-pass <str> * Password for SMTP-AUTH; not necessary. |
| 69 | --smtp-encryption <str> * tls or ssl; anything else disables. |
| 70 | --smtp-ssl * Deprecated. Use '--smtp-encryption ssl'. |
Jari Aalto | 134550f | 2010-03-14 17:16:45 +0200 | [diff] [blame] | 71 | --smtp-domain <str> * The domain name sent to HELO/EHLO handshake |
Jari Aalto | f60812e | 2010-03-14 17:16:09 +0200 | [diff] [blame] | 72 | --smtp-debug <0|1> * Disable, enable Net::SMTP debug. |
Michael Witten | 4ed62b0 | 2008-09-30 07:58:30 -0500 | [diff] [blame] | 73 | |
| 74 | Automating: |
| 75 | --identity <str> * Use the sendemail.<id> options. |
Joe Perches | 6e74e07 | 2010-09-24 10:03:00 -0700 | [diff] [blame] | 76 | --to-cmd <str> * Email To: via `<str> \$patch_path` |
Michael Witten | 4ed62b0 | 2008-09-30 07:58:30 -0500 | [diff] [blame] | 77 | --cc-cmd <str> * Email Cc: via `<str> \$patch_path` |
Jay Soffian | 3531e27 | 2009-02-14 23:32:15 -0500 | [diff] [blame] | 78 | --suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, all. |
| 79 | --[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on. |
Michael Witten | 4ed62b0 | 2008-09-30 07:58:30 -0500 | [diff] [blame] | 80 | --[no-]suppress-from * Send to self. Default off. |
Junio C Hamano | 41fe87f | 2009-08-22 12:48:48 -0700 | [diff] [blame] | 81 | --[no-]chain-reply-to * Chain In-Reply-To: fields. Default off. |
Michael Witten | 4ed62b0 | 2008-09-30 07:58:30 -0500 | [diff] [blame] | 82 | --[no-]thread * Use In-Reply-To: field. Default on. |
| 83 | |
| 84 | Administering: |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 85 | --confirm <str> * Confirm recipients before sending; |
| 86 | auto, cc, compose, always, or never. |
Michael Witten | 4ed62b0 | 2008-09-30 07:58:30 -0500 | [diff] [blame] | 87 | --quiet * Output one line of info per email. |
| 88 | --dry-run * Don't actually send the emails. |
| 89 | --[no-]validate * Perform patch sanity checks. Default on. |
Pierre Habouzit | 5df9fcf | 2008-11-11 00:54:00 +0100 | [diff] [blame] | 90 | --[no-]format-patch * understand any non optional arguments as |
| 91 | `git format-patch` ones. |
Thomas Rast | a03bc5b | 2009-06-08 23:34:12 +0200 | [diff] [blame] | 92 | --force * Send even if safety checks would prevent it. |
Jeff King | c764a0c | 2008-01-18 09:20:10 -0500 | [diff] [blame] | 93 | |
Michael Coleman | 1b0baf1 | 2007-02-27 22:47:54 -0600 | [diff] [blame] | 94 | EOT |
| 95 | exit(1); |
| 96 | } |
| 97 | |
Eric Wong | 4bc87a2 | 2006-03-25 17:20:48 -0800 | [diff] [blame] | 98 | # most mail servers generate the Date: header, but not all... |
Jakub Narebski | 6bdca89 | 2006-07-07 20:57:55 +0200 | [diff] [blame] | 99 | sub format_2822_time { |
| 100 | my ($time) = @_; |
| 101 | my @localtm = localtime($time); |
| 102 | my @gmttm = gmtime($time); |
| 103 | my $localmin = $localtm[1] + $localtm[2] * 60; |
| 104 | my $gmtmin = $gmttm[1] + $gmttm[2] * 60; |
| 105 | if ($localtm[0] != $gmttm[0]) { |
| 106 | die "local zone differs from GMT by a non-minute interval\n"; |
| 107 | } |
| 108 | if ((($gmttm[6] + 1) % 7) == $localtm[6]) { |
| 109 | $localmin += 1440; |
| 110 | } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) { |
| 111 | $localmin -= 1440; |
| 112 | } elsif ($gmttm[6] != $localtm[6]) { |
| 113 | die "local time offset greater than or equal to 24 hours\n"; |
| 114 | } |
| 115 | my $offset = $localmin - $gmtmin; |
| 116 | my $offhour = $offset / 60; |
| 117 | my $offmin = abs($offset % 60); |
| 118 | if (abs($offhour) >= 24) { |
| 119 | die ("local time offset greater than or equal to 24 hours\n"); |
| 120 | } |
| 121 | |
| 122 | return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d", |
| 123 | qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]], |
| 124 | $localtm[3], |
| 125 | qw(Jan Feb Mar Apr May Jun |
| 126 | Jul Aug Sep Oct Nov Dec)[$localtm[4]], |
| 127 | $localtm[5]+1900, |
| 128 | $localtm[2], |
| 129 | $localtm[1], |
| 130 | $localtm[0], |
| 131 | ($offset >= 0) ? '+' : '-', |
| 132 | abs($offhour), |
| 133 | $offmin, |
| 134 | ); |
| 135 | } |
Eric Wong | 4bc87a2 | 2006-03-25 17:20:48 -0800 | [diff] [blame] | 136 | |
Eric Wong | 567ffeb | 2006-03-25 16:47:12 -0800 | [diff] [blame] | 137 | my $have_email_valid = eval { require Email::Valid; 1 }; |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 138 | my $have_mail_address = eval { require Mail::Address; 1 }; |
Eric Wong | 4bc87a2 | 2006-03-25 17:20:48 -0800 | [diff] [blame] | 139 | my $smtp; |
Wincent Colaiuta | 5f5b611 | 2007-11-21 13:35:05 +0100 | [diff] [blame] | 140 | my $auth; |
Eric Wong | 4bc87a2 | 2006-03-25 17:20:48 -0800 | [diff] [blame] | 141 | |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 142 | # Variables we fill in automatically, or via prompting: |
Stephen Boyd | 3c3bb51 | 2010-10-04 00:05:24 -0700 | [diff] [blame] | 143 | my (@to,$no_to,@initial_to,@cc,$no_cc,@initial_cc,@bcclist,$no_bcc,@xh, |
Pierre Habouzit | 8fd5bb7 | 2008-11-11 00:54:01 +0100 | [diff] [blame] | 144 | $initial_reply_to,$initial_subject,@files, |
| 145 | $author,$sender,$smtp_authpass,$annotate,$compose,$time); |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 146 | |
Robin H. Johnson | f073a59 | 2007-04-25 19:37:22 -0700 | [diff] [blame] | 147 | my $envelope_sender; |
Ryan Anderson | 78488b2 | 2005-07-31 20:04:24 -0400 | [diff] [blame] | 148 | |
Ryan Anderson | 9133261 | 2005-07-31 20:04:24 -0400 | [diff] [blame] | 149 | # Example reply to: |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 150 | #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>'; |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 151 | |
Frank Lichtenheld | ad79c02 | 2008-03-14 18:29:30 +0100 | [diff] [blame] | 152 | my $repo = eval { Git->repository() }; |
| 153 | my @repo = $repo ? ($repo) : (); |
Junio C Hamano | 280242d | 2006-07-02 16:03:59 -0700 | [diff] [blame] | 154 | my $term = eval { |
Jay Soffian | 0fb7fc7 | 2008-02-21 19:16:04 -0500 | [diff] [blame] | 155 | $ENV{"GIT_SEND_EMAIL_NOTTY"} |
| 156 | ? new Term::ReadLine 'git-send-email', \*STDIN, \*STDOUT |
| 157 | : new Term::ReadLine 'git-send-email'; |
Junio C Hamano | 280242d | 2006-07-02 16:03:59 -0700 | [diff] [blame] | 158 | }; |
| 159 | if ($@) { |
| 160 | $term = new FakeTerm "$@: going non-interactive"; |
| 161 | } |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 162 | |
Adam Roben | 5483c71 | 2007-06-27 20:59:37 -0700 | [diff] [blame] | 163 | # Behavior modification variables |
| 164 | my ($quiet, $dry_run) = (0, 0); |
Pierre Habouzit | 5df9fcf | 2008-11-11 00:54:00 +0100 | [diff] [blame] | 165 | my $format_patch; |
Jay Soffian | afe756c | 2009-02-23 13:51:37 -0500 | [diff] [blame] | 166 | my $compose_filename; |
Thomas Rast | a03bc5b | 2009-06-08 23:34:12 +0200 | [diff] [blame] | 167 | my $force = 0; |
Adam Roben | 5483c71 | 2007-06-27 20:59:37 -0700 | [diff] [blame] | 168 | |
Pierre Habouzit | 8fd5bb7 | 2008-11-11 00:54:01 +0100 | [diff] [blame] | 169 | # Handle interactive edition of files. |
| 170 | my $multiedit; |
Michael J Gruber | 0ce142c | 2010-03-22 17:12:53 +0100 | [diff] [blame] | 171 | my $editor; |
Jonathan Nieder | b4479f0 | 2009-10-30 20:42:34 -0500 | [diff] [blame] | 172 | |
Pierre Habouzit | 8fd5bb7 | 2008-11-11 00:54:01 +0100 | [diff] [blame] | 173 | sub do_edit { |
Michael J Gruber | 0ce142c | 2010-03-22 17:12:53 +0100 | [diff] [blame] | 174 | if (!defined($editor)) { |
| 175 | $editor = Git::command_oneline('var', 'GIT_EDITOR'); |
| 176 | } |
Pierre Habouzit | 8fd5bb7 | 2008-11-11 00:54:01 +0100 | [diff] [blame] | 177 | if (defined($multiedit) && !$multiedit) { |
Pierre Habouzit | beece9d | 2008-11-11 00:54:02 +0100 | [diff] [blame] | 178 | map { |
| 179 | system('sh', '-c', $editor.' "$@"', $editor, $_); |
| 180 | if (($? & 127) || ($? >> 8)) { |
| 181 | die("the editor exited uncleanly, aborting everything"); |
| 182 | } |
| 183 | } @_; |
Pierre Habouzit | 8fd5bb7 | 2008-11-11 00:54:01 +0100 | [diff] [blame] | 184 | } else { |
| 185 | system('sh', '-c', $editor.' "$@"', $editor, @_); |
Pierre Habouzit | beece9d | 2008-11-11 00:54:02 +0100 | [diff] [blame] | 186 | if (($? & 127) || ($? >> 8)) { |
| 187 | die("the editor exited uncleanly, aborting everything"); |
| 188 | } |
Pierre Habouzit | 8fd5bb7 | 2008-11-11 00:54:01 +0100 | [diff] [blame] | 189 | } |
| 190 | } |
Adam Roben | 5483c71 | 2007-06-27 20:59:37 -0700 | [diff] [blame] | 191 | |
| 192 | # Variables with corresponding config settings |
Joe Perches | 6e74e07 | 2010-09-24 10:03:00 -0700 | [diff] [blame] | 193 | my ($thread, $chain_reply_to, $suppress_from, $signed_off_by_cc); |
| 194 | my ($to_cmd, $cc_cmd); |
Pascal Obry | 052fbea | 2010-09-06 20:12:11 +0200 | [diff] [blame] | 195 | my ($smtp_server, $smtp_server_port, @smtp_server_options); |
| 196 | my ($smtp_authuser, $smtp_encryption); |
Pascal Obry | 1d02a00 | 2010-09-06 20:12:10 +0200 | [diff] [blame] | 197 | my ($identity, $aliasfiletype, @alias_files, $smtp_domain); |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 198 | my ($validate, $confirm); |
David Brown | 6564828 | 2007-12-25 19:56:29 -0800 | [diff] [blame] | 199 | my (@suppress_cc); |
Thomas Rast | 3cae7e5 | 2010-06-17 22:10:39 +0200 | [diff] [blame] | 200 | my ($auto_8bit_encoding); |
Adam Roben | 5483c71 | 2007-06-27 20:59:37 -0700 | [diff] [blame] | 201 | |
Jari Aalto | f60812e | 2010-03-14 17:16:09 +0200 | [diff] [blame] | 202 | my ($debug_net_smtp) = 0; # Net::SMTP, see send_message() |
| 203 | |
Nanako Shiraishi | 528fb08 | 2009-11-29 12:24:48 +0900 | [diff] [blame] | 204 | my $not_set_by_user = "true but not set by the user"; |
| 205 | |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 206 | my %config_bool_settings = ( |
Adam Roben | 5483c71 | 2007-06-27 20:59:37 -0700 | [diff] [blame] | 207 | "thread" => [\$thread, 1], |
Nanako Shiraishi | 528fb08 | 2009-11-29 12:24:48 +0900 | [diff] [blame] | 208 | "chainreplyto" => [\$chain_reply_to, $not_set_by_user], |
David Brown | 6564828 | 2007-12-25 19:56:29 -0800 | [diff] [blame] | 209 | "suppressfrom" => [\$suppress_from, undef], |
Michael Witten | ddc3d4f | 2008-09-30 07:58:32 -0500 | [diff] [blame] | 210 | "signedoffbycc" => [\$signed_off_by_cc, undef], |
| 211 | "signedoffcc" => [\$signed_off_by_cc, undef], # Deprecated |
Michael Witten | dbf5e1e | 2008-09-30 07:58:27 -0500 | [diff] [blame] | 212 | "validate" => [\$validate, 1], |
Jeff King | 829a1c6 | 2012-01-09 17:55:42 -0500 | [diff] [blame] | 213 | "multiedit" => [\$multiedit, undef] |
Adam Roben | e46f7a0 | 2007-06-26 15:48:30 -0700 | [diff] [blame] | 214 | ); |
| 215 | |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 216 | my %config_settings = ( |
| 217 | "smtpserver" => \$smtp_server, |
Junio C Hamano | 44b2476 | 2007-09-25 17:27:54 -0700 | [diff] [blame] | 218 | "smtpserverport" => \$smtp_server_port, |
Pascal Obry | 052fbea | 2010-09-06 20:12:11 +0200 | [diff] [blame] | 219 | "smtpserveroption" => \@smtp_server_options, |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 220 | "smtpuser" => \$smtp_authuser, |
| 221 | "smtppass" => \$smtp_authpass, |
Pascal Obry | e1e9115 | 2010-09-06 20:12:09 +0200 | [diff] [blame] | 222 | "smtpdomain" => \$smtp_domain, |
Stephen Boyd | 3c3bb51 | 2010-10-04 00:05:24 -0700 | [diff] [blame] | 223 | "to" => \@initial_to, |
Joe Perches | 6e74e07 | 2010-09-24 10:03:00 -0700 | [diff] [blame] | 224 | "tocmd" => \$to_cmd, |
Miklos Vajna | 5f8b9fc | 2008-04-27 14:14:58 +0200 | [diff] [blame] | 225 | "cc" => \@initial_cc, |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 226 | "cccmd" => \$cc_cmd, |
| 227 | "aliasfiletype" => \$aliasfiletype, |
| 228 | "bcc" => \@bcclist, |
David Brown | 6564828 | 2007-12-25 19:56:29 -0800 | [diff] [blame] | 229 | "suppresscc" => \@suppress_cc, |
Ask Bjørn Hansen | 9f7820a | 2008-06-07 00:33:42 -0700 | [diff] [blame] | 230 | "envelopesender" => \$envelope_sender, |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 231 | "confirm" => \$confirm, |
Trent Piepho | 09caa24 | 2009-05-12 15:48:56 -0700 | [diff] [blame] | 232 | "from" => \$sender, |
Thomas Rast | 3cae7e5 | 2010-06-17 22:10:39 +0200 | [diff] [blame] | 233 | "assume8bitencoding" => \$auto_8bit_encoding, |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 234 | ); |
Avi Kivity | 4a62d3f | 2007-03-11 19:19:44 +0200 | [diff] [blame] | 235 | |
Cord Seele | cec5dae | 2011-09-30 12:52:25 +0200 | [diff] [blame] | 236 | my %config_path_settings = ( |
| 237 | "aliasesfile" => \@alias_files, |
| 238 | ); |
| 239 | |
Nanako Shiraishi | 528fb08 | 2009-11-29 12:24:48 +0900 | [diff] [blame] | 240 | # Help users prepare for 1.7.0 |
| 241 | sub chain_reply_to { |
| 242 | if (defined $chain_reply_to && |
| 243 | $chain_reply_to eq $not_set_by_user) { |
| 244 | print STDERR |
Junio C Hamano | a19f101 | 2009-12-26 14:03:17 -0800 | [diff] [blame] | 245 | "In git 1.7.0, the default has changed to --no-chain-reply-to\n" . |
Nanako Shiraishi | 528fb08 | 2009-11-29 12:24:48 +0900 | [diff] [blame] | 246 | "Set sendemail.chainreplyto configuration variable to true if\n" . |
| 247 | "you want to keep --chain-reply-to as your default.\n"; |
Junio C Hamano | a19f101 | 2009-12-26 14:03:17 -0800 | [diff] [blame] | 248 | $chain_reply_to = 0; |
Nanako Shiraishi | 528fb08 | 2009-11-29 12:24:48 +0900 | [diff] [blame] | 249 | } |
| 250 | return $chain_reply_to; |
| 251 | } |
| 252 | |
Michael Witten | 8742997 | 2008-02-03 19:53:57 -0500 | [diff] [blame] | 253 | # Handle Uncouth Termination |
| 254 | sub signal_handler { |
| 255 | |
| 256 | # Make text normal |
| 257 | print color("reset"), "\n"; |
| 258 | |
| 259 | # SMTP password masked |
| 260 | system "stty echo"; |
| 261 | |
| 262 | # tmp files from --compose |
Jay Soffian | afe756c | 2009-02-23 13:51:37 -0500 | [diff] [blame] | 263 | if (defined $compose_filename) { |
| 264 | if (-e $compose_filename) { |
| 265 | print "'$compose_filename' contains an intermediate version of the email you were composing.\n"; |
| 266 | } |
| 267 | if (-e ($compose_filename . ".final")) { |
| 268 | print "'$compose_filename.final' contains the composed email.\n" |
| 269 | } |
Michael Witten | 8742997 | 2008-02-03 19:53:57 -0500 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | exit; |
| 273 | }; |
| 274 | |
| 275 | $SIG{TERM} = \&signal_handler; |
| 276 | $SIG{INT} = \&signal_handler; |
| 277 | |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 278 | # Begin by accumulating all the variables (defined above), that we will end up |
| 279 | # needing, first, from the command line: |
| 280 | |
Clemens Buchacher | c597824 | 2011-09-03 19:06:13 +0200 | [diff] [blame] | 281 | my $help; |
| 282 | my $rc = GetOptions("h" => \$help, |
| 283 | "sender|from=s" => \$sender, |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 284 | "in-reply-to=s" => \$initial_reply_to, |
| 285 | "subject=s" => \$initial_subject, |
Stephen Boyd | 3c3bb51 | 2010-10-04 00:05:24 -0700 | [diff] [blame] | 286 | "to=s" => \@initial_to, |
Joe Perches | 6e74e07 | 2010-09-24 10:03:00 -0700 | [diff] [blame] | 287 | "to-cmd=s" => \$to_cmd, |
Stephen Boyd | f434c08 | 2010-03-07 14:46:48 -0800 | [diff] [blame] | 288 | "no-to" => \$no_to, |
Ryan Anderson | da140f8 | 2006-02-13 03:05:15 -0500 | [diff] [blame] | 289 | "cc=s" => \@initial_cc, |
Stephen Boyd | f434c08 | 2010-03-07 14:46:48 -0800 | [diff] [blame] | 290 | "no-cc" => \$no_cc, |
Ryan Anderson | 5806324 | 2006-05-29 12:30:13 -0700 | [diff] [blame] | 291 | "bcc=s" => \@bcclist, |
Stephen Boyd | f434c08 | 2010-03-07 14:46:48 -0800 | [diff] [blame] | 292 | "no-bcc" => \$no_bcc, |
Ryan Anderson | 78488b2 | 2005-07-31 20:04:24 -0400 | [diff] [blame] | 293 | "chain-reply-to!" => \$chain_reply_to, |
Ryan Anderson | 3342d85 | 2005-07-31 20:04:24 -0400 | [diff] [blame] | 294 | "smtp-server=s" => \$smtp_server, |
Pascal Obry | 052fbea | 2010-09-06 20:12:11 +0200 | [diff] [blame] | 295 | "smtp-server-option=s" => \@smtp_server_options, |
Junio C Hamano | 44b2476 | 2007-09-25 17:27:54 -0700 | [diff] [blame] | 296 | "smtp-server-port=s" => \$smtp_server_port, |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 297 | "smtp-user=s" => \$smtp_authuser, |
Michael Witten | 2363d74 | 2008-02-03 19:53:56 -0500 | [diff] [blame] | 298 | "smtp-pass:s" => \$smtp_authpass, |
Thomas Rast | f6bebd1 | 2008-06-25 21:42:43 +0200 | [diff] [blame] | 299 | "smtp-ssl" => sub { $smtp_encryption = 'ssl' }, |
| 300 | "smtp-encryption=s" => \$smtp_encryption, |
Jari Aalto | f60812e | 2010-03-14 17:16:09 +0200 | [diff] [blame] | 301 | "smtp-debug:i" => \$debug_net_smtp, |
Brian Gernhardt | 69cf7bf | 2010-04-10 10:53:56 -0400 | [diff] [blame] | 302 | "smtp-domain:s" => \$smtp_domain, |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 303 | "identity=s" => \$identity, |
Pierre Habouzit | 8fd5bb7 | 2008-11-11 00:54:01 +0100 | [diff] [blame] | 304 | "annotate" => \$annotate, |
Ryan Anderson | 1f038a0 | 2005-09-05 01:13:07 -0400 | [diff] [blame] | 305 | "compose" => \$compose, |
Ryan Anderson | 30d08b3 | 2006-02-02 11:56:06 -0500 | [diff] [blame] | 306 | "quiet" => \$quiet, |
Joe Perches | 324a8bd | 2007-08-17 18:51:12 -0700 | [diff] [blame] | 307 | "cc-cmd=s" => \$cc_cmd, |
Adam Roben | 5483c71 | 2007-06-27 20:59:37 -0700 | [diff] [blame] | 308 | "suppress-from!" => \$suppress_from, |
David Brown | 6564828 | 2007-12-25 19:56:29 -0800 | [diff] [blame] | 309 | "suppress-cc=s" => \@suppress_cc, |
Michael Witten | ddc3d4f | 2008-09-30 07:58:32 -0500 | [diff] [blame] | 310 | "signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc, |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 311 | "confirm=s" => \$confirm, |
Matthew Wilcox | 6130259 | 2006-10-10 08:58:23 -0600 | [diff] [blame] | 312 | "dry-run" => \$dry_run, |
Robin H. Johnson | f073a59 | 2007-04-25 19:37:22 -0700 | [diff] [blame] | 313 | "envelope-sender=s" => \$envelope_sender, |
Adam Roben | 5483c71 | 2007-06-27 20:59:37 -0700 | [diff] [blame] | 314 | "thread!" => \$thread, |
Michael Witten | dbf5e1e | 2008-09-30 07:58:27 -0500 | [diff] [blame] | 315 | "validate!" => \$validate, |
Pierre Habouzit | 5df9fcf | 2008-11-11 00:54:00 +0100 | [diff] [blame] | 316 | "format-patch!" => \$format_patch, |
Thomas Rast | 3cae7e5 | 2010-06-17 22:10:39 +0200 | [diff] [blame] | 317 | "8bit-encoding=s" => \$auto_8bit_encoding, |
Thomas Rast | a03bc5b | 2009-06-08 23:34:12 +0200 | [diff] [blame] | 318 | "force" => \$force, |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 319 | ); |
| 320 | |
Clemens Buchacher | c597824 | 2011-09-03 19:06:13 +0200 | [diff] [blame] | 321 | usage() if $help; |
Michael Coleman | 1b0baf1 | 2007-02-27 22:47:54 -0600 | [diff] [blame] | 322 | unless ($rc) { |
| 323 | usage(); |
| 324 | } |
| 325 | |
Jay Soffian | eed6ca7 | 2009-02-14 23:32:13 -0500 | [diff] [blame] | 326 | die "Cannot run git format-patch from outside a repository\n" |
| 327 | if $format_patch and not $repo; |
| 328 | |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 329 | # Now, let's fill any that aren't set in with defaults: |
| 330 | |
| 331 | sub read_config { |
| 332 | my ($prefix) = @_; |
| 333 | |
| 334 | foreach my $setting (keys %config_bool_settings) { |
| 335 | my $target = $config_bool_settings{$setting}->[0]; |
Frank Lichtenheld | ad79c02 | 2008-03-14 18:29:30 +0100 | [diff] [blame] | 336 | $$target = Git::config_bool(@repo, "$prefix.$setting") unless (defined $$target); |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 337 | } |
| 338 | |
Cord Seele | cec5dae | 2011-09-30 12:52:25 +0200 | [diff] [blame] | 339 | foreach my $setting (keys %config_path_settings) { |
Cord Seele | 463b0ea | 2011-10-14 22:53:31 +0200 | [diff] [blame] | 340 | my $target = $config_path_settings{$setting}; |
| 341 | if (ref($target) eq "ARRAY") { |
| 342 | unless (@$target) { |
| 343 | my @values = Git::config_path(@repo, "$prefix.$setting"); |
| 344 | @$target = @values if (@values && defined $values[0]); |
| 345 | } |
| 346 | } |
| 347 | else { |
| 348 | $$target = Git::config_path(@repo, "$prefix.$setting") unless (defined $$target); |
| 349 | } |
Cord Seele | cec5dae | 2011-09-30 12:52:25 +0200 | [diff] [blame] | 350 | } |
| 351 | |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 352 | foreach my $setting (keys %config_settings) { |
| 353 | my $target = $config_settings{$setting}; |
Stephen Boyd | f434c08 | 2010-03-07 14:46:48 -0800 | [diff] [blame] | 354 | next if $setting eq "to" and defined $no_to; |
| 355 | next if $setting eq "cc" and defined $no_cc; |
| 356 | next if $setting eq "bcc" and defined $no_bcc; |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 357 | if (ref($target) eq "ARRAY") { |
| 358 | unless (@$target) { |
Frank Lichtenheld | ad79c02 | 2008-03-14 18:29:30 +0100 | [diff] [blame] | 359 | my @values = Git::config(@repo, "$prefix.$setting"); |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 360 | @$target = @values if (@values && defined $values[0]); |
| 361 | } |
| 362 | } |
| 363 | else { |
Frank Lichtenheld | ad79c02 | 2008-03-14 18:29:30 +0100 | [diff] [blame] | 364 | $$target = Git::config(@repo, "$prefix.$setting") unless (defined $$target); |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 365 | } |
| 366 | } |
Thomas Rast | f6bebd1 | 2008-06-25 21:42:43 +0200 | [diff] [blame] | 367 | |
| 368 | if (!defined $smtp_encryption) { |
| 369 | my $enc = Git::config(@repo, "$prefix.smtpencryption"); |
| 370 | if (defined $enc) { |
| 371 | $smtp_encryption = $enc; |
| 372 | } elsif (Git::config_bool(@repo, "$prefix.smtpssl")) { |
| 373 | $smtp_encryption = 'ssl'; |
| 374 | } |
| 375 | } |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | # read configuration from [sendemail "$identity"], fall back on [sendemail] |
Frank Lichtenheld | ad79c02 | 2008-03-14 18:29:30 +0100 | [diff] [blame] | 379 | $identity = Git::config(@repo, "sendemail.identity") unless (defined $identity); |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 380 | read_config("sendemail.$identity") if (defined $identity); |
| 381 | read_config("sendemail"); |
| 382 | |
| 383 | # fall back on builtin bool defaults |
| 384 | foreach my $setting (values %config_bool_settings) { |
| 385 | ${$setting->[0]} = $setting->[1] unless (defined (${$setting->[0]})); |
| 386 | } |
| 387 | |
Thomas Rast | fa835cd | 2008-06-26 23:03:21 +0200 | [diff] [blame] | 388 | # 'default' encryption is none -- this only prevents a warning |
| 389 | $smtp_encryption = '' unless (defined $smtp_encryption); |
| 390 | |
David Brown | 6564828 | 2007-12-25 19:56:29 -0800 | [diff] [blame] | 391 | # Set CC suppressions |
| 392 | my(%suppress_cc); |
| 393 | if (@suppress_cc) { |
| 394 | foreach my $entry (@suppress_cc) { |
| 395 | die "Unknown --suppress-cc field: '$entry'\n" |
Ævar Arnfjörð Bjarmason | e9bf741 | 2010-09-30 13:43:04 +0000 | [diff] [blame] | 396 | unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc)$/; |
David Brown | 6564828 | 2007-12-25 19:56:29 -0800 | [diff] [blame] | 397 | $suppress_cc{$entry} = 1; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | if ($suppress_cc{'all'}) { |
Paolo Bonzini | cb8a9bd | 2009-06-18 14:31:32 +0200 | [diff] [blame] | 402 | foreach my $entry (qw (cccmd cc author self sob body bodycc)) { |
David Brown | 6564828 | 2007-12-25 19:56:29 -0800 | [diff] [blame] | 403 | $suppress_cc{$entry} = 1; |
| 404 | } |
| 405 | delete $suppress_cc{'all'}; |
| 406 | } |
| 407 | |
| 408 | # If explicit old-style ones are specified, they trump --suppress-cc. |
| 409 | $suppress_cc{'self'} = $suppress_from if defined $suppress_from; |
Michael Witten | ddc3d4f | 2008-09-30 07:58:32 -0500 | [diff] [blame] | 410 | $suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc; |
David Brown | 6564828 | 2007-12-25 19:56:29 -0800 | [diff] [blame] | 411 | |
Jay Soffian | 3531e27 | 2009-02-14 23:32:15 -0500 | [diff] [blame] | 412 | if ($suppress_cc{'body'}) { |
| 413 | foreach my $entry (qw (sob bodycc)) { |
| 414 | $suppress_cc{$entry} = 1; |
| 415 | } |
| 416 | delete $suppress_cc{'body'}; |
| 417 | } |
| 418 | |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 419 | # Set confirm's default value |
| 420 | my $confirm_unconfigured = !defined $confirm; |
| 421 | if ($confirm_unconfigured) { |
| 422 | $confirm = scalar %suppress_cc ? 'compose' : 'auto'; |
| 423 | }; |
| 424 | die "Unknown --confirm setting: '$confirm'\n" |
| 425 | unless $confirm =~ /^(?:auto|cc|compose|always|never)/; |
| 426 | |
David Brown | 6564828 | 2007-12-25 19:56:29 -0800 | [diff] [blame] | 427 | # Debugging, print out the suppressions. |
| 428 | if (0) { |
| 429 | print "suppressions:\n"; |
| 430 | foreach my $entry (keys %suppress_cc) { |
| 431 | printf " %-5s -> $suppress_cc{$entry}\n", $entry; |
| 432 | } |
| 433 | } |
| 434 | |
Frank Lichtenheld | ad79c02 | 2008-03-14 18:29:30 +0100 | [diff] [blame] | 435 | my ($repoauthor, $repocommitter); |
| 436 | ($repoauthor) = Git::ident_person(@repo, 'author'); |
| 437 | ($repocommitter) = Git::ident_person(@repo, 'committer'); |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 438 | |
Eric W. Biederman | 79ee555 | 2006-06-21 07:17:31 -0600 | [diff] [blame] | 439 | # Verify the user input |
| 440 | |
Stephen Boyd | 3c3bb51 | 2010-10-04 00:05:24 -0700 | [diff] [blame] | 441 | foreach my $entry (@initial_to) { |
Eric W. Biederman | 79ee555 | 2006-06-21 07:17:31 -0600 | [diff] [blame] | 442 | die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/; |
| 443 | } |
| 444 | |
| 445 | foreach my $entry (@initial_cc) { |
| 446 | die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/; |
| 447 | } |
| 448 | |
| 449 | foreach my $entry (@bcclist) { |
| 450 | die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/; |
| 451 | } |
| 452 | |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 453 | sub parse_address_line { |
| 454 | if ($have_mail_address) { |
| 455 | return map { $_->format } Mail::Address->parse($_[0]); |
| 456 | } else { |
| 457 | return split_addrs($_[0]); |
| 458 | } |
| 459 | } |
| 460 | |
Wu Fengguang | 0e73b3e | 2008-12-19 16:10:10 +0800 | [diff] [blame] | 461 | sub split_addrs { |
Junio C Hamano | 2f0e7cb | 2008-12-21 01:57:59 -0800 | [diff] [blame] | 462 | return quotewords('\s*,\s*', 1, @_); |
Wu Fengguang | 0e73b3e | 2008-12-19 16:10:10 +0800 | [diff] [blame] | 463 | } |
| 464 | |
Eric Wong | 994d6c6 | 2006-05-14 19:13:44 -0700 | [diff] [blame] | 465 | my %aliases; |
Eric Wong | 994d6c6 | 2006-05-14 19:13:44 -0700 | [diff] [blame] | 466 | my %parse_alias = ( |
| 467 | # multiline formats can be supported in the future |
| 468 | mutt => sub { my $fh = shift; while (<$fh>) { |
Felipe Contreras | ffc01f9 | 2009-09-30 17:49:36 +0300 | [diff] [blame] | 469 | if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) { |
Eric Wong | 994d6c6 | 2006-05-14 19:13:44 -0700 | [diff] [blame] | 470 | my ($alias, $addr) = ($1, $2); |
| 471 | $addr =~ s/#.*$//; # mutt allows # comments |
| 472 | # commas delimit multiple addresses |
Wu Fengguang | 0e73b3e | 2008-12-19 16:10:10 +0800 | [diff] [blame] | 473 | $aliases{$alias} = [ split_addrs($addr) ]; |
Eric Wong | 994d6c6 | 2006-05-14 19:13:44 -0700 | [diff] [blame] | 474 | }}}, |
| 475 | mailrc => sub { my $fh = shift; while (<$fh>) { |
| 476 | if (/^alias\s+(\S+)\s+(.*)$/) { |
| 477 | # spaces delimit multiple addresses |
Eric W. Biederman | fe87c92 | 2009-05-20 19:45:53 -0700 | [diff] [blame] | 478 | $aliases{$1} = [ quotewords('\s+', 0, $2) ]; |
Eric Wong | 994d6c6 | 2006-05-14 19:13:44 -0700 | [diff] [blame] | 479 | }}}, |
Trent Piepho | 73c427e | 2008-11-25 18:55:00 -0800 | [diff] [blame] | 480 | pine => sub { my $fh = shift; my $f='\t[^\t]*'; |
| 481 | for (my $x = ''; defined($x); $x = $_) { |
| 482 | chomp $x; |
| 483 | $x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/); |
| 484 | $x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next; |
Wu Fengguang | 0e73b3e | 2008-12-19 16:10:10 +0800 | [diff] [blame] | 485 | $aliases{$1} = [ split_addrs($2) ]; |
Trent Piepho | 73c427e | 2008-11-25 18:55:00 -0800 | [diff] [blame] | 486 | }}, |
Bill Pemberton | 7613ea3 | 2009-04-22 09:41:29 -0400 | [diff] [blame] | 487 | elm => sub { my $fh = shift; |
| 488 | while (<$fh>) { |
| 489 | if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) { |
| 490 | my ($alias, $addr) = ($1, $2); |
| 491 | $aliases{$alias} = [ split_addrs($addr) ]; |
| 492 | } |
| 493 | } }, |
| 494 | |
Eric Wong | 994d6c6 | 2006-05-14 19:13:44 -0700 | [diff] [blame] | 495 | gnus => sub { my $fh = shift; while (<$fh>) { |
| 496 | if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) { |
| 497 | $aliases{$1} = [ $2 ]; |
| 498 | }}} |
| 499 | ); |
| 500 | |
Petr Baudis | 3cb8caf | 2006-07-03 22:47:58 +0200 | [diff] [blame] | 501 | if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) { |
Eric Wong | 994d6c6 | 2006-05-14 19:13:44 -0700 | [diff] [blame] | 502 | foreach my $file (@alias_files) { |
| 503 | open my $fh, '<', $file or die "opening $file: $!\n"; |
| 504 | $parse_alias{$aliasfiletype}->($fh); |
| 505 | close $fh; |
| 506 | } |
| 507 | } |
| 508 | |
Uwe Kleine-König | 94638f8 | 2007-08-09 15:27:58 +0200 | [diff] [blame] | 509 | ($sender) = expand_aliases($sender) if defined $sender; |
Michael Hendricks | ae740a5 | 2007-07-04 19:11:36 -0600 | [diff] [blame] | 510 | |
Pierre Habouzit | 5df9fcf | 2008-11-11 00:54:00 +0100 | [diff] [blame] | 511 | # returns 1 if the conflict must be solved using it as a format-patch argument |
| 512 | sub check_file_rev_conflict($) { |
Jay Soffian | eed6ca7 | 2009-02-14 23:32:13 -0500 | [diff] [blame] | 513 | return unless $repo; |
Pierre Habouzit | 5df9fcf | 2008-11-11 00:54:00 +0100 | [diff] [blame] | 514 | my $f = shift; |
| 515 | try { |
| 516 | $repo->command('rev-parse', '--verify', '--quiet', $f); |
| 517 | if (defined($format_patch)) { |
Pierre Habouzit | 5df9fcf | 2008-11-11 00:54:00 +0100 | [diff] [blame] | 518 | return $format_patch; |
| 519 | } |
| 520 | die(<<EOF); |
| 521 | File '$f' exists but it could also be the range of commits |
| 522 | to produce patches for. Please disambiguate by... |
| 523 | |
| 524 | * Saying "./$f" if you mean a file; or |
| 525 | * Giving --format-patch option if you mean a range. |
| 526 | EOF |
| 527 | } catch Git::Error::Command with { |
| 528 | return 0; |
| 529 | } |
| 530 | } |
| 531 | |
Jeff King | aa54892 | 2008-01-18 09:19:36 -0500 | [diff] [blame] | 532 | # Now that all the defaults are set, process the rest of the command line |
| 533 | # arguments and collect up the files that need to be processed. |
Pierre Habouzit | 5df9fcf | 2008-11-11 00:54:00 +0100 | [diff] [blame] | 534 | my @rev_list_opts; |
Junio C Hamano | 69f4ce5 | 2008-11-30 22:38:20 -0800 | [diff] [blame] | 535 | while (defined(my $f = shift @ARGV)) { |
Pierre Habouzit | 5df9fcf | 2008-11-11 00:54:00 +0100 | [diff] [blame] | 536 | if ($f eq "--") { |
| 537 | push @rev_list_opts, "--", @ARGV; |
| 538 | @ARGV = (); |
| 539 | } elsif (-d $f and !check_file_rev_conflict($f)) { |
Ævar Arnfjörð Bjarmason | c603816 | 2010-09-30 13:42:54 +0000 | [diff] [blame] | 540 | opendir my $dh, $f |
Jeff King | aa54892 | 2008-01-18 09:19:36 -0500 | [diff] [blame] | 541 | or die "Failed to opendir $f: $!"; |
| 542 | |
Ævar Arnfjörð Bjarmason | 89bf1ba | 2010-09-14 19:02:24 +0000 | [diff] [blame] | 543 | push @files, grep { -f $_ } map { catfile($f, $_) } |
Ævar Arnfjörð Bjarmason | c603816 | 2010-09-30 13:42:54 +0000 | [diff] [blame] | 544 | sort readdir $dh; |
| 545 | closedir $dh; |
Pierre Habouzit | 5df9fcf | 2008-11-11 00:54:00 +0100 | [diff] [blame] | 546 | } elsif ((-f $f or -p $f) and !check_file_rev_conflict($f)) { |
Jeff King | aa54892 | 2008-01-18 09:19:36 -0500 | [diff] [blame] | 547 | push @files, $f; |
Jeff King | aa54892 | 2008-01-18 09:19:36 -0500 | [diff] [blame] | 548 | } else { |
Pierre Habouzit | 5df9fcf | 2008-11-11 00:54:00 +0100 | [diff] [blame] | 549 | push @rev_list_opts, $f; |
Jeff King | aa54892 | 2008-01-18 09:19:36 -0500 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
Pierre Habouzit | 5df9fcf | 2008-11-11 00:54:00 +0100 | [diff] [blame] | 553 | if (@rev_list_opts) { |
Jay Soffian | eed6ca7 | 2009-02-14 23:32:13 -0500 | [diff] [blame] | 554 | die "Cannot run git format-patch from outside a repository\n" |
| 555 | unless $repo; |
Pierre Habouzit | 5df9fcf | 2008-11-11 00:54:00 +0100 | [diff] [blame] | 556 | push @files, $repo->command('format-patch', '-o', tempdir(CLEANUP => 1), @rev_list_opts); |
| 557 | } |
| 558 | |
Michael Witten | dbf5e1e | 2008-09-30 07:58:27 -0500 | [diff] [blame] | 559 | if ($validate) { |
Jeff King | c764a0c | 2008-01-18 09:20:10 -0500 | [diff] [blame] | 560 | foreach my $f (@files) { |
Kevin Ballard | 300913b | 2008-06-25 15:44:40 -0700 | [diff] [blame] | 561 | unless (-p $f) { |
| 562 | my $error = validate_patch($f); |
| 563 | $error and die "fatal: $f: $error\nwarning: no patches were sent\n"; |
| 564 | } |
Jeff King | c764a0c | 2008-01-18 09:20:10 -0500 | [diff] [blame] | 565 | } |
Jeff King | 747bbff | 2008-01-18 09:19:48 -0500 | [diff] [blame] | 566 | } |
| 567 | |
Jeff King | aa54892 | 2008-01-18 09:19:36 -0500 | [diff] [blame] | 568 | if (@files) { |
| 569 | unless ($quiet) { |
| 570 | print $_,"\n" for (@files); |
| 571 | } |
| 572 | } else { |
| 573 | print STDERR "\nNo patch files specified!\n\n"; |
| 574 | usage(); |
| 575 | } |
| 576 | |
Ævar Arnfjörð Bjarmason | acf071b | 2010-09-30 13:42:57 +0000 | [diff] [blame] | 577 | sub get_patch_subject { |
Pierre Habouzit | beece9d | 2008-11-11 00:54:02 +0100 | [diff] [blame] | 578 | my $fn = shift; |
| 579 | open (my $fh, '<', $fn); |
| 580 | while (my $line = <$fh>) { |
| 581 | next unless ($line =~ /^Subject: (.*)$/); |
| 582 | close $fh; |
| 583 | return "GIT: $1\n"; |
| 584 | } |
| 585 | close $fh; |
| 586 | die "No subject line in $fn ?"; |
| 587 | } |
| 588 | |
| 589 | if ($compose) { |
| 590 | # Note that this does not need to be secure, but we will make a small |
| 591 | # effort to have it be unique |
Jay Soffian | afe756c | 2009-02-23 13:51:37 -0500 | [diff] [blame] | 592 | $compose_filename = ($repo ? |
| 593 | tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) : |
| 594 | tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1]; |
Ævar Arnfjörð Bjarmason | fe0f944 | 2010-09-30 13:42:55 +0000 | [diff] [blame] | 595 | open my $c, ">", $compose_filename |
Pierre Habouzit | beece9d | 2008-11-11 00:54:02 +0100 | [diff] [blame] | 596 | or die "Failed to open for writing $compose_filename: $!"; |
| 597 | |
| 598 | |
| 599 | my $tpl_sender = $sender || $repoauthor || $repocommitter || ''; |
| 600 | my $tpl_subject = $initial_subject || ''; |
| 601 | my $tpl_reply_to = $initial_reply_to || ''; |
| 602 | |
Ævar Arnfjörð Bjarmason | fe0f944 | 2010-09-30 13:42:55 +0000 | [diff] [blame] | 603 | print $c <<EOT; |
Pierre Habouzit | beece9d | 2008-11-11 00:54:02 +0100 | [diff] [blame] | 604 | From $tpl_sender # This line is ignored. |
Michael Witten | 40e6e8a | 2009-04-13 13:23:50 -0500 | [diff] [blame] | 605 | GIT: Lines beginning in "GIT:" will be removed. |
Pierre Habouzit | beece9d | 2008-11-11 00:54:02 +0100 | [diff] [blame] | 606 | GIT: Consider including an overall diffstat or table of contents |
| 607 | GIT: for the patch you are writing. |
| 608 | GIT: |
| 609 | GIT: Clear the body content if you don't wish to send a summary. |
| 610 | From: $tpl_sender |
| 611 | Subject: $tpl_subject |
| 612 | In-Reply-To: $tpl_reply_to |
| 613 | |
| 614 | EOT |
| 615 | for my $f (@files) { |
Ævar Arnfjörð Bjarmason | fe0f944 | 2010-09-30 13:42:55 +0000 | [diff] [blame] | 616 | print $c get_patch_subject($f); |
Pierre Habouzit | beece9d | 2008-11-11 00:54:02 +0100 | [diff] [blame] | 617 | } |
Ævar Arnfjörð Bjarmason | fe0f944 | 2010-09-30 13:42:55 +0000 | [diff] [blame] | 618 | close $c; |
Pierre Habouzit | beece9d | 2008-11-11 00:54:02 +0100 | [diff] [blame] | 619 | |
Pierre Habouzit | beece9d | 2008-11-11 00:54:02 +0100 | [diff] [blame] | 620 | if ($annotate) { |
| 621 | do_edit($compose_filename, @files); |
| 622 | } else { |
| 623 | do_edit($compose_filename); |
| 624 | } |
| 625 | |
Ævar Arnfjörð Bjarmason | fe0f944 | 2010-09-30 13:42:55 +0000 | [diff] [blame] | 626 | open my $c2, ">", $compose_filename . ".final" |
Pierre Habouzit | beece9d | 2008-11-11 00:54:02 +0100 | [diff] [blame] | 627 | or die "Failed to open $compose_filename.final : " . $!; |
| 628 | |
Ævar Arnfjörð Bjarmason | fe0f944 | 2010-09-30 13:42:55 +0000 | [diff] [blame] | 629 | open $c, "<", $compose_filename |
Pierre Habouzit | beece9d | 2008-11-11 00:54:02 +0100 | [diff] [blame] | 630 | or die "Failed to open $compose_filename : " . $!; |
| 631 | |
| 632 | my $need_8bit_cte = file_has_nonascii($compose_filename); |
| 633 | my $in_body = 0; |
| 634 | my $summary_empty = 1; |
Ævar Arnfjörð Bjarmason | fe0f944 | 2010-09-30 13:42:55 +0000 | [diff] [blame] | 635 | while(<$c>) { |
Michael Witten | 40e6e8a | 2009-04-13 13:23:50 -0500 | [diff] [blame] | 636 | next if m/^GIT:/; |
Pierre Habouzit | beece9d | 2008-11-11 00:54:02 +0100 | [diff] [blame] | 637 | if ($in_body) { |
| 638 | $summary_empty = 0 unless (/^\n$/); |
| 639 | } elsif (/^\n$/) { |
| 640 | $in_body = 1; |
| 641 | if ($need_8bit_cte) { |
Ævar Arnfjörð Bjarmason | fe0f944 | 2010-09-30 13:42:55 +0000 | [diff] [blame] | 642 | print $c2 "MIME-Version: 1.0\n", |
Pierre Habouzit | beece9d | 2008-11-11 00:54:02 +0100 | [diff] [blame] | 643 | "Content-Type: text/plain; ", |
Brandon Casey | d1fff6f | 2009-06-06 20:12:31 -0500 | [diff] [blame] | 644 | "charset=UTF-8\n", |
Pierre Habouzit | beece9d | 2008-11-11 00:54:02 +0100 | [diff] [blame] | 645 | "Content-Transfer-Encoding: 8bit\n"; |
| 646 | } |
| 647 | } elsif (/^MIME-Version:/i) { |
| 648 | $need_8bit_cte = 0; |
| 649 | } elsif (/^Subject:\s*(.+)\s*$/i) { |
| 650 | $initial_subject = $1; |
| 651 | my $subject = $initial_subject; |
| 652 | $_ = "Subject: " . |
| 653 | ($subject =~ /[^[:ascii:]]/ ? |
| 654 | quote_rfc2047($subject) : |
| 655 | $subject) . |
| 656 | "\n"; |
| 657 | } elsif (/^In-Reply-To:\s*(.+)\s*$/i) { |
| 658 | $initial_reply_to = $1; |
| 659 | next; |
| 660 | } elsif (/^From:\s*(.+)\s*$/i) { |
| 661 | $sender = $1; |
| 662 | next; |
| 663 | } elsif (/^(?:To|Cc|Bcc):/i) { |
| 664 | print "To/Cc/Bcc fields are not interpreted yet, they have been ignored\n"; |
| 665 | next; |
| 666 | } |
Ævar Arnfjörð Bjarmason | fe0f944 | 2010-09-30 13:42:55 +0000 | [diff] [blame] | 667 | print $c2 $_; |
Pierre Habouzit | beece9d | 2008-11-11 00:54:02 +0100 | [diff] [blame] | 668 | } |
Ævar Arnfjörð Bjarmason | fe0f944 | 2010-09-30 13:42:55 +0000 | [diff] [blame] | 669 | close $c; |
| 670 | close $c2; |
Pierre Habouzit | beece9d | 2008-11-11 00:54:02 +0100 | [diff] [blame] | 671 | |
| 672 | if ($summary_empty) { |
| 673 | print "Summary email is empty, skipping it\n"; |
| 674 | $compose = -1; |
| 675 | } |
| 676 | } elsif ($annotate) { |
| 677 | do_edit(@files); |
| 678 | } |
| 679 | |
Jay Soffian | 6e18251 | 2009-03-28 21:39:10 -0400 | [diff] [blame] | 680 | sub ask { |
| 681 | my ($prompt, %arg) = @_; |
Jay Soffian | 0da43a6 | 2009-04-04 23:23:21 -0400 | [diff] [blame] | 682 | my $valid_re = $arg{valid_re}; |
Jay Soffian | 6e18251 | 2009-03-28 21:39:10 -0400 | [diff] [blame] | 683 | my $default = $arg{default}; |
| 684 | my $resp; |
| 685 | my $i = 0; |
Jay Soffian | 5906f54 | 2009-03-31 12:22:11 -0400 | [diff] [blame] | 686 | return defined $default ? $default : undef |
| 687 | unless defined $term->IN and defined fileno($term->IN) and |
| 688 | defined $term->OUT and defined fileno($term->OUT); |
Jay Soffian | 6e18251 | 2009-03-28 21:39:10 -0400 | [diff] [blame] | 689 | while ($i++ < 10) { |
| 690 | $resp = $term->readline($prompt); |
| 691 | if (!defined $resp) { # EOF |
| 692 | print "\n"; |
| 693 | return defined $default ? $default : undef; |
| 694 | } |
| 695 | if ($resp eq '' and defined $default) { |
| 696 | return $default; |
| 697 | } |
Jay Soffian | 0da43a6 | 2009-04-04 23:23:21 -0400 | [diff] [blame] | 698 | if (!defined $valid_re or $resp =~ /$valid_re/) { |
Jay Soffian | 6e18251 | 2009-03-28 21:39:10 -0400 | [diff] [blame] | 699 | return $resp; |
| 700 | } |
| 701 | } |
| 702 | return undef; |
| 703 | } |
| 704 | |
Thomas Rast | 3cae7e5 | 2010-06-17 22:10:39 +0200 | [diff] [blame] | 705 | my %broken_encoding; |
| 706 | |
Ævar Arnfjörð Bjarmason | 1d50bfd | 2010-09-30 13:42:58 +0000 | [diff] [blame] | 707 | sub file_declares_8bit_cte { |
Thomas Rast | 3cae7e5 | 2010-06-17 22:10:39 +0200 | [diff] [blame] | 708 | my $fn = shift; |
| 709 | open (my $fh, '<', $fn); |
| 710 | while (my $line = <$fh>) { |
| 711 | last if ($line =~ /^$/); |
| 712 | return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/); |
| 713 | } |
| 714 | close $fh; |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | foreach my $f (@files) { |
| 719 | next unless (body_or_subject_has_nonascii($f) |
| 720 | && !file_declares_8bit_cte($f)); |
| 721 | $broken_encoding{$f} = 1; |
| 722 | } |
| 723 | |
| 724 | if (!defined $auto_8bit_encoding && scalar %broken_encoding) { |
| 725 | print "The following files are 8bit, but do not declare " . |
| 726 | "a Content-Transfer-Encoding.\n"; |
| 727 | foreach my $f (sort keys %broken_encoding) { |
| 728 | print " $f\n"; |
| 729 | } |
| 730 | $auto_8bit_encoding = ask("Which 8bit encoding should I declare [UTF-8]? ", |
| 731 | default => "UTF-8"); |
| 732 | } |
| 733 | |
Thomas Rast | a03bc5b | 2009-06-08 23:34:12 +0200 | [diff] [blame] | 734 | if (!$force) { |
| 735 | for my $f (@files) { |
Ævar Arnfjörð Bjarmason | 0d290a4 | 2010-09-30 13:43:01 +0000 | [diff] [blame] | 736 | if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) { |
Thomas Rast | a03bc5b | 2009-06-08 23:34:12 +0200 | [diff] [blame] | 737 | die "Refusing to send because the patch\n\t$f\n" |
| 738 | . "has the template subject '*** SUBJECT HERE ***'. " |
| 739 | . "Pass --force if you really want to send.\n"; |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
Ryan Anderson | 1f038a0 | 2005-09-05 01:13:07 -0400 | [diff] [blame] | 744 | my $prompting = 0; |
Uwe Kleine-König | 94638f8 | 2007-08-09 15:27:58 +0200 | [diff] [blame] | 745 | if (!defined $sender) { |
Frank Lichtenheld | ad79c02 | 2008-03-14 18:29:30 +0100 | [diff] [blame] | 746 | $sender = $repoauthor || $repocommitter || ''; |
Jay Soffian | 6e18251 | 2009-03-28 21:39:10 -0400 | [diff] [blame] | 747 | $sender = ask("Who should the emails appear to be from? [$sender] ", |
| 748 | default => $sender); |
Uwe Kleine-König | 94638f8 | 2007-08-09 15:27:58 +0200 | [diff] [blame] | 749 | print "Emails will be sent from: ", $sender, "\n"; |
Ryan Anderson | 1f038a0 | 2005-09-05 01:13:07 -0400 | [diff] [blame] | 750 | $prompting++; |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 751 | } |
| 752 | |
Junio C Hamano | 8796ff7 | 2010-10-26 22:02:03 -0700 | [diff] [blame] | 753 | if (!@initial_to && !defined $to_cmd) { |
Jay Soffian | 6e18251 | 2009-03-28 21:39:10 -0400 | [diff] [blame] | 754 | my $to = ask("Who should the emails be sent to? "); |
Stephen Boyd | 3c3bb51 | 2010-10-04 00:05:24 -0700 | [diff] [blame] | 755 | push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later |
Ryan Anderson | 1f038a0 | 2005-09-05 01:13:07 -0400 | [diff] [blame] | 756 | $prompting++; |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 757 | } |
| 758 | |
Eric Wong | 994d6c6 | 2006-05-14 19:13:44 -0700 | [diff] [blame] | 759 | sub expand_aliases { |
Jeff King | 302e04e | 2009-07-23 07:09:29 -0400 | [diff] [blame] | 760 | return map { expand_one_alias($_) } @_; |
| 761 | } |
| 762 | |
| 763 | my %EXPANDED_ALIASES; |
| 764 | sub expand_one_alias { |
| 765 | my $alias = shift; |
| 766 | if ($EXPANDED_ALIASES{$alias}) { |
| 767 | die "fatal: alias '$alias' expands to itself\n"; |
| 768 | } |
| 769 | local $EXPANDED_ALIASES{$alias} = 1; |
| 770 | return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias; |
Eric Wong | 994d6c6 | 2006-05-14 19:13:44 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Stephen Boyd | 3c3bb51 | 2010-10-04 00:05:24 -0700 | [diff] [blame] | 773 | @initial_to = expand_aliases(@initial_to); |
| 774 | @initial_to = (map { sanitize_address($_) } @initial_to); |
Eric Wong | 994d6c6 | 2006-05-14 19:13:44 -0700 | [diff] [blame] | 775 | @initial_cc = expand_aliases(@initial_cc); |
Ryan Anderson | 5806324 | 2006-05-29 12:30:13 -0700 | [diff] [blame] | 776 | @bcclist = expand_aliases(@bcclist); |
Eric Wong | 994d6c6 | 2006-05-14 19:13:44 -0700 | [diff] [blame] | 777 | |
Adam Roben | 5483c71 | 2007-06-27 20:59:37 -0700 | [diff] [blame] | 778 | if ($thread && !defined $initial_reply_to && $prompting) { |
Jay Soffian | 6e18251 | 2009-03-28 21:39:10 -0400 | [diff] [blame] | 779 | $initial_reply_to = ask( |
| 780 | "Message-ID to be used as In-Reply-To for the first email? "); |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 781 | } |
Jay Soffian | 1ca3d6e | 2008-02-20 00:55:07 -0500 | [diff] [blame] | 782 | if (defined $initial_reply_to) { |
Jay Soffian | 0fb7fc7 | 2008-02-21 19:16:04 -0500 | [diff] [blame] | 783 | $initial_reply_to =~ s/^\s*<?//; |
| 784 | $initial_reply_to =~ s/>?\s*$//; |
| 785 | $initial_reply_to = "<$initial_reply_to>" if $initial_reply_to ne ''; |
Junio C Hamano | ace9c2a | 2007-12-10 21:44:42 -0800 | [diff] [blame] | 786 | } |
Mike Hommey | ace7208 | 2007-12-09 18:17:28 +0100 | [diff] [blame] | 787 | |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 788 | if (!defined $smtp_server) { |
Eric Wong | aca7ad7 | 2006-05-15 02:34:44 -0700 | [diff] [blame] | 789 | foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) { |
| 790 | if (-x $_) { |
| 791 | $smtp_server = $_; |
| 792 | last; |
| 793 | } |
| 794 | } |
| 795 | $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug* |
Ryan Anderson | 3342d85 | 2005-07-31 20:04:24 -0400 | [diff] [blame] | 796 | } |
| 797 | |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 798 | if ($compose && $compose > 0) { |
| 799 | @files = ($compose_filename . ".final", @files); |
Ryan Anderson | 1f038a0 | 2005-09-05 01:13:07 -0400 | [diff] [blame] | 800 | } |
| 801 | |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 802 | # Variables we set as part of the loop over files |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 803 | our ($message_id, %mail, $subject, $reply_to, $references, $message, |
Jay Soffian | dc1460a | 2009-03-31 12:22:12 -0400 | [diff] [blame] | 804 | $needs_confirm, $message_num, $ask_default); |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 805 | |
Eric Wong | 567ffeb | 2006-03-25 16:47:12 -0800 | [diff] [blame] | 806 | sub extract_valid_address { |
| 807 | my $address = shift; |
Ævar Arnfjörð Bjarmason | 35b6ab9 | 2010-09-30 19:03:31 +0000 | [diff] [blame] | 808 | my $local_part_regexp = qr/[^<>"\s@]+/; |
| 809 | my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/; |
Eric Wong | db3106b | 2006-05-15 02:41:01 -0700 | [diff] [blame] | 810 | |
| 811 | # check for a local address: |
Junio C Hamano | ad9c18f | 2006-06-06 00:05:56 -0700 | [diff] [blame] | 812 | return $address if ($address =~ /^($local_part_regexp)$/); |
Eric Wong | db3106b | 2006-05-15 02:41:01 -0700 | [diff] [blame] | 813 | |
Uwe Kleine-König | 155197e | 2007-08-09 15:27:57 +0200 | [diff] [blame] | 814 | $address =~ s/^\s*<(.*)>\s*$/$1/; |
Eric Wong | 567ffeb | 2006-03-25 16:47:12 -0800 | [diff] [blame] | 815 | if ($have_email_valid) { |
Junio C Hamano | ad9c18f | 2006-06-06 00:05:56 -0700 | [diff] [blame] | 816 | return scalar Email::Valid->address($address); |
Eric Wong | 567ffeb | 2006-03-25 16:47:12 -0800 | [diff] [blame] | 817 | } else { |
| 818 | # less robust/correct than the monster regexp in Email::Valid, |
| 819 | # but still does a 99% job, and one less dependency |
Junio C Hamano | ad9c18f | 2006-06-06 00:05:56 -0700 | [diff] [blame] | 820 | $address =~ /($local_part_regexp\@$domain_regexp)/; |
Horst H. von Brand | e96fd30 | 2006-06-03 13:11:48 -0400 | [diff] [blame] | 821 | return $1; |
Eric Wong | 567ffeb | 2006-03-25 16:47:12 -0800 | [diff] [blame] | 822 | } |
| 823 | } |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 824 | |
| 825 | # Usually don't need to change anything below here. |
| 826 | |
| 827 | # we make a "fake" message id by taking the current number |
| 828 | # of seconds since the beginning of Unix time and tacking on |
| 829 | # a random number to the end, in case we are called quicker than |
| 830 | # 1 second since the last time we were called. |
Ryan Anderson | 8037d1a | 2005-07-31 20:04:24 -0400 | [diff] [blame] | 831 | |
| 832 | # We'll setup a template for the message id, using the "from" address: |
Ryan Anderson | 8037d1a | 2005-07-31 20:04:24 -0400 | [diff] [blame] | 833 | |
Junio C Hamano | be510cf | 2007-09-17 21:18:20 -0700 | [diff] [blame] | 834 | my ($message_id_stamp, $message_id_serial); |
Brian Gernhardt | 68ce933 | 2010-04-10 10:53:53 -0400 | [diff] [blame] | 835 | sub make_message_id { |
Junio C Hamano | be510cf | 2007-09-17 21:18:20 -0700 | [diff] [blame] | 836 | my $uniq; |
| 837 | if (!defined $message_id_stamp) { |
| 838 | $message_id_stamp = sprintf("%s-%s", time, $$); |
| 839 | $message_id_serial = 0; |
| 840 | } |
| 841 | $message_id_serial++; |
| 842 | $uniq = "$message_id_stamp-$message_id_serial"; |
| 843 | |
Junio C Hamano | aeb5932 | 2007-06-20 13:47:34 -0700 | [diff] [blame] | 844 | my $du_part; |
Uwe Kleine-König | 94638f8 | 2007-08-09 15:27:58 +0200 | [diff] [blame] | 845 | for ($sender, $repocommitter, $repoauthor) { |
| 846 | $du_part = extract_valid_address(sanitize_address($_)); |
| 847 | last if (defined $du_part and $du_part ne ''); |
Junio C Hamano | aeb5932 | 2007-06-20 13:47:34 -0700 | [diff] [blame] | 848 | } |
Uwe Kleine-König | 94638f8 | 2007-08-09 15:27:58 +0200 | [diff] [blame] | 849 | if (not defined $du_part or $du_part eq '') { |
Ævar Arnfjörð Bjarmason | 529dd38 | 2010-09-30 13:43:08 +0000 | [diff] [blame] | 850 | require Sys::Hostname; |
Junio C Hamano | aeb5932 | 2007-06-20 13:47:34 -0700 | [diff] [blame] | 851 | $du_part = 'user@' . Sys::Hostname::hostname(); |
| 852 | } |
Junio C Hamano | be510cf | 2007-09-17 21:18:20 -0700 | [diff] [blame] | 853 | my $message_id_template = "<%s-git-send-email-%s>"; |
| 854 | $message_id = sprintf($message_id_template, $uniq, $du_part); |
Ryan Anderson | 8037d1a | 2005-07-31 20:04:24 -0400 | [diff] [blame] | 855 | #print "new message id = $message_id\n"; # Was useful for debugging |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | |
| 859 | |
Eric Wong | a5370b1 | 2006-03-25 03:01:01 -0800 | [diff] [blame] | 860 | $time = time - scalar $#files; |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 861 | |
Jürgen Rühle | 374c590 | 2007-01-10 13:36:39 -0800 | [diff] [blame] | 862 | sub unquote_rfc2047 { |
| 863 | local ($_) = @_; |
Jeff King | 8291db6 | 2007-11-16 05:49:09 -0500 | [diff] [blame] | 864 | my $encoding; |
| 865 | if (s/=\?([^?]+)\?q\?(.*)\?=/$2/g) { |
| 866 | $encoding = $1; |
Jürgen Rühle | 374c590 | 2007-01-10 13:36:39 -0800 | [diff] [blame] | 867 | s/_/ /g; |
| 868 | s/=([0-9A-F]{2})/chr(hex($1))/eg; |
| 869 | } |
Jeff King | 8291db6 | 2007-11-16 05:49:09 -0500 | [diff] [blame] | 870 | return wantarray ? ($_, $encoding) : $_; |
Jürgen Rühle | 374c590 | 2007-01-10 13:36:39 -0800 | [diff] [blame] | 871 | } |
| 872 | |
Jeff King | d54eaaa | 2008-03-28 17:29:01 -0400 | [diff] [blame] | 873 | sub quote_rfc2047 { |
| 874 | local $_ = shift; |
Brandon Casey | d1fff6f | 2009-06-06 20:12:31 -0500 | [diff] [blame] | 875 | my $encoding = shift || 'UTF-8'; |
Jeff King | d54eaaa | 2008-03-28 17:29:01 -0400 | [diff] [blame] | 876 | s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg; |
| 877 | s/(.*)/=\?$encoding\?q\?$1\?=/; |
| 878 | return $_; |
| 879 | } |
| 880 | |
Brandon Casey | a3a8262 | 2009-06-07 19:25:58 -0500 | [diff] [blame] | 881 | sub is_rfc2047_quoted { |
| 882 | my $s = shift; |
Ævar Arnfjörð Bjarmason | 49f7385 | 2010-09-30 13:43:05 +0000 | [diff] [blame] | 883 | my $token = qr/[^][()<>@,;:"\/?.= \000-\037\177-\377]+/; |
| 884 | my $encoded_text = qr/[!->@-~]+/; |
Brandon Casey | a3a8262 | 2009-06-07 19:25:58 -0500 | [diff] [blame] | 885 | length($s) <= 75 && |
| 886 | $s =~ m/^(?:"[[:ascii:]]*"|=\?$token\?$token\?$encoded_text\?=)$/o; |
| 887 | } |
| 888 | |
Uwe Kleine-K,Av(Bnig | 5b56aaa | 2007-08-06 22:34:50 +0200 | [diff] [blame] | 889 | # use the simplest quoting being able to handle the recipient |
Brian Gernhardt | 68ce933 | 2010-04-10 10:53:53 -0400 | [diff] [blame] | 890 | sub sanitize_address { |
Robin H. Johnson | 732263d | 2007-04-25 19:37:19 -0700 | [diff] [blame] | 891 | my ($recipient) = @_; |
Uwe Kleine-K,Av(Bnig | 5b56aaa | 2007-08-06 22:34:50 +0200 | [diff] [blame] | 892 | my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/); |
| 893 | |
| 894 | if (not $recipient_name) { |
Ævar Arnfjörð Bjarmason | ff48389 | 2010-09-30 13:43:02 +0000 | [diff] [blame] | 895 | return $recipient; |
Robin H. Johnson | 732263d | 2007-04-25 19:37:19 -0700 | [diff] [blame] | 896 | } |
Uwe Kleine-K,Av(Bnig | 5b56aaa | 2007-08-06 22:34:50 +0200 | [diff] [blame] | 897 | |
| 898 | # if recipient_name is already quoted, do nothing |
Brandon Casey | a3a8262 | 2009-06-07 19:25:58 -0500 | [diff] [blame] | 899 | if (is_rfc2047_quoted($recipient_name)) { |
Uwe Kleine-K,Av(Bnig | 5b56aaa | 2007-08-06 22:34:50 +0200 | [diff] [blame] | 900 | return $recipient; |
| 901 | } |
| 902 | |
| 903 | # rfc2047 is needed if a non-ascii char is included |
| 904 | if ($recipient_name =~ /[^[:ascii:]]/) { |
Jay Soffian | a61c0ff | 2009-03-31 12:22:14 -0400 | [diff] [blame] | 905 | $recipient_name =~ s/^"(.*)"$/$1/; |
Jeff King | d54eaaa | 2008-03-28 17:29:01 -0400 | [diff] [blame] | 906 | $recipient_name = quote_rfc2047($recipient_name); |
Uwe Kleine-K,Av(Bnig | 5b56aaa | 2007-08-06 22:34:50 +0200 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | # double quotes are needed if specials or CTLs are included |
| 910 | elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) { |
Horst H. von Brand | 18023c2 | 2008-03-28 11:09:04 -0300 | [diff] [blame] | 911 | $recipient_name =~ s/(["\\\r])/\\$1/g; |
Ævar Arnfjörð Bjarmason | d5c7d69 | 2010-09-30 13:43:03 +0000 | [diff] [blame] | 912 | $recipient_name = qq["$recipient_name"]; |
Uwe Kleine-K,Av(Bnig | 5b56aaa | 2007-08-06 22:34:50 +0200 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | return "$recipient_name $recipient_addr"; |
| 916 | |
Robin H. Johnson | 732263d | 2007-04-25 19:37:19 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Jari Aalto | 134550f | 2010-03-14 17:16:45 +0200 | [diff] [blame] | 919 | # Returns the local Fully Qualified Domain Name (FQDN) if available. |
| 920 | # |
| 921 | # Tightly configured MTAa require that a caller sends a real DNS |
| 922 | # domain name that corresponds the IP address in the HELO/EHLO |
| 923 | # handshake. This is used to verify the connection and prevent |
| 924 | # spammers from trying to hide their identity. If the DNS and IP don't |
| 925 | # match, the receiveing MTA may deny the connection. |
| 926 | # |
| 927 | # Here is a deny example of Net::SMTP with the default "localhost.localdomain" |
| 928 | # |
| 929 | # Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain |
| 930 | # Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host |
| 931 | # |
| 932 | # This maildomain*() code is based on ideas in Perl library Test::Reporter |
| 933 | # /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain () |
| 934 | |
Brian Gernhardt | 59a8630 | 2010-04-10 10:53:54 -0400 | [diff] [blame] | 935 | sub valid_fqdn { |
| 936 | my $domain = shift; |
Brandon Casey | 61ef5e9 | 2010-09-26 22:18:01 -0500 | [diff] [blame] | 937 | return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./; |
Brian Gernhardt | 59a8630 | 2010-04-10 10:53:54 -0400 | [diff] [blame] | 938 | } |
| 939 | |
Brian Gernhardt | 68ce933 | 2010-04-10 10:53:53 -0400 | [diff] [blame] | 940 | sub maildomain_net { |
Jari Aalto | 134550f | 2010-03-14 17:16:45 +0200 | [diff] [blame] | 941 | my $maildomain; |
| 942 | |
| 943 | if (eval { require Net::Domain; 1 }) { |
| 944 | my $domain = Net::Domain::domainname(); |
Brian Gernhardt | 59a8630 | 2010-04-10 10:53:54 -0400 | [diff] [blame] | 945 | $maildomain = $domain if valid_fqdn($domain); |
Jari Aalto | 134550f | 2010-03-14 17:16:45 +0200 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | return $maildomain; |
| 949 | } |
| 950 | |
Brian Gernhardt | 68ce933 | 2010-04-10 10:53:53 -0400 | [diff] [blame] | 951 | sub maildomain_mta { |
Jari Aalto | 134550f | 2010-03-14 17:16:45 +0200 | [diff] [blame] | 952 | my $maildomain; |
| 953 | |
| 954 | if (eval { require Net::SMTP; 1 }) { |
| 955 | for my $host (qw(mailhost localhost)) { |
| 956 | my $smtp = Net::SMTP->new($host); |
| 957 | if (defined $smtp) { |
| 958 | my $domain = $smtp->domain; |
| 959 | $smtp->quit; |
| 960 | |
Brian Gernhardt | 59a8630 | 2010-04-10 10:53:54 -0400 | [diff] [blame] | 961 | $maildomain = $domain if valid_fqdn($domain); |
Jari Aalto | 134550f | 2010-03-14 17:16:45 +0200 | [diff] [blame] | 962 | |
| 963 | last if $maildomain; |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | return $maildomain; |
| 969 | } |
| 970 | |
Brian Gernhardt | 68ce933 | 2010-04-10 10:53:53 -0400 | [diff] [blame] | 971 | sub maildomain { |
Brian Gernhardt | 69cf7bf | 2010-04-10 10:53:56 -0400 | [diff] [blame] | 972 | return maildomain_net() || maildomain_mta() || 'localhost.localdomain'; |
Jari Aalto | 134550f | 2010-03-14 17:16:45 +0200 | [diff] [blame] | 973 | } |
| 974 | |
Michael Witten | 15da108 | 2009-04-13 13:23:51 -0500 | [diff] [blame] | 975 | # Returns 1 if the message was sent, and 0 otherwise. |
Markus Heidelberg | a1b5b37 | 2009-06-12 12:51:42 +0200 | [diff] [blame] | 976 | # In actuality, the whole program dies when there |
Michael Witten | 15da108 | 2009-04-13 13:23:51 -0500 | [diff] [blame] | 977 | # is an error sending a message. |
| 978 | |
Brian Gernhardt | 68ce933 | 2010-04-10 10:53:53 -0400 | [diff] [blame] | 979 | sub send_message { |
Eric Wong | 4bc87a2 | 2006-03-25 17:20:48 -0800 | [diff] [blame] | 980 | my @recipients = unique_email_list(@to); |
Ask Bjørn Hansen | 7ac1752 | 2007-11-19 03:00:26 -0800 | [diff] [blame] | 981 | @cc = (grep { my $cc = extract_valid_address($_); |
Joe Perches | 83acaae | 2010-11-20 15:06:05 -0800 | [diff] [blame] | 982 | not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients |
Ask Bjørn Hansen | 7ac1752 | 2007-11-19 03:00:26 -0800 | [diff] [blame] | 983 | } |
| 984 | map { sanitize_address($_) } |
| 985 | @cc); |
Eric Wong | 4bc87a2 | 2006-03-25 17:20:48 -0800 | [diff] [blame] | 986 | my $to = join (",\n\t", @recipients); |
Ryan Anderson | 5806324 | 2006-05-29 12:30:13 -0700 | [diff] [blame] | 987 | @recipients = unique_email_list(@recipients,@cc,@bcclist); |
Robin H. Johnson | c38f024 | 2007-04-25 19:37:20 -0700 | [diff] [blame] | 988 | @recipients = (map { extract_valid_address($_) } @recipients); |
Jakub Narebski | 6bdca89 | 2006-07-07 20:57:55 +0200 | [diff] [blame] | 989 | my $date = format_2822_time($time++); |
Martin Langhoff | e923eff | 2006-05-03 09:44:36 +1200 | [diff] [blame] | 990 | my $gitversion = '@@GIT_VERSION@@'; |
| 991 | if ($gitversion =~ m/..GIT_VERSION../) { |
Petr Baudis | 3cb8caf | 2006-07-03 22:47:58 +0200 | [diff] [blame] | 992 | $gitversion = Git::version(); |
Martin Langhoff | e923eff | 2006-05-03 09:44:36 +1200 | [diff] [blame] | 993 | } |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 994 | |
Joe Perches | 02461e0 | 2009-10-08 10:03:26 -0700 | [diff] [blame] | 995 | my $cc = join(",\n\t", unique_email_list(@cc)); |
Junio C Hamano | f06a6a4 | 2007-04-16 16:51:47 -0700 | [diff] [blame] | 996 | my $ccline = ""; |
| 997 | if ($cc ne '') { |
| 998 | $ccline = "\nCc: $cc"; |
| 999 | } |
Uwe Kleine-König | 94638f8 | 2007-08-09 15:27:58 +0200 | [diff] [blame] | 1000 | my $sanitized_sender = sanitize_address($sender); |
Jeff King | 4f3d370 | 2007-12-17 15:51:34 -0500 | [diff] [blame] | 1001 | make_message_id() unless defined($message_id); |
Junio C Hamano | aeb5932 | 2007-06-20 13:47:34 -0700 | [diff] [blame] | 1002 | |
Uwe Kleine-König | 94638f8 | 2007-08-09 15:27:58 +0200 | [diff] [blame] | 1003 | my $header = "From: $sanitized_sender |
Junio C Hamano | f06a6a4 | 2007-04-16 16:51:47 -0700 | [diff] [blame] | 1004 | To: $to${ccline} |
Eric Wong | 4bc87a2 | 2006-03-25 17:20:48 -0800 | [diff] [blame] | 1005 | Subject: $subject |
Eric Wong | 4bc87a2 | 2006-03-25 17:20:48 -0800 | [diff] [blame] | 1006 | Date: $date |
| 1007 | Message-Id: $message_id |
Martin Langhoff | e923eff | 2006-05-03 09:44:36 +1200 | [diff] [blame] | 1008 | X-Mailer: git-send-email $gitversion |
Eric Wong | 4bc87a2 | 2006-03-25 17:20:48 -0800 | [diff] [blame] | 1009 | "; |
Thomas Rast | 3e0c4ff | 2009-03-01 23:45:41 +0100 | [diff] [blame] | 1010 | if ($reply_to) { |
Ryan Anderson | 7ccf792 | 2006-05-29 12:30:12 -0700 | [diff] [blame] | 1011 | |
| 1012 | $header .= "In-Reply-To: $reply_to\n"; |
| 1013 | $header .= "References: $references\n"; |
| 1014 | } |
Junio C Hamano | ce91c2f | 2006-10-05 16:36:49 -0700 | [diff] [blame] | 1015 | if (@xh) { |
| 1016 | $header .= join("\n", @xh) . "\n"; |
| 1017 | } |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 1018 | |
Robin H. Johnson | c38f024 | 2007-04-25 19:37:20 -0700 | [diff] [blame] | 1019 | my @sendmail_parameters = ('-i', @recipients); |
Uwe Kleine-König | 94638f8 | 2007-08-09 15:27:58 +0200 | [diff] [blame] | 1020 | my $raw_from = $sanitized_sender; |
Felipe Contreras | c89e324 | 2009-11-26 21:04:29 +0200 | [diff] [blame] | 1021 | if (defined $envelope_sender && $envelope_sender ne "auto") { |
| 1022 | $raw_from = $envelope_sender; |
| 1023 | } |
Robin H. Johnson | f073a59 | 2007-04-25 19:37:22 -0700 | [diff] [blame] | 1024 | $raw_from = extract_valid_address($raw_from); |
| 1025 | unshift (@sendmail_parameters, |
| 1026 | '-f', $raw_from) if(defined $envelope_sender); |
Robin H. Johnson | 8e3d436 | 2007-04-25 19:37:17 -0700 | [diff] [blame] | 1027 | |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 1028 | if ($needs_confirm && !$dry_run) { |
| 1029 | print "\n$header\n"; |
| 1030 | if ($needs_confirm eq "inform") { |
| 1031 | $confirm_unconfigured = 0; # squelch this message for the rest of this run |
Jay Soffian | 6e18251 | 2009-03-28 21:39:10 -0400 | [diff] [blame] | 1032 | $ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 1033 | print " The Cc list above has been expanded by additional\n"; |
| 1034 | print " addresses found in the patch commit message. By default\n"; |
| 1035 | print " send-email prompts before sending whenever this occurs.\n"; |
| 1036 | print " This behavior is controlled by the sendemail.confirm\n"; |
| 1037 | print " configuration setting.\n"; |
| 1038 | print "\n"; |
| 1039 | print " For additional information, run 'git send-email --help'.\n"; |
| 1040 | print " To retain the current behavior, but squelch this message,\n"; |
| 1041 | print " run 'git config --global sendemail.confirm auto'.\n\n"; |
| 1042 | } |
Jay Soffian | 6e18251 | 2009-03-28 21:39:10 -0400 | [diff] [blame] | 1043 | $_ = ask("Send this email? ([y]es|[n]o|[q]uit|[a]ll): ", |
| 1044 | valid_re => qr/^(?:yes|y|no|n|quit|q|all|a)/i, |
| 1045 | default => $ask_default); |
| 1046 | die "Send this email reply required" unless defined $_; |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 1047 | if (/^n/i) { |
Michael Witten | 15da108 | 2009-04-13 13:23:51 -0500 | [diff] [blame] | 1048 | return 0; |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 1049 | } elsif (/^q/i) { |
| 1050 | cleanup_compose_files(); |
| 1051 | exit(0); |
| 1052 | } elsif (/^a/i) { |
| 1053 | $confirm = 'never'; |
| 1054 | } |
| 1055 | } |
| 1056 | |
Pascal Obry | 052fbea | 2010-09-06 20:12:11 +0200 | [diff] [blame] | 1057 | unshift (@sendmail_parameters, @smtp_server_options); |
| 1058 | |
Matthew Wilcox | 6130259 | 2006-10-10 08:58:23 -0600 | [diff] [blame] | 1059 | if ($dry_run) { |
| 1060 | # We don't want to send the email. |
| 1061 | } elsif ($smtp_server =~ m#^/#) { |
Eric Wong | aca7ad7 | 2006-05-15 02:34:44 -0700 | [diff] [blame] | 1062 | my $pid = open my $sm, '|-'; |
| 1063 | defined $pid or die $!; |
| 1064 | if (!$pid) { |
Robin H. Johnson | 8e3d436 | 2007-04-25 19:37:17 -0700 | [diff] [blame] | 1065 | exec($smtp_server, @sendmail_parameters) or die $!; |
Eric Wong | aca7ad7 | 2006-05-15 02:34:44 -0700 | [diff] [blame] | 1066 | } |
| 1067 | print $sm "$header\n$message"; |
Ævar Arnfjörð Bjarmason | 5e2c2ab | 2010-09-30 13:43:07 +0000 | [diff] [blame] | 1068 | close $sm or die $!; |
Eric Wong | aca7ad7 | 2006-05-15 02:34:44 -0700 | [diff] [blame] | 1069 | } else { |
Junio C Hamano | 44b2476 | 2007-09-25 17:27:54 -0700 | [diff] [blame] | 1070 | |
| 1071 | if (!defined $smtp_server) { |
| 1072 | die "The required SMTP server is not properly defined." |
| 1073 | } |
| 1074 | |
Thomas Rast | f6bebd1 | 2008-06-25 21:42:43 +0200 | [diff] [blame] | 1075 | if ($smtp_encryption eq 'ssl') { |
Junio C Hamano | 44b2476 | 2007-09-25 17:27:54 -0700 | [diff] [blame] | 1076 | $smtp_server_port ||= 465; # ssmtp |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 1077 | require Net::SMTP::SSL; |
Brian Gernhardt | 69cf7bf | 2010-04-10 10:53:56 -0400 | [diff] [blame] | 1078 | $smtp_domain ||= maildomain(); |
Jari Aalto | 134550f | 2010-03-14 17:16:45 +0200 | [diff] [blame] | 1079 | $smtp ||= Net::SMTP::SSL->new($smtp_server, |
Brian Gernhardt | 69cf7bf | 2010-04-10 10:53:56 -0400 | [diff] [blame] | 1080 | Hello => $smtp_domain, |
Jari Aalto | 134550f | 2010-03-14 17:16:45 +0200 | [diff] [blame] | 1081 | Port => $smtp_server_port); |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 1082 | } |
| 1083 | else { |
| 1084 | require Net::SMTP; |
Brian Gernhardt | 69cf7bf | 2010-04-10 10:53:56 -0400 | [diff] [blame] | 1085 | $smtp_domain ||= maildomain(); |
Junio C Hamano | 44b2476 | 2007-09-25 17:27:54 -0700 | [diff] [blame] | 1086 | $smtp ||= Net::SMTP->new((defined $smtp_server_port) |
| 1087 | ? "$smtp_server:$smtp_server_port" |
Jari Aalto | f60812e | 2010-03-14 17:16:09 +0200 | [diff] [blame] | 1088 | : $smtp_server, |
Brian Gernhardt | 69cf7bf | 2010-04-10 10:53:56 -0400 | [diff] [blame] | 1089 | Hello => $smtp_domain, |
Jari Aalto | f60812e | 2010-03-14 17:16:09 +0200 | [diff] [blame] | 1090 | Debug => $debug_net_smtp); |
Yakov Lerner | fb3650e | 2009-09-25 15:10:21 -0700 | [diff] [blame] | 1091 | if ($smtp_encryption eq 'tls' && $smtp) { |
Thomas Rast | f6bebd1 | 2008-06-25 21:42:43 +0200 | [diff] [blame] | 1092 | require Net::SMTP::SSL; |
| 1093 | $smtp->command('STARTTLS'); |
| 1094 | $smtp->response(); |
| 1095 | if ($smtp->code == 220) { |
| 1096 | $smtp = Net::SMTP::SSL->start_SSL($smtp) |
| 1097 | or die "STARTTLS failed! ".$smtp->message; |
Thomas Rast | 6cbf8b0 | 2008-07-03 00:11:31 +0200 | [diff] [blame] | 1098 | $smtp_encryption = ''; |
Robert Shearman | 9d1ccf5 | 2008-07-09 22:39:40 +0100 | [diff] [blame] | 1099 | # Send EHLO again to receive fresh |
| 1100 | # supported commands |
Matthew Daley | 155b940 | 2011-10-15 04:44:52 -0400 | [diff] [blame] | 1101 | $smtp->hello($smtp_domain); |
Thomas Rast | f6bebd1 | 2008-06-25 21:42:43 +0200 | [diff] [blame] | 1102 | } else { |
| 1103 | die "Server does not support STARTTLS! ".$smtp->message; |
| 1104 | } |
| 1105 | } |
Douglas Stockwell | 34cc60c | 2007-09-03 03:06:25 +0900 | [diff] [blame] | 1106 | } |
Junio C Hamano | 44b2476 | 2007-09-25 17:27:54 -0700 | [diff] [blame] | 1107 | |
| 1108 | if (!$smtp) { |
Jari Aalto | f60812e | 2010-03-14 17:16:09 +0200 | [diff] [blame] | 1109 | die "Unable to initialize SMTP properly. Check config and use --smtp-debug. ", |
Jari Aalto | e5afb3a | 2010-03-14 17:15:33 +0200 | [diff] [blame] | 1110 | "VALUES: server=$smtp_server ", |
| 1111 | "encryption=$smtp_encryption ", |
Brian Gernhardt | 69cf7bf | 2010-04-10 10:53:56 -0400 | [diff] [blame] | 1112 | "hello=$smtp_domain", |
Sylvain Rabot | a1dd7e1 | 2011-04-29 20:23:24 +0200 | [diff] [blame] | 1113 | defined $smtp_server_port ? " port=$smtp_server_port" : ""; |
Junio C Hamano | 44b2476 | 2007-09-25 17:27:54 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
Michael Witten | 2363d74 | 2008-02-03 19:53:56 -0500 | [diff] [blame] | 1116 | if (defined $smtp_authuser) { |
Zbigniew Jędrzejewski-Szmek | ccdbdc7 | 2011-09-29 19:02:45 +0200 | [diff] [blame] | 1117 | # Workaround AUTH PLAIN/LOGIN interaction defect |
| 1118 | # with Authen::SASL::Cyrus |
| 1119 | eval { |
| 1120 | require Authen::SASL; |
| 1121 | Authen::SASL->import(qw(Perl)); |
| 1122 | }; |
Michael Witten | 2363d74 | 2008-02-03 19:53:56 -0500 | [diff] [blame] | 1123 | |
| 1124 | if (!defined $smtp_authpass) { |
| 1125 | |
| 1126 | system "stty -echo"; |
| 1127 | |
| 1128 | do { |
| 1129 | print "Password: "; |
| 1130 | $_ = <STDIN>; |
| 1131 | print "\n"; |
| 1132 | } while (!defined $_); |
| 1133 | |
| 1134 | chomp($smtp_authpass = $_); |
| 1135 | |
| 1136 | system "stty echo"; |
| 1137 | } |
| 1138 | |
Wincent Colaiuta | 5f5b611 | 2007-11-21 13:35:05 +0100 | [diff] [blame] | 1139 | $auth ||= $smtp->auth( $smtp_authuser, $smtp_authpass ) or die $smtp->message; |
Junio C Hamano | 44b2476 | 2007-09-25 17:27:54 -0700 | [diff] [blame] | 1140 | } |
Michael Witten | 2363d74 | 2008-02-03 19:53:56 -0500 | [diff] [blame] | 1141 | |
Robin H. Johnson | 2b69bfc | 2007-04-25 19:37:21 -0700 | [diff] [blame] | 1142 | $smtp->mail( $raw_from ) or die $smtp->message; |
Eric Wong | aca7ad7 | 2006-05-15 02:34:44 -0700 | [diff] [blame] | 1143 | $smtp->to( @recipients ) or die $smtp->message; |
| 1144 | $smtp->data or die $smtp->message; |
| 1145 | $smtp->datasend("$header\n$message") or die $smtp->message; |
| 1146 | $smtp->dataend() or die $smtp->message; |
Michael Witten | 15da108 | 2009-04-13 13:23:51 -0500 | [diff] [blame] | 1147 | $smtp->code =~ /250|200/ or die "Failed to send $subject\n".$smtp->message; |
Eric Wong | aca7ad7 | 2006-05-15 02:34:44 -0700 | [diff] [blame] | 1148 | } |
Ryan Anderson | 2718435 | 2006-02-05 20:13:52 -0500 | [diff] [blame] | 1149 | if ($quiet) { |
Robin H. Johnson | 71c7da9 | 2007-04-25 19:37:16 -0700 | [diff] [blame] | 1150 | printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject); |
Ryan Anderson | 2718435 | 2006-02-05 20:13:52 -0500 | [diff] [blame] | 1151 | } else { |
David D. Kilzer | b7f30e0 | 2007-11-18 20:14:55 -0800 | [diff] [blame] | 1152 | print (($dry_run ? "Dry-" : "")."OK. Log says:\n"); |
Robin H. Johnson | 2b69bfc | 2007-04-25 19:37:21 -0700 | [diff] [blame] | 1153 | if ($smtp_server !~ m#^/#) { |
Eric Wong | aca7ad7 | 2006-05-15 02:34:44 -0700 | [diff] [blame] | 1154 | print "Server: $smtp_server\n"; |
Robin H. Johnson | 2b69bfc | 2007-04-25 19:37:21 -0700 | [diff] [blame] | 1155 | print "MAIL FROM:<$raw_from>\n"; |
Joe Perches | 02461e0 | 2009-10-08 10:03:26 -0700 | [diff] [blame] | 1156 | foreach my $entry (@recipients) { |
| 1157 | print "RCPT TO:<$entry>\n"; |
| 1158 | } |
Eric Wong | aca7ad7 | 2006-05-15 02:34:44 -0700 | [diff] [blame] | 1159 | } else { |
Robin H. Johnson | 8e3d436 | 2007-04-25 19:37:17 -0700 | [diff] [blame] | 1160 | print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n"; |
Eric Wong | aca7ad7 | 2006-05-15 02:34:44 -0700 | [diff] [blame] | 1161 | } |
David D. Kilzer | b7f30e0 | 2007-11-18 20:14:55 -0800 | [diff] [blame] | 1162 | print $header, "\n"; |
Eric Wong | aca7ad7 | 2006-05-15 02:34:44 -0700 | [diff] [blame] | 1163 | if ($smtp) { |
| 1164 | print "Result: ", $smtp->code, ' ', |
| 1165 | ($smtp->message =~ /\n([^\n]+\n)$/s), "\n"; |
| 1166 | } else { |
| 1167 | print "Result: OK\n"; |
| 1168 | } |
Ryan Anderson | 30d08b3 | 2006-02-02 11:56:06 -0500 | [diff] [blame] | 1169 | } |
Michael Witten | 15da108 | 2009-04-13 13:23:51 -0500 | [diff] [blame] | 1170 | |
| 1171 | return 1; |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 1172 | } |
| 1173 | |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 1174 | $reply_to = $initial_reply_to; |
Junio C Hamano | 2186d56 | 2006-05-29 23:53:13 -0700 | [diff] [blame] | 1175 | $references = $initial_reply_to || ''; |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 1176 | $subject = $initial_subject; |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 1177 | $message_num = 0; |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 1178 | |
| 1179 | foreach my $t (@files) { |
Ævar Arnfjörð Bjarmason | f9237e6 | 2010-09-30 13:42:56 +0000 | [diff] [blame] | 1180 | open my $fh, "<", $t or die "can't open file $t"; |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 1181 | |
Uwe Kleine-König | 94638f8 | 2007-08-09 15:27:58 +0200 | [diff] [blame] | 1182 | my $author = undef; |
Jeff King | 8291db6 | 2007-11-16 05:49:09 -0500 | [diff] [blame] | 1183 | my $author_encoding; |
| 1184 | my $has_content_type; |
| 1185 | my $body_encoding; |
Stephen Boyd | 3c3bb51 | 2010-10-04 00:05:24 -0700 | [diff] [blame] | 1186 | @to = (); |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 1187 | @cc = (); |
Junio C Hamano | ce91c2f | 2006-10-05 16:36:49 -0700 | [diff] [blame] | 1188 | @xh = (); |
Junio C Hamano | e6b0964 | 2006-10-07 03:09:05 -0700 | [diff] [blame] | 1189 | my $input_format = undef; |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 1190 | my @header = (); |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 1191 | $message = ""; |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 1192 | $message_num++; |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 1193 | # First unfold multiline header fields |
Ævar Arnfjörð Bjarmason | f9237e6 | 2010-09-30 13:42:56 +0000 | [diff] [blame] | 1194 | while(<$fh>) { |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 1195 | last if /^\s*$/; |
| 1196 | if (/^\s+\S/ and @header) { |
| 1197 | chomp($header[$#header]); |
| 1198 | s/^\s+/ /; |
| 1199 | $header[$#header] .= $_; |
| 1200 | } else { |
| 1201 | push(@header, $_); |
| 1202 | } |
| 1203 | } |
| 1204 | # Now parse the header |
| 1205 | foreach(@header) { |
| 1206 | if (/^From /) { |
| 1207 | $input_format = 'mbox'; |
| 1208 | next; |
| 1209 | } |
| 1210 | chomp; |
| 1211 | if (!defined $input_format && /^[-A-Za-z]+:\s/) { |
| 1212 | $input_format = 'mbox'; |
| 1213 | } |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 1214 | |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 1215 | if (defined $input_format && $input_format eq 'mbox') { |
| 1216 | if (/^Subject:\s+(.*)$/) { |
| 1217 | $subject = $1; |
| 1218 | } |
| 1219 | elsif (/^From:\s+(.*)$/) { |
| 1220 | ($author, $author_encoding) = unquote_rfc2047($1); |
| 1221 | next if $suppress_cc{'author'}; |
| 1222 | next if $suppress_cc{'self'} and $author eq $sender; |
| 1223 | printf("(mbox) Adding cc: %s from line '%s'\n", |
| 1224 | $1, $_) unless $quiet; |
| 1225 | push @cc, $1; |
| 1226 | } |
Stephen Boyd | 21802cd | 2010-09-29 00:26:44 -0700 | [diff] [blame] | 1227 | elsif (/^To:\s+(.*)$/) { |
| 1228 | foreach my $addr (parse_address_line($1)) { |
| 1229 | printf("(mbox) Adding to: %s from line '%s'\n", |
| 1230 | $addr, $_) unless $quiet; |
| 1231 | push @to, sanitize_address($addr); |
| 1232 | } |
| 1233 | } |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 1234 | elsif (/^Cc:\s+(.*)$/) { |
| 1235 | foreach my $addr (parse_address_line($1)) { |
| 1236 | if (unquote_rfc2047($addr) eq $sender) { |
David Brown | 6564828 | 2007-12-25 19:56:29 -0800 | [diff] [blame] | 1237 | next if ($suppress_cc{'self'}); |
David Brown | 6564828 | 2007-12-25 19:56:29 -0800 | [diff] [blame] | 1238 | } else { |
| 1239 | next if ($suppress_cc{'cc'}); |
Junio C Hamano | 8a8e623 | 2006-03-23 23:43:52 -0800 | [diff] [blame] | 1240 | } |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 1241 | printf("(mbox) Adding cc: %s from line '%s'\n", |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 1242 | $addr, $_) unless $quiet; |
| 1243 | push @cc, $addr; |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 1244 | } |
| 1245 | } |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 1246 | elsif (/^Content-type:/i) { |
| 1247 | $has_content_type = 1; |
| 1248 | if (/charset="?([^ "]+)/) { |
| 1249 | $body_encoding = $1; |
| 1250 | } |
| 1251 | push @xh, $_; |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 1252 | } |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 1253 | elsif (/^Message-Id: (.*)/i) { |
| 1254 | $message_id = $1; |
| 1255 | } |
| 1256 | elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) { |
| 1257 | push @xh, $_; |
| 1258 | } |
| 1259 | |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 1260 | } else { |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 1261 | # In the traditional |
| 1262 | # "send lots of email" format, |
| 1263 | # line 1 = cc |
| 1264 | # line 2 = subject |
| 1265 | # So let's support that, too. |
| 1266 | $input_format = 'lots'; |
| 1267 | if (@cc == 0 && !$suppress_cc{'cc'}) { |
| 1268 | printf("(non-mbox) Adding cc: %s from line '%s'\n", |
| 1269 | $_, $_) unless $quiet; |
| 1270 | push @cc, $_; |
| 1271 | } elsif (!defined $subject) { |
| 1272 | $subject = $_; |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 1273 | } |
| 1274 | } |
| 1275 | } |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 1276 | # Now parse the message body |
Ævar Arnfjörð Bjarmason | f9237e6 | 2010-09-30 13:42:56 +0000 | [diff] [blame] | 1277 | while(<$fh>) { |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 1278 | $message .= $_; |
| 1279 | if (/^(Signed-off-by|Cc): (.*)$/i) { |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 1280 | chomp; |
Jay Soffian | 3531e27 | 2009-02-14 23:32:15 -0500 | [diff] [blame] | 1281 | my ($what, $c) = ($1, $2); |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 1282 | chomp $c; |
Jay Soffian | 3531e27 | 2009-02-14 23:32:15 -0500 | [diff] [blame] | 1283 | if ($c eq $sender) { |
| 1284 | next if ($suppress_cc{'self'}); |
| 1285 | } else { |
| 1286 | next if $suppress_cc{'sob'} and $what =~ /Signed-off-by/i; |
| 1287 | next if $suppress_cc{'bodycc'} and $what =~ /Cc/i; |
| 1288 | } |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 1289 | push @cc, $c; |
Jay Soffian | 3531e27 | 2009-02-14 23:32:15 -0500 | [diff] [blame] | 1290 | printf("(body) Adding cc: %s from line '%s'\n", |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 1291 | $c, $_) unless $quiet; |
| 1292 | } |
| 1293 | } |
Ævar Arnfjörð Bjarmason | f9237e6 | 2010-09-30 13:42:56 +0000 | [diff] [blame] | 1294 | close $fh; |
Joe Perches | 324a8bd | 2007-08-17 18:51:12 -0700 | [diff] [blame] | 1295 | |
Joe Perches | 6e74e07 | 2010-09-24 10:03:00 -0700 | [diff] [blame] | 1296 | push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t) |
| 1297 | if defined $to_cmd; |
| 1298 | push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t) |
| 1299 | if defined $cc_cmd && !$suppress_cc{'cccmd'}; |
Joe Perches | 324a8bd | 2007-08-17 18:51:12 -0700 | [diff] [blame] | 1300 | |
Thomas Rast | 3cae7e5 | 2010-06-17 22:10:39 +0200 | [diff] [blame] | 1301 | if ($broken_encoding{$t} && !$has_content_type) { |
| 1302 | $has_content_type = 1; |
| 1303 | push @xh, "MIME-Version: 1.0", |
| 1304 | "Content-Type: text/plain; charset=$auto_8bit_encoding", |
| 1305 | "Content-Transfer-Encoding: 8bit"; |
| 1306 | $body_encoding = $auto_8bit_encoding; |
| 1307 | } |
| 1308 | |
| 1309 | if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) { |
| 1310 | $subject = quote_rfc2047($subject, $auto_8bit_encoding); |
| 1311 | } |
| 1312 | |
Jay Soffian | 5012699 | 2009-02-14 23:32:14 -0500 | [diff] [blame] | 1313 | if (defined $author and $author ne $sender) { |
Uwe Kleine-König | 94638f8 | 2007-08-09 15:27:58 +0200 | [diff] [blame] | 1314 | $message = "From: $author\n\n$message"; |
Jeff King | 8291db6 | 2007-11-16 05:49:09 -0500 | [diff] [blame] | 1315 | if (defined $author_encoding) { |
| 1316 | if ($has_content_type) { |
| 1317 | if ($body_encoding eq $author_encoding) { |
| 1318 | # ok, we already have the right encoding |
| 1319 | } |
| 1320 | else { |
| 1321 | # uh oh, we should re-encode |
| 1322 | } |
| 1323 | } |
| 1324 | else { |
Thomas Rast | 3cae7e5 | 2010-06-17 22:10:39 +0200 | [diff] [blame] | 1325 | $has_content_type = 1; |
Jeff King | 8291db6 | 2007-11-16 05:49:09 -0500 | [diff] [blame] | 1326 | push @xh, |
| 1327 | 'MIME-Version: 1.0', |
Jeff King | 8641ee3 | 2007-11-20 07:54:04 -0500 | [diff] [blame] | 1328 | "Content-Type: text/plain; charset=$author_encoding", |
| 1329 | 'Content-Transfer-Encoding: 8bit'; |
Jeff King | 8291db6 | 2007-11-16 05:49:09 -0500 | [diff] [blame] | 1330 | } |
| 1331 | } |
Junio C Hamano | 8a8e623 | 2006-03-23 23:43:52 -0800 | [diff] [blame] | 1332 | } |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 1333 | |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 1334 | $needs_confirm = ( |
| 1335 | $confirm eq "always" or |
| 1336 | ($confirm =~ /^(?:auto|cc)$/ && @cc) or |
| 1337 | ($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1)); |
| 1338 | $needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc); |
| 1339 | |
Stephen Boyd | 3c3bb51 | 2010-10-04 00:05:24 -0700 | [diff] [blame] | 1340 | @to = (@initial_to, @to); |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 1341 | @cc = (@initial_cc, @cc); |
| 1342 | |
Michael Witten | 15da108 | 2009-04-13 13:23:51 -0500 | [diff] [blame] | 1343 | my $message_was_sent = send_message(); |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 1344 | |
| 1345 | # set up for the next message |
Junio C Hamano | 95a877a | 2009-06-12 09:23:43 -0700 | [diff] [blame] | 1346 | if ($thread && $message_was_sent && |
Antonio Ospite | db54c8e | 2010-11-12 15:55:08 +0100 | [diff] [blame] | 1347 | (chain_reply_to() || !defined $reply_to || length($reply_to) == 0 || |
| 1348 | $message_num == 1)) { |
Ryan Anderson | 78488b2 | 2005-07-31 20:04:24 -0400 | [diff] [blame] | 1349 | $reply_to = $message_id; |
Ryan Anderson | 7ccf792 | 2006-05-29 12:30:12 -0700 | [diff] [blame] | 1350 | if (length $references > 0) { |
YOSHIFUJI Hideaki / 吉藤英明 | a925b89 | 2007-04-06 08:50:24 +0900 | [diff] [blame] | 1351 | $references .= "\n $message_id"; |
Ryan Anderson | 7ccf792 | 2006-05-29 12:30:12 -0700 | [diff] [blame] | 1352 | } else { |
| 1353 | $references = "$message_id"; |
| 1354 | } |
Ryan Anderson | 78488b2 | 2005-07-31 20:04:24 -0400 | [diff] [blame] | 1355 | } |
Jeff King | 4f3d370 | 2007-12-17 15:51:34 -0500 | [diff] [blame] | 1356 | $message_id = undef; |
Ryan Anderson | 83b2443 | 2005-07-31 04:17:25 -0400 | [diff] [blame] | 1357 | } |
Ryan Anderson | e205735 | 2005-08-02 21:45:22 -0400 | [diff] [blame] | 1358 | |
Joe Perches | 6e74e07 | 2010-09-24 10:03:00 -0700 | [diff] [blame] | 1359 | # Execute a command (e.g. $to_cmd) to get a list of email addresses |
| 1360 | # and return a results array |
| 1361 | sub recipients_cmd { |
| 1362 | my ($prefix, $what, $cmd, $file) = @_; |
| 1363 | |
| 1364 | my $sanitized_sender = sanitize_address($sender); |
| 1365 | my @addresses = (); |
Junio C Hamano | 7ebee44 | 2010-10-26 22:02:52 -0700 | [diff] [blame] | 1366 | open my $fh, "$cmd \Q$file\E |" |
Joe Perches | 6e74e07 | 2010-09-24 10:03:00 -0700 | [diff] [blame] | 1367 | or die "($prefix) Could not execute '$cmd'"; |
Junio C Hamano | 7ebee44 | 2010-10-26 22:02:52 -0700 | [diff] [blame] | 1368 | while (my $address = <$fh>) { |
Joe Perches | 6e74e07 | 2010-09-24 10:03:00 -0700 | [diff] [blame] | 1369 | $address =~ s/^\s*//g; |
| 1370 | $address =~ s/\s*$//g; |
| 1371 | $address = sanitize_address($address); |
| 1372 | next if ($address eq $sanitized_sender and $suppress_from); |
| 1373 | push @addresses, $address; |
| 1374 | printf("($prefix) Adding %s: %s from: '%s'\n", |
| 1375 | $what, $address, $cmd) unless $quiet; |
| 1376 | } |
Junio C Hamano | 7ebee44 | 2010-10-26 22:02:52 -0700 | [diff] [blame] | 1377 | close $fh |
Joe Perches | 6e74e07 | 2010-09-24 10:03:00 -0700 | [diff] [blame] | 1378 | or die "($prefix) failed to close pipe to '$cmd'"; |
| 1379 | return @addresses; |
| 1380 | } |
| 1381 | |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 1382 | cleanup_compose_files(); |
Ryan Anderson | 1f038a0 | 2005-09-05 01:13:07 -0400 | [diff] [blame] | 1383 | |
Ævar Arnfjörð Bjarmason | 4bf597e | 2010-09-30 13:43:00 +0000 | [diff] [blame] | 1384 | sub cleanup_compose_files { |
Jay Soffian | c1f2aa4 | 2009-03-02 23:52:18 -0500 | [diff] [blame] | 1385 | unlink($compose_filename, $compose_filename . ".final") if $compose; |
Ryan Anderson | 1f038a0 | 2005-09-05 01:13:07 -0400 | [diff] [blame] | 1386 | } |
| 1387 | |
Eric Wong | 4bc87a2 | 2006-03-25 17:20:48 -0800 | [diff] [blame] | 1388 | $smtp->quit if $smtp; |
Ryan Anderson | e205735 | 2005-08-02 21:45:22 -0400 | [diff] [blame] | 1389 | |
Ævar Arnfjörð Bjarmason | c438ea2 | 2010-09-30 13:42:59 +0000 | [diff] [blame] | 1390 | sub unique_email_list { |
Ryan Anderson | e205735 | 2005-08-02 21:45:22 -0400 | [diff] [blame] | 1391 | my %seen; |
| 1392 | my @emails; |
| 1393 | |
| 1394 | foreach my $entry (@_) { |
Eric Wong | db3106b | 2006-05-15 02:41:01 -0700 | [diff] [blame] | 1395 | if (my $clean = extract_valid_address($entry)) { |
| 1396 | $seen{$clean} ||= 0; |
| 1397 | next if $seen{$clean}++; |
| 1398 | push @emails, $entry; |
| 1399 | } else { |
| 1400 | print STDERR "W: unable to extract a valid address", |
| 1401 | " from: $entry\n"; |
| 1402 | } |
Ryan Anderson | e205735 | 2005-08-02 21:45:22 -0400 | [diff] [blame] | 1403 | } |
| 1404 | return @emails; |
| 1405 | } |
Jeff King | 747bbff | 2008-01-18 09:19:48 -0500 | [diff] [blame] | 1406 | |
| 1407 | sub validate_patch { |
| 1408 | my $fn = shift; |
| 1409 | open(my $fh, '<', $fn) |
| 1410 | or die "unable to open $fn: $!\n"; |
| 1411 | while (my $line = <$fh>) { |
| 1412 | if (length($line) > 998) { |
| 1413 | return "$.: patch contains a line longer than 998 characters"; |
| 1414 | } |
| 1415 | } |
| 1416 | return undef; |
| 1417 | } |
Jeff King | 0706bd1 | 2008-03-28 17:28:33 -0400 | [diff] [blame] | 1418 | |
| 1419 | sub file_has_nonascii { |
| 1420 | my $fn = shift; |
| 1421 | open(my $fh, '<', $fn) |
| 1422 | or die "unable to open $fn: $!\n"; |
| 1423 | while (my $line = <$fh>) { |
| 1424 | return 1 if $line =~ /[^[:ascii:]]/; |
| 1425 | } |
| 1426 | return 0; |
| 1427 | } |
Thomas Rast | 3cae7e5 | 2010-06-17 22:10:39 +0200 | [diff] [blame] | 1428 | |
| 1429 | sub body_or_subject_has_nonascii { |
| 1430 | my $fn = shift; |
| 1431 | open(my $fh, '<', $fn) |
| 1432 | or die "unable to open $fn: $!\n"; |
| 1433 | while (my $line = <$fh>) { |
| 1434 | last if $line =~ /^$/; |
| 1435 | return 1 if $line =~ /^Subject.*[^[:ascii:]]/; |
| 1436 | } |
| 1437 | while (my $line = <$fh>) { |
| 1438 | return 1 if $line =~ /[^[:ascii:]]/; |
| 1439 | } |
| 1440 | return 0; |
| 1441 | } |