From 69ef3ba644cf3e15dbee32994a6f640294bc54b0 Mon Sep 17 00:00:00 2001 From: Michael VanOverbeek Date: Mon, 27 Feb 2017 21:19:31 +0000 Subject: Chat logging. --- ShiftOS.Server/ChatBackend.cs | 69 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 4 deletions(-) (limited to 'ShiftOS.Server/ChatBackend.cs') diff --git a/ShiftOS.Server/ChatBackend.cs b/ShiftOS.Server/ChatBackend.cs index 242ae16..ba1c48e 100644 --- a/ShiftOS.Server/ChatBackend.cs +++ b/ShiftOS.Server/ChatBackend.cs @@ -1,4 +1,4 @@ -/* +/* * MIT License * * Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs @@ -73,12 +73,14 @@ namespace ShiftOS.Server { if (s.Author.Id != client.CurrentUser.Id) { + var msg = new ChatMessage(s.Author.Username, "discord_" + s.Channel.Name, (s as SocketUserMessage).Resolve(0), chatID); server.DispatchAll(new NetObject("chat_msgreceived", new ServerMessage { Name = "chat_msgreceived", GUID = "server", - Contents = JsonConvert.SerializeObject(new ChatMessage(s.Author.Username, "discord_" + s.Channel.Name, (s as SocketUserMessage).Resolve(0), chatID)) + Contents = JsonConvert.SerializeObject(msg) })); + Log(chatID, $"[{msg.Username}@{msg.SystemName}] {msg.Message}"); } } } @@ -94,10 +96,11 @@ namespace ShiftOS.Server var dChan = client.GetChannel(Convert.ToUInt64(chat.DiscordChannelID)) as ISocketMessageChannel; //Relay the message to Discord. dChan.SendMessageAsync($"**[{msg.Username}@{msg.SystemName}]** `` {msg.Message}"); + //Relay it back to all MUD clients. + RelayMessage(g, msg); + Log(chatID, $"[{msg.Username}@{msg.SystemName}] {msg.Message}"); } - //Relay it back to all MUD clients. - RelayMessage(g, msg); } }; Reinitialized += () => @@ -115,6 +118,8 @@ namespace ShiftOS.Server { //Just relay it. RelayMessage(g, msg); + //...Then log it. + Log(chatID, $"[{msg.Username}@{msg.SystemName}] {msg.Message}"); } }; Reinitialized += () => { chatKilled = true; }; @@ -157,6 +162,62 @@ namespace ShiftOS.Server MessageReceived?.Invoke(guid, new ChatMessage(msg["Username"], msg["SystemName"], msg["Message"], msg["Channel"])); } + + [MudRequest("chat_getlog", typeof(ChatLogRequest))] + public static void GetChatlog(string guid, ChatLogRequest req) + { + if (!Directory.Exists("chatlogs")) + Directory.CreateDirectory("chatlogs"); + + if(File.Exists("chatlogs/" + req.Channel + ".log")) + { + string[] log = File.ReadAllLines("chatlogs/" + req.Channel + ".log"); + string seg = ""; + + if(req.Backtrack == 0 || log.Length < req.Backtrack) + { + //send all of it. + foreach(var ln in log) + { + seg += ln + Environment.NewLine; + } + } + else + { + //send only a specific chunk. + for(int i = log.Length - 1; i >= log.Length - req.Backtrack; i--) + { + seg += log[i] + Environment.NewLine; + } + } + + try + { + server.DispatchTo(new Guid(guid), new NetObject("always watching, always listening, my eyes are everywhere, you cannot escape me", new ServerMessage + { + Name = "chatlog", + Contents = seg, + GUID = "server" + })); + } + catch { } + } + } + + + public static void Log(string channel, string line) + { + if (!Directory.Exists("chatlogs")) + Directory.CreateDirectory("chatlogs"); + + List lines = new List(); + if (File.Exists("chatlogs/" + channel + ".log")) + lines = new List(File.ReadAllLines("chatlogs/" + channel + ".log")); + + lines.Add(line); + File.WriteAllLines("chatlogs/" + channel + ".log", lines.ToArray()); + + } } } -- cgit v1.2.3 From 9e30864a106a1a9b5dab0ffd6c333951d3c91dd2 Mon Sep 17 00:00:00 2001 From: Michael VanOverbeek Date: Mon, 6 Mar 2017 17:01:16 +0000 Subject: improvements --- ShiftOS.Objects/Objects.cs | 4 ++++ ShiftOS.Objects/Save.cs | 2 ++ ShiftOS.Server/ChatBackend.cs | 43 ++++++++++++++++++++++++++++++++++++++++++- ShiftOS.Server/Program.cs | 4 ++++ ShiftOS.Server/SaveManager.cs | 8 +++++++- 5 files changed, 59 insertions(+), 2 deletions(-) (limited to 'ShiftOS.Server/ChatBackend.cs') diff --git a/ShiftOS.Objects/Objects.cs b/ShiftOS.Objects/Objects.cs index 17f40ea..c4cd67c 100644 --- a/ShiftOS.Objects/Objects.cs +++ b/ShiftOS.Objects/Objects.cs @@ -96,6 +96,10 @@ namespace ShiftOS.Objects //Don't describe this one. We want it to be hidden from the admin panel's chat editor. public string ID { get; set; } + [FriendlyName("Requires Patreon?")] + [FriendlyDescription("If checked, this chat will only be shown in the MUD Control Centre if the user's save is marked as a Patreon supporter.")] + public bool RequiresPatreon { get; set; } + [FriendlyName("Chat topic")] [FriendlyDescription("A more in-depth version of your chat name. Describe what your chat's about in a sentence.")] public string Topic { get; set; } diff --git a/ShiftOS.Objects/Save.cs b/ShiftOS.Objects/Save.cs index 23c1d0f..35bfdc2 100644 --- a/ShiftOS.Objects/Save.cs +++ b/ShiftOS.Objects/Save.cs @@ -45,6 +45,8 @@ namespace ShiftOS.Objects public int MinorVersion { get; set; } public int Revision { get; set; } + public bool IsPatreon { get; set; } + public string Password { get; set; } public bool PasswordHashed { get; set; } public string SystemName { get; set; } diff --git a/ShiftOS.Server/ChatBackend.cs b/ShiftOS.Server/ChatBackend.cs index ba1c48e..b9fbf25 100644 --- a/ShiftOS.Server/ChatBackend.cs +++ b/ShiftOS.Server/ChatBackend.cs @@ -103,7 +103,21 @@ namespace ShiftOS.Server } } }; - Reinitialized += () => + OnBroadcast += (msg) => + { + if (chatKilled == false) + { + var cMsg = new ChatMessage("sys", "mud", msg, chatID); + RelayMessageToAll(cMsg); + Log(chatID, msg); + //Get the Discord channel for this chat. + var dChan = client.GetChannel(Convert.ToUInt64(chat.DiscordChannelID)) as ISocketMessageChannel; + //Relay the message to Discord. + dChan.SendMessageAsync($"{msg}"); + //Relay it back to all MUD clients. + + } + }; Reinitialized += () => { client.DisconnectAsync(); @@ -122,11 +136,31 @@ namespace ShiftOS.Server Log(chatID, $"[{msg.Username}@{msg.SystemName}] {msg.Message}"); } }; + OnBroadcast += (msg) => + { + if (chatKilled == false) + { + var cMsg = new ChatMessage("sys", "mud", msg, chatID); + RelayMessageToAll(cMsg); + Log(chatID, msg); + } + }; Reinitialized += () => { chatKilled = true; }; } } } + internal static void RelayMessageToAll(ChatMessage msg) + { + server.DispatchAll(new NetObject("chat_msgreceived", new ServerMessage + { + Name = "chat_msgreceived", + GUID = "server", + Contents = JsonConvert.SerializeObject(msg) + })); + + } + internal static void RelayMessage(string guid, ChatMessage msg) { server.DispatchAllExcept(new Guid(guid), new NetObject("chat_msgreceived", new ServerMessage @@ -163,6 +197,13 @@ namespace ShiftOS.Server } + public static event Action OnBroadcast; + + public static void Broadcast(string text) + { + OnBroadcast?.Invoke("[Broadcast] " + text); + } + [MudRequest("chat_getlog", typeof(ChatLogRequest))] public static void GetChatlog(string guid, ChatLogRequest req) { diff --git a/ShiftOS.Server/Program.cs b/ShiftOS.Server/Program.cs index 04d7b2e..3ef9715 100644 --- a/ShiftOS.Server/Program.cs +++ b/ShiftOS.Server/Program.cs @@ -185,6 +185,10 @@ namespace ShiftOS.Server Console.WriteLine("Save not found."); } } + else if(cmd.ToLower().StartsWith("broadcast ")) + { + ChatBackend.Broadcast(cmd.Remove(0, 10)); + } else if (cmd == "purge_all_bad_saves") { foreach(var f in Directory.GetFiles("saves")) diff --git a/ShiftOS.Server/SaveManager.cs b/ShiftOS.Server/SaveManager.cs index cef8b37..e986ecd 100644 --- a/ShiftOS.Server/SaveManager.cs +++ b/ShiftOS.Server/SaveManager.cs @@ -1,4 +1,4 @@ -/* +/* * MIT License * * Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs @@ -73,6 +73,12 @@ namespace ShiftOS.Server if (save.Username == args["username"].ToString() && save.Password == args["password"].ToString()) { + if(save.ID == new Guid()) + { + save.ID = Guid.NewGuid(); + WriteEncFile(savefile, JsonConvert.SerializeObject(save)); + } + Program.server.DispatchTo(new Guid(guid), new NetObject("mud_savefile", new ServerMessage { -- cgit v1.2.3 From 4aa7a6dee500dee2cb0a777a60160bbf3927be9f Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 10 Apr 2017 15:09:52 -0400 Subject: KILL ChatBackend.cs --- ShiftOS.Server/App.config | 14 +- ShiftOS.Server/ChatBackend.cs | 264 ----------------------------------- ShiftOS.Server/Program.cs | 8 -- ShiftOS.Server/ShiftOS.Server.csproj | 69 --------- ShiftOS.Server/packages.config | 29 ---- ShiftOS.Updater/App.config | 14 +- 6 files changed, 20 insertions(+), 378 deletions(-) delete mode 100644 ShiftOS.Server/ChatBackend.cs (limited to 'ShiftOS.Server/ChatBackend.cs') diff --git a/ShiftOS.Server/App.config b/ShiftOS.Server/App.config index cadfa91..a87bf6b 100644 --- a/ShiftOS.Server/App.config +++ b/ShiftOS.Server/App.config @@ -1,18 +1,18 @@ - + - + - + - - + + - - + + diff --git a/ShiftOS.Server/ChatBackend.cs b/ShiftOS.Server/ChatBackend.cs deleted file mode 100644 index b9fbf25..0000000 --- a/ShiftOS.Server/ChatBackend.cs +++ /dev/null @@ -1,264 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ShiftOS.Objects; -using NetSockets; -using Newtonsoft.Json; -using System.IO; -using static ShiftOS.Server.Program; -using Discord; -using Discord.WebSocket; -using Discord.Net.WebSockets; - -namespace ShiftOS.Server -{ - public static class ChatBackend - { - public static async Task StartDiscordBots() - { - Reinitialized?.Invoke(); - if (!File.Exists("chats.json")) - File.WriteAllText("chats.json", "[]"); - foreach (var chat in JsonConvert.DeserializeObject>(File.ReadAllText("chats.json"))) - { - string chatID = chat.ID; - bool chatKilled = false; - if (chat.IsDiscordProxy == true) - { - DiscordSocketConfig builder = new DiscordSocketConfig(); - builder.AudioMode = Discord.Audio.AudioMode.Disabled; - builder.WebSocketProvider = () => Discord.Net.Providers.WS4Net.WS4NetProvider.Instance(); - var client = new DiscordSocketClient(builder); - await client.LoginAsync(TokenType.Bot, chat.DiscordBotToken); - - await client.ConnectAsync(); - await client.SetGameAsync("ShiftOS"); - await client.SetStatusAsync(UserStatus.Online); - //Get the Discord channel for this chat. - var Chan = client.GetChannel(Convert.ToUInt64(chat.DiscordChannelID)) as ISocketMessageChannel; - //Relay the message to Discord. - await Chan.SendMessageAsync("**Hello! Multi-user domain is online.**"); - - client.MessageReceived += async (s) => - { - if (chatKilled == false) - { - if (s.Channel.Id == Convert.ToUInt64(chat.DiscordChannelID)) - { - if (s.Author.Id != client.CurrentUser.Id) - { - var msg = new ChatMessage(s.Author.Username, "discord_" + s.Channel.Name, (s as SocketUserMessage).Resolve(0), chatID); - server.DispatchAll(new NetObject("chat_msgreceived", new ServerMessage - { - Name = "chat_msgreceived", - GUID = "server", - Contents = JsonConvert.SerializeObject(msg) - })); - Log(chatID, $"[{msg.Username}@{msg.SystemName}] {msg.Message}"); - } - } - } - }; - MessageReceived += (g, msg) => - { - if (chatKilled == false) - { - //Determine if the message was sent to this channel. - if (msg.Channel == chat.ID) - { - //Get the Discord channel for this chat. - var dChan = client.GetChannel(Convert.ToUInt64(chat.DiscordChannelID)) as ISocketMessageChannel; - //Relay the message to Discord. - dChan.SendMessageAsync($"**[{msg.Username}@{msg.SystemName}]** `` {msg.Message}"); - //Relay it back to all MUD clients. - RelayMessage(g, msg); - Log(chatID, $"[{msg.Username}@{msg.SystemName}] {msg.Message}"); - - } - } - }; - OnBroadcast += (msg) => - { - if (chatKilled == false) - { - var cMsg = new ChatMessage("sys", "mud", msg, chatID); - RelayMessageToAll(cMsg); - Log(chatID, msg); - //Get the Discord channel for this chat. - var dChan = client.GetChannel(Convert.ToUInt64(chat.DiscordChannelID)) as ISocketMessageChannel; - //Relay the message to Discord. - dChan.SendMessageAsync($"{msg}"); - //Relay it back to all MUD clients. - - } - }; Reinitialized += () => - { - client.DisconnectAsync(); - - chatKilled = true; - }; - } - else - { - MessageReceived += (g, msg) => - { - if (chatKilled == false) - { - //Just relay it. - RelayMessage(g, msg); - //...Then log it. - Log(chatID, $"[{msg.Username}@{msg.SystemName}] {msg.Message}"); - } - }; - OnBroadcast += (msg) => - { - if (chatKilled == false) - { - var cMsg = new ChatMessage("sys", "mud", msg, chatID); - RelayMessageToAll(cMsg); - Log(chatID, msg); - } - }; - Reinitialized += () => { chatKilled = true; }; - } - } - } - - internal static void RelayMessageToAll(ChatMessage msg) - { - server.DispatchAll(new NetObject("chat_msgreceived", new ServerMessage - { - Name = "chat_msgreceived", - GUID = "server", - Contents = JsonConvert.SerializeObject(msg) - })); - - } - - internal static void RelayMessage(string guid, ChatMessage msg) - { - server.DispatchAllExcept(new Guid(guid), new NetObject("chat_msgreceived", new ServerMessage - { - Name = "chat_msgreceived", - GUID = "server", - Contents = JsonConvert.SerializeObject(msg) - })); - - } - - public static event Action MessageReceived; - public static event empty Reinitialized; - - - public delegate void empty(); - - [MudRequest("chat_getallchannels", null)] - public static void GetAllChannels(string guid, object contents) - { - server.DispatchTo(new Guid(guid), new NetObject("chat_all", new ServerMessage - { - Name = "chat_all", - GUID = "Server", - Contents = (File.Exists("chats.json") == true) ? File.ReadAllText("chats.json") : "[]" - })); - } - - [MudRequest("chat_send", typeof(Dictionary))] - public static void ReceiveMessage(string guid, object contents) - { - var msg = contents as Dictionary; - MessageReceived?.Invoke(guid, new ChatMessage(msg["Username"], msg["SystemName"], msg["Message"], msg["Channel"])); - - } - - public static event Action OnBroadcast; - - public static void Broadcast(string text) - { - OnBroadcast?.Invoke("[Broadcast] " + text); - } - - [MudRequest("chat_getlog", typeof(ChatLogRequest))] - public static void GetChatlog(string guid, ChatLogRequest req) - { - if (!Directory.Exists("chatlogs")) - Directory.CreateDirectory("chatlogs"); - - if(File.Exists("chatlogs/" + req.Channel + ".log")) - { - string[] log = File.ReadAllLines("chatlogs/" + req.Channel + ".log"); - string seg = ""; - - if(req.Backtrack == 0 || log.Length < req.Backtrack) - { - //send all of it. - foreach(var ln in log) - { - seg += ln + Environment.NewLine; - } - } - else - { - //send only a specific chunk. - for(int i = log.Length - 1; i >= log.Length - req.Backtrack; i--) - { - seg += log[i] + Environment.NewLine; - } - } - - try - { - server.DispatchTo(new Guid(guid), new NetObject("always watching, always listening, my eyes are everywhere, you cannot escape me", new ServerMessage - { - Name = "chatlog", - Contents = seg, - GUID = "server" - })); - } - catch { } - } - } - - - public static void Log(string channel, string line) - { - if (!Directory.Exists("chatlogs")) - Directory.CreateDirectory("chatlogs"); - - List lines = new List(); - if (File.Exists("chatlogs/" + channel + ".log")) - lines = new List(File.ReadAllLines("chatlogs/" + channel + ".log")); - - lines.Add(line); - File.WriteAllLines("chatlogs/" + channel + ".log", lines.ToArray()); - - } - } - -} diff --git a/ShiftOS.Server/Program.cs b/ShiftOS.Server/Program.cs index 3f64054..afc2541 100644 --- a/ShiftOS.Server/Program.cs +++ b/ShiftOS.Server/Program.cs @@ -143,10 +143,6 @@ namespace ShiftOS.Server AppDomain.CurrentDomain.UnhandledException += (o, a) => { - ChatBackend.Broadcast("**Automatic Broadcast:** The multi-user domain is restarting because of a crash."); -#if DEBUG - ChatBackend.Broadcast("Crash summary: " + a.ExceptionObject.ToString()); -#endif if(server.IsOnline == true) server.Stop(); System.Diagnostics.Process.Start("ShiftOS.Server.exe"); @@ -218,10 +214,6 @@ namespace ShiftOS.Server Console.WriteLine("Save not found."); } } - else if(cmd.ToLower().StartsWith("broadcast ")) - { - ChatBackend.Broadcast(cmd.Remove(0, 10)); - } else if (cmd == "purge_all_bad_saves") { foreach(var f in Directory.GetFiles("saves")) diff --git a/ShiftOS.Server/ShiftOS.Server.csproj b/ShiftOS.Server/ShiftOS.Server.csproj index aed227f..86c7e55 100644 --- a/ShiftOS.Server/ShiftOS.Server.csproj +++ b/ShiftOS.Server/ShiftOS.Server.csproj @@ -34,26 +34,6 @@ 4 - - ..\packages\Discord.Net.Core.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.Core.dll - True - - - ..\packages\Discord.Net.Providers.WS4Net.1.0.0-rc-00595\lib\net45\Discord.Net.Providers.WS4Net.dll - True - - - ..\packages\Discord.Net.Rest.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.Rest.dll - True - - - ..\packages\Discord.Net.Rpc.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.Rpc.dll - True - - - ..\packages\Discord.Net.WebSocket.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.WebSocket.dll - True - ..\packages\Microsoft.Win32.Primitives.4.0.1\lib\net46\Microsoft.Win32.Primitives.dll True @@ -65,64 +45,16 @@ ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll True - - ..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.dll - True - - - ..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Concurrent.dll - True - - - ..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Enlightenment.dll - True - ..\packages\RestSharp.105.2.3\lib\net451\RestSharp.dll True - - ..\packages\System.AppContext.4.1.0\lib\net46\System.AppContext.dll - True - - - ..\packages\System.Collections.Immutable.1.3.0\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll - True - - - ..\packages\System.Console.4.0.0\lib\net46\System.Console.dll - True - - - ..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll - True - - - ..\packages\System.Globalization.Calendars.4.0.1\lib\net46\System.Globalization.Calendars.dll - True - - - ..\packages\System.Interactive.Async.3.1.0\lib\net45\System.Interactive.Async.dll - True - - - ..\packages\System.IO.Compression.ZipFile.4.0.1\lib\net46\System.IO.Compression.ZipFile.dll - True - - - ..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll - True - - - ..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll - True - ..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll True @@ -168,7 +100,6 @@ - diff --git a/ShiftOS.Server/packages.config b/ShiftOS.Server/packages.config index 325c809..11643b9 100644 --- a/ShiftOS.Server/packages.config +++ b/ShiftOS.Server/packages.config @@ -1,38 +1,10 @@  - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -57,6 +29,5 @@ - \ No newline at end of file diff --git a/ShiftOS.Updater/App.config b/ShiftOS.Updater/App.config index 88fa402..757ddce 100644 --- a/ShiftOS.Updater/App.config +++ b/ShiftOS.Updater/App.config @@ -1,6 +1,18 @@ - + + + + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3