From 7f8f8b1c483e04abde8e849e8054a5ff4b651051 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 12 Feb 2017 11:21:20 -0500 Subject: [PATCH] Finish MUD Refactoring --- ShiftOS.Server/Core.cs | 86 ++++++ ShiftOS.Server/MemoManager.cs | 60 +++++ ShiftOS.Server/Program.cs | 384 --------------------------- ShiftOS.Server/ShiftOS.Server.csproj | 3 + ShiftOS.Server/ShiftnetBackend.cs | 114 ++++++++ 5 files changed, 263 insertions(+), 384 deletions(-) create mode 100644 ShiftOS.Server/Core.cs create mode 100644 ShiftOS.Server/MemoManager.cs create mode 100644 ShiftOS.Server/ShiftnetBackend.cs diff --git a/ShiftOS.Server/Core.cs b/ShiftOS.Server/Core.cs new file mode 100644 index 0000000..e761a53 --- /dev/null +++ b/ShiftOS.Server/Core.cs @@ -0,0 +1,86 @@ +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; + + +namespace ShiftOS.Server +{ + public static class Core + { + [MudRequest("getguid_reply")] + public static void GuidBounce(string guid, object contents) + { + //The message's GUID was manipulated by the client to send to another client. + //So we can just bounce back the message to the other client. + server.DispatchTo(new Guid(guid), new NetObject("bounce", new ServerMessage + { + Name = "getguid_reply", + GUID = guid, + Contents = contents as string + })); + + } + + [MudRequest("getguid_send")] + public static void GuidReceiver(string guid, object contents) + { + string usrname = contents as string; + server.DispatchAll(new NetObject("are_you_this_guy", new ServerMessage + { + Name = "getguid_fromserver", + GUID = guid, + Contents = usrname, + })); + + } + + [MudRequest("script")] + public static void RunScript(string guid, object contents) + { + var args = contents as Dictionary; + + string user = ""; + string script = ""; + string sArgs = ""; + + if (!args.ContainsKey("user")) + throw new MudException("No 'user' arg specified in message to server"); + + if (!args.ContainsKey("script")) + throw new MudException("No 'script' arg specified in message to server"); + + if (!args.ContainsKey("args")) + throw new MudException("No 'args' arg specified in message to server"); + + user = args["user"] as string; + script = args["script"] as string; + sArgs = args["args"] as string; + + if (File.Exists($"scripts/{user}/{script}.lua")) + { + var script_arguments = JsonConvert.DeserializeObject>(sArgs); + server.DispatchTo(new Guid(guid), new NetObject("runme", new ServerMessage + { + Name = "run", + GUID = "Server", + Contents = $@"{{ + script:""{File.ReadAllText($"scripts/{user}/{script}.lua").Replace("\"", "\\\"")}"", + args:""{sArgs}"" + }}" + })); + } + else + { + throw new MudException($"{user}.{script}: Script not found."); + } + + } + } +} diff --git a/ShiftOS.Server/MemoManager.cs b/ShiftOS.Server/MemoManager.cs new file mode 100644 index 0000000..ed0a2e9 --- /dev/null +++ b/ShiftOS.Server/MemoManager.cs @@ -0,0 +1,60 @@ +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; + +namespace ShiftOS.Server +{ + public static class MemoManager + { + [MudRequest("get_memos_for_user")] + public static void GetMemosForUser(string guid, object contents) + { + var args = contents as Dictionary; + if (args["username"] != null) + { + string usrnme = args["username"].ToString(); + + List mmos = new List(); + + if (File.Exists("memos.json")) + { + foreach (var mmo in JsonConvert.DeserializeObject(File.ReadAllText("memos.json"))) + { + if (mmo.UserTo == usrnme) + { + mmos.Add(mmo); + } + } + } + + server.DispatchTo(new Guid(guid), new NetObject("mud_memos", new ServerMessage + { + Name = "mud_usermemos", + GUID = "server", + Contents = JsonConvert.SerializeObject(mmos) + })); + } + + } + + [MudRequest("mud_postmemo")] + public static void PostMemo(string guid, object contents) + { + MUDMemo memo = JsonConvert.DeserializeObject(contents as string); + List memos = new List(); + + if (File.Exists("memos.json")) + memos = JsonConvert.DeserializeObject>(File.ReadAllText("memos.json")); + + memos.Add(memo); + File.WriteAllText("memos.txt", JsonConvert.SerializeObject(memos)); + } + } +} diff --git a/ShiftOS.Server/Program.cs b/ShiftOS.Server/Program.cs index cb896cd..46d5ab3 100644 --- a/ShiftOS.Server/Program.cs +++ b/ShiftOS.Server/Program.cs @@ -328,390 +328,6 @@ namespace ShiftOS.Server } ClientDispatcher.DispatchTo("Error", msg.GUID, new MudRequestHandlerNotFoundException()); - - - switch (msg.Name) - { - - case "get_memos_for_user": - if(args["username"] != null) - { - string usrnme = args["username"].ToString(); - - List mmos = new List(); - - if (File.Exists("memos.json")) - { - foreach(var mmo in JsonConvert.DeserializeObject(File.ReadAllText("memos.json"))) - { - if(mmo.UserTo == usrnme) - { - mmos.Add(mmo); - } - } - } - - server.DispatchTo(new Guid(msg.GUID), new NetObject("mud_memos", new ServerMessage - { - Name = "mud_usermemos", - GUID = "server", - Contents = JsonConvert.SerializeObject(mmos) - })); - } - break; - case "mud_post_memo": - MUDMemo memo = JsonConvert.DeserializeObject(msg.Contents); - List memos = new List(); - - if (File.Exists("memos.json")) - memos = JsonConvert.DeserializeObject>(File.ReadAllText("memos.json")); - - memos.Add(memo); - File.WriteAllText("memos.txt", JsonConvert.SerializeObject(memos)); - - - break; - case "download_start": - if (!msg.Contents.StartsWith("shiftnet/")) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("shiftnet_got", new ServerMessage - { - Name = "shiftnet_file", - GUID = "server", - Contents = (File.Exists("badrequest.md") == true) ? File.ReadAllText("badrequest.md") : @"# Bad request. - -You have sent a bad request to the multi-user domain. Please try again." - })); - return; - } - - if (File.Exists(msg.Contents)) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("download", new ServerMessage - { - Name = "download_meta", - GUID = "server", - Contents = JsonConvert.SerializeObject(File.ReadAllBytes(msg.Contents)) - })); - } - else - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("shiftnet_got", new ServerMessage - { - Name = "shiftnet_file", - GUID = "server", - Contents = (File.Exists("notfound.md") == true) ? File.ReadAllText("notfound.md") : @"# Not found. - -The page you requested at was not found on this multi-user domain." - })); - - } - break; - case "shiftnet_get": - string surl = args["url"] as string; - while (surl.EndsWith("/")) - { - surl = surl.Remove(surl.Length - 1, 1); - } - if (!surl.StartsWith("shiftnet/")) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("shiftnet_got", new ServerMessage - { - Name = "shiftnet_file", - GUID = "server", - Contents = (File.Exists("badrequest.md") == true) ? File.ReadAllText("badrequest.md") : @"# Bad request. - -You have sent a bad request to the multi-user domain. Please try again." - })); - - return; - } - - if (File.Exists(surl)) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("shiftnet_got", new ServerMessage - { - Name = "shiftnet_file", - GUID = "server", - Contents = File.ReadAllText(surl) - })); - } - else if (File.Exists(surl + "/home.rnp")) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("shiftnet_got", new ServerMessage - { - Name = "shiftnet_file", - GUID = "server", - Contents = File.ReadAllText(surl + "/home.rnp") - })); - - } - else - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("shiftnet_got", new ServerMessage - { - Name = "shiftnet_file", - GUID = "server", - Contents = (File.Exists("notfound.md") == true) ? File.ReadAllText("notfound.md") : @"# Not found. - -The page you requested at was not found on this multi-user domain." - })); - - } - break; - case "chat_join": - if (args.ContainsKey("id")) - { - var cuser = new Save(); - if (args.ContainsKey("user")) - { - cuser = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(args["user"])); - - - } - int index = -1; - string chat_id = args["id"] as string; - foreach(var chat in chats) - { - if(chat.ID == chat_id) - { - if(chat.Users.Count < chat.MaxUsers || chat.MaxUsers == 0) - { - //user can join chat. - if(cuser != null) - { - index = chats.IndexOf(chat); - } - } - } - } - if(index > -1) - { - chats[index].Users.Add(cuser); - server.DispatchAll(new NetObject("broadcast", new ServerMessage - { - Name = "cbroadcast", - GUID = "server", - Contents = $"{chat_id}: {cuser.Username} {{CHAT_HAS_JOINED}}" - })); - } - else - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("broadcast", new ServerMessage - { - Name = "cbroadcast", - GUID = "server", - Contents = $"{chat_id}: {{CHAT_NOT_FOUND_OR_TOO_MANY_MEMBERS}}" - })); - } - } - break; - case "chat": - if (args.ContainsKey("id")) - { - var cuser = new Save(); - if (args.ContainsKey("user")) - { - cuser = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(args["user"])); - - - } - string message = ""; - if (args.ContainsKey("msg")) - message = args["msg"] as string; - - int index = -1; - string chat_id = args["id"] as string; - foreach (var chat in chats) - { - if (chat.ID == chat_id) - { - if (cuser != null && !string.IsNullOrWhiteSpace(message) && UserInChat(chat, cuser)) - { - index = chats.IndexOf(chat); - } - } - } - if (index > -1) - { - server.DispatchAll(new NetObject("broadcast", new ServerMessage - { - Name = "cbroadcast", - GUID = "server", - Contents = $"{chat_id}/{cuser.Username}: {message}" - })); - } - else - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("broadcast", new ServerMessage - { - Name = "cbroadcast", - GUID = "server", - Contents = $"{chats[index].ID}: {{CHAT_NOT_FOUND_OR_NOT_IN_CHAT}}" - })); - } - } - - break; - case "chat_leave": - if (args.ContainsKey("id")) - { - var cuser = new Save(); - if (args.ContainsKey("user")) - { - cuser = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(args["user"])); - - - } - int index = -1; - string chat_id = args["id"] as string; - foreach (var chat in chats) - { - if (chat.ID == chat_id) - { - if (cuser != null && UserInChat(chat, cuser)) - { - index = chats.IndexOf(chat); - } - } - } - if (index > -1) - { - server.DispatchAll(new NetObject("broadcast", new ServerMessage - { - Name = "cbroadcast", - GUID = "server", - Contents = $"{chats[index].ID}: {cuser.Username} {{HAS_LEFT_CHAT}}" - })); - } - else - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("broadcast", new ServerMessage - { - Name = "cbroadcast", - GUID = "server", - Contents = $"{chat_id}: {{CHAT_NOT_FOUND_OR_NOT_IN_CHAT}}" - })); - } - } - break; - case "chat_create": - string id = ""; - string topic = ""; - string name = ""; - int max_users = 0; - - if (args.ContainsKey("id")) - id = args["id"] as string; - if (args.ContainsKey("topic")) - name = args["topic"] as string; - if (args.ContainsKey("name")) - topic = args["name"] as string; - if (args.ContainsKey("max_users")) - max_users = Convert.ToInt32(args["max_users"].ToString()); - - bool id_taken = false; - - foreach(var chat in chats) - { - if (chat.ID == id) - id_taken = true; - } - - if (id_taken == false) - { - chats.Add(new Channel - { - ID = id, - Name = name, - Topic = topic, - MaxUsers = max_users, - Users = new List() - }); - SaveChats(); - server.DispatchTo(new Guid(msg.GUID), new NetObject("broadcast", new ServerMessage - { - Name = "cbroadcast", - GUID = "server", - Contents = $"{id}: {{SUCCESSFULLY_CREATED_CHAT}}" - })); - } - else - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("broadcast", new ServerMessage - { - Name = "cbroadcast", - GUID = "server", - Contents = $"{id}: {{ID_TAKEN}}" - })); - } - - break; - case "broadcast": - string text = msg.Contents; - if (!string.IsNullOrWhiteSpace(text)) - { - server.DispatchTo(new Guid(msg.GUID), new NetObject("runme", new ServerMessage - { - Name = "broadcast", - GUID = "Server", - Contents = text - - })); - } - break; - case "getguid_reply": - msg.GUID = "server"; - //The message's GUID was manipulated by the client to send to another client. - //So we can just bounce back the message to the other client. - server.DispatchTo(new Guid(msg.GUID), new NetObject("bounce", msg)); - break; - case "getguid_send": - string usrname = msg.Contents; - string guid = msg.GUID; - server.DispatchAll(new NetObject("are_you_this_guy", new ServerMessage - { - Name = "getguid_fromserver", - GUID = guid, - Contents = usrname, - })); - break; - case "script": - string user = ""; - string script = ""; - string sArgs = ""; - - if (!args.ContainsKey("user")) - throw new Exception("No 'user' arg specified in message to server"); - - if (!args.ContainsKey("script")) - throw new Exception("No 'script' arg specified in message to server"); - - if (!args.ContainsKey("args")) - throw new Exception("No 'args' arg specified in message to server"); - - user = args["user"] as string; - script = args["script"] as string; - sArgs = args["args"] as string; - - if(File.Exists($"scripts/{user}/{script}.lua")) - { - var script_arguments = JsonConvert.DeserializeObject>(sArgs); - server.DispatchTo(new Guid(msg.GUID), new NetObject("runme", new ServerMessage { - Name="run", - GUID="Server", - Contents = $@"{{ - script:""{File.ReadAllText($"scripts/{user}/{script}.lua").Replace("\"", "\\\"")}"", - args:""{sArgs}"" - }}" - })); - } - else - { - throw new Exception($"{user}.{script}: Script not found."); - } - break; - } } catch(Exception ex) { diff --git a/ShiftOS.Server/ShiftOS.Server.csproj b/ShiftOS.Server/ShiftOS.Server.csproj index dce2f20..ee12b2e 100644 --- a/ShiftOS.Server/ShiftOS.Server.csproj +++ b/ShiftOS.Server/ShiftOS.Server.csproj @@ -53,7 +53,9 @@ + + @@ -64,6 +66,7 @@ + diff --git a/ShiftOS.Server/ShiftnetBackend.cs b/ShiftOS.Server/ShiftnetBackend.cs new file mode 100644 index 0000000..701021d --- /dev/null +++ b/ShiftOS.Server/ShiftnetBackend.cs @@ -0,0 +1,114 @@ +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; + +namespace ShiftOS.Server +{ + public static class ShiftnetBackend + { + [MudRequest("download_start")] + public static void StartDownload(string guid, object contents) + { + string url = contents as string; + if (!url.StartsWith("shiftnet/")) + { + server.DispatchTo(new Guid(guid), new NetObject("shiftnet_got", new ServerMessage + { + Name = "shiftnet_file", + GUID = "server", + Contents = (File.Exists("badrequest.md") == true) ? File.ReadAllText("badrequest.md") : @"# Bad request. + +You have sent a bad request to the multi-user domain. Please try again." + })); + return; + } + + if (File.Exists(url)) + { + server.DispatchTo(new Guid(guid), new NetObject("download", new ServerMessage + { + Name = "download_meta", + GUID = "server", + Contents = JsonConvert.SerializeObject(File.ReadAllBytes(url)) + })); + } + else + { + server.DispatchTo(new Guid(guid), new NetObject("shiftnet_got", new ServerMessage + { + Name = "shiftnet_file", + GUID = "server", + Contents = (File.Exists("notfound.md") == true) ? File.ReadAllText("notfound.md") : @"# Not found. + +The page you requested at was not found on this multi-user domain." + })); + + } + + } + + [MudRequest("shiftnet_get")] + public static void GetPage(string guid, object contents) + { + var args = contents as Dictionary; + string surl = args["url"] as string; + while (surl.EndsWith("/")) + { + surl = surl.Remove(surl.Length - 1, 1); + } + if (!surl.StartsWith("shiftnet/")) + { + server.DispatchTo(new Guid(guid), new NetObject("shiftnet_got", new ServerMessage + { + Name = "shiftnet_file", + GUID = "server", + Contents = (File.Exists("badrequest.md") == true) ? File.ReadAllText("badrequest.md") : @"# Bad request. + +You have sent a bad request to the multi-user domain. Please try again." + })); + + return; + } + + if (File.Exists(surl)) + { + server.DispatchTo(new Guid(guid), new NetObject("shiftnet_got", new ServerMessage + { + Name = "shiftnet_file", + GUID = "server", + Contents = File.ReadAllText(surl) + })); + } + else if (File.Exists(surl + "/home.rnp")) + { + server.DispatchTo(new Guid(guid), new NetObject("shiftnet_got", new ServerMessage + { + Name = "shiftnet_file", + GUID = "server", + Contents = File.ReadAllText(surl + "/home.rnp") + })); + + } + else + { + server.DispatchTo(new Guid(guid), new NetObject("shiftnet_got", new ServerMessage + { + Name = "shiftnet_file", + GUID = "server", + Contents = (File.Exists("notfound.md") == true) ? File.ReadAllText("notfound.md") : @"# Not found. + +The page you requested at was not found on this multi-user domain." + })); + + } + + } + } +}