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