blib/lib/API/DirectAdmin/DNS.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 135 CC 14 SCAR 47.8
CC
14
Cov
0%
_parse_zone
182.0
dumpzone
6.0
add_record
2.0
CRAP 119.4
1 package API::DirectAdmin::DNS;
2  
3 use Modern::Perl '2010';
4 use Carp;
5  
6 use base 'API::DirectAdmin::Component';
7  
8 our $VERSION = 0.06;
9  
10 # Return zone dump
11 # params: domain
12 sub dumpzone {
13 my ($self, $params ) = @_;
14
15 my %add_params = (
16 noparse => 1,
17 );
18
19 my %params = (%$params, %add_params);
20
21 my $zone = $self->directadmin->query(
22 params => \%params,
23 command => 'CMD_API_DNS_CONTROL',
24 allowed_fields => 'domain noparse',
25 );
26  
27 return _parse_zone($zone, $params->{domain}. '.', '') unless ref $zone eq 'HASH';
28 return $zone;
29 }
30  
31 # Add records A, MX, CNAME, NS, PTR, TXT, AAAA
32 # params: domain, type, name, value
33 sub add_record {
34 my ($self, $params ) = @_;
35  
36 my %add_params = (
37 action => 'add',
38 );
39
40 my %params = (%$params, %add_params);
41  
42 return $self->directadmin->query(
43 params => \%params,
44 command => 'CMD_API_DNS_CONTROL',
45 method => 'POST',
46 allowed_fields => "type name action value domain",
47 );
48 }
49  
50 # Remove records A, MX, CNAME, NS, PTR, TXT, AAAA, SRV
51 # params: domain, type, name, value
52 sub remove_record {
53 my ($self, $params ) = @_;
54
55 my %add_params = (
56 action => 'select',
57 lc $params->{type} . 'recs0' => "name=$params->{name}&value=$params->{value}",
58 );
59
60 delete $params->{type};
61
62 my %params = (%$params, %add_params);
63  
64 return $self->directadmin->query(
65 params => \%params,
66 command => 'CMD_API_DNS_CONTROL',
67 method => 'POST',
68 allowed_fields => 'domain
69 action
70 name
71 value
72 arecs0
73 mxrecs0
74 txtrecs0
75 aaaarecs0
76 nsrecs0
77 cnamerecs0
78 srvrecs0
79 ptrrecs0',
80 );
81 }
82  
83 # Special parser for zone dump
84 # Cropped code from Parse::DNS::Zone
85 sub _parse_zone {
86 my ($zonetext, $origin) = @_;
87
88 my $mrow;
89 my $prev;
90 my %zone;
91  
92 my $zentry = qr/^
93 (\S+)\s+ # name
94 (
95 (?: (?: IN | CH | HS ) \s+ \d+ \s+ ) |
96 (?: \d+ \s+ (?: IN | CH | HS ) \s+ ) |
97 (?: (?: IN | CH | HS ) \s+ ) |
98 (?: \d+ \s+ ) |
99 )? # <ttl> <class> or <class> <ttl>
100 (\S+)\s+ # type
101 (.*) # rdata
102 $/ix;
103  
104 foreach ( split /\n+/, $zonetext ) {
105  
106 chomp;
107 s/;.*$//;
108 next if /^\s*$/;
109 s/\s+/ /g;
110
111 s/^\@ /$origin /g;
112 s/ \@ / $origin /g;
113 s/ \@$/ $origin/g;
114  
115 # handles mutlirow entries, with ()
116 if($mrow) {
117 $mrow.=$_;
118
119 next if(! /\)/);
120  
121 # End of multirow
122 $mrow=~s/[\(\)]//g;
123 $mrow=~s/\n//mg;
124 $mrow=~s/\s+/ /g;
125 $mrow .= "\n";
126  
127 $_ = $mrow;
128 undef $mrow;
129 } elsif(/^.*\([^\)]*$/) {
130 # Start of multirow
131 $mrow.=$_;
132 next;
133 }
134  
135 if(/^ /) {
136 s/^/$prev/;
137 }
138  
139 $origin = $1, next if(/^\$ORIGIN ([\w\-\.]+)\s*$/i);
140  
141 my($name,$ttlclass,$type,$rdata) = /$zentry/;
142  
143 my($ttl, $class);
144 if(defined $ttlclass) {
145 ($ttl) = $ttlclass=~/(\d+)/o;
146 ($class) = $ttlclass=~/(CH|IN|HS)/io;
147  
148 $ttlclass=~s/\d+//;
149 $ttlclass=~s/(?:CH|IN|HS)//;
150 $ttlclass=~s/\s//g;
151 if($ttlclass) {
152 next;
153 }
154 }
155  
156 $ttl = defined $ttl ? $ttl : 14400;
157  
158 next if (!$name || !$type || !$rdata);
159  
160 $prev=$name;
161 $name.=".$origin" if $name ne $origin && $name !~ /\.$/;
162  
163 if($type =~ /^(?:cname|afsdb|mx|ns)$/i and
164 $rdata ne $origin and $rdata !~ /\.$/) {
165 $rdata.=".$origin";
166 }
167  
168 push(@{$zone{lc $name}{lc $type}{rdata}}, $rdata);
169 push(@{$zone{lc $name}{lc $type}{ttl}}, $ttl);
170 push(@{$zone{lc $name}{lc $type}{class}}, $class);
171 }
172  
173 return \%zone;
174 }
175  
176 1;