Ejemplo n.º 1
0
        public SecurityController([NotNull] XContainer el, bool parseExceptions)
        {
            if (el == null)
            {
                throw new ArgumentNullException("el");
            }
            XElement minRankEl = el.Element("minRank");

            if (minRankEl != null)
            {
                minRank = Rank.Parse(minRankEl.Value);
            }
            else
            {
                minRank = null;
            }

            if (parseExceptions)
            {
                foreach (XElement player in el.Elements("included"))
                {
                    if (!Player.IsValidPlayerName(player.Value))
                    {
                        continue;
                    }
                    PlayerInfo info = PlayerDB.FindPlayerInfoExact(player.Value);
                    if (info != null)
                    {
                        Include(info);
                    }
                }

                foreach (XElement player in el.Elements("excluded"))
                {
                    if (!Player.IsValidPlayerName(player.Value))
                    {
                        continue;
                    }
                    PlayerInfo info = PlayerDB.FindPlayerInfoExact(player.Value);
                    if (info != null)
                    {
                        Exclude(info);
                    }
                }
            }
            else
            {
                rawExceptions = el.Elements("included").Union(el.Elements("excluded")).ToArray();
            }
            UpdatePlayerListCache();
        }
Ejemplo n.º 2
0
        public Zone([NotNull] string raw, [CanBeNull] World world)
            : this()
        {
            if (raw == null)
            {
                throw new ArgumentNullException("raw");
            }
            string[] parts = raw.Split(',');

            string[] header = parts[0].Split(' ');
            Name   = header[0];
            Bounds = new BoundingBox(Int32.Parse(header[1]),
                                     Int32.Parse(header[2]),
                                     Int32.Parse(header[3]),
                                     Int32.Parse(header[4]),
                                     Int32.Parse(header[5]),
                                     Int32.Parse(header[6]));

            // If no ranks are loaded (e.g. MapConverter/MapRenderer)(
            if (RankManager.Ranks.Count > 0)
            {
                Rank buildRank = Rank.Parse(header[7]);
                // if all else fails, fall back to lowest class
                if (buildRank == null)
                {
                    if (world != null)
                    {
                        Controller.MinRank = world.BuildSecurity.MinRank;
                    }
                    else
                    {
                        Controller.ResetMinRank();
                    }
                    Logger.Log(LogType.Error,
                               "Zone: Error parsing zone definition: unknown rank \"{0}\". Permission reset to default ({1}).",
                               header[7],
                               Controller.MinRank.Name);
                }
                else
                {
                    Controller.MinRank = buildRank;
                }
            }

            // If PlayerDB is not loaded (e.g. ConfigGUI)
            if (PlayerDB.IsLoaded)
            {
                // Part 2:
                if (parts[1].Length > 0)
                {
                    foreach (string playerName in parts[1].Split(' '))
                    {
                        if (!Player.IsValidPlayerName(playerName))
                        {
                            Logger.Log(LogType.Warning,
                                       "Invalid entry in zone \"{0}\" whitelist: {1}",
                                       Name,
                                       playerName);
                            continue;
                        }
                        PlayerInfo info = PlayerDB.FindPlayerInfoExact(playerName);
                        if (info == null)
                        {
                            Logger.Log(LogType.Warning,
                                       "Unrecognized player in zone \"{0}\" whitelist: {1}",
                                       Name,
                                       playerName);
                            continue; // player name not found in the DB (discarded)
                        }
                        Controller.Include(info);
                    }
                }

                // Part 3: excluded list
                if (parts[2].Length > 0)
                {
                    foreach (string playerName in parts[2].Split(' '))
                    {
                        if (!Player.IsValidPlayerName(playerName))
                        {
                            Logger.Log(LogType.Warning,
                                       "Invalid entry in zone \"{0}\" blacklist: {1}",
                                       Name,
                                       playerName);
                            continue;
                        }
                        PlayerInfo info = PlayerDB.FindPlayerInfoExact(playerName);
                        if (info == null)
                        {
                            Logger.Log(LogType.Warning,
                                       "Unrecognized player in zone \"{0}\" whitelist: {1}",
                                       Name,
                                       playerName);
                            continue; // player name not found in the DB (discarded)
                        }
                        Controller.Exclude(info);
                    }
                }
            }
            else
            {
                RawWhitelist = parts[1];
                RawBlacklist = parts[2];
            }

            // Part 4: extended header
            if (parts.Length > 3)
            {
                string[] xheader = parts[3].Split(' ');
                if (xheader[0] == "-")
                {
                    CreatedBy   = null;
                    CreatedDate = DateTime.MinValue;
                }
                else
                {
                    CreatedBy   = xheader[0];
                    CreatedDate = DateTime.Parse(xheader[1]);
                }

                if (xheader[2] == "-")
                {
                    EditedBy   = null;
                    EditedDate = DateTime.MinValue;
                }
                else
                {
                    EditedBy   = xheader[2];
                    EditedDate = DateTime.Parse(xheader[3]);
                }
            }
        }
