*inbox*: if empty, only bypass *folder* to $MAIL or builtin default
[s-mailx.git] / attachments.c
blob8699b00be07477481bcddf4da7b40d70050ab62b
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 - 2015 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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE attachments
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 /* We use calloc() for struct attachment */
43 CTA(AC_DEFAULT == 0);
45 /* Fill in some attachment fields; don't be interactive if number==0 */
46 static struct attachment * _fill_in(struct attachment *ap,
47 char const *file, ui32_t number);
49 /* Ask the user to edit file names and other data for the given attachment */
50 static struct attachment * _read_attachment_data(struct attachment *ap,
51 ui32_t number);
53 /* Try to create temporary charset converted version */
54 #ifdef HAVE_ICONV
55 static int _attach_iconv(struct attachment *ap);
56 #endif
58 static struct attachment *
59 _fill_in(struct attachment *ap, char const *file, ui32_t number)
61 /* XXX The "attachment-ask-content-*" variables are left undocumented
62 * since "they are for RFC connoisseurs only" ;) */
63 char prefix[80 * 2];
64 NYD_ENTER;
66 ap->a_input_charset = ap->a_charset = NULL;
68 ap->a_name = file;
69 if ((file = strrchr(file, '/')) != NULL)
70 ++file;
71 else
72 file = ap->a_name;
74 ap->a_content_type = mime_type_by_filename(file);
75 if (number > 0 && ok_blook(attachment_ask_content_type)) {
76 snprintf(prefix, sizeof prefix, "#%u\tContent-Type: ", number);
77 ap->a_content_type = n_input_cp_addhist(prefix, ap->a_content_type, TRU1);
80 if (number > 0 && ok_blook(attachment_ask_content_disposition)) {
81 snprintf(prefix, sizeof prefix, "#%u\tContent-Disposition: ", number);
82 if ((ap->a_content_disposition = n_input_cp_addhist(prefix,
83 ap->a_content_disposition, TRU1)) == NULL)
84 goto jcdis;
85 } else
86 jcdis:
87 ap->a_content_disposition = "attachment";
89 if (number > 0 && ok_blook(attachment_ask_content_id)) {
90 snprintf(prefix, sizeof prefix, "#%u\tContent-ID: ", number);
91 ap->a_content_id = n_input_cp_addhist(prefix, ap->a_content_id, TRU1);
92 } else
93 ap->a_content_id = NULL;
95 if (number > 0 && ok_blook(attachment_ask_content_description)) {
96 snprintf(prefix, sizeof prefix, "#%u\tContent-Description: ", number);
97 ap->a_content_description = n_input_cp_addhist(prefix,
98 ap->a_content_description, TRU1);
100 NYD_LEAVE;
101 return ap;
104 static sigjmp_buf __atticonv_jmp; /* TODO oneday, we won't need it no more */
105 static int volatile __atticonv_sig; /* TODO oneday, we won't need it no more */
106 static void
107 __atticonv_onsig(int sig) /* TODO someday, we won't need it no more */
109 NYD_X; /* Signal handler */
110 __atticonv_sig = sig;
111 siglongjmp(__atticonv_jmp, 1);
114 static struct attachment *
115 _read_attachment_data(struct attachment * volatile ap, ui32_t number)
117 sighandler_type volatile ohdl;
118 char prefix[80 * 2];
119 char const *cslc = NULL/*cc uninit?*/, *cp, *defcs;
120 NYD_ENTER;
122 hold_sigs(); /* TODO until we have signal manager (see TODO) */
123 ohdl = safe_signal(SIGINT, SIG_IGN);
124 __atticonv_sig = 0;
125 if (sigsetjmp(__atticonv_jmp, 1)) {
126 ap = NULL;
127 goto jleave;
129 safe_signal(SIGINT, &__atticonv_onsig);
131 if (ap == NULL)
132 ap = csalloc(1, sizeof *ap);
133 else if (ap->a_msgno) {
134 char *ecp = salloc(24);
135 snprintf(ecp, 24, "#%" PRIu32, (ui32_t)ap->a_msgno);
136 ap->a_msgno = 0;
137 ap->a_content_description = NULL;
138 ap->a_name = ecp;
139 } else if (ap->a_conv == AC_TMPFILE) {
140 Fclose(ap->a_tmpf);
141 DBG( ap->a_tmpf = NULL; )
142 ap->a_conv = AC_DEFAULT;
145 rele_sigs(); /* TODO until we have signal manager (see TODO) */
146 snprintf(prefix, sizeof prefix, _("#%" PRIu32 "\tfilename: "), number);
147 for (;;) {
148 if ((cp = ap->a_name) != NULL)
149 cp = fexpand_nshell_quote(cp);
150 if ((cp = n_input_cp_addhist(prefix, cp, TRU1)) == NULL) {
151 ap->a_name = NULL;
152 ap = NULL;
153 goto jleave;
156 /* May be a message number (XXX add "AC_MSG", use that not .a_msgno) */
157 if (cp[0] == '#') {
158 char *ecp;
159 int msgno = (int)strtol(cp + 1, &ecp, 10);
161 if (msgno > 0 && msgno <= msgCount && *ecp == '\0') {
162 ap->a_name = cp;
163 ap->a_msgno = msgno;
164 ap->a_content_type = ap->a_content_disposition =
165 ap->a_content_id = NULL;
166 ap->a_content_description = _("Attached message content");
167 if (options & OPT_INTERACTIVE)
168 printf(_("~@: added message #%" PRIu32 "\n"), (ui32_t)msgno);
169 goto jleave;
173 if ((cp = fexpand(cp, FEXP_LOCAL | FEXP_NSHELL)) != NULL &&
174 !access(cp, R_OK)) {
175 ap->a_name = cp;
176 break;
178 n_perr(cp, 0);
181 ap = _fill_in(ap, cp, number);
184 * Character set of attachments: enum attach_conv
186 cslc = charset_get_lc();
187 #ifdef HAVE_ICONV
188 if (!(options & OPT_INTERACTIVE))
189 goto jcs;
190 if ((cp = ap->a_content_type) != NULL && ascncasecmp(cp, "text/", 5) != 0 &&
191 !getapproval(_("Filename doesn't indicate text content - "
192 "edit charsets nonetheless? "), TRU1)) {
193 ap->a_conv = AC_DEFAULT;
194 goto jleave;
197 jcs_restart:
198 charset_iter_reset(NULL);
199 jcs:
200 #endif
201 snprintf(prefix, sizeof prefix, _("#%" PRIu32 "\tinput charset: "),
202 number);
203 if ((defcs = ap->a_input_charset) == NULL)
204 defcs = cslc;
205 cp = ap->a_input_charset = n_input_cp_addhist(prefix, defcs, TRU1);
206 #ifdef HAVE_ICONV
207 if (!(options & OPT_INTERACTIVE)) {
208 #endif
209 ap->a_conv = (cp != NULL) ? AC_FIX_INCS : AC_DEFAULT;
210 #ifdef HAVE_ICONV
211 goto jleave;
214 snprintf(prefix, sizeof prefix,
215 _("#%" PRIu32 "\toutput (send) charset: "), number);
216 if ((defcs = ap->a_charset) == NULL)
217 defcs = charset_iter();
218 defcs = ap->a_charset = n_input_cp_addhist(prefix, defcs, TRU1);
220 /* Input, no output -> input=as given, output=no conversion at all */
221 if (cp != NULL && defcs == NULL) {
222 ap->a_conv = AC_FIX_INCS;
223 goto jdone;
226 /* No input, no output -> input=*ttycharset*, output=iterator */
227 if (cp == NULL && defcs == NULL) {
228 ap->a_conv = AC_DEFAULT;
229 ap->a_input_charset = cslc;
230 ap->a_charset = charset_iter();
231 assert(charset_iter_is_valid());
232 charset_iter_next();
234 /* No input, output -> input=*ttycharset*, output=as given */
235 else if (cp == NULL && defcs != NULL) {
236 ap->a_conv = AC_FIX_OUTCS;
237 ap->a_input_charset = cslc;
239 /* Input, output -> try conversion from input=as given to output=as given */
241 printf(_("Trying conversion from %s to %s\n"), ap->a_input_charset,
242 ap->a_charset);
243 if (_attach_iconv(ap))
244 ap->a_conv = AC_TMPFILE;
245 else {
246 ap->a_conv = AC_DEFAULT;
247 ap->a_input_charset = cp;
248 ap->a_charset = defcs;
249 if (!charset_iter_is_valid()) {
250 printf(_("*sendcharsets* and *charset-8bit* iteration "
251 "exhausted, restarting\n"));
252 goto jcs_restart;
254 goto jcs;
256 jdone:
257 #endif
258 if (options & OPT_INTERACTIVE)
259 printf(_("~@: added attachment \"%s\"\n"), ap->a_name);
260 jleave:
261 safe_signal(SIGINT, ohdl);/* TODO until we have signal manager (see TODO) */
262 if (__atticonv_sig != 0) {
263 sigset_t nset;
264 sigemptyset(&nset);
265 sigaddset(&nset, SIGINT);
266 sigprocmask(SIG_UNBLOCK, &nset, NULL);
267 /* Caller kills */
269 NYD_LEAVE;
270 return ap;
273 #ifdef HAVE_ICONV
274 static int
275 _attach_iconv(struct attachment *ap)
277 struct str oul = {NULL, 0}, inl = {NULL, 0};
278 FILE *fo = NULL, *fi = NULL;
279 size_t cnt, lbsize;
280 iconv_t icp;
281 NYD_ENTER;
283 hold_sigs(); /* TODO until we have signal manager (see TODO) */
285 icp = n_iconv_open(ap->a_charset, ap->a_input_charset);
286 if (icp == (iconv_t)-1) {
287 if (errno == EINVAL)
288 goto jeconv;
289 else
290 n_perr(_("iconv_open"), 0);
291 goto jerr;
294 if ((fi = Fopen(ap->a_name, "r")) == NULL) {
295 n_perr(ap->a_name, 0);
296 goto jerr;
298 cnt = fsize(fi);
300 if ((fo = Ftmp(NULL, "atic", OF_RDWR | OF_UNLINK | OF_REGISTER, 0600)) ==
301 NULL) {
302 n_perr(_("temporary mail file"), 0);
303 goto jerr;
306 for (lbsize = 0;;) {
307 if (fgetline(&inl.s, &lbsize, &cnt, &inl.l, fi, 0) == NULL) {
308 if (!cnt)
309 break;
310 n_perr(_("I/O read error occurred"), 0);
311 goto jerr;
314 if (n_iconv_str(icp, &oul, &inl, NULL, FAL0) != 0)
315 goto jeconv;
316 if ((inl.l = fwrite(oul.s, sizeof *oul.s, oul.l, fo)) != oul.l) {
317 n_perr(_("I/O write error occurred"), 0);
318 goto jerr;
321 fflush_rewind(fo);
323 ap->a_tmpf = fo;
324 jleave:
325 if (inl.s != NULL)
326 free(inl.s);
327 if (oul.s != NULL)
328 free(oul.s);
329 if (fi != NULL)
330 Fclose(fi);
331 if (icp != (iconv_t)-1)
332 n_iconv_close(icp);
334 rele_sigs(); /* TODO until we have signal manager (see TODO) */
335 NYD_LEAVE;
336 return (fo != NULL);
338 jeconv:
339 n_err(_("Cannot convert from %s to %s\n"),
340 ap->a_input_charset, ap->a_charset);
341 jerr:
342 if (fo != NULL)
343 Fclose(fo);
344 fo = NULL;
345 goto jleave;
347 #endif /* HAVE_ICONV */
349 /* TODO add_attachment(): also work with **aphead, not *aphead ... */
350 FL struct attachment *
351 add_attachment(struct attachment *aphead, char *file, struct attachment **newap)
353 struct attachment *nap = NULL, *ap;
354 NYD_ENTER;
356 if ((file = fexpand(file, FEXP_LOCAL | FEXP_NSHELL)) == NULL)
357 goto jleave;
358 if (access(file, R_OK) != 0)
359 goto jleave;
361 nap = _fill_in(csalloc(1, sizeof *nap), file, 0);
362 if (newap != NULL)
363 *newap = nap;
364 if (aphead != NULL) {
365 for (ap = aphead; ap->a_flink != NULL; ap = ap->a_flink)
367 ap->a_flink = nap;
368 nap->a_blink = ap;
369 } else {
370 nap->a_blink = NULL;
371 aphead = nap;
373 nap = aphead;
374 jleave:
375 NYD_LEAVE;
376 return nap;
379 FL void
380 append_attachments(struct attachment **aphead, char *names)
382 char *cp;
383 struct attachment *xaph, *nap;
384 NYD_ENTER;
386 while ((cp = n_strsep(&names, ',', 1)) != NULL) {
387 xaph = add_attachment(*aphead, fexpand_nshell_quote(cp), &nap);
388 if (xaph != NULL) {
389 *aphead = xaph;
390 if (options & OPT_INTERACTIVE)
391 printf(_("~@: added attachment \"%s\"\n"), nap->a_name);
392 } else
393 n_perr(cp, 0);
395 NYD_LEAVE;
398 FL void
399 edit_attachments(struct attachment **aphead)
401 struct attachment *ap, *fap, *bap;
402 ui32_t attno = 1;
403 NYD_ENTER;
405 if (!(pstate & PS_ATTACHMENTS_NOTED)) {
406 pstate |= PS_ATTACHMENTS_NOTED;
407 printf(_("# Please be aware that \"\\\" must be escaped, e.g., "
408 "\"\\\\\", \"\\$HOME\"\n"));
411 /* Modify already present ones? */
412 for (ap = *aphead; ap != NULL; ap = fap) {
413 if (_read_attachment_data(ap, attno) != NULL) {
414 fap = ap->a_flink;
415 ++attno;
416 continue;
418 fap = ap->a_flink;
419 if ((bap = ap->a_blink) != NULL)
420 bap->a_flink = fap;
421 else
422 *aphead = fap;
423 if (fap != NULL)
424 fap->a_blink = bap;
425 /*else*//* TODO until we have signal manager (see TODO) */
426 if (__atticonv_sig != 0)
427 n_raise(SIGINT);
428 if (fap == NULL)
429 goto jleave;
432 /* Add some more? */
433 if ((bap = *aphead) != NULL)
434 while (bap->a_flink != NULL)
435 bap = bap->a_flink;
436 while ((fap = _read_attachment_data(NULL, attno)) != NULL) {
437 if (bap != NULL)
438 bap->a_flink = fap;
439 else
440 *aphead = fap;
441 fap->a_blink = bap;
442 fap->a_flink = NULL;
443 bap = fap;
444 ++attno;
446 if (__atticonv_sig != 0) /* TODO until we have signal manager (see TODO) */
447 n_raise(SIGINT);
448 jleave:
449 NYD_LEAVE;
452 /* s-it-mode */