adam@0
|
1 #include <stdio.h>
|
adam@0
|
2 #include <string.h>
|
adam@0
|
3 #include <stdlib.h>
|
adam@0
|
4 #include <ctype.h>
|
adam@0
|
5 #include <sys/types.h>
|
adam@0
|
6 #include <sys/socket.h>
|
adam@0
|
7 #include <netinet/in.h>
|
adam@0
|
8 #include <arpa/inet.h>
|
adam@0
|
9
|
adam@0
|
10 #include <urweb.h>
|
adam@0
|
11
|
adam@0
|
12 struct headers {
|
adam@0
|
13 uw_Basis_string from, to, cc, bcc, subject;
|
adam@0
|
14 };
|
adam@0
|
15
|
adam@0
|
16 typedef struct headers *uw_Mail_headers;
|
adam@0
|
17
|
adam@0
|
18 uw_Mail_headers uw_Mail_empty = NULL;
|
adam@0
|
19
|
adam@0
|
20 static void header(uw_context ctx, uw_Basis_string s) {
|
adam@0
|
21 if (strlen(s) > 100)
|
adam@0
|
22 uw_error(ctx, FATAL, "Header value too long");
|
adam@0
|
23
|
adam@0
|
24 for (; *s; ++s)
|
adam@0
|
25 if (*s == '\r' || *s == '\n')
|
adam@0
|
26 uw_error(ctx, FATAL, "Header value contains newline");
|
adam@0
|
27 }
|
adam@0
|
28
|
adam@0
|
29 static void address(uw_context ctx, uw_Basis_string s) {
|
adam@0
|
30 header(ctx, s);
|
adam@0
|
31
|
adam@0
|
32 if (strchr(s, ','))
|
adam@0
|
33 uw_error(ctx, FATAL, "E-mail address contains comma");
|
adam@0
|
34 }
|
adam@0
|
35
|
adam@0
|
36 uw_Mail_headers uw_Mail_from(uw_context ctx, uw_Basis_string s, uw_Mail_headers h) {
|
adam@0
|
37 // char **allowed = uw_get_global(ctx, "mail_from");
|
adam@0
|
38 // Might add this policy checking (or some expanded version of it) back later.
|
adam@0
|
39 uw_Mail_headers h2 = uw_malloc(ctx, sizeof(struct headers));
|
adam@0
|
40
|
adam@0
|
41 if (h)
|
adam@0
|
42 *h2 = *h;
|
adam@0
|
43 else
|
adam@0
|
44 memset(h2, 0, sizeof(*h2));
|
adam@0
|
45
|
adam@0
|
46 if (h2->from)
|
adam@0
|
47 uw_error(ctx, FATAL, "Duplicate From header");
|
adam@0
|
48
|
adam@0
|
49 /*
|
adam@0
|
50 if (!allowed)
|
adam@0
|
51 uw_error(ctx, FATAL, "No From address whitelist has been set. Perhaps you are not authorized to send e-mail.");
|
adam@0
|
52
|
adam@0
|
53 if (!(allowed[0] && !strcmp(allowed[0], "*"))) {
|
adam@0
|
54 for (; *allowed; ++allowed)
|
adam@0
|
55 if (!strcmp(*allowed, s))
|
adam@0
|
56 goto ok;
|
adam@0
|
57
|
adam@0
|
58 uw_error(ctx, FATAL, "From address is not in whitelist");
|
adam@0
|
59 }
|
adam@0
|
60
|
adam@0
|
61 ok:
|
adam@0
|
62 */
|
adam@0
|
63 address(ctx, s);
|
adam@0
|
64 h2->from = s;
|
adam@0
|
65
|
adam@0
|
66 return h2;
|
adam@0
|
67 }
|
adam@0
|
68
|
adam@0
|
69 uw_Mail_headers uw_Mail_to(uw_context ctx, uw_Basis_string s, uw_Mail_headers h) {
|
adam@0
|
70 uw_Mail_headers h2 = uw_malloc(ctx, sizeof(struct headers));
|
adam@0
|
71 if (h)
|
adam@0
|
72 *h2 = *h;
|
adam@0
|
73 else
|
adam@0
|
74 memset(h2, 0, sizeof(*h2));
|
adam@0
|
75
|
adam@0
|
76 address(ctx, s);
|
adam@0
|
77 if (h2->to) {
|
adam@0
|
78 uw_Basis_string all = uw_malloc(ctx, strlen(h2->to) + 1 + strlen(s));
|
adam@0
|
79 sprintf(all, "%s,%s", h2->to, s);
|
adam@0
|
80 h2->to = all;
|
adam@0
|
81 } else
|
adam@0
|
82 h2->to = s;
|
adam@0
|
83
|
adam@0
|
84 return h2;
|
adam@0
|
85 }
|
adam@0
|
86
|
adam@0
|
87 uw_Mail_headers uw_Mail_cc(uw_context ctx, uw_Basis_string s, uw_Mail_headers h) {
|
adam@0
|
88 uw_Mail_headers h2 = uw_malloc(ctx, sizeof(struct headers));
|
adam@0
|
89 if (h)
|
adam@0
|
90 *h2 = *h;
|
adam@0
|
91 else
|
adam@0
|
92 memset(h2, 0, sizeof(*h2));
|
adam@0
|
93
|
adam@0
|
94 address(ctx, s);
|
adam@0
|
95 if (h2->cc) {
|
adam@0
|
96 uw_Basis_string all = uw_malloc(ctx, strlen(h2->cc) + 1 + strlen(s));
|
adam@0
|
97 sprintf(all, "%s,%s", h2->cc, s);
|
adam@0
|
98 h2->cc = all;
|
adam@0
|
99 } else
|
adam@0
|
100 h2->cc = s;
|
adam@0
|
101
|
adam@0
|
102 return h2;
|
adam@0
|
103 }
|
adam@0
|
104
|
adam@0
|
105 uw_Mail_headers uw_Mail_bcc(uw_context ctx, uw_Basis_string s, uw_Mail_headers h) {
|
adam@0
|
106 uw_Mail_headers h2 = uw_malloc(ctx, sizeof(struct headers));
|
adam@0
|
107 if (h)
|
adam@0
|
108 *h2 = *h;
|
adam@0
|
109 else
|
adam@0
|
110 memset(h2, 0, sizeof(*h2));
|
adam@0
|
111
|
adam@0
|
112 address(ctx, s);
|
adam@0
|
113 if (h2->bcc) {
|
adam@0
|
114 uw_Basis_string all = uw_malloc(ctx, strlen(h2->bcc) + 1 + strlen(s));
|
adam@0
|
115 sprintf(all, "%s,%s", h2->bcc, s);
|
adam@0
|
116 h2->bcc = all;
|
adam@0
|
117 } else
|
adam@0
|
118 h2->bcc = s;
|
adam@0
|
119
|
adam@0
|
120 return h2;
|
adam@0
|
121 }
|
adam@0
|
122
|
adam@0
|
123 uw_Mail_headers uw_Mail_subject(uw_context ctx, uw_Basis_string s, uw_Mail_headers h) {
|
adam@0
|
124 uw_Mail_headers h2 = uw_malloc(ctx, sizeof(struct headers));
|
adam@0
|
125
|
adam@0
|
126 if (h)
|
adam@0
|
127 *h2 = *h;
|
adam@0
|
128 else
|
adam@0
|
129 memset(h2, 0, sizeof(*h2));
|
adam@0
|
130
|
adam@0
|
131 if (h2->subject)
|
adam@0
|
132 uw_error(ctx, FATAL, "Duplicate Subject header");
|
adam@0
|
133
|
adam@0
|
134 header(ctx, s);
|
adam@0
|
135 h2->subject = s;
|
adam@0
|
136
|
adam@0
|
137 return h2;
|
adam@0
|
138 }
|
adam@0
|
139
|
adam@0
|
140 typedef struct {
|
adam@0
|
141 uw_context ctx;
|
adam@0
|
142 uw_Mail_headers h;
|
adam@0
|
143 uw_Basis_string body, xbody;
|
adam@0
|
144 } job;
|
adam@0
|
145
|
adam@0
|
146 #define BUFLEN 1024
|
adam@0
|
147
|
adam@0
|
148 static int smtp_read(uw_context ctx, int sock, char *buf, ssize_t *pos) {
|
adam@0
|
149 char *s;
|
adam@0
|
150
|
adam@0
|
151 while (1) {
|
adam@0
|
152 ssize_t recvd;
|
adam@0
|
153
|
adam@0
|
154 buf[*pos] = 0;
|
adam@0
|
155
|
adam@0
|
156 if ((s = strchr(buf, '\n'))) {
|
adam@0
|
157 int n;
|
adam@0
|
158
|
adam@0
|
159 *s = 0;
|
adam@0
|
160
|
adam@0
|
161 if (sscanf(buf, "%d ", &n) != 1) {
|
adam@0
|
162 close(sock);
|
adam@0
|
163 uw_set_error_message(ctx, "Mail server response does not begin with a code.");
|
adam@0
|
164 return 0;
|
adam@0
|
165 }
|
adam@0
|
166
|
adam@0
|
167 *pos -= s - buf + 1;
|
adam@0
|
168 memmove(buf, s+1, *pos);
|
adam@0
|
169
|
adam@0
|
170 return n;
|
adam@0
|
171 }
|
adam@0
|
172
|
adam@0
|
173 recvd = recv(sock, buf + *pos, BUFLEN - *pos - 1, 0);
|
adam@0
|
174
|
adam@0
|
175 if (recvd == 0) {
|
adam@0
|
176 close(sock);
|
adam@0
|
177 uw_set_error_message(ctx, "Mail server response ends unexpectedly.");
|
adam@0
|
178 return 0;
|
adam@0
|
179 } else if (recvd < 0) {
|
adam@0
|
180 close(sock);
|
adam@0
|
181 uw_set_error_message(ctx, "Error reading mail server response.");
|
adam@0
|
182 return 0;
|
adam@0
|
183 }
|
adam@0
|
184
|
adam@0
|
185 *pos += recvd;
|
adam@0
|
186 }
|
adam@0
|
187 }
|
adam@0
|
188
|
adam@0
|
189 static int really_string(int sock, const char *s) {
|
adam@0
|
190 return uw_really_send(sock, s, strlen(s));
|
adam@0
|
191 }
|
adam@0
|
192
|
adam@0
|
193 static int sendAddrs(const char *kind, uw_context ctx, int sock, char *s, char *buf, ssize_t *pos) {
|
adam@0
|
194 char *p;
|
adam@0
|
195 char out[BUFLEN];
|
adam@0
|
196
|
adam@0
|
197 if (!s)
|
adam@0
|
198 return 0;
|
adam@0
|
199
|
adam@0
|
200 for (p = strchr(s, ','); p; p = strchr(p+1, ',')) {
|
adam@0
|
201 *p = 0;
|
adam@0
|
202
|
adam@0
|
203 snprintf(out, sizeof(out), "RCPT TO:%s\n", s);
|
adam@0
|
204 out[sizeof(out)-1] = 0;
|
adam@0
|
205 *p = ',';
|
adam@0
|
206
|
adam@0
|
207 if (really_string(sock, out) < 0) {
|
adam@0
|
208 close(sock);
|
adam@0
|
209 uw_set_error_message(ctx, "Error sending RCPT TO for %s", kind);
|
adam@0
|
210 return 1;
|
adam@0
|
211 }
|
adam@0
|
212
|
adam@0
|
213 if (smtp_read(ctx, sock, buf, pos) != 250) {
|
adam@0
|
214 close(sock);
|
adam@0
|
215 uw_set_error_message(ctx, "Mail server doesn't respond to %s RCPT TO with code 250.", kind);
|
adam@0
|
216 return 1;
|
adam@0
|
217 }
|
adam@0
|
218 }
|
adam@0
|
219
|
adam@0
|
220 if (*s) {
|
adam@0
|
221 snprintf(out, sizeof(out), "RCPT TO:%s\n", s);
|
adam@0
|
222 out[sizeof(out)-1] = 0;
|
adam@0
|
223
|
adam@0
|
224 if (really_string(sock, out) < 0) {
|
adam@0
|
225 close(sock);
|
adam@0
|
226 uw_set_error_message(ctx, "Error sending RCPT TO for %s", kind);
|
adam@0
|
227 return 1;
|
adam@0
|
228 }
|
adam@0
|
229
|
adam@0
|
230 if (smtp_read(ctx, sock, buf, pos) != 250) {
|
adam@0
|
231 close(sock);
|
adam@0
|
232 uw_set_error_message(ctx, "Mail server doesn't respond to %s RCPT TO with code 250.", kind);
|
adam@0
|
233 return 1;
|
adam@0
|
234 }
|
adam@0
|
235 }
|
adam@0
|
236
|
adam@0
|
237 return 0;
|
adam@0
|
238 }
|
adam@0
|
239
|
adam@0
|
240 static void commit(void *data) {
|
adam@0
|
241 job *j = data;
|
adam@0
|
242 int sock;
|
adam@0
|
243 struct sockaddr_in my_addr;
|
adam@0
|
244 char buf[BUFLEN], out[BUFLEN];
|
adam@0
|
245 ssize_t pos = 0;
|
adam@0
|
246 char *s;
|
adam@0
|
247
|
adam@0
|
248 if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
|
adam@0
|
249 uw_set_error_message(j->ctx, "Can't create socket for mail server connection");
|
adam@0
|
250 return;
|
adam@0
|
251 }
|
adam@0
|
252
|
adam@0
|
253 my_addr.sin_family = AF_INET;
|
adam@0
|
254 my_addr.sin_port = htons(25);
|
adam@0
|
255 my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
|
adam@0
|
256 memset(my_addr.sin_zero, 0, sizeof my_addr.sin_zero);
|
adam@0
|
257
|
adam@0
|
258 if (connect(sock, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
|
adam@0
|
259 close(sock);
|
adam@0
|
260 uw_set_error_message(j->ctx, "Error connecting to mail server");
|
adam@0
|
261 return;
|
adam@0
|
262 }
|
adam@0
|
263
|
adam@0
|
264 if (smtp_read(j->ctx, sock, buf, &pos) != 220) {
|
adam@0
|
265 close(sock);
|
adam@0
|
266 uw_set_error_message(j->ctx, "Mail server doesn't greet with code 220.");
|
adam@0
|
267 return;
|
adam@0
|
268 }
|
adam@0
|
269
|
adam@0
|
270 if (really_string(sock, "HELO localhost\n") < 0) {
|
adam@0
|
271 close(sock);
|
adam@0
|
272 uw_set_error_message(j->ctx, "Error sending HELO");
|
adam@0
|
273 return;
|
adam@0
|
274 }
|
adam@0
|
275
|
adam@0
|
276 if (smtp_read(j->ctx, sock, buf, &pos) != 250) {
|
adam@0
|
277 close(sock);
|
adam@0
|
278 uw_set_error_message(j->ctx, "Mail server doesn't respond to HELO with code 250.");
|
adam@0
|
279 return;
|
adam@0
|
280 }
|
adam@0
|
281
|
adam@0
|
282 snprintf(out, sizeof(out), "MAIL FROM:%s\n", j->h->from);
|
adam@0
|
283 out[sizeof(out)-1] = 0;
|
adam@0
|
284
|
adam@0
|
285 if (really_string(sock, out) < 0) {
|
adam@0
|
286 close(sock);
|
adam@0
|
287 uw_set_error_message(j->ctx, "Error sending MAIL FROM");
|
adam@0
|
288 return;
|
adam@0
|
289 }
|
adam@0
|
290
|
adam@0
|
291 if (smtp_read(j->ctx, sock, buf, &pos) != 250) {
|
adam@0
|
292 close(sock);
|
adam@0
|
293 uw_set_error_message(j->ctx, "Mail server doesn't respond to MAIL FROM with code 250.");
|
adam@0
|
294 return;
|
adam@0
|
295 }
|
adam@0
|
296
|
adam@0
|
297 if (sendAddrs("To", j->ctx, sock, j->h->to, buf, &pos)) return;
|
adam@0
|
298 if (sendAddrs("Cc", j->ctx, sock, j->h->cc, buf, &pos)) return;
|
adam@0
|
299 if (sendAddrs("Bcc", j->ctx, sock, j->h->bcc, buf, &pos)) return;
|
adam@0
|
300
|
adam@0
|
301 if (really_string(sock, "DATA\n") < 0) {
|
adam@0
|
302 close(sock);
|
adam@0
|
303 uw_set_error_message(j->ctx, "Error sending DATA");
|
adam@0
|
304 return;
|
adam@0
|
305 }
|
adam@0
|
306
|
adam@0
|
307 if (smtp_read(j->ctx, sock, buf, &pos) != 354) {
|
adam@0
|
308 close(sock);
|
adam@0
|
309 uw_set_error_message(j->ctx, "Mail server doesn't respond to DATA with code 354.");
|
adam@0
|
310 return;
|
adam@0
|
311 }
|
adam@0
|
312
|
adam@0
|
313 snprintf(out, sizeof(out), "From: %s\r\n", j->h->from);
|
adam@0
|
314 out[sizeof(out)-1] = 0;
|
adam@0
|
315
|
adam@0
|
316 if (really_string(sock, out) < 0) {
|
adam@0
|
317 close(sock);
|
adam@0
|
318 uw_set_error_message(j->ctx, "Error sending From");
|
adam@0
|
319 return;
|
adam@0
|
320 }
|
adam@0
|
321
|
adam@0
|
322 if (j->h->subject) {
|
adam@0
|
323 snprintf(out, sizeof(out), "Subject: %s\r\n", j->h->subject);
|
adam@0
|
324 out[sizeof(out)-1] = 0;
|
adam@0
|
325
|
adam@0
|
326 if (really_string(sock, out) < 0) {
|
adam@0
|
327 close(sock);
|
adam@0
|
328 uw_set_error_message(j->ctx, "Error sending Subject");
|
adam@0
|
329 return;
|
adam@0
|
330 }
|
adam@0
|
331 }
|
adam@0
|
332
|
adam@0
|
333 if (j->h->to) {
|
adam@0
|
334 snprintf(out, sizeof(out), "To: %s\r\n", j->h->to);
|
adam@0
|
335 out[sizeof(out)-1] = 0;
|
adam@0
|
336
|
adam@0
|
337 if (really_string(sock, out) < 0) {
|
adam@0
|
338 close(sock);
|
adam@0
|
339 uw_set_error_message(j->ctx, "Error sending To");
|
adam@0
|
340 return;
|
adam@0
|
341 }
|
adam@0
|
342 }
|
adam@0
|
343
|
adam@0
|
344 if (j->h->cc) {
|
adam@0
|
345 snprintf(out, sizeof(out), "Cc: %s\r\n", j->h->cc);
|
adam@0
|
346 out[sizeof(out)-1] = 0;
|
adam@0
|
347
|
adam@0
|
348 if (really_string(sock, out) < 0) {
|
adam@0
|
349 close(sock);
|
adam@0
|
350 uw_set_error_message(j->ctx, "Error sending Cc");
|
adam@0
|
351 return;
|
adam@0
|
352 }
|
adam@0
|
353 }
|
adam@0
|
354
|
adam@0
|
355 if (j->h->bcc) {
|
adam@0
|
356 snprintf(out, sizeof(out), "Bcc: %s\r\n", j->h->bcc);
|
adam@0
|
357 out[sizeof(out)-1] = 0;
|
adam@0
|
358
|
adam@0
|
359 if (really_string(sock, out) < 0) {
|
adam@0
|
360 close(sock);
|
adam@0
|
361 uw_set_error_message(j->ctx, "Error sending Bcc");
|
adam@0
|
362 return;
|
adam@0
|
363 }
|
adam@0
|
364 }
|
adam@0
|
365
|
adam@0
|
366 if ((s = uw_get_global(j->ctx, "extra_mail_headers"))) {
|
adam@0
|
367 if (really_string(sock, s) < 0) {
|
adam@0
|
368 close(sock);
|
adam@0
|
369 uw_set_error_message(j->ctx, "Error sending extra headers");
|
adam@0
|
370 return;
|
adam@0
|
371 }
|
adam@0
|
372 }
|
adam@0
|
373
|
adam@0
|
374 if (j->xbody) {
|
adam@0
|
375 char separator[11];
|
adam@0
|
376 separator[sizeof(separator)-1] = 0;
|
adam@0
|
377
|
adam@0
|
378 do {
|
adam@0
|
379 int i;
|
adam@0
|
380
|
adam@0
|
381 for (i = 0; i < sizeof(separator)-1; ++i)
|
adam@0
|
382 separator[i] = 'A' + (rand() % 26);
|
adam@0
|
383 } while (strstr(j->body, separator) || strstr(j->xbody, separator));
|
adam@0
|
384
|
adam@0
|
385 snprintf(out, sizeof(out), "MIME-Version: 1.0\r\n"
|
adam@0
|
386 "Content-Type: multipart/alternative; boundary=\"%s\"\r\n"
|
adam@0
|
387 "\r\n"
|
adam@0
|
388 "--%s\r\n"
|
adam@0
|
389 "Content-Type: text/plain\r\n"
|
adam@0
|
390 "\r\n",
|
adam@0
|
391 separator, separator);
|
adam@0
|
392 out[sizeof(out)-1] = 0;
|
adam@0
|
393
|
adam@0
|
394 if (really_string(sock, out) < 0) {
|
adam@0
|
395 close(sock);
|
adam@0
|
396 uw_set_error_message(j->ctx, "Error sending multipart beginning");
|
adam@0
|
397 return;
|
adam@0
|
398 }
|
adam@0
|
399
|
adam@0
|
400 if (really_string(sock, j->body) < 0) {
|
adam@0
|
401 close(sock);
|
adam@0
|
402 uw_set_error_message(j->ctx, "Error sending message text body");
|
adam@0
|
403 return;
|
adam@0
|
404 }
|
adam@0
|
405
|
adam@0
|
406 snprintf(out, sizeof(out), "\r\n"
|
adam@0
|
407 "--%s\r\n"
|
adam@0
|
408 "Content-Type: text/html\r\n"
|
adam@0
|
409 "\r\n",
|
adam@0
|
410 separator);
|
adam@0
|
411 out[sizeof(out)-1] = 0;
|
adam@0
|
412
|
adam@0
|
413 if (really_string(sock, out) < 0) {
|
adam@0
|
414 close(sock);
|
adam@0
|
415 uw_set_error_message(j->ctx, "Error sending multipart middle");
|
adam@0
|
416 return;
|
adam@0
|
417 }
|
adam@0
|
418
|
adam@0
|
419 if (really_string(sock, j->xbody) < 0) {
|
adam@0
|
420 close(sock);
|
adam@0
|
421 uw_set_error_message(j->ctx, "Error sending message HTML body");
|
adam@0
|
422 return;
|
adam@0
|
423 }
|
adam@0
|
424
|
adam@0
|
425 snprintf(out, sizeof(out), "\r\n"
|
adam@0
|
426 "--%s--",
|
adam@0
|
427 separator);
|
adam@0
|
428 out[sizeof(out)-1] = 0;
|
adam@0
|
429
|
adam@0
|
430 if (really_string(sock, out) < 0) {
|
adam@0
|
431 close(sock);
|
adam@0
|
432 uw_set_error_message(j->ctx, "Error sending multipart end");
|
adam@0
|
433 return;
|
adam@0
|
434 }
|
adam@0
|
435 } else {
|
adam@0
|
436 if (really_string(sock, "Content-Type: text/plain\r\n\r\n") < 0) {
|
adam@0
|
437 close(sock);
|
adam@0
|
438 uw_set_error_message(j->ctx, "Error sending text Content-Type");
|
adam@0
|
439 return;
|
adam@0
|
440 }
|
adam@0
|
441
|
adam@0
|
442 if (really_string(sock, j->body) < 0) {
|
adam@0
|
443 close(sock);
|
adam@0
|
444 uw_set_error_message(j->ctx, "Error sending message body");
|
adam@0
|
445 return;
|
adam@0
|
446 }
|
adam@0
|
447 }
|
adam@0
|
448
|
adam@0
|
449 if (really_string(sock, "\r\n.\r\n") < 0) {
|
adam@0
|
450 close(sock);
|
adam@0
|
451 uw_set_error_message(j->ctx, "Error sending message terminator");
|
adam@0
|
452 return;
|
adam@0
|
453 }
|
adam@0
|
454
|
adam@0
|
455 if (smtp_read(j->ctx, sock, buf, &pos) != 250) {
|
adam@0
|
456 close(sock);
|
adam@0
|
457 uw_set_error_message(j->ctx, "Mail server doesn't respond to end of message with code 250.");
|
adam@0
|
458 return;
|
adam@0
|
459 }
|
adam@0
|
460
|
adam@0
|
461 if (really_string(sock, "QUIT\n") < 0) {
|
adam@0
|
462 close(sock);
|
adam@0
|
463 uw_set_error_message(j->ctx, "Error sending QUIT");
|
adam@0
|
464 return;
|
adam@0
|
465 }
|
adam@0
|
466
|
adam@0
|
467 if (smtp_read(j->ctx, sock, buf, &pos) != 221) {
|
adam@0
|
468 close(sock);
|
adam@0
|
469 uw_set_error_message(j->ctx, "Mail server doesn't respond to QUIT with code 221.");
|
adam@0
|
470 return;
|
adam@0
|
471 }
|
adam@0
|
472
|
adam@0
|
473 close(sock);
|
adam@0
|
474 }
|
adam@0
|
475
|
adam@0
|
476 uw_unit uw_Mail_send(uw_context ctx, uw_Mail_headers h, uw_Basis_string body, uw_Basis_string xbody) {
|
adam@0
|
477 job *j;
|
adam@0
|
478 char *s;
|
adam@0
|
479
|
adam@0
|
480 if (!h || !h->from)
|
adam@0
|
481 uw_error(ctx, FATAL, "No From address set for e-mail message");
|
adam@0
|
482
|
adam@0
|
483 if (!h->to && !h->cc && !h->bcc)
|
adam@0
|
484 uw_error(ctx, FATAL, "No recipients specified for e-mail message");
|
adam@0
|
485
|
adam@0
|
486 for (s = strchr(body, '.'); s; s = strchr(s+1, '.'))
|
adam@0
|
487 if ((s[1] == '\n' || s[1] == '\r')
|
adam@0
|
488 && (s <= body || s[-1] == '\n' || s[-1] == '\r'))
|
adam@0
|
489 uw_error(ctx, FATAL, "Message body contains a line with just a period");
|
adam@0
|
490
|
adam@0
|
491 if (xbody) {
|
adam@0
|
492 for (s = strchr(xbody, '.'); s; s = strchr(s+1, '.'))
|
adam@0
|
493 if ((s[1] == '\n' || s[1] == '\r')
|
adam@0
|
494 && (s <= xbody || s[-1] == '\n' || s[-1] == '\r'))
|
adam@0
|
495 uw_error(ctx, FATAL, "HTML message body contains a line with just a period");
|
adam@0
|
496 }
|
adam@0
|
497
|
adam@0
|
498 j = uw_malloc(ctx, sizeof(job));
|
adam@0
|
499
|
adam@0
|
500 j->ctx = ctx;
|
adam@0
|
501 j->h = h;
|
adam@0
|
502 j->body = body;
|
adam@0
|
503 j->xbody = xbody;
|
adam@0
|
504
|
adam@0
|
505 uw_register_transactional(ctx, j, commit, NULL, NULL);
|
adam@0
|
506
|
adam@0
|
507 return uw_unit_v;
|
adam@0
|
508 }
|