package CmdRun::Client; # # (c) 2003 by Kirill Miazine # # This software is distributed under an ISC-style license, please see # for details. # use warnings; use strict; use IO::Socket::INET; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; bless $self, $class; my %args = @_; $self->{'_host'} = delete $args{'host'} || 'localhost'; $self->{'_port'} = delete $args{'port'} || 8023; $self->{'_sock'} = IO::Socket::INET->new( PeerAddr => $self->{'_host'}, PeerPort => $self->{'_port'}, Proto => 'tcp', ) or die "Can't create socket: $!"; $self->{'_sock'}->getline(); return $self; } sub auth { my $self = shift; my ($username, $password) = @_; my $sock = $self->{'_sock'}; $sock->print("auth $username $password\n"); my $res = $sock->getline(); return $res =~ /^\+/ ? 1 : 0; } sub cmd { my $self = shift; my ($cmd, @args) = @_; my $sock = $self->{'_sock'}; $sock->print($cmd, ' ', join(' ', map { unpack 'H*', $_ } @args), "\n"); my @res; my $res = $sock->getline(); if ($res =~ /^\+\+/) { while (defined(my $line = $sock->getline())) { last if $line eq ".\n"; $line =~ s/^\.\././; push @res, $line; } } else { $res =~ s/^- //; push @res, "1\n", $res; } return @res; } sub quit { my $self = shift; my $sock = $self->{'_sock'}; $sock->print("quit\n"); my $res = $sock->getline(); $sock->close(); return $res =~ /^\+/ ? 1 : 0; } 1;