-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.c
125 lines (107 loc) · 3.41 KB
/
config.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
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
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define CONFIG_FILE "neurobot.conf"
void config_free(struct irc_t * irc)
{
struct ajoin_channel_t * iterator;
LIST_FOREACH(iterator, &irc->ajoin_channels_head, list) {
free(iterator->channel_name);
free (iterator);
}
free(irc->nickname);
free(irc->password);
free(irc->admin);
free(irc->hostname);
free(irc->port);
}
void config_load(struct irc_t * irc)
{
FILE * file;
char buf[1024];
char * token;
debug("Reading configuration file\n");
file = fopen(CONFIG_FILE, "r");
if (!file)
{
debug("No config file found, using defaults.\n");
irc->nickname = strdup(NICKNAME);
irc->password = strdup(PASSWORD);
irc->admin = strdup(ADMIN);
irc->hostname = strdup(HOST);
irc->port = strdup(PORT);
return;
}
while (fgets(buf, 1024, file))
{
if (buf[0] == '#')
continue;
token = strtok(buf, "=");
if (token == NULL)
continue;
if (!strcmp(token, "nick"))
{
token = strtok(NULL, " \r\n");
if (token) irc->nickname = strdup(token);
else irc->nickname = strdup(NICKNAME);
debug("nickname : %s\n", irc->nickname);
}
else if (!strcmp(token, "pass"))
{
token = strtok(NULL, " \r\n");
if (token) irc->password = strdup(token);
else irc->password = strdup(PASSWORD);
debug("password : %s\n", irc->password);
}
else if (!strcmp(token, "admin"))
{
token = strtok(NULL, " \r\n");
if (token) irc->admin = strdup(token);
else irc->admin = strdup(ADMIN);
debug("admin : %s\n", irc->admin);
}
else if (!strcmp(token, "server"))
{
token = strtok(NULL, " \r\n");
if (token) irc->hostname = strdup(token);
else irc->hostname = strdup(HOST);
debug("host_name : %s\n", irc->hostname);
}
else if (!strcmp(token, "port"))
{
token = strtok(NULL, " \r\n");
if (token) irc->port = strdup(token);
else irc->port = strdup(PORT);
debug("port : %s\n", irc->port);
}
else if (!strcmp(token, "channels"))
{
char print_buffer[4096];
LIST_INIT(&irc->ajoin_channels_head);
while ((token = strtok(NULL, ",\n")) != NULL) {
struct ajoin_channel_t * ajoin_channel = malloc(sizeof *ajoin_channel);
ajoin_channel->channel_name = strdup(token);
LIST_INSERT_HEAD(&irc->ajoin_channels_head, ajoin_channel, list);
}
sprintf(print_buffer, "Autojoin channels: ");
struct ajoin_channel_t * iterator;
LIST_FOREACH(iterator, &irc->ajoin_channels_head, list) {
sprintf(print_buffer + strlen(print_buffer), "%s ", iterator->channel_name);
}
sprintf(print_buffer + strlen(print_buffer), "\n");
debug("%s", print_buffer);
}
}
fclose(file);
}
#ifdef TEST_CONFIG
int main(int argc, char *argv[])
{
struct irc_t irc;
fprintf(stderr, "*** Test for config.c\n");
config_load(&irc);
fprintf(stderr, "*** OK?\n");
return 0;
}
#endif