13 |
require 5.6.0; |
require 5.6.0; |
14 |
use strict; |
use strict; |
15 |
use re 'eval'; |
use re 'eval'; |
16 |
use vars qw(%REG $VERSION); |
use vars qw(%DEFAULT %REG $VERSION); |
17 |
$VERSION = '1.00'; |
$VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
18 |
|
require Message::Util; |
19 |
use overload '""' => sub {shift->stringify}; |
use overload '""' => sub {shift->stringify}; |
20 |
|
|
21 |
=head2 Message::Field::Address->new () |
%DEFAULT = ( |
22 |
|
encoding_after_encode => '*default', |
23 |
|
encoding_before_decode => '*default', |
24 |
|
hook_encode_string => #sub {shift; (value => shift, @_)}, |
25 |
|
\&Message::Util::encode_header_string, |
26 |
|
hook_decode_string => #sub {shift; (value => shift, @_)}, |
27 |
|
\&Message::Util::decode_header_string, |
28 |
|
); |
29 |
|
|
30 |
|
=head2 Message::Field::Unstructured->new () |
31 |
|
|
32 |
Return empty address object. |
Returns new Unstructured Header Field object. |
33 |
|
|
34 |
=cut |
=cut |
35 |
|
|
36 |
sub new ($;%) { |
sub new ($;%) { |
37 |
bless {}, shift; |
my $class = shift; |
38 |
|
my $self = bless {option => {@_}}, $class; |
39 |
|
for (keys %DEFAULT) {$self->{option}->{$_} ||= $DEFAULT{$_}} |
40 |
|
$self; |
41 |
} |
} |
42 |
|
|
43 |
=head2 Message::Field::Address->parse ($unfolded_field_body) |
=head2 Message::Field::Unstructured->new ($field_body) |
44 |
|
|
45 |
Parse structured C<field-body> contain of C<address-list>. |
Reads and returns Unstructured Header Field object. |
46 |
|
|
47 |
=cut |
=cut |
48 |
|
|
49 |
sub parse ($$;%) { |
sub parse ($$;%) { |
50 |
my $self = bless {}, shift; |
my $class = shift; |
51 |
my $field_body = shift; |
my $field_body = shift; |
52 |
$self->{field_body} = $field_body; |
my $self = bless {option => {@_}}, $class; |
53 |
|
for (keys %DEFAULT) {$self->{option}->{$_} ||= $DEFAULT{$_}} |
54 |
|
my %s = &{$self->{option}->{hook_decode_string}} ($self, $field_body, |
55 |
|
type => 'text'); |
56 |
|
$self->{field_body} = $s{value}; |
57 |
$self; |
$self; |
58 |
} |
} |
59 |
|
|
60 |
sub stringify ($) { |
=head2 $self->stringify ([%options]) |
|
my $self = shift; |
|
|
$self->{field_body}; |
|
|
} |
|
61 |
|
|
62 |
sub as_plain_string ($) { |
Returns C<field-body>. |
63 |
|
|
64 |
|
=cut |
65 |
|
|
66 |
|
sub stringify ($;%) { |
67 |
my $self = shift; |
my $self = shift; |
68 |
$self->{field_body}; |
my %option = @_; |
69 |
|
my (%e) = &{$self->{option}->{hook_encode_string}} ($self, |
70 |
|
$self->{field_body}, type => 'text'); |
71 |
|
$e{value}; |
72 |
} |
} |
73 |
|
sub as_string ($;%) {shift->stringify (@_)} |
74 |
|
|
75 |
=head1 EXAMPLE |
sub as_plain_string ($;%) { |
76 |
|
shift->{field_body}; |
77 |
## Compose field-body for To: field. |
} |
|
|
|
|
use Message::Field::Address; |
|
|
my $addr = new Message::Field::Address; |
|
|
$addr->add ('foo@example.org', name => 'Mr. foo bar'); |
|
|
$addr->add ('webmaster@example.org', group => 'administrators'); |
|
|
$addr->add ('postmaster@example.org', group => 'administrators'); |
|
|
|
|
|
my $field_body = $addr->stringify (); |
|
|
|
|
|
|
|
|
## Output parsed address-list tree. |
|
|
|
|
|
use Message::Field::Address; |
|
|
my $addr = Message::Field::Address->parse ($field_body); |
|
|
|
|
|
for my $i (@$addr) { |
|
|
if ($i->{type} eq 'group') { |
|
|
print "\x40 $i->{display_name}: \n"; |
|
|
for my $j (@{$i->{address}}) { |
|
|
print "\t- $j->{display_name} <$j->{route}$j->{addr_spec}>\n"; |
|
|
} |
|
|
} else { |
|
|
print "- $i->{display_name} <$i->{route}$i->{addr_spec}>\n"; |
|
|
} |
|
|
} |
|
78 |
|
|
79 |
=head1 LICENSE |
=head1 LICENSE |
80 |
|
|