static XElement DefineDefaultRanks() { XElement permissions = new XElement("Ranks"); XElement owner = new XElement("Rank"); owner.Add(new XAttribute("id", RankManager.GenerateID())); owner.Add(new XAttribute("name", "owner")); owner.Add(new XAttribute("rank", 100)); owner.Add(new XAttribute("color", "red")); owner.Add(new XAttribute("prefix", "+")); owner.Add(new XAttribute("drawLimit", 0)); owner.Add(new XAttribute("antiGriefBlocks", 0)); owner.Add(new XAttribute("antiGriefSeconds", 0)); owner.Add(new XAttribute("idleKickAfter", 0)); owner.Add(new XAttribute("reserveSlot", true)); owner.Add(new XAttribute("allowSecurityCircumvention", true)); owner.Add(new XElement(Permission.Chat.ToString())); owner.Add(new XElement(Permission.Build.ToString())); owner.Add(new XElement(Permission.Delete.ToString())); owner.Add(new XElement(Permission.UseSpeedHack.ToString())); owner.Add(new XElement(Permission.UseColorCodes.ToString())); owner.Add(new XElement(Permission.PlaceGrass.ToString())); owner.Add(new XElement(Permission.PlaceWater.ToString())); owner.Add(new XElement(Permission.PlaceLava.ToString())); owner.Add(new XElement(Permission.PlaceAdmincrete.ToString())); owner.Add(new XElement(Permission.DeleteAdmincrete.ToString())); owner.Add(new XElement(Permission.Say.ToString())); owner.Add(new XElement(Permission.ReadStaffChat.ToString())); XElement temp = new XElement(Permission.Kick.ToString()); temp.Add(new XAttribute("max", "owner")); owner.Add(temp); temp = new XElement(Permission.Ban.ToString()); temp.Add(new XAttribute("max", "owner")); owner.Add(temp); owner.Add(new XElement(Permission.BanIP.ToString())); owner.Add(new XElement(Permission.BanAll.ToString())); temp = new XElement(Permission.Promote.ToString()); temp.Add(new XAttribute("max", "owner")); owner.Add(temp); temp = new XElement(Permission.Demote.ToString()); temp.Add(new XAttribute("max", "owner")); owner.Add(temp); owner.Add(new XElement(Permission.Hide.ToString())); owner.Add(new XElement(Permission.ViewOthersInfo.ToString())); owner.Add(new XElement(Permission.ViewPlayerIPs.ToString())); owner.Add(new XElement(Permission.EditPlayerDB.ToString())); owner.Add(new XElement(Permission.Teleport.ToString())); owner.Add(new XElement(Permission.Bring.ToString())); owner.Add(new XElement(Permission.BringAll.ToString())); owner.Add(new XElement(Permission.Patrol.ToString())); //owner.Add( new XElement( Permission.Spectate.ToString() ) ); owner.Add(new XElement(Permission.Freeze.ToString())); owner.Add(new XElement(Permission.Mute.ToString())); owner.Add(new XElement(Permission.SetSpawn.ToString())); owner.Add(new XElement(Permission.Lock.ToString())); owner.Add(new XElement(Permission.ManageZones.ToString())); owner.Add(new XElement(Permission.ManageWorlds.ToString())); owner.Add(new XElement(Permission.Import.ToString())); owner.Add(new XElement(Permission.Draw.ToString())); owner.Add(new XElement(Permission.CopyAndPaste.ToString())); owner.Add(new XElement(Permission.ReloadConfig.ToString())); owner.Add(new XElement(Permission.ShutdownServer.ToString())); permissions.Add(owner); try { RankManager.AddRank(new Rank(owner)); } catch (RankDefinitionException ex) { Logger.Log(ex.Message, LogType.Error); } XElement op = new XElement("Rank"); op.Add(new XAttribute("id", RankManager.GenerateID())); op.Add(new XAttribute("name", "op")); op.Add(new XAttribute("rank", 80)); op.Add(new XAttribute("color", "aqua")); op.Add(new XAttribute("prefix", "-")); op.Add(new XAttribute("drawLimit", 0)); op.Add(new XAttribute("antiGriefBlocks", 0)); op.Add(new XAttribute("antiGriefSeconds", 0)); op.Add(new XAttribute("idleKickAfter", 0)); op.Add(new XElement(Permission.Chat.ToString())); op.Add(new XElement(Permission.Build.ToString())); op.Add(new XElement(Permission.Delete.ToString())); op.Add(new XElement(Permission.UseSpeedHack.ToString())); op.Add(new XElement(Permission.UseColorCodes.ToString())); op.Add(new XElement(Permission.PlaceGrass.ToString())); op.Add(new XElement(Permission.PlaceWater.ToString())); op.Add(new XElement(Permission.PlaceLava.ToString())); op.Add(new XElement(Permission.PlaceAdmincrete.ToString())); op.Add(new XElement(Permission.DeleteAdmincrete.ToString())); op.Add(new XElement(Permission.Say.ToString())); op.Add(new XElement(Permission.ReadStaffChat.ToString())); temp = new XElement(Permission.Kick.ToString()); temp.Add(new XAttribute("max", "op")); op.Add(temp); temp = new XElement(Permission.Ban.ToString()); temp.Add(new XAttribute("max", "regular")); op.Add(temp); op.Add(new XElement(Permission.BanIP.ToString())); temp = new XElement(Permission.Promote.ToString()); temp.Add(new XAttribute("max", "regular")); op.Add(temp); temp = new XElement(Permission.Demote.ToString()); temp.Add(new XAttribute("max", "regular")); op.Add(temp); op.Add(new XElement(Permission.Hide.ToString())); op.Add(new XElement(Permission.ViewOthersInfo.ToString())); op.Add(new XElement(Permission.ViewPlayerIPs.ToString())); op.Add(new XElement(Permission.Teleport.ToString())); op.Add(new XElement(Permission.Bring.ToString())); op.Add(new XElement(Permission.Patrol.ToString())); //op.Add( new XElement( Permission.Spectate.ToString() ) ); op.Add(new XElement(Permission.Freeze.ToString())); op.Add(new XElement(Permission.Mute.ToString())); op.Add(new XElement(Permission.SetSpawn.ToString())); op.Add(new XElement(Permission.ManageZones.ToString())); op.Add(new XElement(Permission.Lock.ToString())); op.Add(new XElement(Permission.Draw.ToString())); op.Add(new XElement(Permission.CopyAndPaste.ToString())); permissions.Add(op); try { RankManager.AddRank(new Rank(op)); } catch (RankDefinitionException ex) { Logger.Log(ex.Message, LogType.Error); } XElement regular = new XElement("Rank"); regular.Add(new XAttribute("id", RankManager.GenerateID())); regular.Add(new XAttribute("name", "regular")); regular.Add(new XAttribute("rank", 30)); regular.Add(new XAttribute("color", "white")); regular.Add(new XAttribute("prefix", "")); regular.Add(new XAttribute("drawLimit", 4096)); regular.Add(new XAttribute("antiGriefBlocks", 47)); regular.Add(new XAttribute("antiGriefSeconds", 6)); regular.Add(new XAttribute("idleKickAfter", 20)); regular.Add(new XElement(Permission.Chat.ToString())); regular.Add(new XElement(Permission.Build.ToString())); regular.Add(new XElement(Permission.Delete.ToString())); regular.Add(new XElement(Permission.UseSpeedHack.ToString())); regular.Add(new XElement(Permission.PlaceGrass.ToString())); regular.Add(new XElement(Permission.PlaceWater.ToString())); regular.Add(new XElement(Permission.PlaceLava.ToString())); regular.Add(new XElement(Permission.PlaceAdmincrete.ToString())); regular.Add(new XElement(Permission.DeleteAdmincrete.ToString())); temp = new XElement(Permission.Kick.ToString()); temp.Add(new XAttribute("max", "regular")); regular.Add(temp); regular.Add(new XElement(Permission.ViewOthersInfo.ToString())); regular.Add(new XElement(Permission.Teleport.ToString())); regular.Add(new XElement(Permission.Draw.ToString())); permissions.Add(regular); try { RankManager.AddRank(new Rank(regular)); } catch (RankDefinitionException ex) { Logger.Log(ex.Message, LogType.Error); } XElement guest = new XElement("Rank"); guest.Add(new XAttribute("id", RankManager.GenerateID())); guest.Add(new XAttribute("name", "guest")); guest.Add(new XAttribute("rank", 0)); guest.Add(new XAttribute("color", "silver")); guest.Add(new XAttribute("prefix", "")); guest.Add(new XAttribute("drawLimit", 512)); guest.Add(new XAttribute("antiGriefBlocks", 37)); guest.Add(new XAttribute("antiGriefSeconds", 5)); guest.Add(new XAttribute("idleKickAfter", 20)); guest.Add(new XElement(Permission.Chat.ToString())); guest.Add(new XElement(Permission.Build.ToString())); guest.Add(new XElement(Permission.Delete.ToString())); guest.Add(new XElement(Permission.UseSpeedHack.ToString())); permissions.Add(guest); try { RankManager.AddRank(new Rank(guest)); } catch (RankDefinitionException ex) { Logger.Log(ex.Message, LogType.Error); } return(permissions); }
public Rank([NotNull] XElement el) : this() { if (el == null) { throw new ArgumentNullException("el"); } // Name XAttribute attr = el.Attribute("name"); if (attr == null) { throw new RankDefinitionException(null, "Rank definition with no name was ignored."); } else if (!IsValidRankName(attr.Value.Trim())) { throw new RankDefinitionException(Name, "Invalid name specified for rank \"{0}\". " + "Rank names can only contain letters, digits, and underscores. " + "Rank definition was ignored.", Name); } else { // duplicate Name check is done in RankManager.AddRank() Name = attr.Value.Trim(); } // ID attr = el.Attribute("id"); if (attr == null) { ID = RankManager.GenerateID(); Logger.Log(LogType.Warning, "Rank({0}): No ID specified; issued a new unique ID: {1}", Name, ID); } else if (!IsValidID(attr.Value.Trim())) { ID = RankManager.GenerateID(); Logger.Log(LogType.Warning, "Rank({0}): Invalid ID specified (must be alphanumeric, and exactly 16 characters long); issued a new unique ID: {1}", Name, ID); } else { ID = attr.Value.Trim(); // duplicate ID check is done in RankManager.AddRank() } FullName = Name + "#" + ID; // Color (optional) if ((attr = el.Attribute("color")) != null) { string color = fCraft.Color.Parse(attr.Value); if (color == null) { Logger.Log(LogType.Warning, "Rank({0}): Could not parse rank color. Assuming default (none).", Name); Color = fCraft.Color.White; } else { Color = color; } } else { Color = fCraft.Color.White; } // Prefix (optional) if ((attr = el.Attribute("prefix")) != null) { if (IsValidPrefix(attr.Value)) { Prefix = attr.Value; } else { Logger.Log(LogType.Warning, "Rank({0}): Invalid prefix format. Expecting 1 character.", Name); } } // AntiGrief block limit (assuming unlimited if not given) int value; XAttribute agBlocks = el.Attribute("antiGriefBlocks"); XAttribute agSeconds = el.Attribute("antiGriefSeconds"); if (agBlocks != null && agSeconds != null) { if (Int32.TryParse(agBlocks.Value, out value)) { if (value >= 0 && value < 1000) { AntiGriefBlocks = value; } else { Logger.Log(LogType.Warning, "Rank({0}): Value for antiGriefBlocks is not within valid range (0-1000). Assuming default ({1}).", Name, AntiGriefBlocks); } } else { Logger.Log(LogType.Warning, "Rank({0}): Could not parse the value for antiGriefBlocks. Assuming default ({1}).", Name, AntiGriefBlocks); } if (Int32.TryParse(agSeconds.Value, out value)) { if (value >= 0 && value < 100) { AntiGriefSeconds = value; } else { Logger.Log(LogType.Warning, "Rank({0}): Value for antiGriefSeconds is not within valid range (0-100). Assuming default ({1}).", Name, AntiGriefSeconds); } } else { Logger.Log(LogType.Warning, "Rank({0}): Could not parse the value for antiGriefSeconds. Assuming default ({1}).", Name, AntiGriefSeconds); } } // Draw command limit, in number-of-blocks (assuming unlimited if not given) if ((attr = el.Attribute("drawLimit")) != null) { if (Int32.TryParse(attr.Value, out value)) { if (value >= 0 && value < 100000000) { DrawLimit = value; } else { Logger.Log(LogType.Warning, "Rank({0}): Value for drawLimit is not within valid range (0-100000000). Assuming default ({1}).", Name, DrawLimit); } } else { Logger.Log(LogType.Warning, "Rank({0}): Could not parse the value for drawLimit. Assuming default ({1}).", Name, DrawLimit); } } // Idle kick timer, in minutes. (assuming 'never' if not given) if ((attr = el.Attribute("idleKickAfter")) != null) { if (!Int32.TryParse(attr.Value, out IdleKickTimer)) { Logger.Log(LogType.Warning, "Rank({0}): Could not parse the value for idleKickAfter. Assuming 0 (never).", Name); IdleKickTimer = 0; } } else { IdleKickTimer = 0; } // Reserved slot. (assuming 'no' if not given) if ((attr = el.Attribute("reserveSlot")) != null) { if (!Boolean.TryParse(attr.Value, out HasReservedSlot)) { Logger.Log(LogType.Warning, "Rank({0}): Could not parse value for reserveSlot. Assuming \"false\".", Name); HasReservedSlot = false; } } else { HasReservedSlot = false; } // Security circumvention. (assuming 'no' if not given) if ((attr = el.Attribute("allowSecurityCircumvention")) != null) { if (!Boolean.TryParse(attr.Value, out AllowSecurityCircumvention)) { Logger.Log(LogType.Warning, "Rank({0}): Could not parse the value for allowSecurityCircumvention. Assuming \"false\".", Name); AllowSecurityCircumvention = false; } } else { AllowSecurityCircumvention = false; } // Copy slots (assuming default 2 if not given) if ((attr = el.Attribute("copySlots")) != null) { if (Int32.TryParse(attr.Value, out value)) { if (value > 0 && value < 256) { CopySlots = value; } else { Logger.Log(LogType.Warning, "Rank({0}): Value for copySlots is not within valid range (1-255). Assuming default ({1}).", Name, CopySlots); } } else { Logger.Log(LogType.Warning, "Rank({0}): Could not parse the value for copySlots. Assuming default ({1}).", Name, CopySlots); } } // Fill limit (assuming default 32 if not given) if ((attr = el.Attribute("fillLimit")) != null) { if (Int32.TryParse(attr.Value, out value)) { if (value < 1) { Logger.Log(LogType.Warning, "Rank({0}): Value for fillLimit may not be negative. Assuming default ({1}).", Name, FillLimit); } else if (value > 2048) { FillLimit = 2048; } else { FillLimit = value; } } else { Logger.Log(LogType.Warning, "Rank({0}): Could not parse the value for fillLimit. Assuming default ({1}).", Name, FillLimit); } } // Permissions for (int i = 0; i < Enum.GetValues(typeof(Permission)).Length; i++) { string permission = ((Permission)i).ToString(); XElement temp; if ((temp = el.Element(permission)) != null) { Permissions[i] = true; if ((attr = temp.Attribute("max")) != null) { permissionLimitStrings[i] = attr.Value; } } } // check consistency of ban permissions if (!Can(Permission.Ban) && (Can(Permission.BanAll) || Can(Permission.BanIP))) { Logger.Log(LogType.Warning, "Rank({0}): Rank is allowed to BanIP and/or BanAll but not allowed to Ban. " + "Assuming that all ban permissions were meant to be off.", Name); Permissions[(int)Permission.BanIP] = false; Permissions[(int)Permission.BanAll] = false; } // check consistency of patrol permissions if (!Can(Permission.Teleport) && Can(Permission.Patrol)) { Logger.Log(LogType.Warning, "Rank({0}): Rank is allowed to Patrol but not allowed to Teleport. " + "Assuming that Patrol permission was meant to be off.", Name); Permissions[(int)Permission.Patrol] = false; } // check consistency of draw permissions if (!Can(Permission.Draw) && Can(Permission.DrawAdvanced)) { Logger.Log(LogType.Warning, "Rank({0}): Rank is allowed to DrawAdvanced but not allowed to Draw. " + "Assuming that DrawAdvanced permission was meant to be off.", Name); Permissions[(int)Permission.DrawAdvanced] = false; } // check consistency of Undo permissions if (!Can(Permission.UndoOthersActions) && Can(Permission.UndoAll)) { Logger.Log(LogType.Warning, "Rank({0}): Rank is allowed to UndoAll but not allowed to UndoOthersActions. " + "Assuming that UndoAll permission was meant to be off.", Name); Permissions[(int)Permission.UndoAll] = false; } }
public Rank(XElement el) : this() { if (el == null) { throw new ArgumentNullException("el"); } // Name XAttribute attr = el.Attribute("name"); if (attr == null) { throw new RankDefinitionException("Rank definition with no name was ignored."); } else if (!IsValidRankName(attr.Value.Trim())) { throw new RankDefinitionException("Invalid name specified for rank \"{0}\". " + "Rank names can only contain letters, digits, and underscores. " + "Rank definition was ignored.", Name); } else { // duplicate Name check is done in RankManager.AddRank() Name = attr.Value.Trim(); } // ID attr = el.Attribute("id"); if (attr == null) { Logger.Log("Rank({0}): Issued a new unique ID.", LogType.Warning, Name); ID = RankManager.GenerateID(); } else if (!IsValidID(attr.Value.Trim())) { throw new RankDefinitionException("Invalid ID specified for rank \"{0}\". " + "ID must be alphanumeric, and exactly 16 characters long. " + "Rank definition was ignored.", Name); } else { ID = attr.Value.Trim(); // duplicate ID check is done in RankManager.AddRank() } // Rank if ((attr = el.Attribute("rank")) != null) { Byte.TryParse(attr.Value, out LegacyNumericRank); } // Color (optional) if ((attr = el.Attribute("color")) != null) { if ((Color = fCraft.Color.Parse(attr.Value)) == null) { Logger.Log("Rank({0}): Could not parse rank color. Assuming default (none).", LogType.Warning, Name); Color = ""; } } else { Color = ""; } // Prefix (optional) if ((attr = el.Attribute("prefix")) != null) { if (IsValidPrefix(attr.Value)) { Prefix = attr.Value; } else { Logger.Log("Rank({0}): Invalid prefix format. Expecting 1 character.", LogType.Warning, Name); } } // AntiGrief block limit (assuming unlimited if not given) int value; if ((el.Attribute("antiGriefBlocks") != null) && (el.Attribute("antiGriefSeconds") != null)) { attr = el.Attribute("antiGriefBlocks"); if (Int32.TryParse(attr.Value, out value)) { if (value >= 0 && value < 1000) { AntiGriefBlocks = value; } else { Logger.Log("Rank({0}): Value for antiGriefBlocks is not within valid range (0-1000). Assuming default ({1}).", LogType.Warning, Name, AntiGriefBlocks); } } else { Logger.Log("Rank({0}): Could not parse the value for antiGriefBlocks. Assuming default ({1}).", LogType.Warning, Name, AntiGriefBlocks); } attr = el.Attribute("antiGriefSeconds"); if (Int32.TryParse(attr.Value, out value)) { if (value >= 0 && value < 100) { AntiGriefSeconds = value; } else { Logger.Log("Rank({0}): Value for antiGriefSeconds is not within valid range (0-100). Assuming default ({1}).", LogType.Warning, Name, AntiGriefSeconds); } } else { Logger.Log("Rank({0}): Could not parse the value for antiGriefSeconds. Assuming default ({1}).", LogType.Warning, Name, AntiGriefSeconds); } } // Draw command limit, in number-of-blocks (assuming unlimited if not given) if ((attr = el.Attribute("drawLimit")) != null) { if (Int32.TryParse(attr.Value, out value)) { if (value >= 0 && value < 100000000) { DrawLimit = value; } else { Logger.Log("Rank({0}): Value for drawLimit is not within valid range (0-100000000). Assuming default ({1}).", LogType.Warning, Name, DrawLimit); } } else { Logger.Log("Rank({0}): Could not parse the value for drawLimit. Assuming default ({1}).", LogType.Warning, Name, DrawLimit); } } // Idle kick timer, in minutes. (assuming 'never' if not given) if ((attr = el.Attribute("idleKickAfter")) != null) { if (!Int32.TryParse(attr.Value, out IdleKickTimer)) { Logger.Log("Rank({0}): Could not parse the value for idleKickAfter. Assuming 0 (never).", LogType.Warning, Name); IdleKickTimer = 0; } } else { IdleKickTimer = 0; } // Reserved slot. (assuming 'no' if not given) if ((attr = el.Attribute("reserveSlot")) != null) { if (!Boolean.TryParse(attr.Value, out ReservedSlot)) { Logger.Log("Rank({0}): Could not parse value for reserveSlot. Assuming \"false\".", LogType.Warning, Name); ReservedSlot = false; } } else { ReservedSlot = false; } // Security circumvention. (assuming 'no' if not given) if ((attr = el.Attribute("allowSecurityCircumvention")) != null) { if (!Boolean.TryParse(attr.Value, out AllowSecurityCircumvention)) { Logger.Log("Rank({0}): Could not parse the value for allowSecurityCircumvention. Assuming \"false\".", LogType.Warning, Name); AllowSecurityCircumvention = false; } } else { AllowSecurityCircumvention = false; } // Permissions XElement temp; for (int i = 0; i < Enum.GetValues(typeof(Permission)).Length; i++) { string permission = ((Permission)i).ToString(); if ((temp = el.Element(permission)) != null) { Permissions[i] = true; if ((attr = temp.Attribute("max")) != null) { PermissionLimitStrings[i] = attr.Value; } } } // check consistency of ban permissions if (!Can(Permission.Ban) && (Can(Permission.BanAll) || Can(Permission.BanIP))) { Logger.Log("Rank({0}): Rank is allowed to BanIP and/or BanAll but not allowed to Ban. " + "Assuming that all ban permissions were meant to be off.", LogType.Warning, Name); Permissions[(int)Permission.BanIP] = false; Permissions[(int)Permission.BanAll] = false; } // check consistency of pantrol permissions if (!Can(Permission.Teleport) && Can(Permission.Patrol)) { Logger.Log("Rank({0}): Rank is allowed to Patrol but not allowed to Teleport. " + "Assuming that Patrol permission was meant to be off.", LogType.Warning, Name); Permissions[(int)Permission.Patrol] = false; } }