/// <summary> Bans given IP address. All players from IP are kicked. If an associated PlayerInfo is known, /// use a different overload of this method instead. Throws PlayerOpException on problems. </summary> /// <param name="targetAddress"> IP address that is being banned. </param> /// <param name="player"> Player who is banning. </param> /// <param name="reason"> Reason for ban. May be empty, if permitted by server configuration. </param> /// <param name="announce"> Whether ban should be publicly announced on the server. </param> /// <param name="raiseEvents"> Whether AddingIPBan and AddedIPBan events should be raised. </param> /// <exception cref="ArgumentNullException"> targetAddress or player is null. </exception> /// <exception cref="PlayerOpException"> Permission or configuration issues arise, or if IP is already banned. </exception> public static void BanIP([NotNull] this IPAddress targetAddress, [NotNull] Player player, [CanBeNull] string reason, bool announce, bool raiseEvents) { if (targetAddress == null) { throw new ArgumentNullException("targetAddress"); } if (player == null) { throw new ArgumentNullException("player"); } if (reason != null) { reason = reason.Trim(' '); } if (reason != null && reason.Length == 0) { reason = null; } // Check if player can ban IPs in general if (!player.Can(Permission.Ban, Permission.BanIP)) { PlayerOpException.ThrowPermissionMissing(player, null, "IP-ban", Permission.Ban, Permission.BanIP); } // Check if a non-bannable address was given (0.0.0.0 or 255.255.255.255) if (targetAddress.Equals(IPAddress.None) || targetAddress.Equals(IPAddress.Any)) { PlayerOpException.ThrowInvalidIP(player, null, targetAddress); } // Check if player is trying to ban self if (targetAddress.Equals(player.IP) && !player.IsSuper) { PlayerOpException.ThrowCannotTargetSelf(player, null, "IP-ban"); } lock ( BanListLock ) { CheckIfLoaded(); // Check if target is already banned IPBanInfo existingBan = Get(targetAddress); if (existingBan != null) { string msg; if (player.Can(Permission.ViewPlayerIPs)) { msg = String.Format("IP address {0} is already banned.", targetAddress); } else { msg = String.Format("Given IP address is already banned."); } string colorMsg = "&S" + msg; throw new PlayerOpException(player, null, PlayerOpExceptionCode.NoActionNeeded, msg, colorMsg); } // Check if any high-ranked players use this address PlayerInfo infoWhomPlayerCantBan = PlayerDB.FindPlayers(targetAddress) .FirstOrDefault(info => !player.Can(Permission.Ban, info.Rank)); if (infoWhomPlayerCantBan != null) { PlayerOpException.ThrowPermissionLimitIP(player, infoWhomPlayerCantBan, targetAddress); } PlayerOpException.CheckBanReason(reason, player, null, false); // Actually ban IPBanInfo banInfo = new IPBanInfo(targetAddress, null, player.Name, reason); bool result = Add(banInfo, raiseEvents); if (result) { Logger.Log(LogType.UserActivity, "{0} banned {1} (BanIP {1}). Reason: {2}", player.Name, targetAddress, reason ?? ""); if (announce) { // Announce ban on the server var can = Server.Players.Can(Permission.ViewPlayerIPs); can.Message("&W{0} was banned by {1}", targetAddress, player.ClassyName); var cant = Server.Players.Cant(Permission.ViewPlayerIPs); cant.Message("&WAn IP was banned by {0}", player.ClassyName); if (ConfigKey.AnnounceKickAndBanReasons.Enabled() && reason != null) { Server.Message("&WBanIP reason: {0}", reason); } } // Kick all players connected from address string kickReason; if (reason != null) { kickReason = String.Format("IP-Banned by {0}: {1}", player.Name, reason); } else { kickReason = String.Format("IP-Banned by {0}", player.Name); } foreach (Player other in Server.Players.FromIP(targetAddress)) { if (other.Info.BanStatus != BanStatus.IPBanExempt) { other.Kick(kickReason, LeaveReason.BanIP); // TODO: check side effects of not using DoKick } } } else { // address is already banned string msg; if (player.Can(Permission.ViewPlayerIPs)) { msg = String.Format("{0} is already banned.", targetAddress); } else { msg = "Given IP address is already banned."; } string colorMsg = "&S" + msg; throw new PlayerOpException(player, null, PlayerOpExceptionCode.NoActionNeeded, msg, colorMsg); } } }
static void ZoneRemoveHandler(Player player, CommandReader cmd) { string zoneName = cmd.Next(); if (zoneName == null || cmd.HasNext) { CdZoneRemove.PrintUsage(player); return; } if (zoneName == "*") { if (!cmd.IsConfirmed) { player.Confirm(cmd, "&WRemove ALL zones on this world ({0}&W)? This cannot be undone.&S", player.World.ClassyName); return; } player.WorldMap.Zones.Clear(); Logger.Log(LogType.UserActivity, "Player {0} removed all zones on world {1}", player.Name, player.World.Name); Server.Message("Player {0}&S removed all zones on world {1}", player.ClassyName, player.World.ClassyName); return; } ZoneCollection zones = player.WorldMap.Zones; Zone zone = zones.Find(zoneName); if (zone != null) { if (!player.Info.Rank.AllowSecurityCircumvention) { switch (zone.Controller.CheckDetailed(player.Info)) { case SecurityCheckResult.BlackListed: player.Message("You are not allowed to remove zone {0}: you are blacklisted.", zone.ClassyName); return; case SecurityCheckResult.RankTooLow: player.Message("You are not allowed to remove zone {0}.", zone.ClassyName); return; // TODO: Must be below rank or Operator // case SecurityCheckResult.RankTooLow: } } if (!cmd.IsConfirmed) { player.Confirm(cmd, "Remove zone {0}&S?", zone.ClassyName); return; } if (zones.Remove(zone.Name)) { player.Message("Zone \"{0}\" removed.", zone.Name); } } else { player.MessageNoZone(zoneName); } }
public Map AcceptPlayer([NotNull] Player player, bool announce) { if (player == null) { throw new ArgumentNullException("player"); } lock ( SyncRoot ) { if (IsFull) { if (player.Info.Rank.ReservedSlot) { Player idlestPlayer = Players.Where(p => p.Info.Rank.IdleKickTimer != 0) .OrderBy(p => p.LastActiveTime) .FirstOrDefault(); if (idlestPlayer != null) { idlestPlayer.Kick(Player.Console, "Auto-kicked to make room (idle).", LeaveReason.IdleKick, false, false, false); Server.Message("Player {0}&S was auto-kicked to make room for {1}", idlestPlayer.ClassyName, player.ClassyName); } else { return(null); } } else { return(null); } } if (playerIndex.ContainsKey(player.Name.ToLower())) { Logger.Log(LogType.Error, "This world already contains the player by name ({0}). " + "Some sort of state corruption must have occured.", player.Name); playerIndex.Remove(player.Name.ToLower()); } playerIndex.Add(player.Name.ToLower(), player); // load the map, if it's not yet loaded IsPendingMapUnload = false; Map = LoadMap(); if (ConfigKey.BackupOnJoin.Enabled() && (Map.HasChangedSinceBackup || !ConfigKey.BackupOnlyWhenChanged.Enabled())) { string backupFileName = String.Format(JoinBackupFormat, Name, DateTime.Now, player.Name); // localized Map.SaveBackup(MapFileName, Path.Combine(Paths.BackupPath, backupFileName)); } UpdatePlayerList(); if (announce && ConfigKey.ShowJoinedWorldMessages.Enabled()) { Server.Players.CanSee(player) .Message("&SPlayer {0}&S joined {1}", player.ClassyName, ClassyName); } Logger.Log(LogType.UserActivity, "Player {0} joined world {1}.", player.Name, Name); if (IsLocked) { player.Message("&WThis map is currently locked (read-only)."); } if (player.Info.IsHidden) { player.Message("&8Reminder: You are still hidden."); } return(Map); } }
/// <summary> Unbans given IP address and all accounts on that IP. Throws PlayerOpException on problems. </summary> /// <param name="targetAddress"> IP address that is being unbanned. </param> /// <param name="player"> Player who is unbanning. </param> /// <param name="reason"> Reason for unban. May be null or empty, if permitted by server configuration. </param> /// <param name="announce"> Whether unban should be publicly announced on the server. </param> /// <param name="raiseEvents"> Whether RemovingIPBan, RemovedIPBan, BanChanging, and BanChanged events should be raised. </param> public static void UnbanAll([NotNull] this IPAddress targetAddress, [NotNull] Player player, [CanBeNull] string reason, bool announce, bool raiseEvents) { if (targetAddress == null) { throw new ArgumentNullException("targetAddress"); } if (player == null) { throw new ArgumentNullException("player"); } if (reason != null && reason.Trim().Length == 0) { reason = null; } if (!player.Can(Permission.Ban, Permission.BanIP, Permission.BanAll)) { PlayerOpException.ThrowPermissionMissing(player, null, "unban-all", Permission.Ban, Permission.BanIP, Permission.BanAll); } // Check if player is trying to unban self if (targetAddress.Equals(player.IP) && !player.IsSuper) { PlayerOpException.ThrowCannotTargetSelf(player, null, "unban-all"); } // Check if a non-bannable address was given(0.0.0.0 or 255.255.255.255) if (targetAddress.Equals(IPAddress.None) || targetAddress.Equals(IPAddress.Any)) { PlayerOpException.ThrowInvalidIP(player, null, targetAddress); } PlayerOpException.CheckBanReason(reason, player, null, true); bool somethingGotUnbanned = false; // Unban the IP if (Contains(targetAddress)) { if (Remove(targetAddress, raiseEvents)) { Logger.Log(LogType.UserActivity, "{0} unbanned {1}(UnbanAll {1}). Reason: {2}", player.Name, targetAddress, reason ?? ""); // Announce unban on the server if (announce) { var can = Server.Players.Can(Permission.ViewPlayerIPs); can.Message("&W{0} was unbanned by {1}", targetAddress, player.ClassyName); var cant = Server.Players.Cant(Permission.ViewPlayerIPs); cant.Message("&WAn IP was unbanned by {0}", player.ClassyName); } somethingGotUnbanned = true; } } // Unban individual players PlayerInfo[] allPlayersOnIP = PlayerDB.FindPlayers(targetAddress); foreach (PlayerInfo targetAlt in allPlayersOnIP) { if (targetAlt.BanStatus != BanStatus.Banned) { continue; } // Raise PlayerInfo.BanChanging event PlayerInfoBanChangingEventArgs e = new PlayerInfoBanChangingEventArgs(targetAlt, player, true, reason, announce); if (raiseEvents) { PlayerInfo.RaiseBanChangingEvent(e); if (e.Cancel) { continue; } reason = e.Reason; } // Do the ban if (targetAlt.ProcessUnban(player.Name, reason)) { if (raiseEvents) { PlayerInfo.RaiseBanChangedEvent(e); } // Log and announce ban Logger.Log(LogType.UserActivity, "{0} unbanned {1}(UnbanAll {2}). Reason: {3}", player.Name, targetAlt.Name, targetAddress, reason ?? ""); if (announce) { Server.Message("&WPlayer {0}&W was unbanned by {1}&W(UnbanAll)", targetAlt.ClassyName, player.ClassyName); } somethingGotUnbanned = true; } } // If no one ended up getting unbanned, quit here if (!somethingGotUnbanned) { PlayerOpException.ThrowNoOneToUnban(player, null, targetAddress); } // Announce UnbanAll reason towards the end of all unbans if (announce && ConfigKey.AnnounceKickAndBanReasons.Enabled() && reason != null) { Server.Message("&WUnbanAll reason: {0}", reason); } }
void HandleMessage([NotNull] string message) { if (message == null) { throw new ArgumentNullException("message"); } IRCMessage msg = MessageParser(message, ActualBotNick); #if DEBUG_IRC Logger.Log(LogType.IRC, "[{0}]: {1}", msg.Type, msg.RawMessage); #endif switch (msg.Type) { case IRCMessageType.Login: if (ConfigKey.IRCRegisteredNick.Enabled()) { Send(IRCCommands.Privmsg(ConfigKey.IRCNickServ.GetString(), ConfigKey.IRCNickServMessage.GetString())); } foreach (string channel in channelNames) { Send(IRCCommands.Join(channel)); } IsReady = true; AssignBotForInputParsing(); // bot should be ready to receive input after joining return; case IRCMessageType.Ping: // ping-pong Send(IRCCommands.Pong(msg.RawMessageArray[1].Substring(1))); return; case IRCMessageType.ChannelAction: case IRCMessageType.ChannelMessage: // channel chat if (!ResponsibleForInputParsing) { return; } if (!IsBotNick(msg.Nick)) { string processedMessage = msg.Message; if (msg.Type == IRCMessageType.ChannelAction) { if (processedMessage.StartsWith("\u0001ACTION")) { processedMessage = processedMessage.Substring(8); } else { return; } } processedMessage = NonPrintableChars.Replace(processedMessage, ""); processedMessage = Color.EscapeAmpersands(processedMessage).Trim(); if (processedMessage.Length > 0) { if (ConfigKey.IRCBotForwardFromIRC.Enabled()) { if (msg.Type == IRCMessageType.ChannelAction) { Server.Message("&i(IRC) * {0} {1}", msg.Nick, processedMessage); } else { Server.Message("&i(IRC) {0}{1}: {2}", msg.Nick, Color.White, processedMessage); } } else if (msg.Message.StartsWith("#")) { Server.Message("&i(IRC) {0}{1}: {2}", msg.Nick, Color.White, processedMessage.Substring(1)); } } } return; case IRCMessageType.Join: if (!ResponsibleForInputParsing) { return; } if (ConfigKey.IRCBotAnnounceIRCJoins.Enabled()) { Server.Message("&i(IRC) {0} joined {1}", msg.Nick, msg.Channel); } return; case IRCMessageType.Kick: string kicked = msg.RawMessageArray[3]; if (kicked == ActualBotNick) { Logger.Log(LogType.IRC, "Bot was kicked from {0} by {1} ({2}), rejoining.", msg.Channel, msg.Nick, msg.Message); Thread.Sleep(ReconnectDelay); Send(IRCCommands.Join(msg.Channel)); } else { if (!ResponsibleForInputParsing) { return; } Server.Message("&i(IRC) {0} kicked {1} from {2} ({3})", msg.Nick, kicked, msg.Channel, msg.Message); } return; case IRCMessageType.Part: case IRCMessageType.Quit: if (!ResponsibleForInputParsing) { return; } if (ConfigKey.IRCBotAnnounceIRCJoins.Enabled()) { Server.Message("&i(IRC) {0} left {1}", msg.Nick, msg.Channel); } return; case IRCMessageType.NickChange: if (!ResponsibleForInputParsing) { return; } Server.Message("&i(IRC) {0} is now known as {1}", msg.Nick, msg.Message); return; case IRCMessageType.ErrorMessage: case IRCMessageType.Error: bool die = false; switch (msg.ReplyCode) { case IRCReplyCode.ErrorNicknameInUse: case IRCReplyCode.ErrorNicknameCollision: Logger.Log(LogType.IRC, "Error: Nickname \"{0}\" is already in use. Trying \"{0}_\"", ActualBotNick); ActualBotNick += "_"; Send(IRCCommands.Nick(ActualBotNick)); break; case IRCReplyCode.ErrorBannedFromChannel: case IRCReplyCode.ErrorNoSuchChannel: Logger.Log(LogType.IRC, "Error: {0} ({1})", msg.ReplyCode, msg.Channel); die = true; break; case IRCReplyCode.ErrorBadChannelKey: Logger.Log(LogType.IRC, "Error: Channel password required for {0}. fCraft does not currently support passworded channels.", msg.Channel); die = true; break; default: Logger.Log(LogType.IRC, "Error ({0}): {1}", msg.ReplyCode, msg.RawMessage); break; } if (die) { Logger.Log(LogType.IRC, "Error: Disconnecting."); reconnect = false; DisconnectThread(); } return; case IRCMessageType.QueryAction: // TODO: PMs Logger.Log(LogType.IRC, "Query: {0}", msg.RawMessage); break; case IRCMessageType.Kill: Logger.Log(LogType.IRC, "Bot was killed from {0} by {1} ({2}), reconnecting.", hostName, msg.Nick, msg.Message); reconnect = true; isConnected = false; return; } }
/// <summary> Bans given IP address and all accounts on that IP. All players from IP are kicked. /// Throws PlayerOpException on problems. </summary> /// <param name="targetAddress"> IP address that is being banned. </param> /// <param name="player"> Player who is banning. </param> /// <param name="reason"> Reason for ban. May be empty, if permitted by server configuration. </param> /// <param name="announce"> Whether ban should be publicly announced on the server. </param> /// <param name="raiseEvents"> Whether AddingIPBan, AddedIPBan, BanChanging, and BanChanged events should be raised. </param> public static void BanAll([NotNull] this IPAddress targetAddress, [NotNull] Player player, [CanBeNull] string reason, bool announce, bool raiseEvents) { if (targetAddress == null) { throw new ArgumentNullException("targetAddress"); } if (player == null) { throw new ArgumentNullException("player"); } if (reason != null && reason.Trim().Length == 0) { reason = null; } if (!player.Can(Permission.Ban, Permission.BanIP, Permission.BanAll)) { PlayerOpException.ThrowPermissionMissing(player, null, "ban-all", Permission.Ban, Permission.BanIP, Permission.BanAll); } // Check if player is trying to ban self if (targetAddress.Equals(player.IP) && !player.IsSuper) { PlayerOpException.ThrowCannotTargetSelf(player, null, "ban-all"); } // Check if a non-bannable address was given(0.0.0.0 or 255.255.255.255) if (targetAddress.Equals(IPAddress.None) || targetAddress.Equals(IPAddress.Any)) { PlayerOpException.ThrowInvalidIP(player, null, targetAddress); } // Check if any high-ranked players use this address PlayerInfo[] allPlayersOnIP = PlayerDB.FindPlayers(targetAddress); PlayerInfo infoWhomPlayerCantBan = allPlayersOnIP.FirstOrDefault(info => !player.Can(Permission.Ban, info.Rank)); if (infoWhomPlayerCantBan != null) { PlayerOpException.ThrowPermissionLimitIP(player, infoWhomPlayerCantBan, targetAddress); } PlayerOpException.CheckBanReason(reason, player, null, false); bool somethingGotBanned = false; // Ban the IP if (!Contains(targetAddress)) { IPBanInfo banInfo = new IPBanInfo(targetAddress, null, player.Name, reason); if (Add(banInfo, raiseEvents)) { Logger.Log(LogType.UserActivity, "{0} banned {1}(BanAll {1}). Reason: {2}", player.Name, targetAddress, reason ?? ""); // Announce ban on the server if (announce) { var can = Server.Players.Can(Permission.ViewPlayerIPs); can.Message("&W{0} was banned by {1}", targetAddress, player.ClassyName); var cant = Server.Players.Cant(Permission.ViewPlayerIPs); cant.Message("&WAn IP was banned by {0}", player.ClassyName); } somethingGotBanned = true; } } // Ban individual players foreach (PlayerInfo targetAlt in allPlayersOnIP) { if (targetAlt.BanStatus != BanStatus.NotBanned) { continue; } // Raise PlayerInfo.BanChanging event PlayerInfoBanChangingEventArgs e = new PlayerInfoBanChangingEventArgs(targetAlt, player, false, reason, announce); if (raiseEvents) { PlayerInfo.RaiseBanChangingEvent(e); if (e.Cancel) { continue; } reason = e.Reason; } // Do the ban if (targetAlt.ProcessBan(player, player.Name, reason)) { if (raiseEvents) { PlayerInfo.RaiseBanChangedEvent(e); } // Log and announce ban Logger.Log(LogType.UserActivity, "{0} banned {1}(BanAll {2}). Reason: {3}", player.Name, targetAlt.Name, targetAddress, reason ?? ""); if (announce) { Server.Message("&WPlayer {0}&W was banned by {1}&W(BanAll)", targetAlt.ClassyName, player.ClassyName); } somethingGotBanned = true; } } // If no one ended up getting banned, quit here if (!somethingGotBanned) { PlayerOpException.ThrowNoOneToBan(player, null, targetAddress); } // Announce BanAll reason towards the end of all bans if (announce && ConfigKey.AnnounceKickAndBanReasons.Enabled() && reason != null) { Server.Message("&WBanAll reason: {0}", reason); } // Kick all players from IP Player[] targetsOnline = Server.Players.FromIP(targetAddress).ToArray(); if (targetsOnline.Length > 0) { string kickReason; if (reason != null) { kickReason = String.Format("Banned by {0}: {1}", player.Name, reason); } else { kickReason = String.Format("Banned by {0}", player.Name); } for (int i = 0; i < targetsOnline.Length; i++) { targetsOnline[i].Kick(kickReason, LeaveReason.BanAll); } } }
/// <summary> Unbans an IP address. If an associated PlayerInfo is known, /// use a different overload of this method instead. Throws PlayerOpException on problems. </summary> /// <param name="targetAddress"> IP address that is being unbanned. </param> /// <param name="player"> Player who is unbanning. </param> /// <param name="reason"> Reason for unban. May be empty, if permitted by server configuration. </param> /// <param name="announce"> Whether unban should be publicly announced on the server. </param> /// <param name="raiseEvents"> Whether RemovingIPBan and RemovedIPBan events should be raised. </param> public static void UnbanIP([NotNull] this IPAddress targetAddress, [NotNull] Player player, [CanBeNull] string reason, bool announce, bool raiseEvents) { if (targetAddress == null) { throw new ArgumentNullException("targetAddress"); } if (player == null) { throw new ArgumentNullException("player"); } if (reason != null && reason.Trim().Length == 0) { reason = null; } // Check if player can unban IPs in general if (!player.Can(Permission.Ban, Permission.BanIP)) { PlayerOpException.ThrowPermissionMissing(player, null, "IP-unban", Permission.Ban, Permission.BanIP); } // Check if a non-bannable address was given(0.0.0.0 or 255.255.255.255) if (targetAddress.Equals(IPAddress.None) || targetAddress.Equals(IPAddress.Any)) { PlayerOpException.ThrowInvalidIP(player, null, targetAddress); } // Check if player is trying to unban self if (targetAddress.Equals(player.IP) && !player.IsSuper) { PlayerOpException.ThrowCannotTargetSelf(player, null, "IP-unban"); } PlayerOpException.CheckBanReason(reason, player, null, true); // Actually unban bool result = Remove(targetAddress, raiseEvents); if (result) { Logger.Log(LogType.UserActivity, "{0} unbanned {1}(UnbanIP {1}). Reason: {2}", player.Name, targetAddress, reason ?? ""); if (announce) { var can = Server.Players.Can(Permission.ViewPlayerIPs); can.Message("&W{0} was unbanned by {1}", targetAddress, player.ClassyName); var cant = Server.Players.Cant(Permission.ViewPlayerIPs); cant.Message("&WAn IP was unbanned by {0}", player.ClassyName); if (ConfigKey.AnnounceKickAndBanReasons.Enabled() && reason != null) { Server.Message("&WUnbanIP reason: {0}", reason); } } } else { string msg; if (player.Can(Permission.ViewPlayerIPs)) { msg = String.Format("IP address {0} is not currently banned.", targetAddress); } else { msg = String.Format("Given IP address is not currently banned."); } string colorMsg = "&S" + msg; throw new PlayerOpException(player, null, PlayerOpExceptionCode.NoActionNeeded, msg, colorMsg); } }
internal static void RealmBuild(Player player, Command cmd, string worldName, string name, string NameIfRankIsName) { // Print information about the current realm if (worldName == null) { if (player.World == null) { player.Message("When calling /wbuild from console, you must specify a realm name."); } else { player.Message(player.World.BuildSecurity.GetDescription(player.World, "realm", "modified")); } return; } // Find a realm by name World realm = WorldManager.FindWorldOrPrintMatches(player, worldName); if (realm == null) { return; } if (name == null) { player.Message(realm.BuildSecurity.GetDescription(realm, "realm", "modified")); return; } bool changesWereMade = false; do { if (name.Length < 2) { continue; } // Whitelisting individuals if (name.StartsWith("+")) { PlayerInfo info; if (!PlayerDB.FindPlayerInfo(name.Substring(1), out info)) { player.Message("More than one player found matching \"{0}\"", name.Substring(1)); continue; } else if (info == null) { player.MessageNoPlayer(name.Substring(1)); continue; } if (realm.BuildSecurity.CheckDetailed(info) == SecurityCheckResult.Allowed) { player.Message("{0}&S is already allowed to build in {1}&S (by rank)", info.ClassyName, realm.ClassyName); continue; } Player target = info.PlayerObject; if (target == player) { target = null; // to avoid duplicate messages } switch (realm.BuildSecurity.Include(info)) { case PermissionOverride.Deny: if (realm.BuildSecurity.Check(info)) { player.Message("{0}&S is no longer barred from building in {1}", info.ClassyName, realm.ClassyName); if (target != null) { target.Message( "You can now build in realm {0}&S (removed from blacklist by {1}&S).", realm.ClassyName, player.ClassyName); } } else { player.Message("{0}&S was removed from the build blacklist of {1}&S. " + "Player is still NOT allowed to build (by rank).", info.ClassyName, realm.ClassyName); if (target != null) { target.Message( "You were removed from the build blacklist of realm {0}&S by {1}&S. " + "You are still NOT allowed to build (by rank).", player.ClassyName, realm.ClassyName); } } Logger.Log(LogType.UserActivity, "{0} removed {1} from the build blacklist of {2}", player.Name, info.Name, realm.Name); changesWereMade = true; break; case PermissionOverride.None: player.Message("{0}&S is now allowed to build in {1}", info.ClassyName, realm.ClassyName); if (target != null) { target.Message("You can now build in realm {0}&S (whitelisted by {1}&S).", realm.ClassyName, player.ClassyName); } Logger.Log(LogType.UserActivity, "{0} added {1} to the build whitelist on realm {2}", player.Name, info.Name, realm.Name); break; case PermissionOverride.Allow: player.Message("{0}&S is already on the build whitelist of {1}", info.ClassyName, realm.ClassyName); break; } // Blacklisting individuals } else if (name.StartsWith("-")) { PlayerInfo info; if (!PlayerDB.FindPlayerInfo(name.Substring(1), out info)) { player.Message("More than one player found matching \"{0}\"", name.Substring(1)); continue; } else if (info == null) { player.MessageNoPlayer(name.Substring(1)); continue; } if (realm.BuildSecurity.CheckDetailed(info) == SecurityCheckResult.RankTooHigh || realm.BuildSecurity.CheckDetailed(info) == SecurityCheckResult.RankTooLow) { player.Message("{0}&S is already barred from building in {1}&S (by rank)", info.ClassyName, realm.ClassyName); continue; } Player target = info.PlayerObject; if (target == player) { target = null; // to avoid duplicate messages } switch (realm.BuildSecurity.Exclude(info)) { case PermissionOverride.Deny: player.Message("{0}&S is already on build blacklist of {1}", info.ClassyName, realm.ClassyName); break; case PermissionOverride.None: player.Message("{0}&S is now barred from building in {1}", info.ClassyName, realm.ClassyName); if (target != null) { target.Message("&WYou were barred by {0}&W from building in realm {1}", player.ClassyName, realm.ClassyName); } Logger.Log(LogType.UserActivity, "{0} added {1} to the build blacklist on realm {2}", player.Name, info.Name, realm.Name); changesWereMade = true; break; case PermissionOverride.Allow: if (realm.BuildSecurity.Check(info)) { player.Message("{0}&S is no longer on the build whitelist of {1}&S. " + "Player is still allowed to build (by rank).", info.ClassyName, realm.ClassyName); if (target != null) { target.Message( "You were removed from the build whitelist of realm {0}&S by {1}&S. " + "You are still allowed to build (by rank).", player.ClassyName, realm.ClassyName); } } else { player.Message("{0}&S is no longer allowed to build in {1}", info.ClassyName, realm.ClassyName); if (target != null) { target.Message( "&WYou can no longer build in realm {0}&W (removed from whitelist by {1}&W).", realm.ClassyName, player.ClassyName); } } Logger.Log(LogType.UserActivity, "{0} removed {1} from the build whitelist on realm {2}", player.Name, info.Name, realm.Name); changesWereMade = true; break; } // Setting minimum rank } else { Rank rank = RankManager.FindRank(name); if (rank == null) { player.MessageNoRank(name); } else if (!player.Info.Rank.AllowSecurityCircumvention && realm.BuildSecurity.MinRank > rank && realm.BuildSecurity.MinRank > player.Info.Rank) { player.Message("&WYou must be ranked {0}&W+ to lower build restrictions for realm {1}", realm.BuildSecurity.MinRank.ClassyName, realm.ClassyName); } else { // list players who are redundantly blacklisted var exceptionList = realm.BuildSecurity.ExceptionList; PlayerInfo[] noLongerExcluded = exceptionList.Excluded.Where(excludedPlayer => excludedPlayer.Rank < rank).ToArray(); if (noLongerExcluded.Length > 0) { player.Message("Following players no longer need to be blacklisted on realm {0}&S: {1}", realm.ClassyName, noLongerExcluded.JoinToClassyString()); } // list players who are redundantly whitelisted PlayerInfo[] noLongerIncluded = exceptionList.Included.Where(includedPlayer => includedPlayer.Rank >= rank).ToArray(); if (noLongerIncluded.Length > 0) { player.Message("Following players no longer need to be whitelisted on realm {0}&S: {1}", realm.ClassyName, noLongerIncluded.JoinToClassyString()); } // apply changes realm.BuildSecurity.MinRank = rank; changesWereMade = true; if (realm.BuildSecurity.MinRank == RankManager.LowestRank) { Server.Message("{0}&S allowed anyone to build on realm {1}", player.ClassyName, realm.ClassyName); } else { Server.Message("{0}&S allowed only {1}+&S to build in realm {2}", player.ClassyName, realm.BuildSecurity.MinRank.ClassyName, realm.ClassyName); } Logger.Log(LogType.UserActivity, "{0} set build rank for realm {1} to {2}+", player.Name, realm.Name, realm.BuildSecurity.MinRank.Name); } } } while ((name = cmd.Next()) != null); if (changesWereMade) { WorldManager.SaveWorldList(); } }
internal static void RealmAccess(Player player, Command cmd, string worldName, string name) { // Print information about the current realm if (worldName == null) { if (player.World == null) { player.Message("Error."); } else { player.Message(player.World.AccessSecurity.GetDescription(player.World, "realm", "accessed")); } return; } // Find a realm by name World realm = WorldManager.FindWorldOrPrintMatches(player, worldName); if (realm == null) { return; } if (name == null) { player.Message(realm.AccessSecurity.GetDescription(realm, "realm", "accessed")); return; } if (realm == WorldManager.MainWorld) { player.Message("The main realm cannot have access restrictions."); return; } bool changesWereMade = false; do { if (name.Length < 2) { continue; } // Whitelisting individuals if (name.StartsWith("+")) { PlayerInfo info; if (!PlayerDB.FindPlayerInfo(name.Substring(1), out info)) { player.Message("More than one player found matching \"{0}\"", name.Substring(1)); continue; } else if (info == null) { player.MessageNoPlayer(name.Substring(1)); continue; } // prevent players from whitelisting themselves to bypass protection if (realm.AccessSecurity.CheckDetailed(info) == SecurityCheckResult.Allowed) { player.Message("{0}&S is already allowed to access {1}&S (by rank)", info.ClassyName, realm.ClassyName); continue; } Player target = info.PlayerObject; if (target == player) { target = null; // to avoid duplicate messages } switch (realm.AccessSecurity.Include(info)) { case PermissionOverride.Deny: if (realm.AccessSecurity.Check(info)) { player.Message("{0}&S is unbanned from Realm {1}", info.ClassyName, realm.ClassyName); if (target != null) { target.Message( "You are now unbanned from Realm {0}&S (removed from blacklist by {1}&S).", realm.ClassyName, player.ClassyName); } } else { player.Message("{0}&S was unbanned from Realm {1}&S. " + "Player is still NOT allowed to join (by rank).", info.ClassyName, realm.ClassyName); if (target != null) { target.Message("You were Unbanned from Realm {0}&S by {1}&S. " + "You are still NOT allowed to join (by rank).", player.ClassyName, realm.ClassyName); } } Logger.Log(LogType.UserActivity, "{0} removed {1} from the access blacklist of {2}", player.Name, info.Name, realm.Name); changesWereMade = true; break; case PermissionOverride.None: player.Message("{0}&S is now allowed to access {1}", info.ClassyName, realm.ClassyName); if (target != null) { target.Message("You can now access realm {0}&S (whitelisted by {1}&S).", realm.ClassyName, player.ClassyName); } Logger.Log(LogType.UserActivity, "{0} added {1} to the access whitelist on realm {2}", player.Name, info.Name, realm.Name); changesWereMade = true; break; case PermissionOverride.Allow: player.Message("{0}&S is already on the access whitelist of {1}", info.ClassyName, realm.ClassyName); break; } // Blacklisting individuals } else if (name.StartsWith("-")) { PlayerInfo info; if (!PlayerDB.FindPlayerInfo(name.Substring(1), out info)) { player.Message("More than one player found matching \"{0}\"", name.Substring(1)); continue; } else if (info == null) { player.MessageNoPlayer(name.Substring(1)); continue; } if (realm.AccessSecurity.CheckDetailed(info) == SecurityCheckResult.RankTooHigh || realm.AccessSecurity.CheckDetailed(info) == SecurityCheckResult.RankTooLow) { player.Message("{0}&S is already barred from accessing {1}&S (by rank)", info.ClassyName, realm.ClassyName); continue; } Player target = info.PlayerObject; if (target == player) { target = null; // to avoid duplicate messages } switch (realm.AccessSecurity.Exclude(info)) { case PermissionOverride.Deny: player.Message("{0}&S is already banned from Realm {1}", info.ClassyName, realm.ClassyName); break; case PermissionOverride.None: player.Message("{0}&S is now banned from accessing {1}", info.ClassyName, realm.ClassyName); if (target != null) { target.Message("&WYou were banned by {0}&W from accessing realm {1}", player.ClassyName, realm.ClassyName); } Logger.Log(LogType.UserActivity, "{0} added {1} to the access blacklist on realm {2}", player.Name, info.Name, realm.Name); changesWereMade = true; break; case PermissionOverride.Allow: if (realm.AccessSecurity.Check(info)) { player.Message("{0}&S is no longer on the access whitelist of {1}&S. " + "Player is still allowed to join (by rank).", info.ClassyName, realm.ClassyName); if (target != null) { target.Message("You were banned from Realm {0}&S by {1}&S. " + "You are still allowed to join (by rank).", player.ClassyName, realm.ClassyName); } } else { player.Message("{0}&S is no longer allowed to access {1}", info.ClassyName, realm.ClassyName); if (target != null) { target.Message("&WYou were banned from Realm {0}&W (Banned by {1}&W).", realm.ClassyName, player.ClassyName); } } Logger.Log(LogType.UserActivity, "{0} removed {1} from the access whitelist on realm {2}", player.Name, info.Name, realm.Name); changesWereMade = true; break; } // Setting minimum rank } else { Rank rank = RankManager.FindRank(name); if (rank == null) { player.MessageNoRank(name); } else { // list players who are redundantly blacklisted var exceptionList = realm.AccessSecurity.ExceptionList; PlayerInfo[] noLongerExcluded = exceptionList.Excluded.Where(excludedPlayer => excludedPlayer.Rank < rank).ToArray(); if (noLongerExcluded.Length > 0) { player.Message( "Following players no longer need to be blacklisted to be barred from {0}&S: {1}", realm.ClassyName, noLongerExcluded.JoinToClassyString()); } // list players who are redundantly whitelisted PlayerInfo[] noLongerIncluded = exceptionList.Included.Where(includedPlayer => includedPlayer.Rank >= rank).ToArray(); if (noLongerIncluded.Length > 0) { player.Message("Following players no longer need to be whitelisted to access {0}&S: {1}", realm.ClassyName, noLongerIncluded.JoinToClassyString()); } // apply changes realm.AccessSecurity.MinRank = rank; changesWereMade = true; if (realm.AccessSecurity.MinRank == RankManager.LowestRank) { Server.Message("{0}&S made the realm {1}&S accessible to everyone.", player.ClassyName, realm.ClassyName); } else { Server.Message("{0}&S made the realm {1}&S accessible only by {2}+", player.ClassyName, realm.ClassyName, realm.AccessSecurity.MinRank.ClassyName); } Logger.Log(LogType.UserActivity, "{0} set access rank for realm {1} to {2}+", player.Name, realm.Name, realm.AccessSecurity.MinRank.Name); } } } while ((name = cmd.Next()) != null); if (changesWereMade) { var playersWhoCantStay = realm.Players.Where(p => !p.CanJoin(realm)); foreach (Player p in playersWhoCantStay) { p.Message("&WYou are no longer allowed to join realm {0}", realm.ClassyName); p.JoinWorld(WorldManager.MainWorld, WorldChangeReason.PermissionChanged); } WorldManager.SaveWorldList(); } }
public static void RealmLoad(Player player, Command cmd, string fileName, string worldName, string buildRankName, string accessRankName) { if (worldName == null && player.World == null) { player.Message("When using /realm from console, you must specify the realm name."); return; } if (fileName == null) { // No params given at all return; } string fullFileName = WorldManager.FindMapFile(player, fileName); if (fullFileName == null) { return; } // Loading map into current realm if (worldName == null) { if (!cmd.IsConfirmed) { player.Confirm(cmd, "About to replace THIS REALM with \"{0}\".", fileName); return; } Map map; try { map = MapUtility.Load(fullFileName); } catch (Exception ex) { player.MessageNow("Could not load specified file: {0}: {1}", ex.GetType().Name, ex.Message); return; } World realm = player.World; // Loading to current realm realm.MapChangedBy = player.Name; realm.ChangeMap(map); realm.Players.Message(player, "{0}&S loaded a new map for this realm.", player.ClassyName); player.MessageNow("New map loaded for the realm {0}", realm.ClassyName); Logger.Log(LogType.UserActivity, "{0} loaded new map for realm \"{1}\" from {2}", player.Name, realm.Name, fileName); realm.IsHidden = false; realm.IsRealm = true; WorldManager.SaveWorldList(); } else { // Loading to some other (or new) realm if (!World.IsValidName(worldName)) { player.MessageInvalidWorldName(worldName); return; } Rank buildRank = RankManager.DefaultBuildRank; Rank accessRank = null; if (buildRankName != null) { buildRank = RankManager.FindRank(buildRankName); if (buildRank == null) { player.MessageNoRank(buildRankName); return; } if (accessRankName != null) { accessRank = RankManager.FindRank(accessRankName); if (accessRank == null) { player.MessageNoRank(accessRankName); return; } } } // Retype realm name, if needed if (worldName == "-") { if (player.LastUsedWorldName != null) { worldName = player.LastUsedWorldName; } else { player.Message("Cannot repeat realm name: you haven't used any names yet."); return; } } lock (WorldManager.SyncRoot) { World realm = WorldManager.FindWorldExact(worldName); if (realm != null) { player.LastUsedWorldName = realm.Name; // Replacing existing realm's map if (!cmd.IsConfirmed) { player.Confirm(cmd, "About to replace realm map for {0}&S with \"{1}\".", realm.ClassyName, fileName); return; } Map map; try { map = MapUtility.Load(fullFileName); realm.IsHidden = false; realm.IsRealm = true; WorldManager.SaveWorldList(); } catch (Exception ex) { player.MessageNow("Could not load specified file: {0}: {1}", ex.GetType().Name, ex.Message); return; } try { realm.MapChangedBy = player.Name; realm.ChangeMap(map); realm.IsHidden = false; realm.IsRealm = true; WorldManager.SaveWorldList(); } catch (WorldOpException ex) { Logger.Log(LogType.Error, "Could not complete RealmLoad operation: {0}", ex.Message); player.Message("&WRealmLoad: {0}", ex.Message); return; } realm.Players.Message(player, "{0}&S loaded a new map for the realm {1}", player.ClassyName, realm.ClassyName); player.MessageNow("New map for the realm {0}&S has been loaded.", realm.ClassyName); Logger.Log(LogType.UserActivity, "{0} loaded new map for realm \"{1}\" from {2}", player.Name, realm.Name, fullFileName); } else { // Adding a new realm string targetFullFileName = Path.Combine(Paths.MapPath, worldName + ".fcm"); if (!cmd.IsConfirmed && File.Exists(targetFullFileName) && // target file already exists !Paths.Compare(targetFullFileName, fullFileName)) { // and is different from sourceFile player.Confirm(cmd, "A map named \"{0}\" already exists, and will be overwritten with \"{1}\".", Path.GetFileName(targetFullFileName), Path.GetFileName(fullFileName)); return; } Map map; try { map = MapUtility.Load(fullFileName); //realm.IsHidden = false; //realm.IsRealm = true; //WorldManager.SaveWorldList(); } catch (Exception ex) { player.MessageNow("Could not load \"{0}\": {1}: {2}", fileName, ex.GetType().Name, ex.Message); return; } World newWorld; try { newWorld = WorldManager.AddWorld(player, worldName, map, false); } catch (WorldOpException ex) { player.Message("RealmLoad: {0}", ex.Message); return; } player.LastUsedWorldName = worldName; newWorld.BuildSecurity.MinRank = buildRank; if (accessRank == null) { newWorld.AccessSecurity.ResetMinRank(); } else { newWorld.AccessSecurity.MinRank = accessRank; } newWorld.BlockDB.AutoToggleIfNeeded(); if (BlockDB.IsEnabledGlobally && newWorld.BlockDB.IsEnabled) { player.Message("BlockDB is now auto-enabled on realm {0}", newWorld.ClassyName); } newWorld.LoadedBy = player.Name; newWorld.LoadedOn = DateTime.UtcNow; Server.Message("{0}&S created a new realm named {1}", player.ClassyName, newWorld.ClassyName); Logger.Log(LogType.UserActivity, "{0} created a new realm named \"{1}\" (loaded from \"{2}\")", player.Name, worldName, fileName); newWorld.IsHidden = false; newWorld.IsRealm = true; WorldManager.SaveWorldList(); player.MessageNow("Access permission is {0}+&S, and build permission is {1}+", newWorld.AccessSecurity.MinRank.ClassyName, newWorld.BuildSecurity.MinRank.ClassyName); } } } Server.RequestGc(); }