#!/usr/bin/perl -wT # Copyright (c) 2005, 2006, 2007, 2008 Peter Palfrader # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 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 \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 = ; 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 = ; 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);