Add OPT_ALWAYS_UNICODE_LOCALE (Predrag Punosevac)..
[s-mailx.git] / attachments.c
blobd6858046638926d7230fa4dc1975de49d73f7c01
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 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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(enum n_lexinput_flags lif,
47 struct attachment *ap, char const *file,
48 ui32_t number);
50 /* Ask the user to edit file names and other data for the given attachment */
51 static struct attachment * _read_attachment_data(enum n_lexinput_flags lif,
52 struct attachment *ap, ui32_t number);
54 /* Try to create temporary charset converted version */
55 #ifdef HAVE_ICONV
56 static int _attach_iconv(struct attachment *ap);
57 #endif
59 static struct attachment *
60 _fill_in(enum n_lexinput_flags lif, struct attachment *ap, char const *file,
61 ui32_t number)
63 /* XXX The "attachment-ask-content-*" variables are left undocumented
64 * since "they are for RFC connoisseurs only" ;) */
65 char prefix[80 * 2];
66 NYD_ENTER;
68 ap->a_input_charset = ap->a_charset = NULL;
70 ap->a_name = file;
71 if ((file = strrchr(file, '/')) != NULL)
72 ++file;
73 else
74 file = ap->a_name;
76 ap->a_content_type = mime_type_classify_filename(file);
77 if (number > 0 && ok_blook(attachment_ask_content_type)) {
78 snprintf(prefix, sizeof prefix, "#%-5" PRIu32 " Content-Type: ", number);
79 ap->a_content_type = n_lex_input_cp(lif, prefix, ap->a_content_type);
82 if (number > 0 && ok_blook(attachment_ask_content_disposition)) {
83 snprintf(prefix, sizeof prefix, "#%-5" PRIu32 " Content-Disposition: ",
84 number);
85 if ((ap->a_content_disposition = n_lex_input_cp(lif, prefix,
86 ap->a_content_disposition)) == NULL)
87 goto jcdis;
88 } else
89 jcdis:
90 ap->a_content_disposition = "attachment";
92 if (number > 0 && ok_blook(attachment_ask_content_id)) {
93 snprintf(prefix, sizeof prefix, "#%-5" PRIu32 " Content-ID: ", number);
94 ap->a_content_id = n_lex_input_cp(lif, prefix, ap->a_content_id);
95 } else
96 ap->a_content_id = NULL;
98 if (number > 0 && ok_blook(attachment_ask_content_description)) {
99 snprintf(prefix, sizeof prefix, "#%-5" PRIu32 " Content-Description: ",
100 number);
101 ap->a_content_description = n_lex_input_cp(lif, prefix,
102 ap->a_content_description);
104 NYD_LEAVE;
105 return ap;
108 static sigjmp_buf __atticonv_jmp; /* TODO oneday, we won't need it no more */
109 static int volatile __atticonv_sig; /* TODO oneday, we won't need it no more */
110 static void
111 __atticonv_onsig(int sig) /* TODO someday, we won't need it no more */
113 NYD_X; /* Signal handler */
114 __atticonv_sig = sig;
115 siglongjmp(__atticonv_jmp, 1);
118 static struct attachment *
119 _read_attachment_data(enum n_lexinput_flags lif,
120 struct attachment * volatile ap, ui32_t number)
122 sighandler_type volatile ohdl;
123 char prefix[80 * 2];
124 struct str shin;
125 struct n_string shou, *shoup;
126 char const *cslc, *cp, *defcs;
127 NYD_ENTER;
129 UNINIT(cslc, NULL);
130 shoup = n_string_creat(&shou);
132 hold_sigs(); /* TODO until we have signal manager (see TODO) */
133 ohdl = safe_signal(SIGINT, SIG_IGN);
134 __atticonv_sig = 0;
135 if (sigsetjmp(__atticonv_jmp, 1)) {
136 ap = NULL;
137 goto jleave;
139 safe_signal(SIGINT, &__atticonv_onsig);
141 if (ap == NULL)
142 ap = csalloc(1, sizeof *ap);
143 else if (ap->a_msgno) {
144 char *ecp = salloc(24);
145 snprintf(ecp, 24, "#%" PRIu32, (ui32_t)ap->a_msgno);
146 ap->a_msgno = 0;
147 ap->a_content_description = NULL;
148 ap->a_name = ecp;
149 } else if (ap->a_conv == AC_TMPFILE) {
150 Fclose(ap->a_tmpf);
151 DBG( ap->a_tmpf = NULL; )
152 ap->a_conv = AC_DEFAULT;
155 rele_sigs(); /* TODO until we have signal manager (see TODO) */
156 snprintf(prefix, sizeof prefix, _("#%-5" PRIu32 " filename: "), number);
157 for (;;) {
158 if ((cp = ap->a_name) != NULL)
159 cp = n_shexp_quote_cp(cp, FAL0);
160 if ((cp = n_lex_input_cp(lif, prefix, cp)) == NULL) {
161 ap->a_name = NULL;
162 ap = NULL;
163 goto jleave;
166 shin.s = UNCONST(cp);
167 shin.l = UIZ_MAX;
168 if((n_shexp_parse_token(shoup, &shin, n_SHEXP_PARSE_TRUNC |
169 n_SHEXP_PARSE_TRIMSPACE | n_SHEXP_PARSE_LOG |
170 n_SHEXP_PARSE_IGNORE_EMPTY) &
171 (n_SHEXP_STATE_OUTPUT | n_SHEXP_STATE_ERR_MASK)) !=
172 n_SHEXP_STATE_OUTPUT)
173 continue;
174 if(shin.l != 0)
175 continue;
176 cp = n_string_cp(shoup);
178 /* May be a message number (XXX add "AC_MSG", use that not .a_msgno) */
179 if (cp[0] == '#') {
180 char *ecp;
181 int msgno = (int)strtol(cp + 1, &ecp, 10);
183 if (msgno > 0 && msgno <= msgCount && *ecp == '\0') {
184 ap->a_name = cp;
185 ap->a_msgno = msgno;
186 ap->a_content_type = ap->a_content_disposition =
187 ap->a_content_id = NULL;
188 ap->a_content_description = _("Attached message content");
189 if (options & OPT_INTERACTIVE)
190 printf(_("~@: added message #%" PRIu32 "\n"), (ui32_t)msgno);
191 goto jleave;
195 if ((cp = fexpand(cp, FEXP_LOCAL | FEXP_NVAR)) != NULL &&
196 !access(cp, R_OK)) {
197 ap->a_name = cp;
198 break;
200 n_perr(cp, 0);
203 ap = _fill_in(lif, ap, cp, number);
206 * Character set of attachments: enum attach_conv
208 cslc = charset_get_lc();
209 #ifdef HAVE_ICONV
210 if (!(options & OPT_INTERACTIVE))
211 goto jcs;
212 if ((cp = ap->a_content_type) != NULL && ascncasecmp(cp, "text/", 5) != 0 &&
213 !getapproval(_("File doesn't indicate text content, "
214 "edit character sets"), TRU1)) {
215 ap->a_conv = AC_DEFAULT;
216 goto jleave;
219 jcs_restart:
220 charset_iter_reset(NULL);
221 jcs:
222 #endif
223 snprintf(prefix, sizeof prefix, _("#%-5" PRIu32 " input charset: "),
224 number);
225 if ((defcs = ap->a_input_charset) == NULL)
226 defcs = cslc;
227 cp = ap->a_input_charset = n_lex_input_cp(lif, prefix, defcs);
228 #ifdef HAVE_ICONV
229 if (!(options & OPT_INTERACTIVE)) {
230 #endif
231 ap->a_conv = (cp != NULL) ? AC_FIX_INCS : AC_DEFAULT;
232 #ifdef HAVE_ICONV
233 goto jleave;
236 snprintf(prefix, sizeof prefix,
237 _("#%-5" PRIu32 " output (send) charset: "), number);
238 if ((defcs = ap->a_charset) == NULL)
239 defcs = charset_iter();
240 defcs = ap->a_charset = n_lex_input_cp(lif, prefix, defcs);
242 /* Input, no output -> input=as given, output=no conversion at all */
243 if (cp != NULL && defcs == NULL) {
244 ap->a_conv = AC_FIX_INCS;
245 goto jdone;
248 /* No input, no output -> input=*ttycharset*, output=iterator */
249 if (cp == NULL && defcs == NULL) {
250 ap->a_conv = AC_DEFAULT;
251 ap->a_input_charset = cslc;
252 ap->a_charset = charset_iter();
253 assert(charset_iter_is_valid());
254 charset_iter_next();
256 /* No input, output -> input=*ttycharset*, output=as given */
257 else if (cp == NULL && defcs != NULL) {
258 ap->a_conv = AC_FIX_OUTCS;
259 ap->a_input_charset = cslc;
261 /* Input, output -> try conversion from input=as given to output=as given */
263 printf(_("Trying conversion from %s to %s\n"), ap->a_input_charset,
264 ap->a_charset);
265 if (_attach_iconv(ap))
266 ap->a_conv = AC_TMPFILE;
267 else {
268 ap->a_conv = AC_DEFAULT;
269 ap->a_input_charset = cp;
270 ap->a_charset = defcs;
271 if (!charset_iter_is_valid()) {
272 printf(_("*sendcharsets* and *charset-8bit* iteration "
273 "exhausted, restarting\n"));
274 goto jcs_restart;
276 goto jcs;
278 jdone:
279 #endif
280 if (options & OPT_INTERACTIVE)
281 printf(_("Added attachment %s\n"), n_shexp_quote_cp(ap->a_name, FAL0));
282 jleave:
283 n_string_gut(shoup);
285 safe_signal(SIGINT, ohdl);/* TODO until we have signal manager (see TODO) */
286 if (__atticonv_sig != 0) {
287 sigset_t nset;
288 sigemptyset(&nset);
289 sigaddset(&nset, SIGINT);
290 sigprocmask(SIG_UNBLOCK, &nset, NULL);
291 /* Caller kills */
293 NYD_LEAVE;
294 return ap;
297 #ifdef HAVE_ICONV
298 static int
299 _attach_iconv(struct attachment *ap)
301 struct str oul = {NULL, 0}, inl = {NULL, 0};
302 FILE *fo = NULL, *fi = NULL;
303 size_t cnt, lbsize;
304 iconv_t icp;
305 NYD_ENTER;
307 hold_sigs(); /* TODO until we have signal manager (see TODO) */
309 icp = n_iconv_open(ap->a_charset, ap->a_input_charset);
310 if (icp == (iconv_t)-1) {
311 if (errno == EINVAL)
312 goto jeconv;
313 else
314 n_perr(_("iconv_open"), 0);
315 goto jerr;
318 if ((fi = Fopen(ap->a_name, "r")) == NULL) {
319 n_perr(ap->a_name, 0);
320 goto jerr;
322 cnt = (size_t)fsize(fi);
324 if ((fo = Ftmp(NULL, "aticonv", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
325 NULL) {
326 n_perr(_("temporary mail file"), 0);
327 goto jerr;
330 for (lbsize = 0;;) {
331 if (fgetline(&inl.s, &lbsize, &cnt, &inl.l, fi, 0) == NULL) {
332 if (!cnt)
333 break;
334 n_perr(_("I/O read error occurred"), 0);
335 goto jerr;
338 if (n_iconv_str(icp, n_ICONV_IGN_NOREVERSE, &oul, &inl, NULL) != 0)
339 goto jeconv;
340 if ((inl.l = fwrite(oul.s, sizeof *oul.s, oul.l, fo)) != oul.l) {
341 n_perr(_("I/O write error occurred"), 0);
342 goto jerr;
345 fflush_rewind(fo);
347 ap->a_tmpf = fo;
348 jleave:
349 if (inl.s != NULL)
350 free(inl.s);
351 if (oul.s != NULL)
352 free(oul.s);
353 if (fi != NULL)
354 Fclose(fi);
355 if (icp != (iconv_t)-1)
356 n_iconv_close(icp);
358 rele_sigs(); /* TODO until we have signal manager (see TODO) */
359 NYD_LEAVE;
360 return (fo != NULL);
362 jeconv:
363 n_err(_("Cannot convert from %s to %s\n"),
364 ap->a_input_charset, ap->a_charset);
365 jerr:
366 if (fo != NULL)
367 Fclose(fo);
368 fo = NULL;
369 goto jleave;
371 #endif /* HAVE_ICONV */
373 /* TODO add_attachment(): also work with **aphead, not *aphead ... */
374 FL struct attachment *
375 add_attachment(enum n_lexinput_flags lif, struct attachment *aphead,
376 char *file, struct attachment **newap)
378 struct attachment *nap = NULL, *ap;
379 NYD_ENTER;
381 if ((file = fexpand(file, FEXP_LOCAL | FEXP_NVAR)) == NULL)
382 goto jleave;
383 if (access(file, R_OK) != 0)
384 goto jleave;
386 nap = _fill_in(lif, csalloc(1, sizeof *nap), file, 0);
387 if (newap != NULL)
388 *newap = nap;
389 if (aphead != NULL) {
390 for (ap = aphead; ap->a_flink != NULL; ap = ap->a_flink)
392 ap->a_flink = nap;
393 nap->a_blink = ap;
394 } else {
395 nap->a_blink = NULL;
396 aphead = nap;
398 nap = aphead;
399 jleave:
400 NYD_LEAVE;
401 return nap;
404 FL void
405 append_attachments(enum n_lexinput_flags lif, struct attachment **aphead,
406 char *names){
407 struct str shin;
408 struct n_string shou, *shoup;
409 NYD_ENTER;
411 shoup = n_string_creat_auto(&shou);
413 for(shin.s = names, shin.l = UIZ_MAX;;){
414 struct attachment *xaph, *nap;
415 enum n_shexp_state shs;
417 shs = n_shexp_parse_token(shoup, &shin, n_SHEXP_PARSE_TRUNC |
418 n_SHEXP_PARSE_TRIMSPACE | n_SHEXP_PARSE_LOG |
419 n_SHEXP_PARSE_IFS_ADD_COMMA | n_SHEXP_PARSE_IGNORE_EMPTY);
420 if(shs & n_SHEXP_STATE_ERR_MASK)
421 break;
423 if(shs & n_SHEXP_STATE_OUTPUT){
424 if((xaph = add_attachment(lif, *aphead, n_string_cp(shoup), &nap)
425 ) != NULL){
426 *aphead = xaph;
427 if(options & OPT_INTERACTIVE)
428 printf(_("Added attachment %s\n"),
429 n_shexp_quote_cp(nap->a_name, FAL0));
430 }else
431 n_perr(n_string_cp(shoup), 0);
434 if(shs & n_SHEXP_STATE_STOP)
435 break;
437 n_string_gut(shoup);
438 NYD_LEAVE;
441 FL void
442 edit_attachments(enum n_lexinput_flags lif, struct attachment **aphead)
444 struct attachment *ap, *fap, *bap;
445 ui32_t attno = 1;
446 NYD_ENTER;
448 /* C99 */{
449 static bool_t note_given; /* v15-compat: DROP */
451 if(!note_given){
452 note_given = TRU1;
453 printf(_("# Only supports sh(1)ell-style quoting for file names\n"));
457 /* Modify already present ones? */
458 for (ap = *aphead; ap != NULL; ap = fap) {
459 if (_read_attachment_data(lif, ap, attno) != NULL) {
460 fap = ap->a_flink;
461 ++attno;
462 continue;
464 fap = ap->a_flink;
465 if ((bap = ap->a_blink) != NULL)
466 bap->a_flink = fap;
467 else
468 *aphead = fap;
469 if (fap != NULL)
470 fap->a_blink = bap;
471 /*else*//* TODO until we have signal manager (see TODO) */
472 if (__atticonv_sig != 0)
473 n_raise(SIGINT);
474 if (fap == NULL)
475 goto jleave;
478 /* Add some more? */
479 if ((bap = *aphead) != NULL)
480 while (bap->a_flink != NULL)
481 bap = bap->a_flink;
482 while ((fap = _read_attachment_data(lif, NULL, attno)) != NULL) {
483 if (bap != NULL)
484 bap->a_flink = fap;
485 else
486 *aphead = fap;
487 fap->a_blink = bap;
488 fap->a_flink = NULL;
489 bap = fap;
490 ++attno;
492 if (__atticonv_sig != 0) /* TODO until we have signal manager (see TODO) */
493 n_raise(SIGINT);
494 jleave:
495 NYD_LEAVE;
498 /* s-it-mode */