diff options
| author | 2022-09-19 22:41:30 +0200 | |
|---|---|---|
| committer | 2022-09-19 22:41:30 +0200 | |
| commit | c88d4f1e152859254d28bcd6cd7ad7798f0782c4 (patch) | |
| tree | 9baffa3bda31577cc37827d872f586ea05ca382a /snac.c | |
| parent | Unified error strings. (diff) | |
| download | snac2-c88d4f1e152859254d28bcd6cd7ad7798f0782c4.tar.gz snac2-c88d4f1e152859254d28bcd6cd7ad7798f0782c4.tar.xz snac2-c88d4f1e152859254d28bcd6cd7ad7798f0782c4.zip | |
[data.c] new file.
Diffstat (limited to 'snac.c')
| -rw-r--r-- | snac.c | 124 |
1 files changed, 0 insertions, 124 deletions
| @@ -66,60 +66,6 @@ void srv_debug(int level, d_char *str) | |||
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | 68 | ||
| 69 | int srv_open(char *basedir) | ||
| 70 | /* opens a server */ | ||
| 71 | { | ||
| 72 | int ret = 0; | ||
| 73 | xs *cfg_file = NULL; | ||
| 74 | FILE *f; | ||
| 75 | |||
| 76 | srv_basedir = xs_str_new(basedir); | ||
| 77 | |||
| 78 | cfg_file = xs_fmt("%s/server.json", basedir); | ||
| 79 | |||
| 80 | if ((f = fopen(cfg_file, "r")) == NULL) | ||
| 81 | srv_log(xs_fmt("error opening '%s'", cfg_file)); | ||
| 82 | else { | ||
| 83 | xs *cfg_data; | ||
| 84 | |||
| 85 | /* read full config file */ | ||
| 86 | cfg_data = xs_readall(f); | ||
| 87 | |||
| 88 | /* parse */ | ||
| 89 | srv_config = xs_json_loads(cfg_data); | ||
| 90 | |||
| 91 | if (srv_config == NULL) | ||
| 92 | srv_log(xs_fmt("cannot parse '%s'", cfg_file)); | ||
| 93 | else { | ||
| 94 | char *host; | ||
| 95 | char *prefix; | ||
| 96 | char *dbglvl; | ||
| 97 | |||
| 98 | host = xs_dict_get(srv_config, "host"); | ||
| 99 | prefix = xs_dict_get(srv_config, "prefix"); | ||
| 100 | dbglvl = xs_dict_get(srv_config, "dbglevel"); | ||
| 101 | |||
| 102 | if (host == NULL || prefix == NULL) | ||
| 103 | srv_log(xs_str_new("cannot get server data")); | ||
| 104 | else { | ||
| 105 | srv_baseurl = xs_fmt("https://%s%s", host, prefix); | ||
| 106 | |||
| 107 | dbglevel = (int) xs_number_get(dbglvl); | ||
| 108 | |||
| 109 | if ((dbglvl = getenv("DEBUG")) != NULL) { | ||
| 110 | dbglevel = atoi(dbglvl); | ||
| 111 | srv_log(xs_fmt("DEBUG level set to %d from environment", dbglevel)); | ||
| 112 | } | ||
| 113 | |||
| 114 | ret = 1; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | return ret; | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | int validate_uid(char *uid) | 69 | int validate_uid(char *uid) |
| 124 | /* returns if uid is a valid identifier */ | 70 | /* returns if uid is a valid identifier */ |
| 125 | { | 71 | { |
| @@ -134,76 +80,6 @@ int validate_uid(char *uid) | |||
| 134 | } | 80 | } |
| 135 | 81 | ||
| 136 | 82 | ||
| 137 | void snac_free(snac *snac) | ||
| 138 | /* frees a user snac */ | ||
| 139 | { | ||
| 140 | free(snac->uid); | ||
| 141 | free(snac->basedir); | ||
| 142 | free(snac->config); | ||
| 143 | free(snac->key); | ||
| 144 | free(snac->actor); | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | int snac_open(snac *snac, char *uid) | ||
| 149 | /* opens a user */ | ||
| 150 | { | ||
| 151 | int ret = 0; | ||
| 152 | |||
| 153 | memset(snac, '\0', sizeof(struct _snac)); | ||
| 154 | |||
| 155 | if (validate_uid(uid)) { | ||
| 156 | xs *cfg_file; | ||
| 157 | FILE *f; | ||
| 158 | |||
| 159 | snac->uid = xs_str_new(uid); | ||
| 160 | |||
| 161 | snac->basedir = xs_fmt("%s/user/%s", srv_basedir, uid); | ||
| 162 | |||
| 163 | cfg_file = xs_fmt("%s/user.json", snac->basedir); | ||
| 164 | |||
| 165 | if ((f = fopen(cfg_file, "r")) != NULL) { | ||
| 166 | xs *cfg_data; | ||
| 167 | |||
| 168 | /* read full config file */ | ||
| 169 | cfg_data = xs_readall(f); | ||
| 170 | fclose(f); | ||
| 171 | |||
| 172 | if ((snac->config = xs_json_loads(cfg_data)) != NULL) { | ||
| 173 | xs *key_file = xs_fmt("%s/key.json", snac->basedir); | ||
| 174 | |||
| 175 | if ((f = fopen(key_file, "r")) != NULL) { | ||
| 176 | xs *key_data; | ||
| 177 | |||
| 178 | key_data = xs_readall(f); | ||
| 179 | fclose(f); | ||
| 180 | |||
| 181 | if ((snac->key = xs_json_loads(key_data)) != NULL) { | ||
| 182 | snac->actor = xs_fmt("%s/%s", srv_baseurl, uid); | ||
| 183 | ret = 1; | ||
| 184 | } | ||
| 185 | else | ||
| 186 | srv_log(xs_fmt("cannot parse '%s'", key_file)); | ||
| 187 | } | ||
| 188 | else | ||
| 189 | srv_log(xs_fmt("error opening '%s'", key_file)); | ||
| 190 | } | ||
| 191 | else | ||
| 192 | srv_log(xs_fmt("cannot parse '%s'", cfg_file)); | ||
| 193 | } | ||
| 194 | else | ||
| 195 | srv_log(xs_fmt("error opening '%s'", cfg_file)); | ||
| 196 | } | ||
| 197 | else | ||
| 198 | srv_log(xs_fmt("invalid user '%s'", uid)); | ||
| 199 | |||
| 200 | if (!ret) | ||
| 201 | snac_free(snac); | ||
| 202 | |||
| 203 | return ret; | ||
| 204 | } | ||
| 205 | |||
| 206 | |||
| 207 | void snac_debug(snac *snac, int level, d_char *str) | 83 | void snac_debug(snac *snac, int level, d_char *str) |
| 208 | /* prints a user debugging information */ | 84 | /* prints a user debugging information */ |
| 209 | { | 85 | { |