Filer.pm.html   [plain text]


<HTML>
<HEAD>
  <TITLE>MIME::Parser::Filer</TITLE>
</HEAD>
<BODY 
       bgcolor="#FFFFFF" link="#CC3366" vlink="#993366" alink="#FF6666">
<FONT FACE="sans-serif" SIZE=-1><A HREF="http://www.zeegee.com" TARGET="_top"><IMG SRC="icons/zeegee.gif" ALT="ZeeGee Software" ALIGN="RIGHT" BORDER="0"></A><A NAME="__TOP__"><H1>MIME::Parser::Filer</H1>
</A>
<P><B>This module is <FONT COLOR="#990000">BETA</FONT> code, which means that the interfaces are fairly stable BUT it has not been out in the community long enough to guarantee much testing. Use with caution! Please report any errors back to <A HREF="mailto:eryq@zeegee.com">eryq@zeegee.com</A> as soon as you can.</B><UL>
<LI> <A HREF="#NAME">NAME</A>
<LI> <A HREF="#SYNOPSIS">SYNOPSIS</A>
<UL>
<LI> <A HREF="#Public_interface">Public interface</A>
<LI> <A HREF="#Semi-public_interface">Semi-public interface</A>
</UL>
<LI> <A HREF="#DESCRIPTION">DESCRIPTION</A>
<UL>
<LI> <A HREF="#How_this_class_is_used_when_parsing">How this class is used when parsing</A>
<LI> <A HREF="#Writing_your_own_subclasses">Writing your own subclasses</A>
</UL>
<LI> <A HREF="#PUBLIC_INTERFACE">PUBLIC INTERFACE</A>
<UL>
<LI> <A HREF="#MIME_Parser_Filer">MIME::Parser::Filer</A>
<LI> <A HREF="#MIME_Parser_FileInto">MIME::Parser::FileInto</A>
<LI> <A HREF="#MIME_Parser_FileUnder">MIME::Parser::FileUnder</A>
</UL>
<LI> <A HREF="#AUTHOR">AUTHOR</A>
<LI> <A HREF="#VERSION">VERSION</A>
</UL>
</A>

<P><HR>
<A NAME="NAME"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> NAME</H2></A>


<P>MIME::Parser::Filer - manage file-output of the parser



<P><HR>
<A NAME="SYNOPSIS"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> SYNOPSIS</H2></A>


<P>Before reading further, you should see <A HREF="../../MIME/Parser.pm.html">MIME::Parser</A> to make sure that 
you understand where this module fits into the grand scheme of things.
Go on, do it now.  I'll wait.


<P>Ready?  Ok... now read <A HREF="#DESCRIPTION">DESCRIPTION</A> below, and everything else
should make sense.



<P><HR>
<A NAME="Public_interface"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> Public interface</H3></A>

<FONT SIZE=3 FACE="courier"><PRE>
    ### Create a &quot;filer&quot; of the desired class:
    my $filer = MIME::Parser::FileInto-&gt;new($dir);
    my $filer = MIME::Parser::FileUnder-&gt;new($basedir);
    ...
     
    ### Want added security?  Don't let outsiders name your files:
    $filer-&gt;ignore_filename(1); 
     
    ### Prepare for the parsing of a new top-level message:     
    $filer-&gt;init_parse;
     
    ### Return the path where this message's data should be placed:
    $path = $filer-&gt;output_path($head);
</PRE></FONT>


<P><HR>
<A NAME="Semi-public_interface"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> Semi-public interface</H3></A>


<P>These methods might be overriden or ignored in some subclasses, 
so they don't all make sense in all circumstances:

<FONT SIZE=3 FACE="courier"><PRE>
    ### Tweak the mapping from content-type to extension:
    $emap = $filer-&gt;output_extension_map;
    $emap-&gt;{&quot;text/html&quot;} = &quot;.htm&quot;;
</PRE></FONT>


<P><HR>
<A NAME="DESCRIPTION"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> DESCRIPTION</H2></A>



<P><HR>
<A NAME="How_this_class_is_used_when_parsing"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> How this class is used when parsing</H3></A>


