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