
=head1 NAME

Mail::Message::Field::Full - one line of a message header

=head1 CLASS INHERITANCE

 Mail::Message::Field::Full
   is a Mail::Message::Field
   is a Mail::Reporter

 Mail::Message::Field::Full is extended by
   Mail::Message::Field::Addresses
   Mail::Message::Field::Unstructured

=head1 SYNOPSIS

 !! THE IMPLEMENTATION OF THIS MODULE IS NOT COMPLETELY FINISHED YET!!

 # Getting to understand the complexity of a header field ...

 my $fast = $msg->head->get('subject');
 my $full = Mail::Message::Field::Full->from($fast);

 my $full = $msg->head->get('subject')->study;  # same
 my $full = $msg->head->study('subject');       # same
 my $full = $msg->get('subject');               # same

 # ... or build a complex header field yourself

 my @encode = (charset => 'jp', use_continuations => 1);

 my $f = Mail::Message::Field::Full->new('Content-Type' => 'text/html');
 $f->addExtra(" and more", @encode);

 $f->addPhrase("text ", @encode);
 $f->addPhrase(" and more text", language => 'en-GB' );

 $f->addAttribute('filename=passwd');
 $f->addAttribute(filename => 'passwd', @encode);

 my $attr = Mail::Message::Field::Attribute->new(...);
 $f->addAttribute($attr);

 $f->addComment('just me', $encode);

=head1 DESCRIPTION

This is the full implementation of a header field: it will be quite slow,
because header fields can be very complex.  Of course, this class delivers
the optimal result, but for a quite large penalty in performance and
memory consumption.

This class supports the common header description from RFC2822 (formerly
RFC822), the extensions with respect to character-set encodings as specified
in RFC2047, and the extensions on language specification and long parameter
wrapping from RFC2231.  If you do not need the latter two, then the
Mail::Message::Field::Fast and Mail::Message::Field::Flex are enough for your
application.

=head1 METHODS

=head2 Initiation

=over 4

=item B<from> FIELD, OPTIONS

(Class method) 
Convert any FIELD (a Mail::Message::Field object) into a new
Mail::Message::Field::Full object.  This conversion is done the hard
way: the string which is produced by the original object is parsed
again.  Usually, the string which is parsed is exactly the line (or lines)
as found in the original input source, which is a good thing because Full
fields are much more carefull with the actual content.

OPTIONS are passed to the constructor (see new()).  In any case, some
extensions of this Full field class is returned.  It depends on which
field is created what kind of class we get.

Examples:

 my $fast = $msg->head->get('subject');
 my $full = Mail::Message::Field::Full->from($fast);

 my $full = $msg->head->get('subject')->study;  # same
 my $full = $msg->head->study('subject');       # same
 my $full = $msg->get('subject');               # same

=item B<new> DATA

(Class method) 
Creating a new field object the correct way is a lot of work, because
there is so much freedom in the RFCs, but at the same time so many
restrictions.  Most fields are implemented, but if you have your own
field (and do no want to contribute it to Mail::Box), then simply call
new on your own package.

You have the choice to instantiate the object as string or in prepared
parts:

=over 4

=item * B<new> LINE

=item * B<new> NAME, BODY, [ATTRIBUTES], OPTIONS

=back

The NAME is a wellformed header name (you may use wellformedName()) to
be sure about the casing.  The BODY is a string, one object, or an
ref-array of objects.  In case of objects, they must fit to the
constructor of the field: the types which are accepted may differ.
The optional ATTRIBUTE list contains Mail::Message::Field::Attribute
objects.  Finally, there are some OPTIONS.

 OPTION               DEFAULT
 attributes           []
 charset              C<undef>
 encoding             'q'
 extra                undef
 force                false
 is_structured        C<depends>
 language             C<undef>
 log                  'WARNINGS'
 trace                'WARNINGS'

=over 4

=item B<attributes> =E<gt> ATTRS

