diff --git a/src/CommandHandlers/ChatMessageHandler.cs b/src/CommandHandlers/ChatMessageHandler.cs index ffdacf7..fb754fc 100644 --- a/src/CommandHandlers/ChatMessageHandler.cs +++ b/src/CommandHandlers/ChatMessageHandler.cs @@ -1,4 +1,6 @@ -using sodoffmmo.Attributes; +using System.Net; +using System.Net.Http.Json; +using sodoffmmo.Attributes; using sodoffmmo.Core; using sodoffmmo.Data; using sodoffmmo.Management; @@ -37,6 +39,13 @@ class ChatMessageHandler : CommandHandler { return; } + // send an http request to api to check for 'IndefiniteOpenChatBan' or 'TemporaryOpenChatBan' + ApiWebService apiWebService = new(); + var banType = apiWebService.CheckForUserBan(client); + + if (banType != null && (banType == UserBanType.IndefiniteOpenChatBan || banType == UserBanType.TemporaryOpenChatBan)) + { client.Send(Utils.ArrNetworkPacket(new string[] { "SMF", "-1", "CB", "1", "Sorry, You've Been Banned From Using Type Chat", "1" }, "SMF")); return; } + client.Room.Send(Utils.BuildChatMessage(client.PlayerData.Uid, message, client.PlayerData.DiplayName), client); NetworkObject cmd = new(); diff --git a/src/Core/ApiWebService.cs b/src/Core/ApiWebService.cs new file mode 100644 index 0000000..602ce9b --- /dev/null +++ b/src/Core/ApiWebService.cs @@ -0,0 +1,54 @@ +using System; +using System.Net.Http.Json; +using sodoffmmo.CommandHandlers; + +namespace sodoffmmo.Core; + +public class ApiWebService +{ + public UserBanType? CheckForUserBan(Client client) + { + HttpClient httpClient = new(); + var content = new FormUrlEncodedContent( + new Dictionary { + { "token", client.PlayerData.UNToken } + } + ); + httpClient.Timeout = new TimeSpan(0, 0, 3); + + try + { + var response = httpClient.PostAsync($"{Configuration.ServerConfiguration.ApiUrl}/Moderation/CheckForVikingBan", content).Result; + Log("Moderation/CheckForVikingBan"); + if (response.StatusCode == System.Net.HttpStatusCode.OK && response.Content != null) return response.Content.ReadFromJsonAsync().Result; + else return null; + } catch (Exception e) { LogError(e.Message); return null; } + } + + public string? BanUser(Client client, string userId, string banType, string days) + { + HttpClient httpClient = new(); + var content = new FormUrlEncodedContent( + new Dictionary { + { "token", client.PlayerData.UNToken }, + { "userId", userId }, + { "banType", banType }, + { "days", days } + } + ); + httpClient.Timeout = new TimeSpan(0, 0, 3); + + try + { + var response = httpClient.PostAsync($"{Configuration.ServerConfiguration.ApiUrl}/Moderation/AddBanToVikingByGuid", content).Result; + Log("Moderation/AddBanToVikingByGuid"); + + if (response.StatusCode == System.Net.HttpStatusCode.OK && response.Content != null) return response.Content.ReadAsStringAsync().Result; + else return null; + } catch (Exception e) { LogError(e.Message); return null; } + } + + private void Log(string endpoint) => Console.WriteLine($"Sent API Request To {Configuration.ServerConfiguration.ApiUrl}/{endpoint}"); + + private void LogError(string message) => Console.WriteLine($"An Error Has Occured When Sending An API Request - {message}"); +} diff --git a/src/Core/UserBanType.cs b/src/Core/UserBanType.cs new file mode 100644 index 0000000..294ba61 --- /dev/null +++ b/src/Core/UserBanType.cs @@ -0,0 +1,10 @@ +namespace sodoffmmo.Core; + +public enum UserBanType +{ + NotBanned = 0, + IndefiniteOpenChatBan = 1, + TemporaryOpenChatBan = 2, + IndefiniteAccountBan = 3, + TemporaryAccountBan = 4 +} diff --git a/src/Management/Commands/BanCommand.cs b/src/Management/Commands/BanCommand.cs new file mode 100644 index 0000000..6f8e57a --- /dev/null +++ b/src/Management/Commands/BanCommand.cs @@ -0,0 +1,26 @@ +using System; +using System.Net.Http.Json; +using sodoffmmo.Attributes; +using sodoffmmo.Core; + +namespace sodoffmmo.Management.Commands; + +[ManagementCommand("ban", Role.Moderator)] +class BanCommand : IManagementCommand +{ + public void Handle(Client client, string[] arguments) + { + if(arguments.Length < 2) { client.Send(Utils.BuildServerSideMessage($"Expected 3 Args, Got {arguments.Length + 1}", "Server")); return; } + + if (arguments[0] == "help") client.Send(Utils.BuildServerSideMessage($"::ban - This bans a user who is in-room. First argument is the id of the user in-room. Room user lists start at 0.", "Server")); + var clientToBan = client.Room!.Clients.ToArray()[int.Parse(arguments[0])].PlayerData.Uid; + if (clientToBan == null) { client.Send(Utils.BuildServerSideMessage($"User Could Not Be Found", "Server")); return; } + + // send an http request to the api set in appsettings + ApiWebService apiWebService = new(); + var response = apiWebService.BanUser(client, clientToBan, arguments[1], arguments[2]); + + if (response != null) { client.Send(Utils.BuildServerSideMessage("User Banned Successfully", "Server")); return; } + else { client.Send(Utils.BuildServerSideMessage("Empty Response", "Server")); } + } +} diff --git a/src/appsettings.json b/src/appsettings.json index a9a6b85..98a03f2 100644 --- a/src/appsettings.json +++ b/src/appsettings.json @@ -61,9 +61,9 @@ "// Authentication Optional": "authentication is required only for moderation activities", "// Authentication RequiredForChat": "authentication is required only for moderation activities and using chat (if chat is enabled)", "// Authentication Required": "authentication is required to connect to mmo", - "Authentication": "Disabled", + "Authentication": "Required", - "// ApiUrl": "SoDOff API server URL for authentication calls", + "// ApiUrl": "SoDOff API server URL for authentication calls and other calls", "ApiUrl": "http://localhost:5000", "// BypassToken": "Token allowed to connect without authentication",