src/etpan_mime.c
changeset 89 aef5a4bc78f3
parent 62 ad5e484720e1
child 90 42b5eb9d5af2
     1.1 --- a/src/etpan_mime.c	Wed Mar 04 13:32:07 2015 +0100
     1.2 +++ b/src/etpan_mime.c	Sat Mar 07 20:29:13 2015 +0100
     1.3 @@ -1,12 +1,15 @@
     1.4  #include <string.h>
     1.5  #include <stdlib.h>
     1.6  #include <unistd.h>
     1.7 +#include <assert.h>
     1.8  
     1.9  #include "etpan_mime.h"
    1.10  #ifndef mailmime_param_new_with_data
    1.11  #include <libetpan/mailprivacy_tools.h>
    1.12  #endif
    1.13  
    1.14 +time_t mail_mkgmtime(struct tm * tmp);
    1.15 +
    1.16  #define MAX_MESSAGE_ID 512
    1.17  
    1.18  static char * generate_boundary(const char * boundary_prefix)
    1.19 @@ -242,3 +245,101 @@
    1.20      return NULL;
    1.21  }
    1.22  
    1.23 +struct mailimf_field * _new_field(
    1.24 +        int type,
    1.25 +        _new_func_t new_func,
    1.26 +        void *value
    1.27 +    )
    1.28 +{
    1.29 +    void *data = new_func(value);
    1.30 +    assert(data);
    1.31 +    if (data == NULL)
    1.32 +        return NULL;
    1.33 +
    1.34 +    struct mailimf_field * result = calloc(1, sizeof(struct mailimf_field));
    1.35 +    assert(result);
    1.36 +    if (result == NULL) {
    1.37 +        free(data);
    1.38 +        return NULL;
    1.39 +    }
    1.40 +
    1.41 +    result->fld_type = type;
    1.42 +    result->fld_data.fld_return_path = data;
    1.43 +
    1.44 +    return result;
    1.45 +}
    1.46 +
    1.47 +void _free_field(struct mailimf_field *field)
    1.48 +{
    1.49 +    if (field)
    1.50 +        free(field->fld_data.fld_return_path);
    1.51 +    free(field);
    1.52 +}
    1.53 +
    1.54 +int _append_field(
    1.55 +        clist *list,
    1.56 +        int type,
    1.57 +        _new_func_t new_func,
    1.58 +        void *value
    1.59 +    )
    1.60 +{
    1.61 +    int r;
    1.62 +    struct mailimf_field * field;
    1.63 +
    1.64 +    assert(list);
    1.65 +    assert(new_func);
    1.66 +    assert(value);
    1.67 +
    1.68 +    field = _new_field(type, new_func, value);
    1.69 +    if (field == NULL)
    1.70 +        return -1;
    1.71 +
    1.72 +
    1.73 +    r = clist_append(list, field);
    1.74 +    if (r == -1)
    1.75 +        _free_field(field);
    1.76 +
    1.77 +    return r;
    1.78 +}
    1.79 +
    1.80 +struct mailimf_date_time * timestamp_to_etpantime(const struct tm *ts)
    1.81 +{
    1.82 +    struct mailimf_date_time * result = calloc(1,
    1.83 +            sizeof(struct mailimf_date_time));
    1.84 +    assert(result);
    1.85 +    if (result == NULL)
    1.86 +        return NULL;
    1.87 +
    1.88 +    assert(ts);
    1.89 +
    1.90 +    result->dt_sec = ts->tm_sec;
    1.91 +    result->dt_min = ts->tm_min;
    1.92 +    result->dt_hour = ts->tm_hour;
    1.93 +    result->dt_day = ts->tm_mday;
    1.94 +    result->dt_month = ts->tm_mon + 1;
    1.95 +    result->dt_year = ts->tm_year + 1900;
    1.96 +    result->dt_zone = (int) (ts->tm_gmtoff / 36L);
    1.97 +
    1.98 +    return result;
    1.99 +}
   1.100 +
   1.101 +struct tm * etpantime_to_timestamp(const struct mailimf_date_time *et)
   1.102 +{
   1.103 +    struct tm * result = calloc(1, sizeof(struct tm));
   1.104 +    assert(result);
   1.105 +    if (result == NULL)
   1.106 +        return NULL;
   1.107 +
   1.108 +    assert(et);
   1.109 +
   1.110 +    result->tm_sec = et->dt_sec;
   1.111 +    result->tm_min = et->dt_min;
   1.112 +    result->tm_hour = et->dt_hour;
   1.113 +    result->tm_mday = et->dt_day;
   1.114 +    result->tm_mon = et->dt_month - 1;
   1.115 +    result->tm_year = et->dt_year - 1900;
   1.116 +    result->tm_gmtoff = 36L * (long) et->dt_zone;
   1.117 +
   1.118 +    return result;
   1.119 +}
   1.120 +