| 1 | /* |
| 2 | * plugin_time.c - entry point and HTTP front end for the SCORPIOX BOT world |
| 3 | * time connector. |
| 4 | * |
| 5 | * One binary, five modes, exactly as the plugin_<name> spec requires: |
| 6 | * |
| 7 | * 1. --meta / SX_PLUGIN_META=1 -> one line of discovery JSON, exit 0. |
| 8 | * Unauthenticated on purpose: /plugins probes it, and the |
| 9 | * sx-connector-mcp router probes it before it will run anything else. |
| 10 | * 2. --mcp -> MCP JSON-RPC 2.0 over stdio, one request |
| 11 | * per line, one response per line. Through the single sx-connector-mcp |
| 12 | * router this is what makes the plugin a tool provider for SCORPIOX CODE |
| 13 | * with no clang change and no extra MCP server slot. |
| 14 | * 3. --schema -> the tools/list array, for a human. |
| 15 | * 4. ?action=... | ?format=json | POST -> the JSON API the page talks to. |
| 16 | * Same three tools as --mcp, so the page can never drift from the agent. |
| 17 | * 5. GET (default) -> the World Clock & Timezone Converter page. |
| 18 | * |
| 19 | * Modes 4 and 5 sit behind require_node_auth(): a node's :8150 is reachable |
| 20 | * from the LAN. --meta and --mcp stay open so discovery and the router work; |
| 21 | * neither answers a web request. |
| 22 | * |
| 23 | * MIT License. Copyright (c) 2026 SCORPIOX INC. |
| 24 | */ |
| 25 | |
| 26 | #if defined(_WIN32) && !defined(_WIN32_WINNT) |
| 27 | # define _WIN32_WINNT 0x0601 |
| 28 | #endif |
| 29 | |
| 30 | #if !defined(_WIN32) |
| 31 | # define _POSIX_C_SOURCE 200809L |
| 32 | # define _DEFAULT_SOURCE 1 |
| 33 | # if defined(__APPLE__) |
| 34 | # define _DARWIN_C_SOURCE 1 |
| 35 | # endif |
| 36 | #endif |
| 37 | |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <ctype.h> |
| 42 | |
| 43 | #if defined(_WIN32) |
| 44 | # define WIN32_LEAN_AND_MEAN |
| 45 | # include <windows.h> |
| 46 | # include <io.h> |
| 47 | # include <fcntl.h> |
| 48 | /* MinGW's CRT starts stdout in TEXT mode and rewrites every \n into \r\n. A |
| 49 | * CGI header block must end with exactly CRLF CRLF; text mode turns that into |
| 50 | * \r\r\n\r\r\n, scorpiox-server never finds the separator, and the whole reply |
| 51 | * (headers included) is served as a body. Force binary stdio before main(). */ |
| 52 | __attribute__((constructor)) |
| 53 | static void sx_plugin_binary_stdio(void) |
| 54 | { |
| 55 | _setmode(_fileno(stdout), _O_BINARY); |
| 56 | _setmode(_fileno(stdin), _O_BINARY); |
| 57 | } |
| 58 | #else |
| 59 | # include <unistd.h> |
| 60 | #endif |
| 61 | |
| 62 | #include "plugintime.h" |
| 63 | #include "tz.h" |
| 64 | #include "bot_theme_plugin.h" |
| 65 | #include "ui_html.h" |
| 66 | |
| 67 | /* ------------------------------------------------------------------ */ |
| 68 | /* --meta */ |
| 69 | /* ------------------------------------------------------------------ */ |
| 70 | |
| 71 | static int mode_meta(void) |
| 72 | { |
| 73 | printf("{\"name\":\"%s\",\"desc\":\"%s\",\"kind\":\"mcp\",\"mcp\":true," |
| 74 | "\"daemon\":false,\"menus\":false,\"verified\":\"live\"," |
| 75 | "\"category\":\"utilities\",\"version\":\"%s\"}\n", |
| 76 | PLUGIN_NAME, PLUGIN_DESC, PLUGIN_VER); |
| 77 | fflush(stdout); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | /* ------------------------------------------------------------------ */ |
| 82 | /* MCP over stdio */ |
| 83 | /* ------------------------------------------------------------------ */ |
| 84 | |
| 85 | static void send_raw(const char *json) |
| 86 | { |
| 87 | fputs(json, stdout); |
| 88 | fputc('\n', stdout); |
| 89 | fflush(stdout); |
| 90 | } |
| 91 | |
| 92 | static void send_result_raw(const char *id_json, const char *result_json) |
| 93 | { |
| 94 | buf_t b; |
| 95 | buf_init(&b); |
| 96 | buf_puts(&b, "{\"jsonrpc\":\"2.0\",\"id\":"); |
| 97 | buf_puts(&b, id_json ? id_json : "null"); |
| 98 | buf_puts(&b, ",\"result\":"); |
| 99 | buf_puts(&b, result_json ? result_json : "{}"); |
| 100 | buf_puts(&b, "}"); |
| 101 | send_raw(buf_str(&b)); |
| 102 | buf_free(&b); |
| 103 | } |
| 104 | |
| 105 | static void send_result(yyjson_val *id, const char *result_json) |
| 106 | { |
| 107 | char *js = id ? yyjson_val_write(id, 0, NULL) : NULL; |
| 108 | send_result_raw(js ? js : "null", result_json); |
| 109 | free(js); |
| 110 | } |
| 111 | |
| 112 | static void send_error(yyjson_val *id, int code, const char *msg) |
| 113 | { |
| 114 | buf_t b; |
| 115 | char *js = id ? yyjson_val_write(id, 0, NULL) : NULL; |
| 116 | |
| 117 | buf_init(&b); |
| 118 | buf_puts(&b, "{\"jsonrpc\":\"2.0\",\"id\":"); |
| 119 | /* A parse error has no id to echo; the spec says answer with null, and a |
| 120 | * silent drop makes a broken client look like a hung server. */ |
| 121 | buf_puts(&b, js ? js : "null"); |
| 122 | buf_printf(&b, ",\"error\":{\"code\":%d,\"message\":", code); |
| 123 | json_str(&b, msg); |
| 124 | buf_puts(&b, "}}"); |
| 125 | send_raw(buf_str(&b)); |
| 126 | buf_free(&b); |
| 127 | free(js); |
| 128 | } |
| 129 | |
| 130 | static void handle_request(yyjson_val *root, yyjson_val *id, const char *method) |
| 131 | { |
| 132 | if (strcmp(method, "initialize") == 0) { |
| 133 | buf_t b; |
| 134 | char *local = NULL; |
| 135 | buf_init(&b); |
| 136 | buf_puts(&b, "{\"protocolVersion\":\"" MCP_PROTOCOL "\"," |
| 137 | "\"capabilities\":{\"tools\":{\"listChanged\":false}}," |
| 138 | "\"serverInfo\":{\"name\":\"" SERVER_NAME "\",\"version\":\""); |
| 139 | buf_puts(&b, PLUGIN_VER); |
| 140 | buf_puts(&b, "\"},\"instructions\":"); |
| 141 | { |
| 142 | buf_t d; |
| 143 | buf_init(&d); |
| 144 | buf_puts(&d, "IANA timezone queries and wall-clock conversion for " |
| 145 | "three tools: get_current_time, convert_time and " |
| 146 | "get_time_ago. Times are resolved from an embedded tz " |
| 147 | "database, so every platform answers identically. The " |
| 148 | "server's local timezone is "); |
| 149 | buf_puts(&d, tz_local_name()); |
| 150 | buf_puts(&d, "."); |
| 151 | json_str(&b, buf_str(&d)); |
| 152 | buf_free(&d); |
| 153 | } |
| 154 | buf_puts(&b, "}"); |
| 155 | send_result(id, buf_str(&b)); |
| 156 | buf_free(&b); |
| 157 | free(local); |
| 158 | return; |
| 159 | } |
| 160 | if (strcmp(method, "ping") == 0) { |
| 161 | send_result(id, "{}"); |
| 162 | return; |
| 163 | } |
| 164 | if (strcmp(method, "tools/list") == 0) { |
| 165 | char *tools = tools_list_json(); |
| 166 | buf_t b; |
| 167 | buf_init(&b); |
| 168 | buf_puts(&b, "{\"tools\":"); |
| 169 | buf_puts(&b, tools ? tools : "[]"); |
| 170 | buf_puts(&b, "}"); |
| 171 | send_result(id, buf_str(&b)); |
| 172 | buf_free(&b); |
| 173 | free(tools); |
| 174 | return; |
| 175 | } |
| 176 | if (strcmp(method, "tools/call") == 0) { |
| 177 | yyjson_val *params = yyjson_obj_get(root, "params"); |
| 178 | const char *name = NULL; |
| 179 | yyjson_val *args = NULL; |
| 180 | tool_out_t t; |
| 181 | char *res; |
| 182 | |
| 183 | if (params && yyjson_is_obj(params)) { |
| 184 | yyjson_val *nv = yyjson_obj_get(params, "name"); |
| 185 | if (nv && yyjson_is_str(nv)) name = yyjson_get_str(nv); |
| 186 | args = yyjson_obj_get(params, "arguments"); |
| 187 | if (args && !yyjson_is_obj(args)) args = NULL; |
| 188 | } |
| 189 | if (!name) { |
| 190 | send_error(id, -32602, "tools/call needs params.name"); |
| 191 | return; |
| 192 | } |
| 193 | if (tool_dispatch(name, args, &t) != 0) { |
| 194 | buf_t b; |
| 195 | buf_init(&b); |
| 196 | buf_puts(&b, "Unknown tool: "); |
| 197 | buf_puts(&b, name); |
| 198 | send_error(id, -32602, buf_str(&b)); |
| 199 | buf_free(&b); |
| 200 | tool_free(&t); |
| 201 | return; |
| 202 | } |
| 203 | res = tool_mcp_result(&t); |
| 204 | send_result(id, res ? res : "{\"content\":[],\"isError\":true}"); |
| 205 | free(res); |
| 206 | tool_free(&t); |
| 207 | return; |
| 208 | } |
| 209 | if (strcmp(method, "resources/list") == 0) { |
| 210 | send_result(id, "{\"resources\":[]}"); |
| 211 | return; |
| 212 | } |
| 213 | if (strcmp(method, "resources/templates/list") == 0) { |
| 214 | send_result(id, "{\"resourceTemplates\":[]}"); |
| 215 | return; |
| 216 | } |
| 217 | if (strcmp(method, "prompts/list") == 0) { |
| 218 | send_result(id, "{\"prompts\":[]}"); |
| 219 | return; |
| 220 | } |
| 221 | send_error(id, -32601, "Method not found"); |
| 222 | } |
| 223 | |
| 224 | static int mode_mcp(void) |
| 225 | { |
| 226 | char *line = (char *)malloc((size_t)STDIN_LINE_MAX); |
| 227 | if (!line) return 1; |
| 228 | |
| 229 | for (;;) { |
| 230 | size_t n = 0; |
| 231 | int c; |
| 232 | |
| 233 | /* Read one line by hand: fgets with a fixed buffer would truncate a |
| 234 | * long arguments object, and a truncated line parses as valid-but- |
| 235 | * wrong JSON instead of failing. */ |
| 236 | for (;;) { |
| 237 | c = fgetc(stdin); |
| 238 | if (c == EOF) break; |
| 239 | if (c == '\n') break; |
| 240 | if (n + 1 < (size_t)STDIN_LINE_MAX) line[n++] = (char)c; |
| 241 | } |
| 242 | line[n] = '\0'; |
| 243 | if (n == 0) { |
| 244 | if (c == EOF) { free(line); return 0; } |
| 245 | continue; |
| 246 | } |
| 247 | { |
| 248 | yyjson_doc *doc = yyjson_read(line, n, 0); |
| 249 | yyjson_val *root, *mv, *id; |
| 250 | const char *method; |
| 251 | |
| 252 | if (!doc) { |
| 253 | send_error(NULL, -32700, "Parse error: not valid JSON"); |
| 254 | continue; |
| 255 | } |
| 256 | root = yyjson_doc_get_root(doc); |
| 257 | if (!root || !yyjson_is_obj(root)) { |
| 258 | yyjson_doc_free(doc); |
| 259 | send_error(NULL, -32600, "Invalid request: expected an object"); |
| 260 | continue; |
| 261 | } |
| 262 | mv = yyjson_obj_get(root, "method"); |
| 263 | method = (mv && yyjson_is_str(mv)) ? yyjson_get_str(mv) : NULL; |
| 264 | id = yyjson_obj_get(root, "id"); |
| 265 | |
| 266 | if (!method) { |
| 267 | yyjson_doc_free(doc); |
| 268 | if (yyjson_obj_get(root, "result") || |
| 269 | yyjson_obj_get(root, "error")) continue; |
| 270 | send_error(id, -32600, "Invalid request: missing method"); |
| 271 | continue; |
| 272 | } |
| 273 | if (!id) { /* notification: no reply */ |
| 274 | yyjson_doc_free(doc); |
| 275 | continue; |
| 276 | } |
| 277 | handle_request(root, id, method); |
| 278 | yyjson_doc_free(doc); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /* ------------------------------------------------------------------ */ |
| 284 | /* Query parameters */ |
| 285 | /* ------------------------------------------------------------------ */ |
| 286 | |
| 287 | static int get_query_val(const char *qs, const char *key, char *out, size_t outsz) |
| 288 | { |
| 289 | size_t klen; |
| 290 | |
| 291 | if (!qs || !key || !out || outsz == 0) return 0; |
| 292 | out[0] = '\0'; |
| 293 | klen = strlen(key); |
| 294 | while (*qs) { |
| 295 | if (strncmp(qs, key, klen) == 0 && qs[klen] == '=') { |
| 296 | size_t i = 0; |
| 297 | qs += klen + 1; |
| 298 | while (*qs && *qs != '&' && i + 1 < outsz) { |
| 299 | /* %XX and + decode: a timezone name never needs it, but an |
| 300 | * `America/Argentina/Buenos_Aires` over the wire does not, and |
| 301 | * a page passing `time=09:30` from a form does. */ |
| 302 | if (*qs == '%' && isxdigit((unsigned char)qs[1]) && |
| 303 | isxdigit((unsigned char)qs[2])) { |
| 304 | int hi = isdigit((unsigned char)qs[1]) ? qs[1] - '0' |
| 305 | : (tolower((unsigned char)qs[1]) - 'a' + 10); |
| 306 | int lo = isdigit((unsigned char)qs[2]) ? qs[2] - '0' |
| 307 | : (tolower((unsigned char)qs[2]) - 'a' + 10); |
| 308 | out[i++] = (char)((hi << 4) | lo); |
| 309 | qs += 3; |
| 310 | continue; |
| 311 | } |
| 312 | out[i++] = (*qs == '+') ? ' ' : *qs; |
| 313 | qs++; |
| 314 | } |
| 315 | out[i] = '\0'; |
| 316 | return 1; |
| 317 | } |
| 318 | qs = strchr(qs, '&'); |
| 319 | if (!qs) break; |
| 320 | qs++; |
| 321 | } |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | /* ------------------------------------------------------------------ */ |
| 326 | /* Node authentication */ |
| 327 | /* */ |
| 328 | /* The node's :8150 is LAN-reachable, so the page and the JSON API must */ |
| 329 | /* not answer a bare curl. Three credential forms because three callers */ |
| 330 | /* differ: bot-web proxies with a header, the plugin iframe appends */ |
| 331 | /* ?pwd=, and a script may send a bearer token. --meta and --mcp return */ |
| 332 | /* above this, so discovery and the router stay open. */ |
| 333 | /* ------------------------------------------------------------------ */ |
| 334 | |
| 335 | static void require_node_auth(void) |
| 336 | { |
| 337 | const char *np = getenv("BOT_PASSWORD"); |
| 338 | const char *xp, *auth, *qs; |
| 339 | char qpwd[256]; |
| 340 | |
| 341 | if (!np || !*np) return; |
| 342 | |
| 343 | xp = getenv("HTTP_X_BOT_PASSWORD"); |
| 344 | if (xp && strcmp(xp, np) == 0) return; |
| 345 | |
| 346 | auth = getenv("HTTP_AUTHORIZATION"); |
| 347 | if (auth) { |
| 348 | while (*auth == ' ') auth++; |
| 349 | if (strncmp(auth, "Bearer ", 7) == 0) { |
| 350 | auth += 7; |
| 351 | while (*auth == ' ') auth++; |
| 352 | if (strcmp(auth, np) == 0) return; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | qs = getenv("QUERY_STRING"); |
| 357 | if (qs && get_query_val(qs, "pwd", qpwd, sizeof qpwd) && |
| 358 | strcmp(qpwd, np) == 0) return; |
| 359 | |
| 360 | fputs("Status: 401 Unauthorized\r\n" |
| 361 | "Content-Type: application/json; charset=utf-8\r\n" |
| 362 | "Cache-Control: no-store\r\n" |
| 363 | "X-Content-Type-Options: nosniff\r\n\r\n" |
| 364 | "{\"ok\":false,\"error\":\"invalid node password\"}\n", stdout); |
| 365 | exit(0); |
| 366 | } |
| 367 | |
| 368 | /* ------------------------------------------------------------------ */ |
| 369 | /* Responses */ |
| 370 | /* ------------------------------------------------------------------ */ |
| 371 | |
| 372 | static void json_header(void) |
| 373 | { |
| 374 | fputs("Content-Type: application/json; charset=utf-8\r\n" |
| 375 | "Cache-Control: no-store\r\n" |
| 376 | "X-Content-Type-Options: nosniff\r\n\r\n", stdout); |
| 377 | } |
| 378 | |
| 379 | static void json_raw(const char *json) |
| 380 | { |
| 381 | json_header(); |
| 382 | fputs(json ? json : "{\"ok\":true}", stdout); |
| 383 | fputc('\n', stdout); |
| 384 | fflush(stdout); |
| 385 | } |
| 386 | |
| 387 | static void json_err(int code, const char *msg) |
| 388 | { |
| 389 | buf_t b; |
| 390 | const char *status = "Status: 500 Internal Server Error\r\n"; |
| 391 | |
| 392 | if (code == 400) status = "Status: 400 Bad Request\r\n"; |
| 393 | else if (code == 404) status = "Status: 404 Not Found\r\n"; |
| 394 | else if (code == 413) status = "Status: 413 Payload Too Large\r\n"; |
| 395 | |
| 396 | buf_init(&b); |
| 397 | buf_puts(&b, status); |
| 398 | buf_puts(&b, "Content-Type: application/json; charset=utf-8\r\n" |
| 399 | "Cache-Control: no-store\r\n" |
| 400 | "X-Content-Type-Options: nosniff\r\n\r\n"); |
| 401 | buf_puts(&b, "{\"ok\":false,\"error\":"); |
| 402 | json_str(&b, msg ? msg : "error"); |
| 403 | buf_puts(&b, "}\n"); |
| 404 | fputs(buf_str(&b), stdout); |
| 405 | fflush(stdout); |
| 406 | buf_free(&b); |
| 407 | } |
| 408 | |
| 409 | /* Reads the request body when CONTENT_LENGTH says there is one. Never blocks |
| 410 | * on an inherited empty stdin. */ |
| 411 | static char g_body[REQ_BODY_MAX]; |
| 412 | static yyjson_doc *g_body_doc = NULL; |
| 413 | |
| 414 | static int read_body(void) |
| 415 | { |
| 416 | const char *cl = getenv("CONTENT_LENGTH"); |
| 417 | long len; |
| 418 | size_t got; |
| 419 | |
| 420 | g_body[0] = '\0'; |
| 421 | if (!cl || !*cl) return 0; |
| 422 | len = strtol(cl, NULL, 10); |
| 423 | if (len <= 0) return 0; |
| 424 | if ((size_t)len >= sizeof g_body) return -1; |
| 425 | got = fread(g_body, 1, (size_t)len, stdin); |
| 426 | g_body[got] = '\0'; |
| 427 | return (int)got; |
| 428 | } |
| 429 | |
| 430 | static yyjson_val *body_root(void) |
| 431 | { |
| 432 | if (!g_body_doc) { |
| 433 | if (!g_body[0]) return NULL; |
| 434 | g_body_doc = yyjson_read(g_body, strlen(g_body), 0); |
| 435 | if (!g_body_doc) return NULL; |
| 436 | } |
| 437 | return yyjson_doc_get_root(g_body_doc); |
| 438 | } |
| 439 | |
| 440 | static int param_str(const char *qs, const char *key, char *out, size_t outsz) |
| 441 | { |
| 442 | yyjson_val *root, *v; |
| 443 | |
| 444 | if (out && outsz) out[0] = '\0'; |
| 445 | if (qs && get_query_val(qs, key, out, outsz) && out && out[0]) return 1; |
| 446 | root = body_root(); |
| 447 | v = root ? yyjson_obj_get(root, key) : NULL; |
| 448 | if (v && yyjson_is_str(v)) { |
| 449 | snprintf(out, outsz, "%s", yyjson_get_str(v)); |
| 450 | return 1; |
| 451 | } |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | /* ------------------------------------------------------------------ */ |
| 456 | /* HTML page */ |
| 457 | /* ------------------------------------------------------------------ */ |
| 458 | |
| 459 | /* Expand the two control-byte placeholders from src/ui.html into the bot |
| 460 | * theme macros, so the page and the bot palette can never drift apart. */ |
| 461 | static void emit_ui(void) |
| 462 | { |
| 463 | const char *p = UI_HTML; |
| 464 | |
| 465 | while (*p) { |
| 466 | if (*p == '\001') { |
| 467 | fputs(SX_BOT_THEME_PREPAINT "<style>" SX_BOT_THEME_TOKENS |
| 468 | ":root{--tx:var(--text);--mu:var(--text-2);--ln:var(--line);--ac:var(--blue)}" |
| 469 | "</style>", stdout); |
| 470 | p++; |
| 471 | } else if (*p == '\002') { |
| 472 | fputs(SX_BOT_THEME_HANDOFF, stdout); |
| 473 | p++; |
| 474 | } else { |
| 475 | putchar(*p++); |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | static void mode_html(void) |
| 481 | { |
| 482 | fputs("Content-Type: text/html; charset=utf-8\r\n" |
| 483 | "Cache-Control: no-store\r\n" |
| 484 | "X-Content-Type-Options: nosniff\r\n" |
| 485 | "X-Frame-Options: SAMEORIGIN\r\n" |
| 486 | "Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'; " |
| 487 | "style-src 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; " |
| 488 | "form-action 'none'; base-uri 'none'\r\n\r\n", stdout); |
| 489 | emit_ui(); |
| 490 | } |
| 491 | |
| 492 | /* ------------------------------------------------------------------ */ |
| 493 | /* JSON API */ |
| 494 | /* ------------------------------------------------------------------ */ |
| 495 | |
| 496 | /* Run one tool for an HTTP action. The page sends the same argument names as |
| 497 | * the MCP schema, so one dispatch serves both surfaces. */ |
| 498 | static void api_run(const char *tool, const char *qs) |
| 499 | { |
| 500 | yyjson_mut_doc *md = yyjson_mut_doc_new(NULL); |
| 501 | yyjson_mut_val *args; |
| 502 | yyjson_doc *adoc; |
| 503 | yyjson_val *av; |
| 504 | tool_out_t t; |
| 505 | char *res; |
| 506 | const char *keys[] = { "timezone", "time", "source_timezone", |
| 507 | "target_timezone", "days_ago", "hours_ago", |
| 508 | "minutes_ago", "seconds_ago" }; |
| 509 | size_t i; |
| 510 | |
| 511 | if (!md) { json_err(500, "out of memory"); return; } |
| 512 | args = yyjson_mut_obj(md); |
| 513 | yyjson_mut_doc_set_root(md, args); |
| 514 | |
| 515 | for (i = 0; i < sizeof keys / sizeof keys[0]; i++) { |
| 516 | yyjson_val *bv; |
| 517 | char sval[512]; |
| 518 | |
| 519 | bv = body_root(); |
| 520 | if (bv && yyjson_is_obj(bv)) { |
| 521 | yyjson_val *v = yyjson_obj_get(bv, keys[i]); |
| 522 | if (v && yyjson_is_str(v)) { |
| 523 | yyjson_mut_obj_add_strcpy(md, args, keys[i], yyjson_get_str(v)); |
| 524 | continue; |
| 525 | } |
| 526 | if (v && yyjson_is_int(v)) { |
| 527 | yyjson_mut_obj_add_int(md, args, keys[i], |
| 528 | (long long)yyjson_get_sint(v)); |
| 529 | continue; |
| 530 | } |
| 531 | } |
| 532 | if (qs && get_query_val(qs, keys[i], sval, sizeof sval) && sval[0]) { |
| 533 | /* The integer fields are numeric arguments; a query string always |
| 534 | * arrives as text, so re-type them here rather than making every |
| 535 | * caller remember which surface wants which type. */ |
| 536 | if (strstr(keys[i], "_ago")) { |
| 537 | char *end = NULL; |
| 538 | long long n = strtoll(sval, &end, 10); |
| 539 | if (end && *end == '\0') |
| 540 | yyjson_mut_obj_add_int(md, args, keys[i], n); |
| 541 | else |
| 542 | yyjson_mut_obj_add_strcpy(md, args, keys[i], sval); |
| 543 | } else { |
| 544 | /* strcpy, not str: `sval` is one stack buffer reused for every |
| 545 | * key in this loop, so a borrowed pointer leaves every argument |
| 546 | * pointing at the last value it held. Silent, and the JSON |
| 547 | * still parses. */ |
| 548 | yyjson_mut_obj_add_strcpy(md, args, keys[i], sval); |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | /* The tool implementations read an immutable yyjson_val; serialise the |
| 554 | * mutable object and parse it back so both surfaces share one argument |
| 555 | * shape (and so the HTTP path exercises the same reader as --mcp). */ |
| 556 | { |
| 557 | char *text = yyjson_mut_write(md, 0, NULL); |
| 558 | yyjson_mut_doc_free(md); |
| 559 | adoc = text ? yyjson_read(text, strlen(text), 0) : NULL; |
| 560 | free(text); |
| 561 | } |
| 562 | av = adoc ? yyjson_doc_get_root(adoc) : NULL; |
| 563 | |
| 564 | if (strcmp(tool, "get_current_time") == 0) tool_get_current_time(av, &t); |
| 565 | else if (strcmp(tool, "convert_time") == 0) tool_convert_time(av, &t); |
| 566 | else if (strcmp(tool, "get_time_ago") == 0) tool_get_time_ago(av, &t); |
| 567 | else { if (adoc) yyjson_doc_free(adoc); json_err(400, "unknown tool"); return; } |
| 568 | |
| 569 | res = tool_api_result(&t); |
| 570 | json_raw(res ? res : "{\"ok\":false,\"error\":\"no result\"}"); |
| 571 | free(res); |
| 572 | tool_free(&t); |
| 573 | if (adoc) yyjson_doc_free(adoc); |
| 574 | } |
| 575 | |
| 576 | /* The page's zone picker: every embedded key plus the local one. */ |
| 577 | static void api_zones(void) |
| 578 | { |
| 579 | buf_t b; |
| 580 | int i; |
| 581 | |
| 582 | buf_init(&b); |
| 583 | buf_puts(&b, "{\"ok\":true,\"count\":"); |
| 584 | buf_printf(&b, "%d", tz_count()); |
| 585 | buf_puts(&b, ",\"local\":"); |
| 586 | json_str(&b, tz_local_name()); |
| 587 | buf_puts(&b, ",\"zones\":["); |
| 588 | for (i = 0; i < tz_count(); i++) { |
| 589 | if (i) buf_putc(&b, ','); |
| 590 | json_str(&b, tz_name_at(i)); |
| 591 | } |
| 592 | buf_puts(&b, "]}"); |
| 593 | json_raw(buf_str(&b)); |
| 594 | buf_free(&b); |
| 595 | } |
| 596 | |
| 597 | static int api_handle(void) |
| 598 | { |
| 599 | const char *qs = getenv("QUERY_STRING"); |
| 600 | const char *method = getenv("REQUEST_METHOD"); |
| 601 | char action[64]; |
| 602 | int is_get; |
| 603 | |
| 604 | if (!qs) qs = ""; |
| 605 | is_get = !method || strcmp(method, "GET") == 0; |
| 606 | |
| 607 | require_node_auth(); |
| 608 | |
| 609 | if (!is_get && read_body() < 0) { |
| 610 | json_err(413, "request body too large"); |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | /* Two separate buffers: parsing `format` into `action` would clobber the |
| 615 | * action we just parsed and silently answer the default one. */ |
| 616 | action[0] = '\0'; |
| 617 | if (!get_query_val(qs, "action", action, sizeof action) || !action[0]) |
| 618 | param_str(qs, "action", action, sizeof action); |
| 619 | |
| 620 | if (!action[0]) { |
| 621 | if (strstr(qs, "format=json")) snprintf(action, sizeof action, "current"); |
| 622 | else { mode_html(); return 0; } |
| 623 | } |
| 624 | |
| 625 | if (strcmp(action, "current") == 0 || strcmp(action, "now") == 0) |
| 626 | api_run("get_current_time", qs); |
| 627 | else if (strcmp(action, "convert") == 0) |
| 628 | api_run("convert_time", qs); |
| 629 | else if (strcmp(action, "ago") == 0) |
| 630 | api_run("get_time_ago", qs); |
| 631 | else if (strcmp(action, "zones") == 0) |
| 632 | api_zones(); |
| 633 | else if (strcmp(action, "tools") == 0) { |
| 634 | char *t = tools_list_json(); |
| 635 | buf_t b; |
| 636 | buf_init(&b); |
| 637 | buf_puts(&b, "{\"ok\":true,\"tools\":"); |
| 638 | buf_puts(&b, t ? t : "[]"); |
| 639 | buf_puts(&b, "}"); |
| 640 | json_raw(buf_str(&b)); |
| 641 | buf_free(&b); |
| 642 | free(t); |
| 643 | } else if (strcmp(action, "info") == 0) { |
| 644 | buf_t b; |
| 645 | buf_init(&b); |
| 646 | buf_puts(&b, "{\"ok\":true,\"id\":\"" PLUGIN_ID "\",\"version\":\"" |
| 647 | PLUGIN_VER "\",\"kind\":\"mcp\",\"mcp\":true,\"zones\":"); |
| 648 | buf_printf(&b, "%d", tz_count()); |
| 649 | buf_puts(&b, ",\"local\":"); |
| 650 | json_str(&b, tz_local_name()); |
| 651 | buf_puts(&b, "}"); |
| 652 | json_raw(buf_str(&b)); |
| 653 | buf_free(&b); |
| 654 | } else { |
| 655 | json_err(400, "unknown action"); |
| 656 | } |
| 657 | if (g_body_doc) { yyjson_doc_free(g_body_doc); g_body_doc = NULL; } |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | /* ------------------------------------------------------------------ */ |
| 662 | /* main */ |
| 663 | /* ------------------------------------------------------------------ */ |
| 664 | |
| 665 | static void print_help(void) |
| 666 | { |
| 667 | int i; |
| 668 | printf("%s %s - SCORPIOX BOT world time MCP connector\n\n" |
| 669 | "modes:\n" |
| 670 | " --meta print plugin discovery metadata and exit\n" |
| 671 | " --mcp serve MCP JSON-RPC 2.0 over stdin/stdout\n" |
| 672 | " --schema print the tools/list array\n" |
| 673 | " --win-tz <id> resolve a Windows timezone id to its IANA key\n" |
| 674 | " --version print version\n" |
| 675 | " (no args) CGI: World Clock page + ?action= API\n\n" |
| 676 | "tools: ", PLUGIN_ID, PLUGIN_VER); |
| 677 | for (i = 0; i < TOOL_COUNT; i++) |
| 678 | printf("%s%s", i ? ", " : "", TOOL_NAMES[i]); |
| 679 | printf("\nzones: %d embedded IANA zones; local is %s\n", |
| 680 | tz_count(), tz_local_name()); |
| 681 | } |
| 682 | |
| 683 | int main(int argc, char **argv) |
| 684 | { |
| 685 | int i; |
| 686 | |
| 687 | for (i = 1; i < argc; i++) { |
| 688 | if (strcmp(argv[i], "--meta") == 0) return mode_meta(); |
| 689 | if (strcmp(argv[i], "--mcp") == 0) return mode_mcp(); |
| 690 | if (strcmp(argv[i], "--version") == 0) { |
| 691 | printf("%s %s\n", PLUGIN_ID, PLUGIN_VER); |
| 692 | return 0; |
| 693 | } |
| 694 | if (strcmp(argv[i], "--schema") == 0) { |
| 695 | char *t = tools_list_json(); |
| 696 | printf("%s\n", t ? t : "[]"); |
| 697 | free(t); |
| 698 | return 0; |
| 699 | } |
| 700 | if (strcmp(argv[i], "--win-tz") == 0) { |
| 701 | /* Diagnostic: what does a Windows node's registry name resolve to? |
| 702 | * The mapping is embedded on every platform, so this answers for a |
| 703 | * Windows box from a Linux one. */ |
| 704 | const tz_zone *z = (i + 1 < argc) ? tz_find_windows(argv[i + 1]) : NULL; |
| 705 | if (!z) { |
| 706 | fprintf(stderr, "%s: unknown Windows timezone id%s%s\n", |
| 707 | PLUGIN_ID, (i + 1 < argc) ? ": " : "", |
| 708 | (i + 1 < argc) ? argv[i + 1] : ""); |
| 709 | return 1; |
| 710 | } |
| 711 | printf("%s\n", tz_zone_name(z)); |
| 712 | return 0; |
| 713 | } |
| 714 | if (strcmp(argv[i], "--help") == 0) { print_help(); return 0; } |
| 715 | } |
| 716 | if (getenv("SX_PLUGIN_META")) return mode_meta(); |
| 717 | if (getenv("SX_PLUGIN_MCP")) return mode_mcp(); |
| 718 | |
| 719 | return api_handle(); |
| 720 | } |