… | |
… | |
12 | |
12 | |
13 | # See doc/protocol.xml and doc/doc2messages.xsl |
13 | # See doc/protocol.xml and doc/doc2messages.xsl |
14 | |
14 | |
15 | package KGS::Messages; |
15 | package KGS::Messages; |
16 | |
16 | |
|
|
17 | use KGS::Constants; # REPLACE by parsed file, too |
|
|
18 | |
17 | use strict; |
19 | use strict; |
18 | |
20 | |
19 | our %type; |
21 | our %type; |
20 | our %send; |
22 | our %send; |
21 | our %recv; |
23 | our %recv; |
… | |
… | |
99 | sub enc_U64 { |
101 | sub enc_U64 { |
100 | enc_U32 $_[0] & 0xffffffff; |
102 | enc_U32 $_[0] & 0xffffffff; |
101 | enc_U32 +($_[0] >> 32) & 0xffffffff; |
103 | enc_U32 +($_[0] >> 32) & 0xffffffff; |
102 | } |
104 | } |
103 | |
105 | |
|
|
106 | sub enc_I8 { |
|
|
107 | $data .= pack "c", $_[0]; |
|
|
108 | } |
|
|
109 | |
|
|
110 | sub enc_I16 { |
|
|
111 | enc_U16 unpack "S", pack "s", $_[0]; |
|
|
112 | } |
|
|
113 | |
104 | sub enc_I32 { |
114 | sub enc_I32 { |
105 | enc_U32 unpack "I", pack "i", $_[0]; |
115 | enc_U32 unpack "I", pack "i", $_[0]; |
106 | } |
116 | } |
107 | |
117 | |
108 | sub enc_DATA { |
118 | sub enc_DATA { |
… | |
… | |
112 | |
122 | |
113 | sub enc_STRING { |
123 | sub enc_STRING { |
114 | # should use encode for speed and clarity ;) |
124 | # should use encode for speed and clarity ;) |
115 | $data .= pack "v*", map ord, split //, $_[0]; |
125 | $data .= pack "v*", map ord, split //, $_[0]; |
116 | } |
126 | } |
|
|
127 | |
|
|
128 | sub enc_password { |
|
|
129 | require Math::BigInt; # I insist on 32-bit-perl.. should use C |
|
|
130 | # $hash must be 64 bit |
|
|
131 | my $hash = new Math::BigInt; |
|
|
132 | $hash = $hash * 1055 + ord for split //, $_[0]; |
|
|
133 | enc_U64 $hash; |
|
|
134 | } |
|
|
135 | |
117 | ]]> |
136 | ]]> |
118 | |
137 | |
119 | ############################################################################# |
138 | ############################################################################# |
120 | # types |
139 | # types |
121 | <xsl:apply-templates select="descendant::type"/> |
140 | <xsl:apply-templates select="descendant::type"/> |
… | |
… | |
143 | # this was the most horrible thing to decode. still not everything is decoded correctly(?) |
162 | # this was the most horrible thing to decode. still not everything is decoded correctly(?) |
144 | sub dec_TREE { |
163 | sub dec_TREE { |
145 | my @r; |
164 | my @r; |
146 | while (length $data) { |
165 | while (length $data) { |
147 | my $type = dec_U8; |
166 | my $type = dec_U8; |
|
|
167 | my $add = $type < 128; |
148 | |
168 | |
|
|
169 | $type &= 127; |
|
|
170 | |
149 | if ($type == 255) { |
171 | if ($type == 127) { |
|
|
172 | dec_U8; # unused?? *sigh* |
150 | push @r, [add_child => dec_I32]; |
173 | push @r, [add_node => dec_I32]; |
151 | |
174 | |
|
|
175 | } elsif ($type == 126) { |
|
|
176 | push @r, [set_node => dec_I32]; |
|
|
177 | |
|
|
178 | } elsif ($type == 125) { |
|
|
179 | push @r, [set_current => dec_I32]; |
|
|
180 | |
|
|
181 | } elsif ($type == 34) { |
|
|
182 | push @r, [score => dec_U8, dec_score1000]; |
|
|
183 | |
152 | } elsif ($type == 254) { |
184 | } elsif ($type == 25) { |
153 | push @r, ["done"]; |
185 | push @r, [result => dec_result]; |
154 | |
186 | |
155 | } elsif ($type == 253) { |
187 | } elsif ($type == 23) { |
156 | push @r, ["type253"]; |
188 | push @r, [mark => $add, MARK_GRAYED, dec_U8, dec_U8]; |
157 | # ???? |
|
|
158 | |
189 | |
159 | } elsif ($type == 252) { # even less clear |
190 | } elsif ($type == 22) { |
|
|
191 | push @r, [mark => $add, (&dec_U8 ? MARK_SMALL_W : MARK_SMALL_B), dec_U8, dec_U8]; |
|
|
192 | |
|
|
193 | } elsif ($type == 21) { |
|
|
194 | push @r, [mark => $add, MARK_CIRCLE, dec_U8, dec_U8]; |
|
|
195 | |
|
|
196 | } elsif ($type == 17) { |
|
|
197 | push @r, [set_timer => (dec_U8, dec_U32, dec_time)[0,2,1]]; |
|
|
198 | |
|
|
199 | } elsif ($type == 16) { |
|
|
200 | push @r, [set_stone => $add, dec_U8, dec_U8, dec_U8]; |
|
|
201 | |
|
|
202 | } elsif ($type == 14) { |
|
|
203 | push @r, [move => $add, dec_U8, dec_U8, dec_U8]; |
|
|
204 | |
|
|
205 | } elsif (($type >= 4 && $type <= 9) |
|
|
206 | || ($type >= 11 && $type <= 13) |
|
|
207 | || $type == 24) { |
|
|
208 | |
160 | push @r, ["more"]; |
209 | push @r, [({ |
|
|
210 | 4 => "date", |
|
|
211 | 5 => "unknown_comment5", |
|
|
212 | 6 => "unknown_comment6", |
|
|
213 | 7 => "unknown_comment7", |
|
|
214 | 8 => "unknown_comment8", |
|
|
215 | 9 => "copyright", #? |
|
|
216 | 11 => "unknown_comment11", |
|
|
217 | 12 => "unknown_comment12", |
|
|
218 | 13 => "unknown_comment13", |
|
|
219 | 24 => "comment", |
|
|
220 | })->{$type} => dec_STRING]; |
161 | |
221 | |
|
|
222 | } elsif ($type == 3) { |
|
|
223 | push @r, [rank => dec_U8, dec_U32]; |
|
|
224 | |
|
|
225 | } elsif ($type == 2) { |
|
|
226 | push @r, [player => dec_U8, dec_STRING]; |
|
|
227 | |
162 | } elsif ($type == 10) { |
228 | } elsif ($type == 0) { |
163 | # as usual, wms finds yet another way to duplicate code... oh well, what a mess. |
229 | # as usual, wms finds yet another way to duplicate code... oh well, what a mess. |
164 | # (no wonder he is so keen on keeping it secret...) |
230 | # (no wonder he is so keen on keeping it a secret...) |
165 | |
231 | |
166 | push @r, [rules => dec_rules]; |
232 | push @r, [rules => dec_rules]; |
167 | |
233 | |
168 | } elsif ($type == 9 || $type == 22 || $type == 25 || $type == 31 || $type == 32) { |
234 | # OLD |
169 | push @r, [({ |
|
|
170 | 9 => "comment", |
|
|
171 | 22 => "unknown_comment22", |
|
|
172 | 25 => "copyright", #? |
|
|
173 | 31 => "date", |
|
|
174 | 32 => "unknown_comment32", |
|
|
175 | })->{$type} => dec_STRING]; |
|
|
176 | |
235 | |
177 | } elsif ($type == 11 || $type == 12) { |
|
|
178 | push @r, [player => $type - 11, dec_STRING]; |
|
|
179 | |
|
|
180 | } elsif ($type == 13 || $type == 14) { |
|
|
181 | push @r, [rank => $type - 13, dec_U32]; |
|
|
182 | |
|
|
183 | } elsif ($type == 15 || $type == 16) { |
|
|
184 | push @r, [set_timer => $type - 15, dec_time, dec_U16]; |
|
|
185 | |
|
|
186 | } elsif ($type == 17 || $type == 18) { |
|
|
187 | push @r, [score => $type - 17, dec_score16]; |
|
|
188 | |
|
|
189 | } elsif ($type == 19) { |
236 | } elsif (1) { |
190 | push @r, [result => dec_result]; |
237 | print STDERR KGS::Listener::Debug::dumpval(\@r); |
|
|
238 | open XTYPE, "|xtype"; print XTYPE $data; close XTYPE; |
|
|
239 | die "unknown type $type"; |
191 | |
240 | |
192 | } elsif ($type == 30) { |
241 | } elsif ($type == 30) { |
193 | push @r, [active_player => dec_U8]; |
242 | push @r, [active_player => dec_U8]; |
194 | |
243 | |
195 | } elsif ($type == 0) { # label(?) |
244 | } elsif ($type == 0) { # label(?) |
… | |
… | |
323 | |
372 | |
324 | <xsl:template match="member" mode="enc"> |
373 | <xsl:template match="member" mode="enc"> |
325 | <!-- #d#, remove when kgs is repaired --><xsl:if test="@type = 'username'"> |
374 | <!-- #d#, remove when kgs is repaired --><xsl:if test="@type = 'username'"> |
326 | $_[0]{<xsl:value-of select="@name"/>} =~ /^[A-Za-z][A-Za-z0-9]{0,11}$/ |
375 | $_[0]{<xsl:value-of select="@name"/>} =~ /^[A-Za-z][A-Za-z0-9]{0,11}$/ |
327 | or Carp::confess "FATAL: tried to send illegal username '$_[0]{<xsl:value-of select="@name"/>}'"; |
376 | or Carp::confess "FATAL: tried to send illegal username '$_[0]{<xsl:value-of select="@name"/>}'"; |
328 | </xsl:if><!--#d#--> |
|
|
329 | <!-- #d#, remove when kgs is repaired --><xsl:if test="@name = 'channel'"> |
|
|
330 | $_[0]{<xsl:value-of select="@name"/>} > 0 |
|
|
331 | or Carp::confess "FATAL: tried to send a zero channel id"; |
|
|
332 | </xsl:if><!--#d#--> |
377 | </xsl:if><!--#d#--> |
333 | enc_<xsl:value-of select="@type"/> defined $_[0]{<xsl:value-of select="@name"/>} ? $_[0]{<xsl:value-of select="@name"/> |
378 | enc_<xsl:value-of select="@type"/> defined $_[0]{<xsl:value-of select="@name"/>} ? $_[0]{<xsl:value-of select="@name"/> |
334 | <xsl:text>} : (</xsl:text> |
379 | <xsl:text>} : (</xsl:text> |
335 | <xsl:value-of select="@default"/> |
380 | <xsl:value-of select="@default"/> |
336 | <xsl:text>);</xsl:text> |
381 | <xsl:text>);</xsl:text> |