<P>When a MIME::Parser decides that it wants to output a file to disk,
it uses its &quot;Filer&quot; object -- an instance of a MIME::Parser::Filer 
subclass -- to determine where to put the file.  


<P>Every parser has a single Filer object, which it uses for all
parsing.  You can get the Filer for a given $parser like this:

<FONT SIZE=3 FACE="courier"><PRE>
    $filer = $parser-&gt;filer;
</PRE></FONT>

<P>At the beginning of each <CODE>parse()</CODE>, the filer's internal state
is reset by the parser: 

<FONT SIZE=3 FACE="courier"><PRE>
    $parser-&gt;filer-&gt;init_parse;
</PRE></FONT>

<P>The parser can then get a path for each entity in the message
by handing that entity's header (a MIME::Head) to the filer 
and having it do the work, like this:

<FONT SIZE=3 FACE="courier"><PRE>
    $new_file = $parser-&gt;filer-&gt;output_path($head);
</PRE></FONT>

<P>Since it's nice to be able to clean up after a parse (especially
a failed parse), the parser tells the filer when it has actually 
used a path:

<FONT SIZE=3 FACE="courier"><PRE>
    $parser-&gt;filer-&gt;purgeable($new_file);
</PRE></FONT>

<P>Then, if you want to clean up the files which were created for a
particular parse (and also any directories that the Filer created),
you would do this:

<FONT SIZE=3 FACE="courier"><PRE>
    $parser-&gt;filer-&gt;purge;
</PRE></FONT>


<P><HR>
<A NAME="Writing_your_own_subclasses"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> Writing your own subclasses</H3></A>


<P>There are two standard &quot;Filer&quot; subclasses (see below): 
<B>MIME::Parser::FileInto</B>, which throws all files from all parses
into the same directory, and <B>MIME::Parser::FileUnder</B> (preferred), which 
creates a subdirectory for each message.  Hopefully, these will be 
sufficient for most uses, but just in case...


<P>The only method you have to override is <A HREF="#item:output_path">output_path()</A>:

<FONT SIZE=3 FACE="courier"><PRE>
    $filer-&gt;output_path($head);
</PRE></FONT>

<P>This method is invoked by MIME::Parser when it wants to put a 
decoded message body in an output file.  The method should return a 
path to the file to create.  Failure is indicated by throwing an 
exception.


<P>The path returned by <CODE>output_path()</CODE> should be &quot;ready for open()&quot;:
any necessary parent directories need to exist at that point.
These directories can be created by the Filer, if course, and they
should be marked as <B>purgeable()</B> if a purge should delete them.


<P>Actually, if your issue is more <I>where</I> the files go than
what they're named, you can use the default <A HREF="#item:output_path">output_path()</A>
method and just override one of its components:

<FONT SIZE=3 FACE="courier"><PRE>
    $dir  = $filer-&gt;output_dir($head);
    $name = $filer-&gt;output_filename($head);
    ...
</PRE></FONT>


<P><HR>
<A NAME="PUBLIC_INTERFACE"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> PUBLIC INTERFACE</H2></A>



<P><HR>
<A NAME="MIME_Parser_Filer"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> MIME::Parser::Filer</H3></A>


<P>This is the abstract superclass of all &quot;filer&quot; objects.



<DL>
<P><DT><B><A NAME="item:new">new INITARGS...</A></B></DT>
<DD>
<I>Class method, constructor.</I>
Create a new outputter for the given parser.
Any subsequent arguments are given to init(), which subclasses should
override for their own use (the default init does nothing).

<P><DT><B><A NAME="item:results">results RESULTS</A></B></DT>
<DD>
<I>Instance method.</I>
Link this filer to a MIME::Parser::Results object which will 
tally the messages.  Notice that we avoid linking it to the 
parser to avoid circular reference!

<P><DT><B><A NAME="item:init_parse">init_parse</A></B></DT>
<DD>
<I>Instance method.</I>
Prepare to start parsing a new message.
Subclasses should always be sure to invoke the inherited method.

