comparison 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
comparison
equal deleted inserted replaced
12:bb3d2cd6007e 13:75e1a9bcbc2e
1 /* 1 /*
2 2
3 Copyright (c) 2004 Carl Byington - 510 Software Group, released under 3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under
4 the GPL version 2 or any later version at your choice available at 4 the GPL version 3 or any later version at your choice available at
5 http://www.fsf.org/licenses/gpl.txt 5 http://www.gnu.org/licenses/gpl-3.0.txt
6 6
7 */ 7 */
8 8
9 #include "includes.h" 9 #include "includes.h"
10 10
13 char *token_envfrom; 13 char *token_envfrom;
14 char *token_include; 14 char *token_include;
15 char *token_lbrace; 15 char *token_lbrace;
16 char *token_rbrace; 16 char *token_rbrace;
17 char *token_rcptto; 17 char *token_rcptto;
18 char *token_remove;
18 char *token_semi; 19 char *token_semi;
19 20
20 string_set all_strings; // owns all the strings, only modified by the config loader thread 21 string_set all_strings; // owns all the strings, only modified by the config loader thread
21 const int maxlen = 1000; // used for snprintf buffers 22 const int maxlen = 1000; // used for snprintf buffers
22 23
29 30
30 CONFIG::~CONFIG() { 31 CONFIG::~CONFIG() {
31 } 32 }
32 33
33 34
34 char *CONFIG::find(char *needle, string_map &haystack) { 35 bool CONFIG::find(char *needle, string_set &haystack) {
35 string_map::iterator i = haystack.find(needle); 36 string_set::iterator i = haystack.find(needle);
36 if (i != haystack.end()) return (*i).second; // found user@domain.tld key 37 if (i != haystack.end()) return true; // found user@domain.tld key
37 char *x = strchr(needle, '@'); 38 char *x = strchr(needle, '@');
38 if (x) { 39 if (x) {
39 x++; 40 x++;
40 i = haystack.find(x); 41 i = haystack.find(x);
41 if (i != haystack.end()) return (*i).second; // found domain.tld key 42 if (i != haystack.end()) return true; // found domain.tld key
42 char y = *x; 43 char y = *x;
43 *x = '\0'; 44 *x = '\0';
44 i = haystack.find(needle); 45 i = haystack.find(needle);
45 *x = y; 46 *x = y;
46 if (i != haystack.end()) return (*i).second; // found user@ key 47 if (i != haystack.end()) return true; // found user@ key
48 }
49 return false;
50 }
51
52
53 char *CONFIG::find(char *needle, string_map &haystack) {
54 string_map::iterator i = haystack.find(needle);
55 if (i != haystack.end()) return (*i).second; // found user@domain.tld key
56 char *x = strchr(needle, '@');
57 if (x) {
58 x++;
59 i = haystack.find(x);
60 if (i != haystack.end()) return (*i).second; // found domain.tld key
61 char y = *x;
62 *x = '\0';
63 i = haystack.find(needle);
64 *x = y;
65 if (i != haystack.end()) return (*i).second; // found user@ key
47 } 66 }
48 return NULL; 67 return NULL;
49 } 68 }
50 69
51 70
52 void CONFIG::dump() { 71 void CONFIG::dump() {
53 printf("rcpt_to {\n"); 72 printf("rcpt_to {\n");
54 for (string_map::iterator i=rcpt_to.begin(); i!=rcpt_to.end(); i++) { 73 for (string_map::iterator i=rcpt_to.begin(); i!=rcpt_to.end(); i++) {
55 char *to = (*i).first; 74 char *to = (*i).first;
56 char *target = (*i).second; 75 char *target = (*i).second;
57 printf(" %s \t %s\n", to, target); 76 if (!target) target = "\"\"";
77 bool rem = find_remove(to);
78 printf(" %s \t %s%s;\n", to, target, (rem) ? " remove" : "");
58 } 79 }
59 printf("};\n"); 80 printf("};\n");
60 printf("env_from {\n"); 81 printf("env_from {\n");
61 for (string_map::iterator i=env_from.begin(); i!=env_from.end(); i++) { 82 for (string_map::iterator i=env_from.begin(); i!=env_from.end(); i++) {
62 char *from = (*i).first; 83 char *from = (*i).first;
63 char *target = (*i).second; 84 char *target = (*i).second;
64 printf(" %s \t %s\n", from, target); 85 if (!target) target = "\"\"";
86 printf(" %s \t %s;\n", from, target);
65 } 87 }
66 printf("};\n"); 88 printf("};\n");
67 } 89 }
68 90
69 91
93 //////////////////////////////////////////////// 115 ////////////////////////////////////////////////
94 // register a global string 116 // register a global string
95 // 117 //
96 char* register_string(char *name) { 118 char* register_string(char *name) {
97 return register_string(all_strings, name); 119 return register_string(all_strings, name);
120 }
121
122
123 ////////////////////////////////////////////////
124 // clear all global strings, helper for valgrind checking
125 //
126 void clear_strings() {
127 discard(all_strings);
98 } 128 }
99 129
100 130
101 //////////////////////////////////////////////// 131 ////////////////////////////////////////////////
102 // 132 //
116 if (!tsa(tok, token_lbrace)) return false; 146 if (!tsa(tok, token_lbrace)) return false;
117 while (true) { 147 while (true) {
118 char *have = tok.next(); 148 char *have = tok.next();
119 if (!have) break; 149 if (!have) break;
120 if (have == token_rbrace) break; 150 if (have == token_rbrace) break;
121 if (have == token_semi) { 151 char *target = tok.next();
122 // optional separators 152 dc.add_to(have, target);
123 } 153 target = tok.next();
124 else { 154 if (target == token_remove) {
125 char *target = tok.next(); 155 dc.add_remove(have);
126 dc.add_to(have, target); 156 target = tok.next();
157 }
158 if (target != token_semi) {
159 tok.token_error(token_semi, target);
160 break;
127 } 161 }
128 } 162 }
129 return tsa(tok, token_semi); 163 return tsa(tok, token_semi);
130 } 164 }
131 165
187 token_envfrom = register_string("env_from"); 221 token_envfrom = register_string("env_from");
188 token_include = register_string("include"); 222 token_include = register_string("include");
189 token_lbrace = register_string("{"); 223 token_lbrace = register_string("{");
190 token_rbrace = register_string("}"); 224 token_rbrace = register_string("}");
191 token_rcptto = register_string("rcpt_to"); 225 token_rcptto = register_string("rcpt_to");
226 token_remove = register_string("remove");
192 token_semi = register_string(";"); 227 token_semi = register_string(";");
193 } 228 }