Ejemplo n.º 3
0
        public Zone([NotNull] string raw, [CanBeNull] World world)
            : this()
        {
            if (raw == null)
            {
                throw new ArgumentNullException("raw");
            }
            string[] parts = raw.Split(',');

            string[] header = parts[0].Split(' ');
            Name   = header[0];
            Bounds = new BoundingBox(Int32.Parse(header[1]), Int32.Parse(header[2]), Int32.Parse(header[3]),
                                     Int32.Parse(header[4]), Int32.Parse(header[5]), Int32.Parse(header[6]));

            Rank buildRank = Rank.Parse(header[7]);


            if (header.Length > 8)
            {
                // Part 5: Zone color
                try {
                    bool zoneShow;
                    if (bool.TryParse(header[8], out zoneShow))
                    {
                        ShowZone = zoneShow;
                    }
                    Color = header[9];
                    short zoneAlpha;
                    if (short.TryParse(header[10], out zoneAlpha))
                    {
                        Alpha = zoneAlpha;
                    }
                } catch (Exception ex) {
                    Logger.Log(LogType.Error, "Could not load Zone Colors for {0}", Name);
                    Logger.Log(LogType.Error, ex.StackTrace);
                }
            }

            if (header[0].Contains(SpecialZone.Door))
            {
                buildRank = RankManager.DefaultRank;
            }
            // if all else fails, fall back to lowest class... ignore door instances
            if (buildRank == null && !header[0].Contains(SpecialZone.Door))
            {
                if (world != null)
                {
                    Controller.MinRank = world.BuildSecurity.MinRank;
                }
                else
                {
                    Controller.ResetMinRank();
                }
                Logger.Log(LogType.Error,
                           "Zone: Error parsing zone definition: unknown rank \"{0}\". Permission reset to default ({1}). Ignore this message if you have recently changed rank permissions.",
                           header[7], Controller.MinRank.Name);
            }
            else
            {
                Controller.MinRank = buildRank;
            }

            if (PlayerDB.IsLoaded)
            {
                // Part 2:
                if (parts[1].Length > 0)
                {
                    foreach (string playerName in parts[1].Split(' '))
                    {
                        if (!Player.IsValidPlayerName(playerName))
                        {
                            Logger.Log(LogType.Warning,
                                       "Invalid entry in zone \"{0}\" whitelist: {1}", Name, playerName);
                            continue;
                        }
                        PlayerInfo info = PlayerDB.FindPlayerInfoExact(playerName);
                        if (info == null)
                        {
                            Logger.Log(LogType.Warning,
                                       "Unrecognized player in zone \"{0}\" whitelist: {1}", Name, playerName);
                            continue; // player name not found in the DB (discarded)
                        }
                        Controller.Include(info);
                    }
                }

                // Part 3: excluded list
                if (parts[2].Length > 0)
                {
                    foreach (string playerName in parts[2].Split(' '))
                    {
                        if (!Player.IsValidPlayerName(playerName))
                        {
                            Logger.Log(LogType.Warning,
                                       "Invalid entry in zone \"{0}\" blacklist: {1}", Name, playerName);
                            continue;
                        }
                        PlayerInfo info = PlayerDB.FindPlayerInfoExact(playerName);
                        if (info == null)
                        {
                            Logger.Log(LogType.Warning,
                                       "Unrecognized player in zone \"{0}\" whitelist: {1}", Name, playerName);
                            continue; // player name not found in the DB (discarded)
                        }
                        Controller.Exclude(info);
                    }
                }
            }
            else
            {
                RawWhitelist = parts[1];
                RawBlacklist = parts[2];
            }

            // Part 4: extended header
            if (parts.Length > 3)
            {
                string[] xheader = parts[3].Split(' ');
                if (xheader[0] == "-")
                {
                    CreatedBy   = null;
                    CreatedDate = DateTime.MinValue;
                }
                else
                {
                    CreatedBy   = xheader[0];
                    CreatedDate = DateTime.Parse(xheader[1]);
                }

                if (xheader[2] == "-")
                {
                    EditedBy   = null;
                    EditedDate = DateTime.MinValue;
                }
                else
                {
                    EditedBy   = xheader[2];
                    EditedDate = DateTime.Parse(xheader[3]);
                }
            }
        }
