Ejemplo n.º 1
0
        internal static void ThrowPermissionMissing([NotNull] Player player,
                                                    [NotNull] string action, [NotNull] params Permission[] permissions)
        {
            if (player == null)
            {
                throw new ArgumentNullException(nameof(player));
            }
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }
            if (permissions == null)
            {
                throw new ArgumentNullException(nameof(permissions));
            }
            Rank   minRank = RankManager.GetMinRankWithAllPermissions(permissions);
            string msg, colorMsg;

            if (minRank != null)
            {
                msg      = $"You need to be ranked {minRank.Name}+ to {action}.";
                colorMsg = $"&SYou need to be ranked {minRank.ClassyName}&S+ to {action}.";
            }
            else
            {
                msg      = $"No one is allowed to {action} on this server.";
                colorMsg = $"&SNo one is allowed to {action} on this server.";
            }

            throw new PlayerOpException(player, PlayerOpExceptionCode.PermissionMissing, msg, colorMsg);
        }
Ejemplo n.º 2
0
        internal static void ThrowPermissionMissing([NotNull] Player player, [CanBeNull] PlayerInfo target,
                                                    [NotNull] string action, [NotNull] params Permission[] permissions)
        {
            if (player == null)
            {
                throw new ArgumentNullException("player");
            }
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }
            if (permissions == null)
            {
                throw new ArgumentNullException("permissions");
            }
            Rank   minRank = RankManager.GetMinRankWithAllPermissions(permissions);
            string msg, colorMsg;

            if (minRank != null)
            {
                msg = String.Format("You need to be ranked {0}+ to {1}.",
                                    minRank.Name, action);
                colorMsg = String.Format("&SYou need to be ranked {0}&S+ to {1}.",
                                         minRank.ClassyName, action);
            }
            else
            {
                msg      = String.Format("No one is allowed to {0} on this server.", action);
                colorMsg = String.Format("&SNo one is allowed to {0} on this server.", action);
            }

            throw new PlayerOpException(player, target, PlayerOpExceptionCode.PermissionMissing, msg, colorMsg);
        }
Ejemplo n.º 3
0
        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 = Utils.Color.Parse(attr.Value);
                if (color == null)
                {
                    Logger.Log(LogType.Warning,
                               "Rank({0}): Could not parse rank color. Assuming default (none).", Name);
                    Color = Utils.Color.White;
                }
                else
                {
                    Color = color;
                }
            }
            else
            {
                Color = Utils.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 ReservedSlot))
                {
                    Logger.Log(LogType.Warning,
                               "Rank({0}): Could not parse value for reserveSlot. Assuming \"false\".", 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(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
            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(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 Draw permission were meant to be off.", Name);
                Permissions[(int)Permission.DrawAdvanced] = false;
            }
        }
Ejemplo n.º 4
0
        public Rank([NotNull] JsonObject json) : this()
        {
            if (json == null)
            {
                throw new ArgumentNullException(nameof(json));
            }

            // Name
            string name = json["name"];

            if (name == null)
            {
                throw new RankDefinitionException(null, "Rank definition with no name was ignored.");
            }

            if (!IsValidRankName(name.Trim()))
            {
                throw new RankDefinitionException(Name,
                                                  $"Invalid name specified for rank \"{Name}\". " +
                                                  "Rank names can only contain letters, digits, and underscores. " +
                                                  "Rank defintion was ignored.");
            }
            else
            {
                // duplicate Name check is done in RankManager.AddRank()
                Name = name.Trim();
            }

            // ID
            string id = json["id"];

            if (id == null)
            {
                ID = RankManager.GenerateID();
                Logger.Log(LogType.Warning,
                           $"Rank({Name}): No ID specified; issued a new unique ID: {ID}");
            }
            else if (!IsValidID(id.Trim()))
            {
                ID = RankManager.GenerateID();
                Logger.Log(LogType.Warning,
                           $"Rank({Name}): Invalid ID specified (must be alphanumeric and exactly 16 characters long); " +
                           $"issued a new unique ID: {ID}");
            }
            else
            {
                ID = id.Trim();
                // duplicate ID check is done in RankManager.AddRank()
            }

            FullName = Name + "#" + ID;

            // Color (optional)
            string color = json["color"];

            if (color != null)
            {
                string cColor = ChatColor.Parse(color);
                if (cColor == null)
                {
                    Logger.Log(LogType.Warning,
                               $"Rank({Name}): Could not parse rank color. Assuming default.");
                    Color = ChatColor.White;
                }
                else
                {
                    Color = cColor;
                }
            }
            else
            {
                Color = ChatColor.White;
            }

            // Prefix (Optional)
            string prefix = json["prefix"];

            if (prefix != null)
            {
                if (IsValidPrefix(prefix))
                {
                    Prefix = prefix;
                }
                else
                {
                    Logger.Log(LogType.Warning,
                               $"Rank({Name}): Invalid prefix format. Expecting 1 character.");
                }
            }

            // AntiGrief block limit (assuming unlimited if not given)
            int value;
            int agBlocks  = json["antiGriefBlocks"];
            int agSeconds = json["antiGriefSeconds"];

            if (int.TryParse(json["antiGriefBlocks"], out value))
            {
                if (value >= 0 && value < 1000)
                {
                    AntiGriefBlocks = value;
                }
                else
                {
                    Logger.Log(LogType.Warning,
                               $"Rank({Name}): Value for antiGriefBlocks is not within " +
                               $"valid range (0-1000). Assuming default ({AntiGriefBlocks})");
                }
            }

            if (int.TryParse(json["antiGriefSeconds"], out value))
            {
                if (value >= 0 && value < 100)
                {
                    AntiGriefSeconds = value;
                }
                else
                {
                    Logger.Log(LogType.Warning,
                               $"Rank({Name}): Value for antiGriefSeconds is not within valid range (0-100). Assuming default ({AntiGriefSeconds}).");
                }
            }
            else
            {
                Logger.Log(LogType.Warning,
                           $"Rank({Name}): Could not parse the value for antiGriefSeconds. Assuming default ({AntiGriefSeconds}).");
            }

            // Draw command limit, in number-of-blocks (assuming unlimited if not given)
            if (int.TryParse(json["drawLimit"], 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 (!int.TryParse(json["idleKickAfter"], 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 (!bool.TryParse(json["reserveSlot"], out ReservedSlot))
            {
                Logger.Log(LogType.Warning,
                           "Rank({0}): Could not parse value for reserveSlot. Assuming \"false\".", Name);
                ReservedSlot = false;
            }
            else
            {
                ReservedSlot = false;
            }

            // Security circumvention (assuming 'no' if not given)
            if (!bool.TryParse(json["allowSecurityCircumvention"], 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 (int.TryParse(json["copySlots"], 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 (int.TryParse(json["fillLimit"], 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
            JsonArray foundPerms = json["permissions"];

            for (int i = 0; i < Enum.GetValues(typeof(Permission)).Length; i++)
            {
                PermissionJsonObject pObj = (PermissionJsonObject)foundPerms[i].AsObject;
                if (pObj.PermissionValue != i)
                {
                    continue;
                }
                Permissions[i] = true;
                string val = pObj.PerissionLimitString;
                if (!val.StartsWith("max:"))
                {
                    continue;
                }
                const int startVal = 5;
                string    item     = val.Substring(startVal);
                PermissionLimitStrings[i] = item;
            }

            // Check consistency of ban permissions
        }