OOC <-> Discord link. (#201)

This commit is contained in:
Pieter-Jan Briers
2019-04-17 23:31:43 +02:00
committed by GitHub
parent 903961771b
commit 747cb15888
7 changed files with 186 additions and 1 deletions

View File

@@ -1,7 +1,10 @@
using System.Linq;
using System.Net.Http;
using Content.Server.Interfaces;
using Content.Server.Interfaces.Chat;
using Content.Shared.Chat;
using Robust.Server.Interfaces.Player;
using Robust.Shared.Interfaces.Configuration;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.Interfaces.Resources;
@@ -19,6 +22,7 @@ namespace Content.Server.Chat
#pragma warning disable 649
[Dependency] private readonly IServerNetManager _netManager;
[Dependency] private readonly IPlayerManager _playerManager;
[Dependency] private readonly IMoMMILink _mommiLink;
#pragma warning restore 649
public void Initialize()
@@ -63,6 +67,17 @@ namespace Content.Server.Chat
msg.Message = message;
msg.MessageWrap = $"OOC: {player.SessionId}: {{0}}";
_netManager.ServerSendToAll(msg);
_mommiLink.SendOOCMessage(player.SessionId.ToString(), message);
}
public void SendHookOOC(string sender, string message)
{
var msg = _netManager.CreateNetMessage<MsgChatMessage>();
msg.Channel = ChatChannel.OOC;
msg.Message = message;
msg.MessageWrap = $"OOC: (D){sender}: {{0}}";
_netManager.ServerSendToAll(msg);
}
}
}

View File

@@ -148,11 +148,13 @@
<Compile Include="Interfaces\GameObjects\Components\Movement\IMoverComponent.cs" />
<Compile Include="Interfaces\GameObjects\IOnDamageBehavior.cs" />
<Compile Include="Interfaces\GameTicking\IGameTicker.cs" />
<Compile Include="Interfaces\IMoMMILink.cs" />
<Compile Include="Interfaces\IServerNotifyManager.cs" />
<Compile Include="Mobs\Commands.cs" />
<Compile Include="Mobs\Mind.cs" />
<Compile Include="Mobs\Role.cs" />
<Compile Include="Mobs\Roles\Traitor.cs" />
<Compile Include="MoMMILink.cs" />
<Compile Include="Placement\SpawnHelpers.cs" />
<Compile Include="Players\PlayerData.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@@ -56,6 +56,7 @@ namespace Content.Server
public class EntryPoint : GameServer
{
private IGameTicker _gameTicker;
private IMoMMILink _mommiLink;
private StatusShell _statusShell;
/// <inheritdoc />
@@ -160,6 +161,7 @@ namespace Content.Server
IoCManager.Register<IServerNotifyManager, ServerNotifyManager>();
IoCManager.Register<IGameTicker, GameTicker>();
IoCManager.Register<IChatManager, ChatManager>();
IoCManager.Register<IMoMMILink, MoMMILink>();
IoCManager.BuildGraph();
_gameTicker = IoCManager.Resolve<IGameTicker>();
@@ -167,6 +169,8 @@ namespace Content.Server
IoCManager.Resolve<IServerNotifyManager>().Initialize();
IoCManager.Resolve<IChatManager>().Initialize();
_mommiLink = IoCManager.Resolve<IMoMMILink>();
var playerManager = IoCManager.Resolve<IPlayerManager>();
_statusShell = new StatusShell();
@@ -184,6 +188,7 @@ namespace Content.Server
base.Update(level, frameTime);
_gameTicker.Update(new FrameEventArgs(frameTime));
}
}
}

View File

@@ -17,5 +17,7 @@ namespace Content.Server.Interfaces.Chat
void EntitySay(IEntity source, string message);
void SendOOC(IPlayerSession player, string message);
void SendHookOOC(string sender, string message);
}
}

View File

@@ -0,0 +1,7 @@
namespace Content.Server.Interfaces
{
public interface IMoMMILink
{
void SendOOCMessage(string sender, string message);
}
}

154
Content.Server/MoMMILink.cs Normal file
View File

