home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / Head.pm < prev    next >
Encoding:
Perl POD Document  |  2002-06-14  |  25.1 KB  |  908 lines

  1. package MIME::Head;
  2.  
  3.  
  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<MIME::Tools> 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), "\n";
  49.     @all_received = $head->get('Received');
  50.  
  51.     ### Print the subject, or the empty string if none:
  52.     print "Subject: ", $head->get('Subject',0), "\n";
  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 some
  99. methods geared towards standard (and not so standard) MIME fields as
  100. specified in RFC-1521, I<Multipurpose Internet Mail Extensions>.
  101.  
  102.  
  103. =head1 PUBLIC INTERFACE
  104.  
  105. =cut
  106.  
  107. #------------------------------
  108.  
  109. require 5.002;
  110.  
  111. ### Pragmas:
  112. use strict;
  113. use vars qw($VERSION @ISA @EXPORT_OK);
  114.  
  115. ### System modules:
  116. use IO::Wrap;
  117.  
  118. ### Other modules:
  119. use Mail::Header 1.09 ();
  120. use Mail::Field  1.05 ();
  121.  
  122. ### Kit modules:
  123. use MIME::Words qw(:all);
  124. use MIME::Tools qw(:config :msgs);
  125. use MIME::Field::ParamVal;
  126. use MIME::Field::ConTraEnc;
  127. use MIME::Field::ContDisp;
  128. use MIME::Field::ContType;
  129.  
  130. @ISA = qw(Mail::Header);
  131.  
  132.  
  133. #------------------------------
  134. #
  135. # Public globals...
  136. #
  137. #------------------------------
  138.  
  139. ### The package version, both in 1.23 style *and* usable by MakeMaker:
  140. $VERSION = substr q$Revision: 5.403 $, 10;
  141.  
  142. ### Sanity (we put this test after our own version, for CPAN::):
  143. use Mail::Header 1.06 ();
  144.  
  145.  
  146. #------------------------------
  147.  
  148. =head2 Creation, input, and output
  149.  
  150. =over 4
  151.  
  152. =cut
  153.  
  154. #------------------------------
  155.  
  156.  
  157. #------------------------------
  158.  
  159. =item new [ARG],[OPTIONS]
  160.  
  161. I<Class method, inherited.>
  162. Creates a new header object.  Arguments are the same as those in the
  163. superclass.
  164.  
  165. =cut
  166.  
  167. sub new {
  168.     my $class = shift;
  169.     bless Mail::Header->new(@_), $class;
  170. }
  171.  
  172. #------------------------------
  173.  
  174. =item from_file EXPR,OPTIONS
  175.  
  176. I<Class or instance method>.
  177. For convenience, you can use this to parse a header object in from EXPR,
  178. which may actually be any expression that can be sent to open() so as to
  179. return a readable filehandle.  The "file" will be opened, read, and then
  180. closed:
  181.  
  182.     ### Create a new header by parsing in a file:
  183.     my $head = MIME::Head->from_file("/tmp/test.hdr");
  184.  
  185. Since this method can function as either a class constructor I<or>
  186. an instance initializer, the above is exactly equivalent to:
  187.  
  188.     ### Create a new header by parsing in a file:
  189.     my $head = MIME::Head->new->from_file("/tmp/test.hdr");
  190.  
  191. On success, the object will be returned; on failure, the undefined value.
  192.  
  193. The OPTIONS are the same as in new(), and are passed into new()
  194. if this is invoked as a class method.
  195.  
  196. B<Note:> This is really just a convenience front-end onto C<read()>,
  197. provided mostly for backwards-compatibility with MIME-parser 1.0.
  198.  
  199. =cut
  200.  
  201. sub from_file {
  202.     my ($self, $file, @opts) = @_; ### at this point, $self is inst. or class!
  203.     my $class = ref($self) ? ref($self) : $self;
  204.  
  205.     ### Parse:
  206.     open(HDR, $file) or return error("open $file: $!");
  207.     binmode(HDR);  # we expect to have \r\n at line ends, and want to keep 'em.
  208.     $self = $class->new(\*HDR, @opts);      ### now, $self is instance or undef
  209.     close(HDR);
  210.     $self;
  211. }
  212.  
  213. #------------------------------
  214.  
  215. =item read FILEHANDLE
  216.  
  217. I<Instance (or class) method.>
  218. This initiallizes a header object by reading it in from a FILEHANDLE,
  219. until the terminating blank line is encountered.
  220. A syntax error or end-of-stream will also halt processing.
  221.  
  222. Supply this routine with a reference to a filehandle glob; e.g., C<\*STDIN>:
  223.  
  224.     ### Create a new header by parsing in STDIN:
  225.     $head->read(\*STDIN);
  226.  
  227. On success, the self object will be returned; on failure, a false value.
  228.  
  229. B<Note:> in the MIME world, it is perfectly legal for a header to be
  230. empty, consisting of nothing but the terminating blank line.  Thus,
  231. we can't just use the formula that "no tags equals error".
  232.  
  233. B<Warning:> as of the time of this writing, Mail::Header::read did not flag
  234. either syntax errors or unexpected end-of-file conditions (an EOF
  235. before the terminating blank line).  MIME::ParserBase takes this
  236. into account.
  237.  
  238. =cut
  239.  
  240. sub read {
  241.     my $self = shift;      ### either instance or class!
  242.     ref($self) or $self = $self->new;    ### if used as class method, make new
  243.     $self->SUPER::read(@_);   
  244. }
  245.  
  246.  
  247.  
  248. #------------------------------
  249.  
  250. =back
  251.  
  252. =head2 Getting/setting fields
  253.  
  254. The following are methods related to retrieving and modifying the header
  255. fields.  Some are inherited from Mail::Header, but I've kept the
  256. documentation around for convenience.
  257.  
  258. =over 4
  259.  
  260. =cut
  261.  
  262. #------------------------------
  263.  
  264.  
  265. #------------------------------
  266.  
  267. =item add TAG,TEXT,[INDEX]
  268.  
  269. I<Instance method, inherited.>
  270. Add a new occurence of the field named TAG, given by TEXT:
  271.  
  272.     ### Add the trace information:
  273.     $head->add('Received',
  274.                'from eryq.pr.mcs.net by gonzo.net with smtp');
  275.  
  276. Normally, the new occurence will be I<appended> to the existing
  277. occurences.  However, if the optional INDEX argument is 0, then the
  278. new occurence will be I<prepended>.  If you want to be I<explicit>
  279. about appending, specify an INDEX of -1.
  280.  
  281. B<Warning>: this method always adds new occurences; it doesn't overwrite
  282. any existing occurences... so if you just want to I<change> the value
  283. of a field (creating it if necessary), then you probably B<don't> want to use
  284. this method: consider using C<replace()> instead.
  285.  
  286. =cut
  287.  
  288. ### Inherited.
  289.  
  290. #------------------------------
  291. #
  292. # copy
  293. #
  294. # Instance method, DEPRECATED.
  295. # Duplicate the object.
  296. #
  297. sub copy {
  298.     usage "deprecated: use dup() instead.";
  299.     shift->dup(@_);
  300. }
  301.  
  302. #------------------------------
  303.  
  304. =item count TAG
  305.  
  306. I<Instance method, inherited.>
  307. Returns the number of occurences of a field; in a boolean context, this
  308. tells you whether a given field exists:
  309.  
  310.     ### Was a "Subject:" field given?
  311.     $subject_was_given = $head->count('subject');
  312.  
  313. The TAG is treated in a case-insensitive manner.
  314. This method returns some false value if the field doesn't exist,
  315. and some true value if it does.
  316.  
  317. =cut
  318.  
  319. ### Inherited.
  320.  
  321.  
  322. #------------------------------
  323.  
  324. =item decode [FORCE]
  325.  
  326. I<Instance method, DEPRECATED.>
  327. Go through all the header fields, looking for RFC-1522-style "Q"
  328. (quoted-printable, sort of) or "B" (base64) encoding, and decode them
  329. in-place.  Fellow Americans, you probably don't know what the hell I'm
  330. talking about.  Europeans, Russians, et al, you probably do.  C<:-)>.
  331.  
  332. B<This method has been deprecated.>
  333. See L<MIME::Parser/decode_headers> for the full reasons.
  334. If you absolutely must use it and don't like the warning, then
  335. provide a FORCE:
  336.  
  337.    "I_NEED_TO_FIX_THIS"
  338.           Just shut up and do it.  Not recommended.
  339.           Provided only for those who need to keep old scripts functioning.
  340.  
  341.    "I_KNOW_WHAT_I_AM_DOING"
  342.           Just shut up and do it.  Not recommended.
  343.           Provided for those who REALLY know what they are doing.
  344.  
  345. B<What this method does.>
  346. For an example, let's consider a valid email header you might get:
  347.  
  348.     From: =?US-ASCII?Q?Keith_Moore?= <moore@cs.utk.edu>
  349.     To: =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>
  350.     CC: =?ISO-8859-1?Q?Andr=E9_?= Pirard <PIRARD@vm1.ulg.ac.be>
  351.     Subject: =?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=
  352.      =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=
  353.      =?US-ASCII?Q?.._cool!?=
  354.  
  355. That basically decodes to (sorry, I can only approximate the
  356. Latin characters with 7 bit sequences /o and 'e):
  357.  
  358.     From: Keith Moore <moore@cs.utk.edu>
  359.     To: Keld J/orn Simonsen <keld@dkuug.dk>
  360.     CC: Andr'e  Pirard <PIRARD@vm1.ulg.ac.be>
  361.     Subject: If you can read this you understand the example... cool!
  362.  
  363. B<Note:> currently, the decodings are done without regard to the
  364. character set: thus, the Q-encoding C<=F8> is simply translated to the
  365. octet (hexadecimal C<F8>), period.  For piece-by-piece decoding
  366. of a given field, you want the array context of
  367. C<MIME::Word::decode_mimewords()>.
  368.  
  369. B<Warning:> the CRLF+SPACE separator that splits up long encoded words
  370. into shorter sequences (see the Subject: example above) gets lost
  371. when the field is unfolded, and so decoding after unfolding causes
  372. a spurious space to be left in the field.
  373. I<THEREFORE: if you're going to decode, do so BEFORE unfolding!>
  374.  
  375. This method returns the self object.
  376.  
  377. I<Thanks to Kent Boortz for providing the idea, and the baseline
  378. RFC-1522-decoding code.>
  379.  
  380. =cut
  381.  
  382. sub decode {
  383.     my $self = shift;
  384.  
  385.     ### Warn if necessary:
  386.     my $force = shift || 0;
  387.     unless (($force eq "I_NEED_TO_FIX_THIS") ||
  388.         ($force eq "I_KNOW_WHAT_I_AM_DOING")) {
  389.     usage "decode is deprecated for safety";
  390.     }
  391.  
  392.     my ($tag, $i, @decoded);
  393.     foreach $tag ($self->tags) {
  394.     @decoded = map { scalar(decode_mimewords($_, Field=>$tag))
  395.              } $self->get_all($tag);
  396.     for ($i = 0; $i < @decoded; $i++) {
  397.         $self->replace($tag, $decoded[$i], $i);
  398.     }
  399.     }
  400.     $self->{MH_Decoded} = 1;
  401.     $self;
  402. }
  403.  
  404. #------------------------------
  405.  
  406. =item delete TAG,[INDEX]
  407.  
  408. I<Instance method, inherited.>
  409. Delete all occurences of the field named TAG.
  410.  
  411.     ### Remove some MIME information:
  412.     $head->delete('MIME-Version');
  413.     $head->delete('Content-type');
  414.  
  415. =cut
  416.  
  417. ### Inherited
  418.  
  419.  
  420. #------------------------------
  421. #
  422. # exists
  423. #
  424. sub exists {   
  425.     usage "deprecated; use count() instead";
  426.     shift->count(@_);
  427. }
  428.  
  429. #------------------------------
  430. #
  431. # fields
  432. #
  433. sub fields {
  434.     usage "deprecated: use tags() instead",
  435.     shift->tags(@_);
  436. }
  437.  
  438. #------------------------------
  439.  
  440. =item get TAG,[INDEX]
  441.  
  442. I<Instance method, inherited.>
  443. Get the contents of field TAG.
  444.  
  445. If a B<numeric INDEX> is given, returns the occurence at that index,
  446. or undef if not present:
  447.  
  448.     ### Print the first and last 'Received:' entries (explicitly):
  449.     print "First, or most recent: ", $head->get('received', 0), "\n";
  450.     print "Last, or least recent: ", $head->get('received',-1), "\n";
  451.  
  452. If B<no INDEX> is given, but invoked in a B<scalar> context, then
  453. INDEX simply defaults to 0:
  454.  
  455.     ### Get the first 'Received:' entry (implicitly):
  456.     my $most_recent = $head->get('received');
  457.  
  458. If B<no INDEX> is given, and invoked in an B<array> context, then
  459. I<all> occurences of the field are returned:
  460.  
  461.     ### Get all 'Received:' entries:
  462.     my @all_received = $head->get('received');
  463.  
  464. =cut
  465.  
  466. ### Inherited.
  467.  
  468.  
  469. #------------------------------
  470.  
  471. =item get_all FIELD
  472.  
  473. I<Instance method.>
  474. Returns the list of I<all> occurences of the field, or the
  475. empty list if the field is not present:
  476.  
  477.     ### How did it get here?
  478.     @history = $head->get_all('Received');
  479.  
  480. B<Note:> I had originally experimented with having C<get()> return all
  481. occurences when invoked in an array context... but that causes a lot of
  482. accidents when you get careless and do stuff like this:
  483.  
  484.     print "\u$field: ", $head->get($field), "\n";
  485.  
  486. It also made the intuitive behaviour unclear if the INDEX argument
  487. was given in an array context.  So I opted for an explicit approach
  488. to asking for all occurences.
  489.  
  490. =cut
  491.  
  492. sub get_all {
  493.     my ($self, $tag) = @_;
  494.     $self->count($tag) or return ();          ### empty if doesn't exist
  495.     ($self->get($tag));
  496. }
  497.  
  498. #------------------------------
  499. #
  500. # original_text
  501. #
  502. # Instance method, DEPRECATED.  
  503. # Return an approximation of the original text.
  504. #    
  505. sub original_text {
  506.     usage "deprecated: use stringify() instead";
  507.     shift->stringify(@_);
  508. }
  509.  
  510. #------------------------------
  511.  
  512. =item print [OUTSTREAM]
  513.  
  514. I<Instance method, override.>
  515. Print the header out to the given OUTSTREAM, or the currently-selected
  516. filehandle if none.  The OUTSTREAM may be a filehandle, or any object
  517. that responds to a print() message.
  518.  
  519. The override actually lets you print to any object that responds to
  520. a print() method.  This is vital for outputting MIME entities to scalars.
  521.  
  522. Also, it defaults to the I<currently-selected> filehandle if none is given
  523. (not STDOUT!), so I<please> supply a filehandle to prevent confusion.
  524.  
  525. =cut
  526.  
  527. sub print {
  528.     my ($self, $fh) = @_;
  529.     $fh = wraphandle($fh || select);   ### get output handle, as a print()able
  530.     $fh->print($self->as_string);
  531. }
  532.  
  533. #------------------------------
  534. #
  535. # set TAG,TEXT
  536. #
  537. # Instance method, DEPRECATED.
  538. # Set the field named TAG to [the single occurence given by the TEXT.
  539. #
  540. sub set {
  541.     my $self = shift;
  542.     usage "deprecated: use the replace() method instead.";
  543.     $self->replace(@_);
  544. }
  545.  
  546. #------------------------------
  547.  
  548. =item stringify
  549.  
  550. I<Instance method.>
  551. Return the header as a string.  You can also invoke it as C<as_string>.
  552.  
  553. =cut
  554.  
  555. sub stringify {
  556.     my $self = shift;          ### build clean header, and output...
  557.     my @header = grep {defined($_) ? $_ : ()} @{$self->header};
  558.     join "", map { /\n$/ ? $_ : "$_\n" } @header;
  559. }
  560. sub as_string { shift->stringify(@_) }
  561.  
  562. #------------------------------
  563.  
  564. =item unfold [FIELD]
  565.  
  566. I<Instance method, inherited.>
  567. Unfold (remove newlines in) the text of all occurences of the given FIELD.
  568. If the FIELD is omitted, I<all> fields are unfolded.
  569. Returns the "self" object.
  570.  
  571. =cut
  572.  
  573. ### Inherited
  574.  
  575.  
  576. #------------------------------
  577.  
  578. =back
  579.  
  580. =head2 MIME-specific methods
  581.  
  582. All of the following methods extract information from the following fields:
  583.  
  584.     Content-type
  585.     Content-transfer-encoding
  586.     Content-disposition
  587.  
  588. Be aware that they do not just return the raw contents of those fields,
  589. and in some cases they will fill in sensible (I hope) default values.
  590. Use C<get()> or C<mime_attr()> if you need to grab and process the
  591. raw field text.
  592.  
  593. B<Note:> some of these methods are provided both as a convenience and
  594. for backwards-compatibility only, while others (like
  595. recommended_filename()) I<really do have to be in MIME::Head to work
  596. properly,> since they look for their value in more than one field.
  597. However, if you know that a value is restricted to a single
  598. field, you should really use the Mail::Field interface to get it.
  599.  
  600. =over 4
  601.  
  602. =cut
  603.  
  604. #------------------------------
  605.  
  606.  
  607. #------------------------------
  608. #
  609. # params TAG
  610. #
  611. # Instance method, DEPRECATED.
  612. # Extract parameter info from a structured field, and return
  613. # it as a hash reference.  Provided for 1.0 compatibility only!
  614. # Use the new MIME::Field interface classes (subclasses of Mail::Field).  
  615.  
  616. sub params {
  617.     my ($self, $tag) = @_;
  618.     usage "deprecated: use the MIME::Field interface classes from now on!";
  619.     return MIME::Field::ParamVal->parse_params($self->get($tag,0));     
  620. }
  621.  
  622. #------------------------------
  623.  
  624. =item mime_attr ATTR,[VALUE]
  625.  
  626. A quick-and-easy interface to set/get the attributes in structured
  627. MIME fields:
  628.  
  629.     $head->mime_attr("content-type"         => "text/html");
  630.     $head->mime_attr("content-type.charset" => "US-ASCII");
  631.     $head->mime_attr("content-type.name"    => "homepage.html");
  632.  
  633. This would cause the final output to look something like this:
  634.  
  635.     Content-type: text/html; charset=US-ASCII; name="homepage.html"
  636.  
  637. Note that the special empty sub-field tag indicates the anonymous
  638. first sub-field.
  639.  
  640. B<Giving VALUE as undefined> will cause the contents of the named subfield
  641. to be deleted:
  642.  
  643.     $head->mime_attr("content-type.charset" => undef);
  644.  
  645. B<Supplying no VALUE argument> just returns the attribute's value,
  646. or undefined if it isn't there:
  647.  
  648.     $type = $head->mime_attr("content-type");      ### text/html
  649.     $name = $head->mime_attr("content-type.name"); ### homepage.html
  650.  
  651. In all cases, the new/current value is returned.
  652.  
  653. =cut
  654.  
  655. sub mime_attr {
  656.     my ($self, $attr, $value) = @_;
  657.  
  658.     ### Break attribute name up:
  659.     my ($tag, $subtag) = split /\./, $attr;
  660.     $subtag ||= '_';
  661.  
  662.     ### Set or get?
  663.     my $field = MIME::Field::ParamVal->parse($self->get($tag, 0));
  664.     if (@_ > 2) {   ### set it:
  665.     $field->param($subtag, $value);             ### set subfield
  666.     $self->replace($tag, $field->stringify);    ### replace!
  667.     return $value;
  668.     }
  669.     else {          ### get it:
  670.     return $field->param($subtag);
  671.     }
  672. }
  673.  
  674. #------------------------------
  675.  
  676. =item mime_encoding
  677.  
  678. I<Instance method.>
  679. Try I<real hard> to determine the content transfer encoding
  680. (e.g., C<"base64">, C<"binary">), which is returned in all-lowercase.
  681.  
  682. If no encoding could be found, the default of C<"7bit"> is returned.
  683. I quote from RFC-1521 section 5:
  684.  
  685.     This is the default value -- that is, "Content-Transfer-Encoding: 7BIT"
  686.     is assumed if the Content-Transfer-Encoding header field is not present.
  687.  
  688. =cut
  689.  
  690. sub mime_encoding {
  691.     my $self = shift;
  692.     lc($self->mime_attr('content-transfer-encoding') || '7bit');
  693. }
  694.  
  695. #------------------------------
  696.  
  697. =item mime_type [DEFAULT]
  698.  
  699. I<Instance method.>
  700. Try C<real hard> to determine the content type (e.g., C<"text/plain">,
  701. C<"image/gif">, C<"x-weird-type">, which is returned in all-lowercase.
  702. "Real hard" means that if no content type could be found, the default
  703. (usually C<"text/plain">) is returned.  From RFC-1521 section 7.1:
  704.  
  705.     The default Content-Type for Internet mail is
  706.     "text/plain; charset=us-ascii".
  707.  
  708. Unless this is a part of a "multipart/digest", in which case
  709. "message/rfc822" is the default.  Note that you can also I<set> the
  710. default, but you shouldn't: normally only the MIME parser uses this
  711. feature.
  712.  
  713. =cut
  714.  
  715. sub mime_type {
  716.     my ($self, $default) = @_;
  717.     $self->{MIH_DefaultType} = $default if @_ > 1;
  718.     lc($self->mime_attr('content-type') || 
  719.        $self->{MIH_DefaultType} || 
  720.        'text/plain');
  721. }
  722.  
  723. #------------------------------
  724.  
  725. =item multipart_boundary
  726.  
  727. I<Instance method.>
  728. If this is a header for a multipart message, return the
  729. "encapsulation boundary" used to separate the parts.  The boundary
  730. is returned exactly as given in the C<Content-type:> field; that
  731. is, the leading double-hyphen (C<-->) is I<not> prepended.
  732.  
  733. Well, I<almost> exactly... this passage from RFC-1521 dictates
  734. that we remove any trailing spaces:
  735.  
  736.    If a boundary appears to end with white space, the white space
  737.    must be presumed to have been added by a gateway, and must be deleted.
  738.  
  739. Returns undef (B<not> the empty string) if either the message is not
  740. multipart, if there is no specified boundary, or if the boundary is
  741. illegal (e.g., if it is empty after all trailing whitespace has been
  742. removed).
  743.  
  744. =cut
  745.  
  746. sub multipart_boundary {
  747.     my $self = shift;
  748.     my $value =  $self->mime_attr('content-type.boundary');
  749.     (!defined($value) or $value eq '') ? undef : $value;
  750. }
  751.  
  752. #------------------------------
  753.  
  754. =item recommended_filename
  755.  
  756. I<Instance method.>
  757. Return the recommended external filename.  This is used when
  758. extracting the data from the MIME stream.
  759.  
  760. Returns undef if no filename could be suggested.
  761.  
  762. =cut
  763.  
  764. sub recommended_filename {
  765.     my $self = shift;
  766.     my $value;
  767.  
  768.     ### Start by trying to get 'filename' from the 'content-disposition':
  769.     $value = $self->mime_attr('content-disposition.filename');
  770.     return $value if (defined($value) and $value ne '');
  771.  
  772.     ### No?  Okay, try to get 'name' from the 'content-type':
  773.     $value = $self->mime_attr('content-type.name');
  774.     return $value if (defined($value) and $value ne '');
  775.  
  776.     ### Sorry:
  777.     undef;
  778. }
  779.  
  780. #------------------------------
  781.  
  782. =back
  783.  
  784. =cut
  785.  
  786.  
  787. #------------------------------
  788. #
  789. # tweak_FROM_parsing
  790. #
  791. # DEPRECATED.  Use the inherited mail_from() class method now.
  792.  
  793. sub tweak_FROM_parsing {
  794.     my $self = shift;
  795.     usage "deprecated.  Use mail_from() instead.";
  796.     $self->mail_from(@_);
  797. }
  798.  
  799.  
  800. __END__
  801.  
  802. #------------------------------
  803.  
  804.  
  805. =head1 NOTES
  806.  
  807. =over 4
  808.  
  809. =item Why have separate objects for the entity, head, and body?
  810.  
  811. See the documentation for the MIME-tools distribution
  812. for the rationale behind this decision.
  813.  
  814.  
  815. =item Why assume that MIME headers are email headers?
  816.  
  817. I quote from Achim Bohnet, who gave feedback on v.1.9 (I think
  818. he's using the word "header" where I would use "field"; e.g.,
  819. to refer to "Subject:", "Content-type:", etc.):
  820.  
  821.     There is also IMHO no requirement [for] MIME::Heads to look
  822.     like [email] headers; so to speak, the MIME::Head [simply stores]
  823.     the attributes of a complex object, e.g.:
  824.  
  825.         new MIME::Head type => "text/plain",
  826.                        charset => ...,
  827.                        disposition => ..., ... ;
  828.  
  829. I agree in principle, but (alas and dammit) RFC-1521 says otherwise.
  830. RFC-1521 [MIME] headers are a syntactic subset of RFC-822 [email] headers.
  831. Perhaps a better name for these modules would be RFC1521:: instead of
  832. MIME::, but we're a little beyond that stage now.
  833.  
  834. In my mind's eye, I see an abstract class, call it MIME::Attrs, which does
  835. what Achim suggests... so you could say:
  836.  
  837.      my $attrs = new MIME::Attrs type => "text/plain",
  838.                  charset => ...,
  839.                                  disposition => ..., ... ;
  840.  
  841. We could even make it a superclass of MIME::Head: that way, MIME::Head
  842. would have to implement its interface, I<and> allow itself to be
  843. initiallized from a MIME::Attrs object.
  844.  
  845. However, when you read RFC-1521, you begin to see how much MIME information
  846. is organized by its presence in particular fields.  I imagine that we'd
  847. begin to mirror the structure of RFC-1521 fields and subfields to such
  848. a degree that this might not give us a tremendous gain over just
  849. having MIME::Head.
  850.  
  851.  
  852. =item Why all this "occurence" and "index" jazz?  Isn't every field unique?
  853.  
  854. Aaaaaaaaaahh....no.
  855.  
  856. Looking at a typical mail message header, it is sooooooo tempting to just
  857. store the fields as a hash of strings, one string per hash entry.
  858. Unfortunately, there's the little matter of the C<Received:> field,
  859. which (unlike C<From:>, C<To:>, etc.) will often have multiple
  860. occurences; e.g.:
  861.  
  862.     Received: from gsfc.nasa.gov by eryq.pr.mcs.net  with smtp
  863.         (Linux Smail3.1.28.1 #5) id m0tStZ7-0007X4C;
  864.      Thu, 21 Dec 95 16:34 CST
  865.     Received: from rhine.gsfc.nasa.gov by gsfc.nasa.gov
  866.      (5.65/Ultrix3.0-C) id AA13596;
  867.      Thu, 21 Dec 95 17:20:38 -0500
  868.     Received: (from eryq@localhost) by rhine.gsfc.nasa.gov
  869.      (8.6.12/8.6.12) id RAA28069;
  870.      Thu, 21 Dec 1995 17:27:54 -0500
  871.     Date: Thu, 21 Dec 1995 17:27:54 -0500
  872.     From: Eryq <eryq@rhine.gsfc.nasa.gov>
  873.     Message-Id: <199512212227.RAA28069@rhine.gsfc.nasa.gov>
  874.     To: eryq@eryq.pr.mcs.net
  875.     Subject: Stuff and things
  876.  
  877. The C<Received:> field is used for tracing message routes, and although
  878. it's not generally used for anything other than human debugging, I
  879. didn't want to inconvenience anyone who actually wanted to get at that
  880. information.
  881.  
  882. I also didn't want to make this a special case; after all, who
  883. knows what other fields could have multiple occurences in the
  884. future?  So, clearly, multiple entries had to somehow be stored
  885. multiple times... and the different occurences had to be retrievable.
  886.  
  887. =back
  888.  
  889.  
  890. =head1 AUTHOR
  891.  
  892. Eryq (F<eryq@zeegee.com>), ZeeGee Software Inc (F<http://www.zeegee.com>).
  893.  
  894. All rights reserved.  This program is free software; you can redistribute
  895. it and/or modify it under the same terms as Perl itself.
  896.  
  897. The more-comprehensive filename extraction is courtesy of
  898. Lee E. Brotzman, Advanced Data Solutions.
  899.  
  900.  
  901. =head1 VERSION
  902.  
  903. $Revision: 5.403 $ $Date: 2000/11/04 19:54:46 $
  904.  
  905. =cut
  906.  
  907. 1;
  908.