1 | #!/usr/bin/perl |
1 | #!/usr/bin/perl |
2 | |
2 | |
3 | <<EOF; |
3 | # this crap is an asynchroneous finger client. it's rather idiotic ;) |
4 | finger |
|
|
5 | localhost |
|
|
6 | root |
|
|
7 | finger |
|
|
8 | localhost |
|
|
9 | root |
|
|
10 | EOF |
|
|
11 | |
4 | |
12 | use Coro; |
5 | use Coro; |
13 | use Coro::Event; |
6 | use Coro::Event; |
|
|
7 | use Coro::Socket; |
14 | |
8 | |
15 | #$Event::DebugLevel = 4; |
9 | # this gets started everytime a user enters a finger command |
|
|
10 | sub finger { |
|
|
11 | my $user = shift; |
|
|
12 | my $host = shift; |
16 | |
13 | |
17 | my $stdin = Coro::Event->io(fd => \*STDIN, poll => 'r'); |
14 | my $fh = new Coro::Socket PeerHost => $host, PeerPort => "finger" |
|
|
15 | or die; |
18 | |
16 | |
19 | sub finger { |
|
|
20 | my $host = shift; |
|
|
21 | my $user = shift; |
|
|
22 | use IO::Socket::INET; |
|
|
23 | # is there ANY way to do non-blocking connects with IO::Socket::INET? |
|
|
24 | # I don't see how... |
|
|
25 | my $io = new IO::Socket::INET PeerAddr => "$host:finger"; |
|
|
26 | $io or die; |
|
|
27 | syswrite $io, "$user\n"; |
17 | print $fh "$user\n"; |
28 | my $r = Coro::Event->io(fd => $io, poll => 'r'); |
18 | |
29 | my $buf; |
19 | print "$user\@$host: $_" while <$fh>; |
30 | $r->next while 0 != sysread $io, $buf, 8192, length $buf; |
20 | print "$user\@$host: done\n"; |
31 | #do_timer(after => 5); |
|
|
32 | print $buf; |
|
|
33 | } |
21 | } |
34 | |
22 | |
35 | sub kbdline { |
23 | my $stdin = new_from_fh Coro::Handle \*STDIN; |
36 | $stdin->next; |
|
|
37 | my $x = <STDIN>; chomp $x; $x; |
|
|
38 | } |
|
|
39 | |
24 | |
|
|
25 | # this is the main task |
40 | sub keyboard : Coro { |
26 | sub keyboard : Coro { |
41 | $|=1; |
27 | $|=1; |
42 | while() { |
28 | while() { |
43 | print "cmd> "; my $cmd = kbdline; |
29 | print "cmd> "; my $cmd = <$stdin>; chomp $cmd; |
44 | if ($cmd eq "finger") { |
30 | if ($cmd eq "finger") { |
45 | print "user> "; my $user = kbdline; |
31 | print "user> "; my $user = <$stdin>; chomp $user; |
46 | print "host> "; my $host = kbdline; |
32 | print "host> "; my $host = <$stdin>; chomp $host; |
47 | async { finger(@_) } $user, $host; |
33 | async { finger($user, $host) }; |
48 | } elsif ($cmd eq "quit") { |
34 | } elsif ($cmd eq "quit") { |
49 | unloop(777); |
35 | unloop(777); |
50 | terminate; |
36 | terminate; |
51 | } else { |
37 | } else { |
52 | print "enter command, either 'finger' or 'quit'\n"; |
38 | print "unknown command '$cmd', either 'finger' or 'quit'\n"; |
53 | } |
39 | } |
54 | } |
40 | } |
55 | } |
41 | } |
56 | |
42 | |
|
|
43 | # display the time or garble the display, YMMV. |
57 | sub timer : Coro { |
44 | sub timer : Coro { |
58 | my $w = Coro::Event->timer(interval => 0.001, hard => 1); |
45 | my $w = Coro::Event->timer(interval => 0.001, hard => 1); |
59 | use Time::HiRes qw(time); |
46 | use Time::HiRes qw(time); |
60 | while () { |
47 | while () { |
61 | $w->next; |
48 | $w->next; |
62 | print "\e7\e[A\10\10\10 <time ",time,"> \e8"; |
49 | print "\e7\e[C\e[C\e[C\e[C\e[C\e[C\e[C\e[C <time ",time,"> \e8"; |
63 | }; |
50 | }; |
64 | } |
51 | } |
65 | |
52 | |
66 | print "unlooped with value: ",loop(),"\n"; |
53 | print "unlooped with value: ",loop(),"\n"; |
67 | |
54 | |