-
-
Notifications
You must be signed in to change notification settings - Fork 383
/
Commands.cs
6760 lines (6210 loc) · 228 KB
/
Commands.cs
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
/*
TShock, a server mod for Terraria
Copyright (C) 2011-2019 Pryaxis & TShock Contributors
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 3 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, see <http://www.gnu.org/licenses/>.
*/
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using Terraria;
using Terraria.ID;
using Terraria.Localization;
using TShockAPI.DB;
using TerrariaApi.Server;
using TShockAPI.Hooks;
using Terraria.GameContent.Events;
using Microsoft.Xna.Framework;
using TShockAPI.Localization;
using System.Text.RegularExpressions;
using Terraria.DataStructures;
using Terraria.GameContent.Creative;
namespace TShockAPI
{
public delegate void CommandDelegate(CommandArgs args);
public class CommandArgs : EventArgs
{
public string Message { get; private set; }
public TSPlayer Player { get; private set; }
public bool Silent { get; private set; }
/// <summary>
/// Parameters passed to the argument. Does not include the command name.
/// IE '/kick "jerk face"' will only have 1 argument
/// </summary>
public List<string> Parameters { get; private set; }
public Player TPlayer
{
get { return Player.TPlayer; }
}
public CommandArgs(string message, TSPlayer ply, List<string> args)
{
Message = message;
Player = ply;
Parameters = args;
Silent = false;
}
public CommandArgs(string message, bool silent, TSPlayer ply, List<string> args)
{
Message = message;
Player = ply;
Parameters = args;
Silent = silent;
}
}
public class Command
{
/// <summary>
/// Gets or sets whether to allow non-players to use this command.
/// </summary>
public bool AllowServer { get; set; }
/// <summary>
/// Gets or sets whether to do logging of this command.
/// </summary>
public bool DoLog { get; set; }
/// <summary>
/// Gets or sets the help text of this command.
/// </summary>
public string HelpText { get; set; }
/// <summary>
/// Gets or sets an extended description of this command.
/// </summary>
public string[] HelpDesc { get; set; }
/// <summary>
/// Gets the name of the command.
/// </summary>
public string Name { get { return Names[0]; } }
/// <summary>
/// Gets the names of the command.
/// </summary>
public List<string> Names { get; protected set; }
/// <summary>
/// Gets the permissions of the command.
/// </summary>
public List<string> Permissions { get; protected set; }
private CommandDelegate commandDelegate;
public CommandDelegate CommandDelegate
{
get { return commandDelegate; }
set
{
if (value == null)
throw new ArgumentNullException();
commandDelegate = value;
}
}
public Command(List<string> permissions, CommandDelegate cmd, params string[] names)
: this(cmd, names)
{
Permissions = permissions;
}
public Command(string permissions, CommandDelegate cmd, params string[] names)
: this(cmd, names)
{
Permissions = new List<string> { permissions };
}
public Command(CommandDelegate cmd, params string[] names)
{
if (cmd == null)
throw new ArgumentNullException("cmd");
if (names == null || names.Length < 1)
throw new ArgumentException("names");
AllowServer = true;
CommandDelegate = cmd;
DoLog = true;
HelpText = GetString("No help available.");
HelpDesc = null;
Names = new List<string>(names);
Permissions = new List<string>();
}
public bool Run(string msg, bool silent, TSPlayer ply, List<string> parms)
{
if (!CanRun(ply))
return false;
try
{
CommandDelegate(new CommandArgs(msg, silent, ply, parms));
}
catch (Exception e)
{
ply.SendErrorMessage(GetString("Command failed, check logs for more details."));
TShock.Log.Error(e.ToString());
}
return true;
}
public bool Run(string msg, TSPlayer ply, List<string> parms)
{
return Run(msg, false, ply, parms);
}
public bool HasAlias(string name)
{
return Names.Contains(name);
}
public bool CanRun(TSPlayer ply)
{
if (Permissions == null || Permissions.Count < 1)
return true;
foreach (var Permission in Permissions)
{
if (ply.HasPermission(Permission))
return true;
}
return false;
}
}
public static class Commands
{
public static List<Command> ChatCommands = new List<Command>();
public static ReadOnlyCollection<Command> TShockCommands = new ReadOnlyCollection<Command>(new List<Command>());
/// <summary>
/// The command specifier, defaults to "/"
/// </summary>
public static string Specifier
{
get { return string.IsNullOrWhiteSpace(TShock.Config.Settings.CommandSpecifier) ? "/" : TShock.Config.Settings.CommandSpecifier; }
}
/// <summary>
/// The silent command specifier, defaults to "."
/// </summary>
public static string SilentSpecifier
{
get { return string.IsNullOrWhiteSpace(TShock.Config.Settings.CommandSilentSpecifier) ? "." : TShock.Config.Settings.CommandSilentSpecifier; }
}
private delegate void AddChatCommand(string permission, CommandDelegate command, params string[] names);
public static void InitCommands()
{
List<Command> tshockCommands = new List<Command>(100);
Action<Command> add = (cmd) =>
{
tshockCommands.Add(cmd);
ChatCommands.Add(cmd);
};
add(new Command(SetupToken, "setup")
{
AllowServer = false,
HelpText = GetString("Used to authenticate as superadmin when first setting up TShock.")
});
add(new Command(Permissions.user, ManageUsers, "user")
{
DoLog = false,
HelpText = GetString("Manages user accounts.")
});
#region Account Commands
add(new Command(Permissions.canlogin, AttemptLogin, "login")
{
AllowServer = false,
DoLog = false,
HelpText = GetString("Logs you into an account.")
});
add(new Command(Permissions.canlogout, Logout, "logout")
{
AllowServer = false,
DoLog = false,
HelpText = GetString("Logs you out of your current account.")
});
add(new Command(Permissions.canchangepassword, PasswordUser, "password")
{
AllowServer = false,
DoLog = false,
HelpText = GetString("Changes your account's password.")
});
add(new Command(Permissions.canregister, RegisterUser, "register")
{
AllowServer = false,
DoLog = false,
HelpText = GetString("Registers you an account.")
});
add(new Command(Permissions.checkaccountinfo, ViewAccountInfo, "accountinfo", "ai")
{
HelpText = GetString("Shows information about a user.")
});
#endregion
#region Admin Commands
add(new Command(Permissions.ban, Ban, "ban")
{
HelpText = GetString("Manages player bans.")
});
add(new Command(Permissions.broadcast, Broadcast, "broadcast", "bc", "say")
{
HelpText = GetString("Broadcasts a message to everyone on the server.")
});
add(new Command(Permissions.logs, DisplayLogs, "displaylogs")
{
HelpText = GetString("Toggles whether you receive server logs.")
});
add(new Command(Permissions.managegroup, Group, "group")
{
HelpText = GetString("Manages groups.")
});
add(new Command(Permissions.manageitem, ItemBan, "itemban")
{
HelpText = GetString("Manages item bans.")
});
add(new Command(Permissions.manageprojectile, ProjectileBan, "projban")
{
HelpText = GetString("Manages projectile bans.")
});
add(new Command(Permissions.managetile, TileBan, "tileban")
{
HelpText = GetString("Manages tile bans.")
});
add(new Command(Permissions.manageregion, Region, "region")
{
HelpText = GetString("Manages regions.")
});
add(new Command(Permissions.kick, Kick, "kick")
{
HelpText = GetString("Removes a player from the server.")
});
add(new Command(Permissions.mute, Mute, "mute", "unmute")
{
HelpText = GetString("Prevents a player from talking.")
});
add(new Command(Permissions.savessc, OverrideSSC, "overridessc", "ossc")
{
HelpText = GetString("Overrides serverside characters for a player, temporarily.")
});
add(new Command(Permissions.savessc, SaveSSC, "savessc")
{
HelpText = GetString("Saves all serverside characters.")
});
add(new Command(Permissions.uploaddata, UploadJoinData, "uploadssc")
{
HelpText = GetString("Upload the account information when you joined the server as your Server Side Character data.")
});
add(new Command(Permissions.settempgroup, TempGroup, "tempgroup")
{
HelpText = GetString("Temporarily sets another player's group.")
});
add(new Command(Permissions.su, SubstituteUser, "su")
{
HelpText = GetString("Temporarily elevates you to Super Admin.")
});
add(new Command(Permissions.su, SubstituteUserDo, "sudo")
{
HelpText = GetString("Executes a command as the super admin.")
});
add(new Command(Permissions.userinfo, GrabUserUserInfo, "userinfo", "ui")
{
HelpText = GetString("Shows information about a player.")
});
#endregion
#region Annoy Commands
add(new Command(Permissions.annoy, Annoy, "annoy")
{
HelpText = GetString("Annoys a player for an amount of time.")
});
add(new Command(Permissions.annoy, Rocket, "rocket")
{
HelpText = GetString("Rockets a player upwards. Requires SSC.")
});
add(new Command(Permissions.annoy, FireWork, "firework")
{
HelpText = GetString("Spawns fireworks at a player.")
});
#endregion
#region Configuration Commands
add(new Command(Permissions.maintenance, CheckUpdates, "checkupdates")
{
HelpText = GetString("Checks for TShock updates.")
});
add(new Command(Permissions.maintenance, Off, "off", "exit", "stop")
{
HelpText = GetString("Shuts down the server while saving.")
});
add(new Command(Permissions.maintenance, OffNoSave, "off-nosave", "exit-nosave", "stop-nosave")
{
HelpText = GetString("Shuts down the server without saving.")
});
add(new Command(Permissions.cfgreload, Reload, "reload")
{
HelpText = GetString("Reloads the server configuration file.")
});
add(new Command(Permissions.cfgpassword, ServerPassword, "serverpassword")
{
HelpText = GetString("Changes the server password.")
});
add(new Command(Permissions.maintenance, GetVersion, "version")
{
HelpText = GetString("Shows the TShock version.")
});
add(new Command(Permissions.whitelist, Whitelist, "whitelist")
{
HelpText = GetString("Manages the server whitelist.")
});
#endregion
#region Item Commands
add(new Command(Permissions.give, Give, "give", "g")
{
HelpText = GetString("Gives another player an item.")
});
add(new Command(Permissions.item, Item, "item", "i")
{
AllowServer = false,
HelpText = GetString("Gives yourself an item.")
});
#endregion
#region NPC Commands
add(new Command(Permissions.butcher, Butcher, "butcher")
{
HelpText = GetString("Kills hostile NPCs or NPCs of a certain type.")
});
add(new Command(Permissions.renamenpc, RenameNPC, "renamenpc")
{
HelpText = GetString("Renames an NPC.")
});
add(new Command(Permissions.maxspawns, MaxSpawns, "maxspawns")
{
HelpText = GetString("Sets the maximum number of NPCs.")
});
add(new Command(Permissions.spawnboss, SpawnBoss, "spawnboss", "sb")
{
AllowServer = false,
HelpText = GetString("Spawns a number of bosses around you.")
});
add(new Command(Permissions.spawnmob, SpawnMob, "spawnmob", "sm")
{
AllowServer = false,
HelpText = GetString("Spawns a number of mobs around you.")
});
add(new Command(Permissions.spawnrate, SpawnRate, "spawnrate")
{
HelpText = GetString("Sets the spawn rate of NPCs.")
});
add(new Command(Permissions.clearangler, ClearAnglerQuests, "clearangler")
{
HelpText = GetString("Resets the list of users who have completed an angler quest that day.")
});
#endregion
#region TP Commands
add(new Command(Permissions.home, Home, "home")
{
AllowServer = false,
HelpText = GetString("Sends you to your spawn point.")
});
add(new Command(Permissions.spawn, Spawn, "spawn")
{
AllowServer = false,
HelpText = GetString("Sends you to the world's spawn point.")
});
add(new Command(Permissions.tp, TP, "tp")
{
AllowServer = false,
HelpText = GetString("Teleports a player to another player.")
});
add(new Command(Permissions.tpothers, TPHere, "tphere")
{
AllowServer = false,
HelpText = GetString("Teleports a player to yourself.")
});
add(new Command(Permissions.tpnpc, TPNpc, "tpnpc")
{
AllowServer = false,
HelpText = GetString("Teleports you to an npc.")
});
add(new Command(Permissions.tppos, TPPos, "tppos")
{
AllowServer = false,
HelpText = GetString("Teleports you to tile coordinates.")
});
add(new Command(Permissions.getpos, GetPos, "pos")
{
AllowServer = false,
HelpText = GetString("Returns the user's or specified user's current position.")
});
add(new Command(Permissions.tpallow, TPAllow, "tpallow")
{
AllowServer = false,
HelpText = GetString("Toggles whether other people can teleport you.")
});
#endregion
#region World Commands
add(new Command(Permissions.toggleexpert, ChangeWorldMode, "worldmode", "gamemode")
{
HelpText = GetString("Changes the world mode.")
});
add(new Command(Permissions.antibuild, ToggleAntiBuild, "antibuild")
{
HelpText = GetString("Toggles build protection.")
});
add(new Command(Permissions.grow, Grow, "grow")
{
AllowServer = false,
HelpText = GetString("Grows plants at your location.")
});
add(new Command(Permissions.halloween, ForceHalloween, "forcehalloween")
{
HelpText = GetString("Toggles halloween mode (goodie bags, pumpkins, etc).")
});
add(new Command(Permissions.xmas, ForceXmas, "forcexmas")
{
HelpText = GetString("Toggles christmas mode (present spawning, santa, etc).")
});
add(new Command(Permissions.manageevents, ManageWorldEvent, "worldevent")
{
HelpText = GetString("Enables starting and stopping various world events.")
});
add(new Command(Permissions.hardmode, Hardmode, "hardmode")
{
HelpText = GetString("Toggles the world's hardmode status.")
});
add(new Command(Permissions.editspawn, ProtectSpawn, "protectspawn")
{
HelpText = GetString("Toggles spawn protection.")
});
add(new Command(Permissions.worldsave, Save, "save")
{
HelpText = GetString("Saves the world file.")
});
add(new Command(Permissions.worldspawn, SetSpawn, "setspawn")
{
AllowServer = false,
HelpText = GetString("Sets the world's spawn point to your location.")
});
add(new Command(Permissions.dungeonposition, SetDungeon, "setdungeon")
{
AllowServer = false,
HelpText = GetString("Sets the dungeon's position to your location.")
});
add(new Command(Permissions.worldsettle, Settle, "settle")
{
HelpText = GetString("Forces all liquids to update immediately.")
});
add(new Command(Permissions.time, Time, "time")
{
HelpText = GetString("Sets the world time.")
});
add(new Command(Permissions.wind, Wind, "wind")
{
HelpText = GetString("Changes the wind speed.")
});
add(new Command(Permissions.worldinfo, WorldInfo, "worldinfo")
{
HelpText = GetString("Shows information about the current world.")
});
#endregion
#region Other Commands
add(new Command(Permissions.buff, Buff, "buff")
{
AllowServer = false,
HelpText = GetString("Gives yourself a buff or debuff for an amount of time. Putting -1 for time will set it to 415 days.")
});
add(new Command(Permissions.clear, Clear, "clear")
{
HelpText = GetString("Clears item drops or projectiles.")
});
add(new Command(Permissions.buffplayer, GBuff, "gbuff", "buffplayer")
{
HelpText = GetString("Gives another player a buff or debuff for an amount of time. Putting -1 for time will set it to 415 days.")
});
add(new Command(Permissions.godmode, ToggleGodMode, "godmode", "god")
{
HelpText = GetString("Toggles godmode on a player.")
});
add(new Command(Permissions.heal, Heal, "heal")
{
HelpText = GetString("Heals a player in HP and MP.")
});
add(new Command(Permissions.kill, Kill, "kill", "slay")
{
HelpText = GetString("Kills another player.")
});
add(new Command(Permissions.cantalkinthird, ThirdPerson, "me")
{
HelpText = GetString("Sends an action message to everyone.")
});
add(new Command(Permissions.canpartychat, PartyChat, "party", "p")
{
AllowServer = false,
HelpText = GetString("Sends a message to everyone on your team.")
});
add(new Command(Permissions.whisper, Reply, "reply", "r")
{
HelpText = GetString("Replies to a PM sent to you.")
});
add(new Command(Rests.RestPermissions.restmanage, ManageRest, "rest")
{
HelpText = GetString("Manages the REST API.")
});
add(new Command(Permissions.slap, Slap, "slap")
{
HelpText = GetString("Slaps a player, dealing damage.")
});
add(new Command(Permissions.serverinfo, ServerInfo, "serverinfo")
{
HelpText = GetString("Shows the server information.")
});
add(new Command(Permissions.warp, Warp, "warp")
{
HelpText = GetString("Teleports you to a warp point or manages warps.")
});
add(new Command(Permissions.whisper, Whisper, "whisper", "w", "tell", "pm", "dm")
{
HelpText = GetString("Sends a PM to a player.")
});
add(new Command(Permissions.whisper, Wallow, "wallow", "wa")
{
AllowServer = false,
HelpText = GetString("Toggles to either ignore or recieve whispers from other players.")
});
add(new Command(Permissions.createdumps, CreateDumps, "dump-reference-data")
{
HelpText = GetString("Creates a reference tables for Terraria data types and the TShock permission system in the server folder.")
});
add(new Command(Permissions.synclocalarea, SyncLocalArea, "sync")
{
HelpText = GetString("Sends all tiles from the server to the player to resync the client with the actual world state.")
});
add(new Command(Permissions.respawn, Respawn, "respawn")
{
HelpText = GetString("Respawn yourself or another player.")
});
#endregion
add(new Command(Aliases, "aliases")
{
HelpText = GetString("Shows a command's aliases.")
});
add(new Command(Help, "help")
{
HelpText = GetString("Lists commands or gives help on them.")
});
add(new Command(Motd, "motd")
{
HelpText = GetString("Shows the message of the day.")
});
add(new Command(ListConnectedPlayers, "playing", "online", "who")
{
HelpText = GetString("Shows the currently connected players.")
});
add(new Command(Rules, "rules")
{
HelpText = GetString("Shows the server's rules.")
});
TShockCommands = new ReadOnlyCollection<Command>(tshockCommands);
}
public static bool HandleCommand(TSPlayer player, string text)
{
string cmdText = text.Remove(0, 1);
string cmdPrefix = text[0].ToString();
bool silent = false;
if (cmdPrefix == SilentSpecifier)
silent = true;
int index = -1;
for (int i = 0; i < cmdText.Length; i++)
{
if (IsWhiteSpace(cmdText[i]))
{
index = i;
break;
}
}
string cmdName;
if (index == 0) // Space after the command specifier should not be supported
{
player.SendErrorMessage(GetString("You entered a space after {0} instead of a command. Type {0}help for a list of valid commands.", Specifier));
return true;
}
else if (index < 0)
cmdName = cmdText.ToLower();
else
cmdName = cmdText.Substring(0, index).ToLower();
List<string> args;
if (index < 0)
args = new List<string>();
else
args = ParseParameters(cmdText.Substring(index));
IEnumerable<Command> cmds = ChatCommands.FindAll(c => c.HasAlias(cmdName));
if (Hooks.PlayerHooks.OnPlayerCommand(player, cmdName, cmdText, args, ref cmds, cmdPrefix))
return true;
if (cmds.Count() == 0)
{
if (player.AwaitingResponse.ContainsKey(cmdName))
{
Action<CommandArgs> call = player.AwaitingResponse[cmdName];
player.AwaitingResponse.Remove(cmdName);
call(new CommandArgs(cmdText, player, args));
return true;
}
player.SendErrorMessage(GetString("Invalid command entered. Type {0}help for a list of valid commands.", Specifier));
return true;
}
foreach (Command cmd in cmds)
{
if (!cmd.CanRun(player))
{
if (cmd.DoLog)
TShock.Utils.SendLogs(GetString("{0} tried to execute {1}{2}.", player.Name, Specifier, cmdText), Color.PaleVioletRed, player);
else
TShock.Utils.SendLogs(GetString("{0} tried to execute (args omitted) {1}{2}.", player.Name, Specifier, cmdName), Color.PaleVioletRed, player);
player.SendErrorMessage(GetString("You do not have access to this command."));
if (player.HasPermission(Permissions.su))
{
player.SendInfoMessage(GetString("You can use '{0}sudo {0}{1}' to override this check.", Specifier, cmdText));
}
}
else if (!cmd.AllowServer && !player.RealPlayer)
{
player.SendErrorMessage(GetString("You must use this command in-game."));
}
else
{
if (cmd.DoLog)
TShock.Utils.SendLogs(GetString("{0} executed: {1}{2}.", player.Name, silent ? SilentSpecifier : Specifier, cmdText), Color.PaleVioletRed, player);
else
TShock.Utils.SendLogs(GetString("{0} executed (args omitted): {1}{2}.", player.Name, silent ? SilentSpecifier : Specifier, cmdName), Color.PaleVioletRed, player);
cmd.Run(cmdText, silent, player, args);
}
}
return true;
}
/// <summary>
/// Parses a string of parameters into a list. Handles quotes.
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static List<String> ParseParameters(string str)
{
var ret = new List<string>();
var sb = new StringBuilder();
bool instr = false;
for (int i = 0; i < str.Length; i++)
{
char c = str[i];
if (c == '\\' && ++i < str.Length)
{
if (str[i] != '"' && str[i] != ' ' && str[i] != '\\')
sb.Append('\\');
sb.Append(str[i]);
}
else if (c == '"')
{
instr = !instr;
if (!instr)
{
ret.Add(sb.ToString());
sb.Clear();
}
else if (sb.Length > 0)
{
ret.Add(sb.ToString());
sb.Clear();
}
}
else if (IsWhiteSpace(c) && !instr)
{
if (sb.Length > 0)
{
ret.Add(sb.ToString());
sb.Clear();
}
}
else
sb.Append(c);
}
if (sb.Length > 0)
ret.Add(sb.ToString());
return ret;
}
private static bool IsWhiteSpace(char c)
{
return c == ' ' || c == '\t' || c == '\n';
}
#region Account commands
private static void AttemptLogin(CommandArgs args)
{
if (args.Player.LoginAttempts > TShock.Config.Settings.MaximumLoginAttempts && (TShock.Config.Settings.MaximumLoginAttempts != -1))
{
TShock.Log.Warn(GetString("{0} ({1}) had {2} or more invalid login attempts and was kicked automatically.",
args.Player.IP, args.Player.Name, TShock.Config.Settings.MaximumLoginAttempts));
args.Player.Kick(GetString("Too many invalid login attempts."));
return;
}
if (args.Player.IsLoggedIn)
{
args.Player.SendErrorMessage(GetString("You are already logged in, and cannot login again."));
return;
}
// We need to emulate the checks done in Player.TrySwitchingLoadout, because otherwise the server is not allowed to sync the
// loadout index to the player, causing catastrophic desync.
// The player must not be dead, using an item, or CC'd to switch loadouts.
// Note that we only check for CC'd players when SSC is enabled, as that is only where it can cause issues,
// and the RequireLogin config option (without SSC) will disable player's until they login, creating a vicious cycle.
// FIXME: There is always the chance that in-between the time we check these requirements on the server, and the loadout sync
// packet reaches the client, that the client state has changed, causing the loadout sync to be rejected, even though
// we expected it to succeed.
if (args.TPlayer.dead)
{
args.Player.SendErrorMessage(GetString("You cannot login whilst dead."));
return;
}
// FIXME: This check is not correct -- even though we reject PlayerAnimation whilst disabled, we don't re-sync it to the client,
// meaning these will still be set on the client, and they WILL reject the loadout sync.
if (args.TPlayer.itemTime > 0 || args.TPlayer.itemAnimation > 0)
{
args.Player.SendErrorMessage(GetString("You cannot login whilst using an item."));
return;
}
if (args.TPlayer.CCed && Main.ServerSideCharacter)
{
args.Player.SendErrorMessage(GetString("You cannot login whilst crowd controlled."));
return;
}
UserAccount account = TShock.UserAccounts.GetUserAccountByName(args.Player.Name);
string password = "";
bool usingUUID = false;
if (args.Parameters.Count == 0 && !TShock.Config.Settings.DisableUUIDLogin)
{
if (PlayerHooks.OnPlayerPreLogin(args.Player, args.Player.Name, ""))
return;
usingUUID = true;
}
else if (args.Parameters.Count == 1)
{
if (PlayerHooks.OnPlayerPreLogin(args.Player, args.Player.Name, args.Parameters[0]))
return;
password = args.Parameters[0];
}
else if (args.Parameters.Count == 2 && TShock.Config.Settings.AllowLoginAnyUsername)
{
if (String.IsNullOrEmpty(args.Parameters[0]))
{
args.Player.SendErrorMessage(GetString("Bad login attempt."));
return;
}
if (PlayerHooks.OnPlayerPreLogin(args.Player, args.Parameters[0], args.Parameters[1]))
return;
account = TShock.UserAccounts.GetUserAccountByName(args.Parameters[0]);
password = args.Parameters[1];
}
else
{
if (!TShock.Config.Settings.DisableUUIDLogin)
args.Player.SendMessage(GetString($"{Specifier}login - Authenticates you using your UUID and character name."), Color.White);
if (TShock.Config.Settings.AllowLoginAnyUsername)
args.Player.SendMessage(GetString($"{Specifier}login <username> <password> - Authenticates you using your username and password."), Color.White);
else
args.Player.SendMessage(GetString($"{Specifier}login <password> - Authenticates you using your password and character name."), Color.White);
args.Player.SendWarningMessage(GetString("If you forgot your password, contact the administrator for help."));
return;
}
try
{
if (account == null)
{
args.Player.SendErrorMessage(GetString("A user account by that name does not exist."));
}
else if (account.VerifyPassword(password) ||
(usingUUID && account.UUID == args.Player.UUID && !TShock.Config.Settings.DisableUUIDLogin &&
!String.IsNullOrWhiteSpace(args.Player.UUID)))
{
var group = TShock.Groups.GetGroupByName(account.Group);
if (!TShock.Groups.AssertGroupValid(args.Player, group, false))
{
args.Player.SendErrorMessage(GetString("Login attempt failed - see the message above."));
return;
}
args.Player.PlayerData = TShock.CharacterDB.GetPlayerData(args.Player, account.ID);
args.Player.Group = group;
args.Player.tempGroup = null;
args.Player.Account = account;
args.Player.IsLoggedIn = true;
args.Player.IsDisabledForSSC = false;
if (Main.ServerSideCharacter)
{
if (args.Player.HasPermission(Permissions.bypassssc))
{
args.Player.PlayerData.CopyCharacter(args.Player);
TShock.CharacterDB.InsertPlayerData(args.Player);
}
args.Player.PlayerData.RestoreCharacter(args.Player);
}
args.Player.LoginFailsBySsi = false;
if (args.Player.HasPermission(Permissions.ignorestackhackdetection))
args.Player.IsDisabledForStackDetection = false;
if (args.Player.HasPermission(Permissions.usebanneditem))
args.Player.IsDisabledForBannedWearable = false;
args.Player.SendSuccessMessage(GetString("Authenticated as {0} successfully.", account.Name));
TShock.Log.ConsoleInfo(GetString("{0} authenticated successfully as user: {1}.", args.Player.Name, account.Name));
if ((args.Player.LoginHarassed) && (TShock.Config.Settings.RememberLeavePos))
{
if (TShock.RememberedPos.GetLeavePos(args.Player.Name, args.Player.IP) != Vector2.Zero)
{
Vector2 pos = TShock.RememberedPos.GetLeavePos(args.Player.Name, args.Player.IP);
args.Player.Teleport((int)pos.X * 16, (int)pos.Y * 16);
}
args.Player.LoginHarassed = false;
}
TShock.UserAccounts.SetUserAccountUUID(account, args.Player.UUID);
Hooks.PlayerHooks.OnPlayerPostLogin(args.Player);
}
else
{
if (usingUUID && !TShock.Config.Settings.DisableUUIDLogin)
{
args.Player.SendErrorMessage(GetString("UUID does not match this character."));
}
else
{
args.Player.SendErrorMessage(GetString("Invalid password."));
}
TShock.Log.Warn(GetString("{0} failed to authenticate as user: {1}.", args.Player.IP, account.Name));
args.Player.LoginAttempts++;
}
}
catch (Exception ex)
{
args.Player.SendErrorMessage(GetString("There was an error processing your login or authentication related request."));
TShock.Log.Error(ex.ToString());
}
}
private static void Logout(CommandArgs args)
{
if (!args.Player.IsLoggedIn)
{
args.Player.SendErrorMessage(GetString("You are not logged-in. Therefore, you cannot logout."));
return;
}
if (args.Player.TPlayer.talkNPC != -1)
{
args.Player.SendErrorMessage(GetString("Please close NPC windows before logging out."));
return;
}
args.Player.Logout();
args.Player.SendSuccessMessage(GetString("You have been successfully logged out of your account."));
if (Main.ServerSideCharacter)
{
args.Player.SendWarningMessage(GetString("Server side characters are enabled. You need to be logged-in to play."));
}
}
private static void PasswordUser(CommandArgs args)
{
try
{
if (args.Player.IsLoggedIn && args.Parameters.Count == 2)
{
string password = args.Parameters[0];
if (args.Player.Account.VerifyPassword(password))
{
try
{
args.Player.SendSuccessMessage(GetString("You have successfully changed your password."));
TShock.UserAccounts.SetUserAccountPassword(args.Player.Account, args.Parameters[1]); // SetUserPassword will hash it for you.
TShock.Log.ConsoleInfo(GetString("{0} ({1}) changed the password for account {2}.", args.Player.IP, args.Player.Name, args.Player.Account.Name));
}
catch (ArgumentOutOfRangeException)
{
args.Player.SendErrorMessage(GetString("Password must be greater than or equal to {0} characters.", TShock.Config.Settings.MinimumPasswordLength));
}
}
else
{
args.Player.SendErrorMessage(GetString("You failed to change your password."));
TShock.Log.ConsoleInfo(GetString("{0} ({1}) failed to change the password for account {2}.", args.Player.IP, args.Player.Name, args.Player.Account.Name));
}
}
else
{
args.Player.SendErrorMessage(GetString("Not logged in or Invalid syntax. Proper syntax: {0}password <oldpassword> <newpassword>.", Specifier));
}
}
catch (UserAccountManagerException ex)
{
args.Player.SendErrorMessage(GetString("Sorry, an error occurred: {0}.", ex.Message));
TShock.Log.ConsoleError(GetString("PasswordUser returned an error: {0}.", ex));
}
}