summaryrefslogtreecommitdiff
path: root/parse-mail
blob: 4c64f5705cc8b9d2ddd9910b053854cb61d25ab2 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/perl -wT

use strict;
use English;
use File::Basename;

my $HOME = '/home/commit/';
my $MAX_LINES = 4;
my $ENVELOPE_FROM = 'nobody@commit.noreply.org';
my $HEADER_FROM = 'nobody@commit.noreply.org';
my $BOT_ADDRESS = 'commit@commit.noreply.org';
my $SENDMAIL = '/usr/sbin/sendmail';
my $UNPARSEABLE = $HOME.'/Maildir.unparseable';

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

die ("Usage: $PROGRAM_NAME <project> <mailfile>\n") unless (scalar @ARGV == 2);
my $project = $ARGV[0];

umask 077;

my $TMPDIR = "$HOME/cvs-tmp/";
-d $TMPDIR or
	mkdir $TMPDIR or
		die ("Cannot mkdir $TMPDIR: $0\n");

$project =~ m/^([a-zA-Z-]+(\.[a-zA-Z-]+)*)+$/;
$project = $1;
die ("Project is not a nice name.\n") unless defined $project;

sub whine_and_store_away($) {
	my ($complaint) = @_;

	$ARGV[1] =~ m#(Maildir.[a-z]+/new/[0-9]+\.[0-9_]+\.[a-z]+)#;
	my $clean_filename = $1;
	die ("Unable to clean filename '$ARGV[1]' for taint mode.") unless defined $clean_filename;

	my $newname = $UNPARSEABLE.'/new/'.basename($clean_filename);
	link ($clean_filename, $newname) or die ("Cannot link $clean_filename to $newname: $!\n");
	unlink ($clean_filename) or die ("Cannot unlink $clean_filename: $!\n");
	print $complaint;
	exit (1);
};

my $from;
my $message;
my $in_msg;
my $found_update_of;
my $tag;
my $cvs_mailcommit_directory;

open (MAIL, $ARGV[1]) || die ("Cannot open $ARGV[1]: $!\n");
my @mail = <MAIL>;
close(MAIL);

my $line;
while (defined($line = shift @mail)) {
	if ($line =~ m/^From:.*CVS User ([a-zA-Z0-9-.]*)/ && !defined $from) {
		$from = $1;
	} elsif ($line =~ m/^From:.*<(\S*?)@/ && !defined $from) {
		$from = $1;
	} elsif ($line =~ m/^From:\s*(\S*?)@/ && !defined $from) {
		$from = $1;
	#          Subject: [oftc-cvs-commits] CVS oftc-hybrid
	} elsif ($line =~ m/^Subject: (?:\[\S*\] )?CVS\s*(.*)/) {
		$cvs_mailcommit_directory = $1;
	} elsif ($line =~ m/^Update of /) {
		$found_update_of = 1;
	} elsif ($line =~ m/^\s*Tag:\s*(.*)$/) {
		$tag = $1;
	} elsif ($line =~ m/^Log Message:/) {
		$in_msg = 1;
		last;
	}
};
die ("$PROGRAM_NAME - $project: No author found.\n") unless defined $from;
whine_and_store_away("$PROGRAM_NAME - $project: 'Update of ' not found.\n") unless defined $found_update_of;

my $cur_line = 0;
my $last_line = '';
if ($in_msg) {
	while (defined($line = shift @mail)) {
		if ($line =~ m/^Index: / ||
		    $line =~ m/^--- NEW FILE/ ||
		    $line =~ m/^--/ ||
		    $line =~ m/^\*\*\*\*\*\*\*\*/
		    ) {
			last;
		}
		next if ($line =~ m/^\s*$/);
		if ($cur_line == $MAX_LINES) {
			$last_line = $_;
		} elsif ($cur_line > $MAX_LINES) {
			$last_line = "...\n";
			last;
		} else {
			$message .= $_;
		};
		$cur_line++;
	}
}
$message .= $last_line;

die ("No message found.\n") unless defined $message;
my $notice = "commit by $from: $message";

my $old_notice = '';
my $filename = $TMPDIR.'/'.$project;
if (-e $filename) {
	open(F, $filename) or die ("cannot open $filename: $!\n");
	local $/ = undef;
	$old_notice = <F>;
	close(F);
}
if ($notice eq $old_notice) {
	exit(0);
}
open(F, ">".$filename) or die ("cannot open $filename for writing: $!\n");
print F $notice;
close(F);

#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 (defined $tag ? "[$tag] " : '') . (defined $cvs_mailcommit_directory && length ($cvs_mailcommit_directory) ? "[$cvs_mailcommit_directory] " : ''). $notice;
close(MAIL);