#!/usr/bin/perl
#
# spf_in.pl by nao-pon (hypweb.net)
# License: GPL
#

use strict;

my $file = $ARGV[0];
my $temp = $ARGV[1];

open(FILE, $file) || exit(0);
my @tmp = <FILE>;
close(FILE);

if (!@tmp) {exit(0);}

my @dat = ();
my $check = 0;
my $i = 0;
my $mid;
my $spf_file;
my $spf;
foreach(@tmp){
	$i++;
	chomp;
	if ($i eq 3) {
		$mid = $_;
	}
	if (!$check && /^<<MAIL\-DATA>>$/) {
		$check = 1;
		push(@dat, "$_\n");
		$spf_file = "${temp}/${mid}.SPF";
		open(FILE, $spf_file) || exit(0);
		$spf = <FILE>;
		close(FILE);
		unlink($spf_file);
		chomp($spf);
		push(@dat, "$spf\n");
		next;
	}
	push(@dat, "$_\n");
}

open(FILE,">$file");
	flock(FILE,2);
	print FILE @dat;
close(FILE);

exit(7);
