masqmail

view src/expand.c @ 407:164468dd0953

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