diff --git a/Dockerfile b/Dockerfile index 7c5ed94..5e9281b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -36,6 +36,7 @@ ENV basic_Username='' \ commands_TimeWindowSecs=35 \ commands_Enabled='true' \ commands_ObjectMasterOptout='false' \ + commands_AllowServiceControl='false' \ datastore_AutoCleanAvatars='true' \ datastore_AvatarsCleanAfterMins=10 \ datastore_LocalChatHistoryLimit=120 \ diff --git a/SecondBotEvents/Commands/Services.cs b/SecondBotEvents/Commands/Services.cs new file mode 100644 index 0000000..26da4f8 --- /dev/null +++ b/SecondBotEvents/Commands/Services.cs @@ -0,0 +1,138 @@ +using Newtonsoft.Json; +using OpenMetaverse; +using SecondBotEvents.Services; +using System.Collections.Generic; +using System.Linq; + + +namespace SecondBotEvents.Commands +{ + [ClassInfo("Control services via commands (Requires the command enable service control flag be set to true)

controlable services:

" + + "BotClientService
"+ + "RLVService
" + + "HttpService
" + + "DiscordService
" + + "InteractionService
" + + "HomeboundService
" + + "EventsService
" + + "DialogService
" + + "TriggerOnEventService" + + "RelayService")] + public class Services : CommandsAPI + { + public Services(EventsSecondBot setmaster) : base(setmaster) + { + } + + protected List controlable = new List { "BotClientService","RLVService", + "HttpService", + "DiscordService", + "InteractionService", + "HomeboundService", + "EventsService", + "DialogService", + "TriggerOnEventService", + "RelayService"}; + + [About("Marks a service as enabled and starts it")] + [ReturnHints("ok")] + [ReturnHintsFailure("Unknown service")] + [ReturnHintsFailure("Unable to manage selected service")] + [ReturnHintsFailure("Service control is not enabled by config")] + public object EnableAndStartService(string servicename) + { + if(master.CommandsService.GetAllowServiceCommands() == false) + { + return Failure("Service control is not enabled by config"); + } + if(controlable.Contains(servicename) == false) + { + return Failure("Unknown service"); + } + BotServices service = master.GetService(servicename); + if (service == null) + { + return Failure("Unknown service"); + } + service.enableAndStart(); + return BasicReply("ok"); + } + + [About("stops a service if it is running")] + [ReturnHints("ok")] + [ReturnHintsFailure("Unknown service")] + [ReturnHintsFailure("Unable to manage selected service")] + [ReturnHintsFailure("Service control is not enabled by config")] + public object StopService(string servicename) + { + if (master.CommandsService.GetAllowServiceCommands() == false) + { + return Failure("Service control is not enabled by config"); + } + if (controlable.Contains(servicename) == false) + { + return Failure("Unknown service"); + } + BotServices service = master.GetService(servicename); + if (service == null) + { + return Failure("Unknown service"); + } + service.Stop(); + return BasicReply("ok"); + } + + [About("restarts a service if it is running")] + [ReturnHints("ok")] + [ReturnHintsFailure("Unknown service")] + [ReturnHintsFailure("Unable to manage selected service")] + [ReturnHintsFailure("Service control is not enabled by config")] + public object RestartService(string servicename) + { + if (master.CommandsService.GetAllowServiceCommands() == false) + { + return Failure("Service control is not enabled by config"); + } + if (controlable.Contains(servicename) == false) + { + return Failure("Unknown service"); + } + BotServices service = master.GetService(servicename); + if (service == null) + { + return Failure("Unknown service"); + } + service.Restart(); + return BasicReply("ok"); + } + + [About("Change or add a config value to a service")] + [ReturnHints("ok")] + [ReturnHintsFailure("Unknown service")] + [ReturnHintsFailure("Unable to manage selected service")] + [ReturnHintsFailure("Service control is not enabled by config")] + public object ChangeConfigValue(string servicename,string configname,string configvalue) + { + if (master.CommandsService.GetAllowServiceCommands() == false) + { + return Failure("Service control is not enabled by config"); + } + if (controlable.Contains(servicename) == false) + { + return Failure("Unknown service"); + } + BotServices service = master.GetService(servicename); + if (service == null) + { + return Failure("Unknown service"); + } + service.changeConfig(configname, configvalue); + return BasicReply("ok"); + } + + + + + + } +} diff --git a/SecondBotEvents/Config/CommandsConfig.cs b/SecondBotEvents/Config/CommandsConfig.cs index 845503c..1f2b6ca 100644 --- a/SecondBotEvents/Config/CommandsConfig.cs +++ b/SecondBotEvents/Config/CommandsConfig.cs @@ -20,9 +20,13 @@ namespace SecondBotEvents.Config settings.Add("TimeWindowSecs"); settings.Add("Enabled"); settings.Add("ObjectMasterOptout"); + settings.Add("AllowServiceControl"); settings.Add("HideStatusOutput"); } - + public bool GetAllowServiceControl() + { + return ReadSettingAsBool("AllowServiceControl", false); + } public bool GetEnabled() { return ReadSettingAsBool("Enabled", true); diff --git a/SecondBotEvents/Config/config.cs b/SecondBotEvents/Config/config.cs index 119e502..cb81c84 100644 --- a/SecondBotEvents/Config/config.cs +++ b/SecondBotEvents/Config/config.cs @@ -23,6 +23,16 @@ namespace SecondBotEvents.Config MakeSettings(); LoadSettings(fromENV, fromFolder); } + + public void updateKey(string key, string value) + { + if(mysettings.ContainsKey(key)) + { + mysettings[key] = value; + return; + } + mysettings.Add(key, value); + } public void setEnabled(bool enabled) { mysettings["Enabled"] = enabled.ToString(); diff --git a/SecondBotEvents/EventsSecondBot.cs b/SecondBotEvents/EventsSecondBot.cs index 9743909..0d72450 100644 --- a/SecondBotEvents/EventsSecondBot.cs +++ b/SecondBotEvents/EventsSecondBot.cs @@ -164,6 +164,7 @@ namespace SecondBotEvents RegisterService("DialogService"); RegisterService("CustomCommandsService"); RegisterService("TriggerOnEventService"); + RegisterService("RelayService"); if (BotClient.IsLoaded() == false) { LogFormater.Info("Config is not loaded :("); diff --git a/SecondBotEvents/Services/CommandsService.cs b/SecondBotEvents/Services/CommandsService.cs index 12165de..ad44172 100644 --- a/SecondBotEvents/Services/CommandsService.cs +++ b/SecondBotEvents/Services/CommandsService.cs @@ -49,6 +49,11 @@ namespace SecondBotEvents.Services LoadCommands(); } + public bool GetAllowServiceCommands() + { + return myConfig.GetAllowServiceControl(); + } + protected void LoadCommands() { Dictionary commandmodules = http_commands_helper.getCommandModules(); diff --git a/SecondBotEvents/Services/CustomCommandsService.cs b/SecondBotEvents/Services/CustomCommandsService.cs index c93fc3d..5413edf 100644 --- a/SecondBotEvents/Services/CustomCommandsService.cs +++ b/SecondBotEvents/Services/CustomCommandsService.cs @@ -11,7 +11,7 @@ namespace SecondBotEvents.Services { public class CustomCommandsService: BotServices { - protected CustomCommandsConfig myConfig; + protected new CustomCommandsConfig myConfig; protected bool botConnected = false; protected Dictionary commands = new Dictionary(); public bool HasCommand(string C) diff --git a/SecondBotEvents/Services/DataStoreService.cs b/SecondBotEvents/Services/DataStoreService.cs index 79cf00c..0b4e1c0 100644 --- a/SecondBotEvents/Services/DataStoreService.cs +++ b/SecondBotEvents/Services/DataStoreService.cs @@ -14,7 +14,7 @@ namespace SecondBotEvents.Services { public class DataStoreService : BotServices { - protected DataStoreConfig myConfig; + protected new DataStoreConfig myConfig; protected bool botConnected = false; public DataStoreService(EventsSecondBot setMaster) : base(setMaster) { diff --git a/SecondBotEvents/Services/DialogService.cs b/SecondBotEvents/Services/DialogService.cs index 9f2893c..2d621c8 100644 --- a/SecondBotEvents/Services/DialogService.cs +++ b/SecondBotEvents/Services/DialogService.cs @@ -17,7 +17,7 @@ namespace SecondBotEvents.Services public class DialogService : BotServices { protected bool botConnected = false; - protected DialogRelayConfig myConfig; + protected new DialogRelayConfig myConfig; public DialogService(EventsSecondBot setMaster) : base(setMaster) { myConfig = new DialogRelayConfig(master.fromEnv, master.fromFolder); diff --git a/SecondBotEvents/Services/DiscordService.cs b/SecondBotEvents/Services/DiscordService.cs index 208998e..aea686c 100644 --- a/SecondBotEvents/Services/DiscordService.cs +++ b/SecondBotEvents/Services/DiscordService.cs @@ -15,7 +15,7 @@ namespace SecondBotEvents.Services { public class DiscordService : BotServices { - protected Config.DiscordConfig myConfig = null; + protected new Config.DiscordConfig myConfig = null; protected bool AcceptEventsFromSL = false; protected bool DiscordIsReady = false; protected bool DiscordDisconnectExpected = false; diff --git a/SecondBotEvents/Services/EventsService.cs b/SecondBotEvents/Services/EventsService.cs index e5e5dbb..66d53ac 100644 --- a/SecondBotEvents/Services/EventsService.cs +++ b/SecondBotEvents/Services/EventsService.cs @@ -13,7 +13,7 @@ namespace SecondBotEvents.Services { public class EventsService : BotServices { - protected EventsConfig myConfig; + protected new EventsConfig myConfig; protected bool botConnected = false; protected string lastSimName = ""; protected UUID TrackGroupUUID = UUID.Zero; diff --git a/SecondBotEvents/Services/HomeboundService.cs b/SecondBotEvents/Services/HomeboundService.cs index d73755c..fc07c1c 100644 --- a/SecondBotEvents/Services/HomeboundService.cs +++ b/SecondBotEvents/Services/HomeboundService.cs @@ -8,7 +8,7 @@ namespace SecondBotEvents.Services { public class HomeboundService : BotServices { - protected HomeboundConfig myConfig; + protected new HomeboundConfig myConfig; protected bool botConnected = false; diff --git a/SecondBotEvents/Services/HttpService.cs b/SecondBotEvents/Services/HttpService.cs index d3f40bb..350a187 100644 --- a/SecondBotEvents/Services/HttpService.cs +++ b/SecondBotEvents/Services/HttpService.cs @@ -20,7 +20,7 @@ namespace SecondBotEvents.Services public class HttpService : BotServices { protected WebServer HTTPendpoint = null; - protected HttpConfig myConfig; + protected new HttpConfig myConfig; protected bool acceptBotCommands = false; public HttpService(EventsSecondBot setMaster) : base(setMaster) { diff --git a/SecondBotEvents/Services/InteractionService.cs b/SecondBotEvents/Services/InteractionService.cs index 21833b2..17445a2 100644 --- a/SecondBotEvents/Services/InteractionService.cs +++ b/SecondBotEvents/Services/InteractionService.cs @@ -11,7 +11,7 @@ namespace SecondBotEvents.Services { public class InteractionService : BotServices { - protected InteractionConfig myConfig; + protected new InteractionConfig myConfig; protected bool botConnected = false; public InteractionService(EventsSecondBot setMaster) : base(setMaster) { diff --git a/SecondBotEvents/Services/RelayService.cs b/SecondBotEvents/Services/RelayService.cs index 6587262..4b4dc8b 100644 --- a/SecondBotEvents/Services/RelayService.cs +++ b/SecondBotEvents/Services/RelayService.cs @@ -16,7 +16,7 @@ namespace SecondBotEvents.Services { public class RelayService : BotServices { - protected RelayConfig myConfig; + protected new RelayConfig myConfig; protected bool botConnected = false; public RelayService(EventsSecondBot setMaster) : base(setMaster) { diff --git a/SecondBotEvents/Services/Services.cs b/SecondBotEvents/Services/Services.cs index 4dc13e2..6b6005a 100644 --- a/SecondBotEvents/Services/Services.cs +++ b/SecondBotEvents/Services/Services.cs @@ -2,18 +2,31 @@ using System; using System.Collections.Generic; using System.Text; +using SecondBotEvents.Config; namespace SecondBotEvents.Services { public abstract class BotServices { public EventsSecondBot master; + protected Config.Config myConfig; protected bool running = false; public BotServices(EventsSecondBot setMaster) { master = setMaster; } + public void enableAndStart() + { + myConfig.setEnabled(true); + Restart(); + } + + public void changeConfig(string configKey, string configValue) + { + myConfig.updateKey(configKey, configValue); + } + public bool isRunning() { return running; } public GridClient GetClient() diff --git a/SecondBotEvents/Static/http_commands_helper.cs b/SecondBotEvents/Static/http_commands_helper.cs index d37b89a..f841a4b 100644 --- a/SecondBotEvents/Static/http_commands_helper.cs +++ b/SecondBotEvents/Static/http_commands_helper.cs @@ -27,6 +27,7 @@ namespace SecondBotEvents reply.Add("parcel", typeof(ParcelCommands)); reply.Add("self", typeof(Self)); reply.Add("streamadmin", typeof(StreamAdmin)); + reply.Add("services", typeof(SecondBotEvents.Commands.Services)); return reply; } } diff --git a/SecondBotEvents/WikiService.cs b/SecondBotEvents/WikiService.cs index 686f38d..c5fe05e 100644 --- a/SecondBotEvents/WikiService.cs +++ b/SecondBotEvents/WikiService.cs @@ -55,7 +55,7 @@ namespace SecondBotEvents string card = "
" + "
" + "
" + namespaceworker.FirstCharToUpper() + "
" - + "

" + about + "

" + + "

" + about.Split("" + "View " + commandcount.ToString() + " commands" + "

" + "
";