1 |
wakaba |
1.1 |
|
2 |
|
|
=head1 NAME
|
3 |
|
|
|
4 |
|
|
Message::Field::Structured Perl module
|
5 |
|
|
|
6 |
|
|
=head1 DESCRIPTION
|
7 |
|
|
|
8 |
|
|
Perl module for RFC 822/2822 structured C<field>s.
|
9 |
|
|
|
10 |
|
|
=cut
|
11 |
|
|
|
12 |
|
|
package Message::Field::Structured;
|
13 |
|
|
require 5.6.0;
|
14 |
|
|
use strict;
|
15 |
|
|
use re 'eval';
|
16 |
wakaba |
1.3 |
use vars qw(%DEFAULT %REG $VERSION);
|
17 |
|
|
$VERSION=do{my @r=(q$Revision: 1.3 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
|
18 |
|
|
require Message::Util;
|
19 |
wakaba |
1.1 |
|
20 |
|
|
use overload '""' => sub {shift->stringify};
|
21 |
|
|
|
22 |
|
|
$REG{comment} = qr/\x28(?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x27\x2A-\x5B\x5D-\xFF]|(??{$REG{comment}}))*\x29/;
|
23 |
|
|
$REG{quoted_string} = qr/\x22(?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x21\x23-\x5B\x5D-\xFF])*\x22/;
|
24 |
|
|
$REG{domain_literal} = qr/\x5B(?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x5A\x5E-\xFF])*\x5D/;
|
25 |
|
|
|
26 |
|
|
$REG{WSP} = qr/[\x20\x09]+/;
|
27 |
|
|
$REG{FWS} = qr/[\x20\x09]*/;
|
28 |
|
|
$REG{M_quoted_string} = qr/\x22((?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x21\x23-\x5B\x5D-\xFF])*)\x22/;
|
29 |
|
|
$REG{M_comment} = qr/\x28((?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x27\x2A-\x5B\x5D-\xFF]|(??{$REG{comment}}))*)\x29/;
|
30 |
|
|
|
31 |
|
|
$REG{NON_atom} = qr/[^\x09\x20\x21\x23-\x27\x2A\x2B\x2D\x2F\x30-\x39\x3D\x3F\x41-\x5A\x5E-\x7E]/;
|
32 |
|
|
|
33 |
wakaba |
1.3 |
%DEFAULT = (
|
34 |
|
|
encoding_after_encode => '*default',
|
35 |
|
|
encoding_before_decode => '*default',
|
36 |
|
|
hook_encode_string => #sub {shift; (value => shift, @_)},
|
37 |
|
|
\&Message::Util::encode_header_string,
|
38 |
|
|
hook_decode_string => #sub {shift; (value => shift, @_)},
|
39 |
|
|
\&Message::Util::decode_header_string,
|
40 |
|
|
);
|
41 |
|
|
|
42 |
wakaba |
1.1 |
=head2 Message::Field::Structured->new ()
|
43 |
|
|
|
44 |
|
|
Return empty Message::Field::Structured object.
|
45 |
|
|
|
46 |
|
|
=cut
|
47 |
|
|
|
48 |
wakaba |
1.2 |
sub new ($;%) {
|
49 |
wakaba |
1.3 |
my $class = shift;
|
50 |
|
|
my $self = bless {option => {@_}}, $class;
|
51 |
|
|
for (keys %DEFAULT) {$self->{option}->{$_} ||= $DEFAULT{$_}}
|
52 |
|
|
$self;
|
53 |
wakaba |
1.1 |
}
|
54 |
|
|
|
55 |
|
|
=head2 Message::Field::Structured->parse ($unfolded_field_body)
|
56 |
|
|
|
57 |
|
|
Parse structured C<field-body>.
|
58 |
|
|
|
59 |
|
|
=cut
|
60 |
|
|
|
61 |
wakaba |
1.2 |
sub parse ($$;%) {
|
62 |
wakaba |
1.3 |
my $class = shift;
|
63 |
|
|
my $self = bless {option => {@_}}, $class;
|
64 |
|
|
for (keys %DEFAULT) {$self->{option}->{$_} ||= $DEFAULT{$_}}
|
65 |
|
|
my $field_body = $self->_decode_qcontent (shift);
|
66 |
wakaba |
1.1 |
$self->{field_body} = $field_body;
|
67 |
|
|
$self;
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
=head2 $self->stringify ()
|
71 |
|
|
|
72 |
|
|
Returns C<field-body> as a string.
|
73 |
|
|
|
74 |
|
|
=cut
|
75 |
|
|
|
76 |
|
|
sub stringify ($) {
|
77 |
|
|
my $self = shift;
|
78 |
wakaba |
1.3 |
$self->_encode_qcontent ($self->{field_body});
|
79 |
wakaba |
1.1 |
}
|
80 |
|
|
|
81 |
|
|
=head2 $self->as_plain_string ()
|
82 |
|
|
|
83 |
|
|
Returns C<field-body> contents as a plain text fragment.
|
84 |
|
|
C<quoted-string> and C<quoted-pair> in C<comment> are
|
85 |
|
|
unquoted, so return value of this method can be invalid
|
86 |
|
|
as a part of the C<field>.
|
87 |
|
|
|
88 |
|
|
=cut
|
89 |
|
|
|
90 |
|
|
sub as_plain_string ($) {
|
91 |
|
|
my $self = shift;
|
92 |
|
|
$self->unquote_quoted_string ($self->unquote_comment ($self->{field_body}));
|
93 |
|
|
}
|
94 |
wakaba |
1.4 |
=head2 $self->option ($option_name, [$option_value])
|
95 |
|
|
|
96 |
|
|
Set/gets new value of the option.
|
97 |
|
|
|
98 |
|
|
=cut
|
99 |
|
|
|
100 |
|
|
sub option ($$;$) {
|
101 |
|
|
my $self = shift;
|
102 |
|
|
my ($name, $value) = @_;
|
103 |
|
|
if (defined $value) {
|
104 |
|
|
$self->{option}->{$name} = $value;
|
105 |
|
|
}
|
106 |
|
|
$self->{option}->{$name};
|
107 |
|
|
}
|
108 |
wakaba |
1.1 |
|
109 |
wakaba |
1.3 |
## Decode C<qcontent> (content of C<quoted-string>).
|
110 |
|
|
sub _decode_qcontent ($$) {
|
111 |
|
|
my $self = shift;
|
112 |
|
|
my $quoted_string = shift;
|
113 |
|
|
$quoted_string =~ s{$REG{M_quoted_string}}{
|
114 |
|
|
my ($qtext) = ($1);
|
115 |
|
|
$qtext =~ s/\x5C([\x00-\xFF])/$1/g;
|
116 |
|
|
my %s = &{$self->{option}->{hook_decode_string}} ($self, $qtext,
|
117 |
|
|
type => 'phrase/quoted');
|
118 |
|
|
$s{value} =~ s/([\x22\x5C])([\x20-\xFF])?/"\x5C$1".($2?"\x5C$2":'')/ge;
|
119 |
|
|
'"'.$s{value}.'"';
|
120 |
|
|
}goex;
|
121 |
|
|
$quoted_string;
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
## Encode C<qcontent> (content of C<quoted-string>).
|
125 |
|
|
sub _encode_qcontent ($$) {
|
126 |
|
|
my $self = shift;
|
127 |
|
|
my $quoted_string = shift;
|
128 |
|
|
$quoted_string =~ s{$REG{M_quoted_string}}{
|
129 |
|
|
my ($qtext) = ($1);
|
130 |
|
|
$qtext =~ s/\x5C([\x00-\xFF])/$1/g;
|
131 |
|
|
my %s = &{$self->{option}->{hook_encode_string}} ($self, $qtext,
|
132 |
|
|
type => 'phrase/quoted');
|
133 |
|
|
$s{value} =~ s/([\x22\x5C])([\x20-\xFF])?/"\x5C$1".($2?"\x5C$2":'')/ge;
|
134 |
|
|
'"'.$s{value}.'"';
|
135 |
|
|
}goex;
|
136 |
|
|
$quoted_string;
|
137 |
|
|
}
|
138 |
|
|
|
139 |
wakaba |
1.1 |
sub quote_unsafe_string ($$) {
|
140 |
|
|
my $self = shift;
|
141 |
|
|
my $string = shift;
|
142 |
|
|
if ($string =~ /$REG{NON_atom}/ || $string =~ /$REG{WSP}$REG{WSP}+/) {
|
143 |
wakaba |
1.3 |
$string =~ s/([\x22\x5C])([\x20-\xFF])?/"\x5C$1".($2?"\x5C$2":'')/ge;
|
144 |
wakaba |
1.1 |
$string = '"'.$string.'"';
|
145 |
|
|
}
|
146 |
|
|
$string;
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
=head2 $self->unquote_quoted_string ($string)
|
150 |
|
|
|
151 |
|
|
Unquote C<quoted-string>. Get rid of C<DQUOTE>s and
|
152 |
|
|
C<REVERSED SOLIDUS> included in C<quoted-pair>.
|
153 |
|
|
This method is intended for internal use.
|
154 |
|
|
|
155 |
|
|
=cut
|
156 |
|
|
|
157 |
|
|
sub unquote_quoted_string ($$) {
|
158 |
|
|
my $self = shift;
|
159 |
|
|
my $quoted_string = shift;
|
160 |
|
|
$quoted_string =~ s{$REG{M_quoted_string}}{
|
161 |
|
|
my $qtext = $1;
|
162 |
|
|
$qtext =~ s/\x5C([\x00-\xFF])/$1/g;
|
163 |
|
|
$qtext;
|
164 |
|
|
}goex;
|
165 |
|
|
$quoted_string;
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
sub unquote_comment ($$) {
|
169 |
|
|
my $self = shift;
|
170 |
|
|
my $quoted_string = shift;
|
171 |
|
|
$quoted_string =~ s{$REG{M_comment}}{
|
172 |
|
|
my $qtext = $1;
|
173 |
|
|
$qtext =~ s/\x5C([\x00-\xFF])/$1/g;
|
174 |
|
|
'('.$qtext.')';
|
175 |
|
|
}goex;
|
176 |
|
|
$quoted_string;
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
=head2 $self->delete_comment ($field_body)
|
180 |
|
|
|
181 |
|
|
Remove all C<comment> in given strictured C<field-body>.
|
182 |
|
|
This method is intended for internal use.
|
183 |
|
|
|
184 |
|
|
=cut
|
185 |
|
|
|
186 |
|
|
sub delete_comment ($$) {
|
187 |
|
|
my $self = shift;
|
188 |
|
|
my $body = shift;
|
189 |
|
|
$body =~ s{($REG{quoted_string}|$REG{domain_literal})|$REG{comment}}{
|
190 |
|
|
my $o = $1; $o? $o : ' ';
|
191 |
|
|
}gex;
|
192 |
|
|
$body;
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
=head1 EXAMPLE
|
196 |
|
|
|
197 |
|
|
use Message::Field::Structured;
|
198 |
|
|
|
199 |
|
|
my $field_body = '"This is an example of <\"> (quotation mark)."
|
200 |
|
|
(Comment within \q\u\o\t\e\d\-\p\a\i\r\(\s\))';
|
201 |
|
|
my $field = Message::Field::Structured->parse ($field_body);
|
202 |
|
|
|
203 |
|
|
print $field->as_plain_string;
|
204 |
|
|
|
205 |
|
|
=head1 LICENSE
|
206 |
|
|
|
207 |
|
|
Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
|
208 |
|
|
|
209 |
|
|
This program is free software; you can redistribute it and/or modify
|
210 |
|
|
it under the terms of the GNU General Public License as published by
|
211 |
|
|
the Free Software Foundation; either version 2 of the License, or
|
212 |
|
|
(at your option) any later version.
|
213 |
|
|
|
214 |
|
|
This program is distributed in the hope that it will be useful,
|
215 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
216 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
217 |
|
|
GNU General Public License for more details.
|
218 |
|
|
|
219 |
|
|
You should have received a copy of the GNU General Public License
|
220 |
|
|
along with this program; see the file COPYING. If not, write to
|
221 |
|
|
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
222 |
|
|
Boston, MA 02111-1307, USA.
|
223 |
|
|
|
224 |
|
|
=head1 CHANGE
|
225 |
|
|
|
226 |
|
|
See F<ChangeLog>.
|
227 |
|
|
|
228 |
|
|
=cut
|
229 |
|
|
|
230 |
|
|
1;
|