Files

152 lines
4.6 KiB
C#
Raw Permalink Normal View History

2019-04-17 23:31:43 +02:00
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
2019-04-17 23:31:43 +02:00
using System.Threading.Tasks;
2021-06-09 22:19:39 +02:00
using Content.Server.Chat.Managers;
using Content.Shared.CCVar;
using Robust.Server.ServerStatus;
2019-04-17 23:31:43 +02:00
using Robust.Shared.Asynchronous;
using Robust.Shared.Configuration;
2019-04-17 23:31:43 +02:00
2021-06-09 22:19:39 +02:00
namespace Content.Server.MoMMI
2019-04-17 23:31:43 +02:00
{
internal sealed class MoMMILink : IMoMMILink, IPostInjectInit
{
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
[Dependency] private readonly IStatusHost _statusHost = default!;
[Dependency] private readonly IChatManager _chatManager = default!;
[Dependency] private readonly ITaskManager _taskManager = default!;
2019-04-17 23:31:43 +02:00
private readonly HttpClient _httpClient = new();
2019-04-17 23:31:43 +02:00
void IPostInjectInit.PostInject()
{
_statusHost.AddHandler(HandleChatPost);
2019-04-17 23:31:43 +02:00
}
public async void SendOOCMessage(string sender, string message)
{
var sentMessage = new MoMMIMessageOOC
{
Sender = sender,
Contents = message
};
await SendMessageInternal("ooc", sentMessage);
2019-04-17 23:31:43 +02:00
}
private async Task SendMessageInternal(string type, object messageObject)
2019-04-17 23:31:43 +02:00
{
var url = _configurationManager.GetCVar(CCVars.StatusMoMMIUrl);
var password = _configurationManager.GetCVar(CCVars.StatusMoMMIPassword);
2019-04-17 23:31:43 +02:00
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 request = await _httpClient.PostAsJsonAsync(url, sentMessage);
2019-04-17 23:31:43 +02:00
if (!request.IsSuccessStatusCode)
{
throw new Exception($"MoMMI returned bad status code: {request.StatusCode}");
}
}
private async Task<bool> HandleChatPost(IStatusHandlerContext context)
2019-04-17 23:31:43 +02:00
{
if (context.RequestMethod != HttpMethod.Post || context.Url.AbsolutePath != "/ooc")
2019-04-17 23:31:43 +02:00
{
return false;
}
var password = _configurationManager.GetCVar(CCVars.StatusMoMMIPassword);
2019-04-17 23:31:43 +02:00
if (string.IsNullOrEmpty(password))
{
await context.RespondErrorAsync(HttpStatusCode.Forbidden);
return true;
}
OOCPostMessage? message = null;
try
2019-04-17 23:31:43 +02:00
{
message = await context.RequestBodyJsonAsync<OOCPostMessage>();
}
catch (JsonException)
{
// message null so enters block down below.
2019-04-17 23:31:43 +02:00
}
if (message == null)
{
await context.RespondErrorAsync(HttpStatusCode.BadRequest);
2019-04-17 23:31:43 +02:00
return true;
}
if (message.Password != password)
{
await context.RespondErrorAsync(HttpStatusCode.Forbidden);
2019-04-17 23:31:43 +02:00
return true;
}
var sender = message.Sender;
var contents = message.Contents.ReplaceLineEndings(" ");
2019-04-17 23:31:43 +02:00
_taskManager.RunOnMainThread(() => _chatManager.SendHookOOC(sender, contents));
2019-04-17 23:31:43 +02:00
await context.RespondAsync("Success", HttpStatusCode.OK);
2021-02-13 12:40:41 +01:00
return true;
2019-04-17 23:31:43 +02:00
}
private sealed class MoMMIMessageBase
2019-04-17 23:31:43 +02:00
{
[JsonInclude] [JsonPropertyName("password")]
public string Password = null!;
2019-04-17 23:31:43 +02:00
[JsonInclude] [JsonPropertyName("type")]
public string Type = null!;
2019-04-17 23:31:43 +02:00
[JsonInclude] [JsonPropertyName("contents")]
public object Contents = null!;
2019-04-17 23:31:43 +02:00
}
private sealed class MoMMIMessageOOC
2019-04-17 23:31:43 +02:00
{
[JsonInclude] [JsonPropertyName("sender")]
public string Sender = null!;
2019-04-17 23:31:43 +02:00
[JsonInclude] [JsonPropertyName("contents")]
public string Contents = null!;
2019-04-17 23:31:43 +02:00
}
private sealed class OOCPostMessage
2019-04-17 23:31:43 +02:00
{
#pragma warning disable CS0649
[JsonInclude] [JsonPropertyName("password")]
public string Password = null!;
2019-04-17 23:31:43 +02:00
[JsonInclude] [JsonPropertyName("sender")]
public string Sender = null!;
2019-04-17 23:31:43 +02:00
[JsonInclude] [JsonPropertyName("contents")]
public string Contents = null!;
#pragma warning restore CS0649
2019-04-17 23:31:43 +02:00
}
}
}