File Coverage

blib/lib/DNS/Zone/Label.pm
Criterion Covered Total %
statement 32 101 31.6
branch 3 68 4.4
condition 1 9 11.1
subroutine 9 16 56.2
pod 0 11 0.0
total 45 205 21.9


line stmt bran cond sub pod time code
1             #!/usr/local/bin/perl -w
2             ######################################################################
3             #
4             # DNS/Zone/Label.pm
5             #
6             # $Id: Label.pm,v 1.5 2003/02/04 15:37:35 awolf Exp $
7             # $Revision: 1.5 $
8             # $Author: awolf $
9             # $Date: 2003/02/04 15:37:35 $
10             #
11             # Copyright (C)2001-2003 Andy Wolf. All rights reserved.
12             #
13             # This library is free software; you can redistribute it and/or
14             # modify it under the same terms as Perl itself.
15             #
16             ######################################################################
17              
18             package DNS::Zone::Label;
19              
20 2     2   676 no warnings 'portable';
  2         3  
  2         90  
21 2     2   29 use 5.6.0;
  2         7  
  2         90  
22 2     2   12 use strict;
  2         4  
  2         67  
23 2     2   9 use warnings;
  2         4  
  2         1656  
24              
25             my $VERSION = '0.85';
26             my $REVISION = sprintf("%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/);
27              
28             ###
29             # The label name is always relative to
30             # the zone name. Default type is '' and
31             # represents a comment.
32             ###
33             sub new {
34 4     4 0 9 my($pkg, $label) = @_;
35 4   33     19 my $class = ref($pkg) || $pkg;
36              
37 4         20 my $self = {
38             '_ID' => undef,
39             'LABEL' => $label,
40             'RECORDS' => [],
41             };
42              
43 4         10 bless($self, $class);
44              
45 4         19 return $self;
46             }
47              
48             sub id {
49 0     0 0 0 my($self, $id) = @_;
50            
51 0 0       0 $self->{'_ID'} = $id if($id);
52            
53 0         0 return($self->{'_ID'});
54             }
55              
56             sub label {
57 29     29 0 38 my($self, $label) = @_;
58            
59 29 50       53 $self->{'LABEL'} = $label if($label);
60            
61 29         133 return($self->{'LABEL'});
62             }
63              
64             sub add {
65 17     17 0 23 my($self, $record) = @_;
66            
67 17         16 push @{ $self->{'RECORDS'} }, ($record);
  17         35  
68            
69 17         43 return $record;
70              
71             }
72              
73             sub delete {
74 0     0 0 0 my($self, $record) = @_;
75            
76 0         0 my $found = 0;
77 0         0 my @array = $self->records();
78            
79 0         0 for (my $i=0 ; $array[$i] ; $i++) {
80 0 0       0 if($array[$i] == $record) {
81 0         0 $found = 1;
82 0         0 splice @array, $i, 1;
83             }
84             }
85              
86 0         0 $self->records(@array);
87            
88 0 0       0 return $found ? $self : undef;
89             }
90              
91             sub record {
92 0     0 0 0 my($self, $ref) = @_;
93 0         0 my $record;
94              
95 0 0 0     0 if(exists $ref->{'ID'} && $ref->{'ID'}) {
    0 0        
96 0 0       0 map { $record = $_ if($_->id() eq $ref->{'ID'}) } $self->records();
  0         0  
97             }
98             elsif(exists $ref->{'TYPE'} && $ref->{'TYPE'}) {
99 0 0       0 map { $record = $_ if($_->type() eq $ref->{'TYPE'}) } $self->records();
  0         0  
100             }
101            
102 0         0 return $record;
103             }
104              
105             sub records {
106 8     8 0 14 my($self, @records) = @_;
107            
108 8 50       19 $self->{'RECORDS'} = \@records if(scalar @records);
109            
110 8 50       29 my @result = @{ $self->{'RECORDS'} } if(ref($self->{'RECORDS'}) eq 'ARRAY');
  8         23  
111              
112 8         1151 return @result;
113             }
114              
115             sub dump {
116 0     0 0   my($self, $format, $origin, $ttl_default) = @_;
117              
118 0           my @records = $self->sort()->records();
119            
120 0           my $label = $self->{'LABEL'};
121 0           $label =~ s/\.$origin\.*$//;
122 0 0         $label = '@' if($label eq $origin);
123            
124 0           my $first = 1;
125 0           foreach my $record (@records) {
126 0 0         $label = $first ? $label : '';
127            
128 0           $record->dump($label, $format, $ttl_default);
129            
130 0 0         $first = 0 if($record->type() ne '');
131             }
132            
133 0           return $self;
134             }
135              
136             sub toXML {
137 0     0 0   my($self) = @_;
138 0           my $result;
139            
140 0           $result .= qq(
141 0           $result .= qq(\n) . $self->label() . qq(\n);
142              
143 0           map { $result .= $_->toXML() } $self->records();
  0            
144              
145 0           $result .= qq(\n);
146            
147 0           return $result;
148             }
149              
150             sub debug {
151 0     0 0   my($self) = @_;
152            
153 0 0         return undef unless($self);
154            
155 0           eval {
156 2     2   11 use Data::Dumper;
  2         3  
  2         766  
157            
158 0           print Dumper($self);
159             };
160            
161 0           return $self;
162             }
163              
164             sub sort {
165 0     0 0   my($self) = @_;
166            
167 0 0         my @result = sort {
168 0           return 1 if($b->type() eq '');
169 0 0         return -1 if($a->type() eq '');
170 0 0         return 1 if($b->type() eq 'IN SOA');
171 0 0         return -1 if($a->type() eq 'IN SOA');
172 0 0         return 1 if($b->type() eq 'IN A');
173 0 0         return -1 if($a->type() eq 'IN A');
174 0 0         return 1 if($b->type() eq 'IN NS');
175 0 0         return -1 if($a->type() eq 'IN NS');
176 0 0         return 1 if($b->type() eq 'IN MX');
177 0 0         return -1 if($a->type() eq 'IN MX');
178 0 0         return 1 if($b->type() eq 'IN CNAME');
179 0 0         return -1 if($a->type() eq 'IN CNAME');
180 0 0         return 1 if($b->type() eq 'IN TXT');
181 0 0         return -1 if($a->type() eq 'IN TXT');
182 0 0         return 1 if($b->type() eq 'IN PTR');
183 0 0         return -1 if($a->type() eq 'IN PTR');
184 0 0         return 1 if($b->type() eq 'IN HINFO');
185 0 0         return -1 if($a->type() eq 'IN HINFO');
186 0 0         return 1 if($b->type() eq 'IN WKS');
187 0 0         return -1 if($a->type() eq 'IN WKS');
188              
189 0           return 0;
190             } $self->records();
191            
192 0           $self->records(@result);
193              
194 0           return $self;
195             }
196              
197             1;
198              
199             __END__