summaryrefslogtreecommitdiff
path: root/src/config.c
blob: c4aecf02789b2f878665b43b75a10354050a3302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#define _POSIX_C_SOURCE 200809L
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <err.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <limits.h>
#include <libgen.h>
#include <jansson.h>
#include "string-util.h"
#include "config.h"
#include "log.h"

#define CONFIG_PATH_SUFFIX "/.config/ap_client/config"

struct config _config;
const struct config *config = (const struct config *)&_config;

char *get_config_path()
{
    char *home = getenv("HOME");
    if (!home || !*home) {
        return NULL;
    }
    char config_path[PATH_MAX];
    strlcpy(config_path, home, sizeof(config_path));
    strlcat(config_path, CONFIG_PATH_SUFFIX, sizeof(config_path));
    return strdup(config_path);
}

void config_load()
{
    json_t *root;
    json_error_t error;

    char *path = get_config_path();
    root = json_load_file(path, 0, &error);
    if (!root) {
        log_msg(LOG_ERROR, "load_config", "json parse error: line %d: %s",
                error.line, error.text);
        free(path);
        return;
    }
    free(path);

    if (!json_is_object(root)) {
        log_msg(LOG_ERROR, "load_config", "json root is not object");
        json_decref(root);
        return;
    }

    json_t *instance_url = json_object_get(root, "instance_url");
    if (json_is_string(instance_url)) {
        _config.instance_url = strdup(json_string_value(instance_url));
    }

    json_t *client_id = json_object_get(root, "client_id");
    if (json_is_string(client_id)) {
        _config.client_id = strdup(json_string_value(client_id));
    }

    json_t *client_secret = json_object_get(root, "client_secret");
    if (json_is_string(client_secret)) {
        _config.client_secret = strdup(json_string_value(client_secret));
    }

    json_t *access_token = json_object_get(root, "access_token");
    if (json_is_string(access_token)) {
        _config.access_token = strdup(json_string_value(access_token));
    }

    json_decref(root);
}

int config_save()
{
    json_t *root;
    json_error_t error;

    root = json_pack_ex(&error, 1, "{s:s, s:s, s:s, s:s}", "instance_url",
            _config.instance_url, "client_id", _config.client_id,
            "client_secret", _config.client_secret, "access_token",
            _config.access_token);

    if (!root) {
        log_msg(LOG_ERROR, "register_app", "json pack error: line %d: %s",
                error.line, error.text);
        return -1;
    }

    char *config_path = get_config_path();
    struct stat st;
    char *dir = dirname(config_path);
    if (stat(dir, &st)) {
        if (mkdir(dir, S_IRWXU)) {
            free(config_path);
            log_msg(LOG_ERROR, "load_config", "failed to create config dir");
            return -1;
        }
    }
    free(config_path);

    if (json_dump_file(root, get_config_path(), 0)) {
        log_msg(LOG_ERROR, "load_config", "unable to save json");
        json_decref(root);
        return -1;
    }

    json_decref(root);
    return 0;
}

void config_set_client_id(const char *cid)
{
    _config.client_id = strdup(cid);
}

void config_set_client_secret(const char *cs)
{
    _config.client_secret = strdup(cs);
}

void config_set_instance_url(const char *iu)
{
    printf("updating url: %s", iu);
    _config.instance_url = strdup(iu);
}

void config_set_access_token(const char *a)
{
    _config.access_token = strdup(a);
}

void config_cleanup()
{
    if (_config.client_id)
        free(_config.client_id);
    if (_config.client_secret)
        free(_config.client_secret);
    if (_config.instance_url)
        free(_config.instance_url);
    if (_config.access_token)
        free(_config.access_token);
}