1MIME::Parser::Filer(3)User Contributed Perl DocumentationMIME::Parser::Filer(3)
2
3
4

NAME

6       MIME::Parser::Filer - manage file-output of the parser
7

SYNOPSIS

9       Before reading further, you should see MIME::Parser to make sure that
10       you understand where this module fits into the grand scheme of things.
11       Go on, do it now.  I'll wait.
12
13       Ready?  Ok... now read "DESCRIPTION" below, and everything else should
14       make sense.
15
16   Public interface
17           ### Create a "filer" of the desired class:
18           my $filer = MIME::Parser::FileInto->new($dir);
19           my $filer = MIME::Parser::FileUnder->new($basedir);
20           ...
21
22           ### Want added security?  Don't let outsiders name your files:
23           $filer->ignore_filename(1);
24
25           ### Prepare for the parsing of a new top-level message:
26           $filer->init_parse;
27
28           ### Return the path where this message's data should be placed:
29           $path = $filer->output_path($head);
30
31   Semi-public interface
32       These methods might be overriden or ignored in some subclasses, so they
33       don't all make sense in all circumstances:
34
35           ### Tweak the mapping from content-type to extension:
36           $emap = $filer->output_extension_map;
37           $emap->{"text/html"} = ".htm";
38

DESCRIPTION

40   How this class is used when parsing
41       When a MIME::Parser decides that it wants to output a file to disk, it
42       uses its "Filer" object -- an instance of a MIME::Parser::Filer
43       subclass -- to determine where to put the file.
44
45       Every parser has a single Filer object, which it uses for all parsing.
46       You can get the Filer for a given $parser like this:
47
48           $filer = $parser->filer;
49
50       At the beginning of each "parse()", the filer's internal state is reset
51       by the parser:
52
53           $parser->filer->init_parse;
54
55       The parser can then get a path for each entity in the message by
56       handing that entity's header (a MIME::Head) to the filer and having it
57       do the work, like this:
58
59           $new_file = $parser->filer->output_path($head);
60
61       Since it's nice to be able to clean up after a parse (especially a
62       failed parse), the parser tells the filer when it has actually used a
63       path:
64
65           $parser->filer->purgeable($new_file);
66
67       Then, if you want to clean up the files which were created for a
68       particular parse (and also any directories that the Filer created), you
69       would do this:
70
71           $parser->filer->purge;
72
73   Writing your own subclasses
74       There are two standard "Filer" subclasses (see below):
75       MIME::Parser::FileInto, which throws all files from all parses into the
76       same directory, and MIME::Parser::FileUnder (preferred), which creates
77       a subdirectory for each message.  Hopefully, these will be sufficient
78       for most uses, but just in case...
79
80       The only method you have to override is output_path():
81
82           $filer->output_path($head);
83
84       This method is invoked by MIME::Parser when it wants to put a decoded
85       message body in an output file.  The method should return a path to the
86       file to create.  Failure is indicated by throwing an exception.
87
88       The path returned by "output_path()" should be "ready for open()": any
89       necessary parent directories need to exist at that point.  These
90       directories can be created by the Filer, if course, and they should be
91       marked as purgeable() if a purge should delete them.
92
93       Actually, if your issue is more where the files go than what they're
94       named, you can use the default output_path() method and just override
95       one of its components:
96
97           $dir  = $filer->output_dir($head);
98           $name = $filer->output_filename($head);
99           ...
100

PUBLIC INTERFACE