There are various ways to specify these attributes: pass a reference
to an array which list of key-value pairs representing attributes,
or reference to a hash containing these pairs, or an array with
Mail::Message::Field::Attribute objects.

=item B<charset> =E<gt> STRING

The body is specified in utf8, and must become 7bits ascii to be
transmited.  Specify a charset to which the multi-byte
utf8 is converted before it gets encoded.  See encode(), which does the
job.

=item B<encoding> =E<gt> 'q'|'Q'|'b'|'B'

Non-ascii characters are encoded using Quoted-Printable ('q' or 'Q') or
Base64 ('b' or 'B') encoding.

=item B<extra> =E<gt> STRING

Text which is appended after the line (preceded by a semicolon).

=item B<force> =E<gt> BOOLEAN

Enforce encoding in the specified charset, even when it is not needed
because the body does not contain any non-ascii characters.

=item B<is_structured> =E<gt> BOOLEAN

If the name of the field is known, than the internals know whether the field
is structured or not.  If you call the constructor on your own class which is
derived from Mail::Message::Field::Full, the default is C<true>.  If you have
no own implementation for an unknown field, the boolean is considered C<false>.

For fields which are not known, C<true> means that a
Mail::Message::Field::Structured will be created.  The C<false> value will
create a Mail::Message::Field::Unstructured.

=item B<language> =E<gt> STRING

The language used can be specified, however is rarely used my mail clients.

=item B<log> =E<gt> LEVEL

See Mail::Reporter::new(log)

=item B<trace> =E<gt> LEVEL

See Mail::Reporter::new(trace)

=back

Examples:

 my $s = Mail::Message::Field::Full->new('Subject: Hello World');
 my $s = Mail::Message::Field::Full->new('Subject', 'Hello World');

 my @attrs   = (Mail::Message::Field::Attribute->new(...), ...);
 my @options = (extra => 'the color blue');
 my $t = Mail::Message::Field::Full->new(To => \@addrs, @attrs, @options);

=back

=head2 The Field

=over 4

=item B<addAttribute> OBJECT|(STRING, OPTIONS)|(NAME,VALUE,OPTIONS)

Add an attribute to the field.  The attributes are added left-to-right into
the string representation of the field, although the order of the attributes
is un-important, according to the RFCs.

You may pass a fully prepared Mail::Message::Field::Attribute OBJECT, if you
like to do all preparations for correct representation of the data yourself.
You may also pass one STRING, which is a fully prepared attribute.  This
STRING will not be changed, so be careful about quoting and encodings.

As third possibility, you can specify an attribute NAME and its VALUE.  An
attribute object will be created for you implicitly in both cases where such
object is not supplied, passing the OPTIONS.
See Mail::Message::Field::Attributes::new() about the available OPTIONS.

The attribute object is returned, however, when continuations are used this
may be an object you already know about.  C<undef> is returned when
construction fails (when the attribute is incorrect).

Examples:

 $f->addAttribute('filename=passwd');
 $f->addAttribute(filename => 'passwd', use_continuations => 0);
 my $attr = Mail::Message::Field::Attribute->new(...);
 $f->addAttribute($attr);

=item B<addComment> COMMENT, OPTIONS

Creates a comment (see createComment()) and adds it immediately to
this field.  Empty or undefined COMMENTs are ignored.  The created comment
is returned.

=item B<addExtra> STRING

Adds a string to the line, which is not an attribute however does start with
a semi-colon.  Empty or undefined STRINGs are ignored.

=item B<addPhrase> STRING, OPTIONS

Create a phrase (see createPhrase()) and immediately add it to this field.
Empty or undefined values of STRING are ignored.  The created phrase is
returned.

=item B<attributes> 

Returns a list with all attributes, which are all Mail::Message::Field::Attribute
objects.  The attributes are not ordered in any way.  The list may be empty.
Double attributes or continuations are folded into one.

=item B<clone> 

See Mail::Message::Field::clone()

=item B<createComment> STRING, OPTIONS

