@noldova/checkpoints.git.linux-arm64 0.0.0-stage → 0.0.1

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