masqmail

view src/expand.c @ 369:19d57eb1b6a1

Minor refactoring change (else-if).
author markus schnalke <meillo@marmaro.de>
date Thu, 20 Oct 2011 10:23:47 +0200
parents 41958685480d
children 0dd84c8f2524
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, create_pair_string("rcpt_local", rcpt->local_part));
30 var_table = g_list_prepend(var_table, create_pair_string("rcpt_domain", rcpt->domain));
32 tmp_str = g_strdup_printf("%s@%s", rcpt->local_part, rcpt->domain);
33 var_table = g_list_prepend(var_table, create_pair_string("rcpt", tmp_str));
34 g_free(tmp_str);
36 return var_table;
37 }
39 GList*
40 var_table_msg(GList *var_table, message *msg)
41 {
42 address *ret_path = msg->return_path;
43 gchar *tmp_str;
45 var_table = g_list_prepend(var_table, create_pair_string("uid", msg->uid));
46 var_table = g_list_prepend(var_table, create_pair_string("received_host", msg->received_host ? msg->received_host : ""));
47 var_table = g_list_prepend(var_table, create_pair_string("ident", msg->ident ? msg->ident : ""));
48 var_table = g_list_prepend(var_table, create_pair_string("return_path_local", ret_path->local_part));
49 var_table = g_list_prepend(var_table, create_pair_string("return_path_domain", ret_path->domain));
51 tmp_str = g_strdup_printf("%s@%s", ret_path->local_part, ret_path->domain);
52 var_table = g_list_prepend(var_table, create_pair_string("return_path", tmp_str));
53 g_free(tmp_str);
55 return var_table;
56 }
58 GList*
59 var_table_conf(GList *var_table)
60 {
61 var_table = g_list_prepend(var_table, create_pair_string("host_name", conf.host_name));
62 var_table = g_list_prepend(var_table, create_pair_string("package", PACKAGE));
63 var_table = g_list_prepend(var_table, create_pair_string("version", VERSION));
65 return var_table;
66 }
68 gint
69 expand(GList *var_list, gchar *format, gchar *result, gint result_len)
70 {
71 gchar *p = format, *q = result;
72 gchar *vq;
73 gint i = 0;
74 gboolean escape = FALSE;
76 while (*p && (i < (result_len - 1))) {
77 if ((*p == '$') && !escape) {
78 gchar *value;
79 gchar var[MAX_VAR + 1];
80 int j = 0;
82 p++; /* skip '$' */
83 vq = var;
85 if (*p == '{') {
86 /* ${var} style */
87 p++; /* skip '{' */
88 while (*p && (*p != '}') && (j < MAX_VAR)) {
89 *(vq++) = *(p++);
90 j++;
91 }
92 p++;
93 } else {
94 /* $var style */
95 while (*p && (isalnum(*p) || (*p == '_') || (*p == '-')) && (j < MAX_VAR)) {
96 *(vq++) = *(p++);
97 j++;
98 }
99 }
100 *vq = '\0';
102 if (j < MAX_VAR) {
103 /* search var */
104 value = (gchar *) table_find(var_list, var);
105 if (value) {
106 gchar *vp = value;
107 while (*vp && (i < (result_len - 1))) {
108 *(q++) = *(vp++);
109 i++;
110 }
111 }
112 }
113 } else {
114 if ((*p == '\\') && (!escape)) {
115 escape = TRUE;
116 } else {
117 *(q++) = *p;
118 i++;
119 escape = FALSE;
120 }
121 p++;
122 }
123 }
124 *q = '\0';
126 if (i >= (result_len - 1))
127 return -3;
129 return i;
130 }