From 13a315c9e858255ce773a4501c0c6c223e6f21f5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 16 Feb 2025 17:37:32 -0600 Subject: simplify webhook validation --- src/slskd/Core/Options.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'src/slskd/Core') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index ff9cc407..9f539311 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -2107,14 +2107,10 @@ namespace slskd public IEnumerable Validate(ValidationContext validationContext) { - var results = new List(); - if (!Url.StartsWith("http://", StringComparison.OrdinalIgnoreCase) && !Url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) { - results.Add(new ValidationResult($"The {nameof(Url)} field must contain a fully qualified URL, including protocol (e.g. http:// or https://)")); + yield return new ValidationResult($"The {nameof(Url)} field must contain a fully qualified URL, including protocol (e.g. http:// or https://)"); } - - return results; } } -- cgit v1.2.3 From 94d46b3eb94c3f846b84afe9e58c651b8c63616d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 16 Feb 2025 17:37:53 -0600 Subject: split script Run into Executable and Command, ensure at least one specified --- src/slskd/Core/Options.cs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'src/slskd/Core') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 9f539311..183fcbe5 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -2157,9 +2157,31 @@ namespace slskd /// /// Gets the shell script to invoke. /// - [StringLength(int.MaxValue, MinimumLength = 1)] - [NotNullOrWhiteSpace] - public string Run { get; init; } + public ScriptRunOptions Run { get; init; } = new ScriptRunOptions(); + } + + /// + /// Script run options. + /// + public class ScriptRunOptions : IValidatableObject + { + /// + /// Gets the executable to start. + /// + public string Executable { get; init; } + + /// + /// Gets the command to run. + /// + public string Command { get; init; } + + public IEnumerable Validate(ValidationContext validationContext) + { + if (string.IsNullOrWhiteSpace(Executable) && string.IsNullOrWhiteSpace(Command)) + { + yield return new ValidationResult($"At least one of the {nameof(Executable)} and {nameof(Command)} fields must be specified"); + } + } } /// -- cgit v1.2.3 From c21b459f7dcc9c55260e5b3036a64083eeced140 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 17 Feb 2025 14:15:12 -0600 Subject: document options --- src/slskd/Core/Options.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/slskd/Core') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 183fcbe5..6f8e5682 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -2165,21 +2165,29 @@ namespace slskd /// public class ScriptRunOptions : IValidatableObject { + /// + /// Gets the shell command to run. + /// + public string Command { get; init; } + /// /// Gets the executable to start. /// public string Executable { get; init; } /// - /// Gets the command to run. + /// Gets the arguments to pass to the executable. /// - public string Command { get; init; } + public string Args { get; init; } public IEnumerable Validate(ValidationContext validationContext) { - if (string.IsNullOrWhiteSpace(Executable) && string.IsNullOrWhiteSpace(Command)) + var cmdIsSet = !string.IsNullOrWhiteSpace(Command); + var exeIsSet = !string.IsNullOrWhiteSpace(Executable); + + if ((cmdIsSet && exeIsSet) || (!cmdIsSet && !exeIsSet)) { - yield return new ValidationResult($"At least one of the {nameof(Executable)} and {nameof(Command)} fields must be specified"); + yield return new ValidationResult($"One and only one of the fields {nameof(Command)} or {nameof(Executable)} may be specified for a single script"); } } } -- cgit v1.2.3 From 8c443b2fe6240c8f666f4781b1b21579ff8523b6 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 23 Feb 2025 16:46:17 -0600 Subject: add ArgsList --- src/slskd/Core/Options.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/slskd/Core') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 6f8e5682..440cd8a4 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -2178,8 +2178,19 @@ namespace slskd /// /// Gets the arguments to pass to the executable. /// + /// + /// Mutually exclusive with . + /// public string Args { get; init; } + /// + /// Gets the list of arguments to pass to the executable. + /// + /// + /// Mutually exclusive with . + /// + public string[] ArgsList { get; init; } = null; + public IEnumerable Validate(ValidationContext validationContext) { var cmdIsSet = !string.IsNullOrWhiteSpace(Command); @@ -2189,6 +2200,14 @@ namespace slskd { yield return new ValidationResult($"One and only one of the fields {nameof(Command)} or {nameof(Executable)} may be specified for a single script"); } + + var argsIsSet = !string.IsNullOrWhiteSpace(Args); + var argsListIsSet = ArgsList is not null; + + if (argsIsSet && argsListIsSet) + { + yield return new ValidationResult($"Only one of the fields {nameof(Args)} or {nameof(ArgsList)} may be specified for a single script"); + } } } -- cgit v1.2.3 From f435c1e2e58c16cf33098bab4f946bb7fa0c30be Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 23 Feb 2025 17:00:01 -0600 Subject: adjust validation messages --- src/slskd/Core/Options.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/slskd/Core') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 440cd8a4..5c9c756c 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -2198,7 +2198,7 @@ namespace slskd if ((cmdIsSet && exeIsSet) || (!cmdIsSet && !exeIsSet)) { - yield return new ValidationResult($"One and only one of the fields {nameof(Command)} or {nameof(Executable)} may be specified for a single script"); + yield return new ValidationResult($"One and only one of the fields {nameof(Command)} or {nameof(Executable)} may be specified for a single script. If you intend to use the system shell, omit 'executable'. If you intend to use an executable other than the system shell, omit 'command' and specify either 'args' or 'args_list'."); } var argsIsSet = !string.IsNullOrWhiteSpace(Args); @@ -2206,7 +2206,7 @@ namespace slskd if (argsIsSet && argsListIsSet) { - yield return new ValidationResult($"Only one of the fields {nameof(Args)} or {nameof(ArgsList)} may be specified for a single script"); + yield return new ValidationResult($"Only one of the fields {nameof(Args)} or {nameof(ArgsList)} may be specified for a single script. Specify 'args' if you intend to construct a single quoted string yourself, and specify 'args_list' if you'd like slskd to handle quoting for you."); } } } -- cgit v1.2.3 From d56505cb9528ef5b950c9a2e0ed99555ed23e201 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 1 Mar 2025 18:10:55 -0600 Subject: ArgsList -> Arglist --- src/slskd/Core/Options.cs | 8 ++++---- src/slskd/Integrations/Scripts/ScriptService.cs | 9 +++++---- 2 files changed, 9 insertions(+), 8 deletions(-) (limited to 'src/slskd/Core') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 5c9c756c..266d04aa 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -2179,7 +2179,7 @@ namespace slskd /// Gets the arguments to pass to the executable. /// /// - /// Mutually exclusive with . + /// Mutually exclusive with . /// public string Args { get; init; } @@ -2189,7 +2189,7 @@ namespace slskd /// /// Mutually exclusive with . /// - public string[] ArgsList { get; init; } = null; + public string[] Arglist { get; init; } = null; public IEnumerable Validate(ValidationContext validationContext) { @@ -2202,11 +2202,11 @@ namespace slskd } var argsIsSet = !string.IsNullOrWhiteSpace(Args); - var argsListIsSet = ArgsList is not null; + var argsListIsSet = Arglist is not null; if (argsIsSet && argsListIsSet) { - yield return new ValidationResult($"Only one of the fields {nameof(Args)} or {nameof(ArgsList)} may be specified for a single script. Specify 'args' if you intend to construct a single quoted string yourself, and specify 'args_list' if you'd like slskd to handle quoting for you."); + yield return new ValidationResult($"Only one of the fields {nameof(Args)} or {nameof(Arglist)} may be specified for a single script. Specify 'args' if you intend to construct a single quoted string yourself, and specify 'args_list' if you'd like slskd to handle quoting for you."); } } } diff --git a/src/slskd/Integrations/Scripts/ScriptService.cs b/src/slskd/Integrations/Scripts/ScriptService.cs index 3a05df04..dacead33 100644 --- a/src/slskd/Integrations/Scripts/ScriptService.cs +++ b/src/slskd/Integrations/Scripts/ScriptService.cs @@ -121,7 +121,7 @@ public class ScriptService }, }; } - else if (run.ArgsList is not null) + else if (run.Arglist is not null) { // 'args list' mode takes precedence over 'args' mode // the supplied list of args will be passed to the constructor of ProcessStartInfo, which is @@ -134,7 +134,7 @@ public class ScriptService { StartInfo = new ProcessStartInfo( fileName: executable, - arguments: run.ArgsList) + arguments: run.Arglist) { WorkingDirectory = Program.ScriptDirectory, UseShellExecute = false, @@ -164,7 +164,7 @@ public class ScriptService }; } - Log.Debug("Running script '{Script}': \"{Executable}\" {Args} (id: {ProcessId})", script.Key, executable, run.Args ?? string.Join(' ', run.ArgsList ?? []), processId); + Log.Debug("Running script '{Script}': \"{Executable}\" {Args} (id: {ProcessId})", script.Key, executable, run.Args ?? string.Join(' ', run.Arglist ?? []), processId); var sw = Stopwatch.StartNew(); process.StartInfo.EnvironmentVariables["SLSKD_SCRIPT_DATA"] = data.ToJson(); @@ -187,7 +187,8 @@ public class ScriptService } catch (Exception ex) { - Log.Warning(ex, "Failed to run script '{Script}' for event type {Event}: {Message} (id: {ProcessId})", script.Key, data.Type, ex.Message, processId); + Log.Warning("Failed to run script '{Script}' for event type {Event}: {Message} (id: {ProcessId})", script.Key, data.Type, ex.Message, processId); + Log.Debug(ex, "Failed to run script '{Script}' for event type {Event}: {Message} (id: {ProcessId})", script.Key, data.Type, ex.Message, processId); } finally { -- cgit v1.2.3