MoMMILink.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.Net;
  2. using System.Net.Http;
  3. using System.Net.Http.Json;
  4. using System.Text.Json;
  5. using System.Text.Json.Serialization;
  6. using System.Threading.Tasks;
  7. using Content.Server.Chat.Managers;
  8. using Content.Shared.CCVar;
  9. using Robust.Server.ServerStatus;
  10. using Robust.Shared.Asynchronous;
  11. using Robust.Shared.Configuration;
  12. namespace Content.Server.MoMMI
  13. {
  14. internal sealed class MoMMILink : IMoMMILink, IPostInjectInit
  15. {
  16. [Dependency] private readonly IConfigurationManager _configurationManager = default!;
  17. [Dependency] private readonly IStatusHost _statusHost = default!;
  18. [Dependency] private readonly IChatManager _chatManager = default!;
  19. [Dependency] private readonly ITaskManager _taskManager = default!;
  20. private readonly HttpClient _httpClient = new();
  21. void IPostInjectInit.PostInject()
  22. {
  23. _statusHost.AddHandler(HandleChatPost);
  24. }
  25. public async void SendOOCMessage(string sender, string message)
  26. {
  27. var sentMessage = new MoMMIMessageOOC
  28. {
  29. Sender = sender,
  30. Contents = message
  31. };
  32. await SendMessageInternal("ooc", sentMessage);
  33. }
  34. private async Task SendMessageInternal(string type, object messageObject)
  35. {
  36. var url = _configurationManager.GetCVar(CCVars.StatusMoMMIUrl);
  37. var password = _configurationManager.GetCVar(CCVars.StatusMoMMIPassword);
  38. if (string.IsNullOrWhiteSpace(url))
  39. {
  40. return;
  41. }
  42. if (string.IsNullOrWhiteSpace(password))
  43. {
  44. Logger.WarningS("mommi", "MoMMI URL specified but not password!");
  45. return;
  46. }
  47. var sentMessage = new MoMMIMessageBase
  48. {
  49. Password = password,
  50. Type = type,
  51. Contents = messageObject
  52. };
  53. var request = await _httpClient.PostAsJsonAsync(url, sentMessage);
  54. if (!request.IsSuccessStatusCode)
  55. {
  56. throw new Exception($"MoMMI returned bad status code: {request.StatusCode}");
  57. }
  58. }
  59. private async Task<bool> HandleChatPost(IStatusHandlerContext context)
  60. {
  61. if (context.RequestMethod != HttpMethod.Post || context.Url.AbsolutePath != "/ooc")
  62. {
  63. return false;
  64. }
  65. var password = _configurationManager.GetCVar(CCVars.StatusMoMMIPassword);
  66. if (string.IsNullOrEmpty(password))
  67. {
  68. await context.RespondErrorAsync(HttpStatusCode.Forbidden);
  69. return true;
  70. }
  71. OOCPostMessage? message = null;
  72. try
  73. {
  74. message = await context.RequestBodyJsonAsync<OOCPostMessage>();
  75. }
  76. catch (JsonException)
  77. {
  78. // message null so enters block down below.
  79. }
  80. if (message == null)
  81. {
  82. await context.RespondErrorAsync(HttpStatusCode.BadRequest);
  83. return true;
  84. }
  85. if (message.Password != password)
  86. {
  87. await context.RespondErrorAsync(HttpStatusCode.Forbidden);
  88. return true;
  89. }
  90. var sender = message.Sender;
  91. var contents = message.Contents.ReplaceLineEndings(" ");
  92. _taskManager.RunOnMainThread(() => _chatManager.SendHookOOC(sender, contents));
  93. await context.RespondAsync("Success", HttpStatusCode.OK);
  94. return true;
  95. }
  96. private sealed class MoMMIMessageBase
  97. {
  98. [JsonInclude] [JsonPropertyName("password")]
  99. public string Password = null!;
  100. [JsonInclude] [JsonPropertyName("type")]
  101. public string Type = null!;
  102. [JsonInclude] [JsonPropertyName("contents")]
  103. public object Contents = null!;
  104. }
  105. private sealed class MoMMIMessageOOC
  106. {
  107. [JsonInclude] [JsonPropertyName("sender")]
  108. public string Sender = null!;
  109. [JsonInclude] [JsonPropertyName("contents")]
  110. public string Contents = null!;
  111. }
  112. private sealed class OOCPostMessage
  113. {
  114. #pragma warning disable CS0649
  115. [JsonInclude] [JsonPropertyName("password")]
  116. public string Password = null!;
  117. [JsonInclude] [JsonPropertyName("sender")]
  118. public string Sender = null!;
  119. [JsonInclude] [JsonPropertyName("contents")]
  120. public string Contents = null!;
  121. #pragma warning restore CS0649
  122. }
  123. }
  124. }