comparison src/context.cpp @ 140:4028de9b46dd

cleanup smtp rate limit code
author carl
date Wed, 27 Sep 2006 08:00:13 -0700
parents f4746d8a12a3
children 6256cab02248
comparison
equal deleted inserted replaced
139:003026deaed1 140:4028de9b46dd
492 host_limit = (parent) ? parent->host_limit : 0; 492 host_limit = (parent) ? parent->host_limit : 0;
493 host_limit_message = NULL; 493 host_limit_message = NULL;
494 host_random = (parent) ? parent->host_random : false; 494 host_random = (parent) ? parent->host_random : false;
495 tag_limit = (parent) ? parent->tag_limit : 0; 495 tag_limit = (parent) ? parent->tag_limit : 0;
496 tag_limit_message = NULL; 496 tag_limit_message = NULL;
497 default_rcpt_rate = INT_MAX;
497 } 498 }
498 499
499 500
500 CONTEXT::~CONTEXT() { 501 CONTEXT::~CONTEXT() {
501 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) { 502 for (dnsblp_map::iterator i=dnsbl_names.begin(); i!=dnsbl_names.end(); i++) {
553 else return NULL; 554 else return NULL;
554 } 555 }
555 556
556 557
557 int CONTEXT::find_rate(char *user) { 558 int CONTEXT::find_rate(char *user) {
558 if (rcpt_per_hour.empty()) return INT_MAX; 559 if (rcpt_per_hour.empty()) return default_rcpt_rate;
559 rcpt_rates::iterator i = rcpt_per_hour.find(user); 560 rcpt_rates::iterator i = rcpt_per_hour.find(user);
560 if (i == rcpt_per_hour.end()) i = rcpt_per_hour.find(" "); 561 return (i == rcpt_per_hour.end()) ? default_rcpt_rate : (*i).second;
561 return (i == rcpt_per_hour.end()) ? INT_MAX : (*i).second;
562 } 562 }
563 563
564 564
565 char *CONTEXT::find_from(char *from) { 565 char *CONTEXT::find_from(char *from) {
566 char *rc = token_inherit; 566 char *rc = token_inherit;
802 printf("%s %s \t%s; \n", indent, f, t->name); 802 printf("%s %s \t%s; \n", indent, f, t->name);
803 } 803 }
804 } 804 }
805 printf("%s }; \n", indent); 805 printf("%s }; \n", indent);
806 806
807 if (!rcpt_per_hour.empty()) { 807 printf("%s rate_limit %d { \n", indent, default_rcpt_rate);
808 printf("%s rate_limit { \n", indent); 808 for (rcpt_rates::iterator j=rcpt_per_hour.begin(); j!=rcpt_per_hour.end(); j++) {
809 for (rcpt_rates::iterator j=rcpt_per_hour.begin(); j!=rcpt_per_hour.end(); j++) { 809 char *u = (*j).first;
810 char *u = (*j).first; 810 int l = (*j).second;
811 int l = (*j).second; 811 printf("%s \"%s\" \t%d; \n", indent, u, l);
812 printf("%s \"%s\" \t%d; \n", indent, u, l); 812 }
813 } 813 printf("%s }; \n", indent);
814 printf("%s }; \n", indent);
815 }
816 814
817 printf("%s }; \n", indent); 815 printf("%s }; \n", indent);
818 } 816 }
819 817
820 818
1190 1188
1191 //////////////////////////////////////////////// 1189 ////////////////////////////////////////////////
1192 // 1190 //
1193 bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me); 1191 bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me);
1194 bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me) { 1192 bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me) {
1193 char *def = tok.next();
1194 if (def != token_lbrace) {
1195 tok.push(def);
1196 me.set_default_rate(tok.nextint());
1197 }
1195 if (!tsa(tok, token_lbrace)) return false; 1198 if (!tsa(tok, token_lbrace)) return false;
1196 while (true) { 1199 while (true) {
1197 char *have = tok.next(); 1200 char *have = tok.next();
1198 if (!have) break; 1201 if (!have) break;
1199 if (have == token_rbrace) break; 1202 if (have == token_rbrace) break;
1200 if (have == token_semi) { 1203 if (have == token_semi) {
1201 // optional separators 1204 // optional separators
1202 } 1205 }
1203 else { 1206 else {
1204 char *limit = tok.next(); 1207 me.add_rate(have, tok.nextint());
1205 int lim = (limit) ? atoi(limit) : 0;
1206 me.add_rate(have, lim);
1207 } 1208 }
1208 } 1209 }
1209 return tsa(tok, token_semi); 1210 return tsa(tok, token_semi);
1210 } 1211 }
1211 1212
1238 if (!parse_verify(tok, dc, *con)) return false; 1239 if (!parse_verify(tok, dc, *con)) return false;
1239 } 1240 }
1240 else if (have == token_envfrom) { 1241 else if (have == token_envfrom) {
1241 if (!parse_envfrom(tok, dc, *con)) return false; 1242 if (!parse_envfrom(tok, dc, *con)) return false;
1242 } 1243 }
1243 else if ((have == token_rate) && (!parent) && (!dc.default_context)) { 1244 else if (have == token_rate) {
1245 if (parent || dc.default_context) tok.token_error("rate limit ignored in non default context");
1244 if (!parse_rate(tok, dc, *con)) return false; 1246 if (!parse_rate(tok, dc, *con)) return false;
1245 } 1247 }
1246 else if (have == token_context) { 1248 else if (have == token_context) {
1247 if (!parse_context(tok, dc, con)) return false; 1249 if (!parse_context(tok, dc, con)) return false;
1248 } 1250 }