Server IP : 85.214.239.14 / Your IP : 18.188.102.117 Web Server : Apache/2.4.62 (Debian) System : Linux h2886529.stratoserver.net 4.9.0 #1 SMP Tue Jan 9 19:45:01 MSK 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.18 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, MySQL : OFF | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : OFF Directory : /proc/3/cwd/usr/share/doc/libmime-tools-perl/examples/ |
Upload File : |
#!/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;