annotate src/context.cpp @ 13:75e1a9bcbc2e

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