26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ |
29 */ |
30 |
30 |
|
31 #include <time.h> |
31 #include "rconsession.h" |
32 #include "rconsession.h" |
32 #include "../interface.h" |
33 #include "../interface.h" |
33 |
34 |
34 // ------------------------------------------------------------------------------------------------- |
35 // ------------------------------------------------------------------------------------------------- |
35 // |
36 // |
69 { |
70 { |
70 // Say goodbye to remote |
71 // Say goodbye to remote |
71 Bytestream packet; |
72 Bytestream packet; |
72 packet.write_byte (CLRC_DISCONNECT); |
73 packet.write_byte (CLRC_DISCONNECT); |
73 this->send (packet); |
74 this->send (packet); |
74 m_interface->print ("Disconnected from %1\n", m_address.to_string (IP_WITH_PORT)); |
75 m_interface->print ("Disconnected from %s\n", m_address.to_string (IP_WITH_PORT).chars()); |
75 m_interface->update_statusbar(); |
76 m_interface->update_statusbar(); |
76 } |
77 } |
77 |
78 |
78 m_state = RCON_DISCONNECTED; |
79 m_state = RCON_DISCONNECTED; |
79 } |
80 } |
134 switch (ServerResponse (header)) |
135 switch (ServerResponse (header)) |
135 { |
136 { |
136 case SVRC_OLDPROTOCOL: |
137 case SVRC_OLDPROTOCOL: |
137 m_interface->print_error ("Your RCON client is using outdated protocol.\n"); |
138 m_interface->print_error ("Your RCON client is using outdated protocol.\n"); |
138 m_state = RCON_DISCONNECTED; |
139 m_state = RCON_DISCONNECTED; |
139 m_interface->disconnected(); |
|
140 break; |
140 break; |
141 |
141 |
142 case SVRC_BANNED: |
142 case SVRC_BANNED: |
143 m_interface->print_error ("You have been banned from the server.\n"); |
143 m_interface->print_error ("You have been banned from the server.\n"); |
144 m_state = RCON_DISCONNECTED; |
144 m_state = RCON_DISCONNECTED; |
145 m_interface->disconnected(); |
|
146 break; |
145 break; |
147 |
146 |
148 case SVRC_SALT: |
147 case SVRC_SALT: |
149 m_salt = packet.read_string(); |
148 m_salt = packet.read_string(); |
150 m_state = RCON_AUTHENTICATING; |
149 m_state = RCON_AUTHENTICATING; |
152 break; |
151 break; |
153 |
152 |
154 case SVRC_INVALIDPASSWORD: |
153 case SVRC_INVALIDPASSWORD: |
155 m_interface->print_error ("Login failed.\n"); |
154 m_interface->print_error ("Login failed.\n"); |
156 m_state = RCON_DISCONNECTED; |
155 m_state = RCON_DISCONNECTED; |
157 m_interface->disconnected(); |
|
158 break; |
156 break; |
159 |
157 |
160 case SVRC_MESSAGE: |
158 case SVRC_MESSAGE: |
161 { |
159 { |
162 String message = packet.read_string(); |
160 String message = packet.read_string(); |
163 message.normalize(); |
161 message.normalize(); |
164 m_interface->print ("%1\n", message); |
162 m_interface->print ("%s\n", message.chars()); |
165 } |
163 } |
166 break; |
164 break; |
167 |
165 |
168 case SVRC_LOGGEDIN: |
166 case SVRC_LOGGEDIN: |
169 m_interface->print ("Login successful!\n"); |
167 m_interface->print ("Login successful!\n"); |
192 break; |
190 break; |
193 |
191 |
194 case SVRC_TOOMANYTABCOMPLETES: |
192 case SVRC_TOOMANYTABCOMPLETES: |
195 { |
193 { |
196 unsigned int numCompletions = packet.read_short(); |
194 unsigned int numCompletions = packet.read_short(); |
197 m_interface->print ("%1 completions for '%2'.\n", |
195 m_interface->print ("%d completions for '%s'.\n", |
198 int (numCompletions), m_lastTabComplete); |
196 int (numCompletions), m_lastTabComplete.chars()); |
199 } |
197 } |
200 break; |
198 break; |
201 |
199 |
202 case SVRC_TABCOMPLETE: |
200 case SVRC_TABCOMPLETE: |
203 { |
201 { |
210 { |
208 { |
211 m_interface->tab_complete (m_lastTabComplete, completes[0]); |
209 m_interface->tab_complete (m_lastTabComplete, completes[0]); |
212 } |
210 } |
213 else if (not completes.is_empty()) |
211 else if (not completes.is_empty()) |
214 { |
212 { |
215 m_interface->print ("Completions for '%1':\n", m_lastTabComplete); |
213 m_interface->print ("Completions for '%s':\n", m_lastTabComplete.chars()); |
216 |
214 |
217 for (int i = 0; i < completes.size(); i += 8) |
215 for (int i = 0; i < completes.size(); i += 8) |
218 { |
216 { |
219 Range<int> spliceRange (i, min (i + 8, completes.size() - 1)); |
217 Range<int> spliceRange (i, min (i + 8, completes.size() - 1)); |
220 StringList splice (completes.splice (spliceRange)); |
218 StringList splice (completes.splice (spliceRange)); |
221 m_interface->print ("- %1\n", splice.join (", ")); |
219 m_interface->print ("- %s\n", splice.join (", ").chars()); |
222 } |
220 } |
223 } |
221 } |
224 } |
222 } |
225 break; |
223 break; |
226 |
|
227 case SVRC_WATCHINGCVAR: |
|
228 m_interface->print ("Server acknowledges watch on %1\n", packet.read_string()); |
|
229 m_interface->print ("Its current value is %1\n", packet.read_string()); |
|
230 break; |
|
231 |
|
232 case SVRC_ALREADYWATCHINGCVAR: |
|
233 m_interface->print ("Server says you are already watching %1\n", packet.read_string()); |
|
234 break; |
|
235 |
|
236 case SVRC_WATCHCVARNOTFOUND: |
|
237 m_interface->print ("No such cvar %1\n", packet.read_string()); |
|
238 break; |
|
239 |
|
240 case SVRC_CVARCHANGED: |
|
241 { |
|
242 String name = packet.read_string(); |
|
243 String value = packet.read_string(); |
|
244 m_interface->print ("New value of %1: %2\n", name, value); |
|
245 } |
|
246 break; |
|
247 |
|
248 case SVRC_YOUREDISCONNECTED: |
|
249 { |
|
250 String message = packet.read_string(); |
|
251 m_interface->print_error ("Connection error: %1\n", message); |
|
252 m_state = RCON_DISCONNECTED; |
|
253 m_interface->disconnected(); |
|
254 } |
|
255 break; |
|
256 } |
224 } |
257 } |
225 } |
258 } |
226 } |
259 catch (std::exception& e) |
227 catch (std::exception& e) |
260 { |
228 { |
261 m_interface->print_warning ("Couldn't process packet: %1\n", e.what()); |
229 m_interface->print_warning ("Couldn't process packet: %s\n", e.what()); |
262 } |
230 } |
263 } |
231 } |
264 |
232 |
265 void RCONSession::process_server_updates (Bytestream& packet) |
233 void RCONSession::process_server_updates (Bytestream& packet) |
266 { |
234 { |
304 |
272 |
305 // ------------------------------------------------------------------------------------------------- |
273 // ------------------------------------------------------------------------------------------------- |
306 // |
274 // |
307 void RCONSession::send_hello() |
275 void RCONSession::send_hello() |
308 { |
276 { |
309 m_interface->print ("Connecting to %1...\n", m_address.to_string (IP_WITH_PORT)); |
277 m_interface->print ("Connecting to %s...\n", m_address.to_string (IP_WITH_PORT).chars()); |
310 Bytestream packet; |
278 Bytestream packet; |
311 packet.write_byte (CLRC_BEGINCONNECTION); |
279 packet.write_byte (CLRC_BEGINCONNECTION); |
312 packet.write_byte (RCON_PROTOCOL_VERSION); |
280 packet.write_byte (RCON_PROTOCOL_VERSION); |
313 send (packet); |
281 send (packet); |
314 bump_last_ping(); |
282 bump_last_ping(); |
355 bool RCONSession::send_command (const String& message) |
323 bool RCONSession::send_command (const String& message) |
356 { |
324 { |
357 if (m_state != RCON_CONNECTED or message.is_empty()) |
325 if (m_state != RCON_CONNECTED or message.is_empty()) |
358 return false; |
326 return false; |
359 |
327 |
360 if (message.starts_with ("/watch ")) |
|
361 { |
|
362 String cvarnamelist = message.mid (String ("/watch ").length(), -1); |
|
363 cvarnamelist.normalize(); |
|
364 |
|
365 Bytestream packet; |
|
366 packet.write_byte (CLRC_WATCHCVAR); |
|
367 |
|
368 for (String cvarname : cvarnamelist.split (' ')) |
|
369 { |
|
370 m_interface->print (TEXTCOLOR_Green "Requesting watch on %1...\n", cvarname); |
|
371 packet.write_string (cvarname); |
|
372 } |
|
373 send (packet); |
|
374 bump_last_ping(); |
|
375 return true; |
|
376 } |
|
377 |
|
378 Bytestream packet; |
328 Bytestream packet; |
379 packet.write_byte (CLRC_COMMAND); |
329 packet.write_byte (CLRC_COMMAND); |
380 packet.write_string (message); |
330 packet.write_string (message); |
381 send (packet); |
331 send (packet); |
382 bump_last_ping(); |
332 bump_last_ping(); |