2 * IRC - Internet Relay Chat, ircd/features.c
3 * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 1, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "ircd_features.h"
25 #include "ircd_alloc.h"
27 #include "ircd_reply.h"
28 #include "ircd_string.h"
39 #include "sys.h" /* FALSE bleah */
45 /* List of log output types that can be set */
46 static struct LogTypes {
48 int (*set)(const char *, const char *);
49 char *(*get)(const char *);
51 { "FILE", log_set_file, log_get_file },
52 { "FACILITY", log_set_facility, log_get_facility },
53 { "SNOMASK", log_set_snomask, log_get_snomask },
54 { "LEVEL", log_set_level, log_get_level },
58 /* Look up a struct LogType given the type string */
59 static struct LogTypes *
60 feature_log_desc(struct Client* from, const char *type)
66 for (i = 0; logTypes[i].type; i++) /* find appropriate descriptor */
67 if (!ircd_strcmp(type, logTypes[i].type))
70 Debug((DEBUG_ERROR, "Unknown log feature type \"%s\"", type));
71 if (from) /* send an error; if from is NULL, called from conf parser */
72 send_reply(from, ERR_BADLOGTYPE, type);
74 log_write(LS_CONFIG, L_ERROR, 0, "Unknown log feature type \"%s\"", type);
76 return 0; /* not found */
79 /* Set the value of a log output type for a log subsystem */
81 feature_log_set(struct Client* from, const char* const* fields, int count)
83 struct LogTypes *desc;
86 if (count < 2) { /* set default facility */
87 if (log_set_default(count < 1 ? 0 : fields[0])) {
88 assert(count >= 1); /* should always accept default */
90 if (from) /* send an error */
91 send_reply(from, ERR_BADLOGVALUE, fields[0]);
93 log_write(LS_CONFIG, L_ERROR, 0,
94 "Bad value \"%s\" for default facility", fields[0]);
96 return count < 1 ? -1 : 1; /* tell feature to set or clear mark */
97 } else if (!(subsys = log_canon(fields[0]))) { /* no such subsystem */
98 if (from) /* send an error */
99 send_reply(from, ERR_BADLOGSYS, fields[0]);
101 log_write(LS_CONFIG, L_ERROR, 0,
102 "No such logging subsystem \"%s\"", fields[0]);
103 } else if ((desc = feature_log_desc(from, fields[1]))) { /* set value */
104 if ((*desc->set)(fields[0], count < 3 ? 0 : fields[2])) {
105 assert(count >= 3); /* should always accept default */
107 if (from) /* send an error */
108 send_reply(from, ERR_BADLOGVALUE, fields[2]);
110 log_write(LS_CONFIG, L_ERROR, 0,
111 "Bad value \"%s\" for log type %s (subsystem %s)",
112 fields[2], desc->type, subsys);
119 /* reset a log type for a subsystem to its default value */
121 feature_log_reset(struct Client* from, const char* const* fields, int count)
123 struct LogTypes *desc;
126 assert(0 != from); /* Never called by the .conf parser */
128 if (count < 1) { /* reset default facility */
130 return -1; /* unmark this entry */
131 } else if (count < 2)
132 need_more_params(from, "RESET");
133 else if (!(subsys = log_canon(fields[0]))) /* no such subsystem */
134 send_reply(from, ERR_BADLOGSYS, fields[0]);
135 else if ((desc = feature_log_desc(from, fields[1]))) /* reset value */
136 (*desc->set)(fields[0], 0); /* default should always be accepted */
141 /* report the value of a log setting */
143 feature_log_get(struct Client* from, const char* const* fields, int count)
145 struct LogTypes *desc;
146 char *value, *subsys;
148 assert(0 != from); /* never called by .conf parser */
150 if (count < 1) /* return default facility */
151 send_reply(from, SND_EXPLICIT | RPL_FEATURE, ":Log facility: %s",
154 need_more_params(from, "GET");
155 else if (!(subsys = log_canon(fields[0]))) { /* no such subsystem */
156 send_reply(from, ERR_BADLOGSYS, fields[0]);
157 } else if ((desc = feature_log_desc(from, fields[1]))) {
158 if ((value = (*desc->get)(fields[0]))) /* send along value */
159 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
160 ":Log %s for subsystem %s: %s", desc->type, subsys,
161 (*desc->get)(subsys));
163 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
164 ":No log %s is set for subsystem %s", desc->type, subsys);
168 /* sets a feature to the given value */
169 typedef int (*feat_set_call)(struct Client*, const char* const*, int);
170 /* gets the value of a feature */
171 typedef void (*feat_get_call)(struct Client*, const char* const*, int);
172 /* unmarks all sub-feature values prior to reading .conf */
173 typedef void (*feat_unmark_call)(void);
174 /* resets to defaults all currently unmarked values */
175 typedef void (*feat_mark_call)(int);
176 /* reports features as a /stats f list */
177 typedef void (*feat_report_call)(struct Client*, int);
179 #define FEAT_NONE 0x0000 /* no value */
180 #define FEAT_INT 0x0001 /* set if entry contains an integer value */
181 #define FEAT_BOOL 0x0002 /* set if entry contains a boolean value */
182 #define FEAT_STR 0x0003 /* set if entry contains a string value */
183 #define FEAT_MASK 0x000f /* possible value types */
185 #define FEAT_MARK 0x0010 /* set if entry has been changed */
186 #define FEAT_NULL 0x0020 /* NULL string is permitted */
187 #define FEAT_CASE 0x0040 /* string is case-sensitive */
189 #define FEAT_OPER 0x0100 /* set to display only to opers */
190 #define FEAT_MYOPER 0x0200 /* set to display only to local opers */
192 static struct FeatureDesc {
193 enum Feature feat; /* feature identifier */
194 char* type; /* string describing type */
195 unsigned int flags; /* flags for feature */
196 int v_int; /* integer value */
197 int def_int; /* default value */
198 char* v_str; /* string value */
199 char* def_str; /* default value */
200 feat_set_call set; /* set feature values */
201 feat_set_call reset; /* reset feature values to defaults */
202 feat_get_call get; /* get feature values */
203 feat_unmark_call unmark; /* unmark all feature change values */
204 feat_mark_call mark; /* reset to defaults all unchanged features */
205 feat_report_call report; /* report feature values */
207 #define F_N(type, flags, set, reset, get, unmark, mark, report) \
208 { FEAT_ ## type, #type, FEAT_NONE | (flags), 0, 0, 0, 0, \
209 (set), (reset), (get), (unmark), (mark), (report) }
210 #define F_I(type, flags, v_int) \
211 { FEAT_ ## type, #type, FEAT_INT | (flags), 0, (v_int), 0, 0, \
213 #define F_B(type, flags, v_int) \
214 { FEAT_ ## type, #type, FEAT_BOOL | (flags), 0, (v_int), 0, 0, \
216 #define F_S(type, flags, v_str) \
217 { FEAT_ ## type, #type, FEAT_STR | (flags), 0, 0, 0, (v_str), \
220 F_N(LOG, FEAT_MYOPER, feature_log_set, feature_log_reset, feature_log_get,
221 log_feature_unmark, log_feature_mark, log_feature_report),
223 /* Networking features */
224 F_I(TOS_SERVER, 0, 0x08),
225 F_I(TOS_CLIENT, 0, 0x08),
227 /* features that affect all operators */
228 F_B(OPER_NO_CHAN_LIMIT, 0, 1),
229 F_B(OPER_MODE_LCHAN, 0, 1),
230 F_B(OPER_WALK_THROUGH_LMODES, 0, 0),
231 F_B(NO_OPER_DEOP_LCHAN, 0, 0),
232 F_B(SHOW_INVISIBLE_USERS, 0, 1),
233 F_B(SHOW_ALL_INVISIBLE_USERS, 0, 1),
234 F_B(UNLIMIT_OPER_QUERY, 0, 0),
235 F_B(LOCAL_KILL_ONLY, 0, 0),
236 F_B(CONFIG_OPERCMDS, 0, 1), /* XXX change default before release */
238 /* features that affect global opers on this server */
239 F_B(OPER_KILL, 0, 1),
240 F_B(OPER_REHASH, 0, 1),
241 F_B(OPER_RESTART, 0, 1),
243 F_B(OPER_GLINE, 0, 1),
244 F_B(OPER_LGLINE, 0, 1),
245 F_B(OPER_JUPE, 0, 1),
246 F_B(OPER_LJUPE, 0, 1),
247 F_B(OPER_OPMODE, 0, 1),
248 F_B(OPER_LOPMODE, 0, 1),
249 F_B(OPER_BADCHAN, 0, 0),
250 F_B(OPER_LBADCHAN, 0, 0),
252 F_B(OPERS_SEE_IN_SECRET_CHANNELS, 0, 1),
254 /* features that affect local opers on this server */
255 F_B(LOCOP_KILL, 0, 0),
256 F_B(LOCOP_REHASH, 0, 1),
257 F_B(LOCOP_RESTART, 0, 0),
258 F_B(LOCOP_DIE, 0, 0),
259 F_B(LOCOP_LGLINE, 0, 1),
260 F_B(LOCOP_LJUPE, 0, 1),
261 F_B(LOCOP_LOPMODE, 0, 1),
262 F_B(LOCOP_LBADCHAN, 0, 0),
263 F_B(LOCOP_SET, 0, 0),
264 F_B(LOCOP_SEE_IN_SECRET_CHANNELS, 0, 0),
270 { FEAT_LAST_F, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
273 /* Given a feature's identifier, look up the feature descriptor */
274 static struct FeatureDesc *
275 feature_desc(struct Client* from, const char *feature)
279 assert(0 != feature);
281 for (i = 0; features[i].type; i++) /* find appropriate descriptor */
282 if (!ircd_strcmp(feature, features[i].type))
285 Debug((DEBUG_ERROR, "Unknown feature \"%s\"", feature));
286 if (from) /* report an error */
287 send_reply(from, ERR_NOFEATURE, feature);
289 log_write(LS_CONFIG, L_ERROR, 0, "Unknown feature \"%s\"", feature);
291 return 0; /* not found */
294 /* Given a feature vector string, set the value of a feature */
296 feature_set(struct Client* from, const char* const* fields, int count)
299 struct FeatureDesc *feat;
301 if (from && !HasPriv(from, PRIV_SET))
302 return send_reply(from, ERR_NOPRIVILEGES);
305 if (from) /* report an error in the number of arguments */
306 need_more_params(from, "SET");
308 log_write(LS_CONFIG, L_ERROR, 0, "Not enough fields in F line");
309 } else if ((feat = feature_desc(from, fields[0]))) { /* find feature */
310 switch (feat->flags & FEAT_MASK) {
312 if (feat->set && (i = (*feat->set)(from, fields + 1, count - 1))) {
313 if (i > 0) /* call the set callback and do marking */
314 feat->flags |= FEAT_MARK;
316 feat->flags &= ~FEAT_MARK;
320 case FEAT_INT: /* an integer value */
321 if (count < 2) { /* reset value */
322 feat->v_int = feat->def_int;
323 feat->flags &= ~FEAT_MARK;
324 } else { /* ok, figure out the value and whether to mark it */
325 feat->v_int = atoi(fields[1]);
326 if (feat->v_int == feat->def_int)
327 feat->flags &= ~FEAT_MARK;
329 feat->flags |= FEAT_MARK;
333 case FEAT_BOOL: /* it's a boolean value--true or false */
334 if (count < 2) { /* reset value */
335 feat->v_int = feat->def_int;
336 feat->flags &= ~FEAT_MARK;
337 } else { /* figure out the value and whether to mark it */
338 if (!ircd_strncmp(fields[1], "TRUE", strlen(fields[1])) ||
339 !ircd_strncmp(fields[1], "YES", strlen(fields[1])) ||
340 (strlen(fields[1]) >= 2 &&
341 !ircd_strncmp(fields[1], "ON", strlen(fields[1]))))
343 else if (!ircd_strncmp(fields[1], "FALSE", strlen(fields[1])) ||
344 !ircd_strncmp(fields[1], "NO", strlen(fields[1])) ||
345 (strlen(fields[1]) >= 2 &&
346 !ircd_strncmp(fields[1], "OFF", strlen(fields[1]))))
348 else if (from) /* report an error... */
349 return send_reply(from, ERR_BADFEATVALUE, fields[1], feat->type);
351 log_write(LS_CONFIG, L_ERROR, 0, "Bad value \"%s\" for feature %s",
352 fields[1], feat->type);
356 if (feat->v_int == feat->def_int) /* figure out whether to mark it */
357 feat->flags &= ~FEAT_MARK;
359 feat->flags |= FEAT_MARK;
363 case FEAT_STR: /* it's a string value */
365 !(feat->flags & FEAT_CASE ? strcmp(fields[1], feat->def_str) :
366 ircd_strcmp(fields[1], feat->def_str))) { /* reset to default */
367 if (feat->v_str && feat->v_str != feat->def_str)
368 MyFree(feat->v_str); /* free old value */
369 feat->v_str = feat->def_str; /* very special... */
371 feat->flags &= ~FEAT_MARK; /* unmark it */
373 if (!*fields[1]) { /* empty string translates to NULL */
374 if (feat->flags & FEAT_NULL) { /* permitted? */
375 if (feat->v_str && feat->v_str != feat->def_str)
376 MyFree(feat->v_str); /* free old value */
377 feat->v_str = 0; /* set it to NULL */
378 } else if (from) /* hmmm...not permitted; report error */
379 return send_reply(from, ERR_BADFEATVALUE, "NULL", feat->type);
381 log_write(LS_CONFIG, L_ERROR, 0,
382 "Bad value \"NULL\" for feature %s", feat->type);
385 } else if ((feat->flags & FEAT_CASE ?
386 strcmp(fields[1], feat->v_str) :
387 ircd_strcmp(fields[1], feat->v_str))) { /* new value */
388 if (feat->v_str && feat->v_str != feat->def_str)
389 MyFree(feat->v_str); /* free old value */
390 DupString(feat->v_str, fields[1]); /* store new value */
393 feat->flags |= FEAT_MARK; /* mark it as having been touched */
402 /* reset a feature to its default values */
404 feature_reset(struct Client* from, const char* const* fields, int count)
407 struct FeatureDesc *feat;
411 if (!HasPriv(from, PRIV_SET))
412 return send_reply(from, ERR_NOPRIVILEGES);
414 if (count < 1) /* check arguments */
415 need_more_params(from, "RESET");
416 else if ((feat = feature_desc(from, fields[0]))) { /* get descriptor */
417 switch (feat->flags & FEAT_MASK) {
418 case FEAT_NONE: /* None... */
419 if (feat->reset && (i = (*feat->reset)(from, fields + 1, count - 1))) {
420 if (i > 0) /* call reset callback and parse mark return */
421 feat->flags |= FEAT_MARK;
423 feat->flags &= ~FEAT_MARK;
427 case FEAT_INT: /* Integer... */
428 case FEAT_BOOL: /* Boolean... */
429 feat->v_int = feat->def_int; /* set the default */
430 feat->flags &= ~FEAT_MARK; /* unmark it */
433 case FEAT_STR: /* string! */
434 if (feat->v_str && feat->v_str != feat->def_str)
435 MyFree(feat->v_str); /* free old value */
436 feat->v_str = feat->def_str; /* set it to default */
437 feat->flags &= ~FEAT_MARK; /* unmark it */
445 /* Gets the value of a specific feature and reports it to the user */
447 feature_get(struct Client* from, const char* const* fields, int count)
449 struct FeatureDesc *feat;
453 if (count < 1) /* check parameters */
454 need_more_params(from, "GET");
455 else if ((feat = feature_desc(from, fields[0]))) {
456 if ((feat->flags & FEAT_MYOPER && !MyOper(from)) ||
457 (feat->flags & FEAT_OPER && !IsAnOper(from))) /* check privs */
458 return send_reply(from, ERR_NOPRIVILEGES);
460 switch (feat->flags & FEAT_MASK) {
461 case FEAT_NONE: /* none, call the callback... */
462 if (feat->get) /* if there's a callback, use it */
463 (*feat->get)(from, fields + 1, count - 1);
466 case FEAT_INT: /* integer, report integer value */
467 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
468 ":Integer value of %s: %d", feat->type, feat->v_int);
471 case FEAT_BOOL: /* boolean, report boolean value */
472 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
473 ":Boolean value of %s: %s", feat->type,
474 feat->v_int ? "TRUE" : "FALSE");
477 case FEAT_STR: /* string, report string value */
478 if (feat->v_str) /* deal with null case */
479 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
480 ":String value of %s: %s", feat->type, feat->v_str);
482 send_reply(from, SND_EXPLICIT | RPL_FEATURE,
483 ":String value for %s not set", feat->type);
491 /* called before reading the .conf to clear all marks */
497 for (i = 0; features[i].type; i++) {
498 features[i].flags &= ~FEAT_MARK; /* clear the marks... */
499 if (features[i].unmark) /* call the unmark callback if necessary */
500 (*features[i].unmark)();
504 /* Called after reading the .conf to reset unmodified values to defaults */
510 for (i = 0; features[i].type; i++)
511 switch (features[i].flags & FEAT_MASK) {
513 if (features[i].mark) /* call the mark callback if necessary */
514 (*features[i].mark)(features[i].flags & FEAT_MARK ? 1 : 0);
517 case FEAT_INT: /* Integers or Booleans... */
519 if (!(features[i].flags & FEAT_MARK)) /* not changed? */
520 features[i].v_int = features[i].def_int;
523 case FEAT_STR: /* strings... */
524 if (!(features[i].flags & FEAT_MARK)) { /* not changed? */
525 if (features[i].v_str && features[i].v_str != features[i].def_str)
526 MyFree(features[i].v_str); /* free old value */
527 features[i].v_str = features[i].def_str;
533 /* report all F-lines */
535 feature_report(struct Client* to)
539 for (i = 0; features[i].type; i++) {
540 if ((features[i].flags & FEAT_MYOPER && !MyOper(to)) ||
541 (features[i].flags & FEAT_OPER && !IsAnOper(to)))
542 continue; /* skip this one */
544 switch (features[i].flags & FEAT_MASK) {
546 if (features[i].report) /* let the callback handle this */
547 (*features[i].report)(to, features[i].flags & FEAT_MARK ? 1 : 0);
551 case FEAT_INT: /* Report an F-line with integer values */
552 if (features[i].flags & FEAT_MARK) /* it's been changed */
553 send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F %s %d",
554 features[i].type, features[i].v_int);
557 case FEAT_BOOL: /* Report an F-line with boolean values */
558 if (features[i].flags & FEAT_MARK) /* it's been changed */
559 send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F %s %s",
560 features[i].type, features[i].v_int ? "TRUE" : "FALSE");
563 case FEAT_STR: /* Report an F-line with string values */
564 if (features[i].flags & FEAT_MARK) { /* it's been changed */
565 if (features[i].v_str)
566 send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F %s %s",
567 features[i].type, features[i].v_str);
568 else /* Actually, F:<type> would reset it; you want F:<type>: */
569 send_reply(to, SND_EXPLICIT | RPL_STATSFLINE, "F %s",
577 /* return a feature's integer value */
579 feature_int(enum Feature feat)
581 assert(features[feat].feat == feat);
582 assert((features[feat].flags & FEAT_MASK) == FEAT_INT);
584 return features[feat].v_int;
587 /* return a feature's boolean value */
589 feature_bool(enum Feature feat)
591 assert(features[feat].feat == feat);
592 assert((features[feat].flags & FEAT_MASK) == FEAT_BOOL);
594 return features[feat].v_int;
597 /* return a feature's string value */
599 feature_str(enum Feature feat)
601 assert(features[feat].feat == feat);
602 assert((features[feat].flags & FEAT_MASK) == FEAT_STR);
604 return features[feat].v_str;