aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--docs/config.md2
-rw-r--r--src/slskd/Integrations/Scripts/ScriptService.cs21
2 files changed, 18 insertions, 5 deletions
diff --git a/docs/config.md b/docs/config.md
index 74c29b49..4ff884da 100644
--- a/docs/config.md
+++ b/docs/config.md
@@ -923,7 +923,7 @@ retention:
User-defined scripts can be configured to run when application events are raised. Scripts are given a name (useful for troubleshooting!), a list of triggering events, and a `run` command that's used to execute the script.
-The `run` command must be at least two words, with the first word identifying the executable to run and the rest of the string being the command and arguments that will be passed to the executable when the script is run. The executable needs to be on the system's PATH, otherwise it must be the fully qualified path to the file on disk.
+The `run` command must be at least one word, with the first word identifying the executable to run and the rest of the string being the command and arguments that will be passed to the executable when the script is run. The executable needs to be on the system's PATH, otherwise it must be the fully qualified path to the file on disk.
A reserved placeholder value `$DATA` is replaced with the stringified JSON data associated with the event that caused the script to run. It's the responsibility of the script to parse and handle this JSON.
diff --git a/src/slskd/Integrations/Scripts/ScriptService.cs b/src/slskd/Integrations/Scripts/ScriptService.cs
index 4e086148..48916b2d 100644
--- a/src/slskd/Integrations/Scripts/ScriptService.cs
+++ b/src/slskd/Integrations/Scripts/ScriptService.cs
@@ -67,10 +67,18 @@ public class ScriptService
try
{
- // todo: update options validation to ensure run strings have at least 2 words
var run = script.Value.Run;
- var executable = run.Split(" ", 2)[0];
- var args = run.Split(" ", 2)[1].Replace("$DATA", data.ToJson());
+
+ // split the string into at most 2 parts, leaving part [0] the executable and part [1] the rest of the string, but possibly null
+ var parts = run.Split(" ", count: 2, options: StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.RemoveEmptyEntries);
+
+ var executable = parts[0];
+ string args = default;
+
+ if (parts.Length > 1)
+ {
+ args = parts[1].Replace("$DATA", data.ToJson());
+ }
Log.Debug("Running script '{Script}': {Run}", script.Key, run);
var sw = Stopwatch.StartNew();
@@ -87,7 +95,12 @@ public class ScriptService
};
process.Start();
- process.StandardInput.WriteLine($"{args}");
+
+ if (args is not null)
+ {
+ process.StandardInput.WriteLine(args);
+ }
+
process.StandardInput.WriteLine("exit");
process.WaitForExit();