masqmail

annotate src/address.c @ 352:4905a1d9e6a7

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