Debian Codegraph @

src/bnetd/handle_wol.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2001 Marco Ziech ([email protected])
3 * Copyright (C) 2005 Bryan Biedenkapp ([email protected])
4 * Copyright (C) 2006,2007,2008 Pelish ([email protected])
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include "common/setup_before.h"
22 #include "handle_wol.h"
23
24 #include <cctype>
25 #include <cinttypes>
26 #include <cstdint>
27 #include <cstdlib>
28 #include <cstring>
29
30 #include "compat/strcasecmp.h"
31 #include "common/irc_protocol.h"
32 #include "common/eventlog.h"
33 #include "common/bnethash.h"
34 #include "common/tag.h"
35 #include "common/util.h"
36 #include "common/list.h"
37 #include "common/addr.h"
38 #include "common/trans.h"
39 #include "common/xstring.h"
40
41 #include "common/packet.h"
42
43 #include "prefs.h"
44 #include "command.h"
45 #include "irc.h"
46 #include "account.h"
47 #include "account_wrap.h"
48 #include "command_groups.h"
49 #include "channel.h"
50 #include "message.h"
51 #include "tick.h"
52 #include "topic.h"
53 #include "server.h"
54 #include "friends.h"
55 #include "clan.h"
56 #include "game.h"
57 #include "anongame_wol.h"
58 #include "common/setup_after.h"
59
60 namespace pvpgn
61 {
62
63 namespace bnetd
64 {
65
66 typedef int(*t_wol_command)(t_connection * conn, int numparams, char ** params, char * text);
67
68 typedef struct {
69 const char * wol_command_string;
70 t_wol_command wol_command_handler;
71 } t_wol_command_table_row;
72
73 static int _handle_user_command(t_connection * conn, int numparams, char ** params, char * text);
74 static int _handle_pass_command(t_connection * conn, int numparams, char ** params, char * text);
75 static int _handle_privmsg_command(t_connection * conn, int numparams, char ** params, char * text);
76 static int _handle_quit_command(t_connection * conn, int numparams, char ** params, char * text);
77
78 static int _handle_list_command(t_connection * conn, int numparams, char ** params, char * text);
79 static int _handle_names_command(t_connection * conn, int numparams, char ** params, char * text);
80 static int _handle_part_command(t_connection * conn, int numparams, char ** params, char * text);
81
82 static int _handle_cvers_command(t_connection * conn, int numparams, char ** params, char * text);
83 static int _handle_verchk_command(t_connection * conn, int numparams, char ** params, char * text);
84 static int _handle_apgar_command(t_connection * conn, int numparams, char ** params, char * text);
85 static int _handle_setopt_command(t_connection * conn, int numparams, char ** params, char * text);
86 static int _handle_serial_command(t_connection * conn, int numparams, char ** params, char * text);
87
88 static int _handle_squadinfo_command(t_connection * conn, int numparams, char ** params, char * text);
89 static int _handle_clanbyname_command(t_connection * conn, int numparams, char ** params, char * text);
90 static int _handle_setcodepage_command(t_connection * conn, int numparams, char ** params, char * text);
91 static int _handle_getcodepage_command(t_connection * conn, int numparams, char ** params, char * text);
92 static int _handle_setlocale_command(t_connection * conn, int numparams, char ** params, char * text);
93 static int _handle_getlocale_command(t_connection * conn, int numparams, char ** params, char * text);
94 static int _handle_getinsider_command(t_connection * conn, int numparams, char ** params, char * text);
95 static int _handle_joingame_command(t_connection * conn, int numparams, char ** params, char * text);
96 static int _handle_gameopt_command(t_connection * conn, int numparams, char ** params, char * text);
97 static int _handle_finduser_command(t_connection * conn, int numparams, char ** params, char * text);
98 static int _handle_finduserex_command(t_connection * conn, int numparams, char ** params, char * text);
99 static int _handle_page_command(t_connection * conn, int numparams, char ** params, char * text);
100 static int _handle_startg_command(t_connection * conn, int numparams, char ** params, char * text);
101 static int _handle_advertr_command(t_connection * conn, int numparams, char ** params, char * text);
102 static int _handle_advertc_command(t_connection * conn, int numparams, char ** params, char * text);
103 static int _handle_chanchk_command(t_connection * conn, int numparams, char ** params, char * text);
104 static int _handle_getbuddy_command(t_connection * conn, int numparams, char ** params, char * text);
105 static int _handle_addbuddy_command(t_connection * conn, int numparams, char ** params, char * text);
106 static int _handle_delbuddy_command(t_connection * conn, int numparams, char ** params, char * text);
107 static int _handle_host_command(t_connection * conn, int numparams, char ** params, char * text);
108 static int _handle_invmsg_command(t_connection * conn, int numparams, char ** params, char * text);
109 static int _handle_invdel_command(t_connection * conn, int numparams, char ** params, char * text);
110 static int _handle_userip_command(t_connection * conn, int numparams, char ** params, char * text);
111
112 /* Ladder server commands (we will probalby move this commands to any another handle file */
113 static int _handle_listsearch_command(t_connection * conn, int numparams, char ** params, char * text);
114 static int _handle_rungsearch_command(t_connection * conn, int numparams, char ** params, char * text);
115 static int _handle_highscore_command(t_connection * conn, int numparams, char ** params, char * text);
116
117 /* state "connected" handlers */
118 static const t_wol_command_table_row wol_con_command_table[] =
119 {
120 { "NICK", _handle_nick_command },
121 { "USER", _handle_user_command },
122 { "PING", _handle_ping_command },
123 { "PONG", _handle_pong_command },
124 { "PASS", _handle_pass_command },
125 { "PRIVMSG", _handle_privmsg_command },
126 { "QUIT", _handle_quit_command },
127
128 { "CVERS", _handle_cvers_command },
129 { "VERCHK", _handle_verchk_command },
130 { "APGAR", _handle_apgar_command },
131 { "SETOPT", _handle_setopt_command },
132 { "SERIAL", _handle_serial_command },
133
134 /* Ladder server commands */
135 { "LISTSEARCH", _handle_listsearch_command },
136 { "RUNGSEARCH", _handle_rungsearch_command },
137 { "HIGHSCORE", _handle_highscore_command },
138
139 { NULL, NULL }
140 };
141
142 /* state "logged in" handlers */
143 static const t_wol_command_table_row wol_log_command_table[] =
144 {
145 { "LIST", _handle_list_command },
146 { "TOPIC", _handle_topic_command },
147 { "JOIN", _handle_join_command },
148 { "NAMES", _handle_names_command },
149 { "PART", _handle_part_command },
150
151 { "SQUADINFO", _handle_squadinfo_command },
152 { "CLANBYNAME", _handle_clanbyname_command },
153 { "SETCODEPAGE", _handle_setcodepage_command },
154 { "SETLOCALE", _handle_setlocale_command },
155 { "GETCODEPAGE", _handle_getcodepage_command },
156 { "GETLOCALE", _handle_getlocale_command },
157 { "GETINSIDER", _handle_getinsider_command },
158 { "JOINGAME", _handle_joingame_command },
159 { "GAMEOPT", _handle_gameopt_command },
160 { "FINDUSER", _handle_finduser_command },
161 { "FINDUSEREX", _handle_finduserex_command },
162 { "PAGE", _handle_page_command },
163 { "STARTG", _handle_startg_command },
164 { "ADVERTR", _handle_advertr_command },
165 { "ADVERTC", _handle_advertc_command },
166 { "CHANCHK", _handle_chanchk_command },
167 { "GETBUDDY", _handle_getbuddy_command },
168 { "ADDBUDDY", _handle_addbuddy_command },
169 { "DELBUDDY", _handle_delbuddy_command },
170 { "TIME", _handle_time_command },
171 { "KICK", _handle_kick_command },
172 { "MODE", _handle_mode_command },
173 { "HOST", _handle_host_command },
174 { "INVMSG", _handle_invmsg_command },
175 { "INVDEL", _handle_invdel_command },
176 { "USERIP", _handle_userip_command },
177
178 { NULL, NULL }
179 };
180
181 extern int handle_wol_con_command(t_connection * conn, char const * command, int numparams, char ** params, char * text)
182 {
183 t_wol_command_table_row const *p;
184
185 for (p = wol_con_command_table; p->wol_command_string != NULL; p++) {
186 if (strcasecmp(command, p->wol_command_string) == 0) {
187 if (p->wol_command_handler != NULL)
188 return ((p->wol_command_handler)(conn, numparams, params, text));
189 }
190 }
191 return -1;
192 }
193
194 extern int handle_wol_log_command(t_connection * conn, char const * command, int numparams, char ** params, char * text)
195 {
196 t_wol_command_table_row const *p;
197
198 for (p = wol_log_command_table; p->wol_command_string != NULL; p++) {
199 if (strcasecmp(command, p->wol_command_string) == 0) {
200 if (p->wol_command_handler != NULL)
201 return ((p->wol_command_handler)(conn, numparams, params, text));
202 }
203 }
204 return -1;
205 }
206
207 static int handle_wol_authenticate(t_connection * conn, char const * passhash)
208 {
209 t_account * a;
210 char const * tempapgar;
211 char const * temphash;
212 char const * username;
213
214 if (!conn) {
215 ERROR0("got NULL connection");
216 return 0;
217 }
218 if (!passhash) {
219 eventlog(eventlog_level_error, __FUNCTION__, "got NULL passhash");
220 return 0;
221 }
222 username = conn_get_loggeduser(conn);
223 if (!username) {
224 /* redundant sanity check */
225 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn->protocol.loggeduser");
226 return 0;
227 }
228 a = accountlist_find_account(username);
229 if (!a) {
230 /* FIXME: Send real error code */
231 message_send_text(conn, message_type_notice, NULL, "Authentication failed.");
232 return 0;
233 }
234 tempapgar = conn_wol_get_apgar(conn);
235 temphash = account_get_wol_apgar(a);
236
237 if (connlist_find_connection_by_account(a) && prefs_get_kick_old_login() == 0)
238 {
239 irc_send(conn, ERR_NICKNAMEINUSE, std::string(std::string(conn_get_loggeduser(conn)) + " :Account is already in use!").c_str());
240 }
241 else if (account_get_auth_lock(a) == 1)
242 {
243 /* FIXME: Send real error code */
244 message_send_text(conn, message_type_notice, NULL, "Authentication rejected (account is locked) ");
245 }
246 else {
247 if (!temphash) {
248 /* Account auto creating */
249 account_set_wol_apgar(a, tempapgar);
250 temphash = account_get_wol_apgar(a);
251 }
252 if ((tempapgar) && (temphash) && (std::strcmp(temphash, tempapgar) == 0)) {
253 /* LOGIN is OK. We sends motd */
254 conn_login(conn, a, username);
255 conn_set_state(conn, conn_state_loggedin);
256 irc_send_motd(conn);
257 }
258 else {
259 irc_send(conn, RPL_BAD_LOGIN, ":You have specified an invalid password for that nickname."); /* bad APGAR */
260 conn_increment_passfail_count(conn);
261 //std::sprintf(temp,":Closing Link %s[Some.host]:(Password needed for that nickname.)",conn_get_loggeduser(conn));
262 //message_send_text(conn,message_type_error,conn,temp);
263 }
264 }
265 return 0;
266 }
267
268 extern int handle_wol_welcome(t_connection * conn)
269 {
270 /* This function need rewrite */
271 conn_set_state(conn, conn_state_bot_password);
272
273 if (conn_wol_get_apgar(conn)) {
274 handle_wol_authenticate(conn, conn_wol_get_apgar(conn));
275 }
276 else {
277 message_send_text(conn, message_type_notice, NULL, "No APGAR command received!");
278 }
279
280 return 0;
281 }
282
283 static int handle_wol_send_claninfo(t_connection * conn, t_clan * clan)
284 {
285 unsigned int clanid;
286 const char * clantag;
287 const char * clanname;
288
289 if (!conn)
290 {
291 ERROR0("got NULL connection");
292 return -1;
293 }
294
295 if (clan)
296 {
297 clanid = clan_get_clanid(clan);
298 clantag = clantag_to_str(clan_get_clantag(clan));
299 clanname = clan_get_name(clan);
300 irc_send(conn, RPL_BATTLECLAN, std::string(std::to_string(clanid) + "`" + std::string(clanname) + "`" + std::string(clantag) + "`0`0`1`0`0`0`0`0`0`0`x`x`x").c_str());
301 }
302 else
303 {
304 irc_send(conn, ERR_IDNOEXIST, ":ID does not exist");
305 }
306
307 return 0;
308 }
309
310 /* Commands: */
311
312 static int _handle_user_command(t_connection * conn, int numparams, char ** params, char * text)
313 {
314 /**
315 * In WOL isnt used USER command (only for backward compatibility)
316 * RFC 2812 says:
317 * USER <user> <mode> <unused> :<realname>
318 *
319 * There is WOL imput expected:
320 * USER UserName HostName irc.westwood.com :RealName
321 */
322
323 char * user = NULL;
324 t_account * a;
325
326 user = (char *)conn_get_loggeduser(conn);
327
328 if (conn_get_user(conn)) {
329 /* FIXME: Send real ERROR code/message */
330 irc_send(conn, ERR_ALREADYREGISTRED, ":You are already registred");
331 }
332 else {
333 eventlog(eventlog_level_debug, __FUNCTION__, "[{}][** WOL **] got USER: user=\"{}\"", conn_get_socket(conn), user);
334
335 a = accountlist_find_account(user);
336 if (!a) {
337 /* Auto-create account */
338 t_account * tempacct;
339 t_hash pass_hash;
340 char * pass = xstrdup(conn_wol_get_apgar(conn)); /* FIXME: Do not use bnet passhash when we have wol passhash */
341 strtolower(pass);
342
343 bnet_hash(&pass_hash, std::strlen(pass), pass);
344 xfree((void *)pass);
345
346 tempacct = accountlist_create_account(user, hash_get_str(pass_hash));
347 if (!tempacct) {
348 /* FIXME: Send real ERROR code/message */
349 irc_send(conn, RPL_BAD_LOGIN, ":Account creating failed");
350 return 0;
351 }
352
353 conn_set_user(conn, user);
354 conn_set_owner(conn, user);
355 if (conn_get_loggeduser(conn))
356 handle_wol_welcome(conn); /* only send the welcome if we have USER and NICK */
357 }
358 else {
359 conn_set_user(conn, user);
360 conn_set_owner(conn, user);
361 if (conn_get_loggeduser(conn))
362 handle_wol_welcome(conn); /* only send the welcome if we have USER and NICK */
363 }
364 }
365 return 0;
366 }
367
368 static int _handle_pass_command(t_connection * conn, int numparams, char ** params, char * text)
369 {
370 /**
371 * PASS is not used in WOL
372 * only for backward compatibility sent client PASS supersecret
373 * real password sent client by apgar command
374 */
375
376 return 0;
377 }
378
379 static int _handle_privmsg_command(t_connection * conn, int numparams, char ** params, char * text)
380 {
381 if ((numparams >= 1) && (text))
382 {
383 int i;
384 char ** e;
385
386 e = irc_get_listelems(params[0]);
387 /* FIXME: support wildcards! */
388
389 /* start amadeo: code was sent by some unkown fellow of pvpgn (maybe u wanna give us your name
390 for any credits), it adds nick-registration, i changed some things here and there... */
391 for (i = 0; ((e) && (e[i])); i++) {
392 if (strcasecmp(e[i], "matchbot") == 0) {
393 /* Anongames WOL support */
394 anongame_wol_privmsg(conn, numparams, params, text);
395 }
396 else if (conn_get_state(conn) == conn_state_loggedin) {
397 if (e[i][0] == '#') {
398 /* channel message */
399 t_channel * channel;
400
401 //PELISH: We does not support talk for not inside-channel clients now but in WOL is that feature not needed
402 if (channel = conn_get_channel(conn)) {
403 if ((std::strlen(text) >= 9) && (std::strncmp(text, "\001ACTION ", 8) == 0) && (text[std::strlen(text) - 1] == '\001')) {
404 /* at least "\001ACTION \001" */
405 /* it's a CTCP ACTION message */
406 text = text + 8;
407 text[std::strlen(text) - 1] = '\0';
408 channel_message_send(channel, message_type_emote, conn, text);
409 }
410 else {
411 if (text[0] == '/') {
412 /* "/" commands (like "/help..." */
413 handle_command(conn, text);
414 }
415 else {
416 channel_message_log(channel, conn, 1, text);
417 channel_message_send(channel, message_type_talk, conn, text);
418 }
419 }
420 }
421 else {
422 irc_send(conn, ERR_NOSUCHCHANNEL, std::string(std::string(e[0]) + " :No such channel").c_str());
423 }
424 }
425 else {
426 /* whisper */
427 t_connection * user;
428
429 if ((user = connlist_find_connection_by_accountname(e[i])))
430 {
431 message_send_text(user, message_type_whisper, conn, text);
432 }
433 else
434 {
435 irc_send(conn, ERR_NOSUCHNICK, ":No such user");
436 }
437 }
438 }
439 }
440 if (e)
441 irc_unget_listelems(e);
442 }
443 else
444 irc_send(conn, ERR_NEEDMOREPARAMS, "PRIVMSG :Not enough parameters");
445 return 0;
446 }
447
448 struct gamelist_data {
449 unsigned tcount, counter;
450 t_connection *conn;
451 };
452
453 static int append_game_info(t_game* game, void* vdata)
454 {
455 char temp[MAX_IRC_MESSAGE_LEN];
456 char temp_a[MAX_IRC_MESSAGE_LEN];
457 gamelist_data* data = static_cast<gamelist_data*>(vdata);
458 t_channel * gamechannel;
459 const char * gamename;
460 std::string topicstr;
461
462 std::memset(temp, 0, sizeof(temp));
463 std::memset(temp_a, 0, sizeof(temp_a));
464
465 data->tcount++;
466
467 if (game_get_status(game) != game_status_open) {
468 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] not listing because game is not open", conn_get_socket(data->conn));
469 return 0;
470 }
471 if (game_get_clienttag(game) != conn_get_clienttag(data->conn)) {
472 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] not listing because game is for a different client", conn_get_socket(data->conn));
473 return 0;
474 }
475
476 if (!(gamechannel = game_get_channel(game))) {
477 ERROR0("game have no channel");
478 return 0;
479 }
480 if (!(gamename = irc_convert_channel(gamechannel, data->conn))) {
481 ERROR0("game have no name");
482 return 0;
483 }
484
485 class_topic Topic;
486 topicstr = Topic.get(channel_get_name(gamechannel));
487
488 if (topicstr.empty() == false) {
489 if (std::strlen(gamename) + 1 + 20 + 1 + 1 + std::strlen(topicstr.c_str()) > MAX_IRC_MESSAGE_LEN) {
490 WARN0("LISTREPLY length exceeded");
491 return 0;
492 }
493 }
494 else {
495 if (std::strlen(gamename) + 1 + 20 + 1 + 1 > MAX_IRC_MESSAGE_LEN) {
496 WARN0("LISTREPLY length exceeded");
497 return 0;
498 }
499 }
500
501 /***
502 * WOLv1:
503 * : 326 u #nick's_game 1 0 2 0 0 1122334455 128::
504 * WOLv2:
505 * : 326 u #nick's_game 1 0 21 0 16777216 1122334455 128::g040
506 * : 326 u #nick's_game 1 0 41 1 2048 1122334455 128::g12P25,2097731398,0,0,0,WATERF~3.YRM // anon_game
507 */
508 /**
509 * The layout of the game list entry is something like this:
510 * #game_channel_name currentusers maxplayers gameType gameIsTournment gameExtension longIP LOCK::topic
511 */
512
513 std::strcat(temp, gamename);
514 std::strcat(temp, " ");
515
516 std::snprintf(temp_a, sizeof(temp_a), "%u ", game_get_ref(game)); /* curent players */
517 std::strcat(temp, temp_a);
518
519 std::snprintf(temp_a, sizeof(temp_a), "%u ", game_get_maxplayers(game)); /* max players */
520 std::strcat(temp, temp_a);
521
522 std::snprintf(temp_a, sizeof(temp_a), "%u ", channel_wol_get_game_type(gamechannel)); /* game type */
523 std::strcat(temp, temp_a);
524
525 std::snprintf(temp_a, sizeof(temp_a), "%u ", (game_get_type(game) == game_type_ladder) ? 1 : 0); /* tournament */
526 std::strcat(temp, temp_a);
527
528 std::snprintf(temp_a, sizeof(temp_a), "%s ", channel_wol_get_game_extension(gamechannel)); /* game extension */
529 std::strcat(temp, temp_a);
530
531 std::snprintf(temp_a, sizeof(temp_a), "%u ", conn_get_addr(game_get_owner(game))); /* owner IP - FIXME: address translation here!! */
532 std::strcat(temp, temp_a);
533
534 if (std::strcmp(game_get_pass(game), "") == 0)
535 std::strcat(temp, "128"); /* game is unloocked 128 == no_pass */
536 else
537 std::strcat(temp, "384"); /* game is loocked 384 == pass */
538
539 std::strcat(temp, "::");
540
541 if (topicstr.c_str()) {
542 std::snprintf(temp_a, sizeof(temp_a), "%s", topicstr.c_str()); /* topic */
543 std::strcat(temp, temp_a);
544 }
545
546 data->counter++;
547 irc_send(data->conn, RPL_GAME_CHANNEL, temp);
548
549 return 0;
550 }
551
552 static int _handle_list_command(t_connection * conn, int numparams, char ** params, char * text)
553 {
554 char temp[MAX_IRC_MESSAGE_LEN];
555 t_elem const * curr;
556
557 irc_send(conn, RPL_LISTSTART, "Channel :Users Names"); /* backward compatibility */
558
559 if ((numparams == 0) || ((numparams == 2) && (params[0]) && (params[1]) && (std::strcmp(params[0], params[1]) != 0))) {
560 /**
561 * LIST all chat channels
562 * Emperor sends as params[0] == -1 if want QuickMatch channels too, 0 if not.
563 * This sends also NOX but we dunno why.
564 * DUNE 2000 use params[0] to determine channels by channeltype
565 */
566
567 LIST_TRAVERSE_CONST(channellist(), curr) {
568 t_channel const * channel = (const t_channel*)elem_get_data(curr);
569 char const * tempname;
570
571 tempname = irc_convert_channel(channel, conn);
572
573 if ((tag_check_wolv1(conn_get_clienttag(conn))) && (std::strlen(tempname) > MAX_WOLV1_CHANNELNAME_LEN))
574 continue;
575
576 /* FIXME: Delete this if games are not in channels */
577 if ((channel_wol_get_game_type(channel) != 0))
578 continue;
579
580 sprintf(temp, "%s %u ", tempname, channel_get_length(channel));
581
582 if (channel_get_flags(channel) & channel_flags_permanent)
583 std::strcat(temp, "1"); /* Official channel */
584 else
585 std::strcat(temp, "0"); /* User channel */
586
587 if (tag_check_wolv1(conn_get_clienttag(conn)))
588 std::strcat(temp, ":"); /* WOLv1 ends by ":" FIXME: Should be an TOPIC after ":"*/
589 else
590 std::strcat(temp, " 388"); /* WOLv2 ends by "388" */
591
592 if (std::strlen(temp) > MAX_IRC_MESSAGE_LEN)
593 WARN0("LISTREPLY length exceeded");
594
595 irc_send(conn, RPL_CHANNEL, temp);
596 }
597 }
598 /**
599 * Known channel game types:
600 * 0 = Westwood Chat channels, 1 = Command & Conquer Win95 channels, 2 = Red Alert Win95 channels,
601 * 3 = Red Alert Counterstrike channels, 4 = Red Alert Aftermath channels, 5 = CnC Sole Survivor channels,
602 * 12 = C&C Renegade channels, 14 = Dune 2000 channels, 16 = Nox channels, 18 = Tiberian Sun channels,
603 * 21 = Red Alert 1 v 3.03 channels, 31 = Emperor: Battle for Dune, 33 = Red Alert 2,
604 * 37 = Nox Quest channels, 38,39,40 = Quickgame channels, 41 = Yuri's Revenge
605 */
606 if ((numparams == 0) || ((numparams == 2) && (params[0]) && (params[1]) && (std::strcmp(params[0], params[1]) == 0))) {
607 eventlog(eventlog_level_debug, __FUNCTION__, "[** WOL **] LIST [Game]");
608 /* list games */
609 struct gamelist_data data;
610 data.tcount = 0;
611 data.counter = 0;
612 data.conn = conn;
613 gamelist_traverse(&append_game_info, &data, gamelist_source_none);
614 DEBUG3("[{}] LIST sent {} of {} games", conn_get_socket(conn), data.counter, data.tcount);
615 }
616 irc_send(conn, RPL_LISTEND, ":End of LIST command");
617 return 0;
618 }
619
620 static int _handle_quit_command(t_connection * conn, int numparams, char ** params, char * text)
621 {
622 if (conn_get_channel(conn))
623 conn_quit_channel(conn, text);
624
625 irc_send(conn, RPL_QUIT, ":goodbye");
626
627 conn_set_state(conn, conn_state_destroy);
628
629 return 0;
630 }
631
632 static int _handle_names_command(t_connection * conn, int numparams, char ** params, char * text)
633 {
634 t_channel * channel;
635
636 if (numparams >= 1) {
637 char ** e;
638 char const * verytemp;
639 int i;
640
641 e = irc_get_listelems(params[0]);
642 for (i = 0; ((e) && (e[i])); i++) {
643 verytemp = irc_convert_ircname(e[i]);
644
645 if (!verytemp)
646 continue; /* something is wrong with the name ... */
647 channel = channellist_find_channel_by_name(verytemp, NULL, NULL);
648 if ((!channel) && (!(channel = conn_get_channel(conn))))
649 continue; /* channel doesn't exist */
650 irc_send_rpl_namreply(conn, channel);
651 }
652 if (e)
653 irc_unget_listelems(e);
654 }
655 else if (numparams == 0) {
656 irc_send_rpl_namreply(conn, NULL);
657 }
658 return 0;
659 }
660
661 static int _handle_part_command(t_connection * conn, int numparams, char ** params, char * text)
662 {
663 t_game * game;
664
665 conn_part_channel(conn);
666
667 if ((game = conn_get_game(conn)) && (game_get_status(game) == game_status_open))
668 conn_set_game(conn, NULL, NULL, NULL, game_type_none, 0);
669
670 return 0;
671 }
672
673 /**
674 * Fallowing commands are only in Westwood Online protocol
675 */
676 static int _handle_cvers_command(t_connection * conn, int numparams, char ** params, char * text)
677 {
678 t_clienttag clienttag;
679
680 /* Ignore command but set clienttag */
681
682 /**
683 * Heres the imput expected:
684 * CVERS [oldvernum] [SKU]
685 *
686 * SKU is specific number for any WOL client (Tiberian sun, RedAlert 2 etc.)
687 * This is the best way to set clienttag, because CVERS is the first command which
688 * client send to server.
689 */
690
691 if (numparams == 2) {
692 clienttag = tag_sku_to_uint(std::atoi(params[1]));
693 if (clienttag != CLIENTTAG_WWOL_UINT)
694 conn_set_clienttag(conn, clienttag);
695 }
696 else
697 irc_send(conn, ERR_NEEDMOREPARAMS, "CVERS :Not enough parameters");
698 return 0;
699 }
700
701 static int _handle_verchk_command(t_connection * conn, int numparams, char ** params, char * text)
702 {
703 t_clienttag clienttag;
704
705 /**
706 * Heres the imput expected:
707 * vercheck [SKU] [version]
708 *
709 * Heres the output expected:
710 *
711 * 1) Update non-existant:
712 * :[servername] 379 [username] :none none none 1 [SKU] NONREQ
713 * 2) Update existant:
714 * :[servername] 379 [username] :none none none [oldversnum] [SKU] REQ
715 */
716
717 if (numparams == 2)
718 {
719 clienttag = tag_sku_to_uint(std::atoi(params[0]));
720 if (clienttag != CLIENTTAG_WWOL_UINT)
721 conn_set_clienttag(conn, clienttag);
722
723 std::string tmp(":none none none 1 " + std::string(params[0]) + " NONREQ");
724 eventlog(eventlog_level_debug, __FUNCTION__, "[** WOL **] VERCHK {}", tmp.c_str());
725 irc_send(conn, RPL_VERCHK_NONREQ, tmp.c_str());
726 }
727 else
728 irc_send(conn, ERR_NEEDMOREPARAMS, "VERCHK :Not enough parameters");
729
730 return 0;
731 }
732
733 static int _handle_apgar_command(t_connection * conn, int numparams, char ** params, char * text)
734 {
735 char * apgar = NULL;
736
737 if ((numparams >= 1) && (params[0])) {
738 apgar = params[0];
739 conn_wol_set_apgar(conn, apgar);
740 }
741 else
742 irc_send(conn, ERR_NEEDMOREPARAMS, "APGAR :Not enough parameters");
743 return 0;
744 }
745
746 static int _handle_serial_command(t_connection * conn, int numparams, char ** params, char * text)
747 {
748 // Ignore command
749 return 0;
750 }
751
752 static int _handle_squadinfo_command(t_connection * conn, int numparams, char ** params, char * text)
753 {
754 t_clan * clan;
755
756 if ((numparams >= 1) && (params[0])) {
757 if (std::strcmp(params[0], "0") == 0) {
758 /* 0 == claninfo for itself */
759 clan = account_get_clan(conn_get_account(conn));
760 handle_wol_send_claninfo(conn, clan);
761 }
762 else {
763 /* claninfo for clanid (params[0]) */
764 clan = clanlist_find_clan_by_clanid(std::atoi(params[0]));
765 handle_wol_send_claninfo(conn, clan);
766 }
767 }
768 else
769 irc_send(conn, ERR_NEEDMOREPARAMS, "SQUADINFO :Not enough parameters");
770 return 0;
771 }
772
773 static int _handle_clanbyname_command(t_connection * conn, int numparams, char ** params, char * text)
774 {
775 t_clan * clan;
776
777 if ((numparams >= 1) && (params[0])) {
778 clan = account_get_clan(accountlist_find_account(params[0]));
779 handle_wol_send_claninfo(conn, clan);
780 }
781 else
782 irc_send(conn, ERR_NEEDMOREPARAMS, "CLANBYNAME :Not enough parameters");
783 return 0;
784 }
785
786 static int _handle_setopt_command(t_connection * conn, int numparams, char ** params, char * text)
787 {
788 char ** elems;
789
790 /**
791 * This is option for enabling/disabling Page and Find user.
792 *
793 * Heres the input expected:
794 * SETOPT 17,32
795 *
796 * First parameter: 16 == FindDisabled 17 == FindEnabled
797 * Second parameter: 32 == PageDisabled 33 == PageEnabled
798 */
799
800 if ((numparams >= 1) && (params[0])) {
801 elems = irc_get_listelems(params[0]);
802
803 if ((elems) && (elems[0]) && (elems[1])) {
804 conn_wol_set_findme(conn, ((std::strcmp(elems[0], "17") == 0) ? true : false));
805 conn_wol_set_pageme(conn, ((std::strcmp(elems[1], "33") == 0) ? true : false));
806 }
807 if (elems)
808 irc_unget_listelems(elems);
809 }
810 else
811 irc_send(conn, ERR_NEEDMOREPARAMS, "SETOPT :Not enough parameters");
812 return 0;
813 }
814
815 static int _handle_setcodepage_command(t_connection * conn, int numparams, char ** params, char * text)
816 {
817 char * codepage = NULL;
818
819 if ((numparams >= 1) && (params[0])) {
820 codepage = params[0];
821 conn_wol_set_codepage(conn, std::atoi(codepage));
822 irc_send(conn, RPL_SET_CODEPAGE, codepage);
823 }
824 else
825 irc_send(conn, ERR_NEEDMOREPARAMS, "SETCODEPAGE :Not enough parameters");
826 return 0;
827 }
828
829 static int _handle_getcodepage_command(t_connection * conn, int numparams, char ** params, char * text)
830 {
831 std::string temp;
832
833 if ((numparams >= 1) && (params[0]))
834 {
835 for (auto i = 0; i < numparams; i++)
836 {
837 int codepage = 0;
838 t_connection * user = connlist_find_connection_by_accountname(params[i]);
839 if (user)
840 codepage = conn_wol_get_codepage(user);
841
842 temp.append(std::string(params[i]) + "`" + std::to_string(codepage));
843
844 if (i < numparams - 1)
845 temp.append("`");
846 }
847
848 irc_send(conn, RPL_GET_CODEPAGE, temp.c_str());
849 }
850 else
851 irc_send(conn, ERR_NEEDMOREPARAMS, "GETCODEPAGE :Not enough parameters");
852 return 0;
853 }
854
855 static int _handle_setlocale_command(t_connection * conn, int numparams, char ** params, char * text)
856 {
857 t_account * account = conn_get_account(conn);
858 int locale;
859
860 if ((numparams >= 1) && (params[0])) {
861 locale = std::atoi(params[0]);
862 account_set_locale(account, locale);
863 irc_send(conn, RPL_SET_LOCALE, params[0]);
864 }
865 else
866 irc_send(conn, ERR_NEEDMOREPARAMS, "SETLOCALE :Not enough parameters");
867 return 0;
868 }
869
870 static int _handle_getlocale_command(t_connection * conn, int numparams, char ** params, char * text)
871 {
872 std::string temp;
873
874 if ((numparams >= 1) && (params[0]))
875 {
876 for (auto i = 0; i < numparams; i++)
877 {
878 int locale = 0;
879 t_account * account = accountlist_find_account(params[i]);
880 if (account)
881 locale = account_get_locale(account);
882
883 temp.append(std::string(params[i]) + "`" + std::to_string(locale));
884 if (i < numparams - 1)
885 temp.append("`");
886 }
887 irc_send(conn, RPL_GET_LOCALE, temp.c_str());
888 }
889 else
890 irc_send(conn, ERR_NEEDMOREPARAMS, "GETLOCALE :Not enough parameters");
891 return 0;
892 }
893
894 static int _handle_getinsider_command(t_connection * conn, int numparams, char ** params, char * text)
895 {
896 /**
897 * Here is imput expected:
898 * GETINSIDER [nickname]
899 * Here is output expected:
900 * :[servername] 399 [nick] [nickname]`0
901 */
902
903 if ((numparams >= 1) && (params[0]))
904 irc_send(conn, RPL_GET_INSIDER, std::string(std::string(params[0]) + "`0").c_str());
905 else
906 irc_send(conn, ERR_NEEDMOREPARAMS, "GETINSIDER :Not enough parameters");
907
908 return 0;
909 }
910
911 static int _handle_joingame_command(t_connection * conn, int numparams, char ** params, char * text)
912 {
913 char _temp[MAX_IRC_MESSAGE_LEN];
914
915 std::memset(_temp, 0, sizeof(_temp));
916
917 /**
918 * Basically this has 2 modes, Join Game and Create Game output is pretty much
919 * the same...input and output of JOINGAME is listed below. By the way, there is a
920 * hack in here, for Red Alert 1, it use's JOINGAME for some reason to join a lobby channel.
921 *
922 * Here is WOLv1 input expected:
923 * JOINGAME [#Game_channel_name] [MinPlayers] [MaxPlayers] [channelType] 1 1 [gameIsTournament]
924 * Knowed channelTypes (0-chat, 1-cnc, 2-ra1, 3-racs, 4-raam, 5-solsurv... listed in tag.cpp)
925 *
926 * Here is WOLv2 input expected:
927 * JOINGAME [#Game_channel_name] [MinPlayers] [MaxPlayers] [channelType] unknown unknown [gameIsTournament] [gameExtension] [password_optional]
928 *
929 * Heres the output expected:
930 * user!WWOL@hostname JOINGAME [MinPlayers] [MaxPlayers] [channelType] unknown clanID [longIP] [gameIsTournament] :[#Game_channel_name]
931 */
932 if ((numparams == 2) || (numparams == 3)) {
933 char ** e;
934
935 e = irc_get_listelems(params[0]);
936 if ((e) && (e[0])) {
937 char const * gamename = irc_convert_ircname(e[0]);
938 // char * old_channel_name = NULL;
939 t_game * game;
940 t_game_type gametype;
941 t_channel * channel;
942 t_channel * old_channel = conn_get_channel(conn);
943 char gamepass[MAX_GAMEPASS_LEN];
944
945 std::memset(gamepass, 0, sizeof(gamepass));
946
947 if ((conn_get_clienttag(conn) == CLIENTTAG_REDALERT_UINT)
948 && (channellist_find_channel_by_name(gamename, NULL, NULL) != NULL)
949 && (gamelist_find_game_available(gamename, conn_get_clienttag(conn), game_type_all) == NULL)) {
950 /* BUG in Red Alert 1 v3.03e - forwarding to _handle_join_command */
951 DEBUG0("BUG in RA1 v3.03e - forwarding to _handle_join_command");
952 _handle_join_command(conn, numparams, params, text);
953 if (e)
954 irc_unget_listelems(e);
955 return 0;
956 }
957
958 if (!(gamename) || !(game = gamelist_find_game_available(gamename, conn_get_clienttag(conn), game_type_all)))
959 {
960 irc_send(conn, ERR_GAMEHASCLOSED, std::string(std::string(e[0]) + " :Game channel has closed").c_str());
961
962 if (e)
963 irc_unget_listelems(e);
964
965 return 0;
966 }
967
968 channel = game_get_channel(game);
969
970 if (game_get_ref(game) == game_get_maxplayers(game))
971 {
972 irc_send(conn, ERR_CHANNELISFULL, std::string(std::string(e[0]) + " :Channel is full.").c_str());
973
974 if (e)
975 irc_unget_listelems(e);
976
977 return 0;
978 }
979
980 if (channel_check_banning(channel, conn))
981 {
982 irc_send(conn, ERR_BANNEDFROMCHAN, std::string(std::string(e[0]) + " :You are banned from that channel.").c_str());
983
984 if (e)
985 irc_unget_listelems(e);
986
987 return 0;
988 }
989
990 if (std::strcmp(game_get_pass(game), "") != 0)
991 {
992 if ((numparams == 3) && (params[2]) && (std::strcmp(params[2], game_get_pass(game)) == 0))
993 {
994 std::strcpy(gamepass, params[2]);
995 }
996 else
997 {
998 irc_send(conn, ERR_BADCHANNELKEY, std::string(std::string(e[0]) + ":Bad password").c_str());
999
1000 if (e)
1001 irc_unget_listelems(e);
1002
1003 return 0;
1004 }
1005 }
1006
1007 gametype = game_get_type(game);
1008
1009 if ((conn_set_game(conn, gamename, gamepass, "", gametype, 0)) < 0)
1010 {
1011 irc_send(conn, ERR_GAMEHASCLOSED, std::string(std::string(e[0]) + " :JOINGAME failed").c_str());
1012 }
1013 else
1014 {
1015 /*conn_set_channel()*/
1016 channel = game_get_channel(game);
1017 conn_set_channel_var(conn, channel);
1018 channel_add_connection(channel, conn);
1019 channel = conn_get_channel(conn);
1020
1021 if (channel != old_channel) {
1022 if (tag_check_wolv1(conn_get_clienttag(conn))) {
1023 /* WOLv1 JOINGAME message */
1024 std::sprintf(_temp, "%u %u %u 1 1 %u :%s", channel_get_min(channel), game_get_maxplayers(game), channel_wol_get_game_type(channel),
1025 ((game_get_type(game) == game_type_ladder) ? 1 : 0), irc_convert_channel(channel, conn));
1026 }
1027 else {
1028 /* WOLv2 JOINGAME message with BATTLECLAN support */
1029 t_clan * clan = account_get_clan(conn_get_account(conn));
1030 unsigned int clanid = 0;
1031
1032 if (clan)
1033 clanid = clan_get_clanid(clan);
1034
1035 std::sprintf(_temp, "%u %u %u 1 %u %u %u :%s", channel_get_min(channel), game_get_maxplayers(game), channel_wol_get_game_type(channel),
1036 clanid, conn_get_addr(conn), ((game_get_type(game) == game_type_ladder) ? 1 : 0), irc_convert_channel(channel, conn));
1037 }
1038
1039 channel_set_userflags(conn);
1040 /* we have to send the JOINGAME acknowledgement */
1041 channel_message_send(channel, message_wol_joingame, conn, _temp);
1042
1043 irc_send_topic(conn, channel);
1044
1045 irc_send_rpl_namreply(conn, channel);
1046 }
1047 else
1048 {
1049 irc_send(conn, ERR_GAMEHASCLOSED, std::string(std::string(e[0]) + " :JOINGAME failed").c_str());
1050 }
1051 }
1052 }
1053 if (e)
1054 irc_unget_listelems(e);
1055 return 0;
1056 }
1057 else if ((numparams >= 7)) {
1058 char ** e;
1059
1060 eventlog(eventlog_level_debug, __FUNCTION__, "[** WOL **] JOINGAME: * Create * ({}, {})",
1061 params[0], params[1]);
1062
1063 if ((numparams == 7)) {
1064 /* WOLv1 JOINGAME Create */
1065 std::snprintf(_temp, sizeof(_temp), "%s %s %s %s 0 %s :%s", params[1], params[2], params[3], params[4], params[6], params[0]);
1066 }
1067 /* WOLv2 JOINGAME Create */
1068 else if ((numparams >= 8)) {
1069 t_clan * clan = account_get_clan(conn_get_account(conn));
1070 unsigned int clanid = 0;
1071
1072 if (clan)
1073 clanid = clan_get_clanid(clan);
1074 std::snprintf(_temp, sizeof(_temp), "%s %s %s %s %u %u %s :%s", params[1], params[2], params[3], params[4], clanid, conn_get_addr(conn), params[6], params[0]);
1075 }
1076 eventlog(eventlog_level_debug, __FUNCTION__, "[** WOL **] JOINGAME [Game Options] ({})", _temp);
1077
1078 e = irc_get_listelems(params[0]);
1079 if ((e) && (e[0])) {
1080 char const * gamename = irc_convert_ircname(e[0]);
1081 t_game_type gametype;
1082 char gamepass[MAX_GAMEPASS_LEN];
1083
1084 std::memset(gamepass, 0, sizeof(gamepass));
1085
1086 if (std::strcmp(params[6], "1") == 0)
1087 gametype = game_type_ladder;
1088 else
1089 gametype = game_type_ffa;
1090 // gametype = game_type_none;
1091
1092 if ((numparams >= 8) && (params[8]))
1093 {
1094 std::snprintf(gamepass, sizeof gamepass, "%s", params[8]);
1095 }
1096
1097 if ((!(gamename)) || ((conn_set_game(conn, gamename, gamepass, "", gametype, 0))<0)) {
1098 irc_send(conn, ERR_NOSUCHCHANNEL, ":JOINGAME failed"); /* FIXME: be more precise; what is the real error code for that? */
1099 }
1100 else {
1101 t_game * game = conn_get_game(conn);
1102 t_channel * channel = channel_create(gamename, gamename, 0, 0, 1, 1, prefs_get_chanlog(), NULL, NULL, (prefs_get_maxusers_per_channel() > 0) ? prefs_get_maxusers_per_channel() : -1, 0, 0, 0, NULL);
1103 game_set_channel(game, channel);
1104 conn_set_channel_var(conn, channel);
1105 channel_add_connection(channel, conn);
1106 channel_set_userflags(conn);
1107
1108 game_set_maxplayers(game, std::atoi(params[2]));
1109
1110 channel_set_min(channel, std::atoi(params[1]));
1111 // HACK: Currently, this is the best way to set the channel game type...
1112 channel_wol_set_game_type(channel, std::atoi(params[3]));
1113
1114 if (params[7])
1115 channel_wol_set_game_extension(channel, params[7]);
1116 else
1117 channel_wol_set_game_extension(channel, "0");
1118
1119 // we have to send the JOINGAME acknowledgement
1120 message_send_text(conn, message_wol_joingame, conn, _temp);
1121 irc_send_topic(conn, channel);
1122 irc_send_rpl_namreply(conn, channel);
1123 }
1124 }
1125 if (e)
1126 irc_unget_listelems(e);
1127 }
1128 else
1129 irc_send(conn, ERR_NEEDMOREPARAMS, "JOINGAME :Not enough parameters");
1130 return 0;
1131 }
1132
1133 static int _handle_gameopt_command(t_connection * conn, int numparams, char ** params, char * text)
1134 {
1135 char temp[MAX_IRC_MESSAGE_LEN];
1136
1137 /**
1138 * Basically this has 2 modes as like in PRIVMSG - whisper and talk. What is in
1139 * text is pretty much unknown, we just dump this to the client to deal with...
1140 *
1141 * Heres the output expected (when gameopt is channel talk):
1142 * user!WWOL@hostname GAMEOPT #game_channel_name :gameOptions
1143 *
1144 * Heres the output expected (when gameopt is whispered):
1145 * user!WWOL@hostname GAMEOPT sender_nick_name :gameOptions
1146 */
1147
1148 if ((numparams >= 1) && (params[0]) && (text)) {
1149 int i;
1150 char ** e;
1151 t_connection * user;
1152 t_channel * channel;
1153
1154 e = irc_get_listelems(params[0]);
1155 /* FIXME: support wildcards! */
1156
1157 for (i = 0; ((e) && (e[i])); i++) {
1158 if (e[i][0] == '#') {
1159 /* channel gameopt */
1160 if (channel = conn_get_channel(conn)) {
1161 channel_message_send(channel, message_type_gameopt_talk, conn, text);
1162 }
1163 else {
1164 std::snprintf(temp, sizeof(temp), "%s :No such channel", params[0]);
1165 irc_send(conn, ERR_NOSUCHCHANNEL, temp);
1166 }
1167 }
1168 else {
1169 /* user gameopt */
1170 if ((user = connlist_find_connection_by_accountname(e[i]))) {
1171 message_send_text(user, message_type_gameopt_whisper, conn, text);
1172 }
1173 else {
1174 std::snprintf(temp, sizeof(temp), "%s :No such nick", e[i]);
1175 irc_send(conn, ERR_NOSUCHNICK, temp);
1176 }
1177 }
1178 }
1179 if (e)
1180 irc_unget_listelems(e);
1181 }
1182 else
1183 irc_send(conn, ERR_NEEDMOREPARAMS, "GAMEOPT :Not enough parameters");
1184 return 0;
1185 }
1186
1187 static int _handle_finduser_command(t_connection * conn, int numparams, char ** params, char * text)
1188 {
1189 char _temp[MAX_IRC_MESSAGE_LEN];
1190 char const * wolname = NULL;
1191
1192 std::memset(_temp, 0, sizeof(_temp));
1193
1194 if ((numparams >= 1) && (params[0])) {
1195 t_connection * user;
1196
1197 if ((user = connlist_find_connection_by_accountname(params[0])) && (conn_wol_get_findme(user))) {
1198 wolname = irc_convert_channel(conn_get_channel(user), conn);
1199 std::snprintf(_temp, sizeof(_temp), "0 :%s", wolname); /* User found in channel wolname */
1200 }
1201 else
1202 std::snprintf(_temp, sizeof(_temp), "1 :"); /* user not loged or have not allowed find */
1203
1204 irc_send(conn, RPL_FIND_USER, _temp);
1205 }
1206 else
1207 irc_send(conn, ERR_NEEDMOREPARAMS, "FINDUSER :Not enough parameters");
1208 return 0;
1209 }
1210
1211 static int _handle_finduserex_command(t_connection * conn, int numparams, char ** params, char * text)
1212 {
1213 char _temp[MAX_IRC_MESSAGE_LEN];
1214 char const * wolname = NULL;
1215
1216 std::memset(_temp, 0, sizeof(_temp));
1217
1218 if ((numparams >= 1) && (params[0])) {
1219 t_connection * user;
1220
1221 if ((user = connlist_find_connection_by_accountname(params[0])) && (conn_wol_get_findme(user))) {
1222 wolname = irc_convert_channel(conn_get_channel(user), conn);
1223 std::snprintf(_temp, sizeof(_temp), "0 :%s,0", wolname); /* User found in channel wolname */
1224 }
1225 else
1226 std::snprintf(_temp, sizeof(_temp), "1 :"); /* user not loged or have not allowed find */
1227
1228 irc_send(conn, RPL_FIND_USER_EX, _temp);
1229 }
1230 else
1231 irc_send(conn, ERR_NEEDMOREPARAMS, "FINDUSEREX :Not enough parameters");
1232 return 0;;
1233 }
1234
1235 static int _handle_page_command(t_connection * conn, int numparams, char ** params, char * text)
1236 {
1237 char _temp[MAX_IRC_MESSAGE_LEN];
1238 bool paged = false;
1239
1240 if ((numparams >= 1) && (params[0]) && (text)) {
1241 t_connection * user;
1242
1243 if (std::strcmp(params[0], "0") == 0) {
1244 /* PAGE for MY BATTLECLAN */
1245 t_clan * clan = account_get_clan(conn_get_account(conn));
1246
1247 if ((clan) && (clan_send_message_to_online_members(clan, message_type_page, conn, text) >= 1))
1248 paged = true;
1249 }
1250 else if ((user = connlist_find_connection_by_accountname(params[0])) && (conn_wol_get_pageme(user))) {
1251 message_send_text(user, message_type_page, conn, text);
1252 paged = true;
1253 }
1254
1255 if (paged)
1256 std::snprintf(_temp, sizeof(_temp), "0 :"); /* Page was succesfull */
1257 else
1258 std::snprintf(_temp, sizeof(_temp), "1 :"); /* User not loged in or have not allowed page */
1259
1260 irc_send(conn, RPL_PAGE, _temp);
1261 }
1262 else
1263 irc_send(conn, ERR_NEEDMOREPARAMS, "PAGE :Not enough parameters");
1264 return 0;
1265 }
1266
1267 static int _handle_startg_command(t_connection * conn, int numparams, char ** params, char * text)
1268 {
1269 char temp[MAX_IRC_MESSAGE_LEN];
1270 char _temp_a[MAX_IRC_MESSAGE_LEN];
1271 t_channel * channel;
1272 t_game * game;
1273
1274 /**
1275 * Imput expected:
1276 * STARTG [channel_name] [nick1](,nick2_optional)
1277 *
1278 * Heres the output expected (this can have up-to 8 entries (ie 8 players):
1279 * (we are assuming for this example that user1 is the game owner)
1280 *
1281 * WOLv1:
1282 * :user1!WWOL@hostname STARTG u :owner_ip gameNumber time_t
1283 *
1284 * WOLv2:
1285 * :user1!WWOL@hostname STARTG u :user1 xxx.xxx.xxx.xxx user2 xxx.xxx.xxx.xxx :gameNumber time_t
1286 */
1287
1288 if ((numparams >= 2) && (params[1])) {
1289 int i;
1290
1291 std::memset(temp, 0, sizeof(temp));
1292
1293 char ** e = irc_get_listelems(params[1]);
1294 /* FIXME: support wildcards! */
1295
1296 if (!(game = conn_get_game(conn))) {
1297 ERROR0("conn has not game");
1298 irc_unget_listelems(e);
1299 return 0;
1300 }
1301
1302 std::strcat(temp, ":");
1303
1304 if (tag_check_wolv1(conn_get_clienttag(conn))) {
1305 t_connection * user;
1306 channel = conn_get_channel(conn);
1307
1308 for (user = channel_get_first(channel); user; user = channel_get_next()) {
1309 char const * name = conn_get_chatname(user);
1310 if (std::strcmp(conn_get_chatname(conn), name) != 0) {
1311 std::snprintf(temp, sizeof(temp), "%s ", addr_num_to_ip_str(conn_get_addr(game_get_owner(game))));
1312 }
1313 }
1314 }
1315 else {
1316 for (i = 0; ((e) && (e[i])); i++) {
1317 t_connection * user;
1318 const char * addr = NULL;
1319
1320 if ((user = connlist_find_connection_by_accountname(e[i]))) {
1321 addr = addr_num_to_ip_str(conn_get_addr(user));
1322 }
1323 std::snprintf(_temp_a, sizeof(_temp_a), "%s %s ", e[i], addr);
1324 std::strcat(temp, _temp_a);
1325 }
1326 std::strcat(temp, ":");
1327 }
1328
1329 game_set_status(game, game_status_started);
1330
1331 std::snprintf(_temp_a, sizeof(_temp_a), "%u %" PRId64, game_get_id(game), static_cast<std::int64_t>(game_get_start_time(game)));
1332 std::strcat(temp, _temp_a);
1333
1334 for (i = 0; ((e) && (e[i])); i++) {
1335 t_connection * user;
1336 if ((user = connlist_find_connection_by_accountname(e[i]))) {
1337 message_send_text(user, message_wol_start_game, conn, temp);
1338 }
1339 }
1340
1341 if (e)
1342 irc_unget_listelems(e);
1343 }
1344 else
1345 irc_send(conn, ERR_NEEDMOREPARAMS, "STARTG :Not enough parameters");
1346 return 0;
1347 }
1348
1349 static int _handle_advertr_command(t_connection * conn, int numparams, char ** params, char * text)
1350 {
1351 char temp[MAX_IRC_MESSAGE_LEN];
1352
1353 std::memset(temp, 0, sizeof(temp));
1354
1355 /**
1356 * Heres the imput expected
1357 * ADVERTR [channel]
1358 *
1359 * Heres the output expected
1360 * :[servername] ADVERTR 5 [channel]
1361 */
1362
1363 if ((numparams >= 1) && (params[0])) {
1364 std::snprintf(temp, sizeof(temp), "5 %s", params[0]);
1365 message_send_text(conn, message_wol_advertr, conn, temp);
1366 }
1367 else
1368 irc_send(conn, ERR_NEEDMOREPARAMS, "ADVERTR :Not enough parameters");
1369 return 0;
1370 }
1371
1372 static int _handle_advertc_command(t_connection * conn, int numparams, char ** params, char * text)
1373 {
1374 /* FIXME: Not implemented yet */
1375 return 0;
1376 }
1377
1378 static int _handle_chanchk_command(t_connection * conn, int numparams, char ** params, char * text)
1379 {
1380 char temp[MAX_IRC_MESSAGE_LEN];
1381 t_channel * channel;
1382
1383 std::memset(temp, 0, sizeof(temp));
1384
1385 /**
1386 * Heres the imput expected
1387 * chanchk [channel]
1388 *
1389 * Heres the output expected
1390 * :[servername] CHANCHK [channel]
1391 */
1392
1393 if ((numparams >= 1) && (params[0]))
1394 {
1395 if ((channel = channellist_find_channel_by_name(irc_convert_ircname(params[0]), NULL, NULL)))
1396 {
1397 std::snprintf(temp, sizeof temp, "%s", params[0]);
1398 message_send_text(conn, message_wol_chanchk, conn, temp);
1399 }
1400 else
1401 {
1402 /* FIXME: This is not dumped from original servers... this is probably wrong */
1403 std::snprintf(temp, sizeof(temp), "%s :No such channel", params[0]);
1404 irc_send(conn, ERR_NOSUCHCHANNEL, temp);
1405 }
1406 }
1407 else
1408 irc_send(conn, ERR_NEEDMOREPARAMS, "CHANCHK :Not enough parameters");
1409 return 0;
1410 }
1411
1412 static int _handle_getbuddy_command(t_connection * conn, int numparams, char ** params, char * text)
1413 {
1414 char temp[MAX_IRC_MESSAGE_LEN];
1415 char _temp[MAX_IRC_MESSAGE_LEN];
1416 char const * friend_name;
1417 t_account * my_acc;
1418 t_account * friend_acc;
1419 t_list * flist;
1420 t_friend * fr;
1421 int num;
1422 unsigned int uid;
1423 int i;
1424
1425 std::memset(temp, 0, sizeof(temp));
1426 std::memset(_temp, 0, sizeof(_temp));
1427
1428 /**
1429 * Heres the output expected
1430 * :[servername] 333 [user] [buddy_name1]`[buddy_name2]`
1431 *
1432 * Without names:
1433 * :[servername] 333 [user]
1434 */
1435
1436 my_acc = conn_get_account(conn);
1437 num = account_get_friendcount(my_acc);
1438
1439 flist = account_get_friends(my_acc);
1440
1441 if (flist != NULL) {
1442 for (i = 0; i < num; i++) {
1443 if ((!(uid = account_get_friend(my_acc, i))) || (!(fr = friendlist_find_uid(flist, uid)))) {
1444 eventlog(eventlog_level_error, __FUNCTION__, "friend uid in list");
1445 continue;
1446 }
1447 friend_acc = friend_get_account(fr);
1448 friend_name = account_get_name(friend_acc);
1449 std::snprintf(_temp, sizeof(_temp), "%s`", friend_name);
1450 std::strcat(temp, _temp);
1451 }
1452 }
1453 irc_send(conn, RPL_GET_BUDDY, temp);
1454 return 0;
1455 }
1456
1457 static int _handle_addbuddy_command(t_connection * conn, int numparams, char ** params, char * text)
1458 {
1459 char temp[MAX_IRC_MESSAGE_LEN];
1460 t_account * my_acc;
1461 t_account * friend_acc;
1462
1463 std::memset(temp, 0, sizeof(temp));
1464
1465 /**
1466 * Heres the imput expected
1467 * ADDBUDDY [buddy_name]
1468 *
1469 * Heres the output expected
1470 * :[servername] 334 [user] [buddy_name]
1471 */
1472
1473 if ((numparams >= 1) && (params[0])) {
1474 my_acc = conn_get_account(conn);
1475 if (friend_acc = accountlist_find_account(params[0])) {
1476 account_add_friend(my_acc, friend_acc);
1477 /* FIXME: Check if add friend is done if not then send right message */
1478
1479 std::snprintf(temp, sizeof(temp), "%s", params[0]);
1480 irc_send(conn, RPL_ADD_BUDDY, temp);
1481 }
1482 else {
1483 std::snprintf(temp, sizeof(temp), "%s :No such nick", params[0]);
1484 irc_send(conn, ERR_NOSUCHNICK, temp);
1485 /* NOTE: this is not dumped from WOL, this not shows message
1486 but in Emperor doesnt gives name to list, in RA2 have no efect */
1487 }
1488 }
1489 else
1490 irc_send(conn, ERR_NEEDMOREPARAMS, "ADDBUDDY :Not enough parameters");
1491 return 0;
1492 }
1493
1494 static int _handle_delbuddy_command(t_connection * conn, int numparams, char ** params, char * text)
1495 {
1496 char temp[MAX_IRC_MESSAGE_LEN];
1497 char const * friend_name;
1498 t_account * my_acc;
1499 int num;
1500
1501 std::memset(temp, 0, sizeof(temp));
1502
1503 /**
1504 * Heres the imput expected
1505 * DELBUDDY [buddy_name]
1506 *
1507 * Heres the output expected
1508 * :[servername] 335 [user] [buddy_name]
1509 */
1510
1511 if ((numparams >= 1) && (params[0])) {
1512 friend_name = params[0];
1513 my_acc = conn_get_account(conn);
1514
1515 num = account_remove_friend2(my_acc, friend_name);
1516
1517 /**
1518 * FIXME: Check if remove friend is done if not then send right message
1519 * Btw I dont know another then RPL_DEL_BUDDY message yet.
1520 */
1521
1522 std::snprintf(temp, sizeof(temp), "%s", friend_name);
1523 irc_send(conn, RPL_DEL_BUDDY, temp);
1524 }
1525 else
1526 irc_send(conn, ERR_NEEDMOREPARAMS, "DELBUDDY :Not enough parameters");
1527 return 0;
1528 }
1529
1530 static int _handle_host_command(t_connection * conn, int numparams, char ** params, char * text)
1531 {
1532 char temp[MAX_IRC_MESSAGE_LEN];
1533 t_connection * user;
1534
1535 std::memset(temp, 0, sizeof(temp));
1536
1537 if ((numparams >= 1) && (params[0])) {
1538 if ((user = connlist_find_connection_by_accountname(params[0]))) {
1539 std::snprintf(temp, sizeof(temp), ": %s", text);
1540 message_send_text(user, message_type_host, conn, temp);
1541 }
1542 else {
1543 std::snprintf(temp, sizeof(temp), "%s :No such nick", params[0]);
1544 irc_send(conn, ERR_NOSUCHNICK, temp);
1545 }
1546 }
1547 else
1548 irc_send(conn, ERR_NEEDMOREPARAMS, "HOST :Not enough parameters");
1549 return 0;
1550 }
1551
1552 static int _handle_invmsg_command(t_connection * conn, int numparams, char ** params, char * text)
1553 {
1554 char temp[MAX_IRC_MESSAGE_LEN];
1555 char ** e;
1556 t_connection * user;
1557 int i;
1558
1559 /**
1560 * Here is the imput expected:
1561 * INVMSG [channel] [unknown] [invited],[invited2_optional]
1562 * [unknown] can be 1 or 2
1563 *
1564 * Here is the output expected:
1565 * :user!WWOL@hostname INVMSG [invited] [channel] [unknown]
1566 */
1567
1568 if ((numparams >= 3) && (params[0]) && (params[1]) && (params[2])) {
1569 std::memset(temp, 0, sizeof(temp));
1570 e = irc_get_listelems(params[2]);
1571
1572 for (i = 0; ((e) && (e[i])); i++) {
1573 if ((user = connlist_find_connection_by_accountname(e[i]))) {
1574 std::snprintf(temp, sizeof(temp), "%s %s", params[0], params[1]);
1575 /* FIXME: set user to linvitelist! */
1576 message_send_text(user, message_type_invmsg, conn, temp);
1577 }
1578 }
1579
1580 if (e)
1581 irc_unget_listelems(e);
1582 }
1583 else {
1584 irc_send(conn, ERR_NEEDMOREPARAMS, "INVMSG :Not enough parameters");
1585 }
1586 return 0;
1587 }
1588
1589 static int _handle_invdel_command(t_connection * conn, int numparams, char ** params, char * text)
1590 {
1591 /* FIXME: Not implemented yet */
1592 return 0;
1593 }
1594
1595 static int _handle_userip_command(t_connection * conn, int numparams, char ** params, char * text)
1596 {
1597 char temp[MAX_IRC_MESSAGE_LEN];
1598 t_connection * user;
1599 const char * addr = NULL;
1600
1601 std::memset(temp, 0, sizeof(temp));
1602
1603 if ((numparams >= 1) && (params[0])) {
1604 if ((user = connlist_find_connection_by_accountname(params[0]))) {
1605 addr = addr_num_to_ip_str(conn_get_addr(user));
1606 //FIXME: We are not sure of first parameter. It can be also nickname of command sender
1607 std::snprintf(temp, sizeof(temp), "%s %s", params[0], addr);
1608 message_send_text(conn, message_wol_userip, conn, temp);
1609 }
1610 else {
1611 std::snprintf(temp, sizeof(temp), "%s :No such nick", params[0]);
1612 irc_send(conn, ERR_NOSUCHNICK, temp);
1613 }
1614 }
1615 else
1616 irc_send(conn, ERR_NEEDMOREPARAMS, "USERIP :Not enough parameters");
1617 return 0;
1618 }
1619
1620 /**
1621 * LADDER Server commands:
1622 */
1623 static int _ladder_send(t_connection * conn, char const * command)
1624 {
1625 char data[MAX_IRC_MESSAGE_LEN + 1];
1626 unsigned len = 0;
1627
1628 t_packet* const p = packet_create(packet_class_raw);
1629 if (!p)
1630 {
1631 return -1;
1632 }
1633
1634 if (command)
1635 len = (std::strlen(command) + 6);
1636
1637 if (len > MAX_IRC_MESSAGE_LEN)
1638 {
1639 eventlog(eventlog_level_error, __FUNCTION__, "message to send is too large ({} bytes)", len);
1640 packet_del_ref(p);
1641 return -1;
1642 }
1643
1644
1645 std::sprintf(data, "\r\n\r\n\r\n%s", command);
1646
1647 packet_set_size(p, 0);
1648 packet_append_data(p, data, len);
1649 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] sent \"{}\"", conn_get_socket(conn), data);
1650 conn_push_outqueue(conn, p);
1651 packet_del_ref(p);
1652
1653 /* In ladder server we must destroy connection after send packet */
1654 conn_set_state(conn, conn_state_destroy);
1655
1656 return 0;
1657 }
1658
1659 static int _ladder_is_integer(char * test)
1660 {
1661 for (char const* ptr = test; *ptr; ++ptr) {
1662 if (!std::isdigit(*ptr)) {
1663 return 0; /* Is not integer */
1664 }
1665 else
1666 return 1; /* Is integer */
1667 }
1668
1669 return -1; /* We shouldn't get here */
1670 }
1671
1672 static int _handle_listsearch_command(t_connection * conn, int numparams, char ** params, char * text)
1673 {
1674 char ** e;
1675 int i = 0;
1676 unsigned rank = 0;
1677 unsigned points = 0;
1678 unsigned wins = 0;
1679 unsigned losses = 0;
1680 unsigned disconnects = 0;
1681 char temp[MAX_IRC_MESSAGE_LEN];
1682 char data[MAX_IRC_MESSAGE_LEN];
1683 t_account * cl_account;
1684 t_clienttag cl_tag;
1685 t_ladder_id id = ladder_id_solo;
1686
1687 std::memset(data, 0, sizeof(data));
1688
1689 if ((numparams >= 1) && (params[0]) && (text)) {
1690 cl_tag = tag_sku_to_uint(std::atoi(params[0]));
1691
1692 if (e = irc_get_ladderelems(text))
1693 {
1694 //TIMESTAMP 1147130452
1695 //TOTAL 12033
1696 //NOTFOUND
1697 // TIMESTAMP 1188740860
1698 // 'TOTAL 27466
1699 /* std::sprintf(temp,"TIMESTAMP %lu\n", std::time(NULL));
1700 std::strcat(data,temp);
1701 std::sprintf(temp,"TOTAL 88\n");
1702 std::strcat(data,temp);*/
1703
1704 for (i = 0; e[i]; i++) {
1705 /* Now we have in e[i] names */
1706 if (e[i] && (std::strcmp(e[i], ":") != 0)) {
1707 cl_account = accountlist_find_account(e[i]);
1708 if (cl_account && cl_tag && (rank = account_get_ladder_rank(cl_account, cl_tag, id))) {
1709 points = account_get_ladder_points(cl_account, cl_tag, id);
1710 wins = account_get_ladder_wins(cl_account, cl_tag, id);
1711 losses = account_get_ladder_losses(cl_account, cl_tag, id);
1712 disconnects = account_get_ladder_disconnects(cl_account, cl_tag, id);
1713 std::sprintf(temp, "%u %s %u %u %u 0 %u\r\n", rank, e[i], points, wins, losses, disconnects);
1714 std::strcat(data, temp);
1715 }
1716 else
1717 std::strcat(data, "NOTFOUND\r\n");
1718 }
1719 }
1720 irc_unget_ladderelems(e);
1721
1722 _ladder_send(conn, data);
1723 }
1724 }
1725 else {
1726 WARN0("Not enough parameters");
1727 conn_set_state(conn, conn_state_destroy);
1728 return 0;
1729 }
1730 return 0;
1731 }
1732
1733 static int _handle_rungsearch_command(t_connection * conn, int numparams, char ** params, char * text)
1734 {
1735 char temp[MAX_IRC_MESSAGE_LEN];
1736 char data[MAX_IRC_MESSAGE_LEN];
1737 unsigned rank = 0;
1738 unsigned points = 0;
1739 unsigned wins = 0;
1740 unsigned losses = 0;
1741 unsigned disconnects = 0;
1742 t_account * cl_account;
1743 t_clienttag cl_tag;
1744 t_ladder_id id = ladder_id_solo;
1745
1746 std::memset(data, 0, sizeof(data));
1747
1748 if ((numparams >= 4) && (params[0]) && (params[1]) && (params[3])) {
1749 cl_tag = tag_sku_to_uint(std::atoi(params[3]));
1750
1751 if ((cl_tag != CLIENTTAG_TIBERNSUN_UINT) && (cl_tag != CLIENTTAG_TIBSUNXP_UINT)
1752 && (cl_tag != CLIENTTAG_REDALERT2_UINT) && (cl_tag != CLIENTTAG_YURISREV_UINT)) {
1753 // PELISH: We are not supporting ladders for all WOL clients yet
1754 std::strcat(data, "\r\n");
1755 _ladder_send(conn, data);
1756 DEBUG1("Wants rung search for SKU {}", params[3]);
1757 return 0;
1758 }
1759
1760 if (_ladder_is_integer(params[0]) == 0) {
1761 /* rungsearch want to line for one player (nick is in params[0]) */
1762 cl_account = accountlist_find_account(params[0]);
1763 if (cl_account && cl_tag && (rank = account_get_ladder_rank(cl_account, cl_tag, id))) {
1764 points = account_get_ladder_points(cl_account, cl_tag, id);
1765 wins = account_get_ladder_wins(cl_account, cl_tag, id);
1766 losses = account_get_ladder_losses(cl_account, cl_tag, id);
1767 disconnects = account_get_ladder_disconnects(cl_account, cl_tag, id);
1768 std::sprintf(temp, "%u %s %u %u %u 0 %u\r\n", rank, params[0], points, wins, losses, disconnects);
1769 }
1770 else
1771 std::sprintf(temp, "\r\n");
1772 _ladder_send(conn, temp);
1773 }
1774 else {
1775 /* Standard RUNG search */
1776 unsigned start = std::atoi(params[0]);
1777 unsigned count = std::atoi(params[1]);
1778
1779 eventlog(eventlog_level_debug, __FUNCTION__, "Start({}) Count({})", start, count);
1780
1781 LadderList* ladderList = NULL;
1782
1783 ladderList = ladders.getLadderList(LadderKey(id, cl_tag, ladder_sort_default, ladder_time_default));
1784 for (unsigned int i = start; i < start + count; i++) {
1785 const LadderReferencedObject* referencedObject = NULL;
1786 cl_account = NULL;
1787 if (((referencedObject = ladderList->getReferencedObject(i))) && (cl_account = referencedObject->getAccount())) {
1788 rank = account_get_ladder_rank(cl_account, cl_tag, id);
1789 points = account_get_ladder_points(cl_account, cl_tag, id);
1790 wins = account_get_ladder_wins(cl_account, cl_tag, id);
1791 losses = account_get_ladder_losses(cl_account, cl_tag, id);
1792 disconnects = account_get_ladder_disconnects(cl_account, cl_tag, id);
1793 std::sprintf(temp, "%u %s %u %u %u 0 %u\r\n", rank, account_get_name(cl_account), points, wins, losses, disconnects);
1794 std::strcat(data, temp);
1795 }
1796 else {
1797 std::strcat(data, "\r\n");
1798 _ladder_send(conn, data);
1799 return 0;
1800 }
1801 }
1802 _ladder_send(conn, data);
1803 }
1804 }
1805 else {
1806 WARN0("Not enough parameters");
1807 conn_set_state(conn, conn_state_destroy);
1808 return 0;
1809 }
1810 return 0;
1811 }
1812
1813 static int _handle_highscore_command(t_connection * conn, int numparams, char ** params, char * text)
1814 {
1815 /* char ** e;
1816 int i = 0;
1817 unsigned rank = 2;
1818 unsigned points = 258;
1819 unsigned wins = 0;
1820 unsigned losses = 0;
1821 unsigned unknown = 0; // Here is nuber before Nick and Honor Badges in Yuri (1-999)
1822 unsigned disconnects = 0;
1823 char temp[MAX_IRC_MESSAGE_LEN];
1824 char data[MAX_IRC_MESSAGE_LEN];
1825 t_account * cl_account;
1826 t_clienttag cltag;
1827
1828 std::memset(temp,0,sizeof(temp));
1829 std::memset(data,0,sizeof(data));
1830
1831 if (text)
1832 e = irc_get_ladderelems(text);
1833
1834 if (params[0])
1835 cltag = tag_sku_to_uint(std::atoi(params[0]));
1836
1837 for (i=0;e[i];i++) {
1838 if (e[i] && (std::strcmp(e[i], ":") != 0)) {
1839 cl_account = accountlist_find_account(e[i]);
1840 if (cl_account) {
1841 wins = account_get_normal_wins(cl_account, cltag);
1842 losses = account_get_normal_losses(cl_account, cltag);
1843 disconnects = account_get_normal_disconnects(cl_account, cltag);
1844 std::sprintf(temp,"%u %s %u %u %u %u %u\r\n",rank+i,e[i],points,wins,losses,unknown,disconnects);
1845 std::strcat(data,temp);
1846 }
1847 else
1848 std::strcat(data,"NOTFOUND\r\n");
1849 }
1850 }
1851
1852 if (e)
1853 irc_unget_ladderelems(e);
1854
1855 _ladder_send(conn,data);
1856 */
1857 conn_set_state(conn, conn_state_destroy);
1858 return 0;
1859 }
1860
1861 }
1862
1863 }