#!/usr/bin/perl use warnings; use strict; use File::Slurp; use File::Temp qw(tempfile); my ($fh, $filename) = tempfile(CLEANUP => 1); @ARGV = ('-') if not @ARGV; my $i = 0; foreach (@ARGV) { my @lines = read_file $_; s/^From\b/>From/ foreach @lines; print $fh "From x$i\n", "To: test$i\@test.com\n", "Subject: test$i\n\n", @lines, "\n" or die; ++$i; } close $fh or die; # Assume File::Temp generated name has no nasty chars... open SA, "spamassassin -L --mbox $filename |" or die "cannot run spamassassin: $!"; my $current; $i = 0; sub do_current { if ($current) { my $x = $i - 1; my @l = @$current; die if @l < 3; die if (shift @l) ne "To: test$x\@test.com"; die if (shift @l) ne "Subject: test$x"; my $result; while (@l) { local $_ = shift @l; last if $_ eq ''; if (/^X-Spam-Status: /) { chomp; s/^X-Spam-Status: // or die; $result = $_; while (@l and $l[0] =~ /^\s/) { my $l = shift @l; $l =~ s/^\s+/ / or die; chomp $l; $result .= $l; } last; } } die 'did not find X-Spam-Status in headers' if not defined $result; print "$ARGV[$x]:$result\n"; } $current = []; } while () { chomp; if (/^From x(\d+)$/ and $1 == $i) { do_current; ++$i; } else { die "saw bit of message without 'From ' line first" if not $current; push @$current, $_; } } close SA or die "error closing pipe from spamassassin: $!"; do_current;