]> git.sommitrealweird.co.uk Git - onak.git/blob - onak-conf.c
Escape colons and similar in MRHKP output
[onak.git] / onak-conf.c
1 /*
2  * onak-conf.c - Routines related to runtime config.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2002 Project Purple
7  */
8
9 #include "config.h"
10
11 #include <ctype.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "ll.h"
17 #include "log.h"
18 #include "onak-conf.h"
19
20 extern struct dbfuncs DBFUNCS;
21
22 /*
23  *      config - Runtime configuration for onak.
24  *
25  *      This is the default config; normally overridden with values from the
26  *      config file.
27  */
28 struct onak_config config = {
29         128,                    /* maxkeys */
30         NULL,                   /* thissite */
31         NULL,                   /* adminemail */
32         NULL,                   /* mta */
33         NULL,                   /* syncsites */
34         NULL,                   /* logfile */
35
36         /*
37          * Options for directory backends.
38          */
39         NULL,                   /* db_dir */
40
41         /*
42          * Options for the Postgres backend.
43          */
44         NULL,                   /* pg_dbhost */
45         NULL,                   /* pg_dbname */
46         NULL,                   /* pg_dbuser */
47         NULL,                   /* pg_dbpass */
48
49         /*
50          * Options for dynamic backends.
51          */
52         NULL,                   /* db_backend */
53         NULL,                   /* backends_dir */
54
55         &DBFUNCS,               /* Default dbfuncs struct */
56 };
57
58 void readconfig(const char *configfile) {
59         FILE *conffile;
60         char  curline[1024];
61         int   i;
62
63         curline[1023] = 0;
64         if (configfile == NULL) {
65                 conffile = fopen(CONFIGFILE, "r");
66         } else {
67                 conffile = fopen(configfile, "r");
68         }
69         if (conffile != NULL) {
70                 fgets(curline, 1023, conffile);
71
72                 while (!feof(conffile)) {
73                         for (i = strlen(curline) - 1;
74                                         i >= 0 && isspace(curline[i]);
75                                         i--) {
76                                 curline[i] = 0;
77                         }
78
79                 if (curline[0] == '#' || curline[0] == 0) {
80                         /*
81                          * Comment line, ignore.
82                          */
83                 } else if (!strncmp("db_dir ", curline, 7)) {
84                         config.db_dir = strdup(&curline[7]);
85                 } else if (!strncmp("debug ", curline, 6)) {
86                         /*
87                          * Not supported yet; ignore for compatibility with
88                          * pksd.
89                          */
90                 } else if (!strncmp("default_language ", curline, 17)) {
91                         /*
92                          * Not supported yet; ignore for compatibility with
93                          * pksd.
94                          */
95                 } else if (!strncmp("mail_delivery_client ", curline, 21)) {
96                         config.mta = strdup(&curline[21]);
97                 } else if (!strncmp("maintainer_email ", curline, 17)) {
98                         config.adminemail = strdup(&curline[17]);
99                 } else if (!strncmp("mail_intro_file ", curline, 16)) {
100                         /*
101                          * Not supported yet; ignore for compatibility with
102                          * pksd.
103                          */
104                 } else if (!strncmp("help_dir ", curline, 9)) {
105                         /*
106                          * Not supported yet; ignore for compatibility with
107                          * pksd.
108                          */
109                 } else if (!strncmp("max_last ", curline, 9)) {
110                         /*
111                          * Not supported yet; ignore for compatibility with
112                          * pksd.
113                          */
114                 } else if (!strncmp("max_reply_keys ", curline, 15)) {
115                         config.maxkeys = atoi(&curline[15]);
116                 } else if (!strncmp("pg_dbhost ", curline, 10)) {
117                         config.pg_dbhost = strdup(&curline[10]);
118                 } else if (!strncmp("pg_dbname ", curline, 10)) {
119                         config.pg_dbname = strdup(&curline[10]);
120                 } else if (!strncmp("pg_dbuser ", curline, 10)) {
121                         config.pg_dbuser = strdup(&curline[10]);
122                 } else if (!strncmp("pg_dbpass ", curline, 10)) {
123                         config.pg_dbpass = strdup(&curline[10]);
124                 } else if (!strncmp("syncsite ", curline, 9)) {
125                         config.syncsites =
126                                 lladd(config.syncsites, strdup(&curline[9]));
127                 } else if (!strncmp("logfile ", curline, 8)) {
128                         config.logfile = strdup(&curline[8]);
129                 } else if (!strncmp("loglevel ", curline, 9)) {
130                         setlogthreshold(atoi(&curline[9]));
131                 } else if (!strncmp("this_site ", curline, 10)) {
132                         config.thissite = strdup(&curline[10]);
133                 } else if (!strncmp("socket_name ", curline, 12) ||
134                                 !strncmp("pks_bin_dir ", curline, 12) ||
135                                 !strncmp("mail_dir ", curline, 9) ||
136                                 !strncmp("www_port ", curline, 9)) {
137                         /*
138                          * Not applicable; ignored for compatibility with pksd.
139                          */
140                 } else if (!strncmp("db_backend ", curline, 11)) {
141                         config.db_backend = strdup(&curline[11]);
142                 } else if (!strncmp("backends_dir ", curline, 13)) {
143                         config.backends_dir = strdup(&curline[13]);
144                 } else {
145                         logthing(LOGTHING_ERROR,
146                                 "Unknown config line: %s", curline);
147                 }
148
149                         fgets(curline, 1023, conffile);
150                 }
151                 fclose(conffile);
152         } else {
153                 logthing(LOGTHING_NOTICE,
154                                 "Couldn't open config file; using defaults.");
155         }
156 }
157
158 void cleanupconfig(void) {
159         if (config.thissite != NULL) {
160                 free(config.thissite);
161                 config.thissite = NULL;
162         }
163         if (config.adminemail != NULL) {
164                 free(config.adminemail);
165                 config.adminemail = NULL;
166         }
167         if (config.mta != NULL) {
168                 free(config.mta);
169                 config.mta = NULL;
170         }
171         if (config.db_dir != NULL) {
172                 free(config.db_dir);
173                 config.db_dir = NULL;
174         }
175         if (config.pg_dbhost != NULL) {
176                 free(config.pg_dbhost);
177                 config.pg_dbhost = NULL;
178         }
179         if (config.pg_dbname != NULL) {
180                 free(config.pg_dbname);
181                 config.pg_dbname = NULL;
182         }
183         if (config.pg_dbuser != NULL) {
184                 free(config.pg_dbuser);
185                 config.pg_dbuser = NULL;
186         }
187         if (config.pg_dbpass != NULL) {
188                 free(config.pg_dbpass);
189                 config.pg_dbpass = NULL;
190         }
191         if (config.syncsites != NULL) {
192                 llfree(config.syncsites, free);
193                 config.syncsites = NULL;
194         }
195         if (config.logfile != NULL) {
196                 free(config.logfile);
197                 config.logfile = NULL;
198         }
199         if (config.db_backend != NULL) {
200                 free(config.db_backend);
201                 config.db_backend = NULL;
202         }
203         if (config.backends_dir != NULL) {
204                 free(config.backends_dir);
205                 config.backends_dir = NULL;
206         }
207 }