DNS-WorldWideDns

 view release on metacpan or  search on metacpan

lib/DNS/WorldWideDns.pm  view on Meta::CPAN

package DNS::WorldWideDns;

BEGIN {
    use vars qw($VERSION);
    $VERSION     = '0.0102';
}


use strict;
use Class::InsideOut qw(readonly private id register);
use Exception::Class (
        'Exception' => {
            description => 'A general error.',
        },

        'MissingParam' => {
            isa         => 'Exception',
            description => 'Expected a parameter that was not specified.',
        },

        'InvalidParam' => {
            isa         => 'Exception',
            description => 'A parameter passed in did not match what it was supposed to be.',
            fields      => [qw(got)],
        },

        'InvalidAccount' => {
            isa         => 'RequestError',
            description => 'Authentication failed.',
        },

        'RequestError' => {
            isa         => 'Exception',
            description => 'Something bad happened during the request.',
            fields      => [qw(url response code)],
        },

    );
use HTTP::Request;
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;

readonly username => my %username;
readonly password => my %password;



=head1 NAME

DNS::WorldWideDns - An interface to the worldwidedns.net service.

=head1 SYNOPSIS

 use DNS::WorldWideDns;
 
 $dns = DNS::WorldWideDns->new($user, $pass);
 
 $hashRef = $dns->getDomains;
 $hashRef = $dns->getDomain($domain);
 
 $dns->addDomain($domain);
 $dns->updateDomain($domain, $properties);
 $dns->deleteDomain($domain);

=head1 DESCRIPTION

This module allows you to dynamically create, remove, update, delete, and report on domains hosted at L<http://www.worldwidedns.net>. It makes working with their sometimes obtuse, but very useful, DNS API protocol (L<http://www.worldwidedns.net/dns_a...

=head1 USAGE

The following methods are available from this class:

=cut


###############################################################

=head2 addDomain ( domain, [ isPrimary, isDynamic ] )

Adds a domain to your account. Throws MissingParam, InvalidParam, InvalidAccount and RequestError.

B<NOTE:> You should use updateDomain() directly after adding a domain to give it some properties and records.

Returns a 1 on success.

=head3 domain

A domain to add.

=head3 isPrimary

A boolean indicating if this is a primary domain, or a secondary. Defaults to 1.

B<NOTE:> This module currently only supports primary domains.

=head3 isDynamic

A boolean indicating whether this domain is to allow Dynamic DNS ip updating. Defaults to 0.

=cut

lib/DNS/WorldWideDns.pm  view on Meta::CPAN

    # hostmaster, refresh, retry, expires, ttl
    if ($content =~ m{^\@\s+IN\s+SOA\s+[\w\.\-]+\.\s+([\w\.\-]+)\.\s+\d+\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*$}xmsi) {
        $domain{hostmaster}     = $1;
        $domain{refresh}        = $2;
        $domain{retry}          = $3;
        $domain{expire}         = $4;
        $domain{ttl}            = $5;
    }
    
    # records
    while ($content =~ m{^(\@|\*|[\w\.\-]+)?\s+(\d+)?\s*(?:IN)?\s+(A|A6|AAAA|AFSDB|CNAME|DNAME|HINFO|ISDN|MB|MG|MINFO|MR|MX|NS|NSAP|PTR|RP|RT|SRV|TXT|X25)\s+(.*?)\s*$}xmsig) {
        push @{$domain{records}}, {
            name   => $1,
            ttl    => $2,
            type   => $3,
            data   => $4,
        };
    }
    
    return \%domain;
}


###############################################################

=head2 getDomains ( )

Returns a hash reference where the key is the domain and the value is either a 'Primary' or an 'Secondary'. Throws InvalidAccount and RequestError.

B<NOTE:> This module does not currently handle creating, reading, or updating secondary domains, but it may in the future, so this indicator is left in.

=cut

sub getDomains {
    my $self = shift;
    my $url = 'https://www.worldwidedns.net/api_dns_list.asp?NAME='.$self->username.'&PASSWORD='.$self->password;
    my $content = $self->makeRequest($url)->content; 
    my %domains;
    while ($content =~ m{([\w+\.\-]+)\x1F(P|S)}xmsig) {
        my $type = ($2 eq 'P') ? 'Primary' : 'Secondary';
        $domains{$1} = $type;
    }
    return \%domains;
}


###############################################################

=head2 makeRequest ( url, [ request ] )

Makes a GET request. Returns the HTTP::Response from the request. Throws MissingParam, InvalidParam, InvalidAccount and RequestError.

B<NOTE:> Normally you never need to use this method, it's used by the other methods in this class. However, it may be useful in subclassing this module.

=head3 url

The URL to request.

=head3 request

Normally an HTTP::Request object is created for you on the fly. But if you want to make your own and pass it in you are welcome to do so.

=cut

sub makeRequest {
    my ($self, $url, $request) = @_;
	unless (defined $url) {
        MissingParam->throw(error=>'Need a url.');
    }
	unless ($url =~ m{^https://www.worldwidedns.net/.*$}xms) {
        InvalidParam->throw(error=>'URL is improperly formatted.', got=>$url);
    }
    $request ||= HTTP::Request->new(GET => $url);
    my $ua = LWP::UserAgent->new;
    my $response = $ua->request($request);
    
    # request is good
    if ($response->is_success) {
        my $content = $response->content;
        chomp $content;
        
        # is our account still active
        if ($content eq "401") {
            InvalidAccount->throw(
                error       => 'Login suspended.',
                url         => $url,
                code        => 401,
                response    => $response,
            );
        }
        
        # is our user/pass good
        elsif ($content eq "403") {
            InvalidAccount->throw(
                error       => 'Invalid user/pass combination.',
                url         => $url,
                code        => 403,
                response    => $response,
            );
        }
        
        # we're good, let's get back to work
        return $response;
    }
    
    # the request went totally off the reservation
    RequestError->throw(
        error       => $response->message,
        url         => $url,
        response    => $response,
    );

}

###############################################################

=head2 new ( username, password )

Constructor. Throws MissingParam.

=head3 username

Your worldwidedns.net username.

=head3 password

The password to go with username.

=cut

sub new {
    my ($class, $username, $password) = @_;



( run in 0.403 second using v1.01-cache-2.11-cpan-aadc1410aed )