Fix URL handling and more (Gavin Troy, Ypnose, Karol Blazewicz)..
[s-mailx.git] / attachments.c
blobe172922ce9d666e145716d6ebd8059c20d306982
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 - 2014 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, ui32_t 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 ui32_t 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, 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_classify_content_type_by_fileext(file);
77 if (number > 0 && ok_blook(attachment_ask_content_type)) {
78 snprintf(prefix, sizeof prefix, "#%u\tContent-Type: ", number);
79 ap->a_content_type = readstr_input(prefix, ap->a_content_type);
82 if (number > 0 && ok_blook(attachment_ask_content_disposition)) {
83 snprintf(prefix, sizeof prefix, "#%u\tContent-Disposition: ", number);
84 if ((ap->a_content_disposition = readstr_input(prefix,
85 ap->a_content_disposition)) == 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, "#%u\tContent-ID: ", number);
93 ap->a_content_id = readstr_input(prefix, ap->a_content_id);
94 } else
95 ap->a_content_id = NULL;
97 if (number > 0 && ok_blook(attachment_ask_content_description)) {
98 snprintf(prefix, sizeof prefix, "#%u\tContent-Description: ", number);
99 ap->a_content_description = readstr_input(prefix,
100 ap->a_content_description);
102 NYD_LEAVE;
103 return ap;
106 static sigjmp_buf __atticonv_jmp; /* TODO someday, we won't need it no more */
107 static int __atticonv_sig; /* TODO someday, we won't need it no more */
108 static void
109 __atticonv_onsig(int sig) /* TODO someday, we won't need it no more */
111 NYD_X; /* Signal handler */
112 __atticonv_sig = sig;
113 siglongjmp(__atticonv_jmp, 1);
116 static struct attachment *
117 _read_attachment_data(struct attachment * volatile ap, ui32_t number)
119 sighandler_type volatile ohdl;
120 char prefix[80 * 2];
121 char const *cslc = NULL/*cc uninit?*/, *cp, *defcs;
122 NYD_ENTER;
124 hold_sigs(); /* TODO until we have signal manager (see TODO) */
125 ohdl = safe_signal(SIGINT, SIG_IGN);
126 __atticonv_sig = 0;
127 if (sigsetjmp(__atticonv_jmp, 1)) {
128 ap = NULL;
129 goto jleave;
131 safe_signal(SIGINT, &__atticonv_onsig);
133 if (ap == NULL)
134 ap = csalloc(1, sizeof *ap);
135 else if (ap->a_msgno) {
136 char *ecp = salloc(24);
137 snprintf(ecp, 24, "#%u", (ui_it)ap->a_msgno);
138 ap->a_msgno = 0;
139 ap->a_content_description = NULL;
140 ap->a_name = ecp;
141 } else if (ap->a_conv == AC_TMPFILE) {
142 Fclose(ap->a_tmpf);
143 DBG( ap->a_tmpf = NULL; )
144 ap->a_conv = AC_DEFAULT;
147 rele_sigs(); /* TODO until we have signal manager (see TODO) */
148 snprintf(prefix, sizeof prefix, tr(50, "#%u\tfilename: "), number);
149 for (;;) {
150 if ((ap->a_name = readstr_input(prefix, ap->a_name)) == NULL) {
151 ap = NULL;
152 goto jleave;
155 /* May be a message number (XXX add "AC_MSG", use that not .a_msgno) */
156 if (ap->a_name[0] == '#') {
157 char *ecp;
158 int msgno = (int)strtol(ap->a_name + 1, &ecp, 10);
159 if (msgno > 0 && msgno <= msgCount && *ecp == '\0') {
160 ap->a_msgno = msgno;
161 ap->a_content_type = ap->a_content_disposition =
162 ap->a_content_id = NULL;
163 ap->a_content_description = tr(513, "Attached message content");
164 if (options & OPT_INTERACTIVE)
165 printf(tr(2, "~@: added message #%u\n"), msgno);
166 goto jleave;
170 if ((cp = file_expand(ap->a_name)) != NULL && !access(cp, R_OK)) {
171 ap->a_name = cp;
172 break;
174 perror(cp);
177 ap = _fill_in(ap, cp, number);
180 * Character set of attachments: enum attach_conv
182 cslc = charset_get_lc();
183 #ifdef HAVE_ICONV
184 if (!(options & OPT_INTERACTIVE))
185 goto jcs;
186 if ((cp = ap->a_content_type) != NULL && ascncasecmp(cp, "text/", 5) != 0 &&
187 !getapproval(tr(162, "Filename doesn't indicate text content - "
188 "edit charsets nonetheless? "), TRU1)) {
189 ap->a_conv = AC_DEFAULT;
190 goto jleave;
193 jcs_restart:
194 charset_iter_reset(NULL);
195 jcs:
196 #endif
197 snprintf(prefix, sizeof prefix, tr(160, "#%u\tinput charset: "), number);
198 if ((defcs = ap->a_input_charset) == NULL)
199 defcs = cslc;
200 cp = ap->a_input_charset = readstr_input(prefix, defcs);
201 #ifdef HAVE_ICONV
202 if (!(options & OPT_INTERACTIVE)) {
203 #endif
204 ap->a_conv = (cp != NULL) ? AC_FIX_INCS : AC_DEFAULT;
205 #ifdef HAVE_ICONV
206 goto jleave;
209 snprintf(prefix, sizeof prefix, tr(161, "#%u\toutput (send) charset: "),
210 number);
211 if ((defcs = ap->a_charset) == NULL)
212 defcs = charset_iter();
213 defcs = ap->a_charset = readstr_input(prefix, defcs);
215 /* Input, no output -> input=as given, output=no conversion at all */
216 if (cp != NULL && defcs == NULL) {
217 ap->a_conv = AC_FIX_INCS;
218 goto jdone;
221 /* No input, no output -> input=*ttycharset*, output=iterator */
222 if (cp == NULL && defcs == NULL) {
223 ap->a_conv = AC_DEFAULT;
224 ap->a_input_charset = cslc;
225 ap->a_charset = charset_iter();
226 assert(charset_iter_is_valid());
227 charset_iter_next();
229 /* No input, output -> input=*ttycharset*, output=as given */
230 else if (cp == NULL && defcs != NULL) {
231 ap->a_conv = AC_FIX_OUTCS;
232 ap->a_input_charset = cslc;
234 /* Input, output -> try conversion from input=as given to output=as given */
236 printf(tr(197, "Trying conversion from %s to %s\n"), ap->a_input_charset,
237 ap->a_charset);
238 if (_attach_iconv(ap))
239 ap->a_conv = AC_TMPFILE;
240 else {
241 ap->a_conv = AC_DEFAULT;
242 ap->a_input_charset = cp;
243 ap->a_charset = defcs;
244 if (!charset_iter_is_valid()) {
245 printf(tr(572, "*sendcharsets* and *charset-8bit* iteration "
246 "exhausted, restarting\n"));
247 goto jcs_restart;
249 goto jcs;
251 jdone:
252 #endif
253 if (options & OPT_INTERACTIVE)
254 printf(tr(19, "~@: added attachment \"%s\"\n"), ap->a_name);
255 jleave:
256 safe_signal(SIGINT, ohdl);/* TODO until we have signal manager (see TODO) */
257 if (__atticonv_sig != 0) {
258 sigset_t nset;
259 sigemptyset(&nset);
260 sigaddset(&nset, SIGINT);
261 sigprocmask(SIG_UNBLOCK, &nset, NULL);
262 /* Caller kills */
264 NYD_LEAVE;
265 return ap;
268 #ifdef HAVE_ICONV
269 static int
270 _attach_iconv(struct attachment *ap)
272 struct str oul = {NULL, 0}, inl = {NULL, 0};
273 FILE *fo = NULL, *fi = NULL;
274 size_t cnt, lbsize;
275 iconv_t icp;
276 NYD_ENTER;
278 hold_sigs(); /* TODO until we have signal manager (see TODO) */
280 icp = n_iconv_open(ap->a_charset, ap->a_input_charset);
281 if (icp == (iconv_t)-1) {
282 if (errno == EINVAL)
283 goto jeconv;
284 else
285 perror("iconv_open");
286 goto jerr;
289 if ((fi = Fopen(ap->a_name, "r")) == NULL) {
290 perror(ap->a_name);
291 goto jerr;
293 cnt = fsize(fi);
295 if ((fo = Ftmp(NULL, "atic", OF_RDWR | OF_UNLINK | OF_REGISTER, 0600)) ==
296 NULL) {
297 perror(tr(51, "temporary mail file"));
298 goto jerr;
301 for (lbsize = 0;;) {
302 if (fgetline(&inl.s, &lbsize, &cnt, &inl.l, fi, 0) == NULL) {
303 if (!cnt)
304 break;
305 perror(tr(195, "I/O read error occurred"));
306 goto jerr;
309 if (n_iconv_str(icp, &oul, &inl, NULL, FAL0) != 0)
310 goto jeconv;
311 if ((inl.l = fwrite(oul.s, sizeof *oul.s, oul.l, fo)) != oul.l) {
312 perror(tr(196, "I/O write error occurred"));
313 goto jerr;
316 fflush_rewind(fo);
318 ap->a_tmpf = fo;
319 jleave:
320 if (inl.s != NULL)
321 free(inl.s);
322 if (oul.s != NULL)
323 free(oul.s);
324 if (fi != NULL)
325 Fclose(fi);
326 if (icp != (iconv_t)-1)
327 n_iconv_close(icp);
329 rele_sigs(); /* TODO until we have signal manager (see TODO) */
330 NYD_LEAVE;
331 return (fo != NULL);
333 jeconv:
334 fprintf(stderr, tr(179, "Cannot convert from %s to %s\n"),
335 ap->a_input_charset, ap->a_charset);
336 jerr:
337 if (fo != NULL)
338 Fclose(fo);
339 fo = NULL;
340 goto jleave;
342 #endif /* HAVE_ICONV */
344 /* TODO add_attachment(): also work with **aphead, not *aphead ... */
345 FL struct attachment *
346 add_attachment(struct attachment *aphead, char *file, struct attachment **newap)
348 struct attachment *nap = NULL, *ap;
349 NYD_ENTER;
351 if ((file = file_expand(file)) == NULL)
352 goto jleave;
353 if (access(file, R_OK) != 0)
354 goto jleave;
356 nap = _fill_in(csalloc(1, sizeof *nap), file, 0);
357 if (newap != NULL)
358 *newap = nap;
359 if (aphead != NULL) {
360 for (ap = aphead; ap->a_flink != NULL; ap = ap->a_flink)
362 ap->a_flink = nap;
363 nap->a_blink = ap;
364 } else {
365 nap->a_blink = NULL;
366 aphead = nap;
368 nap = aphead;
369 jleave:
370 NYD_LEAVE;
371 return nap;
374 FL void
375 append_attachments(struct attachment **aphead, char *names)
377 char *cp;
378 struct attachment *xaph, *nap;
379 NYD_ENTER;
381 while ((cp = n_strsep(&names, ',', 1)) != NULL) {
382 if ((xaph = add_attachment(*aphead, cp, &nap)) != NULL) {
383 *aphead = xaph;
384 if (options & OPT_INTERACTIVE)
385 printf(tr(19, "~@: added attachment \"%s\"\n"), nap->a_name);
386 } else
387 perror(cp);
389 NYD_LEAVE;
392 FL void
393 edit_attachments(struct attachment **aphead)
395 struct attachment *ap, *fap, *bap;
396 ui32_t attno = 1;
397 NYD_ENTER;
399 /* Modify already present ones? */
400 for (ap = *aphead; ap != NULL; ap = fap) {
401 if (_read_attachment_data(ap, attno) != NULL) {
402 fap = ap->a_flink;
403 ++attno;
404 continue;
406 fap = ap->a_flink;
407 if ((bap = ap->a_blink) != NULL)
408 bap->a_flink = fap;
409 else
410 *aphead = fap;
411 if (fap != NULL)
412 fap->a_blink = bap;
413 /*else*//* TODO until we have signal manager (see TODO) */
414 if (__atticonv_sig != 0)
415 kill(0, SIGINT);
416 if (fap == NULL)
417 goto jleave;
420 /* Add some more? */
421 if ((bap = *aphead) != NULL)
422 while (bap->a_flink != NULL)
423 bap = bap->a_flink;
424 while ((fap = _read_attachment_data(NULL, attno)) != NULL) {
425 if (bap != NULL)
426 bap->a_flink = fap;
427 else
428 *aphead = fap;
429 fap->a_blink = bap;
430 fap->a_flink = NULL;
431 bap = fap;
432 ++attno;
434 if (__atticonv_sig != 0) /* TODO until we have signal manager (see TODO) */
435 kill(0, SIGINT);
436 jleave:
437 NYD_LEAVE;
440 /* vim:set fenc=utf-8:s-it-mode */