src/etpan_mime.c
changeset 94 9ec29aa10b58
parent 92 c9be62386ffb
child 114 7a006f74365d
     1.1 --- a/src/etpan_mime.c	Sun Mar 08 12:18:20 2015 +0100
     1.2 +++ b/src/etpan_mime.c	Mon Mar 09 23:20:08 2015 +0100
     1.3 @@ -372,3 +372,66 @@
     1.4      return NULL;
     1.5  }
     1.6  
     1.7 +struct mailimf_field * create_optional_field(
     1.8 +        const char *field,
     1.9 +        const char *value
    1.10 +    )
    1.11 +{
    1.12 +    char *_field = NULL;
    1.13 +    char *_value = NULL;
    1.14 +    struct mailimf_optional_field *optional_field = NULL;
    1.15 +
    1.16 +    _field = strdup(field);
    1.17 +    if (_field == NULL)
    1.18 +        goto enomem;
    1.19 +
    1.20 +    _value = mailmime_encode_subject_header("utf-8", value, 0);
    1.21 +    if (_value == NULL)
    1.22 +        goto enomem;
    1.23 +
    1.24 +    optional_field = mailimf_optional_field_new(_field, _value);
    1.25 +    if (optional_field == NULL)
    1.26 +        goto enomem;
    1.27 +
    1.28 +    struct mailimf_field * result = calloc(1, sizeof(struct mailimf_field));
    1.29 +    assert(result);
    1.30 +    if (result == NULL)
    1.31 +        goto enomem;
    1.32 +
    1.33 +    result->fld_type = MAILIMF_FIELD_OPTIONAL_FIELD;
    1.34 +    result->fld_data.fld_optional_field = optional_field;
    1.35 +
    1.36 +    return result;
    1.37 +
    1.38 +enomem:
    1.39 +    if (optional_field) {
    1.40 +        mailimf_optional_field_free(optional_field);
    1.41 +    }
    1.42 +    else {
    1.43 +        free(_field);
    1.44 +        free(_value);
    1.45 +    }
    1.46 +
    1.47 +    return NULL;
    1.48 +}
    1.49 +
    1.50 +int _append_optional_field(
    1.51 +        clist *list,
    1.52 +        const char *field,
    1.53 +        const char *value
    1.54 +    )
    1.55 +{
    1.56 +    int r;
    1.57 +    struct mailimf_field * optional_field =
    1.58 +            create_optional_field(field, value);
    1.59 +
    1.60 +    if (optional_field == NULL)
    1.61 +        return -1;
    1.62 +
    1.63 +    r = clist_append(list, optional_field);
    1.64 +    if (r)
    1.65 +        mailimf_field_free(optional_field);
    1.66 +
    1.67 +    return r;
    1.68 +}
    1.69 +