2 * Sub-authentication Daemon: Commands
4 * (c) 2017 Martin Mares <mj@ucw.cz>
11 void cmd_error(struct client *c, const char *err)
13 json_object_set(c->reply, "error", json_new_string(c->json, err));
16 static void cmd_ok(struct client *c)
21 static const char *get_string(struct json_node *n, const char *key)
23 struct json_node *s = json_object_get(n, key);
24 if (s && s->type == JSON_STRING)
30 static void cmd_nop(struct client *c)
37 void (*handler)(struct client *c);
40 static const struct command command_table[] = {
44 void cmd_dispatch(struct client *c)
46 struct json_node *rq = c->request;
49 if (rq->type != JSON_OBJECT || !(cmd = get_string(rq, "cmd")))
51 cmd_error(c, "Malformed request");
55 for (uint i=0; i < ARRAY_SIZE(command_table); i++)
56 if (!strcmp(cmd, command_table[i].cmd))
58 command_table[i].handler(c);
62 cmd_error(c, "No such command");