using System; using System.Text.RegularExpressions; namespace RynthCore.Plugin.RynthChat; // ChatMessageType values from ACE.Entity.Enum.ChatMessageType (ACE binary is // byte-identical to retail, so these are authoritative for retail acclient.exe too). internal static class ChatClassifier { internal const string Chat = "Chat"; // Local/Tell/Allegiance/Fellow — all player-to-player internal const string Channels = "Channels"; // General/Trade/LFG/Roleplay/Society/Admin — all 0x08/0x09 internal const string System = "System"; internal const string Combat = "Combat"; internal const string Rynth = "Rynth"; // Any RynthCore/RynthAi/RynthChat plugin output ("[Rynth…]") internal const string Items = "Items"; // RynthAi's item info ("[RynthAi Item] ", on select or /ra iteminfo) internal const string Other = "Other"; internal static readonly string[] AllChannels = { Chat, Channels, System, Combat, Rynth, Items, Other }; /// /// True for output emitted by the Rynth stack itself — every Rynth plugin tags /// its chat lines with a "[Rynth…]" prefix (e.g. "[RynthAi]", "[RynthChat]", /// "[RynthAi Craft]"). These are routed to the dedicated Rynth tab regardless of /// the chatType the plugin happened to write them with, so nothing gets scattered /// across System/Other/Chat and lost. /// internal static bool IsRynthOutput(string text) => text.StartsWith("[Rynth", StringComparison.OrdinalIgnoreCase); /// RynthAi's item info (RynthAiPlugin.ItemPrefix): its own Items tab (2026-10-09). internal static bool IsItemInfo(string text) => text.StartsWith("[RynthAi Item] ", StringComparison.Ordinal); // ── Channel classification ───────────────────────────────────────────── /// /// Channel tab for a line. Prefer the text's [General]/[Trade]/… tag when present — /// ILT (and some ACE builds) deliver those as type 0x1C / 28 instead of 0x08/0x09, which /// used to dump Trade/General into Other and leave the Channels tab empty. /// internal static string ChannelFor(string? text, int chatType) { if (IsChannelTagged(text)) return Channels; return ChannelForType(chatType); } /// Legacy type-only map (tests / callers without the line text). internal static string ChannelFor(int chatType) => ChannelForType(chatType); internal static string ChannelForType(int chatType) => (uint)chatType switch { 0x02 or 0x03 or 0x04 or 0x0A or 0x0B or 0x0C or 0x12 or 0x13 => Chat, 0x08 or 0x09 or 0x1C => Channels, 0x00 or 0x05 or 0x0D or 0x14 or 0x17 or 0x18 or 0x19 or 0x1F => System, 0x06 or 0x07 or 0x11 or 0x15 or 0x16 => Combat, _ => Other, }; /// True when the line opens with a retail/ILT channel tag ([Trade], [General], …). internal static bool IsChannelTagged(string? text) { if (string.IsNullOrEmpty(text) || text[0] != '[') return false; return text.StartsWith("[General]", StringComparison.OrdinalIgnoreCase) || text.StartsWith("[Trade]", StringComparison.OrdinalIgnoreCase) || text.StartsWith("[LFG]", StringComparison.OrdinalIgnoreCase) || text.StartsWith("[Roleplay]", StringComparison.OrdinalIgnoreCase) || text.StartsWith("[Society]", StringComparison.OrdinalIgnoreCase) || text.StartsWith("[Celestial Hand]", StringComparison.OrdinalIgnoreCase) || text.StartsWith("[Eldrytch Web]", StringComparison.OrdinalIgnoreCase) || text.StartsWith("[Radiant Blood]", StringComparison.OrdinalIgnoreCase) || text.StartsWith("[Admin]", StringComparison.OrdinalIgnoreCase); } // ── Player chat (0.4.0) ──────────────────────────────────────────────── // The engine's ChatPlayerText (RynthCore, UI/Data/ChatPlayerText.cs) has the same rule; keep // the two the same. ChatBuffer keeps these lines in a ring of their own, so a reload or a // closed chat window doesn't lose tells and fellowship lines to combat spam. /// /// True for a line a player typed: say, tells both ways, emotes, allegiance, fellowship, every /// channel line, an admin's tell, and a room line of any other type ("[Room] Name says, \""). /// Never plugin output, item info, combat or spell words. /// internal static bool IsPlayerChat(string channel, int chatType, string? text) { if (channel is Rynth or Items) return false; if (channel is Chat or Channels) return true; if (chatType == 0x1F) return true; // AdminTell if (channel == Combat) return false; return LooksLikeSpeech(text); } /// "[Tag] Name says, \"" / "[Tag] Name tells you, \"" (AC's name links allowed), or "You say ..., \"". internal static bool LooksLikeSpeech(string? text) { if (string.IsNullOrEmpty(text) || text.Length < 8) return false; int start = 0; bool tagged = false; if (text[0] == '[') { int close = text.IndexOf(']', 1); if (close < 2 || close > 41 || close + 1 >= text.Length || text[close + 1] != ' ') return false; if (text.IndexOf('[', 1, close - 1) >= 0) return false; start = close + 2; tagged = true; } int quote = text.IndexOf(", \"", start, StringComparison.Ordinal); if (quote < 0 || quote - start > 220) return false; string who = text.Substring(start, quote - start); if (who.IndexOf('"') >= 0) return false; if (who.StartsWith("You say", StringComparison.Ordinal) || who.StartsWith("You tell ", StringComparison.Ordinal)) return true; return tagged && (who.Contains(" says", StringComparison.Ordinal) || who.Contains(" tells you", StringComparison.Ordinal)); } internal static uint ColorFor(string channel) => channel switch { Chat => 0xFFE0E0E0, Channels => 0xFF7AB8F5, System => 0xFF8CA6BF, Combat => 0xFFD93333, _ => 0xFFAAAAAA, }; // ── Sender extraction ────────────────────────────────────────────────── // Bob says, "..." / Bob tells you, "..." / [General] Bob says, "..." // [Allegiance] Bob says, "..." / Bob says on the Fellowship channel, "..." // Before 0.2.0 the channel-tag forms captured "[General] Bob" as the sender. // The sender is metadata (mentions, rules); the engine no longer prints it // in front of the line, which already names the speaker. private static readonly TimeSpan Timeout = TimeSpan.FromMilliseconds(20); private static readonly Regex _incomingRe = new( @"^(?:\[[^\]]{1,40}\] )?(.+?) (?:says|tells you)\b[^""]{0,60}?, """, RegexOptions.CultureInvariant, Timeout); private static readonly Regex _tellOutRe = new(@"^You tell (.+?), """, RegexOptions.CultureInvariant, Timeout); internal static string? SenderFor(string text, int chatType) => (uint)chatType switch { 0x02 or 0x03 or 0x08 or 0x0A or 0x12 or 0x13 => Match(_incomingRe, text), 0x04 => TellOutSender(text), _ => null, }; private static string? Match(Regex re, string text) { try { var m = re.Match(text); return m.Success ? m.Groups[1].Value : null; } catch (RegexMatchTimeoutException) { return null; } } private static string? TellOutSender(string text) { string? name = Match(_tellOutRe, text); return name != null ? $"→{name}" : null; } }