masqmail-0.2
view src/queue.c @ 189:bdd343cc4e19
bumped version number
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Sat, 27 Aug 2011 18:51:46 +0200 |
parents | a80ebfa16cd5 |
children |
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
3 Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
20 #include <sys/stat.h>
21 #include <glob.h>
23 #include "masqmail.h"
25 static void
26 mix_arr(int *buf, int len)
27 {
28 int i;
30 for (i = 0; i < len; i++)
31 buf[i] = i;
32 for (i = 0; i < len - 1; i++) {
33 int j = (int) ((float) (len - i) * ((float) rand()) / (RAND_MAX + 1.0));
34 int tmp;
36 if (i != j) {
37 tmp = buf[i];
38 buf[i] = buf[j];
39 buf[j] = tmp;
40 }
41 }
42 }
44 GList*
45 read_queue(gboolean do_readdata)
46 {
47 GList *msg_list = NULL;
48 glob_t gl;
49 gchar *pattern;
50 int i, *idx_arr;
52 /* Escaping the question marks prevents them from being
53 interpreted as trigraphs */
54 pattern = g_strdup_printf("%s/input/?????\?-??\?-?\?-H", conf.spool_dir);
55 gl.gl_offs = 0;
56 glob(pattern, 0, NULL, &gl);
58 g_free(pattern);
60 DEBUG(4) {
61 int i;
62 for (i = 0; i < gl.gl_pathc; i++) {
63 debugf("spoolfile: %s\n", gl.gl_pathv[i]);
64 }
65 }
67 idx_arr = g_malloc(sizeof(int) * gl.gl_pathc);
68 mix_arr(idx_arr, gl.gl_pathc);
70 for (i = 0; i < gl.gl_pathc; i++) {
71 gchar *uid;
73 /* copy 13 chars, offset spooldir path + 7 chars for /input/ */
74 /* uid length = 6 chars + '-' + 3 chars + '-' + 2 = 13 chars */
75 uid = g_strndup(&(gl.gl_pathv[idx_arr[i]][strlen(conf.spool_dir) + 7]), 13);
77 DEBUG(5) debugf("uid: %s\n", uid);
79 msg_list = g_list_append(msg_list, msg_spool_read(uid, do_readdata));
81 DEBUG(5) debugf("after read spool file for %s\n", uid);
83 g_free(uid);
84 }
85 return msg_list;
86 }
88 gboolean
89 queue_run()
90 {
91 GList *msg_list;
92 gboolean ok = TRUE;
94 logwrite(LOG_NOTICE, "Starting queue run.\n");
96 msg_list = read_queue(FALSE);
98 if (msg_list != NULL) {
99 ok = deliver_msg_list(msg_list, DLVR_ALL);
100 destroy_msg_list(msg_list);
101 logwrite(LOG_NOTICE, " deliver_msg_list()=%d.\n", ok);
102 }
103 logwrite(LOG_NOTICE, "Finished queue run.\n");
105 return ok;
106 }
108 gboolean
109 queue_run_online()
110 {
111 GList *msg_list = read_queue(FALSE);
112 gboolean ok = TRUE;
114 logwrite(LOG_NOTICE, "Starting online queue run.\n");
115 if (msg_list != NULL) {
116 ok = deliver_msg_list(msg_list, DLVR_ONLINE);
117 destroy_msg_list(msg_list);
118 }
119 logwrite(LOG_NOTICE, "Finished online queue run.\n");
121 return ok;
122 }
124 static gchar*
125 format_difftime(double secs)
126 {
127 if (secs > 86400)
128 return g_strdup_printf("%.1fd", secs / 86400);
129 else if (secs > 3600)
130 return g_strdup_printf("%.1fh", secs / 3600);
131 else if (secs > 60)
132 return g_strdup_printf("%.1fm", secs / 60);
133 else
134 return g_strdup_printf("%.0fs", secs);
135 }
137 void
138 queue_list()
139 {
140 GList *msg_list;
141 GList *msg_node;
143 msg_list = read_queue(FALSE);
145 if (msg_list == NULL) {
146 printf("mail queue is empty.\n");
147 return;
148 }
150 foreach(msg_list, msg_node) {
151 message *msg = (message *) (msg_node->data);
152 GList *rcpt_node;
153 gchar *size_str = NULL;
154 gchar *time_str = NULL;
155 gchar *host_str = NULL;
156 gchar *ident_str = NULL;
158 if (msg->data_size >= 0)
159 size_str = g_strdup_printf(" size=%d", msg->data_size);
160 if (msg->received_time > 0) {
161 gchar *tmp_str;
162 time_str = g_strdup_printf(" age=%s", tmp_str = format_difftime(difftime(time(NULL), msg->received_time)));
163 g_free(tmp_str);
164 }
165 if (msg->received_host != NULL)
166 host_str = g_strdup_printf(" host=%s", msg->received_host);
167 if (msg->ident != NULL)
168 ident_str = g_strdup_printf(" ident=%s", msg->ident);
170 printf("%s <= %s%s%s%s%s\n", msg->uid, addr_string(msg->return_path), size_str ? size_str : "",
171 time_str ? time_str : "", host_str ? host_str : "", ident_str ? ident_str : "");
173 if (size_str)
174 g_free(size_str);
175 if (time_str)
176 g_free(time_str);
177 if (host_str)
178 g_free(host_str);
179 if (ident_str)
180 g_free(ident_str);
182 foreach(msg->rcpt_list, rcpt_node) {
183 address *rcpt = (address *) (rcpt_node->data);
185 printf(" %s %s\n", addr_is_delivered(rcpt) ? "=>" : (addr_is_failed(rcpt) ? "!=" : "=="), addr_string(rcpt));
186 }
187 g_free(msg);
188 }
189 }
191 gboolean
192 queue_delete(gchar * uid)
193 {
194 gboolean hdr_ok = TRUE;
195 gboolean dat_ok = TRUE;
196 gchar *hdr_name = g_strdup_printf("%s/input/%s-H", conf.spool_dir, uid);
197 gchar *dat_name = g_strdup_printf("%s/input/%s-D", conf.spool_dir, uid);
198 struct stat stat_buf;
200 if (!spool_lock(uid)) {
201 fprintf(stderr, "message %s is locked.\n", uid);
202 return FALSE;
203 }
205 if (stat(hdr_name, &stat_buf) == 0) {
206 if (unlink(hdr_name) != 0) {
207 fprintf(stderr, "could not unlink %s: %s\n", hdr_name, strerror(errno));
208 hdr_ok = FALSE;
209 }
210 } else {
211 fprintf(stderr, "could not stat file %s: %s\n", hdr_name, strerror(errno));
212 hdr_ok = FALSE;
213 }
214 if (stat(dat_name, &stat_buf) == 0) {
215 if (unlink(dat_name) != 0) {
216 fprintf(stderr, "could not unlink %s: %s\n", dat_name, strerror(errno));
217 dat_ok = FALSE;
218 }
219 } else {
220 fprintf(stderr, "could not stat file %s: %s\n", dat_name, strerror(errno));
221 dat_ok = FALSE;
222 }
223 printf("message %s deleted\n", uid);
225 spool_unlock(uid);
227 return (dat_ok && hdr_ok);
228 }