git
/
git-send-email.perl
2261 строка · 64.1 Кб
1#!/usr/bin/perl
2#
3# Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
4# Copyright 2005 Ryan Anderson <ryan@michonline.com>
5#
6# GPL v2 (See COPYING)
7#
8# Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com>
9#
10# Sends a collection of emails to the given email addresses, disturbingly fast.
11#
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:
15# first line of the message is who to CC,
16# and second line is the subject of the message.
17#
18
19use 5.008001;
20use strict;
21use warnings $ENV{GIT_PERL_FATAL_WARNINGS} ? qw(FATAL all) : ();
22use Getopt::Long;
23use Git::LoadCPAN::Error qw(:try);
24use Git;
25use Git::I18N;
26
27Getopt::Long::Configure qw/ pass_through /;
28
29sub usage {
30print <<EOT;
31git send-email [<options>] <file|directory>
32git send-email [<options>] <format-patch options>
33git send-email --dump-aliases
34git send-email --translate-aliases
35
36Composing:
37--from <str> * Email From:
38--[no-]to <str> * Email To:
39--[no-]cc <str> * Email Cc:
40--[no-]bcc <str> * Email Bcc:
41--subject <str> * Email "Subject:"
42--reply-to <str> * Email "Reply-To:"
43--in-reply-to <str> * Email "In-Reply-To:"
44--[no-]xmailer * Add "X-Mailer:" header (default).
45--[no-]annotate * Review each patch that will be sent in an editor.
46--compose * Open an editor for introduction.
47--compose-encoding <str> * Encoding to assume for introduction.
48--8bit-encoding <str> * Encoding to assume 8bit mails if undeclared
49--transfer-encoding <str> * Transfer encoding to use (quoted-printable, 8bit, base64)
50
51Sending:
52--envelope-sender <str> * Email envelope sender.
53--sendmail-cmd <str> * Command to run to send email.
54--smtp-server <str:int> * Outgoing SMTP server to use. The port
55is optional. Default 'localhost'.
56--smtp-server-option <str> * Outgoing SMTP server option to use.
57--smtp-server-port <int> * Outgoing SMTP server port.
58--smtp-user <str> * Username for SMTP-AUTH.
59--smtp-pass <str> * Password for SMTP-AUTH; not necessary.
60--smtp-encryption <str> * tls or ssl; anything else disables.
61--smtp-ssl * Deprecated. Use '--smtp-encryption ssl'.
62--smtp-ssl-cert-path <str> * Path to ca-certificates (either directory or file).
63Pass an empty string to disable certificate
64verification.
65--smtp-domain <str> * The domain name sent to HELO/EHLO handshake
66--smtp-auth <str> * Space-separated list of allowed AUTH mechanisms, or
67"none" to disable authentication.
68This setting forces to use one of the listed mechanisms.
69--no-smtp-auth Disable SMTP authentication. Shorthand for
70`--smtp-auth=none`
71--smtp-debug <0|1> * Disable, enable Net::SMTP debug.
72
73--batch-size <int> * send max <int> message per connection.
74--relogin-delay <int> * delay <int> seconds between two successive login.
75This option can only be used with --batch-size
76
77Automating:
78--identity <str> * Use the sendemail.<id> options.
79--to-cmd <str> * Email To: via `<str> \$patch_path`.
80--cc-cmd <str> * Email Cc: via `<str> \$patch_path`.
81--header-cmd <str> * Add headers via `<str> \$patch_path`.
82--no-header-cmd * Disable any header command in use.
83--suppress-cc <str> * author, self, sob, cc, cccmd, body, bodycc, misc-by, all.
84--[no-]cc-cover * Email Cc: addresses in the cover letter.
85--[no-]to-cover * Email To: addresses in the cover letter.
86--[no-]signed-off-by-cc * Send to Signed-off-by: addresses. Default on.
87--[no-]suppress-from * Send to self. Default off.
88--[no-]chain-reply-to * Chain In-Reply-To: fields. Default off.
89--[no-]thread * Use In-Reply-To: field. Default on.
90
91Administering:
92--confirm <str> * Confirm recipients before sending;
93auto, cc, compose, always, or never.
94--quiet * Output one line of info per email.
95--dry-run * Don't actually send the emails.
96--[no-]validate * Perform patch sanity checks. Default on.
97--[no-]format-patch * understand any non optional arguments as
98`git format-patch` ones.
99--force * Send even if safety checks would prevent it.
100
101Information:
102--dump-aliases * Dump configured aliases and exit.
103--translate-aliases * Translate aliases read from standard
104input according to the configured email
105alias file(s), outputting the result to
106standard output.
107
108EOT
109exit(1);
110}
111
112sub uniq {
113my %seen;
114grep !$seen{$_}++, @_;
115}
116
117sub completion_helper {
118my ($original_opts) = @_;
119my %not_for_completion = (
120"git-completion-helper" => undef,
121"h" => undef,
122);
123my @send_email_opts = ();
124
125foreach my $key (keys %$original_opts) {
126unless (exists $not_for_completion{$key}) {
127my $negatable = ($key =~ s/!$//);
128
129if ($key =~ /[:=][si]$/) {
130$key =~ s/[:=][si]$//;
131push (@send_email_opts, "--$_=") foreach (split (/\|/, $key));
132} else {
133push (@send_email_opts, "--$_") foreach (split (/\|/, $key));
134if ($negatable) {
135push (@send_email_opts, "--no-$_") foreach (split (/\|/, $key));
136}
137}
138}
139}
140
141my @format_patch_opts = split(/ /, Git::command('format-patch', '--git-completion-helper'));
142my @opts = (@send_email_opts, @format_patch_opts);
143@opts = uniq (grep !/^$/, @opts);
144# There's an implicit '\n' here already, no need to add an explicit one.
145print "@opts";
146exit(0);
147}
148
149# most mail servers generate the Date: header, but not all...
150sub format_2822_time {
151my ($time) = @_;
152my @localtm = localtime($time);
153my @gmttm = gmtime($time);
154my $localmin = $localtm[1] + $localtm[2] * 60;
155my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
156if ($localtm[0] != $gmttm[0]) {
157die __("local zone differs from GMT by a non-minute interval\n");
158}
159if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
160$localmin += 1440;
161} elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
162$localmin -= 1440;
163} elsif ($gmttm[6] != $localtm[6]) {
164die __("local time offset greater than or equal to 24 hours\n");
165}
166my $offset = $localmin - $gmtmin;
167my $offhour = $offset / 60;
168my $offmin = abs($offset % 60);
169if (abs($offhour) >= 24) {
170die __("local time offset greater than or equal to 24 hours\n");
171}
172
173return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
174qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
175$localtm[3],
176qw(Jan Feb Mar Apr May Jun
177Jul Aug Sep Oct Nov Dec)[$localtm[4]],
178$localtm[5]+1900,
179$localtm[2],
180$localtm[1],
181$localtm[0],
182($offset >= 0) ? '+' : '-',
183abs($offhour),
184$offmin,
185);
186}
187
188my $smtp;
189my $auth;
190my $num_sent = 0;
191
192# Regexes for RFC 2047 productions.
193my $re_token = qr/[^][()<>@,;:\\"\/?.= \000-\037\177-\377]+/;
194my $re_encoded_text = qr/[^? \000-\037\177-\377]+/;
195my $re_encoded_word = qr/=\?($re_token)\?($re_token)\?($re_encoded_text)\?=/;
196
197# Variables we fill in automatically, or via prompting:
198my (@to,@cc,@xh,$envelope_sender,
199$initial_in_reply_to,$reply_to,$initial_subject,@files,
200$author,$sender,$smtp_authpass,$annotate,$compose,$time);
201# Things we either get from config, *or* are overridden on the
202# command-line.
203my ($no_cc, $no_to, $no_bcc, $no_identity, $no_header_cmd);
204my (@config_to, @getopt_to);
205my (@config_cc, @getopt_cc);
206my (@config_bcc, @getopt_bcc);
207
208# Example reply to:
209#$initial_in_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
210
211my $repo = eval { Git->repository() };
212my @repo = $repo ? ($repo) : ();
213
214# Behavior modification variables
215my ($quiet, $dry_run) = (0, 0);
216my $format_patch;
217my $compose_filename;
218my $force = 0;
219my $dump_aliases = 0;
220my $translate_aliases = 0;
221
222# Variables to prevent short format-patch options from being captured
223# as abbreviated send-email options
224my $reroll_count;
225
226# Handle interactive edition of files.
227my $multiedit;
228my $editor;
229
230sub system_or_msg {
231my ($args, $msg, $cmd_name) = @_;
232system(@$args);
233my $signalled = $? & 127;
234my $exit_code = $? >> 8;
235return unless $signalled or $exit_code;
236
237my @sprintf_args = ($cmd_name ? $cmd_name : $args->[0], $exit_code);
238if (defined $msg) {
239# Quiet the 'redundant' warning category, except we
240# need to support down to Perl 5.8.1, so we can't do a
241# "no warnings 'redundant'", since that category was
242# introduced in perl 5.22, and asking for it will die
243# on older perls.
244no warnings;
245return sprintf($msg, @sprintf_args);
246}
247return sprintf(__("fatal: command '%s' died with exit code %d"),
248@sprintf_args);
249}
250
251sub system_or_die {
252my $msg = system_or_msg(@_);
253die $msg if $msg;
254}
255
256sub do_edit {
257if (!defined($editor)) {
258$editor = Git::command_oneline('var', 'GIT_EDITOR');
259}
260my $die_msg = __("the editor exited uncleanly, aborting everything");
261if (defined($multiedit) && !$multiedit) {
262system_or_die(['sh', '-c', $editor.' "$@"', $editor, $_], $die_msg) for @_;
263} else {
264system_or_die(['sh', '-c', $editor.' "$@"', $editor, @_], $die_msg);
265}
266}
267
268# Variables with corresponding config settings
269my ($suppress_from, $signed_off_by_cc);
270my ($cover_cc, $cover_to);
271my ($to_cmd, $cc_cmd, $header_cmd);
272my ($smtp_server, $smtp_server_port, @smtp_server_options);
273my ($smtp_authuser, $smtp_encryption, $smtp_ssl_cert_path);
274my ($batch_size, $relogin_delay);
275my ($identity, $aliasfiletype, @alias_files, $smtp_domain, $smtp_auth);
276my ($confirm);
277my (@suppress_cc);
278my ($auto_8bit_encoding);
279my ($compose_encoding);
280my ($sendmail_cmd);
281# Variables with corresponding config settings & hardcoded defaults
282my ($debug_net_smtp) = 0; # Net::SMTP, see send_message()
283my $thread = 1;
284my $chain_reply_to = 0;
285my $use_xmailer = 1;
286my $validate = 1;
287my $target_xfer_encoding = 'auto';
288my $forbid_sendmail_variables = 1;
289
290my %config_bool_settings = (
291"thread" => \$thread,
292"chainreplyto" => \$chain_reply_to,
293"suppressfrom" => \$suppress_from,
294"signedoffbycc" => \$signed_off_by_cc,
295"cccover" => \$cover_cc,
296"tocover" => \$cover_to,
297"signedoffcc" => \$signed_off_by_cc,
298"validate" => \$validate,
299"multiedit" => \$multiedit,
300"annotate" => \$annotate,
301"xmailer" => \$use_xmailer,
302"forbidsendmailvariables" => \$forbid_sendmail_variables,
303);
304
305my %config_settings = (
306"smtpencryption" => \$smtp_encryption,
307"smtpserver" => \$smtp_server,
308"smtpserverport" => \$smtp_server_port,
309"smtpserveroption" => \@smtp_server_options,
310"smtpuser" => \$smtp_authuser,
311"smtppass" => \$smtp_authpass,
312"smtpdomain" => \$smtp_domain,
313"smtpauth" => \$smtp_auth,
314"smtpbatchsize" => \$batch_size,
315"smtprelogindelay" => \$relogin_delay,
316"to" => \@config_to,
317"tocmd" => \$to_cmd,
318"cc" => \@config_cc,
319"cccmd" => \$cc_cmd,
320"headercmd" => \$header_cmd,
321"aliasfiletype" => \$aliasfiletype,
322"bcc" => \@config_bcc,
323"suppresscc" => \@suppress_cc,
324"envelopesender" => \$envelope_sender,
325"confirm" => \$confirm,
326"from" => \$sender,
327"assume8bitencoding" => \$auto_8bit_encoding,
328"composeencoding" => \$compose_encoding,
329"transferencoding" => \$target_xfer_encoding,
330"sendmailcmd" => \$sendmail_cmd,
331);
332
333my %config_path_settings = (
334"aliasesfile" => \@alias_files,
335"smtpsslcertpath" => \$smtp_ssl_cert_path,
336);
337
338# Handle Uncouth Termination
339sub signal_handler {
340# Make text normal
341require Term::ANSIColor;
342print Term::ANSIColor::color("reset"), "\n";
343
344# SMTP password masked
345system "stty echo";
346
347# tmp files from --compose
348if (defined $compose_filename) {
349if (-e $compose_filename) {
350printf __("'%s' contains an intermediate version ".
351"of the email you were composing.\n"),
352$compose_filename;
353}
354if (-e ($compose_filename . ".final")) {
355printf __("'%s.final' contains the composed email.\n"),
356$compose_filename;
357}
358}
359
360exit;
361};
362
363$SIG{TERM} = \&signal_handler;
364$SIG{INT} = \&signal_handler;
365
366# Read our sendemail.* config
367sub read_config {
368my ($known_keys, $configured, $prefix) = @_;
369
370foreach my $setting (keys %config_bool_settings) {
371my $target = $config_bool_settings{$setting};
372my $key = "$prefix.$setting";
373next unless exists $known_keys->{$key};
374my $v = (@{$known_keys->{$key}} == 1 &&
375(defined $known_keys->{$key}->[0] &&
376$known_keys->{$key}->[0] =~ /^(?:true|false)$/s))
377? $known_keys->{$key}->[0] eq 'true'
378: Git::config_bool(@repo, $key);
379next unless defined $v;
380next if $configured->{$setting}++;
381$$target = $v;
382}
383
384foreach my $setting (keys %config_path_settings) {
385my $target = $config_path_settings{$setting};
386my $key = "$prefix.$setting";
387next unless exists $known_keys->{$key};
388if (ref($target) eq "ARRAY") {
389my @values = Git::config_path(@repo, $key);
390next unless @values;
391next if $configured->{$setting}++;
392@$target = @values;
393}
394else {
395my $v = Git::config_path(@repo, "$prefix.$setting");
396next unless defined $v;
397next if $configured->{$setting}++;
398$$target = $v;
399}
400}
401
402foreach my $setting (keys %config_settings) {
403my $target = $config_settings{$setting};
404my $key = "$prefix.$setting";
405next unless exists $known_keys->{$key};
406if (ref($target) eq "ARRAY") {
407my @values = @{$known_keys->{$key}};
408@values = grep { defined } @values;
409next if $configured->{$setting}++;
410@$target = @values;
411}
412else {
413my $v = $known_keys->{$key}->[-1];
414next unless defined $v;
415next if $configured->{$setting}++;
416$$target = $v;
417}
418}
419}
420
421sub config_regexp {
422my ($regex) = @_;
423my @ret;
424eval {
425my $ret = Git::command(
426'config',
427'--null',
428'--get-regexp',
429$regex,
430);
431@ret = map {
432# We must always return ($k, $v) here, since
433# empty config values will be just "key\0",
434# not "key\nvalue\0".
435my ($k, $v) = split /\n/, $_, 2;
436($k, $v);
437} split /\0/, $ret;
4381;
439} or do {
440# If we have no keys we're OK, otherwise re-throw
441die $@ if $@->value != 1;
442};
443return @ret;
444}
445
446# Save ourselves a lot of work of shelling out to 'git config' (it
447# parses 'bool' etc.) by only doing so for config keys that exist.
448my %known_config_keys;
449{
450my @kv = config_regexp("^sende?mail[.]");
451while (my ($k, $v) = splice @kv, 0, 2) {
452push @{$known_config_keys{$k}} => $v;
453}
454}
455
456# sendemail.identity yields to --identity. We must parse this
457# special-case first before the rest of the config is read.
458{
459my $key = "sendemail.identity";
460$identity = Git::config(@repo, $key) if exists $known_config_keys{$key};
461}
462my %identity_options = (
463"identity=s" => \$identity,
464"no-identity" => \$no_identity,
465);
466my $rc = GetOptions(%identity_options);
467usage() unless $rc;
468undef $identity if $no_identity;
469
470# Now we know enough to read the config
471{
472my %configured;
473read_config(\%known_config_keys, \%configured, "sendemail.$identity") if defined $identity;
474read_config(\%known_config_keys, \%configured, "sendemail");
475}
476
477# Begin by accumulating all the variables (defined above), that we will end up
478# needing, first, from the command line:
479
480my $help;
481my $git_completion_helper;
482my %dump_aliases_options = (
483"h" => \$help,
484"dump-aliases" => \$dump_aliases,
485"translate-aliases" => \$translate_aliases,
486);
487$rc = GetOptions(%dump_aliases_options);
488usage() unless $rc;
489die __("--dump-aliases incompatible with other options\n")
490if !$help and ($dump_aliases or $translate_aliases) and @ARGV;
491die __("--dump-aliases and --translate-aliases are mutually exclusive\n")
492if !$help and $dump_aliases and $translate_aliases;
493my %options = (
494"sender|from=s" => \$sender,
495"in-reply-to=s" => \$initial_in_reply_to,
496"reply-to=s" => \$reply_to,
497"subject=s" => \$initial_subject,
498"to=s" => \@getopt_to,
499"to-cmd=s" => \$to_cmd,
500"no-to" => \$no_to,
501"cc=s" => \@getopt_cc,
502"no-cc" => \$no_cc,
503"bcc=s" => \@getopt_bcc,
504"no-bcc" => \$no_bcc,
505"chain-reply-to!" => \$chain_reply_to,
506"sendmail-cmd=s" => \$sendmail_cmd,
507"smtp-server=s" => \$smtp_server,
508"smtp-server-option=s" => \@smtp_server_options,
509"smtp-server-port=s" => \$smtp_server_port,
510"smtp-user=s" => \$smtp_authuser,
511"smtp-pass:s" => \$smtp_authpass,
512"smtp-ssl" => sub { $smtp_encryption = 'ssl' },
513"smtp-encryption=s" => \$smtp_encryption,
514"smtp-ssl-cert-path=s" => \$smtp_ssl_cert_path,
515"smtp-debug:i" => \$debug_net_smtp,
516"smtp-domain:s" => \$smtp_domain,
517"smtp-auth=s" => \$smtp_auth,
518"no-smtp-auth" => sub {$smtp_auth = 'none'},
519"annotate!" => \$annotate,
520"compose" => \$compose,
521"quiet" => \$quiet,
522"cc-cmd=s" => \$cc_cmd,
523"header-cmd=s" => \$header_cmd,
524"no-header-cmd" => \$no_header_cmd,
525"suppress-from!" => \$suppress_from,
526"suppress-cc=s" => \@suppress_cc,
527"signed-off-cc|signed-off-by-cc!" => \$signed_off_by_cc,
528"cc-cover!" => \$cover_cc,
529"to-cover!" => \$cover_to,
530"confirm=s" => \$confirm,
531"dry-run" => \$dry_run,
532"envelope-sender=s" => \$envelope_sender,
533"thread!" => \$thread,
534"validate!" => \$validate,
535"transfer-encoding=s" => \$target_xfer_encoding,
536"format-patch!" => \$format_patch,
537"8bit-encoding=s" => \$auto_8bit_encoding,
538"compose-encoding=s" => \$compose_encoding,
539"force" => \$force,
540"xmailer!" => \$use_xmailer,
541"batch-size=i" => \$batch_size,
542"relogin-delay=i" => \$relogin_delay,
543"git-completion-helper" => \$git_completion_helper,
544"v=s" => \$reroll_count,
545);
546$rc = GetOptions(%options);
547
548# Munge any "either config or getopt, not both" variables
549my @initial_to = @getopt_to ? @getopt_to : ($no_to ? () : @config_to);
550my @initial_cc = @getopt_cc ? @getopt_cc : ($no_cc ? () : @config_cc);
551my @initial_bcc = @getopt_bcc ? @getopt_bcc : ($no_bcc ? () : @config_bcc);
552
553usage() if $help;
554my %all_options = (%options, %dump_aliases_options, %identity_options);
555completion_helper(\%all_options) if $git_completion_helper;
556unless ($rc) {
557usage();
558}
559
560if ($forbid_sendmail_variables && grep { /^sendmail/s } keys %known_config_keys) {
561die __("fatal: found configuration options for 'sendmail'\n" .
562"git-send-email is configured with the sendemail.* options - note the 'e'.\n" .
563"Set sendemail.forbidSendmailVariables to false to disable this check.\n");
564}
565
566die __("Cannot run git format-patch from outside a repository\n")
567if $format_patch and not $repo;
568
569die __("`batch-size` and `relogin` must be specified together " .
570"(via command-line or configuration option)\n")
571if defined $relogin_delay and not defined $batch_size;
572
573# 'default' encryption is none -- this only prevents a warning
574$smtp_encryption = '' unless (defined $smtp_encryption);
575
576# Set CC suppressions
577my(%suppress_cc);
578if (@suppress_cc) {
579foreach my $entry (@suppress_cc) {
580# Please update $__git_send_email_suppresscc_options
581# in git-completion.bash when you add new options.
582die sprintf(__("Unknown --suppress-cc field: '%s'\n"), $entry)
583unless $entry =~ /^(?:all|cccmd|cc|author|self|sob|body|bodycc|misc-by)$/;
584$suppress_cc{$entry} = 1;
585}
586}
587
588if ($suppress_cc{'all'}) {
589foreach my $entry (qw (cccmd cc author self sob body bodycc misc-by)) {
590$suppress_cc{$entry} = 1;
591}
592delete $suppress_cc{'all'};
593}
594
595# If explicit old-style ones are specified, they trump --suppress-cc.
596$suppress_cc{'self'} = $suppress_from if defined $suppress_from;
597$suppress_cc{'sob'} = !$signed_off_by_cc if defined $signed_off_by_cc;
598
599if ($suppress_cc{'body'}) {
600foreach my $entry (qw (sob bodycc misc-by)) {
601$suppress_cc{$entry} = 1;
602}
603delete $suppress_cc{'body'};
604}
605
606# Set confirm's default value
607my $confirm_unconfigured = !defined $confirm;
608if ($confirm_unconfigured) {
609$confirm = scalar %suppress_cc ? 'compose' : 'auto';
610};
611# Please update $__git_send_email_confirm_options in
612# git-completion.bash when you add new options.
613die sprintf(__("Unknown --confirm setting: '%s'\n"), $confirm)
614unless $confirm =~ /^(?:auto|cc|compose|always|never)/;
615
616# Debugging, print out the suppressions.
617if (0) {
618print "suppressions:\n";
619foreach my $entry (keys %suppress_cc) {
620printf " %-5s -> $suppress_cc{$entry}\n", $entry;
621}
622}
623
624my ($repoauthor, $repocommitter);
625{
626my %cache;
627my ($author, $committer);
628my $common = sub {
629my ($what) = @_;
630return $cache{$what} if exists $cache{$what};
631($cache{$what}) = Git::ident_person(@repo, $what);
632return $cache{$what};
633};
634$repoauthor = sub { $common->('author') };
635$repocommitter = sub { $common->('committer') };
636}
637
638sub parse_address_line {
639require Git::LoadCPAN::Mail::Address;
640return map { $_->format } Mail::Address->parse($_[0]);
641}
642
643sub split_addrs {
644require Text::ParseWords;
645return Text::ParseWords::quotewords('\s*,\s*', 1, @_);
646}
647
648my %aliases;
649
650sub parse_sendmail_alias {
651local $_ = shift;
652if (/"/) {
653printf STDERR __("warning: sendmail alias with quotes is not supported: %s\n"), $_;
654} elsif (/:include:/) {
655printf STDERR __("warning: `:include:` not supported: %s\n"), $_;
656} elsif (/[\/|]/) {
657printf STDERR __("warning: `/file` or `|pipe` redirection not supported: %s\n"), $_;
658} elsif (/^(\S+?)\s*:\s*(.+)$/) {
659my ($alias, $addr) = ($1, $2);
660$aliases{$alias} = [ split_addrs($addr) ];
661} else {
662printf STDERR __("warning: sendmail line is not recognized: %s\n"), $_;
663}
664}
665
666sub parse_sendmail_aliases {
667my $fh = shift;
668my $s = '';
669while (<$fh>) {
670chomp;
671next if /^\s*$/ || /^\s*#/;
672$s .= $_, next if $s =~ s/\\$// || s/^\s+//;
673parse_sendmail_alias($s) if $s;
674$s = $_;
675}
676$s =~ s/\\$//; # silently tolerate stray '\' on last line
677parse_sendmail_alias($s) if $s;
678}
679
680my %parse_alias = (
681# multiline formats can be supported in the future
682mutt => sub { my $fh = shift; while (<$fh>) {
683if (/^\s*alias\s+(?:-group\s+\S+\s+)*(\S+)\s+(.*)$/) {
684my ($alias, $addr) = ($1, $2);
685$addr =~ s/#.*$//; # mutt allows # comments
686# commas delimit multiple addresses
687my @addr = split_addrs($addr);
688
689# quotes may be escaped in the file,
690# unescape them so we do not double-escape them later.
691s/\\"/"/g foreach @addr;
692$aliases{$alias} = \@addr
693}}},
694mailrc => sub { my $fh = shift; while (<$fh>) {
695if (/^alias\s+(\S+)\s+(.*?)\s*$/) {
696require Text::ParseWords;
697# spaces delimit multiple addresses
698$aliases{$1} = [ Text::ParseWords::quotewords('\s+', 0, $2) ];
699}}},
700pine => sub { my $fh = shift; my $f='\t[^\t]*';
701for (my $x = ''; defined($x); $x = $_) {
702chomp $x;
703$x .= $1 while(defined($_ = <$fh>) && /^ +(.*)$/);
704$x =~ /^(\S+)$f\t\(?([^\t]+?)\)?(:?$f){0,2}$/ or next;
705$aliases{$1} = [ split_addrs($2) ];
706}},
707elm => sub { my $fh = shift;
708while (<$fh>) {
709if (/^(\S+)\s+=\s+[^=]+=\s(\S+)/) {
710my ($alias, $addr) = ($1, $2);
711$aliases{$alias} = [ split_addrs($addr) ];
712}
713} },
714sendmail => \&parse_sendmail_aliases,
715gnus => sub { my $fh = shift; while (<$fh>) {
716if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
717$aliases{$1} = [ $2 ];
718}}}
719# Please update _git_config() in git-completion.bash when you
720# add new MUAs.
721);
722
723if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
724foreach my $file (@alias_files) {
725open my $fh, '<', $file or die "opening $file: $!\n";
726$parse_alias{$aliasfiletype}->($fh);
727close $fh;
728}
729}
730
731if ($dump_aliases) {
732print "$_\n" for (sort keys %aliases);
733exit(0);
734}
735
736if ($translate_aliases) {
737while (<STDIN>) {
738my @addr_list = parse_address_line($_);
739@addr_list = expand_aliases(@addr_list);
740@addr_list = sanitize_address_list(@addr_list);
741print "$_\n" for @addr_list;
742}
743exit(0);
744}
745
746# is_format_patch_arg($f) returns 0 if $f names a patch, or 1 if
747# $f is a revision list specification to be passed to format-patch.
748sub is_format_patch_arg {
749return unless $repo;
750my $f = shift;
751try {
752$repo->command('rev-parse', '--verify', '--quiet', $f);
753if (defined($format_patch)) {
754return $format_patch;
755}
756die sprintf(__(<<EOF), $f, $f);
757File '%s' exists but it could also be the range of commits
758to produce patches for. Please disambiguate by...
759
760* Saying "./%s" if you mean a file; or
761* Giving --format-patch option if you mean a range.
762EOF
763} catch Git::Error::Command with {
764# Not a valid revision. Treat it as a filename.
765return 0;
766}
767}
768
769# Now that all the defaults are set, process the rest of the command line
770# arguments and collect up the files that need to be processed.
771my @rev_list_opts;
772while (defined(my $f = shift @ARGV)) {
773if ($f eq "--") {
774push @rev_list_opts, "--", @ARGV;
775@ARGV = ();
776} elsif (-d $f and !is_format_patch_arg($f)) {
777opendir my $dh, $f
778or die sprintf(__("Failed to opendir %s: %s"), $f, $!);
779
780require File::Spec;
781push @files, grep { -f $_ } map { File::Spec->catfile($f, $_) }
782sort readdir $dh;
783closedir $dh;
784} elsif ((-f $f or -p $f) and !is_format_patch_arg($f)) {
785push @files, $f;
786} else {
787push @rev_list_opts, $f;
788}
789}
790
791if (@rev_list_opts) {
792die __("Cannot run git format-patch from outside a repository\n")
793unless $repo;
794require File::Temp;
795push @files, $repo->command('format-patch', '-o', File::Temp::tempdir(CLEANUP => 1),
796defined $reroll_count ? ('-v', $reroll_count) : (),
797@rev_list_opts);
798}
799
800if (defined $sender) {
801$sender =~ s/^\s+|\s+$//g;
802($sender) = expand_aliases($sender);
803} else {
804$sender = $repoauthor->() || $repocommitter->() || '';
805}
806
807# $sender could be an already sanitized address
808# (e.g. sendemail.from could be manually sanitized by user).
809# But it's a no-op to run sanitize_address on an already sanitized address.
810$sender = sanitize_address($sender);
811
812$time = time - scalar $#files;
813
814@files = handle_backup_files(@files);
815
816if (@files) {
817unless ($quiet) {
818print $_,"\n" for (@files);
819}
820} else {
821print STDERR __("\nNo patch files specified!\n\n");
822usage();
823}
824
825sub get_patch_subject {
826my $fn = shift;
827open (my $fh, '<', $fn);
828while (my $line = <$fh>) {
829next unless ($line =~ /^Subject: (.*)$/);
830close $fh;
831return "GIT: $1\n";
832}
833close $fh;
834die sprintf(__("No subject line in %s?"), $fn);
835}
836
837if ($compose) {
838# Note that this does not need to be secure, but we will make a small
839# effort to have it be unique
840require File::Temp;
841$compose_filename = ($repo ?
842File::Temp::tempfile(".gitsendemail.msg.XXXXXX", DIR => $repo->repo_path()) :
843File::Temp::tempfile(".gitsendemail.msg.XXXXXX", DIR => "."))[1];
844open my $c, ">", $compose_filename
845or die sprintf(__("Failed to open for writing %s: %s"), $compose_filename, $!);
846
847
848my $tpl_sender = $sender || $repoauthor->() || $repocommitter->() || '';
849my $tpl_subject = $initial_subject || '';
850my $tpl_in_reply_to = $initial_in_reply_to || '';
851my $tpl_reply_to = $reply_to || '';
852my $tpl_to = join(',', @initial_to);
853my $tpl_cc = join(',', @initial_cc);
854my $tpl_bcc = join(', ', @initial_bcc);
855
856print $c <<EOT1, Git::prefix_lines("GIT: ", __(<<EOT2)), <<EOT3;
857From $tpl_sender # This line is ignored.
858EOT1
859Lines beginning in "GIT:" will be removed.
860Consider including an overall diffstat or table of contents
861for the patch you are writing.
862
863Clear the body content if you don't wish to send a summary.
864EOT2
865From: $tpl_sender
866To: $tpl_to
867Cc: $tpl_cc
868Bcc: $tpl_bcc
869Reply-To: $tpl_reply_to
870Subject: $tpl_subject
871In-Reply-To: $tpl_in_reply_to
872
873EOT3
874for my $f (@files) {
875print $c get_patch_subject($f);
876}
877close $c;
878
879if ($annotate) {
880do_edit($compose_filename, @files);
881} else {
882do_edit($compose_filename);
883}
884
885open my $c2, ">", $compose_filename . ".final"
886or die sprintf(__("Failed to open %s.final: %s"), $compose_filename, $!);
887
888open $c, "<", $compose_filename
889or die sprintf(__("Failed to open %s: %s"), $compose_filename, $!);
890
891my $need_8bit_cte = file_has_nonascii($compose_filename);
892my $in_body = 0;
893my $summary_empty = 1;
894if (!defined $compose_encoding) {
895$compose_encoding = "UTF-8";
896}
897while(<$c>) {
898next if m/^GIT:/;
899if ($in_body) {
900$summary_empty = 0 unless (/^\n$/);
901} elsif (/^\n$/) {
902$in_body = 1;
903if ($need_8bit_cte) {
904print $c2 "MIME-Version: 1.0\n",
905"Content-Type: text/plain; ",
906"charset=$compose_encoding\n",
907"Content-Transfer-Encoding: 8bit\n";
908}
909} elsif (/^MIME-Version:/i) {
910$need_8bit_cte = 0;
911} elsif (/^Subject:\s*(.+)\s*$/i) {
912$initial_subject = $1;
913my $subject = $initial_subject;
914$_ = "Subject: " .
915quote_subject($subject, $compose_encoding) .
916"\n";
917} elsif (/^In-Reply-To:\s*(.+)\s*$/i) {
918$initial_in_reply_to = $1;
919next;
920} elsif (/^Reply-To:\s*(.+)\s*$/i) {
921$reply_to = $1;
922} elsif (/^From:\s*(.+)\s*$/i) {
923$sender = $1;
924next;
925} elsif (/^To:\s*(.+)\s*$/i) {
926@initial_to = parse_address_line($1);
927next;
928} elsif (/^Cc:\s*(.+)\s*$/i) {
929@initial_cc = parse_address_line($1);
930next;
931} elsif (/^Bcc:/i) {
932@initial_bcc = parse_address_line($1);
933next;
934}
935print $c2 $_;
936}
937close $c;
938close $c2;
939
940if ($summary_empty) {
941print __("Summary email is empty, skipping it\n");
942$compose = -1;
943}
944} elsif ($annotate) {
945do_edit(@files);
946}
947
948{
949# Only instantiate one $term per program run, since some
950# Term::ReadLine providers refuse to create a second instance.
951my $term;
952sub term {
953require Term::ReadLine;
954if (!defined $term) {
955$term = $ENV{"GIT_SEND_EMAIL_NOTTY"}
956? Term::ReadLine->new('git-send-email', \*STDIN, \*STDOUT)
957: Term::ReadLine->new('git-send-email');
958}
959return $term;
960}
961}
962
963sub ask {
964my ($prompt, %arg) = @_;
965my $valid_re = $arg{valid_re};
966my $default = $arg{default};
967my $confirm_only = $arg{confirm_only};
968my $resp;
969my $i = 0;
970my $term = term();
971return defined $default ? $default : undef
972unless defined $term->IN and defined fileno($term->IN) and
973defined $term->OUT and defined fileno($term->OUT);
974while ($i++ < 10) {
975$resp = $term->readline($prompt);
976if (!defined $resp) { # EOF
977print "\n";
978return defined $default ? $default : undef;
979}
980if ($resp eq '' and defined $default) {
981return $default;
982}
983if (!defined $valid_re or $resp =~ /$valid_re/) {
984return $resp;
985}
986if ($confirm_only) {
987my $yesno = $term->readline(
988# TRANSLATORS: please keep [y/N] as is.
989sprintf(__("Are you sure you want to use <%s> [y/N]? "), $resp));
990if (defined $yesno && $yesno =~ /y/i) {
991return $resp;
992}
993}
994}
995return;
996}
997
998my %broken_encoding;
999
1000sub file_declares_8bit_cte {
1001my $fn = shift;
1002open (my $fh, '<', $fn);
1003while (my $line = <$fh>) {
1004last if ($line =~ /^$/);
1005return 1 if ($line =~ /^Content-Transfer-Encoding: .*8bit.*$/);
1006}
1007close $fh;
1008return 0;
1009}
1010
1011foreach my $f (@files) {
1012next unless (body_or_subject_has_nonascii($f)
1013&& !file_declares_8bit_cte($f));
1014$broken_encoding{$f} = 1;
1015}
1016
1017if (!defined $auto_8bit_encoding && scalar %broken_encoding) {
1018print __("The following files are 8bit, but do not declare " .
1019"a Content-Transfer-Encoding.\n");
1020foreach my $f (sort keys %broken_encoding) {
1021print " $f\n";
1022}
1023$auto_8bit_encoding = ask(__("Which 8bit encoding should I declare [UTF-8]? "),
1024valid_re => qr/.{4}/, confirm_only => 1,
1025default => "UTF-8");
1026}
1027
1028if (!$force) {
1029for my $f (@files) {
1030if (get_patch_subject($f) =~ /\Q*** SUBJECT HERE ***\E/) {
1031die sprintf(__("Refusing to send because the patch\n\t%s\n"
1032. "has the template subject '*** SUBJECT HERE ***'. "
1033. "Pass --force if you really want to send.\n"), $f);
1034}
1035}
1036}
1037
1038my $to_whom = __("To whom should the emails be sent (if anyone)?");
1039my $prompting = 0;
1040if (!@initial_to && !defined $to_cmd) {
1041my $to = ask("$to_whom ",
1042default => "",
1043valid_re => qr/\@.*\./, confirm_only => 1);
1044push @initial_to, parse_address_line($to) if defined $to; # sanitized/validated later
1045$prompting++;
1046}
1047
1048sub expand_aliases {
1049return map { expand_one_alias($_) } @_;
1050}
1051
1052my %EXPANDED_ALIASES;
1053sub expand_one_alias {
1054my $alias = shift;
1055if ($EXPANDED_ALIASES{$alias}) {
1056die sprintf(__("fatal: alias '%s' expands to itself\n"), $alias);
1057}
1058local $EXPANDED_ALIASES{$alias} = 1;
1059return $aliases{$alias} ? expand_aliases(@{$aliases{$alias}}) : $alias;
1060}
1061
1062@initial_to = process_address_list(@initial_to);
1063@initial_cc = process_address_list(@initial_cc);
1064@initial_bcc = process_address_list(@initial_bcc);
1065
1066if ($thread && !defined $initial_in_reply_to && $prompting) {
1067$initial_in_reply_to = ask(
1068__("Message-ID to be used as In-Reply-To for the first email (if any)? "),
1069default => "",
1070valid_re => qr/\@.*\./, confirm_only => 1);
1071}
1072if (defined $initial_in_reply_to) {
1073$initial_in_reply_to =~ s/^\s*<?//;
1074$initial_in_reply_to =~ s/>?\s*$//;
1075$initial_in_reply_to = "<$initial_in_reply_to>" if $initial_in_reply_to ne '';
1076}
1077
1078if (defined $reply_to) {
1079$reply_to =~ s/^\s+|\s+$//g;
1080($reply_to) = expand_aliases($reply_to);
1081$reply_to = sanitize_address($reply_to);
1082}
1083
1084if (!defined $sendmail_cmd && !defined $smtp_server) {
1085my @sendmail_paths = qw( /usr/sbin/sendmail /usr/lib/sendmail );
1086push @sendmail_paths, map {"$_/sendmail"} split /:/, $ENV{PATH};
1087foreach (@sendmail_paths) {
1088if (-x $_) {
1089$sendmail_cmd = $_;
1090last;
1091}
1092}
1093
1094if (!defined $sendmail_cmd) {
1095$smtp_server = 'localhost'; # could be 127.0.0.1, too... *shrug*
1096}
1097}
1098
1099if ($compose && $compose > 0) {
1100@files = ($compose_filename . ".final", @files);
1101}
1102
1103# Variables we set as part of the loop over files
1104our ($message_id, %mail, $subject, $in_reply_to, $references, $message,
1105$needs_confirm, $message_num, $ask_default);
1106
1107sub extract_valid_address {
1108my $address = shift;
1109my $local_part_regexp = qr/[^<>"\s@]+/;
1110my $domain_regexp = qr/[^.<>"\s@]+(?:\.[^.<>"\s@]+)+/;
1111
1112# check for a local address:
1113return $address if ($address =~ /^($local_part_regexp)$/);
1114
1115$address =~ s/^\s*<(.*)>\s*$/$1/;
1116my $have_email_valid = eval { require Email::Valid; 1 };
1117if ($have_email_valid) {
1118return scalar Email::Valid->address($address);
1119}
1120
1121# less robust/correct than the monster regexp in Email::Valid,
1122# but still does a 99% job, and one less dependency
1123return $1 if $address =~ /($local_part_regexp\@$domain_regexp)/;
1124return;
1125}
1126
1127sub extract_valid_address_or_die {
1128my $address = shift;
1129my $valid_address = extract_valid_address($address);
1130die sprintf(__("error: unable to extract a valid address from: %s\n"), $address)
1131if !$valid_address;
1132return $valid_address;
1133}
1134
1135sub validate_address {
1136my $address = shift;
1137while (!extract_valid_address($address)) {
1138printf STDERR __("error: unable to extract a valid address from: %s\n"), $address;
1139# TRANSLATORS: Make sure to include [q] [d] [e] in your
1140# translation. The program will only accept English input
1141# at this point.
1142$_ = ask(__("What to do with this address? ([q]uit|[d]rop|[e]dit): "),
1143valid_re => qr/^(?:quit|q|drop|d|edit|e)/i,
1144default => 'q');
1145if (/^d/i) {
1146return undef;
1147} elsif (/^q/i) {
1148cleanup_compose_files();
1149exit(0);
1150}
1151$address = ask("$to_whom ",
1152default => "",
1153valid_re => qr/\@.*\./, confirm_only => 1);
1154}
1155return $address;
1156}
1157
1158sub validate_address_list {
1159return (grep { defined $_ }
1160map { validate_address($_) } @_);
1161}
1162
1163# Usually don't need to change anything below here.
1164
1165# we make a "fake" message id by taking the current number
1166# of seconds since the beginning of Unix time and tacking on
1167# a random number to the end, in case we are called quicker than
1168# 1 second since the last time we were called.
1169
1170# We'll setup a template for the message id, using the "from" address:
1171
1172my ($message_id_stamp, $message_id_serial);
1173sub make_message_id {
1174my $uniq;
1175if (!defined $message_id_stamp) {
1176require POSIX;
1177$message_id_stamp = POSIX::strftime("%Y%m%d%H%M%S.$$", gmtime(time));
1178$message_id_serial = 0;
1179}
1180$message_id_serial++;
1181$uniq = "$message_id_stamp-$message_id_serial";
1182
1183my $du_part;
1184for ($sender, $repocommitter->(), $repoauthor->()) {
1185$du_part = extract_valid_address(sanitize_address($_));
1186last if (defined $du_part and $du_part ne '');
1187}
1188if (not defined $du_part or $du_part eq '') {
1189require Sys::Hostname;
1190$du_part = 'user@' . Sys::Hostname::hostname();
1191}
1192my $message_id_template = "<%s-%s>";
1193$message_id = sprintf($message_id_template, $uniq, $du_part);
1194#print "new message id = $message_id\n"; # Was useful for debugging
1195}
1196
1197sub unquote_rfc2047 {
1198local ($_) = @_;
1199my $charset;
1200my $sep = qr/[ \t]+/;
1201s{$re_encoded_word(?:$sep$re_encoded_word)*}{
1202my @words = split $sep, $&;
1203foreach (@words) {
1204m/$re_encoded_word/;
1205$charset = $1;
1206my $encoding = $2;
1207my $text = $3;
1208if ($encoding eq 'q' || $encoding eq 'Q') {
1209$_ = $text;
1210s/_/ /g;
1211s/=([0-9A-F]{2})/chr(hex($1))/egi;
1212} else {
1213# other encodings not supported yet
1214}
1215}
1216join '', @words;
1217}eg;
1218return wantarray ? ($_, $charset) : $_;
1219}
1220
1221sub quote_rfc2047 {
1222local $_ = shift;
1223my $encoding = shift || 'UTF-8';
1224s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
1225s/(.*)/=\?$encoding\?q\?$1\?=/;
1226return $_;
1227}
1228
1229sub is_rfc2047_quoted {
1230my $s = shift;
1231length($s) <= 75 &&
1232$s =~ m/^(?:"[[:ascii:]]*"|$re_encoded_word)$/o;
1233}
1234
1235sub subject_needs_rfc2047_quoting {
1236my $s = shift;
1237
1238return ($s =~ /[^[:ascii:]]/) || ($s =~ /=\?/);
1239}
1240
1241sub quote_subject {
1242local $subject = shift;
1243my $encoding = shift || 'UTF-8';
1244
1245if (subject_needs_rfc2047_quoting($subject)) {
1246return quote_rfc2047($subject, $encoding);
1247}
1248return $subject;
1249}
1250
1251# use the simplest quoting being able to handle the recipient
1252sub sanitize_address {
1253my ($recipient) = @_;
1254
1255# remove garbage after email address
1256$recipient =~ s/(.*>).*$/$1/;
1257
1258my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
1259
1260if (not $recipient_name) {
1261return $recipient;
1262}
1263
1264# if recipient_name is already quoted, do nothing
1265if (is_rfc2047_quoted($recipient_name)) {
1266return $recipient;
1267}
1268
1269# remove non-escaped quotes
1270$recipient_name =~ s/(^|[^\\])"/$1/g;
1271
1272# rfc2047 is needed if a non-ascii char is included
1273if ($recipient_name =~ /[^[:ascii:]]/) {
1274$recipient_name = quote_rfc2047($recipient_name);
1275}
1276
1277# double quotes are needed if specials or CTLs are included
1278elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
1279$recipient_name =~ s/([\\\r])/\\$1/g;
1280$recipient_name = qq["$recipient_name"];
1281}
1282
1283return "$recipient_name $recipient_addr";
1284
1285}
1286
1287sub strip_garbage_one_address {
1288my ($addr) = @_;
1289chomp $addr;
1290if ($addr =~ /^(("[^"]*"|[^"<]*)? *<[^>]*>).*/) {
1291# "Foo Bar" <foobar@example.com> [possibly garbage here]
1292# Foo Bar <foobar@example.com> [possibly garbage here]
1293return $1;
1294}
1295if ($addr =~ /^(<[^>]*>).*/) {
1296# <foo@example.com> [possibly garbage here]
1297# if garbage contains other addresses, they are ignored.
1298return $1;
1299}
1300if ($addr =~ /^([^"#,\s]*)/) {
1301# address without quoting: remove anything after the address
1302return $1;
1303}
1304return $addr;
1305}
1306
1307sub sanitize_address_list {
1308return (map { sanitize_address($_) } @_);
1309}
1310
1311sub process_address_list {
1312my @addr_list = map { parse_address_line($_) } @_;
1313@addr_list = expand_aliases(@addr_list);
1314@addr_list = sanitize_address_list(@addr_list);
1315@addr_list = validate_address_list(@addr_list);
1316return @addr_list;
1317}
1318
1319# Returns the local Fully Qualified Domain Name (FQDN) if available.
1320#
1321# Tightly configured MTAa require that a caller sends a real DNS
1322# domain name that corresponds the IP address in the HELO/EHLO
1323# handshake. This is used to verify the connection and prevent
1324# spammers from trying to hide their identity. If the DNS and IP don't
1325# match, the receiving MTA may deny the connection.
1326#
1327# Here is a deny example of Net::SMTP with the default "localhost.localdomain"
1328#
1329# Net::SMTP=GLOB(0x267ec28)>>> EHLO localhost.localdomain
1330# Net::SMTP=GLOB(0x267ec28)<<< 550 EHLO argument does not match calling host
1331#
1332# This maildomain*() code is based on ideas in Perl library Test::Reporter
1333# /usr/share/perl5/Test/Reporter/Mail/Util.pm ==> sub _maildomain ()
1334
1335sub valid_fqdn {
1336my $domain = shift;
1337return defined $domain && !($^O eq 'darwin' && $domain =~ /\.local$/) && $domain =~ /\./;
1338}
1339
1340sub maildomain_net {
1341my $maildomain;
1342
1343require Net::Domain;
1344my $domain = Net::Domain::domainname();
1345$maildomain = $domain if valid_fqdn($domain);
1346
1347return $maildomain;
1348}
1349
1350sub maildomain_mta {
1351my $maildomain;
1352
1353for my $host (qw(mailhost localhost)) {
1354require Net::SMTP;
1355my $smtp = Net::SMTP->new($host);
1356if (defined $smtp) {
1357my $domain = $smtp->domain;
1358$smtp->quit;
1359
1360$maildomain = $domain if valid_fqdn($domain);
1361
1362last if $maildomain;
1363}
1364}
1365
1366return $maildomain;
1367}
1368
1369sub maildomain {
1370return maildomain_net() || maildomain_mta() || 'localhost.localdomain';
1371}
1372
1373sub smtp_host_string {
1374if (defined $smtp_server_port) {
1375return "$smtp_server:$smtp_server_port";
1376} else {
1377return $smtp_server;
1378}
1379}
1380
1381# Returns 1 if authentication succeeded or was not necessary
1382# (smtp_user was not specified), and 0 otherwise.
1383
1384sub smtp_auth_maybe {
1385if (!defined $smtp_authuser || $auth || (defined $smtp_auth && $smtp_auth eq "none")) {
1386return 1;
1387}
1388
1389# Workaround AUTH PLAIN/LOGIN interaction defect
1390# with Authen::SASL::Cyrus
1391eval {
1392require Authen::SASL;
1393Authen::SASL->import(qw(Perl));
1394};
1395
1396# Check mechanism naming as defined in:
1397# https://tools.ietf.org/html/rfc4422#page-8
1398if ($smtp_auth && $smtp_auth !~ /^(\b[A-Z0-9-_]{1,20}\s*)*$/) {
1399die "invalid smtp auth: '${smtp_auth}'";
1400}
1401
1402# TODO: Authentication may fail not because credentials were
1403# invalid but due to other reasons, in which we should not
1404# reject credentials.
1405$auth = Git::credential({
1406'protocol' => 'smtp',
1407'host' => smtp_host_string(),
1408'username' => $smtp_authuser,
1409# if there's no password, "git credential fill" will
1410# give us one, otherwise it'll just pass this one.
1411'password' => $smtp_authpass
1412}, sub {
1413my $cred = shift;
1414
1415if ($smtp_auth) {
1416my $sasl = Authen::SASL->new(
1417mechanism => $smtp_auth,
1418callback => {
1419user => $cred->{'username'},
1420pass => $cred->{'password'},
1421authname => $cred->{'username'},
1422}
1423);
1424
1425return !!$smtp->auth($sasl);
1426}
1427
1428return !!$smtp->auth($cred->{'username'}, $cred->{'password'});
1429});
1430
1431return $auth;
1432}
1433
1434sub ssl_verify_params {
1435eval {
1436require IO::Socket::SSL;
1437IO::Socket::SSL->import(qw/SSL_VERIFY_PEER SSL_VERIFY_NONE/);
1438};
1439if ($@) {
1440print STDERR "Not using SSL_VERIFY_PEER due to out-of-date IO::Socket::SSL.\n";
1441return;
1442}
1443
1444if (!defined $smtp_ssl_cert_path) {
1445# use the OpenSSL defaults
1446return (SSL_verify_mode => SSL_VERIFY_PEER());
1447}
1448
1449if ($smtp_ssl_cert_path eq "") {
1450return (SSL_verify_mode => SSL_VERIFY_NONE());
1451} elsif (-d $smtp_ssl_cert_path) {
1452return (SSL_verify_mode => SSL_VERIFY_PEER(),
1453SSL_ca_path => $smtp_ssl_cert_path);
1454} elsif (-f $smtp_ssl_cert_path) {
1455return (SSL_verify_mode => SSL_VERIFY_PEER(),
1456SSL_ca_file => $smtp_ssl_cert_path);
1457} else {
1458die sprintf(__("CA path \"%s\" does not exist"), $smtp_ssl_cert_path);
1459}
1460}
1461
1462sub file_name_is_absolute {
1463my ($path) = @_;
1464
1465# msys does not grok DOS drive-prefixes
1466if ($^O eq 'msys') {
1467return ($path =~ m#^/# || $path =~ m#^[a-zA-Z]\:#)
1468}
1469
1470require File::Spec::Functions;
1471return File::Spec::Functions::file_name_is_absolute($path);
1472}
1473
1474sub gen_header {
1475my @recipients = unique_email_list(@to);
1476@cc = (grep { my $cc = extract_valid_address_or_die($_);
1477not grep { $cc eq $_ || $_ =~ /<\Q${cc}\E>$/ } @recipients
1478}
1479@cc);
1480my $to = join (",\n\t", @recipients);
1481@recipients = unique_email_list(@recipients,@cc,@initial_bcc);
1482@recipients = (map { extract_valid_address_or_die($_) } @recipients);
1483my $date = format_2822_time($time++);
1484my $gitversion = '@@GIT_VERSION@@';
1485if ($gitversion =~ m/..GIT_VERSION../) {
1486$gitversion = Git::version();
1487}
1488
1489my $cc = join(",\n\t", unique_email_list(@cc));
1490my $ccline = "";
1491if ($cc ne '') {
1492$ccline = "\nCc: $cc";
1493}
1494make_message_id() unless defined($message_id);
1495
1496my $header = "From: $sender
1497To: $to${ccline}
1498Subject: $subject
1499Date: $date
1500Message-ID: $message_id
1501";
1502if ($use_xmailer) {
1503$header .= "X-Mailer: git-send-email $gitversion\n";
1504}
1505if ($in_reply_to) {
1506
1507$header .= "In-Reply-To: $in_reply_to\n";
1508$header .= "References: $references\n";
1509}
1510if ($reply_to) {
1511$header .= "Reply-To: $reply_to\n";
1512}
1513if (@xh) {
1514$header .= join("\n", @xh) . "\n";
1515}
1516my $recipients_ref = \@recipients;
1517return ($recipients_ref, $to, $date, $gitversion, $cc, $ccline, $header);
1518}
1519
1520# Prepares the email, then asks the user what to do.
1521#
1522# If the user chooses to send the email, it's sent and 1 is returned.
1523# If the user chooses not to send the email, 0 is returned.
1524# If the user decides they want to make further edits, -1 is returned and the
1525# caller is expected to call send_message again after the edits are performed.
1526#
1527# If an error occurs sending the email, this just dies.
1528
1529sub send_message {
1530my ($recipients_ref, $to, $date, $gitversion, $cc, $ccline, $header) = gen_header();
1531my @recipients = @$recipients_ref;
1532
1533my @sendmail_parameters = ('-i', @recipients);
1534my $raw_from = $sender;
1535if (defined $envelope_sender && $envelope_sender ne "auto") {
1536$raw_from = $envelope_sender;
1537}
1538$raw_from = extract_valid_address($raw_from);
1539unshift (@sendmail_parameters,
1540'-f', $raw_from) if(defined $envelope_sender);
1541
1542if ($needs_confirm && !$dry_run) {
1543print "\n$header\n";
1544if ($needs_confirm eq "inform") {
1545$confirm_unconfigured = 0; # squelch this message for the rest of this run
1546$ask_default = "y"; # assume yes on EOF since user hasn't explicitly asked for confirmation
1547print __ <<EOF ;
1548The Cc list above has been expanded by additional
1549addresses found in the patch commit message. By default
1550send-email prompts before sending whenever this occurs.
1551This behavior is controlled by the sendemail.confirm
1552configuration setting.
1553
1554For additional information, run 'git send-email --help'.
1555To retain the current behavior, but squelch this message,
1556run 'git config --global sendemail.confirm auto'.
1557
1558EOF
1559}
1560# TRANSLATORS: Make sure to include [y] [n] [e] [q] [a] in your
1561# translation. The program will only accept English input
1562# at this point.
1563$_ = ask(__("Send this email? ([y]es|[n]o|[e]dit|[q]uit|[a]ll): "),
1564valid_re => qr/^(?:yes|y|no|n|edit|e|quit|q|all|a)/i,
1565default => $ask_default);
1566die __("Send this email reply required") unless defined $_;
1567if (/^n/i) {
1568return 0;
1569} elsif (/^e/i) {
1570return -1;
1571} elsif (/^q/i) {
1572cleanup_compose_files();
1573exit(0);
1574} elsif (/^a/i) {
1575$confirm = 'never';
1576}
1577}
1578
1579unshift (@sendmail_parameters, @smtp_server_options);
1580
1581if ($dry_run) {
1582# We don't want to send the email.
1583} elsif (defined $sendmail_cmd || file_name_is_absolute($smtp_server)) {
1584my $pid = open my $sm, '|-';
1585defined $pid or die $!;
1586if (!$pid) {
1587if (defined $sendmail_cmd) {
1588exec ("sh", "-c", "$sendmail_cmd \"\$@\"", "-", @sendmail_parameters)
1589or die $!;
1590} else {
1591exec ($smtp_server, @sendmail_parameters)
1592or die $!;
1593}
1594}
1595print $sm "$header\n$message";
1596close $sm or die $!;
1597} else {
1598
1599if (!defined $smtp_server) {
1600die __("The required SMTP server is not properly defined.")
1601}
1602
1603require Net::SMTP;
1604my $use_net_smtp_ssl = version->parse($Net::SMTP::VERSION) < version->parse("2.34");
1605$smtp_domain ||= maildomain();
1606
1607if ($smtp_encryption eq 'ssl') {
1608$smtp_server_port ||= 465; # ssmtp
1609require IO::Socket::SSL;
1610
1611# Suppress "variable accessed once" warning.
1612{
1613no warnings 'once';
1614$IO::Socket::SSL::DEBUG = 1;
1615}
1616
1617# Net::SMTP::SSL->new() does not forward any SSL options
1618IO::Socket::SSL::set_client_defaults(
1619ssl_verify_params());
1620
1621if ($use_net_smtp_ssl) {
1622require Net::SMTP::SSL;
1623$smtp ||= Net::SMTP::SSL->new($smtp_server,
1624Hello => $smtp_domain,
1625Port => $smtp_server_port,
1626Debug => $debug_net_smtp);
1627}
1628else {
1629$smtp ||= Net::SMTP->new($smtp_server,
1630Hello => $smtp_domain,
1631Port => $smtp_server_port,
1632Debug => $debug_net_smtp,
1633SSL => 1);
1634}
1635}
1636elsif (!$smtp) {
1637$smtp_server_port ||= 25;
1638$smtp ||= Net::SMTP->new($smtp_server,
1639Hello => $smtp_domain,
1640Debug => $debug_net_smtp,
1641Port => $smtp_server_port);
1642if ($smtp_encryption eq 'tls' && $smtp) {
1643if ($use_net_smtp_ssl) {
1644$smtp->command('STARTTLS');
1645$smtp->response();
1646if ($smtp->code != 220) {
1647die sprintf(__("Server does not support STARTTLS! %s"), $smtp->message);
1648}
1649require Net::SMTP::SSL;
1650$smtp = Net::SMTP::SSL->start_SSL($smtp,
1651ssl_verify_params())
1652or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
1653}
1654else {
1655$smtp->starttls(ssl_verify_params())
1656or die sprintf(__("STARTTLS failed! %s"), IO::Socket::SSL::errstr());
1657}
1658# Send EHLO again to receive fresh
1659# supported commands
1660$smtp->hello($smtp_domain);
1661}
1662}
1663
1664if (!$smtp) {
1665die __("Unable to initialize SMTP properly. Check config and use --smtp-debug."),
1666" VALUES: server=$smtp_server ",
1667"encryption=$smtp_encryption ",
1668"hello=$smtp_domain",
1669defined $smtp_server_port ? " port=$smtp_server_port" : "";
1670}
1671
1672smtp_auth_maybe or die $smtp->message;
1673
1674$smtp->mail( $raw_from ) or die $smtp->message;
1675$smtp->to( @recipients ) or die $smtp->message;
1676$smtp->data or die $smtp->message;
1677$smtp->datasend("$header\n") or die $smtp->message;
1678my @lines = split /^/, $message;
1679foreach my $line (@lines) {
1680$smtp->datasend("$line") or die $smtp->message;
1681}
1682$smtp->dataend() or die $smtp->message;
1683$smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message;
1684}
1685if ($quiet) {
1686printf($dry_run ? __("Dry-Sent %s") : __("Sent %s"), $subject);
1687print "\n";
1688} else {
1689print($dry_run ? __("Dry-OK. Log says:") : __("OK. Log says:"));
1690print "\n";
1691if (!defined $sendmail_cmd && !file_name_is_absolute($smtp_server)) {
1692print "Server: $smtp_server\n";
1693print "MAIL FROM:<$raw_from>\n";
1694foreach my $entry (@recipients) {
1695print "RCPT TO:<$entry>\n";
1696}
1697} else {
1698my $sm;
1699if (defined $sendmail_cmd) {
1700$sm = $sendmail_cmd;
1701} else {
1702$sm = $smtp_server;
1703}
1704
1705print "Sendmail: $sm ".join(' ',@sendmail_parameters)."\n";
1706}
1707print $header, "\n";
1708if ($smtp) {
1709print __("Result: "), $smtp->code, ' ',
1710($smtp->message =~ /\n([^\n]+\n)$/s);
1711} else {
1712print __("Result: OK");
1713}
1714print "\n";
1715}
1716
1717return 1;
1718}
1719
1720sub pre_process_file {
1721my ($t, $quiet) = @_;
1722
1723open my $fh, "<", $t or die sprintf(__("can't open file %s"), $t);
1724
1725my $author = undef;
1726my $sauthor = undef;
1727my $author_encoding;
1728my $has_content_type;
1729my $body_encoding;
1730my $xfer_encoding;
1731my $has_mime_version;
1732@to = ();
1733@cc = ();
1734@xh = ();
1735my $input_format = undef;
1736my @header = ();
1737$subject = $initial_subject;
1738$message = "";
1739$message_num++;
1740undef $message_id;
1741# Retrieve and unfold header fields.
1742my @header_lines = ();
1743while(<$fh>) {
1744last if /^\s*$/;
1745push(@header_lines, $_);
1746}
1747@header = unfold_headers(@header_lines);
1748# Add computed headers, if applicable.
1749unless ($no_header_cmd || ! $header_cmd) {
1750push @header, invoke_header_cmd($header_cmd, $t);
1751}
1752# Now parse the header
1753foreach(@header) {
1754if (/^From /) {
1755$input_format = 'mbox';
1756next;
1757}
1758chomp;
1759if (!defined $input_format && /^[-A-Za-z]+:\s/) {
1760$input_format = 'mbox';
1761}
1762
1763if (defined $input_format && $input_format eq 'mbox') {
1764if (/^Subject:\s+(.*)$/i) {
1765$subject = $1;
1766}
1767elsif (/^From:\s+(.*)$/i) {
1768($author, $author_encoding) = unquote_rfc2047($1);
1769$sauthor = sanitize_address($author);
1770next if $suppress_cc{'author'};
1771next if $suppress_cc{'self'} and $sauthor eq $sender;
1772printf(__("(mbox) Adding cc: %s from line '%s'\n"),
1773$1, $_) unless $quiet;
1774push @cc, $1;
1775}
1776elsif (/^To:\s+(.*)$/i) {
1777foreach my $addr (parse_address_line($1)) {
1778printf(__("(mbox) Adding to: %s from line '%s'\n"),
1779$addr, $_) unless $quiet;
1780push @to, $addr;
1781}
1782}
1783elsif (/^Cc:\s+(.*)$/i) {
1784foreach my $addr (parse_address_line($1)) {
1785my $qaddr = unquote_rfc2047($addr);
1786my $saddr = sanitize_address($qaddr);
1787if ($saddr eq $sender) {
1788next if ($suppress_cc{'self'});
1789} else {
1790next if ($suppress_cc{'cc'});
1791}
1792printf(__("(mbox) Adding cc: %s from line '%s'\n"),
1793$addr, $_) unless $quiet;
1794push @cc, $addr;
1795}
1796}
1797elsif (/^Content-type:/i) {
1798$has_content_type = 1;
1799if (/charset="?([^ "]+)/) {
1800$body_encoding = $1;
1801}
1802push @xh, $_;
1803}
1804elsif (/^MIME-Version/i) {
1805$has_mime_version = 1;
1806push @xh, $_;
1807}
1808elsif (/^Message-ID: (.*)/i) {
1809$message_id = $1;
1810}
1811elsif (/^Content-Transfer-Encoding: (.*)/i) {
1812$xfer_encoding = $1 if not defined $xfer_encoding;
1813}
1814elsif (/^In-Reply-To: (.*)/i) {
1815if (!$initial_in_reply_to || $thread) {
1816$in_reply_to = $1;
1817}
1818}
1819elsif (/^References: (.*)/i) {
1820if (!$initial_in_reply_to || $thread) {
1821$references = $1;
1822}
1823}
1824elsif (!/^Date:\s/i && /^[-A-Za-z]+:\s+\S/) {
1825push @xh, $_;
1826}
1827} else {
1828# In the traditional
1829# "send lots of email" format,
1830# line 1 = cc
1831# line 2 = subject
1832# So let's support that, too.
1833$input_format = 'lots';
1834if (@cc == 0 && !$suppress_cc{'cc'}) {
1835printf(__("(non-mbox) Adding cc: %s from line '%s'\n"),
1836$_, $_) unless $quiet;
1837push @cc, $_;
1838} elsif (!defined $subject) {
1839$subject = $_;
1840}
1841}
1842}
1843# Now parse the message body
1844while(<$fh>) {
1845$message .= $_;
1846if (/^([a-z][a-z-]*-by|Cc): (.*)/i) {
1847chomp;
1848my ($what, $c) = ($1, $2);
1849# strip garbage for the address we'll use:
1850$c = strip_garbage_one_address($c);
1851# sanitize a bit more to decide whether to suppress the address:
1852my $sc = sanitize_address($c);
1853if ($sc eq $sender) {
1854next if ($suppress_cc{'self'});
1855} else {
1856if ($what =~ /^Signed-off-by$/i) {
1857next if $suppress_cc{'sob'};
1858} elsif ($what =~ /-by$/i) {
1859next if $suppress_cc{'misc-by'};
1860} elsif ($what =~ /Cc/i) {
1861next if $suppress_cc{'bodycc'};
1862}
1863}
1864if ($c !~ /.+@.+|<.+>/) {
1865printf("(body) Ignoring %s from line '%s'\n",
1866$what, $_) unless $quiet;
1867next;
1868}
1869push @cc, $sc;
1870printf(__("(body) Adding cc: %s from line '%s'\n"),
1871$sc, $_) unless $quiet;
1872}
1873}
1874close $fh;
1875
1876push @to, recipients_cmd("to-cmd", "to", $to_cmd, $t, $quiet)
1877if defined $to_cmd;
1878push @cc, recipients_cmd("cc-cmd", "cc", $cc_cmd, $t, $quiet)
1879if defined $cc_cmd && !$suppress_cc{'cccmd'};
1880
1881if ($broken_encoding{$t} && !$has_content_type) {
1882$xfer_encoding = '8bit' if not defined $xfer_encoding;
1883$has_content_type = 1;
1884push @xh, "Content-Type: text/plain; charset=$auto_8bit_encoding";
1885$body_encoding = $auto_8bit_encoding;
1886}
1887
1888if ($broken_encoding{$t} && !is_rfc2047_quoted($subject)) {
1889$subject = quote_subject($subject, $auto_8bit_encoding);
1890}
1891
1892if (defined $sauthor and $sauthor ne $sender) {
1893$message = "From: $author\n\n$message";
1894if (defined $author_encoding) {
1895if ($has_content_type) {
1896if ($body_encoding eq $author_encoding) {
1897# ok, we already have the right encoding
1898}
1899else {
1900# uh oh, we should re-encode
1901}
1902}
1903else {
1904$xfer_encoding = '8bit' if not defined $xfer_encoding;
1905$has_content_type = 1;
1906push @xh,
1907"Content-Type: text/plain; charset=$author_encoding";
1908}
1909}
1910}
1911$xfer_encoding = '8bit' if not defined $xfer_encoding;
1912($message, $xfer_encoding) = apply_transfer_encoding(
1913$message, $xfer_encoding, $target_xfer_encoding);
1914push @xh, "Content-Transfer-Encoding: $xfer_encoding";
1915unshift @xh, 'MIME-Version: 1.0' unless $has_mime_version;
1916
1917$needs_confirm = (
1918$confirm eq "always" or
1919($confirm =~ /^(?:auto|cc)$/ && @cc) or
1920($confirm =~ /^(?:auto|compose)$/ && $compose && $message_num == 1));
1921$needs_confirm = "inform" if ($needs_confirm && $confirm_unconfigured && @cc);
1922
1923@to = process_address_list(@to);
1924@cc = process_address_list(@cc);
1925
1926@to = (@initial_to, @to);
1927@cc = (@initial_cc, @cc);
1928
1929if ($message_num == 1) {
1930if (defined $cover_cc and $cover_cc) {
1931@initial_cc = @cc;
1932}
1933if (defined $cover_to and $cover_to) {
1934@initial_to = @to;
1935}
1936}
1937}
1938
1939# Prepares the email, prompts the user, and sends it out
1940# Returns 0 if an edit was done and the function should be called again, or 1
1941# on the email being successfully sent out.
1942sub process_file {
1943my ($t) = @_;
1944
1945pre_process_file($t, $quiet);
1946
1947my $message_was_sent = send_message();
1948if ($message_was_sent == -1) {
1949do_edit($t);
1950return 0;
1951}
1952
1953# set up for the next message
1954if ($thread) {
1955if ($message_was_sent &&
1956($chain_reply_to || !defined $in_reply_to || length($in_reply_to) == 0 ||
1957$message_num == 1)) {
1958$in_reply_to = $message_id;
1959if (length $references > 0) {
1960$references .= "\n $message_id";
1961} else {
1962$references = "$message_id";
1963}
1964}
1965} elsif (!defined $initial_in_reply_to) {
1966# --thread and --in-reply-to manage the "In-Reply-To" header and by
1967# extension the "References" header. If these commands are not used, reset
1968# the header values to their defaults.
1969$in_reply_to = undef;
1970$references = '';
1971}
1972$message_id = undef;
1973$num_sent++;
1974if (defined $batch_size && $num_sent == $batch_size) {
1975$num_sent = 0;
1976$smtp->quit if defined $smtp;
1977undef $smtp;
1978undef $auth;
1979sleep($relogin_delay) if defined $relogin_delay;
1980}
1981
1982return 1;
1983}
1984
1985sub initialize_modified_loop_vars {
1986$in_reply_to = $initial_in_reply_to;
1987$references = $initial_in_reply_to || '';
1988$message_num = 0;
1989}
1990
1991if ($validate) {
1992# FIFOs can only be read once, exclude them from validation.
1993my @real_files = ();
1994foreach my $f (@files) {
1995unless (-p $f) {
1996push(@real_files, $f);
1997}
1998}
1999
2000# Run the loop once again to avoid gaps in the counter due to FIFO
2001# arguments provided by the user.
2002my $num = 1;
2003my $num_files = scalar @real_files;
2004$ENV{GIT_SENDEMAIL_FILE_TOTAL} = "$num_files";
2005initialize_modified_loop_vars();
2006foreach my $r (@real_files) {
2007$ENV{GIT_SENDEMAIL_FILE_COUNTER} = "$num";
2008pre_process_file($r, 1);
2009validate_patch($r, $target_xfer_encoding);
2010$num += 1;
2011}
2012delete $ENV{GIT_SENDEMAIL_FILE_COUNTER};
2013delete $ENV{GIT_SENDEMAIL_FILE_TOTAL};
2014}
2015
2016initialize_modified_loop_vars();
2017foreach my $t (@files) {
2018while (!process_file($t)) {
2019# user edited the file
2020}
2021}
2022
2023# Execute a command and return its output lines as an array. Blank
2024# lines which do not appear at the end of the output are reported as
2025# errors.
2026sub execute_cmd {
2027my ($prefix, $cmd, $file) = @_;
2028my @lines = ();
2029my $seen_blank_line = 0;
2030open my $fh, "-|", "$cmd \Q$file\E"
2031or die sprintf(__("(%s) Could not execute '%s'"), $prefix, $cmd);
2032while (my $line = <$fh>) {
2033die sprintf(__("(%s) Malformed output from '%s'"), $prefix, $cmd)
2034if $seen_blank_line;
2035if ($line =~ /^$/) {
2036$seen_blank_line = $line =~ /^$/;
2037next;
2038}
2039push @lines, $line;
2040}
2041close $fh
2042or die sprintf(__("(%s) failed to close pipe to '%s'"), $prefix, $cmd);
2043return @lines;
2044}
2045
2046# Process headers lines, unfolding multiline headers as defined by RFC
2047# 2822.
2048sub unfold_headers {
2049my @headers;
2050foreach(@_) {
2051last if /^\s*$/;
2052if (/^\s+\S/ and @headers) {
2053chomp($headers[$#headers]);
2054s/^\s+/ /;
2055$headers[$#headers] .= $_;
2056} else {
2057push(@headers, $_);
2058}
2059}
2060return @headers;
2061}
2062
2063# Invoke the provided CMD with FILE as an argument, which should
2064# output RFC 2822 email headers. Fold multiline headers and return the
2065# headers as an array.
2066sub invoke_header_cmd {
2067my ($cmd, $file) = @_;
2068my @lines = execute_cmd("header-cmd", $header_cmd, $file);
2069return unfold_headers(@lines);
2070}
2071
2072# Execute a command (e.g. $to_cmd) to get a list of email addresses
2073# and return a results array
2074sub recipients_cmd {
2075my ($prefix, $what, $cmd, $file, $quiet) = @_;
2076my @lines = ();
2077my @addresses = ();
2078
2079@lines = execute_cmd($prefix, $cmd, $file);
2080for my $address (@lines) {
2081$address =~ s/^\s*//g;
2082$address =~ s/\s*$//g;
2083$address = sanitize_address($address);
2084next if ($address eq $sender and $suppress_cc{'self'});
2085push @addresses, $address;
2086printf(__("(%s) Adding %s: %s from: '%s'\n"),
2087$prefix, $what, $address, $cmd) unless $quiet;
2088}
2089return @addresses;
2090}
2091
2092cleanup_compose_files();
2093
2094sub cleanup_compose_files {
2095unlink($compose_filename, $compose_filename . ".final") if $compose;
2096}
2097
2098$smtp->quit if $smtp;
2099
2100sub apply_transfer_encoding {
2101my $message = shift;
2102my $from = shift;
2103my $to = shift;
2104
2105return ($message, $to) if ($from eq $to and $from ne '7bit');
2106
2107require MIME::QuotedPrint;
2108require MIME::Base64;
2109
2110$message = MIME::QuotedPrint::decode($message)
2111if ($from eq 'quoted-printable');
2112$message = MIME::Base64::decode($message)
2113if ($from eq 'base64');
2114
2115$to = ($message =~ /(?:.{999,}|\r)/) ? 'quoted-printable' : '8bit'
2116if $to eq 'auto';
2117
2118die __("cannot send message as 7bit")
2119if ($to eq '7bit' and $message =~ /[^[:ascii:]]/);
2120return ($message, $to)
2121if ($to eq '7bit' or $to eq '8bit');
2122return (MIME::QuotedPrint::encode($message, "\n", 0), $to)
2123if ($to eq 'quoted-printable');
2124return (MIME::Base64::encode($message, "\n"), $to)
2125if ($to eq 'base64');
2126die __("invalid transfer encoding");
2127}
2128
2129sub unique_email_list {
2130my %seen;
2131my @emails;
2132
2133foreach my $entry (@_) {
2134my $clean = extract_valid_address_or_die($entry);
2135$seen{$clean} ||= 0;
2136next if $seen{$clean}++;
2137push @emails, $entry;
2138}
2139return @emails;
2140}
2141
2142sub validate_patch {
2143my ($fn, $xfer_encoding) = @_;
2144
2145if ($repo) {
2146my $hook_name = 'sendemail-validate';
2147my $hooks_path = $repo->command_oneline('rev-parse', '--git-path', 'hooks');
2148require File::Spec;
2149my $validate_hook = File::Spec->catfile($hooks_path, $hook_name);
2150my $hook_error;
2151if (-x $validate_hook) {
2152require Cwd;
2153my $target = Cwd::abs_path($fn);
2154# The hook needs a correct cwd and GIT_DIR.
2155my $cwd_save = Cwd::getcwd();
2156chdir($repo->wc_path() or $repo->repo_path())
2157or die("chdir: $!");
2158local $ENV{"GIT_DIR"} = $repo->repo_path();
2159
2160my ($recipients_ref, $to, $date, $gitversion, $cc, $ccline, $header) = gen_header();
2161
2162require File::Temp;
2163my ($header_filehandle, $header_filename) = File::Temp::tempfile(
2164TEMPLATE => ".gitsendemail.header.XXXXXX",
2165DIR => $repo->repo_path(),
2166UNLINK => 1,
2167);
2168print $header_filehandle $header;
2169
2170my @cmd = ("git", "hook", "run", "--ignore-missing",
2171$hook_name, "--");
2172my @cmd_msg = (@cmd, "<patch>", "<header>");
2173my @cmd_run = (@cmd, $target, $header_filename);
2174$hook_error = system_or_msg(\@cmd_run, undef, "@cmd_msg");
2175chdir($cwd_save) or die("chdir: $!");
2176}
2177if ($hook_error) {
2178$hook_error = sprintf(
2179__("fatal: %s: rejected by %s hook\n%s\nwarning: no patches were sent\n"),
2180$fn, $hook_name, $hook_error);
2181die $hook_error;
2182}
2183}
2184
2185# Any long lines will be automatically fixed if we use a suitable transfer
2186# encoding.
2187unless ($xfer_encoding =~ /^(?:auto|quoted-printable|base64)$/) {
2188open(my $fh, '<', $fn)
2189or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
2190while (my $line = <$fh>) {
2191if (length($line) > 998) {
2192die sprintf(__("fatal: %s:%d is longer than 998 characters\n" .
2193"warning: no patches were sent\n"), $fn, $.);
2194}
2195}
2196}
2197return;
2198}
2199
2200sub handle_backup {
2201my ($last, $lastlen, $file, $known_suffix) = @_;
2202my ($suffix, $skip);
2203
2204$skip = 0;
2205if (defined $last &&
2206($lastlen < length($file)) &&
2207(substr($file, 0, $lastlen) eq $last) &&
2208($suffix = substr($file, $lastlen)) !~ /^[a-z0-9]/i) {
2209if (defined $known_suffix && $suffix eq $known_suffix) {
2210printf(__("Skipping %s with backup suffix '%s'.\n"), $file, $known_suffix);
2211$skip = 1;
2212} else {
2213# TRANSLATORS: please keep "[y|N]" as is.
2214my $answer = ask(sprintf(__("Do you really want to send %s? [y|N]: "), $file),
2215valid_re => qr/^(?:y|n)/i,
2216default => 'n');
2217$skip = ($answer ne 'y');
2218if ($skip) {
2219$known_suffix = $suffix;
2220}
2221}
2222}
2223return ($skip, $known_suffix);
2224}
2225
2226sub handle_backup_files {
2227my @file = @_;
2228my ($last, $lastlen, $known_suffix, $skip, @result);
2229for my $file (@file) {
2230($skip, $known_suffix) = handle_backup($last, $lastlen,
2231$file, $known_suffix);
2232push @result, $file unless $skip;
2233$last = $file;
2234$lastlen = length($file);
2235}
2236return @result;
2237}
2238
2239sub file_has_nonascii {
2240my $fn = shift;
2241open(my $fh, '<', $fn)
2242or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
2243while (my $line = <$fh>) {
2244return 1 if $line =~ /[^[:ascii:]]/;
2245}
2246return 0;
2247}
2248
2249sub body_or_subject_has_nonascii {
2250my $fn = shift;
2251open(my $fh, '<', $fn)
2252or die sprintf(__("unable to open %s: %s\n"), $fn, $!);
2253while (my $line = <$fh>) {
2254last if $line =~ /^$/;
2255return 1 if $line =~ /^Subject.*[^[:ascii:]]/;
2256}
2257while (my $line = <$fh>) {
2258return 1 if $line =~ /[^[:ascii:]]/;
2259}
2260return 0;
2261}
2262