masqmail

diff src/fail_msg.c @ 10:26e34ae9a3e3

changed indention and line wrapping to a more consistent style
author meillo@marmaro.de
date Mon, 27 Oct 2008 16:23:10 +0100
parents 08114f7dcc23
children f671821d8222
line diff
     1.1 --- a/src/fail_msg.c	Mon Oct 27 16:21:27 2008 +0100
     1.2 +++ b/src/fail_msg.c	Mon Oct 27 16:23:10 2008 +0100
     1.3 @@ -5,7 +5,7 @@
     1.4   * it under the terms of the GNU General Public License as published by
     1.5   * the Free Software Foundation; either version 2 of the License, or
     1.6   * (at your option) any later version.
     1.7 - * 
     1.8 + *
     1.9   * This program is distributed in the hope that it will be useful,
    1.10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.12 @@ -22,109 +22,107 @@
    1.13  #include "peopen.h"
    1.14  #include "readsock.h"
    1.15  
    1.16 -gboolean fail_msg(message *msg, gchar *template,
    1.17 -		  GList *failed_rcpts, gchar *err_fmt, va_list args)
    1.18 +gboolean
    1.19 +fail_msg(message * msg, gchar * template, GList * failed_rcpts, gchar * err_fmt, va_list args)
    1.20  {
    1.21 -  gboolean ok = FALSE;
    1.22 -  address *ret_path = NULL;
    1.23 +	gboolean ok = FALSE;
    1.24 +	address *ret_path = NULL;
    1.25  
    1.26 -  /* do not bounce bounces, send to postmaster instead */
    1.27 -  if(msg->return_path->local_part[0] == 0){
    1.28 -    GList *node;
    1.29 +	/* do not bounce bounces, send to postmaster instead */
    1.30 +	if (msg->return_path->local_part[0] == 0) {
    1.31 +		GList *node;
    1.32  
    1.33 -    ret_path = create_address_qualified("postmaster", TRUE, conf.host_name);
    1.34 -    foreach(failed_rcpts, node){
    1.35 -      address *addr = (address *)(node->data);
    1.36 -      if(addr_isequal_parent(addr, ret_path)){
    1.37 -	logwrite(LOG_ALERT, "%s == %s: postmaster address failed\n",
    1.38 -		 msg->uid, addr_string(ret_path));
    1.39 -	return FALSE;
    1.40 -      }
    1.41 -    }
    1.42 -  }else
    1.43 -    ret_path = copy_address(msg->return_path);
    1.44 +		ret_path = create_address_qualified("postmaster", TRUE, conf.host_name);
    1.45 +		foreach(failed_rcpts, node) {
    1.46 +			address *addr = (address *) (node->data);
    1.47 +			if (addr_isequal_parent(addr, ret_path)) {
    1.48 +				logwrite(LOG_ALERT, "%s == %s: postmaster address failed\n", msg->uid, addr_string(ret_path));
    1.49 +				return FALSE;
    1.50 +			}
    1.51 +		}
    1.52 +	} else
    1.53 +		ret_path = copy_address(msg->return_path);
    1.54  
    1.55 -  DEBUG(1) debugf("sending failure notice to %s.\n", addr_string(ret_path));
    1.56 +	DEBUG(1) debugf("sending failure notice to %s.\n", addr_string(ret_path));
    1.57  
    1.58 -  if(template){
    1.59 -    FILE *file;
    1.60 -    GList *var_table = var_table_conf(var_table_msg(NULL, msg));
    1.61 -    gchar *err_msg = g_strdup_vprintf(err_fmt, args);
    1.62 +	if (template) {
    1.63 +		FILE *file;
    1.64 +		GList *var_table = var_table_conf(var_table_msg(NULL, msg));
    1.65 +		gchar *err_msg = g_strdup_vprintf(err_fmt, args);
    1.66  
    1.67 -    var_table = g_list_prepend(var_table, create_pair_string("err_msg", err_msg));
    1.68 -    g_free(err_msg);
    1.69 +		var_table = g_list_prepend(var_table, create_pair_string("err_msg", err_msg));
    1.70 +		g_free(err_msg);
    1.71  
    1.72 -    if((file = fopen(template, "r"))){
    1.73 -      FILE *out;
    1.74 -      gchar *cmd;
    1.75 -      pid_t pid;
    1.76 +		if ((file = fopen(template, "r"))) {
    1.77 +			FILE *out;
    1.78 +			gchar *cmd;
    1.79 +			pid_t pid;
    1.80  
    1.81 -      //      cmd = g_strdup_printf(SBINDIR"/masqmail -oi -f \"<>\" %s@%s",
    1.82 -      //			    ret_path->local_part, ret_path->domain);
    1.83 -      cmd = g_strdup_printf(SBINDIR"/masqmail -oi -f <> %s@%s",
    1.84 -			    ret_path->local_part, ret_path->domain);
    1.85 -      if((out = peidopen(cmd, "w", environ, &pid, conf.mail_uid, conf.mail_gid))){
    1.86 -	gchar fmt[256], line[256];
    1.87 -	int status, ret;
    1.88 +			//      cmd = g_strdup_printf(SBINDIR"/masqmail -oi -f \"<>\" %s@%s",
    1.89 +			//                ret_path->local_part, ret_path->domain);
    1.90 +			cmd = g_strdup_printf(SBINDIR "/masqmail -oi -f <> %s@%s", ret_path->local_part, ret_path->domain);
    1.91 +			if ((out = peidopen(cmd, "w", environ, &pid, conf.mail_uid, conf.mail_gid))) {
    1.92 +				gchar fmt[256], line[256];
    1.93 +				int status, ret;
    1.94  
    1.95 -	while((ret = read_sockline(file, fmt, 256, 0, 0)) > 0){
    1.96 -	  if(fmt[0] == '@'){
    1.97 -	    GList *node;
    1.98 -	    if(strncmp(fmt, "@failed_rcpts", 13) == 0){
    1.99 -	      foreach(failed_rcpts, node){
   1.100 -		address *rcpt = (address *)(node->data);
   1.101 -		fprintf(out, "\t%s\n", addr_string(rcpt));
   1.102 -	      }
   1.103 -	    }else if(strncmp(fmt, "@msg_headers", 12) == 0){
   1.104 -	      foreach(msg->hdr_list, node){
   1.105 -		header *hdr = (header *)(node->data);
   1.106 -		fputs(hdr->header, out);
   1.107 -	      }
   1.108 -	    }else if(strncmp(fmt, "@msg_body", 9) == 0){
   1.109 -	      /* we may have to read the data at this point
   1.110 -		 and remember if we did */
   1.111 -	      gboolean flag = (msg->data_list == NULL);
   1.112 -	      if(flag){
   1.113 -		if(!spool_read_data(msg)){
   1.114 -		  logwrite(LOG_ALERT, "could not open data spool file %s\n",
   1.115 -			   msg->uid);
   1.116 -		}
   1.117 -	      }
   1.118 -	      foreach(msg->data_list, node){
   1.119 -		gchar *line = (gchar *)(node->data);
   1.120 -		fputs(line, out);
   1.121 -	      }
   1.122 -	      if(flag) msg_free_data(msg);
   1.123 -	    }
   1.124 -	  }else{
   1.125 -	    expand(var_table, fmt, line, 256);
   1.126 -	    fputs(line, out);
   1.127 -	  }
   1.128 +				while ((ret = read_sockline(file, fmt, 256, 0, 0)) > 0) {
   1.129 +					if (fmt[0] == '@') {
   1.130 +						GList *node;
   1.131 +						if (strncmp(fmt, "@failed_rcpts", 13) == 0) {
   1.132 +							foreach(failed_rcpts, node) {
   1.133 +								address *rcpt = (address *) (node->data);
   1.134 +								fprintf(out, "\t%s\n", addr_string(rcpt));
   1.135 +							}
   1.136 +						} else if (strncmp(fmt, "@msg_headers", 12) == 0) {
   1.137 +							foreach(msg->hdr_list, node) {
   1.138 +								header *hdr = (header *) (node->data);
   1.139 +								fputs(hdr->header, out);
   1.140 +							}
   1.141 +						} else if (strncmp(fmt, "@msg_body", 9) == 0) {
   1.142 +							/* we may have to read the data at this point
   1.143 +							   and remember if we did */
   1.144 +							gboolean flag = (msg->data_list == NULL);
   1.145 +							if (flag) {
   1.146 +								if (!spool_read_data(msg)) {
   1.147 +									logwrite(LOG_ALERT, "could not open data spool file %s\n", msg->uid);
   1.148 +								}
   1.149 +							}
   1.150 +							foreach(msg->data_list, node) {
   1.151 +								gchar *line = (gchar *) (node->data);
   1.152 +								fputs(line, out);
   1.153 +							}
   1.154 +							if (flag)
   1.155 +								msg_free_data(msg);
   1.156 +						}
   1.157 +					} else {
   1.158 +						expand(var_table, fmt, line, 256);
   1.159 +						fputs(line, out);
   1.160 +					}
   1.161 +				}
   1.162 +
   1.163 +				fclose(out);
   1.164 +				waitpid(pid, &status, 0);
   1.165 +				if ((WEXITSTATUS(status) != EXIT_SUCCESS) || WIFSIGNALED(status)) {
   1.166 +					if (WEXITSTATUS(status) != EXIT_SUCCESS)
   1.167 +						logwrite(LOG_WARNING, "child returned %d\n", WEXITSTATUS(status));
   1.168 +					if (WIFSIGNALED(status))
   1.169 +						logwrite(LOG_WARNING, "child got signal: %d\n", WTERMSIG(status));
   1.170 +				} else
   1.171 +					ok = TRUE;
   1.172 +			} else {
   1.173 +				logwrite(LOG_ERR, "peopen failed: %s\n", strerror(errno));
   1.174 +			}
   1.175 +			g_free(cmd);
   1.176 +			fclose(file);
   1.177 +		} else
   1.178 +			logwrite(LOG_ALERT, "could not open failure message template %s: %s\n", conf.errmsg_file, strerror(errno));
   1.179 +
   1.180 +		destroy_table(var_table);
   1.181  	}
   1.182  
   1.183 -	fclose(out);
   1.184 -	waitpid(pid, &status, 0);
   1.185 -	if((WEXITSTATUS(status) != EXIT_SUCCESS) || WIFSIGNALED(status)){
   1.186 -	  if(WEXITSTATUS(status) != EXIT_SUCCESS)
   1.187 -	    logwrite(LOG_WARNING, "child returned %d\n", WEXITSTATUS(status));
   1.188 -	  if(WIFSIGNALED(status))
   1.189 -	    logwrite(LOG_WARNING, "child got signal: %d\n", WTERMSIG(status));
   1.190 -	}else ok = TRUE;
   1.191 -      }else{
   1.192 -	logwrite(LOG_ERR, "peopen failed: %s\n", strerror(errno));
   1.193 -      }
   1.194 -      g_free(cmd);
   1.195 -      fclose(file);
   1.196 -    }else
   1.197 -      logwrite(LOG_ALERT, "could not open failure message template %s: %s\n",
   1.198 -	       conf.errmsg_file, strerror(errno));
   1.199 +	destroy_address(ret_path);
   1.200  
   1.201 -    destroy_table(var_table);
   1.202 -  }
   1.203 -    
   1.204 -  destroy_address(ret_path);
   1.205 -
   1.206 -  return ok;
   1.207 +	return ok;
   1.208  }
   1.209  
   1.210  /*
   1.211 @@ -133,45 +131,43 @@
   1.212  result: |nnnyyyynnnnyyyyyyyyyynnnnnnn
   1.213  */
   1.214  
   1.215 -static
   1.216 -gboolean warn_msg_is_due(message *msg)
   1.217 +static gboolean
   1.218 +warn_msg_is_due(message * msg)
   1.219  {
   1.220 -  time_t now = time(NULL);
   1.221 -  gint dummy;
   1.222 -  
   1.223 -  GList *node;
   1.224 -  for(node = g_list_last(conf.warn_intervals); node; node = g_list_previous(node)){
   1.225 -    gchar *str_ival = (gchar *)(node->data);
   1.226 -    gint ival = time_interval(str_ival, &dummy);
   1.227 -    if(ival >= 0){
   1.228 -      DEBUG(5) debugf("ival = %d\n", ival);
   1.229 -      DEBUG(5) debugf("now - msg->received_time = %d\n", now - msg->received_time);
   1.230 -      if((now - msg->received_time) > ival){
   1.231 -	if(msg->warned_time != 0){
   1.232 -	  if((msg->warned_time - msg->received_time) < ival)
   1.233 -	    return TRUE;
   1.234 -	}else
   1.235 -	  return TRUE;
   1.236 -      }
   1.237 -    }else
   1.238 -      logwrite(LOG_WARNING, "invalid time interval: %s\n", str_ival);
   1.239 -  }
   1.240 -  return FALSE;
   1.241 +	time_t now = time(NULL);
   1.242 +	gint dummy;
   1.243 +
   1.244 +	GList *node;
   1.245 +	for (node = g_list_last(conf.warn_intervals); node; node = g_list_previous(node)) {
   1.246 +		gchar *str_ival = (gchar *) (node->data);
   1.247 +		gint ival = time_interval(str_ival, &dummy);
   1.248 +		if (ival >= 0) {
   1.249 +			DEBUG(5) debugf("ival = %d\n", ival);
   1.250 +			DEBUG(5) debugf("now - msg->received_time = %d\n", now - msg->received_time);
   1.251 +			if ((now - msg->received_time) > ival) {
   1.252 +				if (msg->warned_time != 0) {
   1.253 +					if ((msg->warned_time - msg->received_time) < ival)
   1.254 +						return TRUE;
   1.255 +				} else
   1.256 +					return TRUE;
   1.257 +			}
   1.258 +		} else
   1.259 +			logwrite(LOG_WARNING, "invalid time interval: %s\n", str_ival);
   1.260 +	}
   1.261 +	return FALSE;
   1.262  }
   1.263  
   1.264 -gboolean warn_msg(message *msg, gchar *template,
   1.265 -		  GList *defered_rcpts, gchar *err_fmt, va_list args)
   1.266 +gboolean
   1.267 +warn_msg(message * msg, gchar * template, GList * defered_rcpts, gchar * err_fmt, va_list args)
   1.268  {
   1.269 -  time_t now = time(NULL);
   1.270 +	time_t now = time(NULL);
   1.271  
   1.272 -  if(warn_msg_is_due(msg)){
   1.273 -    if(fail_msg(msg, template, defered_rcpts, err_fmt, args)){
   1.274 -      msg->warned_time = now;
   1.275 -      return TRUE;
   1.276 -    }else
   1.277 -      return FALSE;
   1.278 -  }
   1.279 -  return TRUE;
   1.280 +	if (warn_msg_is_due(msg)) {
   1.281 +		if (fail_msg(msg, template, defered_rcpts, err_fmt, args)) {
   1.282 +			msg->warned_time = now;
   1.283 +			return TRUE;
   1.284 +		} else
   1.285 +			return FALSE;
   1.286 +	}
   1.287 +	return TRUE;
   1.288  }
   1.289 -
   1.290 -