App-pepper

 view release on metacpan or  search on metacpan

lib/App/pepper.pm  view on Meta::CPAN

    'newpw=s',
    'exec=s',
    'port=i',
    'timeout=i',
    'help',
    'insecure',
    'lang=s',
    'debug',
    'cert=s',
    'key=s',
    'login-security',
    'nossl',
);

if (!$result || $opt->{'help'}) {
    pod2usage(
        '-verbose'    => 99,
        '-sections'    => 'USAGE',
        '-input'    => __FILE__,
    );
    exit;
}

my $handlers = {
    'timeout'        => \&handle_timeout,
    'ssl'            => \&handle_ssl,
    'host'            => \&handle_host,
    'port'            => \&handle_port,
    'credentials'    => \&handle_credentials,
    'id'            => \&handle_id,
    'pw'            => \&handle_pw,
    'newpw'            => \&handle_newpw,
    'connect'        => \&handle_connect,
    'login'            => \&handle_login,
    'logout'        => \&handle_logout,
    'hello'            => \&handle_hello,
    'check'            => \&handle_check,
    'info'            => \&handle_info,
    'poll'            => \&handle_poll,
    'help'            => \&handle_help,
    'send'            => \&handle_send,
    'BEGIN'            => \&handle_begin,
    'exit'            => \&handle_exit,
    'transfer'        => \&handle_transfer,
    'clone'            => \&handle_clone,
    'delete'        => \&handle_delete,
    'renew'            => \&handle_renew,
    'create'        => \&handle_create,
    'edit'            => \&handle_edit,
    'cert'            => \&handle_cert,
    'key'            => \&handle_key,
    'restore'        => \&handle_restore,
    'update'        => \&handle_update,
};

our $term;
$term = Term::ReadLine->new('pepper') if (-t STDIN && -t STDOUT);

my $outfh = ($term ? \*STDOUT : \*STDERR);

my $prompt = 'pepper> ';

my $histfile = $ENV{'HOME'}.'/.pepper_history';

my $xml = XML::LibXML->new;

if ($term) {
    $term->ReadHistory($histfile) if ('Term::ReadLine::Gnu' eq $term->ReadLine);

    note('Welcome to pepper!');

    note(color('yellow').'> For best results, install Term::ReadLine::Gnu <'.color('reset')) if ($term && 'Term::ReadLine::Gnu' ne $term->ReadLine);
}

my $epp = App::pepper::EPPClient->new(
    'host'                => '',
    'connect'            => undef,
    'debug'                => $opt->{'debug'},
    'login'                => undef,
    'reconnect'            => 0,
    'verify'            => ($opt->{'insecure'} ? undef : 1),
    'ca_file'            => Mozilla::CA::SSL_ca_file(),
    'lang'                => ($opt->{'lang'} ? $opt->{'lang'} : 'en'),
    'appname'           => sprintf('Pepper %s', $VERSION),
    'login_security'    => $opt->{'login-security'},
);

execute_command('ssl off')                                                if ($opt->{'nossl'});
execute_command(sprintf('timeout %d',    $opt->{'timeout'}))                if ($opt->{'timeout'});
execute_command(sprintf('port %d',    $   opt->{'port'}))                    if ($opt->{'port'});
execute_command(sprintf('host "%s"',    quotemeta($opt->{'host'})))        if ($opt->{'host'});
execute_command(sprintf('id "%s"',        quotemeta($opt->{'user'})))        if ($opt->{'user'});
execute_command(sprintf('pw "%s"',        quotemeta($opt->{'pass'})))        if ($opt->{'pass'});
execute_command(sprintf('newpw "%s"',    quotemeta($opt->{'newpw'})))    if ($opt->{'newpw'});
execute_command(sprintf('cert "%s"',    quotemeta($opt->{'cert'})))        if ($opt->{'cert'});
execute_command(sprintf('key "%s"',        quotemeta($opt->{'key'})))        if ($opt->{'key'});

if ($epp->{'user'} ne '' && $epp->{'pass'} ne '') {
    execute_command('login');

} elsif ($epp->{'host'} ne '') {
    execute_command('connect');

}

if ($opt->{'exec'}) {
    execute_command($opt->{'exec'});
    handle_exit();

} elsif ($term) {
    note("Entering interactive mode, exit with Ctrl-D, Ctrl-C or 'exit'");

} else {
    note("Entering batch mode");

}

