summaryrefslogtreecommitdiffstats
path: root/src/rest.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rest.c')
-rw-r--r--src/rest.c240
1 files changed, 239 insertions, 1 deletions
diff --git a/src/rest.c b/src/rest.c
index e3d9aa7..b48de4c 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -4607,8 +4607,12 @@ static int og_cmd_oglive_list(char *buffer_reply)
struct og_buffer og_buffer = {
.data = buffer_reply
};
+
+ const char *msglog, *live_name, *live_datetime;
+ json_t *root, *live_entry, *oglive_array;
json_error_t json_err;
- json_t *root;
+ struct og_dbi *dbi;
+ dbi_result result;
root = json_load_file(OG_LIVE_JSON_FILE_PATH, 0, &json_err);
if (!root) {
@@ -4617,6 +4621,58 @@ static int og_cmd_oglive_list(char *buffer_reply)
return -1;
}
+ oglive_array = json_object_get(root, "oglive");
+ if (!oglive_array || !json_is_array(oglive_array)) {
+ syslog(LOG_ERR, "Expected 'oglive' to be a JSON array\n");
+ json_decref(root);
+ return -1;
+ }
+
+ dbi = og_dbi_open(&ogconfig.db);
+ if (!dbi) {
+ syslog(LOG_ERR, "cannot open conection database (%s:%d)\n",
+ __func__, __LINE__);
+ json_decref(root);
+ return -1;
+ }
+
+ result = dbi_conn_queryf(dbi->conn,
+ "SELECT name, "
+ "DATE_FORMAT(creation_date, '%%a %%b %%d %%H:%%i:%%s %%Y') AS formatted_date "
+ "FROM oglive");
+
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ og_dbi_close(dbi);
+ json_decref(root);
+ return -1;
+ }
+
+ while (dbi_result_next_row(result) > 0) {
+ live_name = dbi_result_get_string(result, "name");
+ live_datetime = dbi_result_get_string(result, "formatted_date");
+
+ live_entry = json_object();
+ if (!live_entry) {
+ syslog(LOG_ERR, "Cannot allocate JSON object (%s:%d)\n",
+ __func__, __LINE__);
+ dbi_result_free(result);
+ og_dbi_close(dbi);
+ json_decref(root);
+ return -1;
+ }
+
+ json_object_set_new(live_entry, "name", json_string(live_name));
+ json_object_set_new(live_entry, "date", json_string(live_datetime));
+
+ json_array_append_new(oglive_array, live_entry);
+ }
+
+ dbi_result_free(result);
+ og_dbi_close(dbi);
+
if (json_dump_callback(root, og_json_dump_clients, &og_buffer, JSON_ENSURE_ASCII)) {
json_decref(root);
return -1;
@@ -5600,6 +5656,160 @@ static int og_cmd_oglive_set(json_t *element, struct og_msg_params *params)
return 0;
}
+static int og_cmd_oglive_add(json_t *element, struct og_msg_params *params)
+{
+ const char *oglive_str;
+ bool is_update = false;
+ int is_default_value;
+ const char *msglog;
+ struct og_dbi *dbi;
+ uint64_t flags = 0;
+ dbi_result result;
+ const char *key;
+ json_t *value;
+ int err = 0;
+
+ json_object_foreach(element, key, value) {
+ if (!strcmp(key, "name")) {
+ err = og_json_parse_string(value, &oglive_str);
+ flags |= OG_REST_PARAM_NAME;
+ } else {
+ err = -1;
+ }
+
+ if (err < 0)
+ return err;
+ }
+
+ if (!og_flags_validate(flags, OG_REST_PARAM_NAME))
+ return -1;
+
+ dbi = og_dbi_open(&ogconfig.db);
+ if (!dbi) {
+ syslog(LOG_ERR, "cannot open connection database (%s:%d)\n",
+ __func__, __LINE__);
+ return -1;
+ }
+
+ result = dbi_conn_queryf(dbi->conn,
+ "SELECT name FROM oglive WHERE name = '%s'",
+ oglive_str);
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ og_dbi_close(dbi);
+ return -1;
+ }
+
+ if (dbi_result_next_row(result)) {
+ is_update = true;
+ syslog(LOG_INFO, "Found oglive '%s' in database, updating entry", oglive_str);
+ }
+
+ dbi_result_free(result);
+
+ is_default_value = 0;
+
+ if (is_update)
+ result = dbi_conn_queryf(dbi->conn,
+ "UPDATE oglive SET creation_date = NOW(), is_default = %d "
+ "WHERE name = '%s'",
+ is_default_value,
+ oglive_str);
+ else
+ result = dbi_conn_queryf(dbi->conn,
+ "INSERT INTO oglive (name, creation_date, is_default) "
+ "VALUES ('%s', NOW(), %d)",
+ oglive_str,
+ is_default_value);
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ og_dbi_close(dbi);
+ return -1;
+ }
+
+ dbi_result_free(result);
+ og_dbi_close(dbi);
+
+ return 0;
+}
+
+static int og_cmd_oglive_delete(json_t *element, struct og_msg_params *params)
+{
+ const char *oglive_str;
+ const char *msglog;
+ struct og_dbi *dbi;
+ uint64_t flags = 0;
+ dbi_result result;
+ const char *key;
+ json_t *value;
+ int err = 0;
+
+ json_object_foreach(element, key, value) {
+ if (!strcmp(key, "name")) {
+ err = og_json_parse_string(value, &oglive_str);
+ flags |= OG_REST_PARAM_NAME;
+ } else {
+ err = -1;
+ }
+
+ if (err < 0)
+ return err;
+ }
+
+ if (!og_flags_validate(flags, OG_REST_PARAM_NAME))
+ return -1;
+
+ dbi = og_dbi_open(&ogconfig.db);
+ if (!dbi) {
+ syslog(LOG_ERR, "cannot open connection database (%s:%d)\n",
+ __func__, __LINE__);
+ return -1;
+ }
+
+ result = dbi_conn_queryf(dbi->conn,
+ "SELECT oglivedir FROM ordenadores WHERE oglivedir = '%s'",
+ oglive_str);
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ og_dbi_close(dbi);
+ return -1;
+ }
+
+ if (dbi_result_next_row(result)) {
+ syslog(LOG_ERR, "failed to delete oglive '%s', live in use by a client", oglive_str);
+ dbi_result_free(result);
+ og_dbi_close(dbi);
+ return -1;
+ }
+
+ dbi_result_free(result);
+
+ result = dbi_conn_queryf(dbi->conn,
+ "DELETE FROM oglive WHERE name = '%s'",
+ oglive_str);
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
+ __func__, __LINE__, msglog);
+ og_dbi_close(dbi);
+ return -1;
+ }
+
+ if (dbi_result_get_numrows_affected(result) == 0)
+ syslog(LOG_WARNING, "No live entries found with name %s in database ", oglive_str);
+
+ dbi_result_free(result);
+ og_dbi_close(dbi);
+
+ return 0;
+}
+
static int og_cmd_get_server_stats(char *buffer_reply)
{
json_t *root, *time_obj, *memory, *swap;
@@ -5786,6 +5996,8 @@ struct {
[OG_URI_EFI] = { "efi", },
[OG_URI_PART_SETUP] = { "setup", },
[OG_URI_OGLIVE_LIST] = { "oglive/list", },
+ [OG_URI_OGLIVE_ADD] = { "oglive/add", },
+ [OG_URI_OGLIVE_DELETE] = { "oglive/delete", },
[OG_URI_OGLIVE_SET] = { "oglive/set", },
[OG_URI_CENTER_ADD] = { "center/add", },
[OG_URI_CENTER_UPDATE] = { "center/update", },
@@ -6433,6 +6645,32 @@ int og_client_state_process_payload_rest(struct og_client *cli)
}
err = og_cmd_oglive_list(buf_reply);
+ } else if (!strncmp(cmd, "oglive/add", strlen("oglive/add"))) {
+ if (method != OG_METHOD_POST) {
+ err = og_client_method_not_found(cli);
+ goto err_process_rest_payload;
+ }
+
+ if (!root) {
+ syslog(LOG_ERR,
+ "command oglive add with no payload\n");
+ err = og_client_bad_request(cli);
+ goto err_process_rest_payload;
+ }
+ err = og_cmd_oglive_add(root, &params);
+ } else if (!strncmp(cmd, "oglive/delete", strlen("oglive/delete"))) {
+ if (method != OG_METHOD_POST) {
+ err = og_client_method_not_found(cli);
+ goto err_process_rest_payload;
+ }
+
+ if (!root) {
+ syslog(LOG_ERR,
+ "command oglive delete with no payload\n");
+ err = og_client_bad_request(cli);
+ goto err_process_rest_payload;
+ }
+ err = og_cmd_oglive_delete(root, &params);
} else if (!strncmp(cmd, "oglive/set", strlen("oglive/set"))) {
if (method != OG_METHOD_POST) {
err = og_client_method_not_found(cli);