<P><DT><B><A NAME="item:evil_filename">evil_filename FILENAME</A></B></DT>
<DD>
<I>Instance method.</I>
Is this an evil filename; i.e., one which should not be used
in generating a disk file name?  It is if any of these are true:

<P><UL><LI> it is empty<LI> it is a string of dots: &quot;.&quot;, &quot;..&quot;, etc.<LI> it contains a known &quot;path&quot; character: '/' '\' ':' '[' ']'<LI> it is too long</UL>
<P>If you just want to change this behavior, you should override 
this method in the subclass of MIME::Parser::Filer that you use.


<P><B>Warning:</B> at the time this method is invoked, the FILENAME has 
already been unmime'd into the local character set.  
If you're using any character set other than ASCII, ISO-8859-*, 
or UTF-8, the interpretation of the &quot;path&quot; characters might be 
very different, and you will probably need to override this method.
See <A HREF="../MIME/WordDecoder.pm.html#item:unmime">unmime</A> for more details.


<P><B>Note:</B> subclasses of MIME::Parser::Filer which override 
output_path() might not consult this method; note, however, that
the built-in subclasses do consult it.


<P><I>Thanks to Andrew Pimlott for finding a real dumb bug in the original
version.  Thanks to Nickolay Saukh for noting that evil is in the 
eye of the beholder.</I>

<P><DT><B><A NAME="item:exorcise_filename">exorcise_filename FILENAME</A></B></DT>
<DD>
<I>Instance method.</I>
If a given filename is evil (see <A HREF="#item:evil_filename">evil_filename</A>) we try to
rescue it by performing some basic operations: shortening it,
removing bad characters, etc., and checking each against
evil_filename().


<P>Returns the exorcised filename (which is guaranteed to not
be evil), or undef if it could not be salvaged.


<P><B>Warning:</B> at the time this method is invoked, the FILENAME has 
already been unmime'd into the local character set.  
If you're using anything character set other than ASCII, ISO-8859-*, 
or UTF-8, the interpretation of the &quot;path&quot; characters might be very 
very different, and you will probably need to override this method.
See <A HREF="../MIME/WordDecoder.pm.html#item:unmime">unmime</A> for more details.

<P><DT><B><A NAME="item:find_unused_path">find_unused_path DIR, FILENAME</A></B></DT>
<DD>
<I>Instance method, subclasses only.</I>
We have decided on an output directory and tentative filename,
but there is a chance that it might already exist.  Keep
adding a numeric suffix &quot;-1&quot;, &quot;-2&quot;, etc. to the filename
until an unused path is found, and then return that path.


<P>The suffix is actually added before the first &quot;.&quot; in the filename
is there is one; for example:

<FONT SIZE=3 FACE="courier"><PRE>
    picture.gif       archive.tar.gz      readme
    picture-1.gif     archive-1.tar.gz    readme-1
    picture-2.gif     archive-2.tar.gz    readme-2
    ...               ...                 ...
    picture-10.gif
    ...
</PRE></FONT>

<P>This can be a costly operation, and risky if you don't want files
renamed, so it is in your best interest to minimize situations
where these kinds of collisions occur.  Unfortunately, if
a multipart message gives all of its parts the same recommended
filename, and you are placing them all in the same directory,
this method might be unavoidable.

<P><DT><B><A NAME="item:ignore_filename">ignore_filename [YESNO]</A></B></DT>
<DD>
<I>Instance method.</I>
Return true if we should always ignore recommended filenames in
messages, choosing instead to always generate our own filenames.  
With argument, sets this value.


<P><B>Note:</B> subclasses of MIME::Parser::Filer which override 
output_path() might not honor this setting; note, however, that
the built-in subclasses honor it.

<P><DT><B><A NAME="item:output_dir">output_dir HEAD</A></B></DT>
<DD>
<I>Instance method.</I>
Return the output directory for the given header.
The default method returns &quot;.&quot;.

<P><DT><B><A NAME="item:output_filename">output_filename HEAD</A></B></DT>
<DD>
<I>Instance method, subclasses only.</I>
A given recommended filename was either not given, or it was judged
to be evil.  Return a fake name, possibly using information in the 
message HEADer.  Note that this is just the filename, not the full path.


