/[svn]/ircd-hybrid/src/conf/conf.c
ViewVC logotype

Contents of /ircd-hybrid/src/conf/conf.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 628 - (show annotations)
Mon May 29 20:04:32 2006 UTC (16 years, 6 months ago) by adx
File MIME type: text/x-chdr
File size: 8412 byte(s)
Added contrib module for character set recoding

Note it doesn't work at the moment because listen{} itself
is not yet modularised.  Will probably also need some modifications
to include iconv headers and libraries in the build process.

1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 * conf.c: Configuration manager.
4 *
5 * Copyright (C) 2003 by Piotr Nizynski, Advanced IRC Services Project
6 * Copyright (C) 2005 by the Hybrid Development Team.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * USA
22 *
23 * $Id: conf.c 69 2005-10-04 16:09:51Z adx $
24 */
25
26 #define IN_CONF_C
27 #include "stdinc.h"
28 #include "conf/conf.h"
29 #include "client.h"
30 #include "restart.h"
31 #include "send.h"
32
33 int conf_pass, conf_cold = YES;
34 struct ConfParserContext conf_curctx;
35 char conf_linebuf[CONF_BUFSIZE];
36
37 struct Callback *reset_conf = NULL;
38 struct Callback *verify_conf = NULL;
39 struct Callback *switch_conf_pass = NULL;
40
41 static dlink_list conf_section_list = {NULL, NULL, 0};
42
43 extern int yyparse(void);
44
45 /*
46 * init_conf()
47 *
48 * Initializes the configuration manager.
49 *
50 * inputs: none
51 * output: none
52 */
53 void
54 init_conf(void)
55 {
56 reset_conf = register_callback("reset_conf", NULL);
57 verify_conf = register_callback("verify_conf", NULL);
58 switch_conf_pass = register_callback("switch_conf_pass", NULL);
59
60 init_serverinfo();
61 init_admin();
62 init_logging();
63 init_class();
64 init_channel();
65 init_serverhide();
66 init_general();
67 #ifndef STATIC_MODULES
68 init_modules();
69 #endif
70 }
71
72 /*
73 * parse_error()
74 *
75 * Log a parse error message.
76 *
77 * inputs:
78 * fmt - error message format
79 * output: none
80 */
81 static void
82 do_parse_error(int fatal, const char *fmt, va_list args)
83 {
84 char *newbuf = stripws(conf_linebuf);
85 char msg[CONF_BUFSIZE];
86
87 vsnprintf(msg, CONF_BUFSIZE, fmt, args);
88
89 if (conf_pass != 0)
90 {
91 sendto_realops_flags(UMODE_ALL, L_ALL, "\"%s\", line %u: %s: %s",
92 conf_curctx.filename, conf_curctx.lineno, msg, newbuf);
93 ilog(fatal ? L_CRIT : L_WARN, "\"%s\", line %u: %s: %s",
94 conf_curctx.filename, conf_curctx.lineno, msg, newbuf);
95 }
96 else
97 {
98 sendto_realops_flags(UMODE_ALL, L_ALL, "Conf %s: %s",
99 fatal ? "FATAL" : "ERROR", msg);
100 ilog(fatal ? L_CRIT : L_WARN, "Conf %s: %s",
101 fatal ? "FATAL" : "ERROR", msg);
102 }
103 }
104
105 void
106 parse_error(const char *fmt, ...)
107 {
108 va_list args;
109
110 va_start(args, fmt);
111 do_parse_error(NO, fmt, args);
112 va_end(args);
113 }
114
115 void
116 parse_fatal(const char *fmt, ...)
117 {
118 va_list args;
119
120 va_start(args, fmt);
121 do_parse_error(YES, fmt, args);
122 va_end(args);
123
124 server_die("misconfigured server", NO);
125 }
126
127 /*
128 * yyerror()
129 *
130 * Log a parse error message if the current pass is 2.
131 *
132 * inputs:
133 * msg - error message
134 * output: none
135 */
136 void
137 _yyerror(const char *msg)
138 {
139 if (conf_pass == 2)
140 parse_error("%s", msg);
141 }
142
143 /*
144 * conf_yy_input()
145 *
146 * Loads a block of data from the conf file.
147 *
148 * inputs:
149 * buf - address of destination buffer
150 * siz - size of the buffer
151 * output: number of bytes actually read
152 */
153 int
154 conf_yy_input(char *buf, int siz)
155 {
156 return fbgets(buf, siz, conf_curctx.f) == NULL ? 0 : strlen(buf);
157 }
158
159 /*
160 * find_conf_section()
161 *
162 * Finds a conf block (section) structure.
163 *
164 * inputs:
165 * name - name of the block
166 * output: pointer to struct ConfSection or NULL if not found
167 */
168 struct ConfSection *
169 find_conf_section(const char *name)
170 {
171 dlink_node *ptr;
172 struct ConfSection *section;
173
174 DLINK_FOREACH(ptr, conf_section_list.head)
175 {
176 section = ptr->data;
177 if (!strcasecmp(section->name, name))
178 return section;
179 }
180
181 return NULL;
182 }
183
184 /*
185 * add_conf_section()
186 *
187 * Adds a conf block (section) structure.
188 *
189 * inputs:
190 * name - name of the block
191 * pass - pass when the section should be parsed (1 or 2)
192 * output: pointer to struct ConfSection or NULL if already exists
193 */
194 struct ConfSection *
195 add_conf_section(const char *name, int pass)
196 {
197 struct ConfSection *section;
198
199 if (find_conf_section(name) != NULL)
200 return NULL;
201
202 section = MyMalloc(sizeof(struct ConfSection));
203 section->name = name;
204 section->pass = pass;
205 dlinkAdd(section, &section->node, &conf_section_list);
206 return section;
207 }
208
209 /*
210 * delete_conf_section()
211 *
212 * Deletes a conf block (section) structure.
213 *
214 * inputs:
215 * section - pointer to ConfSection structure
216 * output: none
217 */
218 void
219 delete_conf_section(struct ConfSection *section)
220 {
221 dlink_node *ptr, *ptr_next;
222
223 DLINK_FOREACH_SAFE(ptr, ptr_next, section->fields.head)
224 {
225 dlinkDelete(ptr, &section->fields);
226 MyFree(ptr->data);
227 }
228
229 dlinkDelete(&section->node, &conf_section_list);
230 }
231
232 struct ConfField *
233 find_conf_field(struct ConfSection *section, char *name)
234 {
235 dlink_node *ptr;
236 struct ConfField *field;
237
238 if (section == NULL)
239 return NULL;
240 if (section->pass != conf_pass)
241 return NULL;
242
243 DLINK_FOREACH(ptr, section->fields.head)
244 {
245 field = ptr->data;
246 if (!strcasecmp(field->name, name))
247 return field;
248 }
249
250 parse_error("unknown field");
251 return NULL;
252 }
253
254 /*
255 * conf_assign()
256 *
257 * Called when a conf item (xxx = 'yyy'; form) is parsed.
258 *
259 * inputs:
260 * type - type of supplied value
261 * field - pointer to struct ConfField
262 * value - address of value data
263 * output: none
264 */
265 void
266 conf_assign(int type, struct ConfField *field, void *value)
267 {
268 static char *field_types[] =
269 {"NUMBER", "BOOLEAN", "TIME", "SIZE", "STRING", "LIST", "NLIST"};
270
271 if (field == NULL)
272 return;
273
274 if (type == field->type || (type == CT_NUMBER &&
275 (field->type == CT_TIME || field->type == CT_SIZE)))
276 {
277 if (field->handler != NULL)
278 field->handler(value, field->param);
279 }
280 else if (type == CT_NUMBER && field->type == CT_NLIST)
281 {
282 dlink_list list = {NULL, NULL, 0};
283 dlink_node node;
284
285 dlinkAdd((void *) (long) (*(int *) value), &node, &list);
286
287 if (field->handler != NULL)
288 field->handler(&list, field->param);
289 }
290 else
291 parse_error("type mismatch, expected %s", field_types[type]);
292 }
293
294 /*
295 * conf_assign_*()
296 *
297 * Simple field handlers which just write a variable.
298 *
299 * inputs:
300 * value - address of data
301 * var - where to write it
302 * output: none
303 */
304 void
305 conf_assign_bool(void *value, void *var)
306 {
307 *(char *) var = *(int *) value;
308 }
309
310 void
311 conf_assign_number(void *value, void *var)
312 {
313 *(int *) var = *(int *) value;
314 }
315
316 void
317 conf_assign_string(void *value, void *var)
318 {
319 MyFree(*(char **) var);
320 DupString(*(char **) var, (char *) value);
321 }
322
323 /*
324 * add_conf_field()
325 *
326 * Adds a field of type number to the given section.
327 *
328 * inputs:
329 * section - pointer to ConfSection structure
330 * name - name of the field
331 * type - type of the field as defined in conf.h
332 * handler - function to be called when an assignment
333 * is encountered (one argument: info)
334 * output: none
335 */
336 struct ConfField *
337 add_conf_field(struct ConfSection *section, const char *name, int type,
338 CONFF_HANDLER *handler, void *param)
339 {
340 struct ConfField *field = MyMalloc(sizeof(struct ConfField));
341
342 if (handler == NULL)
343 switch (type)
344 {
345 case CT_NUMBER:
346 case CT_TIME:
347 case CT_SIZE:
348 handler = conf_assign_number;
349 break;
350 case CT_BOOL:
351 handler = conf_assign_bool;
352 break;
353 case CT_STRING:
354 handler = conf_assign_string;
355 break;
356 default:
357 assert(0);
358 }
359
360 field->name = name;
361 field->type = type;
362 field->handler = handler;
363 field->param = param;
364 dlinkAdd(field, &field->node, &section->fields);
365 return field;
366 }
367
368 /*
369 * delete_conf_field()
370 *
371 * Deletes a field from a section. Will crash if called incorrectly.
372 *
373 * inputs:
374 * section - pointer to ConfSection structure
375 * field - pointer to ConfField to delete
376 * output: none
377 */
378 void
379 delete_conf_field(struct ConfSection *section, struct ConfField *field)
380 {
381 dlinkDelete(&field->node, &section->fields);
382 MyFree(field);
383 }
384
385 struct ConfItem *
386 make_conf_item2(ConfType type)
387 {
388 return NULL;
389 }

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28