masqmail
diff src/address.c @ 0:08114f7dcc23
this is masqmail-0.2.21 from oliver kurth
author | meillo@marmaro.de |
---|---|
date | Fri, 26 Sep 2008 17:05:23 +0200 |
parents | |
children | 26e34ae9a3e3 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/address.c Fri Sep 26 17:05:23 2008 +0200 1.3 @@ -0,0 +1,189 @@ 1.4 +/* MasqMail 1.5 + Copyright (C) 1999-2001 Oliver Kurth 1.6 + 1.7 + This program is free software; you can redistribute it and/or modify 1.8 + it under the terms of the GNU General Public License as published by 1.9 + the Free Software Foundation; either version 2 of the License, or 1.10 + (at your option) any later version. 1.11 + 1.12 + This program is distributed in the hope that it will be useful, 1.13 + but WITHOUT ANY WARRANTY; without even the implied warranty of 1.14 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.15 + GNU General Public License for more details. 1.16 + 1.17 + You should have received a copy of the GNU General Public License 1.18 + along with this program; if not, write to the Free Software 1.19 + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 1.20 +*/ 1.21 + 1.22 +#include "masqmail.h" 1.23 +#include <fnmatch.h> 1.24 + 1.25 +address *create_address(gchar *path, gboolean is_rfc821) 1.26 +{ 1.27 + address *addr; 1.28 + addr = _create_address(path, NULL, is_rfc821); 1.29 + 1.30 + if(addr != NULL){ 1.31 + addr_unmark_delivered(addr); 1.32 + } 1.33 + return addr; 1.34 +} 1.35 + 1.36 +address *create_address_qualified(gchar *path, gboolean is_rfc821, 1.37 + gchar *domain) 1.38 +{ 1.39 + address *addr = create_address(path, is_rfc821); 1.40 + if(addr != NULL){ 1.41 + if(addr->domain == NULL) 1.42 + addr->domain = g_strdup(domain); 1.43 + } 1.44 + 1.45 + return addr; 1.46 +} 1.47 + 1.48 +/* nothing special about pipes here, 1.49 + but its only called for that purpose */ 1.50 +address *create_address_pipe(gchar *path) 1.51 +{ 1.52 + address *addr = g_malloc(sizeof(address)); 1.53 + 1.54 + if(addr){ 1.55 + memset(addr, 0, sizeof(address)); 1.56 + addr->address = g_strchomp(g_strdup(path)); 1.57 + addr->local_part = g_strdup(addr->address); 1.58 + 1.59 + addr->domain = g_strdup("localhost"); /* quick hack */ 1.60 + } 1.61 + return addr; 1.62 +} 1.63 + 1.64 +void destroy_address(address *addr) 1.65 +{ 1.66 + DEBUG(6) debugf("destroy_address entered\n"); 1.67 + 1.68 + g_free(addr->address); 1.69 + g_free(addr->local_part); 1.70 + g_free(addr->domain); 1.71 + 1.72 + g_free(addr); 1.73 +} 1.74 + 1.75 +address *copy_modify_address(const address *orig, gchar *l_part, gchar *dom) 1.76 +{ 1.77 + address *addr = NULL; 1.78 + 1.79 + if(orig){ 1.80 + addr = g_malloc(sizeof(address)); 1.81 + if(addr){ 1.82 + addr->address = g_strdup(orig->address); 1.83 + 1.84 + if(l_part == NULL) 1.85 + addr->local_part = g_strdup(orig->local_part); 1.86 + else 1.87 + addr->local_part = g_strdup(l_part); 1.88 + 1.89 + if(dom == NULL) 1.90 + addr->domain = g_strdup(orig->domain); 1.91 + else 1.92 + addr->domain = g_strdup(dom); 1.93 + 1.94 + addr->flags = 0; 1.95 + addr->children = NULL; 1.96 + addr->parent = NULL; 1.97 + } 1.98 + } 1.99 + return addr; 1.100 +} 1.101 + 1.102 +gboolean addr_isequal(address *addr1, address *addr2) 1.103 +{ 1.104 + return 1.105 + (strcmp(addr1->local_part, addr2->local_part) == 0) && 1.106 + (strcasecmp(addr1->domain, addr2->domain) == 0); 1.107 +} 1.108 + 1.109 +/* searches in ancestors of addr1 */ 1.110 +gboolean addr_isequal_parent(address *addr1, address *addr2) 1.111 +{ 1.112 + address *addr; 1.113 + 1.114 + for(addr = addr1; addr; addr = addr->parent) 1.115 + if(addr_isequal(addr, addr2)) 1.116 + return TRUE; 1.117 + 1.118 + return FALSE; 1.119 +} 1.120 + 1.121 +/* careful, this is recursive */ 1.122 +/* returns TRUE if ALL children have been delivered */ 1.123 +gboolean addr_is_delivered_children(address *addr) 1.124 +{ 1.125 + GList *addr_node; 1.126 + 1.127 + if(addr->children == NULL) return addr_is_delivered(addr); 1.128 + 1.129 + foreach(addr->children, addr_node){ 1.130 + address *addr = (address *)(addr_node->data); 1.131 + if(!addr_is_delivered_children(addr)) 1.132 + return FALSE; 1.133 + } 1.134 + return TRUE; 1.135 +} 1.136 + 1.137 +/* careful, this is recursive */ 1.138 +/* returns TRUE if ALL children have been either delivered or have failed */ 1.139 +gboolean addr_is_finished_children(address *addr) 1.140 +{ 1.141 + GList *addr_node; 1.142 + 1.143 + if(addr->children == NULL) return (addr_is_failed(addr) || addr_is_delivered(addr)); 1.144 + 1.145 + foreach(addr->children, addr_node){ 1.146 + address *addr = (address *)(addr_node->data); 1.147 + if(!addr_is_finished_children(addr)) 1.148 + return FALSE; 1.149 + } 1.150 + return TRUE; 1.151 +} 1.152 + 1.153 +/* find original address */ 1.154 +address *addr_find_ancestor(address *addr) 1.155 +{ 1.156 + while(addr->parent) addr = addr->parent; 1.157 + return addr; 1.158 +} 1.159 + 1.160 +gchar *addr_string(address *addr) 1.161 +{ 1.162 + static gchar *buffer = NULL; 1.163 + 1.164 + if(addr == NULL){ 1.165 + g_free(buffer); 1.166 + buffer = NULL; 1.167 + return NULL; 1.168 + } 1.169 + if(buffer) 1.170 + g_free(buffer); 1.171 + 1.172 + if(addr->local_part[0] == 0){ 1.173 + buffer = g_strdup("<>"); 1.174 + }else{ 1.175 + buffer = g_strdup_printf("<%s@%s>", 1.176 + addr->local_part ? addr->local_part : "", 1.177 + addr->domain ? addr->domain : ""); 1.178 + } 1.179 + return buffer; 1.180 +} 1.181 + 1.182 +gint addr_match(address *addr1, address *addr2) 1.183 +{ 1.184 + int res; 1.185 + 1.186 + if((res = fnmatch(addr1->local_part, addr2->local_part, 0)) == 0){ 1.187 + if((res = fnmatch(addr1->domain, addr2->domain, FNM_CASEFOLD)) == 0) 1.188 + return 0; 1.189 + } 1.190 + return res; 1.191 +} 1.192 +