forked from d1zzy/pvpgn
-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathhandle_wol.cpp
More file actions
1860 lines (1583 loc) · 58.9 KB
/
handle_wol.cpp
File metadata and controls
1860 lines (1583 loc) · 58.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2001 Marco Ziech (mmz@gmx.net)
* Copyright (C) 2005 Bryan Biedenkapp (gatekeep@gmail.com)
* Copyright (C) 2006,2007,2008 Pelish (pelish@gmail.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "common/setup_before.h"
#include "handle_wol.h"
#include <cctype>
#include <cinttypes>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include "compat/strcasecmp.h"
#include "common/irc_protocol.h"
#include "common/eventlog.h"
#include "common/bnethash.h"
#include "common/tag.h"
#include "common/util.h"
#include "common/list.h"
#include "common/addr.h"
#include "common/trans.h"
#include "common/xstring.h"
#include "common/packet.h"
#include "prefs.h"
#include "command.h"
#include "irc.h"
#include "account.h"
#include "account_wrap.h"
#include "command_groups.h"
#include "channel.h"
#include "message.h"
#include "tick.h"
#include "topic.h"
#include "server.h"
#include "friends.h"
#include "clan.h"
#include "game.h"
#include "anongame_wol.h"
#include "common/setup_after.h"
namespace pvpgn
{
namespace bnetd
{
typedef int(*t_wol_command)(t_connection * conn, int numparams, char ** params, char * text);
typedef struct {
const char * wol_command_string;
t_wol_command wol_command_handler;
} t_wol_command_table_row;
static int _handle_user_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_pass_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_privmsg_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_quit_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_list_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_names_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_part_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_cvers_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_verchk_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_apgar_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_setopt_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_serial_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_squadinfo_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_clanbyname_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_setcodepage_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_getcodepage_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_setlocale_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_getlocale_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_getinsider_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_joingame_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_gameopt_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_finduser_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_finduserex_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_page_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_startg_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_advertr_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_advertc_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_chanchk_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_getbuddy_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_addbuddy_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_delbuddy_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_host_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_invmsg_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_invdel_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_userip_command(t_connection * conn, int numparams, char ** params, char * text);
/* Ladder server commands (we will probalby move this commands to any another handle file */
static int _handle_listsearch_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_rungsearch_command(t_connection * conn, int numparams, char ** params, char * text);
static int _handle_highscore_command(t_connection * conn, int numparams, char ** params, char * text);
/* state "connected" handlers */
static const t_wol_command_table_row wol_con_command_table[] =
{
{ "NICK", _handle_nick_command },
{ "USER", _handle_user_command },
{ "PING", _handle_ping_command },
{ "PONG", _handle_pong_command },
{ "PASS", _handle_pass_command },
{ "PRIVMSG", _handle_privmsg_command },
{ "QUIT", _handle_quit_command },
{ "CVERS", _handle_cvers_command },
{ "VERCHK", _handle_verchk_command },
{ "APGAR", _handle_apgar_command },
{ "SETOPT", _handle_setopt_command },
{ "SERIAL", _handle_serial_command },
/* Ladder server commands */
{ "LISTSEARCH", _handle_listsearch_command },
{ "RUNGSEARCH", _handle_rungsearch_command },
{ "HIGHSCORE", _handle_highscore_command },
{ NULL, NULL }
};
/* state "logged in" handlers */
static const t_wol_command_table_row wol_log_command_table[] =
{
{ "LIST", _handle_list_command },
{ "TOPIC", _handle_topic_command },
{ "JOIN", _handle_join_command },
{ "NAMES", _handle_names_command },
{ "PART", _handle_part_command },
{ "SQUADINFO", _handle_squadinfo_command },
{ "CLANBYNAME", _handle_clanbyname_command },
{ "SETCODEPAGE", _handle_setcodepage_command },
{ "SETLOCALE", _handle_setlocale_command },
{ "GETCODEPAGE", _handle_getcodepage_command },
{ "GETLOCALE", _handle_getlocale_command },
{ "GETINSIDER", _handle_getinsider_command },
{ "JOINGAME", _handle_joingame_command },
{ "GAMEOPT", _handle_gameopt_command },
{ "FINDUSER", _handle_finduser_command },
{ "FINDUSEREX", _handle_finduserex_command },
{ "PAGE", _handle_page_command },
{ "STARTG", _handle_startg_command },
{ "ADVERTR", _handle_advertr_command },
{ "ADVERTC", _handle_advertc_command },
{ "CHANCHK", _handle_chanchk_command },
{ "GETBUDDY", _handle_getbuddy_command },
{ "ADDBUDDY", _handle_addbuddy_command },
{ "DELBUDDY", _handle_delbuddy_command },
{ "TIME", _handle_time_command },
{ "KICK", _handle_kick_command },
{ "MODE", _handle_mode_command },
{ "HOST", _handle_host_command },
{ "INVMSG", _handle_invmsg_command },
{ "INVDEL", _handle_invdel_command },
{ "USERIP", _handle_userip_command },
{ NULL, NULL }
};
extern int handle_wol_con_command(t_connection * conn, char const * command, int numparams, char ** params, char * text)
{
t_wol_command_table_row const *p;
for (p = wol_con_command_table; p->wol_command_string != NULL; p++) {
if (strcasecmp(command, p->wol_command_string) == 0) {
if (p->wol_command_handler != NULL)
return ((p->wol_command_handler)(conn, numparams, params, text));
}
}
return -1;
}
extern int handle_wol_log_command(t_connection * conn, char const * command, int numparams, char ** params, char * text)
{
t_wol_command_table_row const *p;
for (p = wol_log_command_table; p->wol_command_string != NULL; p++) {
if (strcasecmp(command, p->wol_command_string) == 0) {
if (p->wol_command_handler != NULL)
return ((p->wol_command_handler)(conn, numparams, params, text));
}
}
return -1;
}
static int handle_wol_authenticate(t_connection * conn, char const * passhash)
{
t_account * a;
char const * tempapgar;
char const * temphash;
char const * username;
if (!conn) {
ERROR0("got NULL connection");
return 0;
}
if (!passhash) {
eventlog(eventlog_level_error, __FUNCTION__, "got NULL passhash");
return 0;
}
username = conn_get_loggeduser(conn);
if (!username) {
/* redundant sanity check */
eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn->protocol.loggeduser");
return 0;
}
a = accountlist_find_account(username);
if (!a) {
/* FIXME: Send real error code */
message_send_text(conn, message_type_notice, NULL, "Authentication failed.");
return 0;
}
tempapgar = conn_wol_get_apgar(conn);
temphash = account_get_wol_apgar(a);
if (connlist_find_connection_by_account(a) && prefs_get_kick_old_login() == 0)
{
irc_send(conn, ERR_NICKNAMEINUSE, std::string(std::string(conn_get_loggeduser(conn)) + " :Account is already in use!").c_str());
}
else if (account_get_auth_lock(a) == 1)
{
/* FIXME: Send real error code */
message_send_text(conn, message_type_notice, NULL, "Authentication rejected (account is locked) ");
}
else {
if (!temphash) {
/* Account auto creating */
account_set_wol_apgar(a, tempapgar);
temphash = account_get_wol_apgar(a);
}
if ((tempapgar) && (temphash) && (std::strcmp(temphash, tempapgar) == 0)) {
/* LOGIN is OK. We sends motd */
conn_login(conn, a, username);
conn_set_state(conn, conn_state_loggedin);
irc_send_motd(conn);
}
else {
irc_send(conn, RPL_BAD_LOGIN, ":You have specified an invalid password for that nickname."); /* bad APGAR */
conn_increment_passfail_count(conn);
//std::sprintf(temp,":Closing Link %s[Some.host]:(Password needed for that nickname.)",conn_get_loggeduser(conn));
//message_send_text(conn,message_type_error,conn,temp);
}
}
return 0;
}
extern int handle_wol_welcome(t_connection * conn)
{
/* This function need rewrite */
conn_set_state(conn, conn_state_bot_password);
if (conn_wol_get_apgar(conn)) {
handle_wol_authenticate(conn, conn_wol_get_apgar(conn));
}
else {
message_send_text(conn, message_type_notice, NULL, "No APGAR command received!");
}
return 0;
}
static int handle_wol_send_claninfo(t_connection * conn, t_clan * clan)
{
unsigned int clanid;
const char * clantag;
const char * clanname;
if (!conn)
{
ERROR0("got NULL connection");
return -1;
}
if (clan)
{
clanid = clan_get_clanid(clan);
clantag = clantag_to_str(clan_get_clantag(clan));
clanname = clan_get_name(clan);
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());
}
else
{
irc_send(conn, ERR_IDNOEXIST, ":ID does not exist");
}
return 0;
}
/* Commands: */
static int _handle_user_command(t_connection * conn, int numparams, char ** params, char * text)
{
/**
* In WOL isnt used USER command (only for backward compatibility)
* RFC 2812 says:
* USER <user> <mode> <unused> :<realname>
*
* There is WOL imput expected:
* USER UserName HostName irc.westwood.com :RealName
*/
char * user = NULL;
t_account * a;
user = (char *)conn_get_loggeduser(conn);
if (conn_get_user(conn)) {
/* FIXME: Send real ERROR code/message */
irc_send(conn, ERR_ALREADYREGISTRED, ":You are already registred");
}
else {
eventlog(eventlog_level_debug, __FUNCTION__, "[{}][** WOL **] got USER: user=\"{}\"", conn_get_socket(conn), user);
a = accountlist_find_account(user);
if (!a) {
/* Auto-create account */
t_account * tempacct;
t_hash pass_hash;
char * pass = xstrdup(conn_wol_get_apgar(conn)); /* FIXME: Do not use bnet passhash when we have wol passhash */
strtolower(pass);
bnet_hash(&pass_hash, std::strlen(pass), pass);
xfree((void *)pass);
tempacct = accountlist_create_account(user, hash_get_str(pass_hash));
if (!tempacct) {
/* FIXME: Send real ERROR code/message */
irc_send(conn, RPL_BAD_LOGIN, ":Account creating failed");
return 0;
}
conn_set_user(conn, user);
conn_set_owner(conn, user);
if (conn_get_loggeduser(conn))
handle_wol_welcome(conn); /* only send the welcome if we have USER and NICK */
}
else {
conn_set_user(conn, user);
conn_set_owner(conn, user);
if (conn_get_loggeduser(conn))
handle_wol_welcome(conn); /* only send the welcome if we have USER and NICK */
}
}
return 0;
}
static int _handle_pass_command(t_connection * conn, int numparams, char ** params, char * text)
{
/**
* PASS is not used in WOL
* only for backward compatibility sent client PASS supersecret
* real password sent client by apgar command
*/
return 0;
}
static int _handle_privmsg_command(t_connection * conn, int numparams, char ** params, char * text)
{
if ((numparams >= 1) && (text))
{
int i;
char ** e;
e = irc_get_listelems(params[0]);
/* FIXME: support wildcards! */
/* start amadeo: code was sent by some unkown fellow of pvpgn (maybe u wanna give us your name
for any credits), it adds nick-registration, i changed some things here and there... */
for (i = 0; ((e) && (e[i])); i++) {
if (strcasecmp(e[i], "matchbot") == 0) {
/* Anongames WOL support */
anongame_wol_privmsg(conn, numparams, params, text);
}
else if (conn_get_state(conn) == conn_state_loggedin) {
if (e[i][0] == '#') {
/* channel message */
t_channel * channel;
//PELISH: We does not support talk for not inside-channel clients now but in WOL is that feature not needed
if (channel = conn_get_channel(conn)) {
if ((std::strlen(text) >= 9) && (std::strncmp(text, "\001ACTION ", 8) == 0) && (text[std::strlen(text) - 1] == '\001')) {
/* at least "\001ACTION \001" */
/* it's a CTCP ACTION message */
text = text + 8;
text[std::strlen(text) - 1] = '\0';
channel_message_send(channel, message_type_emote, conn, text);
}
else {
if (text[0] == '/') {
/* "/" commands (like "/help..." */
handle_command(conn, text);
}
else {
channel_message_log(channel, conn, 1, text);
channel_message_send(channel, message_type_talk, conn, text);
}
}
}
else {
irc_send(conn, ERR_NOSUCHCHANNEL, std::string(std::string(e[0]) + " :No such channel").c_str());
}
}
else {
/* whisper */
t_connection * user;
if ((user = connlist_find_connection_by_accountname(e[i])))
{
message_send_text(user, message_type_whisper, conn, text);
}
else
{
irc_send(conn, ERR_NOSUCHNICK, ":No such user");
}
}
}
}
if (e)
irc_unget_listelems(e);
}
else
irc_send(conn, ERR_NEEDMOREPARAMS, "PRIVMSG :Not enough parameters");
return 0;
}
struct gamelist_data {
unsigned tcount, counter;
t_connection *conn;
};
static int append_game_info(t_game* game, void* vdata)
{
char temp[MAX_IRC_MESSAGE_LEN];
char temp_a[MAX_IRC_MESSAGE_LEN];
gamelist_data* data = static_cast<gamelist_data*>(vdata);
t_channel * gamechannel;
const char * gamename;
std::string topicstr;
std::memset(temp, 0, sizeof(temp));
std::memset(temp_a, 0, sizeof(temp_a));
data->tcount++;
if (game_get_status(game) != game_status_open) {
eventlog(eventlog_level_debug, __FUNCTION__, "[{}] not listing because game is not open", conn_get_socket(data->conn));
return 0;
}
if (game_get_clienttag(game) != conn_get_clienttag(data->conn)) {
eventlog(eventlog_level_debug, __FUNCTION__, "[{}] not listing because game is for a different client", conn_get_socket(data->conn));
return 0;
}
if (!(gamechannel = game_get_channel(game))) {
ERROR0("game have no channel");
return 0;
}
if (!(gamename = irc_convert_channel(gamechannel, data->conn))) {
ERROR0("game have no name");
return 0;
}
class_topic Topic;
topicstr = Topic.get(channel_get_name(gamechannel));
if (topicstr.empty() == false) {
if (std::strlen(gamename) + 1 + 20 + 1 + 1 + std::strlen(topicstr.c_str()) > MAX_IRC_MESSAGE_LEN) {
WARN0("LISTREPLY length exceeded");
return 0;
}
}
else {
if (std::strlen(gamename) + 1 + 20 + 1 + 1 > MAX_IRC_MESSAGE_LEN) {
WARN0("LISTREPLY length exceeded");
return 0;
}
}
/***
* WOLv1:
* : 326 u #nick's_game 1 0 2 0 0 1122334455 128::
* WOLv2:
* : 326 u #nick's_game 1 0 21 0 16777216 1122334455 128::g040
* : 326 u #nick's_game 1 0 41 1 2048 1122334455 128::g12P25,2097731398,0,0,0,WATERF~3.YRM // anon_game
*/
/**
* The layout of the game list entry is something like this:
* #game_channel_name currentusers maxplayers gameType gameIsTournment gameExtension longIP LOCK::topic
*/
std::strcat(temp, gamename);
std::strcat(temp, " ");
std::snprintf(temp_a, sizeof(temp_a), "%u ", game_get_ref(game)); /* curent players */
std::strcat(temp, temp_a);
std::snprintf(temp_a, sizeof(temp_a), "%u ", game_get_maxplayers(game)); /* max players */
std::strcat(temp, temp_a);
std::snprintf(temp_a, sizeof(temp_a), "%u ", channel_wol_get_game_type(gamechannel)); /* game type */
std::strcat(temp, temp_a);
std::snprintf(temp_a, sizeof(temp_a), "%u ", (game_get_type(game) == game_type_ladder) ? 1 : 0); /* tournament */
std::strcat(temp, temp_a);
std::snprintf(temp_a, sizeof(temp_a), "%s ", channel_wol_get_game_extension(gamechannel)); /* game extension */
std::strcat(temp, temp_a);
std::snprintf(temp_a, sizeof(temp_a), "%u ", conn_get_addr(game_get_owner(game))); /* owner IP - FIXME: address translation here!! */
std::strcat(temp, temp_a);
if (std::strcmp(game_get_pass(game), "") == 0)
std::strcat(temp, "128"); /* game is unloocked 128 == no_pass */
else
std::strcat(temp, "384"); /* game is loocked 384 == pass */
std::strcat(temp, "::");
if (topicstr.c_str()) {
std::snprintf(temp_a, sizeof(temp_a), "%s", topicstr.c_str()); /* topic */
std::strcat(temp, temp_a);
}
data->counter++;
irc_send(data->conn, RPL_GAME_CHANNEL, temp);
return 0;
}
static int _handle_list_command(t_connection * conn, int numparams, char ** params, char * text)
{
char temp[MAX_IRC_MESSAGE_LEN];
t_elem const * curr;
irc_send(conn, RPL_LISTSTART, "Channel :Users Names"); /* backward compatibility */
if ((numparams == 0) || ((numparams == 2) && (params[0]) && (params[1]) && (std::strcmp(params[0], params[1]) != 0))) {
/**
* LIST all chat channels
* Emperor sends as params[0] == -1 if want QuickMatch channels too, 0 if not.
* This sends also NOX but we dunno why.
* DUNE 2000 use params[0] to determine channels by channeltype
*/
LIST_TRAVERSE_CONST(channellist(), curr) {
t_channel const * channel = (const t_channel*)elem_get_data(curr);
char const * tempname;
tempname = irc_convert_channel(channel, conn);
if ((tag_check_wolv1(conn_get_clienttag(conn))) && (std::strlen(tempname) > MAX_WOLV1_CHANNELNAME_LEN))
continue;
/* FIXME: Delete this if games are not in channels */
if ((channel_wol_get_game_type(channel) != 0))
continue;
sprintf(temp, "%s %u ", tempname, channel_get_length(channel));
if (channel_get_flags(channel) & channel_flags_permanent)
std::strcat(temp, "1"); /* Official channel */
else
std::strcat(temp, "0"); /* User channel */
if (tag_check_wolv1(conn_get_clienttag(conn)))
std::strcat(temp, ":"); /* WOLv1 ends by ":" FIXME: Should be an TOPIC after ":"*/
else
std::strcat(temp, " 388"); /* WOLv2 ends by "388" */
irc_send(conn, RPL_CHANNEL, temp);
}
}
/**
* Known channel game types:
* 0 = Westwood Chat channels, 1 = Command & Conquer Win95 channels, 2 = Red Alert Win95 channels,
* 3 = Red Alert Counterstrike channels, 4 = Red Alert Aftermath channels, 5 = CnC Sole Survivor channels,
* 12 = C&C Renegade channels, 14 = Dune 2000 channels, 16 = Nox channels, 18 = Tiberian Sun channels,
* 21 = Red Alert 1 v 3.03 channels, 31 = Emperor: Battle for Dune, 33 = Red Alert 2,
* 37 = Nox Quest channels, 38,39,40 = Quickgame channels, 41 = Yuri's Revenge
*/
if ((numparams == 0) || ((numparams == 2) && (params[0]) && (params[1]) && (std::strcmp(params[0], params[1]) == 0))) {
eventlog(eventlog_level_debug, __FUNCTION__, "[** WOL **] LIST [Game]");
/* list games */
struct gamelist_data data;
data.tcount = 0;
data.counter = 0;
data.conn = conn;
gamelist_traverse(&append_game_info, &data, gamelist_source_none);
DEBUG3("[{}] LIST sent {} of {} games", conn_get_socket(conn), data.counter, data.tcount);
}
irc_send(conn, RPL_LISTEND, ":End of LIST command");
return 0;
}
static int _handle_quit_command(t_connection * conn, int numparams, char ** params, char * text)
{
if (conn_get_channel(conn))
conn_quit_channel(conn, text);
irc_send(conn, RPL_QUIT, ":goodbye");
conn_set_state(conn, conn_state_destroy);
return 0;
}
static int _handle_names_command(t_connection * conn, int numparams, char ** params, char * text)
{
t_channel * channel;
if (numparams >= 1) {
char ** e;
char const * verytemp;
int i;
e = irc_get_listelems(params[0]);
for (i = 0; ((e) && (e[i])); i++) {
verytemp = irc_convert_ircname(e[i]);
if (!verytemp)
continue; /* something is wrong with the name ... */
channel = channellist_find_channel_by_name(verytemp, NULL, NULL);
if ((!channel) && (!(channel = conn_get_channel(conn))))
continue; /* channel doesn't exist */
irc_send_rpl_namreply(conn, channel);
}
if (e)
irc_unget_listelems(e);
}
else if (numparams == 0) {
irc_send_rpl_namreply(conn, NULL);
}
return 0;
}
static int _handle_part_command(t_connection * conn, int numparams, char ** params, char * text)
{
t_game * game;
conn_part_channel(conn);
if ((game = conn_get_game(conn)) && (game_get_status(game) == game_status_open))
conn_set_game(conn, NULL, NULL, NULL, game_type_none, 0);
return 0;
}
/**
* Fallowing commands are only in Westwood Online protocol
*/
static int _handle_cvers_command(t_connection * conn, int numparams, char ** params, char * text)
{
t_clienttag clienttag;
/* Ignore command but set clienttag */
/**
* Heres the imput expected:
* CVERS [oldvernum] [SKU]
*
* SKU is specific number for any WOL client (Tiberian sun, RedAlert 2 etc.)
* This is the best way to set clienttag, because CVERS is the first command which
* client send to server.
*/
if (numparams == 2) {
clienttag = tag_sku_to_uint(std::atoi(params[1]));
if (clienttag != CLIENTTAG_WWOL_UINT)
conn_set_clienttag(conn, clienttag);
}
else
irc_send(conn, ERR_NEEDMOREPARAMS, "CVERS :Not enough parameters");
return 0;
}
static int _handle_verchk_command(t_connection * conn, int numparams, char ** params, char * text)
{
t_clienttag clienttag;
/**
* Heres the imput expected:
* vercheck [SKU] [version]
*
* Heres the output expected:
*
* 1) Update non-existant:
* :[servername] 379 [username] :none none none 1 [SKU] NONREQ
* 2) Update existant:
* :[servername] 379 [username] :none none none [oldversnum] [SKU] REQ
*/
if (numparams == 2)
{
clienttag = tag_sku_to_uint(std::atoi(params[0]));
if (clienttag != CLIENTTAG_WWOL_UINT)
conn_set_clienttag(conn, clienttag);
std::string tmp(":none none none 1 " + std::string(params[0]) + " NONREQ");
eventlog(eventlog_level_debug, __FUNCTION__, "[** WOL **] VERCHK {}", tmp.c_str());
irc_send(conn, RPL_VERCHK_NONREQ, tmp.c_str());
}
else
irc_send(conn, ERR_NEEDMOREPARAMS, "VERCHK :Not enough parameters");
return 0;
}
static int _handle_apgar_command(t_connection * conn, int numparams, char ** params, char * text)
{
char * apgar = NULL;
if ((numparams >= 1) && (params[0])) {
apgar = params[0];
conn_wol_set_apgar(conn, apgar);
}
else
irc_send(conn, ERR_NEEDMOREPARAMS, "APGAR :Not enough parameters");
return 0;
}
static int _handle_serial_command(t_connection * conn, int numparams, char ** params, char * text)
{
// Ignore command
return 0;
}
static int _handle_squadinfo_command(t_connection * conn, int numparams, char ** params, char * text)
{
t_clan * clan;
if ((numparams >= 1) && (params[0])) {
if (std::strcmp(params[0], "0") == 0) {
/* 0 == claninfo for itself */
clan = account_get_clan(conn_get_account(conn));
handle_wol_send_claninfo(conn, clan);
}
else {
/* claninfo for clanid (params[0]) */
clan = clanlist_find_clan_by_clanid(std::atoi(params[0]));
handle_wol_send_claninfo(conn, clan);
}
}
else
irc_send(conn, ERR_NEEDMOREPARAMS, "SQUADINFO :Not enough parameters");
return 0;
}
static int _handle_clanbyname_command(t_connection * conn, int numparams, char ** params, char * text)
{
t_clan * clan;
if ((numparams >= 1) && (params[0])) {
clan = account_get_clan(accountlist_find_account(params[0]));
handle_wol_send_claninfo(conn, clan);
}
else
irc_send(conn, ERR_NEEDMOREPARAMS, "CLANBYNAME :Not enough parameters");
return 0;
}
static int _handle_setopt_command(t_connection * conn, int numparams, char ** params, char * text)
{
char ** elems;
/**
* This is option for enabling/disabling Page and Find user.
*
* Heres the input expected:
* SETOPT 17,32
*
* First parameter: 16 == FindDisabled 17 == FindEnabled
* Second parameter: 32 == PageDisabled 33 == PageEnabled
*/
if ((numparams >= 1) && (params[0])) {
elems = irc_get_listelems(params[0]);
if ((elems) && (elems[0]) && (elems[1])) {
conn_wol_set_findme(conn, ((std::strcmp(elems[0], "17") == 0) ? true : false));
conn_wol_set_pageme(conn, ((std::strcmp(elems[1], "33") == 0) ? true : false));
}
if (elems)
irc_unget_listelems(elems);
}
else
irc_send(conn, ERR_NEEDMOREPARAMS, "SETOPT :Not enough parameters");
return 0;
}
static int _handle_setcodepage_command(t_connection * conn, int numparams, char ** params, char * text)
{
char * codepage = NULL;
if ((numparams >= 1) && (params[0])) {
codepage = params[0];
conn_wol_set_codepage(conn, std::atoi(codepage));
irc_send(conn, RPL_SET_CODEPAGE, codepage);
}
else
irc_send(conn, ERR_NEEDMOREPARAMS, "SETCODEPAGE :Not enough parameters");
return 0;
}
static int _handle_getcodepage_command(t_connection * conn, int numparams, char ** params, char * text)
{
std::string temp;
if ((numparams >= 1) && (params[0]))
{
for (auto i = 0; i < numparams; i++)
{
int codepage = 0;
t_connection * user = connlist_find_connection_by_accountname(params[i]);
if (user)
codepage = conn_wol_get_codepage(user);
temp.append(std::string(params[i]) + "`" + std::to_string(codepage));
if (i < numparams - 1)
temp.append("`");
}
irc_send(conn, RPL_GET_CODEPAGE, temp.c_str());
}
else
irc_send(conn, ERR_NEEDMOREPARAMS, "GETCODEPAGE :Not enough parameters");
return 0;
}
static int _handle_setlocale_command(t_connection * conn, int numparams, char ** params, char * text)
{
t_account * account = conn_get_account(conn);
int locale;
if ((numparams >= 1) && (params[0])) {
locale = std::atoi(params[0]);
account_set_locale(account, locale);
irc_send(conn, RPL_SET_LOCALE, params[0]);
}
else
irc_send(conn, ERR_NEEDMOREPARAMS, "SETLOCALE :Not enough parameters");
return 0;
}
static int _handle_getlocale_command(t_connection * conn, int numparams, char ** params, char * text)
{
std::string temp;
if ((numparams >= 1) && (params[0]))
{
for (auto i = 0; i < numparams; i++)
{
int locale = 0;
t_account * account = accountlist_find_account(params[i]);
if (account)
locale = account_get_locale(account);
temp.append(std::string(params[i]) + "`" + std::to_string(locale));
if (i < numparams - 1)
temp.append("`");
}
irc_send(conn, RPL_GET_LOCALE, temp.c_str());
}
else
irc_send(conn, ERR_NEEDMOREPARAMS, "GETLOCALE :Not enough parameters");
return 0;
}
static int _handle_getinsider_command(t_connection * conn, int numparams, char ** params, char * text)
{
/**
* Here is imput expected:
* GETINSIDER [nickname]
* Here is output expected:
* :[servername] 399 [nick] [nickname]`0
*/
if ((numparams >= 1) && (params[0]))
irc_send(conn, RPL_GET_INSIDER, std::string(std::string(params[0]) + "`0").c_str());
else
irc_send(conn, ERR_NEEDMOREPARAMS, "GETINSIDER :Not enough parameters");
return 0;
}
static int _handle_joingame_command(t_connection * conn, int numparams, char ** params, char * text)
{
char _temp[MAX_IRC_MESSAGE_LEN];
std::memset(_temp, 0, sizeof(_temp));
/**
* Basically this has 2 modes, Join Game and Create Game output is pretty much
* the same...input and output of JOINGAME is listed below. By the way, there is a
* hack in here, for Red Alert 1, it use's JOINGAME for some reason to join a lobby channel.
*
* Here is WOLv1 input expected:
* JOINGAME [#Game_channel_name] [MinPlayers] [MaxPlayers] [channelType] 1 1 [gameIsTournament]
* Knowed channelTypes (0-chat, 1-cnc, 2-ra1, 3-racs, 4-raam, 5-solsurv... listed in tag.cpp)
*
* Here is WOLv2 input expected:
* JOINGAME [#Game_channel_name] [MinPlayers] [MaxPlayers] [channelType] unknown unknown [gameIsTournament] [gameExtension] [password_optional]
*
* Heres the output expected:
* user!WWOL@hostname JOINGAME [MinPlayers] [MaxPlayers] [channelType] unknown clanID [longIP] [gameIsTournament] :[#Game_channel_name]
*/
if ((numparams == 2) || (numparams == 3)) {
char ** e;
e = irc_get_listelems(params[0]);
if ((e) && (e[0])) {
char const * gamename = irc_convert_ircname(e[0]);
// char * old_channel_name = NULL;
t_game * game;
t_game_type gametype;
t_channel * channel;
t_channel * old_channel = conn_get_channel(conn);
char gamepass[MAX_GAMEPASS_LEN];
std::memset(gamepass, 0, sizeof(gamepass));
if ((conn_get_clienttag(conn) == CLIENTTAG_REDALERT_UINT)
&& (channellist_find_channel_by_name(gamename, NULL, NULL) != NULL)
&& (gamelist_find_game_available(gamename, conn_get_clienttag(conn), game_type_all) == NULL)) {
/* BUG in Red Alert 1 v3.03e - forwarding to _handle_join_command */
DEBUG0("BUG in RA1 v3.03e - forwarding to _handle_join_command");
_handle_join_command(conn, numparams, params, text);
if (e)
irc_unget_listelems(e);
return 0;
}
if (!(gamename) || !(game = gamelist_find_game_available(gamename, conn_get_clienttag(conn), game_type_all)))
{
irc_send(conn, ERR_GAMEHASCLOSED, std::string(std::string(e[0]) + " :Game channel has closed").c_str());
if (e)
irc_unget_listelems(e);
return 0;
}
channel = game_get_channel(game);
if (game_get_ref(game) == game_get_maxplayers(game))
{
irc_send(conn, ERR_CHANNELISFULL, std::string(std::string(e[0]) + " :Channel is full.").c_str());
if (e)
irc_unget_listelems(e);
return 0;
}
if (channel_check_banning(channel, conn))
{
irc_send(conn, ERR_BANNEDFROMCHAN, std::string(std::string(e[0]) + " :You are banned from that channel.").c_str());
if (e)
irc_unget_listelems(e);
return 0;
}
if (std::strcmp(game_get_pass(game), "") != 0)
{
if ((numparams == 3) && (params[2]) && (std::strcmp(params[2], game_get_pass(game)) == 0))
{
std::strcpy(gamepass, params[2]);
}
else
{
irc_send(conn, ERR_BADCHANNELKEY, std::string(std::string(e[0]) + ":Bad password").c_str());
if (e)
irc_unget_listelems(e);
return 0;