-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbird
executable file
·180 lines (161 loc) · 4.26 KB
/
bird
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env perl
# bird -- client for birdhouse
use 5.10.0;
use Mojo::UserAgent;
use Mojo::JSON;
use Mojo::Log;
use File::Basename qw/dirname/;
use Term::Size;
use Term::ANSIScreen qw/:color :cursor :screen/;
use List::Util qw/max/;
use Cwd qw/abs_path/;
use warnings;
use strict;
$|=1;
my $ua = Mojo::UserAgent->new();
my $id = $ENV{USER} || [ getpwuid($<) ]->[0];
my $birds = [];
my $ws;
my $server; # host:port
my $ws_url; # ws://host:port/path
my ( $cols, $rows ) = Term::Size::chars * STDOUT { IO };
my $log = Mojo::Log->new(level => 'debug');
&main;
exit;
sub debug { $log->debug(@_); }
sub init_birdhouse {
chdir dirname $0;
-d 'log' or mkdir 'log' or die $!;
$server = $ARGV[0] or die "Usage $0 <host:port>\n";
$server = "http://$server" unless $server=~/:\/\//;
$ws_url = Mojo::URL->new($server);
$ws_url->scheme("ws");
$ws_url->path("door");
$log->path('./log/birdhouse.log');
$ua->inactivity_timeout(60 * 60);
}
sub init_screen {
$Term::ANSIScreen::AUTORESET = 1;
print color 'white on black';
cls();
locate 1, 1;
setscroll 1, $rows - 3;
}
sub write_status {
my %a = @_;
savepos;
locate $rows- 2, 1;
my $str = "birdhouse at $server @$birds";
print( ( color 'white on blue' ), $str);
print ' ' for length($str)..($cols-1);
loadpos;
}
sub prompt {
locate $rows - 1, 1;
clline;
print ( ( color 'yellow on black'), "$id> " );
locate $rows - 1, length("$id> ") + 1;
print color 'white on black';
}
sub timestamp {
print color 'green on black';
print "[",scalar(localtime)=~/(\S+) \d+$/,"] ";
}
sub write_server_msg {
my $msg = shift;
savepos;
locate $rows-3, 1;
timestamp();
print color 'cyan on black';
say $msg;
loadpos;
}
sub write_chat_msg {
my ($who,$msg) = @_;
savepos;
locate $rows-3, 1;
timestamp();
print color 'yellow on black';
my $max = max map length, @$birds;
printf("%${max}s: ",$who);
print color 'white on black';
say $msg;
loadpos;
}
sub init_connection {
debug "initializing";
my $connect;
$connect = sub {
debug "connecting";
my ($ua, $tx) = @_;
write_server_msg("connecting to $ws_url");
if ( my $e = $tx->error ) {
debug "error (retry in 5 seconds): $e";
write_server_msg($e." (retry in 5 seconds)");
Mojo::IOLoop->singleton->timer( 5 => sub { $ua->websocket( $ws_url => $connect ); }
);
return;
}
$ws = $tx;
$tx->send("hello, i am $id");
$tx->unsubscribe('message');
$tx->on(json =>
sub {
my ( $tx, $msg ) = @_;
if ( $msg->{id} eq 'server' ) {
if ($msg->{birds}) {
$birds = [ @{ $msg->{birds} } ];
write_status();
}
write_server_msg( $msg->{msg}) if $msg->{msg};
}
else {
write_chat_msg($msg->{id} ,$msg->{msg});
}
}
);
$tx->on( finish =>
sub {
write_server_msg "lost connection to $ws_url";
Mojo::IOLoop->singleton->timer(
2 => sub { $ua->websocket( $ws_url => $connect ); } );
}
);
$SIG{INT} = $SIG{QUIT} = sub {
$ws->finish if $ws && $ws->is_websocket;
setscroll 1, $rows;
locate $rows-1,1;
print "\nbye!\n";
exit;
};
write_server_msg("connected.");
prompt();
};
$ua->websocket( $ws_url => $connect );
}
sub init_stdin {
$ua->ioloop(Mojo::IOLoop->singleton);
my $w = $ua->ioloop->reactor;
$w->io(
\*STDIN,
sub {
chomp( my $input = <STDIN> );
unless ( $ws && $ws->is_websocket ) {
write_server_msg("write failed : no connection to $server");
return;
}
$ws->send($input);
prompt();
}
);
$w->watch(\*STDIN, 1, 0); # watch for read events
}
sub main {
init_birdhouse();
debug "starting";
init_screen();
write_status();
init_stdin();
init_connection();
Mojo::IOLoop->start;
}