/// <summary> /// Adds a command to the list of known commands /// </summary> /// <param name="c"></param> /// <returns></returns> public static bool AddCommand(CommandData c) { if (Commands.ContainsKey(c.CommandName)) { return false; } Commands.Add(c.CommandName, c); if (c.CommandGroup == "" || c.CommandGroup == null) { c.CommandGroup = "General"; } List<CommandData> cg = null; if (!CommandsByGroup.TryGetValue(c.CommandGroup, out cg)) { cg = new List<CommandData>(); CommandsByGroup.Add(c.CommandGroup, cg); } cg.Add(c); return true; }
private static bool CanExecute(string[] RolesHave, string executor, CommandData c, string[] parms, ref string msg) { if (Array.IndexOf(RolesHave, "Administrator") < 0) { bool allowed = false; for (int i = 0; i < c.AllowedRoles.Length; i++) { if (Array.IndexOf(RolesHave, c.AllowedRoles[i]) > -1) { allowed = true; break; } } if (!allowed) { Log1.Logger("Server.Commands").Warn("[" +executor + "] attempted executing command [" + c.CommandName + "] without adequate permissions."); msg = "Not enough permissions for that command."; return false; } } if (parms.Length == 0 || parms.Length-1 != c.ParmNames.Length) { msg = "Not enough parameters. " + c.UsageHelp; return false; } return true; }
private static bool CanExecute(ServerUser user, CommandData c, string[] parms, ref string msg) { return CanExecute(user.Profile.UserRoles, user.AccountName, c, parms, ref msg); }
public static void LoadFromConfig() { try { CommandConfigSection section = (CommandConfigSection)ConfigurationManager.GetSection("ServerCommands"); if (section != null) { foreach (CommandElement cmd in section.CommandItems) { CommandData c = new CommandData(); try { List<string> roles = new List<string>(); c.CommandName = cmd.CommandName; string blob = cmd.AllowedRoles; string[] parts = blob.Split(char.Parse("|")); for (int i = 0; i < parts.Length; i++) { if (parts[i].Trim().Length > 0) { roles.Add(parts[i].Trim()); } } if (roles.IndexOf("Administrator") == -1) { roles.Add("Administrator"); } c.AllowedRoles = roles.ToArray(); c.AssemblyName = cmd.Assembly; c.ClassName = cmd.ClassName; c.MethodName = cmd.MethodName; List<string> parms = new List<string>(); blob = cmd.ParmNames; parts = blob.Split(char.Parse("|")); for (int i = 0; i < parts.Length; i++) { if (parts[i].Trim().Length > 0) { parms.Add(parts[i].Trim()); } } c.ParmNames = parms.ToArray(); c.UsageHelp = cmd.UsageHelp; c.CommandGroup = cmd.CommandGroup; AddCommand(c); Log1.Logger("Server.Commands").Info("Loaded data for server command [" + c.CommandName + "]."); } catch (Exception e) { string cmdt = ""; if (c.CommandName != null && c.CommandName.Length > 0) { cmdt = c.CommandName; } else { cmdt = "Unknown Command Name"; } Log1.Logger("Server.Commands").Error("Unable to read Server command from config file. [" + cmdt + "]. " + e.Message, e); } } } } catch (Exception fatl) { Log1.Logger("Server.Commands").Error("Unable to load Server commands from config file. " + fatl.Message, fatl); } }