/* * plugin_hello — SCORPIOX BOT open-source starter plugin * * A single clean-room C99 binary that serves THREE modes: * 1. --meta / SX_PLUGIN_META=1 -> JSON metadata for discovery * 2. ?action=... | ?format=json -> JSON node API * 3. GET (default) -> responsive HTML starter bench * * Zero dependencies. Builds with gcc, musl-gcc -static, Apple clang and * x86_64-w64-mingw32-gcc -static. Runs as a CGI-style executable under * scorpiox-server on the node. * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #if !defined(_WIN32) # define _POSIX_C_SOURCE 200809L # define _DEFAULT_SOURCE 1 #endif #include #include #include #include #include #if defined(_WIN32) # define WIN32_LEAN_AND_MEAN # include # include # include /* MinGW/MSVC CRT starts stdout in TEXT mode, which rewrites every \n we emit * into \r\n. A CGI header block must end with exactly CRLF CRLF; text mode * turns "\r\n\r\n" into "\r\r\n\r\r\n", scorpiox-server cannot find the * header/body separator, and the entire reply (headers included) is served as * a body. Force binary stdio before main() runs — same constructor the * scorpiox-bot-api handlers use (sx_shell.h). */ __attribute__((constructor)) static void sx_plugin_binary_stdio(void) { _setmode(_fileno(stdout), _O_BINARY); _setmode(_fileno(stdin), _O_BINARY); } #else # include # include #endif #include "bot_theme_plugin.h" #define PLUGIN_SLUG "hello" #define PLUGIN_ID "plugin_hello" #define PLUGIN_VER "1.1.4" /* ------------------------------------------------------------------ */ /* Query string helpers */ /* ------------------------------------------------------------------ */ /* Percent-decode in place (handles '+' as 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])) { int hi = s[1] <= '9' ? s[1] - '0' : (tolower((unsigned char)s[1]) - 'a' + 10); int lo = s[2] <= '9' ? s[2] - '0' : (tolower((unsigned char)s[2]) - 'a' + 10); *o++ = (char)((hi << 4) | lo); s += 3; } else if (*s == '+') { *o++ = ' '; s++; } else { *o++ = *s++; } } *o = '\0'; } /* Read a single query parameter (decoded) into out. Returns 1 if found. */ static int get_query_val(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 output helpers */ /* ------------------------------------------------------------------ */ 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('"'); } /* ------------------------------------------------------------------ */ /* Node facts */ /* ------------------------------------------------------------------ */ static int node_hostname(char *buf, size_t sz) { buf[0] = '\0'; #if defined(_WIN32) { DWORD n = (DWORD)sz; if (!GetComputerNameA(buf, &n)) { strncpy(buf, "unknown", sz - 1); buf[sz - 1] = '\0'; } } #else if (gethostname(buf, sz) != 0) { strncpy(buf, "unknown", sz - 1); buf[sz - 1] = '\0'; } #endif buf[sz - 1] = '\0'; return 0; } 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(_WIN64) || defined(__x86_64__) || defined(__amd64__) return "x86_64"; #elif defined(_WIN32) || defined(__i386__) return "x86"; #elif defined(__aarch64__) || defined(__arm64__) return "arm64"; #elif defined(__arm__) return "arm"; #else return "unknown"; #endif } static const char *node_libc(void) { #if defined(_WIN32) return "native"; #elif defined(__APPLE__) return "libc"; #elif defined(__MUSL__) return "musl"; #else return "glibc"; #endif } static long node_pid(void) { #if defined(_WIN32) return (long)GetCurrentProcessId(); #else return (long)getpid(); #endif } /* ISO-8601 UTC timestamp. */ 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 (:8150) is reachable from the LAN, so a plugin must not answer * an unauthenticated caller. Accepted forms match the rest of the node API: * X-Bot-Password, Authorization: Bearer, or ?pwd=. When BOT_PASSWORD is unset * the node is in explicitly open mode and this is a no-op, exactly like * terminal_tmux.c and node_rdp.c. */ static void require_node_auth(void) { const char *np = getenv("BOT_PASSWORD"); const char *xp; const char *auth; const char *qs; char qpwd[256]; if (!np || !*np) return; xp = getenv("HTTP_X_BOT_PASSWORD"); if (xp && strcmp(xp, np) == 0) return; auth = getenv("HTTP_AUTHORIZATION"); if (auth) { while (*auth == ' ') auth++; if (strncmp(auth, "Bearer ", 7) == 0) { auth += 7; while (*auth == ' ') auth++; if (strcmp(auth, np) == 0) return; } } qs = getenv("QUERY_STRING"); if (qs && get_query_val(qs, "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); } /* ------------------------------------------------------------------ */ /* Modes */ /* ------------------------------------------------------------------ */ static int mode_meta(void) { puts("{\"name\":\"hello\",\"desc\":\"Starter demo\"}"); return 0; } static void json_header(void) { /* Deliberately no Content-Length — the server owns framing. */ 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); } static void emit_hints(void) { fputs(",\"hints\":{" "\"next_step\":\"Edit src/plugin_hello.c, then run `make` (or `make musl win64`)\"," "\"install\":\"cp plugin_hello ~/.scorpiox/plugins/ \"," "\"docs\":\"See README.md for the 3-mode contract and the fork workflow\"," "\"verify\":\"plugin_hello --meta && QUERY_STRING='action=info' plugin_hello\"" "}", stdout); } static void emit_info_body(void) { char host[256]; char ts[64]; const char *qs = getenv("QUERY_STRING"); node_hostname(host, sizeof(host)); utc_now(ts, sizeof(ts)); fputs("{\"ok\":true,\"plugin\":\"" PLUGIN_ID "\",\"slug\":\"" PLUGIN_SLUG "\"," "\"version\":\"" PLUGIN_VER "\",\"node\":{", stdout); fputs("\"hostname\":", stdout); json_str(host); fputs(",\"os\":", stdout); json_str(node_os()); fputs(",\"arch\":", stdout); json_str(node_arch()); fputs(",\"libc\":", stdout); json_str(node_libc()); printf(",\"pid\":%ld", node_pid()); fputs(",\"utc\":", stdout); json_str(ts); fputs("},\"query_string\":", stdout); json_str(qs ? qs : ""); emit_hints(); fputs("}\n", stdout); } static int mode_json(const char *qs) { char action[64]; char msg[512]; char format[32]; action[0] = '\0'; msg[0] = '\0'; format[0] = '\0'; get_query_val(qs, "action", action, sizeof(action)); get_query_val(qs, "msg", msg, sizeof(msg)); get_query_val(qs, "format", format, sizeof(format)); json_header(); if (!action[0] || strcmp(action, "info") == 0) { emit_info_body(); return 0; } if (strcmp(action, "echo") == 0) { fputs("{\"ok\":true,\"action\":\"echo\",\"msg\":", stdout); json_str(msg); printf(",\"len\":%lu}", (unsigned long)strlen(msg)); fputs("\n", stdout); return 0; } fputs("{\"ok\":false,\"error\":\"unknown action\",\"action\":", stdout); json_str(action); fputs(",\"supported\":[\"info\",\"echo\"]}\n", stdout); return 0; } /* ------------------------------------------------------------------ */ /* HTML UI */ /* ------------------------------------------------------------------ */ static int mode_html(int embed) { 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("\n" "" "hello", stdout); /* Theme tokens + prepaint come from src/bot_theme_plugin.h (the bot's own * palette), so ?theme= / the bot's postMessage land before first paint. */ fputs(SX_BOT_THEME_PREPAINT "
", stdout); if (!embed) { fputs("", stdout); } fputs("

hello

" "" PLUGIN_VER "" "connecting" "" "" "
", stdout); /* ---- live node card ---- */ fputs("

Live node info

" "
hostname
—
" "
os / arch
—
" "
libc
—
" "
pid
—
" "
utc
—
" "
loading…
", stdout); /* ---- echo tester ---- */ fputs("

Echo action tester

" "
" "
Run an echo to see the JSON response.
", stdout); /* ---- guide ---- */ fputs("

Make it yours

" "

This is a clean-room reference plugin. " "Fork it, edit one C file, rebuild in one command.

" "
    " "
  1. Clone: git clone https://plugingit.scorpiox.net/scorpiox-bot-plugin-hello.git
  2. " "
  3. Edit src/plugin_hello.c — change the metadata, add an action, restyle the UI.
  4. " "
  5. Build: make (or make musl, make win64)
  6. " "
  7. Install: cp plugin_hello ~/.scorpiox/plugins/
  8. " "
  9. Verify: plugin_hello --meta — no server restart needed.
  10. " "
" "
QUERY_STRING='action=info' ./plugin_hello\n"
          "QUERY_STRING='action=echo&msg=hi' ./plugin_hello
" "
", stdout); /* ---- script ---- */ fputs( "