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) {
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 {
/// <summary> Sends a message to all players who are have the permission to read opchat. </summary>
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>
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

View File

@ -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);

View File

@ -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) {

View File

@ -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) {

View File

@ -19,7 +19,7 @@ using System;
using System.Collections.Generic;
using System.IO;
namespace MCGalaxy {
namespace MCGalaxy {
/// <summary> These are extra permissions for certain commands </summary>
public static class CommandOtherPerms {
@ -29,7 +29,7 @@ namespace MCGalaxy {
public static List<OtherPerms> list = new List<OtherPerms>();
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);

View File

@ -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();

View File

@ -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();
}

View File

@ -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);
}
/// <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) {

View File

@ -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<string, string> 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;
}

View File

@ -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)]

View File

@ -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();
}