forked from cee-studio/orca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreddit-client.c
78 lines (62 loc) · 1.85 KB
/
reddit-client.c
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
#include <string.h>
#include <errno.h>
#include "reddit.h"
#include "reddit-internal.h"
static void
_reddit_init(struct reddit *new_client)
{
new_client->adapter.p_client = new_client;
reddit_adapter_init(&new_client->adapter, &new_client->conf);
}
struct reddit*
reddit_init(
const char username[],
const char password[],
const char client_id[],
const char client_secret[])
{
struct reddit *new_client = calloc(1, sizeof *new_client);
logconf_setup(&new_client->conf, "REDDIT", NULL);
*new_client = (struct reddit){
.username = {
.start = (char*)username,
.size = cee_str_bounds_check(username, 128)
},
.password = {
.start = (char*)password,
.size = cee_str_bounds_check(password, 128)
},
.client_id = {
.start = (char*)client_id,
.size = cee_str_bounds_check(client_id, 128)
},
.client_secret = {
.start = (char*)client_secret,
.size = cee_str_bounds_check(client_secret, 128)
}
};
_reddit_init(new_client);
return new_client;
}
struct reddit*
reddit_config_init(const char config_file[])
{
struct reddit *new_client = calloc(1, sizeof *new_client);
FILE *fp = fopen(config_file, "rb");
VASSERT_S(fp != NULL, "Couldn't open '%s': %s", config_file, strerror(errno));
logconf_setup(&new_client->conf, "REDDIT", fp);
fclose(fp);
new_client->username = logconf_get_field(&new_client->conf, "reddit.username");
new_client->password = logconf_get_field(&new_client->conf, "reddit.password");
new_client->client_id = logconf_get_field(&new_client->conf, "reddit.client_id");
new_client->client_secret = logconf_get_field(&new_client->conf, "reddit.client_secret");
_reddit_init(new_client);
return new_client;
}
void
reddit_cleanup(struct reddit *client)
{
logconf_cleanup(&client->conf);
reddit_adapter_cleanup(&client->adapter);
free(client);
}