(Class or Instance method) 
Create a comment to become part in a field.  Comments are automatically
included within parenthesis.  Matching pairs of parenthesis are
permitted within the STRING.  When a non-matching parenthesis are used,
it is only permitted with an escape (a backslash) in front of them.
These backslashes will be added automatically if needed (don't worry!).
Backslashes will stay, except at the end, where it will be doubled.

The OPTIONS are C<charset>, C<language>, and C<encoding> as always.
See addComment().  The created comment is returned.

=item B<createPhrase> STRING, OPTIONS

(Class or Instance method) 
A phrase is a text which plays a well defined role.  This is the main difference
with comments, which have do specified meaning.  Some special characters
in the phrase will cause it to be surrounded with double quotes: do not specify
them yourself.

The OPTIONS are C<charset>, C<language>, and C<encoding> as always.
See addPhrase().

=item B<isStructured> 

See Mail::Message::Field::isStructured()

=item B<length> 

See Mail::Message::Field::length()

=back

=head2 Access to the Field

=over 4

=item B<Name> 

See Mail::Message::Field::Name()

=item B<addresses> 

See Mail::Message::Field::addresses()

=item B<attribute> NAME [, VALUE]

See Mail::Message::Field::attribute()

=item B<body> 

See Mail::Message::Field::body()

=item B<comment> [STRING]

See Mail::Message::Field::comment()

=item B<dateToTimestamp> STRING

See Mail::Message::Field::dateToTimestamp()

=item B<decodedBody> OPTIONS

Returns the unfolded body of the field, where encodings are resolved.  The
returned line will still contain comments and such.  The OPTIONS are passed
to the decoder, see decode().

BE WARNED: if the field is a structured field, the content may change syntax,
because of encapsulated special characters.  By default, the body is decoded
as text, which results in a small difference within comments as well
(read the RFC).

=item B<folded> 

See Mail::Message::Field::folded()

=item B<foldedBody> [BODY]

See Mail::Message::Field::foldedBody()

=item B<name> 

See Mail::Message::Field::name()

=item B<nrLines> 

See Mail::Message::Field::nrLines()

=item B<print> [FILEHANDLE]

See Mail::Message::Field::print()

=item B<size> 

See Mail::Message::Field::size()

=item B<string> [WRAP]

See Mail::Message::Field::string()

=item B<stripCFWS> [STRING]

See Mail::Message::Field::stripCFWS()

=item B<toDate> [TIME]

See Mail::Message::Field::toDate()

=item B<toDisclose> 

See Mail::Message::Field::toDisclose()

=item B<toInt> 

See Mail::Message::Field::toInt()

=item B<unfoldedBody> [BODY, [WRAP]]

See Mail::Message::Field::unfoldedBody()

=item B<wellformedName> [STRING]

See Mail::Message::Field::wellformedName()

=back

=head2 Reading and Writing [internals]

=over 4

=item B<consume> LINE | (NAME,BODY|OBJECTS)

See Mail::Message::Field::consume()

=item B<consumeComment> STRING

(Class or Instance method) 
Try to read a comment from the STRING.  When successful, the comment
without encapsulation parenthesis is returned, together with the rest
of the string.

=item B<consumeDotAtom> STRING

Returns three elemens: the atom-text, the rest string, and the
concatenated comments.  Both atom and comments can be undef.

=item B<consumePhrase> STRING

(Class or Instance method) 
Take the STRING, and try to strip-off a valid phrase.  In the obsolete
phrase syntax, any sequence of words is accepted as phrase (as long as
certain special characters are not used).  RFC2882 is stricter: only
one word or a quoted string is allowed.  As always, the obsolete
syntax is accepted, and the new syntax is produced.

This method returns two elements: the phrase (or undef) followed
by the resulting string.  The phrase will be removed from the optional
quotes.  Be warned that C<""> will return an empty, valid phrase.

Examples:

 my ($phrase, $rest) = $field->consumePhrase( q["hi!" <sales@example.com>] );

=item B<decode> STRING, OPTIONS

(Class or Instance method) 
Decode field encoded STRING to an utf8 string.  The input STRING is part of
a header field, and as such, may contain encoded words in C<=?...?.?...?=>
format defined by RFC2047.  The STRING may contain multiple encoded parts,
maybe using different character sets.

Be warned:  you MUST first interpret the field into parts, like phrases and
comments, and then decode each part separately, otherwise the decoded text
may interfere with your markup characters.

Be warned: language information, which is defined in RFC2231, is ignored.

 OPTION               DEFAULT
 is_text              => 1

=over 4

=item B<is_text> =E<gt> => BOOLEAN

Encoding on text is slightly more complicated than encoding structured data,
because it contains blanks.  Visible blanks have to be ignored between two
encoded words in the text, but not when an encoded word follows or preceeds
an unencoded word.  Phrases and comments are texts.

=back

Examples:

   print Mail::Message::Field::Full->decode('=?iso-8859-1?Q?J=F8rgen?=');
      # prints   JE<0slash>rgen

=item B<defaultWrapLength> [LENGTH]

See Mail::Message::Field::defaultWrapLength()

=item B<encode> STRING, OPTIONS

Encode the (possibly utf8 encoded) STRING to a string which is acceptable
to the RFC2047 definition of a header: only containing us-ascii characters.

 OPTION               DEFAULT
 charset              'us-ascii'
 encoding             'q'
 force                0
 language             C<undef>

=over 4

=item B<charset> =E<gt> STRING

STRING is an utf8 string which has to be translated into any byte-wise
character set for transport, because MIME-headers can only contain ascii
characters.

=item B<encoding> =E<gt> 'q'|'Q'|'b'|'B'

The character encoding to be used.  With C<q> or C<Q>, quoted-printable
encoding will be used.  With C<b> or C<B>, base64 encoding will be taken.

=item B<force> =E<gt> BOOLEAN

Encode the string, even when it only contains us-ascii characters.  By
default, this is off because it decreases readibility of the produced
header fields.

=item B<language> =E<gt> STRING

RFC2231 defines how to specify language encodings in encoded words.  The
STRING is a strandard iso language name.

=back

=item B<fold> NAME, BODY, [MAXCHARS]

See Mail::Message::Field::fold()

=item B<setWrapLength> [LENGTH]

See Mail::Message::Field::setWrapLength()

=item B<unfold> STRING

See Mail::Message::Field::unfold()

=back

=head2 Logging and Tracing

=over 4

=item B<defaultTrace> [LEVEL, [LEVEL]

See Mail::Reporter::defaultTrace()

=item B<errors> 

See Mail::Reporter::errors()

=item B<log> [LEVEL [,STRINGS]]

See Mail::Reporter::log()

=item B<report> [LEVEL]

See Mail::Reporter::report()

=item B<reportAll> [LEVEL]

See Mail::Reporter::reportAll()

=item B<trace> [LEVEL]

See Mail::Reporter::trace()

=item B<warnings> 

See Mail::Reporter::warnings()

=back

=head2 Other Methods

=over 4

=item B<AUTOLOAD> 

See Mail::Reporter::AUTOLOAD()

=item B<DESTROY> 

See Mail::Reporter::DESTROY()

=item B<inGlobalDestruction> 

See Mail::Reporter::inGlobalDestruction()

=item B<logPriority> LEVEL

See Mail::Reporter::logPriority()

=item B<logSettings> 

See Mail::Reporter::logSettings()

=item B<notImplemented> 

See Mail::Reporter::notImplemented()

=back

=head1 SEE ALSO

A good start to read is Mail::Box-Overview.
More documentation and a mailinglist are available from the project's
website at L<http://perl.overmeer.net/mailbox/>.

=head1 AUTHOR

Written by Mark Overmeer (L<mark@overmeer.net|mailto:mark@overmeer.net>)
with the help of many.  See the ChangeLog for details.

=head1 VERSION

This code is beta, version 2.037.

Copyright (c) 2001-2003 by the authors. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
