Only support sh(1)-style quoting for some new commands..
[s-mailx.git] / attachments.c
blob762e0b13b0b73415ce18dd6464c7998aca95ac5b
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_classify_filename(file);
75 if (number > 0 && ok_blook(attachment_ask_content_type)) {
76 snprintf(prefix, sizeof prefix, "#%-5" PRIu32 " Content-Type: ", number);
77 ap->a_content_type = n_lex_input_cp_addhist(prefix, ap->a_content_type,
78 TRU1);
81 if (number > 0 && ok_blook(attachment_ask_content_disposition)) {
82 snprintf(prefix, sizeof prefix, "#%-5" PRIu32 " Content-Disposition: ",
83 number);
84 if ((ap->a_content_disposition = n_lex_input_cp_addhist(prefix,
85 ap->a_content_disposition, TRU1)) == NULL)
86 goto jcdis;
87 } else
88 jcdis:
89 ap->a_content_disposition = "attachment";
91 if (number > 0 && ok_blook(attachment_ask_content_id)) {
92 snprintf(prefix, sizeof prefix, "#%-5" PRIu32 " Content-ID: ", number);
93 ap->a_content_id = n_lex_input_cp_addhist(prefix, ap->a_content_id, TRU1);
94 } else
95 ap->a_content_id = NULL;
97 if (number > 0 && ok_blook(attachment_ask_content_description)) {
98 snprintf(prefix, sizeof prefix, "#%-5" PRIu32 " Content-Description: ",
99 number);
100 ap->a_content_description = n_lex_input_cp_addhist(prefix,
101 ap->a_content_description, TRU1);
103 NYD_LEAVE;
104 return ap;
107 static sigjmp_buf __atticonv_jmp; /* TODO oneday, we won't need it no more */
108 static int volatile __atticonv_sig; /* TODO oneday, we won't need it no more */
109 static void
110 __atticonv_onsig(int sig) /* TODO someday, we won't need it no more */
112 NYD_X; /* Signal handler */
113 __atticonv_sig = sig;
114 siglongjmp(__atticonv_jmp, 1);
117 static struct attachment *
118 _read_attachment_data(struct attachment * volatile ap, ui32_t number)
120 sighandler_type volatile ohdl;
121 char prefix[80 * 2];
122 char const *cslc = NULL/*cc uninit?*/, *cp, *defcs;
123 NYD_ENTER;
125 hold_sigs(); /* TODO until we have signal manager (see TODO) */
126 ohdl = safe_signal(SIGINT, SIG_IGN);
127 __atticonv_sig = 0;
128 if (sigsetjmp(__atticonv_jmp, 1)) {
129 ap = NULL;
130 goto jleave;
132 safe_signal(SIGINT, &__atticonv_onsig);
134 if (ap == NULL)
135 ap = csalloc(1, sizeof *ap);
136 else if (ap->a_msgno) {
137 char *ecp = salloc(24);
138 snprintf(ecp, 24, "#%" PRIu32, (ui32_t)ap->a_msgno);
139 ap->a_msgno = 0;
140 ap->a_content_description = NULL;
141 ap->a_name = ecp;
142 } else if (ap->a_conv == AC_TMPFILE) {
143 Fclose(ap->a_tmpf);
144 DBG( ap->a_tmpf = NULL; )
145 ap->a_conv = AC_DEFAULT;
148 rele_sigs(); /* TODO until we have signal manager (see TODO) */
149 snprintf(prefix, sizeof prefix, _("#%-5" PRIu32 " filename: "), number);
150 for (;;) {
151 if ((cp = ap->a_name) != NULL)
152 cp = fexpand_nshell_quote(cp);
153 if ((cp = n_lex_input_cp_addhist(prefix, cp, TRU1)) == NULL) {
154 ap->a_name = NULL;
155 ap = NULL;
156 goto jleave;
159 /* May be a message number (XXX add "AC_MSG", use that not .a_msgno) */
160 if (cp[0] == '#') {
161 char *ecp;
162 int msgno = (int)strtol(cp + 1, &ecp, 10);
164 if (msgno > 0 && msgno <= msgCount && *ecp == '\0') {
165 ap->a_name = cp;
166 ap->a_msgno = msgno;
167 ap->a_content_type = ap->a_content_disposition =
168 ap->a_content_id = NULL;
169 ap->a_content_description = _("Attached message content");
170 if (options & OPT_INTERACTIVE)
171 printf(_("~@: added message #%" PRIu32 "\n"), (ui32_t)msgno);
172 goto jleave;
176 if ((cp = fexpand(cp, FEXP_LOCAL | FEXP_NSHELL)) != NULL &&
177 !access(cp, R_OK)) {
178 ap->a_name = cp;
179 break;
181 n_perr(cp, 0);
184 ap = _fill_in(ap, cp, number);
187 * Character set of attachments: enum attach_conv
189 cslc = charset_get_lc();
190 #ifdef HAVE_ICONV
191 if (!(options & OPT_INTERACTIVE))
192 goto jcs;
193 if ((cp = ap->a_content_type) != NULL && ascncasecmp(cp, "text/", 5) != 0 &&
194 !getapproval(_("File doesn't indicate text content, "
195 "edit character sets"), TRU1)) {
196 ap->a_conv = AC_DEFAULT;
197 goto jleave;
200 jcs_restart:
201 charset_iter_reset(NULL);
202 jcs:
203 #endif
204 snprintf(prefix, sizeof prefix, _("#%-5" PRIu32 " input charset: "),
205 number);
206 if ((defcs = ap->a_input_charset) == NULL)
207 defcs = cslc;
208 cp = ap->a_input_charset = n_lex_input_cp_addhist(prefix, defcs, TRU1);
209 #ifdef HAVE_ICONV
210 if (!(options & OPT_INTERACTIVE)) {
211 #endif
212 ap->a_conv = (cp != NULL) ? AC_FIX_INCS : AC_DEFAULT;
213 #ifdef HAVE_ICONV
214 goto jleave;
217 snprintf(prefix, sizeof prefix,
218 _("#%-5" PRIu32 " output (send) charset: "), number);
219 if ((defcs = ap->a_charset) == NULL)
220 defcs = charset_iter();
221 defcs = ap->a_charset = n_lex_input_cp_addhist(prefix, defcs, TRU1);
223 /* Input, no output -> input=as given, output=no conversion at all */
224 if (cp != NULL && defcs == NULL) {
225 ap->a_conv = AC_FIX_INCS;
226 goto jdone;
229 /* No input, no output -> input=*ttycharset*, output=iterator */
230 if (cp == NULL && defcs == NULL) {
231 ap->a_conv = AC_DEFAULT;
232 ap->a_input_charset = cslc;
233 ap->a_charset = charset_iter();
234 assert(charset_iter_is_valid());
235 charset_iter_next();
237 /* No input, output -> input=*ttycharset*, output=as given */
238 else if (cp == NULL && defcs != NULL) {
239 ap->a_conv = AC_FIX_OUTCS;
240 ap->a_input_charset = cslc;
242 /* Input, output -> try conversion from input=as given to output=as given */
244 printf(_("Trying conversion from %s to %s\n"), ap->a_input_charset,
245 ap->a_charset);
246 if (_attach_iconv(ap))
247 ap->a_conv = AC_TMPFILE;
248 else {
249 ap->a_conv = AC_DEFAULT;
250 ap->a_input_charset = cp;
251 ap->a_charset = defcs;
252 if (!charset_iter_is_valid()) {
253 printf(_("*sendcharsets* and *charset-8bit* iteration "
254 "exhausted, restarting\n"));
255 goto jcs_restart;
257 goto jcs;
259 jdone:
260 #endif
261 if (options & OPT_INTERACTIVE)
262 printf(_("~@: added attachment \"%s\"\n"), ap->a_name);
263 jleave:
264 safe_signal(SIGINT, ohdl);/* TODO until we have signal manager (see TODO) */
265 if (__atticonv_sig != 0) {
266 sigset_t nset;
267 sigemptyset(&nset);
268 sigaddset(&nset, SIGINT);
269 sigprocmask(SIG_UNBLOCK, &nset, NULL);
270 /* Caller kills */
272 NYD_LEAVE;
273 return ap;
276 #ifdef HAVE_ICONV
277 static int
278 _attach_iconv(struct attachment *ap)
280 struct str oul = {NULL, 0}, inl = {NULL, 0};
281 FILE *fo = NULL, *fi = NULL;
282 size_t cnt, lbsize;
283 iconv_t icp;
284 NYD_ENTER;
286 hold_sigs(); /* TODO until we have signal manager (see TODO) */
288 icp = n_iconv_open(ap->a_charset, ap->a_input_charset);
289 if (icp == (iconv_t)-1) {
290 if (errno == EINVAL)
291 goto jeconv;
292 else
293 n_perr(_("iconv_open"), 0);
294 goto jerr;
297 if ((fi = Fopen(ap->a_name, "r")) == NULL) {
298 n_perr(ap->a_name, 0);
299 goto jerr;
301 cnt = fsize(fi);
303 if ((fo = Ftmp(NULL, "aticonv", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
304 NULL) {
305 n_perr(_("temporary mail file"), 0);
306 goto jerr;
309 for (lbsize = 0;;) {
310 if (fgetline(&inl.s, &lbsize, &cnt, &inl.l, fi, 0) == NULL) {
311 if (!cnt)
312 break;
313 n_perr(_("I/O read error occurred"), 0);
314 goto jerr;
317 if (n_iconv_str(icp, &oul, &inl, NULL, FAL0) != 0)
318 goto jeconv;
319 if ((inl.l = fwrite(oul.s, sizeof *oul.s, oul.l, fo)) != oul.l) {
320 n_perr(_("I/O write error occurred"), 0);
321 goto jerr;
324 fflush_rewind(fo);
326 ap->a_tmpf = fo;
327 jleave:
328 if (inl.s != NULL)
329 free(inl.s);
330 if (oul.s != NULL)
331 free(oul.s);
332 if (fi != NULL)
333 Fclose(fi);
334 if (icp != (iconv_t)-1)
335 n_iconv_close(icp);
337 rele_sigs(); /* TODO until we have signal manager (see TODO) */
338 NYD_LEAVE;
339 return (fo != NULL);
341 jeconv:
342 n_err(_("Cannot convert from %s to %s\n"),
343 ap->a_input_charset, ap->a_charset);
344 jerr:
345 if (fo != NULL)
346 Fclose(fo);
347 fo = NULL;
348 goto jleave;
350 #endif /* HAVE_ICONV */
352 /* TODO add_attachment(): also work with **aphead, not *aphead ... */
353 FL struct attachment *
354 add_attachment(struct attachment *aphead, char *file, struct attachment **newap)
356 struct attachment *nap = NULL, *ap;
357 NYD_ENTER;
359 if ((file = fexpand(file, FEXP_LOCAL | FEXP_NSHELL)) == NULL)
360 goto jleave;
361 if (access(file, R_OK) != 0)
362 goto jleave;
364 nap = _fill_in(csalloc(1, sizeof *nap), file, 0);
365 if (newap != NULL)
366 *newap = nap;
367 if (aphead != NULL) {
368 for (ap = aphead; ap->a_flink != NULL; ap = ap->a_flink)
370 ap->a_flink = nap;
371 nap->a_blink = ap;
372 } else {
373 nap->a_blink = NULL;
374 aphead = nap;
376 nap = aphead;
377 jleave:
378 NYD_LEAVE;
379 return nap;
382 FL void
383 append_attachments(struct attachment **aphead, char *names)
385 char *cp;
386 struct attachment *xaph, *nap;
387 NYD_ENTER;
389 while ((cp = n_strescsep(&names, ',', TRU1)) != NULL) {
390 xaph = add_attachment(*aphead, fexpand_nshell_quote(cp), &nap);
391 if (xaph != NULL) {
392 *aphead = xaph;
393 if (options & OPT_INTERACTIVE)
394 printf(_("~@: added attachment \"%s\"\n"), nap->a_name);
395 } else
396 n_perr(cp, 0);
398 NYD_LEAVE;
401 FL void
402 edit_attachments(struct attachment **aphead)
404 struct attachment *ap, *fap, *bap;
405 ui32_t attno = 1;
406 NYD_ENTER;
408 if (!(pstate & PS_ATTACHMENTS_NOTED)) {
409 pstate |= PS_ATTACHMENTS_NOTED;
410 printf(_("# Please be aware that \"\\\" must be escaped, e.g., "
411 "\"\\\\\", \"\\$HOME\"\n"));
414 /* Modify already present ones? */
415 for (ap = *aphead; ap != NULL; ap = fap) {
416 if (_read_attachment_data(ap, attno) != NULL) {
417 fap = ap->a_flink;
418 ++attno;
419 continue;
421 fap = ap->a_flink;
422 if ((bap = ap->a_blink) != NULL)
423 bap->a_flink = fap;
424 else
425 *aphead = fap;
426 if (fap != NULL)
427 fap->a_blink = bap;
428 /*else*//* TODO until we have signal manager (see TODO) */
429 if (__atticonv_sig != 0)
430 n_raise(SIGINT);
431 if (fap == NULL)
432 goto jleave;
435 /* Add some more? */
436 if ((bap = *aphead) != NULL)
437 while (bap->a_flink != NULL)
438 bap = bap->a_flink;
439 while ((fap = _read_attachment_data(NULL, attno)) != NULL) {
440 if (bap != NULL)
441 bap->a_flink = fap;
442 else
443 *aphead = fap;
444 fap->a_blink = bap;
445 fap->a_flink = NULL;
446 bap = fap;
447 ++attno;
449 if (__atticonv_sig != 0) /* TODO until we have signal manager (see TODO) */
450 n_raise(SIGINT);
451 jleave:
452 NYD_LEAVE;
455 /* s-it-mode */