diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2017-01-16 13:00:20 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2017-01-16 13:00:20 +0200 |
commit | fdf27cc40bc54494c89e86da5dfdce8b0248f2d3 (patch) | |
tree | beef09a23d662163d80b757742e7b84dc9c58284 | |
parent | 57c987c11677924928716730abdd68c3d75ea044 (diff) | |
download | mailutils-fdf27cc40bc54494c89e86da5dfdce8b0248f2d3.tar.gz mailutils-fdf27cc40bc54494c89e86da5dfdce8b0248f2d3.tar.bz2 |
Improve mu_attachment_copy_ interface
* libmailutils/mime/attachment.c (mu_attachment_copy_from_stream)
(mu_attachment_copy_from_file): Remove the encoding parameter. Take
the encoding to use from the value of the Content-Transfer-Encoding
header. Return EINVAL if it is not present.
* include/mailutils/message.h (mu_attachment_copy_from_stream)
(mu_attachment_copy_from_file): Change signature. All uses changed.
-rw-r--r-- | include/mailutils/message.h | 6 | ||||
-rw-r--r-- | libmailutils/mime/attachment.c | 57 | ||||
-rw-r--r-- | mail/send.c | 2 |
3 files changed, 39 insertions, 26 deletions
diff --git a/include/mailutils/message.h b/include/mailutils/message.h index 7abf9548b..6f8fb6b95 100644 --- a/include/mailutils/message.h +++ b/include/mailutils/message.h @@ -216,11 +216,9 @@ extern int mu_attachment_create (mu_message_t *newmsg, const char *encoding, const char *name, const char *filename); extern int mu_attachment_copy_from_stream (mu_message_t att, - mu_stream_t stream, - char const *encoding); + mu_stream_t stream); extern int mu_attachment_copy_from_file (mu_message_t att, - char const *filename, - char const *encoding); + char const *filename); extern int mu_message_create_attachment (const char *content_type, const char *encoding, const char *filename, diff --git a/libmailutils/mime/attachment.c b/libmailutils/mime/attachment.c index 6a1d491be..50c928949 100644 --- a/libmailutils/mime/attachment.c +++ b/libmailutils/mime/attachment.c @@ -108,7 +108,8 @@ at_hdr (mu_header_t hdr, const char *content_type, const char *encoding, rc = mu_header_append (hdr, MU_HEADER_CONTENT_DISPOSITION, "attachment"); if (rc) return rc; - return mu_header_append (hdr, MU_HEADER_CONTENT_TRANSFER_ENCODING, encoding); + return mu_header_append (hdr, MU_HEADER_CONTENT_TRANSFER_ENCODING, + encoding ? encoding : "8bit"); } /* Create in *NEWMSG an empty attachment of given CONTENT_TYPE and ENCODING. @@ -149,42 +150,58 @@ mu_attachment_create (mu_message_t *newmsg, /* ATT is an attachment created by a previous call to mu_attachment_create(). - Fills in the attachment body with the data from STREAM using the specified - ENCODING. + Fills in the attachment body with the data from STREAM using the encoding + stored in the Content-Transfer-Encoding header of ATT. */ int -mu_attachment_copy_from_stream (mu_message_t att, mu_stream_t stream, - char const *encoding) +mu_attachment_copy_from_stream (mu_message_t att, mu_stream_t stream) { mu_body_t body; mu_stream_t bstr; mu_stream_t tstream; + mu_header_t hdr; int rc; + char *encoding; + + mu_message_get_header (att, &hdr); + rc = mu_header_aget_value_unfold (hdr, MU_HEADER_CONTENT_TRANSFER_ENCODING, + &encoding); + switch (rc) + { + case 0: + break; + + case MU_ERR_NOENT: + return EINVAL; + + default: + return rc; + } mu_message_get_body (att, &body); rc = mu_body_get_streamref (body, &bstr); - if (rc) - return rc; - - rc = mu_filter_create (&tstream, stream, encoding, MU_FILTER_ENCODE, - MU_STREAM_READ); if (rc == 0) { - rc = mu_stream_copy (bstr, tstream, 0, NULL); - mu_stream_unref (tstream); + rc = mu_filter_create (&tstream, stream, encoding, MU_FILTER_ENCODE, + MU_STREAM_READ); + if (rc == 0) + { + rc = mu_stream_copy (bstr, tstream, 0, NULL); + mu_stream_unref (tstream); + } + mu_stream_unref (bstr); } - mu_stream_unref (bstr); + free (encoding); return rc; } /* ATT is an attachment created by a previous call to mu_attachment_create(). - Fills in the attachment body with the data from FILENAME using the specified - ENCODING. + Fills in the attachment body with the data from FILENAME using the encoding + specified in the Content-Transfer-Encoding header. */ int -mu_attachment_copy_from_file (mu_message_t att, char const *filename, - char const *encoding) +mu_attachment_copy_from_file (mu_message_t att, char const *filename) { mu_stream_t stream; int rc; @@ -192,7 +209,7 @@ mu_attachment_copy_from_file (mu_message_t att, char const *filename, rc = mu_file_stream_create (&stream, filename, MU_STREAM_READ); if (rc == 0) { - rc = mu_attachment_copy_from_stream (att, stream, encoding); + rc = mu_attachment_copy_from_stream (att, stream); mu_stream_unref (stream); } return rc; @@ -208,8 +225,6 @@ mu_message_create_attachment (const char *content_type, const char *encoding, if (content_type == NULL) content_type = "text/plain"; - if (encoding == NULL) - encoding = "7bit"; name = strrchr (filename, '/'); if (name) @@ -220,7 +235,7 @@ mu_message_create_attachment (const char *content_type, const char *encoding, rc = mu_attachment_create (&att, content_type, encoding, name, filename); if (rc == 0) { - rc = mu_attachment_copy_from_file (att, filename, encoding); + rc = mu_attachment_copy_from_file (att, filename); if (rc) mu_message_destroy (&att, NULL); } diff --git a/mail/send.c b/mail/send.c index c36962693..22c238c74 100644 --- a/mail/send.c +++ b/mail/send.c @@ -362,7 +362,7 @@ saveatt (void *item, void *data) return 1; } - rc = mu_attachment_copy_from_stream (part, aptr->source, aptr->encoding); + rc = mu_attachment_copy_from_stream (part, aptr->source); if (rc) { mu_error (_("cannot attach %s: %s"), aptr->id, mu_strerror (rc)); |