Decompiled source of AutoModeration v1.0.4

AutoModeration.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.Text;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Mirror;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("com.AutoModeration")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.4.0")]
[assembly: AssemblyInformationalVersion("1.0.4+d2286c1e563307c992735a09145adb037af56239")]
[assembly: AssemblyProduct("Auto Moderation")]
[assembly: AssemblyTitle("AutoModeration")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.4.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace AutoModeration
{
	public class WarningRecord
	{
		public DateTime Timestamp { get; set; }

		public string PlayerName { get; set; }

		public string SteamID { get; set; }

		public string TriggeringMessage { get; set; }

		public int WarnCount { get; set; }

		public int MaxWarnings { get; set; }

		public override string ToString()
		{
			return $"[{Timestamp:yyyy-MM-dd HH:mm:ss}] Player: {PlayerName} (ID: {SteamID}) | Warning {WarnCount}/{MaxWarnings} | Trigger: \"{TriggeringMessage}\"";
		}
	}
	public class BlockRule
	{
		public string Pattern { get; set; }

		public MatchType Type { get; set; }

		public bool IsMatch(string message)
		{
			return Type switch
			{
				MatchType.Contains => message.IndexOf(Pattern, StringComparison.OrdinalIgnoreCase) >= 0, 
				MatchType.StartsWith => message.StartsWith(Pattern, StringComparison.OrdinalIgnoreCase), 
				MatchType.EndsWith => message.EndsWith(Pattern, StringComparison.OrdinalIgnoreCase), 
				MatchType.Exact => Regex.IsMatch(message, "\\b" + Regex.Escape(Pattern) + "\\b", RegexOptions.IgnoreCase), 
				_ => false, 
			};
		}
	}
	public enum MatchType
	{
		Contains,
		StartsWith,
		EndsWith,
		Exact
	}
	[BepInPlugin("s0apy", "AutoModeration", "1.0.4")]
	public class Main : BaseUnityPlugin
	{
		internal static ManualLogSource Log;

		internal static string WarningLogPath;

		internal static List<BlockRule> ParsedBlockRules = new List<BlockRule>();

		internal static HashSet<string> HashedBlockedWords = new HashSet<string>();

		internal static List<string> ParsedAllowedPhrases = new List<string>();

		internal static List<Regex> ParsedRegexPatterns = new List<Regex>();

		internal static Dictionary<string, int> PlayerWarningLevels = new Dictionary<string, int>();

		internal static List<string> MonitoredChannels = new List<string>();

		internal static ConfigEntry<bool> AutoModEnabled;

		internal static ConfigEntry<bool> DisableInSinglePlayer;

		internal static ConfigEntry<string> MonitoredChatChannels;

		internal static ConfigEntry<string> BlockedWords;

		internal static ConfigEntry<string> AllowedPhrases;

		internal static ConfigEntry<string> RegexPatterns;

		internal static ConfigEntry<bool> EnableHostActions;

		internal static ConfigEntry<string> HostAction;

		internal static ConfigEntry<bool> WarningSystemEnabled;

		internal static ConfigEntry<int> WarningsUntilAction;

		internal static ConfigEntry<bool> ResetWarningsOnDisconnect;

		private void Awake()
		{
			//IL_0157: Unknown result type (might be due to invalid IL or missing references)
			//IL_0161: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
			WarningLogPath = Path.Combine(directoryName, "AutoMod_WarningLog.txt");
			AutoModEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1. General", "Enabled", true, "Enables the auto-moderator to block messages.");
			DisableInSinglePlayer = ((BaseUnityPlugin)this).Config.Bind<bool>("1. General", "Disable in Single-Player", true, "istg if u use ts on singleplayer i am NOT helping u fix this do not PM me.");
			MonitoredChatChannels = ((BaseUnityPlugin)this).Config.Bind<string>("1. General", "Monitored Channels", "GLOBAL", "Comma-separated list of chat channels to monitor. (e.g., GLOBAL, ROOM, PARTY). Case-insensitive.");
			BlockedWords = ((BaseUnityPlugin)this).Config.Bind<string>("2. Word Filters", "Blocked Words", "*badword*, rude*, *insult, heck, crypto*", "Comma-separated list of words/phrases to block. Use '*' for wildcards. Non-wildcard words are hashed for security.");
			AllowedPhrases = ((BaseUnityPlugin)this).Config.Bind<string>("2. Word Filters", "Allowed Phrases (Whitelist)", "crypto, grapefruit, have a nice day", "Comma-separated list of phrases that act as exceptions to your blocked words/patterns.");
			RegexPatterns = ((BaseUnityPlugin)this).Config.Bind<string>("3. Advanced Filters", "Regex Patterns", "", "Comma-separated list of Regex patterns for advanced filtering.");
			EnableHostActions = ((BaseUnityPlugin)this).Config.Bind<bool>("4. Punishments", "Enable Host Actions", true, "If enabled, the host will automatically take action against players.");
			HostAction = ((BaseUnityPlugin)this).Config.Bind<string>("4. Punishments", "Action Type", "Kick", new ConfigDescription("The action to take when a player reaches the warning limit.", (AcceptableValueBase)(object)new AcceptableValueList<string>(new string[2] { "Kick", "Ban" }), Array.Empty<object>()));
			WarningSystemEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("5. Warning System", "Enabled", true, "Enable the progressive warning system. If false, punishments are immediate.");
			WarningsUntilAction = ((BaseUnityPlugin)this).Config.Bind<int>("5. Warning System", "Warnings Until Action", 3, "Number of infractions a player can have before the 'Action Type' is triggered.");
			ResetWarningsOnDisconnect = ((BaseUnityPlugin)this).Config.Bind<bool>("5. Warning System", "Reset Warnings On Disconnect", true, "If true, a player's warning count is cleared when they leave the server.");
			UpdateMonitoredChannelsList();
			UpdateBlockRulesList();
			UpdateAllowedPhrasesList();
			UpdateRegexPatternsList();
			Harmony.CreateAndPatchAll(typeof(HarmonyPatches), (string)null);
			Log.LogInfo((object)("[AutoModeration v1.0.4] has loaded with cryptographic hashing. Warning log saved to: " + WarningLogPath));
		}

		internal static string ComputeSha256Hash(string rawData)
		{
			using SHA256 sHA = SHA256.Create();
			byte[] array = sHA.ComputeHash(Encoding.UTF8.GetBytes(rawData));
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < array.Length; i++)
			{
				stringBuilder.Append(array[i].ToString("x2"));
			}
			return stringBuilder.ToString();
		}

		private void UpdateMonitoredChannelsList()
		{
			if (string.IsNullOrWhiteSpace(MonitoredChatChannels.Value))
			{
				MonitoredChannels.Clear();
			}
			else
			{
				MonitoredChannels = (from c in MonitoredChatChannels.Value.Split(',')
					select c.Trim().ToUpperInvariant() into c
					where !string.IsNullOrEmpty(c)
					select c).ToList();
			}
			Log.LogInfo((object)("Auto-moderator will monitor the following channels: " + string.Join(", ", MonitoredChannels)));
		}

		private void UpdateBlockRulesList()
		{
			ParsedBlockRules.Clear();
			HashedBlockedWords.Clear();
			if (string.IsNullOrWhiteSpace(BlockedWords.Value))
			{
				return;
			}
			string[] array = BlockedWords.Value.Split(',');
			string[] array2 = array;
			foreach (string text in array2)
			{
				string text2 = text.Trim();
				if (string.IsNullOrEmpty(text2))
				{
					continue;
				}
				if (text2.Contains("*"))
				{
					bool flag = text2.StartsWith("*");
					bool flag2 = text2.EndsWith("*");
					string pattern = text2.Trim('*');
					if (flag && flag2)
					{
						ParsedBlockRules.Add(new BlockRule
						{
							Pattern = pattern,
							Type = MatchType.Contains
						});
					}
					else if (flag)
					{
						ParsedBlockRules.Add(new BlockRule
						{
							Pattern = pattern,
							Type = MatchType.EndsWith
						});
					}
					else if (flag2)
					{
						ParsedBlockRules.Add(new BlockRule
						{
							Pattern = pattern,
							Type = MatchType.StartsWith
						});
					}
				}
				else
				{
					string item = ComputeSha256Hash(text2.ToLowerInvariant());
					HashedBlockedWords.Add(item);
				}
			}
			Log.LogInfo((object)$"{HashedBlockedWords.Count} words hashed, {ParsedBlockRules.Count} wildcard rules loaded.");
		}

		private void UpdateAllowedPhrasesList()
		{
			if (string.IsNullOrWhiteSpace(AllowedPhrases.Value))
			{
				ParsedAllowedPhrases.Clear();
				return;
			}
			ParsedAllowedPhrases = (from p in AllowedPhrases.Value.Split(',')
				select p.Trim() into p
				where !string.IsNullOrEmpty(p)
				select p).ToList();
		}

		private void UpdateRegexPatternsList()
		{
			ParsedRegexPatterns.Clear();
			if (string.IsNullOrWhiteSpace(RegexPatterns.Value))
			{
				return;
			}
			string[] array = RegexPatterns.Value.Split(',');
			string[] array2 = array;
			foreach (string text in array2)
			{
				try
				{
					string text2 = text.Trim();
					if (!string.IsNullOrEmpty(text2))
					{
						ParsedRegexPatterns.Add(new Regex(text2, RegexOptions.IgnoreCase | RegexOptions.Compiled));
					}
				}
				catch (Exception ex)
				{
					Log.LogError((object)("[AUTOMOD] Invalid Regex pattern '" + text + "' skipped. Error: " + ex.Message));
				}
			}
		}
	}
	[HarmonyPatch]
	internal static class HarmonyPatches
	{
		[HarmonyPrefix]
		[HarmonyPatch(typeof(ChatBehaviour), "UserCode_Rpc_RecieveChatMessage__String__Boolean__ChatChannel")]
		internal static bool InterceptChatMessage_Prefix(ChatBehaviour __instance, string message, ChatChannel _chatChannel)
		{
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			if (Main.DisableInSinglePlayer.Value && AtlyssNetworkManager._current._soloMode)
			{
				return true;
			}
			if (!Main.AutoModEnabled.Value || !Main.MonitoredChannels.Contains(((object)(ChatChannel)(ref _chatChannel)).ToString().ToUpperInvariant()))
			{
				return true;
			}
			try
			{
				string text = Regex.Replace(message, "<color=#([0-9a-fA-F]{6})>|</color>", string.Empty);
				object? obj = typeof(ChatBehaviour).GetField("_player", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(__instance);
				Player val = (Player)((obj is Player) ? obj : null);
				if (val != null)
				{
					string text2 = val._nickname ?? "Unknown Player";
					string text3 = text;
					foreach (string parsedAllowedPhrase in Main.ParsedAllowedPhrases)
					{
						text3 = Regex.Replace(text3, Regex.Escape(parsedAllowedPhrase), string.Empty, RegexOptions.IgnoreCase);
					}
					string text4 = FindInfractionReason(text3);
					if (!string.IsNullOrEmpty(text4))
					{
						string text5 = $"[AUTOMOD] Infraction by [{text2}] in channel [{_chatChannel}]. Reason: {text4}. Original Message: \"{text}\"";
						Main.Log.LogWarning((object)text5);
						ProcessInfraction(val, text2, text);
						return false;
					}
				}
			}
			catch (Exception arg)
			{
				Main.Log.LogError((object)$"[AUTOMOD] Error during message interception: {arg}");
			}
			return true;
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(HostConsole), "Destroy_PeerListEntry")]
		internal static void OnPlayerDisconnect_Postfix(HostConsole __instance, int _connID)
		{
			if (Main.ResetWarningsOnDisconnect.Value)
			{
				HC_PeerListEntry val = ((IEnumerable<HC_PeerListEntry>)__instance._peerListEntries).FirstOrDefault((Func<HC_PeerListEntry, bool>)((HC_PeerListEntry e) => ((ListDataEntry)e)._dataID == _connID));
				if ((Object)(object)val?._peerPlayer != (Object)null && !string.IsNullOrEmpty(val._peerPlayer._steamID) && Main.PlayerWarningLevels.ContainsKey(val._peerPlayer._steamID))
				{
					Main.PlayerWarningLevels.Remove(val._peerPlayer._steamID);
					Main.Log.LogInfo((object)("[AUTOMOD] Cleared warnings for disconnected player: " + val._peerPlayer._nickname));
				}
			}
		}

		private static string FindInfractionReason(string message)
		{
			if (string.IsNullOrWhiteSpace(message))
			{
				return string.Empty;
			}
			foreach (BlockRule parsedBlockRule in Main.ParsedBlockRules)
			{
				if (parsedBlockRule.IsMatch(message))
				{
					return "matched wildcard rule '" + parsedBlockRule.Pattern + "'";
				}
			}
			char[] separator = new char[11]
			{
				' ', '.', ',', '!', '?', ';', ':', '-', '_', '\n',
				'\r'
			};
			string[] array = message.Split(separator, StringSplitOptions.RemoveEmptyEntries);
			string[] array2 = array;
			foreach (string text in array2)
			{
				if (!string.IsNullOrWhiteSpace(text))
				{
					string item = Main.ComputeSha256Hash(text.ToLowerInvariant());
					if (Main.HashedBlockedWords.Contains(item))
					{
						return "matched hashed word '" + text + "'";
					}
				}
			}
			foreach (Regex parsedRegexPattern in Main.ParsedRegexPatterns)
			{
				if (parsedRegexPattern.IsMatch(message))
				{
					return $"matched Regex pattern '{parsedRegexPattern}'";
				}
			}
			return string.Empty;
		}

		private static void ProcessInfraction(Player targetPlayer, string targetPlayerName, string triggeringMessage)
		{
			Player mainPlayer = Player._mainPlayer;
			if (mainPlayer == null || !mainPlayer._isHostPlayer)
			{
				return;
			}
			if (!Main.WarningSystemEnabled.Value)
			{
				if (Main.EnableHostActions.Value)
				{
					TakeHostAction(targetPlayer, targetPlayerName, triggeringMessage);
				}
				return;
			}
			string steamID = targetPlayer._steamID;
			if (string.IsNullOrEmpty(steamID))
			{
				Main.Log.LogError((object)("[AUTOMOD] Cannot warn player [" + targetPlayerName + "] - they have no Steam ID."));
				return;
			}
			if (!Main.PlayerWarningLevels.ContainsKey(steamID))
			{
				Main.PlayerWarningLevels[steamID] = 0;
			}
			Main.PlayerWarningLevels[steamID]++;
			int num = Main.PlayerWarningLevels[steamID];
			int value = Main.WarningsUntilAction.Value;
			WarningRecord record = new WarningRecord
			{
				Timestamp = DateTime.Now,
				PlayerName = targetPlayerName,
				SteamID = steamID,
				TriggeringMessage = triggeringMessage,
				WarnCount = num,
				MaxWarnings = value
			};
			SaveWarningToFile(record);
			if (num >= value)
			{
				Main.Log.LogInfo((object)$"[AUTOMOD] Player [{targetPlayerName}] reached {num}/{value} warnings. Taking action.");
				if (Main.EnableHostActions.Value)
				{
					TakeHostAction(targetPlayer, targetPlayerName, triggeringMessage);
				}
			}
		}

		private static void TakeHostAction(Player targetPlayer, string targetPlayerName, string triggeringMessage)
		{
			if ((Object)(object)HostConsole._current == (Object)null || ((NetworkBehaviour)targetPlayer).connectionToClient == null)
			{
				return;
			}
			try
			{
				int connectionId = ((NetworkConnection)((NetworkBehaviour)targetPlayer).connectionToClient).connectionId;
				string text = Main.HostAction.Value.ToLower();
				string text2 = ((text == "kick") ? $"/kick {connectionId}" : $"/ban {connectionId}");
				Main.Log.LogInfo((object)("[AUTOMOD] Host executing command: \"" + text2 + "\" on player [" + targetPlayerName + "]."));
				HostConsole._current.Init_ServerMessage(text2);
				string record = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] ACTION: Player {targetPlayerName} (ID: {targetPlayer._steamID}) was {text.ToUpper()}ed for accumulating too many warnings. Final straw: \"{triggeringMessage}\"";
				SaveWarningToFile(record);
			}
			catch (Exception arg)
			{
				Main.Log.LogError((object)$"[AUTOMOD] Failed to perform host action on [{targetPlayerName}]: {arg}");
			}
		}

		private static void SaveWarningToFile(object record)
		{
			try
			{
				File.AppendAllText(Main.WarningLogPath, record.ToString() + Environment.NewLine);
			}
			catch (Exception ex)
			{
				Main.Log.LogError((object)("[AUTOMOD] Failed to write to warning log: " + ex.Message));
			}
		}
	}
	internal static class ModInfo
	{
		public const string GUID = "s0apy";

		public const string NAME = "AutoModeration";

		public const string VERSION = "1.0.4";
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}