masqmail-0.2

annotate src/expand.c @ 3:8c55886cacd8

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