Debian Codegraph @

src/bnetd/anongame_wol.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2008 Pelish ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #define ANONGAME_WOL_INTERNAL_ACCESS
20 #include "common/setup_before.h"
21 #include "anongame_wol.h"
22
23 #include <cstring>
24 #include <cctype>
25 #include <cstdlib>
26
27 #include "compat/strcasecmp.h"
28
29 #include "common/irc_protocol.h"
30 #include "common/packet.h"
31 #include "common/eventlog.h"
32 #include "common/tag.h"
33 #include "common/list.h"
34 #include "common/anongame_protocol.h"
35
36 #include "irc.h"
37 #include "handle_wol.h"
38 #include "connection.h"
39 #include "channel.h"
40 #include "anongame.h"
41 #include "common/setup_after.h"
42
43 namespace pvpgn
44 {
45
46 namespace bnetd
47 {
48
49 typedef int(*t_anong_tag)(t_anongame_wol_player * player, char * param);
50
51 typedef struct {
52 const char * wol_anong_tag_string;
53 t_anong_tag wol_anong_tag_handler;
54 } t_wol_anongame_tag_table_row;
55
56 static t_list * anongame_wol_matchlist_head = NULL;
57
58 static int _handle_address_tag(t_anongame_wol_player * player, char * param);
59 static int _handle_port_tag(t_anongame_wol_player * player, char * param);
60 static int _handle_country_tag(t_anongame_wol_player * player, char * param);
61 static int _handle_colour_tag(t_anongame_wol_player * player, char * param);
62
63 static const t_wol_anongame_tag_table_row t_wol_anongame_tag_table[] =
64 {
65 { MATCHTAG_ADDRESS, _handle_address_tag },
66 { MATCHTAG_PORT, _handle_port_tag },
67 { MATCHTAG_COUNTRY, _handle_country_tag },
68 { MATCHTAG_COLOUR, _handle_colour_tag },
69
70 { NULL, NULL }
71 };
72
73 static int anongame_wol_set_playersetting(t_anongame_wol_player * player, char const * tag, char * param)
74 {
75 t_wol_anongame_tag_table_row const *p;
76
77 for (p = t_wol_anongame_tag_table; p->wol_anong_tag_string != NULL; p++) {
78 if (strcasecmp(tag, p->wol_anong_tag_string) == 0) {
79 if (p->wol_anong_tag_handler != NULL)
80 return ((p->wol_anong_tag_handler)(player, param));
81 }
82 }
83 return -1;
84 }
85
86 /* anongame_wol player functions: */
87
88 static t_anongame_wol_player * anongame_wol_player_create(t_connection * conn)
89 {
90 t_anongame_wol_player * player;
91
92 player = (t_anongame_wol_player*)xmalloc(sizeof(t_anongame_wol_player));
93
94 player->conn = conn;
95
96 /* Used only in Red Alert 2 and Yuri's Revenge */
97 player->address = 0;
98 player->port = 0;
99 player->country = -2; /* Default values are form packet dumps - not prefered country */
100 player->colour = -2; /* Default values are form packet dumps - not prefered colour */
101
102 conn_wol_set_anongame_player(conn, player);
103
104 list_append_data(anongame_wol_matchlist_head, player);
105
106 DEBUG1("[** WOL **] annongame player created: {}", conn_get_chatname(conn));
107
108 return player;
109 }
110
111 static int anongame_wol_player_destroy(t_anongame_wol_player * player, t_elem ** curr)
112 {
113 if (list_remove_data(anongame_wol_matchlist_head, player, curr) < 0){
114 ERROR0("could not remove item from list");
115 return -1;
116 }
117
118 DEBUG0("[** WOL **] destroying annongame player");
119
120 xfree(player);
121
122 return 0;
123 }
124
125 static t_connection * anongame_wol_player_get_conn(t_anongame_wol_player const * player)
126 {
127 if (!player) {
128 ERROR0("got NULL player");
129 return NULL;
130 }
131
132 return player->conn;
133 }
134
135
136 static int anongame_wol_player_get_address(t_anongame_wol_player const * player)
137 {
138 if (!player) {
139 ERROR0("got NULL player");
140 return -1;
141 }
142
143 return player->address;
144 }
145
146 static int _handle_address_tag(t_anongame_wol_player * player, char * param)
147 {
148 if (!player) {
149 ERROR0("got NULL player");
150 return -1;
151 }
152
153 if (param)
154 player->address = std::atoi(param);
155
156 return 0;
157 }
158
159 static int anongame_wol_player_get_port(t_anongame_wol_player const * player)
160 {
161 if (!player) {
162 ERROR0("got NULL player");
163 return -1;
164 }
165
166 return player->port;
167 }
168
169 static int _handle_port_tag(t_anongame_wol_player * player, char * param)
170 {
171 if (!player) {
172 ERROR0("got NULL player");
173 return -1;
174 }
175
176 if (param)
177 player->port = std::atoi(param);
178
179 return 0;
180 }
181
182 static int anongame_wol_player_get_country(t_anongame_wol_player const * player)
183 {
184 if (!player) {
185 ERROR0("got NULL player");
186 return -1;
187 }
188
189 return player->country;
190 }
191
192 static int _handle_country_tag(t_anongame_wol_player * player, char * param)
193 {
194 if (!player) {
195 ERROR0("got NULL player");
196 return -1;
197 }
198
199 if (param)
200 player->country = std::atoi(param);
201
202 return 0;
203 }
204
205 static int anongame_wol_player_get_colour(t_anongame_wol_player const * player)
206 {
207 if (!player) {
208 ERROR0("got NULL player");
209 return -1;
210 }
211
212 return player->colour;
213 }
214
215 static int _handle_colour_tag(t_anongame_wol_player * player, char * param)
216 {
217 if (!player) {
218 ERROR0("got NULL player");
219 return -1;
220 }
221
222 if (param)
223 player->colour = std::atoi(param);
224
225 return 0;
226 }
227
228 /* Matchlist functions:*/
229
230 extern int anongame_wol_matchlist_create(void)
231 {
232 anongame_wol_matchlist_head = list_create();
233
234 return 0;
235 }
236
237 extern int anongame_wol_matchlist_destroy(void)
238 {
239 t_anongame_wol_player * player;
240 t_elem * curr;
241
242 if (anongame_wol_matchlist_head) {
243 LIST_TRAVERSE(anongame_wol_matchlist_head, curr) {
244 if (!(player = (t_anongame_wol_player*)elem_get_data(curr))) { /* should not happen */
245 ERROR0("wol_matchlist contains NULL item");
246 continue;
247 }
248 anongame_wol_player_destroy(player, &curr);
249 }
250
251 if (list_destroy(anongame_wol_matchlist_head) < 0)
252 return -1;
253 anongame_wol_matchlist_head = NULL;
254 }
255
256 return 0;
257 }
258
259 extern t_list * anongame_wol_matchlist(void)
260 {
261 return anongame_wol_matchlist_head;
262 }
263
264 static t_anongame_wol_player * anongame_wol_matchlist_find_player_by_conn(t_connection * conn)
265 {
266 t_elem * curr;
267
268 if (!conn) {
269 ERROR0("got NULL conn");
270 return NULL;
271 }
272
273 LIST_TRAVERSE(anongame_wol_matchlist(), curr) {
274 t_anongame_wol_player * player = (t_anongame_wol_player *)elem_get_data(curr);
275 if (conn == anongame_wol_player_get_conn(player)) {
276 return player;
277 }
278 }
279
280 return NULL;
281 }
282
283 static int anongame_wol_matchlist_get_length(void)
284 {
285 return list_get_length(anongame_wol_matchlist_head);
286 }
287
288 /* support functions */
289
290 static int _send_msg(t_connection * conn, char const * command, char const * text)
291 {
292 if (!conn)
293 {
294 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
295 return -1;
296 }
297 if (!command)
298 {
299 eventlog(eventlog_level_error, __FUNCTION__, "got NULL command");
300 return -1;
301 }
302 if (!text)
303 {
304 eventlog(eventlog_level_error, __FUNCTION__, "got NULL text");
305 return -1;
306 }
307
308 t_packet * p = packet_create(packet_class_raw);
309 if (!p)
310 {
311 eventlog(eventlog_level_error, __FUNCTION__, "could not create packet");
312 return -1;
313 }
314
315 const char *nick = conn_get_loggeduser(conn);
316 if (!nick)
317 nick = "UserName";
318
319 std::string data(":matchbot!u@h " + std::string(command) + " " + std::string(nick) + " " + std::string(text));
320 data.erase(MAX_IRC_MESSAGE_LEN, std::string::npos);
321
322 DEBUG2("[{}] sent \"{}\"", conn_get_socket(conn), data.c_str());
323 data.append("\r\n");
324 packet_set_size(p, 0);
325 packet_append_data(p, data.c_str(), data.length());
326 conn_push_outqueue(conn, p);
327 packet_del_ref(p);
328 return 0;
329 }
330
331 static int _get_pair(int * i, int * j, int max, bool different)
332 {
333 max++; /* We want to use just real maximum number - no metter what function do */
334
335 if (*i == -2)
336 *i = (max * rand() / (RAND_MAX + 1));
337
338 if (*j == -2)
339 *j = (max * rand() / (RAND_MAX + 1));
340
341 if ((different) && (*i == *j)) {
342 do {
343 *j = (max * rand() / (RAND_MAX + 1));
344 } while (*i == *j);
345 }
346 return 0;
347 }
348
349
350 static int anongame_wol_trystart(t_anongame_wol_player const * player1)
351 {
352 t_elem * curr;
353 char temp[MAX_IRC_MESSAGE_LEN];
354 char _temp[MAX_IRC_MESSAGE_LEN];
355 int random;
356
357 t_anongame_wol_player * player2;
358 t_connection * conn_pl1;
359 t_connection * conn_pl2;
360 char const * channelname;
361 const char * mapname;
362 t_clienttag ctag;
363
364 std::memset(temp, 0, sizeof(temp));
365 std::memset(_temp, 0, sizeof(_temp));
366
367 /**
368 * Expected start message is
369 *
370 * Red Alert 2:
371 * :Start [rand_num],0,0,[credits],0,1,[super_weapon],1,1,0,1,x,2,1,[mapsize_kilobytes],[mapname],1:
372 * [nick1_name],[nick1_country],[nick1_colour],[nick1_addr_hex],[nick1_nat],[nick1_prt_hex],
373 * [nick2_name],[nick2_country],[nick2_colour],[nick2_addr_hex],[nick2_nat],[nick2_prt_hex]
374 *
375 * Yuris Revenge Quick game:
376 *
377 * Yuris Revenge Quick Coop:
378 *
379 */
380
381 if (!player1) {
382 ERROR0("got NULL player");
383 return -1;
384 }
385
386 conn_pl1 = anongame_wol_player_get_conn(player1);
387 ctag = conn_get_clienttag(conn_pl1);
388 channelname = channel_get_name(conn_get_channel(conn_pl1));
389
390 LIST_TRAVERSE(anongame_wol_matchlist(), curr) {
391 player2 = (t_anongame_wol_player *)elem_get_data(curr);
392 conn_pl2 = anongame_wol_player_get_conn(player2);
393
394 if ((player1 != player2) && ((conn_get_channel(conn_pl1)) == (conn_get_channel(conn_pl2)))) {
395 switch (ctag) {
396 case CLIENTTAG_REDALERT2_UINT: {
397 random = rand();
398
399 if (std::strcmp(channelname, RAL2_CHANNEL_FFA) == 0) {
400 int pl1_colour = anongame_wol_player_get_colour(player1);
401 int pl1_country = anongame_wol_player_get_country(player1);
402 int pl2_colour = anongame_wol_player_get_colour(player2);
403 int pl2_country = anongame_wol_player_get_country(player2);
404
405 DEBUG0("Generating SOLO game for Red Alert 2");
406
407 _get_pair(&pl1_colour, &pl2_colour, 7, true);
408 _get_pair(&pl1_country, &pl2_country, 8, false);
409 mapname = anongame_get_map_from_prefs(ANONGAME_TYPE_1V1, ctag);
410
411 /* We have madatory of game */
412 std::snprintf(_temp, sizeof(_temp), ":Start %d,0,0,10000,0,1,0,1,1,0,1,x,2,1,165368,%s,1:", random, mapname);
413 std::strcat(temp, _temp);
414
415 /* GameHost informations */
416 std::snprintf(_temp, sizeof(_temp), "%s,%d,%d,%x,1,%x,", conn_get_chatname(conn_pl1), pl1_country, pl1_colour, anongame_wol_player_get_address(player1), anongame_wol_player_get_port(player1));
417 std::strcat(temp, _temp);
418
419 /* GameJoinie informations */
420 std::snprintf(_temp, sizeof(_temp), "%s,%d,%d,%x,1,%x", conn_get_chatname(conn_pl2), pl2_country, pl2_colour, anongame_wol_player_get_address(player2), anongame_wol_player_get_port(player2));
421 std::strcat(temp, _temp);
422
423 _send_msg(conn_pl1, "PRIVMSG", temp);
424 _send_msg(conn_pl2, "PRIVMSG", temp);
425 }
426 else
427 ERROR1("undefined channel type for {} channel", channelname);
428 }
429 return 0;
430 case CLIENTTAG_YURISREV_UINT: {
431 random = rand();
432
433 if (std::strcmp(channelname, YURI_CHANNEL_FFA) == 0) {
434 int pl1_colour = anongame_wol_player_get_colour(player1);
435 int pl1_country = anongame_wol_player_get_country(player1);
436 int pl2_colour = anongame_wol_player_get_colour(player2);
437 int pl2_country = anongame_wol_player_get_country(player2);
438
439 DEBUG0("Generating SOLO game for Yuri's Revenge");
440
441 _get_pair(&pl1_colour, &pl2_colour, 7, true);
442 _get_pair(&pl1_country, &pl2_country, 9, false);
443 mapname = anongame_get_map_from_prefs(ANONGAME_TYPE_1V1, ctag);
444
445 /* We have madatory of game */
446 std::snprintf(_temp, sizeof(_temp), ":Start %d,0,0,10000,0,0,1,1,1,0,3,0,x,2,1,163770,%s,1:", random, mapname);
447 std::strcat(temp, _temp);
448
449 /* GameHost informations */
450 std::snprintf(_temp, sizeof(_temp), "%s,%d,%d,-2,-2,", conn_get_chatname(conn_pl1), pl1_country, pl1_colour);
451 std::strcat(temp, _temp);
452 std::snprintf(_temp, sizeof(_temp), "%x,1,%x,", anongame_wol_player_get_address(player1), anongame_wol_player_get_port(player1));
453 std::strcat(temp, _temp);
454
455 /* GameJoinie informations */
456 std::snprintf(_temp, sizeof(_temp), "%s,%d,%d,-2,-2,", conn_get_chatname(conn_pl2), pl2_country, pl2_colour);
457 std::strcat(temp, _temp);
458 std::snprintf(_temp, sizeof(_temp), "%x,1,%x", anongame_wol_player_get_address(player2), anongame_wol_player_get_port(player2));
459 std::strcat(temp, _temp);
460
461 _send_msg(conn_pl1, "PRIVMSG", temp);
462 _send_msg(conn_pl2, "PRIVMSG", temp);
463 }
464 else if (std::strcmp(channelname, YURI_CHANNEL_COOP) == 0) {
465 DEBUG0("Generating COOP game for Yuri's Revenge");
466
467 /* We have madatory of game */
468 std::snprintf(_temp, sizeof(_temp), ":Start %d,0,0,10000,10,0,1,1,0,1,3,0,x,2,1,163770,C1A01MD.MAP,1:", random);
469 std::strcat(temp, _temp);
470
471 /* GameHost informations */
472 std::snprintf(_temp, sizeof(_temp), "%s,0,4,0,-2,", conn_get_chatname(conn_pl1));
473 std::strcat(temp, _temp);
474 std::snprintf(_temp, sizeof(_temp), "%x,1,%x,", anongame_wol_player_get_address(player1), anongame_wol_player_get_port(player1));
475 std::strcat(temp, _temp);
476
477 /* GameJoinie informations */
478 std::snprintf(_temp, sizeof(_temp), "%s,0,5,1,-2,", conn_get_chatname(anongame_wol_player_get_conn(player2)));
479 std::strcat(temp, _temp);
480 std::snprintf(_temp, sizeof(_temp), "%x,1,%x", anongame_wol_player_get_address(player2), anongame_wol_player_get_port(player2));
481 std::strcat(temp, _temp);
482
483 /* Some computers for coop games */
484 std::snprintf(_temp, sizeof(_temp), ":@:0,-1,-1,-2,-2,0,-1,-1,-2,-2,1,8,1,-2,-2,1,8,2,-2,-2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,");
485 std::strcat(temp, _temp);
486
487 _send_msg(conn_pl1, "PRIVMSG", temp);
488 _send_msg(conn_pl2, "PRIVMSG", temp);
489 }
490 else
491 ERROR1("undefined channel type for {} channel", channelname);
492 }
493 return 0;
494 default:
495 DEBUG0("unsupported client for WOL Matchgame");
496 return 0;
497 }
498 }
499 }
500
501 return 0;
502 }
503
504 static int anongame_wol_tokenize_line(t_connection * conn, char const * text)
505 {
506 t_anongame_wol_player * player;
507 char * command; /* Match, CINFO, SINFO, Pings */
508 char * tag; /* ADR, COL, COU... */
509 char * param; /* value of paramtag */
510 char * line; /* Copy of text */
511 char * temp;
512 char * p;
513
514 if (!conn) {
515 ERROR0("got NULL conn");
516 return -1;
517 }
518
519 if (!text) {
520 ERROR0("got NULL text");
521 return -1;
522 }
523
524 if (!(player = anongame_wol_player_create(conn))) {
525 ERROR0("player was not created");
526 return -1;
527 }
528
529 /**
530 * Here are expected privmsgs:
531 * :user!EMPR@host PRIVMSG matchbot :Match COU=-1,COL=-1,SHA=-1,SHB=-1,LOC=2,RAT=0
532 * :user!RAL2@host PRIVMSG matchbot :Match COU=-2, COL=-2, LOC=1, RAT=3, RES=640, ADR=202680512, NAT=1, PRT=1295
533 * :user!RAL2@host PRIVMSG matchbot :Match COU=-2, COL=-2, LOC=1, RAT=3, RES=640, ADR=202680512, NAT=1, PRT=1340
534 * :user!RAL2@host PRIVMSG matchbot :Match COU=-2, COL=-2, LOC=25, RAT=1, RES=640, ADR=-501962560, NAT=1, PRT=1320
535 * :user!YURI@host PRIVMSG matchbot :Match COU=-2, COL=-2, MBR=1, LOC=5, RAT=2, RES=800, ADR=202680512, NAT=1, PRT=1390
536 * :user!RNGD@host PRIVMSG matchbot :CINFO VER=1329937315 CPU=2981 MEM=1023 TPOINTS=0 PLAYED=0 PINGS=00FFFFFFFFFFFFFF
537 * :user!RNGD@host PRIVMSG matchbot :SINFO 4F453BA3DBAE41CB00000000000000002d# 9Dedicated Renegade Server-C&C_Field.mix07FF0656FFFF1C2D|090000000000
538 * :user!YURI@host PRIVMSG matchbot :Pings nickname,2;
539 */
540
541 line = (char *)xmalloc(std::strlen(text) + 2);
542 strcpy(line, text);
543
544 command = line;
545 if (!(temp = strchr(command, ' '))) {
546 WARN0("got malformed line (missing command)");
547 xfree(line);
548 return -1;
549 }
550 *temp++ = '\0';
551
552 if ((std::strcmp(command, "Match") == 0)) {
553 strcat(temp, ","); /* FIXME: This is DUMB - without that we lost the last tag/param */
554
555 for (p = temp; *p && (*p != '\0'); p++) {
556 if ((*p == ',') || (*p == ' ')) {
557 *p++ = '\0';
558 if (temp[0] == ' ') /* Pelish: Emperor sends line without spaces but RA2/Yuri do */
559 *temp++;
560 tag = temp;
561 param = strchr(tag, '=');
562 if (param)
563 *param++ = '\0';
564 if (anongame_wol_set_playersetting(player, tag, param) == -1)
565 WARN2("[** WOL **] got unknown tag {} param {}", tag, param);
566 temp = p;
567 }
568 }
569 _send_msg(conn, "PRIVMSG", ":Working");
570 anongame_wol_trystart(player);
571 }
572 else {
573 DEBUG1("[** WOL **] got line /{}/", text);
574 }
575
576 if (line)
577 xfree(line);
578
579 return 0;
580 }
581
582 /* Functions for getting/sending player informations */
583
584 extern int anongame_wol_destroy(t_connection * conn)
585 {
586 t_elem *curr;
587
588 /* Player destroying */
589
590 LIST_TRAVERSE(anongame_wol_matchlist_head, curr) {
591 t_anongame_wol_player * player = (t_anongame_wol_player*)elem_get_data(curr);
592
593 if (conn == anongame_wol_player_get_conn(player))
594 anongame_wol_player_destroy(player, &curr);
595 }
596
597 return 0;
598 }
599
600 extern int anongame_wol_privmsg(t_connection * conn, int numparams, char ** params, char * text)
601 {
602
603 if (!conn) {
604 ERROR0("got NULL conn");
605 return -1;
606 }
607
608 anongame_wol_tokenize_line(conn, text);
609
610 return 0;
611 }
612
613 }
614
615 }