masqmail

view src/tables.c @ 323:29de6a1c4538

Fixed an important bug with folded headers! g_strconcat() returns a *copy* of the string, but hdr->value still pointed to the old header (which probably was a memory leak, too). If the folded part had been quite small it was likely that the new string was at the same position as the old one, thus making everything go well. But if pretty long headers were folded several times it was likely that the new string was allocated somewhere else in memory, thus breaking things. In result mails to lots of recipients (folded header) were frequently only sent to the ones in the first line. Sorry for the inconvenience.
author meillo@marmaro.de
date Fri, 03 Jun 2011 09:47:27 +0200
parents 3b344bf57162
children 41958685480d
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
3 Copyright (C) 2008 markus schnalke <meillo@marmaro.de>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
20 #include <fnmatch.h>
22 #include "masqmail.h"
24 table_pair*
25 create_pair(gchar * key, gpointer value)
26 {
27 table_pair *pair;
29 pair = g_malloc(sizeof(table_pair));
30 pair->key = g_strdup(key);
31 pair->value = value;
33 return pair;
34 }
36 table_pair*
37 create_pair_string(gchar * key, gpointer value)
38 {
39 table_pair *pair;
41 pair = g_malloc(sizeof(table_pair));
42 pair->key = g_strdup(key);
43 pair->value = (gpointer) (g_strdup(value));
45 return pair;
46 }
48 table_pair*
49 parse_table_pair(gchar * line, char delim)
50 {
51 gchar buf[256];
52 gchar *p, *q;
53 table_pair *pair;
55 p = line;
56 q = buf;
57 while ((*p != '\0') && (*p != delim) && q < buf + 255)
58 *(q++) = *(p++);
59 *q = '\0';
61 pair = g_malloc(sizeof(table_pair));
62 pair->key = g_strdup(g_strstrip(buf));
64 if (*p) {
65 p++;
66 /* while(isspace(*p)) p++; */
67 pair->value = (gpointer *) (g_strdup(g_strstrip(p)));
68 } else
69 pair->value = (gpointer *) g_strdup("");
71 return pair;
72 }
74 gpointer*
75 table_find_func(GList * table_list, gchar * key, int (*cmp_func) (const char *, const char *))
76 {
77 GList *node;
79 foreach(table_list, node) {
80 table_pair *pair = (table_pair *) (node->data);
81 if (cmp_func(pair->key, key) == 0)
82 return pair->value;
83 }
84 return NULL;
85 }
87 gpointer*
88 table_find(GList * table_list, gchar * key)
89 {
90 return table_find_func(table_list, key, strcmp);
91 }
93 gpointer*
94 table_find_case(GList * table_list, gchar * key)
95 {
96 return table_find_func(table_list, key, strcasecmp);
97 }
99 static int
100 fnmatch0(const char *pattern, const char *string)
101 {
102 return fnmatch(pattern, string, 0);
103 }
105 gpointer*
106 table_find_fnmatch(GList * table_list, gchar * key)
107 {
108 return table_find_func(table_list, key, fnmatch0);
109 }
111 GList*
112 table_read(gchar * fname, gchar delim)
113 {
114 GList *list = NULL;
115 FILE *fptr;
117 if ((fptr = fopen(fname, "rt"))) {
118 gchar buf[256];
120 while (fgets(buf, 255, fptr)) {
121 if (buf[0] && (buf[0] != '#') && (buf[0] != '\n')) {
122 table_pair *pair;
123 g_strchomp(buf);
124 pair = parse_table_pair(buf, delim);
125 list = g_list_append(list, pair);
126 }
127 }
128 fclose(fptr);
129 if (!list)
130 logwrite(LOG_NOTICE, "table file %s contained no entries\n", fname);
131 return list;
132 }
133 logwrite(LOG_ALERT, "could not open table file %s: %s."
134 " Thus no aliasing will be done\n", fname, strerror(errno));
136 return NULL;
137 }
139 void
140 destroy_table(GList * table)
141 {
142 GList *node;
144 foreach(table, node) {
145 table_pair *p = (table_pair *) (node->data);
146 g_free(p->key);
147 g_free(p->value);
148 g_free(p);
149 }
150 g_list_free(table);
151 }