masqmail-0.2
view src/address.c @ 189:bdd343cc4e19
bumped version number
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Sat, 27 Aug 2011 18:51:46 +0200 |
parents | a8f3424347dc |
children |
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
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.
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.
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 */
19 #include "masqmail.h"
20 #include <fnmatch.h>
22 address*
23 create_address(gchar * path, gboolean is_rfc821)
24 {
25 address *addr;
26 addr = _create_address(path, NULL, is_rfc821);
28 if (addr != NULL) {
29 addr_unmark_delivered(addr);
30 }
31 return addr;
32 }
34 address*
35 create_address_qualified(gchar * path, gboolean is_rfc821, gchar * domain)
36 {
37 address *addr = create_address(path, is_rfc821);
39 if (addr != NULL && addr->domain == NULL) {
40 addr->domain = g_strdup(domain);
41 }
42 return addr;
43 }
45 /* nothing special about pipes here, but its only called for that purpose */
46 address*
47 create_address_pipe(gchar * path)
48 {
49 address *addr = g_malloc(sizeof(address));
51 if (addr) {
52 memset(addr, 0, sizeof(address));
53 addr->address = g_strchomp(g_strdup(path));
54 addr->local_part = g_strdup(addr->address);
56 addr->domain = g_strdup("localhost"); /* quick hack */
57 }
58 return addr;
59 }
61 void
62 destroy_address(address * addr)
63 {
64 DEBUG(6) debugf("destroy_address entered\n");
66 g_free(addr->address);
67 g_free(addr->local_part);
68 g_free(addr->domain);
70 g_free(addr);
71 }
73 address*
74 copy_modify_address(const address * orig, gchar * l_part, gchar * dom)
75 {
76 address *addr = NULL;
78 if (!orig) {
79 return NULL;
80 }
82 if ((addr = g_malloc(sizeof(address))) == NULL) {
83 return NULL;
84 }
86 addr->address = g_strdup(orig->address);
88 if (l_part == NULL)
89 addr->local_part = g_strdup(orig->local_part);
90 else
91 addr->local_part = g_strdup(l_part);
93 if (dom == NULL)
94 addr->domain = g_strdup(orig->domain);
95 else
96 addr->domain = g_strdup(dom);
98 addr->flags = 0;
99 addr->children = NULL;
100 addr->parent = NULL;
101 return addr;
102 }
104 gboolean
105 addr_isequal(address * addr1, address * addr2)
106 {
107 return (strcmp(addr1->local_part, addr2->local_part) == 0)
108 && (strcasecmp(addr1->domain, addr2->domain) == 0);
109 }
111 /* searches in ancestors of addr1 */
112 gboolean
113 addr_isequal_parent(address * addr1, address * addr2)
114 {
115 address *addr;
117 for (addr = addr1; addr; addr = addr->parent)
118 if (addr_isequal(addr, addr2))
119 return TRUE;
121 return FALSE;
122 }
124 /* careful, this is recursive */
125 /* returns TRUE if ALL children have been delivered */
126 gboolean
127 addr_is_delivered_children(address * addr)
128 {
129 GList *addr_node;
131 if (addr->children == NULL)
132 return addr_is_delivered(addr);
134 foreach(addr->children, addr_node) {
135 address *addr = (address *) (addr_node->data);
136 if (!addr_is_delivered_children(addr))
137 return FALSE;
138 }
139 return TRUE;
140 }
142 /* careful, this is recursive */
143 /* returns TRUE if ALL children have been either delivered or have failed */
144 gboolean
145 addr_is_finished_children(address * addr)
146 {
147 GList *addr_node;
149 if (addr->children == NULL)
150 return (addr_is_failed(addr) || addr_is_delivered(addr));
152 foreach(addr->children, addr_node) {
153 address *addr = (address *) (addr_node->data);
154 if (!addr_is_finished_children(addr))
155 return FALSE;
156 }
157 return TRUE;
158 }
160 /* find original address */
161 address*
162 addr_find_ancestor(address * addr)
163 {
164 while (addr->parent)
165 addr = addr->parent;
166 return addr;
167 }
169 gchar*
170 addr_string(address * addr)
171 {
172 static gchar *buffer = NULL;
174 if (addr == NULL) {
175 g_free(buffer);
176 buffer = NULL;
177 return NULL;
178 }
179 if (buffer)
180 g_free(buffer);
182 if (addr->local_part[0] == '\0') {
183 buffer = g_strdup("<>");
184 } else {
185 buffer = g_strdup_printf("<%s@%s>", addr->local_part ? addr->local_part : "", addr->domain ? addr->domain : "");
186 }
187 return buffer;
188 }
190 gint
191 addr_match(address * addr1, address * addr2)
192 {
193 int res;
195 if ((res = fnmatch(addr1->local_part, addr2->local_part, 0)) == 0) {
196 if ((res = fnmatch(addr1->domain, addr2->domain, FNM_CASEFOLD)) == 0)
197 return 0;
198 }
199 return res;
200 }