Mercurial > masqmail
annotate src/address.c @ 286:e2f6eefbd573
further, minor, improvements to man/masqmail.8
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Tue, 07 Dec 2010 17:21:07 -0300 (2010-12-07) |
parents | bc9d9cd9ee8e |
children | 55b7bde95d37 |
rev | line source |
---|---|
0 | 1 /* MasqMail |
2 Copyright (C) 1999-2001 Oliver Kurth | |
3 | |
4 This program is free software; you can redistribute it and/or modify | |
5 it under the terms of the GNU General Public License as published by | |
6 the Free Software Foundation; either version 2 of the License, or | |
7 (at your option) any later version. | |
8 | |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
15 along with this program; if not, write to the Free Software | |
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 */ | |
18 | |
19 #include "masqmail.h" | |
20 #include <fnmatch.h> | |
21 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
22 address* |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
23 create_address(gchar * path, gboolean is_rfc821) |
0 | 24 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
25 address *addr; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
26 addr = _create_address(path, NULL, is_rfc821); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
27 |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
28 if (addr != NULL) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
29 addr_unmark_delivered(addr); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
30 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
31 return addr; |
0 | 32 } |
33 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
34 address* |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
35 create_address_qualified(gchar * path, gboolean is_rfc821, gchar * domain) |
0 | 36 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
37 address *addr = create_address(path, is_rfc821); |
23
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
38 |
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
39 if (addr != NULL && addr->domain == NULL) { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
40 addr->domain = g_strdup(domain); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
41 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
42 return addr; |
0 | 43 } |
44 | |
13 | 45 /* nothing special about pipes here, but its only called for that purpose */ |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
46 address* |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
47 create_address_pipe(gchar * path) |
0 | 48 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
49 address *addr = g_malloc(sizeof(address)); |
0 | 50 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
51 if (addr) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
52 memset(addr, 0, sizeof(address)); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
53 addr->address = g_strchomp(g_strdup(path)); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
54 addr->local_part = g_strdup(addr->address); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
55 |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
56 addr->domain = g_strdup("localhost"); /* quick hack */ |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
57 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
58 return addr; |
0 | 59 } |
60 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
61 void |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
62 destroy_address(address * addr) |
0 | 63 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
64 DEBUG(6) debugf("destroy_address entered\n"); |
0 | 65 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
66 g_free(addr->address); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
67 g_free(addr->local_part); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
68 g_free(addr->domain); |
0 | 69 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
70 g_free(addr); |
0 | 71 } |
72 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
73 address* |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
74 copy_modify_address(const address * orig, gchar * l_part, gchar * dom) |
0 | 75 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
76 address *addr = NULL; |
0 | 77 |
23
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
78 if (!orig) { |
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
79 return NULL; |
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
80 } |
0 | 81 |
23
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
82 if ((addr = g_malloc(sizeof(address))) == NULL) { |
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
83 return NULL; |
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
84 } |
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
85 |
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
86 addr->address = g_strdup(orig->address); |
0 | 87 |
23
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
88 if (l_part == NULL) |
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
89 addr->local_part = g_strdup(orig->local_part); |
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
90 else |
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
91 addr->local_part = g_strdup(l_part); |
0 | 92 |
23
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
93 if (dom == NULL) |
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
94 addr->domain = g_strdup(orig->domain); |
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
95 else |
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
96 addr->domain = g_strdup(dom); |
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
97 |
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
98 addr->flags = 0; |
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
99 addr->children = NULL; |
4a3bc3a7afbe
refactoring: early exit instead of deep if levels
meillo@marmaro.de
parents:
14
diff
changeset
|
100 addr->parent = NULL; |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
101 return addr; |
0 | 102 } |
103 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
104 gboolean |
242
bc9d9cd9ee8e
made addr_isequal() and addr_isequal_parent() more flexible
markus schnalke <meillo@marmaro.de>
parents:
23
diff
changeset
|
105 addr_isequal(address * addr1, address * addr2, int (*cmpfunc) (const char*, const char*)) |
0 | 106 { |
242
bc9d9cd9ee8e
made addr_isequal() and addr_isequal_parent() more flexible
markus schnalke <meillo@marmaro.de>
parents:
23
diff
changeset
|
107 return (cmpfunc(addr1->local_part, addr2->local_part) == 0) |
13 | 108 && (strcasecmp(addr1->domain, addr2->domain) == 0); |
0 | 109 } |
110 | |
111 /* searches in ancestors of addr1 */ | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
112 gboolean |
242
bc9d9cd9ee8e
made addr_isequal() and addr_isequal_parent() more flexible
markus schnalke <meillo@marmaro.de>
parents:
23
diff
changeset
|
113 addr_isequal_parent(address* addr1, address* addr2, int (*cmpfunc) (const char*, const char*)) |
0 | 114 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
115 address *addr; |
0 | 116 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
117 for (addr = addr1; addr; addr = addr->parent) |
242
bc9d9cd9ee8e
made addr_isequal() and addr_isequal_parent() more flexible
markus schnalke <meillo@marmaro.de>
parents:
23
diff
changeset
|
118 if (addr_isequal(addr, addr2, cmpfunc)) |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
119 return TRUE; |
0 | 120 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
121 return FALSE; |
0 | 122 } |
123 | |
124 /* careful, this is recursive */ | |
125 /* returns TRUE if ALL children have been delivered */ | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
126 gboolean |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
127 addr_is_delivered_children(address * addr) |
0 | 128 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
129 GList *addr_node; |
0 | 130 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
131 if (addr->children == NULL) |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
132 return addr_is_delivered(addr); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
133 |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
134 foreach(addr->children, addr_node) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
135 address *addr = (address *) (addr_node->data); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
136 if (!addr_is_delivered_children(addr)) |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
137 return FALSE; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
138 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
139 return TRUE; |
0 | 140 } |
141 | |
142 /* careful, this is recursive */ | |
143 /* returns TRUE if ALL children have been either delivered or have failed */ | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
144 gboolean |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
145 addr_is_finished_children(address * addr) |
0 | 146 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
147 GList *addr_node; |
0 | 148 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
149 if (addr->children == NULL) |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
150 return (addr_is_failed(addr) || addr_is_delivered(addr)); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
151 |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
152 foreach(addr->children, addr_node) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
153 address *addr = (address *) (addr_node->data); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
154 if (!addr_is_finished_children(addr)) |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
155 return FALSE; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
156 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
157 return TRUE; |
0 | 158 } |
159 | |
160 /* find original address */ | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
161 address* |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
162 addr_find_ancestor(address * addr) |
0 | 163 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
164 while (addr->parent) |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
165 addr = addr->parent; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
166 return addr; |
0 | 167 } |
168 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
169 gchar* |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
170 addr_string(address * addr) |
0 | 171 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
172 static gchar *buffer = NULL; |
0 | 173 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
174 if (addr == NULL) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
175 g_free(buffer); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
176 buffer = NULL; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
177 return NULL; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
178 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
179 if (buffer) |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
180 g_free(buffer); |
0 | 181 |
14
a8f3424347dc
replaced number 0 with character \0 where appropriate
meillo@marmaro.de
parents:
13
diff
changeset
|
182 if (addr->local_part[0] == '\0') { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
183 buffer = g_strdup("<>"); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
184 } else { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
185 buffer = g_strdup_printf("<%s@%s>", addr->local_part ? addr->local_part : "", addr->domain ? addr->domain : ""); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
186 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
187 return buffer; |
0 | 188 } |
189 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
190 gint |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
191 addr_match(address * addr1, address * addr2) |
0 | 192 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
193 int res; |
0 | 194 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
195 if ((res = fnmatch(addr1->local_part, addr2->local_part, 0)) == 0) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
196 if ((res = fnmatch(addr1->domain, addr2->domain, FNM_CASEFOLD)) == 0) |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
197 return 0; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
198 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
199 return res; |
0 | 200 } |