| 1 | /* |
| 2 | * plugintime.h - shared declarations for plugin_time. |
| 3 | * |
| 4 | * NOT time.h: gcc searches -I directories for #include <...> as well, so a |
| 5 | * local time.h silently shadows the C library's <time.h> and every struct tm |
| 6 | * in the tree stops existing. |
| 7 | * |
| 8 | * plugin_time is a SCORPIOX BOT connector plugin that reimplements the |
| 9 | * reference MCP "time" server in pure C99: IANA timezone queries and |
| 10 | * wall-clock conversions, served as MCP tools over stdio and as a JSON API |
| 11 | * behind a browser UI. |
| 12 | * |
| 13 | * plugin_time.c main(), --meta, --mcp loop, CGI dispatch, auth, HTML |
| 14 | * timeutil.c the three tools, ISO formatting, tool schemas |
| 15 | * tz.c the embedded IANA table: keys, offsets, wall resolution |
| 16 | * |
| 17 | * MIT License. Copyright (c) 2026 SCORPIOX INC. |
| 18 | */ |
| 19 | #ifndef SX_PLUGIN_TIME_H |
| 20 | #define SX_PLUGIN_TIME_H |
| 21 | |
| 22 | #include <stddef.h> |
| 23 | #include <stdint.h> |
| 24 | #include "yyjson.h" |
| 25 | |
| 26 | #define PLUGIN_SLUG "time" |
| 27 | #define PLUGIN_ID "plugin_time" |
| 28 | #define PLUGIN_VER "1.0.2" |
| 29 | #define PLUGIN_NAME "time" |
| 30 | #define PLUGIN_DESC "World time MCP" |
| 31 | |
| 32 | #define MCP_PROTOCOL "2025-06-18" |
| 33 | #define SERVER_NAME "time-server" |
| 34 | |
| 35 | #define TZ_NAME_MAX 96 |
| 36 | #define STDIN_LINE_MAX (1024 * 1024) |
| 37 | #define REQ_BODY_MAX (1024 * 1024) |
| 38 | |
| 39 | /* ------------------------------------------------------------------ */ |
| 40 | /* Growable byte buffer (timeutil.c) */ |
| 41 | /* ------------------------------------------------------------------ */ |
| 42 | |
| 43 | typedef struct { |
| 44 | char *p; |
| 45 | size_t n; |
| 46 | size_t cap; |
| 47 | } buf_t; |
| 48 | |
| 49 | void buf_init(buf_t *b); |
| 50 | int buf_need(buf_t *b, size_t extra); |
| 51 | void buf_add(buf_t *b, const char *s, size_t n); |
| 52 | void buf_puts(buf_t *b, const char *s); |
| 53 | void buf_putc(buf_t *b, int c); |
| 54 | void buf_printf(buf_t *b, const char *fmt, ...); |
| 55 | const char *buf_str(const buf_t *b); |
| 56 | void buf_free(buf_t *b); |
| 57 | |
| 58 | /* JSON string escape into a buf_t (safe against control characters). */ |
| 59 | void json_str(buf_t *b, const char *s); |
| 60 | |
| 61 | /* ------------------------------------------------------------------ */ |
| 62 | /* Time formatting */ |
| 63 | /* ------------------------------------------------------------------ */ |
| 64 | |
| 65 | /* Local wall time for a UTC instant in `offset` (east of UTC, seconds). */ |
| 66 | void time_split(int64_t utc, int offset, int *year, int *month, int *day, |
| 67 | int *hour, int *min, int *sec, int *weekday); |
| 68 | /* ISO-8601 with seconds precision and a numeric offset, e.g. |
| 69 | * 2026-11-14T13:21:10+13:00. `out` needs ~40 bytes. */ |
| 70 | void time_iso(int64_t utc, int offset, char *out, size_t outsz); |
| 71 | /* "+13:00", "-05:00", "+05:45", "+00:00". */ |
| 72 | void time_offset_str(int offset, char *out, size_t outsz); |
| 73 | /* "Friday" */ |
| 74 | const char *time_weekday_name(int weekday); |
| 75 | /* The current UTC instant; SX_TIME_NOW pins it for deterministic tests. */ |
| 76 | int64_t time_now_utc(void); |
| 77 | |
| 78 | /* "+3.0h" / "+5.75h" / "-0.5h" - the reference server's format. */ |
| 79 | void time_diff_str(int source_off, int target_off, char *out, size_t outsz); |
| 80 | |
| 81 | /* ------------------------------------------------------------------ */ |
| 82 | /* Tool results */ |
| 83 | /* ------------------------------------------------------------------ */ |
| 84 | |
| 85 | typedef struct { |
| 86 | yyjson_mut_doc *md; /* owns root */ |
| 87 | yyjson_mut_val *root; |
| 88 | char *error; /* error text when is_error */ |
| 89 | int is_error; |
| 90 | } tool_out_t; |
| 91 | |
| 92 | void tool_out_init(tool_out_t *t); |
| 93 | void tool_err(tool_out_t *t, const char *fmt, ...); |
| 94 | void tool_free(tool_out_t *t); |
| 95 | |
| 96 | void tool_get_current_time(yyjson_val *args, tool_out_t *t); |
| 97 | void tool_convert_time(yyjson_val *args, tool_out_t *t); |
| 98 | void tool_get_time_ago(yyjson_val *args, tool_out_t *t); |
| 99 | |
| 100 | /* The full tools/list array as JSON text (caller frees). */ |
| 101 | char *tools_list_json(void); |
| 102 | int tool_dispatch(const char *name, yyjson_val *args, tool_out_t *out); |
| 103 | /* MCP result: content[] + structuredContent + isError. Caller frees. */ |
| 104 | char *tool_mcp_result(tool_out_t *t); |
| 105 | /* HTTP API result: {"ok":true,...} or {"ok":false,"error":"..."}. Caller frees. */ |
| 106 | char *tool_api_result(tool_out_t *t); |
| 107 | |
| 108 | /* The three tool names, for --help and the page's tool list. */ |
| 109 | extern const char *const TOOL_NAMES[]; |
| 110 | extern const int TOOL_COUNT; |
| 111 | |
| 112 | #endif /* SX_PLUGIN_TIME_H */ |