diff 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
line wrap: on
line diff
--- a/src/context.cpp	Tue Sep 26 15:21:17 2006 -0700
+++ b/src/context.cpp	Wed Sep 27 08:00:13 2006 -0700
@@ -494,6 +494,7 @@
 	host_random 		= (parent) ? parent->host_random : false;
 	tag_limit			= (parent) ? parent->tag_limit	 : 0;
 	tag_limit_message	= NULL;
+	default_rcpt_rate	= INT_MAX;
 }
 
 
@@ -555,10 +556,9 @@
 
 
 int CONTEXT::find_rate(char *user) {
-	if (rcpt_per_hour.empty()) return INT_MAX;
+	if (rcpt_per_hour.empty()) return default_rcpt_rate;
 	rcpt_rates::iterator i = rcpt_per_hour.find(user);
-	if (i == rcpt_per_hour.end()) i = rcpt_per_hour.find(" ");
-	return (i == rcpt_per_hour.end()) ? INT_MAX : (*i).second;
+	return (i == rcpt_per_hour.end()) ? default_rcpt_rate : (*i).second;
 }
 
 
@@ -804,15 +804,13 @@
 	}
 	printf("%s     }; \n", indent);
 
-	if (!rcpt_per_hour.empty()) {
-		printf("%s     rate_limit { \n", indent);
+	printf("%s     rate_limit %d { \n", indent, default_rcpt_rate);
 		for (rcpt_rates::iterator j=rcpt_per_hour.begin(); j!=rcpt_per_hour.end(); j++) {
 			char	*u = (*j).first;
 			int 	 l = (*j).second;
 			printf("%s         \"%s\" \t%d; \n", indent, u, l);
 		}
 		printf("%s     }; \n", indent);
-	}
 
 	printf("%s }; \n", indent);
 }
@@ -1192,6 +1190,11 @@
 //
 bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me);
 bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me) {
+	char *def = tok.next();
+	if (def != token_lbrace) {
+		tok.push(def);
+		me.set_default_rate(tok.nextint());
+	}
 	if (!tsa(tok, token_lbrace)) return false;
 	while (true) {
 		char *have = tok.next();
@@ -1201,9 +1204,7 @@
 			// optional separators
 		}
 		else {
-			char *limit = tok.next();
-			int lim = (limit) ? atoi(limit) : 0;
-			me.add_rate(have, lim);
+			me.add_rate(have, tok.nextint());
 		}
 	}
 	return tsa(tok, token_semi);
@@ -1240,7 +1241,8 @@
 		else if (have == token_envfrom) {
 			if (!parse_envfrom(tok, dc, *con)) return false;
 		}
-		else if ((have == token_rate) && (!parent) && (!dc.default_context)) {
+		else if (have == token_rate) {
+			if (parent || dc.default_context) tok.token_error("rate limit ignored in non default context");
 			if (!parse_rate(tok, dc, *con)) return false;
 		}
 		else if (have == token_context) {