Mozilla/CVSROOT/cvsmailfilter.pl
2001-04-25 03:35:37 +00:00

164 lines
3.7 KiB
Perl
Executable File

#! /tools/ns/bin/perl5.004
# -*- Mode: perl; indent-tabs-mode: nil -*-
# Arguments:
#
# -a Only send checkin messages which contain added files. All other checkin
# messages will be ignored.
# -u <url> Base URL for the Bonsai directory; "/cvsview2.cgi" will get
# appended to this with appropriate args.
# -h <hostname> Host whose SMTP server we will contact to send mail.
# -s <string> String specifying dir and filenames. As generated by "%s"
# in a CVSROOT/loginfo file
# -f <file> A regexp. If present, then only checkins to files whose
# name (without the directory) matches the regexp will generate mail.
#
# The remaining args are email addresses of people who should get notified.
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 $debug = 0;
my $addsonly = 0;
my $mailhost = "127.0.0.1";
my $urlbase = "";
my $cvsargs = "";
my $cvsroot = "";
my @mailto;
my $fileregexp = "";
while (@ARGV) {
my $arg = shift @ARGV;
if ($arg eq '-d') {
$debug = 1;
print STDERR "Debug turned on...\n";
} elsif ($arg eq '-r') {
$cvsroot = shift @ARGV;
} elsif ($arg eq '-h') {
$mailhost = shift @ARGV;
} elsif ($arg eq '-u') {
$urlbase = shift @ARGV;
} elsif ($arg eq '-s') {
$cvsargs = shift @ARGV;
} elsif ($arg eq '-f') {
$fileregexp = shift @ARGV;
} elsif ($arg eq '-a') {
$addsonly = 1;
} else {
push(@mailto, $arg);
}
}
my $url = "";
if ($urlbase ne "" && $cvsargs ne "") {
my @list = split(/ /, $cvsargs);
my $dir = shift @list;
if ($fileregexp ne "") {
if (grep(m/$fileregexp/, @list) <= 0) {
exit;
}
}
$url = $urlbase . "/cvsview2.cgi?command=DIRECTORY&subdir=$dir&files=" .
join('%2B', @list);
}
my $message = "";
my $filesadded = 0;
while (<>) {
my $line = $_;
if ($line =~ m@^Revision/Branch: (.*)$@) {
if ($url ne "") {
$url .= "&branch=$1";
}
}
# if we see that files have been added on this checkin, remember that fact
#
if ($line =~ m@^Added Files:@) {
$filesadded = 1;
}
$message .= $line;
}
# bail out if this is an adds-only run and no files have been added
#
if ($addsonly == 1 && $filesadded == 0 ) {
exit 0;
}
if ($url ne "") {
if ($cvsroot ne "") {
$url .= "&root=$cvsroot";
}
$message = "Diffs: $url\n\n" . $message;
}
chop(my $hostname = `/bin/hostname`);
my ($remote,$port, $iaddr, $paddr, $proto, $line);
$remote = $mailhost;
$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: cvsmailfilter\@$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);
print S "Subject: $cvsargs\n";
print S "\n";
print S $message . "\n";
print S ".\n";
get_response_code(250);
print S "QUIT\n";
close(S);