masqmail-0.2

view src/fail_msg.c @ 10:26e34ae9a3e3

changed indention and line wrapping to a more consistent style
author meillo@marmaro.de
date Mon, 27 Oct 2008 16:23:10 +0100
parents 08114f7dcc23
children f671821d8222
line source
1 /* MasqMail
2 Copyright (C) 2000-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., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
19 #include <sys/wait.h>
21 #include "masqmail.h"
22 #include "peopen.h"
23 #include "readsock.h"
25 gboolean
26 fail_msg(message * msg, gchar * template, GList * failed_rcpts, gchar * err_fmt, va_list args)
27 {
28 gboolean ok = FALSE;
29 address *ret_path = NULL;
31 /* do not bounce bounces, send to postmaster instead */
32 if (msg->return_path->local_part[0] == 0) {
33 GList *node;
35 ret_path = create_address_qualified("postmaster", TRUE, conf.host_name);
36 foreach(failed_rcpts, node) {
37 address *addr = (address *) (node->data);
38 if (addr_isequal_parent(addr, ret_path)) {
39 logwrite(LOG_ALERT, "%s == %s: postmaster address failed\n", msg->uid, addr_string(ret_path));
40 return FALSE;
41 }
42 }
43 } else
44 ret_path = copy_address(msg->return_path);
46 DEBUG(1) debugf("sending failure notice to %s.\n", addr_string(ret_path));
48 if (template) {
49 FILE *file;
50 GList *var_table = var_table_conf(var_table_msg(NULL, msg));
51 gchar *err_msg = g_strdup_vprintf(err_fmt, args);
53 var_table = g_list_prepend(var_table, create_pair_string("err_msg", err_msg));
54 g_free(err_msg);
56 if ((file = fopen(template, "r"))) {
57 FILE *out;
58 gchar *cmd;
59 pid_t pid;
61 // cmd = g_strdup_printf(SBINDIR"/masqmail -oi -f \"<>\" %s@%s",
62 // ret_path->local_part, ret_path->domain);
63 cmd = g_strdup_printf(SBINDIR "/masqmail -oi -f <> %s@%s", ret_path->local_part, ret_path->domain);
64 if ((out = peidopen(cmd, "w", environ, &pid, conf.mail_uid, conf.mail_gid))) {
65 gchar fmt[256], line[256];
66 int status, ret;
68 while ((ret = read_sockline(file, fmt, 256, 0, 0)) > 0) {
69 if (fmt[0] == '@') {
70 GList *node;
71 if (strncmp(fmt, "@failed_rcpts", 13) == 0) {
72 foreach(failed_rcpts, node) {
73 address *rcpt = (address *) (node->data);
74 fprintf(out, "\t%s\n", addr_string(rcpt));
75 }
76 } else if (strncmp(fmt, "@msg_headers", 12) == 0) {
77 foreach(msg->hdr_list, node) {
78 header *hdr = (header *) (node->data);
79 fputs(hdr->header, out);
80 }
81 } else if (strncmp(fmt, "@msg_body", 9) == 0) {
82 /* we may have to read the data at this point
83 and remember if we did */
84 gboolean flag = (msg->data_list == NULL);
85 if (flag) {
86 if (!spool_read_data(msg)) {
87 logwrite(LOG_ALERT, "could not open data spool file %s\n", msg->uid);
88 }
89 }
90 foreach(msg->data_list, node) {
91 gchar *line = (gchar *) (node->data);
92 fputs(line, out);
93 }
94 if (flag)
95 msg_free_data(msg);
96 }
97 } else {
98 expand(var_table, fmt, line, 256);
99 fputs(line, out);
100 }
101 }
103 fclose(out);
104 waitpid(pid, &status, 0);
105 if ((WEXITSTATUS(status) != EXIT_SUCCESS) || WIFSIGNALED(status)) {
106 if (WEXITSTATUS(status) != EXIT_SUCCESS)
107 logwrite(LOG_WARNING, "child returned %d\n", WEXITSTATUS(status));
108 if (WIFSIGNALED(status))
109 logwrite(LOG_WARNING, "child got signal: %d\n", WTERMSIG(status));
110 } else
111 ok = TRUE;
112 } else {
113 logwrite(LOG_ERR, "peopen failed: %s\n", strerror(errno));
114 }
115 g_free(cmd);
116 fclose(file);
117 } else
118 logwrite(LOG_ALERT, "could not open failure message template %s: %s\n", conf.errmsg_file, strerror(errno));
120 destroy_table(var_table);
121 }
123 destroy_address(ret_path);
125 return ok;
126 }
128 /*
129 ival : |--|--|----|--------|--------|
130 warned: |-------W-------------W------
131 result: |nnnyyyynnnnyyyyyyyyyynnnnnnn
132 */
134 static gboolean
135 warn_msg_is_due(message * msg)
136 {
137 time_t now = time(NULL);
138 gint dummy;
140 GList *node;
141 for (node = g_list_last(conf.warn_intervals); node; node = g_list_previous(node)) {
142 gchar *str_ival = (gchar *) (node->data);
143 gint ival = time_interval(str_ival, &dummy);
144 if (ival >= 0) {
145 DEBUG(5) debugf("ival = %d\n", ival);
146 DEBUG(5) debugf("now - msg->received_time = %d\n", now - msg->received_time);
147 if ((now - msg->received_time) > ival) {
148 if (msg->warned_time != 0) {
149 if ((msg->warned_time - msg->received_time) < ival)
150 return TRUE;
151 } else
152 return TRUE;
153 }
154 } else
155 logwrite(LOG_WARNING, "invalid time interval: %s\n", str_ival);
156 }
157 return FALSE;
158 }
160 gboolean
161 warn_msg(message * msg, gchar * template, GList * defered_rcpts, gchar * err_fmt, va_list args)
162 {
163 time_t now = time(NULL);
165 if (warn_msg_is_due(msg)) {
166 if (fail_msg(msg, template, defered_rcpts, err_fmt, args)) {
167 msg->warned_time = now;
168 return TRUE;
169 } else
170 return FALSE;
171 }
172 return TRUE;
173 }