1a85aa543STrond Norbye#include <stdio.h>
2a85aa543STrond Norbye#include <stdlib.h>
3a85aa543STrond Norbye#include <string.h>
4d0366df5STrond Norbye#include <platform/cbassert.h>
5a85aa543STrond Norbye
6a85aa543STrond Norbye#include <libconflate/conflate.h>
7a85aa543STrond Norbye#include "conflate_internal.h"
8a85aa543STrond Norbye
9a85aa543STrond Norbyebool commands_initialized = false;
10a85aa543STrond Norbye
11a85aa543STrond Norbyestatic enum conflate_mgmt_cb_result process_serverlist(void *opaque,
12a85aa543STrond Norbye                                                       conflate_handle_t *handle,
13a85aa543STrond Norbye                                                       const char *cmd,
14a85aa543STrond Norbye                                                       bool direct,
15a85aa543STrond Norbye                                                       kvpair_t *conf,
16a85aa543STrond Norbye                                                       conflate_form_result *r)
17a85aa543STrond Norbye{
18a85aa543STrond Norbye   (void)opaque;
19a85aa543STrond Norbye   (void)cmd;
20a85aa543STrond Norbye   (void)r;
21a85aa543STrond Norbye
22a85aa543STrond Norbye    /* If we have "config_is_private" set to "yes" we should only
23a85aa543STrond Norbye       process this if it's direct (i.e. ignore pubsub) */
24a85aa543STrond Norbye    if (!direct) {
25a85aa543STrond Norbye        char *priv = conflate_get_private(handle, "config_is_private",
26a85aa543STrond Norbye                                          handle->conf->save_path);
27a85aa543STrond Norbye
28a85aa543STrond Norbye        if (priv && strcmp(priv, "yes") == 0) {
29a85aa543STrond Norbye            handle->conf->log(handle->conf->userdata, LOG_LVL_INFO,
30a85aa543STrond Norbye                              "Currently using a private config, ignoring update.");
31a85aa543STrond Norbye            return RV_OK;
32a85aa543STrond Norbye        }
33a85aa543STrond Norbye        free(priv);
34a85aa543STrond Norbye    }
35a85aa543STrond Norbye
36a85aa543STrond Norbye    handle->conf->log(handle->conf->userdata, LOG_LVL_INFO,
37a85aa543STrond Norbye                      "Processing a serverlist");
38a85aa543STrond Norbye
39a85aa543STrond Norbye    /* Persist the config lists */
40a85aa543STrond Norbye    if (!save_kvpairs(handle, conf, handle->conf->save_path)) {
41a85aa543STrond Norbye        handle->conf->log(handle->conf->userdata, LOG_LVL_ERROR,
42a85aa543STrond Norbye                          "Can not save config to %s",
43a85aa543STrond Norbye                          handle->conf->save_path);
44a85aa543STrond Norbye    }
45a85aa543STrond Norbye
46a85aa543STrond Norbye    /* Send the config to the callback */
47a85aa543STrond Norbye    handle->conf->new_config(handle->conf->userdata, conf);
48a85aa543STrond Norbye
49a85aa543STrond Norbye    return RV_OK;
50a85aa543STrond Norbye}
51a85aa543STrond Norbye
52a85aa543STrond Norbyestatic enum conflate_mgmt_cb_result process_set_private(void *opaque,
53a85aa543STrond Norbye                                                        conflate_handle_t *handle,
54a85aa543STrond Norbye                                                        const char *cmd,
55a85aa543STrond Norbye                                                        bool direct,
56a85aa543STrond Norbye                                                        kvpair_t *form,
57a85aa543STrond Norbye                                                        conflate_form_result *r)
58a85aa543STrond Norbye{
59a85aa543STrond Norbye    enum conflate_mgmt_cb_result rv = RV_ERROR;
60a85aa543STrond Norbye
61a85aa543STrond Norbye    char *key = get_simple_kvpair_val(form, "key");
62a85aa543STrond Norbye    char *value = get_simple_kvpair_val(form, "value");
63a85aa543STrond Norbye
64a85aa543STrond Norbye    (void)opaque;
65a85aa543STrond Norbye    (void)cmd;
66a85aa543STrond Norbye    (void)r;
67a85aa543STrond Norbye
68a85aa543STrond Norbye    /* Only direct stat requests are handled. */
69d0366df5STrond Norbye    cb_assert(direct);
70a85aa543STrond Norbye
71a85aa543STrond Norbye    if (key && value) {
72a85aa543STrond Norbye        if (conflate_save_private(handle, key, value,
73a85aa543STrond Norbye                                  handle->conf->save_path)) {
74a85aa543STrond Norbye            rv = RV_OK;
75a85aa543STrond Norbye        }
76a85aa543STrond Norbye    } else {
77a85aa543STrond Norbye        rv = RV_BADARG;
78a85aa543STrond Norbye    }
79a85aa543STrond Norbye
80a85aa543STrond Norbye    return rv;
81a85aa543STrond Norbye}
82a85aa543STrond Norbye
83a85aa543STrond Norbyestatic enum conflate_mgmt_cb_result process_get_private(void *opaque,
84a85aa543STrond Norbye                                                        conflate_handle_t *handle,
85a85aa543STrond Norbye                                                        const char *cmd,
86a85aa543STrond Norbye                                                        bool direct,
87a85aa543STrond Norbye                                                        kvpair_t *form,
88a85aa543STrond Norbye                                                        conflate_form_result *r)
89a85aa543STrond Norbye{
90a85aa543STrond Norbye    enum conflate_mgmt_cb_result rv = RV_ERROR;
91a85aa543STrond Norbye    char *key = get_simple_kvpair_val(form, "key");
92a85aa543STrond Norbye    (void)opaque;
93a85aa543STrond Norbye    (void)cmd;
94a85aa543STrond Norbye
95a85aa543STrond Norbye    /* Only direct stat requests are handled. */
96d0366df5STrond Norbye    cb_assert(direct);
97a85aa543STrond Norbye
98a85aa543STrond Norbye    if (key) {
99a85aa543STrond Norbye        /* Initialize the form so there's always one there */
100a85aa543STrond Norbye        char *value;
101a85aa543STrond Norbye        conflate_init_form(r);
102a85aa543STrond Norbye        value = conflate_get_private(handle, key, handle->conf->save_path);
103a85aa543STrond Norbye        if (value) {
104a85aa543STrond Norbye            conflate_add_field(r, key, value);
105a85aa543STrond Norbye            free(value);
106a85aa543STrond Norbye        }
107a85aa543STrond Norbye
108a85aa543STrond Norbye        rv = RV_OK;
109a85aa543STrond Norbye    } else {
110a85aa543STrond Norbye        rv = RV_BADARG;
111a85aa543STrond Norbye    }
112a85aa543STrond Norbye
113a85aa543STrond Norbye    return rv;
114a85aa543STrond Norbye}
115a85aa543STrond Norbye
116a85aa543STrond Norbyestatic enum conflate_mgmt_cb_result process_delete_private(void *opaque,
117a85aa543STrond Norbye                                                           conflate_handle_t *handle,
118a85aa543STrond Norbye                                                           const char *cmd,
119a85aa543STrond Norbye                                                           bool direct,
120a85aa543STrond Norbye                                                           kvpair_t *form,
121a85aa543STrond Norbye                                                           conflate_form_result *r)
122a85aa543STrond Norbye{
123a85aa543STrond Norbye    enum conflate_mgmt_cb_result rv = RV_ERROR;
124a85aa543STrond Norbye    char *key = get_simple_kvpair_val(form, "key");
125a85aa543STrond Norbye    (void)opaque;
126a85aa543STrond Norbye    (void)cmd;
127a85aa543STrond Norbye    (void)r;
128a85aa543STrond Norbye
129a85aa543STrond Norbye    /* Only direct stat requests are handled. */
130d0366df5STrond Norbye    cb_assert(direct);
131a85aa543STrond Norbye
132a85aa543STrond Norbye    if (key) {
133a85aa543STrond Norbye        if (conflate_delete_private(handle, key, handle->conf->save_path)) {
134a85aa543STrond Norbye            rv = RV_OK;
135a85aa543STrond Norbye        }
136a85aa543STrond Norbye    } else {
137a85aa543STrond Norbye        rv = RV_BADARG;
138a85aa543STrond Norbye    }
139a85aa543STrond Norbye
140a85aa543STrond Norbye    return rv;
141a85aa543STrond Norbye}
142a85aa543STrond Norbye
143a85aa543STrond Norbyevoid conflate_init_commands(void)
144a85aa543STrond Norbye{
145a85aa543STrond Norbye    if (commands_initialized) {
146a85aa543STrond Norbye        return;
147a85aa543STrond Norbye    }
148a85aa543STrond Norbye
149a85aa543STrond Norbye    conflate_register_mgmt_cb("set_private",
150a85aa543STrond Norbye                              "Set a private value on the agent.",
151a85aa543STrond Norbye                              process_set_private);
152a85aa543STrond Norbye    conflate_register_mgmt_cb("get_private",
153a85aa543STrond Norbye                              "Get a private value from the agent.",
154a85aa543STrond Norbye                              process_get_private);
155a85aa543STrond Norbye    conflate_register_mgmt_cb("rm_private",
156a85aa543STrond Norbye                              "Delete a private value from the agent.",
157a85aa543STrond Norbye                              process_delete_private);
158a85aa543STrond Norbye
159a85aa543STrond Norbye    conflate_register_mgmt_cb("serverlist", "Configure a server list.",
160a85aa543STrond Norbye                              process_serverlist);
161a85aa543STrond Norbye
162a85aa543STrond Norbye    commands_initialized = true;
163a85aa543STrond Norbye}