Offer a simple coloured display..
[s-mailx.git] / attachments.c
blob22ba7e0b9b51c58b9ab538d01d14628553e262b6
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Handling of attachments.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2013 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
40 #ifndef HAVE_AMALGAMATION
41 # include "nail.h"
42 #endif
44 /* We use calloc() for struct attachment */
45 CTA(AC_DEFAULT == 0);
47 /* Fill in some attachment fields; don't be interactive if number==0n */
48 static struct attachment * _fill_in(struct attachment *ap,
49 char const *file, ui_it number);
51 /* Ask the user to edit file names and other data for the given attachment */
52 static struct attachment * _read_attachment_data(struct attachment *ap,
53 ui_it number);
55 /* Try to create temporary charset converted version */
56 #ifdef HAVE_ICONV
57 static int _attach_iconv(struct attachment *ap);
58 #endif
60 static struct attachment *
61 _fill_in(struct attachment *ap, char const *file, ui_it number)
64 * XXX The "attachment-ask-content-*" variables are left undocumented
65 * since they are for RFC connoisseurs only.
67 char prefix[80 * 2];
69 ap->a_name = file;
70 if ((file = strrchr(file, '/')) != NULL)
71 ++file;
72 else
73 file = ap->a_name;
75 ap->a_content_type = mime_classify_content_type_by_fileext(file);
76 if (number > 0 && ok_blook(attachment_ask_content_type)) {
77 snprintf(prefix, sizeof prefix, "#%u\tContent-Type: ", number);
78 ap->a_content_type = readstr_input(prefix, ap->a_content_type);
81 if (number > 0 && ok_blook(attachment_ask_content_disposition)) {
82 snprintf(prefix, sizeof prefix,
83 "#%u\tContent-Disposition: ", number);
84 ap->a_content_disposition = readstr_input(prefix,
85 ap->a_content_disposition);
87 if (ap->a_content_disposition == NULL)
88 ap->a_content_disposition = "attachment";
90 if (number > 0 && ok_blook(attachment_ask_content_id)) {
91 snprintf(prefix, sizeof prefix, "#%u\tContent-ID: ", number);
92 ap->a_content_id = readstr_input(prefix, ap->a_content_id);
95 if (number > 0 && ok_blook(attachment_ask_content_description)) {
96 snprintf(prefix, sizeof prefix,
97 "#%u\tContent-Description: ", number);
98 ap->a_content_description = readstr_input(prefix,
99 ap->a_content_description);
101 return ap;
104 static struct attachment *
105 _read_attachment_data(struct attachment *ap, ui_it number)
107 char prefix[80 * 2];
108 char const *cslc = NULL/*cc uninit?*/, *cp, *defcs;
110 if (ap == NULL)
111 ap = csalloc(1, sizeof *ap);
112 else if (ap->a_msgno) {
113 char *ecp = salloc(16);
114 snprintf(ecp, 16, "#%u", (ui_it)ap->a_msgno);
115 ap->a_msgno = 0;
116 ap->a_name = ecp;
117 } else if (ap->a_conv == AC_TMPFILE) {
118 Fclose(ap->a_tmpf);
119 ap->a_conv = AC_DEFAULT;
122 snprintf(prefix, sizeof prefix, tr(50, "#%u\tfilename: "), number);
123 for (;;) {
124 if ((ap->a_name = readstr_input(prefix, ap->a_name)) == NULL) {
125 ap = NULL;
126 goto jleave;
128 /* May be a message number */
129 if (ap->a_name[0] == '#') {
130 char *ecp;
131 int msgno = (int)strtol(ap->a_name + 1, &ecp, 10);
132 if (msgno > 0 && msgno <= msgCount && *ecp == '\0') {
133 ap->a_msgno = msgno;
134 ap->a_content_description =
135 tr(513, "Attached message content");
136 if (options & OPT_INTERACTIVE)
137 printf(tr(2, "~@: added message #%u\n"),
138 (ui_it)msgno);
139 goto jleave;
142 if ((cp = file_expand(ap->a_name)) != NULL &&
143 access(cp, R_OK) == 0) {
144 ap->a_name = cp;
145 break;
147 perror(cp);
150 ap = _fill_in(ap, cp, number);
153 * Character set of attachments: enum attach_conv
155 cslc = charset_get_lc();
156 #ifdef HAVE_ICONV
157 if (! (options & OPT_INTERACTIVE))
158 goto jcs;
159 if ((cp = ap->a_content_type) != NULL &&
160 ascncasecmp(cp, "text/", 5) != 0 &&
161 ! yorn(tr(162, "Filename doesn't indicate text "
162 "content - want to edit charsets? "))) {
163 ap->a_conv = AC_DEFAULT;
164 goto jleave;
167 charset_iter_reset(NULL);
168 jcs:
169 #endif
170 snprintf(prefix, sizeof prefix, tr(160, "#%u\tinput charset: "),
171 number);
172 if ((defcs = ap->a_input_charset) == NULL)
173 defcs = cslc;
174 cp = ap->a_input_charset = readstr_input(prefix, defcs);
175 #ifdef HAVE_ICONV
176 if (! (options & OPT_INTERACTIVE)) {
177 #endif
178 ap->a_conv = (cp != NULL) ? AC_FIX_INCS : AC_DEFAULT;
179 #ifdef HAVE_ICONV
180 goto jleave;
183 snprintf(prefix, sizeof prefix, tr(161, "#%u\toutput (send) charset: "),
184 number);
185 if ((defcs = ap->a_charset) == NULL)
186 defcs = charset_iter_next();
187 defcs = ap->a_charset = readstr_input(prefix, defcs);
189 if (cp != NULL && defcs == NULL) {
190 ap->a_conv = AC_FIX_INCS;
191 goto jdone;
193 if (cp == NULL && defcs == NULL) {
194 ap->a_conv = AC_DEFAULT;
195 ap->a_input_charset = cslc;
196 ap->a_charset = charset_iter_current();
197 } else if (cp == NULL && defcs != NULL) {
198 ap->a_conv = AC_FIX_OUTCS;
199 ap->a_input_charset = cslc;
200 } else
201 ap->a_conv = AC_TMPFILE;
203 printf(tr(197, "Trying conversion from %s to %s\n"),
204 ap->a_input_charset, ap->a_charset);
205 if (_attach_iconv(ap))
206 ap->a_conv = AC_TMPFILE;
207 else {
208 ap->a_input_charset = cp;
209 ap->a_charset = defcs;
210 goto jcs;
212 jdone:
213 #endif
214 if (options & OPT_INTERACTIVE)
215 printf(tr(19, "~@: added attachment \"%s\"\n"), ap->a_name);
216 jleave:
217 return ap;
220 #ifdef HAVE_ICONV
221 static int
222 _attach_iconv(struct attachment *ap)
224 struct str oul = {NULL, 0}, inl = {NULL, 0};
225 FILE *fo = NULL, *fi = NULL;
226 size_t cnt, lbsize;
227 iconv_t icp;
229 if ((icp = n_iconv_open(ap->a_charset, ap->a_input_charset))
230 == (iconv_t)-1) {
231 if (errno == EINVAL)
232 goto jeconv;
233 else
234 perror("iconv_open");
235 goto jerr;
238 if ((fi = Fopen(ap->a_name, "r")) == NULL) {
239 perror(ap->a_name);
240 goto jerr;
242 cnt = fsize(fi);
244 inl.s = NULL;
245 if ((fo = Ftemp(&inl.s, "aiconv", "w+", 0600, 1)) == NULL) {
246 perror(tr(51, "temporary mail file"));
247 inl.s = NULL;
248 goto jerr;
250 unlink(inl.s);
251 Ftfree(&inl.s);
253 for (inl.s = NULL, lbsize = 0;;) {
254 if (fgetline(&inl.s, &lbsize, &cnt, &inl.l, fi, 0) == NULL) {
255 if (! cnt)
256 break;
257 perror(tr(195, "I/O read error occurred"));
258 goto jerr;
261 if (n_iconv_str(icp, &oul, &inl, NULL, FAL0) != 0)
262 goto jeconv;
263 if ((inl.l=fwrite(oul.s, sizeof *oul.s, oul.l, fo)) != oul.l) {
264 perror(tr(196, "I/O write error occurred"));
265 goto jerr;
268 fflush_rewind(fo);
270 ap->a_tmpf = fo;
271 jleave:
272 if (inl.s != NULL)
273 free(inl.s);
274 if (oul.s != NULL)
275 free(oul.s);
276 if (fi != NULL)
277 Fclose(fi);
278 if (icp != (iconv_t)-1)
279 n_iconv_close(icp);
280 return (fo != NULL);
281 jeconv:
282 fprintf(stderr, tr(179, "Cannot convert from %s to %s\n"),
283 ap->a_input_charset, ap->a_charset);
284 jerr:
285 if (fo != NULL)
286 Fclose(fo);
287 fo = NULL;
288 goto jleave;
290 #endif /* HAVE_ICONV */
292 FL struct attachment *
293 add_attachment(struct attachment *aphead, char *file, struct attachment **newap)
295 struct attachment *nap = NULL, *ap;
297 if ((file = file_expand(file)) == NULL)
298 goto jleave;
299 if (access(file, R_OK) != 0)
300 goto jleave;
302 nap = _fill_in(csalloc(1, sizeof *nap), file, 0);
303 if (newap != NULL)
304 *newap = nap;
305 if (aphead != NULL) {
306 for (ap = aphead; ap->a_flink != NULL; ap = ap->a_flink)
308 ap->a_flink = nap;
309 nap->a_blink = ap;
310 } else {
311 nap->a_blink = NULL;
312 aphead = nap;
314 nap = aphead;
315 jleave:
316 return nap;
319 FL struct attachment *
320 append_attachments(struct attachment *aphead, char *names)
322 char *cp;
323 struct attachment *xaph, *nap;
325 while ((cp = strcomma(&names, 1)) != NULL) {
326 if ((xaph = add_attachment(aphead, cp, &nap)) != NULL) {
327 aphead = xaph;
328 if (options & OPT_INTERACTIVE)
329 printf(tr(19, "~@: added attachment \"%s\"\n"),
330 nap->a_name);
331 } else
332 perror(cp);
334 return aphead;
337 FL struct attachment *
338 edit_attachments(struct attachment *aphead)
340 struct attachment *ap, *nap;
341 ui_it attno = 1;
343 /* Modify already present ones? */
344 for (ap = aphead; ap != NULL; ap = ap->a_flink) {
345 if (_read_attachment_data(ap, attno) != NULL) {
346 ++attno;
347 continue;
349 nap = ap->a_flink;
350 if (ap->a_blink != NULL)
351 ap->a_blink->a_flink = nap;
352 else
353 aphead = nap;
354 if (nap != NULL)
355 nap->a_blink = ap->a_blink;
356 else
357 goto jleave;
360 /* Add some more? */
361 while ((nap = _read_attachment_data(NULL, attno)) != NULL) {
362 if ((ap = aphead) != NULL) {
363 while (ap->a_flink != NULL)
364 ap = ap->a_flink;
365 ap->a_flink = nap;
367 nap->a_blink = ap;
368 nap->a_flink = NULL;
369 if (aphead == NULL)
370 aphead = nap;
371 ++attno;
373 jleave:
374 return aphead;