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>.
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
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
40 #ifndef HAVE_AMALGAMATION
44 /* We use calloc() for struct attachment */
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
,
55 /* Try to create temporary charset converted version */
57 static int _attach_iconv(struct attachment
*ap
);
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" ;) */
68 ap
->a_input_charset
= ap
->a_charset
= NULL
;
71 if ((file
= strrchr(file
, '/')) != NULL
)
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
)
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
);
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
);
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 */
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
;
121 char const *cslc
= NULL
/*cc uninit?*/, *cp
, *defcs
;
124 hold_sigs(); /* TODO until we have signal manager (see TODO) */
125 ohdl
= safe_signal(SIGINT
, SIG_IGN
);
127 if (sigsetjmp(__atticonv_jmp
, 1)) {
131 safe_signal(SIGINT
, &__atticonv_onsig
);
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
);
139 ap
->a_content_description
= NULL
;
141 } else if (ap
->a_conv
== AC_TMPFILE
) {
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
);
150 if ((ap
->a_name
= readstr_input(prefix
, ap
->a_name
)) == NULL
) {
155 /* May be a message number (XXX add "AC_MSG", use that not .a_msgno) */
156 if (ap
->a_name
[0] == '#') {
158 int msgno
= (int)strtol(ap
->a_name
+ 1, &ecp
, 10);
159 if (msgno
> 0 && msgno
<= msgCount
&& *ecp
== '\0') {
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
);
170 if ((cp
= file_expand(ap
->a_name
)) != NULL
&& !access(cp
, R_OK
)) {
177 ap
= _fill_in(ap
, cp
, number
);
180 * Character set of attachments: enum attach_conv
182 cslc
= charset_get_lc();
184 if (!(options
& OPT_INTERACTIVE
))
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
;
194 charset_iter_reset(NULL
);
197 snprintf(prefix
, sizeof prefix
, tr(160, "#%u\tinput charset: "), number
);
198 if ((defcs
= ap
->a_input_charset
) == NULL
)
200 cp
= ap
->a_input_charset
= readstr_input(prefix
, defcs
);
202 if (!(options
& OPT_INTERACTIVE
)) {
204 ap
->a_conv
= (cp
!= NULL
) ? AC_FIX_INCS
: AC_DEFAULT
;
209 snprintf(prefix
, sizeof prefix
, tr(161, "#%u\toutput (send) charset: "),
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
;
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());
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
,
238 if (_attach_iconv(ap
))
239 ap
->a_conv
= AC_TMPFILE
;
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"));
253 if (options
& OPT_INTERACTIVE
)
254 printf(tr(19, "~@: added attachment \"%s\"\n"), ap
->a_name
);
256 safe_signal(SIGINT
, ohdl
);/* TODO until we have signal manager (see TODO) */
257 if (__atticonv_sig
!= 0) {
260 sigaddset(&nset
, SIGINT
);
261 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
270 _attach_iconv(struct attachment
*ap
)
272 struct str oul
= {NULL
, 0}, inl
= {NULL
, 0};
273 FILE *fo
= NULL
, *fi
= NULL
;
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) {
285 perror("iconv_open");
289 if ((fi
= Fopen(ap
->a_name
, "r")) == NULL
) {
295 if ((fo
= Ftmp(NULL
, "atic", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
297 perror(tr(51, "temporary mail file"));
302 if (fgetline(&inl
.s
, &lbsize
, &cnt
, &inl
.l
, fi
, 0) == NULL
) {
305 perror(tr(195, "I/O read error occurred"));
309 if (n_iconv_str(icp
, &oul
, &inl
, NULL
, FAL0
) != 0)
311 if ((inl
.l
= fwrite(oul
.s
, sizeof *oul
.s
, oul
.l
, fo
)) != oul
.l
) {
312 perror(tr(196, "I/O write error occurred"));
326 if (icp
!= (iconv_t
)-1)
329 rele_sigs(); /* TODO until we have signal manager (see TODO) */
334 fprintf(stderr
, tr(179, "Cannot convert from %s to %s\n"),
335 ap
->a_input_charset
, ap
->a_charset
);
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
;
351 if ((file
= file_expand(file
)) == NULL
)
353 if (access(file
, R_OK
) != 0)
356 nap
= _fill_in(csalloc(1, sizeof *nap
), file
, 0);
359 if (aphead
!= NULL
) {
360 for (ap
= aphead
; ap
->a_flink
!= NULL
; ap
= ap
->a_flink
)
375 append_attachments(struct attachment
**aphead
, char *names
)
378 struct attachment
*xaph
, *nap
;
381 while ((cp
= n_strsep(&names
, ',', 1)) != NULL
) {
382 if ((xaph
= add_attachment(*aphead
, cp
, &nap
)) != NULL
) {
384 if (options
& OPT_INTERACTIVE
)
385 printf(tr(19, "~@: added attachment \"%s\"\n"), nap
->a_name
);
393 edit_attachments(struct attachment
**aphead
)
395 struct attachment
*ap
, *fap
, *bap
;
399 /* Modify already present ones? */
400 for (ap
= *aphead
; ap
!= NULL
; ap
= fap
) {
401 if (_read_attachment_data(ap
, attno
) != NULL
) {
407 if ((bap
= ap
->a_blink
) != NULL
)
413 /*else*//* TODO until we have signal manager (see TODO) */
414 if (__atticonv_sig
!= 0)
421 if ((bap
= *aphead
) != NULL
)
422 while (bap
->a_flink
!= NULL
)
424 while ((fap
= _read_attachment_data(NULL
, attno
)) != NULL
) {
434 if (__atticonv_sig
!= 0) /* TODO until we have signal manager (see TODO) */
440 /* vim:set fenc=utf-8:s-it-mode */