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