#!/usr/bin/perl

# useless stuff

use Coro;
use Coro::Event;

my $stdin = Coro::Event->io(fd => \*STDIN, poll => 'r');

# this gets started everytime a user enters a finger command
sub finger {
   my $user = shift;
   my $host = shift;
   use IO::Socket::INET;
   # is there ANY way to do non-blocking connects with IO::Socket::INET?
   # I don't see how...
   my $io = new IO::Socket::INET PeerAddr => "$host:finger";
   $io or die;
   syswrite $io, "$user\n";
   my $r = Coro::Event->io(fd => $io, poll => 'r');
   my $buf;
   $r->next while 0 != sysread $io, $buf, 8192, length $buf;
   #do_timer(after => 5);
   print $buf;
}

# this reads one line from the keyboard
sub kbdline {
   $stdin->next;
   my $x = <STDIN>; chomp $x; $x;
}

# this is the main task
sub keyboard : Coro {
   $|=1;
   while() {
      print "cmd> ";  my $cmd  = kbdline;
      if ($cmd eq "finger") {
         print "user> "; my $user = kbdline;
         print "host> "; my $host = kbdline;
         async { finger(@_) } $user, $host;
      } elsif ($cmd eq "quit") {
         unloop(777);
         terminate;
      } else {
         print "enter command, either 'finger' or 'quit'\n";
      }
   }
}

# display the time or garble the display, YMMV.
sub timer : Coro {
   my $w = Coro::Event->timer(interval => 0.001, hard => 1);
   use Time::HiRes qw(time);
   while () {
      $w->next;
      print "\e7\e[C\e[C\e[C\e[C\e[C\e[C\e[C\e[C   <time ",time,">   \e8";
   };
}

print "unlooped with value: ",loop(),"\n";

