/* * tasksd-http — CGI front-end for tasksd. * * C99, HTML 4.01, no JavaScript, no auth. Every list, read, create, edit and * state change goes through tasksd's MCP server. The CGI never reads the task * files itself. * * Routes (mounted at /cgi-bin/tasksd-http under busybox httpd): * / — tasks, filter with ?state=…&scope=… * /scopes — scope overview with counts * /new — create a task * /t/ — one task with controls * /t//edit — title, goal, scope, priority, tags * /t//pause — POST, note optional * /t//resume — POST, note optional * /t//done — POST, result optional * /t//drop — POST, result optional * * Environment: TASKSD_URL (default http://127.0.0.1:8781/mcp) * * Build: * cc -std=c99 -O2 -Wall -Wextra -Wpedantic -D_POSIX_C_SOURCE=200809L \ * -Ivendor/libmcp -Ivendor/cgi -o tasksd-http tasksd-http.c */ #define _POSIX_C_SOURCE 200809L #define MCP_IMPLEMENTATION #include "mcp.h" #include "cgi.h" #define ENV_URL "TASKSD_URL" #define DEF_URL "http://127.0.0.1:8781/mcp" #define APP "tasksd-http" static void state_badge(const char *state) { const char *s = state != NULL ? state : "?"; fputs("", stdout); cg_html(s); fputs("", stdout); } static void state_tabs(const char *scope, const char *active) { static const char *states[] = { "", "pending", "running", "paused", "done", "dropped" }; static const char *labels[] = { "All", "Pending", "Running", "Paused", "Done", "Dropped" }; fputs("
", stdout); for (size_t i = 0; i < sizeof(states) / sizeof(states[0]); i++) { int on = active != NULL && strcmp(active, states[i]) == 0; if (active == NULL && states[i][0] == '\0') on = 1; fputs("", stdout); if (on) fputs("", stdout); fputs(labels[i], stdout); if (on) fputs("", stdout); fputs(" ", stdout); } fputs("
\n", stdout); } static void task_row(const mcp_json *t) { const char *id = mcp_arg_string(t, "id"); const char *title = mcp_arg_string(t, "title"); const char *scope = mcp_arg_string(t, "scope"); const char *state = mcp_arg_string(t, "state"); const char *tags = mcp_arg_string(t, "tags"); double prio = mcp_arg_num(t, "priority", 0); if (id == NULL) return; fputs("", stdout); cg_html(id); fputs("", stdout); state_badge(state); fputs("", stdout); cg_html(title != NULL ? title : "(untitled)"); fputs("
scope ", stdout); cg_html(scope != NULL ? scope : ""); printf(" · priority %.0f", prio); if (tags != NULL && *tags != '\0') { fputs(" · tags ", stdout); cg_html(tags); } fputs("", stdout); cg_time((long)mcp_arg_num(t, "created", 0)); fputs("\n", stdout); } static void render_tasks(const mcp_json *arr) { size_t n = mcp_array_len(arr); if (n == 0) { fputs("

No tasks match.

\n", stdout); return; } fputs("" "\n", stdout); for (size_t i = 0; i < n; i++) task_row(mcp_array_at(arr, i)); fputs("
IDStateTaskCreated
\n", stdout); } static void route_list(const char *query) { char *scope = cg_param(query, "scope"); char *state = cg_param(query, "state"); char *limstr = cg_param(query, "limit"); int limit = limstr != NULL ? atoi(limstr) : 100; mcp_buf args; int first = 1; mcp_json *v; if (limit <= 0 || limit > 500) limit = 100; mcp_buf_init(&args); mcp_buf_puts(&args, "{"); cg_args_add_str(&args, &first, "scope", scope); if (state != NULL && *state != '\0' && strcmp(state, "all") != 0) cg_args_add_str(&args, &first, "state", state); cg_args_add_int(&args, &first, "limit", limit); mcp_buf_puts(&args, "}"); v = cg_rpc_json(ENV_URL, DEF_URL, "task_list", args.p); mcp_buf_free(&args); cg_head(APP, "Tasks"); fputs("

