masqmail

diff src/queue.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/queue.c	Fri Sep 26 17:05:23 2008 +0200
     1.3 @@ -0,0 +1,224 @@
     1.4 +/*  MasqMail
     1.5 +    Copyright (C) 1999-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 +#include <sys/stat.h>
    1.25 +#include <glob.h>
    1.26 +
    1.27 +static
    1.28 +void mix_arr(int *buf, int len)
    1.29 +{
    1.30 +  int i;
    1.31 +
    1.32 +  for(i = 0; i < len; i++)
    1.33 +    buf[i] = i;
    1.34 +  for(i = 0; i < len-1; i++){
    1.35 +    int j = (int)((float)(len-i) * ((float)rand())/(RAND_MAX + 1.0));
    1.36 +    int tmp;
    1.37 +
    1.38 +    if(i != j){
    1.39 +      tmp = buf[i]; buf[i] = buf[j]; buf[j] = tmp;
    1.40 +    }
    1.41 +  }
    1.42 +}
    1.43 +
    1.44 +GList *read_queue(gboolean do_readdata)
    1.45 +{
    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 +
    1.55 +  g_free(pattern);
    1.56 +
    1.57 +  DEBUG(4){
    1.58 +    int i;
    1.59 +    for(i = 0; i < gl.gl_pathc; i++){
    1.60 +      debugf("spoolfile: %s\n", gl.gl_pathv[i]);
    1.61 +    }
    1.62 +  }
    1.63 +
    1.64 +  idx_arr = g_malloc(sizeof(int) * gl.gl_pathc);
    1.65 +  mix_arr(idx_arr, gl.gl_pathc);
    1.66 +
    1.67 +  for(i = 0; i < gl.gl_pathc; i++){
    1.68 +    gchar *uid;
    1.69 +
    1.70 +    /* copy 13 chars, offset spooldir path + 7 chars for /input/ */
    1.71 +    /* uid length = 6 chars + '-' + 3 chars + '-' + 2 = 13 chars */
    1.72 +    uid = g_strndup(&(gl.gl_pathv[idx_arr[i]][strlen(conf.spool_dir) + 7]), 13);
    1.73 +
    1.74 +    DEBUG(5) debugf("uid: %s\n", uid);
    1.75 +
    1.76 +    msg_list = g_list_append(msg_list, msg_spool_read(uid, do_readdata));
    1.77 +
    1.78 +    DEBUG(5) debugf("after read spool file for %s\n", uid);
    1.79 +
    1.80 +    g_free(uid);
    1.81 +  }
    1.82 +  return msg_list;
    1.83 +}
    1.84 +
    1.85 +gboolean queue_run()
    1.86 +{
    1.87 +  GList *msg_list;
    1.88 +  gboolean ok = TRUE;
    1.89 +
    1.90 +  logwrite(LOG_NOTICE, "Starting queue run.\n");
    1.91 +
    1.92 +  msg_list = read_queue(FALSE);
    1.93 +
    1.94 +  if(msg_list != NULL){
    1.95 +    ok = deliver_msg_list(msg_list, DLVR_ALL);
    1.96 +    destroy_msg_list(msg_list);
    1.97 +  }
    1.98 +  logwrite(LOG_NOTICE, "Finished queue run.\n");
    1.99 +
   1.100 +  return ok;
   1.101 +}
   1.102 +
   1.103 +gboolean queue_run_online()
   1.104 +{
   1.105 +  GList *msg_list = read_queue(FALSE);
   1.106 +  gboolean ok = TRUE;
   1.107 +
   1.108 +  logwrite(LOG_NOTICE, "Starting online queue run.\n");
   1.109 +  if(msg_list != NULL){
   1.110 +    ok = deliver_msg_list(msg_list, DLVR_ONLINE);
   1.111 +    destroy_msg_list(msg_list);
   1.112 +  }
   1.113 +  logwrite(LOG_NOTICE, "Finished online queue run.\n");
   1.114 +
   1.115 +  return ok;
   1.116 +}
   1.117 +
   1.118 +static
   1.119 +gchar *format_difftime(double secs)
   1.120 +{
   1.121 +  if(secs > 86400)
   1.122 +    return g_strdup_printf("%.1fd", secs/86400);
   1.123 +  else if(secs > 3600)
   1.124 +    return g_strdup_printf("%.1fh", secs/3600);
   1.125 +  else if(secs > 60)
   1.126 +    return g_strdup_printf("%.1fm", secs/60);
   1.127 +  else
   1.128 +    return g_strdup_printf("%.0fs", secs);
   1.129 +}  
   1.130 +
   1.131 +void queue_list()
   1.132 +{
   1.133 +  GList *msg_list;
   1.134 +  GList *msg_node;
   1.135 +
   1.136 +  msg_list = read_queue(FALSE);
   1.137 +
   1.138 +  if(msg_list != NULL){
   1.139 +    foreach(msg_list, msg_node){
   1.140 +      message *msg = (message *)(msg_node->data);
   1.141 +      GList *rcpt_node;
   1.142 +      gchar *size_str = NULL;
   1.143 +      gchar *time_str = NULL;
   1.144 +      gchar *host_str = NULL;
   1.145 +      gchar *ident_str = NULL;
   1.146 +    
   1.147 +      if(msg->data_size >= 0)
   1.148 +	size_str = g_strdup_printf(" size=%d", msg->data_size);
   1.149 +      if(msg->received_time > 0){
   1.150 +	gchar *tmp_str;
   1.151 +	time_str =
   1.152 +	  g_strdup_printf(" age=%s",
   1.153 +			  tmp_str = format_difftime(difftime(time(NULL),
   1.154 +							     msg->received_time)));
   1.155 +	g_free(tmp_str);
   1.156 +      }
   1.157 +      if(msg->received_host != NULL)
   1.158 +	host_str = g_strdup_printf(" host=%s", msg->received_host);
   1.159 +      if(msg->ident != NULL)
   1.160 +	ident_str = g_strdup_printf(" ident=%s", msg->ident);
   1.161 +
   1.162 +      printf("%s <= %s%s%s%s%s\n", msg->uid,
   1.163 +	     addr_string(msg->return_path),
   1.164 +	     size_str ? size_str : "",
   1.165 +	     time_str ? time_str : "",
   1.166 +	     host_str ? host_str : "",
   1.167 +	     ident_str ? ident_str : ""
   1.168 +	     );
   1.169 +
   1.170 +      if(size_str) g_free(size_str);
   1.171 +      if(time_str) g_free(time_str);
   1.172 +      if(host_str) g_free(host_str);
   1.173 +      if(ident_str) g_free(ident_str);
   1.174 +
   1.175 +      foreach(msg->rcpt_list, rcpt_node){
   1.176 +	address *rcpt = (address *)(rcpt_node->data);
   1.177 +      
   1.178 +	printf("              %s %s\n",
   1.179 +	       addr_is_delivered(rcpt) ? "=>" : (addr_is_failed(rcpt) ? "!=" : "=="),
   1.180 +	       addr_string(rcpt));
   1.181 +      }
   1.182 +      g_free(msg);
   1.183 +    }
   1.184 +  }else
   1.185 +    printf("mail queue is empty.\n");
   1.186 +}  
   1.187 +
   1.188 +gboolean queue_delete(gchar *uid)
   1.189 +{
   1.190 +  gboolean hdr_ok = TRUE;
   1.191 +  gboolean dat_ok = TRUE;
   1.192 +  gchar *hdr_name = g_strdup_printf("%s/input/%s-H", conf.spool_dir, uid);
   1.193 +  gchar *dat_name = g_strdup_printf("%s/input/%s-D", conf.spool_dir, uid);
   1.194 +  struct stat stat_buf;
   1.195 +
   1.196 +  if(spool_lock(uid)){
   1.197 +
   1.198 +    if(stat(hdr_name, &stat_buf) == 0){
   1.199 +      if(unlink(hdr_name) != 0){
   1.200 +	fprintf(stderr, "could not unlink %s: %s\n", hdr_name, strerror(errno));
   1.201 +	hdr_ok = FALSE;
   1.202 +      }
   1.203 +    }else{
   1.204 +      fprintf(stderr, "could not stat file %s: %s\n", hdr_name, strerror(errno));
   1.205 +      hdr_ok = FALSE;
   1.206 +    }
   1.207 +    if(stat(dat_name, &stat_buf) == 0){
   1.208 +      if(unlink(dat_name) != 0){
   1.209 +	fprintf(stderr, "could not unlink %s: %s\n", dat_name, strerror(errno));
   1.210 +	dat_ok = FALSE;
   1.211 +      }
   1.212 +    }else{
   1.213 +      fprintf(stderr, "could not stat file %s: %s\n", dat_name, strerror(errno));
   1.214 +      dat_ok = FALSE;
   1.215 +    }
   1.216 +    printf("message %s deleted\n", uid);
   1.217 +
   1.218 +    spool_unlock(uid);
   1.219 +
   1.220 +  }else{
   1.221 +
   1.222 +    fprintf(stderr, "message %s is locked.\n", uid);
   1.223 +    return FALSE;
   1.224 +  }
   1.225 +
   1.226 +  return (dat_ok && hdr_ok);
   1.227 +}