File Coverage

blib/lib/MIME/Head.pm
Criterion Covered Total %
statement 117 132 88.6
branch 25 34 73.5
condition 17 25 68.0
subroutine 28 34 82.3
pod 15 21 71.4
total 202 246 82.1


line stmt bran cond sub pod time code
1             package MIME::Head;
2              
3 24     24   461674 use MIME::WordDecoder;
  24         106  
  24         2798  
4             =head1 NAME
5              
6             MIME::Head - MIME message header (a subclass of Mail::Header)
7              
8              
9             =head1 SYNOPSIS
10              
11             Before reading further, you should see L to make sure that
12             you understand where this module fits into the grand scheme of things.
13             Go on, do it now. I'll wait.
14              
15             Ready? Ok...
16              
17             =head2 Construction
18              
19             ### Create a new, empty header, and populate it manually:
20             $head = MIME::Head->new;
21             $head->replace('content-type', 'text/plain; charset=US-ASCII');
22             $head->replace('content-length', $len);
23              
24             ### Parse a new header from a filehandle:
25             $head = MIME::Head->read(\*STDIN);
26              
27             ### Parse a new header from a file, or a readable pipe:
28             $testhead = MIME::Head->from_file("/tmp/test.hdr");
29             $a_b_head = MIME::Head->from_file("cat a.hdr b.hdr |");
30              
31              
32             =head2 Output
33              
34             ### Output to filehandle:
35             $head->print(\*STDOUT);
36              
37             ### Output as string:
38             print STDOUT $head->as_string;
39             print STDOUT $head->stringify;
40              
41              
42             =head2 Getting field contents
43              
44             ### Is this a reply?
45             $is_reply = 1 if ($head->get('Subject') =~ /^Re: /);
46              
47             ### Get receipt information:
48             print "Last received from: ", $head->get('Received', 0);
49             @all_received = $head->get('Received');
50              
51             ### Print the subject, or the empty string if none:
52             print "Subject: ", $head->get('Subject',0);
53              
54             ### Too many hops? Count 'em and see!
55             if ($head->count('Received') > 5) { ...
56              
57             ### Test whether a given field exists
58             warn "missing subject!" if (! $head->count('subject'));
59              
60              
61             =head2 Setting field contents
62              
63             ### Declare this to be an HTML header:
64             $head->replace('Content-type', 'text/html');
65              
66              
67             =head2 Manipulating field contents
68              
69             ### Get rid of internal newlines in fields:
70             $head->unfold;
71              
72             ### Decode any Q- or B-encoded-text in fields (DEPRECATED):
73             $head->decode;
74              
75              
76             =head2 Getting high-level MIME information
77              
78             ### Get/set a given MIME attribute:
79             unless ($charset = $head->mime_attr('content-type.charset')) {
80             $head->mime_attr("content-type.charset" => "US-ASCII");
81             }
82              
83             ### The content type (e.g., "text/html"):
84             $mime_type = $head->mime_type;
85              
86             ### The content transfer encoding (e.g., "quoted-printable"):
87             $mime_encoding = $head->mime_encoding;
88              
89             ### The recommended name when extracted:
90             $file_name = $head->recommended_filename;
91              
92             ### The boundary text, for multipart messages:
93             $boundary = $head->multipart_boundary;
94              
95              
96             =head1 DESCRIPTION
97              
98             A class for parsing in and manipulating RFC-822 message headers, with
99             some methods geared towards standard (and not so standard) MIME fields
100             as specified in the various I
101             RFCs (starting with RFC 2045)
102              
103              
104             =head1 PUBLIC INTERFACE
105              
106             =cut
107              
108             #------------------------------
109              
110             require 5.002;
111              
112             ### Pragmas:
113 24     24   192 use strict;
  24         54  
  24         780  
114 24     24   122 use vars qw($VERSION @ISA @EXPORT_OK);
  24         48  
  24         1530  
115              
116             ### System modules:
117 24     24   1836 use IO::File;
  24         28301  
  24         4330  
118              
119             ### Other modules:
120 24     24   10843 use Mail::Header 1.09 ();
  24         111572  
  24         1353  
121 24     24   10156 use Mail::Field 1.05 ();
  24         67348  
  24         912  
122              
123             ### Kit modules:
124 24     24   170 use MIME::Words qw(:all);
  24         74  
  24         5034  
125 24     24   2207 use MIME::Tools qw(:config :msgs);
  24         67  
  24         3745  
126 24     24   35745 use MIME::Field::ParamVal;
  24         132  
  24         261  
127 24     24   36433 use MIME::Field::ConTraEnc;
  24         82  
  24         149  
128 24     24   35575 use MIME::Field::ContDisp;
  24         77  
  24         134  
129 24     24   33937 use MIME::Field::ContType;
  24         109  
  24         145  
130              
131             @ISA = qw(Mail::Header);
132              
133             # The presence of more than one of the following headers
134             # in a given MIME entity could indicate an ambiguous parse
135             # and hence a security risk
136              
137             my $singleton_headers =
138             [
139             'content-type',
140             'content-disposition',
141             'content-transfer-encoding',
142             'content-id',
143             ];
144              
145             # The presence of a duplicated or empty parameters in one of the following
146             # headers in a given MIME entity could indicate an ambiguous parse and
147             # hence a security risk
148             my $singleton_parameter_headers =
149             [
150             'content-type',
151             'content-disposition',
152             ];
153              
154              
155             #------------------------------
156             #
157             # Public globals...
158             #
159             #------------------------------
160              
161             ### The package version, both in 1.23 style *and* usable by MakeMaker:
162             $VERSION = "5.517";
163              
164             ### Sanity (we put this test after our own version, for CPAN::):
165 24     24   20077 use Mail::Header 1.06 ();
  24         470  
  24         45793  
166              
167              
168             #------------------------------
169              
170             =head2 Creation, input, and output
171              
172             =over 4
173              
174             =cut
175              
176             #------------------------------
177              
178              
179             #------------------------------
180              
181             =item new [ARG],[OPTIONS]
182              
183             I
184             Creates a new header object. Arguments are the same as those in the
185             superclass.
186              
187             =cut
188              
189             sub new {
190 516     516 1 380625 my $class = shift;
191 516         2202 bless Mail::Header->new(@_), $class;
192             }
193              
194             =item ambiguous_content
195              
196             I
197              
198             Returns true if this header has any the following properties:
199              
200             =over 4
201              
202             More than one Content-Type, Content-ID, Content-Transfer-Encoding or
203             Content-Disposition header.
204              
205             A Content-Type or Content-Disposition header contains a repeated
206             parameter.
207              
208             =back
209              
210             Messages with ambiguous content should be treated as a security risk.
211             In particular, if MIME-tools is used in an email security tool,
212             ambiguous messages should not be delivered to end-users.
213              
214             =cut
215             sub ambiguous_content {
216 223     223 1 484 my ($self) = @_;
217              
218 223         667 foreach my $hdr (@$singleton_headers) {
219 878 100       19255 if ($self->count($hdr) > 1) {
220 9         1665 return 1;
221             }
222             }
223              
224 214         5868 foreach my $hdr (@$singleton_parameter_headers) {
225 420 100       1747 if ($self->mime_attr($hdr . '.@duplicate_parameters')) {
226 7         65 return 1;
227             }
228 413 100       1685 if ($self->mime_attr($hdr . '.@empty_parameters')) {
229 4         34 return 1;
230             }
231             }
232 203         812 return 0;
233             }
234             #------------------------------
235              
236             =item from_file EXPR,OPTIONS
237              
238             I.
239             For convenience, you can use this to parse a header object in from EXPR,
240             which may actually be any expression that can be sent to open() so as to
241             return a readable filehandle. The "file" will be opened, read, and then
242             closed:
243              
244             ### Create a new header by parsing in a file:
245             my $head = MIME::Head->from_file("/tmp/test.hdr");
246              
247             Since this method can function as either a class constructor I
248             an instance initializer, the above is exactly equivalent to:
249              
250             ### Create a new header by parsing in a file:
251             my $head = MIME::Head->new->from_file("/tmp/test.hdr");
252              
253             On success, the object will be returned; on failure, the undefined value.
254              
255             The OPTIONS are the same as in new(), and are passed into new()
256             if this is invoked as a class method.
257              
258             B This is really just a convenience front-end onto C,
259             provided mostly for backwards-compatibility with MIME-parser 1.0.
260              
261             =cut
262              
263             sub from_file {
264 6     6 1 233480 my ($self, $file, @opts) = @_; ### at this point, $self is inst. or class!
265 6 100       23 my $class = ref($self) ? ref($self) : $self;
266              
267             ### Parse:
268 6 100       48 my $fh = IO::File->new($file, '<') or return error("open $file: $!");
269 5 50       718 $fh->binmode() or return error("binmode $file: $!"); # we expect to have \r\n at line ends, and want to keep 'em.
270 5         85 $self = $class->new($fh, @opts); ### now, $self is instance or undef
271 5 50       6077 $fh->close or return error("close $file: $!");
272 5         179 $self;
273             }
274              
275             #------------------------------
276              
277             =item read FILEHANDLE
278              
279             I
280             This initializes a header object by reading it in from a FILEHANDLE,
281             until the terminating blank line is encountered.
282             A syntax error or end-of-stream will also halt processing.
283              
284             Supply this routine with a reference to a filehandle glob; e.g., C<\*STDIN>:
285              
286             ### Create a new header by parsing in STDIN:
287             $head->read(\*STDIN);
288              
289             On success, the self object will be returned; on failure, a false value.
290              
291             B in the MIME world, it is perfectly legal for a header to be
292             empty, consisting of nothing but the terminating blank line. Thus,
293             we can't just use the formula that "no tags equals error".
294              
295             B as of the time of this writing, Mail::Header::read did not flag
296             either syntax errors or unexpected end-of-file conditions (an EOF
297             before the terminating blank line). MIME::ParserBase takes this
298             into account.
299              
300             =cut
301              
302             sub read {
303 201     201 1 368 my $self = shift; ### either instance or class!
304 201 50       684 ref($self) or $self = $self->new; ### if used as class method, make new
305 201         1057 $self->SUPER::read(@_);
306             }
307              
308              
309              
310             #------------------------------
311              
312             =back
313              
314             =head2 Getting/setting fields
315              
316             The following are methods related to retrieving and modifying the header
317             fields. Some are inherited from Mail::Header, but I've kept the
318             documentation around for convenience.
319              
320             =over 4
321              
322             =cut
323              
324             #------------------------------
325              
326              
327             #------------------------------
328              
329             =item add TAG,TEXT,[INDEX]
330              
331             I
332             Add a new occurrence of the field named TAG, given by TEXT:
333              
334             ### Add the trace information:
335             $head->add('Received',
336             'from eryq.pr.mcs.net by gonzo.net with smtp');
337              
338             Normally, the new occurrence will be I to the existing
339             occurrences. However, if the optional INDEX argument is 0, then the
340             new occurrence will be I. If you want to be I
341             about appending, specify an INDEX of -1.
342              
343             B: this method always adds new occurrences; it doesn't overwrite
344             any existing occurrences... so if you just want to I the value
345             of a field (creating it if necessary), then you probably B want to use
346             this method: consider using C instead.
347              
348             =cut
349              
350             ### Inherited.
351              
352             #------------------------------
353             #
354             # copy
355             #
356             # Instance method, DEPRECATED.
357             # Duplicate the object.
358             #
359             sub copy {
360 0     0 0 0 usage "deprecated: use dup() instead.";
361 0         0 shift->dup(@_);
362             }
363              
364             #------------------------------
365              
366             =item count TAG
367              
368             I
369             Returns the number of occurrences of a field; in a boolean context, this
370             tells you whether a given field exists:
371              
372             ### Was a "Subject:" field given?
373             $subject_was_given = $head->count('subject');
374              
375             The TAG is treated in a case-insensitive manner.
376             This method returns some false value if the field doesn't exist,
377             and some true value if it does.
378              
379             =cut
380              
381             ### Inherited.
382              
383              
384             #------------------------------
385              
386             =item decode [FORCE]
387              
388             I
389             Go through all the header fields, looking for RFC 1522 / RFC 2047 style
390             "Q" (quoted-printable, sort of) or "B" (base64) encoding, and decode
391             them in-place. Fellow Americans, you probably don't know what the hell
392             I'm talking about. Europeans, Russians, et al, you probably do.
393             C<:-)>.
394              
395             B
396             See L for the full reasons.
397             If you absolutely must use it and don't like the warning, then
398             provide a FORCE:
399              
400             "I_NEED_TO_FIX_THIS"
401             Just shut up and do it. Not recommended.
402             Provided only for those who need to keep old scripts functioning.
403              
404             "I_KNOW_WHAT_I_AM_DOING"
405             Just shut up and do it. Not recommended.
406             Provided for those who REALLY know what they are doing.
407              
408             B
409             For an example, let's consider a valid email header you might get:
410              
411             From: =?US-ASCII?Q?Keith_Moore?=
412             To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?=
413             CC: =?ISO-8859-1?Q?Andr=E9_?= Pirard
414             Subject: =?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=
415             =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=
416             =?US-ASCII?Q?.._cool!?=
417              
418             That basically decodes to (sorry, I can only approximate the
419             Latin characters with 7 bit sequences /o and 'e):
420              
421             From: Keith Moore
422             To: Keld J/orn Simonsen
423             CC: Andr'e Pirard
424             Subject: If you can read this you understand the example... cool!
425              
426             B currently, the decodings are done without regard to the
427             character set: thus, the Q-encoding C<=F8> is simply translated to the
428             octet (hexadecimal C), period. For piece-by-piece decoding
429             of a given field, you want the array context of
430             C.
431              
432             B the CRLF+SPACE separator that splits up long encoded words
433             into shorter sequences (see the Subject: example above) gets lost
434             when the field is unfolded, and so decoding after unfolding causes
435             a spurious space to be left in the field.
436             I
437              
438             This method returns the self object.
439              
440             I
441             RFC-1522-decoding code.>
442              
443             =cut
444              
445             sub decode {
446 2     2 1 1015 my $self = shift;
447              
448             ### Warn if necessary:
449 2   50     15 my $force = shift || 0;
450 2 50 33     13 unless (($force eq "I_NEED_TO_FIX_THIS") ||
451             ($force eq "I_KNOW_WHAT_I_AM_DOING")) {
452 2         8 usage "decode is deprecated for safety";
453             }
454              
455 2         5 my ($tag, $i, @decoded);
456 2         12 foreach $tag ($self->tags) {
457 20         3233 @decoded = map { scalar(decode_mimewords($_, Field=>$tag))
  20         721  
458             } $self->get_all($tag);
459 20         61 for ($i = 0; $i < @decoded; $i++) {
460 20         76 $self->replace($tag, $decoded[$i], $i);
461             }
462             }
463 2         346 $self->{MH_Decoded} = 1;
464 2         8 $self;
465             }
466              
467             #------------------------------
468              
469             =item delete TAG,[INDEX]
470              
471             I
472             Delete all occurrences of the field named TAG.
473              
474             ### Remove some MIME information:
475             $head->delete('MIME-Version');
476             $head->delete('Content-type');
477              
478             =cut
479              
480             ### Inherited
481              
482              
483             #------------------------------
484             #
485             # exists
486             #
487             sub exists {
488 0     0 0 0 usage "deprecated; use count() instead";
489 0         0 shift->count(@_);
490             }
491              
492             #------------------------------
493             #
494             # fields
495             #
496             sub fields {
497 0     0 1 0 usage "deprecated: use tags() instead",
498             shift->tags(@_);
499             }
500              
501             #------------------------------
502              
503             =item get TAG,[INDEX]
504              
505             I
506             Get the contents of field TAG.
507              
508             If a B is given, returns the occurrence at that index,
509             or undef if not present:
510              
511             ### Print the first and last 'Received:' entries (explicitly):
512             print "First, or most recent: ", $head->get('received', 0);
513             print "Last, or least recent: ", $head->get('received',-1);
514              
515             If B is given, but invoked in a B context, then
516             INDEX simply defaults to 0:
517              
518             ### Get the first 'Received:' entry (implicitly):
519             my $most_recent = $head->get('received');
520              
521             If B is given, and invoked in an B context, then
522             I occurrences of the field are returned:
523              
524             ### Get all 'Received:' entries:
525             my @all_received = $head->get('received');
526              
527             B: The header(s) returned may end with a newline. If you don't
528             want this, then B the return value.
529              
530             =cut
531              
532             ### Inherited.
533              
534              
535             #------------------------------
536              
537             =item get_all FIELD
538              
539             I
540             Returns the list of I occurrences of the field, or the
541             empty list if the field is not present:
542              
543             ### How did it get here?
544             @history = $head->get_all('Received');
545              
546             B I had originally experimented with having C return all
547             occurrences when invoked in an array context... but that causes a lot of
548             accidents when you get careless and do stuff like this:
549              
550             print "\u$field: ", $head->get($field);
551              
552             It also made the intuitive behaviour unclear if the INDEX argument
553             was given in an array context. So I opted for an explicit approach
554             to asking for all occurrences.
555              
556             =cut
557              
558             sub get_all {
559 21     21 1 744 my ($self, $tag) = @_;
560 21 50       77 $self->count($tag) or return (); ### empty if doesn't exist
561 21         611 ($self->get($tag));
562             }
563              
564             #------------------------------
565             #
566             # original_text
567             #
568             # Instance method, DEPRECATED.
569             # Return an approximation of the original text.
570             #
571             sub original_text {
572 0     0 0 0 usage "deprecated: use stringify() instead";
573 0         0 shift->stringify(@_);
574             }
575              
576             #------------------------------
577              
578             =item print [OUTSTREAM]
579              
580             I
581             Print the header out to the given OUTSTREAM, or the currently-selected
582             filehandle if none. The OUTSTREAM may be a filehandle, or any object
583             that responds to a print() message.
584              
585             The override actually lets you print to any object that responds to
586             a print() method. This is vital for outputting MIME entities to scalars.
587              
588             Also, it defaults to the I filehandle if none is given
589             (not STDOUT!), so I supply a filehandle to prevent confusion.
590              
591             =cut
592              
593             sub print {
594 77     77 1 4466 my ($self, $fh) = @_;
595 77   33     211 $fh ||= select;
596 77         325 $fh->print($self->as_string);
597             }
598              
599             #------------------------------
600             #
601             # set TAG,TEXT
602             #
603             # Instance method, DEPRECATED.
604             # Set the field named TAG to [the single occurrence given by the TEXT.
605             #
606             sub set {
607 0     0 0 0 my $self = shift;
608 0         0 usage "deprecated: use the replace() method instead.";
609 0         0 $self->replace(@_);
610             }
611              
612             #------------------------------
613              
614             =item stringify
615              
616             I
617             Return the header as a string. You can also invoke it as C.
618              
619             If you set the variable $MIME::Entity::BOUNDARY_DELIMITER to a string,
620             that string will be used as line-end delimiter. If it is not set,
621             the line ending will be a newline character (\n)
622              
623             =cut
624              
625             sub stringify {
626 80     80 1 1649 my $self = shift; ### build clean header, and output...
627 80 50       147 my @header = grep {defined($_) ? $_ : ()} @{$self->header};
  418         4384  
  80         372  
628 80   100     389 my $header_delimiter = $MIME::Entity::BOUNDARY_DELIMITER || "\n";
629 80 50       201 join "", map { /\n$/ ? substr($_, 0, -1) . $header_delimiter : $_ . $header_delimiter } @header;
  418         2227  
630             }
631 77     77 1 249 sub as_string { shift->stringify(@_) }
632              
633             #------------------------------
634              
635             =item unfold [FIELD]
636              
637             I
638             Unfold (remove newlines in) the text of all occurrences of the given FIELD.
639             If the FIELD is omitted, I fields are unfolded.
640             Returns the "self" object.
641              
642             =cut
643              
644             ### Inherited
645              
646              
647             #------------------------------
648              
649             =back
650              
651             =head2 MIME-specific methods
652              
653             All of the following methods extract information from the following fields:
654              
655             Content-type
656             Content-transfer-encoding
657             Content-disposition
658              
659             Be aware that they do not just return the raw contents of those fields,
660             and in some cases they will fill in sensible (I hope) default values.
661             Use C or C if you need to grab and process the
662             raw field text.
663              
664             B some of these methods are provided both as a convenience and
665             for backwards-compatibility only, while others (like
666             recommended_filename()) I
667             properly,> since they look for their value in more than one field.
668             However, if you know that a value is restricted to a single
669             field, you should really use the Mail::Field interface to get it.
670              
671             =over 4
672              
673             =cut
674              
675             #------------------------------
676              
677              
678             #------------------------------
679             #
680             # params TAG
681             #
682             # Instance method, DEPRECATED.
683             # Extract parameter info from a structured field, and return
684             # it as a hash reference. Provided for 1.0 compatibility only!
685             # Use the new MIME::Field interface classes (subclasses of Mail::Field).
686              
687             sub params {
688 1     1 0 4568 my ($self, $tag) = @_;
689 1         8 usage "deprecated: use the MIME::Field interface classes from now on!";
690 1         6 return MIME::Field::ParamVal->parse_params($self->get($tag,0));
691             }
692              
693             #------------------------------
694              
695             =item mime_attr ATTR,[VALUE]
696              
697             A quick-and-easy interface to set/get the attributes in structured
698             MIME fields:
699              
700             $head->mime_attr("content-type" => "text/html");
701             $head->mime_attr("content-type.charset" => "US-ASCII");
702             $head->mime_attr("content-type.name" => "homepage.html");
703              
704             This would cause the final output to look something like this:
705              
706             Content-type: text/html; charset=US-ASCII; name="homepage.html"
707              
708             Note that the special empty sub-field tag indicates the anonymous
709             first sub-field.
710              
711             B will cause the contents of the named subfield
712             to be deleted:
713              
714             $head->mime_attr("content-type.charset" => undef);
715              
716             B just returns the attribute's value,
717             or undefined if it isn't there:
718              
719             $type = $head->mime_attr("content-type"); ### text/html
720             $name = $head->mime_attr("content-type.name"); ### homepage.html
721              
722             In all cases, the new/current value is returned.
723              
724             The special sub-field tag C<@duplicate_parameters> (which can never be
725             a real tag) returns an arrayref of tags that were duplicated in the header,
726             or C if no such tags were found. For example, given the header:
727              
728             Content-Type: multipart/mixed; boundary="foo"; boundary="bar"
729              
730             Then:
731              
732             $head->mime_attr('content-type.@duplicate_parameters')
733              
734             would return:
735              
736             [ 'boundary' ]
737              
738             Similarly he special sub-field tag C<@empty_parameters> (which can never be
739             a real tag) returns an arrayref of tags that had an empty parameter value
740             in the header, or C if no such tags were found. For example, given
741             the header:
742              
743             Content-Type: multipart/mixed; boundary=;
744              
745             Then:
746              
747             $head->mime_attr('content-type.@emtpy_parameters')
748              
749             would return:
750              
751             [ 'boundary' ]
752              
753             A duplicate or empty "boundary" tag should be treated as a security risk, as
754             should duplicate Content-Type headers in a message. Since such messages
755             cannot be parsed unambiguously, we strongly recommend that they never be
756             delivered to end-users.
757              
758             Note also that a parameter explicitly set to blank via something like:
759              
760             Content-Type: application/octet-stream; boundary=""
761              
762             I populate C<@empty_parameters>
763              
764             =cut
765              
766             sub mime_attr {
767 2639     2639 1 6730 my ($self, $attr, $value) = @_;
768              
769             ### Break attribute name up:
770 2639         9294 my ($tag, $subtag) = split /\./, $attr;
771 2639   100     10440 $subtag ||= '_';
772              
773             ### Set or get?
774 2639         8046 my $field = MIME::Field::ParamVal->parse($self->get($tag, 0));
775 2639 100       8439 if (@_ > 2) { ### set it:
776 11         28 $field->param($subtag, $value); ### set subfield
777 11         24 $self->replace($tag, $field->stringify); ### replace!
778 11         1603 return $value;
779             }
780             else { ### get it:
781 2628         8270 return $field->param($subtag);
782             }
783             }
784              
785             #------------------------------
786              
787             =item mime_encoding
788              
789             I
790             Try I to determine the content transfer encoding
791             (e.g., C<"base64">, C<"binary">), which is returned in all-lowercase.
792              
793             If no encoding could be found, the default of C<"7bit"> is returned
794             I quote from RFC 2045 section 6.1:
795              
796             This is the default value -- that is, "Content-Transfer-Encoding: 7BIT"
797             is assumed if the Content-Transfer-Encoding header field is not present.
798              
799             I do one other form of fixup: "7_bit", "7-bit", and "7 bit" are
800             corrected to "7bit"; likewise for "8bit".
801              
802             =cut
803              
804             sub mime_encoding {
805 316     316 1 2281 my $self = shift;
806 316   100     807 my $enc = lc($self->mime_attr('content-transfer-encoding') || '7bit');
807 316         740 $enc =~ s{^([78])[ _-]bit\Z}{$1bit};
808 316         1011 $enc;
809             }
810              
811             #------------------------------
812              
813             =item mime_type [DEFAULT]
814              
815             I
816             Try C to determine the content type (e.g., C<"text/plain">,
817             C<"image/gif">, C<"x-weird-type">, which is returned in all-lowercase.
818             "Real hard" means that if no content type could be found, the default
819             (usually C<"text/plain">) is returned. From RFC 2045 section 5.2:
820              
821             Default RFC 822 messages without a MIME Content-Type header are
822             taken by this protocol to be plain text in the US-ASCII character
823             set, which can be explicitly specified as:
824              
825             Content-type: text/plain; charset=us-ascii
826              
827             This default is assumed if no Content-Type header field is specified.
828              
829             Unless this is a part of a "multipart/digest", in which case
830             "message/rfc822" is the default. Note that you can also I the
831             default, but you shouldn't: normally only the MIME parser uses this
832             feature.
833              
834             =cut
835              
836             sub mime_type {
837 993     993 1 11377 my ($self, $default) = @_;
838 993 100       2049 $self->{MIH_DefaultType} = $default if @_ > 1;
839             my $s = $self->mime_attr('content-type') ||
840             $self->{MIH_DefaultType} ||
841 993   100     3179 'text/plain';
842             # avoid [perl #87336] bug, lc laundering tainted data
843 993 50 33     8878 return lc($s) if $] <= 5.008 || $] >= 5.014;
844 0         0 $s =~ tr/A-Z/a-z/;
845 0         0 $s;
846             }
847              
848             #------------------------------
849              
850             =item multipart_boundary
851              
852             I
853             If this is a header for a multipart message, return the
854             "encapsulation boundary" used to separate the parts. The boundary
855             is returned exactly as given in the C field; that
856             is, the leading double-hyphen (C<-->) is I prepended.
857              
858             Well, I exactly... this passage from RFC 2046 dictates
859             that we remove any trailing spaces:
860              
861             If a boundary appears to end with white space, the white space
862             must be presumed to have been added by a gateway, and must be deleted.
863              
864             Returns undef (B the empty string) if either the message is not
865             multipart or if there is no specified boundary.
866              
867             =cut
868              
869             sub multipart_boundary {
870 86     86 1 510 my $self = shift;
871 86         2631 my $value = $self->mime_attr('content-type.boundary');
872 86 50       400 (!defined($value)) ? undef : $value;
873             }
874              
875             #------------------------------
876              
877             =item recommended_filename
878              
879             I
880             Return the recommended external filename. This is used when
881             extracting the data from the MIME stream. The filename is always
882             returned as a string in Perl's internal format (the UTF8 flag may be on!)
883              
884             Returns undef if no filename could be suggested.
885              
886             =cut
887              
888             sub recommended_filename
889             {
890 190     190 1 1064 my $self = shift;
891              
892             # Try these headers in order, taking the first defined,
893             # non-blank one we find.
894 190         1500 my $wd = supported MIME::WordDecoder 'UTF-8';
895 190         466 foreach my $attr_name ( qw( content-disposition.filename content-type.name ) ) {
896 327         714 my $value = $self->mime_attr( $attr_name );
897 327 100 66     1605 if ( defined $value
      100        
898             && $value ne ''
899             && $value =~ /\S/ ) {
900 77         374 return $wd->decode($value);
901             }
902             }
903              
904 113         300 return undef;
905             }
906              
907             #------------------------------
908              
909             =back
910              
911             =cut
912              
913              
914             #------------------------------
915             #
916             # tweak_FROM_parsing
917             #
918             # DEPRECATED. Use the inherited mail_from() class method now.
919              
920             sub tweak_FROM_parsing {
921 0     0 0   my $self = shift;
922 0           usage "deprecated. Use mail_from() instead.";
923 0           $self->mail_from(@_);
924             }
925              
926              
927             __END__