1 |
pcg |
1.1 |
package app; |
2 |
|
|
|
3 |
|
|
use KGS::Protocol::Client; |
4 |
|
|
|
5 |
|
|
use Scalar::Util; |
6 |
|
|
|
7 |
|
|
use base KGS::Listener; |
8 |
pcg |
1.9 |
|
9 |
|
|
use Glib::Object::Subclass |
10 |
|
|
Gtk2::Window; |
11 |
pcg |
1.1 |
|
12 |
|
|
my %context_id; |
13 |
|
|
|
14 |
pcg |
1.9 |
our $self; |
15 |
|
|
|
16 |
pcg |
1.1 |
sub status { |
17 |
|
|
my ($type, $text) = @_; |
18 |
|
|
|
19 |
pcg |
1.9 |
$::self->{status}->pop ($context_id{$type}) if $context_id{$type}; |
20 |
|
|
$::self->{status}->push ($context_id{$type} ||= $::self->{status}->get_context_id ($type), $text) if $text; |
21 |
pcg |
1.1 |
} |
22 |
|
|
|
23 |
|
|
sub new { |
24 |
pcg |
1.10 |
my ($self, %arg) = @_; |
25 |
pcg |
1.9 |
$self = $self->Glib::Object::new; |
26 |
pcg |
1.10 |
$self->{$_} = delete $arg{$_} for keys %arg; |
27 |
pcg |
1.1 |
|
28 |
pcg |
1.9 |
Scalar::Util::weaken ($::self = $self); # singleton... |
29 |
pcg |
1.1 |
|
30 |
root |
1.20 |
$self->{protocol} = new KGS::Protocol::Client; |
31 |
pcg |
1.1 |
|
32 |
root |
1.20 |
$self->{roomlist} = new roomlist app => $self; |
33 |
pcg |
1.1 |
|
34 |
pcg |
1.9 |
$self->set_title ('kgsueme'); |
35 |
root |
1.19 |
gtk::state $self, "main::window", undef, window_size => [400, 580]; |
36 |
pcg |
1.9 |
$self->signal_connect (destroy => sub { %{$_[0]} = () }); |
37 |
|
|
$self->signal_connect (delete_event => sub { main_quit Gtk2; 1 }); |
38 |
pcg |
1.1 |
|
39 |
pcg |
1.9 |
$self->add (my $vbox = new Gtk2::VBox); |
40 |
pcg |
1.1 |
|
41 |
pcg |
1.9 |
$vbox->pack_start (($buttonbox = new Gtk2::HButtonBox), 0, 1, 0); |
42 |
|
|
$buttonbox->set_spacing (0); |
43 |
pcg |
1.1 |
|
44 |
|
|
my $button = sub { |
45 |
pcg |
1.9 |
$buttonbox->add (my $button = new Gtk2::Button $_[0]); |
46 |
pcg |
1.1 |
signal_connect $button clicked => $_[1]; |
47 |
|
|
}; |
48 |
|
|
|
49 |
|
|
$button->("Login", sub { $self->login; }); |
50 |
|
|
$button->("Roomlist", sub { $self->{roomlist}->show; }); |
51 |
|
|
$button->("Save Config & Layout", \&util::save_config); |
52 |
root |
1.21 |
$button->("Logout", sub { $self->{conn}->send ("quit") if $self->{conn} }); |
53 |
pcg |
1.1 |
$button->("Quit", sub { main_quit Gtk2 }); |
54 |
|
|
|
55 |
pcg |
1.9 |
$vbox->pack_start ((my $hbox = new Gtk2::HBox), 0, 1, 0); |
56 |
pcg |
1.1 |
|
57 |
pcg |
1.9 |
$hbox->add (new Gtk2::Label "Login"); |
58 |
pcg |
1.1 |
|
59 |
pcg |
1.9 |
$hbox->add ($self->{login} = new_with_max_length Gtk2::Entry 12); |
60 |
|
|
$self->{login}->set_text ($::config->{login}); |
61 |
pcg |
1.1 |
|
62 |
pcg |
1.9 |
$hbox->add (new Gtk2::Label "Password"); |
63 |
|
|
$hbox->add ($self->{password} = new Gtk2::Entry); |
64 |
root |
1.16 |
$self->{password}->set (visibility => 0, is_focus => 1); |
65 |
|
|
$self->{password}->signal_connect (activate => sub { $self->login }); |
66 |
pcg |
1.1 |
|
67 |
pcg |
1.13 |
$vbox->pack_start ((my $vpane = new Gtk2::VPaned), 1, 1, 0); |
68 |
|
|
$vpane->set (position_set => 1); |
69 |
root |
1.19 |
gtk::state $vpane, "main::vpane", undef, position => 400; |
70 |
pcg |
1.13 |
|
71 |
|
|
$vbox->pack_start(($self->{status} = new Gtk2::Statusbar), 0, 1, 0); |
72 |
|
|
|
73 |
root |
1.20 |
$self->{gamelist} = new gamelist app => $self; |
74 |
pcg |
1.13 |
$vpane->pack1 ($self->{gamelist}, 1, 1); |
75 |
|
|
|
76 |
|
|
$self->{rooms} = new Gtk2::Notebook; |
77 |
root |
1.18 |
$self->{rooms}->set (enable_popup => 1, scrollable => 1); |
78 |
pcg |
1.1 |
|
79 |
pcg |
1.13 |
$vpane->pack2 ($self->{rooms}, 1, 1); |
80 |
pcg |
1.1 |
|
81 |
pcg |
1.9 |
$self->show_all; |
82 |
pcg |
1.1 |
|
83 |
|
|
$self; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
sub login { |
87 |
|
|
my ($self) = @_; |
88 |
|
|
|
89 |
root |
1.20 |
$self->{protocol}->disconnect; |
90 |
pcg |
1.1 |
|
91 |
|
|
# initialize new socket and connection |
92 |
pcg |
1.12 |
my $sock = new IO::Socket::INET PeerHost => KGS::Protocol::KGSHOST, PeerPort => KGS::Protocol::KGSPORT |
93 |
root |
1.20 |
or do { |
94 |
|
|
app::status ("connect", "connect: $!"); |
95 |
|
|
$self->event_quit; |
96 |
|
|
return; |
97 |
|
|
}; |
98 |
|
|
|
99 |
|
|
$self->{login}->set (sensitive => 0); |
100 |
|
|
$self->{password}->set (sensitive => 0); |
101 |
pcg |
1.1 |
|
102 |
root |
1.15 |
$sock->blocking (1); |
103 |
root |
1.20 |
$self->{protocol}->handshake ($sock); |
104 |
root |
1.15 |
$sock->blocking (0); |
105 |
pcg |
1.1 |
|
106 |
root |
1.20 |
$self->listen ($self->{protocol}); |
107 |
|
|
$self->{roomlist}->listen ($self->{protocol}); |
108 |
|
|
#$self->{gamelist}->listen ($conn); |
109 |
|
|
|
110 |
|
|
# now login |
111 |
|
|
$ENV{KGSUEME_CLIENTVER} ||= "1.4.1_01:Swing app:Sun Microsystems Inc."; # he asked for it...#d# |
112 |
|
|
$self->{protocol}->login ($ENV{KGSUEME_CLIENTVER} || "kgsueme $VERSION $^O", # allow users to overwrite |
113 |
|
|
$self->{login}->get_text, |
114 |
|
|
$self->{password}->get_text); |
115 |
|
|
|
116 |
pcg |
1.1 |
my $input; $input = add_watch Glib::IO fileno $sock, [G_IO_IN, G_IO_ERR, G_IO_HUP], sub { |
117 |
|
|
# this is dorked |
118 |
|
|
my $buf; |
119 |
|
|
my $len = sysread $sock, $buf, 16384; |
120 |
|
|
if ($len) { |
121 |
root |
1.20 |
$self->{protocol}->feed_data ($buf); |
122 |
pcg |
1.1 |
} elsif (defined $len || (!$!{EINTR} and !$!{EAGAIN})) { |
123 |
|
|
remove Glib::Source $input; |
124 |
root |
1.20 |
$self->{protocol}->disconnect; |
125 |
|
|
warn "disconnect: $!";#TODO# |
126 |
|
|
app::status ("disconnect", "$!");#TODO# not visible |
127 |
pcg |
1.1 |
} |
128 |
|
|
1; |
129 |
|
|
}, G_PRIORITY_HIGH; |
130 |
|
|
} |
131 |
|
|
|
132 |
pcg |
1.5 |
sub inject_login { |
133 |
pcg |
1.1 |
my ($self, $msg) = @_; |
134 |
|
|
|
135 |
pcg |
1.5 |
$self->{name} = $self->{conn}{name}; |
136 |
pcg |
1.1 |
|
137 |
pcg |
1.5 |
$::config->{login} = $self->{name}; |
138 |
pcg |
1.1 |
|
139 |
pcg |
1.6 |
app::status("login", "logged in as '$self->{name}' with status '$msg->{message}' ('$msg->{reason}')"); |
140 |
pcg |
1.1 |
|
141 |
|
|
if ($msg->{success}) { |
142 |
|
|
# auto-join |
143 |
|
|
$self->open_room (%$_) for values %{$::config->{rooms}}; |
144 |
|
|
|
145 |
|
|
sound::play 3, "connect"; |
146 |
|
|
} elsif ($msg->{result} eq "user unknown") { |
147 |
|
|
sound::play 2, "user_unknown"; |
148 |
|
|
} else { |
149 |
|
|
sound::play 2, "warning"; |
150 |
|
|
} |
151 |
|
|
} |
152 |
|
|
|
153 |
root |
1.20 |
sub listen { |
154 |
|
|
my $self = shift; |
155 |
|
|
|
156 |
|
|
$self->SUPER::listen (@_, qw(login userpic idle_warn msg_chat chal_defaults)); |
157 |
|
|
} |
158 |
|
|
|
159 |
pcg |
1.5 |
sub inject_idle_warn { |
160 |
|
|
my ($self, $msg) = @_; |
161 |
|
|
|
162 |
|
|
$self->send ("idle_reset"); |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
sub inject_msg_chat { |
166 |
|
|
my ($self, $msg) = @_; |
167 |
|
|
|
168 |
|
|
if ((lc $msg->{name2}) eq (lc $self->{name})) { |
169 |
|
|
unless ($self->{user}{lc $msg->{name}}) { |
170 |
|
|
$self->open_user (name => $msg->{name})->inject ($msg); |
171 |
root |
1.17 |
sound::play 2, "ring"; |
172 |
pcg |
1.5 |
} |
173 |
|
|
} |
174 |
|
|
} |
175 |
|
|
|
176 |
pcg |
1.14 |
sub inject_chal_defaults { |
177 |
|
|
my ($self, $msg) = @_; |
178 |
|
|
|
179 |
|
|
$self->{defaults} = $msg->{defaults}; |
180 |
|
|
} |
181 |
|
|
|
182 |
pcg |
1.1 |
my %userpic; |
183 |
|
|
my %userpic_cb; |
184 |
|
|
|
185 |
|
|
# static method to request the userimage and call the cb when it's available. |
186 |
|
|
sub userpic { |
187 |
|
|
my ($self, $name, $cb) = @_; |
188 |
|
|
$self->get_userpic ($name, $cb); |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
sub get_userpic { |
192 |
|
|
my ($self, $name, $cb) = @_; |
193 |
|
|
|
194 |
|
|
if (exists $userpic{$name}) { |
195 |
|
|
$cb->($userpic{$name}); |
196 |
|
|
} else { |
197 |
|
|
if (!exists $userpic_cb{$name}) { |
198 |
|
|
# after 10 seconds, flush callbacks |
199 |
|
|
$self->send (req_pic => name => $name); |
200 |
|
|
add Glib::Timeout 10000, sub { |
201 |
|
|
$_->() for @{delete $userpic_cb{$name} || []}; |
202 |
|
|
0; |
203 |
|
|
}; |
204 |
|
|
} |
205 |
|
|
push @{$userpic_cb{$name}}, $cb; |
206 |
|
|
} |
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
sub inject_userpic { |
210 |
|
|
my ($self, $msg) = @_; |
211 |
|
|
|
212 |
|
|
$userpic{$msg->{name}} = $msg->{data}; |
213 |
|
|
$_->($userpic{$msg->{name}}) for @{delete $userpic_cb{$msg->{name}} || []}; |
214 |
|
|
} |
215 |
|
|
|
216 |
root |
1.20 |
sub event_quit { |
217 |
|
|
my ($self) = @_; |
218 |
|
|
|
219 |
|
|
$self->SUPER::event_quit; |
220 |
|
|
|
221 |
|
|
$self->{login}->set (sensitive => 1); |
222 |
|
|
$self->{password}->set (sensitive => 1); |
223 |
|
|
|
224 |
|
|
sound::play 2, "warning"; |
225 |
root |
1.15 |
} |
226 |
pcg |
1.1 |
|
227 |
|
|
sub open_game { |
228 |
|
|
my ($self, %arg) = @_; |
229 |
|
|
|
230 |
|
|
($self->{game}{$arg{channel}} ||= new game %arg, conn => $self->{conn}, app => $self) |
231 |
|
|
->join; |
232 |
pcg |
1.4 |
Scalar::Util::weaken $self->{game}{$arg{channel}}; |
233 |
pcg |
1.2 |
$self->{game}{$arg{channel}}; |
234 |
pcg |
1.1 |
} |
235 |
|
|
|
236 |
|
|
sub open_room { |
237 |
|
|
my ($self, %arg) = @_; |
238 |
|
|
|
239 |
pcg |
1.13 |
my $room = $self->{room}{$arg{channel}} ||= do { |
240 |
|
|
my $room = new room %arg, conn => $self->{conn}, app => $self; |
241 |
|
|
$room->show_all; |
242 |
|
|
$self->{rooms}->append_page ($room, new Gtk2::Label $room->{name}); |
243 |
|
|
$room; |
244 |
|
|
}; |
245 |
pcg |
1.8 |
Scalar::Util::weaken $self->{room}{$arg{channel}}; |
246 |
pcg |
1.13 |
|
247 |
|
|
$room->join; |
248 |
pcg |
1.2 |
$self->{room}{$arg{channel}}; |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
sub open_user { |
252 |
|
|
my ($self, %arg) = @_; |
253 |
|
|
|
254 |
pcg |
1.3 |
($self->{user}{lc $arg{name}} ||= new user %arg, conn => $self->{conn}, app => $self) |
255 |
pcg |
1.2 |
->join; |
256 |
pcg |
1.4 |
Scalar::Util::weaken $self->{user}{lc $arg{name}}; |
257 |
pcg |
1.3 |
$self->{user}{lc $arg{name}}; |
258 |
pcg |
1.2 |
} |
259 |
|
|
|
260 |
|
|
sub do_command { |
261 |
|
|
my ($self, $chat, $cmd, $arg, %arg) = @_; |
262 |
|
|
|
263 |
pcg |
1.5 |
if ($cmd eq "say") { |
264 |
|
|
if (my $context = $arg{game} || $arg{room} || $arg{user}) { |
265 |
pcg |
1.2 |
$context->say ($arg); |
266 |
|
|
} else { |
267 |
|
|
$chat->append_text ("\n<error>no context for message</error>"); |
268 |
|
|
} |
269 |
|
|
} elsif ($cmd eq "whois" or $cmd eq "w") { |
270 |
|
|
$self->open_user (name => $arg); |
271 |
|
|
} else { |
272 |
|
|
$chat->append_text ("\n<error>unknown command</error>"); |
273 |
|
|
} |
274 |
pcg |
1.1 |
} |
275 |
|
|
|
276 |
|
|
1; |
277 |
|
|
|