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