...
6 #include "etpan_mime.h"
7 #ifndef mailmime_param_new_with_data
8 #include <libetpan/mailprivacy_tools.h>
11 time_t mail_mkgmtime(struct tm * tmp);
13 #define MAX_MESSAGE_ID 512
15 static char * generate_boundary(void)
17 char id[MAX_MESSAGE_ID];
24 // no random needed here
31 snprintf(id, MAX_MESSAGE_ID, "%.4lx%.4lx%.4lx%.4lx", value1, value2,
37 struct mailmime * part_new_empty(
38 struct mailmime_content * content,
39 struct mailmime_fields * mime_fields,
43 struct mailmime * build_info;
47 char * attr_name = NULL;
48 char * attr_value = NULL;
49 struct mailmime_parameter * param = NULL;
50 clist * parameters = NULL;
51 char *boundary = NULL;
56 mime_type = MAILMIME_SINGLE;
59 switch (content->ct_type->tp_type) {
60 case MAILMIME_TYPE_DISCRETE_TYPE:
61 mime_type = MAILMIME_SINGLE;
64 case MAILMIME_TYPE_COMPOSITE_TYPE:
65 switch (content->ct_type->tp_data.tp_composite_type->ct_type) {
66 case MAILMIME_COMPOSITE_TYPE_MULTIPART:
67 mime_type = MAILMIME_MULTIPLE;
70 case MAILMIME_COMPOSITE_TYPE_MESSAGE:
71 if (strcasecmp(content->ct_subtype, "rfc822") == 0)
72 mime_type = MAILMIME_MESSAGE;
74 mime_type = MAILMIME_SINGLE;
87 if (mime_type == MAILMIME_MULTIPLE) {
93 attr_name = strdup("boundary");
95 if (attr_name == NULL)
98 boundary = generate_boundary();
100 attr_value = boundary;
101 if (attr_value == NULL)
104 param = mailmime_parameter_new(attr_name, attr_value);
111 if (content->ct_parameters == NULL) {
112 parameters = clist_new();
114 if (parameters == NULL)
118 parameters = content->ct_parameters;
121 r = clist_append(parameters, param);
126 if (content->ct_parameters == NULL)
127 content->ct_parameters = parameters;
130 build_info = mailmime_new(mime_type, NULL, 0, mime_fields, content, NULL,
131 NULL, NULL, list, NULL, NULL);
132 if (build_info == NULL)
142 if (content->ct_parameters == NULL)
144 clist_free(parameters);
149 struct mailmime * get_pgp_encrypted_part(void)
151 struct mailmime * mime = NULL;
152 struct mailmime_fields * mime_fields = NULL;
153 struct mailmime_content * content = NULL;
156 content = mailmime_content_new_with_str("application/pgp-encrypted");
160 mime_fields = mailmime_fields_new_empty();
161 if (mime_fields == NULL)
164 mime = part_new_empty(content, mime_fields, 1);
170 r = mailmime_set_body_text(mime, "Version: 1\n", 10);
178 mailmime_content_free(content);
180 mailmime_fields_free(mime_fields);
187 struct mailmime * get_text_part(
188 const char * filename,
189 const char * mime_type,
195 char * disposition_name = NULL;
196 struct mailmime_fields * mime_fields = NULL;
197 struct mailmime * mime = NULL;
198 struct mailmime_content * content = NULL;
199 struct mailmime_parameter * param = NULL;
200 struct mailmime_disposition * disposition = NULL;
201 struct mailmime_mechanism * encoding = NULL;
204 if (filename != NULL) {
205 disposition_name = strdup(filename);
206 if (disposition_name == NULL)
211 encoding = mailmime_mechanism_new(encoding_type, NULL);
212 if (encoding == NULL)
217 mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_INLINE,
218 disposition_name, NULL, NULL, NULL, (size_t) -1);
219 if (disposition == NULL)
221 disposition_name = NULL;
223 mime_fields = mailmime_fields_new_with_data(encoding, NULL, NULL,
225 if (mime_fields == NULL)
230 content = mailmime_content_new_with_str(mime_type);
234 if (encoding_type != MAILMIME_MECHANISM_7BIT) {
235 param = mailmime_param_new_with_data("charset", "utf-8");
236 r = clist_append(content->ct_parameters, param);
241 mime = part_new_empty(content, mime_fields, 1);
248 r = mailmime_set_body_text(mime, (char *) text, length);
256 free(disposition_name);
258 mailmime_fields_free(mime_fields);
262 mailmime_content_free(content);
264 mailmime_parameter_free(param);
266 mailmime_disposition_free(disposition);
268 mailmime_mechanism_free(encoding);
273 struct mailmime * get_file_part(
274 const char * filename,
275 const char * mime_type,
280 char * disposition_name = NULL;
282 struct mailmime_disposition * disposition = NULL;
283 struct mailmime_mechanism * encoding = NULL;
284 struct mailmime_content * content = NULL;
285 struct mailmime * mime = NULL;
286 struct mailmime_fields * mime_fields = NULL;
289 if (filename != NULL) {
290 disposition_name = strdup(filename);
291 if (disposition_name == NULL)
296 mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_ATTACHMENT,
297 disposition_name, NULL, NULL, NULL, (size_t) -1);
298 if (disposition == NULL)
300 disposition_name = NULL;
302 content = mailmime_content_new_with_str(mime_type);
306 encoding_type = MAILMIME_MECHANISM_BASE64;
307 encoding = mailmime_mechanism_new(encoding_type, NULL);
308 if (encoding == NULL)
311 mime_fields = mailmime_fields_new_with_data(encoding, NULL, NULL,
313 if (mime_fields == NULL)
318 mime = part_new_empty(content, mime_fields, 1);
324 r = mailmime_set_body_text(mime, data, length);
331 free(disposition_name);
333 mailmime_disposition_free(disposition);
335 mailmime_mechanism_free(encoding);
337 mailmime_content_free(content);
339 mailmime_fields_free(mime_fields);
346 struct mailmime * part_multiple_new(const char *type)
348 struct mailmime_fields *mime_fields = NULL;
349 struct mailmime_content *content = NULL;
350 struct mailmime *mp = NULL;
352 mime_fields = mailmime_fields_new_empty();
353 if (mime_fields == NULL)
356 content = mailmime_content_new_with_str(type);
360 mp = part_new_empty(content, mime_fields, 0);
368 mailmime_content_free(content);
370 mailmime_fields_free(mime_fields);
375 struct mailimf_field * _new_field(
377 _new_func_t new_func,
381 void *data = new_func(value);
386 struct mailimf_field * result = calloc(1, sizeof(struct mailimf_field));
388 if (result == NULL) {
393 result->fld_type = type;
394 result->fld_data.fld_return_path = data;
399 void _free_field(struct mailimf_field *field)
402 free(field->fld_data.fld_return_path);
409 _new_func_t new_func,
414 struct mailimf_field * field;
420 field = _new_field(type, new_func, value);
424 r = clist_append(list, field);
431 // http://media2.giga.de/2014/02/Image-28.jpg
433 struct mailimf_date_time * timestamp_to_etpantime(const struct tm *ts)
435 struct mailimf_date_time * result = calloc(1,
436 sizeof(struct mailimf_date_time));
443 result->dt_sec = ts->tm_sec;
444 result->dt_min = ts->tm_min;
445 result->dt_hour = ts->tm_hour;
446 result->dt_day = ts->tm_mday;
447 result->dt_month = ts->tm_mon + 1;
448 result->dt_year = ts->tm_year + 1900;
449 result->dt_zone = (int) (ts->tm_gmtoff / 36L);
454 struct tm * etpantime_to_timestamp(const struct mailimf_date_time *et)
456 struct tm * result = calloc(1, sizeof(struct tm));
463 result->tm_sec = et->dt_sec;
464 result->tm_min = et->dt_min;
465 result->tm_hour = et->dt_hour;
466 result->tm_mday = et->dt_day;
467 result->tm_mon = et->dt_month - 1;
468 result->tm_year = et->dt_year - 1900;
469 result->tm_gmtoff = 36L * (long) et->dt_zone;
474 struct mailimf_mailbox * mailbox_from_string(
479 struct mailimf_mailbox *mb = NULL;
481 char *_address = NULL;
485 _name = name ? strdup(name) : strdup("");
489 _address = strdup(address);
490 if (_address == NULL)
493 mb = mailimf_mailbox_new(_name, _address);
507 struct mailimf_field * create_optional_field(
514 struct mailimf_optional_field *optional_field = NULL;
516 _field = strdup(field);
520 _value = mailmime_encode_subject_header("utf-8", value, 0);
524 optional_field = mailimf_optional_field_new(_field, _value);
525 if (optional_field == NULL)
528 struct mailimf_field * result = calloc(1, sizeof(struct mailimf_field));
533 result->fld_type = MAILIMF_FIELD_OPTIONAL_FIELD;
534 result->fld_data.fld_optional_field = optional_field;
539 if (optional_field) {
540 mailimf_optional_field_free(optional_field);
550 int _append_optional_field(
557 struct mailimf_field * optional_field =
558 create_optional_field(field, value);
560 if (optional_field == NULL)
563 r = clist_append(list, optional_field);
565 mailimf_field_free(optional_field);