/usr/share/doc/libmime-tools-perl/examples/mimeref is in libmime-tools-perl 5.509-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 133 134 135 136 137 138 139 140 141 | #!/usr/bin/perl -w
=head1 NAME
mimeref - create a .ref file for a message
=head1 SYNOPSIS
Usage:
mimeref [-options] *.msg
Options:
-d DIR Output directory for parser (default is ./testout/mimeref)
-w Write the .ref file
=head1 DESCRIPTION
Parse a message file, and spit out a .ref file.
The .ref files are not really useful; they're just used by
the t/Ref.t test.
=head1 AUTHOR
Eryq, eryq@zeegee.com
=cut
use strict;
use lib "./lib";
use MIME::Parser;
use File::Path;
use Getopt::Std;
use Data::Dumper;
### Get options:
my %opts;
getopts("d:wv", \%opts) || die "usage error ($!)\n";
my (@msgs) = @ARGV; @msgs or die "missing message\n";
### Get path to output space:
my $output_base = $opts{'d'} || "./testout/mimeref";
(-d $output_base) or mkdir($output_base, 0777) or die "mkdir $output_base: $!\n";
MIME::Tools->debugging($opts{'v'});
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 1;
$Data::Dumper::Useqq = 1;
foreach my $msg (@msgs) {
do_msg($msg);
}
exit 0;
#------------------------------
sub do_msg {
my $msg = shift;
### Create a parser:
my $parser = new MIME::Parser;
$parser->output_under($output_base);
$parser->extract_nested_messages(1);
### Parse:
my $ent = eval { $parser->parse_open($msg) || die "parse failed: $!\n"; };
if (!$ent) {
rmtree $parser->output_dir;
die $@;
}
### Decompose:
my $ref = {};
$ref->{Parser} = {
Name => "anonymous",
Message => $msg,
OutputToCore => $parser->output_to_core,
ExtractNested => $parser->extract_nested_messages,
};
summarize($ref, $ent);
$ent->dump_skeleton() if $opts{'v'};
if ($opts{'w'}) {
my $refpath = $msg;
$refpath =~ s/\.msg$//; $refpath .= ".ref";
open OUT, ">$refpath" or die "$refpath: $!\n";
print OUT Dumper($ref);
close OUT;
print STDERR "Wrote: $refpath\n";
}
else {
print Dumper($ref);
}
rmtree $parser->output_dir;
}
#------------------------------
sub set {
my ($hash, $param, $val) = @_;
if (defined($val)) {
$hash->{$param} = $val;
}
}
sub c {
my $x = shift;
$x =~ s/\r?\n$// if defined($x);
$x;
}
#------------------------------
sub summarize {
my ($ref, $ent, $name) = @_;
$name ||= "Msg";
my $head = $ent->head;
$head->unfold;
my $body = $ent->bodyhandle;
my $sum = {};
set($sum, From => c($head->get("From", 0)));
set($sum, To => c($head->get("To", 0)));
set($sum, Subject => c($head->get("Subject", 0)));
set($sum, Type => $head->mime_type);
set($sum, Encoding=> $head->mime_encoding);
set($sum, Charset => $head->mime_attr("content-type.charset"));
set($sum, Boundary => $head->multipart_boundary);
set($sum, Disposition => $head->mime_attr("content-disposition"));
set($sum, Filename => $head->recommended_filename);
if ($body and $body->path) {
set($sum, Size => (-s $body->path));
}
$ref->{$name} = $sum;
my $root = (($name eq 'Msg') ? 'Part' : $name);
for (1 .. $ent->parts) {
summarize($ref, $ent->parts($_ - 1), "${root}_$_");
}
}
1;
|