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@15
|
19 #include <fnmatch.h>
|
meillo@15
|
20
|
meillo@0
|
21 #include "masqmail.h"
|
meillo@0
|
22
|
meillo@10
|
23 table_pair*
|
meillo@10
|
24 create_pair(gchar * key, gpointer value)
|
meillo@0
|
25 {
|
meillo@10
|
26 table_pair *pair;
|
meillo@0
|
27
|
meillo@10
|
28 pair = g_malloc(sizeof(table_pair));
|
meillo@10
|
29 pair->key = g_strdup(key);
|
meillo@10
|
30 pair->value = value;
|
meillo@10
|
31
|
meillo@10
|
32 return pair;
|
meillo@0
|
33 }
|
meillo@0
|
34
|
meillo@10
|
35 table_pair*
|
meillo@10
|
36 create_pair_string(gchar * key, gpointer value)
|
meillo@0
|
37 {
|
meillo@10
|
38 table_pair *pair;
|
meillo@0
|
39
|
meillo@10
|
40 pair = g_malloc(sizeof(table_pair));
|
meillo@10
|
41 pair->key = g_strdup(key);
|
meillo@10
|
42 pair->value = (gpointer) (g_strdup(value));
|
meillo@10
|
43
|
meillo@10
|
44 return pair;
|
meillo@0
|
45 }
|
meillo@0
|
46
|
meillo@10
|
47 table_pair*
|
meillo@10
|
48 parse_table_pair(gchar * line, char delim)
|
meillo@0
|
49 {
|
meillo@10
|
50 gchar buf[256];
|
meillo@10
|
51 gchar *p, *q;
|
meillo@10
|
52 table_pair *pair;
|
meillo@0
|
53
|
meillo@10
|
54 p = line;
|
meillo@10
|
55 q = buf;
|
meillo@15
|
56 while ((*p != '\0') && (*p != delim) && q < buf + 255)
|
meillo@10
|
57 *(q++) = *(p++);
|
meillo@15
|
58 *q = '\0';
|
meillo@0
|
59
|
meillo@10
|
60 pair = g_malloc(sizeof(table_pair));
|
meillo@10
|
61 pair->key = g_strdup(g_strstrip(buf));
|
meillo@0
|
62
|
meillo@10
|
63 if (*p) {
|
meillo@10
|
64 p++;
|
meillo@10
|
65 /* while(isspace(*p)) p++; */
|
meillo@10
|
66 pair->value = (gpointer *) (g_strdup(g_strstrip(p)));
|
meillo@10
|
67 } else
|
meillo@10
|
68 pair->value = (gpointer *) g_strdup("");
|
meillo@0
|
69
|
meillo@10
|
70 return pair;
|
meillo@0
|
71 }
|
meillo@0
|
72
|
meillo@10
|
73 gpointer*
|
meillo@10
|
74 table_find_func(GList * table_list, gchar * key, int (*cmp_func) (const char *, const char *))
|
meillo@0
|
75 {
|
meillo@10
|
76 GList *node;
|
meillo@0
|
77
|
meillo@10
|
78 foreach(table_list, node) {
|
meillo@10
|
79 table_pair *pair = (table_pair *) (node->data);
|
meillo@10
|
80 if (cmp_func(pair->key, key) == 0)
|
meillo@10
|
81 return pair->value;
|
meillo@10
|
82 }
|
meillo@10
|
83 return NULL;
|
meillo@0
|
84 }
|
meillo@0
|
85
|
meillo@10
|
86 gpointer*
|
meillo@10
|
87 table_find(GList * table_list, gchar * key)
|
meillo@0
|
88 {
|
meillo@10
|
89 return table_find_func(table_list, key, strcmp);
|
meillo@0
|
90 }
|
meillo@0
|
91
|
meillo@10
|
92 gpointer*
|
meillo@10
|
93 table_find_case(GList * table_list, gchar * key)
|
meillo@0
|
94 {
|
meillo@10
|
95 return table_find_func(table_list, key, strcasecmp);
|
meillo@0
|
96 }
|
meillo@0
|
97
|
meillo@10
|
98 static int
|
meillo@10
|
99 fnmatch0(const char *pattern, const char *string)
|
meillo@0
|
100 {
|
meillo@10
|
101 return fnmatch(pattern, string, 0);
|
meillo@0
|
102 }
|
meillo@0
|
103
|
meillo@10
|
104 gpointer*
|
meillo@10
|
105 table_find_fnmatch(GList * table_list, gchar * key)
|
meillo@0
|
106 {
|
meillo@10
|
107 return table_find_func(table_list, key, fnmatch0);
|
meillo@0
|
108 }
|
meillo@0
|
109
|
meillo@10
|
110 GList*
|
meillo@10
|
111 table_read(gchar * fname, gchar delim)
|
meillo@0
|
112 {
|
meillo@10
|
113 GList *list = NULL;
|
meillo@10
|
114 FILE *fptr;
|
meillo@0
|
115
|
meillo@10
|
116 if ((fptr = fopen(fname, "rt"))) {
|
meillo@10
|
117 gchar buf[256];
|
meillo@0
|
118
|
meillo@10
|
119 while (fgets(buf, 255, fptr)) {
|
meillo@10
|
120 if (buf[0] && (buf[0] != '#') && (buf[0] != '\n')) {
|
meillo@10
|
121 table_pair *pair;
|
meillo@10
|
122 g_strchomp(buf);
|
meillo@10
|
123 pair = parse_table_pair(buf, delim);
|
meillo@10
|
124 list = g_list_append(list, pair);
|
meillo@10
|
125 }
|
meillo@10
|
126 }
|
meillo@10
|
127 fclose(fptr);
|
meillo@17
|
128 if (list == NULL)
|
meillo@17
|
129 logwrite(LOG_NOTICE, "empty table file %s\n", fname);
|
meillo@10
|
130 return list;
|
meillo@10
|
131 }
|
meillo@17
|
132 logwrite(LOG_ALERT, "could not open table file %s: %s. Thus no aliasing is done\n", fname, strerror(errno));
|
meillo@0
|
133
|
meillo@10
|
134 return NULL;
|
meillo@0
|
135 }
|
meillo@0
|
136
|
meillo@10
|
137 void
|
meillo@10
|
138 destroy_table(GList * table)
|
meillo@0
|
139 {
|
meillo@10
|
140 GList *node;
|
meillo@0
|
141
|
meillo@10
|
142 foreach(table, node) {
|
meillo@10
|
143 table_pair *p = (table_pair *) (node->data);
|
meillo@10
|
144 g_free(p->key);
|
meillo@10
|
145 g_free(p->value);
|
meillo@10
|
146 g_free(p);
|
meillo@10
|
147 }
|
meillo@10
|
148 g_list_free(table);
|
meillo@0
|
149 }
|