masqmail-0.2

view src/address.c @ 12:9fb7ddbaf129

removed noop-code; some beautifying
author meillo@marmaro.de
date Wed, 29 Oct 2008 16:34:13 +0100
parents 08114f7dcc23
children 49dab67fe461
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);
38 if (addr != NULL) {
39 if (addr->domain == NULL)
40 addr->domain = g_strdup(domain);
41 }
43 return addr;
44 }
46 /* nothing special about pipes here,
47 but its only called for that purpose */
48 address*
49 create_address_pipe(gchar * path)
50 {
51 address *addr = g_malloc(sizeof(address));
53 if (addr) {
54 memset(addr, 0, sizeof(address));
55 addr->address = g_strchomp(g_strdup(path));
56 addr->local_part = g_strdup(addr->address);
58 addr->domain = g_strdup("localhost"); /* quick hack */
59 }
60 return addr;
61 }
63 void
64 destroy_address(address * addr)
65 {
66 DEBUG(6) debugf("destroy_address entered\n");
68 g_free(addr->address);
69 g_free(addr->local_part);
70 g_free(addr->domain);
72 g_free(addr);
73 }
75 address*
76 copy_modify_address(const address * orig, gchar * l_part, gchar * dom)
77 {
78 address *addr = NULL;
80 if (orig) {
81 addr = g_malloc(sizeof(address));
82 if (addr) {
83 addr->address = g_strdup(orig->address);
85 if (l_part == NULL)
86 addr->local_part = g_strdup(orig->local_part);
87 else
88 addr->local_part = g_strdup(l_part);
90 if (dom == NULL)
91 addr->domain = g_strdup(orig->domain);
92 else
93 addr->domain = g_strdup(dom);
95 addr->flags = 0;
96 addr->children = NULL;
97 addr->parent = NULL;
98 }
99 }
100 return addr;
101 }
103 gboolean
104 addr_isequal(address * addr1, address * addr2)
105 {
106 return
107 (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 }