diff --git a/api.lsl b/api.lsl index 4638ab7..061defe 100644 --- a/api.lsl +++ b/api.lsl @@ -1,11 +1,28 @@ integer connected = 0; key SECRET_KEY = "29731e5170353a8b235098c43cd2099a4e805c55fb4395890e81f437c17334a9"; +list commands = []; +key request_id; default { state_entry() { llRequestSecureURL(); + llMessageLinked(LINK_SET, 0, "", "request_command_info"); + } + + link_message(integer link, integer num, string msg, key id) + { + if(id == "command_info") + { + commands += llParseString2List(msg, ["|"], []); + } + else if(id == request_id && num == 1) + { + // TODO Properly escape and serialize to JSON + llHTTPResponse(id, 200, "{\"result\": \"" + msg + "\"}"); + request_id = ""; + } } http_request(key id, string method, string body) @@ -30,9 +47,16 @@ default llHTTPResponse(id, 200, "{\"result\": \"disconnected\"}"); connected = 0; } + else if(llJsonGetValue(body, ["command"]) == "get_commands") + { + llHTTPResponse(id, 200, "{\"available_commands\": " + \ + llList2Json(JSON_OBJECT, commands) + "}"); + } else { - llHTTPResponse(id, 200, "{\"result\": \"success\"}"); + // Relay the message to other scripts, we handle the response later + request_id = id; + llMessageLinked(LINK_SET, 0, llJsonGetValue(body, ["command"]), id); } } else diff --git a/lslsh.py b/lslsh.py index 7a7a8c0..83c1c0e 100755 --- a/lslsh.py +++ b/lslsh.py @@ -67,6 +67,10 @@ class Shell(cmd.Cmd): print("Error: Invalid response") return + available_commands = self.send_cmd(url, "get_commands") + for key, value in available_commands.get("available_commands").items(): + self.add_cmd(key, value) + print(f"Connected to {uuid}") print( "_______________________________________________________________________________" @@ -99,8 +103,8 @@ class Shell(cmd.Cmd): """Make a new command available within the shell.""" def do_cmd(arg): - result = self.send_cmd(self.url, arg) - print(result) + result = self.send_cmd(self.url, f"{do_cmd.__name__} {arg}") + print(result.get("result")) do_cmd.__doc__ = help_text do_cmd.__name__ = name diff --git a/modules/echo.lsl b/modules/echo.lsl new file mode 100644 index 0000000..08f9950 --- /dev/null +++ b/modules/echo.lsl @@ -0,0 +1,19 @@ +default +{ + link_message(integer sender, integer num, string msg, key id) + { + list params = llParseString2List(msg, [" "], [""]); + string param0 = llList2String(params, 0); + string param1 = llList2String(params, 1); + + if(id == "request_command_info") + { + llMessageLinked(LINK_SET, 0, "echo|echo: echo [arg ...]", "command_info"); + } + else if(param0 == "echo") + { + string response = llDumpList2String(llDeleteSubList(params, 0, 0), " "); + llMessageLinked(LINK_SET, 1, response, id); + } + } +} diff --git a/todo.md b/todo.md index 9e8dee5..134f490 100644 --- a/todo.md +++ b/todo.md @@ -6,3 +6,5 @@ - [ ] Add non-interactive mode to allow use in other scripts - [ ] Add verbose mode to see all communication - [x] Add command history +- [x] Make lsl API script discover modules +- [x] Make lsl API script communicate with modules