From 611d4a166ba0b6e78a9d3dd36369da18059f088a Mon Sep 17 00:00:00 2001 From: AaronDewes Date: Mon, 16 May 2022 07:19:50 +0000 Subject: [PATCH] Add Karen v2 demo --- .gitignore | 1 + karen-client.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ karen-v2 | 30 +++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100755 karen-client.js create mode 100755 karen-v2 diff --git a/.gitignore b/.gitignore index 190b7c5..94b4eb5 100644 --- a/.gitignore +++ b/.gitignore @@ -52,3 +52,4 @@ db/citadel-seed/* services/installed.json services/installed.yml +events/karen diff --git a/karen-client.js b/karen-client.js new file mode 100755 index 0000000..c691213 --- /dev/null +++ b/karen-client.js @@ -0,0 +1,51 @@ +#!/usr/bin/env node + +const net = require("net"); + +let connection; + +function initConnection() { + return new Promise((resolve) => { + connection = net.createConnection("events/karen", () => resolve()); + }); +} + +async function runCommand(command) { + await initConnection(); + return new Promise((resolve, reject) => { + const errorListener = (error) => { + reject(error); + }; + connection.on("error", errorListener); + + connection.write(command, () => { + connection.off("error", errorListener); + resolve(); + }); + }); +} + +const readline = require("readline/promises"); +const { stdin, stdout } = require("process"); + +async function main() { + const rl = readline.createInterface({ input: stdin, output: stdout }); + while (true) { + const userInput = await rl.question("> "); + if (userInput === "exit" || userInput === "quit") { + rl.close(); + break; + } else if (userInput === "help") { + console.log(`Available commands: +trigger: Execute a certain trigger + +help: Print this message +quit: Exit this client +exit: Same as quit`); + } + await runCommand(userInput); + console.log("Command executed"); + } +} + +main(); diff --git a/karen-v2 b/karen-v2 new file mode 100755 index 0000000..72b41d7 --- /dev/null +++ b/karen-v2 @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +import socket +import os +import time +from collections import deque + +rootDir = os.path.dirname(os.path.abspath(__file__)) +os.chdir(rootDir) + +if os.path.exists("events/karen"): + os.remove("events/karen") + +server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) +server.bind("events/karen") + +while True: + server.listen(1) + conn, addr = server.accept() + datagram = conn.recv(1024) + if datagram: + instructions = datagram.decode("utf-8").strip().split() + cmd = instructions[0] + if(cmd == "trigger"): + trigger = instructions[1] + instructions.pop(0) + instructions.pop(0) + print("Executing trigger {} with arguments {}".format(trigger, " ".join(instructions))) + os.system("events/triggers/{} {}".format(trigger, " ".join(instructions))) + conn.close()