]>
Commit | Line | Data |
---|---|---|
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) | |
5825e5b2 | 7 | # |
83b24437 RA |
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. |
5825e5b2 | 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: | |
5825e5b2 | 15 | # first line of the message is who to CC, |
f3d9f354 | 16 | # and second line is the subject of the message. |
5825e5b2 | 17 | # |
83b24437 RA |
18 | |
19 | use strict; | |
20 | use warnings; | |
21 | use Term::ReadLine; | |
9133261f | 22 | use Mail::Sendmail qw(sendmail %mailcfg); |
83b24437 RA |
23 | use Getopt::Long; |
24 | use Data::Dumper; | |
25 | use Email::Valid; | |
26 | ||
e205735d | 27 | sub unique_email_list(@); |
1f038a0c RA |
28 | sub cleanup_compose_files(); |
29 | ||
30 | # Constants (essentially) | |
31 | my $compose_filename = ".msg.$$"; | |
e205735d | 32 | |
83b24437 | 33 | # Variables we fill in automatically, or via prompting: |
1f038a0c | 34 | my (@to,@cc,$initial_reply_to,$initial_subject,@files,$from,$compose); |
83b24437 | 35 | |
78488b2c | 36 | # Behavior modification variables |
3342d850 | 37 | my ($chain_reply_to, $smtp_server) = (1, "localhost"); |
78488b2c | 38 | |
9133261f | 39 | # Example reply to: |
83b24437 | 40 | #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>'; |
83b24437 RA |
41 | |
42 | my $term = new Term::ReadLine 'git-send-email'; | |
43 | ||
44 | # Begin by accumulating all the variables (defined above), that we will end up | |
45 | # needing, first, from the command line: | |
46 | ||
47 | my $rc = GetOptions("from=s" => \$from, | |
48 | "in-reply-to=s" => \$initial_reply_to, | |
49 | "subject=s" => \$initial_subject, | |
50 | "to=s" => \@to, | |
78488b2c | 51 | "chain-reply-to!" => \$chain_reply_to, |
3342d850 | 52 | "smtp-server=s" => \$smtp_server, |
1f038a0c | 53 | "compose" => \$compose, |
83b24437 RA |
54 | ); |
55 | ||
56 | # Now, let's fill any that aren't set in with defaults: | |
57 | ||
58 | open(GITVAR,"-|","git-var","-l") | |
59 | or die "Failed to open pipe from git-var: $!"; | |
60 | ||
61 | my ($author,$committer); | |
62 | while(<GITVAR>) { | |
63 | chomp; | |
64 | my ($var,$data) = split /=/,$_,2; | |
65 | my @fields = split /\s+/, $data; | |
66 | ||
67 | my $ident = join(" ", @fields[0...(@fields-3)]); | |
68 | ||
69 | if ($var eq 'GIT_AUTHOR_IDENT') { | |
70 | $author = $ident; | |
71 | } elsif ($var eq 'GIT_COMMITTER_IDENT') { | |
72 | $committer = $ident; | |
73 | } | |
74 | } | |
75 | close(GITVAR); | |
76 | ||
1f038a0c | 77 | my $prompting = 0; |
83b24437 RA |
78 | if (!defined $from) { |
79 | $from = $author || $committer; | |
8037d1a3 RA |
80 | do { |
81 | $_ = $term->readline("Who should the emails appear to be from? ", | |
82 | $from); | |
ca9a7d65 | 83 | } while (!defined $_); |
8037d1a3 | 84 | |
83b24437 RA |
85 | $from = $_; |
86 | print "Emails will be sent from: ", $from, "\n"; | |
1f038a0c | 87 | $prompting++; |
83b24437 RA |
88 | } |
89 | ||
90 | if (!@to) { | |
8037d1a3 | 91 | do { |
5825e5b2 | 92 | $_ = $term->readline("Who should the emails be sent to? ", |
8037d1a3 RA |
93 | ""); |
94 | } while (!defined $_); | |
83b24437 RA |
95 | my $to = $_; |
96 | push @to, split /,/, $to; | |
1f038a0c | 97 | $prompting++; |
83b24437 RA |
98 | } |
99 | ||
1f038a0c | 100 | if (!defined $initial_subject && $compose) { |
8037d1a3 | 101 | do { |
5825e5b2 | 102 | $_ = $term->readline("What subject should the emails start with? ", |
8037d1a3 RA |
103 | $initial_subject); |
104 | } while (!defined $_); | |
83b24437 | 105 | $initial_subject = $_; |
1f038a0c | 106 | $prompting++; |
83b24437 RA |
107 | } |
108 | ||
1f038a0c | 109 | if (!defined $initial_reply_to && $prompting) { |
8037d1a3 | 110 | do { |
1f038a0c | 111 | $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ", |
8037d1a3 RA |
112 | $initial_reply_to); |
113 | } while (!defined $_); | |
114 | ||
83b24437 | 115 | $initial_reply_to = $_; |
78488b2c | 116 | $initial_reply_to =~ s/(^\s+|\s+$)//g; |
83b24437 RA |
117 | } |
118 | ||
3342d850 RA |
119 | if (!defined $smtp_server) { |
120 | $smtp_server = "localhost"; | |
121 | } | |
122 | ||
1f038a0c RA |
123 | if ($compose) { |
124 | # Note that this does not need to be secure, but we will make a small | |
125 | # effort to have it be unique | |
126 | open(C,">",$compose_filename) | |
127 | or die "Failed to open for writing $compose_filename: $!"; | |
128 | print C "From \n"; | |
129 | printf C "Subject: %s\n\n", $initial_subject; | |
130 | printf C <<EOT; | |
131 | GIT: Please enter your email below. | |
132 | GIT: Lines beginning in "GIT: " will be removed. | |
133 | GIT: Consider including an overall diffstat or table of contents | |
134 | GIT: for the patch you are writing. | |
135 | ||
136 | EOT | |
137 | close(C); | |
138 | ||
139 | my $editor = $ENV{EDITOR}; | |
140 | $editor = 'vi' unless defined $editor; | |
141 | system($editor, $compose_filename); | |
142 | ||
143 | open(C2,">",$compose_filename . ".final") | |
144 | or die "Failed to open $compose_filename.final : " . $!; | |
145 | ||
146 | open(C,"<",$compose_filename) | |
147 | or die "Failed to open $compose_filename : " . $!; | |
148 | ||
149 | while(<C>) { | |
150 | next if m/^GIT: /; | |
151 | print C2 $_; | |
152 | } | |
153 | close(C); | |
154 | close(C2); | |
155 | ||
156 | do { | |
157 | $_ = $term->readline("Send this email? (y|n) "); | |
158 | } while (!defined $_); | |
159 | ||
160 | if (uc substr($_,0,1) ne 'Y') { | |
161 | cleanup_compose_files(); | |
162 | exit(0); | |
163 | } | |
164 | ||
165 | @files = ($compose_filename . ".final"); | |
166 | } | |
167 | ||
168 | ||
83b24437 RA |
169 | # Now that all the defaults are set, process the rest of the command line |
170 | # arguments and collect up the files that need to be processed. | |
171 | for my $f (@ARGV) { | |
172 | if (-d $f) { | |
173 | opendir(DH,$f) | |
174 | or die "Failed to opendir $f: $!"; | |
175 | ||
5825e5b2 | 176 | push @files, grep { -f $_ } map { +$f . "/" . $_ } |
8037d1a3 RA |
177 | sort readdir(DH); |
178 | ||
83b24437 RA |
179 | } elsif (-f $f) { |
180 | push @files, $f; | |
181 | ||
182 | } else { | |
183 | print STDERR "Skipping $f - not found.\n"; | |
184 | } | |
185 | } | |
186 | ||
187 | if (@files) { | |
188 | print $_,"\n" for @files; | |
189 | } else { | |
190 | print <<EOT; | |
215a7ad1 | 191 | git-send-email [options] <file | directory> [... file | directory ] |
83b24437 RA |
192 | Options: |
193 | --from Specify the "From:" line of the email to be sent. | |
1f038a0c | 194 | |
5825e5b2 | 195 | --to Specify the primary "To:" line of the email. |
1f038a0c RA |
196 | |
197 | --compose Use \$EDITOR to edit an introductory message for the | |
198 | patch series. | |
199 | ||
83b24437 | 200 | --subject Specify the initial "Subject:" line. |
1f038a0c RA |
201 | Only necessary if --compose is also set. If --compose |
202 | is not set, this will be prompted for. | |
203 | ||
83b24437 | 204 | --in-reply-to Specify the first "In-Reply-To:" header line. |
1f038a0c RA |
205 | Only used if --compose is also set. If --compose is not |
206 | set, this will be prompted for. | |
207 | ||
e205735d | 208 | --chain-reply-to If set, the replies will all be to the previous |
5825e5b2 JH |
209 | email sent, rather than to the first email sent. |
210 | Defaults to on. | |
1f038a0c | 211 | |
3342d850 RA |
212 | --smtp-server If set, specifies the outgoing SMTP server to use. |
213 | Defaults to localhost. | |
83b24437 RA |
214 | |
215 | Error: Please specify a file or a directory on the command line. | |
216 | EOT | |
217 | exit(1); | |
218 | } | |
219 | ||
220 | # Variables we set as part of the loop over files | |
221 | our ($message_id, $cc, %mail, $subject, $reply_to, $message); | |
222 | ||
223 | ||
224 | # Usually don't need to change anything below here. | |
225 | ||
226 | # we make a "fake" message id by taking the current number | |
227 | # of seconds since the beginning of Unix time and tacking on | |
228 | # a random number to the end, in case we are called quicker than | |
229 | # 1 second since the last time we were called. | |
8037d1a3 RA |
230 | |
231 | # We'll setup a template for the message id, using the "from" address: | |
232 | my $message_id_from = Email::Valid->address($from); | |
e205735d | 233 | my $message_id_template = "<%s-git-send-email-$message_id_from>"; |
8037d1a3 | 234 | |
83b24437 RA |
235 | sub make_message_id |
236 | { | |
237 | my $date = `date "+\%s"`; | |
238 | chomp($date); | |
239 | my $pseudo_rand = int (rand(4200)); | |
8037d1a3 RA |
240 | $message_id = sprintf $message_id_template, "$date$pseudo_rand"; |
241 | #print "new message id = $message_id\n"; # Was useful for debugging | |
83b24437 RA |
242 | } |
243 | ||
244 | ||
245 | ||
246 | $cc = ""; | |
247 | ||
248 | sub send_message | |
249 | { | |
e205735d | 250 | my $to = join (", ", unique_email_list(@to)); |
83b24437 RA |
251 | |
252 | %mail = ( To => $to, | |
253 | From => $from, | |
254 | CC => $cc, | |
255 | Subject => $subject, | |
256 | Message => $message, | |
257 | 'Reply-to' => $from, | |
258 | 'In-Reply-To' => $reply_to, | |
259 | 'Message-ID' => $message_id, | |
215a7ad1 | 260 | 'X-Mailer' => "git-send-email", |
83b24437 RA |
261 | ); |
262 | ||
3342d850 | 263 | $mail{smtp} = $smtp_server; |
9133261f | 264 | $mailcfg{mime} = 0; |
83b24437 RA |
265 | |
266 | #print Data::Dumper->Dump([\%mail],[qw(*mail)]); | |
267 | ||
268 | sendmail(%mail) or die $Mail::Sendmail::error; | |
269 | ||
270 | print "OK. Log says:\n", $Mail::Sendmail::log; | |
271 | print "\n\n" | |
272 | } | |
273 | ||
274 | ||
275 | $reply_to = $initial_reply_to; | |
276 | make_message_id(); | |
277 | $subject = $initial_subject; | |
278 | ||
279 | foreach my $t (@files) { | |
280 | my $F = $t; | |
281 | open(F,"<",$t) or die "can't open file $t"; | |
282 | ||
283 | @cc = (); | |
284 | my $found_mbox = 0; | |
285 | my $header_done = 0; | |
286 | $message = ""; | |
287 | while(<F>) { | |
288 | if (!$header_done) { | |
289 | $found_mbox = 1, next if (/^From /); | |
290 | chomp; | |
291 | ||
292 | if ($found_mbox) { | |
293 | if (/^Subject:\s+(.*)$/) { | |
294 | $subject = $1; | |
295 | ||
296 | } elsif (/^(Cc|From):\s+(.*)$/) { | |
297 | printf("(mbox) Adding cc: %s from line '%s'\n", | |
298 | $2, $_); | |
299 | push @cc, $2; | |
300 | } | |
301 | ||
302 | } else { | |
303 | # In the traditional | |
304 | # "send lots of email" format, | |
305 | # line 1 = cc | |
306 | # line 2 = subject | |
307 | # So let's support that, too. | |
308 | if (@cc == 0) { | |
309 | printf("(non-mbox) Adding cc: %s from line '%s'\n", | |
310 | $_, $_); | |
311 | ||
312 | push @cc, $_; | |
313 | ||
314 | } elsif (!defined $subject) { | |
315 | $subject = $_; | |
316 | } | |
317 | } | |
5825e5b2 | 318 | |
83b24437 RA |
319 | # A whitespace line will terminate the headers |
320 | if (m/^\s*$/) { | |
321 | $header_done = 1; | |
322 | } | |
323 | } else { | |
324 | $message .= $_; | |
325 | if (/^Signed-off-by: (.*)$/i) { | |
326 | my $c = $1; | |
327 | chomp $c; | |
328 | push @cc, $c; | |
329 | printf("(sob) Adding cc: %s from line '%s'\n", | |
330 | $c, $_); | |
331 | } | |
332 | } | |
333 | } | |
334 | close F; | |
335 | ||
e205735d | 336 | $cc = join(", ", unique_email_list(@cc)); |
83b24437 RA |
337 | |
338 | send_message(); | |
339 | ||
340 | # set up for the next message | |
78488b2c RA |
341 | if ($chain_reply_to || length($reply_to) == 0) { |
342 | $reply_to = $message_id; | |
343 | } | |
83b24437 | 344 | make_message_id(); |
83b24437 | 345 | } |
e205735d | 346 | |
1f038a0c RA |
347 | if ($compose) { |
348 | cleanup_compose_files(); | |
349 | } | |
350 | ||
351 | sub cleanup_compose_files() { | |
352 | unlink($compose_filename, $compose_filename . ".final"); | |
353 | ||
354 | } | |
355 | ||
356 | ||
e205735d RA |
357 | |
358 | sub unique_email_list(@) { | |
359 | my %seen; | |
360 | my @emails; | |
361 | ||
362 | foreach my $entry (@_) { | |
363 | my $clean = Email::Valid->address($entry); | |
364 | next if $seen{$clean}++; | |
365 | push @emails, $entry; | |
366 | } | |
367 | return @emails; | |
368 | } |