masqmail

diff src/queue.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/queue.c	Mon Oct 27 16:21:27 2008 +0100
     1.2 +++ b/src/queue.c	Mon Oct 27 16:23:10 2008 +0100
     1.3 @@ -21,204 +21,205 @@
     1.4  #include <sys/stat.h>
     1.5  #include <glob.h>
     1.6  
     1.7 -static
     1.8 -void mix_arr(int *buf, int len)
     1.9 +static void
    1.10 +mix_arr(int *buf, int len)
    1.11  {
    1.12 -  int i;
    1.13 +	int i;
    1.14  
    1.15 -  for(i = 0; i < len; i++)
    1.16 -    buf[i] = i;
    1.17 -  for(i = 0; i < len-1; i++){
    1.18 -    int j = (int)((float)(len-i) * ((float)rand())/(RAND_MAX + 1.0));
    1.19 -    int tmp;
    1.20 +	for (i = 0; i < len; i++)
    1.21 +		buf[i] = i;
    1.22 +	for (i = 0; i < len - 1; i++) {
    1.23 +		int j = (int) ((float) (len - i) * ((float) rand()) / (RAND_MAX + 1.0));
    1.24 +		int tmp;
    1.25  
    1.26 -    if(i != j){
    1.27 -      tmp = buf[i]; buf[i] = buf[j]; buf[j] = tmp;
    1.28 -    }
    1.29 -  }
    1.30 +		if (i != j) {
    1.31 +			tmp = buf[i];
    1.32 +			buf[i] = buf[j];
    1.33 +			buf[j] = tmp;
    1.34 +		}
    1.35 +	}
    1.36  }
    1.37  
    1.38 -GList *read_queue(gboolean do_readdata)
    1.39 +GList*
    1.40 +read_queue(gboolean do_readdata)
    1.41  {
    1.42 -  GList *msg_list = NULL;
    1.43 -  glob_t gl;
    1.44 -  gchar *pattern;
    1.45 -  int i, *idx_arr;
    1.46 +	GList *msg_list = NULL;
    1.47 +	glob_t gl;
    1.48 +	gchar *pattern;
    1.49 +	int i, *idx_arr;
    1.50  
    1.51 -  pattern = g_strdup_printf("%s/input/??????-???-??-H", conf.spool_dir);
    1.52 -  gl.gl_offs = 0;
    1.53 -  glob(pattern, 0, NULL, &gl);
    1.54 +	pattern = g_strdup_printf("%s/input/??????-???-??-H", conf.spool_dir);
    1.55 +	gl.gl_offs = 0;
    1.56 +	glob(pattern, 0, NULL, &gl);
    1.57  
    1.58 -  g_free(pattern);
    1.59 +	g_free(pattern);
    1.60  
    1.61 -  DEBUG(4){
    1.62 -    int i;
    1.63 -    for(i = 0; i < gl.gl_pathc; i++){
    1.64 -      debugf("spoolfile: %s\n", gl.gl_pathv[i]);
    1.65 -    }
    1.66 -  }
    1.67 +	DEBUG(4) {
    1.68 +		int i;
    1.69 +		for (i = 0; i < gl.gl_pathc; i++) {
    1.70 +			debugf("spoolfile: %s\n", gl.gl_pathv[i]);
    1.71 +		}
    1.72 +	}
    1.73  
    1.74 -  idx_arr = g_malloc(sizeof(int) * gl.gl_pathc);
    1.75 -  mix_arr(idx_arr, gl.gl_pathc);
    1.76 +	idx_arr = g_malloc(sizeof(int) * gl.gl_pathc);
    1.77 +	mix_arr(idx_arr, gl.gl_pathc);
    1.78  
    1.79 -  for(i = 0; i < gl.gl_pathc; i++){
    1.80 -    gchar *uid;
    1.81 +	for (i = 0; i < gl.gl_pathc; i++) {
    1.82 +		gchar *uid;
    1.83  
    1.84 -    /* copy 13 chars, offset spooldir path + 7 chars for /input/ */
    1.85 -    /* uid length = 6 chars + '-' + 3 chars + '-' + 2 = 13 chars */
    1.86 -    uid = g_strndup(&(gl.gl_pathv[idx_arr[i]][strlen(conf.spool_dir) + 7]), 13);
    1.87 +		/* copy 13 chars, offset spooldir path + 7 chars for /input/ */
    1.88 +		/* uid length = 6 chars + '-' + 3 chars + '-' + 2 = 13 chars */
    1.89 +		uid = g_strndup(&(gl.gl_pathv[idx_arr[i]][strlen(conf.spool_dir) + 7]), 13);
    1.90  
    1.91 -    DEBUG(5) debugf("uid: %s\n", uid);
    1.92 +		DEBUG(5) debugf("uid: %s\n", uid);
    1.93  
    1.94 -    msg_list = g_list_append(msg_list, msg_spool_read(uid, do_readdata));
    1.95 +		msg_list = g_list_append(msg_list, msg_spool_read(uid, do_readdata));
    1.96  
    1.97 -    DEBUG(5) debugf("after read spool file for %s\n", uid);
    1.98 +		DEBUG(5) debugf("after read spool file for %s\n", uid);
    1.99  
   1.100 -    g_free(uid);
   1.101 -  }
   1.102 -  return msg_list;
   1.103 +		g_free(uid);
   1.104 +	}
   1.105 +	return msg_list;
   1.106  }
   1.107  
   1.108 -gboolean queue_run()
   1.109 +gboolean
   1.110 +queue_run()
   1.111  {
   1.112 -  GList *msg_list;
   1.113 -  gboolean ok = TRUE;
   1.114 +	GList *msg_list;
   1.115 +	gboolean ok = TRUE;
   1.116  
   1.117 -  logwrite(LOG_NOTICE, "Starting queue run.\n");
   1.118 +	logwrite(LOG_NOTICE, "Starting queue run.\n");
   1.119  
   1.120 -  msg_list = read_queue(FALSE);
   1.121 +	msg_list = read_queue(FALSE);
   1.122  
   1.123 -  if(msg_list != NULL){
   1.124 -    ok = deliver_msg_list(msg_list, DLVR_ALL);
   1.125 -    destroy_msg_list(msg_list);
   1.126 -  }
   1.127 -  logwrite(LOG_NOTICE, "Finished queue run.\n");
   1.128 +	if (msg_list != NULL) {
   1.129 +		ok = deliver_msg_list(msg_list, DLVR_ALL);
   1.130 +		destroy_msg_list(msg_list);
   1.131 +	}
   1.132 +	logwrite(LOG_NOTICE, "Finished queue run.\n");
   1.133  
   1.134 -  return ok;
   1.135 +	return ok;
   1.136  }
   1.137  
   1.138 -gboolean queue_run_online()
   1.139 +gboolean
   1.140 +queue_run_online()
   1.141  {
   1.142 -  GList *msg_list = read_queue(FALSE);
   1.143 -  gboolean ok = TRUE;
   1.144 +	GList *msg_list = read_queue(FALSE);
   1.145 +	gboolean ok = TRUE;
   1.146  
   1.147 -  logwrite(LOG_NOTICE, "Starting online queue run.\n");
   1.148 -  if(msg_list != NULL){
   1.149 -    ok = deliver_msg_list(msg_list, DLVR_ONLINE);
   1.150 -    destroy_msg_list(msg_list);
   1.151 -  }
   1.152 -  logwrite(LOG_NOTICE, "Finished online queue run.\n");
   1.153 +	logwrite(LOG_NOTICE, "Starting online queue run.\n");
   1.154 +	if (msg_list != NULL) {
   1.155 +		ok = deliver_msg_list(msg_list, DLVR_ONLINE);
   1.156 +		destroy_msg_list(msg_list);
   1.157 +	}
   1.158 +	logwrite(LOG_NOTICE, "Finished online queue run.\n");
   1.159  
   1.160 -  return ok;
   1.161 +	return ok;
   1.162  }
   1.163  
   1.164 -static
   1.165 -gchar *format_difftime(double secs)
   1.166 +static gchar*
   1.167 +format_difftime(double secs)
   1.168  {
   1.169 -  if(secs > 86400)
   1.170 -    return g_strdup_printf("%.1fd", secs/86400);
   1.171 -  else if(secs > 3600)
   1.172 -    return g_strdup_printf("%.1fh", secs/3600);
   1.173 -  else if(secs > 60)
   1.174 -    return g_strdup_printf("%.1fm", secs/60);
   1.175 -  else
   1.176 -    return g_strdup_printf("%.0fs", secs);
   1.177 -}  
   1.178 +	if (secs > 86400)
   1.179 +		return g_strdup_printf("%.1fd", secs / 86400);
   1.180 +	else if (secs > 3600)
   1.181 +		return g_strdup_printf("%.1fh", secs / 3600);
   1.182 +	else if (secs > 60)
   1.183 +		return g_strdup_printf("%.1fm", secs / 60);
   1.184 +	else
   1.185 +		return g_strdup_printf("%.0fs", secs);
   1.186 +}
   1.187  
   1.188 -void queue_list()
   1.189 +void
   1.190 +queue_list()
   1.191  {
   1.192 -  GList *msg_list;
   1.193 -  GList *msg_node;
   1.194 +	GList *msg_list;
   1.195 +	GList *msg_node;
   1.196  
   1.197 -  msg_list = read_queue(FALSE);
   1.198 +	msg_list = read_queue(FALSE);
   1.199  
   1.200 -  if(msg_list != NULL){
   1.201 -    foreach(msg_list, msg_node){
   1.202 -      message *msg = (message *)(msg_node->data);
   1.203 -      GList *rcpt_node;
   1.204 -      gchar *size_str = NULL;
   1.205 -      gchar *time_str = NULL;
   1.206 -      gchar *host_str = NULL;
   1.207 -      gchar *ident_str = NULL;
   1.208 -    
   1.209 -      if(msg->data_size >= 0)
   1.210 -	size_str = g_strdup_printf(" size=%d", msg->data_size);
   1.211 -      if(msg->received_time > 0){
   1.212 -	gchar *tmp_str;
   1.213 -	time_str =
   1.214 -	  g_strdup_printf(" age=%s",
   1.215 -			  tmp_str = format_difftime(difftime(time(NULL),
   1.216 -							     msg->received_time)));
   1.217 -	g_free(tmp_str);
   1.218 -      }
   1.219 -      if(msg->received_host != NULL)
   1.220 -	host_str = g_strdup_printf(" host=%s", msg->received_host);
   1.221 -      if(msg->ident != NULL)
   1.222 -	ident_str = g_strdup_printf(" ident=%s", msg->ident);
   1.223 +	if (msg_list != NULL) {
   1.224 +		foreach(msg_list, msg_node) {
   1.225 +			message *msg = (message *) (msg_node->data);
   1.226 +			GList *rcpt_node;
   1.227 +			gchar *size_str = NULL;
   1.228 +			gchar *time_str = NULL;
   1.229 +			gchar *host_str = NULL;
   1.230 +			gchar *ident_str = NULL;
   1.231  
   1.232 -      printf("%s <= %s%s%s%s%s\n", msg->uid,
   1.233 -	     addr_string(msg->return_path),
   1.234 -	     size_str ? size_str : "",
   1.235 -	     time_str ? time_str : "",
   1.236 -	     host_str ? host_str : "",
   1.237 -	     ident_str ? ident_str : ""
   1.238 -	     );
   1.239 +			if (msg->data_size >= 0)
   1.240 +				size_str = g_strdup_printf(" size=%d", msg->data_size);
   1.241 +			if (msg->received_time > 0) {
   1.242 +				gchar *tmp_str;
   1.243 +				time_str = g_strdup_printf(" age=%s", tmp_str = format_difftime(difftime(time(NULL), msg->received_time)));
   1.244 +				g_free(tmp_str);
   1.245 +			}
   1.246 +			if (msg->received_host != NULL)
   1.247 +				host_str = g_strdup_printf(" host=%s", msg->received_host);
   1.248 +			if (msg->ident != NULL)
   1.249 +				ident_str = g_strdup_printf(" ident=%s", msg->ident);
   1.250  
   1.251 -      if(size_str) g_free(size_str);
   1.252 -      if(time_str) g_free(time_str);
   1.253 -      if(host_str) g_free(host_str);
   1.254 -      if(ident_str) g_free(ident_str);
   1.255 +			printf("%s <= %s%s%s%s%s\n", msg->uid, addr_string(msg->return_path), size_str ? size_str : "",
   1.256 +			       time_str ? time_str : "", host_str ? host_str : "", ident_str ? ident_str : "");
   1.257  
   1.258 -      foreach(msg->rcpt_list, rcpt_node){
   1.259 -	address *rcpt = (address *)(rcpt_node->data);
   1.260 -      
   1.261 -	printf("              %s %s\n",
   1.262 -	       addr_is_delivered(rcpt) ? "=>" : (addr_is_failed(rcpt) ? "!=" : "=="),
   1.263 -	       addr_string(rcpt));
   1.264 -      }
   1.265 -      g_free(msg);
   1.266 -    }
   1.267 -  }else
   1.268 -    printf("mail queue is empty.\n");
   1.269 -}  
   1.270 +			if (size_str)
   1.271 +				g_free(size_str);
   1.272 +			if (time_str)
   1.273 +				g_free(time_str);
   1.274 +			if (host_str)
   1.275 +				g_free(host_str);
   1.276 +			if (ident_str)
   1.277 +				g_free(ident_str);
   1.278  
   1.279 -gboolean queue_delete(gchar *uid)
   1.280 +			foreach(msg->rcpt_list, rcpt_node) {
   1.281 +				address *rcpt = (address *) (rcpt_node->data);
   1.282 +
   1.283 +				printf("              %s %s\n", addr_is_delivered(rcpt) ? "=>" : (addr_is_failed(rcpt) ? "!=" : "=="), addr_string(rcpt));
   1.284 +			}
   1.285 +			g_free(msg);
   1.286 +		}
   1.287 +	} else
   1.288 +		printf("mail queue is empty.\n");
   1.289 +}
   1.290 +
   1.291 +gboolean
   1.292 +queue_delete(gchar * uid)
   1.293  {
   1.294 -  gboolean hdr_ok = TRUE;
   1.295 -  gboolean dat_ok = TRUE;
   1.296 -  gchar *hdr_name = g_strdup_printf("%s/input/%s-H", conf.spool_dir, uid);
   1.297 -  gchar *dat_name = g_strdup_printf("%s/input/%s-D", conf.spool_dir, uid);
   1.298 -  struct stat stat_buf;
   1.299 +	gboolean hdr_ok = TRUE;
   1.300 +	gboolean dat_ok = TRUE;
   1.301 +	gchar *hdr_name = g_strdup_printf("%s/input/%s-H", conf.spool_dir, uid);
   1.302 +	gchar *dat_name = g_strdup_printf("%s/input/%s-D", conf.spool_dir, uid);
   1.303 +	struct stat stat_buf;
   1.304  
   1.305 -  if(spool_lock(uid)){
   1.306 +	if (spool_lock(uid)) {
   1.307  
   1.308 -    if(stat(hdr_name, &stat_buf) == 0){
   1.309 -      if(unlink(hdr_name) != 0){
   1.310 -	fprintf(stderr, "could not unlink %s: %s\n", hdr_name, strerror(errno));
   1.311 -	hdr_ok = FALSE;
   1.312 -      }
   1.313 -    }else{
   1.314 -      fprintf(stderr, "could not stat file %s: %s\n", hdr_name, strerror(errno));
   1.315 -      hdr_ok = FALSE;
   1.316 -    }
   1.317 -    if(stat(dat_name, &stat_buf) == 0){
   1.318 -      if(unlink(dat_name) != 0){
   1.319 -	fprintf(stderr, "could not unlink %s: %s\n", dat_name, strerror(errno));
   1.320 -	dat_ok = FALSE;
   1.321 -      }
   1.322 -    }else{
   1.323 -      fprintf(stderr, "could not stat file %s: %s\n", dat_name, strerror(errno));
   1.324 -      dat_ok = FALSE;
   1.325 -    }
   1.326 -    printf("message %s deleted\n", uid);
   1.327 +		if (stat(hdr_name, &stat_buf) == 0) {
   1.328 +			if (unlink(hdr_name) != 0) {
   1.329 +				fprintf(stderr, "could not unlink %s: %s\n", hdr_name, strerror(errno));
   1.330 +				hdr_ok = FALSE;
   1.331 +			}
   1.332 +		} else {
   1.333 +			fprintf(stderr, "could not stat file %s: %s\n", hdr_name, strerror(errno));
   1.334 +			hdr_ok = FALSE;
   1.335 +		}
   1.336 +		if (stat(dat_name, &stat_buf) == 0) {
   1.337 +			if (unlink(dat_name) != 0) {
   1.338 +				fprintf(stderr, "could not unlink %s: %s\n", dat_name, strerror(errno));
   1.339 +				dat_ok = FALSE;
   1.340 +			}
   1.341 +		} else {
   1.342 +			fprintf(stderr, "could not stat file %s: %s\n", dat_name, strerror(errno));
   1.343 +			dat_ok = FALSE;
   1.344 +		}
   1.345 +		printf("message %s deleted\n", uid);
   1.346  
   1.347 -    spool_unlock(uid);
   1.348 +		spool_unlock(uid);
   1.349  
   1.350 -  }else{
   1.351 +	} else {
   1.352  
   1.353 -    fprintf(stderr, "message %s is locked.\n", uid);
   1.354 -    return FALSE;
   1.355 -  }
   1.356 +		fprintf(stderr, "message %s is locked.\n", uid);
   1.357 +		return FALSE;
   1.358 +	}
   1.359  
   1.360 -  return (dat_ok && hdr_ok);
   1.361 +	return (dat_ok && hdr_ok);
   1.362  }