meillo@0: /* MasqMail meillo@0: Copyright (C) 1999-2001 Oliver Kurth meillo@76: Copyright (C) 2008 markus schnalke meillo@0: meillo@0: This program is free software; you can redistribute it and/or modify meillo@0: it under the terms of the GNU General Public License as published by meillo@0: the Free Software Foundation; either version 2 of the License, or meillo@0: (at your option) any later version. meillo@0: meillo@0: This program is distributed in the hope that it will be useful, meillo@0: but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@0: GNU General Public License for more details. meillo@0: meillo@0: You should have received a copy of the GNU General Public License meillo@0: along with this program; if not, write to the Free Software meillo@0: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. meillo@0: */ meillo@0: meillo@15: #include meillo@15: meillo@0: #include "masqmail.h" meillo@0: meillo@10: table_pair* meillo@366: create_pair(gchar *key, gpointer value) meillo@0: { meillo@10: table_pair *pair; meillo@0: meillo@10: pair = g_malloc(sizeof(table_pair)); meillo@10: pair->key = g_strdup(key); meillo@10: pair->value = value; meillo@10: meillo@10: return pair; meillo@0: } meillo@0: meillo@10: table_pair* meillo@366: create_pair_string(gchar *key, gpointer value) meillo@0: { meillo@10: table_pair *pair; meillo@0: meillo@10: pair = g_malloc(sizeof(table_pair)); meillo@10: pair->key = g_strdup(key); meillo@10: pair->value = (gpointer) (g_strdup(value)); meillo@10: meillo@10: return pair; meillo@0: } meillo@0: meillo@10: table_pair* meillo@366: parse_table_pair(gchar *line, char delim) meillo@0: { meillo@10: gchar buf[256]; meillo@10: gchar *p, *q; meillo@10: table_pair *pair; meillo@0: meillo@10: p = line; meillo@10: q = buf; meillo@15: while ((*p != '\0') && (*p != delim) && q < buf + 255) meillo@10: *(q++) = *(p++); meillo@15: *q = '\0'; meillo@0: meillo@10: pair = g_malloc(sizeof(table_pair)); meillo@10: pair->key = g_strdup(g_strstrip(buf)); meillo@0: meillo@10: if (*p) { meillo@10: p++; meillo@10: /* while(isspace(*p)) p++; */ meillo@10: pair->value = (gpointer *) (g_strdup(g_strstrip(p))); meillo@10: } else meillo@10: pair->value = (gpointer *) g_strdup(""); meillo@0: meillo@10: return pair; meillo@0: } meillo@0: meillo@10: gpointer* meillo@366: table_find_func(GList *table_list, gchar *key, int (*cmp_func) (const char *, const char *)) meillo@0: { meillo@10: GList *node; meillo@0: meillo@10: foreach(table_list, node) { meillo@10: table_pair *pair = (table_pair *) (node->data); meillo@10: if (cmp_func(pair->key, key) == 0) meillo@10: return pair->value; meillo@10: } meillo@10: return NULL; meillo@0: } meillo@0: meillo@10: gpointer* meillo@366: table_find(GList *table_list, gchar *key) meillo@0: { meillo@10: return table_find_func(table_list, key, strcmp); meillo@0: } meillo@0: meillo@10: gpointer* meillo@366: table_find_case(GList *table_list, gchar *key) meillo@0: { meillo@10: return table_find_func(table_list, key, strcasecmp); meillo@0: } meillo@0: meillo@10: static int meillo@10: fnmatch0(const char *pattern, const char *string) meillo@0: { meillo@10: return fnmatch(pattern, string, 0); meillo@0: } meillo@0: meillo@10: gpointer* meillo@366: table_find_fnmatch(GList *table_list, gchar *key) meillo@0: { meillo@10: return table_find_func(table_list, key, fnmatch0); meillo@0: } meillo@0: meillo@10: GList* meillo@366: table_read(gchar *fname, gchar delim) meillo@0: { meillo@10: GList *list = NULL; meillo@10: FILE *fptr; meillo@0: meillo@10: if ((fptr = fopen(fname, "rt"))) { meillo@10: gchar buf[256]; meillo@0: meillo@10: while (fgets(buf, 255, fptr)) { meillo@10: if (buf[0] && (buf[0] != '#') && (buf[0] != '\n')) { meillo@10: table_pair *pair; meillo@10: g_strchomp(buf); meillo@10: pair = parse_table_pair(buf, delim); meillo@10: list = g_list_append(list, pair); meillo@10: } meillo@10: } meillo@10: fclose(fptr); meillo@238: if (!list) meillo@238: logwrite(LOG_NOTICE, "table file %s contained no entries\n", fname); meillo@10: return list; meillo@10: } meillo@238: logwrite(LOG_ALERT, "could not open table file %s: %s." meillo@238: " Thus no aliasing will be done\n", fname, strerror(errno)); meillo@0: meillo@10: return NULL; meillo@0: } meillo@0: meillo@10: void meillo@366: destroy_table(GList *table) meillo@0: { meillo@10: GList *node; meillo@0: meillo@10: foreach(table, node) { meillo@10: table_pair *p = (table_pair *) (node->data); meillo@10: g_free(p->key); meillo@10: g_free(p->value); meillo@10: g_free(p); meillo@10: } meillo@10: g_list_free(table); meillo@0: }