comparison src/context.cpp @ 76:81f1e400e8ab

start coding on new config syntax
author carl
date Sat, 16 Jul 2005 13:47:19 -0700
parents 1142e46be550
children 8487650c98ee
comparison
equal deleted inserted replaced
75:1142e46be550 76:81f1e400e8ab
59 default_context = NULL; 59 default_context = NULL;
60 } 60 }
61 61
62 62
63 CONFIG::~CONFIG() { 63 CONFIG::~CONFIG() {
64 if (debug_syslog) my_syslog("config::~config destructor");
64 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) { 65 for (context_list::iterator i=contexts.begin(); i!=contexts.end(); i++) {
65 CONTEXT *c = *i; 66 CONTEXT *c = *i;
66 delete c; 67 delete c;
67 } 68 }
68 } 69 }
78 79
79 80
80 void CONFIG::add_to(char *to, CONTEXTP con) { 81 void CONFIG::add_to(char *to, CONTEXTP con) {
81 context_map::iterator i = env_to.find(to); 82 context_map::iterator i = env_to.find(to);
82 if (i != env_to.end()) { 83 if (i != env_to.end()) {
83 CONTEXTP c = (*i).second; 84 CONTEXTP c = (*i).second;
85 int s = strlen(to);
86 bool at = s && (to[s-1] == '@');
87 if (at && con->is_parent(c->get_parent())) {
88 if (debug_syslog) {
89 char oldname[maxlen];
90 char newname[maxlen];
91 char *oldn = c->get_full_name(oldname, maxlen);
92 char *newn = con->get_full_name(newname, maxlen);
93 char buf[maxlen*3];
94 snprintf(buf, maxlen*3, "both %s and %s claim envelope to %s, the first one wins", oldn, newn, to);
95 my_syslog(buf);
96 }
97 return; // don't take over user@ entries from your ancestors children
98 }
84 if ((c != con) && (c != con->get_parent())) { 99 if ((c != con) && (c != con->get_parent())) {
85 char oldname[maxlen]; 100 char oldname[maxlen];
86 char newname[maxlen]; 101 char newname[maxlen];
87 char *oldn = c->get_full_name(oldname, maxlen); 102 char *oldn = c->get_full_name(oldname, maxlen);
88 char *newn = con->get_full_name(newname, maxlen); 103 char *newn = con->get_full_name(newname, maxlen);
143 tag_limit_message = NULL; 158 tag_limit_message = NULL;
144 } 159 }
145 160
146 161
147 CONTEXT::~CONTEXT() { 162 CONTEXT::~CONTEXT() {
163 if (debug_syslog) {
164 char buf[maxlen];
165 char msg[maxlen];
166 snprintf(msg, maxlen, "context::~context %s destructor", get_full_name(buf,maxlen));
167 my_syslog(msg);
168 }
148 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) { 169 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) {
149 DNSBLP d = (*i).second; 170 DNSBLP d = (*i).second;
150 // delete the underlying DNSBL objects. 171 // delete the underlying DNSBL objects.
151 delete d; 172 delete d;
152 } 173 }
174 }
175
176
177 bool CONTEXT::is_parent(CONTEXTP p) {
178 if (p == parent) return true;
179 if (!parent) return false;
180 return parent->is_parent(p);
153 } 181 }
154 182
155 183
156 char *CONTEXT::get_full_name(char *buffer, int size) { 184 char *CONTEXT::get_full_name(char *buffer, int size) {
157 if (!parent) return name; 185 if (!parent) return name;
172 return false; 200 return false;
173 } 201 }
174 202
175 203
176 char *CONTEXT::find_from(char *from) { 204 char *CONTEXT::find_from(char *from) {
205 char *rc = token_inherit;
177 string_map::iterator i = env_from.find(from); 206 string_map::iterator i = env_from.find(from);
178 if (i != env_from.end()) return (*i).second; // found user@domain.tld key 207 if (i != env_from.end()) rc = (*i).second; // found user@domain.tld key
179 char *x = strchr(from, '@'); 208 else {
180 if (x) { 209 char *x = strchr(from, '@');
181 x++; 210 if (x) {
182 i = env_from.find(x); 211 x++;
183 if (i != env_from.end()) return (*i).second; // found domain.tld key 212 i = env_from.find(x);
184 char y = *x; 213 if (i != env_from.end()) rc = (*i).second; // found domain.tld key
185 *x = '\0'; 214 else {
186 i = env_from.find(from); 215 char y = *x;
187 *x = y; 216 *x = '\0';
188 if (i != env_from.end()) return (*i).second; // found user@ key 217 i = env_from.find(from);
189 } 218 *x = y;
190 if ((env_from_default == token_inherit) && parent) { 219 if (i != env_from.end()) rc = (*i).second; // found user@ key
191 return parent->find_from(from); 220 }
192 } 221 }
193 return (env_from_default == token_inherit) ? token_unknown : env_from_default; 222 }
223 if (rc == token_inherit) rc = env_from_default;
224 if ((rc == token_inherit) && parent) return parent->find_from(from);
225 return (rc == token_inherit) ? token_unknown : rc;
194 } 226 }
195 227
196 228
197 CONTEXTP CONTEXT::find_context(char *from) { 229 CONTEXTP CONTEXT::find_context(char *from) {
198 context_map::iterator i = env_from_context.find(from); 230 context_map::iterator i = env_from_context.find(from);
225 if (parent) return parent->find_dnsbl(name); 257 if (parent) return parent->find_dnsbl(name);
226 return NULL; 258 return NULL;
227 } 259 }
228 260
229 261
262 char* CONTEXT::get_content_suffix() {
263 if (!content_suffix && parent) return parent->get_content_suffix();
264 return content_suffix;
265 }
266
267
268 char* CONTEXT::get_content_message() {
269 if (!content_message && parent) return parent->get_content_message();
270 return content_message;
271 }
272
273
274 string_set& CONTEXT::get_content_host_ignore() {
275 if (content_host_ignore.empty() && parent) return parent->get_content_host_ignore();
276 return content_host_ignore;
277 }
278
279
280 string_set& CONTEXT::get_content_tlds() {
281 if (content_tlds.empty() && parent) return parent->get_content_tlds();
282 return content_tlds;
283 }
284
285
286 string_set& CONTEXT::get_html_tags() {
287 if (html_tags.empty() && parent) return parent->get_html_tags();
288 return html_tags;
289 }
290
291
292 dnsblp_list& CONTEXT::get_dnsbl_list() {
293 if (dnsbl_list.empty() && parent) return parent->get_dnsbl_list();
294 return dnsbl_list;
295 }
296
297
230 bool CONTEXT::acceptable_content(recorder &memory, char *&msg) { 298 bool CONTEXT::acceptable_content(recorder &memory, char *&msg) {
231 if (memory.excessive_bad_tags(tag_limit)) { 299 if (memory.excessive_bad_tags(tag_limit)) {
232 msg = tag_limit_message; 300 msg = tag_limit_message;
233 return false; 301 return false;
234 } 302 }
425 493
426 //////////////////////////////////////////////// 494 ////////////////////////////////////////////////
427 // 495 //
428 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me); 496 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me);
429 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me) { 497 bool parse_content(TOKEN &tok, CONFIG &dc, CONTEXT &me) {
430 bool topdefault = (!me.get_parent()) && (!dc.default_context);
431 char *setting = tok.next(); 498 char *setting = tok.next();
432 if (setting == token_on) { 499 if (setting == token_on) {
433 me.set_content_filtering(true); 500 me.set_content_filtering(true);
434 } 501 }
435 else if (setting == token_off) { 502 else if (setting == token_off) {
444 char *have = tok.next(); 511 char *have = tok.next();
445 if (!have) break; 512 if (!have) break;
446 if (have == token_filter) { 513 if (have == token_filter) {
447 char *suffix = tok.next(); 514 char *suffix = tok.next();
448 char *messag = tok.next(); 515 char *messag = tok.next();
449 if (topdefault) { 516 me.set_content_suffix(suffix);
450 me.set_content_suffix(suffix); 517 me.set_content_message(messag);
451 me.set_content_message(messag);
452 }
453 if (!tsa(tok, token_semi)) return false; 518 if (!tsa(tok, token_semi)) return false;
454 if (!topdefault) tok.token_error("content filters may only be speciried in the top default context");
455 } 519 }
456 else if (have == token_ignore) { 520 else if (have == token_ignore) {
457 if (!tsa(tok, token_lbrace)) return false; 521 if (!tsa(tok, token_lbrace)) return false;
458 while (true) { 522 while (true) {
459 if (!have) break; 523 if (!have) break;
460 char *have = tok.next(); 524 char *have = tok.next();
461 if (have == token_rbrace) { 525 if (have == token_rbrace) break; // done
462 break; // done 526 me.add_ignore(have);
463 }
464 else {
465 me.add_ignore(have);
466 }
467 } 527 }
468 if (!tsa(tok, token_semi)) return false; 528 if (!tsa(tok, token_semi)) return false;
469 } 529 }
470 else if (have == token_tld) { 530 else if (have == token_tld) {
471 if (!tsa(tok, token_lbrace)) return false; 531 if (!tsa(tok, token_lbrace)) return false;
472 while (true) { 532 while (true) {
473 char *have = tok.next(); 533 char *have = tok.next();
474 if (!have) break; 534 if (!have) break;
475 if (have == token_rbrace) { 535 if (have == token_rbrace) break; // done
476 break; // done 536 me.add_tld(have);
477 }
478 else {
479 if (topdefault) me.add_tld(have);
480 }
481 } 537 }
482 if (!tsa(tok, token_semi)) return false; 538 if (!tsa(tok, token_semi)) return false;
483 if (!topdefault) tok.token_error("tld values may only be specified in the top default context");
484 } 539 }
485 else if (have == token_html_limit) { 540 else if (have == token_html_limit) {
486 have = tok.next(); 541 have = tok.next();
487 if (have == token_on) { 542 if (have == token_on) {
488 me.set_tag_limit(tok.nextint()); 543 me.set_tag_limit(tok.nextint());
505 if (!have) break; 560 if (!have) break;
506 if (have == token_rbrace) { 561 if (have == token_rbrace) {
507 break; // done 562 break; // done
508 } 563 }
509 else { 564 else {
510 if (topdefault) me.add_tag(have); 565 me.add_tag(have);
511 } 566 }
512 } 567 }
513 if (!tsa(tok, token_semi)) return false; 568 if (!tsa(tok, token_semi)) return false;
514 if (!topdefault) tok.token_error("html tags may only be specified in the top default context");
515 } 569 }
516 else if (have == token_host_limit) { 570 else if (have == token_host_limit) {
517 have = tok.next(); 571 have = tok.next();
518 if (have == token_on) { 572 if (have == token_on) {
519 me.set_host_limit(tok.nextint()); 573 me.set_host_limit(tok.nextint());