Tasks

\n", stdout); state_tabs(scope, state); fputs("
", stdout); if (state != NULL && *state != '\0') { fputs("", stdout); } fputs("
\n", stdout); fputs("\n", stdout); if (v == NULL) { fputs("
tasksd did not answer.
\n", stdout); } else { render_tasks(mcp_arg(v, "tasks")); printf("

%.0f task(s) shown

\n", mcp_arg_num(v, "count", 0)); mcp_json_free(v); } cg_foot(); free(scope); free(state); free(limstr); } static void route_scopes(void) { mcp_json *v = cg_rpc_json(ENV_URL, DEF_URL, "task_scopes", "{}"); cg_head(APP, "Task scopes"); fputs("

Task scopes

\n", stdout); if (v == NULL) { fputs("
tasksd did not answer.
\n", stdout); } else { const mcp_json *arr = mcp_arg(v, "scopes"); size_t n = mcp_array_len(arr); fputs("" "" "\n", stdout); for (size_t i = 0; i < n; i++) { const mcp_json *s = mcp_array_at(arr, i); const char *scope = mcp_arg_string(s, "scope"); if (scope == NULL) continue; fputs("", stdout); printf("" "\n", mcp_arg_num(s, "pending", 0), mcp_arg_num(s, "running", 0), mcp_arg_num(s, "paused", 0), mcp_arg_num(s, "done", 0), mcp_arg_num(s, "dropped", 0)); } fputs("
ScopePendingRunningPausedDoneDropped
", stdout); cg_html(scope); fputs("%.0f%.0f%.0f%.0f%.0f
\n", stdout); mcp_json_free(v); } fputs("\n", stdout); cg_foot(); } static void task_kv(const char *key, const char *val) { if (val == NULL || *val == '\0') return; fputs("
", stdout); cg_html(key); fputs("
", stdout); cg_md(val); fputs("
\n", stdout); } static void action_form(const char *id, const char *action, const char *label, const char *field, const char *placeholder) { fputs("

" "

\n", stdout); } static void task_actions(const char *id, const char *state) { int paused = state != NULL && strcmp(state, "paused") == 0; int closed = state != NULL && (strcmp(state, "done") == 0 || strcmp(state, "dropped") == 0); fputs("

Controls

\n", stdout); if (paused) { action_form(id, "resume", "Resume", "note", "Note (what was left, why now continues)"); } else if (!closed) { action_form(id, "pause", "Pause", "note", "Note (why parked, what is left)"); } if (!closed) { action_form(id, "done", "Mark done", "result", "Result (what was achieved, evidence)"); action_form(id, "drop", "Drop", "result", "Reason (why it is not worth doing)"); } } static void route_detail(const char *id) { mcp_buf args; int first = 1; char *text; mcp_json *v; mcp_buf_init(&args); mcp_buf_puts(&args, "{"); cg_args_add_str(&args, &first, "id", id); mcp_buf_puts(&args, "}"); text = cg_rpc(ENV_URL, DEF_URL, "task_get", args.p); mcp_buf_free(&args); if (text == NULL) { cg_error_page(APP, "Task not found", "tasksd did not answer."); return; } v = mcp_parse(text); free(text); if (v == NULL) { cg_error_page(APP, "Task not found", "tasksd returned no task."); return; } cg_head(APP, id); { const char *title = mcp_arg_string(v, "title"); const char *scope = mcp_arg_string(v, "scope"); const char *state = mcp_arg_string(v, "state"); const char *tags = mcp_arg_string(v, "tags"); const char *result = mcp_arg_string(v, "result"); fputs("

", stdout); cg_html(title != NULL ? title : id); fputs("

\n

", stdout); state_badge(state); fputs(" ", stdout); cg_html(id); fputs(" · scope ", stdout); if (scope != NULL) { fputs("", stdout); cg_html(scope); fputs("", stdout); } printf(" · priority %.0f", mcp_arg_num(v, "priority", 0)); if (tags != NULL && *tags != '\0') { fputs(" · tags ", stdout); cg_html(tags); } fputs("

