From 08d64656d44935d783599d44d9ba4f3b8ad49b0d Mon Sep 17 00:00:00 2001 From: Madpeter <57852827+Madpeterz@users.noreply.github.com> Date: Wed, 8 Jun 2022 09:47:53 +0100 Subject: [PATCH] wip relay --- SecondBotEvents/Config/RelayConfig.cs | 60 ++++++ SecondBotEvents/Config/config.cs | 4 + SecondBotEvents/Services/DiscordService.cs | 8 + SecondBotEvents/Services/RelayService.cs | 232 +++++++++++++++++++++ SecondBotEvents/Services/Services.cs | 2 +- 5 files changed, 305 insertions(+), 1 deletion(-) create mode 100644 SecondBotEvents/Config/RelayConfig.cs create mode 100644 SecondBotEvents/Services/RelayService.cs diff --git a/SecondBotEvents/Config/RelayConfig.cs b/SecondBotEvents/Config/RelayConfig.cs new file mode 100644 index 0000000..b17e963 --- /dev/null +++ b/SecondBotEvents/Config/RelayConfig.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace SecondBotEvents.Config +{ + public class RelayConfig : Config + { + public RelayConfig(bool fromENV, string fromFolder = "") : base(fromENV, fromFolder) { } + + protected override void MakeSettings() + { + filename = "relay"; + settings.Add("count"); + int loop = 1; + while(loop <= GetRelayCount()) + { + settings.Add(loop.ToString() + "_enabled"); + settings.Add(loop.ToString() + "_source_type"); + settings.Add(loop.ToString() + "_source_option"); + settings.Add(loop.ToString() + "_target_type"); + settings.Add(loop.ToString() + "_target_option"); + loop++; + } + } + + public int GetRelayCount() + { + return ReadSettingAsInt("count", 1); + } + + public bool GetRelayEnabled(int relayID) + { + return ReadSettingAsBool(relayID.ToString() + "_enabled", false); + } + + public string RelaySourceType(int relayID) + { + return ReadSettingAsString(relayID.ToString() + "_source_type", "none"); + } + + public string RelaySourceOption(int relayID) + { + return ReadSettingAsString(relayID.ToString() + "_source_option", ""); + } + + public string RelayTargetType(int relayID) + { + return ReadSettingAsString(relayID.ToString() + "_target_type", "none"); + } + + public string RelayTargetOption(int relayID) + { + return ReadSettingAsString(relayID.ToString() + "_target_option", ""); + } + + + } + +} \ No newline at end of file diff --git a/SecondBotEvents/Config/config.cs b/SecondBotEvents/Config/config.cs index 8387dcd..77eaded 100644 --- a/SecondBotEvents/Config/config.cs +++ b/SecondBotEvents/Config/config.cs @@ -43,6 +43,10 @@ namespace SecondBotEvents.Config { string reader = "Get"+ StringExtensions.FirstCharToUpper(setting); MethodInfo theMethod = thisType.GetMethod(reader); + if (theMethod == null) + { + continue; + } string value = theMethod.Invoke(this, null).ToString(); saveMe.Add(setting, value); } diff --git a/SecondBotEvents/Services/DiscordService.cs b/SecondBotEvents/Services/DiscordService.cs index 4a7f898..71c8c6c 100644 --- a/SecondBotEvents/Services/DiscordService.cs +++ b/SecondBotEvents/Services/DiscordService.cs @@ -694,6 +694,14 @@ namespace SecondBotEvents.Services return reply.Id; } + public ulong SendMessageToChannel(ulong serverid, ulong channelid, string message) + { + SocketGuild server = DiscordClient.GetGuild(serverid); + ITextChannel channel = server.GetTextChannel(channelid); + IMessage reply = channel.SendMessageAsync(message).GetAwaiter().GetResult(); + return reply.Id; + } + protected string GetChannelName(string input) { input = input.Replace("-", ""); diff --git a/SecondBotEvents/Services/RelayService.cs b/SecondBotEvents/Services/RelayService.cs new file mode 100644 index 0000000..70f38bb --- /dev/null +++ b/SecondBotEvents/Services/RelayService.cs @@ -0,0 +1,232 @@ +using Newtonsoft.Json; +using OpenMetaverse; +using SecondBotEvents.Config; +using System; +using System.Linq; +using System.Collections; +using System.Collections.Generic; +using System.Net.Http; + +namespace SecondBotEvents.Services +{ + public class RelayService : BotServices + { + protected RelayConfig myConfig; + protected bool botConnected = false; + protected List Relays = new List(); + public RelayService(EventsSecondBot setMaster) : base(setMaster) + { + myConfig = new RelayConfig(master.fromEnv, master.fromFolder); + } + + public override string Status() + { + if (myConfig == null) + { + return "Config broken"; + } + if (botConnected == false) + { + return "Waiting for bot"; + } + return "Active"; + } + + protected void BotClientRestart(object o, BotClientNotice e) + { + botConnected = false; + Console.WriteLine("Interaction Service [Attached to new client]"); + GetClient().Network.LoggedOut += BotLoggedOut; + GetClient().Network.SimConnected += BotLoggedIn; + } + + protected void BotLoggedOut(object o, LoggedOutEventArgs e) + { + botConnected = false; + GetClient().Network.SimConnected += BotLoggedIn; + Console.WriteLine("Interaction Service [Standby]"); + } + + protected void BotLoggedIn(object o, SimConnectedEventArgs e) + { + GetClient().Network.SimConnected -= BotLoggedIn; + botConnected = true; + Console.WriteLine("Interaction Service [Active]"); + } + + public override void Start() + { + Stop(); + master.BotClientNoticeEvent += BotClientRestart; + Console.WriteLine("Interaction Service [Starting]"); + } + + public override void Stop() + { + master.BotClientNoticeEvent -= BotClientRestart; + if (master.BotClient != null) + { + if (GetClient() != null) + { + + } + } + Console.WriteLine("Interaction Service [Stopping]"); + } + } + + public class RelaySys + { + protected RelayService master; + public bool enabled = false; + public string fromType = ""; + public string fromOption = ""; + public string toType = ""; + public string toOption = ""; + protected int filterOnIndex = 0; + protected int relayID = 0; + + protected UUID targetuuid = UUID.Zero; + protected int targetchannel = 0; + + protected ulong targetulong1 = 0; + protected ulong targetulong2 = 0; + + protected string targeturl = ""; + protected string targetsharedsecret = ""; + protected int nonce = 1; + + public RelaySys(RelayService setMaster, bool setEnabled, string setFromType, string setFromOption, string setToType, string setToOption) + { + master = setMaster; + enabled = setEnabled; + fromType = setFromType; + fromOption = setFromOption; + toType = setToType; + toOption = setToOption; + Vaildate(); + } + + protected void Vaildate() + { + } + + protected HttpClient HTTPclient = new HttpClient(); + public void TriggerWith(string sourceType, string message, string filterOption1, string filterOption2=null, string filterOption3 = null) + { + // avatarim, {message}, avatarUUID + // groupim, {message}, groupUUID + // localchat, {message}, channel, TalkerType, TalkerUUID + // discord, {message}, serverid, channelid, talkerid + // *, {message} + /* + * fromType=discord + * fromOption=* + * fromOption=server@XXX + * fromOption=channel@XXX + * fromOption=user@XXX + * + * fromType=localchat + * fromOption=* + * fromOption=channel@4 + * fromOption=[type@avatar], [type@object], [type@service] + * fromOption=uuid@XXX-XXX-XXX-XXX + * + * fromType=groupim + * fromOption=* + * fromOption=group@XXX-XXX-XXX-XXX + * + * fromType=avatarim + * fromOption=* + * fromOption=avatar@XXX-XXX-XXX-XXX + * + * fromType=* + * fromOption=* + * + * toType=discord + * toOption=channelid + * + * toType=http + * toOption=url@sharedsecret + * + * toType=localchat + * toOption=channel + * + * toType=avatar + * toOption=avataruuid + * + * toType=group + * toOption=groupuuid + */ + if(enabled == false) { return; } // not enabled + if (message.Contains("{RELAY}") == true) { return; } // no messages from a relay should be relayed we dont like loops here + else if ((sourceType != fromType) && (sourceType != "*")) { return; } // not for this relay + else if ((filterOnIndex == 0) && (fromOption != "*")) { return; } // invaild filter selected [but some how not disabled] + else if ((filterOnIndex == 1) && (fromOption != filterOption1)) { return; } // did not match filter + else if ((filterOnIndex == 2) && (fromOption != filterOption2)) { return; } // did not match filter + else if ((filterOnIndex == 3) && (fromOption != filterOption3)) { return; } // did not match filter + message = "{RELAY}" + message; + switch (toType) + { + case "discord": + { + master.master.DiscordService.SendMessageToChannel(targetulong1, targetulong2, message); + break; + } + case "http": + { + long unixtime = SecondbotHelpers.UnixTimeNow(); + Dictionary values = new Dictionary + { + { "fromType", fromType }, + { "fromOption", fromOption }, + { "arg1", filterOption1 }, + { "arg2", filterOption2 }, + { "arg3", filterOption3 }, + { "message", message }, + { "unixtime", unixtime.ToString() }, + { "nonce", nonce.ToString() } + }; + nonce++; + string raw = ""; + foreach(string A in values.Values) + { + raw += A; + } + values.Add("hash", SecondbotHelpers.GetSHA1(raw + targetsharedsecret)); + + var content = new FormUrlEncodedContent(values); + try + { + HTTPclient.PostAsync(targeturl, content); + } + catch (Exception e) + { + LogFormater.Crit("[Relay] HTTP failed: " + e.Message + ""); + } + break; + } + case "localchat": + { + master.GetClient().Self.Chat(message, targetchannel, ChatType.Normal); + break; + } + case "avatar": + { + master.GetClient().Self.InstantMessage(targetuuid, message); + break; + } + case "group": + { + master.GetClient().Self.InstantMessageGroup(targetuuid, message); + break; + } + default: + { + break; + } + } + } + } +} + diff --git a/SecondBotEvents/Services/Services.cs b/SecondBotEvents/Services/Services.cs index 70a4d44..99a7233 100644 --- a/SecondBotEvents/Services/Services.cs +++ b/SecondBotEvents/Services/Services.cs @@ -13,7 +13,7 @@ namespace SecondBotEvents.Services master = setMaster; } - protected GridClient GetClient() + public GridClient GetClient() { return master.BotClient.client; }