meillo@0: /* MasqMail meillo@0: Copyright (C) 2000-2001 Oliver Kurth 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@0: #include "masqmail.h" meillo@0: meillo@0: #define MAX_VAR 50 meillo@0: meillo@10: GList* meillo@366: var_table_rcpt(GList *var_table, address *rcpt) meillo@0: { meillo@10: gchar *tmp_str; meillo@0: meillo@10: var_table = g_list_prepend(var_table, create_pair_string("rcpt_local", rcpt->local_part)); meillo@10: var_table = g_list_prepend(var_table, create_pair_string("rcpt_domain", rcpt->domain)); meillo@10: meillo@10: tmp_str = g_strdup_printf("%s@%s", rcpt->local_part, rcpt->domain); meillo@10: var_table = g_list_prepend(var_table, create_pair_string("rcpt", tmp_str)); meillo@10: g_free(tmp_str); meillo@10: meillo@10: return var_table; meillo@0: } meillo@0: meillo@10: GList* meillo@366: var_table_msg(GList *var_table, message *msg) meillo@0: { meillo@10: address *ret_path = msg->return_path; meillo@10: gchar *tmp_str; meillo@0: meillo@10: var_table = g_list_prepend(var_table, create_pair_string("uid", msg->uid)); meillo@10: var_table = g_list_prepend(var_table, create_pair_string("received_host", msg->received_host ? msg->received_host : "")); meillo@10: var_table = g_list_prepend(var_table, create_pair_string("ident", msg->ident ? msg->ident : "")); meillo@10: var_table = g_list_prepend(var_table, create_pair_string("return_path_local", ret_path->local_part)); meillo@10: var_table = g_list_prepend(var_table, create_pair_string("return_path_domain", ret_path->domain)); meillo@10: meillo@10: tmp_str = g_strdup_printf("%s@%s", ret_path->local_part, ret_path->domain); meillo@10: var_table = g_list_prepend(var_table, create_pair_string("return_path", tmp_str)); meillo@10: g_free(tmp_str); meillo@10: meillo@10: return var_table; meillo@0: } meillo@0: meillo@10: GList* meillo@366: var_table_conf(GList *var_table) meillo@0: { meillo@10: var_table = g_list_prepend(var_table, create_pair_string("host_name", conf.host_name)); meillo@10: var_table = g_list_prepend(var_table, create_pair_string("package", PACKAGE)); meillo@10: var_table = g_list_prepend(var_table, create_pair_string("version", VERSION)); meillo@0: meillo@10: return var_table; meillo@0: } meillo@0: meillo@10: gint meillo@366: expand(GList *var_list, gchar *format, gchar *result, gint result_len) meillo@0: { meillo@10: gchar *p = format, *q = result; meillo@10: gchar *vq; meillo@10: gint i = 0; meillo@10: gboolean escape = FALSE; meillo@0: meillo@10: while (*p && (i < (result_len - 1))) { meillo@10: if ((*p == '$') && !escape) { meillo@10: gchar *value; meillo@10: gchar var[MAX_VAR + 1]; meillo@10: int j = 0; meillo@0: meillo@10: p++; /* skip '$' */ meillo@10: vq = var; meillo@0: meillo@10: if (*p == '{') { meillo@10: /* ${var} style */ meillo@10: p++; /* skip '{' */ meillo@10: while (*p && (*p != '}') && (j < MAX_VAR)) { meillo@10: *(vq++) = *(p++); meillo@10: j++; meillo@10: } meillo@10: p++; meillo@10: } else { meillo@10: /* $var style */ meillo@10: while (*p && (isalnum(*p) || (*p == '_') || (*p == '-')) && (j < MAX_VAR)) { meillo@10: *(vq++) = *(p++); meillo@10: j++; meillo@10: } meillo@10: } meillo@15: *vq = '\0'; meillo@10: meillo@10: if (j < MAX_VAR) { meillo@10: /* search var */ meillo@10: value = (gchar *) table_find(var_list, var); meillo@10: if (value) { meillo@10: gchar *vp = value; meillo@10: while (*vp && (i < (result_len - 1))) { meillo@10: *(q++) = *(vp++); meillo@10: i++; meillo@10: } meillo@10: } meillo@10: } meillo@10: } else { meillo@10: if ((*p == '\\') && (!escape)) { meillo@10: escape = TRUE; meillo@10: } else { meillo@10: *(q++) = *p; meillo@10: i++; meillo@10: escape = FALSE; meillo@10: } meillo@10: p++; meillo@10: } meillo@0: } meillo@15: *q = '\0'; meillo@0: meillo@10: if (i >= (result_len - 1)) meillo@10: return -3; meillo@0: meillo@10: return i; meillo@0: }