\n", stdout); task_kv("Goal", mcp_arg_string(v, "goal")); if (result != NULL && *result != '\0') task_kv("Result", result); fputs("

created ", stdout); cg_time((long)mcp_arg_num(v, "created", 0)); fputs(" · started ", stdout); cg_time((long)mcp_arg_num(v, "started", 0)); fputs(" · ended ", stdout); cg_time((long)mcp_arg_num(v, "ended", 0)); fputs("

\n", stdout); fputs("\n", stdout); task_actions(id, state); } mcp_json_free(v); cg_foot(); } static void edit_form(const char *id, const mcp_json *v) { const char *title = mcp_arg_string(v, "title"); const char *goal = mcp_arg_string(v, "goal"); const char *scope = mcp_arg_string(v, "scope"); const char *tags = mcp_arg_string(v, "tags"); fputs("
\n

\n

\n

\n

\n

\n

\n" "
\n", stdout); } static void route_edit(const char *id, const char *body) { if (strcmp(cg_method(), "POST") == 0) { char *title = cg_param(body, "title"); char *goal = cg_param(body, "goal"); char *scope = cg_param(body, "scope"); char *tags = cg_param(body, "tags"); char *prio = cg_param(body, "priority"); mcp_buf args; int first = 1; mcp_json *v; mcp_buf_init(&args); mcp_buf_puts(&args, "{"); cg_args_add_str(&args, &first, "id", id); cg_args_add_str(&args, &first, "title", title); cg_args_add_str(&args, &first, "goal", goal); cg_args_add_str(&args, &first, "scope", scope); cg_args_add_str(&args, &first, "tags", tags); if (prio != NULL && *prio != '\0') cg_args_add_int(&args, &first, "priority", atoi(prio)); mcp_buf_puts(&args, "}"); v = cg_rpc_json(ENV_URL, DEF_URL, "task_update", args.p); mcp_buf_free(&args); cg_head(APP, "Edit task"); if (v == NULL) { fputs("
tasksd did not answer.
\n", stdout); } else { fputs("

Updated

\n
", stdout); cg_html(mcp_arg_string(v, "id")); fputs(" — ", stdout); cg_html(mcp_arg_string(v, "title")); fputs("
\n\n", stdout); mcp_json_free(v); } cg_foot(); free(title); free(goal); free(scope); free(tags); free(prio); return; } { mcp_buf args; int first = 1; char *text; mcp_json *v; mcp_buf_init(&args); mcp_buf_puts(&args, "{"); cg_args_add_str(&args, &first, "id", id); mcp_buf_puts(&args, "}"); text = cg_rpc(ENV_URL, DEF_URL, "task_get", args.p); mcp_buf_free(&args); cg_head(APP, "Edit task"); fputs("

Edit ", stdout); cg_html(id); fputs("

\n", stdout); if (text == NULL) { fputs("
tasksd did not answer.
\n", stdout); } else { v = mcp_parse(text); free(text); if (v != NULL) { edit_form(id, v); mcp_json_free(v); } else { fputs("
Task not found.
\n", stdout); } } cg_foot(); } } static void route_new(const char *body) { if (strcmp(cg_method(), "POST") != 0) { cg_head(APP, "Create a task"); fputs("

Create a task

\n
\n

\n" "

\n" "

\n" "

\n" "

\n" "

