scorpiox-bot-plugin-time
←plugintime.hc
112 lines · 4 KBHEADBlameDownload
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
43typedef struct {
44 char *p;
45 size_t n;
46 size_t cap;
47} buf_t;
48
49void buf_init(buf_t *b);
50int buf_need(buf_t *b, size_t extra);
51void buf_add(buf_t *b, const char *s, size_t n);
52void buf_puts(buf_t *b, const char *s);
53void buf_putc(buf_t *b, int c);
54void buf_printf(buf_t *b, const char *fmt, ...);
55const char *buf_str(const buf_t *b);
56void buf_free(buf_t *b);
57
58/* JSON string escape into a buf_t (safe against control characters). */
59void 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). */
66void 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. */
70void time_iso(int64_t utc, int offset, char *out, size_t outsz);
71/* "+13:00", "-05:00", "+05:45", "+00:00". */
72void time_offset_str(int offset, char *out, size_t outsz);
73/* "Friday" */
74const char *time_weekday_name(int weekday);
75/* The current UTC instant; SX_TIME_NOW pins it for deterministic tests. */
76int64_t time_now_utc(void);
77
78/* "+3.0h" / "+5.75h" / "-0.5h" - the reference server's format. */
79void time_diff_str(int source_off, int target_off, char *out, size_t outsz);
80
81/* ------------------------------------------------------------------ */
82/* Tool results */
83/* ------------------------------------------------------------------ */
84
85typedef 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
92void tool_out_init(tool_out_t *t);
93void tool_err(tool_out_t *t, const char *fmt, ...);
94void tool_free(tool_out_t *t);
95
96void tool_get_current_time(yyjson_val *args, tool_out_t *t);
97void tool_convert_time(yyjson_val *args, tool_out_t *t);
98void tool_get_time_ago(yyjson_val *args, tool_out_t *t);
99
100/* The full tools/list array as JSON text (caller frees). */
101char *tools_list_json(void);
102int tool_dispatch(const char *name, yyjson_val *args, tool_out_t *out);
103/* MCP result: content[] + structuredContent + isError. Caller frees. */
104char *tool_mcp_result(tool_out_t *t);
105/* HTTP API result: {"ok":true,...} or {"ok":false,"error":"..."}. Caller frees. */
106char *tool_api_result(tool_out_t *t);
107
108/* The three tool names, for --help and the page's tool list. */
109extern const char *const TOOL_NAMES[];
110extern const int TOOL_COUNT;
111
112#endif /* SX_PLUGIN_TIME_H */