summaryrefslogtreecommitdiff
path: root/rlog-parse
blob: a94cbeb6b7490780ee8c9d7af390759ee0e629ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/perl -wT

use Rcs;
use strict;
use English;
use Data::Dumper;
use File::Basename;

$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

my $STATEFILEPREFIX='/var/spool/rlog-parse/state-';
my $ENVELOPE_FROM = 'XXXXXXXcommit.noreply.org';
my $HEADER_FROM = 'XXXXXXXcommit.noreply.org';
my $BOT_ADDRESS = 'XXXXXXXcommit.noreply.org';
my $SENDMAIL = '/usr/sbin/sendmail';
#my $PROJECT = 'oftc';
#my $PROJECT = 'weasel';

sub clean_filename($) {
	my ($file) = @_;

	my $b = basename($file);
	$b =~ m/^([a-z.,-]+)$/;
	my $f = $1;
	die ("Evil filename $file") unless $f;

	return $f
};
sub make_statefile($) {
	my ($file) = @_;

	return $STATEFILEPREFIX.clean_filename($file);
};

sub read_state($) {
	my ($file) = @_;

	my $sf = make_statefile($file);
	my $res = open(F, "< $sf");
	if ($res) {
		my $rev = <F>;
		close(F);
		chomp $rev;
		return $rev;
	} else {
		return undef;
	};
};

sub update_state($$) {
	my ($file, $rev) = @_;
	my $sf = make_statefile($file);
	open(F, "> $sf") or die ("Cannot write to $sf: $!\n");
	print F $rev,"\n";
	close(F);
};

sub handle_rev($$$$) {
	my ($file, $rcs, $comments, $rev) = @_;
	update_state($file, $rev);

	my $author = $rcs->author($rev);
	my $comment = $comments->{$rev};
	my $base = basename($file);
	$base =~ s/,v//;

	#open(MAIL, "|cat") or
	open(MAIL, "|$SENDMAIL -t -oi -f $ENVELOPE_FROM") or
		die ("Cannot exec sendmail: $!\n");
	print MAIL "From: $HEADER_FROM\n";
	print MAIL "To: $BOT_ADDRESS\n";
	print MAIL "Subject: Announce $PROJECT\n";
	print MAIL "Precedence: junk\n";
	print MAIL "\n";
	print MAIL "$author committed r$rev to $base: $comment\n";
	close(MAIL);
};


die ("Usage: $PROGRAM_NAME <rcsfile>\n") unless (scalar @ARGV == 1);
my $file = $ARGV[0];

my $rcs = Rcs->new($file);
my %comments = $rcs->comments;
my @revisions = reverse $rcs->revisions;

my $old_rev = read_state($file);
if (defined $old_rev) {
	my $rev;
	while (defined($rev = shift @revisions)) {
		last if $rev eq $old_rev;
	};
	die ("Apparently did not find $old_rev in revisions\n") unless (defined $rev);
} else {
	print STDERR "First ivocation.  Not sending mails but creating state file\n";
	update_state($file, $revisions[-1]);
	exit (0);
};

for my $r (@revisions) {
	handle_rev($file, $rcs, \%comments, $r);
};