\n
\n", stdout); cg_foot(); return; } { char *title = cg_param(body, "title"); char *goal = cg_param(body, "goal"); char *scope = cg_param(body, "scope"); char *tags = cg_param(body, "tags"); char *prio = cg_param(body, "priority"); mcp_buf args; int first = 1; mcp_json *v; if (title == NULL || *title == '\0' || goal == NULL || *goal == '\0' || scope == NULL || *scope == '\0') { cg_error_page(APP, "Create a task", "Title, goal and scope are required."); free(title); free(goal); free(scope); free(tags); free(prio); return; } mcp_buf_init(&args); mcp_buf_puts(&args, "{"); cg_args_add_str(&args, &first, "title", title); cg_args_add_str(&args, &first, "goal", goal); cg_args_add_str(&args, &first, "scope", scope); if (prio != NULL && *prio != '\0') cg_args_add_int(&args, &first, "priority", atoi(prio)); cg_args_add_str(&args, &first, "tags", tags); mcp_buf_puts(&args, "}"); v = cg_rpc_json(ENV_URL, DEF_URL, "task_create", args.p); mcp_buf_free(&args); cg_head(APP, "Create a task"); if (v == NULL) { fputs("
tasksd did not answer.
\n", stdout); } else { const char *id = mcp_arg_string(v, "id"); fputs("

Created

\n
", stdout); cg_html(id != NULL ? id : ""); fputs(" — ", stdout); cg_html(mcp_arg_string(v, "title")); fputs("
\n\n", stdout); mcp_json_free(v); } cg_foot(); free(title); free(goal); free(scope); free(tags); free(prio); } } static void route_state(const char *id, const char *action, const char *body) { const char *tool = NULL; const char *field = NULL; char *note = NULL; mcp_buf args; int first = 1; mcp_json *v; if (strcmp(cg_method(), "POST") != 0) { cg_error_page(APP, "Task state", "State changes need a POST."); return; } if (strcmp(action, "pause") == 0) { tool = "task_pause"; field = "note"; } else if (strcmp(action, "resume") == 0) { tool = "task_resume"; field = "note"; } else if (strcmp(action, "done") == 0) { tool = "task_done"; field = "result"; } else if (strcmp(action, "drop") == 0) { tool = "task_drop"; field = "result"; } if (tool == NULL) { cg_error_page(APP, "Task state", "Unknown action."); return; } note = cg_param(body, field); mcp_buf_init(&args); mcp_buf_puts(&args, "{"); cg_args_add_str(&args, &first, "id", id); cg_args_add_str(&args, &first, field, note); mcp_buf_puts(&args, "}"); v = cg_rpc_json(ENV_URL, DEF_URL, tool, args.p); mcp_buf_free(&args); cg_head(APP, "Task state"); if (v == NULL) { fputs("
tasksd did not answer.
\n", stdout); } else { const char *state = mcp_arg_string(v, "state"); fputs("

", stdout); cg_html(state != NULL ? state : "changed"); fputs("

\n
", stdout); cg_html(mcp_arg_string(v, "id")); fputs(" — ", stdout); cg_html(mcp_arg_string(v, "title")); fputs("
\n\n", stdout); mcp_json_free(v); } cg_foot(); free(note); } /* ------------------------------------------------------------------ router */ static void handle(const char *path, const char *query, const char *body) { if (strcmp(path, "/") == 0 || strcmp(path, "") == 0) { route_list(query); return; } if (strcmp(path, "/scopes") == 0) { route_scopes(); return; } if (strcmp(path, "/new") == 0) { route_new(body); return; } if (strncmp(path, "/t/", 3) == 0) { const char *rest = path + 3; size_t idlen = strcspn(rest, "/"); char id[128]; const char *tail; if (idlen == 0 || idlen >= sizeof(id)) { cg_error_page(APP, "Not found", "No task id in the path."); return; } memcpy(id, rest, idlen); id[idlen] = '\0'; cg_url_decode(id); tail = rest + idlen; if (strcmp(tail, "/edit") == 0) { route_edit(id, body); return; } if (tail[0] == '/' && tail[1] != '\0') { route_state(id, tail + 1, body); return; } if (*tail == '\0') { route_detail(id); return; } } cg_error_page(APP, "Not found", "No such page."); } int main(void) { char path[4096], query[4096]; char *body = NULL; cg_route(path, sizeof(path), query, sizeof(query)); if (strcmp(cg_method(), "POST") == 0) body = cg_read_body(); cg_header(); handle(path, query, body != NULL ? body : ""); free(body); return 0; }