From 9873c3255077ddb0c100aa217c2b0e9419673805 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 31 Aug 2016 11:58:58 +1000 Subject: [PATCH] Rewrite opchat/adminchat perms as extra command permissions for /opchat and /adminchat. --- Chat/Chat.cs | 8 +++--- Chat/ChatModes.cs | 8 +++--- Commands/Chat/CmdAdminChat.cs | 5 +++- Commands/Chat/CmdOpChat.cs | 5 +++- Commands/CommandOtherPerms.cs | 49 +++++++++++++++++------------------ Config/Properties.cs | 15 +++++++---- GUI/PropertyWindow.cs | 35 +++++++++++++------------ Levels/Level.cs | 14 +++++++--- Player/Player.Login.cs | 11 ++++---- Server/Server.Fields.cs | 6 ++--- Server/Server.cs | 24 ++++++++++------- 11 files changed, 105 insertions(+), 75 deletions(-) diff --git a/Chat/Chat.cs b/Chat/Chat.cs index babc62c71..515ccff88 100644 --- a/Chat/Chat.cs +++ b/Chat/Chat.cs @@ -68,7 +68,7 @@ namespace MCGalaxy { public static void MessageWhere(string message, Predicate filter) { Player[] players = PlayerInfo.Online.Items; foreach (Player p in players) { - if (filter(p)) Player.Message(p, message); + if (filter(p)) Player.Message(p, message); } } @@ -79,12 +79,14 @@ namespace MCGalaxy { /// Sends a message to all players who are have the permission to read opchat. public static void MessageOps(string message) { - MessageWhere(message, pl => pl.Rank >= Server.opchatperm); + LevelPermission rank = CommandOtherPerms.FindPerm("opchat", LevelPermission.Operator); + MessageWhere(message, pl => pl.Rank >= rank); } /// Sends a message to all players who are have the permission to read adminchat. public static void MessageAdmins(string message) { - MessageWhere(message, pl => pl.Rank >= Server.adminchatperm); + LevelPermission rank = CommandOtherPerms.FindPerm("adminchat", LevelPermission.Admin); + MessageWhere(message, pl => pl.Rank >= rank); } /// Sends a message to all players, who do not have diff --git a/Chat/ChatModes.cs b/Chat/ChatModes.cs index e0d525e32..5b30010a5 100644 --- a/Chat/ChatModes.cs +++ b/Chat/ChatModes.cs @@ -60,11 +60,13 @@ namespace MCGalaxy { } public static void MessageOps(Player p, string message) { - MessageStaff(p, message, Server.opchatperm, "Ops"); + LevelPermission rank = CommandOtherPerms.FindPerm("opchat", LevelPermission.Operator); + MessageStaff(p, message, rank, "Ops"); } public static void MessageAdmins(Player p, string message) { - MessageStaff(p, message, Server.adminchatperm, "Admins"); + LevelPermission rank = CommandOtherPerms.FindPerm("adminchat", LevelPermission.Admin); + MessageStaff(p, message, rank, "Admins"); } public static void MessageStaff(Player p, string message, @@ -76,7 +78,7 @@ namespace MCGalaxy { Chat.MessageWhere(format, pl => pl.Rank >= perm && !pl.listignored.Contains(name), displayName, message); - if (p != null && p.Rank < Server.adminchatperm) + if (p != null && p.Rank < perm) Player.Message(p, format, displayName, message); Server.s.Log("(" + group + "): " + name + ": " + message); diff --git a/Commands/Chat/CmdAdminChat.cs b/Commands/Chat/CmdAdminChat.cs index d5cd23227..b4129abaa 100644 --- a/Commands/Chat/CmdAdminChat.cs +++ b/Commands/Chat/CmdAdminChat.cs @@ -18,7 +18,10 @@ namespace MCGalaxy.Commands { public override string shortcut { get { return ""; } } public override string type { get { return CommandTypes.Chat; } } public override bool museumUsable { get { return true; } } - public override LevelPermission defaultRank { get { return LevelPermission.Admin; } } + public override LevelPermission defaultRank { get { return LevelPermission.Guest; } } + public override CommandPerm[] ExtraPerms { + get { return new[] { new CommandPerm(LevelPermission.Admin, "+ can read adminchat messages") }; } + } public CmdAdminChat() { } public override void Use(Player p, string message) { diff --git a/Commands/Chat/CmdOpChat.cs b/Commands/Chat/CmdOpChat.cs index 3a0f93086..b5a4abc91 100644 --- a/Commands/Chat/CmdOpChat.cs +++ b/Commands/Chat/CmdOpChat.cs @@ -21,7 +21,10 @@ namespace MCGalaxy.Commands { public override string shortcut { get { return ""; } } public override string type { get { return CommandTypes.Chat; } } public override bool museumUsable { get { return true; } } - public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } } + public override LevelPermission defaultRank { get { return LevelPermission.Guest; } } + public override CommandPerm[] ExtraPerms { + get { return new[] { new CommandPerm(LevelPermission.Operator, "+ can read opchat messages") }; } + } public CmdOpChat() { } public override void Use(Player p, string message) { diff --git a/Commands/CommandOtherPerms.cs b/Commands/CommandOtherPerms.cs index ae064ddce..bbf70d803 100644 --- a/Commands/CommandOtherPerms.cs +++ b/Commands/CommandOtherPerms.cs @@ -19,7 +19,7 @@ using System; using System.Collections.Generic; using System.IO; -namespace MCGalaxy { +namespace MCGalaxy { /// These are extra permissions for certain commands public static class CommandOtherPerms { @@ -29,7 +29,7 @@ namespace MCGalaxy { public static List list = new List(); public class OtherPerms { - public Command cmd; + public string cmd; public int Permission; public string Description = ""; public int number; @@ -40,30 +40,32 @@ namespace MCGalaxy { return otpe.Permission; } - public static OtherPerms Find(Command cmd, int number = 1) { + public static OtherPerms Find(Command cmd, int number = 1) { return Find(cmd.name, number); } + + public static OtherPerms Find(string cmd, int number = 1) { foreach (OtherPerms perms in list) { if (perms.cmd == cmd && perms.number == number) return perms; } return null; } + + public static LevelPermission FindPerm(string cmd, LevelPermission defPerm, int number = 1) { + OtherPerms perms = Find(cmd, number); + return perms == null ? defPerm : (LevelPermission)perms.Permission; + } public static void Add(Command command, int Perm, string desc, int number = 1) { if (Perm > 120) return; OtherPerms otpe = new OtherPerms(); - otpe.cmd = command; + otpe.cmd = command.name; otpe.Permission = Perm; otpe.Description = desc; otpe.number = number; list.Add(otpe); } - public static void Edit(OtherPerms op, int perm) { - if (perm > 120) return; - OtherPerms otpe = op; - list.Remove(op); - otpe.Permission = perm; - list.Add(otpe); - } + [Obsolete("This method is completely unnecessary")] + public static void Edit(OtherPerms op, int perm) { op.Permission = perm; } public static int GetMaxNumber(Command cmd) { for (int i = 1; ; i++) { @@ -77,8 +79,9 @@ namespace MCGalaxy { SaveCore(); } + const string file = "properties/ExtraCommandPermissions.properties"; static void SaveCore() { - using (StreamWriter w = new StreamWriter("properties/ExtraCommandPermissions.properties")) { + using (StreamWriter w = new StreamWriter(file)) { w.WriteLine("# This file is used for setting up additional permissions that are needed in commands!!"); w.WriteLine("#"); w.WriteLine("# LAYOUT:"); @@ -89,9 +92,9 @@ namespace MCGalaxy { w.WriteLine("# Please also note that descriptions cannot contain ':' and permissions cannot be above 120"); w.WriteLine("#"); - foreach (OtherPerms otpe in list) { + foreach (OtherPerms perms in list) { try { - w.WriteLine(otpe.cmd.name + ":" + otpe.number + ":" + otpe.Permission + ":" + otpe.Description); + w.WriteLine(perms.cmd + ":" + perms.number + ":" + perms.Permission + ":" + perms.Description); } catch (Exception ex) { Server.s.Log("Saving an additional command permission failed."); Server.ErrorLog(ex); @@ -102,21 +105,17 @@ namespace MCGalaxy { public static void Load() { if (list.Count == 0) { AddDefaultPerms(); } - if (!File.Exists("properties/ExtraCommandPermissions.properties")) - Save(); + if (!File.Exists(file)) Save(); - using (StreamReader r = new StreamReader("properties/ExtraCommandPermissions.properties")) { + using (StreamReader r = new StreamReader(file)) { string line; while ((line = r.ReadLine()) != null) { + if (line.Length == 0 || line[0] == '#' || line.IndexOf(':') == -1) continue; try { - if (!line.StartsWith("#") && line.IndexOf(':') >= 0) { - string[] parts = line.ToLower().Split(':'); - Command cmd = Command.all.Find(parts[0]); - - OtherPerms perms = Find(cmd, int.Parse(parts[1])); - if (perms == null && cmd != null) continue; // command has no additional perms, so skip - Edit(perms, int.Parse(parts[2])); - } + string[] parts = line.ToLower().Split(':'); + OtherPerms perms = Find(parts[0], int.Parse(parts[1])); + if (perms == null) continue; // command has no additional perms, so skip + perms.Permission = int.Parse(parts[2]); } catch (Exception ex) { Server.s.Log("Loading an additional command permission failed!!"); Server.ErrorLog(ex); diff --git a/Config/Properties.cs b/Config/Properties.cs index f54ec2bc7..881d10788 100644 --- a/Config/Properties.cs +++ b/Config/Properties.cs @@ -36,8 +36,8 @@ namespace MCGalaxy { } Server.salt = sb.ToString(); - reviewPerms = new ReviewPerms(); - if (PropertiesFile.Read(givenPath, ref reviewPerms, LineProcessor)) + oldPerms = new OldPerms(); + if (PropertiesFile.Read(givenPath, ref oldPerms, LineProcessor)) Server.s.SettingsUpdate(); if (!Directory.Exists(Server.backupLocation)) @@ -46,7 +46,7 @@ namespace MCGalaxy { Save(givenPath); } - static void LineProcessor(string key, string value, ref ReviewPerms perms) { + static void LineProcessor(string key, string value, ref OldPerms perms) { switch (key.ToLower()) { // Backwards compatibility with old config, where review permissions where global case "review-enter-perm": @@ -58,6 +58,10 @@ namespace MCGalaxy { perms.nextPerm = int.Parse(value); break; case "review-clear-perm": perms.clearPerm = int.Parse(value); break; + case "opchat-perm": + perms.opchatPerm = int.Parse(value); break; + case "adminchat-perm": + perms.adminchatPerm = int.Parse(value); break; default: if (!ConfigElement.Parse(Server.serverConfig, key, value, null)) @@ -65,8 +69,9 @@ namespace MCGalaxy { break; } } - internal static ReviewPerms reviewPerms; - internal class ReviewPerms { public int viewPerm = -1, nextPerm = -1, clearPerm = -1; } + internal static OldPerms oldPerms; + internal class OldPerms { public int viewPerm = -1, nextPerm = -1, + clearPerm = -1, opchatPerm = -1, adminchatPerm = -1; } public static void Save() { Save("properties/server.properties"); } static readonly object saveLock = new object(); diff --git a/GUI/PropertyWindow.cs b/GUI/PropertyWindow.cs index f3a390313..a90d4430b 100644 --- a/GUI/PropertyWindow.cs +++ b/GUI/PropertyWindow.cs @@ -49,14 +49,14 @@ namespace MCGalaxy.Gui { ToggleMySQLSettings(Server.useMySQL); ToggleAutoMuteSettings(Server.checkspam); - string opchatperm = String.Empty; - string adminchatperm = String.Empty; - string verifyadminsperm = String.Empty; - string grieferstonerank = String.Empty; - string afkkickrank = String.Empty; - string osmaprank = String.Empty; + string opchatperm = "", adminchatperm = ""; + string verifyadminsperm = "", afkkickrank = "", osmaprank = ""; + LevelPermission adminChatRank = + CommandOtherPerms.FindPerm("adminchat", LevelPermission.Admin); + LevelPermission opChatRank = + CommandOtherPerms.FindPerm("opchat", LevelPermission.Operator); - foreach ( Group grp in Group.GroupList ) { + foreach (Group grp in Group.GroupList) { cmbDefaultRank.Items.Add(grp.name); cmbOpChat.Items.Add(grp.name); cmbAdminChat.Items.Add(grp.name); @@ -66,15 +66,15 @@ namespace MCGalaxy.Gui { cmbAFKKickPerm.Items.Add(grp.name); cmbOsMap.Items.Add(grp.name); - if ( grp.Permission == Server.opchatperm ) + if (grp.Permission == opChatRank) opchatperm = grp.name; - if ( grp.Permission == Server.adminchatperm ) + if (grp.Permission == adminChatRank) adminchatperm = grp.name; - if ( grp.Permission == Server.verifyadminsrank ) + if (grp.Permission == Server.verifyadminsrank) verifyadminsperm = grp.name; - if ( grp.Permission == Server.afkkickperm ) + if (grp.Permission == Server.afkkickperm) afkkickrank = grp.name; - if( grp.Permission == Server.osPerbuildDefault ) + if (grp.Permission == Server.osPerbuildDefault) osmaprank = grp.name; } @@ -541,6 +541,7 @@ namespace MCGalaxy.Gui { try { ApplyAll(); SrvProperties.Save(); + CommandOtherPerms.Save(); } catch( Exception ex ) { Server.ErrorLog(ex); Server.s.Log("SAVE FAILED! properties/server.properties"); @@ -584,10 +585,12 @@ namespace MCGalaxy.Gui { //Server.useWhitelist = ; //We don't have a setting for this? Server.moneys = txtMoneys.Text; - Server.osPerbuildDefault = Group.Find(cmbOsMap.SelectedItem.ToString()).Permission; - Server.opchatperm = Group.Find(cmbOpChat.SelectedItem.ToString()).Permission; - Server.adminchatperm = Group.Find(cmbAdminChat.SelectedItem.ToString()).Permission; + Server.osPerbuildDefault = Group.Find(cmbOsMap.SelectedItem.ToString()).Permission; Server.afkkickperm = Group.Find(cmbAFKKickPerm.SelectedItem.ToString()).Permission; + var perms = CommandOtherPerms.Find("opchat"); + perms.Permission = (int)Group.Find(cmbOpChat.SelectedItem.ToString()).Permission; + perms = CommandOtherPerms.Find("adminchat"); + perms.Permission = (int)Group.Find(cmbAdminChat.SelectedItem.ToString()).Permission; Server.logbeat = chkLogBeat.Checked; Server.profanityFilter = chkProfanityFilter.Checked; @@ -1237,7 +1240,7 @@ txtBackupLocation.Text = folderDialog.SelectedPath; private void SaveOldExtraCustomCmdChanges() { if (oldcmd == null || skipExtraPermChanges) return; - CommandOtherPerms.Edit(CommandOtherPerms.Find(oldcmd, oldnumber), int.Parse(extracmdpermperm.Text)); + CommandOtherPerms.Find(oldcmd, oldnumber).Permission = int.Parse(extracmdpermperm.Text); CommandOtherPerms.Save(); } diff --git a/Levels/Level.cs b/Levels/Level.cs index a21fb0660..4c383eae9 100644 --- a/Levels/Level.cs +++ b/Levels/Level.cs @@ -393,11 +393,19 @@ namespace MCGalaxy { return load; } - public void ChatLevel(string message) { ChatLevel(message, LevelPermission.Banned); } + public void ChatLevel(string message) { + ChatLevel(message, LevelPermission.Banned); + } - public void ChatLevelOps(string message) { ChatLevel(message, Server.opchatperm); } + public void ChatLevelOps(string message) { + LevelPermission rank = CommandOtherPerms.FindPerm("opchat", LevelPermission.Operator); + ChatLevel(message, rank); + } - public void ChatLevelAdmins(string message) { ChatLevel(message, Server.adminchatperm); } + public void ChatLevelAdmins(string message) { + LevelPermission rank = CommandOtherPerms.FindPerm("adminchat", LevelPermission.Admin); + ChatLevel(message, rank); + } /// Sends a chat messages to all players in the level, who have at least the minPerm rank. public void ChatLevel(string message, LevelPermission minPerm) { diff --git a/Player/Player.Login.cs b/Player/Player.Login.cs index 2407112f2..1aa89c25d 100644 --- a/Player/Player.Login.cs +++ b/Player/Player.Login.cs @@ -196,6 +196,8 @@ namespace MCGalaxy { } void CompleteLoginProcess() { + LevelPermission adminChatRank = CommandOtherPerms.FindPerm("adminchat", LevelPermission.Admin); + try { SendUserMOTD(); SendMap(null); @@ -205,7 +207,6 @@ namespace MCGalaxy { PlayerInfo.Online.Add(this); connections.Remove(this); RemoveFromPending(); - Server.s.PlayerListUpdate(); //Test code to show when people come back with different accounts on the same IP @@ -213,14 +214,14 @@ namespace MCGalaxy { bool found = false; if (!ip.StartsWith("127.0.0.")) { foreach (KeyValuePair prev in left) { - if (prev.Value == ip) - { + if (prev.Value == ip) { found = true; alts += " " + prev.Key; } } + if (found) { - if (group.Permission < Server.adminchatperm || !Server.adminsjoinsilent) { + if (group.Permission < adminChatRank || !Server.adminsjoinsilent) { Chat.MessageOps(alts); //IRCBot.Say(temp, true); //Tells people in op channel on IRC } @@ -264,7 +265,7 @@ namespace MCGalaxy { hidden = group.CanExecute("hide") && Server.hidden.Contains(name); if (hidden) SendMessage("&8Reminder: You are still hidden."); - if (group.Permission >= Server.adminchatperm && Server.adminsjoinsilent) { + if (group.Permission >= adminChatRank && Server.adminsjoinsilent) { hidden = true; adminchat = true; } diff --git a/Server/Server.Fields.cs b/Server/Server.Fields.cs index 920c67abd..b8ad329d0 100644 --- a/Server/Server.Fields.cs +++ b/Server/Server.Fields.cs @@ -335,10 +335,8 @@ namespace MCGalaxy { public static string defaultDemoteMessage = "&4DEMOTED! &6We're sorry for your loss. Good luck on your future endeavors! &1:'("; [ConfigString("money-name", "Other", null, "moneys")] - public static string moneys = "moneys"; - [ConfigPerm("opchat-perm", "Other", null, LevelPermission.Operator)] - public static LevelPermission opchatperm = LevelPermission.Operator; - [ConfigPerm("adminchat-perm", "Other", null, LevelPermission.Admin)] + public static string moneys = "moneys"; + public static LevelPermission opchatperm = LevelPermission.Operator; public static LevelPermission adminchatperm = LevelPermission.Admin; [ConfigBool("log-heartbeat", "Other", null, false)] diff --git a/Server/Server.cs b/Server/Server.cs index dde1a0570..f217c1ee0 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -219,21 +219,27 @@ namespace MCGalaxy { ProfanityFilter.Init(); Team.LoadList(); ChatTokens.LoadCustom(); - FixupOldReviewPerms(); + FixupOldPerms(); } - static void FixupOldReviewPerms() { - Command cmd = Command.all.Find("review"); - var perms = SrvProperties.reviewPerms; - if (perms.clearPerm == -1 && perms.nextPerm == -1 && perms.viewPerm == -1) return; + static void FixupOldPerms() { + var perms = SrvProperties.oldPerms; + Server.opchatperm = CommandOtherPerms.FindPerm("opchat", LevelPermission.Operator); + Server.adminchatperm = CommandOtherPerms.FindPerm("adminchat", LevelPermission.Admin); + if (perms.clearPerm == -1 && perms.nextPerm == -1 && perms.viewPerm == -1 + && perms.opchatPerm == -1 && perms.adminchatPerm == -1) return; - // Backwards compatibility with old config, where review permissions where global + // Backwards compatibility with old config, where some permissions were global if (perms.viewPerm != -1) - CommandOtherPerms.Edit(CommandOtherPerms.Find(cmd, 1), perms.viewPerm); + CommandOtherPerms.Find("review", 1).Permission = perms.viewPerm; if (perms.nextPerm != -1) - CommandOtherPerms.Edit(CommandOtherPerms.Find(cmd, 2), perms.nextPerm); + CommandOtherPerms.Find("review", 2).Permission = perms.nextPerm; if (perms.clearPerm != -1) - CommandOtherPerms.Edit(CommandOtherPerms.Find(cmd, 3), perms.clearPerm); + CommandOtherPerms.Find("review", 3).Permission = perms.clearPerm; + if (perms.opchatPerm != -1) + CommandOtherPerms.Find("opchat").Permission = perms.opchatPerm; + if (perms.adminchatPerm != -1) + CommandOtherPerms.Find("adminchat").Permission = perms.adminchatPerm; CommandOtherPerms.Save(); }