102   MIME::Parser::Filer
103       This is the abstract superclass of all "filer" objects.
104
105       new INITARGS...
106           Class method, constructor.  Create a new outputter for the given
107           parser.  Any subsequent arguments are given to init(), which
108           subclasses should override for their own use (the default init does
109           nothing).
110
111       results RESULTS
112           Instance method.  Link this filer to a MIME::Parser::Results object
113           which will tally the messages.  Notice that we avoid linking it to
114           the parser to avoid circular reference!
115
116       init_parse
117           Instance method.  Prepare to start parsing a new message.
118           Subclasses should always be sure to invoke the inherited method.
119
120       evil_filename FILENAME
121           Instance method.  Is this an evil filename; i.e., one which should
122           not be used in generating a disk file name?  It is if any of these
123           are true:
124
125               * it is empty
126               * it is a string of dots: ".", "..", etc.
127               * it contains characters not in the set: "A" - "Z", "a" - "z",
128                 "0" - "9", "-", "_", "+", "=", ".", ",", "@", "#",
129                 "$", and " ".
130               * it is too long
131
132           If you just want to change this behavior, you should override this
133           method in the subclass of MIME::Parser::Filer that you use.
134
135           Warning: at the time this method is invoked, the FILENAME has
136           already been unmime'd into the local character set.  If you're
137           using any character set other than ASCII, ISO-8859-*, or UTF-8, the
138           interpretation of the "path" characters might be very different,
139           and you will probably need to override this method.  See "unmime"
140           in MIME::WordDecoder for more details.
141
142           Note: subclasses of MIME::Parser::Filer which override
143           output_path() might not consult this method; note, however, that
144           the built-in subclasses do consult it.
145
146           Thanks to Andrew Pimlott for finding a real dumb bug in the
147           original version.  Thanks to Nickolay Saukh for noting that evil is
148           in the eye of the beholder.
149
150       exorcise_filename FILENAME
151           Instance method.  If a given filename is evil (see "evil_filename")
152           we try to rescue it by performing some basic operations: shortening
153           it, removing bad characters, etc., and checking each against
154           evil_filename().
155
156           Returns the exorcised filename (which is guaranteed to not be
157           evil), or undef if it could not be salvaged.
158
159           Warning: at the time this method is invoked, the FILENAME has
160           already been unmime'd into the local character set.  If you're
161           using anything character set other than ASCII, ISO-8859-*, or
162           UTF-8, the interpretation of the "path" characters might be very
163           very different, and you will probably need to override this method.
164           See "unmime" in MIME::WordDecoder for more details.
165
166       find_unused_path DIR, FILENAME
167           Instance method, subclasses only.  We have decided on an output
168           directory and tentative filename, but there is a chance that it
169           might already exist.  Keep adding a numeric suffix "-1", "-2", etc.
170           to the filename until an unused path is found, and then return that
171           path.
172
173           The suffix is actually added before the first "." in the filename
174           is there is one; for example:
175
176               picture.gif       archive.tar.gz      readme
177               picture-1.gif     archive-1.tar.gz    readme-1
178               picture-2.gif     archive-2.tar.gz    readme-2
179               ...               ...                 ...
180               picture-10.gif
181               ...
182
183           This can be a costly operation, and risky if you don't want files
184           renamed, so it is in your best interest to minimize situations
185           where these kinds of collisions occur.  Unfortunately, if a
186           multipart message gives all of its parts the same recommended
187           filename, and you are placing them all in the same directory, this
188           method might be unavoidable.
189
190       ignore_filename [YESNO]
191           Instance method.  Return true if we should always ignore
192           recommended filenames in messages, choosing instead to always
193           generate our own filenames.  With argument, sets this value.
194
195           Note: subclasses of MIME::Parser::Filer which override
196           output_path() might not honor this setting; note, however, that the
197           built-in subclasses honor it.
198
199       output_dir HEAD
200           Instance method.  Return the output directory for the given header.
201           The default method returns ".".
202
203       output_filename HEAD
204           Instance method, subclasses only.  A given recommended filename was
205           either not given, or it was judged to be evil.  Return a fake name,
206           possibly using information in the message HEADer.  Note that this
207           is just the filename, not the full path.
208
209           Used by output_path().  If you're using the default
210           "output_path()", you probably don't need to worry about avoiding
211           collisions with existing files; we take care of that in
212           find_unused_path().
213
214       output_prefix [PREFIX]
215           Instance method.  Get the short string that all filenames for
216           extracted body-parts will begin with (assuming that there is no
217           better "recommended filename").  The default is "msg".
218
219           If PREFIX is not given, the current output prefix is returned.  If
220           PREFIX is given, the output prefix is set to the new value, and the
221           previous value is returned.
222
223           Used by output_filename().
224
225           Note: subclasses of MIME::Parser::Filer which override
226           output_path() or output_filename() might not honor this setting;
227           note, however, that the built-in subclasses honor it.
228
229       output_type_ext
230           Instance method.  Return a reference to the hash used by the
231           default output_filename() for mapping from content-types to
232           extensions when there is no default extension to use.
233
234               $emap = $filer->output_typemap;
235               $emap->{'text/plain'} = '.txt';
236               $emap->{'text/html'}  = '.html';
237               $emap->{'text/*'}     = '.txt';
238               $emap->{'*/*'}        = '.dat';
239
240           Note: subclasses of MIME::Parser::Filer which override
241           output_path() or output_filename() might not consult this hash;
242           note, however, that the built-in subclasses consult it.
243
244       output_path HEAD
245           Instance method, subclasses only.  Given a MIME head for a file to
246           be extracted, come up with a good output pathname for the extracted
247           file.  This is the only method you need to worry about if you are
248           building a custom filer.
249
250           The default implementation does a lot of work; subclass
251           implementers really should try to just override its components
252           instead of the whole thing.  It works basically as follows:
253
254               $directory = $self->output_dir($head);
255
256               $filename = $head->recommended_filename();
257               if (!$filename or
258                    $self->ignore_filename() or
259                    $self->evil_filename($filename)) {
260                   $filename = $self->output_filename($head);
261               }
262
263               return $self->find_unused_path($directory, $filename);
264
265           Note: There are many, many, many ways you might want to control the
266           naming of files, based on your application.  If you don't like the
267           behavior of this function, you can easily define your own subclass
268           of MIME::Parser::Filer and override it there.
269
270           Note: Nickolay Saukh pointed out that, given the subjective nature
271           of what is "evil", this function really shouldn't warn about an
272           evil filename, but maybe just issue a debug message.  I considered
273           that, but then I thought: if debugging were off, people wouldn't
274           know why (or even if) a given filename had been ignored.  In mail
275           robots that depend on externally-provided filenames, this could
276           cause hard-to-diagnose problems.  So, the message is still a
277           warning.
278
279           Thanks to Laurent Amon for pointing out problems with the original
280           implementation, and for making some good suggestions.  Thanks also
281           to Achim Bohnet for pointing out that there should be a hookless,
282           OO way of overriding the output path.
283
284       purge
285           Instance method, final.  Purge all files/directories created by the
286           last parse.  This method simply goes through the purgeable list in
287           reverse order (see "purgeable") and removes all existing
288           files/directories in it.  You should not need to override this
289           method.
290
291       purgeable [FILE]
292           Instance method, final.  Add FILE to the list of "purgeable"
293           files/directories (those which will be removed if you do a
294           "purge()").  You should not need to override this method.
295
296           If FILE is not given, the "purgeable" list is returned.  This may
297           be used for more-sophisticated purging.
298
299           As a special case, invoking this method with a FILE that is an
300           arrayref will replace the purgeable list with a copy of the array's
301           contents, so [] may be used to clear the list.
302
303           Note that the "purgeable" list is cleared when a parser begins a
304           new parse; therefore, if you want to use purge() to do cleanup, you
305           must do so before starting a new parse!
306
307   MIME::Parser::FileInto
308       This concrete subclass of MIME::Parser::Filer supports filing into a
309       given directory.
310
311       init DIRECTORY
312           Instance method, initiallizer.  Set the directory where all files
313           will go.
314
315   MIME::Parser::FileUnder
316       This concrete subclass of MIME::Parser::Filer supports filing under a
317       given directory, using one subdirectory per message, but with all
318       message parts in the same directory.
319
320       init BASEDIR, OPTSHASH...
321           Instance method, initiallizer.  Set the base directory which will
322           contain the message directories.  If used, then each parse of
323           begins by creating a new subdirectory of BASEDIR where the actual
324           parts of the message are placed.  OPTSHASH can contain the
325           following:
326
327           DirName
328               Explicitly set the name of the subdirectory which is created.
329               The default is to use the time, process id, and a sequence
330               number, but you might want a predictable directory.
331
332           Purge
333               Automatically purge the contents of the directory (including
334               all subdirectories) before each parse.  This is really only
335               needed if using an explicit DirName, and is provided as a
336               convenience only.  Currently we use the 1-arg form of
337               File::Path::rmtree; you should familiarize yourself with the
338               caveats therein.
339
340           The output_dir() will return the path to this message-specific
341           directory until the next parse is begun, so you can do this:
342
343               use File::Path;
344
345               $parser->output_under("/tmp");
346               $ent = eval { $parser->parse_open($msg); };   ### parse
347               if (!$ent) {         ### parse failed
348                   rmtree($parser->output_dir);
349                   die "parse failed: $@";
350               }
351               else {               ### parse succeeded
352                   ...do stuff...
353               }
354

SEE ALSO

356       MIME::Tools, MIME::Parser
357

AUTHOR

359       Eryq (eryq@zeegee.com), ZeeGee Software Inc (http://www.zeegee.com).
360
361       All rights reserved.  This program is free software; you can
362       redistribute it and/or modify it under the same terms as Perl itself.
363
364
365
366perl v5.12.0                      2008-06-30            MIME::Parser::Filer(3)
Impressum