-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathemail.c
463 lines (384 loc) · 11.5 KB
/
email.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <urweb/urweb.h>
struct headers {
uw_Basis_string from, to, cc, bcc, subject, user_agent;
};
typedef struct headers *uw_Email_headers;
static uw_Basis_string copy_string(uw_Basis_string s) {
if (s == NULL)
return NULL;
else
return strdup(s);
}
// Some values are expected to be long enough that they might exceed SMTP's limit on line length.
// Let's at least make sure that the line breaks are clear to SMTP,
// by changing bare '\n' into "\r\n".
static uw_Basis_string copy_long_string(uw_context ctx, uw_Basis_string s) {
uw_Basis_string copy, in, out;
int last_was_cr = 0;
if (s == NULL)
return NULL;
copy = uw_malloc(ctx, 2 * strlen(s) + 1);
out = copy;
for (in = s; *in; ++in) {
if (*in == '\n' && !last_was_cr) {
*out++ = '\r';
*out++ = '\n';
} else
*out++ = *in;
last_was_cr = (*in == '\r');
}
*out = 0;
return strdup(copy);
}
static void free_string(uw_Basis_string s) {
if (s == NULL)
return;
else
free(s);
}
static uw_Email_headers copy_headers(uw_Email_headers h) {
uw_Email_headers h2 = malloc(sizeof(struct headers));
h2->from = copy_string(h->from);
h2->to = copy_string(h->to);
h2->cc = copy_string(h->cc);
h2->bcc = copy_string(h->bcc);
h2->subject = copy_string(h->subject);
h2->user_agent = copy_string(h->user_agent);
return h2;
}
static void free_headers(uw_Email_headers h) {
free_string(h->from);
free_string(h->to);
free_string(h->cc);
free_string(h->bcc);
free_string(h->subject);
free_string(h->user_agent);
free(h);
}
uw_Email_headers uw_Email_empty = NULL;
static void header(uw_context ctx, uw_Basis_string s) {
if (strlen(s) > 100)
uw_error(ctx, FATAL, "Header value too long");
for (; *s; ++s)
if (*s == '\r' || *s == '\n')
uw_error(ctx, FATAL, "Header value contains newline");
}
static void address(uw_context ctx, uw_Basis_string s) {
header(ctx, s);
if (strchr(s, ','))
uw_error(ctx, FATAL, "E-mail address contains comma");
}
uw_Email_headers uw_Email_from(uw_context ctx, uw_Basis_string s, uw_Email_headers h) {
uw_Email_headers h2 = uw_malloc(ctx, sizeof(struct headers));
if (h)
*h2 = *h;
else
memset(h2, 0, sizeof(*h2));
if (h2->from)
uw_error(ctx, FATAL, "Duplicate From header");
address(ctx, s);
h2->from = uw_strdup(ctx, s);
return h2;
}
uw_Email_headers uw_Email_to(uw_context ctx, uw_Basis_string s, uw_Email_headers h) {
uw_Email_headers h2 = uw_malloc(ctx, sizeof(struct headers));
if (h)
*h2 = *h;
else
memset(h2, 0, sizeof(*h2));
address(ctx, s);
if (h2->to) {
uw_Basis_string all = uw_malloc(ctx, strlen(h2->to) + 2 + strlen(s));
sprintf(all, "%s,%s", h2->to, s);
h2->to = all;
} else
h2->to = uw_strdup(ctx, s);
return h2;
}
uw_Email_headers uw_Email_cc(uw_context ctx, uw_Basis_string s, uw_Email_headers h) {
uw_Email_headers h2 = uw_malloc(ctx, sizeof(struct headers));
if (h)
*h2 = *h;
else
memset(h2, 0, sizeof(*h2));
address(ctx, s);
if (h2->cc) {
uw_Basis_string all = uw_malloc(ctx, strlen(h2->cc) + 2 + strlen(s));
sprintf(all, "%s,%s", h2->cc, s);
h2->cc = all;
} else
h2->cc = uw_strdup(ctx, s);
return h2;
}
uw_Email_headers uw_Email_bcc(uw_context ctx, uw_Basis_string s, uw_Email_headers h) {
uw_Email_headers h2 = uw_malloc(ctx, sizeof(struct headers));
if (h)
*h2 = *h;
else
memset(h2, 0, sizeof(*h2));
address(ctx, s);
if (h2->bcc) {
uw_Basis_string all = uw_malloc(ctx, strlen(h2->bcc) + 2 + strlen(s));
sprintf(all, "%s,%s", h2->bcc, s);
h2->bcc = all;
} else
h2->bcc = uw_strdup(ctx, s);
return h2;
}
uw_Email_headers uw_Email_subject(uw_context ctx, uw_Basis_string s, uw_Email_headers h) {
uw_Email_headers h2 = uw_malloc(ctx, sizeof(struct headers));
if (h)
*h2 = *h;
else
memset(h2, 0, sizeof(*h2));
if (h2->subject)
uw_error(ctx, FATAL, "Duplicate Subject header");
header(ctx, s);
h2->subject = uw_strdup(ctx, s);
return h2;
}
uw_Email_headers uw_Email_user_agent(uw_context ctx, uw_Basis_string s, uw_Email_headers h) {
uw_Email_headers h2 = uw_malloc(ctx, sizeof(struct headers));
if (h)
*h2 = *h;
else
memset(h2, 0, sizeof(*h2));
if (h2->user_agent)
uw_error(ctx, FATAL, "Duplicate User-Agent header");
header(ctx, s);
h2->user_agent = uw_strdup(ctx, s);
return h2;
}
typedef struct {
uw_context ctx;
uw_Email_headers h;
uw_Basis_string server, ca, user, password, body, xbody;
uw_Basis_bool ssl;
} job;
typedef struct {
const char *content;
size_t length;
} upload_status;
static size_t do_upload(void *ptr, size_t size, size_t nmemb, void *userp)
{
upload_status *upload_ctx = (upload_status *)userp;
size *= nmemb;
if (size > upload_ctx->length)
size = upload_ctx->length;
memcpy(ptr, upload_ctx->content, size);
upload_ctx->content += size;
upload_ctx->length -= size;
return size;
}
// Extract e-mail address from a string that is either *just* an e-mail address or looks like "Recipient <address>".
// Note: it's destructive!
// Luckily, we only apply it to strings we are done using for other purposes (copied into buffer with e-mail contents).
static char *addrOf(char *s) {
char *p = strchr(s, '<');
if (p) {
char *p2 = strchr(p+1, '>');
if (p2) {
*p2 = 0;
return p+1;
} else
return s;
} else
return s;
}
static void commit(void *data) {
job *j = data;
char *buf, *cur;
size_t buflen = 50;
CURL *curl;
CURLcode res;
upload_status upload_ctx;
struct curl_slist *recipients = NULL;
buflen += 8 + strlen(j->h->from);
if (j->h->to)
buflen += 6 + strlen(j->h->to);
if (j->h->cc)
buflen += 6 + strlen(j->h->cc);
if (j->h->bcc)
buflen += 7 + strlen(j->h->bcc);
if (j->h->subject)
buflen += 11 + strlen(j->h->subject);
if (j->h->user_agent)
buflen += 14 + strlen(j->h->user_agent);
buflen += strlen(j->body);
if (j->xbody)
buflen += 219 + strlen(j->xbody);
cur = buf = malloc(buflen);
if (!buf) {
uw_set_error_message(j->ctx, "Can't allocate buffer for message contents");
return;
}
if (j->h->from) {
int written = sprintf(cur, "From: %s\r\n", j->h->from);
if (written < 0) {
uw_set_error_message(j->ctx, "Error writing From address");
free(buf);
return;
} else
cur += written;
}
if (j->h->subject) {
int written = sprintf(cur, "Subject: %s\r\n", j->h->subject);
if (written < 0) {
uw_set_error_message(j->ctx, "Error writing Subject");
free(buf);
return;
} else
cur += written;
}
if (j->h->to) {
int written = sprintf(cur, "To: %s\r\n", j->h->to);
if (written < 0) {
uw_set_error_message(j->ctx, "Error writing To addresses");
free(buf);
return;
} else
cur += written;
}
if (j->h->cc) {
int written = sprintf(cur, "Cc: %s\r\n", j->h->cc);
if (written < 0) {
uw_set_error_message(j->ctx, "Error writing Cc addresses");
free(buf);
return;
} else
cur += written;
}
if (j->h->user_agent) {
int written = sprintf(cur, "User-Agent: %s\r\n", j->h->user_agent);
if (written < 0) {
uw_set_error_message(j->ctx, "Error writing User-Agent");
free(buf);
return;
} else
cur += written;
}
if (j->xbody) {
int written;
char separator[11];
separator[sizeof(separator)-1] = 0;
do {
int i;
for (i = 0; i < sizeof(separator)-1; ++i)
separator[i] = 'A' + (rand() % 26);
} while (strstr(j->body, separator) || strstr(j->xbody, separator));
written = sprintf(cur, "MIME-Version: 1.0\r\n"
"Content-Type: multipart/alternative; boundary=\"%s\"\r\n"
"\r\n"
"--%s\r\n"
"Content-Type: text/plain; charset=utf-8\r\n"
"\r\n"
"%s\r\n"
"--%s\r\n"
"Content-Type: text/html; charset=utf-8\r\n"
"\r\n"
"%s\r\n"
"--%s--",
separator, separator, j->body, separator, j->xbody, separator);
if (written < 0) {
uw_set_error_message(j->ctx, "Error writing bodies, including HTML");
free(buf);
return;
}
} else {
int written = sprintf(cur, "Content-Type: text/plain; charset=utf-8\r\n"
"\r\n"
"%s",
j->body);
if (written < 0) {
uw_set_error_message(j->ctx, "Error writing body");
free(buf);
return;
}
}
upload_ctx.content = buf;
upload_ctx.length = strlen(buf);
if (j->h->to) {
char *saveptr, *addr = strtok_r(j->h->to, ",", &saveptr);
do {
recipients = curl_slist_append(recipients, addrOf(addr));
} while ((addr = strtok_r(NULL, ",", &saveptr)));
}
if (j->h->cc) {
char *saveptr, *addr = strtok_r(j->h->cc, ",", &saveptr);
do {
recipients = curl_slist_append(recipients, addrOf(addr));
} while ((addr = strtok_r(NULL, ",", &saveptr)));
}
if (j->h->bcc) {
char *saveptr, *addr = strtok_r(j->h->bcc, ",", &saveptr);
do {
recipients = curl_slist_append(recipients, addrOf(addr));
} while ((addr = strtok_r(NULL, ",", &saveptr)));
}
curl = curl_easy_init();
if (!curl) {
free(buf);
uw_set_error_message(j->ctx, "Can't create curl object");
return;
}
curl_easy_setopt(curl, CURLOPT_USERNAME, j->user);
curl_easy_setopt(curl, CURLOPT_PASSWORD, j->password);
curl_easy_setopt(curl, CURLOPT_URL, j->server);
if (j->ssl) {
curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
if (j->ca) {
curl_easy_setopt(curl, CURLOPT_CAINFO, j->ca);
} else {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
}
}
curl_easy_setopt(curl, CURLOPT_MAIL_FROM, addrOf(j->h->from));
curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, do_upload);
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
uw_set_error_message(j->ctx, "Curl error sending e-mail: %s", curl_easy_strerror(res));
curl_slist_free_all(recipients);
curl_easy_cleanup(curl);
free(buf);
}
static void free_job(void *p, int will_retry) {
job *j = p;
free_headers(j->h);
free_string(j->server);
free_string(j->ca);
free_string(j->user);
free_string(j->password);
free_string(j->body);
free_string(j->xbody);
free(j);
}
uw_unit uw_Email_send(uw_context ctx, uw_Basis_string server,
uw_Basis_bool ssl, uw_Basis_string ca,
uw_Basis_string user, uw_Basis_string password,
uw_Email_headers h, uw_Basis_string body, uw_Basis_string xbody) {
job *j;
if (!h || !h->from)
uw_error(ctx, FATAL, "No From address set for e-mail message");
if (!h->to && !h->cc && !h->bcc)
uw_error(ctx, FATAL, "No recipients specified for e-mail message");
j = malloc(sizeof(job));
j->ctx = ctx;
j->h = copy_headers(h);
j->server = copy_string(server);
j->ssl = ssl;
j->ca = copy_string(ca);
j->user = copy_string(user);
j->password = copy_string(password);
j->body = copy_long_string(ctx, body);
j->xbody = copy_long_string(ctx, xbody);
uw_register_transactional(ctx, j, commit, NULL, free_job);
return uw_unit_v;
}