meillo@0: /* MasqMail meillo@0: Copyright (C) 1999-2001 Oliver Kurth meillo@174: Copyright (C) 2010 markus schnalke meillo@0: meillo@0: This program is free software; you can redistribute it and/or modify meillo@0: it under the terms of the GNU General Public License as published by meillo@0: the Free Software Foundation; either version 2 of the License, or meillo@0: (at your option) any later version. meillo@0: meillo@0: This program is distributed in the hope that it will be useful, meillo@0: but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@0: GNU General Public License for more details. meillo@0: meillo@0: You should have received a copy of the GNU General Public License meillo@0: along with this program; if not, write to the Free Software meillo@0: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. meillo@0: */ meillo@0: meillo@0: #include meillo@0: #include meillo@0: meillo@15: #include "masqmail.h" meillo@15: meillo@10: static void meillo@10: mix_arr(int *buf, int len) meillo@0: { meillo@10: int i; meillo@0: meillo@10: for (i = 0; i < len; i++) meillo@10: buf[i] = i; meillo@10: for (i = 0; i < len - 1; i++) { meillo@10: int j = (int) ((float) (len - i) * ((float) rand()) / (RAND_MAX + 1.0)); meillo@10: int tmp; meillo@0: meillo@10: if (i != j) { meillo@10: tmp = buf[i]; meillo@10: buf[i] = buf[j]; meillo@10: buf[j] = tmp; meillo@10: } meillo@10: } meillo@0: } meillo@0: meillo@10: GList* meillo@10: read_queue(gboolean do_readdata) meillo@0: { meillo@10: GList *msg_list = NULL; meillo@10: glob_t gl; meillo@10: gchar *pattern; meillo@10: int i, *idx_arr; meillo@0: meillo@38: /* Escaping the question marks prevents them from being meillo@38: interpreted as trigraphs */ meillo@38: pattern = g_strdup_printf("%s/input/?????\?-??\?-?\?-H", conf.spool_dir); meillo@10: gl.gl_offs = 0; meillo@10: glob(pattern, 0, NULL, &gl); meillo@0: meillo@10: g_free(pattern); meillo@0: meillo@10: DEBUG(4) { meillo@10: int i; meillo@10: for (i = 0; i < gl.gl_pathc; i++) { meillo@10: debugf("spoolfile: %s\n", gl.gl_pathv[i]); meillo@10: } meillo@10: } meillo@0: meillo@10: idx_arr = g_malloc(sizeof(int) * gl.gl_pathc); meillo@10: mix_arr(idx_arr, gl.gl_pathc); meillo@0: meillo@10: for (i = 0; i < gl.gl_pathc; i++) { meillo@10: gchar *uid; meillo@0: meillo@10: /* copy 13 chars, offset spooldir path + 7 chars for /input/ */ meillo@10: /* uid length = 6 chars + '-' + 3 chars + '-' + 2 = 13 chars */ meillo@10: uid = g_strndup(&(gl.gl_pathv[idx_arr[i]][strlen(conf.spool_dir) + 7]), 13); meillo@0: meillo@10: DEBUG(5) debugf("uid: %s\n", uid); meillo@0: meillo@10: msg_list = g_list_append(msg_list, msg_spool_read(uid, do_readdata)); meillo@0: meillo@10: DEBUG(5) debugf("after read spool file for %s\n", uid); meillo@0: meillo@10: g_free(uid); meillo@10: } meillo@10: return msg_list; meillo@0: } meillo@0: meillo@10: gboolean meillo@10: queue_run() meillo@0: { meillo@10: GList *msg_list; meillo@10: gboolean ok = TRUE; meillo@0: meillo@10: logwrite(LOG_NOTICE, "Starting queue run.\n"); meillo@0: meillo@10: msg_list = read_queue(FALSE); meillo@0: meillo@10: if (msg_list != NULL) { meillo@10: ok = deliver_msg_list(msg_list, DLVR_ALL); meillo@10: destroy_msg_list(msg_list); meillo@114: logwrite(LOG_NOTICE, " deliver_msg_list()=%d.\n", ok); meillo@10: } meillo@10: logwrite(LOG_NOTICE, "Finished queue run.\n"); meillo@0: meillo@10: return ok; meillo@0: } meillo@0: meillo@10: gboolean meillo@10: queue_run_online() meillo@0: { meillo@10: GList *msg_list = read_queue(FALSE); meillo@10: gboolean ok = TRUE; meillo@0: meillo@10: logwrite(LOG_NOTICE, "Starting online queue run.\n"); meillo@10: if (msg_list != NULL) { meillo@10: ok = deliver_msg_list(msg_list, DLVR_ONLINE); meillo@10: destroy_msg_list(msg_list); meillo@10: } meillo@10: logwrite(LOG_NOTICE, "Finished online queue run.\n"); meillo@0: meillo@10: return ok; meillo@0: } meillo@0: meillo@10: static gchar* meillo@10: format_difftime(double secs) meillo@0: { meillo@10: if (secs > 86400) meillo@10: return g_strdup_printf("%.1fd", secs / 86400); meillo@10: else if (secs > 3600) meillo@10: return g_strdup_printf("%.1fh", secs / 3600); meillo@10: else if (secs > 60) meillo@10: return g_strdup_printf("%.1fm", secs / 60); meillo@10: else meillo@10: return g_strdup_printf("%.0fs", secs); meillo@10: } meillo@0: meillo@10: void meillo@10: queue_list() meillo@0: { meillo@10: GList *msg_list; meillo@10: GList *msg_node; meillo@0: meillo@10: msg_list = read_queue(FALSE); meillo@0: meillo@82: if (msg_list == NULL) { meillo@82: printf("mail queue is empty.\n"); meillo@82: return; meillo@82: } meillo@0: meillo@82: foreach(msg_list, msg_node) { meillo@82: message *msg = (message *) (msg_node->data); meillo@82: GList *rcpt_node; meillo@82: gchar *size_str = NULL; meillo@82: gchar *time_str = NULL; meillo@82: gchar *host_str = NULL; meillo@82: gchar *ident_str = NULL; meillo@0: meillo@82: if (msg->data_size >= 0) meillo@82: size_str = g_strdup_printf(" size=%d", msg->data_size); meillo@82: if (msg->received_time > 0) { meillo@82: gchar *tmp_str; meillo@82: time_str = g_strdup_printf(" age=%s", tmp_str = format_difftime(difftime(time(NULL), msg->received_time))); meillo@82: g_free(tmp_str); meillo@82: } meillo@82: if (msg->received_host != NULL) meillo@82: host_str = g_strdup_printf(" host=%s", msg->received_host); meillo@82: if (msg->ident != NULL) meillo@82: ident_str = g_strdup_printf(" ident=%s", msg->ident); meillo@0: meillo@82: printf("%s <= %s%s%s%s%s\n", msg->uid, addr_string(msg->return_path), size_str ? size_str : "", meillo@82: time_str ? time_str : "", host_str ? host_str : "", ident_str ? ident_str : ""); meillo@0: meillo@82: if (size_str) meillo@82: g_free(size_str); meillo@82: if (time_str) meillo@82: g_free(time_str); meillo@82: if (host_str) meillo@82: g_free(host_str); meillo@82: if (ident_str) meillo@82: g_free(ident_str); meillo@10: meillo@82: foreach(msg->rcpt_list, rcpt_node) { meillo@82: address *rcpt = (address *) (rcpt_node->data); meillo@82: meillo@82: printf(" %s %s\n", addr_is_delivered(rcpt) ? "=>" : (addr_is_failed(rcpt) ? "!=" : "=="), addr_string(rcpt)); meillo@10: } meillo@82: g_free(msg); meillo@82: } meillo@10: } meillo@10: meillo@10: gboolean meillo@10: queue_delete(gchar * uid) meillo@0: { meillo@10: gboolean hdr_ok = TRUE; meillo@10: gboolean dat_ok = TRUE; meillo@10: gchar *hdr_name = g_strdup_printf("%s/input/%s-H", conf.spool_dir, uid); meillo@10: gchar *dat_name = g_strdup_printf("%s/input/%s-D", conf.spool_dir, uid); meillo@10: struct stat stat_buf; meillo@0: meillo@82: if (!spool_lock(uid)) { meillo@10: fprintf(stderr, "message %s is locked.\n", uid); meillo@10: return FALSE; meillo@10: } meillo@0: meillo@82: if (stat(hdr_name, &stat_buf) == 0) { meillo@82: if (unlink(hdr_name) != 0) { meillo@82: fprintf(stderr, "could not unlink %s: %s\n", hdr_name, strerror(errno)); meillo@82: hdr_ok = FALSE; meillo@82: } meillo@82: } else { meillo@82: fprintf(stderr, "could not stat file %s: %s\n", hdr_name, strerror(errno)); meillo@82: hdr_ok = FALSE; meillo@82: } meillo@82: if (stat(dat_name, &stat_buf) == 0) { meillo@82: if (unlink(dat_name) != 0) { meillo@82: fprintf(stderr, "could not unlink %s: %s\n", dat_name, strerror(errno)); meillo@82: dat_ok = FALSE; meillo@82: } meillo@82: } else { meillo@82: fprintf(stderr, "could not stat file %s: %s\n", dat_name, strerror(errno)); meillo@82: dat_ok = FALSE; meillo@82: } meillo@82: printf("message %s deleted\n", uid); meillo@82: meillo@82: spool_unlock(uid); meillo@82: meillo@10: return (dat_ok && hdr_ok); meillo@0: }