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