masqmail

changeset 82:83a182793503

refactoring
author meillo@marmaro.de
date Sat, 19 Jun 2010 12:35:08 +0200
parents 71ce3a1568e9
children 085d6cd44462
files src/queue.c src/spool.c
diffstat 2 files changed, 172 insertions(+), 168 deletions(-) [+]
line diff
     1.1 --- a/src/queue.c	Sat Jun 19 11:14:34 2010 +0200
     1.2 +++ b/src/queue.c	Sat Jun 19 12:35:08 2010 +0200
     1.3 @@ -140,48 +140,50 @@
     1.4  
     1.5  	msg_list = read_queue(FALSE);
     1.6  
     1.7 -	if (msg_list != NULL) {
     1.8 -		foreach(msg_list, msg_node) {
     1.9 -			message *msg = (message *) (msg_node->data);
    1.10 -			GList *rcpt_node;
    1.11 -			gchar *size_str = NULL;
    1.12 -			gchar *time_str = NULL;
    1.13 -			gchar *host_str = NULL;
    1.14 -			gchar *ident_str = NULL;
    1.15 +	if (msg_list == NULL) {
    1.16 +		printf("mail queue is empty.\n");
    1.17 +		return;
    1.18 +	}
    1.19  
    1.20 -			if (msg->data_size >= 0)
    1.21 -				size_str = g_strdup_printf(" size=%d", msg->data_size);
    1.22 -			if (msg->received_time > 0) {
    1.23 -				gchar *tmp_str;
    1.24 -				time_str = g_strdup_printf(" age=%s", tmp_str = format_difftime(difftime(time(NULL), msg->received_time)));
    1.25 -				g_free(tmp_str);
    1.26 -			}
    1.27 -			if (msg->received_host != NULL)
    1.28 -				host_str = g_strdup_printf(" host=%s", msg->received_host);
    1.29 -			if (msg->ident != NULL)
    1.30 -				ident_str = g_strdup_printf(" ident=%s", msg->ident);
    1.31 +	foreach(msg_list, msg_node) {
    1.32 +		message *msg = (message *) (msg_node->data);
    1.33 +		GList *rcpt_node;
    1.34 +		gchar *size_str = NULL;
    1.35 +		gchar *time_str = NULL;
    1.36 +		gchar *host_str = NULL;
    1.37 +		gchar *ident_str = NULL;
    1.38  
    1.39 -			printf("%s <= %s%s%s%s%s\n", msg->uid, addr_string(msg->return_path), size_str ? size_str : "",
    1.40 -			       time_str ? time_str : "", host_str ? host_str : "", ident_str ? ident_str : "");
    1.41 +		if (msg->data_size >= 0)
    1.42 +			size_str = g_strdup_printf(" size=%d", msg->data_size);
    1.43 +		if (msg->received_time > 0) {
    1.44 +			gchar *tmp_str;
    1.45 +			time_str = g_strdup_printf(" age=%s", tmp_str = format_difftime(difftime(time(NULL), msg->received_time)));
    1.46 +			g_free(tmp_str);
    1.47 +		}
    1.48 +		if (msg->received_host != NULL)
    1.49 +			host_str = g_strdup_printf(" host=%s", msg->received_host);
    1.50 +		if (msg->ident != NULL)
    1.51 +			ident_str = g_strdup_printf(" ident=%s", msg->ident);
    1.52  
    1.53 -			if (size_str)
    1.54 -				g_free(size_str);
    1.55 -			if (time_str)
    1.56 -				g_free(time_str);
    1.57 -			if (host_str)
    1.58 -				g_free(host_str);
    1.59 -			if (ident_str)
    1.60 -				g_free(ident_str);
    1.61 +		printf("%s <= %s%s%s%s%s\n", msg->uid, addr_string(msg->return_path), size_str ? size_str : "",
    1.62 +		       time_str ? time_str : "", host_str ? host_str : "", ident_str ? ident_str : "");
    1.63  
    1.64 -			foreach(msg->rcpt_list, rcpt_node) {
    1.65 -				address *rcpt = (address *) (rcpt_node->data);
    1.66 +		if (size_str)
    1.67 +			g_free(size_str);
    1.68 +		if (time_str)
    1.69 +			g_free(time_str);
    1.70 +		if (host_str)
    1.71 +			g_free(host_str);
    1.72 +		if (ident_str)
    1.73 +			g_free(ident_str);
    1.74  
    1.75 -				printf("              %s %s\n", addr_is_delivered(rcpt) ? "=>" : (addr_is_failed(rcpt) ? "!=" : "=="), addr_string(rcpt));
    1.76 -			}
    1.77 -			g_free(msg);
    1.78 +		foreach(msg->rcpt_list, rcpt_node) {
    1.79 +			address *rcpt = (address *) (rcpt_node->data);
    1.80 +
    1.81 +			printf("              %s %s\n", addr_is_delivered(rcpt) ? "=>" : (addr_is_failed(rcpt) ? "!=" : "=="), addr_string(rcpt));
    1.82  		}
    1.83 -	} else
    1.84 -		printf("mail queue is empty.\n");
    1.85 +		g_free(msg);
    1.86 +	}
    1.87  }
    1.88  
    1.89  gboolean
    1.90 @@ -193,35 +195,32 @@
    1.91  	gchar *dat_name = g_strdup_printf("%s/input/%s-D", conf.spool_dir, uid);
    1.92  	struct stat stat_buf;
    1.93  
    1.94 -	if (spool_lock(uid)) {
    1.95 -
    1.96 -		if (stat(hdr_name, &stat_buf) == 0) {
    1.97 -			if (unlink(hdr_name) != 0) {
    1.98 -				fprintf(stderr, "could not unlink %s: %s\n", hdr_name, strerror(errno));
    1.99 -				hdr_ok = FALSE;
   1.100 -			}
   1.101 -		} else {
   1.102 -			fprintf(stderr, "could not stat file %s: %s\n", hdr_name, strerror(errno));
   1.103 -			hdr_ok = FALSE;
   1.104 -		}
   1.105 -		if (stat(dat_name, &stat_buf) == 0) {
   1.106 -			if (unlink(dat_name) != 0) {
   1.107 -				fprintf(stderr, "could not unlink %s: %s\n", dat_name, strerror(errno));
   1.108 -				dat_ok = FALSE;
   1.109 -			}
   1.110 -		} else {
   1.111 -			fprintf(stderr, "could not stat file %s: %s\n", dat_name, strerror(errno));
   1.112 -			dat_ok = FALSE;
   1.113 -		}
   1.114 -		printf("message %s deleted\n", uid);
   1.115 -
   1.116 -		spool_unlock(uid);
   1.117 -
   1.118 -	} else {
   1.119 -
   1.120 +	if (!spool_lock(uid)) {
   1.121  		fprintf(stderr, "message %s is locked.\n", uid);
   1.122  		return FALSE;
   1.123  	}
   1.124  
   1.125 +	if (stat(hdr_name, &stat_buf) == 0) {
   1.126 +		if (unlink(hdr_name) != 0) {
   1.127 +			fprintf(stderr, "could not unlink %s: %s\n", hdr_name, strerror(errno));
   1.128 +			hdr_ok = FALSE;
   1.129 +		}
   1.130 +	} else {
   1.131 +		fprintf(stderr, "could not stat file %s: %s\n", hdr_name, strerror(errno));
   1.132 +		hdr_ok = FALSE;
   1.133 +	}
   1.134 +	if (stat(dat_name, &stat_buf) == 0) {
   1.135 +		if (unlink(dat_name) != 0) {
   1.136 +			fprintf(stderr, "could not unlink %s: %s\n", dat_name, strerror(errno));
   1.137 +			dat_ok = FALSE;
   1.138 +		}
   1.139 +	} else {
   1.140 +		fprintf(stderr, "could not stat file %s: %s\n", dat_name, strerror(errno));
   1.141 +		dat_ok = FALSE;
   1.142 +	}
   1.143 +	printf("message %s deleted\n", uid);
   1.144 +
   1.145 +	spool_unlock(uid);
   1.146 +
   1.147  	return (dat_ok && hdr_ok);
   1.148  }
     2.1 --- a/src/spool.c	Sat Jun 19 11:14:34 2010 +0200
     2.2 +++ b/src/spool.c	Sat Jun 19 12:35:08 2010 +0200
     2.3 @@ -91,106 +91,110 @@
     2.4  spool_read_data(message * msg)
     2.5  {
     2.6  	FILE *in;
     2.7 -	gboolean ok = FALSE;
     2.8  	gchar *spool_file;
     2.9  
    2.10  	DEBUG(5) debugf("spool_read_data entered\n");
    2.11  	spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid);
    2.12  	DEBUG(5) debugf("reading data spool file '%s'\n", spool_file);
    2.13 -	if ((in = fopen(spool_file, "r"))) {
    2.14 -		char buf[MAX_DATALINE];
    2.15 -		int len;
    2.16 +	in = fopen(spool_file, "r");
    2.17 +	if (!in) {
    2.18 +		logwrite(LOG_ALERT, "could not open spool data file %s: %s\n", spool_file, strerror(errno));
    2.19 +		return FALSE;
    2.20 +	}
    2.21  
    2.22 -		/* msg uid */
    2.23 -		read_line(in, buf, MAX_DATALINE);
    2.24 +	char buf[MAX_DATALINE];
    2.25 +	int len;
    2.26  
    2.27 -		/* data */
    2.28 -		msg->data_list = NULL;
    2.29 -		while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
    2.30 -			msg->data_list = g_list_prepend(msg->data_list, g_strdup(buf));
    2.31 -		}
    2.32 -		msg->data_list = g_list_reverse(msg->data_list);
    2.33 -		fclose(in);
    2.34 -		ok = TRUE;
    2.35 -	} else
    2.36 -		logwrite(LOG_ALERT, "could not open spool data file %s: %s\n", spool_file, strerror(errno));
    2.37 -	return ok;
    2.38 +	/* msg uid */
    2.39 +	read_line(in, buf, MAX_DATALINE);
    2.40 +
    2.41 +	/* data */
    2.42 +	msg->data_list = NULL;
    2.43 +	while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
    2.44 +		msg->data_list = g_list_prepend(msg->data_list, g_strdup(buf));
    2.45 +	}
    2.46 +	msg->data_list = g_list_reverse(msg->data_list);
    2.47 +	fclose(in);
    2.48 +	return TRUE;
    2.49  }
    2.50  
    2.51  gboolean
    2.52  spool_read_header(message * msg)
    2.53  {
    2.54  	FILE *in;
    2.55 -	gboolean ok = FALSE;
    2.56  	gchar *spool_file;
    2.57  
    2.58  	/* header spool: */
    2.59  	spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid);
    2.60 -	if ((in = fopen(spool_file, "r"))) {
    2.61 -		header *hdr = NULL;
    2.62 -		char buf[MAX_DATALINE];
    2.63 -		int len;
    2.64 +	in = fopen(spool_file, "r");
    2.65 +	if (!in) {
    2.66 +		logwrite(LOG_ALERT, "could not open spool header file %s: %s\n",
    2.67 +		         spool_file, strerror(errno));
    2.68 +		return FALSE;
    2.69 +	}
    2.70  
    2.71 -		/* msg uid */
    2.72 -		read_line(in, buf, MAX_DATALINE);
    2.73 +	header *hdr = NULL;
    2.74 +	char buf[MAX_DATALINE];
    2.75 +	int len;
    2.76  
    2.77 -		/* envelope header */
    2.78 -		while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
    2.79 -			if (buf[0] == '\n')
    2.80 -				break;
    2.81 -			else if (strncasecmp(buf, "MF:", 3) == 0) {
    2.82 -				msg->return_path = create_address(&(buf[3]), TRUE);
    2.83 -				DEBUG(3) debugf("spool_read: MAIL FROM: %s", msg->return_path->address);
    2.84 -			} else if (strncasecmp(buf, "RT:", 3) == 0) {
    2.85 -				address *addr;
    2.86 -				addr = spool_scan_rcpt(buf);
    2.87 -				if (!addr_is_delivered(addr) && !addr_is_failed(addr)) {
    2.88 -					msg->rcpt_list = g_list_append(msg->rcpt_list, addr);
    2.89 -				} else {
    2.90 -					msg->non_rcpt_list = g_list_append(msg->non_rcpt_list, addr);
    2.91 +	/* msg uid */
    2.92 +	read_line(in, buf, MAX_DATALINE);
    2.93 +
    2.94 +	/* envelope header */
    2.95 +	while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
    2.96 +		if (buf[0] == '\n') {
    2.97 +			break;
    2.98 +		} else if (strncasecmp(buf, "MF:", 3) == 0) {
    2.99 +			msg->return_path = create_address(&(buf[3]), TRUE);
   2.100 +			DEBUG(3) debugf("spool_read: MAIL FROM: %s", msg->return_path->address);
   2.101 +		} else if (strncasecmp(buf, "RT:", 3) == 0) {
   2.102 +			address *addr;
   2.103 +			addr = spool_scan_rcpt(buf);
   2.104 +			if (!addr_is_delivered(addr) && !addr_is_failed(addr)) {
   2.105 +				msg->rcpt_list = g_list_append(msg->rcpt_list, addr);
   2.106 +			} else {
   2.107 +				msg->non_rcpt_list = g_list_append(msg->non_rcpt_list, addr);
   2.108 +			}
   2.109 +		} else if (strncasecmp(buf, "PR:", 3) == 0) {
   2.110 +			prot_id i;
   2.111 +			for (i = 0; i < PROT_NUM; i++) {
   2.112 +				if (strncasecmp(prot_names[i], &(buf[3]), strlen(prot_names[i])) == 0) {
   2.113 +					break;
   2.114  				}
   2.115 -			} else if (strncasecmp(buf, "PR:", 3) == 0) {
   2.116 -				prot_id i;
   2.117 -				for (i = 0; i < PROT_NUM; i++) {
   2.118 -					if (strncasecmp(prot_names[i], &(buf[3]), strlen(prot_names[i])) == 0) {
   2.119 -						break;
   2.120 -					}
   2.121 -				}
   2.122 -				msg->received_prot = i;
   2.123 -			} else if (strncasecmp(buf, "RH:", 3) == 0) {
   2.124 -				g_strchomp(buf);
   2.125 -				msg->received_host = g_strdup(&(buf[3]));
   2.126 -			} else if (strncasecmp(buf, "ID:", 3) == 0) {
   2.127 -				g_strchomp(buf);
   2.128 -				msg->ident = g_strdup(&(buf[3]));
   2.129 -			} else if (strncasecmp(buf, "DS:", 3) == 0) {
   2.130 -				msg->data_size = atoi(&(buf[3]));
   2.131 -			} else if (strncasecmp(buf, "TR:", 3) == 0) {
   2.132 -				msg->received_time = (time_t) (atoi(&(buf[3])));
   2.133 -			} else if (strncasecmp(buf, "TW:", 3) == 0) {
   2.134 -				msg->warned_time = (time_t) (atoi(&(buf[3])));
   2.135  			}
   2.136 -			/* so far ignore other tags */
   2.137 +			msg->received_prot = i;
   2.138 +		} else if (strncasecmp(buf, "RH:", 3) == 0) {
   2.139 +			g_strchomp(buf);
   2.140 +			msg->received_host = g_strdup(&(buf[3]));
   2.141 +		} else if (strncasecmp(buf, "ID:", 3) == 0) {
   2.142 +			g_strchomp(buf);
   2.143 +			msg->ident = g_strdup(&(buf[3]));
   2.144 +		} else if (strncasecmp(buf, "DS:", 3) == 0) {
   2.145 +			msg->data_size = atoi(&(buf[3]));
   2.146 +		} else if (strncasecmp(buf, "TR:", 3) == 0) {
   2.147 +			msg->received_time = (time_t) (atoi(&(buf[3])));
   2.148 +		} else if (strncasecmp(buf, "TW:", 3) == 0) {
   2.149 +			msg->warned_time = (time_t) (atoi(&(buf[3])));
   2.150  		}
   2.151 +		/* so far ignore other tags */
   2.152 +	}
   2.153  
   2.154 -		/* mail headers */
   2.155 -		while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
   2.156 -			if (strncasecmp(buf, "HD:", 3) == 0) {
   2.157 -				hdr = get_header(&(buf[3]));
   2.158 -				msg->hdr_list = g_list_append(msg->hdr_list, hdr);
   2.159 -			} else if ((buf[0] == ' ' || buf[0] == '\t') && hdr) {
   2.160 -				char *tmp = hdr->header;
   2.161 -				/* header continuation */
   2.162 -				hdr->header = g_strconcat(hdr->header, buf, NULL);
   2.163 -				hdr->value = hdr->header + (hdr->value - tmp);
   2.164 -			} else
   2.165 -				break;
   2.166 +	/* mail headers */
   2.167 +	while ((len = read_line(in, buf, MAX_DATALINE)) > 0) {
   2.168 +		if (strncasecmp(buf, "HD:", 3) == 0) {
   2.169 +			hdr = get_header(&(buf[3]));
   2.170 +			msg->hdr_list = g_list_append(msg->hdr_list, hdr);
   2.171 +		} else if ((buf[0] == ' ' || buf[0] == '\t') && hdr) {
   2.172 +			char *tmp = hdr->header;
   2.173 +			/* header continuation */
   2.174 +			hdr->header = g_strconcat(hdr->header, buf, NULL);
   2.175 +			hdr->value = hdr->header + (hdr->value - tmp);
   2.176 +		} else {
   2.177 +			break;
   2.178  		}
   2.179 -		fclose(in);
   2.180 -		ok = TRUE;
   2.181 -	} else
   2.182 -		logwrite(LOG_ALERT, "could not open spool header file %s: %s\n", spool_file, strerror(errno));
   2.183 -	return ok;
   2.184 +	}
   2.185 +	fclose(in);
   2.186 +	return TRUE;
   2.187  }
   2.188  
   2.189  message*
   2.190 @@ -308,39 +312,38 @@
   2.191  	/* header spool: */
   2.192  	ok = spool_write_header(msg);
   2.193  
   2.194 -	if (ok) {
   2.195 +	if (ok && do_write_data) {
   2.196 +		/* data spool: */
   2.197 +		tmp_file = g_strdup_printf("%s/input/%d-D.tmp", conf.spool_dir, getpid());
   2.198 +		DEBUG(4) debugf("tmp_file = %s\n", tmp_file);
   2.199  
   2.200 -		if (do_write_data) {
   2.201 -			/* data spool: */
   2.202 -			tmp_file = g_strdup_printf("%s/input/%d-D.tmp", conf.spool_dir, getpid());
   2.203 -			DEBUG(4) debugf("tmp_file = %s\n", tmp_file);
   2.204 +		if ((out = fopen(tmp_file, "w"))) {
   2.205 +			fprintf(out, "%s\n", msg->uid);
   2.206 +			for (list = g_list_first(msg->data_list); list != NULL; list = g_list_next(list)) {
   2.207 +				fprintf(out, "%s", (gchar *) (list->data));
   2.208 +			}
   2.209  
   2.210 -			if ((out = fopen(tmp_file, "w"))) {
   2.211 -				fprintf(out, "%s\n", msg->uid);
   2.212 -				for (list = g_list_first(msg->data_list); list != NULL; list = g_list_next(list)) {
   2.213 -					fprintf(out, "%s", (gchar *) (list->data));
   2.214 +			/* possibly paranoid ;-) */
   2.215 +			if (fflush(out) == EOF) {
   2.216 +				ok = FALSE;
   2.217 +			} else if (fdatasync(fileno(out)) != 0) {
   2.218 +				if (errno != EINVAL) {  /* some fs do not support this..  I hope this also means that it is not necessary */
   2.219 +					ok = FALSE;
   2.220  				}
   2.221 -
   2.222 -				/* possibly paranoid ;-) */
   2.223 -				if (fflush(out) == EOF)
   2.224 -					ok = FALSE;
   2.225 -				else if (fdatasync(fileno(out)) != 0) {
   2.226 -					if (errno != EINVAL)  /* some fs do not support this..  I hope this also means that it is not necessary */
   2.227 -						ok = FALSE;
   2.228 -				}
   2.229 -				fclose(out);
   2.230 -				if (ok) {
   2.231 -					spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid);
   2.232 -					DEBUG(4) debugf("spool_file = %s\n", spool_file);
   2.233 -					ok = (rename(tmp_file, spool_file) != -1);
   2.234 -					g_free(spool_file);
   2.235 -				}
   2.236 -			} else {
   2.237 -				logwrite(LOG_ALERT, "could not open temporary data spool file: %s\n", strerror(errno));
   2.238 -				ok = FALSE;
   2.239  			}
   2.240 -			g_free(tmp_file);
   2.241 +			fclose(out);
   2.242 +			if (ok) {
   2.243 +				spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid);
   2.244 +				DEBUG(4) debugf("spool_file = %s\n", spool_file);
   2.245 +				ok = (rename(tmp_file, spool_file) != -1);
   2.246 +				g_free(spool_file);
   2.247 +			}
   2.248 +		} else {
   2.249 +			logwrite(LOG_ALERT, "could not open temporary data spool file: %s\n",
   2.250 +			         strerror(errno));
   2.251 +			ok = FALSE;
   2.252  		}
   2.253 +		g_free(tmp_file);
   2.254  	}
   2.255  
   2.256  	/* set uid and gid back */
   2.257 @@ -421,14 +424,16 @@
   2.258  
   2.259  	/* header spool: */
   2.260  	spool_file = g_strdup_printf("%s/input/%s-H", conf.spool_dir, msg->uid);
   2.261 -	if (unlink(spool_file) != 0)
   2.262 +	if (unlink(spool_file) != 0) {
   2.263  		logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", spool_file, strerror(errno));
   2.264 +	}
   2.265  	g_free(spool_file);
   2.266  
   2.267  	/* data spool: */
   2.268  	spool_file = g_strdup_printf("%s/input/%s-D", conf.spool_dir, msg->uid);
   2.269 -	if (unlink(spool_file) != 0)
   2.270 +	if (unlink(spool_file) != 0) {
   2.271  		logwrite(LOG_ALERT, "could not delete spool file %s: %s\n", spool_file, strerror(errno));
   2.272 +	}
   2.273  	g_free(spool_file);
   2.274  
   2.275  	/* set uid and gid back */