masqmail

diff src/spool.c @ 82:83a182793503

refactoring
author meillo@marmaro.de
date Sat, 19 Jun 2010 12:35:08 +0200
parents 3b344bf57162
children 46f407c0727a
line diff
     1.1 --- a/src/spool.c	Sat Jun 19 11:14:34 2010 +0200
     1.2 +++ b/src/spool.c	Sat Jun 19 12:35:08 2010 +0200
     1.3 @@ -91,106 +91,110 @@
     1.4  spool_read_data(message * msg)
     1.5  {
     1.6  	FILE *in;
     1.7 -	gboolean ok = FALSE;
     1.8  	gchar *spool_file;
     1.9  
    1.10  	DEBUG(5) debugf("spool_read_data entered\n");
    1.11  	spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid);
    1.12  	DEBUG(5) debugf("reading data spool file '%s'\n", spool_file);
    1.13 -	if ((in = fopen(spool_file, "r"))) {
    1.14 -		char buf[MAX_DATALINE];
    1.15 -		int len;
    1.16 +	in = fopen(spool_file, "r");
    1.17 +	if (!in) {
    1.18 +		logwrite(LOG_ALERT, "could not open spool data file %s: %s\n", spool_file, strerror(errno));
    1.19 +		return FALSE;
    1.20 +	}
    1.21  
    1.22 -		/* msg uid */
    1.23 -		read_line(in, buf, MAX_DATALINE);
    1.24 +	char buf[MAX_DATALINE];
    1.25 +	int len;
    1.26  
    1.27 -		/* data */
    1.28 -		msg->data_list = NULL;
    1.29 -		while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
    1.30 -			msg->data_list = g_list_prepend(msg->data_list, g_strdup(buf));
    1.31 -		}
    1.32 -		msg->data_list = g_list_reverse(msg->data_list);
    1.33 -		fclose(in);
    1.34 -		ok = TRUE;
    1.35 -	} else
    1.36 -		logwrite(LOG_ALERT, "could not open spool data file %s: %s\n", spool_file, strerror(errno));
    1.37 -	return ok;
    1.38 +	/* msg uid */
    1.39 +	read_line(in, buf, MAX_DATALINE);
    1.40 +
    1.41 +	/* data */
    1.42 +	msg->data_list = NULL;
    1.43 +	while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
    1.44 +		msg->data_list = g_list_prepend(msg->data_list, g_strdup(buf));
    1.45 +	}
    1.46 +	msg->data_list = g_list_reverse(msg->data_list);
    1.47 +	fclose(in);
    1.48 +	return TRUE;
    1.49  }
    1.50  
    1.51  gboolean
    1.52  spool_read_header(message * msg)
    1.53  {
    1.54  	FILE *in;
    1.55 -	gboolean ok = FALSE;
    1.56  	gchar *spool_file;
    1.57  
    1.58  	/* header spool: */
    1.59  	spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid);
    1.60 -	if ((in = fopen(spool_file, "r"))) {
    1.61 -		header *hdr = NULL;
    1.62 -		char buf[MAX_DATALINE];
    1.63 -		int len;
    1.64 +	in = fopen(spool_file, "r");
    1.65 +	if (!in) {
    1.66 +		logwrite(LOG_ALERT, "could not open spool header file %s: %s\n",
    1.67 +		         spool_file, strerror(errno));
    1.68 +		return FALSE;
    1.69 +	}
    1.70  
    1.71 -		/* msg uid */
    1.72 -		read_line(in, buf, MAX_DATALINE);
    1.73 +	header *hdr = NULL;
    1.74 +	char buf[MAX_DATALINE];
    1.75 +	int len;
    1.76  
    1.77 -		/* envelope header */
    1.78 -		while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
    1.79 -			if (buf[0] == '\n')
    1.80 -				break;
    1.81 -			else if (strncasecmp(buf, "MF:", 3) == 0) {
    1.82 -				msg->return_path = create_address(&(buf[3]), TRUE);
    1.83 -				DEBUG(3) debugf("spool_read: MAIL FROM: %s", msg->return_path->address);
    1.84 -			} else if (strncasecmp(buf, "RT:", 3) == 0) {
    1.85 -				address *addr;
    1.86 -				addr = spool_scan_rcpt(buf);
    1.87 -				if (!addr_is_delivered(addr) && !addr_is_failed(addr)) {
    1.88 -					msg->rcpt_list = g_list_append(msg->rcpt_list, addr);
    1.89 -				} else {
    1.90 -					msg->non_rcpt_list = g_list_append(msg->non_rcpt_list, addr);
    1.91 +	/* msg uid */
    1.92 +	read_line(in, buf, MAX_DATALINE);
    1.93 +
    1.94 +	/* envelope header */
    1.95 +	while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
    1.96 +		if (buf[0] == '\n') {
    1.97 +			break;
    1.98 +		} else if (strncasecmp(buf, "MF:", 3) == 0) {
    1.99 +			msg->return_path = create_address(&(buf[3]), TRUE);
   1.100 +			DEBUG(3) debugf("spool_read: MAIL FROM: %s", msg->return_path->address);
   1.101 +		} else if (strncasecmp(buf, "RT:", 3) == 0) {
   1.102 +			address *addr;
   1.103 +			addr = spool_scan_rcpt(buf);
   1.104 +			if (!addr_is_delivered(addr) && !addr_is_failed(addr)) {
   1.105 +				msg->rcpt_list = g_list_append(msg->rcpt_list, addr);
   1.106 +			} else {
   1.107 +				msg->non_rcpt_list = g_list_append(msg->non_rcpt_list, addr);
   1.108 +			}
   1.109 +		} else if (strncasecmp(buf, "PR:", 3) == 0) {
   1.110 +			prot_id i;
   1.111 +			for (i = 0; i < PROT_NUM; i++) {
   1.112 +				if (strncasecmp(prot_names[i], &(buf[3]), strlen(prot_names[i])) == 0) {
   1.113 +					break;
   1.114  				}
   1.115 -			} else if (strncasecmp(buf, "PR:", 3) == 0) {
   1.116 -				prot_id i;
   1.117 -				for (i = 0; i < PROT_NUM; i++) {
   1.118 -					if (strncasecmp(prot_names[i], &(buf[3]), strlen(prot_names[i])) == 0) {
   1.119 -						break;
   1.120 -					}
   1.121 -				}
   1.122 -				msg->received_prot = i;
   1.123 -			} else if (strncasecmp(buf, "RH:", 3) == 0) {
   1.124 -				g_strchomp(buf);
   1.125 -				msg->received_host = g_strdup(&(buf[3]));
   1.126 -			} else if (strncasecmp(buf, "ID:", 3) == 0) {
   1.127 -				g_strchomp(buf);
   1.128 -				msg->ident = g_strdup(&(buf[3]));
   1.129 -			} else if (strncasecmp(buf, "DS:", 3) == 0) {
   1.130 -				msg->data_size = atoi(&(buf[3]));
   1.131 -			} else if (strncasecmp(buf, "TR:", 3) == 0) {
   1.132 -				msg->received_time = (time_t) (atoi(&(buf[3])));
   1.133 -			} else if (strncasecmp(buf, "TW:", 3) == 0) {
   1.134 -				msg->warned_time = (time_t) (atoi(&(buf[3])));
   1.135  			}
   1.136 -			/* so far ignore other tags */
   1.137 +			msg->received_prot = i;
   1.138 +		} else if (strncasecmp(buf, "RH:", 3) == 0) {
   1.139 +			g_strchomp(buf);
   1.140 +			msg->received_host = g_strdup(&(buf[3]));
   1.141 +		} else if (strncasecmp(buf, "ID:", 3) == 0) {
   1.142 +			g_strchomp(buf);
   1.143 +			msg->ident = g_strdup(&(buf[3]));
   1.144 +		} else if (strncasecmp(buf, "DS:", 3) == 0) {
   1.145 +			msg->data_size = atoi(&(buf[3]));
   1.146 +		} else if (strncasecmp(buf, "TR:", 3) == 0) {
   1.147 +			msg->received_time = (time_t) (atoi(&(buf[3])));
   1.148 +		} else if (strncasecmp(buf, "TW:", 3) == 0) {
   1.149 +			msg->warned_time = (time_t) (atoi(&(buf[3])));
   1.150  		}
   1.151 +		/* so far ignore other tags */
   1.152 +	}
   1.153  
   1.154 -		/* mail headers */
   1.155 -		while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
   1.156 -			if (strncasecmp(buf, "HD:", 3) == 0) {
   1.157 -				hdr = get_header(&(buf[3]));
   1.158 -				msg->hdr_list = g_list_append(msg->hdr_list, hdr);
   1.159 -			} else if ((buf[0] == ' ' || buf[0] == '\t') && hdr) {
   1.160 -				char *tmp = hdr->header;
   1.161 -				/* header continuation */
   1.162 -				hdr->header = g_strconcat(hdr->header, buf, NULL);
   1.163 -				hdr->value = hdr->header + (hdr->value - tmp);
   1.164 -			} else
   1.165 -				break;
   1.166 +	/* mail headers */
   1.167 +	while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
   1.168 +		if (strncasecmp(buf, "HD:", 3) == 0) {
   1.169 +			hdr = get_header(&(buf[3]));
   1.170 +			msg->hdr_list = g_list_append(msg->hdr_list, hdr);
   1.171 +		} else if ((buf[0] == ' ' || buf[0] == '\t') && hdr) {
   1.172 +			char *tmp = hdr->header;
   1.173 +			/* header continuation */
   1.174 +			hdr->header = g_strconcat(hdr->header, buf, NULL);
   1.175 +			hdr->value = hdr->header + (hdr->value - tmp);
   1.176 +		} else {
   1.177 +			break;
   1.178  		}
   1.179 -		fclose(in);
   1.180 -		ok = TRUE;
   1.181 -	} else
   1.182 -		logwrite(LOG_ALERT, "could not open spool header file %s: %s\n", spool_file, strerror(errno));
   1.183 -	return ok;
   1.184 +	}
   1.185 +	fclose(in);
   1.186 +	return TRUE;
   1.187  }
   1.188  
   1.189  message*
   1.190 @@ -308,39 +312,38 @@
   1.191  	/* header spool: */
   1.192  	ok = spool_write_header(msg);
   1.193  
   1.194 -	if (ok) {
   1.195 +	if (ok && do_write_data) {
   1.196 +		/* data spool: */
   1.197 +		tmp_file = g_strdup_printf("%s/input/%d-D.tmp", conf.spool_dir, getpid());
   1.198 +		DEBUG(4) debugf("tmp_file = %s\n", tmp_file);
   1.199  
   1.200 -		if (do_write_data) {
   1.201 -			/* data spool: */
   1.202 -			tmp_file = g_strdup_printf("%s/input/%d-D.tmp", conf.spool_dir, getpid());
   1.203 -			DEBUG(4) debugf("tmp_file = %s\n", tmp_file);
   1.204 +		if ((out = fopen(tmp_file, "w"))) {
   1.205 +			fprintf(out, "%s\n", msg->uid);
   1.206 +			for (list = g_list_first(msg->data_list); list != NULL; list = g_list_next(list)) {
   1.207 +				fprintf(out, "%s", (gchar *) (list->data));
   1.208 +			}
   1.209  
   1.210 -			if ((out = fopen(tmp_file, "w"))) {
   1.211 -				fprintf(out, "%s\n", msg->uid);
   1.212 -				for (list = g_list_first(msg->data_list); list != NULL; list = g_list_next(list)) {
   1.213 -					fprintf(out, "%s", (gchar *) (list->data));
   1.214 +			/* possibly paranoid ;-) */
   1.215 +			if (fflush(out) == EOF) {
   1.216 +				ok = FALSE;
   1.217 +			} else if (fdatasync(fileno(out)) != 0) {
   1.218 +				if (errno != EINVAL) {  /* some fs do not support this..  I hope this also means that it is not necessary */
   1.219 +					ok = FALSE;
   1.220  				}
   1.221 -
   1.222 -				/* possibly paranoid ;-) */
   1.223 -				if (fflush(out) == EOF)
   1.224 -					ok = FALSE;
   1.225 -				else if (fdatasync(fileno(out)) != 0) {
   1.226 -					if (errno != EINVAL)  /* some fs do not support this..  I hope this also means that it is not necessary */
   1.227 -						ok = FALSE;
   1.228 -				}
   1.229 -				fclose(out);
   1.230 -				if (ok) {
   1.231 -					spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid);
   1.232 -					DEBUG(4) debugf("spool_file = %s\n", spool_file);
   1.233 -					ok = (rename(tmp_file, spool_file) != -1);
   1.234 -					g_free(spool_file);
   1.235 -				}
   1.236 -			} else {
   1.237 -				logwrite(LOG_ALERT, "could not open temporary data spool file: %s\n", strerror(errno));
   1.238 -				ok = FALSE;
   1.239  			}
   1.240 -			g_free(tmp_file);
   1.241 +			fclose(out);
   1.242 +			if (ok) {
   1.243 +				spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid);
   1.244 +				DEBUG(4) debugf("spool_file = %s\n", spool_file);
   1.245 +				ok = (rename(tmp_file, spool_file) != -1);
   1.246 +				g_free(spool_file);
   1.247 +			}
   1.248 +		} else {
   1.249 +			logwrite(LOG_ALERT, "could not open temporary data spool file: %s\n",
   1.250 +			         strerror(errno));
   1.251 +			ok = FALSE;
   1.252  		}
   1.253 +		g_free(tmp_file);
   1.254  	}
   1.255  
   1.256  	/* set uid and gid back */
   1.257 @@ -421,14 +424,16 @@
   1.258  
   1.259  	/* header spool: */
   1.260  	spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid);
   1.261 -	if (unlink(spool_file) != 0)
   1.262 +	if (unlink(spool_file) != 0) {
   1.263  		logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", spool_file, strerror(errno));
   1.264 +	}
   1.265  	g_free(spool_file);
   1.266  
   1.267  	/* data spool: */
   1.268  	spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid);
   1.269 -	if (unlink(spool_file) != 0)
   1.270 +	if (unlink(spool_file) != 0) {
   1.271  		logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", spool_file, strerror(errno));
   1.272 +	}
   1.273  	g_free(spool_file);
   1.274  
   1.275  	/* set uid and gid back */