1 |
wakaba |
1.1 |
|
2 |
|
|
=head1 NAME
|
3 |
|
|
|
4 |
|
|
Message::Field::Address Perl module
|
5 |
|
|
|
6 |
|
|
=head1 DESCRIPTION
|
7 |
|
|
|
8 |
|
|
Perl module for RFC 822/2822 address related C<field>s.
|
9 |
|
|
|
10 |
|
|
=cut
|
11 |
|
|
|
12 |
|
|
package Message::Field::Address;
|
13 |
|
|
require 5.6.0;
|
14 |
|
|
use strict;
|
15 |
|
|
use re 'eval';
|
16 |
wakaba |
1.4 |
use vars qw(%DEFAULT %REG $VERSION);
|
17 |
wakaba |
1.1 |
$VERSION = '1.00';
|
18 |
wakaba |
1.4 |
use Message::Util;
|
19 |
wakaba |
1.2 |
use overload '@{}' => sub {shift->{address}},
|
20 |
|
|
'""' => sub {shift->stringify};
|
21 |
wakaba |
1.1 |
|
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{atext} = qr/[\x21\x23-\x27\x2A\x2B\x2D\x2F\x30-\x39\x3D\x3F\x41-\x5A\x5E-\x7E]+/;
|
29 |
|
|
$REG{dot_atom} = qr/$REG{atext}(?:$REG{FWS}\x2E$REG{FWS}$REG{atext})*/;
|
30 |
|
|
$REG{dot_word} = qr/(?:$REG{atext}|$REG{quoted_string})(?:$REG{FWS}\x2E$REG{FWS}(?:$REG{atext}|$REG{quoted_string}))*/;
|
31 |
|
|
$REG{phrase} = qr/(?:$REG{atext}|$REG{quoted_string})(?:$REG{atext}|$REG{quoted_string}|\.|$REG{FWS})*/;
|
32 |
|
|
$REG{obs_route} = qr/(?:\x40$REG{FWS}(?:$REG{dot_word}|$REG{domain_literal})(?:$REG{FWS},?$REG{FWS}\x40$REG{FWS}(?:$REG{dot_word}|$REG{domain_literal}))*):/;
|
33 |
|
|
$REG{addr_spec} = qr/$REG{dot_word}$REG{FWS}\x40$REG{FWS}(?:$REG{dot_atom}|$REG{domain_literal})/;
|
34 |
wakaba |
1.5 |
$REG{mailbox} = qr/(?:(?:$REG{phrase})?<$REG{FWS}(?:(?:$REG{obs_route})?$REG{FWS}$REG{addr_spec}$REG{FWS})?>|$REG{addr_spec}|$REG{atext})/;
|
35 |
wakaba |
1.1 |
$REG{mailbox_list} = qr/$REG{mailbox}(?:$REG{FWS},(?:$REG{FWS}$REG{mailbox})?)*/;
|
36 |
wakaba |
1.5 |
$REG{address} = qr/(?:(?:$REG{phrase})?(?:<$REG{FWS}(?:(?:$REG{obs_route})?$REG{FWS}$REG{addr_spec}$REG{FWS})?>|:$REG{FWS}(?:$REG{mailbox_list}$REG{FWS})?;)|$REG{addr_spec}|$REG{atext})/;
|
37 |
wakaba |
1.1 |
$REG{address_list} = qr/$REG{address}(?:$REG{FWS},(?:$REG{FWS}$REG{address})?)*/;
|
38 |
|
|
$REG{M_group} = qr/($REG{phrase}):/;
|
39 |
|
|
$REG{M_mailbox} = qr/(?:($REG{phrase})?<$REG{FWS}($REG{obs_route})?$REG{FWS}($REG{addr_spec})$REG{FWS}>|($REG{addr_spec}))/;
|
40 |
wakaba |
1.5 |
$REG{M_mailbox_empty} = qr/(?:($REG{phrase})?<$REG{FWS}>)/;
|
41 |
wakaba |
1.1 |
$REG{M_quoted_string} = qr/\x22((?:\x5C[\x00-\xFF]|[\x00-\x0C\x0E-\x21\x23-\x5B\x5D-\xFF])*)\x22/;
|
42 |
|
|
|
43 |
|
|
$REG{NON_atom} = qr/[^\x09\x20\x21\x23-\x27\x2A\x2B\x2D\x2F\x30-\x39\x3D\x3F\x41-\x5A\x5E-\x7E]/;
|
44 |
|
|
|
45 |
wakaba |
1.4 |
%DEFAULT = (
|
46 |
wakaba |
1.5 |
dont_reply => -1, ## See C<$self->dont_reply>
|
47 |
|
|
dont_reply_display_name => '',
|
48 |
wakaba |
1.4 |
encoding_after_encode => '*default',
|
49 |
|
|
encoding_before_decode => '*default',
|
50 |
wakaba |
1.6 |
format => 'rfc2822',
|
51 |
wakaba |
1.4 |
hook_encode_string => #sub {shift; (value => shift, @_)},
|
52 |
|
|
\&Message::Util::encode_header_string,
|
53 |
|
|
hook_decode_string => #sub {shift; (value => shift, @_)},
|
54 |
|
|
\&Message::Util::decode_header_string,
|
55 |
wakaba |
1.5 |
is_mailbox => -1, ## Allow multiple mail addresses?
|
56 |
wakaba |
1.3 |
is_return_path => -1,
|
57 |
|
|
use_display_name => 1,
|
58 |
wakaba |
1.5 |
use_dont_reply => -1, ## See C<$self->dont_reply>
|
59 |
wakaba |
1.3 |
use_group => 1,
|
60 |
wakaba |
1.5 |
use_keyword => -1, ## See C<$self->keyword>
|
61 |
|
|
use_keyword_multiple => -1,
|
62 |
wakaba |
1.3 |
);
|
63 |
wakaba |
1.5 |
## RFC 2822 C<mailbox> => is_mailbox = +1, (use_group = -1)
|
64 |
|
|
## RFC 2822 C<mailbox-list> => is_mailbox = -1, use_group = -1
|
65 |
|
|
## RFC 2822 C<address-list> => is_mailbox = -1, use_group = +1
|
66 |
wakaba |
1.3 |
|
67 |
wakaba |
1.5 |
## Initialization of option value by C<field-name> and
|
68 |
|
|
## version of specification(s).
|
69 |
wakaba |
1.3 |
sub _init_option ($$) {
|
70 |
|
|
my $self = shift;
|
71 |
wakaba |
1.5 |
my $name = shift;
|
72 |
|
|
my $spec = $self->{option}->{format};
|
73 |
wakaba |
1.6 |
my %is_mailbox_name = ( ## C<is_mailbox>
|
74 |
|
|
'complaints-to' => 1,
|
75 |
|
|
'mail-copies-to' => 1, 'resent-sender' => 1,
|
76 |
|
|
'return-path' => 1, sender => 1,
|
77 |
|
|
'x-complaints-to' => 1,
|
78 |
|
|
);
|
79 |
|
|
my %use_group_name = ( ## C<use_group>
|
80 |
|
|
'approved' => -1,
|
81 |
|
|
'from' => -1, 'resent-from' => -1,
|
82 |
|
|
);
|
83 |
|
|
if ($is_mailbox_name{$name}) {
|
84 |
|
|
$self->{option}->{is_mailbox} = $is_mailbox_name{$name};
|
85 |
|
|
} elsif ($use_group_name{$name}) {
|
86 |
|
|
$self->{option}->{use_group} = $use_group_name{$name};
|
87 |
wakaba |
1.5 |
}
|
88 |
|
|
if ($spec eq 'usefor') {
|
89 |
|
|
if ($name eq 'reply-to') {
|
90 |
|
|
$self->{option}->{use_group} = -1;
|
91 |
|
|
$self->{option}->{use_dont_reply} = 1;
|
92 |
|
|
}
|
93 |
|
|
}
|
94 |
|
|
if ($name eq 'mail-copies-to') {
|
95 |
|
|
$self->{option}->{use_keyword} = 1;
|
96 |
|
|
}
|
97 |
|
|
if ($name eq 'return-path') {
|
98 |
wakaba |
1.3 |
$self->{option}->{is_return_path} = 1;
|
99 |
|
|
$self->{option}->{use_display_name} = -1;
|
100 |
|
|
}
|
101 |
|
|
$self;
|
102 |
|
|
}
|
103 |
|
|
|
104 |
wakaba |
1.2 |
=head2 Message::Field::Address->new ()
|
105 |
wakaba |
1.1 |
|
106 |
|
|
Return empty address object.
|
107 |
|
|
|
108 |
|
|
=cut
|
109 |
|
|
|
110 |
wakaba |
1.3 |
sub new ($;%) {
|
111 |
|
|
my $self = bless {type => '_ROOT'}, shift;
|
112 |
|
|
my %option = @_;
|
113 |
wakaba |
1.4 |
for (%DEFAULT) {$option{$_} ||= $DEFAULT{$_}}
|
114 |
wakaba |
1.3 |
$self->{option} = \%option;
|
115 |
|
|
$self->_init_option ($self->{option}->{field_name});
|
116 |
|
|
$self;
|
117 |
wakaba |
1.1 |
}
|
118 |
|
|
|
119 |
wakaba |
1.2 |
=head2 Message::Field::Address->parse ($unfolded_field_body)
|
120 |
wakaba |
1.1 |
|
121 |
|
|
Parse structured C<field-body> contain of C<address-list>.
|
122 |
|
|
|
123 |
|
|
=cut
|
124 |
|
|
|
125 |
wakaba |
1.3 |
sub parse ($$;%) {
|
126 |
wakaba |
1.1 |
my $self = bless {}, shift;
|
127 |
|
|
my $field_body = shift;
|
128 |
wakaba |
1.3 |
my %option = @_;
|
129 |
wakaba |
1.4 |
for (%DEFAULT) {$option{$_} ||= $DEFAULT{$_}}
|
130 |
wakaba |
1.3 |
$self->{option} = \%option;
|
131 |
|
|
$self->_init_option ($self->{option}->{field_name});
|
132 |
wakaba |
1.1 |
$field_body = $self->delete_comment ($field_body);
|
133 |
|
|
my %addr = $self->parse_address_list ($field_body);
|
134 |
|
|
$self->{address} = $addr{address};
|
135 |
|
|
$self->{type} = $addr{type};
|
136 |
|
|
$self;
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
=head2 $self->address ()
|
140 |
|
|
|
141 |
|
|
Return address list in the format described in
|
142 |
|
|
L<$self-E<gt>parse_address_list ()>.
|
143 |
|
|
|
144 |
|
|
=cut
|
145 |
|
|
|
146 |
|
|
sub address ($) {@{shift->{address}}}
|
147 |
|
|
|
148 |
wakaba |
1.4 |
=head2 $self->addr_spec ([$index])
|
149 |
|
|
|
150 |
|
|
Returns (C<$index>'th or all) C<addr-spec>.
|
151 |
wakaba |
1.5 |
(First C<addr-spec>'s C<$index> is C<1>, not C<0>.)
|
152 |
wakaba |
1.4 |
|
153 |
|
|
=cut
|
154 |
|
|
|
155 |
|
|
sub addr_spec ($;$) {
|
156 |
|
|
my $self = shift;
|
157 |
|
|
my $i = shift;
|
158 |
wakaba |
1.5 |
#return $self->{address}->[$i]->{addr_spec}
|
159 |
|
|
# if defined $i && ref $self->{address}->[$i];
|
160 |
|
|
#map {$_->{addr_spec}} @{$self->{address}};
|
161 |
|
|
my @r = (); my $j = 0;
|
162 |
|
|
for my $m (@{$self->{address}}) {
|
163 |
|
|
if ($m->{type} eq 'group') {
|
164 |
|
|
for my $n (@{$m->{address}}) {
|
165 |
|
|
$j++;
|
166 |
|
|
return $n->{addr_spec} if $j == $i;
|
167 |
|
|
push @r, $m->{addr_spec};
|
168 |
|
|
}
|
169 |
|
|
} else {
|
170 |
|
|
$j++;
|
171 |
|
|
return $m->{addr_spec} if $j == $i;
|
172 |
|
|
push @r, $m->{addr_spec};
|
173 |
|
|
}
|
174 |
|
|
}
|
175 |
|
|
@r;
|
176 |
wakaba |
1.4 |
}
|
177 |
|
|
|
178 |
wakaba |
1.1 |
=head2 $self->add ($addr_spec, [%option])
|
179 |
|
|
|
180 |
|
|
Add an mail address to C<$self> (address object).
|
181 |
|
|
%option = (name => C<display-name>, route => C<route>,
|
182 |
|
|
group => C<display-name> of C<group>)
|
183 |
|
|
|
184 |
|
|
Note that this method (and other methods) does not check
|
185 |
|
|
$addr_spec and $option{route} is valid or not.
|
186 |
|
|
|
187 |
|
|
=cut
|
188 |
|
|
|
189 |
|
|
sub add ($$;%) {
|
190 |
|
|
my $self = shift;
|
191 |
|
|
my ($addr, %option) = @_;
|
192 |
|
|
my $name = $option{name} || $option{display_name};
|
193 |
|
|
unless ($option{group}) {
|
194 |
|
|
push @{$self->{address}}, {type => 'mailbox',
|
195 |
|
|
addr_spec => $addr, display_name => $name, route => $option{route}};
|
196 |
|
|
} else {
|
197 |
|
|
for my $i (@{$self->{address}}) {
|
198 |
|
|
if ($i->{type} eq 'group' && $i->{display_name} eq $option{group}) {
|
199 |
|
|
push @{$i->{address}}, {type => 'mailbox',
|
200 |
|
|
addr_spec => $addr, display_name => $name, route => $option{route}};
|
201 |
|
|
return $self;
|
202 |
|
|
}
|
203 |
|
|
}
|
204 |
|
|
push @{$self->{address}}, {type => 'group', display_name => $option{group},
|
205 |
|
|
address => [
|
206 |
|
|
{type => 'mailbox',
|
207 |
|
|
addr_spec => $addr, display_name => $name, route => $option{route}}
|
208 |
|
|
]};
|
209 |
|
|
}
|
210 |
|
|
$self;
|
211 |
|
|
}
|
212 |
|
|
|
213 |
wakaba |
1.3 |
sub stringify ($;%) {
|
214 |
wakaba |
1.1 |
my $self = shift;
|
215 |
wakaba |
1.3 |
my %option = @_;
|
216 |
|
|
$option{is_mailbox} ||= $self->{option}->{is_mailbox};
|
217 |
|
|
$option{is_return_path} ||= $self->{option}->{is_return_path};
|
218 |
|
|
$option{use_display_name} ||= $self->{option}->{use_display_name};
|
219 |
|
|
$option{use_group} ||= $self->{option}->{use_group};
|
220 |
wakaba |
1.5 |
$option{use_keyword} ||= $self->{option}->{use_keyword};
|
221 |
|
|
$option{use_keyword_multiple} ||= $self->{option}->{use_keyword_multiple};
|
222 |
|
|
$option{use_dont_reply} ||= $self->{option}->{use_dont_reply};
|
223 |
|
|
$option{dont_reply} ||= $self->{option}->{dont_reply};
|
224 |
|
|
$option{dont_reply_display_name} = $self->{option}->{dont_reply_display_name}
|
225 |
|
|
unless defined $option{dont_reply_display_name};
|
226 |
|
|
if ($option{use_dont_reply}>0 && $option{dont_reply}>0) {
|
227 |
|
|
## usefor-article Reply-To:
|
228 |
|
|
my $g_return = '';
|
229 |
|
|
if ($option{dont_reply_display_name} && $option{use_display_name}>0) {
|
230 |
|
|
my %s = &{$self->{option}->{hook_encode_string}} ($self,
|
231 |
|
|
$option{dont_reply_display_name}, type => 'phrase');
|
232 |
|
|
$g_return = $self->quote_unsafe_string ($s{value}) .' ';
|
233 |
|
|
}
|
234 |
|
|
$g_return .= '<>';
|
235 |
|
|
return $g_return;
|
236 |
|
|
}
|
237 |
wakaba |
1.1 |
my @return;
|
238 |
wakaba |
1.5 |
if ($option{use_keyword}>0) {
|
239 |
|
|
my @kw = grep {$self->{keyword}->{$_}>0} keys %{$self->{keyword}};
|
240 |
|
|
return $kw[0] if $kw[0] && $option{use_keyword_multiple}<0;
|
241 |
|
|
push @return, @kw;
|
242 |
|
|
}
|
243 |
wakaba |
1.1 |
for my $address (@{$self->{address}}) {
|
244 |
|
|
my $return = '';
|
245 |
|
|
next if !$address->{addr_spec} && $address->{type} ne 'group';
|
246 |
wakaba |
1.3 |
if ($address->{display_name} && $option{use_display_name}>0) {
|
247 |
wakaba |
1.5 |
if ($address->{type} eq 'group' && $option{use_group}>0) {
|
248 |
|
|
my %s = &{$self->{option}->{hook_encode_string}} ($self,
|
249 |
|
|
$address->{display_name}, type => 'phrase');
|
250 |
|
|
$return = $self->quote_unsafe_string ($s{value}).': ';
|
251 |
|
|
#} else {
|
252 |
|
|
# my %s = &{$self->{option}->{hook_encode_string}} ($self,
|
253 |
|
|
# $address->{display_name}, type => 'comment');
|
254 |
|
|
# $s{value} =~ s/[\x28\x29\x5C]/\\$&/g;
|
255 |
|
|
# $return = '('.$s{value}.') ';
|
256 |
|
|
} elsif ($address->{type} ne 'group') {
|
257 |
wakaba |
1.4 |
my %s = &{$self->{option}->{hook_encode_string}} ($self,
|
258 |
|
|
$address->{display_name}, type => 'phrase');
|
259 |
wakaba |
1.5 |
$return = $self->quote_unsafe_string ($s{value}).' ';
|
260 |
|
|
}
|
261 |
wakaba |
1.1 |
}
|
262 |
|
|
if ($address->{type} ne 'group') {
|
263 |
|
|
$return .= '<'.$address->{route}.$address->{addr_spec}.'>';
|
264 |
|
|
} else {
|
265 |
|
|
my (@g_return);
|
266 |
|
|
for my $mailbox (@{$address->{address}}) {
|
267 |
|
|
next unless $mailbox->{addr_spec};
|
268 |
|
|
my $g_return = '';
|
269 |
wakaba |
1.4 |
if ($mailbox->{display_name} && $option{use_display_name}>0) {
|
270 |
|
|
my %s = &{$self->{option}->{hook_encode_string}} ($self,
|
271 |
|
|
$mailbox->{display_name}, type => 'phrase');
|
272 |
|
|
$g_return = $self->quote_unsafe_string ($s{value}) .' ';
|
273 |
|
|
}
|
274 |
wakaba |
1.1 |
$g_return .= '<'.$mailbox->{route}.$mailbox->{addr_spec}.'>';
|
275 |
|
|
push @g_return, $g_return;
|
276 |
wakaba |
1.3 |
last if $option{is_mailbox}>0;
|
277 |
wakaba |
1.1 |
}
|
278 |
|
|
$return .= join ', ', @g_return;
|
279 |
wakaba |
1.3 |
$return .= ';' if $address->{type} eq 'group' && $option{use_group}>0;
|
280 |
wakaba |
1.1 |
}
|
281 |
|
|
push @return, $return;
|
282 |
wakaba |
1.3 |
last if $option{is_mailbox}>0;
|
283 |
|
|
}
|
284 |
wakaba |
1.5 |
if ($#return == -1) {
|
285 |
|
|
if ($option{is_return_path}>0) {
|
286 |
|
|
push @return, '<>';
|
287 |
|
|
#} elsif ($option{use_dont_reply}>0) { ## usefor-article Reply-To:
|
288 |
|
|
# my $g_return = '';
|
289 |
|
|
# if ($option{dont_reply_display_name} && $option{use_display_name}>0) {
|
290 |
|
|
# my %s = &{$self->{option}->{hook_encode_string}} ($self,
|
291 |
|
|
# $option{dont_reply_display_name}, type => 'phrase');
|
292 |
|
|
# $g_return = $self->quote_unsafe_string ($s{value}) .' ';
|
293 |
|
|
# }
|
294 |
|
|
# $g_return .= '<>';
|
295 |
|
|
# push @return, $g_return;
|
296 |
|
|
}
|
297 |
wakaba |
1.1 |
}
|
298 |
|
|
join ', ', @return;
|
299 |
|
|
}
|
300 |
|
|
|
301 |
wakaba |
1.5 |
=head2 $self->dont_reply ([$don't_reply?, [$display_name]])
|
302 |
|
|
|
303 |
|
|
Set/gets whether C<field-body> content is "don't reply!"
|
304 |
|
|
or not. Second argument is short string used as C<display-name>.
|
305 |
|
|
|
306 |
|
|
This function returns an array of C<($don't_reply?, $display_name)>.
|
307 |
|
|
<$don't_reply> takes value C<1> (true) or C<-1> (false).
|
308 |
|
|
|
309 |
|
|
Note: This don't reply convention is defined by draft-usefor-article.
|
310 |
|
|
You should not use this in RFC 2822 mail message.
|
311 |
|
|
|
312 |
|
|
Enable this, set C<use_dont_reply> option to C<1>. (Default is C<-1>.
|
313 |
|
|
|
314 |
|
|
Note: Set this value does not mean clear address list that
|
315 |
|
|
current C<Message::Field::Address> has. You can get it
|
316 |
|
|
as well as before setting new value. But C<stringify> method
|
317 |
|
|
does not output any of addresses if <$don't_reply> is C<1>.
|
318 |
|
|
|
319 |
|
|
=head3 example
|
320 |
|
|
|
321 |
|
|
my $a = Message::Field::Address->new (use_reply => 1);
|
322 |
|
|
$a->dont_reply (1, 'Please do not reply');
|
323 |
|
|
print $a; # "Please do not reply <>"
|
324 |
|
|
|
325 |
|
|
=cut
|
326 |
|
|
|
327 |
|
|
sub dont_reply ($;$$) {
|
328 |
|
|
my $self = shift;
|
329 |
|
|
my $dr = shift;
|
330 |
|
|
my $dname = shift;
|
331 |
|
|
$self->{option}->{dont_reply} = $dr if $dr;
|
332 |
|
|
$self->{option}->{dont_reply_display_name} = $dname if defined $dname;
|
333 |
|
|
($self->{dont_reply}, $self->{dont_reply_display_name});
|
334 |
|
|
}
|
335 |
|
|
|
336 |
|
|
=head2 $self->keyword ($keyword, [$true_of_false])
|
337 |
|
|
|
338 |
|
|
Set/gets whether keyword is specified. C<$true_or_false>
|
339 |
|
|
takes values C<1> and C<-1>.
|
340 |
|
|
|
341 |
|
|
This function is intended to be used for keyword(s) allowed
|
342 |
|
|
mail addresses field, such as C<Mail-Copies-To:> defined by
|
343 |
|
|
draft-usefor-article.
|
344 |
|
|
|
345 |
|
|
There are two related options, C<use_keyword> and C<use_keyword_multiple>.
|
346 |
|
|
Former is on/off switch of this function. Later allows
|
347 |
|
|
multiple options/addresses, such as C<keyworda, E<lt>foo@bar.exampleE<gt>>.
|
348 |
|
|
|
349 |
|
|
=cut
|
350 |
|
|
|
351 |
|
|
sub keyword ($$;$) {
|
352 |
|
|
my $self = shift;
|
353 |
|
|
my $key = shift;
|
354 |
|
|
my $tf = shift;
|
355 |
|
|
$self->{keyword}->{$key} = $tf if $tf;
|
356 |
|
|
$self->{keyword}->{$key} || -1;
|
357 |
|
|
}
|
358 |
|
|
|
359 |
wakaba |
1.1 |
sub quote_unsafe_string ($$) {
|
360 |
|
|
my $self = shift;
|
361 |
|
|
my $string = shift;
|
362 |
|
|
if ($string =~ /$REG{NON_atom}/ || $string =~ /$REG{WSP}$REG{WSP}+/) {
|
363 |
|
|
$string =~ s/([\x22\x5C])/\x5C$1/g;
|
364 |
|
|
$string = '"'.$string.'"';
|
365 |
|
|
}
|
366 |
|
|
$string;
|
367 |
|
|
}
|
368 |
|
|
|
369 |
|
|
=head2 $self->unquote_quoted_string ($string)
|
370 |
|
|
|
371 |
|
|
Unquote C<quoted-string>. Get rid of C<DQUOTE>s and
|
372 |
|
|
C<REVERSED SOLIDUS> included in C<quoted-pair>.
|
373 |
|
|
This method is intended for internal use.
|
374 |
|
|
|
375 |
|
|
=cut
|
376 |
|
|
|
377 |
|
|
sub unquote_quoted_string ($$) {
|
378 |
|
|
my $self = shift;
|
379 |
|
|
my $quoted_string = shift;
|
380 |
|
|
$quoted_string =~ s{$REG{M_quoted_string}}{
|
381 |
|
|
my $qtext = $1;
|
382 |
|
|
$qtext =~ s/\x5C([\x00-\xFF])/$1/g;
|
383 |
|
|
$qtext;
|
384 |
|
|
}goex;
|
385 |
|
|
$quoted_string;
|
386 |
|
|
}
|
387 |
|
|
|
388 |
wakaba |
1.4 |
sub _decode_quoted_string ($$) {
|
389 |
|
|
my $self = shift;
|
390 |
|
|
my $quoted_string = shift;
|
391 |
|
|
$quoted_string =~ s{$REG{M_quoted_string}|([^\x22]+)}{
|
392 |
|
|
my ($qtext,$t) = ($1, $2);
|
393 |
|
|
if ($t) {
|
394 |
|
|
my %s = &{$self->{option}->{hook_decode_string}} ($self, $t,
|
395 |
|
|
type => 'value');
|
396 |
|
|
$s{value};
|
397 |
|
|
} else {
|
398 |
|
|
$qtext =~ s/\x5C([\x00-\xFF])/$1/g;
|
399 |
|
|
my %s = &{$self->{option}->{hook_decode_string}} ($self, $qtext,
|
400 |
|
|
type => 'value/quoted');
|
401 |
|
|
$s{value};
|
402 |
|
|
}
|
403 |
|
|
}goex;
|
404 |
|
|
$quoted_string;
|
405 |
|
|
}
|
406 |
|
|
|
407 |
wakaba |
1.1 |
=head2 $self->parse_mailbox ($mailbox)
|
408 |
|
|
|
409 |
|
|
Parse C<mailbox> and return array of C<addr-spec>,
|
410 |
|
|
C<display-name> and C<route> (aka C<obs-route> of RFC 2822).
|
411 |
|
|
This method is intended for internal use.
|
412 |
|
|
|
413 |
|
|
=cut
|
414 |
|
|
|
415 |
|
|
sub parse_mailbox ($$) {
|
416 |
|
|
my $self = shift;
|
417 |
|
|
my $mailbox = shift;
|
418 |
|
|
if ($mailbox =~ /$REG{M_mailbox}/) {
|
419 |
|
|
my ($display_name, $route, $addr_spec) = ($1, $2, $3 || $4);
|
420 |
|
|
$display_name =~ s/$REG{WSP}+$//;
|
421 |
wakaba |
1.4 |
$display_name = $self->_decode_quoted_string ($display_name);
|
422 |
wakaba |
1.1 |
$addr_spec =~ s{($REG{quoted_string}|$REG{domain_literal})|$REG{WSP}}{$1}go;
|
423 |
|
|
$route =~ s{($REG{quoted_string}|$REG{domain_literal})|$REG{WSP}}{$1}go;
|
424 |
|
|
return ($addr_spec, $display_name, $route);
|
425 |
wakaba |
1.5 |
} elsif ($mailbox =~ /$REG{M_mailbox_empty}/) {
|
426 |
|
|
my ($display_name) = ($1);
|
427 |
|
|
$display_name =~ s/$REG{WSP}+$//;
|
428 |
|
|
$display_name = $self->_decode_quoted_string ($display_name);
|
429 |
|
|
return ('', $display_name, 'dummy');
|
430 |
|
|
} elsif ($mailbox =~ /^$REG{atext}$/) {
|
431 |
|
|
$self->{keyword}->{$mailbox} = 1;
|
432 |
wakaba |
1.1 |
}
|
433 |
wakaba |
1.5 |
(undef, undef, undef);
|
434 |
wakaba |
1.1 |
}
|
435 |
|
|
|
436 |
|
|
=head2 $self->parse_address_list ($address_list)
|
437 |
|
|
|
438 |
|
|
Parse C<address-list> and return hash.
|
439 |
|
|
This method is intended for internal use.
|
440 |
|
|
|
441 |
|
|
=head3 Structure of hash returned by parse_address_list
|
442 |
|
|
|
443 |
|
|
%address = (
|
444 |
|
|
|
445 |
|
|
type => '_ROOT',
|
446 |
|
|
address => [
|
447 |
|
|
|
448 |
|
|
## mailbox
|
449 |
|
|
{
|
450 |
|
|
type => 'mailbox',
|
451 |
|
|
display_name => 'Foo H. Bar',
|
452 |
|
|
addr_spec => 'foo@bar.example',
|
453 |
|
|
route => '@hoge.example:',
|
454 |
|
|
},
|
455 |
|
|
|
456 |
|
|
## group
|
457 |
|
|
{
|
458 |
|
|
type => 'group',
|
459 |
|
|
display_name => 'The committee',
|
460 |
|
|
address => [
|
461 |
|
|
|
462 |
|
|
## mailbox
|
463 |
|
|
{
|
464 |
|
|
type => 'mailbox',
|
465 |
|
|
display_name => 'Tom (Director)',
|
466 |
|
|
addr_spec => 'tom@committee.example',
|
467 |
|
|
route => '',
|
468 |
|
|
}
|
469 |
|
|
|
470 |
|
|
],
|
471 |
|
|
},
|
472 |
|
|
|
473 |
|
|
],
|
474 |
|
|
|
475 |
|
|
);
|
476 |
|
|
|
477 |
|
|
=cut
|
478 |
|
|
|
479 |
|
|
sub parse_address_list ($$) {
|
480 |
|
|
my $self = shift;
|
481 |
|
|
my $address_list = shift;
|
482 |
|
|
my %r_addr = (type => '_ROOT');
|
483 |
|
|
$address_list =~ s{($REG{address})}{
|
484 |
|
|
my $address = $1;
|
485 |
|
|
if ($address =~ /^$REG{M_group}/) {
|
486 |
|
|
my %r_group = (type => 'group', display_name => $1);
|
487 |
|
|
$r_group{display_name} =~ s/$REG{WSP}+$//;
|
488 |
|
|
$r_group{display_name} = $self->unquote_quoted_string ($r_group{display_name});
|
489 |
|
|
$address =~ s{($REG{mailbox})}{
|
490 |
|
|
my ($addr, $name, $route) = $self->parse_mailbox ($1);
|
491 |
|
|
push @{$r_group{address}}, {type => 'mailbox',
|
492 |
wakaba |
1.5 |
display_name => $name, route => $route, addr_spec => $addr}
|
493 |
|
|
if $addr;
|
494 |
wakaba |
1.1 |
}goex;
|
495 |
|
|
push @{$r_addr{address}}, \%r_group;
|
496 |
|
|
} else {
|
497 |
|
|
my ($addr, $name, $route) = $self->parse_mailbox ($address);
|
498 |
wakaba |
1.5 |
if ($addr) {
|
499 |
|
|
push @{$r_addr{address}}, {type => 'mailbox',
|
500 |
|
|
display_name => $name, route => $route, addr_spec => $addr};
|
501 |
|
|
} elsif ($route) { # dummy
|
502 |
|
|
$self->{option}->{dont_reply} = 1;
|
503 |
|
|
$self->{option}->{dont_reply_display_name} = $name;
|
504 |
|
|
}
|
505 |
wakaba |
1.1 |
}
|
506 |
|
|
}goex;
|
507 |
|
|
%r_addr;
|
508 |
|
|
}
|
509 |
|
|
|
510 |
|
|
=head2 $self->delete_comment ($field_body)
|
511 |
|
|
|
512 |
|
|
Remove all C<comment> in given strictured C<field-body>.
|
513 |
|
|
This method is intended for internal use.
|
514 |
|
|
|
515 |
|
|
=cut
|
516 |
|
|
|
517 |
|
|
sub delete_comment ($$) {
|
518 |
|
|
my $self = shift;
|
519 |
|
|
my $body = shift;
|
520 |
|
|
$body =~ s{($REG{quoted_string}|$REG{domain_literal})|$REG{comment}}{
|
521 |
|
|
my $o = $1; $o? $o : ' ';
|
522 |
|
|
}gex;
|
523 |
|
|
$body;
|
524 |
|
|
}
|
525 |
|
|
|
526 |
|
|
=head1 EXAMPLE
|
527 |
|
|
|
528 |
|
|
## Compose field-body for To: field.
|
529 |
|
|
|
530 |
wakaba |
1.2 |
use Message::Field::Address;
|
531 |
wakaba |
1.1 |
my $addr = new Message::Field::Address;
|
532 |
|
|
$addr->add ('foo@example.org', name => 'Mr. foo bar');
|
533 |
|
|
$addr->add ('webmaster@example.org', group => 'administrators');
|
534 |
|
|
$addr->add ('postmaster@example.org', group => 'administrators');
|
535 |
|
|
|
536 |
|
|
my $field_body = $addr->stringify ();
|
537 |
|
|
|
538 |
|
|
|
539 |
|
|
## Output parsed address-list tree.
|
540 |
|
|
|
541 |
|
|
use Message::Field::Address;
|
542 |
|
|
my $addr = Message::Field::Address->parse ($field_body);
|
543 |
|
|
|
544 |
|
|
for my $i (@$addr) {
|
545 |
|
|
if ($i->{type} eq 'group') {
|
546 |
|
|
print "\x40 $i->{display_name}: \n";
|
547 |
|
|
for my $j (@{$i->{address}}) {
|
548 |
|
|
print "\t- $j->{display_name} <$j->{route}$j->{addr_spec}>\n";
|
549 |
|
|
}
|
550 |
|
|
} else {
|
551 |
|
|
print "- $i->{display_name} <$i->{route}$i->{addr_spec}>\n";
|
552 |
|
|
}
|
553 |
|
|
}
|
554 |
|
|
|
555 |
|
|
=head1 LICENSE
|
556 |
|
|
|
557 |
|
|
Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
|
558 |
|
|
|
559 |
|
|
This program is free software; you can redistribute it and/or modify
|
560 |
|
|
it under the terms of the GNU General Public License as published by
|
561 |
|
|
the Free Software Foundation; either version 2 of the License, or
|
562 |
|
|
(at your option) any later version.
|
563 |
|
|
|
564 |
|
|
This program is distributed in the hope that it will be useful,
|
565 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
566 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
567 |
|
|
GNU General Public License for more details.
|
568 |
|
|
|
569 |
|
|
You should have received a copy of the GNU General Public License
|
570 |
|
|
along with this program; see the file COPYING. If not, write to
|
571 |
|
|
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
572 |
|
|
Boston, MA 02111-1307, USA.
|
573 |
|
|
|
574 |
|
|
=head1 CHANGE
|
575 |
|
|
|
576 |
|
|
See F<ChangeLog>.
|
577 |
wakaba |
1.6 |
$Date: 2002/03/31 13:11:55 $
|
578 |
wakaba |
1.1 |
|
579 |
|
|
=cut
|
580 |
|
|
|
581 |
|
|
1;
|