ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/app.pl
Revision: 1.2
Committed: Fri Jul 25 03:50:33 2003 UTC (20 years, 10 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +26 -0 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 use base gtk::widget;
9
10 my %context_id;
11
12 sub status {
13 my ($type, $text) = @_;
14
15 $self->{status}->pop($context_id{$type}) if $context_id{$type};
16 $self->{status}->push($context_id{$type} ||= $self->{status}->get_context_id($type), $text) if $text;
17 }
18
19 sub new {
20 my $self = shift;
21 $self = $self->SUPER::new(@_);
22
23 $app::self = $self; # singleton
24 Scalar::Util::weaken $app::self;
25
26 $self->{conn} = new KGS::Protocol::Client;
27
28 KGS::Listener::Debug->new->listen($self->{conn}, "any"); #d# debug only :)
29
30 $self->listen($self->{conn}, qw(login userpic idle_warn));
31
32 $self->{roomlist} = new roomlist conn => $self->{conn}, app => $self;
33
34 $self->{window} = new Gtk2::Window 'toplevel';
35 $self->{window}->set_title('kgsueme');
36 gtk::state $self->{window}, "main::window", undef, window_size => [400, 400];
37 $self->{window}->signal_connect(delete_event => sub { main_quit Gtk2; 1 });
38
39 $self->{window}->add(my $vbox = new Gtk2::VBox);
40
41 $vbox->pack_start(($buttonbox = new Gtk2::HButtonBox), 0, 1, 0);
42 $buttonbox->set_spacing(0);
43
44 my $button = sub {
45 $buttonbox->add(my $button = new Gtk2::Button $_[0]);
46 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 $button->("Quit", sub { main_quit Gtk2 });
53
54 $vbox->pack_start((my $hbox = new Gtk2::HBox), 0, 1, 0);
55
56 $hbox->add(new Gtk2::Label "Login");
57
58 $hbox->add($self->{login} = new_with_max_length Gtk2::Entry 12);
59 $self->{login}->set_text($::config->{login});
60
61 if ($::HACK) {
62 $self->{login}->signal_connect(activate => sub {
63 $self->{conn}{name} = $self->{login}->get_text;
64 });
65 }
66
67 $hbox->add(new Gtk2::Label "Password");
68 $hbox->add($self->{password} = new Gtk2::Entry);
69 $self->{password}->set_visibility(0);
70
71 $self->{gamelist} = new gamelist conn => $self->{conn}, app => $self;
72 $vbox->pack_start ($self->{gamelist}->widget, 1, 1, 0);
73
74 $vbox->pack_start(($self->{status} = new Gtk2::Statusbar), 0, 1, 0);
75
76 $self->{window}->show_all;
77
78 $self;
79 }
80
81 sub login {
82 my ($self) = @_;
83
84 $self->{conn}->disconnect;
85
86 # initialize new socket and connection
87 #my $sock = new IO::Socket::INET PeerHost => "kgs.kiseido.com", PeerPort => "2379"
88 my $sock = new IO::Socket::INET PeerHost => $ENV{KGSHOST} || "kgs.kiseido.com", PeerPort => "2379"
89 or die;
90
91 $sock->blocking(1);
92 $self->{conn}->handshake($sock);
93 $sock->blocking(0);
94
95 my $input; $input = add_watch Glib::IO fileno $sock, [G_IO_IN, G_IO_ERR, G_IO_HUP], sub {
96 # this is dorked
97 my $buf;
98 my $len = sysread $sock, $buf, 16384;
99 if ($len) {
100 $self->{conn}->feed_data($buf);
101 } elsif (defined $len || (!$!{EINTR} and !$!{EAGAIN})) {
102 warn "disconnected";#d#
103 remove Glib::Source $input;
104 $self->event_disconnect;
105 }
106 1;
107 }, G_PRIORITY_HIGH;
108
109 # now login
110 $ENV{KGSUEME_CLIENTVER} = "1.4.1_01:Swing app:Sun Microsystems Inc."; # he asked for it...#d#
111 $self->{conn}->login($ENV{KGSUEME_CLIENTVER} || "kgsueme $VERSION $^O", # allow users to overwrite
112 $self->{login}->get_text,
113 $self->{password}->get_text);
114 }
115
116 sub inject_idle_warn {
117 my ($self, $msg) = @_;
118
119 $self->send ("idle_reset");
120 }
121
122 sub inject_login {
123 my ($self, $msg) = @_;
124
125 app::status("login", "logged in as '$self->{conn}{name}' with status '$msg->{result}' ('$msg->{reason}')");
126 $::config->{login} = $self->{conn}{name};
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 my %userpic;
141 my %userpic_cb;
142
143 # static method to request the userimage and call the cb when it's available.
144 sub userpic {
145 my ($self, $name, $cb) = @_;
146 $self->get_userpic ($name, $cb);
147 }
148
149 sub get_userpic {
150 my ($self, $name, $cb) = @_;
151
152 if (exists $userpic{$name}) {
153 $cb->($userpic{$name});
154 } else {
155 if (!exists $userpic_cb{$name}) {
156 # after 10 seconds, flush callbacks
157 $self->send (req_pic => name => $name);
158 add Glib::Timeout 10000, sub {
159 $_->() for @{delete $userpic_cb{$name} || []};
160 0;
161 };
162 }
163 push @{$userpic_cb{$name}}, $cb;
164 }
165 }
166
167 sub inject_userpic {
168 my ($self, $msg) = @_;
169
170 $userpic{$msg->{name}} = $msg->{data};
171 $_->($userpic{$msg->{name}}) for @{delete $userpic_cb{$msg->{name}} || []};
172 }
173
174 sub event_disconnect { }
175
176 sub open_game {
177 my ($self, %arg) = @_;
178
179 ($self->{game}{$arg{channel}} ||= new game %arg, conn => $self->{conn}, app => $self)
180 ->join;
181 $self->{game}{$arg{channel}};
182 }
183
184 sub open_room {
185 my ($self, %arg) = @_;
186
187 ($self->{room}{$arg{channel}} ||= new room %arg, conn => $self->{conn}, app => $self)
188 ->join;
189 $self->{room}{$arg{channel}};
190 }
191
192 sub open_user {
193 my ($self, %arg) = @_;
194
195 ($self->{user}{$arg{name}} ||= new user %arg, conn => $self->{conn}, app => $self)
196 ->join;
197 $self->{user}{$arg{name}};
198 }
199
200 sub do_command {
201 my ($self, $chat, $cmd, $arg, %arg) = @_;
202
203 if ($cmd eq "msg") {
204 if (my $context = $arg{game} || $arg{room}) {
205 $context->say ($arg);
206 } else {
207 $chat->append_text ("\n<error>no context for message</error>");
208 }
209 } elsif ($cmd eq "whois" or $cmd eq "w") {
210 $self->open_user (name => $arg);
211 } else {
212 $chat->append_text ("\n<error>unknown command</error>");
213 }
214 }
215
216 1;
217