Generate UPI number and copy over files.

This commit is contained in:
Bradley M. Kuhn 2023-06-03 05:34:57 -07:00
parent 4627b4e24d
commit 111f569b9c

View file

@ -86,7 +86,7 @@ if ($GROUP_NAMES_BY_DIR{$GROUP} eq 'PRIVILEGE') {
my @CSV_OUTPUT_ROWS;
sub ProcessMailDir($$$) {
my($rfp, $inputMailDir, $destOutDir) = @_;
my($rfp, $inputMailDir, $outputDir) = @_;
my @msgDirs = (catfile($inputMailDir, 'new'), catfile($inputMailDir, 'cur'));
foreach my $dir (@msgDirs) {
@ -99,8 +99,7 @@ sub ProcessMailDir($$$) {
next if -d $file; # skip directories
my $msgFile = catfile($dir, $file);
open(my $msgFH, "<", $msgFile);
my $upiFull = sprintf("UPI-SFC-%07d", ++$upiCurrentNum);
print " $msgFile\n";
my $upiFull = sprintf("UPI-SFC-%07d", $upiCurrentNum++);
my $header = new Mail::Header($msgFH);
my $fields = $header->header_hashref;
my %parsed = (FromName => '', ToName => '', FromAddr => "", ToAddr => "", CcName => '', CcAddr => '', 'Subject' => '',
@ -132,7 +131,6 @@ sub ProcessMailDir($$$) {
$subjectDashes =~ s/[^a-zA-Z0-9]/_/g; $subjectDashes =~ s/_+/_/g; $subjectDashes =~ s/_+$//g; $subjectDashes =~ s/^_+//g;
my $fileName = $upiFull . '-' . $GROUP_NAMES_BY_DIR{$GROUP} . '-' .
UnixDate($parsed{Date}, '%Y%m%d-%H%M-') . $subjectDashes . '.eml';
print "$fileName\n";
die "$fileName has no subject" if not defined $parsed{Subject};
if ($GROUP_NAMES_BY_DIR{$GROUP} eq 'PRIVILEGE') {
push(@CSV_OUTPUT_ROWS, [ $upiFull, $parsed{FromName}, $parsed{FromAddr}, $parsed{Subject},
@ -141,6 +139,10 @@ sub ProcessMailDir($$$) {
} else {
push(@CSV_OUTPUT_ROWS, [ $upiFull, $fileName, uc($rfp) ]);
}
my $copiedFile = catfile($outputDir, $fileName);
copy($msgFile, $copiedFile);
system('/usr/bin/unix2dos', '-q', $copiedFile);
die "unable to copy $msgFile to $copiedFile" unless -f $copiedFile;
}
closedir $mailDirFH;
}
@ -160,9 +162,13 @@ while (my $rfp = readdir $topDH) {
next if $typeName =~ /^\s*\.\.?\s*$/;
my $typeDirName = catfile($INPUT_TOPLEVEL_DIR, $rfp, $bucketName, $typeName);
die "regular file found where we expected a type in $typeName" unless -d $typeDirName;
print " $typeDirName\n";
my $nativeOutputDirOneUp = File::Spec->rel2abs(catfile($OUTPUT_TOPLEVEL_DIR, 'native', $rfp, $bucketName));
my $numberedOutputDir = File::Spec->rel2abs(catfile($OUTPUT_TOPLEVEL_DIR, 'numbered', $rfp, $bucketName, $typeName));
make_path($nativeOutputDirOneUp, { verbose => 1, mode => 0755 });
make_path($numberedOutputDir, { verbose => 1, mode => 0755 });
if ($typeName =~ /email/i) {
ProcessMailDir($rfp, $typeDirName, "");
ProcessMailDir($rfp, $typeDirName, $numberedOutputDir);
move($typeDirName, $nativeOutputDirOneUp);
}
}
closedir $bucketDH;
@ -176,6 +182,7 @@ open my $csvFH, ">:encoding(utf8)", $csvLogFile;
$csvOutFormat->say($csvFH, \@headerFields);
$csvOutFormat->say($csvFH, $_) for @CSV_OUTPUT_ROWS; close $csvFH;
print STDERR "$GROUP ($GROUP_NAMES_BY_DIR{$GROUP}) starts at $upiStart and ends at $upiCurrentNum\n";
open($upiFH, ">", $upiNumberFile);
print $upiFH ++$upiCurrentNum, "\n";
close $upiFH;