Mercurial > sm-archive
annotate src/context.cpp @ 22:67481a1a502f
Added tag stable-1-0-8 for changeset 09564d4acd9e
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Fri, 24 Dec 2010 15:15:41 -0800 |
parents | 09564d4acd9e |
children |
rev | line source |
---|---|
0 | 1 /* |
2 | |
13 | 3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under |
4 the GPL version 3 or any later version at your choice available at | |
5 http://www.gnu.org/licenses/gpl-3.0.txt | |
0 | 6 |
7 */ | |
8 | |
9 #include "includes.h" | |
10 | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
11 const char *token_envfrom; |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
12 const char *token_include; |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
13 const char *token_lbrace; |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
14 const char *token_rbrace; |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
15 const char *token_rcptto; |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
16 const char *token_remove; |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
17 const char *token_semi; |
0 | 18 |
19 string_set all_strings; // owns all the strings, only modified by the config loader thread | |
20 const int maxlen = 1000; // used for snprintf buffers | |
21 | |
22 CONFIG::CONFIG() { | |
23 reference_count = 0; | |
24 generation = 0; | |
25 load_time = 0; | |
26 } | |
27 | |
28 | |
29 CONFIG::~CONFIG() { | |
30 } | |
31 | |
32 | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
33 bool CONFIG::find(const char *needle, string_set &haystack) { |
13 | 34 string_set::iterator i = haystack.find(needle); |
35 if (i != haystack.end()) return true; // found user@domain.tld key | |
21
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
36 const char *x = strchr(needle, '@'); |
0 | 37 if (x) { |
38 x++; | |
39 i = haystack.find(x); | |
13 | 40 if (i != haystack.end()) return true; // found domain.tld key |
21
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
41 string userpart(needle, x-needle); |
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
42 i = haystack.find(userpart.c_str()); |
13 | 43 if (i != haystack.end()) return true; // found user@ key |
44 } | |
45 return false; | |
46 } | |
47 | |
48 | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
49 const char *CONFIG::find(const char *needle, string_map &haystack) { |
13 | 50 string_map::iterator i = haystack.find(needle); |
51 if (i != haystack.end()) return (*i).second; // found user@domain.tld key | |
21
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
52 const char *x = strchr(needle, '@'); |
13 | 53 if (x) { |
54 x++; | |
55 i = haystack.find(x); | |
56 if (i != haystack.end()) return (*i).second; // found domain.tld key | |
21
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
57 string userpart(needle, x-needle); |
09564d4acd9e
patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents:
19
diff
changeset
|
58 i = haystack.find(userpart.c_str()); |
13 | 59 if (i != haystack.end()) return (*i).second; // found user@ key |
0 | 60 } |
61 return NULL; | |
62 } | |
63 | |
64 | |
65 void CONFIG::dump() { | |
66 printf("rcpt_to {\n"); | |
67 for (string_map::iterator i=rcpt_to.begin(); i!=rcpt_to.end(); i++) { | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
68 const char *to = (*i).first; |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
69 const char *target = (*i).second; |
13 | 70 if (!target) target = "\"\""; |
71 bool rem = find_remove(to); | |
72 printf(" %s \t %s%s;\n", to, target, (rem) ? " remove" : ""); | |
0 | 73 } |
74 printf("};\n"); | |
75 printf("env_from {\n"); | |
76 for (string_map::iterator i=env_from.begin(); i!=env_from.end(); i++) { | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
77 const char *from = (*i).first; |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
78 const char *target = (*i).second; |
13 | 79 if (!target) target = "\"\""; |
80 printf(" %s \t %s;\n", from, target); | |
0 | 81 } |
82 printf("};\n"); | |
83 } | |
84 | |
85 | |
86 //////////////////////////////////////////////// | |
87 // helper to discard the strings held by a string_set | |
88 // | |
89 void discard(string_set &s) { | |
90 for (string_set::iterator i=s.begin(); i!=s.end(); i++) { | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
91 free((void*)*i); |
0 | 92 } |
93 s.clear(); | |
94 } | |
95 | |
96 | |
97 //////////////////////////////////////////////// | |
98 // helper to register a string in a string set | |
99 // | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
100 const char* register_string(string_set &s, const char *name) { |
0 | 101 string_set::iterator i = s.find(name); |
102 if (i != s.end()) return *i; | |
103 char *x = strdup(name); | |
104 s.insert(x); | |
105 return x; | |
106 } | |
107 | |
108 | |
109 //////////////////////////////////////////////// | |
110 // register a global string | |
111 // | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
112 const char* register_string(const char *name) { |
0 | 113 return register_string(all_strings, name); |
114 } | |
115 | |
116 | |
117 //////////////////////////////////////////////// | |
13 | 118 // clear all global strings, helper for valgrind checking |
119 // | |
120 void clear_strings() { | |
121 discard(all_strings); | |
122 } | |
123 | |
124 | |
125 //////////////////////////////////////////////// | |
0 | 126 // |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
127 bool tsa(TOKEN &tok, const char *token); |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
128 bool tsa(TOKEN &tok, const char *token) { |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
129 const char *have = tok.next(); |
0 | 130 if (have == token) return true; |
131 tok.token_error(token, have); | |
132 return false; | |
133 } | |
134 | |
135 | |
136 //////////////////////////////////////////////// | |
137 // | |
138 bool parse_rcpt_to(TOKEN &tok, CONFIG &dc); | |
139 bool parse_rcpt_to(TOKEN &tok, CONFIG &dc) { | |
140 if (!tsa(tok, token_lbrace)) return false; | |
141 while (true) { | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
142 const char *have = tok.next(); |
0 | 143 if (!have) break; |
144 if (have == token_rbrace) break; | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
145 const char *target = tok.next(); |
13 | 146 dc.add_to(have, target); |
147 target = tok.next(); | |
148 if (target == token_remove) { | |
149 dc.add_remove(have); | |
150 target = tok.next(); | |
0 | 151 } |
13 | 152 if (target != token_semi) { |
153 tok.token_error(token_semi, target); | |
154 break; | |
0 | 155 } |
156 } | |
157 return tsa(tok, token_semi); | |
158 } | |
159 | |
160 | |
161 //////////////////////////////////////////////// | |
162 // | |
163 bool parse_env_from(TOKEN &tok, CONFIG &dc); | |
164 bool parse_env_from(TOKEN &tok, CONFIG &dc) { | |
165 if (!tsa(tok, token_lbrace)) return false; | |
166 while (true) { | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
167 const char *have = tok.next(); |
0 | 168 if (!have) break; |
169 if (have == token_rbrace) break; | |
170 if (have == token_semi) { | |
171 // optional separators | |
172 } | |
173 else { | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
174 const char *target = tok.next(); |
0 | 175 dc.add_from(have, target); |
176 } | |
177 } | |
178 return tsa(tok, token_semi); | |
179 } | |
180 | |
181 | |
182 //////////////////////////////////////////////// | |
183 // parse a config file | |
184 // | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
185 bool load_conf(CONFIG &dc, const char *fn) { |
0 | 186 TOKEN tok(fn, &dc.config_files); |
187 while (true) { | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
188 const char *have = tok.next(); |
0 | 189 if (!have) break; |
190 if (have == token_envfrom) { | |
191 if (!parse_env_from(tok, dc)) { | |
192 tok.token_error("load_conf() failed to parse env_from"); | |
193 return false; | |
194 } | |
195 } | |
196 else if (have == token_rcptto) { | |
197 if (!parse_rcpt_to(tok, dc)) { | |
198 tok.token_error("load_conf() failed to parse rcpt_to"); | |
199 return false; | |
200 } | |
201 } | |
202 else { | |
3 | 203 tok.token_error("env_from/rcpt_to", have); |
0 | 204 return false; |
205 } | |
206 } | |
207 return true; | |
208 } | |
209 | |
210 | |
211 //////////////////////////////////////////////// | |
212 // init the tokens | |
213 // | |
214 void token_init() { | |
215 token_envfrom = register_string("env_from"); | |
3 | 216 token_include = register_string("include"); |
0 | 217 token_lbrace = register_string("{"); |
218 token_rbrace = register_string("}"); | |
219 token_rcptto = register_string("rcpt_to"); | |
13 | 220 token_remove = register_string("remove"); |
0 | 221 token_semi = register_string(";"); |
222 } |