]> git.ipfire.org Git - thirdparty/git.git/blame - git-send-email-script
[PATCH] git-send-email-script: Reformat readline interface and generate a better...
[thirdparty/git.git] / git-send-email-script
CommitLineData
83b24437 1#!/usr/bin/perl -w
83b24437 2#
f3d9f354
RA
3# Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
4# Copyright 2005 Ryan Anderson <ryan@michonline.com>
83b24437
RA
5#
6# GPL v2 (See COPYING)
7#
8# Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com>
9#
f3d9f354 10# Sends a collection of emails to the given email addresses, disturbingly fast.
83b24437 11#
f3d9f354
RA
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.
83b24437
RA
17#
18
19use strict;
20use warnings;
21use Term::ReadLine;
9133261f 22use Mail::Sendmail qw(sendmail %mailcfg);
83b24437
RA
23use Getopt::Long;
24use Data::Dumper;
25use Email::Valid;
26
27# Variables we fill in automatically, or via prompting:
28my (@to,@cc,$initial_reply_to,$initial_subject,@files,$from);
29
78488b2c
RA
30# Behavior modification variables
31my ($chain_reply_to) = (1);
32
9133261f 33# Example reply to:
83b24437 34#$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
83b24437
RA
35
36my $term = new Term::ReadLine 'git-send-email';
37
38# Begin by accumulating all the variables (defined above), that we will end up
39# needing, first, from the command line:
40
41my $rc = GetOptions("from=s" => \$from,
42 "in-reply-to=s" => \$initial_reply_to,
43 "subject=s" => \$initial_subject,
44 "to=s" => \@to,
78488b2c 45 "chain-reply-to!" => \$chain_reply_to,
83b24437
RA
46 );
47
48# Now, let's fill any that aren't set in with defaults:
49
50open(GITVAR,"-|","git-var","-l")
51 or die "Failed to open pipe from git-var: $!";
52
53my ($author,$committer);
54while(<GITVAR>) {
55 chomp;
56 my ($var,$data) = split /=/,$_,2;
57 my @fields = split /\s+/, $data;
58
59 my $ident = join(" ", @fields[0...(@fields-3)]);
60
61 if ($var eq 'GIT_AUTHOR_IDENT') {
62 $author = $ident;
63 } elsif ($var eq 'GIT_COMMITTER_IDENT') {
64 $committer = $ident;
65 }
66}
67close(GITVAR);
68
69
70if (!defined $from) {
71 $from = $author || $committer;
8037d1a3
RA
72 do {
73 $_ = $term->readline("Who should the emails appear to be from? ",
74 $from);
75 while (!defined $_);
76
83b24437
RA
77 $from = $_;
78 print "Emails will be sent from: ", $from, "\n";
79}
80
81if (!@to) {
8037d1a3
RA
82 do {
83 $_ = $term->readline("Who should the emails be sent to? ",
84 "");
85 } while (!defined $_);
83b24437
RA
86 my $to = $_;
87 push @to, split /,/, $to;
88}
89
90if (!defined $initial_subject) {
8037d1a3
RA
91 do {
92 $_ = $term->readline("What subject should the emails start with? ",
93 $initial_subject);
94 } while (!defined $_);
83b24437
RA
95 $initial_subject = $_;
96}
97
98if (!defined $initial_reply_to) {
8037d1a3
RA
99 do {
100 $_= $term->readline("Message-ID to be used as In-Reply-To? ",
101 $initial_reply_to);
102 } while (!defined $_);
103
83b24437 104 $initial_reply_to = $_;
78488b2c 105 $initial_reply_to =~ s/(^\s+|\s+$)//g;
83b24437
RA
106}
107
108# Now that all the defaults are set, process the rest of the command line
109# arguments and collect up the files that need to be processed.
110for my $f (@ARGV) {
111 if (-d $f) {
112 opendir(DH,$f)
113 or die "Failed to opendir $f: $!";
114
8037d1a3
RA
115 push @files, map { +$f . "/" . $_ } grep { -f $_ }
116 sort readdir(DH);
117
83b24437
RA
118 } elsif (-f $f) {
119 push @files, $f;
120
121 } else {
122 print STDERR "Skipping $f - not found.\n";
123 }
124}
125
126if (@files) {
127 print $_,"\n" for @files;
128} else {
129 print <<EOT;
130git-send-email-script [options] <file | directory> [... file | directory ]
131Options:
132 --from Specify the "From:" line of the email to be sent.
133 --to Specify the primary "To:" line of the email.
134 --subject Specify the initial "Subject:" line.
135 --in-reply-to Specify the first "In-Reply-To:" header line.
78488b2c
RA
136 --chain-reply-to If set, the replies will all be to the first
137 email sent, rather than to the last email sent.
83b24437
RA
138
139Error: Please specify a file or a directory on the command line.
140EOT
141 exit(1);
142}
143
144# Variables we set as part of the loop over files
145our ($message_id, $cc, %mail, $subject, $reply_to, $message);
146
147
148# Usually don't need to change anything below here.
149
150# we make a "fake" message id by taking the current number
151# of seconds since the beginning of Unix time and tacking on
152# a random number to the end, in case we are called quicker than
153# 1 second since the last time we were called.
8037d1a3
RA
154
155# We'll setup a template for the message id, using the "from" address:
156my $message_id_from = Email::Valid->address($from);
157my $message_id_template = "<%s-git-send-email-$from>";
158
83b24437
RA
159sub make_message_id
160{
161 my $date = `date "+\%s"`;
162 chomp($date);
163 my $pseudo_rand = int (rand(4200));
8037d1a3
RA
164 $message_id = sprintf $message_id_template, "$date$pseudo_rand";
165 #print "new message id = $message_id\n"; # Was useful for debugging
83b24437
RA
166}
167
168
169
170$cc = "";
171
172sub send_message
173{
174 my %to;
175 $to{lc(Email::Valid->address($_))}++ for (@to);
176
177 my $to = join(",", keys %to);
178
179 %mail = ( To => $to,
180 From => $from,
181 CC => $cc,
182 Subject => $subject,
183 Message => $message,
184 'Reply-to' => $from,
185 'In-Reply-To' => $reply_to,
186 'Message-ID' => $message_id,
187 'X-Mailer' => "git-send-email-script",
188 );
189
190 $mail{smtp} = 'localhost';
9133261f 191 $mailcfg{mime} = 0;
83b24437
RA
192
193 #print Data::Dumper->Dump([\%mail],[qw(*mail)]);
194
195 sendmail(%mail) or die $Mail::Sendmail::error;
196
197 print "OK. Log says:\n", $Mail::Sendmail::log;
198 print "\n\n"
199}
200
201
202$reply_to = $initial_reply_to;
203make_message_id();
204$subject = $initial_subject;
205
206foreach my $t (@files) {
207 my $F = $t;
208 open(F,"<",$t) or die "can't open file $t";
209
210 @cc = ();
211 my $found_mbox = 0;
212 my $header_done = 0;
213 $message = "";
214 while(<F>) {
215 if (!$header_done) {
216 $found_mbox = 1, next if (/^From /);
217 chomp;
218
219 if ($found_mbox) {
220 if (/^Subject:\s+(.*)$/) {
221 $subject = $1;
222
223 } elsif (/^(Cc|From):\s+(.*)$/) {
224 printf("(mbox) Adding cc: %s from line '%s'\n",
225 $2, $_);
226 push @cc, $2;
227 }
228
229 } else {
230 # In the traditional
231 # "send lots of email" format,
232 # line 1 = cc
233 # line 2 = subject
234 # So let's support that, too.
235 if (@cc == 0) {
236 printf("(non-mbox) Adding cc: %s from line '%s'\n",
237 $_, $_);
238
239 push @cc, $_;
240
241 } elsif (!defined $subject) {
242 $subject = $_;
243 }
244 }
245
246 # A whitespace line will terminate the headers
247 if (m/^\s*$/) {
248 $header_done = 1;
249 }
250 } else {
251 $message .= $_;
252 if (/^Signed-off-by: (.*)$/i) {
253 my $c = $1;
254 chomp $c;
255 push @cc, $c;
256 printf("(sob) Adding cc: %s from line '%s'\n",
257 $c, $_);
258 }
259 }
260 }
261 close F;
262
263 my %clean_ccs;
264 $clean_ccs{lc(Email::Valid->address($_))}++ for @cc;
265
266 $cc = join(",", keys %clean_ccs);
267
268 send_message();
269
270 # set up for the next message
78488b2c
RA
271 if ($chain_reply_to || length($reply_to) == 0) {
272 $reply_to = $message_id;
273 }
83b24437
RA
274 make_message_id();
275# $subject = "Re: ".$initial_subject;
276}