masqmail-0.2

view src/expand.c @ 9:31cc8a89cb74

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