| 1 |
wakaba |
1.1 |
|
| 2 |
|
|
=head1 NAME |
| 3 |
|
|
|
| 4 |
|
|
Message::Field::Domain --- A perl module for an Internet |
| 5 |
|
|
domain name which is part of Internet Messages |
| 6 |
|
|
|
| 7 |
|
|
=cut |
| 8 |
|
|
|
| 9 |
|
|
package Message::Field::Domain; |
| 10 |
|
|
require 5.6.0; |
| 11 |
|
|
use strict; |
| 12 |
|
|
use re 'eval'; |
| 13 |
|
|
use vars qw(%DEFAULT @ISA %REG $VERSION); |
| 14 |
wakaba |
1.4 |
$VERSION=do{my @r=(q$Revision: 1.3 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
| 15 |
wakaba |
1.1 |
require Message::Field::Structured; |
| 16 |
|
|
push @ISA, qw(Message::Field::Structured); |
| 17 |
|
|
|
| 18 |
wakaba |
1.3 |
%REG = %Message::Util::REG; |
| 19 |
wakaba |
1.1 |
|
| 20 |
|
|
=head1 CONSTRUCTORS |
| 21 |
|
|
|
| 22 |
|
|
The following methods construct new objects: |
| 23 |
|
|
|
| 24 |
|
|
=over 4 |
| 25 |
|
|
|
| 26 |
|
|
=cut |
| 27 |
|
|
|
| 28 |
|
|
## Initialize of this class -- called by constructors |
| 29 |
|
|
%DEFAULT = ( |
| 30 |
|
|
-_ARRAY_NAME => 'value', |
| 31 |
|
|
-_MEMBERS => [qw|type|], |
| 32 |
|
|
-_METHODS => [qw|reverse type|], |
| 33 |
|
|
-allow_special_name => 1, ## not implemented yet |
| 34 |
|
|
-allow_special_ipv4 => 1, ## |
| 35 |
|
|
-allow_special_ipv6 => 1, ## |
| 36 |
|
|
-encoding_after_encode => 'unknown-8bit', |
| 37 |
|
|
-encoding_before_decode => 'unknown-8bit', |
| 38 |
|
|
#field_param_name |
| 39 |
|
|
#field_name |
| 40 |
|
|
#format |
| 41 |
|
|
-format_ipv4 => '[%vd]', |
| 42 |
wakaba |
1.4 |
-format_ipv6 => '[IPv6:%s]', |
| 43 |
wakaba |
1.1 |
-format_name => '%s', |
| 44 |
|
|
-format_name_literal => '[%s]', |
| 45 |
|
|
#hook_encode_string |
| 46 |
|
|
#hook_decode_string |
| 47 |
|
|
-output_comment => 0, |
| 48 |
|
|
-separator => '.', |
| 49 |
|
|
-use_comment => 0, |
| 50 |
|
|
-use_domain_literal => 1, |
| 51 |
wakaba |
1.4 |
-use_ipv4_address => 1, |
| 52 |
|
|
-use_ipv6_address => 1, |
| 53 |
wakaba |
1.1 |
); |
| 54 |
|
|
sub _init ($;%) { |
| 55 |
|
|
my $self = shift; |
| 56 |
wakaba |
1.3 |
my $DEFAULT = Message::Util::make_clone (\%DEFAULT); |
| 57 |
|
|
$self->SUPER::_init (%$DEFAULT, @_); |
| 58 |
wakaba |
1.1 |
} |
| 59 |
|
|
|
| 60 |
|
|
=item $addr = Message::Field::Domain->new ([%options]) |
| 61 |
|
|
|
| 62 |
|
|
Constructs a new object. You might pass some options as parameters |
| 63 |
|
|
to the constructor. |
| 64 |
|
|
|
| 65 |
|
|
=cut |
| 66 |
|
|
|
| 67 |
|
|
## Inherited |
| 68 |
|
|
|
| 69 |
|
|
=item $addr = Message::Field::Domain->parse ($field-body, [%options]) |
| 70 |
|
|
|
| 71 |
|
|
Constructs a new object with given field body. You might pass |
| 72 |
|
|
some options as parameters to the constructor. |
| 73 |
|
|
|
| 74 |
|
|
=cut |
| 75 |
|
|
|
| 76 |
|
|
sub parse ($$;%) { |
| 77 |
|
|
my $class = shift; |
| 78 |
|
|
my $self = bless {}, $class; |
| 79 |
|
|
my $body = shift; |
| 80 |
|
|
$self->_init (@_); |
| 81 |
|
|
($body, @{$self->{comment}}) |
| 82 |
|
|
= $self->Message::Util::delete_comment_to_array ($body) |
| 83 |
|
|
if $self->{option}->{use_comment}; |
| 84 |
|
|
my @d; |
| 85 |
|
|
$body =~ s{($REG{domain_literal}|[^\x5B\x2E])+}{ |
| 86 |
|
|
my ($d, $isd) = ($&, 0); |
| 87 |
|
|
$d =~ s/^$REG{WSP}+//; $d =~ s/$REG{WSP}+$//; |
| 88 |
|
|
($d, $isd) = Message::Util::unquote_if_domain_literal ($d); |
| 89 |
|
|
my %s = &{$self->{option}->{hook_decode_string}} |
| 90 |
|
|
($self, $d, type => 'domain'.($isd?'/literal':'')); |
| 91 |
|
|
push @d, $s{value}; |
| 92 |
|
|
}gex; |
| 93 |
wakaba |
1.4 |
if ($self->{option}->{use_ipv4_address} && @d == 1 && $d[0] =~ /^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/) { |
| 94 |
wakaba |
1.1 |
$d[0] = pack 'C4', $1, $2, $3, $4; |
| 95 |
|
|
$self->{type} = 'ipv4'; |
| 96 |
wakaba |
1.4 |
} elsif ($self->{option}->{use_ipv6_address} && @d == 1 && $d[0] =~ s/^IPv6://i) { |
| 97 |
wakaba |
1.1 |
$self->{type} = 'ipv6'; |
| 98 |
wakaba |
1.4 |
} elsif ($self->{option}->{use_ipv6_address} && @d == 1 && $d[0] !~ /[^0-9:.]/) { |
| 99 |
|
|
$self->{type} = 'ipv6'; |
| 100 |
|
|
} elsif ($self->{option}->{use_ipv4_address} && @d == 4) { |
| 101 |
wakaba |
1.2 |
if ($d[0] !~ /[^0-9]/ && 0 <= $d[0] && $d[0] < 256 |
| 102 |
|
|
&& $d[1] !~ /[^0-9]/ && 0 <= $d[1] && $d[1] < 256 |
| 103 |
|
|
&& $d[2] !~ /[^0-9]/ && 0 <= $d[2] && $d[2] < 256 |
| 104 |
|
|
&& $d[3] !~ /[^0-9]/ && 0 <= $d[3] && $d[3] < 256) { |
| 105 |
wakaba |
1.1 |
$self->{type} = 'ipv4'; |
| 106 |
|
|
@d = (pack ('C4', @d)); |
| 107 |
|
|
} |
| 108 |
|
|
} |
| 109 |
|
|
$self->{type} ||= 'name'; |
| 110 |
|
|
$self->{value} = \@d; |
| 111 |
|
|
$self; |
| 112 |
|
|
} |
| 113 |
|
|
|
| 114 |
|
|
sub reverse ($) {$_[0]->{value} = [reverse @{$_[0]->{value}}];$_[0]} |
| 115 |
|
|
sub type ($;$) { |
| 116 |
|
|
my $self = shift; |
| 117 |
|
|
my $newtype = shift; |
| 118 |
|
|
if ($newtype) { |
| 119 |
|
|
$self->{type} = $newtype; |
| 120 |
|
|
} |
| 121 |
|
|
$self->{type}; |
| 122 |
|
|
} |
| 123 |
|
|
|
| 124 |
|
|
sub stringify ($;%) { |
| 125 |
|
|
my $self = shift; |
| 126 |
|
|
my %o = @_; my %option = %{$self->{option}}; |
| 127 |
|
|
for (grep {/^-/} keys %o) {$option{substr ($_, 1)} = $o{$_}} |
| 128 |
|
|
my $s = ''; |
| 129 |
wakaba |
1.4 |
if ($option{use_ipv6_address} && $self->{type} eq 'ipv6') { |
| 130 |
wakaba |
1.1 |
$s = sprintf $option{format_ipv6}, $self->{value}->[0]; |
| 131 |
wakaba |
1.4 |
} elsif ($option{use_ipv4_address} && $self->{type} eq 'ipv4') { |
| 132 |
wakaba |
1.1 |
$s = sprintf $option{format_ipv4}, $self->{value}->[0]; |
| 133 |
|
|
} else { |
| 134 |
|
|
my $dl = 0; |
| 135 |
|
|
my $d = join $option{separator}, map { |
| 136 |
|
|
my %s = &{$option{hook_encode_string}} ($self, |
| 137 |
|
|
$_, type => 'domain'); |
| 138 |
|
|
if ($option{use_domain_literal} && $s{value} =~ /$REG{NON_atext}/) { |
| 139 |
|
|
$s{value} =~ s/[\x5B-\x5D]/\x5C$&/g; |
| 140 |
|
|
$s{value} = sprintf $option{format_name_literal}, $s{value}; |
| 141 |
|
|
} |
| 142 |
|
|
$s{value}; |
| 143 |
|
|
} @{$self->{value}}; |
| 144 |
|
|
$s = sprintf $option{format_name}, $d; |
| 145 |
|
|
} |
| 146 |
wakaba |
1.4 |
if ($option{use_comment} && $option{output_comment}) { |
| 147 |
|
|
my $c = $self->_comment_stringify; |
| 148 |
|
|
$s .= ' ' . $c if $c; |
| 149 |
wakaba |
1.1 |
} |
| 150 |
|
|
$s; |
| 151 |
|
|
} |
| 152 |
|
|
|
| 153 |
|
|
=head1 LICENSE |
| 154 |
|
|
|
| 155 |
|
|
Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>. |
| 156 |
|
|
|
| 157 |
|
|
This program is free software; you can redistribute it and/or modify |
| 158 |
|
|
it under the terms of the GNU General Public License as published by |
| 159 |
|
|
the Free Software Foundation; either version 2 of the License, or |
| 160 |
|
|
(at your option) any later version. |
| 161 |
|
|
|
| 162 |
|
|
This program is distributed in the hope that it will be useful, |
| 163 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 164 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 165 |
|
|
GNU General Public License for more details. |
| 166 |
|
|
|
| 167 |
|
|
You should have received a copy of the GNU General Public License |
| 168 |
|
|
along with this program; see the file COPYING. If not, write to |
| 169 |
|
|
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 170 |
|
|
Boston, MA 02111-1307, USA. |
| 171 |
|
|
|
| 172 |
|
|
=head1 CHANGE |
| 173 |
|
|
|
| 174 |
|
|
See F<ChangeLog>. |
| 175 |
wakaba |
1.4 |
$Date: 2002/05/16 11:43:40 $ |
| 176 |
wakaba |
1.1 |
|
| 177 |
|
|
=cut |
| 178 |
|
|
|
| 179 |
|
|
1; |