Ejemplo n.º 4
0
        internal static PlayerInfo LoadFormat2([NotNull] string[] fields)
        {
            if (fields.Length < 44)
            {
                throw new FormatException("PlayerInfo record did not contain all the expected information. " +
                                          "This record, or maybe the whole file, may be corrupted.");
            }

            if (!Player.IsValidPlayerName(fields[0]))
            {
                throw new FormatException("Unacceptable player name");
            }
            PlayerInfo info = new PlayerInfo {
                Name = fields[0]
            };

            if (fields[1].Length == 0 || !IPAddress.TryParse(fields[1], out info.LastIP))
            {
                info.LastIP = IPAddress.None;
            }

            info.Rank = Rank.Parse(fields[2]) ?? RankManager.DefaultRank;
            DateTimeUtil.TryParseDateTime(fields[3], ref info.RankChangeDate);
            if (fields[4].Length > 0)
            {
                info.RankChangedBy = PlayerDB.Unescape(fields[4]);
            }

            switch (fields[5])
            {
            case "b":
                info.BanStatus = BanStatus.Banned;
                break;

            case "x":
                info.BanStatus = BanStatus.IPBanExempt;
                break;

            default:
                info.BanStatus = BanStatus.NotBanned;
                break;
            }

            // ban information
            if (DateTimeUtil.TryParseDateTime(fields[6], ref info.BanDate))
            {
                if (fields[7].Length > 0)
                {
                    info.BannedBy = PlayerDB.Unescape(fields[7]);
                }
                if (fields[10].Length > 0)
                {
                    info.BanReason = PlayerDB.Unescape(fields[10]);
                }
            }

            // unban information
            if (DateTimeUtil.TryParseDateTime(fields[8], ref info.UnbanDate))
            {
                if (fields[9].Length > 0)
                {
                    info.UnbannedBy = PlayerDB.Unescape(fields[9]);
                }
                if (fields[11].Length > 0)
                {
                    info.UnbanReason = PlayerDB.Unescape(fields[11]);
                }
            }

            // failed logins
            DateTimeUtil.TryParseDateTime(fields[12], ref info.LastFailedLoginDate);

            if (fields[13].Length < 1 || !IPAddress.TryParse(fields[13], out info.LastFailedLoginIP))
            {
                // LEGACY
                info.LastFailedLoginIP = IPAddress.None;
            }
            // skip 14

            DateTimeUtil.TryParseDateTime(fields[15], ref info.FirstLoginDate);

            // login/logout times
            DateTimeUtil.TryParseDateTime(fields[16], ref info.LastLoginDate);
            DateTimeUtil.TryParseTimeSpan(fields[17], out info.TotalTime);

            // stats
            if (fields[18].Length > 0)
            {
                Int32.TryParse(fields[18], NumberStyles.Integer, NumberFormatter, out info.BlocksBuilt);
            }
            if (fields[19].Length > 0)
            {
                Int32.TryParse(fields[19], NumberStyles.Integer, NumberFormatter, out info.BlocksDeleted);
            }
            if (fields[20].Length > 0)
            {
                Int32.TryParse(fields[20], NumberStyles.Integer, NumberFormatter, out info.TimesVisited);
            }
            if (fields[21].Length > 0)
            {
                Int32.TryParse(fields[21], NumberStyles.Integer, NumberFormatter, out info.MessagesWritten);
            }
            // fields 22-23 are no longer in use

            if (fields[24].Length > 0)
            {
                info.PreviousRank = Rank.Parse(fields[24]);
            }
            if (fields[25].Length > 0)
            {
                info.RankChangeReason = PlayerDB.Unescape(fields[25]);
            }
            Int32.TryParse(fields[26], NumberStyles.Integer, NumberFormatter, out info.TimesKicked);
            Int32.TryParse(fields[27], NumberStyles.Integer, NumberFormatter, out info.TimesKickedOthers);
            Int32.TryParse(fields[28], NumberStyles.Integer, NumberFormatter, out info.TimesBannedOthers);

            info.Id = Int32.Parse(fields[29]);
            if (info.Id < 256)
            {
                info.Id = PlayerDB.GetNextId();
            }

            byte rankChangeTypeCode;

            if (Byte.TryParse(fields[30], out rankChangeTypeCode))
            {
                info.RankChangeType = (RankChangeType)rankChangeTypeCode;
                switch (info.RankChangeType)
                {
                case RankChangeType.AutoDemoted:
                case RankChangeType.AutoPromoted:
                case RankChangeType.Default:
                case RankChangeType.Demoted:
                case RankChangeType.Promoted:
                    break;

                default:
                    info.GuessRankChangeType();
                    break;
                }
            }
            else
            {
                info.GuessRankChangeType();
            }

            DateTimeUtil.TryParseDateTime(fields[31], ref info.LastKickDate);
            if (!DateTimeUtil.TryParseDateTime(fields[32], ref info.LastSeen) || info.LastSeen < info.LastLoginDate)
            {
                info.LastSeen = info.LastLoginDate;
            }
            Int64.TryParse(fields[33], NumberStyles.Integer, NumberFormatter, out info.BlocksDrawn);

            if (fields[34].Length > 0)
            {
                info.LastKickBy = PlayerDB.Unescape(fields[34]);
            }
            if (fields[35].Length > 0)
            {
                info.LastKickReason = PlayerDB.Unescape(fields[35]);
            }

            DateTimeUtil.TryParseDateTime(fields[36], ref info.BannedUntil);
            info.IsFrozen = (fields[37] == "f");
            if (fields[38].Length > 0)
            {
                info.FrozenBy = PlayerDB.Unescape(fields[38]);
            }
            DateTimeUtil.TryParseDateTime(fields[39], ref info.FrozenOn);
            DateTimeUtil.TryParseDateTime(fields[40], ref info.MutedUntil);
            if (fields[41].Length > 0)
            {
                info.MutedBy = PlayerDB.Unescape(fields[41]);
            }
            info.Password = PlayerDB.Unescape(fields[42]);
            // fields[43] is "online", and is ignored

            byte bandwidthUseModeCode;

            if (Byte.TryParse(fields[44], NumberStyles.Integer, NumberFormatter, out bandwidthUseModeCode))
            {
                info.BandwidthUseMode = (BandwidthUseMode)bandwidthUseModeCode;
                switch (info.BandwidthUseMode)
                {
                case BandwidthUseMode.High:
                case BandwidthUseMode.Low:
                case BandwidthUseMode.Normal:
                case BandwidthUseMode.VeryHigh:
                case BandwidthUseMode.VeryLow:
                    break;

                default:
                    info.BandwidthUseMode = BandwidthUseMode.Default;
                    break;
                }
            }
            else
            {
                info.BandwidthUseMode = BandwidthUseMode.Default;
            }

            if (fields.Length > 45 && fields[45] == "h")
            {
                info.IsHidden = info.Rank.Can(Permission.Hide);
            }
            if (fields.Length > 46)
            {
                DateTimeUtil.TryParseDateTime(fields[46], ref info.LastModified);
            }
            if (fields.Length > 47 && fields[47].Length > 0)
            {
                info.DisplayedName = PlayerDB.Unescape(fields[47]);
            }
            if (fields.Length > 48)
            {
                byte accountTypeCode;
                if (Byte.TryParse(fields[48], NumberStyles.Integer, NumberFormatter, out accountTypeCode))
                {
                    info.AccountType = (AccountType)accountTypeCode;
                    if (!Enum.IsDefined(typeof(AccountType), accountTypeCode))
                    {
                        info.AccountType = AccountType.Unknown;
                    }
                }
            }
            if (fields.Length > 49 && fields[49].Length > 0)
            {
                info.Email = PlayerDB.Unescape(fields[49]);
            }

            if (fields.Length > 50 && fields[50].Length > 0)
            {
                info.AltNames = fields[50].Split('|');
            }

            // date consistency checks
            if (info.LastSeen < info.FirstLoginDate)
            {
                info.LastSeen = info.FirstLoginDate;
            }
            if (info.LastLoginDate < info.FirstLoginDate)
            {
                info.LastLoginDate = info.FirstLoginDate;
            }

            return(info);
        }