1 |
wakaba |
1.1 |
|
2 |
|
|
=head1 NAME
|
3 |
|
|
|
4 |
wakaba |
1.5 |
Message::Field::Structured -- Perl module for
|
5 |
|
|
structured header field bodies of the Internet message
|
6 |
wakaba |
1.1 |
|
7 |
|
|
=cut
|
8 |
|
|
|
9 |
|
|
package Message::Field::Structured;
|
10 |
|
|
use strict;
|
11 |
wakaba |
1.5 |
use vars qw($VERSION);
|
12 |
|
|
$VERSION=do{my @r=(q$Revision: 1.4 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
|
13 |
wakaba |
1.3 |
require Message::Util;
|
14 |
wakaba |
1.5 |
use overload '""' => sub { $_[0]->stringify },
|
15 |
|
|
'.=' => sub { $_[0]->value_append ($_[1]) },
|
16 |
|
|
'eq' => sub { $_[0]->{field_body} eq $_[1] },
|
17 |
|
|
'ne' => sub { $_[0]->{field_body} ne $_[1] },
|
18 |
|
|
fallback => 1;
|
19 |
wakaba |
1.1 |
|
20 |
wakaba |
1.5 |
=head1 CONSTRUCTORS
|
21 |
wakaba |
1.1 |
|
22 |
wakaba |
1.5 |
The following methods construct new C<Message::Field::Structured> objects:
|
23 |
wakaba |
1.1 |
|
24 |
wakaba |
1.5 |
=over 4
|
25 |
wakaba |
1.1 |
|
26 |
wakaba |
1.5 |
=cut
|
27 |
wakaba |
1.1 |
|
28 |
wakaba |
1.5 |
## Initialize of this class -- called by constructors
|
29 |
|
|
sub _init ($;%) {
|
30 |
|
|
my $self = shift;
|
31 |
|
|
my %options = @_;
|
32 |
|
|
$self->{option} = {
|
33 |
wakaba |
1.3 |
encoding_after_encode => '*default',
|
34 |
|
|
encoding_before_decode => '*default',
|
35 |
|
|
hook_encode_string => #sub {shift; (value => shift, @_)},
|
36 |
|
|
\&Message::Util::encode_header_string,
|
37 |
|
|
hook_decode_string => #sub {shift; (value => shift, @_)},
|
38 |
|
|
\&Message::Util::decode_header_string,
|
39 |
wakaba |
1.5 |
};
|
40 |
|
|
$self->{field_body} = '';
|
41 |
|
|
|
42 |
|
|
for my $name (keys %options) {
|
43 |
|
|
if (substr ($name, 0, 1) eq '-') {
|
44 |
|
|
$self->{option}->{substr ($name, 1)} = $options{$name};
|
45 |
|
|
} elsif (lc $name eq 'body') {
|
46 |
|
|
$self->{field_body} = $options{$name};
|
47 |
|
|
}
|
48 |
|
|
}
|
49 |
|
|
}
|
50 |
wakaba |
1.3 |
|
51 |
wakaba |
1.5 |
=item Message::Field::Structured->new ([%options])
|
52 |
wakaba |
1.1 |
|
53 |
wakaba |
1.5 |
Constructs a new C<Message::Field::Structured> object. You might pass some
|
54 |
|
|
options as parameters to the constructor.
|
55 |
wakaba |
1.1 |
|
56 |
|
|
=cut
|
57 |
|
|
|
58 |
wakaba |
1.2 |
sub new ($;%) {
|
59 |
wakaba |
1.3 |
my $class = shift;
|
60 |
wakaba |
1.5 |
my $self = bless {}, $class;
|
61 |
|
|
$self->_init (@_);
|
62 |
wakaba |
1.3 |
$self;
|
63 |
wakaba |
1.1 |
}
|
64 |
|
|
|
65 |
wakaba |
1.5 |
=item Message::Field::Structured->parse ($field-body, [%options])
|
66 |
wakaba |
1.1 |
|
67 |
wakaba |
1.5 |
Constructs a new C<Message::Field::Structured> object with
|
68 |
|
|
given field body. You might pass some options as parameters to the constructor.
|
69 |
wakaba |
1.1 |
|
70 |
|
|
=cut
|
71 |
|
|
|
72 |
wakaba |
1.2 |
sub parse ($$;%) {
|
73 |
wakaba |
1.3 |
my $class = shift;
|
74 |
wakaba |
1.5 |
my $self = bless {}, $class;
|
75 |
|
|
$self->_init (@_);
|
76 |
|
|
#my $field_body = $self->Message::Util::decode_qcontent (shift);
|
77 |
|
|
$self->{field_body} = shift; #$field_body;
|
78 |
wakaba |
1.1 |
$self;
|
79 |
|
|
}
|
80 |
|
|
|
81 |
wakaba |
1.5 |
=back
|
82 |
|
|
|
83 |
|
|
=head1 METHODS
|
84 |
|
|
|
85 |
|
|
=over 4
|
86 |
|
|
|
87 |
|
|
=item $self->stringify ([%options])
|
88 |
wakaba |
1.1 |
|
89 |
wakaba |
1.5 |
Returns field body as a string. Returned string is encoded,
|
90 |
|
|
quoted if necessary (by C<hook_encode_string>).
|
91 |
wakaba |
1.1 |
|
92 |
|
|
=cut
|
93 |
|
|
|
94 |
|
|
sub stringify ($) {
|
95 |
|
|
my $self = shift;
|
96 |
wakaba |
1.5 |
#$self->Message::Util::encode_qcontent ($self->{field_body});
|
97 |
|
|
$self->{field_body};
|
98 |
wakaba |
1.1 |
}
|
99 |
wakaba |
1.5 |
*as_string = \&stringify;
|
100 |
wakaba |
1.1 |
|
101 |
wakaba |
1.5 |
=item $self->as_plain_string
|
102 |
wakaba |
1.1 |
|
103 |
wakaba |
1.5 |
Returns field body as a string. Returned string is not encoded
|
104 |
|
|
or quoted, i.e. internal/bare coded string. This string
|
105 |
|
|
may be unable to use as field body content. (Its I<structures>
|
106 |
|
|
such as C<comment> and C<quoted-string> are lost.)
|
107 |
wakaba |
1.1 |
|
108 |
|
|
=cut
|
109 |
|
|
|
110 |
|
|
sub as_plain_string ($) {
|
111 |
|
|
my $self = shift;
|
112 |
wakaba |
1.5 |
my $s = $self->Message::Util::decode_qcontent ($self->{field_body});
|
113 |
|
|
Message::Util::unquote_quoted_string (Message::Util::unquote_ccontent ($s));
|
114 |
wakaba |
1.1 |
}
|
115 |
wakaba |
1.4 |
|
116 |
wakaba |
1.5 |
=item $self->option ( $option-name / $option-name, $option-value, ...)
|
117 |
wakaba |
1.4 |
|
118 |
wakaba |
1.5 |
If @_ == 1, returns option value. Else...
|
119 |
wakaba |
1.4 |
|
120 |
wakaba |
1.5 |
Set option value. You can pass multiple option name-value pair
|
121 |
|
|
as parameter. Example:
|
122 |
wakaba |
1.1 |
|
123 |
wakaba |
1.5 |
$msg->option (-format => 'mail-rfc822',
|
124 |
|
|
-capitalize => 0);
|
125 |
|
|
print $msg->option ('-format'); ## mail-rfc822
|
126 |
wakaba |
1.3 |
|
127 |
wakaba |
1.5 |
Note that introduction character, i.e. C<-> (HYPHEN-MINUS)
|
128 |
|
|
is optional. You can also write as this:
|
129 |
wakaba |
1.3 |
|
130 |
wakaba |
1.5 |
$msg->option (format => 'mail-rfc822',
|
131 |
|
|
capitalize => 0);
|
132 |
|
|
print $msg->option ('format'); ## mail-rfc822
|
133 |
wakaba |
1.1 |
|
134 |
|
|
=cut
|
135 |
|
|
|
136 |
wakaba |
1.5 |
sub option ($@) {
|
137 |
wakaba |
1.1 |
my $self = shift;
|
138 |
wakaba |
1.5 |
if (@_ == 1) {
|
139 |
|
|
return $self->{option}->{ $_[0] };
|
140 |
|
|
}
|
141 |
|
|
while (my ($name, $value) = splice (@_, 0, 2)) {
|
142 |
|
|
$name =~ s/^-//;
|
143 |
|
|
$self->{option}->{$name} = $value;
|
144 |
|
|
}
|
145 |
wakaba |
1.1 |
}
|
146 |
|
|
|
147 |
wakaba |
1.5 |
=item $self->clone ()
|
148 |
wakaba |
1.1 |
|
149 |
wakaba |
1.5 |
Returns a copy of Message::Field::Structured object.
|
150 |
wakaba |
1.1 |
|
151 |
|
|
=cut
|
152 |
|
|
|
153 |
wakaba |
1.5 |
sub clone ($) {
|
154 |
wakaba |
1.1 |
my $self = shift;
|
155 |
wakaba |
1.5 |
my $clone = ref($self)->new;
|
156 |
|
|
for my $name (%{$self->{option}}) {
|
157 |
|
|
if (ref $self->{option}->{$name} eq 'HASH') {
|
158 |
|
|
$clone->{option}->{$name} = {%{$self->{option}->{$name}}};
|
159 |
|
|
} elsif (ref $self->{option}->{$name} eq 'ARRAY') {
|
160 |
|
|
$clone->{option}->{$name} = [@{$self->{option}->{$name}}];
|
161 |
|
|
} else {
|
162 |
|
|
$clone->{option}->{$name} = $self->{option}->{$name};
|
163 |
|
|
}
|
164 |
|
|
}
|
165 |
|
|
$clone->{field_body} = ref $self->{field_body}?
|
166 |
|
|
$self->{field_body}->clone:
|
167 |
|
|
$self->{field_body};
|
168 |
|
|
## Common hash value (not used in this module)
|
169 |
|
|
$clone->{value} = ref $self->{value}?
|
170 |
|
|
$self->{value}->clone:
|
171 |
|
|
$self->{value};
|
172 |
|
|
for my $i (@{$self->{comment}}) {
|
173 |
|
|
if (ref $self->{comment}->[$i] eq 'HASH') {
|
174 |
|
|
$clone->{comment}->[$i] = {%{$self->{comment}->[$i]}};
|
175 |
|
|
} elsif (ref $self->{comment}->[$i] eq 'ARRAY') {
|
176 |
|
|
$clone->{comment}->[$i] = [@{$self->{comment}->[$i]}];
|
177 |
|
|
} else {
|
178 |
|
|
$clone->{comment}->[$i] = $self->{comment}->[$i];
|
179 |
|
|
}
|
180 |
|
|
}
|
181 |
|
|
$clone;
|
182 |
wakaba |
1.1 |
}
|
183 |
|
|
|
184 |
wakaba |
1.5 |
|
185 |
wakaba |
1.1 |
=head1 EXAMPLE
|
186 |
|
|
|
187 |
|
|
use Message::Field::Structured;
|
188 |
|
|
|
189 |
|
|
my $field_body = '"This is an example of <\"> (quotation mark)."
|
190 |
|
|
(Comment within \q\u\o\t\e\d\-\p\a\i\r\(\s\))';
|
191 |
|
|
my $field = Message::Field::Structured->parse ($field_body);
|
192 |
|
|
|
193 |
|
|
print $field->as_plain_string;
|
194 |
|
|
|
195 |
wakaba |
1.5 |
=head1 SEE ALSO
|
196 |
|
|
|
197 |
|
|
=over 4
|
198 |
|
|
|
199 |
|
|
=item L<Message::Entity>, L<Message::Header>
|
200 |
|
|
|
201 |
|
|
=item L<Message::Field::Unstructured>
|
202 |
|
|
|
203 |
|
|
=item RFC 2822 E<lt>urn:ietf:rfc:2822E<gt>, usefor-article, HTTP/1.0, HTTP/1.1
|
204 |
|
|
|
205 |
|
|
=back
|
206 |
|
|
|
207 |
wakaba |
1.1 |
=head1 LICENSE
|
208 |
|
|
|
209 |
|
|
Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
|
210 |
|
|
|
211 |
|
|
This program is free software; you can redistribute it and/or modify
|
212 |
|
|
it under the terms of the GNU General Public License as published by
|
213 |
|
|
the Free Software Foundation; either version 2 of the License, or
|
214 |
|
|
(at your option) any later version.
|
215 |
|
|
|
216 |
|
|
This program is distributed in the hope that it will be useful,
|
217 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
218 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
219 |
|
|
GNU General Public License for more details.
|
220 |
|
|
|
221 |
|
|
You should have received a copy of the GNU General Public License
|
222 |
|
|
along with this program; see the file COPYING. If not, write to
|
223 |
|
|
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
224 |
|
|
Boston, MA 02111-1307, USA.
|
225 |
|
|
|
226 |
|
|
=head1 CHANGE
|
227 |
|
|
|
228 |
|
|
See F<ChangeLog>.
|
229 |
wakaba |
1.5 |
$Date: $
|
230 |
wakaba |
1.1 |
|
231 |
|
|
=cut
|
232 |
|
|
|
233 |
|
|
1;
|