ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/app.pl
Revision: 1.13
Committed: Sat May 29 02:01:43 2004 UTC (20 years ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.12: +19 -5 lines
Log Message:
*** empty log message ***

File Contents

# Content
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, 500];
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 $vbox->pack_start ((my $vpane = new Gtk2::VPaned), 1, 1, 0);
68 $vpane->set (position_set => 1);
69 gtk::state $vpane, "main::vpane", undef, position => 250;
70
71 $vbox->pack_start(($self->{status} = new Gtk2::Statusbar), 0, 1, 0);
72
73 $self->{gamelist} = new gamelist conn => $self->{conn}, app => $self;
74 $vpane->pack1 ($self->{gamelist}, 1, 1);
75
76 $self->{rooms} = new Gtk2::Notebook;
77
78 $vpane->pack2 ($self->{rooms}, 1, 1);
79
80 $self->show_all;
81
82 $self;
83 }
84
85 sub login {
86 my ($self) = @_;
87
88 $self->{conn}->disconnect;
89
90 # initialize new socket and connection
91 my $sock = new IO::Socket::INET PeerHost => KGS::Protocol::KGSHOST, PeerPort => KGS::Protocol::KGSPORT
92 or die "connect: $!";
93
94 $sock->blocking(1);
95 $self->{conn}->handshake($sock);
96 $sock->blocking(0);
97
98 my $input; $input = add_watch Glib::IO fileno $sock, [G_IO_IN, G_IO_ERR, G_IO_HUP], sub {
99 # this is dorked
100 my $buf;
101 my $len = sysread $sock, $buf, 16384;
102 if ($len) {
103 $self->{conn}->feed_data($buf);
104 } elsif (defined $len || (!$!{EINTR} and !$!{EAGAIN})) {
105 warn "disconnected";#d#
106 remove Glib::Source $input;
107 $self->event_disconnect;
108 }
109 1;
110 }, G_PRIORITY_HIGH;
111
112 # now login
113 $ENV{KGSUEME_CLIENTVER} = "1.4.1_01:Swing app:Sun Microsystems Inc."; # he asked for it...#d#
114 $self->{conn}->login($ENV{KGSUEME_CLIENTVER} || "kgsueme $VERSION $^O", # allow users to overwrite
115 $self->{login}->get_text,
116 $self->{password}->get_text);
117 }
118
119 sub inject_login {
120 my ($self, $msg) = @_;
121
122 $self->{name} = $self->{conn}{name};
123
124 $::config->{login} = $self->{name};
125
126 app::status("login", "logged in as '$self->{name}' with status '$msg->{message}' ('$msg->{reason}')");
127
128 if ($msg->{success}) {
129 # auto-join
130 $self->open_room (%$_) for values %{$::config->{rooms}};
131
132 sound::play 3, "connect";
133 } elsif ($msg->{result} eq "user unknown") {
134 sound::play 2, "user_unknown";
135 } else {
136 sound::play 2, "warning";
137 }
138 }
139
140 sub inject_idle_warn {
141 my ($self, $msg) = @_;
142
143 $self->send ("idle_reset");
144 }
145
146 sub inject_msg_chat {
147 my ($self, $msg) = @_;
148
149 if ((lc $msg->{name2}) eq (lc $self->{name})) {
150 unless ($self->{user}{lc $msg->{name}}) {
151 $self->open_user (name => $msg->{name})->inject ($msg);
152 }
153 }
154 }
155
156 my %userpic;
157 my %userpic_cb;
158
159 # static method to request the userimage and call the cb when it's available.
160 sub userpic {
161 my ($self, $name, $cb) = @_;
162 $self->get_userpic ($name, $cb);
163 }
164
165 sub get_userpic {
166 my ($self, $name, $cb) = @_;
167
168 if (exists $userpic{$name}) {
169 $cb->($userpic{$name});
170 } else {
171 if (!exists $userpic_cb{$name}) {
172 # after 10 seconds, flush callbacks
173 $self->send (req_pic => name => $name);
174 add Glib::Timeout 10000, sub {
175 $_->() for @{delete $userpic_cb{$name} || []};
176 0;
177 };
178 }
179 push @{$userpic_cb{$name}}, $cb;
180 }
181 }
182
183 sub inject_userpic {
184 my ($self, $msg) = @_;
185
186 $userpic{$msg->{name}} = $msg->{data};
187 $_->($userpic{$msg->{name}}) for @{delete $userpic_cb{$msg->{name}} || []};
188 }
189
190 sub event_disconnect { }
191
192 sub open_game {
193 my ($self, %arg) = @_;
194
195 ($self->{game}{$arg{channel}} ||= new game %arg, conn => $self->{conn}, app => $self)
196 ->join;
197 Scalar::Util::weaken $self->{game}{$arg{channel}};
198 $self->{game}{$arg{channel}};
199 }
200
201 sub open_room {
202 my ($self, %arg) = @_;
203
204 my $room = $self->{room}{$arg{channel}} ||= do {
205 my $room = new room %arg, conn => $self->{conn}, app => $self;
206 $room->show_all;
207 $self->{rooms}->append_page ($room, new Gtk2::Label $room->{name});
208 $room;
209 };
210 Scalar::Util::weaken $self->{room}{$arg{channel}};
211
212 $room->join;
213 $self->{room}{$arg{channel}};
214 }
215
216 sub open_user {
217 my ($self, %arg) = @_;
218
219 ($self->{user}{lc $arg{name}} ||= new user %arg, conn => $self->{conn}, app => $self)
220 ->join;
221 Scalar::Util::weaken $self->{user}{lc $arg{name}};
222 $self->{user}{lc $arg{name}};
223 }
224
225 sub do_command {
226 my ($self, $chat, $cmd, $arg, %arg) = @_;
227
228 if ($cmd eq "say") {
229 if (my $context = $arg{game} || $arg{room} || $arg{user}) {
230 $context->say ($arg);
231 } else {
232 $chat->append_text ("\n<error>no context for message</error>");
233 }
234 } elsif ($cmd eq "whois" or $cmd eq "w") {
235 $self->open_user (name => $arg);
236 } else {
237 $chat->append_text ("\n<error>unknown command</error>");
238 }
239 }
240
241 1;
242