summaryrefslogtreecommitdiff
path: root/daemons
diff options
context:
space:
mode:
authorRaindropsSys <raindrops@equestria.dev>2024-03-30 23:40:33 +0100
committerRaindropsSys <raindrops@equestria.dev>2024-03-30 23:40:33 +0100
commit6b796258d413f00e498ce7f80f73a9f6c061f29c (patch)
tree49e64a5dd4cde2acff7f0a93ed3f8e20e1cb2dc8 /daemons
parent5860551daa0f60103ad24e93da29f401a653f144 (diff)
downloadpluralconnect-6b796258d413f00e498ce7f80f73a9f6c061f29c.tar.gz
pluralconnect-6b796258d413f00e498ce7f80f73a9f6c061f29c.tar.bz2
pluralconnect-6b796258d413f00e498ce7f80f73a9f6c061f29c.zip
Updated 5 files, added 2 files, deleted 495 files and renamed 7 files (automated)
Diffstat (limited to 'daemons')
-rw-r--r--daemons/jobs.js110
-rw-r--r--daemons/next.php165
-rw-r--r--daemons/signal.js61
3 files changed, 336 insertions, 0 deletions
diff --git a/daemons/jobs.js b/daemons/jobs.js
new file mode 100644
index 0000000..0ca8552
--- /dev/null
+++ b/daemons/jobs.js
@@ -0,0 +1,110 @@
+const fs = require('fs');
+const child_process = require('child_process');
+
+let jobsList = [];
+let jobs = [];
+let history = require('../data/history.json');
+
+fs.writeFileSync("../data/running.json", "null");
+
+setInterval(() => {
+ if (JSON.stringify(fs.readdirSync("../data/jobs")) !== JSON.stringify(jobsList)) {
+ console.log("Updating the jobs list...");
+ jobs = fs.readdirSync("../data/jobs").map((i) => {
+ let obj = JSON.parse(fs.readFileSync("../data/jobs/" + i).toString());
+ obj["_id"] = i;
+ return obj;
+ });
+ jobsList = fs.readdirSync("../data/jobs");
+ }
+}, 1000);
+
+setTimeout(() => {
+ setInterval(() => {
+ if (jobs.length > 0) {
+ let pickup = new Date();
+
+ console.log(jobs.length + " job(s)");
+ console.log("\nRunning jobs:");
+
+ for (let job of jobs) {
+ console.log(" " + job.name + " [" + job._id + "]");
+ let output;
+ let start;
+ let end;
+
+ fs.writeFileSync("../data/running.json", JSON.stringify(job._id));
+
+ try {
+ start = new Date();
+ output = child_process.execFileSync("php", [job.name + ".php", JSON.stringify(job.options)], { cwd: "../jobs" });
+ end = new Date();
+
+ let runtime = end.getTime() - start.getTime();
+ let description = "";
+
+ for (let name of Object.keys(job.options)) {
+ description = "," + name + "=" + JSON.stringify(job.options[name]);
+ }
+
+ fs.unlinkSync("../data/jobs/" + job._id);
+
+ history.unshift({
+ completed: true,
+ error: null,
+ options: job.options,
+ name: job.name + "(" + description.substring(1) + ")",
+ output: output.toString(),
+ time: runtime,
+ tracking: {
+ queue: new Date(job.date).toISOString(),
+ pickup: pickup.toISOString(),
+ start: start.toISOString(),
+ end: end.toISOString(),
+ logged: new Date().toISOString()
+ }
+ });
+
+ history = history.slice(0, 200);
+ fs.writeFileSync("../data/history.json", JSON.stringify(history));
+ fs.writeFileSync("../data/running.json", "null");
+ } catch (e) {
+ end = start ? new Date() : null;
+ console.log(" Failed to process job");
+ console.error(e);
+
+ let runtime = end ? (start ? end.getTime() - start.getTime() : null) : null;
+ let description = "";
+
+ for (let name of Object.keys(job.options)) {
+ description = "," + name + "=" + JSON.stringify(job.options[name]);
+ }
+
+ fs.unlinkSync("../data/jobs/" + job._id);
+
+ history.unshift({
+ completed: false,
+ error: e.stack,
+ options: job.options,
+ name: job.name + "(" + description.substring(1) + ")",
+ output: (output ?? e.stdout ?? e.stderr).toString(),
+ time: runtime,
+ tracking: {
+ queue: new Date(job.date).toISOString(),
+ pickup: pickup.toISOString(),
+ start: start ? start.toISOString() : null,
+ end: end ? end.toISOString() : null,
+ logged: new Date().toISOString()
+ }
+ });
+
+ history = history.slice(0, 200);
+ fs.writeFileSync("../data/history.json", JSON.stringify(history));
+ fs.writeFileSync("../data/running.json", "null");
+ }
+ }
+
+ console.log("\nCompleted");
+ }
+ }, 1000);
+}, 500);
diff --git a/daemons/next.php b/daemons/next.php
new file mode 100644
index 0000000..c004a66
--- /dev/null
+++ b/daemons/next.php
@@ -0,0 +1,165 @@
+<?php
+
+$_SERVER['DOCUMENT_ROOT'] = "..";
+
+$ntfy = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/app.json"), true)["ntfy"];
+$pairs = [];
+
+$pairs2 = [
+ ["jybgq"],
+ ["ujcmc"],
+ ["vzyai"],
+ ["alqer"]
+];
+
+// --------------------
+
+if (!function_exists("formatPonypush")) {
+ function formatPonypush($message) {
+ return "Update to Ponypush 3.1.0 or later — (\$PA1$\$" . base64_encode($message) . "\$\$)";
+ }
+}
+
+function getLastFronted($members, $id) {
+ foreach ($members as $member) {
+ if ($member["id"] === $id) {
+ return $member["_lastFronted"];
+ }
+ }
+
+ return -1;
+}
+
+function getMemberWithoutSystem(string $id) {
+ global $isLowerLoggedIn; global $isLoggedIn;
+ $member = null;
+
+ $members2 = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/data/gdapd/members.json"), true);
+ foreach ($members2 as $m) {
+ $m["_system"] = "gdapd";
+ $m["system"] = "gdapd";
+ if ($m["id"] === $id) $member = $m;
+ }
+
+ $members3 = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/data/hrbom/members.json"), true);
+ foreach ($members3 as $m) {
+ $m["_system"] = "hrbom";
+ $m["system"] = "hrbom";
+ if ($m["id"] === $id) $member = $m;
+ }
+
+ if ($isLowerLoggedIn || $isLoggedIn) {
+ $app = $GLOBALS["ColdHazeApp"] ?? json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/app.json"), true);
+ $members4 = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/data/other/members.json"), true);
+
+ foreach ($members4 as $m) {
+ $m["_system"] = $app["other"]["id"];
+ $m["system"] = $app["other"]["id"];
+ if ($m["id"] === $id) $member = $m;
+ }
+ }
+
+ return $member;
+}
+
+$members = array_map(function ($i) {
+ $system = "gdapd";
+ $i["_lastFronted"] = -1;
+ $id = $i["id"];
+ $memberData = $i;
+
+ $fronters = array_map(function ($item) {
+ return $item["id"];
+ }, json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/data/$system/fronters.json"), true)["members"]);
+
+ if (in_array($id, $fronters)) {
+ $i["_lastFronted"] = time();
+ } else {
+ $switches = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/data/$system/switches.json"), true);
+
+ $thisMember = array_filter($switches, function ($item) use ($memberData) {
+ return in_array($memberData["id"], $item["members"]);
+ });
+
+ $thisMember = array_values($thisMember);
+ $frontingEnd = null;
+
+ if (count($thisMember) > 0) {
+ $thisIndex = array_search($thisMember[0], $switches);
+
+ $frontingStart = $thisMember[0];
+ $frontingEnd = $switches[$thisIndex - 1];
+ }
+
+ if ($frontingEnd !== null && isset($frontingStart)) {
+ $i["_lastFronted"] = strtotime($frontingEnd["timestamp"]);
+ }
+ }
+
+ return $i;
+}, json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/data/gdapd/members.json"), true));
+
+$requested = []; foreach ([...$pairs, ...$pairs2] as $pair) {
+ array_push($requested, ...$pair);
+} $requested = array_unique($requested);
+
+usort($pairs2, function ($a, $b) use ($members) {
+ $times = [];
+
+ foreach ($a as $id) {
+ if (getLastFronted($members, $id) > 0) $times[] = getLastFronted($members, $id);
+ }
+
+ $timeA = time() - min($times);
+ $times = [];
+
+ foreach ($b as $id) {
+ if (getLastFronted($members, $id) > 0) $times[] = getLastFronted($members, $id);
+ }
+
+ $timeB = time() - min($times);
+
+ if ($timeA < 5 || $timeB < 5) {
+ return -INF;
+ } else {
+ return $timeB - $timeA;
+ }
+}); $pairsM = array_values($pairs2);
+
+$requested = array_map(function ($i) { return $i[0]; }, $pairsM);
+
+$fronters = array_map(function ($i) {
+ return $i["id"];
+}, json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/data/gdapd/fronters.json"), true)["members"]);
+
+$requested = array_values(array_filter(array_unique($requested), function ($i) use ($fronters) {
+ return !in_array($i, $fronters);
+}));
+
+$i = 0;
+$list = [$requested[$i]];
+
+if (count($list) > 1) {
+ $str = implode(" or ", array_map(function ($i) {
+ return (getMemberWithoutSystem($i)["display_name"] ?? getMemberWithoutSystem($i)["name"]);
+ }, $list)) . " is fronting today.";
+} elseif (count($list) > 0) {
+ $str = (getMemberWithoutSystem($list[0])["display_name"] ?? getMemberWithoutSystem($list[0])["name"]) . " is fronting today.";
+} else {
+ $str = "We couldn't tell who will front today, sorry.";
+}
+
+$context = stream_context_create([
+ 'http' => [
+ 'method' => 'POST',
+ 'header' =>
+ "Content-Type: text/plain\r\n" .
+ "Title: " . formatPonypush("🎠 Pony fronting today") . "\r\n" .
+ "Priority: high\r\n" .
+ "Tags: schedule\r\n" .
+ "Authorization: Basic " . base64_encode($ntfy["user"] . ":" . $ntfy["password"]),
+ 'content' => formatPonypush($str)
+ ]
+]);
+
+file_get_contents('https://' . $ntfy["server"] . '/main', false, $context);
diff --git a/daemons/signal.js b/daemons/signal.js
new file mode 100644
index 0000000..4996c50
--- /dev/null
+++ b/daemons/signal.js
@@ -0,0 +1,61 @@
+const user = process.argv[2];
+const number = require('../app.json')['signal'][user];
+const fs = require('fs');
+
+if (typeof number !== "string") throw new Error("Unable to find info");
+
+const child_process = require("child_process");
+
+(async () => {
+ console.log("Ready");
+
+ let system = require('../data/' + (user === "raindrops" ? "gdapd" : (user === "Moonglow" ? "hrbom" : "other")) + "/general.json");
+ let fronters = require('../data/' + (user === "raindrops" ? "gdapd" : (user === "Moonglow" ? "hrbom" : "other")) + "/fronters.json");
+
+ console.log(system.name);
+ console.log(fronters.members.length + " member(s) at front");
+
+ let name = system.name;
+ let avatar1 = system.avatar_url;
+ let avatar2 = null;
+
+ if (fronters.members.length === 1) {
+ name = fronters.members[0].display_name ?? fronters.members[0].name;
+ avatar1 = fronters.members[0].avatar_url ?? avatar1;
+ } else if (fronters.members.length >= 2) {
+ name = (fronters.members[0].display_name ?? fronters.members[0].name) + " and " + (fronters.members[1].display_name ?? fronters.members[1].name);
+ avatar1 = fronters.members[0].avatar_url ?? avatar1;
+ avatar2 = fronters.members[1].avatar_url ?? avatar2;
+ }
+
+ let pfp;
+
+ if (avatar2) {
+ let img1 = Buffer.from(await (await fetch(avatar1)).arrayBuffer());
+ let img2 = Buffer.from(await (await fetch(avatar2)).arrayBuffer());
+
+ fs.writeFileSync("/tmp/chm-" + user + "-1", img1);
+ fs.writeFileSync("/tmp/chm-" + user + "-2", img2);
+
+ child_process.execSync("convert /tmp/chm-" + user + "-1 -gravity center -resize 512x512 -crop 50%x100% /tmp/chm-" + user + "-1.webp");
+ child_process.execSync("convert /tmp/chm-" + user + "-2 -gravity center -resize 512x512 -crop 50%x100% /tmp/chm-" + user + "-2.webp");
+ child_process.execSync("montage -mode concatenate -tile x1 /tmp/chm-" + user + "-1.webp /tmp/chm-" + user + "-2.webp /tmp/chm-" + user + ".webp");
+
+ pfp = fs.readFileSync("/tmp/chm-" + user + ".webp");
+
+ fs.unlinkSync("/tmp/chm-" + user + "-1");
+ fs.unlinkSync("/tmp/chm-" + user + "-2");
+ fs.unlinkSync("/tmp/chm-" + user + "-1.webp");
+ fs.unlinkSync("/tmp/chm-" + user + "-2.webp");
+ fs.unlinkSync("/tmp/chm-" + user + ".webp");
+ } else {
+ pfp = Buffer.from(await (await fetch(avatar1)).arrayBuffer());
+ }
+
+ fs.writeFileSync("/tmp/chm-" + user + "-pfp-final", pfp);
+
+ child_process.execFileSync("signal-cli", [ "-a", number, "updateProfile", "--given-name", name, "--family-name", "", "--avatar", "/tmp/chm-" + user + "-pfp-final" ]);
+ child_process.execFileSync("signal-cli", [ "-a", number, "updateAccount", "--device-name", "Ponycule to Signal integration (v" + new Date().toISOString().split("T")[0].replaceAll("-", ".") + ")" ]);
+ child_process.execFileSync("signal-cli", [ "-a", number, "sendSyncRequest" ]);
+ fs.unlinkSync("/tmp/chm-" + user + "-pfp-final");
+})();