/// <summary> Creates a SecurityController based on a XML serialised object. </summary> /// <param name="el"> XML element that contains the serialized settings. </param> /// <param name="parseExceptions"> Whether or not the the player exception list should be parsed. /// If PlayerDB is not loaded, exceptions should NOT be parsed - otherwise they will not be preserved. </param> public SecurityController([NotNull] XContainer el, bool parseExceptions) { if (el == null) { throw new ArgumentNullException("el"); } XElement tempEl; if ((tempEl = el.Element("minRank")) != null) { minRank = Rank.Parse(tempEl.Value); } if (parseExceptions) { foreach (XElement player in el.Elements("included")) { if (!Player.IsValidName(player.Value)) { continue; } PlayerInfo info = PlayerDB.FindExact(player.Value); if (info != null) { Include(info); } } foreach (XElement player in el.Elements("excluded")) { if (!Player.IsValidName(player.Value)) { continue; } PlayerInfo info = PlayerDB.FindExact(player.Value); if (info != null) { Exclude(info); } } } else { rawExceptions = el.Elements("included").Union(el.Elements("excluded")).ToArray(); } UpdatePlayerListCache(); }
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 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.IsLoaded) { // Part 2: foreach (string playerName in parts[1].Split(' ')) { if (!Player.IsValidName(playerName)) { Logger.Log(LogType.Warning, "Invalid entry in zone \"{0}\" whitelist: {1}", Name, playerName); continue; } PlayerInfo info = PlayerDB.FindExact(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 foreach (string playerName in parts[2].Split(' ')) { if (!Player.IsValidName(playerName)) { Logger.Log(LogType.Warning, "Invalid entry in zone \"{0}\" blacklist: {1}", Name, playerName); continue; } PlayerInfo info = PlayerDB.FindExact(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]); } } }
static void ImportRanks(Player player, CommandReader cmd) { string serverName = cmd.Next(); string fileName = cmd.Next(); string rankName = cmd.Next(); bool silent = (cmd.Next() != null); // Make sure all parameters are specified if (serverName == null || fileName == null || rankName == null) { CdImport.PrintUsage(player); return; } // Check if file exists if (!File.Exists(fileName)) { player.Message("File not found: {0}", fileName); return; } Rank targetRank = RankManager.FindRank(rankName); if (targetRank == null) { player.MessageNoRank(rankName); return; } string[] names; switch (serverName.ToLower()) { case "mcsharp": case "mczall": case "mclawl": try { names = File.ReadAllLines(fileName); } catch (Exception ex) { Logger.Log(LogType.Error, "Could not open \"{0}\" to import ranks: {1}", fileName, ex); return; } break; default: player.Message("fCraft does not support importing from {0}", serverName); return; } if (!cmd.IsConfirmed) { player.Confirm(cmd, "Import {0} player ranks from \"{1}\"?", names.Length, Path.GetFileName(fileName)); return; } string reason = "(Import from " + serverName + ")"; foreach (string name in names) { PlayerInfo info = PlayerDB.FindExact(name) ?? PlayerDB.AddUnrecognizedPlayer(name, RankChangeType.Promoted); try { info.ChangeRank(player, targetRank, reason, !silent, true, false); } catch (PlayerOpException ex) { player.Message(ex.MessageColored); } } PlayerDB.Save(); }
static void ImportBans(Player player, CommandReader cmd) { string serverName = cmd.Next(); string fileName = cmd.Next(); // Make sure all parameters are specified if (serverName == null || fileName == null) { CdImport.PrintUsage(player); return; } // Check if file exists if (!File.Exists(fileName)) { player.Message("File not found: {0}", fileName); return; } string[] names; switch (serverName.ToLower()) { case "mcsharp": case "mczall": case "mclawl": try { names = File.ReadAllLines(fileName); } catch (Exception ex) { Logger.Log(LogType.Error, "Could not open \"{0}\" to import bans: {1}", fileName, ex); return; } break; default: player.Message("fCraft does not support importing from {0}", serverName); return; } if (!cmd.IsConfirmed) { player.Confirm(cmd, "Import {0} bans from \"{1}\"?", names.Length, Path.GetFileName(fileName)); return; } string reason = "(import from " + serverName + ")"; foreach (string name in names) { if (Player.IsValidName(name)) { PlayerInfo info = PlayerDB.FindExact(name) ?? PlayerDB.AddUnrecognizedPlayer(name, RankChangeType.Default); info.Ban(player, reason, true, true); } else { IPAddress ip; if (IPAddressUtil.IsIP(name) && IPAddress.TryParse(name, out ip)) { ip.BanIP(player, reason, true, true); } else { player.Message("Could not parse \"{0}\" as either name or IP. Skipping.", name); } } } PlayerDB.Save(); IPBanList.Save(); }