Mozilla/CVSROOT/sendnotification.pl
justdave%bugzilla.org 4a6e40e949 Fix sendnotification.pl to know the hostname of the mail server
git-svn-id: svn://10.0.0.236/trunk@201169 18797224-902f-48f8-a5cc-f745e15eee43
2006-06-29 00:00:43 +00:00

79 lines
1.7 KiB
Perl
Executable File

#!/usr/bin/perl
use Socket;
sub get_response_code {
my ($expecting) = @_;
# if ($flag_debug) {
# print STDERR "SMTP: Waiting for code $expecting\n";
# }
while (1) {
my $line = <S>;
# if ($flag_debug) {
# print STDERR "SMTP: $line";
# }
if ($line =~ /^[0-9]*-/) {
next;
}
if ($line =~ /(^[0-9]*) /) {
my $code = $1;
if ($code == $expecting) {
# if ($flag_debug) {
# print STDERR "SMTP: got it.\n";
# }
return;
}
die "Bad response from SMTP -- $line";
}
}
}
my @mailto;
my $i;
foreach $i (@ARGV) {
# Deal with our "%" encoding of email addresses.
if ($i !~ /\@/) {
$i =~ s/%/\@/;
}
push(@mailto, $i);
}
chop(my $hostname = `/bin/hostname`);
my ($remote,$port, $iaddr, $paddr, $proto, $line);
$remote = "localhost";
$port = 25;
if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
die "No port" unless $port;
$iaddr = inet_aton($remote) || die "no host: $remote";
$paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
socket(S, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
connect(S, $paddr) || die "connect: $!";
select(S); $| = 1; select(STDOUT);
get_response_code(220);
print S "EHLO $hostname\n";
get_response_code(250);
print S "MAIL FROM: cvs-notify-daemon\@$hostname\n";
get_response_code(250);
foreach $i (@mailto) {
print S "RCPT TO: $i\n";
get_response_code(250);
}
print S "DATA\n";
get_response_code(354);
# Get one line starting with "354 ".
print S "Subject: CVS notification\n";
print S "To: " . join(',', @mailto) . "\n";
print S "\n";
while (<STDIN>) {
print S $_;
}
print S ".\n";
get_response_code(250);
print S "QUIT\n";
close(S);