#
# main loop
#
my $last;
while (1) {
    $prompt = sprintf('pepper (%s@%s)> ', $epp->{'user'}, $epp->{'host'}) if ($epp->authenticated);

    my $command;
    if ($term) {
        $command = $term->readline($prompt);

    } else {
        $command = <STDIN>;
        chomp($command);

        # remove any trailing comment
        $command =~ s/\#.*//g;
    }

    if (!defined($command)) {
        last;

    } elsif ($command ne '') {
        if ($term && $command eq '!!') {
            execute_command($last) if ($last ne '');

        } else {
            $last = $command;

            note("Executing command '%s'", $command) if (!$term);
            execute_command($command);
        }
    }
}

$term->WriteHistory($histfile) if ($term && 'Term::ReadLine::Gnu' eq $term->ReadLine);

handle_logout() if ($epp->connected && $epp->authenticated);
$epp->disconnect if ($epp->connected);

note('Bye!');

exit(0);

sub execute_command {
    my $line = shift;

    return if ($line eq '');

    my @args = shellwords($line);

    my $command = shift(@args);

    my $fail;
    if (!$term && $command =~/^!/) {
        $fail = 1;
        $command = substr($command, 1);
    }

    if (!defined($handlers->{$command})) {
        error("Unknown command '$command'");
        handle_exit() if (!$term);

    } else {
        &{$handlers->{$command}}(@args);
        if (!$term && $fail) {
            note("%s%04d%s %s", color($Net::EPP::Simple::Code < 2000 ? 'green' : 'red'), $Net::EPP::Simple::Code, color('reset'), $Net::EPP::Simple::Message);
            fatal($Net::EPP::Simple::Message) if ($Net::EPP::Simple::Code > 1999);
        }
    }

lib/App/pepper.pm  view on Meta::CPAN


=pod

=for comment
To generate the README.md, run pod2markdown pepper | pandoc -f markdown -t markdown -s --toc > README.md

=head1 NAME

Pepper - A command-line EPP client.

=head1 DESCRIPTION

Pepper is a command-line client for the EPP protocol. It's written in Perl and uses the L<Net::EPP> module.

=head1 USAGE

    pepper [OPTIONS]

Available command-line options:

=over

=item * C<--help> - show help and exit.

=item * C<--host=HOST> - sets the host to connect to.

=item * C<--port=PORT> - sets the port. Defaults to 700.

=item * C<--timeout=TIMEOUT> - sets the timeout. Defaults to 3.

=item * C<--user=USER> - sets the client ID.

=item * C<--pass=PASS> - sets the client password.

=item * C<--newpw=PASS> - specify a new password to replace the current password.

=item * C<--login-security> - force the use of the Login Security extension (RFC 8807).

=item * C<--cert=FILE> - specify the client certificate to use to connect.

=item * C<--key=FILE> - specify the private key for the client certificate.

=item * C<--exec=COMMAND> - specify a command to execute. If not provided, pepper goes into interactive mode.

=item * C<--nossl> - disable TLS and connect over plaintext.

=item * C<--insecure> - disable TLS certificate checks.

=item * C<--lang=LANG> - set the language when logging in.

=item * C<--debug> - debug mode, makes C<Net::EPP::Simple> verbose.

=back

=head1 USAGE MODES

Pepper supports two usage modes:

=over

=item 1. B<Interactive mode:> this is the default mode. Pepper will provide a command prompt (with history and line editing capabilities) allowing you to input commands manually.

=item 2. B<Script mode:> if Pepper's C<STDIN> is fed a stream of text (ie, it's not attached to a terminal) then commands will be read from C<STDIN> and executed sequentially. Pepper will exit once EOF is reached.

=back

=head1 SYNTAX

Once running in interactive mode, Pepper provides a simple command-line interface. The available commands are listed below.

=head2 Getting Help

Use C<help COMMAND> at any time to get information about that command. Where a command supports different object types (ie domain, host, contact), use C<help command-type>, ie C<help create-domain>.

=head2 Connection Management

=over

=item * C<host HOST> - sets the host to connect to.

=item * C<port PORT> - sets the port. Defaults to 700.

=item * C<ssl on|off> - enable/disable SSL (default is on)

=item * C<key FILE> - sets the private key

=item * C<cert FILE> - sets the client certificate.

=item * C<timeout TIMEOUT> - sets the timeout

=item * C<connect> - connect to the server.

=item * C<hello> - gets the greeting from server.

=item * C<exit> - quit the program (logging out if necessary)

=back

=head2 Session Management

=over

=item * C<id USER> - sets the client ID.

=item * C<pw PASS> - sets the client password.

=item * C<login> - log in.

=item * C<logout> - log out.

=item * C<poll req> - requests the most recent poll message.

=item * C<poll ack ID> - acknowledge the poll message with ID C<ID>.

=back

=head2 Query Commands

=head3 Availability Checks

    check TYPE OBJECT



( run in 4.009 seconds using v1.01-cache-2.11-cpan-6aa56a78535 )