<P>Used by <A HREF="#item:output_path">output_path()</A>.
If you're using the default <CODE>output_path()</CODE>, you probably don't 
need to worry about avoiding collisions with existing files; 
we take care of that in <A HREF="#item:find_unused_path">find_unused_path()</A>.

<P><DT><B><A NAME="item:output_prefix">output_prefix [PREFIX]</A></B></DT>
<DD>
<I>Instance method.</I>
Get the short string that all filenames for extracted body-parts 
will begin with (assuming that there is no better &quot;recommended filename&quot;).  
The default is <I><FILE>&quot;msg&quot;</FILE></I>.


<P>If PREFIX <I>is not</I> given, the current output prefix is returned.
If PREFIX <I>is</I> given, the output prefix is set to the new value,
and the previous value is returned.


<P>Used by <A HREF="#item:output_filename">output_filename()</A>.


<P><B>Note:</B> subclasses of MIME::Parser::Filer which override 
output_path() or output_filename() might not honor this setting; 
note, however, that the built-in subclasses honor it.

<P><DT><B><A NAME="item:output_type_ext">output_type_ext</A></B></DT>
<DD>
<I>Instance method.</I>
Return a reference to the hash used by the default 
<A HREF="#item:output_filename">output_filename()</A> for mapping from content-types 
to extensions when there is no default extension to use.

<FONT SIZE=3 FACE="courier"><PRE>
    $emap = $filer-&gt;output_typemap;
    $emap-&gt;{'text/plain'} = '.txt';
    $emap-&gt;{'text/html'}  = '.html';
    $emap-&gt;{'text/*'}     = '.txt';
    $emap-&gt;{'*/*'}        = '.dat';
</PRE></FONT>

<P><B>Note:</B> subclasses of MIME::Parser::Filer which override 
output_path() or output_filename() might not consult this hash; 
note, however, that the built-in subclasses consult it.

<P><DT><B><A NAME="item:output_path">output_path HEAD</A></B></DT>
<DD>
<I>Instance method, subclasses only.</I>
Given a MIME head for a file to be extracted, come up with a good
output pathname for the extracted file.  This is the only method
you need to worry about if you are building a custom filer.


<P>The default implementation does a lot of work; subclass 
implementers <I>really</I> should try to just override its components 
instead of the whole thing.  It works basically as follows:

<FONT SIZE=3 FACE="courier"><PRE>
    $directory = $self-&gt;output_dir($head);   
     
    $filename = $head-&gt;recommended_filename();
    if (!$filename or 
	 $self-&gt;ignore_filename() or 
	 $self-&gt;evil_filename($filename)) {
 	$filename = $self-&gt;output_filename($head);
    }
    
    return $self-&gt;find_unused_path($directory, $filename);
</PRE></FONT>

<P><B>Note:</B> There are many, many, many ways you might want to control
the naming of files, based on your application.  If you don't like 
the behavior of this function, you can easily define your own subclass 
of MIME::Parser::Filer and override it there.


<P><B>Note:</B> Nickolay Saukh pointed out that, given the subjective nature of
what is &quot;evil&quot;, this function really shouldn't <I>warn</I> about an evil
filename, but maybe just issue a <I>debug</I> message.  I considered that, 
but then I thought: if debugging were off, people wouldn't know why 
(or even if) a given filename had been ignored.  In mail robots
that depend on externally-provided filenames, this could cause 
hard-to-diagnose problems.  So, the message is still a warning.


<P><I>Thanks to Laurent Amon for pointing out problems with the original
implementation, and for making some good suggestions.  Thanks also to
Achim Bohnet for pointing out that there should be a hookless, OO way of 
overriding the output path.</I>

<P><DT><B><A NAME="item:purge">purge</A></B></DT>
<DD>
<I>Instance method, final.</I>
Purge all files/directories created by the last parse.
This method simply goes through the purgeable list in reverse order 
(see <A HREF="#item:purgeable">purgeable</A>) and removes all existing files/directories in it.
You should not need to override this method.

