-
-
Notifications
You must be signed in to change notification settings - Fork 415
/
CConsoleCommands.cpp
1854 lines (1654 loc) · 70.4 KB
/
CConsoleCommands.cpp
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
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: mods/deathmatch/logic/CConsoleCommands.cpp
* PURPOSE: Server console command definitions class
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#include "StdInc.h"
extern CGame* g_pGame;
// Helper functions
static string GetAdminNameForLog(CClient* pClient)
{
string strName = pClient->GetNick();
string strAccountName = pClient->GetAccount() ? pClient->GetAccount()->GetName() : "no account";
if (strName == strAccountName)
return strName;
return SString("%s(%s)", strName.c_str(), strAccountName.c_str());
}
static void BeginConsoleOutputCapture(CClient* pClient)
{
if (pClient->GetClientType() != CClient::CLIENT_CONSOLE)
CLogger::BeginConsoleOutputCapture();
}
static void EndConsoleOutputCapture(CClient* pClient, const SString& strIfNoOutput = "")
{
if (pClient->GetClientType() != CClient::CLIENT_CONSOLE)
{
std::vector<SString> lines;
CLogger::EndConsoleOutputCapture().Split("\n", lines);
if (lines.size() == 1 && lines[0].empty())
lines[0] = strIfNoOutput;
for (uint i = 0; i < lines.size(); i++)
if (!lines[i].empty())
pClient->SendConsole(lines[i]);
}
}
bool CConsoleCommands::StartResource(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
SString strResponse;
if (szArguments && szArguments[0])
{
CResource* resource = g_pGame->GetResourceManager()->GetResource(szArguments);
if (resource)
{
if (pClient->GetNick())
CLogger::LogPrintf("start: Requested by %s\n", GetAdminNameForLog(pClient).c_str());
if (resource->IsLoaded())
{
if (!resource->IsActive())
{
if (g_pGame->GetResourceManager()->StartResource(resource, NULL, true))
{
strResponse = SString("start: Resource '%s' started", szArguments);
}
else
{
strResponse = SString("start: Resource '%s' start was requested (%s)", szArguments, resource->GetFailureReason().c_str());
}
}
else
strResponse = "start: Resource is already running";
}
else
strResponse = SString("start: Resource is loaded, but has errors (%s)", resource->GetFailureReason().c_str());
}
else
strResponse = "start: Resource could not be found";
}
else
strResponse = "* Syntax: start <resource-name>";
pEchoClient->SendConsole(strResponse);
return true;
}
bool CConsoleCommands::RestartResource(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
if (szArguments && szArguments[0])
{
CResource* resource = g_pGame->GetResourceManager()->GetResource(szArguments);
if (resource)
{
if (pClient->GetNick())
CLogger::LogPrintf("restart: Requested by %s\n", GetAdminNameForLog(pClient).c_str());
if (resource->IsLoaded())
{
if (resource->IsActive())
{
if (resource->IsProtected())
{
if (!g_pGame->GetACLManager()->CanObjectUseRight(pClient->GetNick(), CAccessControlListGroupObject::OBJECT_TYPE_USER,
"restart.protected", CAccessControlListRight::RIGHT_TYPE_COMMAND, false))
{
pEchoClient->SendConsole("restart: Resource could not be restarted as it is protected");
return false;
}
}
g_pGame->GetResourceManager()->QueueResource(resource, CResourceManager::QUEUE_RESTART, NULL);
pEchoClient->SendConsole("restart: Resource restarting...");
}
else
pEchoClient->SendConsole("restart: Resource is not running");
}
else
pEchoClient->SendConsole(SString("restart: Resource is loaded, but has errors (%s)", resource->GetFailureReason().c_str()));
}
else
pEchoClient->SendConsole("restart: Resource could not be found");
return true;
}
else
pEchoClient->SendConsole("* Syntax: restart <resource-name>");
return false;
}
bool CConsoleCommands::RefreshResources(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
BeginConsoleOutputCapture(pEchoClient);
g_pGame->GetResourceManager()->Refresh(false, szArguments);
EndConsoleOutputCapture(pEchoClient, "refresh completed");
return true;
}
bool CConsoleCommands::RefreshAllResources(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
BeginConsoleOutputCapture(pEchoClient);
g_pGame->GetResourceManager()->Refresh(true, szArguments);
EndConsoleOutputCapture(pEchoClient, "refreshall completed");
return true;
}
bool CConsoleCommands::ListResources(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
// To work on remote clients, 'list' needs ACL entry + console capture
if (pClient->GetClientType() != CClient::CLIENT_CONSOLE)
return false;
SString strListType = szArguments;
if (strListType.empty())
strListType = "all";
g_pGame->GetResourceManager()->ListResourcesLoaded(strListType);
return true;
}
bool CConsoleCommands::ResourceInfo(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
// To work on remote clients, 'info' needs ACL entry + console capture
if (pClient->GetClientType() != CClient::CLIENT_CONSOLE)
return false;
if (szArguments && szArguments[0])
{
CResource* resource = g_pGame->GetResourceManager()->GetResource(szArguments);
if (resource)
{
resource->DisplayInfo();
}
else
pEchoClient->SendConsole("info: Resource was not found");
return true;
}
return false;
}
bool CConsoleCommands::StopResource(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
if (szArguments && szArguments[0])
{
CResource* resource = g_pGame->GetResourceManager()->GetResource(szArguments);
if (resource)
{
if (pClient->GetNick())
CLogger::LogPrintf("stop: Requested by %s\n", GetAdminNameForLog(pClient).c_str());
if (resource->IsLoaded())
{
if (resource->IsActive())
{
if (resource->IsProtected())
{
if (!g_pGame->GetACLManager()->CanObjectUseRight(pClient->GetNick(), CAccessControlListGroupObject::OBJECT_TYPE_USER, "stop.protected",
CAccessControlListRight::RIGHT_TYPE_COMMAND, false))
{
pEchoClient->SendConsole("stop: Resource could not be stopped as it is protected");
return false;
}
}
g_pGame->GetResourceManager()->QueueResource(resource, CResourceManager::QUEUE_STOP, NULL);
pEchoClient->SendConsole("stop: Resource stopping");
}
else
pEchoClient->SendConsole("stop: Resource is not running");
}
else
pEchoClient->SendConsole(SString("stop: Resource is loaded, but has errors (%s)", resource->GetFailureReason().c_str()));
}
else
pEchoClient->SendConsole("stop: Resource could not be found");
return true;
}
else
pEchoClient->SendConsole("* Syntax: stop <resource-name>");
return false;
}
bool CConsoleCommands::StopAllResources(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
if (!g_pGame->GetACLManager()->CanObjectUseRight(pClient->GetNick(), CAccessControlListGroupObject::OBJECT_TYPE_USER, "stopall",
CAccessControlListRight::RIGHT_TYPE_COMMAND, false))
{
pEchoClient->SendConsole("stopall: You do not have sufficient rights to stop all the resources.");
return false;
}
g_pGame->GetResourceManager()->QueueResource(NULL, CResourceManager::QUEUE_STOPALL, NULL);
pEchoClient->SendConsole("stopall: Stopping all resources");
return true;
}
bool CConsoleCommands::UpgradeResources(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
// To work on remote clients, 'upgrade' needs ACL entry + console capture
if (pClient->GetClientType() != CClient::CLIENT_CONSOLE)
return false;
if (szArguments && szArguments[0])
{
if (SStringX("all") == szArguments)
{
pEchoClient->SendConsole("Upgrading all resources...");
g_pGame->GetResourceManager()->UpgradeResources();
pEchoClient->SendEcho("Upgrade completed. Refreshing all resources...");
g_pGame->GetResourceManager()->Refresh(true);
}
else
{
CResource* resource = g_pGame->GetResourceManager()->GetResource(szArguments);
if (resource)
{
g_pGame->GetResourceManager()->UpgradeResources(resource);
g_pGame->GetResourceManager()->Refresh(true, resource->GetName());
pEchoClient->SendEcho("Upgrade completed.");
}
else
pEchoClient->SendConsole(SString("upgrade: Resource '%s' could not be found", szArguments));
}
}
else
{
pEchoClient->SendConsole("* Syntax: upgrade <resource-name> | all");
}
return true;
}
bool CConsoleCommands::CheckResources(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
// To work on remote clients, 'check' needs ACL entry + console capture
if (pClient->GetClientType() != CClient::CLIENT_CONSOLE)
return false;
if (szArguments && szArguments[0])
{
if (SStringX("all") == szArguments)
{
pEchoClient->SendConsole("Checking all resources...");
g_pGame->GetResourceManager()->CheckResources();
pEchoClient->SendEcho("Check completed");
}
else
{
CResource* resource = g_pGame->GetResourceManager()->GetResource(szArguments);
if (resource)
{
g_pGame->GetResourceManager()->CheckResources(resource);
pEchoClient->SendEcho("Check completed");
}
else
pEchoClient->SendConsole(SString("check: Resource '%s' could not be found", szArguments));
}
}
else
{
pEchoClient->SendConsole("* Syntax: check <resource-name> | all");
}
return true;
}
bool CConsoleCommands::Say(CConsole* pConsole, const char* szInArguments, CClient* pClient, CClient* pEchoClient)
{
// say <text>
// If he isn't muted
if (pClient->GetClientType() != CClient::CLIENT_PLAYER || !static_cast<CPlayer*>(pClient)->IsMuted())
{
// Got a message?
if (szInArguments)
{
// Strip any unwanted characters
COPY_CSTR_TO_TEMP_BUFFER(szArguments, szInArguments, 256);
stripControlCodes(szArguments);
// Long/short enough?
size_t sizeArguments = MbUTF8ToUTF16(szArguments).size();
if (sizeArguments >= MIN_CHAT_LENGTH && sizeArguments <= MAX_CHAT_LENGTH)
{
// Grab the nick
const char* szNick = pClient->GetNick();
if (szNick)
{
SString strEcho;
char szEcho[MAX_CHATECHO_LENGTH];
szEcho[0] = '\0';
switch (pClient->GetClientType())
{
case CClient::CLIENT_PLAYER:
{
strEcho.Format("%s: #EBDDB2%s", szNick, szArguments);
// Send the chat message and player pointer to the script
CLuaArguments Arguments;
Arguments.PushString(szArguments);
Arguments.PushNumber(0); // Normal chat
bool bContinue = static_cast<CPlayer*>(pClient)->CallEvent("onPlayerChat", Arguments);
if (bContinue)
{
// Log it in the console
CLogger::LogPrintf("CHAT: %s: %s\n", szNick, szArguments);
unsigned char ucR = 0xFF, ucG = 0xFF, ucB = 0xFF;
CPlayer* pPlayer = static_cast<CPlayer*>(pClient);
CTeam* pTeam = pPlayer->GetTeam();
if (pTeam)
{
pTeam->GetColor(ucR, ucG, ucB);
}
// Broadcast the message to all clients
auto ChatEchoPacket = CChatEchoPacket(strEcho, ucR, ucG, ucB, true);
ChatEchoPacket.SetSourceElement(pPlayer);
pConsole->GetPlayerManager()->BroadcastOnlyJoined(ChatEchoPacket);
// Call onChatMessage if players chat message was delivered
CLuaArguments Arguments2;
Arguments2.PushString(szArguments);
Arguments2.PushElement(pPlayer);
pPlayer->CallEvent("onChatMessage", Arguments2);
}
break;
}
case CClient::CLIENT_REMOTECLIENT:
{
// Populate a chat message
snprintf(szEcho, MAX_CHATECHO_LENGTH, "%s: %s", szNick, szArguments);
szEcho[MAX_CHATECHO_LENGTH - 1] = '\0';
// Log it in the console
CLogger::LogPrintf("ADMINCHAT: %s: %s\n", szNick, szArguments);
// Broadcast the message to all clients
pConsole->GetPlayerManager()->BroadcastOnlyJoined(CChatEchoPacket(szEcho, CHATCOLOR_ADMINSAY));
break;
}
case CClient::CLIENT_CONSOLE:
{
// Populate a chat message
snprintf(szEcho, MAX_CHATECHO_LENGTH, "%s: %s", szNick, szArguments);
szEcho[MAX_CHATECHO_LENGTH - 1] = '\0';
// Log it in the console
CLogger::LogPrintf("CONSOLECHAT: %s\n", szArguments);
// Broadcast the message to all clients
pConsole->GetPlayerManager()->BroadcastOnlyJoined(CChatEchoPacket(szEcho, CHATCOLOR_CONSOLESAY));
break;
}
case CClient::CLIENT_SCRIPT:
{
// Populate a chat message
snprintf(szEcho, MAX_CHATECHO_LENGTH, "%s: %s", szNick, szArguments);
szEcho[MAX_CHATECHO_LENGTH - 1] = '\0';
// Log it in the console
CLogger::LogPrintf("SCRIPTCHAT: %s\n", szEcho);
// Broadcast the message to all clients
pConsole->GetPlayerManager()->BroadcastOnlyJoined(CChatEchoPacket(szEcho, CHATCOLOR_SAY));
break;
}
default:
break;
}
// Success
return true;
}
}
else
{
pEchoClient->SendEcho("say: Invalid text length");
}
}
else
{
// pEchoClient->SendEcho ( "say: Syntax is 'say <text>'" );
}
}
else
{
pEchoClient->SendEcho("say: You are muted");
}
return false;
}
bool CConsoleCommands::TeamSay(CConsole* pConsole, const char* szInArguments, CClient* pClient, CClient* pEchoClient)
{
// teamsay <text>
// If its a player (only players have teams)
if (pClient->GetClientType() == CClient::CLIENT_PLAYER)
{
CPlayer* pPlayer = static_cast<CPlayer*>(pClient);
// If he isn't muted
if (!pPlayer->IsMuted())
{
// If he's on a team
CTeam* pTeam = pPlayer->GetTeam();
if (pTeam)
{
// Got a message?
if (szInArguments)
{
// Strip any unwanted characters
COPY_CSTR_TO_TEMP_BUFFER(szArguments, szInArguments, 256);
stripControlCodes(szArguments);
// Long/short enough?
size_t sizeArguments = MbUTF8ToUTF16(szArguments).size();
if (sizeArguments >= MIN_CHAT_LENGTH && sizeArguments <= MAX_CHAT_LENGTH)
{
// Grab the nick
const char* szNick = pClient->GetNick();
if (szNick)
{
SString strEcho;
// Populate a chat message
strEcho.Format("(TEAM) %s: #EBDDB2%s", szNick, szArguments);
// Send the chat message and player pointer to the script
CLuaArguments Arguments;
Arguments.PushString(szArguments);
Arguments.PushNumber(2); // Team chat
bool bContinue = static_cast<CPlayer*>(pClient)->CallEvent("onPlayerChat", Arguments);
if (bContinue)
{
// Log it in the console
CLogger::LogPrintf("TEAMCHAT: %s: %s\n", szNick, szArguments);
unsigned char ucRed = 0xFF, ucGreen = 0xFF, ucBlue = 0xFF;
pTeam->GetColor(ucRed, ucGreen, ucBlue);
// Broadcast to all the team members
list<CPlayer*>::const_iterator iter = pTeam->PlayersBegin();
for (; iter != pTeam->PlayersEnd(); iter++)
{
(*iter)->Send(CChatEchoPacket(strEcho, ucRed, ucGreen, ucBlue, true));
}
// Call onChatMessage if players chat message was delivered
CLuaArguments Arguments2;
Arguments2.PushString(szArguments);
Arguments2.PushElement(pTeam);
static_cast<CPlayer*>(pClient)->CallEvent("onChatMessage", Arguments2);
}
// Success
return true;
}
}
else
{
pEchoClient->SendEcho("teamsay: Invalid text length");
}
}
}
else
{
// pEchoClient->SendEcho ( "teamsay: You aren't on a team" );
}
}
else
{
pEchoClient->SendEcho("teamsay: You are muted");
}
}
else
{
// pEchoClient->SendEcho ( "teamsay: Only players have teams" );
}
return false;
}
bool CConsoleCommands::Msg(CConsole* pConsole, const char* szInArguments, CClient* pClient, CClient* pEchoClient)
{
// msg <player> <text>
// If he isn't muted
if (pClient->GetClientType() != CClient::CLIENT_PLAYER || !static_cast<CPlayer*>(pClient)->IsMuted())
{
// Got a message?
if (szInArguments)
{
// Strip any unwanted characters
COPY_CSTR_TO_TEMP_BUFFER(szArguments, szInArguments, 256);
stripControlCodes(szArguments);
COPY_CSTR_TO_TEMP_BUFFER(szBuffer, szArguments, 256);
char* szPlayer = strtok(szBuffer, " ");
char* szMessage = strtok(NULL, "\0");
if (szPlayer)
{
CPlayer* pPlayer = pConsole->GetPlayerManager()->Get(szPlayer);
if (pPlayer)
{
if (szMessage)
{
// Long/short enough?
size_t sizeMessage = MbUTF8ToUTF16(szArguments).size();
if (sizeMessage >= MIN_CHAT_LENGTH && sizeMessage <= MAX_CHAT_LENGTH)
{
// Grab the nick
const char* szNick = pClient->GetNick();
if (szNick)
{
// Populate the message to the player
SString strMessage("* PM from %s: %s", szNick, szMessage);
switch (pClient->GetClientType())
{
case CClient::CLIENT_PLAYER:
{
CPlayer* pSender = static_cast<CPlayer*>(pClient);
if (pPlayer == pSender)
{
pEchoClient->SendEcho("msg: You cannot message yourself");
return false;
}
// Log it
CLogger::LogPrintf("MSG: %s to %s: %s\n", szNick, pPlayer->GetNick(), szMessage);
// Send the message and player pointer to the script
CLuaArguments Arguments;
Arguments.PushString(szArguments);
Arguments.PushElement(pPlayer);
bool bContinue = pSender->CallEvent("onPlayerPrivateMessage", Arguments);
if (bContinue)
{
// Send it to the player
pPlayer->Send(CChatEchoPacket(strMessage, CHATCOLOR_INFO));
// Send a reponse to the player who sent it
pEchoClient->SendEcho(SString("-> %s: %s", pPlayer->GetNick(), szMessage));
}
break;
}
case CClient::CLIENT_CONSOLE:
{
// Log it
CLogger::LogPrintf("CONSOLEMSG: %s to %s: %s\n", szNick, pPlayer->GetNick(), szMessage);
// Send it to the player
pPlayer->Send(CChatEchoPacket(strMessage, CHATCOLOR_INFO));
}
case CClient::CLIENT_SCRIPT:
{
// Log it
CLogger::LogPrintf("SCRIPTMSG: %s to %s: %s\n", szNick, pPlayer->GetNick(), szMessage);
// Send it to the player
pPlayer->Send(CChatEchoPacket(strMessage, CHATCOLOR_INFO));
break;
}
default:
break;
}
// Success
return true;
}
}
else
{
pEchoClient->SendEcho("msg: Invalid message length");
}
}
else
{
pEchoClient->SendEcho("msg: Invalid message");
}
}
else
{
pEchoClient->SendEcho("msg: Player not found");
}
}
else
{
pEchoClient->SendEcho("msg: Player not found");
}
}
else
{
// pEchoClient->SendEcho ( "msg: Syntax is 'msg <nick> <msg>'" );
}
}
else
{
pEchoClient->SendEcho("msg: You are muted");
}
return false;
}
bool CConsoleCommands::Me(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
// me <text>
// If he isn't muted
if (pClient->GetClientType() != CClient::CLIENT_PLAYER || !static_cast<CPlayer*>(pClient)->IsMuted())
{
// Got a message?
if (szArguments)
{
// Long/short enough?
size_t sizeArguments = MbUTF8ToUTF16(szArguments).size();
if (sizeArguments >= MIN_CHAT_LENGTH && sizeArguments <= MAX_CHAT_LENGTH)
{
// Grab the nick
const char* szNick = pClient->GetNick();
if (szNick)
{
// Populate a chat message depending on if it starts on /me or not
SString strEcho("* %s %s", szNick, szArguments);
// Send the chat message and player pointer to the script IF the client is a player
if (pClient->GetClientType() == CClient::CLIENT_PLAYER)
{
CLuaArguments Arguments;
Arguments.PushString(szArguments); // text
Arguments.PushNumber(1); // Me chat
bool bContinue = static_cast<CPlayer*>(pClient)->CallEvent("onPlayerChat", Arguments);
if (bContinue)
{
// Log it in the console
CLogger::LogPrintf("CHAT: %s\n", strEcho.c_str());
// Broadcast the message to all clients
pConsole->GetPlayerManager()->BroadcastOnlyJoined(CChatEchoPacket(strEcho, CHATCOLOR_ME));
// Call onChatMessage if players chat message was delivered
CPlayer* pPlayer = static_cast<CPlayer*>(pClient);
CLuaArguments Arguments2;
Arguments2.PushString(szArguments);
Arguments2.PushElement(pPlayer);
static_cast<CPlayer*>(pClient)->CallEvent("onChatMessage", Arguments2);
}
}
// Success
return true;
}
}
}
}
else
{
pEchoClient->SendEcho("me: You are muted");
}
return false;
}
bool CConsoleCommands::Nick(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
// nick <name>
// Only players can change their nicks
if (pClient->GetClientType() == CClient::CLIENT_PLAYER)
{
// Got a nickname?
if (szArguments)
{
// Copy the nickname
COPY_CSTR_TO_TEMP_BUFFER(szNewNick, szArguments, 64);
// Check its validity
if (IsNickValid(szNewNick))
{
if (CheckNickProvided(szNewNick))
{
// Verify the length
size_t sizeNewNick = strlen(szNewNick);
if (sizeNewNick >= MIN_PLAYER_NICK_LENGTH && sizeNewNick <= MAX_PLAYER_NICK_LENGTH)
{
// Does the nickname differ from the previous nickname?
const char* szNick = pClient->GetNick();
if (!szNick || strcmp(szNewNick, szNick) != 0)
{
// Check that it doesn't already exist, or if it matches our current nick case-independantly (means we changed to the same nick but
// in a different case)
if ((szNick && stricmp(szNick, szNewNick) == 0) || !pConsole->GetPlayerManager()->Get(szNewNick))
{
CPlayer* pPlayer = static_cast<CPlayer*>(pClient);
// Call the event
CLuaArguments Arguments;
Arguments.PushString(pClient->GetNick());
Arguments.PushString(szNewNick);
Arguments.PushBoolean(true); // manually changed
if (pPlayer->CallEvent("onPlayerChangeNick", Arguments))
{
// Tell the console
CLogger::LogPrintf("NICK: %s is now known as %s\n", szNick, szNewNick);
// Change the nick
pPlayer->SetNick(szNewNick);
// Tell all ingame players about the nick change
CPlayerChangeNickPacket Packet(szNewNick);
Packet.SetSourceElement(pPlayer);
pConsole->GetPlayerManager()->BroadcastOnlyJoined(Packet);
return true;
}
else
return false;
}
else
{
pEchoClient->SendEcho("nick: Chosen nickname is already in use");
}
}
else
{
// Tell the player
pEchoClient->SendEcho(SString("nick: Nickname is already %s", szNick));
}
}
else
{
// Tell the player
pEchoClient->SendEcho(SString("nick: Nick must be between %u and %u characters", MIN_PLAYER_NICK_LENGTH, MAX_PLAYER_NICK_LENGTH));
}
}
else
{
pEchoClient->SendEcho("nick: Chosen nickname is not allowed");
}
}
else
{
pEchoClient->SendEcho("nick: Chosen nickname contains illegal characters");
}
}
else
{
// pEchoClient->SendEcho ( "nick: Syntax is 'nick <name>'" );
}
}
else
{
pEchoClient->SendEcho("nick: Only players can change their nick");
}
return false;
}
// HACKED IN FOR NOW
#include "CGame.h"
extern CGame* g_pGame;
bool CConsoleCommands::LogIn(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
// login [<name>] <pass>
// Grab the sender's nick
const char* szNick = pClient->GetNick();
const char* szPassword = szArguments;
// Got any arguments?
if (szArguments && szArguments[0])
{
COPY_CSTR_TO_TEMP_BUFFER(szTemp, szArguments, 256);
char* szTempNick = strtok(szTemp, " ");
char* szTempPassword = strtok(NULL, "\0");
if (szTempPassword)
{
szNick = szTempNick;
szPassword = szTempPassword;
}
if (CAccountManager::IsValidAccountName(szNick) && CAccountManager::IsValidPassword(szPassword))
{
return g_pGame->GetAccountManager()->LogIn(pClient, pEchoClient, szNick, szPassword);
}
else
{
if (pEchoClient)
pEchoClient->SendEcho("login: Syntax is 'login [<nick>] <password>'");
}
}
else
{
if (pEchoClient)
pEchoClient->SendEcho("login: Syntax is 'login [<nick>] <password>'");
}
return false;
}
bool CConsoleCommands::LogOut(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
// logout
return g_pGame->GetAccountManager()->LogOut(pClient, pEchoClient);
}
bool CConsoleCommands::ChgMyPass(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
// chgmypass <oldpass> <newpass>
// User must be logged in with the account password is changed for
// Got any arguments?
if (szArguments)
{
// Is the user logged in?
if (pClient->IsRegistered())
{
// Copy the command
COPY_CSTR_TO_TEMP_BUFFER(szBuffer, szArguments, 256);
// Split it up into nick and password
char* szOldPassword = strtok(szBuffer, " ");
char* szNewPassword = strtok(NULL, "\0");
if (CAccountManager::IsValidPassword(szOldPassword) && CAccountManager::IsValidNewPassword(szNewPassword))
{
// Grab the account with that nick
CAccount* pAccount = pClient->GetAccount();
if (pAccount)
{
// Check old password is correct
if (pAccount->IsPassword(szOldPassword))
{
// Set the new password
pAccount->SetPassword(szNewPassword);
// Tell the client
if (pClient->GetClientType() != CClient::CLIENT_CONSOLE)
pEchoClient->SendEcho(SString("chgmypass: Your password was changed to '%s'", szNewPassword));
CLogger::LogPrintf("ACCOUNTS: %s changed their account password\n", GetAdminNameForLog(pClient).c_str());
return true;
}
else
{
if (pClient->GetClientType() != CClient::CLIENT_CONSOLE)
pEchoClient->SendEcho("chgmypass: Bad old password");
CLogger::LogPrintf("ACCOUNTS: %s failed to change their account password (Bad old password)\n", GetAdminNameForLog(pClient).c_str());
}
}
else
{
pEchoClient->SendEcho("chgmypass: The account you were logged in as no longer exists");
}
}
else
{
pEchoClient->SendEcho("chgmypass: Syntax is 'chgmypass <oldpass> <newpass>'");
}
}
else
{
pEchoClient->SendEcho("chgmypass: You must be logged in to use this command");
}
}
else
{
pEchoClient->SendEcho("chgmypass: Syntax is 'chgmypass <oldpass> <newpass>'");
}
return false;
}
bool CConsoleCommands::AddAccount(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
// addaccount <nick> <password>
// Got any arguments?
if (szArguments)
{
// Copy the argument buffer
COPY_CSTR_TO_TEMP_BUFFER(szBuffer, szArguments, 256);
// Split it into nick and reason
char* szNick = strtok(szBuffer, " ");
char* szPassword = strtok(NULL, " ");
// Check that we got everything
if (szNick && szPassword)
{
// Long enough strings?
if (CAccountManager::IsValidNewAccountName(szNick) && CAccountManager::IsValidNewPassword(szPassword))
{
SString strCaseVariation = g_pGame->GetAccountManager()->GetActiveCaseVariation(szNick);
if (strCaseVariation.empty())
{
// Try creating the account
if (!g_pGame->GetAccountManager()->Get(szNick))
{
g_pGame->GetAccountManager()->AddNewPlayerAccount(szNick, szPassword);
// Tell the user
if (pClient->GetClientType() != CClient::CLIENT_CONSOLE)
pClient->SendEcho(SString("addaccount: Added account '%s' with password '%s'", szNick, szPassword));
// Tell the console
CLogger::LogPrintf("ACCOUNTS: %s added account '%s'\n", GetAdminNameForLog(pClient).c_str(), szNick);
return true;
}
else
{
pEchoClient->SendEcho("addaccount: Already an account with that name");
}
}
else
{
pEchoClient->SendEcho(SString("addaccount: Already an account using a case variation of that name ('%s')", *strCaseVariation));
}
}
else
{
pEchoClient->SendEcho("addaccount: Syntax is 'addaccount <nick> <password>'");
}
}
else
{
pEchoClient->SendEcho("addaccount: Syntax is 'addaccount <nick> <password>'");
}
}
else
{
pEchoClient->SendEcho("addaccount: Syntax is 'addaccount <nick> <password>'");
}
return false;
}
bool CConsoleCommands::DelAccount(CConsole* pConsole, const char* szArguments, CClient* pClient, CClient* pEchoClient)
{
// delaccount <nick>
const char* szNick = szArguments;
// Got any arguments?
if (szArguments)
{
// Grab the account with that nick
CAccount* pAccount = g_pGame->GetAccountManager()->Get(szNick);