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