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 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 bool get_uint(struct json_node *n, const char *key, uint *dest)
32 struct json_node *s = json_object_get(n, key);
33 if (s && s->type == JSON_NUMBER)
35 uint u = (uint) s->number;
36 if ((double) u == s->number)
46 struct json_node **get_array(struct json_node *n, const char *key)
48 struct json_node *s = json_object_get(n, key);
49 if (s && s->type == JSON_ARRAY)
55 struct json_node *get_object(struct json_node *n, const char *key)
57 struct json_node *s = json_object_get(n, key);
58 if (s && s->type == JSON_OBJECT)
64 static void cmd_nop(struct client *c)
71 void (*handler)(struct client *c);
74 static const struct command command_table[] = {
78 void cmd_dispatch(struct client *c)
80 struct json_node *rq = c->request;
83 if (rq->type != JSON_OBJECT || !(cmd = get_string(rq, "cmd")))
85 cmd_error(c, "Malformed request");
89 for (uint i=0; i < ARRAY_SIZE(command_table); i++)
90 if (!strcmp(cmd, command_table[i].cmd))
92 command_table[i].handler(c);
96 cmd_error(c, "No such command");