Rewrite opchat/adminchat perms as extra command permissions for /opchat and /adminchat.

This commit is contained in:
UnknownShadow200 2016-08-31 11:58:58 +10:00
parent f169240c3a
commit 9873c32550
11 changed files with 105 additions and 75 deletions

View File

@ -68,7 +68,7 @@ namespace MCGalaxy {
public static void MessageWhere(string message, Predicate<Player> filter) { public static void MessageWhere(string message, Predicate<Player> filter) {
Player[] players = PlayerInfo.Online.Items; Player[] players = PlayerInfo.Online.Items;
foreach (Player p in players) { 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 {
/// <summary> Sends a message to all players who are have the permission to read opchat. </summary> /// <summary> Sends a message to all players who are have the permission to read opchat. </summary>
public static void MessageOps(string message) { 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);
} }
/// <summary> Sends a message to all players who are have the permission to read adminchat. </summary> /// <summary> Sends a message to all players who are have the permission to read adminchat. </summary>
public static void MessageAdmins(string message) { 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);
} }
/// <summary> Sends a message to all players, who do not have /// <summary> Sends a message to all players, who do not have

View File

@ -60,11 +60,13 @@ namespace MCGalaxy {
} }
public static void MessageOps(Player p, string message) { 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) { 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, public static void MessageStaff(Player p, string message,
@ -76,7 +78,7 @@ namespace MCGalaxy {
Chat.MessageWhere(format, Chat.MessageWhere(format,
pl => pl.Rank >= perm && !pl.listignored.Contains(name), pl => pl.Rank >= perm && !pl.listignored.Contains(name),
displayName, message); displayName, message);
if (p != null && p.Rank < Server.adminchatperm) if (p != null && p.Rank < perm)
Player.Message(p, format, displayName, message); Player.Message(p, format, displayName, message);
Server.s.Log("(" + group + "): " + name + ": " + message); Server.s.Log("(" + group + "): " + name + ": " + message);

View File

@ -18,7 +18,10 @@ namespace MCGalaxy.Commands {
public override string shortcut { get { return ""; } } public override string shortcut { get { return ""; } }
public override string type { get { return CommandTypes.Chat; } } public override string type { get { return CommandTypes.Chat; } }
public override bool museumUsable { get { return true; } } 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 CmdAdminChat() { }
public override void Use(Player p, string message) { public override void Use(Player p, string message) {

View File

@ -21,7 +21,10 @@ namespace MCGalaxy.Commands {
public override string shortcut { get { return ""; } } public override string shortcut { get { return ""; } }
public override string type { get { return CommandTypes.Chat; } } public override string type { get { return CommandTypes.Chat; } }
public override bool museumUsable { get { return true; } } 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 CmdOpChat() { }
public override void Use(Player p, string message) { public override void Use(Player p, string message) {

View File

@ -29,7 +29,7 @@ namespace MCGalaxy {
public static List<OtherPerms> list = new List<OtherPerms>(); public static List<OtherPerms> list = new List<OtherPerms>();
public class OtherPerms { public class OtherPerms {
public Command cmd; public string cmd;
public int Permission; public int Permission;
public string Description = ""; public string Description = "";
public int number; public int number;
@ -40,30 +40,32 @@ namespace MCGalaxy {
return otpe.Permission; 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) { foreach (OtherPerms perms in list) {
if (perms.cmd == cmd && perms.number == number) return perms; if (perms.cmd == cmd && perms.number == number) return perms;
} }
return null; 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) { public static void Add(Command command, int Perm, string desc, int number = 1) {
if (Perm > 120) return; if (Perm > 120) return;
OtherPerms otpe = new OtherPerms(); OtherPerms otpe = new OtherPerms();
otpe.cmd = command; otpe.cmd = command.name;
otpe.Permission = Perm; otpe.Permission = Perm;
otpe.Description = desc; otpe.Description = desc;
otpe.number = number; otpe.number = number;
list.Add(otpe); list.Add(otpe);
} }
public static void Edit(OtherPerms op, int perm) { [Obsolete("This method is completely unnecessary")]
if (perm > 120) return; public static void Edit(OtherPerms op, int perm) { op.Permission = perm; }
OtherPerms otpe = op;
list.Remove(op);
otpe.Permission = perm;
list.Add(otpe);
}
public static int GetMaxNumber(Command cmd) { public static int GetMaxNumber(Command cmd) {
for (int i = 1; ; i++) { for (int i = 1; ; i++) {
@ -77,8 +79,9 @@ namespace MCGalaxy {
SaveCore(); SaveCore();
} }
const string file = "properties/ExtraCommandPermissions.properties";
static void SaveCore() { 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("# This file is used for setting up additional permissions that are needed in commands!!");
w.WriteLine("#"); w.WriteLine("#");
w.WriteLine("# LAYOUT:"); 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("# Please also note that descriptions cannot contain ':' and permissions cannot be above 120");
w.WriteLine("#"); w.WriteLine("#");
foreach (OtherPerms otpe in list) { foreach (OtherPerms perms in list) {
try { 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) { } catch (Exception ex) {
Server.s.Log("Saving an additional command permission failed."); Server.s.Log("Saving an additional command permission failed.");
Server.ErrorLog(ex); Server.ErrorLog(ex);
@ -102,21 +105,17 @@ namespace MCGalaxy {
public static void Load() { public static void Load() {
if (list.Count == 0) { AddDefaultPerms(); } if (list.Count == 0) { AddDefaultPerms(); }
if (!File.Exists("properties/ExtraCommandPermissions.properties")) if (!File.Exists(file)) Save();
Save();
using (StreamReader r = new StreamReader("properties/ExtraCommandPermissions.properties")) { using (StreamReader r = new StreamReader(file)) {
string line; string line;
while ((line = r.ReadLine()) != null) { while ((line = r.ReadLine()) != null) {
if (line.Length == 0 || line[0] == '#' || line.IndexOf(':') == -1) continue;
try { try {
if (!line.StartsWith("#") && line.IndexOf(':') >= 0) { string[] parts = line.ToLower().Split(':');
string[] parts = line.ToLower().Split(':'); OtherPerms perms = Find(parts[0], int.Parse(parts[1]));
Command cmd = Command.all.Find(parts[0]); if (perms == null) continue; // command has no additional perms, so skip
perms.Permission = int.Parse(parts[2]);
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]));
}
} catch (Exception ex) { } catch (Exception ex) {
Server.s.Log("Loading an additional command permission failed!!"); Server.s.Log("Loading an additional command permission failed!!");
Server.ErrorLog(ex); Server.ErrorLog(ex);

View File

@ -36,8 +36,8 @@ namespace MCGalaxy {
} }
Server.salt = sb.ToString(); Server.salt = sb.ToString();
reviewPerms = new ReviewPerms(); oldPerms = new OldPerms();
if (PropertiesFile.Read(givenPath, ref reviewPerms, LineProcessor)) if (PropertiesFile.Read(givenPath, ref oldPerms, LineProcessor))
Server.s.SettingsUpdate(); Server.s.SettingsUpdate();
if (!Directory.Exists(Server.backupLocation)) if (!Directory.Exists(Server.backupLocation))
@ -46,7 +46,7 @@ namespace MCGalaxy {
Save(givenPath); 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()) { switch (key.ToLower()) {
// Backwards compatibility with old config, where review permissions where global // Backwards compatibility with old config, where review permissions where global
case "review-enter-perm": case "review-enter-perm":
@ -58,6 +58,10 @@ namespace MCGalaxy {
perms.nextPerm = int.Parse(value); break; perms.nextPerm = int.Parse(value); break;
case "review-clear-perm": case "review-clear-perm":
perms.clearPerm = int.Parse(value); break; 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: default:
if (!ConfigElement.Parse(Server.serverConfig, key, value, null)) if (!ConfigElement.Parse(Server.serverConfig, key, value, null))
@ -65,8 +69,9 @@ namespace MCGalaxy {
break; break;
} }
} }
internal static ReviewPerms reviewPerms; internal static OldPerms oldPerms;
internal class ReviewPerms { public int viewPerm = -1, nextPerm = -1, clearPerm = -1; } internal class OldPerms { public int viewPerm = -1, nextPerm = -1,
clearPerm = -1, opchatPerm = -1, adminchatPerm = -1; }
public static void Save() { Save("properties/server.properties"); } public static void Save() { Save("properties/server.properties"); }
static readonly object saveLock = new object(); static readonly object saveLock = new object();

View File

@ -49,14 +49,14 @@ namespace MCGalaxy.Gui {
ToggleMySQLSettings(Server.useMySQL); ToggleMySQLSettings(Server.useMySQL);
ToggleAutoMuteSettings(Server.checkspam); ToggleAutoMuteSettings(Server.checkspam);
string opchatperm = String.Empty; string opchatperm = "", adminchatperm = "";
string adminchatperm = String.Empty; string verifyadminsperm = "", afkkickrank = "", osmaprank = "";
string verifyadminsperm = String.Empty; LevelPermission adminChatRank =
string grieferstonerank = String.Empty; CommandOtherPerms.FindPerm("adminchat", LevelPermission.Admin);
string afkkickrank = String.Empty; LevelPermission opChatRank =
string osmaprank = String.Empty; CommandOtherPerms.FindPerm("opchat", LevelPermission.Operator);
foreach ( Group grp in Group.GroupList ) { foreach (Group grp in Group.GroupList) {
cmbDefaultRank.Items.Add(grp.name); cmbDefaultRank.Items.Add(grp.name);
cmbOpChat.Items.Add(grp.name); cmbOpChat.Items.Add(grp.name);
cmbAdminChat.Items.Add(grp.name); cmbAdminChat.Items.Add(grp.name);
@ -66,15 +66,15 @@ namespace MCGalaxy.Gui {
cmbAFKKickPerm.Items.Add(grp.name); cmbAFKKickPerm.Items.Add(grp.name);
cmbOsMap.Items.Add(grp.name); cmbOsMap.Items.Add(grp.name);
if ( grp.Permission == Server.opchatperm ) if (grp.Permission == opChatRank)
opchatperm = grp.name; opchatperm = grp.name;
if ( grp.Permission == Server.adminchatperm ) if (grp.Permission == adminChatRank)
adminchatperm = grp.name; adminchatperm = grp.name;
if ( grp.Permission == Server.verifyadminsrank ) if (grp.Permission == Server.verifyadminsrank)
verifyadminsperm = grp.name; verifyadminsperm = grp.name;
if ( grp.Permission == Server.afkkickperm ) if (grp.Permission == Server.afkkickperm)
afkkickrank = grp.name; afkkickrank = grp.name;
if( grp.Permission == Server.osPerbuildDefault ) if (grp.Permission == Server.osPerbuildDefault)
osmaprank = grp.name; osmaprank = grp.name;
} }
@ -541,6 +541,7 @@ namespace MCGalaxy.Gui {
try { try {
ApplyAll(); ApplyAll();
SrvProperties.Save(); SrvProperties.Save();
CommandOtherPerms.Save();
} catch( Exception ex ) { } catch( Exception ex ) {
Server.ErrorLog(ex); Server.ErrorLog(ex);
Server.s.Log("SAVE FAILED! properties/server.properties"); Server.s.Log("SAVE FAILED! properties/server.properties");
@ -585,9 +586,11 @@ namespace MCGalaxy.Gui {
Server.moneys = txtMoneys.Text; Server.moneys = txtMoneys.Text;
Server.osPerbuildDefault = Group.Find(cmbOsMap.SelectedItem.ToString()).Permission; 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.afkkickperm = Group.Find(cmbAFKKickPerm.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.logbeat = chkLogBeat.Checked;
Server.profanityFilter = chkProfanityFilter.Checked; Server.profanityFilter = chkProfanityFilter.Checked;
@ -1237,7 +1240,7 @@ txtBackupLocation.Text = folderDialog.SelectedPath;
private void SaveOldExtraCustomCmdChanges() { private void SaveOldExtraCustomCmdChanges() {
if (oldcmd == null || skipExtraPermChanges) return; 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(); CommandOtherPerms.Save();
} }

View File

@ -393,11 +393,19 @@ namespace MCGalaxy {
return load; 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);
}
/// <summary> Sends a chat messages to all players in the level, who have at least the minPerm rank. </summary> /// <summary> Sends a chat messages to all players in the level, who have at least the minPerm rank. </summary>
public void ChatLevel(string message, LevelPermission minPerm) { public void ChatLevel(string message, LevelPermission minPerm) {

View File

@ -196,6 +196,8 @@ namespace MCGalaxy {
} }
void CompleteLoginProcess() { void CompleteLoginProcess() {
LevelPermission adminChatRank = CommandOtherPerms.FindPerm("adminchat", LevelPermission.Admin);
try { try {
SendUserMOTD(); SendUserMOTD();
SendMap(null); SendMap(null);
@ -205,7 +207,6 @@ namespace MCGalaxy {
PlayerInfo.Online.Add(this); PlayerInfo.Online.Add(this);
connections.Remove(this); connections.Remove(this);
RemoveFromPending(); RemoveFromPending();
Server.s.PlayerListUpdate(); Server.s.PlayerListUpdate();
//Test code to show when people come back with different accounts on the same IP //Test code to show when people come back with different accounts on the same IP
@ -213,14 +214,14 @@ namespace MCGalaxy {
bool found = false; bool found = false;
if (!ip.StartsWith("127.0.0.")) { if (!ip.StartsWith("127.0.0.")) {
foreach (KeyValuePair<string, string> prev in left) { foreach (KeyValuePair<string, string> prev in left) {
if (prev.Value == ip) if (prev.Value == ip) {
{
found = true; found = true;
alts += " " + prev.Key; alts += " " + prev.Key;
} }
} }
if (found) { if (found) {
if (group.Permission < Server.adminchatperm || !Server.adminsjoinsilent) { if (group.Permission < adminChatRank || !Server.adminsjoinsilent) {
Chat.MessageOps(alts); Chat.MessageOps(alts);
//IRCBot.Say(temp, true); //Tells people in op channel on IRC //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); hidden = group.CanExecute("hide") && Server.hidden.Contains(name);
if (hidden) SendMessage("&8Reminder: You are still hidden."); 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; hidden = true; adminchat = true;
} }

View File

@ -336,9 +336,7 @@ namespace MCGalaxy {
[ConfigString("money-name", "Other", null, "moneys")] [ConfigString("money-name", "Other", null, "moneys")]
public static string moneys = "moneys"; public static string moneys = "moneys";
[ConfigPerm("opchat-perm", "Other", null, LevelPermission.Operator)]
public static LevelPermission opchatperm = LevelPermission.Operator; public static LevelPermission opchatperm = LevelPermission.Operator;
[ConfigPerm("adminchat-perm", "Other", null, LevelPermission.Admin)]
public static LevelPermission adminchatperm = LevelPermission.Admin; public static LevelPermission adminchatperm = LevelPermission.Admin;
[ConfigBool("log-heartbeat", "Other", null, false)] [ConfigBool("log-heartbeat", "Other", null, false)]

View File

@ -219,21 +219,27 @@ namespace MCGalaxy {
ProfanityFilter.Init(); ProfanityFilter.Init();
Team.LoadList(); Team.LoadList();
ChatTokens.LoadCustom(); ChatTokens.LoadCustom();
FixupOldReviewPerms(); FixupOldPerms();
} }
static void FixupOldReviewPerms() { static void FixupOldPerms() {
Command cmd = Command.all.Find("review"); var perms = SrvProperties.oldPerms;
var perms = SrvProperties.reviewPerms; Server.opchatperm = CommandOtherPerms.FindPerm("opchat", LevelPermission.Operator);
if (perms.clearPerm == -1 && perms.nextPerm == -1 && perms.viewPerm == -1) return; 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) if (perms.viewPerm != -1)
CommandOtherPerms.Edit(CommandOtherPerms.Find(cmd, 1), perms.viewPerm); CommandOtherPerms.Find("review", 1).Permission = perms.viewPerm;
if (perms.nextPerm != -1) if (perms.nextPerm != -1)
CommandOtherPerms.Edit(CommandOtherPerms.Find(cmd, 2), perms.nextPerm); CommandOtherPerms.Find("review", 2).Permission = perms.nextPerm;
if (perms.clearPerm != -1) 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(); CommandOtherPerms.Save();
} }