masqmail

view src/queue.c @ 6:c9bce6bb2a5d

switched tests dir to ordinary Makefile
author meillo@marmaro.de
date Fri, 26 Sep 2008 22:55:52 +0200
parents
children 26e34ae9a3e3
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
19 #include "masqmail.h"
21 #include <sys/stat.h>
22 #include <glob.h>
24 static
25 void mix_arr(int *buf, int len)
26 {
27 int i;
29 for(i = 0; i < len; i++)
30 buf[i] = i;
31 for(i = 0; i < len-1; i++){
32 int j = (int)((float)(len-i) * ((float)rand())/(RAND_MAX + 1.0));
33 int tmp;
35 if(i != j){
36 tmp = buf[i]; buf[i] = buf[j]; buf[j] = tmp;
37 }
38 }
39 }
41 GList *read_queue(gboolean do_readdata)
42 {
43 GList *msg_list = NULL;
44 glob_t gl;
45 gchar *pattern;
46 int i, *idx_arr;
48 pattern = g_strdup_printf("%s/input/??????-???-??-H", conf.spool_dir);
49 gl.gl_offs = 0;
50 glob(pattern, 0, NULL, &gl);
52 g_free(pattern);
54 DEBUG(4){
55 int i;
56 for(i = 0; i < gl.gl_pathc; i++){
57 debugf("spoolfile: %s\n", gl.gl_pathv[i]);
58 }
59 }
61 idx_arr = g_malloc(sizeof(int) * gl.gl_pathc);
62 mix_arr(idx_arr, gl.gl_pathc);
64 for(i = 0; i < gl.gl_pathc; i++){
65 gchar *uid;
67 /* copy 13 chars, offset spooldir path + 7 chars for /input/ */
68 /* uid length = 6 chars + '-' + 3 chars + '-' + 2 = 13 chars */
69 uid = g_strndup(&(gl.gl_pathv[idx_arr[i]][strlen(conf.spool_dir) + 7]), 13);
71 DEBUG(5) debugf("uid: %s\n", uid);
73 msg_list = g_list_append(msg_list, msg_spool_read(uid, do_readdata));
75 DEBUG(5) debugf("after read spool file for %s\n", uid);
77 g_free(uid);
78 }
79 return msg_list;
80 }
82 gboolean queue_run()
83 {
84 GList *msg_list;
85 gboolean ok = TRUE;
87 logwrite(LOG_NOTICE, "Starting queue run.\n");
89 msg_list = read_queue(FALSE);
91 if(msg_list != NULL){
92 ok = deliver_msg_list(msg_list, DLVR_ALL);
93 destroy_msg_list(msg_list);
94 }
95 logwrite(LOG_NOTICE, "Finished queue run.\n");
97 return ok;
98 }
100 gboolean queue_run_online()
101 {
102 GList *msg_list = read_queue(FALSE);
103 gboolean ok = TRUE;
105 logwrite(LOG_NOTICE, "Starting online queue run.\n");
106 if(msg_list != NULL){
107 ok = deliver_msg_list(msg_list, DLVR_ONLINE);
108 destroy_msg_list(msg_list);
109 }
110 logwrite(LOG_NOTICE, "Finished online queue run.\n");
112 return ok;
113 }
115 static
116 gchar *format_difftime(double secs)
117 {
118 if(secs > 86400)
119 return g_strdup_printf("%.1fd", secs/86400);
120 else if(secs > 3600)
121 return g_strdup_printf("%.1fh", secs/3600);
122 else if(secs > 60)
123 return g_strdup_printf("%.1fm", secs/60);
124 else
125 return g_strdup_printf("%.0fs", secs);
126 }
128 void queue_list()
129 {
130 GList *msg_list;
131 GList *msg_node;
133 msg_list = read_queue(FALSE);
135 if(msg_list != NULL){
136 foreach(msg_list, msg_node){
137 message *msg = (message *)(msg_node->data);
138 GList *rcpt_node;
139 gchar *size_str = NULL;
140 gchar *time_str = NULL;
141 gchar *host_str = NULL;
142 gchar *ident_str = NULL;
144 if(msg->data_size >= 0)
145 size_str = g_strdup_printf(" size=%d", msg->data_size);
146 if(msg->received_time > 0){
147 gchar *tmp_str;
148 time_str =
149 g_strdup_printf(" age=%s",
150 tmp_str = format_difftime(difftime(time(NULL),
151 msg->received_time)));
152 g_free(tmp_str);
153 }
154 if(msg->received_host != NULL)
155 host_str = g_strdup_printf(" host=%s", msg->received_host);
156 if(msg->ident != NULL)
157 ident_str = g_strdup_printf(" ident=%s", msg->ident);
159 printf("%s <= %s%s%s%s%s\n", msg->uid,
160 addr_string(msg->return_path),
161 size_str ? size_str : "",
162 time_str ? time_str : "",
163 host_str ? host_str : "",
164 ident_str ? ident_str : ""
165 );
167 if(size_str) g_free(size_str);
168 if(time_str) g_free(time_str);
169 if(host_str) g_free(host_str);
170 if(ident_str) g_free(ident_str);
172 foreach(msg->rcpt_list, rcpt_node){
173 address *rcpt = (address *)(rcpt_node->data);
175 printf(" %s %s\n",
176 addr_is_delivered(rcpt) ? "=>" : (addr_is_failed(rcpt) ? "!=" : "=="),
177 addr_string(rcpt));
178 }
179 g_free(msg);
180 }
181 }else
182 printf("mail queue is empty.\n");
183 }
185 gboolean queue_delete(gchar *uid)
186 {
187 gboolean hdr_ok = TRUE;
188 gboolean dat_ok = TRUE;
189 gchar *hdr_name = g_strdup_printf("%s/input/%s-H", conf.spool_dir, uid);
190 gchar *dat_name = g_strdup_printf("%s/input/%s-D", conf.spool_dir, uid);
191 struct stat stat_buf;
193 if(spool_lock(uid)){
195 if(stat(hdr_name, &stat_buf) == 0){
196 if(unlink(hdr_name) != 0){
197 fprintf(stderr, "could not unlink %s: %s\n", hdr_name, strerror(errno));
198 hdr_ok = FALSE;
199 }
200 }else{
201 fprintf(stderr, "could not stat file %s: %s\n", hdr_name, strerror(errno));
202 hdr_ok = FALSE;
203 }
204 if(stat(dat_name, &stat_buf) == 0){
205 if(unlink(dat_name) != 0){
206 fprintf(stderr, "could not unlink %s: %s\n", dat_name, strerror(errno));
207 dat_ok = FALSE;
208 }
209 }else{
210 fprintf(stderr, "could not stat file %s: %s\n", dat_name, strerror(errno));
211 dat_ok = FALSE;
212 }
213 printf("message %s deleted\n", uid);
215 spool_unlock(uid);
217 }else{
219 fprintf(stderr, "message %s is locked.\n", uid);
220 return FALSE;
221 }
223 return (dat_ok && hdr_ok);
224 }