/[suikacvs]/messaging/manakai/lib/Message/Field/Domain.pm
Suika

Contents of /messaging/manakai/lib/Message/Field/Domain.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download)
Thu May 16 11:43:40 2002 UTC (22 years, 6 months ago) by wakaba
Branch: MAIN
Changes since 1.2: +5 -7 lines
2002-05-16  wakaba <w@suika.fam.cx>

	* Date.pm: Remade.

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.3 $VERSION=do{my @r=(q$Revision: 1.2 $=~/\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     -format_ipv6 => '[%s]',
43     -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     );
52     sub _init ($;%) {
53     my $self = shift;
54 wakaba 1.3 my $DEFAULT = Message::Util::make_clone (\%DEFAULT);
55     $self->SUPER::_init (%$DEFAULT, @_);
56 wakaba 1.1 }
57    
58     =item $addr = Message::Field::Domain->new ([%options])
59    
60     Constructs a new object. You might pass some options as parameters
61     to the constructor.
62    
63     =cut
64    
65     ## Inherited
66    
67     =item $addr = Message::Field::Domain->parse ($field-body, [%options])
68    
69     Constructs a new object with given field body. You might pass
70     some options as parameters to the constructor.
71    
72     =cut
73    
74     sub parse ($$;%) {
75     my $class = shift;
76     my $self = bless {}, $class;
77     my $body = shift;
78     $self->_init (@_);
79     ($body, @{$self->{comment}})
80     = $self->Message::Util::delete_comment_to_array ($body)
81     if $self->{option}->{use_comment};
82     my @d;
83     $body =~ s{($REG{domain_literal}|[^\x5B\x2E])+}{
84     my ($d, $isd) = ($&, 0);
85     $d =~ s/^$REG{WSP}+//; $d =~ s/$REG{WSP}+$//;
86     ($d, $isd) = Message::Util::unquote_if_domain_literal ($d);
87     my %s = &{$self->{option}->{hook_decode_string}}
88     ($self, $d, type => 'domain'.($isd?'/literal':''));
89     push @d, $s{value};
90     }gex;
91     if (@d == 1 && $d[0] =~ /^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/) {
92     $d[0] = pack 'C4', $1, $2, $3, $4;
93     $self->{type} = 'ipv4';
94     } elsif (@d == 1 && $d[0] !~ /[^0-9:.]/) {
95     $self->{type} = 'ipv6';
96     } elsif (@d == 4) {
97 wakaba 1.2 if ($d[0] !~ /[^0-9]/ && 0 <= $d[0] && $d[0] < 256
98     && $d[1] !~ /[^0-9]/ && 0 <= $d[1] && $d[1] < 256
99     && $d[2] !~ /[^0-9]/ && 0 <= $d[2] && $d[2] < 256
100     && $d[3] !~ /[^0-9]/ && 0 <= $d[3] && $d[3] < 256) {
101 wakaba 1.1 $self->{type} = 'ipv4';
102     @d = (pack ('C4', @d));
103     }
104     }
105     $self->{type} ||= 'name';
106     $self->{value} = \@d;
107     $self;
108     }
109    
110     sub reverse ($) {$_[0]->{value} = [reverse @{$_[0]->{value}}];$_[0]}
111     sub type ($;$) {
112     my $self = shift;
113     my $newtype = shift;
114     if ($newtype) {
115     $self->{type} = $newtype;
116     }
117     $self->{type};
118     }
119    
120     sub stringify ($;%) {
121     my $self = shift;
122     my %o = @_; my %option = %{$self->{option}};
123     for (grep {/^-/} keys %o) {$option{substr ($_, 1)} = $o{$_}}
124     my $s = '';
125     if ($self->{type} eq 'ipv6') {
126     $s = sprintf $option{format_ipv6}, $self->{value}->[0];
127     } elsif ($self->{type} eq 'ipv4') {
128     $s = sprintf $option{format_ipv4}, $self->{value}->[0];
129     } else {
130     my $dl = 0;
131     my $d = join $option{separator}, map {
132     my %s = &{$option{hook_encode_string}} ($self,
133     $_, type => 'domain');
134     if ($option{use_domain_literal} && $s{value} =~ /$REG{NON_atext}/) {
135     $s{value} =~ s/[\x5B-\x5D]/\x5C$&/g;
136     $s{value} = sprintf $option{format_name_literal}, $s{value};
137     }
138     $s{value};
139     } @{$self->{value}};
140     $s = sprintf $option{format_name}, $d;
141     }
142     if ($option{output_comment} && @{$self->{comment}} > 0) {
143     $s .= ' ' . $self->_comment_stringify;
144     }
145     $s;
146     }
147    
148     =head1 LICENSE
149    
150     Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
151    
152     This program is free software; you can redistribute it and/or modify
153     it under the terms of the GNU General Public License as published by
154     the Free Software Foundation; either version 2 of the License, or
155     (at your option) any later version.
156    
157     This program is distributed in the hope that it will be useful,
158     but WITHOUT ANY WARRANTY; without even the implied warranty of
159     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
160     GNU General Public License for more details.
161    
162     You should have received a copy of the GNU General Public License
163     along with this program; see the file COPYING. If not, write to
164     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
165     Boston, MA 02111-1307, USA.
166    
167     =head1 CHANGE
168    
169     See F<ChangeLog>.
170 wakaba 1.3 $Date: 2002/05/15 07:29:09 $
171 wakaba 1.1
172     =cut
173    
174     1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24