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 overridden or ignored in some subclasses, so
33       they 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 or entirely whitespace
126               * it contains leading or trailing whitespace
127               * it is a string of dots: ".", "..", etc.
128               * it contains characters not in the set: "A" - "Z", "a" - "z",
129                 "0" - "9", "-", "_", "+", "=", ".", ",", "@", "#",
130                 "$", and " ".
131               * it is too long
132
133           If you just want to change this behavior, you should override this
134           method in the subclass of MIME::Parser::Filer that you use.
135
136           Warning: at the time this method is invoked, the FILENAME has
137           already been unmime'd into the local character set.  If you're
138           using any character set other than ASCII, ISO-8859-*, or UTF-8, the
139           interpretation of the "path" characters might be very different,
140           and you will probably need to override this method.  See "unmime"
141           in MIME::WordDecoder for more details.
142
143           Note: subclasses of MIME::Parser::Filer which override
144           output_path() might not consult this method; note, however, that
145           the built-in subclasses do consult it.
146
147           Thanks to Andrew Pimlott for finding a real dumb bug in the
148           original version.  Thanks to Nickolay Saukh for noting that evil is
149           in the eye of the beholder.
150
151       exorcise_filename FILENAME
152           Instance method.  If a given filename is evil (see "evil_filename")
153           we try to rescue it by performing some basic operations: shortening
154           it, removing bad characters, etc., and checking each against
155           evil_filename().
156
157           Returns the exorcised filename (which is guaranteed to not be
158           evil), or undef if it could not be salvaged.
159
160           Warning: at the time this method is invoked, the FILENAME has
161           already been unmime'd into the local character set.  If you're
162           using anything character set other than ASCII, ISO-8859-*, or
163           UTF-8, the interpretation of the "path" characters might be very
164           very different, and you will probably need to override this method.
165           See "unmime" in MIME::WordDecoder for more details.
166
167       find_unused_path DIR, FILENAME
168           Instance method, subclasses only.  We have decided on an output
169           directory and tentative filename, but there is a chance that it
170           might already exist.  Keep adding a numeric suffix "-1", "-2", etc.
171           to the filename until an unused path is found, and then return that
172           path.
173
174           The suffix is actually added before the first "." in the filename
175           is there is one; for example:
176
177               picture.gif       archive.tar.gz      readme
178               picture-1.gif     archive-1.tar.gz    readme-1
179               picture-2.gif     archive-2.tar.gz    readme-2
180               ...               ...                 ...
181               picture-10.gif
182               ...
183
184           This can be a costly operation, and risky if you don't want files
185           renamed, so it is in your best interest to minimize situations
186           where these kinds of collisions occur.  Unfortunately, if a
187           multipart message gives all of its parts the same recommended
188           filename, and you are placing them all in the same directory, this
189           method might be unavoidable.
190
191       ignore_filename [YESNO]
192           Instance method.  Return true if we should always ignore
193           recommended filenames in messages, choosing instead to always
194           generate our own filenames.  With argument, sets this value.
195
196           Note: subclasses of MIME::Parser::Filer which override
197           output_path() might not honor this setting; note, however, that the
198           built-in subclasses honor it.
199
200       output_dir HEAD
201           Instance method.  Return the output directory for the given header.
202           The default method returns ".".
203
204       output_filename HEAD
205           Instance method, subclasses only.  A given recommended filename was
206           either not given, or it was judged to be evil.  Return a fake name,
207           possibly using information in the message HEADer.  Note that this
208           is just the filename, not the full path.
209
210           Used by output_path().  If you're using the default output_path(),
211           you probably don't need to worry about avoiding collisions with
212           existing files; we take care of that in 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.36.0                      2023-01-20            MIME::Parser::Filer(3)
Impressum