/*
* plugin_starter.c — SCORPIOX BOT single-binary plugin starter (C99, zero deps)
*
* Copy this file, change the three PLUGIN_* defines, edit emit_info() and the
* CSS block, build with `make`, and drop the binary into a plugin directory.
*
* The three modes one binary MUST serve:
* 1. --meta | SX_PLUGIN_META=1 -> {"name":"...","desc":"..."}
* 2. ?action=... | ?format=json -> application/json
* 3. GET (default) -> text/html, responsive dark/light
*
* Build:
* cc -O2 -std=c99 -Wall -Wextra -pedantic -o plugin_starter plugin_starter.c
* musl-gcc -O2 -std=c99 -static -o plugin_starter plugin_starter.c
* x86_64-w64-mingw32-gcc -O2 -std=c99 -static -o plugin_starter.exe plugin_starter.c
*
* Install (the whole installation is the copy):
* cp plugin_starter ~/.scorpiox/plugins/
* cp plugin_starter /workspace/.scorpiox/plugins/ (persistent on a container)
*
* MIT License. Copyright (c) 2026 SCORPIOX INC.
*/
#if !defined(_WIN32)
# define _POSIX_C_SOURCE 200809L
# define _DEFAULT_SOURCE 1
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#if defined(_WIN32)
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#else
# include <unistd.h>
#endif
/* ------------------------------------------------------------------ */
/* Identity — change these, then rename the binary */
/* ------------------------------------------------------------------ */
#define PLUGIN_SLUG "starter" /* binary name: plugin_starter */
#define PLUGIN_ID "plugin_" PLUGIN_SLUG
#define PLUGIN_VER "1.0.0"
#define PLUGIN_NAME "Starter Plugin"
#define PLUGIN_DESC "A copy-paste single-binary SCORPIOX BOT plugin"
/* ------------------------------------------------------------------ */
/* Query string */
/* ------------------------------------------------------------------ */
/* Percent-decode in place; '+' becomes a space. */
static void url_decode(char *s)
{
char *o = s;
if (!s) return;
while (*s) {
if (*s == '%' && isxdigit((unsigned char)s[1]) && isxdigit((unsigned char)s[2])) {
unsigned char c1 = (unsigned char)s[1], c2 = (unsigned char)s[2];
int hi = isdigit(c1) ? c1 - '0' : tolower(c1) - 'a' + 10;
int lo = isdigit(c2) ? c2 - '0' : tolower(c2) - 'a' + 10;
*o++ = (char)((hi << 4) | lo);
s += 3;
} else if (*s == '+') {
*o++ = ' ';
s++;
} else {
*o++ = *s++;
}
}
*o = '\0';
}
/* Read one query parameter, decoded. Returns 1 when present. */
static int qs_get(const char *qs, const char *key, char *out, size_t outsz)
{
size_t klen;
const char *p;
if (!out || outsz == 0) return 0;
out[0] = '\0';
if (!qs || !key) return 0;
klen = strlen(key);
for (p = qs; *p; ) {
if (strncmp(p, key, klen) == 0 && p[klen] == '=') {
const char *v = p + klen + 1;
size_t i = 0;
while (*v && *v != '&' && i + 1 < outsz) out[i++] = *v++;
out[i] = '\0';
url_decode(out);
return 1;
}
p = strchr(p, '&');
if (!p) break;
p++;
}
return 0;
}
/* ------------------------------------------------------------------ */
/* JSON */
/* ------------------------------------------------------------------ */
static void json_str(const char *s)
{
putchar('"');
for (; s && *s; s++) {
unsigned char c = (unsigned char)*s;
switch (c) {
case '"': fputs("\\\"", stdout); break;
case '\\': fputs("\\\\", stdout); break;
case '\n': fputs("\\n", stdout); break;
case '\r': fputs("\\r", stdout); break;
case '\t': fputs("\\t", stdout); break;
default:
if (c < 0x20) printf("\\u%04x", (unsigned)c);
else putchar((int)c);
}
}
putchar('"');
}
/* Never emit Content-Length: the server owns framing. */
static void json_header(void)
{
fputs("Content-Type: application/json; charset=utf-8\r\n"
"Cache-Control: no-store\r\n"
"X-Content-Type-Options: nosniff\r\n\r\n", stdout);
}
/* ------------------------------------------------------------------ */
/* Node facts */
/* ------------------------------------------------------------------ */
static const char *node_os(void)
{
#if defined(_WIN32)
return "windows";
#elif defined(__APPLE__)
return "macos";
#else
return "linux";
#endif
}
static const char *node_arch(void)
{
#if defined(__x86_64__) || defined(_M_X64)
return "x86_64";
#elif defined(__aarch64__) || defined(_M_ARM64)
return "arm64";
#else
return "unknown";
#endif
}
static long node_pid(void)
{
#if defined(_WIN32)
return (long)GetCurrentProcessId();
#else
return (long)getpid();
#endif
}
static void utc_now(char *buf, size_t sz)
{
time_t t = time(NULL);
struct tm tmv;
#if defined(_WIN32)
gmtime_s(&tmv, &t);
#else
gmtime_r(&t, &tmv);
#endif
if (strftime(buf, sz, "%Y-%m-%dT%H:%M:%SZ", &tmv) == 0) {
strncpy(buf, "1970-01-01T00:00:00Z", sz - 1);
buf[sz - 1] = '\0';
}
}
/* ------------------------------------------------------------------ */
/* Node auth */
/* ------------------------------------------------------------------ */
/*
* The node API is reachable from the LAN. A plugin must not answer an
* unauthenticated caller. Accepted: X-Bot-Password header, Authorization:
* Bearer, or ?pwd=. When BOT_PASSWORD is unset the node is explicitly open
* and this is a no-op, exactly like every other node route.
*/
static void require_node_auth(void)
{
const char *np = getenv("BOT_PASSWORD");
const char *h;
char qpwd[256];
if (!np || !*np) return;
h = getenv("HTTP_X_BOT_PASSWORD");
if (h && strcmp(h, np) == 0) return;
h = getenv("HTTP_AUTHORIZATION");
if (h) {
while (*h == ' ') h++;
if (strncmp(h, "Bearer ", 7) == 0) {
h += 7;
while (*h == ' ') h++;
if (strcmp(h, np) == 0) return;
}
}
if (qs_get(getenv("QUERY_STRING"), "pwd", qpwd, sizeof qpwd) &&
strcmp(qpwd, np) == 0)
return;
fputs("Status: 401 Unauthorized\r\n"
"Content-Type: application/json; charset=utf-8\r\n"
"Cache-Control: no-store\r\n"
"X-Content-Type-Options: nosniff\r\n\r\n"
"{\"ok\":false,\"error\":\"invalid node password\"}\n", stdout);
exit(0);
}
/* ------------------------------------------------------------------ */
/* Mode 1 — metadata (discovery) */
/* ------------------------------------------------------------------ */
static int mode_meta(void)
{
printf("{\"name\":\"" PLUGIN_NAME "\",\"desc\":\"" PLUGIN_DESC "\"}\n");
return 0;
}
/* ------------------------------------------------------------------ */
/* Mode 2 — JSON actions */
/* ------------------------------------------------------------------ */
static void emit_info(void)
{
char ts[64];
utc_now(ts, sizeof ts);
printf("{\"ok\":true,\"plugin\":\"" PLUGIN_ID "\",\"version\":\"" PLUGIN_VER "\","
"\"node\":{\"os\":\"%s\",\"arch\":\"%s\",\"pid\":%ld,\"utc\":\"%s\"}}\n",
node_os(), node_arch(), node_pid(), ts);
}
static int mode_json(void)
{
char action[64];
const char *qs = getenv("QUERY_STRING");
require_node_auth();
qs_get(qs, "action", action, sizeof action);
json_header();
if (!action[0] || strcmp(action, "info") == 0) {
emit_info();
return 0;
}
fputs("{\"ok\":false,\"error\":\"unknown action\",\"action\":", stdout);
json_str(action);
fputs(",\"supported\":[\"info\"]}\n", stdout);
return 0;
}
/* ------------------------------------------------------------------ */
/* Mode 3 — HTML UI */
/* ------------------------------------------------------------------ */
static int mode_html(void)
{
fputs("Content-Type: text/html; charset=utf-8\r\n"
"Cache-Control: no-store\r\n"
"X-Content-Type-Options: nosniff\r\n\r\n", stdout);
fputs("<!DOCTYPE html>\n<html lang=\"en\"><head><meta charset=\"utf-8\">"
"<meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">"
"<title>" PLUGIN_NAME "</title><style>", stdout);
fputs(":root{color-scheme:dark light;"
"--bg:#0d0d10;--surface:#16161a;--surface2:#1e1e24;--text:#f5f5f7;"
"--muted:#8e8e96;--line:rgba(255,255,255,.11);--accent:#0a84ff;"
"--mono:ui-monospace,SFMono-Regular,Menlo,monospace}"
"html[data-theme=light]{--bg:#f4f4f7;--surface:#fff;--surface2:#f0f0f4;"
"--text:#1c1c1e;--muted:#6b6b73;--line:rgba(0,0,0,.10)}"
"*{box-sizing:border-box}"
"body{margin:0;padding:16px;background:var(--bg);color:var(--text);"
"font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;"
"font-size:14px;line-height:1.5}"
".wrap{max-width:820px;margin:0 auto}"
".top{display:flex;align-items:center;gap:10px;margin-bottom:14px;flex-wrap:wrap}"
".title{font-size:20px;font-weight:700;margin:0}"
".pill{font-size:11px;font-weight:600;padding:2px 8px;border-radius:999px;"
"background:var(--surface2);color:var(--muted);border:1px solid var(--line)}"
".spacer{flex:1}"
".card{background:var(--surface);border:1px solid var(--line);border-radius:14px;"
"padding:16px;margin-bottom:14px}"
".card h2{margin:0 0 10px;font-size:12px;letter-spacing:.06em;"
"text-transform:uppercase;color:var(--muted)}"
".grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(150px,1fr));gap:10px}"
".kpi{background:var(--surface2);border:1px solid var(--line);"
"border-radius:10px;padding:10px 12px}"
".kpi .k{font-size:11px;color:var(--muted);text-transform:uppercase}"
".kpi .v{font-size:15px;font-weight:600;font-family:var(--mono);margin-top:2px;"
"word-break:break-all}"
"button{background:var(--accent);color:#fff;border:0;border-radius:999px;"
"padding:7px 16px;font-weight:600;font-size:13px;cursor:pointer}"
"pre{background:var(--surface2);border:1px solid var(--line);border-radius:10px;"
"padding:12px;overflow:auto;font-family:var(--mono);font-size:12px;"
"margin:10px 0 0;white-space:pre-wrap;word-break:break-word}"
"</style></head><body data-theme=\"dark\"><div class=\"wrap\">", stdout);
fputs("<div class=\"top\"><h1 class=\"title\">" PLUGIN_NAME "</h1>"
"<span class=\"pill\" id=\"badge\">loading</span>"
"<span class=\"pill\">" PLUGIN_ID "</span>"
"<span class=\"spacer\"></span>"
"<button type=\"button\" id=\"themeBtn\">Theme</button></div>", stdout);
fputs("<div class=\"card\"><h2>Node</h2><div class=\"grid\">"
"<div class=\"kpi\"><div class=\"k\">os</div><div class=\"v\" id=\"v-os\">—</div></div>"
"<div class=\"kpi\"><div class=\"k\">arch</div><div class=\"v\" id=\"v-arch\">—</div></div>"
"<div class=\"kpi\"><div class=\"k\">pid</div><div class=\"v\" id=\"v-pid\">—</div></div>"
"<div class=\"kpi\"><div class=\"k\">utc</div><div class=\"v\" id=\"v-utc\">—</div></div>"
"</div><pre id=\"raw\">loading…</pre></div>", stdout);
fputs("<div class=\"card\"><h2>Make it yours</h2>"
"<pre>1. edit the PLUGIN_* defines\n"
"2. edit emit_info() and the CSS block above\n"
"3. make\n"
"4. cp plugin_starter ~/.scorpiox/plugins/</pre></div>", stdout);
fputs("</div><script>\n"
"(function(){\n"
" var q = new URLSearchParams(location.search);\n"
" var pwd = q.get('pwd') || '';\n"
" var node = q.get('node') || '';\n"
" var hub = q.get('plugin') || '';\n"
" function api(action){\n"
" var u = hub ? '/plugins?plugin=' + encodeURIComponent(hub)\n"
" : '/" PLUGIN_ID "?action=' + encodeURIComponent(action);\n"
" if (hub) u += '&action=' + encodeURIComponent(action);\n"
" if (node) u += '&node=' + encodeURIComponent(node);\n"
" if (pwd) u += '&pwd=' + encodeURIComponent(pwd);\n"
" return fetch(u, {headers: pwd ? {'X-Bot-Password': pwd} : {}}).then(function(r){return r.json();});\n"
" }\n"
" function set(id, v){ var e = document.getElementById(id);\n"
" if (e) e.textContent = (v === undefined || v === null || v === '') ? '\\u2014' : v; }\n"
" api('info').then(function(d){\n"
" var n = d.node || {};\n"
" set('v-os', n.os); set('v-arch', n.arch); set('v-pid', n.pid); set('v-utc', n.utc);\n"
" document.getElementById('badge').textContent = 'live';\n"
" document.getElementById('raw').textContent = JSON.stringify(d, null, 2);\n"
" }).catch(function(e){\n"
" document.getElementById('badge').textContent = 'offline';\n"
" document.getElementById('raw').textContent = 'error: ' + e;\n"
" });\n"
" var saved = localStorage.getItem('bot_theme') || 'dark';\n"
" document.body.setAttribute('data-theme', saved);\n"
" document.getElementById('themeBtn').addEventListener('click', function(){\n"
" var next = document.body.getAttribute('data-theme') === 'light' ? 'dark' : 'light';\n"
" document.body.setAttribute('data-theme', next);\n"
" localStorage.setItem('bot_theme', next);\n"
" });\n"
"})();\n"
"</script></body></html>\n", stdout);
return 0;
}
/* ------------------------------------------------------------------ */
/* Dispatch */
/* ------------------------------------------------------------------ */
int main(int argc, char **argv)
{
const char *qs = getenv("QUERY_STRING");
char format[24], action[64];
if ((argc > 1 && strcmp(argv[1], "--meta") == 0) || getenv("SX_PLUGIN_META"))
return mode_meta();
qs_get(qs, "format", format, sizeof format);
qs_get(qs, "action", action, sizeof action);
if (strcmp(format, "json") == 0 || action[0]) return mode_json();
return mode_html();
}