// SPDX-License-Identifier: LicenseRef-OpenGenesisLINK-EULA using log4net; using Mono.Addins; using Nini.Config; using OpenMetaverse; using OpenSim.Framework; using OpenSim.Region.CoreModules.Avatar.InstantMessage; namespace OpenGenesisLINK.Addons.UniversalTranslator; /// /// Direct person-to-person IM translation before the existing grid transfer /// module; offline IM processing and all system/group dialogs remain untouched. /// [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "OglTranslatedInstantMessageModule")] public sealed class OglTranslatedInstantMessageModule : InstantMessageModule { private static readonly ILog Log = LogManager.GetLogger(typeof(OglTranslatedInstantMessageModule)); private readonly TranslatorRuntime _runtime = TranslatorRuntime.Shared; public override string Name => "OglTranslatedInstantMessageModule"; public override void Initialise(IConfigSource source) { _runtime.Configure(source); m_enabled = source.Configs["OglUniversalTranslator"]?.GetBoolean("Enabled", false) == true; if (m_enabled) Log.Info("[OGL TRANSLATOR] Direct IM translation module enabled (bilateral opt-in)."); } public override void OnInstantMessage(IClientAPI? client, GridInstantMessage im) { if (client is null || im is null || im.fromGroup || im.dialog != (byte)InstantMessageDialog.MessageFromAgent || client.AgentId.Guid != im.fromAgentID || !_runtime.Eligible(new UUID(im.fromAgentID), new UUID(im.toAgentID), im.message, true, out string sourceLanguage, out string targetLanguage)) { base.OnInstantMessage(client!, im); return; } // Clone before async work: caller may reuse/mutate the original message. GridInstantMessage outgoing = new(im, false); outgoing.timestamp = im.timestamp; _ = Task.Run(async () => { try { outgoing.message = await _runtime.TranslateAsync(im.message, new UUID(im.fromAgentID),new UUID(im.toAgentID),true).ConfigureAwait(false); base.OnInstantMessage(client, outgoing); } catch (Exception e) { Log.Warn("[OGL TRANSLATOR] IM translation failed, sending original: " + e.GetType().Name); try { base.OnInstantMessage(client, im); } catch (Exception sendError) { Log.Error("[OGL TRANSLATOR] Original IM delivery failed", sendError); } } }); } }