diff --git a/IPAndSiteBlocker.cs b/IPAndSiteBlocker.cs index a76da50..585f8ad 100644 --- a/IPAndSiteBlocker.cs +++ b/IPAndSiteBlocker.cs @@ -2,8 +2,13 @@ using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Modules.Commands; using CounterStrikeSharp.API.Modules.Utils; +using CounterStrikeSharp.API.Modules.Memory; +using CounterStrikeSharp.API.Core.Attributes.Registration; using System.Text.Json.Serialization; using System.Text.RegularExpressions; +using System.Reflection; +using System.Text.Json; + namespace SiteAndIPBlocker; @@ -11,15 +16,24 @@ public class SiteAndIPBlockerConfig : BasePluginConfig { [JsonPropertyName("whitelist")] public List Whitelist { get; set; } = new List(); - + [JsonPropertyName("block_message")] - public string BlockMessage { get; set; } = " {darkred}Blocked: Sending IP addresses or websites is not allowed."; + public string BlockMessage { get; set; } = "{darkred}Blocked: Sending IP addresses or websites is not allowed."; + + [JsonPropertyName("name_action")] + public int NameAction { get; set; } = 0; + + [JsonPropertyName("rename_message")] + public string RenameMessage { get; set; } = "{darkred}Your name contains a blocked IP address or website. It will be renamed."; + + [JsonPropertyName("ConfigVersion")] + public override int Version { get; set; } = 2; } public class SiteAndIPBlocker : BasePlugin, IPluginConfig { public override string ModuleName => "Site and IP Blocker"; - public override string ModuleVersion => "1.0.0"; + public override string ModuleVersion => "1.1.0"; public override string ModuleAuthor => "Nathy"; public override string ModuleDescription => "Block sites and IP addresses in chat."; @@ -30,8 +44,29 @@ public class SiteAndIPBlocker : BasePlugin, IPluginConfig(T config) where T : BasePluginConfig, new() + { + var newCfgVersion = new T().Version; + + if (config.Version == newCfgVersion) + return; + + config.Version = newCfgVersion; + + var updatedJsonContent = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true }); + File.WriteAllText(CfgPath, updatedJsonContent); + + Console.WriteLine($"Config updated for V{newCfgVersion}."); + } + + + public override void Load(bool hotReload) { AddCommandListener("say", OnPlayerChatAll); @@ -65,6 +100,12 @@ public override void Load(bool hotReload) private string ReplaceColorPlaceholders(string message) { + + if (!string.IsNullOrEmpty(message) && message[0] != ' ') + { + message = " " + message; + } + foreach (var colorPlaceholder in ColorMap) { message = message.Replace(colorPlaceholder.Key, colorPlaceholder.Value.ToString()); @@ -122,6 +163,81 @@ private HookResult OnPlayerChatTeam(CCSPlayerController? player, CommandInfo mes return HookResult.Continue; } + [GameEventHandler] + public HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventInfo info) + { + checkPlayerName(@event.Userid); + return HookResult.Continue; + } + + + private void checkPlayerName(CCSPlayerController? player) + { + if (player == null || player.IsBot) + { + return; + } + string playerName = player.PlayerName; + + if (ContainsUrlOrIp(playerName)) + { + if (!IsWhitelisted(playerName)) + { + if (Config.NameAction == 0) + { + NativeAPI.IssueServerCommand($"kickid {player.UserId}"); + } + else if (Config.NameAction == 1) + { + RenamePlayer(player, playerName); + } + } + } + } + + + [GameEventHandler] + public HookResult OnPlayerChangeName(EventPlayerChangename @event, GameEventInfo info) + { + CCSPlayerController player = @event.Userid; + + if (player == null || player.IsBot) + { + return HookResult.Continue; + } + + string newName = @event.Newname; + + if (ContainsUrlOrIp(newName)) + { + if (!IsWhitelisted(newName)) + { + if (Config.NameAction == 0) + { + NativeAPI.IssueServerCommand($"kickid {player.UserId}"); + return HookResult.Handled; + } + else if (Config.NameAction == 1) + { + RenamePlayer(player, newName); + } + } + } + + return HookResult.Continue; + } + + + private void RenamePlayer(CCSPlayerController player, string playerName) + { + string cleanedName = UrlOrIpRegex.Replace(playerName, "").Trim(); + + NativeAPI.IssueServerCommand($"css_rename \"{player.PlayerName}\" \"{cleanedName}\""); + + string renameMessage = ReplaceColorPlaceholders(Config.RenameMessage); + + player.PrintToChat(renameMessage); + } private bool IsWhitelisted(string message) @@ -130,4 +246,4 @@ private bool IsWhitelisted(string message) return Config.Whitelist.Any(whitelistedItem => lowerCaseMessage.Contains(whitelistedItem.ToLowerInvariant())); } -} \ No newline at end of file +}