]> asedeno.scripts.mit.edu Git - git.git/blob - git-send-email.perl
git-send-email --cc-cmd
[git.git] / git-send-email.perl
1 #!/usr/bin/perl -w
2 #
3 # Copyright 2002,2005 Greg Kroah-Hartman <greg@kroah.com>
4 # Copyright 2005 Ryan Anderson <ryan@michonline.com>
5 #
6 # GPL v2 (See COPYING)
7 #
8 # Ported to support git "mbox" format files by Ryan Anderson <ryan@michonline.com>
9 #
10 # Sends a collection of emails to the given email addresses, disturbingly fast.
11 #
12 # Supports two formats:
13 # 1. mbox format files (ignoring most headers and MIME formatting - this is designed for sending patches)
14 # 2. The original format support by Greg's script:
15 #    first line of the message is who to CC,
16 #    and second line is the subject of the message.
17 #
18
19 use strict;
20 use warnings;
21 use Term::ReadLine;
22 use Getopt::Long;
23 use Data::Dumper;
24 use Git;
25
26 package FakeTerm;
27 sub new {
28         my ($class, $reason) = @_;
29         return bless \$reason, shift;
30 }
31 sub readline {
32         my $self = shift;
33         die "Cannot use readline on FakeTerm: $$self";
34 }
35 package main;
36
37
38 sub usage {
39         print <<EOT;
40 git-send-email [options] <file | directory>...
41 Options:
42    --from         Specify the "From:" line of the email to be sent.
43
44    --to           Specify the primary "To:" line of the email.
45
46    --cc           Specify an initial "Cc:" list for the entire series
47                   of emails.
48
49    --cc-cmd       Specify a command to execute per file which adds
50                   per file specific cc address entries
51
52    --bcc          Specify a list of email addresses that should be Bcc:
53                   on all the emails.
54
55    --compose      Use \$GIT_EDITOR, core.editor, \$EDITOR, or \$VISUAL to edit
56                   an introductory message for the patch series.
57
58    --subject      Specify the initial "Subject:" line.
59                   Only necessary if --compose is also set.  If --compose
60                   is not set, this will be prompted for.
61
62    --in-reply-to  Specify the first "In-Reply-To:" header line.
63                   Only used if --compose is also set.  If --compose is not
64                   set, this will be prompted for.
65
66    --chain-reply-to If set, the replies will all be to the previous
67                   email sent, rather than to the first email sent.
68                   Defaults to on.
69
70    --signed-off-cc Automatically add email addresses that appear in
71                  Signed-off-by: or Cc: lines to the cc: list. Defaults to on.
72
73    --smtp-server  If set, specifies the outgoing SMTP server to use.
74                   Defaults to localhost.
75
76    --suppress-from Suppress sending emails to yourself if your address
77                   appears in a From: line. Defaults to off.
78
79    --thread       Specify that the "In-Reply-To:" header should be set on all
80                   emails. Defaults to on.
81
82    --quiet        Make git-send-email less verbose.  One line per email
83                   should be all that is output.
84
85    --dry-run      Do everything except actually send the emails.
86
87    --envelope-sender    Specify the envelope sender used to send the emails.
88
89 EOT
90         exit(1);
91 }
92
93 # most mail servers generate the Date: header, but not all...
94 sub format_2822_time {
95         my ($time) = @_;
96         my @localtm = localtime($time);
97         my @gmttm = gmtime($time);
98         my $localmin = $localtm[1] + $localtm[2] * 60;
99         my $gmtmin = $gmttm[1] + $gmttm[2] * 60;
100         if ($localtm[0] != $gmttm[0]) {
101                 die "local zone differs from GMT by a non-minute interval\n";
102         }
103         if ((($gmttm[6] + 1) % 7) == $localtm[6]) {
104                 $localmin += 1440;
105         } elsif ((($gmttm[6] - 1) % 7) == $localtm[6]) {
106                 $localmin -= 1440;
107         } elsif ($gmttm[6] != $localtm[6]) {
108                 die "local time offset greater than or equal to 24 hours\n";
109         }
110         my $offset = $localmin - $gmtmin;
111         my $offhour = $offset / 60;
112         my $offmin = abs($offset % 60);
113         if (abs($offhour) >= 24) {
114                 die ("local time offset greater than or equal to 24 hours\n");
115         }
116
117         return sprintf("%s, %2d %s %d %02d:%02d:%02d %s%02d%02d",
118                        qw(Sun Mon Tue Wed Thu Fri Sat)[$localtm[6]],
119                        $localtm[3],
120                        qw(Jan Feb Mar Apr May Jun
121                           Jul Aug Sep Oct Nov Dec)[$localtm[4]],
122                        $localtm[5]+1900,
123                        $localtm[2],
124                        $localtm[1],
125                        $localtm[0],
126                        ($offset >= 0) ? '+' : '-',
127                        abs($offhour),
128                        $offmin,
129                        );
130 }
131
132 my $have_email_valid = eval { require Email::Valid; 1 };
133 my $smtp;
134
135 sub unique_email_list(@);
136 sub cleanup_compose_files();
137
138 # Constants (essentially)
139 my $compose_filename = ".msg.$$";
140
141 # Variables we fill in automatically, or via prompting:
142 my (@to,@cc,@initial_cc,@bcclist,@xh,
143         $initial_reply_to,$initial_subject,@files,$author,$sender,$compose,$time);
144
145 my $smtp_server;
146 my $envelope_sender;
147
148 # Example reply to:
149 #$initial_reply_to = ''; #<20050203173208.GA23964@foobar.com>';
150
151 my $repo = Git->repository();
152 my $term = eval {
153         new Term::ReadLine 'git-send-email';
154 };
155 if ($@) {
156         $term = new FakeTerm "$@: going non-interactive";
157 }
158
159 # Behavior modification variables
160 my ($quiet, $dry_run) = (0, 0);
161
162 # Variables with corresponding config settings
163 my ($thread, $chain_reply_to, $suppress_from, $signed_off_cc, $cc_cmd);
164
165 my %config_settings = (
166     "thread" => [\$thread, 1],
167     "chainreplyto" => [\$chain_reply_to, 1],
168     "suppressfrom" => [\$suppress_from, 0],
169     "signedoffcc" => [\$signed_off_cc, 1],
170     "cccmd" => [\$cc_cmd, ""],
171 );
172
173 foreach my $setting (keys %config_settings) {
174     my $config = $repo->config_bool("sendemail.$setting");
175     ${$config_settings{$setting}->[0]} = (defined $config) ? $config : $config_settings{$setting}->[1];
176 }
177
178 @bcclist = $repo->config('sendemail.bcc');
179 if (!@bcclist or !$bcclist[0]) {
180     @bcclist = ();
181 }
182
183 # Begin by accumulating all the variables (defined above), that we will end up
184 # needing, first, from the command line:
185
186 my $rc = GetOptions("sender|from=s" => \$sender,
187                     "in-reply-to=s" => \$initial_reply_to,
188                     "subject=s" => \$initial_subject,
189                     "to=s" => \@to,
190                     "cc=s" => \@initial_cc,
191                     "bcc=s" => \@bcclist,
192                     "chain-reply-to!" => \$chain_reply_to,
193                     "smtp-server=s" => \$smtp_server,
194                     "compose" => \$compose,
195                     "quiet" => \$quiet,
196                     "cc-cmd=s" => \$cc_cmd,
197                     "suppress-from!" => \$suppress_from,
198                     "signed-off-cc|signed-off-by-cc!" => \$signed_off_cc,
199                     "dry-run" => \$dry_run,
200                     "envelope-sender=s" => \$envelope_sender,
201                     "thread!" => \$thread,
202          );
203
204 unless ($rc) {
205     usage();
206 }
207
208 # Verify the user input
209
210 foreach my $entry (@to) {
211         die "Comma in --to entry: $entry'\n" unless $entry !~ m/,/;
212 }
213
214 foreach my $entry (@initial_cc) {
215         die "Comma in --cc entry: $entry'\n" unless $entry !~ m/,/;
216 }
217
218 foreach my $entry (@bcclist) {
219         die "Comma in --bcclist entry: $entry'\n" unless $entry !~ m/,/;
220 }
221
222 # Now, let's fill any that aren't set in with defaults:
223
224 my ($repoauthor) = $repo->ident_person('author');
225 my ($repocommitter) = $repo->ident_person('committer');
226
227 my %aliases;
228 my @alias_files = $repo->config('sendemail.aliasesfile');
229 my $aliasfiletype = $repo->config('sendemail.aliasfiletype');
230 my %parse_alias = (
231         # multiline formats can be supported in the future
232         mutt => sub { my $fh = shift; while (<$fh>) {
233                 if (/^\s*alias\s+(\S+)\s+(.*)$/) {
234                         my ($alias, $addr) = ($1, $2);
235                         $addr =~ s/#.*$//; # mutt allows # comments
236                          # commas delimit multiple addresses
237                         $aliases{$alias} = [ split(/\s*,\s*/, $addr) ];
238                 }}},
239         mailrc => sub { my $fh = shift; while (<$fh>) {
240                 if (/^alias\s+(\S+)\s+(.*)$/) {
241                         # spaces delimit multiple addresses
242                         $aliases{$1} = [ split(/\s+/, $2) ];
243                 }}},
244         pine => sub { my $fh = shift; while (<$fh>) {
245                 if (/^(\S+)\t.*\t(.*)$/) {
246                         $aliases{$1} = [ split(/\s*,\s*/, $2) ];
247                 }}},
248         gnus => sub { my $fh = shift; while (<$fh>) {
249                 if (/\(define-mail-alias\s+"(\S+?)"\s+"(\S+?)"\)/) {
250                         $aliases{$1} = [ $2 ];
251                 }}}
252 );
253
254 if (@alias_files and $aliasfiletype and defined $parse_alias{$aliasfiletype}) {
255         foreach my $file (@alias_files) {
256                 open my $fh, '<', $file or die "opening $file: $!\n";
257                 $parse_alias{$aliasfiletype}->($fh);
258                 close $fh;
259         }
260 }
261
262 ($sender) = expand_aliases($sender) if defined $sender;
263
264 my $prompting = 0;
265 if (!defined $sender) {
266         $sender = $repoauthor || $repocommitter;
267         do {
268                 $_ = $term->readline("Who should the emails appear to be from? [$sender] ");
269         } while (!defined $_);
270
271         $sender = $_ if ($_);
272         print "Emails will be sent from: ", $sender, "\n";
273         $prompting++;
274 }
275
276 if (!@to) {
277         do {
278                 $_ = $term->readline("Who should the emails be sent to? ",
279                                 "");
280         } while (!defined $_);
281         my $to = $_;
282         push @to, split /,/, $to;
283         $prompting++;
284 }
285
286 sub expand_aliases {
287         my @cur = @_;
288         my @last;
289         do {
290                 @last = @cur;
291                 @cur = map { $aliases{$_} ? @{$aliases{$_}} : $_ } @last;
292         } while (join(',',@cur) ne join(',',@last));
293         return @cur;
294 }
295
296 @to = expand_aliases(@to);
297 @to = (map { sanitize_address($_) } @to);
298 @initial_cc = expand_aliases(@initial_cc);
299 @bcclist = expand_aliases(@bcclist);
300
301 if (!defined $initial_subject && $compose) {
302         do {
303                 $_ = $term->readline("What subject should the emails start with? ",
304                         $initial_subject);
305         } while (!defined $_);
306         $initial_subject = $_;
307         $prompting++;
308 }
309
310 if ($thread && !defined $initial_reply_to && $prompting) {
311         do {
312                 $_= $term->readline("Message-ID to be used as In-Reply-To for the first email? ",
313                         $initial_reply_to);
314         } while (!defined $_);
315
316         $initial_reply_to = $_;
317         $initial_reply_to =~ s/(^\s+|\s+$)//g;
318 }
319
320 if (!$smtp_server) {
321         $smtp_server = $repo->config('sendemail.smtpserver');
322 }
323 if (!$smtp_server) {
324         foreach (qw( /usr/sbin/sendmail /usr/lib/sendmail )) {
325                 if (-x $_) {
326                         $smtp_server = $_;
327                         last;
328                 }
329         }
330         $smtp_server ||= 'localhost'; # could be 127.0.0.1, too... *shrug*
331 }
332
333 if ($compose) {
334         # Note that this does not need to be secure, but we will make a small
335         # effort to have it be unique
336         open(C,">",$compose_filename)
337                 or die "Failed to open for writing $compose_filename: $!";
338         print C "From $sender # This line is ignored.\n";
339         printf C "Subject: %s\n\n", $initial_subject;
340         printf C <<EOT;
341 GIT: Please enter your email below.
342 GIT: Lines beginning in "GIT: " will be removed.
343 GIT: Consider including an overall diffstat or table of contents
344 GIT: for the patch you are writing.
345
346 EOT
347         close(C);
348
349         my $editor = $ENV{GIT_EDITOR} || $repo->config("core.editor") || $ENV{VISUAL} || $ENV{EDITOR} || "vi";
350         system($editor, $compose_filename);
351
352         open(C2,">",$compose_filename . ".final")
353                 or die "Failed to open $compose_filename.final : " . $!;
354
355         open(C,"<",$compose_filename)
356                 or die "Failed to open $compose_filename : " . $!;
357
358         while(<C>) {
359                 next if m/^GIT: /;
360                 print C2 $_;
361         }
362         close(C);
363         close(C2);
364
365         do {
366                 $_ = $term->readline("Send this email? (y|n) ");
367         } while (!defined $_);
368
369         if (uc substr($_,0,1) ne 'Y') {
370                 cleanup_compose_files();
371                 exit(0);
372         }
373
374         @files = ($compose_filename . ".final");
375 }
376
377
378 # Now that all the defaults are set, process the rest of the command line
379 # arguments and collect up the files that need to be processed.
380 for my $f (@ARGV) {
381         if (-d $f) {
382                 opendir(DH,$f)
383                         or die "Failed to opendir $f: $!";
384
385                 push @files, grep { -f $_ } map { +$f . "/" . $_ }
386                                 sort readdir(DH);
387
388         } elsif (-f $f) {
389                 push @files, $f;
390
391         } else {
392                 print STDERR "Skipping $f - not found.\n";
393         }
394 }
395
396 if (@files) {
397         unless ($quiet) {
398                 print $_,"\n" for (@files);
399         }
400 } else {
401         print STDERR "\nNo patch files specified!\n\n";
402         usage();
403 }
404
405 # Variables we set as part of the loop over files
406 our ($message_id, %mail, $subject, $reply_to, $references, $message);
407
408 sub extract_valid_address {
409         my $address = shift;
410         my $local_part_regexp = '[^<>"\s@]+';
411         my $domain_regexp = '[^.<>"\s@]+(?:\.[^.<>"\s@]+)+';
412
413         # check for a local address:
414         return $address if ($address =~ /^($local_part_regexp)$/);
415
416         $address =~ s/^\s*<(.*)>\s*$/$1/;
417         if ($have_email_valid) {
418                 return scalar Email::Valid->address($address);
419         } else {
420                 # less robust/correct than the monster regexp in Email::Valid,
421                 # but still does a 99% job, and one less dependency
422                 $address =~ /($local_part_regexp\@$domain_regexp)/;
423                 return $1;
424         }
425 }
426
427 # Usually don't need to change anything below here.
428
429 # we make a "fake" message id by taking the current number
430 # of seconds since the beginning of Unix time and tacking on
431 # a random number to the end, in case we are called quicker than
432 # 1 second since the last time we were called.
433
434 # We'll setup a template for the message id, using the "from" address:
435
436 sub make_message_id
437 {
438         my $date = time;
439         my $pseudo_rand = int (rand(4200));
440         my $du_part;
441         for ($sender, $repocommitter, $repoauthor) {
442                 $du_part = extract_valid_address(sanitize_address($_));
443                 last if (defined $du_part and $du_part ne '');
444         }
445         if (not defined $du_part or $du_part eq '') {
446                 use Sys::Hostname qw();
447                 $du_part = 'user@' . Sys::Hostname::hostname();
448         }
449         my $message_id_template = "<%s-git-send-email-$du_part>";
450         $message_id = sprintf $message_id_template, "$date$pseudo_rand";
451         #print "new message id = $message_id\n"; # Was useful for debugging
452 }
453
454
455
456 $time = time - scalar $#files;
457
458 sub unquote_rfc2047 {
459         local ($_) = @_;
460         if (s/=\?utf-8\?q\?(.*)\?=/$1/g) {
461                 s/_/ /g;
462                 s/=([0-9A-F]{2})/chr(hex($1))/eg;
463         }
464         return "$_";
465 }
466
467 # use the simplest quoting being able to handle the recipient
468 sub sanitize_address
469 {
470         my ($recipient) = @_;
471         my ($recipient_name, $recipient_addr) = ($recipient =~ /^(.*?)\s*(<.*)/);
472
473         if (not $recipient_name) {
474                 return "$recipient";
475         }
476
477         # if recipient_name is already quoted, do nothing
478         if ($recipient_name =~ /^(".*"|=\?utf-8\?q\?.*\?=)$/) {
479                 return $recipient;
480         }
481
482         # rfc2047 is needed if a non-ascii char is included
483         if ($recipient_name =~ /[^[:ascii:]]/) {
484                 $recipient_name =~ s/([^-a-zA-Z0-9!*+\/])/sprintf("=%02X", ord($1))/eg;
485                 $recipient_name =~ s/(.*)/=\?utf-8\?q\?$1\?=/;
486         }
487
488         # double quotes are needed if specials or CTLs are included
489         elsif ($recipient_name =~ /[][()<>@,;:\\".\000-\037\177]/) {
490                 $recipient_name =~ s/(["\\\r])/\\$1/;
491                 $recipient_name = "\"$recipient_name\"";
492         }
493
494         return "$recipient_name $recipient_addr";
495
496 }
497
498 sub send_message
499 {
500         my @recipients = unique_email_list(@to);
501         @cc = (map { sanitize_address($_) } @cc);
502         my $to = join (",\n\t", @recipients);
503         @recipients = unique_email_list(@recipients,@cc,@bcclist);
504         @recipients = (map { extract_valid_address($_) } @recipients);
505         my $date = format_2822_time($time++);
506         my $gitversion = '@@GIT_VERSION@@';
507         if ($gitversion =~ m/..GIT_VERSION../) {
508             $gitversion = Git::version();
509         }
510
511         my $cc = join(", ", unique_email_list(@cc));
512         my $ccline = "";
513         if ($cc ne '') {
514                 $ccline = "\nCc: $cc";
515         }
516         my $sanitized_sender = sanitize_address($sender);
517         make_message_id();
518
519         my $header = "From: $sanitized_sender
520 To: $to${ccline}
521 Subject: $subject
522 Date: $date
523 Message-Id: $message_id
524 X-Mailer: git-send-email $gitversion
525 ";
526         if ($thread && $reply_to) {
527
528                 $header .= "In-Reply-To: $reply_to\n";
529                 $header .= "References: $references\n";
530         }
531         if (@xh) {
532                 $header .= join("\n", @xh) . "\n";
533         }
534
535         my @sendmail_parameters = ('-i', @recipients);
536         my $raw_from = $sanitized_sender;
537         $raw_from = $envelope_sender if (defined $envelope_sender);
538         $raw_from = extract_valid_address($raw_from);
539         unshift (@sendmail_parameters,
540                         '-f', $raw_from) if(defined $envelope_sender);
541
542         if ($dry_run) {
543                 # We don't want to send the email.
544         } elsif ($smtp_server =~ m#^/#) {
545                 my $pid = open my $sm, '|-';
546                 defined $pid or die $!;
547                 if (!$pid) {
548                         exec($smtp_server, @sendmail_parameters) or die $!;
549                 }
550                 print $sm "$header\n$message";
551                 close $sm or die $?;
552         } else {
553                 require Net::SMTP;
554                 $smtp ||= Net::SMTP->new( $smtp_server );
555                 $smtp->mail( $raw_from ) or die $smtp->message;
556                 $smtp->to( @recipients ) or die $smtp->message;
557                 $smtp->data or die $smtp->message;
558                 $smtp->datasend("$header\n$message") or die $smtp->message;
559                 $smtp->dataend() or die $smtp->message;
560                 $smtp->ok or die "Failed to send $subject\n".$smtp->message;
561         }
562         if ($quiet) {
563                 printf (($dry_run ? "Dry-" : "")."Sent %s\n", $subject);
564         } else {
565                 print (($dry_run ? "Dry-" : "")."OK. Log says:\nDate: $date\n");
566                 if ($smtp_server !~ m#^/#) {
567                         print "Server: $smtp_server\n";
568                         print "MAIL FROM:<$raw_from>\n";
569                         print "RCPT TO:".join(',',(map { "<$_>" } @recipients))."\n";
570                 } else {
571                         print "Sendmail: $smtp_server ".join(' ',@sendmail_parameters)."\n";
572                 }
573                 print "From: $sanitized_sender\nSubject: $subject\nCc: $cc\nTo: $to\n\n";
574                 if ($smtp) {
575                         print "Result: ", $smtp->code, ' ',
576                                 ($smtp->message =~ /\n([^\n]+\n)$/s), "\n";
577                 } else {
578                         print "Result: OK\n";
579                 }
580         }
581 }
582
583 $reply_to = $initial_reply_to;
584 $references = $initial_reply_to || '';
585 $subject = $initial_subject;
586
587 foreach my $t (@files) {
588         open(F,"<",$t) or die "can't open file $t";
589
590         my $author = undef;
591         @cc = @initial_cc;
592         @xh = ();
593         my $input_format = undef;
594         my $header_done = 0;
595         $message = "";
596         while(<F>) {
597                 if (!$header_done) {
598                         if (/^From /) {
599                                 $input_format = 'mbox';
600                                 next;
601                         }
602                         chomp;
603                         if (!defined $input_format && /^[-A-Za-z]+:\s/) {
604                                 $input_format = 'mbox';
605                         }
606
607                         if (defined $input_format && $input_format eq 'mbox') {
608                                 if (/^Subject:\s+(.*)$/) {
609                                         $subject = $1;
610
611                                 } elsif (/^(Cc|From):\s+(.*)$/) {
612                                         if (unquote_rfc2047($2) eq $sender) {
613                                                 next if ($suppress_from);
614                                         }
615                                         elsif ($1 eq 'From') {
616                                                 $author = unquote_rfc2047($2);
617                                         }
618                                         printf("(mbox) Adding cc: %s from line '%s'\n",
619                                                 $2, $_) unless $quiet;
620                                         push @cc, $2;
621                                 }
622                                 elsif (!/^Date:\s/ && /^[-A-Za-z]+:\s+\S/) {
623                                         push @xh, $_;
624                                 }
625
626                         } else {
627                                 # In the traditional
628                                 # "send lots of email" format,
629                                 # line 1 = cc
630                                 # line 2 = subject
631                                 # So let's support that, too.
632                                 $input_format = 'lots';
633                                 if (@cc == 0) {
634                                         printf("(non-mbox) Adding cc: %s from line '%s'\n",
635                                                 $_, $_) unless $quiet;
636
637                                         push @cc, $_;
638
639                                 } elsif (!defined $subject) {
640                                         $subject = $_;
641                                 }
642                         }
643
644                         # A whitespace line will terminate the headers
645                         if (m/^\s*$/) {
646                                 $header_done = 1;
647                         }
648                 } else {
649                         $message .=  $_;
650                         if (/^(Signed-off-by|Cc): (.*)$/i && $signed_off_cc) {
651                                 my $c = $2;
652                                 chomp $c;
653                                 push @cc, $c;
654                                 printf("(sob) Adding cc: %s from line '%s'\n",
655                                         $c, $_) unless $quiet;
656                         }
657                 }
658         }
659         close F;
660
661         if ($cc_cmd ne "") {
662                 open(F, "$cc_cmd $t |")
663                         or die "(cc-cmd) Could not execute '$cc_cmd'";
664                 while(<F>) {
665                         my $c = $_;
666                         $c =~ s/^\s*//g;
667                         $c =~ s/\n$//g;
668                         push @cc, $c;
669                         printf("(cc-cmd) Adding cc: %s from: '%s'\n",
670                                 $c, $cc_cmd) unless $quiet;
671                 }
672                 close F
673                         or die "(cc-cmd) failed to close pipe to '$cc_cmd'";
674         }
675
676         if (defined $author) {
677                 $message = "From: $author\n\n$message";
678         }
679
680         send_message();
681
682         # set up for the next message
683         if ($chain_reply_to || !defined $reply_to || length($reply_to) == 0) {
684                 $reply_to = $message_id;
685                 if (length $references > 0) {
686                         $references .= "\n $message_id";
687                 } else {
688                         $references = "$message_id";
689                 }
690         }
691 }
692
693 if ($compose) {
694         cleanup_compose_files();
695 }
696
697 sub cleanup_compose_files() {
698         unlink($compose_filename, $compose_filename . ".final");
699
700 }
701
702 $smtp->quit if $smtp;
703
704 sub unique_email_list(@) {
705         my %seen;
706         my @emails;
707
708         foreach my $entry (@_) {
709                 if (my $clean = extract_valid_address($entry)) {
710                         $seen{$clean} ||= 0;
711                         next if $seen{$clean}++;
712                         push @emails, $entry;
713                 } else {
714                         print STDERR "W: unable to extract a valid address",
715                                         " from: $entry\n";
716                 }
717         }
718         return @emails;
719 }