All pastes #756865 Raw Edit

user admin client v0.0.2-rc4

public text v1 · immutable
#756865 ·published 2007-10-31 20:09 UTC
rendered paste body
use strict;
use IO::Socket;

##things that need to be first
my $CNAME = $ENV{COMPUTERNAME}; #our name
my $GET = sub {
  my $listen = new IO::Socket::INET (
    LocalHost => $CNAME,
    LocalPort => '7070',
    Proto     => 'tcp',
    Listen    => 1,
#    Timeout   => 5,
  );
  death('$GET: Unable to listen.') unless $listen;
  my $data;
  my $sock = $listen->accept();
  while (<$sock>) {
    $data .= $_;
  }  
  close($sock) or note('\$GET: Sock not open.') and return 0; #close sock
  note("\$GET: Data: @{[ $data ? $data : 'No data.']}");
  return split(/::/,$data); #and return
};
my $SND = sub {
  my $peer = shift or death('$SND: No one to talk to.'); #die if no peer is given
  my $say = new IO::Socket::INET (
    PeerAddr => $peer,
    PeerPort => '7070',
    Proto    => 'tcp',
    Timeout  => 10,
  ); #open sock (out)
  death('$SND: Unable to speak.') unless $say; #die if we failed
  print $say shift or death('$SND: Nothing to say.'); #die if there's nothing to send
  my @remainder = split(/\s+/,shift);
  foreach my $item (@remainder) {
    print $say '::', $item;
  } #if there's anything else, send it too
  close($say); #close sock
  return 1;
};

##global
my %global = (
  'server' => 'koizumi',
);

##command hash
my %cmnd = (
  'intro' => \&intro,
  'help'  => \&help,
  'get'   => \&cmd_get,
  'raw'   => \&cmd_raw,
  'set'   => \&cmd_set,
  'kill'  => sub {
    note('Thanks for playing.');
    exit 1;
  }
);

##progrum
my $input = 'intro';
while (1) {
  foreach my $regex (sort keys %cmnd) {
    if ($input =~ /^\s*($regex)(?:\s+(.*))?/i) {
      $input = $cmnd{$1}->($2);
    }
  }
  next if $input =~ /help/i;
  $input = join ' ', @{[ $GET->() ]};
}

##command subs
sub cmd_get {
  print "hey something worked";
  exit 1;
}

sub cmd_raw {
  return 1;
}

sub cmd_set {
  my ($item,$value) = (shift,shift);
  note('Server passed non-existant item.') and return 0 unless defined($global{$item});
  $global{$item} = $value;
  return 1;
}

##core
sub parse_net_user {
  my $data = `net user`;
  $data =~ s/\n/ /g;
  $data =~ s/\s+/ /g;
  $data =~ s/^.*-+\s(.*)/$1/;
  $data =~ s/(.*)\sThe command completed successfully\..*/$1/;
  $data =~ s/\s+/:/g;
  return $data;
}

##micro functions
sub death {
  print curti(), ' - ', shift, "\n";
  exit 0;
}

sub note {
  print curti(), ' - ', shift, "\n";
  return 1;
}

sub curti {
  my @time = @{[ gmtime ]}[2,1,0];
  $time[0] = $time[0]-6 < 0 ? $time[0]+6 : $time[0]-6;
  return sprintf('%02d:%02d:%02d', @time);
}

##information
sub intro {
  note('Dean\'s multibox user admin client starting up.');
  note('Awaiting instructions...');
  return 'help';
}

sub help {
  note('Help not yet written.');
  return 1;
}