masqmail

annotate src/address.c @ 394:c8e3d1a79313

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