rev |
line source |
meillo@367
|
1 /*
|
meillo@367
|
2 ** MasqMail
|
meillo@367
|
3 ** Copyright (C) 1999-2001 Oliver Kurth
|
meillo@367
|
4 ** Copyright (C) 2008 markus schnalke <meillo@marmaro.de>
|
meillo@367
|
5 **
|
meillo@367
|
6 ** This program is free software; you can redistribute it and/or modify
|
meillo@367
|
7 ** it under the terms of the GNU General Public License as published by
|
meillo@367
|
8 ** the Free Software Foundation; either version 2 of the License, or
|
meillo@367
|
9 ** (at your option) any later version.
|
meillo@367
|
10 **
|
meillo@367
|
11 ** This program is distributed in the hope that it will be useful,
|
meillo@367
|
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
meillo@367
|
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
meillo@367
|
14 ** GNU General Public License for more details.
|
meillo@367
|
15 **
|
meillo@367
|
16 ** You should have received a copy of the GNU General Public License
|
meillo@367
|
17 ** along with this program; if not, write to the Free Software
|
meillo@367
|
18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
meillo@0
|
19 */
|
meillo@0
|
20
|
meillo@15
|
21 #include <fnmatch.h>
|
meillo@385
|
22 #include "masqmail.h"
|
meillo@15
|
23
|
meillo@0
|
24
|
meillo@10
|
25 table_pair*
|
meillo@366
|
26 create_pair(gchar *key, gpointer value)
|
meillo@0
|
27 {
|
meillo@10
|
28 table_pair *pair;
|
meillo@0
|
29
|
meillo@10
|
30 pair = g_malloc(sizeof(table_pair));
|
meillo@10
|
31 pair->key = g_strdup(key);
|
meillo@10
|
32 pair->value = value;
|
meillo@10
|
33
|
meillo@10
|
34 return pair;
|
meillo@0
|
35 }
|
meillo@0
|
36
|
meillo@10
|
37 table_pair*
|
meillo@366
|
38 create_pair_string(gchar *key, gpointer value)
|
meillo@0
|
39 {
|
meillo@10
|
40 table_pair *pair;
|
meillo@0
|
41
|
meillo@10
|
42 pair = g_malloc(sizeof(table_pair));
|
meillo@10
|
43 pair->key = g_strdup(key);
|
meillo@10
|
44 pair->value = (gpointer) (g_strdup(value));
|
meillo@10
|
45
|
meillo@10
|
46 return pair;
|
meillo@0
|
47 }
|
meillo@0
|
48
|
meillo@10
|
49 table_pair*
|
meillo@366
|
50 parse_table_pair(gchar *line, char delim)
|
meillo@0
|
51 {
|
meillo@10
|
52 gchar buf[256];
|
meillo@10
|
53 gchar *p, *q;
|
meillo@10
|
54 table_pair *pair;
|
meillo@0
|
55
|
meillo@10
|
56 p = line;
|
meillo@10
|
57 q = buf;
|
meillo@385
|
58 while (*p && (*p != delim) && q < buf + 255) {
|
meillo@10
|
59 *(q++) = *(p++);
|
meillo@385
|
60 }
|
meillo@15
|
61 *q = '\0';
|
meillo@0
|
62
|
meillo@10
|
63 pair = g_malloc(sizeof(table_pair));
|
meillo@10
|
64 pair->key = g_strdup(g_strstrip(buf));
|
meillo@0
|
65
|
meillo@10
|
66 if (*p) {
|
meillo@10
|
67 p++;
|
meillo@385
|
68 /* while(isspace(*p)) p++; */
|
meillo@10
|
69 pair->value = (gpointer *) (g_strdup(g_strstrip(p)));
|
meillo@385
|
70 } else {
|
meillo@10
|
71 pair->value = (gpointer *) g_strdup("");
|
meillo@385
|
72 }
|
meillo@0
|
73
|
meillo@10
|
74 return pair;
|
meillo@0
|
75 }
|
meillo@0
|
76
|
meillo@10
|
77 gpointer*
|
meillo@385
|
78 table_find_func(GList *table_list, gchar *key,
|
meillo@385
|
79 int (*cmp_func) (const char *, const char *))
|
meillo@0
|
80 {
|
meillo@10
|
81 GList *node;
|
meillo@0
|
82
|
meillo@10
|
83 foreach(table_list, node) {
|
meillo@10
|
84 table_pair *pair = (table_pair *) (node->data);
|
meillo@385
|
85 if (cmp_func(pair->key, key) == 0) {
|
meillo@10
|
86 return pair->value;
|
meillo@385
|
87 }
|
meillo@10
|
88 }
|
meillo@10
|
89 return NULL;
|
meillo@0
|
90 }
|
meillo@0
|
91
|
meillo@10
|
92 gpointer*
|
meillo@366
|
93 table_find(GList *table_list, gchar *key)
|
meillo@0
|
94 {
|
meillo@10
|
95 return table_find_func(table_list, key, strcmp);
|
meillo@0
|
96 }
|
meillo@0
|
97
|
meillo@10
|
98 gpointer*
|
meillo@366
|
99 table_find_case(GList *table_list, gchar *key)
|
meillo@0
|
100 {
|
meillo@10
|
101 return table_find_func(table_list, key, strcasecmp);
|
meillo@0
|
102 }
|
meillo@0
|
103
|
meillo@10
|
104 static int
|
meillo@10
|
105 fnmatch0(const char *pattern, const char *string)
|
meillo@0
|
106 {
|
meillo@10
|
107 return fnmatch(pattern, string, 0);
|
meillo@0
|
108 }
|
meillo@0
|
109
|
meillo@10
|
110 gpointer*
|
meillo@366
|
111 table_find_fnmatch(GList *table_list, gchar *key)
|
meillo@0
|
112 {
|
meillo@10
|
113 return table_find_func(table_list, key, fnmatch0);
|
meillo@0
|
114 }
|
meillo@0
|
115
|
meillo@10
|
116 GList*
|
meillo@366
|
117 table_read(gchar *fname, gchar delim)
|
meillo@0
|
118 {
|
meillo@10
|
119 GList *list = NULL;
|
meillo@10
|
120 FILE *fptr;
|
meillo@385
|
121 gchar buf[256];
|
meillo@0
|
122
|
meillo@385
|
123 if (!(fptr = fopen(fname, "rt"))) {
|
meillo@385
|
124 logwrite(LOG_ALERT, "could not open table file %s: %s. Thus "
|
meillo@385
|
125 "no aliasing will be done\n",
|
meillo@385
|
126 fname, strerror(errno));
|
meillo@385
|
127 return NULL;
|
meillo@385
|
128 }
|
meillo@0
|
129
|
meillo@385
|
130 while (fgets(buf, sizeof buf, fptr)) {
|
meillo@385
|
131 if (!*buf || *buf == '#' || *buf == '\n') {
|
meillo@385
|
132 continue;
|
meillo@10
|
133 }
|
meillo@385
|
134 table_pair *pair;
|
meillo@385
|
135 g_strchomp(buf);
|
meillo@385
|
136 pair = parse_table_pair(buf, delim);
|
meillo@385
|
137 list = g_list_append(list, pair);
|
meillo@10
|
138 }
|
meillo@385
|
139 fclose(fptr);
|
meillo@385
|
140 if (!list) {
|
meillo@385
|
141 logwrite(LOG_NOTICE, "table file %s contained no entries\n",
|
meillo@385
|
142 fname);
|
meillo@385
|
143 }
|
meillo@385
|
144 return list;
|
meillo@0
|
145 }
|
meillo@0
|
146
|
meillo@10
|
147 void
|
meillo@366
|
148 destroy_table(GList *table)
|
meillo@0
|
149 {
|
meillo@10
|
150 GList *node;
|
meillo@0
|
151
|
meillo@10
|
152 foreach(table, node) {
|
meillo@10
|
153 table_pair *p = (table_pair *) (node->data);
|
meillo@10
|
154 g_free(p->key);
|
meillo@10
|
155 g_free(p->value);
|
meillo@10
|
156 g_free(p);
|
meillo@10
|
157 }
|
meillo@10
|
158 g_list_free(table);
|
meillo@0
|
159 }
|