masqmail-0.2

view src/expand.c @ 27:3654c502a4df

g_malloc terminates the program on failure automatically
author meillo@marmaro.de
date Thu, 06 May 2010 11:50:40 +0200
parents 26e34ae9a3e3
children
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*
24 var_table_rcpt(GList * var_table, address * rcpt)
25 {
26 gchar *tmp_str;
28 var_table = g_list_prepend(var_table, create_pair_string("rcpt_local", rcpt->local_part));
29 var_table = g_list_prepend(var_table, create_pair_string("rcpt_domain", rcpt->domain));
31 tmp_str = g_strdup_printf("%s@%s", rcpt->local_part, rcpt->domain);
32 var_table = g_list_prepend(var_table, create_pair_string("rcpt", tmp_str));
33 g_free(tmp_str);
35 return var_table;
36 }
38 GList*
39 var_table_msg(GList * var_table, message * msg)
40 {
41 address *ret_path = msg->return_path;
42 gchar *tmp_str;
44 var_table = g_list_prepend(var_table, create_pair_string("uid", msg->uid));
45 var_table = g_list_prepend(var_table, create_pair_string("received_host", msg->received_host ? msg->received_host : ""));
46 var_table = g_list_prepend(var_table, create_pair_string("ident", msg->ident ? msg->ident : ""));
47 var_table = g_list_prepend(var_table, create_pair_string("return_path_local", ret_path->local_part));
48 var_table = g_list_prepend(var_table, create_pair_string("return_path_domain", ret_path->domain));
50 tmp_str = g_strdup_printf("%s@%s", ret_path->local_part, ret_path->domain);
51 var_table = g_list_prepend(var_table, create_pair_string("return_path", tmp_str));
52 g_free(tmp_str);
54 return var_table;
55 }
57 GList*
58 var_table_conf(GList * var_table)
59 {
60 var_table = g_list_prepend(var_table, create_pair_string("host_name", conf.host_name));
61 var_table = g_list_prepend(var_table, create_pair_string("package", PACKAGE));
62 var_table = g_list_prepend(var_table, create_pair_string("version", VERSION));
64 return var_table;
65 }
67 gint
68 expand(GList * var_list, gchar * format, gchar * result, gint result_len)
69 {
70 gchar *p = format, *q = result;
71 gchar *vq;
72 gint i = 0;
73 gboolean escape = FALSE;
75 while (*p && (i < (result_len - 1))) {
76 if ((*p == '$') && !escape) {
77 gchar *value;
78 gchar var[MAX_VAR + 1];
79 int j = 0;
81 p++; /* skip '$' */
82 vq = var;
84 if (*p == '{') {
85 /* ${var} style */
86 p++; /* skip '{' */
87 while (*p && (*p != '}') && (j < MAX_VAR)) {
88 *(vq++) = *(p++);
89 j++;
90 }
91 p++;
92 } else {
93 /* $var style */
94 while (*p && (isalnum(*p) || (*p == '_') || (*p == '-')) && (j < MAX_VAR)) {
95 *(vq++) = *(p++);
96 j++;
97 }
98 }
99 *vq = '\0';
101 if (j < MAX_VAR) {
102 /* search var */
103 value = (gchar *) table_find(var_list, var);
104 if (value) {
105 gchar *vp = value;
106 while (*vp && (i < (result_len - 1))) {
107 *(q++) = *(vp++);
108 i++;
109 }
110 }
111 }
112 } else {
113 if ((*p == '\\') && (!escape)) {
114 escape = TRUE;
115 } else {
116 *(q++) = *p;
117 i++;
118 escape = FALSE;
119 }
120 p++;
121 }
122 }
123 *q = '\0';
125 if (i >= (result_len - 1))
126 return -3;
128 return i;
129 }