KILL ChatBackend.cs

This commit is contained in:
Michael 2017-04-10 15:09:52 -04:00
parent a9a9beaf63
commit 4aa7a6dee5
6 changed files with 20 additions and 378 deletions

View file

@ -1,18 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
<runtime>
<loadFromRemoteSources enabled="true"/>
<loadFromRemoteSources enabled="true" />
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0"/>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Interactive.Async" publicKeyToken="94bc3704cddfc263" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.0.1000.0" newVersion="3.0.1000.0"/>
<assemblyIdentity name="System.Interactive.Async" publicKeyToken="94bc3704cddfc263" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1000.0" newVersion="3.0.1000.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>

View file

@ -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<List<ShiftOS.Objects.Channel>>(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}]** `<mud/{msg.Channel}>` {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<string, ChatMessage> 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<string, string>))]
public static void ReceiveMessage(string guid, object contents)
{
var msg = contents as Dictionary<string, string>;
MessageReceived?.Invoke(guid, new ChatMessage(msg["Username"], msg["SystemName"], msg["Message"], msg["Channel"]));
}
public static event Action<string> 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<string> lines = new List<string>();
if (File.Exists("chatlogs/" + channel + ".log"))
lines = new List<string>(File.ReadAllLines("chatlogs/" + channel + ".log"));
lines.Add(line);
File.WriteAllLines("chatlogs/" + channel + ".log", lines.ToArray());
}
}
}

View file

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

View file

@ -34,26 +34,6 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Discord.Net.Core, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.Core.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Discord.Net.Providers.WS4Net, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.Providers.WS4Net.1.0.0-rc-00595\lib\net45\Discord.Net.Providers.WS4Net.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Discord.Net.Rest, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.Rest.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.Rest.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Discord.Net.Rpc, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.Rpc.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.Rpc.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Discord.Net.WebSocket, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Discord.Net.WebSocket.1.0.0-rc-00595\lib\netstandard1.1\Discord.Net.WebSocket.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Win32.Primitives, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Win32.Primitives.4.0.1\lib\net46\Microsoft.Win32.Primitives.dll</HintPath>
<Private>True</Private>
@ -65,64 +45,16 @@
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nito.AsyncEx, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nito.AsyncEx.Concurrent, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Concurrent.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nito.AsyncEx.Enlightenment, Version=3.0.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Enlightenment.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="RestSharp, Version=105.2.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\RestSharp.105.2.3\lib\net451\RestSharp.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.AppContext, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.AppContext.4.1.0\lib\net46\System.AppContext.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Collections.Immutable, Version=1.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Collections.Immutable.1.3.0\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Console, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Console.4.0.0\lib\net46\System.Console.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Diagnostics.DiagnosticSource, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Diagnostics.DiagnosticSource.4.3.0\lib\net46\System.Diagnostics.DiagnosticSource.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.Globalization.Calendars, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Globalization.Calendars.4.0.1\lib\net46\System.Globalization.Calendars.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Interactive.Async, Version=3.0.1000.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263, processorArchitecture=MSIL">
<HintPath>..\packages\System.Interactive.Async.3.1.0\lib\net45\System.Interactive.Async.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
<Reference Include="System.IO.Compression.ZipFile, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.Compression.ZipFile.4.0.1\lib\net46\System.IO.Compression.ZipFile.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.IO.FileSystem, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.FileSystem.4.3.0\lib\net46\System.IO.FileSystem.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.IO.FileSystem.Primitives, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.IO.FileSystem.Primitives.4.3.0\lib\net46\System.IO.FileSystem.Primitives.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Net.Sockets, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Net.Sockets.4.3.0\lib\net46\System.Net.Sockets.dll</HintPath>
<Private>True</Private>
@ -168,7 +100,6 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ChatBackend.cs" />
<Compile Include="Core.cs" />
<Compile Include="LegionManager.cs" />
<Compile Include="MemoManager.cs" />

View file

@ -1,38 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Discord.Net.Core" version="1.0.0-rc-00595" targetFramework="net451" />
<package id="Discord.Net.Providers.WS4Net" version="1.0.0-rc-00595" targetFramework="net461" />
<package id="Discord.Net.Rest" version="1.0.0-rc-00595" targetFramework="net451" />
<package id="Discord.Net.Rpc" version="1.0.0-rc-00595" targetFramework="net451" />
<package id="Discord.Net.WebSocket" version="1.0.0-rc-00595" targetFramework="net451" />
<package id="DynamicLua" version="1.1.2.0" targetFramework="net452" />
<package id="Microsoft.NETCore.Platforms" version="1.0.1" targetFramework="net451" />
<package id="Microsoft.Win32.Primitives" version="4.0.1" targetFramework="net461" requireReinstallation="true" />
<package id="NETStandard.Library" version="1.6.0" targetFramework="net451" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net451" />
<package id="Nito.AsyncEx" version="3.0.1" targetFramework="net451" />
<package id="RestSharp" version="105.2.3" targetFramework="net451" />
<package id="System.AppContext" version="4.1.0" targetFramework="net461" requireReinstallation="true" />
<package id="System.Collections" version="4.0.11" targetFramework="net451" />
<package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net451" />
<package id="System.Collections.Immutable" version="1.3.0" targetFramework="net451" />
<package id="System.Console" version="4.0.0" targetFramework="net461" requireReinstallation="true" />
<package id="System.Diagnostics.Debug" version="4.0.11" targetFramework="net451" />
<package id="System.Diagnostics.DiagnosticSource" version="4.3.0" targetFramework="net461" requireReinstallation="true" />
<package id="System.Diagnostics.Tools" version="4.0.1" targetFramework="net451" />
<package id="System.Diagnostics.Tracing" version="4.1.0" targetFramework="net451" />
<package id="System.Globalization" version="4.0.11" targetFramework="net451" />
<package id="System.Globalization.Calendars" version="4.0.1" targetFramework="net461" requireReinstallation="true" />
<package id="System.Interactive.Async" version="3.1.0" targetFramework="net451" />
<package id="System.IO" version="4.1.0" targetFramework="net451" />
<package id="System.IO.Compression" version="4.3.0" targetFramework="net451" />
<package id="System.IO.Compression.ZipFile" version="4.0.1" targetFramework="net461" requireReinstallation="true" />
<package id="System.IO.FileSystem" version="4.3.0" targetFramework="net461" requireReinstallation="true" />
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net461" requireReinstallation="true" />
<package id="System.Linq" version="4.3.0" targetFramework="net451" />
<package id="System.Linq.Expressions" version="4.3.0" targetFramework="net451" />
<package id="System.Net.Http" version="4.3.0" targetFramework="net451" />
<package id="System.Net.Primitives" version="4.0.11" targetFramework="net451" />
<package id="System.Net.Sockets" version="4.3.0" targetFramework="net461" requireReinstallation="true" />
<package id="System.ObjectModel" version="4.0.12" targetFramework="net451" />
<package id="System.Reflection" version="4.3.0" targetFramework="net451" />
@ -57,6 +29,5 @@
<package id="System.Threading.Tasks" version="4.0.11" targetFramework="net451" />
<package id="System.Threading.Timer" version="4.0.1" targetFramework="net451" />
<package id="System.Xml.ReaderWriter" version="4.0.11" targetFramework="net451" />
<package id="System.Xml.XDocument" version="4.0.11" targetFramework="net451" />
<package id="WebSocket4Net" version="0.14.1" targetFramework="net451" />
</packages>

View file

@ -1,6 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Interactive.Async" publicKeyToken="94bc3704cddfc263" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1000.0" newVersion="3.0.1000.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>