Mercurial > masqmail
annotate src/queue.c @ 125:6eec8bcfd320
don't install the tests
author | meillo@marmaro.de |
---|---|
date | Thu, 01 Jul 2010 14:17:50 +0200 (2010-07-01) |
parents | a80ebfa16cd5 |
children | 996b53a50f55 |
rev | line source |
---|---|
0 | 1 /* MasqMail |
2 Copyright (C) 1999-2001 Oliver Kurth | |
3 | |
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. | |
8 | |
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. | |
13 | |
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 */ | |
18 | |
19 #include <sys/stat.h> | |
20 #include <glob.h> | |
21 | |
15 | 22 #include "masqmail.h" |
23 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
24 static void |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
25 mix_arr(int *buf, int len) |
0 | 26 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
27 int i; |
0 | 28 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
29 for (i = 0; i < len; i++) |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
30 buf[i] = i; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
31 for (i = 0; i < len - 1; i++) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
32 int j = (int) ((float) (len - i) * ((float) rand()) / (RAND_MAX + 1.0)); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
33 int tmp; |
0 | 34 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
35 if (i != j) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
36 tmp = buf[i]; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
37 buf[i] = buf[j]; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
38 buf[j] = tmp; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
39 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
40 } |
0 | 41 } |
42 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
43 GList* |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
44 read_queue(gboolean do_readdata) |
0 | 45 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
46 GList *msg_list = NULL; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
47 glob_t gl; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
48 gchar *pattern; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
49 int i, *idx_arr; |
0 | 50 |
38 | 51 /* Escaping the question marks prevents them from being |
52 interpreted as trigraphs */ | |
53 pattern = g_strdup_printf("%s/input/?????\?-??\?-?\?-H", conf.spool_dir); | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
54 gl.gl_offs = 0; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
55 glob(pattern, 0, NULL, &gl); |
0 | 56 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
57 g_free(pattern); |
0 | 58 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
59 DEBUG(4) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
60 int i; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
61 for (i = 0; i < gl.gl_pathc; i++) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
62 debugf("spoolfile: %s\n", gl.gl_pathv[i]); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
63 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
64 } |
0 | 65 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
66 idx_arr = g_malloc(sizeof(int) * gl.gl_pathc); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
67 mix_arr(idx_arr, gl.gl_pathc); |
0 | 68 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
69 for (i = 0; i < gl.gl_pathc; i++) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
70 gchar *uid; |
0 | 71 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
72 /* copy 13 chars, offset spooldir path + 7 chars for /input/ */ |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
73 /* uid length = 6 chars + '-' + 3 chars + '-' + 2 = 13 chars */ |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
74 uid = g_strndup(&(gl.gl_pathv[idx_arr[i]][strlen(conf.spool_dir) + 7]), 13); |
0 | 75 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
76 DEBUG(5) debugf("uid: %s\n", uid); |
0 | 77 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
78 msg_list = g_list_append(msg_list, msg_spool_read(uid, do_readdata)); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
79 |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
80 DEBUG(5) debugf("after read spool file for %s\n", uid); |
0 | 81 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
82 g_free(uid); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
83 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
84 return msg_list; |
0 | 85 } |
86 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
87 gboolean |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
88 queue_run() |
0 | 89 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
90 GList *msg_list; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
91 gboolean ok = TRUE; |
0 | 92 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
93 logwrite(LOG_NOTICE, "Starting queue run.\n"); |
0 | 94 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
95 msg_list = read_queue(FALSE); |
0 | 96 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
97 if (msg_list != NULL) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
98 ok = deliver_msg_list(msg_list, DLVR_ALL); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
99 destroy_msg_list(msg_list); |
114 | 100 logwrite(LOG_NOTICE, " deliver_msg_list()=%d.\n", ok); |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
101 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
102 logwrite(LOG_NOTICE, "Finished queue run.\n"); |
0 | 103 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
104 return ok; |
0 | 105 } |
106 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
107 gboolean |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
108 queue_run_online() |
0 | 109 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
110 GList *msg_list = read_queue(FALSE); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
111 gboolean ok = TRUE; |
0 | 112 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
113 logwrite(LOG_NOTICE, "Starting online queue run.\n"); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
114 if (msg_list != NULL) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
115 ok = deliver_msg_list(msg_list, DLVR_ONLINE); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
116 destroy_msg_list(msg_list); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
117 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
118 logwrite(LOG_NOTICE, "Finished online queue run.\n"); |
0 | 119 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
120 return ok; |
0 | 121 } |
122 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
123 static gchar* |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
124 format_difftime(double secs) |
0 | 125 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
126 if (secs > 86400) |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
127 return g_strdup_printf("%.1fd", secs / 86400); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
128 else if (secs > 3600) |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
129 return g_strdup_printf("%.1fh", secs / 3600); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
130 else if (secs > 60) |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
131 return g_strdup_printf("%.1fm", secs / 60); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
132 else |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
133 return g_strdup_printf("%.0fs", secs); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
134 } |
0 | 135 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
136 void |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
137 queue_list() |
0 | 138 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
139 GList *msg_list; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
140 GList *msg_node; |
0 | 141 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
142 msg_list = read_queue(FALSE); |
0 | 143 |
82 | 144 if (msg_list == NULL) { |
145 printf("mail queue is empty.\n"); | |
146 return; | |
147 } | |
0 | 148 |
82 | 149 foreach(msg_list, msg_node) { |
150 message *msg = (message *) (msg_node->data); | |
151 GList *rcpt_node; | |
152 gchar *size_str = NULL; | |
153 gchar *time_str = NULL; | |
154 gchar *host_str = NULL; | |
155 gchar *ident_str = NULL; | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
156 |
82 | 157 if (msg->data_size >= 0) |
158 size_str = g_strdup_printf(" size=%d", msg->data_size); | |
159 if (msg->received_time > 0) { | |
160 gchar *tmp_str; | |
161 time_str = g_strdup_printf(" age=%s", tmp_str = format_difftime(difftime(time(NULL), msg->received_time))); | |
162 g_free(tmp_str); | |
163 } | |
164 if (msg->received_host != NULL) | |
165 host_str = g_strdup_printf(" host=%s", msg->received_host); | |
166 if (msg->ident != NULL) | |
167 ident_str = g_strdup_printf(" ident=%s", msg->ident); | |
168 | |
169 printf("%s <= %s%s%s%s%s\n", msg->uid, addr_string(msg->return_path), size_str ? size_str : "", | |
170 time_str ? time_str : "", host_str ? host_str : "", ident_str ? ident_str : ""); | |
0 | 171 |
82 | 172 if (size_str) |
173 g_free(size_str); | |
174 if (time_str) | |
175 g_free(time_str); | |
176 if (host_str) | |
177 g_free(host_str); | |
178 if (ident_str) | |
179 g_free(ident_str); | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
180 |
82 | 181 foreach(msg->rcpt_list, rcpt_node) { |
182 address *rcpt = (address *) (rcpt_node->data); | |
0 | 183 |
82 | 184 printf(" %s %s\n", addr_is_delivered(rcpt) ? "=>" : (addr_is_failed(rcpt) ? "!=" : "=="), addr_string(rcpt)); |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
185 } |
82 | 186 g_free(msg); |
187 } | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
188 } |
0 | 189 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
190 gboolean |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
191 queue_delete(gchar * uid) |
0 | 192 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
193 gboolean hdr_ok = TRUE; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
194 gboolean dat_ok = TRUE; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
195 gchar *hdr_name = g_strdup_printf("%s/input/%s-H", conf.spool_dir, uid); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
196 gchar *dat_name = g_strdup_printf("%s/input/%s-D", conf.spool_dir, uid); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
197 struct stat stat_buf; |
0 | 198 |
82 | 199 if (!spool_lock(uid)) { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
200 fprintf(stderr, "message %s is locked.\n", uid); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
201 return FALSE; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
202 } |
0 | 203 |
82 | 204 if (stat(hdr_name, &stat_buf) == 0) { |
205 if (unlink(hdr_name) != 0) { | |
206 fprintf(stderr, "could not unlink %s: %s\n", hdr_name, strerror(errno)); | |
207 hdr_ok = FALSE; | |
208 } | |
209 } else { | |
210 fprintf(stderr, "could not stat file %s: %s\n", hdr_name, strerror(errno)); | |
211 hdr_ok = FALSE; | |
212 } | |
213 if (stat(dat_name, &stat_buf) == 0) { | |
214 if (unlink(dat_name) != 0) { | |
215 fprintf(stderr, "could not unlink %s: %s\n", dat_name, strerror(errno)); | |
216 dat_ok = FALSE; | |
217 } | |
218 } else { | |
219 fprintf(stderr, "could not stat file %s: %s\n", dat_name, strerror(errno)); | |
220 dat_ok = FALSE; | |
221 } | |
222 printf("message %s deleted\n", uid); | |
223 | |
224 spool_unlock(uid); | |
225 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
226 return (dat_ok && hdr_ok); |
0 | 227 } |