masqmail

diff src/expand.c @ 0:08114f7dcc23

this is masqmail-0.2.21 from oliver kurth
author meillo@marmaro.de
date Fri, 26 Sep 2008 17:05:23 +0200
parents
children 26e34ae9a3e3
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/expand.c	Fri Sep 26 17:05:23 2008 +0200
     1.3 @@ -0,0 +1,125 @@
     1.4 +/*  MasqMail
     1.5 +    Copyright (C) 2000-2001 Oliver Kurth
     1.6 +
     1.7 +    This program is free software; you can redistribute it and/or modify
     1.8 +    it under the terms of the GNU General Public License as published by
     1.9 +    the Free Software Foundation; either version 2 of the License, or
    1.10 +    (at your option) any later version.
    1.11 +
    1.12 +    This program is distributed in the hope that it will be useful,
    1.13 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 +    GNU General Public License for more details.
    1.16 +
    1.17 +    You should have received a copy of the GNU General Public License
    1.18 +    along with this program; if not, write to the Free Software
    1.19 +    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    1.20 +*/
    1.21 +
    1.22 +#include "masqmail.h"
    1.23 +
    1.24 +#define MAX_VAR 50
    1.25 +
    1.26 +GList *var_table_rcpt(GList *var_table, address *rcpt)
    1.27 +{
    1.28 +    gchar *tmp_str;
    1.29 +    
    1.30 +    var_table = g_list_prepend(var_table, create_pair_string("rcpt_local", rcpt->local_part));
    1.31 +    var_table = g_list_prepend(var_table, create_pair_string("rcpt_domain", rcpt->domain));
    1.32 +    
    1.33 +    tmp_str = g_strdup_printf("%s@%s", rcpt->local_part, rcpt->domain);
    1.34 +    var_table = g_list_prepend(var_table, create_pair_string("rcpt", tmp_str));
    1.35 +    g_free(tmp_str);
    1.36 +
    1.37 +    return var_table;
    1.38 +}
    1.39 +
    1.40 +GList *var_table_msg(GList *var_table, message *msg)
    1.41 +{
    1.42 +    address *ret_path = msg->return_path;
    1.43 +    gchar *tmp_str;
    1.44 +    
    1.45 +    var_table = g_list_prepend(var_table, create_pair_string("uid", msg->uid));
    1.46 +    var_table = g_list_prepend(var_table, create_pair_string("received_host",
    1.47 +							    msg->received_host ? msg->received_host : ""));
    1.48 +    var_table = g_list_prepend(var_table, create_pair_string("ident", msg->ident ? msg->ident : ""));
    1.49 +    var_table = g_list_prepend(var_table, create_pair_string("return_path_local", ret_path->local_part));
    1.50 +    var_table = g_list_prepend(var_table, create_pair_string("return_path_domain", ret_path->domain));
    1.51 +    
    1.52 +    tmp_str = g_strdup_printf("%s@%s", ret_path->local_part, ret_path->domain);
    1.53 +    var_table = g_list_prepend(var_table, create_pair_string("return_path", tmp_str));
    1.54 +    g_free(tmp_str);
    1.55 +
    1.56 +    return var_table;
    1.57 +}
    1.58 +
    1.59 +GList *var_table_conf(GList *var_table)
    1.60 +{
    1.61 +    var_table = g_list_prepend(var_table, create_pair_string("host_name", conf.host_name));
    1.62 +    var_table = g_list_prepend(var_table, create_pair_string("package", PACKAGE));
    1.63 +    var_table = g_list_prepend(var_table, create_pair_string("version", VERSION));
    1.64 +
    1.65 +    return var_table;
    1.66 +}
    1.67 +
    1.68 +gint expand(GList *var_list, gchar *format, gchar *result, gint result_len)
    1.69 +{
    1.70 +  gchar *p = format, *q = result;
    1.71 +  gchar *vq;
    1.72 +  gint i = 0;
    1.73 +  gboolean escape = FALSE;
    1.74 +
    1.75 +  while(*p && (i < (result_len -1))){
    1.76 +    if((*p == '$') && !escape){
    1.77 +      gchar *value;
    1.78 +      gchar var[MAX_VAR+1];
    1.79 +      int j = 0;
    1.80 +
    1.81 +      p++; /* skip '$' */
    1.82 +      vq = var;
    1.83 +
    1.84 +      if(*p == '{'){
    1.85 +	/* ${var} style */
    1.86 +	p++; /* skip '{' */
    1.87 +	while(*p && (*p != '}') && (j < MAX_VAR)){
    1.88 +	  *(vq++) = *(p++);
    1.89 +	  j++;
    1.90 +	}
    1.91 +	p++;
    1.92 +      }else{
    1.93 +	/* $var style */
    1.94 +	while(*p && (isalnum(*p) || (*p == '_') || (*p == '-')) && (j < MAX_VAR)){
    1.95 +	  *(vq++) = *(p++);
    1.96 +	  j++;
    1.97 +	}
    1.98 +      }
    1.99 +      *vq = 0;
   1.100 +
   1.101 +      if(j < MAX_VAR){
   1.102 +	/* search var */
   1.103 +	value = (gchar *)table_find(var_list, var);
   1.104 +	if(value){
   1.105 +	  gchar *vp = value;
   1.106 +	  while(*vp && (i < (result_len -1))){
   1.107 +	    *(q++) = *(vp++); i++;
   1.108 +	  }
   1.109 +	}
   1.110 +      }
   1.111 +    }else{
   1.112 +      if((*p == '\\') && (!escape)){
   1.113 +	escape = TRUE;
   1.114 +      }else{
   1.115 +	*(q++) = *p; i++;
   1.116 +	escape = FALSE;
   1.117 +      }
   1.118 +      p++;
   1.119 +    }
   1.120 +  }
   1.121 +  *q = 0;
   1.122 +
   1.123 +  if(i >= (result_len -1))
   1.124 +    return -3;
   1.125 +
   1.126 +  return i;
   1.127 +}
   1.128 +