masqmail

annotate src/expand.c @ 385:01769f722a18

Refactoring in tables.c.
author markus schnalke <meillo@marmaro.de>
date Thu, 16 Feb 2012 13:34:44 +0100
parents 41958685480d
children 0dd84c8f2524
rev   line source
meillo@367 1 /*
meillo@367 2 ** MasqMail
meillo@367 3 ** Copyright (C) 2000-2001 Oliver Kurth
meillo@367 4 **
meillo@367 5 ** This program is free software; you can redistribute it and/or modify
meillo@367 6 ** it under the terms of the GNU General Public License as published by
meillo@367 7 ** the Free Software Foundation; either version 2 of the License, or
meillo@367 8 ** (at your option) any later version.
meillo@367 9 **
meillo@367 10 ** This program is distributed in the hope that it will be useful,
meillo@367 11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@367 12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@367 13 ** GNU General Public License for more details.
meillo@367 14 **
meillo@367 15 ** You should have received a copy of the GNU General Public License
meillo@367 16 ** along with this program; if not, write to the Free Software
meillo@367 17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
meillo@0 18 */
meillo@0 19
meillo@0 20 #include "masqmail.h"
meillo@0 21
meillo@0 22 #define MAX_VAR 50
meillo@0 23
meillo@10 24 GList*
meillo@366 25 var_table_rcpt(GList *var_table, address *rcpt)
meillo@0 26 {
meillo@10 27 gchar *tmp_str;
meillo@0 28
meillo@10 29 var_table = g_list_prepend(var_table, create_pair_string("rcpt_local", rcpt->local_part));
meillo@10 30 var_table = g_list_prepend(var_table, create_pair_string("rcpt_domain", rcpt->domain));
meillo@10 31
meillo@10 32 tmp_str = g_strdup_printf("%s@%s", rcpt->local_part, rcpt->domain);
meillo@10 33 var_table = g_list_prepend(var_table, create_pair_string("rcpt", tmp_str));
meillo@10 34 g_free(tmp_str);
meillo@10 35
meillo@10 36 return var_table;
meillo@0 37 }
meillo@0 38
meillo@10 39 GList*
meillo@366 40 var_table_msg(GList *var_table, message *msg)
meillo@0 41 {
meillo@10 42 address *ret_path = msg->return_path;
meillo@10 43 gchar *tmp_str;
meillo@0 44
meillo@10 45 var_table = g_list_prepend(var_table, create_pair_string("uid", msg->uid));
meillo@10 46 var_table = g_list_prepend(var_table, create_pair_string("received_host", msg->received_host ? msg->received_host : ""));
meillo@10 47 var_table = g_list_prepend(var_table, create_pair_string("ident", msg->ident ? msg->ident : ""));
meillo@10 48 var_table = g_list_prepend(var_table, create_pair_string("return_path_local", ret_path->local_part));
meillo@10 49 var_table = g_list_prepend(var_table, create_pair_string("return_path_domain", ret_path->domain));
meillo@10 50
meillo@10 51 tmp_str = g_strdup_printf("%s@%s", ret_path->local_part, ret_path->domain);
meillo@10 52 var_table = g_list_prepend(var_table, create_pair_string("return_path", tmp_str));
meillo@10 53 g_free(tmp_str);
meillo@10 54
meillo@10 55 return var_table;
meillo@0 56 }
meillo@0 57
meillo@10 58 GList*
meillo@366 59 var_table_conf(GList *var_table)
meillo@0 60 {
meillo@10 61 var_table = g_list_prepend(var_table, create_pair_string("host_name", conf.host_name));
meillo@10 62 var_table = g_list_prepend(var_table, create_pair_string("package", PACKAGE));
meillo@10 63 var_table = g_list_prepend(var_table, create_pair_string("version", VERSION));
meillo@0 64
meillo@10 65 return var_table;
meillo@0 66 }
meillo@0 67
meillo@10 68 gint
meillo@366 69 expand(GList *var_list, gchar *format, gchar *result, gint result_len)
meillo@0 70 {
meillo@10 71 gchar *p = format, *q = result;
meillo@10 72 gchar *vq;
meillo@10 73 gint i = 0;
meillo@10 74 gboolean escape = FALSE;
meillo@0 75
meillo@10 76 while (*p && (i < (result_len - 1))) {
meillo@10 77 if ((*p == '$') && !escape) {
meillo@10 78 gchar *value;
meillo@10 79 gchar var[MAX_VAR + 1];
meillo@10 80 int j = 0;
meillo@0 81
meillo@10 82 p++; /* skip '$' */
meillo@10 83 vq = var;
meillo@0 84
meillo@10 85 if (*p == '{') {
meillo@10 86 /* ${var} style */
meillo@10 87 p++; /* skip '{' */
meillo@10 88 while (*p && (*p != '}') && (j < MAX_VAR)) {
meillo@10 89 *(vq++) = *(p++);
meillo@10 90 j++;
meillo@10 91 }
meillo@10 92 p++;
meillo@10 93 } else {
meillo@10 94 /* $var style */
meillo@10 95 while (*p && (isalnum(*p) || (*p == '_') || (*p == '-')) && (j < MAX_VAR)) {
meillo@10 96 *(vq++) = *(p++);
meillo@10 97 j++;
meillo@10 98 }
meillo@10 99 }
meillo@15 100 *vq = '\0';
meillo@10 101
meillo@10 102 if (j < MAX_VAR) {
meillo@10 103 /* search var */
meillo@10 104 value = (gchar *) table_find(var_list, var);
meillo@10 105 if (value) {
meillo@10 106 gchar *vp = value;
meillo@10 107 while (*vp && (i < (result_len - 1))) {
meillo@10 108 *(q++) = *(vp++);
meillo@10 109 i++;
meillo@10 110 }
meillo@10 111 }
meillo@10 112 }
meillo@10 113 } else {
meillo@10 114 if ((*p == '\\') && (!escape)) {
meillo@10 115 escape = TRUE;
meillo@10 116 } else {
meillo@10 117 *(q++) = *p;
meillo@10 118 i++;
meillo@10 119 escape = FALSE;
meillo@10 120 }
meillo@10 121 p++;
meillo@10 122 }
meillo@0 123 }
meillo@15 124 *q = '\0';
meillo@0 125
meillo@10 126 if (i >= (result_len - 1))
meillo@10 127 return -3;
meillo@0 128
meillo@10 129 return i;
meillo@0 130 }