Unverified Commit 2a3d0db0 authored by Gavin Brown's avatar Gavin Brown
Browse files

enhancements. implements domain update!

parent e33eeecb
...@@ -47,7 +47,7 @@ sub request { ...@@ -47,7 +47,7 @@ sub request {
my $response = $self->SUPER::request($frame); my $response = $self->SUPER::request($frame);
if (!$response) { if (!$response) {
$self->{'outfh'}->print(sprintf("%04d %s\n", $Net::EPP::Simple::Code, $Net::EPP::Simple::Error)); $self->{'outfh'}->print(sprintf("%s%04d%s %s\n", color($Net::EPP::Simple::Code < 2000 ? 'green' : 'red'), $Net::EPP::Simple::Code, color('reset'), $Net::EPP::Simple::Message));
} else { } else {
return $response; return $response;
...@@ -92,6 +92,7 @@ sub EndDocument { ...@@ -92,6 +92,7 @@ sub EndDocument {
} }
package main; package main;
use Term::ANSIColor;
use Term::ReadLine; use Term::ReadLine;
use Net::EPP::Simple; use Net::EPP::Simple;
use Getopt::Long; use Getopt::Long;
...@@ -227,7 +228,7 @@ while (1) { ...@@ -227,7 +228,7 @@ while (1) {
$term->WriteHistory($histfile); $term->WriteHistory($histfile);
$epp->logout if ($epp->connected && $epp->authenticated); handle_logout() if ($epp->connected && $epp->authenticated);
$epp->disconnect if ($epp->connected); $epp->disconnect if ($epp->connected);
note('bye!'); note('bye!');
...@@ -255,7 +256,10 @@ sub handle_timeout { ...@@ -255,7 +256,10 @@ sub handle_timeout {
} }
sub handle_ssl { sub handle_ssl {
if ($_[0] eq 'on') { if ($epp->connected) {
return error("Already connected");
} elsif ($_[0] eq 'on') {
$epp->{'ssl'} = 1; $epp->{'ssl'} = 1;
note('SSL enabled'); note('SSL enabled');
...@@ -271,19 +275,32 @@ sub handle_ssl { ...@@ -271,19 +275,32 @@ sub handle_ssl {
} }
sub handle_host { sub handle_host {
$epp->{'host'} = $_[0]; if ($epp->connected) {
note("Host set to %s", $_[0]); return error("Already connected");
return 1;
} else {
$epp->{'host'} = $_[0];
note("Host set to %s", $_[0]);
return 1;
}
} }
sub handle_port { sub handle_port {
$epp->{'port'} = int($_[0]); if ($epp->connected) {
note("Port set to %d", $_[0]); return error("Already connected");
return 1;
} else {
$epp->{'port'} = int($_[0]);
note("Port set to %d", $_[0]);
return 1;
}
} }
sub handle_connect { sub handle_connect {
if ($epp->{'host'} eq '') { if ($epp->connected) {
return error("Already connected");
} elsif ($epp->{'host'} eq '') {
return error('No host specified'); return error('No host specified');
} else { } else {
...@@ -312,44 +329,53 @@ sub handle_credentials { ...@@ -312,44 +329,53 @@ sub handle_credentials {
return error('Missing password'); return error('Missing password');
} else { } else {
handle_id($_[0]); handle_id($_[0]) && handle_pw($_[1]);
handle_pw($_[1]);
return 1; return 1;
} }
} }
sub handle_id { sub handle_id {
$epp->{'user'} = shift; if ($epp->authenticated) {
note("User ID set to '%s'", $epp->{'user'}); return error("Already authenticated");
} else {
$epp->{'user'} = shift;
note("User ID set to '%s'", $epp->{'user'});
}
} }
sub handle_pw { sub handle_pw {
$epp->{'pass'} = shift; if ($epp->authenticated) {
note("Password set to '%s'", ('*' x length($epp->{'pass'}))); return error("Already authenticated");
} else {
$epp->{'pass'} = shift;
note("Password set to '%s'", ('*' x length($epp->{'pass'})));
}
} }
sub handle_login { sub handle_login {
my $verbose = shift; my $verbose = shift;
if ($epp->{'host'} eq '') { if (!$epp->connected) {
return error('No host specified');
} elsif ($epp->{'user'} eq '' || $epp->{'pass'} eq '') {
return error('No credentials specified');
} elsif (!$epp->connected) {
return handle_connect() && handle_login(); return handle_connect() && handle_login();
} elsif ($epp->authenticated) { } elsif ($epp->authenticated) {
return error('Already logged in'); return error('Already logged in');
} elsif ($epp->{'host'} eq '') {
return error('No host specified');
} elsif ($epp->{'user'} eq '' || $epp->{'pass'} eq '') {
return error('No credentials specified');
} else { } else {
$epp->{'quiet'} = ($verbose ? 0 : 1); $epp->{'quiet'} = ($verbose ? 0 : 1);
my $result = $epp->_login; my $result = $epp->_login;
$epp->{'quiet'} = 0; $epp->{'quiet'} = 0;
note("%04d %s", $Net::EPP::Simple::Code, $Net::EPP::Simple::Message); note("%s%04d%s %s", color($Net::EPP::Simple::Code < 2000 ? 'green' : 'red'), $Net::EPP::Simple::Code, color('reset'), $Net::EPP::Simple::Message);
return $result; return $result;
} }
} }
...@@ -362,35 +388,46 @@ sub handle_logout { ...@@ -362,35 +388,46 @@ sub handle_logout {
$epp->{'quiet'} = 1; $epp->{'quiet'} = 1;
my $result = $epp->logout; my $result = $epp->logout;
$epp->{'quiet'} = 0; $epp->{'quiet'} = 0;
note("%04d %s", $Net::EPP::Simple::Code, $Net::EPP::Simple::Message); note("%s%04d%s %s", color($Net::EPP::Simple::Code < 2000 ? 'green' : 'red'), $Net::EPP::Simple::Code, color('reset'), $Net::EPP::Simple::Message);
return $result; return $result;
} }
} }
sub handle_hello { sub handle_hello {
return $epp->ping; if (!$epp->connected) {
return error('Not connected');
} else {
return $epp->ping;
}
} }
sub handle_check { sub handle_check {
my ($type, $id, @extra) = @_; if (!$epp->authenticated) {
if ($type eq 'domain') { return error('Not connected');
return $epp->check_domain($id);
} elsif ($type eq 'claims') { } else {
return handle_claims_check($id); my ($type, $id, @extra) = @_;
if ($type eq 'domain') {
return $epp->check_domain($id);
} elsif ($type eq 'fee') { } elsif ($type eq 'claims') {
return handle_fee_check($id, @extra); return handle_claims_check($id);
} elsif ($type eq 'host') { } elsif ($type eq 'fee') {
return $epp->check_host($id); return handle_fee_check($id, @extra);
} elsif ($type eq 'contact') { } elsif ($type eq 'host') {
return $epp->check_contact($id); return $epp->check_host($id);
} else { } elsif ($type eq 'contact') {
return error("Unsupported object type '$type'"); return $epp->check_contact($id);
} else {
return error("Unsupported object type '$type'");
}
} }
} }
...@@ -453,56 +490,77 @@ sub handle_fee_check { ...@@ -453,56 +490,77 @@ sub handle_fee_check {
} }
sub handle_info { sub handle_info {
my ($type, $id, $authInfo, $opt) = @_; if (!$epp->authenticated) {
if ($type eq 'domain') { return error('Not connected');
$opt = $opt || 'all';
return $epp->domain_info($id, $authInfo, undef, $opt);
} elsif ($type eq 'host') { } else {
return $epp->host_info($id); my ($type, $id, $authInfo, $opt) = @_;
if ($type eq 'domain') {
$opt = $opt || 'all';
return $epp->domain_info($id, $authInfo, undef, $opt);
} elsif ($type eq 'contact') { } elsif ($type eq 'host') {
return $epp->contact_info($id, $authInfo, $opt); return $epp->host_info($id);
} else { } elsif ($type eq 'contact') {
return error("Unsupported object type '$type'"); return $epp->contact_info($id, $authInfo, $opt);
} else {
return error("Unsupported object type '$type'");
}
} }
} }
sub handle_poll { sub handle_poll {
my ($op, $id) = @_; if (!$epp->authenticated) {
if ($op eq 'req') { return error('Not connected');
my $frame = Net::EPP::Frame::Command::Poll::Req->new;
$epp->request($frame);
} elsif ($op eq 'ack') {
my $frame = Net::EPP::Frame::Command::Poll::Ack->new;
$frame->setMsgID($id);
$epp->request($frame);
} else { } else {
error("Unsupported poll op '$op'"); my ($op, $id) = @_;
if ($op eq 'req') {
my $frame = Net::EPP::Frame::Command::Poll::Req->new;
$epp->request($frame);
} elsif ($op eq 'ack') {
my $frame = Net::EPP::Frame::Command::Poll::Ack->new;
$frame->setMsgID($id);
$epp->request($frame);
} else {
error("Unsupported poll op '$op'");
}
} }
} }
sub handle_send { sub handle_send {
$epp->request($_[0]); if (!$epp->connected) {
return error('Not connected');
} else {
$epp->request($_[0]);
}
} }
sub handle_begin { sub handle_begin {
my $buffer = ''; if (!$epp->connected) {
while (my $line = <STDIN>) { return error('Not connected');
if ($line =~ /^END/) {
last;
} else { } else {
$buffer .= $line; my $buffer = '';
while (my $line = <STDIN>) {
if ($line =~ /^END/) {
last;
} else {
$buffer .= $line;
}
} }
$epp->request($buffer);
} }
$epp->request($buffer);
} }
sub handle_help { sub handle_help {
...@@ -521,26 +579,36 @@ sub handle_exit { ...@@ -521,26 +579,36 @@ sub handle_exit {
} }
sub handle_transfer { sub handle_transfer {
my ($type, $object, $cmd, $authinfo, $period) = @_; if (!$epp->authenticated) {
return error('Not connected');
} else {
my ($type, $object, $cmd, $authinfo, $period) = @_;
return error("invalid object type '%s'", $type) if ($type ne 'domain' && $type ne 'contact'); return error("invalid object type '%s'", $type) if ($type ne 'domain' && $type ne 'contact');
return error("invalid command '%s'", $cmd) if ($cmd ne 'query' && $cmd ne 'request' && $cmd ne 'cancel' && $cmd ne 'approve' && $cmd ne 'reject'); return error("invalid command '%s'", $cmd) if ($cmd ne 'query' && $cmd ne 'request' && $cmd ne 'cancel' && $cmd ne 'approve' && $cmd ne 'reject');
return $epp->_transfer_request($cmd, $type, $object, $authinfo, $period); return $epp->_transfer_request($cmd, $type, $object, $authinfo, $period);
}
} }
sub handle_clone { sub handle_clone {
my ($type, $old, $new) = @_; if (!$epp->authenticated) {
return error('Not connected');
if (lc($type) eq 'contact') { } else {
return handle_contact_clone($old, $new); my ($type, $old, $new) = @_;
} elsif (lc($type) eq 'domain') { if (lc($type) eq 'contact') {
return handle_domain_clone($old, $new); return handle_contact_clone($old, $new);
} else { } elsif (lc($type) eq 'domain') {
error("Unsupported object type '$type'"); return handle_domain_clone($old, $new);
} else {
error("Unsupported object type '$type'");
}
} }
} }
...@@ -576,47 +644,64 @@ sub generate_authinfo { ...@@ -576,47 +644,64 @@ sub generate_authinfo {
} }
sub handle_delete { sub handle_delete {
my ($type, $id) = @_; if (!$epp->authenticated) {
if ($type eq 'domain') { return error('Not connected');
return $epp->delete_domain($id);
} elsif ($type eq 'host') { } else {
return $epp->delete_host($id); my ($type, $id) = @_;
if ($type eq 'domain') {
return $epp->delete_domain($id);
} elsif ($type eq 'contact') { } elsif ($type eq 'host') {
return $epp->delete_contact($id); return $epp->delete_host($id);
} else { } elsif ($type eq 'contact') {
return error("Unsupported object type '$type'"); return $epp->delete_contact($id);
} else {
return error("Unsupported object type '$type'");
}
} }
} }
sub handle_renew { sub handle_renew {
my ($domain, $period) = @_; if (!$epp->authenticated) {
$period = 1 if (!$period); return error('Not connected');
my $info = $epp->domain_info($domain); } else {
return undef if (!$info); my ($domain, $period, $date) = @_;
$period = 1 if (!$period);
if (!$date) {
my $info = $epp->domain_info($domain);
return undef if (!$info);
my ($date, undef) = split(/T/, $info->{'exDate'}, 2); ($date, undef) = split(/T/, $info->{'exDate'}, 2);
}
return $epp->renew_domain({ return $epp->renew_domain({
'name' => $domain, 'name' => $domain,
'period' => $period, 'period' => $period,
'cur_exp_date' => $date, 'cur_exp_date' => $date,
}); });
}
} }
sub handle_create { sub handle_create {
my $type = lc(shift); if (!$epp->authenticated) {
return error('Not connected');
if ($type eq 'host') {
return create_host(@_);
} else { } else {
return error("invalid type '%s'", $type); my $type = lc(shift);
if ($type eq 'host') {
return create_host(@_);
} else {
return error("invalid type '%s'", $type);
}
} }
} }
...@@ -634,78 +719,181 @@ sub create_host { ...@@ -634,78 +719,181 @@ sub create_host {
} }
sub handle_edit { sub handle_edit {
my $file = tmpnam(); if (!$epp->connected) {
if (0 != system(sprintf("%s %s", $ENV{'EDITOR'}, quotemeta($file)))) { return error('Not connected');
error("$ENV{'EDITOR'} exited abnormally");
} else { } else {
if (!-e $file || 0 == (stat($file))[7]) { my $file = tmpnam();
note("no data to send"); if (0 != system(sprintf("%s %s", $ENV{'EDITOR'}, quotemeta($file)))) {
error("$ENV{'EDITOR'} exited abnormally");
} else { } else {
local $/ = undef; if (!-e $file || 0 == (stat($file))[7]) {
open(FILE, $file); note("no data to send");
my $frame = <FILE>;
close(FILE); } else {
unlink($file); local $/ = undef;
$epp->request($frame); open(FILE, $file);
my $frame = <FILE>;
close(FILE);
unlink($file);
$epp->request($frame);
}
} }
} }
} }
sub handle_key { sub handle_key {
my ($key, $pass) = @_; if ($epp->connected) {
$epp->{'key'} = $key; return error('Already connected');
$epp->{'passphrase'} = $pass;
note("Using '$key' as private key"); } else {
my ($key, $pass) = @_;
$epp->{'key'} = $key;
$epp->{'passphrase'} = $pass;
note("Using '$key' as private key");
}
} }
sub handle_cert { sub handle_cert {
my $cert = shift; if ($epp->connected) {
$epp->{'cert'} = $cert; return error('Already connected');
note("Using '$cert' as certificate");
} else {
my $cert = shift;
$epp->{'cert'} = $cert;
note("Using '$cert' as certificate");
}
} }