masqmail-0.2

view src/queue.c @ 12:9fb7ddbaf129

removed noop-code; some beautifying
author meillo@marmaro.de
date Wed, 29 Oct 2008 16:34:13 +0100
parents 08114f7dcc23
children f671821d8222
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 void
25 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];
37 buf[i] = buf[j];
38 buf[j] = tmp;
39 }
40 }
41 }
43 GList*
44 read_queue(gboolean do_readdata)
45 {
46 GList *msg_list = NULL;
47 glob_t gl;
48 gchar *pattern;
49 int i, *idx_arr;
51 pattern = g_strdup_printf("%s/input/??????-???-??-H", conf.spool_dir);
52 gl.gl_offs = 0;
53 glob(pattern, 0, NULL, &gl);
55 g_free(pattern);
57 DEBUG(4) {
58 int i;
59 for (i = 0; i < gl.gl_pathc; i++) {
60 debugf("spoolfile: %s\n", gl.gl_pathv[i]);
61 }
62 }
64 idx_arr = g_malloc(sizeof(int) * gl.gl_pathc);
65 mix_arr(idx_arr, gl.gl_pathc);
67 for (i = 0; i < gl.gl_pathc; i++) {
68 gchar *uid;
70 /* copy 13 chars, offset spooldir path + 7 chars for /input/ */
71 /* uid length = 6 chars + '-' + 3 chars + '-' + 2 = 13 chars */
72 uid = g_strndup(&(gl.gl_pathv[idx_arr[i]][strlen(conf.spool_dir) + 7]), 13);
74 DEBUG(5) debugf("uid: %s\n", uid);
76 msg_list = g_list_append(msg_list, msg_spool_read(uid, do_readdata));
78 DEBUG(5) debugf("after read spool file for %s\n", uid);
80 g_free(uid);
81 }
82 return msg_list;
83 }
85 gboolean
86 queue_run()
87 {
88 GList *msg_list;
89 gboolean ok = TRUE;
91 logwrite(LOG_NOTICE, "Starting queue run.\n");
93 msg_list = read_queue(FALSE);
95 if (msg_list != NULL) {
96 ok = deliver_msg_list(msg_list, DLVR_ALL);
97 destroy_msg_list(msg_list);
98 }
99 logwrite(LOG_NOTICE, "Finished queue run.\n");
101 return ok;
102 }
104 gboolean
105 queue_run_online()
106 {
107 GList *msg_list = read_queue(FALSE);
108 gboolean ok = TRUE;
110 logwrite(LOG_NOTICE, "Starting online queue run.\n");
111 if (msg_list != NULL) {
112 ok = deliver_msg_list(msg_list, DLVR_ONLINE);
113 destroy_msg_list(msg_list);
114 }
115 logwrite(LOG_NOTICE, "Finished online queue run.\n");
117 return ok;
118 }
120 static gchar*
121 format_difftime(double secs)
122 {
123 if (secs > 86400)
124 return g_strdup_printf("%.1fd", secs / 86400);
125 else if (secs > 3600)
126 return g_strdup_printf("%.1fh", secs / 3600);
127 else if (secs > 60)
128 return g_strdup_printf("%.1fm", secs / 60);
129 else
130 return g_strdup_printf("%.0fs", secs);
131 }
133 void
134 queue_list()
135 {
136 GList *msg_list;
137 GList *msg_node;
139 msg_list = read_queue(FALSE);
141 if (msg_list != NULL) {
142 foreach(msg_list, msg_node) {
143 message *msg = (message *) (msg_node->data);
144 GList *rcpt_node;
145 gchar *size_str = NULL;
146 gchar *time_str = NULL;
147 gchar *host_str = NULL;
148 gchar *ident_str = NULL;
150 if (msg->data_size >= 0)
151 size_str = g_strdup_printf(" size=%d", msg->data_size);
152 if (msg->received_time > 0) {
153 gchar *tmp_str;
154 time_str = g_strdup_printf(" age=%s", tmp_str = format_difftime(difftime(time(NULL), msg->received_time)));
155 g_free(tmp_str);
156 }
157 if (msg->received_host != NULL)
158 host_str = g_strdup_printf(" host=%s", msg->received_host);
159 if (msg->ident != NULL)
160 ident_str = g_strdup_printf(" ident=%s", msg->ident);
162 printf("%s <= %s%s%s%s%s\n", msg->uid, addr_string(msg->return_path), size_str ? size_str : "",
163 time_str ? time_str : "", host_str ? host_str : "", ident_str ? ident_str : "");
165 if (size_str)
166 g_free(size_str);
167 if (time_str)
168 g_free(time_str);
169 if (host_str)
170 g_free(host_str);
171 if (ident_str)
172 g_free(ident_str);
174 foreach(msg->rcpt_list, rcpt_node) {
175 address *rcpt = (address *) (rcpt_node->data);
177 printf(" %s %s\n", addr_is_delivered(rcpt) ? "=>" : (addr_is_failed(rcpt) ? "!=" : "=="), addr_string(rcpt));
178 }
179 g_free(msg);
180 }
181 } else
182 printf("mail queue is empty.\n");
183 }
185 gboolean
186 queue_delete(gchar * uid)
187 {
188 gboolean hdr_ok = TRUE;
189 gboolean dat_ok = TRUE;
190 gchar *hdr_name = g_strdup_printf("%s/input/%s-H", conf.spool_dir, uid);
191 gchar *dat_name = g_strdup_printf("%s/input/%s-D", conf.spool_dir, uid);
192 struct stat stat_buf;
194 if (spool_lock(uid)) {
196 if (stat(hdr_name, &stat_buf) == 0) {
197 if (unlink(hdr_name) != 0) {
198 fprintf(stderr, "could not unlink %s: %s\n", hdr_name, strerror(errno));
199 hdr_ok = FALSE;
200 }
201 } else {
202 fprintf(stderr, "could not stat file %s: %s\n", hdr_name, strerror(errno));
203 hdr_ok = FALSE;
204 }
205 if (stat(dat_name, &stat_buf) == 0) {
206 if (unlink(dat_name) != 0) {
207 fprintf(stderr, "could not unlink %s: %s\n", dat_name, strerror(errno));
208 dat_ok = FALSE;
209 }
210 } else {
211 fprintf(stderr, "could not stat file %s: %s\n", dat_name, strerror(errno));
212 dat_ok = FALSE;
213 }
214 printf("message %s deleted\n", uid);
216 spool_unlock(uid);
218 } else {
220 fprintf(stderr, "message %s is locked.\n", uid);
221 return FALSE;
222 }
224 return (dat_ok && hdr_ok);
225 }