Some include tidying.
[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         DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR)
492         break;
493       }
494       result = p;
495       if ((n = mailobj->bodylist->email_info_attachment_read(mailobj->bodylist->handle, result + resultlen, BODY_BUFFER_SIZE)) > 0)
496         resultlen += n;
497     } while (n > 0);
498     if (mailobj->bodylist->email_info_attachment_close)
499       mailobj->bodylist->email_info_attachment_close(mailobj->bodylist->handle);
500     //else
501     //  free(mailobj->bodylist->handle);
502     mailobj->bodylist->handle = NULL;
503   }
504   return result;
505 }
506
507 void quickmail_add_body_file (quickmail mailobj, const char* mimetype, const char* path)
508 {
509   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);
510 }
511
512 void quickmail_add_body_memory (quickmail mailobj, const char* mimetype, char* data, size_t datalen, int mustfree)
513 {
514   email_info_attachment_list_add_memory(&mailobj->bodylist, (mimetype ? mimetype : default_mime_type), (mimetype ? mimetype : default_mime_type), data, datalen, mustfree);
515 }
516
517 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)
518 {
519   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);
520 }
521
522 int quickmail_remove_body (quickmail mailobj, const char* mimetype)
523 {
524   return email_info_attachment_list_delete(&mailobj->bodylist, mimetype);
525 }
526
527 void quickmail_list_bodies (quickmail mailobj, quickmail_list_attachment_callback_fn callback, void* callbackdata)
528 {
529   struct email_info_attachment_list_struct* p = mailobj->bodylist;
530   while (p) {
531     callback(mailobj, p->filename, p->mimetype, p->email_info_attachment_open, p->email_info_attachment_read, p->email_info_attachment_close, callbackdata);
532     p = p->next;
533   }
534 }
535
536 void quickmail_add_attachment_file (quickmail mailobj, const char* path, const char* mimetype)
537 {
538   email_info_attachment_list_add_file(&mailobj->attachmentlist, path, mimetype);
539 }
540
541 void quickmail_add_attachment_memory (quickmail mailobj, const char* filename, const char* mimetype, char* data, size_t datalen, int mustfree)
542 {
543   email_info_attachment_list_add_memory(&mailobj->attachmentlist, filename, mimetype, data, datalen, mustfree);
544 }
545
546 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)
547 {
548   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);
549 }
550
551 int quickmail_remove_attachment (quickmail mailobj, const char* filename)
552 {
553   return email_info_attachment_list_delete(&mailobj->attachmentlist, filename);
554 }
555
556 void quickmail_list_attachments (quickmail mailobj, quickmail_list_attachment_callback_fn callback, void* callbackdata)
557 {
558   struct email_info_attachment_list_struct* p = mailobj->attachmentlist;
559   while (p) {
560     callback(mailobj, p->filename, p->mimetype, p->email_info_attachment_open, p->email_info_attachment_read, p->email_info_attachment_close, callbackdata);
561     p = p->next;
562   }
563 }
564
565 void quickmail_set_debug_log (quickmail mailobj, FILE* filehandle)
566 {
567   mailobj->debuglog = filehandle;
568 }
569
570 void quickmail_fsave (quickmail mailobj, FILE* filehandle)
571 {
572   size_t n;
573   char buf[80];
574   while ((n = quickmail_get_data(buf, sizeof(buf), 1, mailobj)) > 0) {
575     for (size_t i = 0; i < n; i++)
576       fprintf(filehandle, "%c", buf[i]);
577   }
578 }
579
580 size_t quickmail_get_data (void* ptr, size_t size, size_t nmemb, void* userp)
581 {
582   struct email_info_struct* mailobj = (struct email_info_struct*)userp;
583
584   //abort if no data is requested
585   if (size * nmemb == 0)
586     return 0;
587
588   //initialize on first run
589   if (mailobj->current == MAILPART_INITIALIZE) {
590     free(mailobj->buf);
591     mailobj->buf = NULL;
592     mailobj->buflen = 0;
593     free(mailobj->mime_boundary_body);
594     mailobj->mime_boundary_body = NULL;
595     free(mailobj->mime_boundary_part);
596     mailobj->mime_boundary_part = NULL;
597     mailobj->current_attachment = mailobj->bodylist;
598     mailobj->current++;
599   }
600
601   //process current part of mail if no partial data is pending
602   while (mailobj->buflen == 0) {
603     if (mailobj->buflen == 0 && mailobj->current == MAILPART_HEADER) {
604       char* s;
605       //generate header part
606       char** p = &mailobj->buf;
607       mailobj->buf = NULL;
608       str_append(p, "User-Agent: libquickmail");
609       if (mailobj->timestamp != 0) {
610         char timestamptext[32];
611         if (strftime(timestamptext, sizeof(timestamptext), "%a, %d %b %Y %H:%M:%S %z", localtime(&mailobj->timestamp))) {\r
612           str_append(p, "Date: ");
613           str_append(p, timestamptext);
614           str_append(p, NEWLINE);
615         }
616 #ifdef _WIN32\r
617         //fallback method for Windows when %z (time zone offset) fails
618         else if (strftime(timestamptext, sizeof(timestamptext), "%a, %d %b %Y %H:%M:%S", localtime(&mailobj->timestamp))) {\r
619           TIME_ZONE_INFORMATION tzinfo;\r
620           if (GetTimeZoneInformation(&tzinfo) != TIME_ZONE_ID_INVALID)\r
621             sprintf(timestamptext + strlen(timestamptext), " %c%02i%02i", (tzinfo.Bias > 0 ? '-' : '+'), (int)-tzinfo.Bias / 60, (int)-tzinfo.Bias % 60);\r
622           str_append(p, "Date: ");
623           str_append(p, timestamptext);
624           str_append(p, NEWLINE);
625         }
626 #endif\r
627       }
628       if (mailobj->from && *mailobj->from) {
629         str_append(p, "From: <");
630         str_append(p, mailobj->from);
631         str_append(p, ">" NEWLINE);
632       }
633       if ((s = email_info_string_list_concatenate(mailobj->to)) != NULL) {
634         str_append(p, "To: ");
635         str_append(p, s);
636         str_append(p, NEWLINE);
637         free(s);
638       }
639       if ((s = email_info_string_list_concatenate(mailobj->cc)) != NULL) {
640         str_append(p, "Cc: ");
641         str_append(p, s);
642         str_append(p, NEWLINE);
643         free(s);
644       }
645       if (mailobj->subject) {
646         str_append(p, "Subject: ");
647         str_append(p, mailobj->subject);
648         str_append(p, NEWLINE);
649       }
650       if (mailobj->header) {
651         str_append(p, mailobj->header);
652       }
653       if (mailobj->attachmentlist) {
654         str_append(p, "MIME-Version: 1.0" NEWLINE);
655       }
656       if (mailobj->attachmentlist) {
657         mailobj->mime_boundary_part = randomize_zeros(strdup("=PART=SEPARATOR=_0000_0000_0000_0000_0000_0000_="));
658         str_append(p, "Content-Type: multipart/mixed; boundary=\"");
659         str_append(p, mailobj->mime_boundary_part);
660         str_append(p, "\"" NEWLINE NEWLINE "This is a multipart message in MIME format." NEWLINE NEWLINE "--");
661         str_append(p, mailobj->mime_boundary_part);
662         str_append(p, NEWLINE);
663       }
664       if (mailobj->bodylist && mailobj->bodylist->next) {
665         mailobj->mime_boundary_body = randomize_zeros(strdup("=BODY=SEPARATOR=_0000_0000_0000_0000_0000_0000_="));
666         str_append(p, "Content-Type: multipart/alternative; boundary=\"");
667         str_append(p, mailobj->mime_boundary_body);
668         str_append(p, NEWLINE);
669       }
670       mailobj->buflen = strlen(mailobj->buf);
671       mailobj->current++;
672     }
673     if (mailobj->buflen == 0 && mailobj->current == MAILPART_BODY) {
674       if (mailobj->current_attachment) {
675         if (!mailobj->current_attachment->handle) {
676           //open file with body data
677           while (mailobj->current_attachment) {
678             if ((mailobj->current_attachment->handle = mailobj->current_attachment->email_info_attachment_open(mailobj->current_attachment->filedata)) != NULL) {
679               break;
680             }
681             mailobj->current_attachment = mailobj->current_attachment->next;
682           }
683           if (!mailobj->current_attachment) {
684             mailobj->current_attachment = mailobj->attachmentlist;
685             mailobj->current++;
686           }
687           //generate attachment header
688           if (mailobj->current_attachment && mailobj->current_attachment->handle) {
689             mailobj->buf = NULL;
690             if (mailobj->mime_boundary_body) {
691               mailobj->buf = str_append(&mailobj->buf, NEWLINE "--");
692               mailobj->buf = str_append(&mailobj->buf, mailobj->mime_boundary_body);
693               mailobj->buf = str_append(&mailobj->buf, NEWLINE);
694             }
695             mailobj->buf = str_append(&mailobj->buf, "Content-Type: ");
696             mailobj->buf = str_append(&mailobj->buf, (mailobj->bodylist && mailobj->current_attachment->filename ? mailobj->current_attachment->filename : default_mime_type));
697             mailobj->buf = str_append(&mailobj->buf, NEWLINE "Content-Transfer-Encoding: 8bit" NEWLINE "Content-Disposition: inline" NEWLINE NEWLINE);
698             mailobj->buflen = strlen(mailobj->buf);
699           }
700         }
701         if (mailobj->buflen == 0 && mailobj->current_attachment && mailobj->current_attachment->handle) {
702           //read body data
703         if ((mailobj->buf = (char *) malloc(BODY_BUFFER_SIZE)) == NULL) {
704             DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR)
705           }
706           if (mailobj->buf == NULL || (mailobj->buflen = mailobj->current_attachment->email_info_attachment_read(mailobj->current_attachment->handle, mailobj->buf, BODY_BUFFER_SIZE)) <= 0) {
707             //end of file
708             free(mailobj->buf);
709             mailobj->buflen = 0;
710             if (mailobj->current_attachment->email_info_attachment_close)
711               mailobj->current_attachment->email_info_attachment_close(mailobj->current_attachment->handle);
712             //else
713             //  free(mailobj->current_attachment->handle);
714             mailobj->current_attachment->handle = NULL;
715             mailobj->current_attachment = mailobj->current_attachment->next;
716           }
717         }
718       } else {
719         mailobj->current_attachment = mailobj->attachmentlist;
720         mailobj->current++;
721       }
722     }
723     if (mailobj->buflen == 0 && mailobj->current == MAILPART_BODY_DONE) {
724       mailobj->buf = NULL;
725       if (mailobj->mime_boundary_body) {
726         mailobj->buf = str_append(&mailobj->buf, NEWLINE "--");
727         mailobj->buf = str_append(&mailobj->buf, mailobj->mime_boundary_body);
728         mailobj->buf = str_append(&mailobj->buf, "--" NEWLINE);
729         mailobj->buflen = strlen(mailobj->buf);
730         free(mailobj->mime_boundary_body);
731         mailobj->mime_boundary_body = NULL;
732       }
733       mailobj->current++;
734     }
735     if (mailobj->buflen == 0 && mailobj->current == MAILPART_ATTACHMENT) {
736       if (mailobj->current_attachment) {
737         if (!mailobj->current_attachment->handle) {
738           //open file to attach
739           while (mailobj->current_attachment) {
740             if ((mailobj->current_attachment->handle = mailobj->current_attachment->email_info_attachment_open(mailobj->current_attachment->filedata)) != NULL) {
741               break;
742             }
743             mailobj->current_attachment = mailobj->current_attachment->next;
744           }
745           //generate attachment header
746           if (mailobj->current_attachment && mailobj->current_attachment->handle) {
747             mailobj->buf = NULL;
748             if (mailobj->mime_boundary_part) {
749               mailobj->buf = str_append(&mailobj->buf, NEWLINE "--");
750               mailobj->buf = str_append(&mailobj->buf, mailobj->mime_boundary_part);
751               mailobj->buf = str_append(&mailobj->buf, NEWLINE);
752             }
753             mailobj->buf = str_append(&mailobj->buf, "Content-Type: ");
754             mailobj->buf = str_append(&mailobj->buf, (mailobj->current_attachment->mimetype ? mailobj->current_attachment->mimetype : "application/octet-stream"));
755             mailobj->buf = str_append(&mailobj->buf, "; Name=\"");
756             mailobj->buf = str_append(&mailobj->buf, mailobj->current_attachment->filename);
757             mailobj->buf = str_append(&mailobj->buf, "\"" NEWLINE "Content-Transfer-Encoding: base64" NEWLINE NEWLINE);
758             mailobj->buflen = strlen(mailobj->buf);
759           }
760         } else {
761           //generate next line of attachment data
762           size_t n = 0;
763           int mimelinepos = 0;
764           unsigned char igroup[3] = {0, 0, 0};
765           unsigned char ogroup[4];
766           mailobj->buflen = 0;
767           if ((mailobj->buf = (char*)malloc(MIME_LINE_WIDTH + NEWLINELENGTH + 1)) == NULL) {
768             DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR)
769             n = 0;
770           } else {
771             while (mimelinepos < MIME_LINE_WIDTH && (n = mailobj->current_attachment->email_info_attachment_read(mailobj->current_attachment->handle, igroup, 3)) > 0) {
772               //code data
773               ogroup[0] = mailobj->dtable[igroup[0] >> 2];
774               ogroup[1] = mailobj->dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
775               ogroup[2] = mailobj->dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
776               ogroup[3] = mailobj->dtable[igroup[2] & 0x3F];
777               //padd with "=" characters if less than 3 characters were read
778               if (n < 3) {
779                 ogroup[3] = '=';
780                 if (n < 2)
781                   ogroup[2] = '=';
782               }
783               memcpy(mailobj->buf + mimelinepos, ogroup, 4);
784               mailobj->buflen += 4;
785               mimelinepos += 4;
786             }
787             if (mimelinepos > 0) {
788               memcpy(mailobj->buf + mimelinepos, NEWLINE, NEWLINELENGTH);
789               mailobj->buflen += NEWLINELENGTH;
790             }
791           }
792           if (n <= 0) {
793             //end of file
794             if (mailobj->current_attachment->email_info_attachment_close)
795               mailobj->current_attachment->email_info_attachment_close(mailobj->current_attachment->handle);
796             else
797               free(mailobj->current_attachment->handle);
798             mailobj->current_attachment->handle = NULL;
799             mailobj->current_attachment = mailobj->current_attachment->next;
800           }
801         }
802       } else {
803         mailobj->current++;
804       }
805     }
806     if (mailobj->buflen == 0 && mailobj->current == MAILPART_END) {
807       mailobj->buf = NULL;
808       mailobj->buflen = 0;
809       if (mailobj->mime_boundary_part) {
810         mailobj->buf = str_append(&mailobj->buf, NEWLINE "--");
811         mailobj->buf = str_append(&mailobj->buf, mailobj->mime_boundary_part);
812         mailobj->buf = str_append(&mailobj->buf, "--" NEWLINE);
813         mailobj->buflen = strlen(mailobj->buf);
814         free(mailobj->mime_boundary_part);
815         mailobj->mime_boundary_part = NULL;
816       }
817       //mailobj->buf = str_append(&mailobj->buf, NEWLINE "." NEWLINE);
818       //mailobj->buflen = strlen(mailobj->buf);
819       mailobj->current++;
820     }
821     if (mailobj->buflen == 0 && mailobj->current == MAILPART_DONE) {
822       break;
823     }
824   }
825
826   //flush pending data if any
827   if (mailobj->buflen > 0) {
828           int len = ((size_t) mailobj->buflen > size * nmemb ? size * nmemb : mailobj->buflen);
829     memcpy(ptr, mailobj->buf, len);
830     if (len < mailobj->buflen) {
831       mailobj->buf = (char *) memmove(mailobj->buf, mailobj->buf + len, mailobj->buflen - len);
832       mailobj->buflen -= len;
833     } else {
834       free(mailobj->buf);
835       mailobj->buf = NULL;
836       mailobj->buflen = 0;
837     }
838     return len;
839   }
840
841   //if (mailobj->current != MAILPART_DONE)
842   //  ;//this should never be reached
843   mailobj->current = 0;
844   return 0;
845 }
846
847 char* add_angle_brackets (const char* data)
848 {
849   size_t datalen = strlen(data);
850   char* result;
851   if ((result = (char*)malloc(datalen + 3)) == NULL) {
852     DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR)
853     return NULL;
854   }
855   result[0] = '<';
856   memcpy(result + 1, data, datalen);
857   result[datalen + 1] = '>';
858   result[datalen + 2] = 0;
859   return result;
860 }
861
862 const char* quickmail_send (quickmail mailobj, const char* smtpserver, unsigned int smtpport, const char* username, const char* password)
863 {
864   //libcurl based sending
865   CURL *curl;
866   CURLcode result = CURLE_FAILED_INIT;
867   //curl_global_init(CURL_GLOBAL_ALL);
868   if ((curl = curl_easy_init()) != NULL) {
869     struct curl_slist *recipients = NULL;
870     struct email_info_email_list_struct* listentry;
871     //set destination URL
872     char* addr;
873     size_t len = strlen(smtpserver) + 14;
874     if ((addr = (char*)malloc(len)) == NULL) {
875       DEBUG_ERROR(ERRMSG_MEMORY_ALLOCATION_ERROR)
876       return ERRMSG_MEMORY_ALLOCATION_ERROR;
877     }
878     snprintf(addr, len, "smtp://%s:%u", smtpserver, smtpport);
879     curl_easy_setopt(curl, CURLOPT_URL, addr);
880     free(addr);
881     //try Transport Layer Security (TLS), but continue anyway if it fails
882     curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_TRY);
883     //don't fail if the TLS/SSL a certificate could not be verified
884     //alternative: add the issuer certificate (or the host certificate if
885     //the certificate is self-signed) to the set of certificates that are
886     //known to libcurl using CURLOPT_CAINFO and/or CURLOPT_CAPATH
887     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
888     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
889     //set authentication credentials if provided
890     if (username && *username)
891       curl_easy_setopt(curl, CURLOPT_USERNAME, username);
892     if (password)
893       curl_easy_setopt(curl, CURLOPT_PASSWORD, password);
894     //set from value for envelope reverse-path
895     if (mailobj->from && *mailobj->from) {
896       addr = add_angle_brackets(mailobj->from);
897       curl_easy_setopt(curl, CURLOPT_MAIL_FROM, addr);
898       free(addr);
899     }
900     //set recipients
901     listentry = mailobj->to;
902     while (listentry) {
903       if (listentry->data && *listentry->data) {
904         addr = add_angle_brackets(listentry->data);
905         recipients = curl_slist_append(recipients, addr);
906         free(addr);
907       }
908       listentry = listentry->next;
909     }
910     listentry = mailobj->cc;
911     while (listentry) {
912       if (listentry->data && *listentry->data) {
913         addr = add_angle_brackets(listentry->data);
914         recipients = curl_slist_append(recipients, addr);
915         free(addr);
916       }
917       listentry = listentry->next;
918     }
919     listentry = mailobj->bcc;
920     while (listentry) {
921       if (listentry->data && *listentry->data) {
922         addr = add_angle_brackets(listentry->data);
923         recipients = curl_slist_append(recipients, addr);
924         free(addr);
925       }
926       listentry = listentry->next;
927     }
928     curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
929     //set callback function for getting message body
930     curl_easy_setopt(curl, CURLOPT_READFUNCTION, quickmail_get_data);
931     curl_easy_setopt(curl, CURLOPT_READDATA, mailobj);
932     /* Without this curl sends VRFY, which exim errors on
933        (at least on main.carlh.net)
934     */
935     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
936     //enable debugging if requested
937     if (mailobj->debuglog) {
938       curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
939       curl_easy_setopt(curl, CURLOPT_STDERR, mailobj->debuglog);
940     }
941     //send the message
942     result = curl_easy_perform(curl);
943     //free the list of recipients and clean up
944     curl_slist_free_all(recipients);
945     curl_easy_cleanup(curl);
946   }
947   return (result == CURLE_OK ? NULL : curl_easy_strerror(result));
948 }