From 69ef3ba644cf3e15dbee32994a6f640294bc54b0 Mon Sep 17 00:00:00 2001 From: Michael VanOverbeek Date: Mon, 27 Feb 2017 21:19:31 +0000 Subject: [PATCH] Chat logging. --- ShiftOS.Objects/MudAttributes.cs | 14 ++++++- ShiftOS.Server/ChatBackend.cs | 69 ++++++++++++++++++++++++++++++-- ShiftOS.Server/ShopBackend.cs | 19 +++++---- 3 files changed, 89 insertions(+), 13 deletions(-) diff --git a/ShiftOS.Objects/MudAttributes.cs b/ShiftOS.Objects/MudAttributes.cs index 1822356..d34c40e 100644 --- a/ShiftOS.Objects/MudAttributes.cs +++ b/ShiftOS.Objects/MudAttributes.cs @@ -1,4 +1,4 @@ -/* +/* * MIT License * * Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs @@ -64,6 +64,18 @@ namespace ShiftOS.Objects public string ResponseName { get; private set; } } + public class ChatLogRequest + { + public ChatLogRequest(string chan, int backtrack = 0) + { + Channel = chan; + Backtrack = backtrack; + } + + public int Backtrack { get; set; } + public string Channel { get; set; } + } + } 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()); + + } } } diff --git a/ShiftOS.Server/ShopBackend.cs b/ShiftOS.Server/ShopBackend.cs index 12f69ce..baf1e7a 100644 --- a/ShiftOS.Server/ShopBackend.cs +++ b/ShiftOS.Server/ShopBackend.cs @@ -1,4 +1,4 @@ -/* +/* * MIT License * * Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs @@ -185,15 +185,18 @@ namespace ShiftOS.Server if (File.Exists("shops.json")) foreach (var shop in JsonConvert.DeserializeObject>(File.ReadAllText("shops.json"))) { - if (shop.Owner == shopOwner) + if (shop != null) { - server.DispatchTo(new Guid(guid), new NetObject("ruecuodaL", new ServerMessage + if (shop.Owner == shopOwner) { - Name = "user_shop", - GUID = "server", - Contents = JsonConvert.SerializeObject(shop) - })); - return; + server.DispatchTo(new Guid(guid), new NetObject("ruecuodaL", new ServerMessage + { + Name = "user_shop", + GUID = "server", + Contents = JsonConvert.SerializeObject(shop) + })); + return; + } } }