Skip to content

Commit

Permalink
Update IPAndSiteBlocker.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
NaathySz authored Apr 26, 2024
1 parent 2acd28f commit 2ed272a
Showing 1 changed file with 120 additions and 4 deletions.
124 changes: 120 additions & 4 deletions IPAndSiteBlocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,38 @@
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;

public class SiteAndIPBlockerConfig : BasePluginConfig
{
[JsonPropertyName("whitelist")]
public List<string> Whitelist { get; set; } = new List<string>();

[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<SiteAndIPBlockerConfig>
{
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.";

Expand All @@ -30,8 +44,29 @@ public class SiteAndIPBlocker : BasePlugin, IPluginConfig<SiteAndIPBlockerConfig
public void OnConfigParsed(SiteAndIPBlockerConfig config)
{
Config = config;
UpdateConfig(config);
}

private static readonly string AssemblyName = Assembly.GetExecutingAssembly().GetName().Name ?? "";
private static readonly string CfgPath = $"{Server.GameDirectory}/csgo/addons/counterstrikesharp/configs/plugins/{AssemblyName}/{AssemblyName}.json";

private static void UpdateConfig<T>(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);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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)
Expand All @@ -130,4 +246,4 @@ private bool IsWhitelisted(string message)

return Config.Whitelist.Any(whitelistedItem => lowerCaseMessage.Contains(whitelistedItem.ToLowerInvariant()));
}
}
}

0 comments on commit 2ed272a

Please sign in to comment.