masqmail

annotate src/expand.c @ 323:29de6a1c4538

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