@@ -0,0 +1,154 @@
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Content.Server.Interfaces;
using Content.Server.Interfaces.Chat;
using Newtonsoft.Json;
using Robust.Server.Interfaces.ServerStatus;
using Robust.Server.ServerStatus;
using Robust.Shared.Asynchronous;
using Robust.Shared.Interfaces.Configuration;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Utility;
namespace Content.Server
{
internal sealed class MoMMILink : IMoMMILink, IPostInjectInit
{
#pragma warning disable 649
[Dependency] private readonly IConfigurationManager _configurationManager;
[Dependency] private readonly IStatusHost _statusHost;
[Dependency] private readonly IChatManager _chatManager;
[Dependency] private readonly ITaskManager _taskManager;
#pragma warning restore 649
private readonly HttpClient _httpClient = new HttpClient();
void IPostInjectInit.PostInject()
{
_configurationManager.RegisterCVar<string>("status.mommiurl", null);
_configurationManager.RegisterCVar<string>("status.mommipassword", null);
_statusHost.AddHandler(_handleChatPost);
}
public async void SendOOCMessage(string sender, string message)
{
var sentMessage = new MoMMIMessageOOC
{
Sender = sender,
Contents = message
};
await _sendMessageInternal("ooc", sentMessage);
}
private async Task _sendMessageInternal(string type, object messageObject)
{
var url = _configurationManager.GetCVar<string>("status.mommiurl");
var password = _configurationManager.GetCVar<string>("status.mommipassword");
if (string.IsNullOrWhiteSpace(url))
{
return;
}
if (string.IsNullOrWhiteSpace(password))
{
Logger.WarningS("mommi", "MoMMI URL specified but not password!");
return;
}
var sentMessage = new MoMMIMessageBase
{
Password = password,
Type = type,
Contents = messageObject
};
var jsonMessage = JsonConvert.SerializeObject(sentMessage);
var request =
await _httpClient.PostAsync(url, new StringContent(jsonMessage, Encoding.UTF8, "application/json"));
if (!request.IsSuccessStatusCode)
{
throw new Exception($"MoMMI returned bad status code: {request.StatusCode}");
}
}
private bool _handleChatPost(HttpMethod method, HttpListenerRequest request, HttpListenerResponse response)
{
if (method != HttpMethod.Post || request.Url.AbsolutePath != "/ooc")
{
return false;
}
var password = _configurationManager.GetCVar<string>("status.mommipassword");
OOCPostMessage message;
using (var streamReader = new StreamReader(request.InputStream, EncodingHelpers.UTF8))
using (var jsonReader = new JsonTextReader(streamReader))
{
var serializer = new JsonSerializer();
try
{
message = serializer.Deserialize<OOCPostMessage>(jsonReader);
}
catch (JsonSerializationException)
{
response.Respond(method, "400 Bad Request", HttpStatusCode.BadRequest, "text/plain");
return true;
}
}
if (message == null)
{
response.Respond(method, "400 Bad Request", HttpStatusCode.BadRequest, "text/plain");
return true;
}
if (message.Password != password)
{
response.Respond(method, "Incorrect password", HttpStatusCode.Forbidden, "text/plain");
return true;
}
_taskManager.RunOnMainThread(() => _chatManager.SendHookOOC(message.Sender, message.Contents));
response.Respond(method, "Message received", HttpStatusCode.OK, "text/plain");
return false;
}
[JsonObject(MemberSerialization.Fields)]
private class MoMMIMessageBase
{
[JsonProperty("password")] public string Password;
[JsonProperty("type")] public string Type;
[JsonProperty("contents")] public object Contents;
}
[JsonObject(MemberSerialization.Fields)]
private class MoMMIMessageOOC
{
[JsonProperty("sender")] public string Sender;
[JsonProperty("contents")] public string Contents;
}
[JsonObject(MemberSerialization.Fields, ItemRequired = Required.Always)]
private class OOCPostMessage
{
[JsonProperty("password")] public string Password;
[JsonProperty("sender")] public string Sender;
[JsonProperty("contents")] public string Contents;
}
}
}