From 9e419f71fb9d89df3196def214d52b1a5f71bc70 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 24 Dec 2016 19:09:35 +1100 Subject: [PATCH] DB: now block changes by IRC properly log to BlockDB, including the irc user's nick. --- MCGalaxy/Commands/Chat/CmdEmote.cs | 2 +- MCGalaxy/Commands/Moderation/CmdFreeze.cs | 2 +- MCGalaxy/Commands/Moderation/CmdHide.cs | 2 +- MCGalaxy/Commands/Moderation/CmdMute.cs | 2 +- MCGalaxy/Commands/World/CmdLockdown.cs | 2 +- MCGalaxy/Database/BlockDB/NameConverter.cs | 8 ++++ MCGalaxy/Network/IRCBot.cs | 53 ++++++++++++---------- MCGalaxy/Player/List/PlayerList.cs | 9 ++-- MCGalaxy/Player/PlayerData.cs | 5 +- MCGalaxy/Server/Tasks/UpgradeTasks.cs | 2 +- 10 files changed, 50 insertions(+), 37 deletions(-) diff --git a/MCGalaxy/Commands/Chat/CmdEmote.cs b/MCGalaxy/Commands/Chat/CmdEmote.cs index 10149bd04..d6bb16126 100644 --- a/MCGalaxy/Commands/Chat/CmdEmote.cs +++ b/MCGalaxy/Commands/Chat/CmdEmote.cs @@ -30,7 +30,7 @@ namespace MCGalaxy.Commands { bool addToList = p.parseEmotes != Server.parseSmiley; if (!addToList) Server.noEmotes.Remove(p.name); - else Server.noEmotes.AddOrReplace(p.name); + else Server.noEmotes.AddIfNotExists(p.name); Server.noEmotes.Save(); Player.Message(p, "Emote parsing is {0}.", p.parseEmotes ? "enabled" : "disabled"); } diff --git a/MCGalaxy/Commands/Moderation/CmdFreeze.cs b/MCGalaxy/Commands/Moderation/CmdFreeze.cs index 9d38030cc..8cbf187b4 100644 --- a/MCGalaxy/Commands/Moderation/CmdFreeze.cs +++ b/MCGalaxy/Commands/Moderation/CmdFreeze.cs @@ -40,7 +40,7 @@ namespace MCGalaxy.Commands Player.SendChatFrom(who, who.ColoredName + " %Swas &bfrozen %Sby " + frozenby + "%S.", false); Server.s.Log(who.name + " was frozen by " + frozenby); Player.AddNote(who.name, p, "F"); - Server.frozen.AddOrReplace(who.name); + Server.frozen.AddIfNotExists(who.name); } else { Player.SendChatFrom(who, who.ColoredName + " %Swas &adefrosted %Sby " + frozenby + "%S.", false); Server.s.Log(who.name + " was defrosted by " + frozenby); diff --git a/MCGalaxy/Commands/Moderation/CmdHide.cs b/MCGalaxy/Commands/Moderation/CmdHide.cs index fe6c2b5f3..4ac91b49b 100644 --- a/MCGalaxy/Commands/Moderation/CmdHide.cs +++ b/MCGalaxy/Commands/Moderation/CmdHide.cs @@ -66,7 +66,7 @@ namespace MCGalaxy.Commands { Player.SendChatFrom(p, "&c- " + p.FullName + " %S" + discMsg, false); Server.IRC.Say(p.DisplayName + " %Sleft the game (" + discMsg + "%S)"); if (messageOps && !p.opchat) opchat.Use(p, message); - Server.hidden.AddOrReplace(p.name); + Server.hidden.AddIfNotExists(p.name); } else { Entities.GlobalSpawn(p, false); p.otherRankHidden = false; diff --git a/MCGalaxy/Commands/Moderation/CmdMute.cs b/MCGalaxy/Commands/Moderation/CmdMute.cs index 194f25483..8b1a44c56 100644 --- a/MCGalaxy/Commands/Moderation/CmdMute.cs +++ b/MCGalaxy/Commands/Moderation/CmdMute.cs @@ -50,7 +50,7 @@ namespace MCGalaxy.Commands.Moderation { } who.muted = true; Player.SendChatFrom(who, who.ColoredName + " %Swas &8muted", false); - Server.muted.AddOrReplace(who.name); + Server.muted.AddIfNotExists(who.name); Player.AddNote(who.name, p, "M"); } Server.muted.Save(); diff --git a/MCGalaxy/Commands/World/CmdLockdown.cs b/MCGalaxy/Commands/World/CmdLockdown.cs index eab2addca..a0de0f1ad 100644 --- a/MCGalaxy/Commands/World/CmdLockdown.cs +++ b/MCGalaxy/Commands/World/CmdLockdown.cs @@ -48,7 +48,7 @@ namespace MCGalaxy.Commands { Server.lockdown.Remove(args[1]); Chat.MessageOps("Unlocked by: " + srcName); } else { - Server.lockdown.AddOrReplace(args[1]); + Server.lockdown.AddIfNotExists(args[1]); Chat.MessageOps("Locked by: " + srcName); } Server.lockdown.Save(); diff --git a/MCGalaxy/Database/BlockDB/NameConverter.cs b/MCGalaxy/Database/BlockDB/NameConverter.cs index 6a80ef473..caf1ccb71 100644 --- a/MCGalaxy/Database/BlockDB/NameConverter.cs +++ b/MCGalaxy/Database/BlockDB/NameConverter.cs @@ -51,5 +51,13 @@ namespace MCGalaxy.DB { } return ids; } + + public static int InvalidNameID(string name) { + bool added = Server.invalidIds.AddIfNotExists(name); + if (added) Server.invalidIds.Save(); + + int index = Server.invalidIds.All().IndexOf(name.ToLower()); + return int.MaxValue - index; + } } } \ No newline at end of file diff --git a/MCGalaxy/Network/IRCBot.cs b/MCGalaxy/Network/IRCBot.cs index 626fe41d9..58aa9cce0 100644 --- a/MCGalaxy/Network/IRCBot.cs +++ b/MCGalaxy/Network/IRCBot.cs @@ -14,12 +14,13 @@ BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the Licenses for the specific language governing permissions and limitations under the Licenses. -*/ + */ using System; using System.Collections.Generic; using System.IO; using System.Text; using MCGalaxy.Commands; +using MCGalaxy.DB; using Sharkbite.Irc; namespace MCGalaxy { @@ -83,8 +84,8 @@ namespace MCGalaxy { UpdateState(); connection.connectionArgs = args; - try { - connection.Connect(); + try { + connection.Connect(); } catch (Exception e) { Server.s.Log("Failed to connect to IRC!"); Server.ErrorLog(e); @@ -95,7 +96,7 @@ namespace MCGalaxy { if (!IsConnected()) return; if (hookedEvents) UnhookEvents(); - connection.Disconnect(reason); + connection.Disconnect(reason); Server.s.Log("Disconnected from IRC!"); users.Clear(); } @@ -151,8 +152,8 @@ namespace MCGalaxy { #region In-game event handlers - void Player_PlayerAction(Player p, PlayerAction action, - string message, bool stealth) { + void Player_PlayerAction(Player p, PlayerAction action, + string message, bool stealth) { if (!Server.irc ||!IsConnected()) return; string msg = null; @@ -192,7 +193,7 @@ namespace MCGalaxy { string name = Server.ircPlayerTitles ? p.FullName : p.group.prefix + p.ColoredName; Say(name + "%S: " + message, p.opchat); - } + } #endregion @@ -249,7 +250,7 @@ namespace MCGalaxy { if (!CheckIRCCommand(user, cmdName, chan, out error)) { if (error != null) Pm(user.Nick, error); return; - } + } HandleIRCCommand(user.Nick, user.Nick, cmdName, cmdArgs); } @@ -270,15 +271,15 @@ namespace MCGalaxy { if (channel.CaselessEq(opchannel)) { Server.s.Log(String.Format("(OPs): [IRC] {0}: {1}", user.Nick, message)); - Chat.MessageOps(String.Format("To Ops &f-%I[IRC] {0}&f- {1}", user.Nick, - Server.profanityFilter ? ProfanityFilter.Parse(message) : message)); + Chat.MessageOps(String.Format("To Ops &f-%I[IRC] {0}&f- {1}", user.Nick, + Server.profanityFilter ? ProfanityFilter.Parse(message) : message)); } else { Server.s.Log(String.Format("[IRC] {0}: {1}", user.Nick, message)); - Player.GlobalIRCMessage(String.Format("%I[IRC] {0}: &f{1}", user.Nick, + Player.GlobalIRCMessage(String.Format("%I[IRC] {0}: &f{1}", user.Nick, Server.profanityFilter ? ProfanityFilter.Parse(message) : message)); } } - + bool HandlePublicCommand(UserInfo user, string channel, string message, string[] parts, bool opchat) { string cmdName = parts.Length > 1 ? parts[1].ToLower() : ""; @@ -301,7 +302,7 @@ namespace MCGalaxy { if (!whoCmd || (DateTime.UtcNow - last).TotalSeconds <= 1) return false; try { - Player p = MakeIRCPlayer(nick); + Player p = MakeIRCPlayer(nick, null); CmdPlayers.DisplayPlayers(p, "", false, false); } catch (Exception e) { Server.ErrorLog(e); @@ -312,13 +313,13 @@ namespace MCGalaxy { return true; } - bool HandleIRCCommand(string nick, string logNick, string cmdName, string cmdArgs) { + bool HandleIRCCommand(string nick, string userNick, string cmdName, string cmdArgs) { Command cmd = Command.all.Find(cmdName); - Player p = MakeIRCPlayer(nick); - if (cmd == null) { Player.Message(p, "Unknown command!"); return false; } + Player p = MakeIRCPlayer(nick, userNick); + if (cmd == null) { Player.Message(p, "Unknown command!"); return false; } string logCmd = cmdArgs == "" ? cmdName : cmdName + " " + cmdArgs; - Server.s.Log("IRC Command: /" + logCmd + " (by " + logNick + ")"); + Server.s.Log("IRC Command: /" + logCmd + " (by " + userNick + ")"); try { if (!p.group.CanExecute(cmd)) { cmd.MessageCannotUse(p); return false; } @@ -349,13 +350,17 @@ namespace MCGalaxy { return true; } - static Player MakeIRCPlayer(string ircNick) { + static Player MakeIRCPlayer(string ircNick, string userNick) { Player p = new Player("IRC"); p.group = Group.findPerm(Server.ircControllerRank); if (p.group == null) p.group = Group.findPerm(LevelPermission.Nobody); + p.ircNick = ircNick; - p.color = "&a"; return p; + p.color = "&a"; + if (userNick != null) + p.UserID = NameConverter.InvalidNameID("(IRC " + userNick + ")"); + return p; } void Listener_OnRegistered() { @@ -428,7 +433,7 @@ namespace MCGalaxy { } } - void Listener_OnNames(string channel, string[] nicks, bool last) { + void Listener_OnNames(string channel, string[] nicks, bool last) { List chanNicks = GetNicks(channel); foreach (string n in nicks) UpdateNick(n, chanNicks); @@ -442,7 +447,7 @@ namespace MCGalaxy { List chanNicks = GetNicks(channel); RemoveNick(user.Nick, chanNicks); Server.s.Log(user.Nick + " kicked " + kickee + " from IRC"); - Player.GlobalIRCMessage("%I[IRC] " + user.Nick + " kicked " + kickee); + Player.GlobalIRCMessage("%I[IRC] " + user.Nick + " kicked " + kickee); } void Listener_OnKill(UserInfo user, string nick, string reason) { @@ -507,9 +512,9 @@ namespace MCGalaxy { int GetPrefixLength(string nick) { int prefixChars = 0; for (int i = 0; i < nick.Length; i++) { - if (!IsNickChar(nick[i])) + if (!IsNickChar(nick[i])) prefixChars++; - else + else return prefixChars; } return prefixChars; @@ -575,7 +580,7 @@ namespace MCGalaxy { void UnhookEvents() { hookedEvents = false; - // Register events for outgoing + // Register events for outgoing Player.PlayerChat -= Player_PlayerChat; Player.PlayerConnect -= Player_PlayerConnect; Player.PlayerDisconnect -= Player_PlayerDisconnect; diff --git a/MCGalaxy/Player/List/PlayerList.cs b/MCGalaxy/Player/List/PlayerList.cs index 440a4fc4d..92b879002 100644 --- a/MCGalaxy/Player/List/PlayerList.cs +++ b/MCGalaxy/Player/List/PlayerList.cs @@ -51,16 +51,17 @@ namespace MCGalaxy { public int Count { get { lock (locker) return players.Count; } } - /// Adds or replaces the given name, - /// returning the index of the item within the list. - public void AddOrReplace(string p) { + /// Adds or replaces the given name. + /// Whether the given player name was added to the list. + public bool AddIfNotExists(string p) { p = p.ToLower(); lock (locker) { int idx = players.IndexOf(p); - if (idx >= 0) return; + if (idx >= 0) return false; players.Add(p); } + return true; } public string FindMatches(Player p, string name, string type, out int matches) { diff --git a/MCGalaxy/Player/PlayerData.cs b/MCGalaxy/Player/PlayerData.cs index bcdc2e46a..c2cf1d8d0 100644 --- a/MCGalaxy/Player/PlayerData.cs +++ b/MCGalaxy/Player/PlayerData.cs @@ -17,6 +17,7 @@ */ using System; using System.Data; +using MCGalaxy.DB; using MCGalaxy.SQL; namespace MCGalaxy { @@ -52,9 +53,7 @@ namespace MCGalaxy { string id = ids.Rows[0]["ID"].ToString(); p.UserID = PlayerData.ParseInt(id); } else { - Server.invalidIds.AddOrReplace(p.name); - int index = Server.invalidIds.All().IndexOf(p.name.ToLower()); - p.UserID = int.MaxValue - index; + p.UserID = NameConverter.InvalidNameID(p.name); } } } diff --git a/MCGalaxy/Server/Tasks/UpgradeTasks.cs b/MCGalaxy/Server/Tasks/UpgradeTasks.cs index a719fddd9..060729235 100644 --- a/MCGalaxy/Server/Tasks/UpgradeTasks.cs +++ b/MCGalaxy/Server/Tasks/UpgradeTasks.cs @@ -137,7 +137,7 @@ namespace MCGalaxy.Tasks { for (int i = 0; i < files.Length; i++) { File.Delete(files[i]); string level = Path.GetFileName(files[i]); - Server.lockdown.AddOrReplace(level); + Server.lockdown.AddIfNotExists(level); } Server.lockdown.Save();