Fix crashes on x-thread signal emission.
[dcpomatic.git] / src / lib / quickmail.cc
1 /*
2     This file is part of libquickmail.
3
4     libquickmail 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 3 of the License, or
7     (at your option) any later version.
8
9     libquickmail 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 libquickmail.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "quickmail.h"
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <time.h>
23 #ifndef _WIN32
24 #include <unistd.h>
25 #endif
26 #include <curl/curl.h>
27
28 #define NEWLINE "\r\n"
29 #define NEWLINELENGTH 2
30
31 #define MIME_LINE_WIDTH 72
32 #define BODY_BUFFER_SIZE 256
33
34 //definitions of the differen stages of generating the message data
35 #define MAILPART_INITIALIZE 0
36 #define MAILPART_HEADER     1
37 #define MAILPART_BODY       2
38 #define MAILPART_BODY_DONE  3
39 #define MAILPART_ATTACHMENT 4
40 #define MAILPART_END        5
41 #define MAILPART_DONE       6
42
43 static const char* default_mime_type = "text/plain";
44
45 ////////////////////////////////////////////////////////////////////////
46
47 #define DEBUG_ERROR(errmsg)
48 static const char* ERRMSG_MEMORY_ALLOCATION_ERROR = "Memory allocation error";
49
50 ////////////////////////////////////////////////////////////////////////
51
52 char* randomize_zeros (char* data)
53 {
54   //replace all 0s with random digits
55   char* p = data;
56   while (*p) {
57     if (*p == '0')
58       *p = '0' + rand() % 10;
59     p++;
60   }
61   return data;
62 }
63
64 char* str_append (char** data, const char* newdata)
65 {
66   //append a string to the end of an existing string
67   char* p;
68   int len = (*data ? strlen(*data) : 0);
69   if ((p = (char*)realloc(*data, len + strlen(newdata) + 1)) == NULL) {
70     free(p);
71     DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR)
72     return NULL;
73   }
74   *data = p;
75   strcpy(*data + len, newdata);
76   return *data;
77 }
78
79 ////////////////////////////////////////////////////////////////////////
80
81 struct email_info_struct {
82   int current;  //must be zet to 0
83   time_t timestamp;
84   char* from;
85   struct email_info_email_list_struct* to;
86   struct email_info_email_list_struct* cc;
87   struct email_info_email_list_struct* bcc;
88   char* subject;
89   char* header;
90   struct email_info_attachment_list_struct* bodylist;
91   struct email_info_attachment_list_struct* attachmentlist;
92   char* buf;
93   int buflen;
94   char* mime_boundary_body;
95   char* mime_boundary_part;
96   struct email_info_attachment_list_struct* current_attachment;
97   FILE* debuglog;
98   char dtable[64];
99 };
100
101 ////////////////////////////////////////////////////////////////////////
102
103 struct email_info_email_list_struct {
104   char* data;
105   struct email_info_email_list_struct* next;
106 };
107
108 void email_info_string_list_add (struct email_info_email_list_struct** list, const char* data)
109 {
110   struct email_info_email_list_struct** p = list;
111   while (*p)
112     p = &(*p)->next;
113   if ((*p = (struct email_info_email_list_struct*)malloc(sizeof(struct email_info_email_list_struct))) == NULL) {
114     DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR)
115     return;
116   }
117   (*p)->data = (data ? strdup(data) : NULL);
118   (*p)->next = NULL;
119 }
120
121 void email_info_string_list_free (struct email_info_email_list_struct** list)
122 {
123   struct email_info_email_list_struct* p = *list;
124   struct email_info_email_list_struct* current;
125   while (p) {
126     current = p;
127     p = current->next;
128     free(current->data);
129     free(current);
130   }
131   *list = NULL;
132 }
133
134 char* email_info_string_list_concatenate (struct email_info_email_list_struct* list)
135 {
136   char* result = NULL;
137   struct email_info_email_list_struct* listentry = list;
138   while (listentry) {
139     if (listentry->data && *listentry->data) {
140       if (result)
141         str_append(&result, "," NEWLINE "\t");
142       str_append(&result, "<");
143       str_append(&result, listentry->data);
144       str_append(&result, ">");
145     }
146     listentry = listentry->next;
147   }
148   return result;
149 }
150
151 ////////////////////////////////////////////////////////////////////////
152
153 struct email_info_attachment_list_struct {
154   char* filename;
155   char* mimetype;
156   void* filedata;
157   void* handle;
158   quickmail_attachment_open_fn email_info_attachment_open;
159   quickmail_attachment_read_fn email_info_attachment_read;
160   quickmail_attachment_close_fn email_info_attachment_close;
161   quickmail_attachment_free_filedata_fn email_info_attachment_filedata_free;
162   struct email_info_attachment_list_struct* next;
163 };
164
165 struct email_info_attachment_list_struct* email_info_attachment_list_add (struct email_info_attachment_list_struct** list, const char* filename, const char* mimetype, void* filedata, quickmail_attachment_open_fn email_info_attachment_open, quickmail_attachment_read_fn email_info_attachment_read, quickmail_attachment_close_fn email_info_attachment_close, quickmail_attachment_free_filedata_fn email_info_attachment_filedata_free)
166 {
167   struct email_info_attachment_list_struct** p = list;
168   while (*p)
169     p = &(*p)->next;
170   if ((*p = (struct email_info_attachment_list_struct*)malloc(sizeof(struct email_info_attachment_list_struct))) == NULL) {
171     DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR)
172     return NULL;
173   }
174   (*p)->filename = strdup(filename ? filename : "UNNAMED");
175   (*p)->mimetype = (mimetype ? strdup(mimetype) : NULL);
176   (*p)->filedata = filedata;
177   (*p)->handle = NULL;
178   (*p)->email_info_attachment_open = email_info_attachment_open;
179   (*p)->email_info_attachment_read = email_info_attachment_read;
180   (*p)->email_info_attachment_close = email_info_attachment_close;
181   (*p)->email_info_attachment_filedata_free = email_info_attachment_filedata_free;
182   (*p)->next = NULL;
183   return *p;
184 }
185
186 void email_info_attachment_list_free_entry (struct email_info_attachment_list_struct* current)
187 {
188   if (current->handle) {
189     if (current->email_info_attachment_close)
190       current->email_info_attachment_close(current->handle);
191     //else
192     //  free(current->handle);
193     current->handle = NULL;
194   }
195   if (current->filedata) {
196     if (current->email_info_attachment_filedata_free)
197       current->email_info_attachment_filedata_free(current->filedata);
198     else
199       free(current->filedata);
200   }
201   if (current->mimetype)
202     free(current->mimetype);
203   free(current->filename);
204   free(current);
205 }
206
207 void email_info_attachment_list_free (struct email_info_attachment_list_struct** list)
208 {
209   struct email_info_attachment_list_struct* p = *list;
210   struct email_info_attachment_list_struct* current;
211   while (p) {
212     current = p;
213     p = current->next;
214     email_info_attachment_list_free_entry(current);
215   }
216   *list = NULL;
217 }
218
219 int email_info_attachment_list_delete (struct email_info_attachment_list_struct** list, const char* filename)
220 {
221   struct email_info_attachment_list_struct** p = list;
222   while (*p) {
223     if (strcmp((*p)->filename, filename) == 0) {
224       struct email_info_attachment_list_struct* current = *p;
225       *p = current->next;
226       email_info_attachment_list_free_entry(current);
227       return 0;
228     }
229     p = &(*p)->next;
230   }
231   return -1;
232 }
233
234 void email_info_attachment_list_close_handles (struct email_info_attachment_list_struct* list)
235 {
236   struct email_info_attachment_list_struct* p = list;
237   while (p) {
238     if (p->handle) {
239       if (p->email_info_attachment_close)
240         p->email_info_attachment_close(p->handle);
241       //else
242       //  free(p->handle);
243       p->handle = NULL;
244     }
245     p = p->next;
246   }
247 }
248
249 //dummy attachment functions
250
251 void* email_info_attachment_open_dummy (void *)
252 {
253         return (void *) &email_info_attachment_open_dummy;
254 }
255
256 size_t email_info_attachment_read_dummy (void *, void *, size_t)
257 {
258   return 0;
259 }
260
261 struct email_info_attachment_list_struct* email_info_attachment_list_add_dummy (struct email_info_attachment_list_struct** list, const char* filename, const char* mimetype)
262 {
263   return email_info_attachment_list_add(list, filename, mimetype, NULL, email_info_attachment_open_dummy, email_info_attachment_read_dummy, NULL, NULL);
264 }
265
266 //file attachment functions
267
268 void* email_info_attachment_open_file (void* filedata)
269 {
270   return (void*)fopen((char*)filedata, "rb");
271 }
272
273 size_t email_info_attachment_read_file (void* handle, void* buf, size_t len)
274 {
275   return fread(buf, 1, len, (FILE*)handle);
276 }
277
278 void email_info_attachment_close_file (void* handle)
279 {
280   if (handle)
281     fclose((FILE*)handle);
282 }
283
284 struct email_info_attachment_list_struct* email_info_attachment_list_add_file (struct email_info_attachment_list_struct** list, const char* path, const char* mimetype)
285 {
286   //determine base filename
287   const char* basename = path + strlen(path);
288   while (basename != path) {
289     basename--;
290     if (*basename == '/'
291 #ifdef _WIN32
292         || *basename == '\\' || *basename == ':'
293 #endif
294     ) {
295       basename++;
296       break;
297     }
298   }
299   return email_info_attachment_list_add(list, basename, mimetype, (void*)strdup(path), email_info_attachment_open_file, email_info_attachment_read_file, email_info_attachment_close_file, NULL);
300 }
301
302 //memory attachment functions
303
304 struct email_info_attachment_memory_filedata_struct {
305   char* data;
306   size_t datalen;
307   int mustfree;
308 };
309
310 struct email_info_attachment_memory_handle_struct {
311   const char* data;
312   size_t datalen;
313   size_t pos;
314 };
315
316 void* email_info_attachment_open_memory (void* filedata)
317 {
318   struct email_info_attachment_memory_filedata_struct* data;
319   struct email_info_attachment_memory_handle_struct* result;
320   data = ((struct email_info_attachment_memory_filedata_struct*)filedata);
321   if (!data->data)
322     return NULL;
323   if ((result = (struct email_info_attachment_memory_handle_struct*)malloc(sizeof(struct email_info_attachment_memory_handle_struct))) == NULL) {
324     DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR)
325     return NULL;
326   }
327   result->data = data->data;
328   result->datalen = data->datalen;
329   result->pos = 0;
330   return result;
331 }
332
333 size_t email_info_attachment_read_memory (void* handle, void* buf, size_t len)
334 {
335   struct email_info_attachment_memory_handle_struct* h = (struct email_info_attachment_memory_handle_struct*)handle;
336   size_t n = (h->pos + len <= h->datalen ? len : h->datalen - h->pos);
337   memcpy(buf, h->data + h->pos, n);
338   h->pos += n;
339   return n;
340 }
341
342 void email_info_attachment_close_memory (void* handle)
343 {
344   if (handle)
345     free(handle);
346 }
347
348 void email_info_attachment_filedata_free_memory (void* filedata)
349 {
350   struct email_info_attachment_memory_filedata_struct* data = ((struct email_info_attachment_memory_filedata_struct*)filedata);
351   if (data) {
352     if (data->mustfree)
353       free(data->data);
354     free(data);
355   }
356 }
357
358 struct email_info_attachment_list_struct* email_info_attachment_list_add_memory (struct email_info_attachment_list_struct** list, const char* filename, const char* mimetype, char* data, size_t datalen, int mustfree)
359 {
360   struct email_info_attachment_memory_filedata_struct* filedata;
361   if ((filedata = (struct email_info_attachment_memory_filedata_struct*)malloc(sizeof(struct email_info_attachment_memory_filedata_struct))) == NULL) {
362     DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR)
363     return NULL;
364   }
365   filedata->data = data;
366   filedata->datalen = datalen;
367   filedata->mustfree = mustfree;
368   return email_info_attachment_list_add(list, filename, mimetype, filedata, email_info_attachment_open_memory, email_info_attachment_read_memory, email_info_attachment_close_memory, email_info_attachment_filedata_free_memory);
369 }
370
371 ////////////////////////////////////////////////////////////////////////
372
373 int quickmail_initialize ()
374 {
375   return 0;
376 }
377
378 quickmail quickmail_create (const char* from, const char* subject)
379 {
380   int i;
381   struct email_info_struct* mailobj;
382   if ((mailobj = (struct email_info_struct*)malloc(sizeof(struct email_info_struct))) == NULL) {
383     DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR)
384     return NULL;
385   }
386   mailobj->current = 0;
387   mailobj->timestamp = time(NULL);
388   mailobj->from = (from ? strdup(from) : NULL);
389   mailobj->to = NULL;
390   mailobj->cc = NULL;
391   mailobj->bcc = NULL;
392   mailobj->subject = (subject ? strdup(subject) : NULL);
393   mailobj->header = NULL;
394   mailobj->bodylist = NULL;
395   mailobj->attachmentlist = NULL;
396   mailobj->buf = NULL;
397   mailobj->buflen = 0;
398   mailobj->mime_boundary_body = NULL;
399   mailobj->mime_boundary_part = NULL;
400   mailobj->current_attachment = NULL;
401   mailobj->debuglog = NULL;
402   for (i = 0; i < 26; i++) {
403     mailobj->dtable[i] = (char)('A' + i);
404     mailobj->dtable[26 + i] = (char)('a' + i);
405   }
406   for (i = 0; i < 10; i++) {
407     mailobj->dtable[52 + i] = (char)('0' + i);
408   }
409   mailobj->dtable[62] = '+';
410   mailobj->dtable[63] = '/';
411   srand(time(NULL));
412   return mailobj;
413 }
414
415 void quickmail_destroy (quickmail mailobj)
416 {
417   free(mailobj->from);
418   email_info_string_list_free(&mailobj->to);
419   email_info_string_list_free(&mailobj->cc);
420   email_info_string_list_free(&mailobj->bcc);
421   free(mailobj->subject);
422   free(mailobj->header);
423   email_info_attachment_list_free(&mailobj->bodylist);
424   email_info_attachment_list_free(&mailobj->attachmentlist);
425   free(mailobj->buf);
426   free(mailobj->mime_boundary_body);
427   free(mailobj->mime_boundary_part);
428   free(mailobj);
429 }
430
431 void quickmail_set_from (quickmail mailobj, const char* from)
432 {
433   free(mailobj->from);
434   mailobj->from = strdup(from);
435 }
436
437 const char* quickmail_get_from (quickmail mailobj)
438 {
439   return mailobj->from;
440 }
441
442 void quickmail_add_to (quickmail mailobj, const char* email)
443 {
444   email_info_string_list_add(&mailobj->to, email);
445 }
446
447 void quickmail_add_cc (quickmail mailobj, const char* email)
448 {
449   email_info_string_list_add(&mailobj->cc, email);
450 }
451
452 void quickmail_add_bcc (quickmail mailobj, const char* email)
453 {
454   email_info_string_list_add(&mailobj->bcc, email);
455 }
456
457 void quickmail_set_subject (quickmail mailobj, const char* subject)
458 {
459   free(mailobj->subject);
460   mailobj->subject = (subject ? strdup(subject) : NULL);
461 }
462
463 const char* quickmail_get_subject (quickmail mailobj)
464 {
465   return mailobj->subject;
466 }
467
468 void quickmail_add_header (quickmail mailobj, const char* headerline)
469 {
470   str_append(&mailobj->header, headerline);
471   str_append(&mailobj->header, NEWLINE);
472 }
473
474 void quickmail_set_body (quickmail mailobj, const char* body)
475 {
476   email_info_attachment_list_free(&mailobj->bodylist);
477   if (body)
478     email_info_attachment_list_add_memory(&mailobj->bodylist, default_mime_type, default_mime_type, strdup(body), strlen(body), 1);
479 }
480
481 char* quickmail_get_body (quickmail mailobj)
482 {
483   size_t n;
484   char* p;
485   char* result = NULL;
486   size_t resultlen = 0;
487   if (mailobj->bodylist && (mailobj->bodylist->handle = mailobj->bodylist->email_info_attachment_open(mailobj->bodylist->filedata)) != NULL) {
488     do {
489       if ((p = (char*)realloc(result, resultlen + BODY_BUFFER_SIZE)) == NULL) {
490         free(result);
491         result = NULL;
492         DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR)
493         break;
494       }
495       result = p;
496       if ((n = mailobj->bodylist->email_info_attachment_read(mailobj->bodylist->handle, result + resultlen, BODY_BUFFER_SIZE)) > 0)
497         resultlen += n;
498     } while (n > 0);
499     if (mailobj->bodylist->email_info_attachment_close)
500       mailobj->bodylist->email_info_attachment_close(mailobj->bodylist->handle);
501     //else
502     //  free(mailobj->bodylist->handle);
503     mailobj->bodylist->handle = NULL;
504   }
505   return result;
506 }
507
508 void quickmail_add_body_file (quickmail mailobj, const char* mimetype, const char* path)
509 {
510   email_info_attachment_list_add(&mailobj->bodylist, (mimetype ? mimetype : default_mime_type), (mimetype ? mimetype : default_mime_type), (void*)strdup(path), email_info_attachment_open_file, email_info_attachment_read_file, email_info_attachment_close_file, NULL);
511 }
512
513 void quickmail_add_body_memory (quickmail mailobj, const char* mimetype, char* data, size_t datalen, int mustfree)
514 {
515   email_info_attachment_list_add_memory(&mailobj->bodylist, (mimetype ? mimetype : default_mime_type), (mimetype ? mimetype : default_mime_type), data, datalen, mustfree);
516 }
517
518 void quickmail_add_body_custom (quickmail mailobj, const char* mimetype, char* data, quickmail_attachment_open_fn attachment_data_open, quickmail_attachment_read_fn attachment_data_read, quickmail_attachment_close_fn attachment_data_close, quickmail_attachment_free_filedata_fn attachment_data_filedata_free)
519 {
520   email_info_attachment_list_add(&mailobj->bodylist, (mimetype ? mimetype : default_mime_type), (mimetype ? mimetype : default_mime_type), data, (attachment_data_open ? attachment_data_open : email_info_attachment_open_dummy), (attachment_data_read ? attachment_data_read : email_info_attachment_read_dummy), attachment_data_close, attachment_data_filedata_free);
521 }
522
523 int quickmail_remove_body (quickmail mailobj, const char* mimetype)
524 {
525   return email_info_attachment_list_delete(&mailobj->bodylist, mimetype);
526 }
527
528 void quickmail_list_bodies (quickmail mailobj, quickmail_list_attachment_callback_fn callback, void* callbackdata)
529 {
530   struct email_info_attachment_list_struct* p = mailobj->bodylist;
531   while (p) {
532     callback(mailobj, p->filename, p->mimetype, p->email_info_attachment_open, p->email_info_attachment_read, p->email_info_attachment_close, callbackdata);
533     p = p->next;
534   }
535 }
536
537 void quickmail_add_attachment_file (quickmail mailobj, const char* path, const char* mimetype)
538 {
539   email_info_attachment_list_add_file(&mailobj->attachmentlist, path, mimetype);
540 }
541
542 void quickmail_add_attachment_memory (quickmail mailobj, const char* filename, const char* mimetype, char* data, size_t datalen, int mustfree)
543 {
544   email_info_attachment_list_add_memory(&mailobj->attachmentlist, filename, mimetype, data, datalen, mustfree);
545 }
546
547 void quickmail_add_attachment_custom (quickmail mailobj, const char* filename, const char* mimetype, char* data, quickmail_attachment_open_fn attachment_data_open, quickmail_attachment_read_fn attachment_data_read, quickmail_attachment_close_fn attachment_data_close, quickmail_attachment_free_filedata_fn attachment_data_filedata_free)
548 {
549   email_info_attachment_list_add(&mailobj->attachmentlist, filename, mimetype, data, (attachment_data_open ? attachment_data_open : email_info_attachment_open_dummy), (attachment_data_read ? attachment_data_read : email_info_attachment_read_dummy), attachment_data_close, attachment_data_filedata_free);
550 }
551
552 int quickmail_remove_attachment (quickmail mailobj, const char* filename)
553 {
554   return email_info_attachment_list_delete(&mailobj->attachmentlist, filename);
555 }
556
557 void quickmail_list_attachments (quickmail mailobj, quickmail_list_attachment_callback_fn callback, void* callbackdata)
558 {
559   struct email_info_attachment_list_struct* p = mailobj->attachmentlist;
560   while (p) {
561     callback(mailobj, p->filename, p->mimetype, p->email_info_attachment_open, p->email_info_attachment_read, p->email_info_attachment_close, callbackdata);
562     p = p->next;
563   }
564 }
565
566 void quickmail_set_debug_log (quickmail mailobj, FILE* filehandle)
567 {
568   mailobj->debuglog = filehandle;
569 }
570
571 void quickmail_fsave (quickmail mailobj, FILE* filehandle)
572 {
573   size_t n;
574   char buf[80];
575   while ((n = quickmail_get_data(buf, sizeof(buf), 1, mailobj)) > 0) {
576     for (size_t i = 0; i < n; i++)
577       fprintf(filehandle, "%c", buf[i]);
578   }
579 }
580
581 size_t quickmail_get_data (void* ptr, size_t size, size_t nmemb, void* userp)
582 {
583   struct email_info_struct* mailobj = (struct email_info_struct*)userp;
584
585   //abort if no data is requested
586   if (size * nmemb == 0)
587     return 0;
588
589   //initialize on first run
590   if (mailobj->current == MAILPART_INITIALIZE) {
591     free(mailobj->buf);
592     mailobj->buf = NULL;
593     mailobj->buflen = 0;
594     free(mailobj->mime_boundary_body);
595     mailobj->mime_boundary_body = NULL;
596     free(mailobj->mime_boundary_part);
597     mailobj->mime_boundary_part = NULL;
598     mailobj->current_attachment = mailobj->bodylist;
599     mailobj->current++;
600   }
601
602   //process current part of mail if no partial data is pending
603   while (mailobj->buflen == 0) {
604     if (mailobj->buflen == 0 && mailobj->current == MAILPART_HEADER) {
605       char* s;
606       //generate header part
607       char** p = &mailobj->buf;
608       mailobj->buf = NULL;
609       str_append(p, "User-Agent: libquickmail\n");
610       if (mailobj->timestamp != 0) {
611         char timestamptext[32];
612         if (strftime(timestamptext, sizeof(timestamptext), "%a, %d %b %Y %H:%M:%S %z", localtime(&mailobj->timestamp))) {\r
613           str_append(p, "Date: ");
614           str_append(p, timestamptext);
615           str_append(p, NEWLINE);
616         }
617 #ifdef _WIN32\r
618         //fallback method for Windows when %z (time zone offset) fails
619         else if (strftime(timestamptext, sizeof(timestamptext), "%a, %d %b %Y %H:%M:%S", localtime(&mailobj->timestamp))) {\r
620           TIME_ZONE_INFORMATION tzinfo;\r
621           if (GetTimeZoneInformation(&tzinfo) != TIME_ZONE_ID_INVALID)\r
622             sprintf(timestamptext + strlen(timestamptext), " %c%02i%02i", (tzinfo.Bias > 0 ? '-' : '+'), (int)-tzinfo.Bias / 60, (int)-tzinfo.Bias % 60);\r
623           str_append(p, "Date: ");
624           str_append(p, timestamptext);
625           str_append(p, NEWLINE);
626         }
627 #endif\r
628       }
629       if (mailobj->from && *mailobj->from) {
630         str_append(p, "From: <");
631         str_append(p, mailobj->from);
632         str_append(p, ">" NEWLINE);
633       }
634       if ((s = email_info_string_list_concatenate(mailobj->to)) != NULL) {
635         str_append(p, "To: ");
636         str_append(p, s);
637         str_append(p, NEWLINE);
638         free(s);
639       }
640       if ((s = email_info_string_list_concatenate(mailobj->cc)) != NULL) {
641         str_append(p, "Cc: ");
642         str_append(p, s);
643         str_append(p, NEWLINE);
644         free(s);
645       }
646       if (mailobj->subject) {
647         str_append(p, "Subject: ");
648         str_append(p, mailobj->subject);
649         str_append(p, NEWLINE);
650       }
651       if (mailobj->header) {
652         str_append(p, mailobj->header);
653       }
654       if (mailobj->attachmentlist) {
655         str_append(p, "MIME-Version: 1.0" NEWLINE);
656       }
657       if (mailobj->attachmentlist) {
658         mailobj->mime_boundary_part = randomize_zeros(strdup("=PART=SEPARATOR=_0000_0000_0000_0000_0000_0000_="));
659         str_append(p, "Content-Type: multipart/mixed; boundary=\"");
660         str_append(p, mailobj->mime_boundary_part);
661         str_append(p, "\"" NEWLINE NEWLINE "This is a multipart message in MIME format." NEWLINE NEWLINE "--");
662         str_append(p, mailobj->mime_boundary_part);
663         str_append(p, NEWLINE);
664       }
665       if (mailobj->bodylist && mailobj->bodylist->next) {
666         mailobj->mime_boundary_body = randomize_zeros(strdup("=BODY=SEPARATOR=_0000_0000_0000_0000_0000_0000_="));
667         str_append(p, "Content-Type: multipart/alternative; boundary=\"");
668         str_append(p, mailobj->mime_boundary_body);
669         str_append(p, NEWLINE);
670       }
671       mailobj->buflen = strlen(mailobj->buf);
672       mailobj->current++;
673     }
674     if (mailobj->buflen == 0 && mailobj->current == MAILPART_BODY) {
675       if (mailobj->current_attachment) {
676         if (!mailobj->current_attachment->handle) {
677           //open file with body data
678           while (mailobj->current_attachment) {
679             if ((mailobj->current_attachment->handle = mailobj->current_attachment->email_info_attachment_open(mailobj->current_attachment->filedata)) != NULL) {
680               break;
681             }
682             mailobj->current_attachment = mailobj->current_attachment->next;
683           }
684           if (!mailobj->current_attachment) {
685             mailobj->current_attachment = mailobj->attachmentlist;
686             mailobj->current++;
687           }
688           //generate attachment header
689           if (mailobj->current_attachment && mailobj->current_attachment->handle) {
690             mailobj->buf = NULL;
691             if (mailobj->mime_boundary_body) {
692               mailobj->buf = str_append(&mailobj->buf, NEWLINE "--");
693               mailobj->buf = str_append(&mailobj->buf, mailobj->mime_boundary_body);
694               mailobj->buf = str_append(&mailobj->buf, NEWLINE);
695             }
696             mailobj->buf = str_append(&mailobj->buf, "Content-Type: ");
697             mailobj->buf = str_append(&mailobj->buf, (mailobj->bodylist && mailobj->current_attachment->filename ? mailobj->current_attachment->filename : default_mime_type));
698             mailobj->buf = str_append(&mailobj->buf, NEWLINE "Content-Transfer-Encoding: 8bit" NEWLINE "Content-Disposition: inline" NEWLINE NEWLINE);
699             mailobj->buflen = strlen(mailobj->buf);
700           }
701         }
702         if (mailobj->buflen == 0 && mailobj->current_attachment && mailobj->current_attachment->handle) {
703           //read body data
704         if ((mailobj->buf = (char *) malloc(BODY_BUFFER_SIZE)) == NULL) {
705             DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR)
706           }
707           if (mailobj->buf == NULL || (mailobj->buflen = mailobj->current_attachment->email_info_attachment_read(mailobj->current_attachment->handle, mailobj->buf, BODY_BUFFER_SIZE)) <= 0) {
708             //end of file
709             free(mailobj->buf);
710             mailobj->buflen = 0;
711             if (mailobj->current_attachment->email_info_attachment_close)
712               mailobj->current_attachment->email_info_attachment_close(mailobj->current_attachment->handle);
713             //else
714             //  free(mailobj->current_attachment->handle);
715             mailobj->current_attachment->handle = NULL;
716             mailobj->current_attachment = mailobj->current_attachment->next;
717           }
718         }
719       } else {
720         mailobj->current_attachment = mailobj->attachmentlist;
721         mailobj->current++;
722       }
723     }
724     if (mailobj->buflen == 0 && mailobj->current == MAILPART_BODY_DONE) {
725       mailobj->buf = NULL;
726       if (mailobj->mime_boundary_body) {
727         mailobj->buf = str_append(&mailobj->buf, NEWLINE "--");
728         mailobj->buf = str_append(&mailobj->buf, mailobj->mime_boundary_body);
729         mailobj->buf = str_append(&mailobj->buf, "--" NEWLINE);
730         mailobj->buflen = strlen(mailobj->buf);
731         free(mailobj->mime_boundary_body);
732         mailobj->mime_boundary_body = NULL;
733       }
734       mailobj->current++;
735     }
736     if (mailobj->buflen == 0 && mailobj->current == MAILPART_ATTACHMENT) {
737       if (mailobj->current_attachment) {
738         if (!mailobj->current_attachment->handle) {
739           //open file to attach
740           while (mailobj->current_attachment) {
741             if ((mailobj->current_attachment->handle = mailobj->current_attachment->email_info_attachment_open(mailobj->current_attachment->filedata)) != NULL) {
742               break;
743             }
744             mailobj->current_attachment = mailobj->current_attachment->next;
745           }
746           //generate attachment header
747           if (mailobj->current_attachment && mailobj->current_attachment->handle) {
748             mailobj->buf = NULL;
749             if (mailobj->mime_boundary_part) {
750               mailobj->buf = str_append(&mailobj->buf, NEWLINE "--");
751               mailobj->buf = str_append(&mailobj->buf, mailobj->mime_boundary_part);
752               mailobj->buf = str_append(&mailobj->buf, NEWLINE);
753             }
754             mailobj->buf = str_append(&mailobj->buf, "Content-Type: ");
755             mailobj->buf = str_append(&mailobj->buf, (mailobj->current_attachment->mimetype ? mailobj->current_attachment->mimetype : "application/octet-stream"));
756             mailobj->buf = str_append(&mailobj->buf, "; Name=\"");
757             mailobj->buf = str_append(&mailobj->buf, mailobj->current_attachment->filename);
758             mailobj->buf = str_append(&mailobj->buf, "\"" NEWLINE "Content-Transfer-Encoding: base64" NEWLINE NEWLINE);
759             mailobj->buflen = strlen(mailobj->buf);
760           }
761         } else {
762           //generate next line of attachment data
763           size_t n = 0;
764           int mimelinepos = 0;
765           unsigned char igroup[3] = {0, 0, 0};
766           unsigned char ogroup[4];
767           mailobj->buflen = 0;
768           if ((mailobj->buf = (char*)malloc(MIME_LINE_WIDTH + NEWLINELENGTH + 1)) == NULL) {
769             DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR)
770             n = 0;
771           } else {
772             while (mimelinepos < MIME_LINE_WIDTH && (n = mailobj->current_attachment->email_info_attachment_read(mailobj->current_attachment->handle, igroup, 3)) > 0) {
773               //code data
774               ogroup[0] = mailobj->dtable[igroup[0] >> 2];
775               ogroup[1] = mailobj->dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
776               ogroup[2] = mailobj->dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
777               ogroup[3] = mailobj->dtable[igroup[2] & 0x3F];
778               //padd with "=" characters if less than 3 characters were read
779               if (n < 3) {
780                 ogroup[3] = '=';
781                 if (n < 2)
782                   ogroup[2] = '=';
783               }
784               memcpy(mailobj->buf + mimelinepos, ogroup, 4);
785               mailobj->buflen += 4;
786               mimelinepos += 4;
787             }
788             if (mimelinepos > 0) {
789               memcpy(mailobj->buf + mimelinepos, NEWLINE, NEWLINELENGTH);
790               mailobj->buflen += NEWLINELENGTH;
791             }
792           }
793           if (n <= 0) {
794             //end of file
795             if (mailobj->current_attachment->email_info_attachment_close)
796               mailobj->current_attachment->email_info_attachment_close(mailobj->current_attachment->handle);
797             else
798               free(mailobj->current_attachment->handle);
799             mailobj->current_attachment->handle = NULL;
800             mailobj->current_attachment = mailobj->current_attachment->next;
801           }
802         }
803       } else {
804         mailobj->current++;
805       }
806     }
807     if (mailobj->buflen == 0 && mailobj->current == MAILPART_END) {
808       mailobj->buf = NULL;
809       mailobj->buflen = 0;
810       if (mailobj->mime_boundary_part) {
811         mailobj->buf = str_append(&mailobj->buf, NEWLINE "--");
812         mailobj->buf = str_append(&mailobj->buf, mailobj->mime_boundary_part);
813         mailobj->buf = str_append(&mailobj->buf, "--" NEWLINE);
814         mailobj->buflen = strlen(mailobj->buf);
815         free(mailobj->mime_boundary_part);
816         mailobj->mime_boundary_part = NULL;
817       }
818       //mailobj->buf = str_append(&mailobj->buf, NEWLINE "." NEWLINE);
819       //mailobj->buflen = strlen(mailobj->buf);
820       mailobj->current++;
821     }
822     if (mailobj->buflen == 0 && mailobj->current == MAILPART_DONE) {
823       break;
824     }
825   }
826
827   //flush pending data if any
828   if (mailobj->buflen > 0) {
829           int len = ((size_t) mailobj->buflen > size * nmemb ? size * nmemb : mailobj->buflen);
830     memcpy(ptr, mailobj->buf, len);
831     if (len < mailobj->buflen) {
832       mailobj->buf = (char *) memmove(mailobj->buf, mailobj->buf + len, mailobj->buflen - len);
833       mailobj->buflen -= len;
834     } else {
835       free(mailobj->buf);
836       mailobj->buf = NULL;
837       mailobj->buflen = 0;
838     }
839     return len;
840   }
841
842   //if (mailobj->current != MAILPART_DONE)
843   //  ;//this should never be reached
844   mailobj->current = 0;
845   return 0;
846 }
847
848 char* add_angle_brackets (const char* data)
849 {
850   size_t datalen = strlen(data);
851   char* result;
852   if ((result = (char*)malloc(datalen + 3)) == NULL) {
853     DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR)
854     return NULL;
855   }
856   result[0] = '<';
857   memcpy(result + 1, data, datalen);
858   result[datalen + 1] = '>';
859   result[datalen + 2] = 0;
860   return result;
861 }
862
863 const char* quickmail_send (quickmail mailobj, const char* smtpserver, unsigned int smtpport, const char* username, const char* password)
864 {
865   //libcurl based sending
866   CURL *curl;
867   CURLcode result = CURLE_FAILED_INIT;
868   //curl_global_init(CURL_GLOBAL_ALL);
869   if ((curl = curl_easy_init()) != NULL) {
870     struct curl_slist *recipients = NULL;
871     struct email_info_email_list_struct* listentry;
872     //set destination URL
873     char* addr;
874     size_t len = strlen(smtpserver) + 14;
875     if ((addr = (char*)malloc(len)) == NULL) {
876       DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR)
877       return ERRMSG_MEMORY_ALLOCATION_ERROR;
878     }
879     snprintf(addr, len, "smtp://%s:%u", smtpserver, smtpport);
880     curl_easy_setopt(curl, CURLOPT_URL, addr);
881     free(addr);
882     //try Transport Layer Security (TLS), but continue anyway if it fails
883     curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_TRY);
884     //don't fail if the TLS/SSL a certificate could not be verified
885     //alternative: add the issuer certificate (or the host certificate if
886     //the certificate is self-signed) to the set of certificates that are
887     //known to libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH
888     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
889     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
890     //set authentication credentials if provided
891     if (username && *username)
892       curl_easy_setopt(curl, CURLOPT_USERNAME, username);
893     if (password)
894       curl_easy_setopt(curl, CURLOPT_PASSWORD, password);
895     //set from value for envelope reverse-path
896     if (mailobj->from && *mailobj->from) {
897       addr = add_angle_brackets(mailobj->from);
898       curl_easy_setopt(curl, CURLOPT_MAIL_FROM, addr);
899       free(addr);
900     }
901     //set recipients
902     listentry = mailobj->to;
903     while (listentry) {
904       if (listentry->data && *listentry->data) {
905         addr = add_angle_brackets(listentry->data);
906         recipients = curl_slist_append(recipients, addr);
907         free(addr);
908       }
909       listentry = listentry->next;
910     }
911     listentry = mailobj->cc;
912     while (listentry) {
913       if (listentry->data && *listentry->data) {
914         addr = add_angle_brackets(listentry->data);
915         recipients = curl_slist_append(recipients, addr);
916         free(addr);
917       }
918       listentry = listentry->next;
919     }
920     listentry = mailobj->bcc;
921     while (listentry) {
922       if (listentry->data && *listentry->data) {
923         addr = add_angle_brackets(listentry->data);
924         recipients = curl_slist_append(recipients, addr);
925         free(addr);
926       }
927       listentry = listentry->next;
928     }
929     curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
930     //set callback function for getting message body
931     curl_easy_setopt(curl, CURLOPT_READFUNCTION, quickmail_get_data);
932     curl_easy_setopt(curl, CURLOPT_READDATA, mailobj);
933     /* Without this curl sends VRFY, which exim errors on
934        (at least on main.carlh.net)
935     */
936     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
937     //enable debugging if requested
938     if (mailobj->debuglog) {
939       curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
940       curl_easy_setopt(curl, CURLOPT_STDERR, mailobj->debuglog);
941     }
942     //send the message
943     result = curl_easy_perform(curl);
944     //free the list of recipients and clean up
945     curl_slist_free_all(recipients);
946     curl_easy_cleanup(curl);
947   }
948   return (result == CURLE_OK ? NULL : curl_easy_strerror(result));
949 }