static void prepare_mime_single(struct mailmime * mime) { struct mailmime_single_fields single_fields; int encoding; int r; if (mime->mm_mime_fields != NULL) { mailmime_single_fields_init(&single_fields, mime->mm_mime_fields, mime->mm_content_type); if (single_fields.fld_encoding != NULL) { encoding = single_fields.fld_encoding->enc_type; switch (encoding) { case MAILMIME_MECHANISM_8BIT: case MAILMIME_MECHANISM_7BIT: case MAILMIME_MECHANISM_BINARY: single_fields.fld_encoding->enc_type = MAILMIME_MECHANISM_QUOTED_PRINTABLE; break; } } else { struct mailmime_mechanism * mechanism; struct mailmime_field * field; mechanism = mailmime_mechanism_new(MAILMIME_MECHANISM_QUOTED_PRINTABLE, NULL); if (mechanism == NULL) return; field = mailmime_field_new(MAILMIME_FIELD_TRANSFER_ENCODING, NULL, mechanism, NULL, NULL, 0, NULL, NULL, NULL); if (field == NULL) { mailmime_mechanism_free(mechanism); return; } r = clist_append(mime->mm_mime_fields->fld_list, field); if (r < 0) { mailmime_field_free(field); return; } } } if (mime->mm_type == MAILMIME_SINGLE) { switch (mime->mm_data.mm_single->dt_encoding) { case MAILMIME_MECHANISM_8BIT: case MAILMIME_MECHANISM_7BIT: case MAILMIME_MECHANISM_BINARY: mime->mm_data.mm_single->dt_encoding = MAILMIME_MECHANISM_QUOTED_PRINTABLE; mime->mm_data.mm_single->dt_encoded = 0; break; } } }
struct mailmime_fields * mailmime_fields_new_with_version(struct mailmime_mechanism * encoding, char * id, char * description, struct mailmime_disposition * disposition, struct mailmime_language * language) { struct mailmime_field * field; struct mailmime_fields * fields; int r; fields = mailmime_fields_new_with_data(encoding, id, description, disposition, language); if (fields == NULL) goto err; field = mailmime_field_new(MAILMIME_FIELD_VERSION, NULL, NULL, NULL, NULL, MIME_VERSION, NULL, NULL, NULL); if (field == NULL) goto free; r = mailmime_fields_add(fields, field); if (r != MAILIMF_NO_ERROR) { mailmime_field_detach(field); mailmime_field_free(field); goto free; } return fields; free: clist_foreach(fields->fld_list, (clist_func) mailmime_field_detach, NULL); mailmime_fields_free(fields); err: return NULL; }
LIBETPAN_EXPORT int mailmime_field_parse(struct mailimf_optional_field * field, struct mailmime_field ** result) { char * name; char * value; int guessed_type; size_t cur_token; struct mailmime_content * content; struct mailmime_mechanism * encoding; char * id; char * description; uint32_t version; struct mailmime_field * mime_field; struct mailmime_language * language; struct mailmime_disposition * disposition; char * location; int res; int r; name = field->fld_name; value = field->fld_value; cur_token = 0; content = NULL; encoding = NULL; id = NULL; description = NULL; version = 0; disposition = NULL; language = NULL; location = NULL; guessed_type = guess_field_type(name); switch (guessed_type) { case MAILMIME_FIELD_TYPE: if (strcasecmp(name, "Content-Type") != 0) return MAILIMF_ERROR_PARSE; { size_t cur_token = 0; char * decoded_value; r = mailmime_encoded_phrase_parse("us-ascii", value, strlen(value), &cur_token, "utf-8", &decoded_value); if (r != MAILIMF_NO_ERROR) { cur_token = 0; r = mailmime_content_parse(value, strlen(value), &cur_token, &content); } else { cur_token = 0; r = mailmime_content_parse(decoded_value, strlen(decoded_value), &cur_token, &content); free(decoded_value); } if (r != MAILIMF_NO_ERROR) return r; } break; case MAILMIME_FIELD_TRANSFER_ENCODING: if (strcasecmp(name, "Content-Transfer-Encoding") != 0) return MAILIMF_ERROR_PARSE; r = mailmime_encoding_parse(value, strlen(value), &cur_token, &encoding); if (r != MAILIMF_NO_ERROR) return r; break; case MAILMIME_FIELD_ID: if (strcasecmp(name, "Content-ID") != 0) return MAILIMF_ERROR_PARSE; r = mailmime_id_parse(value, strlen(value), &cur_token, &id); if (r != MAILIMF_NO_ERROR) return r; break; case MAILMIME_FIELD_DESCRIPTION: if (strcasecmp(name, "Content-Description") != 0) return MAILIMF_ERROR_PARSE; r = mailmime_description_parse(value, strlen(value), &cur_token, &description); if (r != MAILIMF_NO_ERROR) return r; break; case MAILMIME_FIELD_VERSION: if (strcasecmp(name, "MIME-Version") != 0) return MAILIMF_ERROR_PARSE; r = mailmime_version_parse(value, strlen(value), &cur_token, &version); if (r != MAILIMF_NO_ERROR) return r; break; case MAILMIME_FIELD_DISPOSITION: if (strcasecmp(name, "Content-Disposition") != 0) return MAILIMF_ERROR_PARSE; r = mailmime_disposition_parse(value, strlen(value), &cur_token, &disposition); if (r != MAILIMF_NO_ERROR) return r; break; case MAILMIME_FIELD_LANGUAGE: if (strcasecmp(name, "Content-Language") != 0) return MAILIMF_ERROR_PARSE; r = mailmime_language_parse(value, strlen(value), &cur_token, &language); if (r != MAILIMF_NO_ERROR) return r; break; case MAILMIME_FIELD_LOCATION: if (strcasecmp(name, "Content-Location") != 0) return MAILIMF_ERROR_PARSE; r = mailmime_location_parse(value, strlen(value), &cur_token, &location); if (r != MAILIMF_NO_ERROR) return r; break; default: return MAILIMF_ERROR_PARSE; } mime_field = mailmime_field_new(guessed_type, content, encoding, id, description, version, disposition, language, location); if (mime_field == NULL) { res = MAILIMF_ERROR_MEMORY; goto free; } * result = mime_field; return MAILIMF_NO_ERROR; free: if (location != NULL) mailmime_location_free(location); if (language != NULL) mailmime_language_free(language); if (content != NULL) mailmime_content_free(content); if (encoding != NULL) mailmime_encoding_free(encoding); if (id != NULL) mailmime_id_free(id); if (description != NULL) mailmime_description_free(description); return res; }
struct mailmime_fields * mailmime_fields_new_with_data(struct mailmime_mechanism * encoding, char * id, char * description, struct mailmime_disposition * disposition, struct mailmime_language * language) { struct mailmime_field * field; struct mailmime_fields * fields; int r; fields = mailmime_fields_new_empty(); if (fields == NULL) goto err; #if 0 if (content != NULL) { field = mailmime_field_new(MAILMIME_FIELD_TYPE, content, NULL, NULL, NULL, 0, NULL, NULL); if (field == NULL) goto free; r = mailmime_fields_add(fields, field); if (r != MAILIMF_NO_ERROR) { mailmime_field_detach(field); mailmime_field_free(field); goto free; } } #endif if (encoding != NULL) { field = mailmime_field_new(MAILMIME_FIELD_TRANSFER_ENCODING, NULL, encoding, NULL, NULL, 0, NULL, NULL, NULL); if (field == NULL) goto free; r = mailmime_fields_add(fields, field); if (r != MAILIMF_NO_ERROR) { mailmime_field_detach(field); mailmime_field_free(field); goto free; } } if (id != NULL) { field = mailmime_field_new(MAILMIME_FIELD_ID, NULL, NULL, id, NULL, 0, NULL, NULL, NULL); if (field == NULL) goto free; r = mailmime_fields_add(fields, field); if (r != MAILIMF_NO_ERROR) { mailmime_field_detach(field); mailmime_field_free(field); goto free; } } if (description != NULL) { field = mailmime_field_new(MAILMIME_FIELD_DESCRIPTION, NULL, NULL, NULL, description, 0, NULL, NULL, NULL); if (field == NULL) goto free; r = mailmime_fields_add(fields, field); if (r != MAILIMF_NO_ERROR) { mailmime_field_detach(field); mailmime_field_free(field); goto free; } } if (disposition != NULL) { field = mailmime_field_new(MAILMIME_FIELD_DISPOSITION, NULL, NULL, NULL, NULL, 0, disposition, NULL, NULL); if (field == NULL) goto free; r = mailmime_fields_add(fields, field); if (r != MAILIMF_NO_ERROR) { mailmime_field_detach(field); mailmime_field_free(field); goto free; } } if (language != NULL) { field = mailmime_field_new(MAILMIME_FIELD_DISPOSITION, NULL, NULL, NULL, NULL, 0, NULL, language, NULL); if (field == NULL) goto free; r = mailmime_fields_add(fields, field); if (r != MAILIMF_NO_ERROR) { mailmime_field_detach(field); mailmime_field_free(field); goto free; } } return fields; free: clist_foreach(fields->fld_list, (clist_func) mailmime_field_detach, NULL); mailmime_fields_free(fields); err: return NULL; }