masqmail-0.2

view src/address.c @ 13:49dab67fe461

code beautifying
author meillo@marmaro.de
date Wed, 29 Oct 2008 21:10:18 +0100
parents 26e34ae9a3e3
children a8f3424347dc
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, but its only called for that purpose */
47 address*
48 create_address_pipe(gchar * path)
49 {
50 address *addr = g_malloc(sizeof(address));
52 if (addr) {
53 memset(addr, 0, sizeof(address));
54 addr->address = g_strchomp(g_strdup(path));
55 addr->local_part = g_strdup(addr->address);
57 addr->domain = g_strdup("localhost"); /* quick hack */
58 }
59 return addr;
60 }
62 void
63 destroy_address(address * addr)
64 {
65 DEBUG(6) debugf("destroy_address entered\n");
67 g_free(addr->address);
68 g_free(addr->local_part);
69 g_free(addr->domain);
71 g_free(addr);
72 }
74 address*
75 copy_modify_address(const address * orig, gchar * l_part, gchar * dom)
76 {
77 address *addr = NULL;
79 if (orig) {
80 addr = g_malloc(sizeof(address));
81 if (addr) {
82 addr->address = g_strdup(orig->address);
84 if (l_part == NULL)
85 addr->local_part = g_strdup(orig->local_part);
86 else
87 addr->local_part = g_strdup(l_part);
89 if (dom == NULL)
90 addr->domain = g_strdup(orig->domain);
91 else
92 addr->domain = g_strdup(dom);
94 addr->flags = 0;
95 addr->children = NULL;
96 addr->parent = NULL;
97 }
98 }
99 return addr;
100 }
102 gboolean
103 addr_isequal(address * addr1, address * addr2)
104 {
105 return (strcmp(addr1->local_part, addr2->local_part) == 0)
106 && (strcasecmp(addr1->domain, addr2->domain) == 0);
107 }
109 /* searches in ancestors of addr1 */
110 gboolean
111 addr_isequal_parent(address * addr1, address * addr2)
112 {
113 address *addr;
115 for (addr = addr1; addr; addr = addr->parent)
116 if (addr_isequal(addr, addr2))
117 return TRUE;
119 return FALSE;
120 }
122 /* careful, this is recursive */
123 /* returns TRUE if ALL children have been delivered */
124 gboolean
125 addr_is_delivered_children(address * addr)
126 {
127 GList *addr_node;
129 if (addr->children == NULL)
130 return addr_is_delivered(addr);
132 foreach(addr->children, addr_node) {
133 address *addr = (address *) (addr_node->data);
134 if (!addr_is_delivered_children(addr))
135 return FALSE;
136 }
137 return TRUE;
138 }
140 /* careful, this is recursive */
141 /* returns TRUE if ALL children have been either delivered or have failed */
142 gboolean
143 addr_is_finished_children(address * addr)
144 {
145 GList *addr_node;
147 if (addr->children == NULL)
148 return (addr_is_failed(addr) || addr_is_delivered(addr));
150 foreach(addr->children, addr_node) {
151 address *addr = (address *) (addr_node->data);
152 if (!addr_is_finished_children(addr))
153 return FALSE;
154 }
155 return TRUE;
156 }
158 /* find original address */
159 address*
160 addr_find_ancestor(address * addr)
161 {
162 while (addr->parent)
163 addr = addr->parent;
164 return addr;
165 }
167 gchar*
168 addr_string(address * addr)
169 {
170 static gchar *buffer = NULL;
172 if (addr == NULL) {
173 g_free(buffer);
174 buffer = NULL;
175 return NULL;
176 }
177 if (buffer)
178 g_free(buffer);
180 if (addr->local_part[0] == 0) {
181 buffer = g_strdup("<>");
182 } else {
183 buffer = g_strdup_printf("<%s@%s>", addr->local_part ? addr->local_part : "", addr->domain ? addr->domain : "");
184 }
185 return buffer;
186 }
188 gint
189 addr_match(address * addr1, address * addr2)
190 {
191 int res;
193 if ((res = fnmatch(addr1->local_part, addr2->local_part, 0)) == 0) {
194 if ((res = fnmatch(addr1->domain, addr2->domain, FNM_CASEFOLD)) == 0)
195 return 0;
196 }
197 return res;
198 }