diff src/context.cpp @ 136:f4746d8a12a3

add smtp auth rate limits
author carl
date Tue, 26 Sep 2006 13:59:14 -0700
parents c5cd1261394d
children 4028de9b46dd
line wrap: on
line diff
--- a/src/context.cpp	Wed Aug 02 21:33:34 2006 -0700
+++ b/src/context.cpp	Tue Sep 26 13:59:14 2006 -0700
@@ -45,6 +45,7 @@
 char *token_ok2;
 char *token_ok;
 char *token_on;
+char *token_rate;
 char *token_rbrace;
 char *token_semi;
 char *token_soft;
@@ -553,6 +554,14 @@
 }
 
 
+int CONTEXT::find_rate(char *user) {
+	if (rcpt_per_hour.empty()) return INT_MAX;
+	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;
+}
+
+
 char *CONTEXT::find_from(char *from) {
 	char *rc = token_inherit;
 	string_map::iterator i = env_from.find(from);
@@ -795,6 +804,16 @@
 	}
 	printf("%s     }; \n", indent);
 
+	if (!rcpt_per_hour.empty()) {
+		printf("%s     rate_limit { \n", indent);
+		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);
 }
 
@@ -1171,6 +1190,28 @@
 
 ////////////////////////////////////////////////
 //
+bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me);
+bool parse_rate(TOKEN &tok, CONFIG &dc, CONTEXT &me) {
+	if (!tsa(tok, token_lbrace)) return false;
+	while (true) {
+		char *have = tok.next();
+		if (!have) break;
+		if (have == token_rbrace) break;
+		if (have == token_semi) {
+			// optional separators
+		}
+		else {
+			char *limit = tok.next();
+			int lim = (limit) ? atoi(limit) : 0;
+			me.add_rate(have, lim);
+		}
+	}
+	return tsa(tok, token_semi);
+}
+
+
+////////////////////////////////////////////////
+//
 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent);
 bool parse_context(TOKEN &tok, CONFIG &dc, CONTEXTP parent) {
 	char *name = tok.next();
@@ -1199,6 +1240,9 @@
 		else if (have == token_envfrom) {
 			if (!parse_envfrom(tok, dc, *con)) return false;
 		}
+		else if ((have == token_rate) && (!parent) && (!dc.default_context)) {
+			if (!parse_rate(tok, dc, *con)) return false;
+		}
 		else if (have == token_context) {
 			if (!parse_context(tok, dc, con)) return false;
 		}
@@ -1300,6 +1344,7 @@
 	token_ok		 = register_string("ok");
 	token_ok2		 = register_string("ok2");
 	token_on		 = register_string("on");
+	token_rate		 = register_string("rate_limit");
 	token_rbrace	 = register_string("}");
 	token_semi		 = register_string(";");
 	token_soft		 = register_string("soft");