Commit fdf27cc4 fdf27cc40bc54494c89e86da5dfdce8b0248f2d3 by Sergey Poznyakoff

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.
1 parent 57c987c1
...@@ -216,11 +216,9 @@ extern int mu_attachment_create (mu_message_t *newmsg, ...@@ -216,11 +216,9 @@ extern int mu_attachment_create (mu_message_t *newmsg,
216 const char *encoding, 216 const char *encoding,
217 const char *name, const char *filename); 217 const char *name, const char *filename);
218 extern int mu_attachment_copy_from_stream (mu_message_t att, 218 extern int mu_attachment_copy_from_stream (mu_message_t att,
219 mu_stream_t stream, 219 mu_stream_t stream);
220 char const *encoding);
221 extern int mu_attachment_copy_from_file (mu_message_t att, 220 extern int mu_attachment_copy_from_file (mu_message_t att,
222 char const *filename, 221 char const *filename);
223 char const *encoding);
224 extern int mu_message_create_attachment (const char *content_type, 222 extern int mu_message_create_attachment (const char *content_type,
225 const char *encoding, 223 const char *encoding,
226 const char *filename, 224 const char *filename,
......
...@@ -108,7 +108,8 @@ at_hdr (mu_header_t hdr, const char *content_type, const char *encoding, ...@@ -108,7 +108,8 @@ at_hdr (mu_header_t hdr, const char *content_type, const char *encoding,
108 rc = mu_header_append (hdr, MU_HEADER_CONTENT_DISPOSITION, "attachment"); 108 rc = mu_header_append (hdr, MU_HEADER_CONTENT_DISPOSITION, "attachment");
109 if (rc) 109 if (rc)
110 return rc; 110 return rc;
111 return mu_header_append (hdr, MU_HEADER_CONTENT_TRANSFER_ENCODING, encoding); 111 return mu_header_append (hdr, MU_HEADER_CONTENT_TRANSFER_ENCODING,
112 encoding ? encoding : "8bit");
112 } 113 }
113 114
114 /* Create in *NEWMSG an empty attachment of given CONTENT_TYPE and ENCODING. 115 /* Create in *NEWMSG an empty attachment of given CONTENT_TYPE and ENCODING.
...@@ -149,23 +150,38 @@ mu_attachment_create (mu_message_t *newmsg, ...@@ -149,23 +150,38 @@ mu_attachment_create (mu_message_t *newmsg,
149 150
150 /* ATT is an attachment created by a previous call to mu_attachment_create(). 151 /* ATT is an attachment created by a previous call to mu_attachment_create().
151 152
152 Fills in the attachment body with the data from STREAM using the specified 153 Fills in the attachment body with the data from STREAM using the encoding
153 ENCODING. 154 stored in the Content-Transfer-Encoding header of ATT.
154 */ 155 */
155 int 156 int
156 mu_attachment_copy_from_stream (mu_message_t att, mu_stream_t stream, 157 mu_attachment_copy_from_stream (mu_message_t att, mu_stream_t stream)
157 char const *encoding)
158 { 158 {
159 mu_body_t body; 159 mu_body_t body;
160 mu_stream_t bstr; 160 mu_stream_t bstr;
161 mu_stream_t tstream; 161 mu_stream_t tstream;
162 mu_header_t hdr;
162 int rc; 163 int rc;
164 char *encoding;
163 165
164 mu_message_get_body (att, &body); 166 mu_message_get_header (att, &hdr);
165 rc = mu_body_get_streamref (body, &bstr); 167 rc = mu_header_aget_value_unfold (hdr, MU_HEADER_CONTENT_TRANSFER_ENCODING,
166 if (rc) 168 &encoding);
169 switch (rc)
170 {
171 case 0:
172 break;
173
174 case MU_ERR_NOENT:
175 return EINVAL;
176
177 default:
167 return rc; 178 return rc;
179 }
168 180
181 mu_message_get_body (att, &body);
182 rc = mu_body_get_streamref (body, &bstr);
183 if (rc == 0)
184 {
169 rc = mu_filter_create (&tstream, stream, encoding, MU_FILTER_ENCODE, 185 rc = mu_filter_create (&tstream, stream, encoding, MU_FILTER_ENCODE,
170 MU_STREAM_READ); 186 MU_STREAM_READ);
171 if (rc == 0) 187 if (rc == 0)
...@@ -174,17 +190,18 @@ mu_attachment_copy_from_stream (mu_message_t att, mu_stream_t stream, ...@@ -174,17 +190,18 @@ mu_attachment_copy_from_stream (mu_message_t att, mu_stream_t stream,
174 mu_stream_unref (tstream); 190 mu_stream_unref (tstream);
175 } 191 }
176 mu_stream_unref (bstr); 192 mu_stream_unref (bstr);
193 }
194 free (encoding);
177 return rc; 195 return rc;
178 } 196 }
179 197
180 /* ATT is an attachment created by a previous call to mu_attachment_create(). 198 /* ATT is an attachment created by a previous call to mu_attachment_create().
181 199
182 Fills in the attachment body with the data from FILENAME using the specified 200 Fills in the attachment body with the data from FILENAME using the encoding
183 ENCODING. 201 specified in the Content-Transfer-Encoding header.
184 */ 202 */
185 int 203 int
186 mu_attachment_copy_from_file (mu_message_t att, char const *filename, 204 mu_attachment_copy_from_file (mu_message_t att, char const *filename)
187 char const *encoding)
188 { 205 {
189 mu_stream_t stream; 206 mu_stream_t stream;
190 int rc; 207 int rc;
...@@ -192,7 +209,7 @@ mu_attachment_copy_from_file (mu_message_t att, char const *filename, ...@@ -192,7 +209,7 @@ mu_attachment_copy_from_file (mu_message_t att, char const *filename,
192 rc = mu_file_stream_create (&stream, filename, MU_STREAM_READ); 209 rc = mu_file_stream_create (&stream, filename, MU_STREAM_READ);
193 if (rc == 0) 210 if (rc == 0)
194 { 211 {
195 rc = mu_attachment_copy_from_stream (att, stream, encoding); 212 rc = mu_attachment_copy_from_stream (att, stream);
196 mu_stream_unref (stream); 213 mu_stream_unref (stream);
197 } 214 }
198 return rc; 215 return rc;
...@@ -208,8 +225,6 @@ mu_message_create_attachment (const char *content_type, const char *encoding, ...@@ -208,8 +225,6 @@ mu_message_create_attachment (const char *content_type, const char *encoding,
208 225
209 if (content_type == NULL) 226 if (content_type == NULL)
210 content_type = "text/plain"; 227 content_type = "text/plain";
211 if (encoding == NULL)
212 encoding = "7bit";
213 228
214 name = strrchr (filename, '/'); 229 name = strrchr (filename, '/');
215 if (name) 230 if (name)
...@@ -220,7 +235,7 @@ mu_message_create_attachment (const char *content_type, const char *encoding, ...@@ -220,7 +235,7 @@ mu_message_create_attachment (const char *content_type, const char *encoding,
220 rc = mu_attachment_create (&att, content_type, encoding, name, filename); 235 rc = mu_attachment_create (&att, content_type, encoding, name, filename);
221 if (rc == 0) 236 if (rc == 0)
222 { 237 {
223 rc = mu_attachment_copy_from_file (att, filename, encoding); 238 rc = mu_attachment_copy_from_file (att, filename);
224 if (rc) 239 if (rc)
225 mu_message_destroy (&att, NULL); 240 mu_message_destroy (&att, NULL);
226 } 241 }
......
...@@ -362,7 +362,7 @@ saveatt (void *item, void *data) ...@@ -362,7 +362,7 @@ saveatt (void *item, void *data)
362 return 1; 362 return 1;
363 } 363 }
364 364
365 rc = mu_attachment_copy_from_stream (part, aptr->source, aptr->encoding); 365 rc = mu_attachment_copy_from_stream (part, aptr->source);
366 if (rc) 366 if (rc)
367 { 367 {
368 mu_error (_("cannot attach %s: %s"), aptr->id, mu_strerror (rc)); 368 mu_error (_("cannot attach %s: %s"), aptr->id, mu_strerror (rc));
......