<P><DT><B><A NAME="item:purgeable">purgeable [FILE]</A></B></DT>
<DD>
<I>Instance method, final.</I>
Add FILE to the list of &quot;purgeable&quot; files/directories (those which
will be removed if you do a <CODE>purge()</CODE>).
You should not need to override this method.


<P>If FILE is not given, the &quot;purgeable&quot; list is returned.
This may be used for more-sophisticated purging.


<P>As a special case, invoking this method with a FILE that is an
arrayref will replace the purgeable list with a copy of the
array's contents, so [] may be used to clear the list.


<P>Note that the &quot;purgeable&quot; list is cleared when a parser begins a 
new parse; therefore, if you want to use purge() to do cleanup,
you <I>must</I> do so <I>before</I> starting a new parse!

</DL>



<P><HR>
<A NAME="MIME_Parser_FileInto"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> MIME::Parser::FileInto</H3></A>


<P>This concrete subclass of MIME::Parser::Filer supports filing 
into a given directory.



<DL>
<P><DT><B><A NAME="item:init">init DIRECTORY</A></B></DT>
<DD>
<I>Instance method, initiallizer.</I>
Set the directory where all files will go.

</DL>



<P><HR>
<A NAME="MIME_Parser_FileUnder"><H3><A HREF="#__TOP__"><IMG SRC="icons/h2bullet.gif" ALT="Top" BORDER="0"></A> MIME::Parser::FileUnder</H3></A>


<P>This concrete subclass of MIME::Parser::Filer supports filing under 
a given directory, using one subdirectory per message, but with
all message parts in the same directory.



<DL>
<P><DT><B><A NAME="item:init">init BASEDIR, OPTSHASH...</A></B></DT>
<DD>
<I>Instance method, initiallizer.</I>
Set the base directory which will contain the message directories.
If used, then each parse of begins by creating a new subdirectory
of BASEDIR where the actual parts of the message are placed.  
OPTSHASH can contain the following:



<DL>
<P><DT><B><A NAME="item:DirName">DirName</A></B></DT>
<DD>
Explicitly set the name of the subdirectory which is created.
The default is to use the time, process id, and a sequence number,
but you might want a predictable directory.  

<P><DT><B><A NAME="item:Purge">Purge</A></B></DT>
<DD>
Automatically purge the contents of the directory (including all
subdirectories) before each parse.  This is really only needed if
using an explicit DirName, and is provided as a convenience only.
Currently we use the 1-arg form of File::Path::rmtree; you should
familiarize yourself with the caveats therein.

</DL>


<P>The output_dir() will return the path to this message-specific directory 
until the next parse is begun, so you can do this:

<FONT SIZE=3 FACE="courier"><PRE>
    use File::Path;
     
    $parser-&gt;output_under(&quot;/tmp&quot;);
    $ent = eval { $parser-&gt;parse_open($msg); };   ### parse
    if (!$ent) {	 ### parse failed
	rmtree($parser-&gt;output_dir);
	die &quot;parse failed: $@&quot;;
    } 
    else {               ### parse succeeded
	...do stuff...
    }
</PRE></FONT>
</DL>



<P><HR>
<A NAME="AUTHOR"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> AUTHOR</H2></A>


<P>Eryq (<I><FILE><A HREF="mailto:eryq@zeegee.com">eryq@zeegee.com</A></FILE></I>), ZeeGee Software Inc (<I><FILE><A HREF="http://www.zeegee.com">http://www.zeegee.com</A></FILE></I>).


<P>All rights reserved.  This program is free software; you can redistribute 
it and/or modify it under the same terms as Perl itself.



<P><HR>
<A NAME="VERSION"><H2><A HREF="#__TOP__"><IMG SRC="icons/h1bullet.gif" ALT="Top" BORDER="0"></A> VERSION</H2></A>


<P>$Revision: 1.1 $

<P><HR>
<ADDRESS><FONT SIZE=-1>
Generated Wed Jan 17 01:58:27 2001 by cvu_pod2html
</FONT></ADDRESS>
</FONT></BODY>
</HTML>