]> jfr.im git - irc/evilnet/x3.git/blame - src/mail-sendmail.c
make regex case sensitive, and old memoserv fix
[irc/evilnet/x3.git] / src / mail-sendmail.c
CommitLineData
ff3b058a 1/* mail-common.c - mail sending utilities
2 * Copyright 2002-2004, 2007 srvx Development Team
d76ed9a9 3 *
ff3b058a 4 * This file is part of srvx.
d76ed9a9 5 *
ff3b058a 6 * srvx is free software; you can redistribute it and/or modify
d76ed9a9 7 * it under the terms of the GNU General Public License as published by
ff3b058a 8 * the Free Software Foundation; either version 2 of the License, or
d76ed9a9
AS
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
ff3b058a 21#include "mail-common.c"
d76ed9a9
AS
22
23#ifdef HAVE_SYS_WAIT_H
24#include <sys/wait.h>
25#endif
26
d76ed9a9
AS
27/* This function sends the given "paragraph" as flowed text, as
28 * defined in RFC 2646. It lets us only worry about line wrapping
29 * here, and not in the code that generates mail.
30 */
31static void
32send_flowed_text(FILE *where, const char *para)
33{
34 const char *eol = strchr(para, '\n');
35 unsigned int shift;
36
37 while (*para) {
38 /* Do we need to space-stuff the line? */
39 if ((*para == ' ') || (*para == '>') || !strncmp(para, "From ", 5)) {
40 fputc(' ', where);
41 shift = 1;
42 } else {
43 shift = 0;
44 }
45 /* How much can we put on this line? */
46 if (!eol && (strlen(para) < (80 - shift))) {
47 /* End of paragraph; can put on one line. */
48 fputs(para, where);
49 fputs("\n", where);
50 break;
51 } else if (eol && (eol < para + (80 - shift))) {
52 /* Newline inside paragraph, no need to wrap. */
2f61d1d7 53 fprintf(where, "%.*s\n", (int)(eol - para), para);
d76ed9a9
AS
54 para = eol + 1;
55 } else {
56 int pos;
57 /* Need to wrap. Where's the last space in the line? */
58 for (pos=72-shift; pos && (para[pos] != ' '); pos--) ;
59 /* If we didn't find a space, look ahead instead. */
60 if (pos == 0) pos = strcspn(para, " \n");
61 fprintf(where, "%.*s\n", pos+1, para);
62 para += pos + 1;
63 }
64 if (eol && (eol < para)) eol = strchr(para, '\n');
65 }
66}
67
68void
ff3b058a 69mail_send(struct userNode *from, struct handle_info *to, const char *subject, const char *body, int first_time)
d76ed9a9
AS
70{
71 pid_t child;
72 int infds[2], outfds[2];
73 const char *fromaddr, *str;
74
75 /* Grab some config items first. */
76 str = conf_get_data("mail/enable", RECDB_QSTRING);
77 if (!str || !enabled_string(str))
78 return;
79 fromaddr = conf_get_data("mail/from_address", RECDB_QSTRING);
80
81 /* How this works: We fork, and the child tries to send the mail.
82 * It does this by setting up a pipe pair, and forking again (the
83 * grandchild exec()'s the mailer program). The mid-level child
84 * sends the text to the grandchild's stdin, and then logs the
85 * success or failure.
86 */
87
88 child = fork();
89 if (child < 0) {
90 log_module(MAIN_LOG, LOG_ERROR, "sendmail() to %s couldn't fork(): %s (%d)", to->email_addr, strerror(errno), errno);
91 return;
92 } else if (child > 0) {
93 return;
94 }
95 /* We're in a child now; must _exit() to die properly. */
96 if (pipe(infds) < 0) {
97 log_module(MAIN_LOG, LOG_ERROR, "sendmail() child to %s couldn't pipe(infds): %s (%d)", to->email_addr, strerror(errno), errno);
98 _exit(1);
99 }
100 if (pipe(outfds) < 0) {
101 log_module(MAIN_LOG, LOG_ERROR, "sendmail() child to %s couldn't pipe(outfds): %s (%d)", to->email_addr, strerror(errno), errno);
102 _exit(1);
103 }
104 child = fork();
105 if (child < 0) {
106 log_module(MAIN_LOG, LOG_ERROR, "sendmail() child to %s couldn't fork(): %s (%d)", to->email_addr, strerror(errno), errno);
107 _exit(1);
108 } else if (child > 0) {
109 /* Mid-level child; get ready to send the mail. */
110 FILE *out = fdopen(infds[1], "w");
111 struct string_list *extras;
112 unsigned int nn;
113 int res, rv;
114
115 /* Close the end of pipes we do not use. */
116 close(infds[0]);
117 close(outfds[1]);
118
119 /* Do we have any "extra" headers to send? */
120 extras = conf_get_data("mail/extra_headers", RECDB_STRING_LIST);
121 if (extras) {
122 for (nn=0; nn<extras->used; nn++) {
123 fputs(extras->list[nn], out);
124 fputs("\n", out);
125 }
126 }
127
128 /* Content type? (format=flowed is a standard for plain text
129 * that lets the receiver reconstruct paragraphs, defined in
130 * RFC 2646. See comment above send_flowed_text() for more.)
131 */
132 if (!(str = conf_get_data("mail/charset", RECDB_QSTRING))) str = "us-ascii";
133 fprintf(out, "Content-Type: text/plain; charset=%s; format=flowed\n", str);
134
135 /* Send From, To and Subject headers */
136 if (!fromaddr) fromaddr = "admin@poorly.configured.network";
137 fprintf(out, "From: %s <%s>\n", from->nick, fromaddr);
138 fprintf(out, "To: \"%s\" <%s>\n", to->handle, to->email_addr);
139 fprintf(out, "Subject: %s\n", subject);
140
141 /* Send mail body */
142 fputs("\n", out); /* terminate headers */
143 extras = conf_get_data((first_time?"mail/body_prefix_first":"mail/body_prefix"), RECDB_STRING_LIST);
144 if (extras) {
145 for (nn=0; nn<extras->used; nn++) {
146 send_flowed_text(out, extras->list[nn]);
147 }
148 fputs("\n", out);
149 }
150 send_flowed_text(out, body);
151 extras = conf_get_data((first_time?"mail/body_suffix_first":"mail/body_suffix"), RECDB_STRING_LIST);
152 if (extras) {
153 fputs("\n", out);
154 for (nn=0; nn<extras->used; nn++)
155 send_flowed_text(out, extras->list[nn]);
156 }
157
158 /* Close file (sending mail) and check for return code */
159 fflush(out);
160 fclose(out);
161 do {
162 rv = wait4(child, &res, 0, NULL);
163 } while ((rv == -1) && (errno == EINTR));
164 if (rv == child) {
165 /* accept the wait() result */
166 } else {
167 log_module(MAIN_LOG, LOG_ERROR, "sendmail() child to %s: Bad wait() return code %d: %s (%d)", to->email_addr, rv, strerror(errno), errno);
168 _exit(1);
169 }
170 if (res) {
171 log_module(MAIN_LOG, LOG_ERROR, "sendmail() grandchild to %s: Exited with code %d", to->email_addr, res);
172 _exit(1);
173 } else {
174 log_module(MAIN_LOG, LOG_INFO, "sendmail() sent email to %s <%s>: %s", to->handle, to->email_addr, subject);
175 }
176 _exit(0);
177 } else {
178 /* Grandchild; dup2 the fds and exec the mailer. */
179 const char *argv[10], *mpath;
180 unsigned int argc = 0;
181
182 /* Close the end of pipes we do not use. */
183 close(infds[1]);
184 close(outfds[0]);
185
186 dup2(infds[0], STDIN_FILENO);
187 dup2(outfds[1], STDOUT_FILENO);
188 mpath = conf_get_data("mail/mailer", RECDB_QSTRING);
189 if (!mpath) mpath = "/usr/sbin/sendmail";
190 argv[argc++] = mpath;
191 if (fromaddr) {
192 argv[argc++] = "-f";
193 argv[argc++] = fromaddr;
194 }
195 argv[argc++] = to->email_addr;
196 argv[argc++] = NULL;
197 if (execv(mpath, (char**)argv) < 0) {
198 log_module(MAIN_LOG, LOG_ERROR, "sendmail() grandchild to %s couldn't execv(): %s (%d)", to->email_addr, strerror(errno), errno);
199 }
200 _exit(1);
201 }
202}
203
d76ed9a9 204void
ff3b058a 205mail_init(void)
d76ed9a9 206{
ff3b058a 207 mail_common_init();
d76ed9a9 208}