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 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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. 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
36 #define n_FILE attachment
38 #ifndef HAVE_AMALGAMATION
42 /* We use calloc() for struct attachment */
43 n_CTAV(AC_DEFAULT
== 0);
45 /* Return >=0 if file denotes a valid message number */
46 static int a_attachment_is_msg(char const *file
);
48 /* Fill in some basic attachment fields */
49 static struct attachment
*a_attachment_setup_base(struct attachment
*ap
,
52 /* Setup ap to point to a message */
53 static struct attachment
*a_attachment_setup_msg(struct attachment
*ap
,
54 char const *msgcp
, int msgno
);
56 /* Try to create temporary charset converted version */
58 static bool_t
a_attachment_iconv(struct attachment
*ap
, FILE *ifp
);
62 static void a_attachment_yay(struct attachment
const *ap
);
65 a_attachment_is_msg(char const *file
){
74 /* TODO Message numbers should be size_t, and 0 may be a valid one */
75 if((n_idec_uiz_cp(&ib
, &file
[1], 10, NULL
76 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
77 ) != n_IDEC_STATE_CONSUMED
|| ib
== 0 || UICMP(z
, ib
, >, msgCount
))
86 static struct attachment
*
87 a_attachment_setup_base(struct attachment
*ap
, char const *file
){
89 ap
->a_input_charset
= ap
->a_charset
= NULL
;
90 ap
->a_path_user
= ap
->a_path
= ap
->a_path_bname
= ap
->a_name
= file
;
91 if((file
= strrchr(file
, '/')) != NULL
)
92 ap
->a_path_bname
= ap
->a_name
= ++file
;
95 ap
->a_content_type
= n_mimetype_classify_filename(file
);
96 ap
->a_content_disposition
= "attachment";
97 ap
->a_content_description
= NULL
;
98 ap
->a_content_id
= NULL
;
103 static struct attachment
*
104 a_attachment_setup_msg(struct attachment
*ap
, char const *msgcp
, int msgno
){
106 ap
->a_path_user
= ap
->a_path
= ap
->a_path_bname
= ap
->a_name
= msgcp
;
109 ap
->a_content_description
=
110 ap
->a_content_disposition
= NULL
;
111 ap
->a_content_id
= NULL
;
118 a_attachment_iconv(struct attachment
*ap
, FILE *ifp
){
119 struct str oul
= {NULL
, 0}, inl
= {NULL
, 0};
125 hold_sigs(); /* TODO until we have signal manager (see TODO) */
129 icp
= n_iconv_open(ap
->a_charset
, ap
->a_input_charset
);
130 if(icp
== (iconv_t
)-1){
131 if(n_err_no
== n_ERR_INVAL
)
134 n_perr(_("iconv_open"), 0);
138 cnt
= (size_t)fsize(ifp
);
140 if((ofp
= Ftmp(NULL
, "atticonv", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) ==NULL
){
141 n_perr(_("Temporary attachment data file"), 0);
146 if(fgetline(&inl
.s
, &lbsize
, &cnt
, &inl
.l
, ifp
, 0) == NULL
){
149 n_perr(_("I/O read error occurred"), 0);
153 if(n_iconv_str(icp
, n_ICONV_IGN_NOREVERSE
, &oul
, &inl
, NULL
) != 0)
155 if((inl
.l
= fwrite(oul
.s
, sizeof *oul
.s
, oul
.l
, ofp
)) != oul
.l
){
156 n_perr(_("I/O write error occurred"), 0);
168 if(icp
!= (iconv_t
)-1)
172 rele_sigs(); /* TODO until we have signal manager (see TODO) */
174 return (ofp
!= NULL
);
177 n_err(_("Cannot convert from %s to %s\n"),
178 ap
->a_input_charset
, ap
->a_charset
);
185 #endif /* HAVE_ICONV */
188 a_attachment_yay(struct attachment
const *ap
){
191 fprintf(n_stdout
, _("Added message/rfc822 attachment for message #%u\n"),
194 fprintf(n_stdout
, _("Added attachment %s (%s)\n"),
195 n_shexp_quote_cp(ap
->a_name
, FAL0
),
196 n_shexp_quote_cp(ap
->a_path_user
, FAL0
));
200 FL
struct attachment
*
201 n_attachment_append(struct attachment
*aplist
, char const *file
,
202 enum n_attach_error
*aerr_or_null
, struct attachment
**newap_or_null
){
207 char const *file_user
, *incs
, *oucs
;
208 struct attachment
*nap
, *ap
;
209 enum n_attach_error aerr
;
215 aerr
= n_ATTACH_ERR_NONE
;
220 aerr
= n_ATTACH_ERR_OTHER
;
223 file_user
= savestr(file
); /* TODO recreate after fexpand()!?! */
225 if((msgno
= a_attachment_is_msg(file
)) < 0){
227 char const *cp
, *ncp
;
230 if((file
= fexpand(file
, FEXP_LOCAL
| FEXP_NVAR
)) == NULL
){
231 aerr
= n_ATTACH_ERR_OTHER
;
236 if(oucs
!= NULL
&& oucs
!= (char*)-1){
237 n_err(_("No iconv support, cannot do %s\n"),
238 n_shexp_quote_cp(file_user
, FAL0
));
239 aerr
= n_ATTACH_ERR_ICONV_NAVAIL
;
246 (oucs
!= NULL
&& oucs
!= (char*)-1)
247 ? (cnvfp
= Fopen(file
, "r")) == NULL
:
249 access(file
, R_OK
) != 0)){
252 /* It may not have worked because of a character-set specification,
253 * so try to extract that and retry once */
254 if(incs
== NULL
&& (cp
= strrchr(file
, '=')) != NULL
){
258 nfp
= savestrbuf(file
, PTR2SIZE(cp
- file
));
260 for(ncp
= ++cp
; (c
= *cp
) != '\0'; ++cp
)
261 if(!alnumchar(c
) && !punctchar(c
))
265 i
= PTR2SIZE(cp
- ncp
);
266 if(i
== 0 || (i
== 1 && ncp
[0] == '-'))
268 else if((incs
= n_iconv_normalize_name(savestrbuf(ncp
, i
))
280 i
= PTR2SIZE(cp
- ncp
);
281 if(i
== 0 || (i
== 1 && ncp
[0] == '-'))
283 else if((xp
= n_iconv_normalize_name(savestrbuf(ncp
, i
))
298 n_err(_("Failed to access attachment %s: %s\n"),
299 n_shexp_quote_cp(file
, FAL0
), n_err_to_doc(e
));
300 aerr
= n_ATTACH_ERR_FILE_OPEN
;
305 nap
= a_attachment_setup_base(csalloc(1, sizeof *nap
), file
);
306 nap
->a_path_user
= file_user
;
308 nap
= a_attachment_setup_msg(nap
, file
, msgno
);
310 nap
->a_input_charset
= (incs
== NULL
|| incs
== (char*)-1)
311 ? savestr(ok_vlook(ttycharset
)) : incs
;
314 nap
->a_charset
= oucs
;
315 if(!a_attachment_iconv(nap
, cnvfp
)){
317 aerr
= n_ATTACH_ERR_ICONV_FAILED
;
320 nap
->a_conv
= AC_TMPFILE
;
323 if(incs
!= NULL
&& oucs
== NULL
)
324 nap
->a_conv
= AC_FIX_INCS
;
326 nap
->a_conv
= AC_DEFAULT
;
330 for(ap
= aplist
; ap
->a_flink
!= NULL
; ap
= ap
->a_flink
)
338 if(aerr_or_null
!= NULL
)
339 *aerr_or_null
= aerr
;
340 if(newap_or_null
!= NULL
)
341 *newap_or_null
= nap
;
346 FL
struct attachment
*
347 n_attachment_append_list(struct attachment
*aplist
, char const *names
){
349 struct n_string shou
, *shoup
;
352 shoup
= n_string_creat_auto(&shou
);
354 for(shin
.s
= n_UNCONST(names
), shin
.l
= UIZ_MAX
;;){
355 struct attachment
*nap
;
356 enum n_shexp_state shs
;
358 shs
= n_shexp_parse_token((n_SHEXP_PARSE_TRUNC
|
359 n_SHEXP_PARSE_TRIM_SPACE
| n_SHEXP_PARSE_LOG
|
360 n_SHEXP_PARSE_IFS_ADD_COMMA
| n_SHEXP_PARSE_IGNORE_EMPTY
),
362 if(shs
& n_SHEXP_STATE_ERR_MASK
)
365 if(shs
& n_SHEXP_STATE_OUTPUT
){
366 aplist
= n_attachment_append(aplist
, n_string_cp(shoup
), NULL
, &nap
);
368 if(n_psonce
& n_PSO_INTERACTIVE
)
369 a_attachment_yay(nap
);
373 if(shs
& n_SHEXP_STATE_STOP
)
381 FL
struct attachment
*
382 n_attachment_remove(struct attachment
*aplist
, struct attachment
*ap
){
383 struct attachment
*bap
, *fap
;
387 for(bap
= aplist
; aplist
!= NULL
&& aplist
!= ap
; aplist
= aplist
->a_flink
)
389 assert(aplist
!= NULL
);
394 if((aplist
= ap
->a_flink
) != NULL
)
395 aplist
->a_blink
= NULL
;
405 if(ap
->a_conv
== AC_TMPFILE
)
411 FL
struct attachment
*
412 n_attachment_find(struct attachment
*aplist
, char const *name
,
413 bool_t
*stat_or_null
){
417 struct attachment
*saved
;
423 if((bname
= strrchr(name
, '/')) != NULL
){
424 for(++bname
; aplist
!= NULL
; aplist
= aplist
->a_flink
)
425 if(!strcmp(name
, aplist
->a_path
)){
427 /* Exact match with path components: done */
429 }else if(!strcmp(bname
, aplist
->a_path_bname
)){
436 }else if((msgno
= a_attachment_is_msg(name
)) < 0){
437 for(sym
= FAL0
; aplist
!= NULL
; aplist
= aplist
->a_flink
){
438 if(!strcmp(name
, aplist
->a_name
)){
443 }else if(!strcmp(name
, aplist
->a_path_bname
)){
448 status
= status
? TRUM1
: TRU1
;
451 for(; aplist
!= NULL
; aplist
= aplist
->a_flink
){
452 if(aplist
->a_msgno
> 0 && aplist
->a_msgno
== msgno
){
462 if(stat_or_null
!= NULL
)
463 *stat_or_null
= status
;
468 FL
struct attachment
*
469 n_attachment_list_edit(struct attachment
*aplist
, enum n_go_input_flags gif
){
472 struct n_string shou
, *shoup
;
473 struct attachment
*naplist
, *ap
;
477 if((n_psonce
& (n_PSO_INTERACTIVE
| n_PSO_ATTACH_QUOTE_NOTED
)
478 ) == n_PSO_INTERACTIVE
){
479 n_psonce
|= n_PSO_ATTACH_QUOTE_NOTED
;
481 _("# Only supports sh(1)ell-style quoting for file names\n"));
484 shoup
= n_string_creat_auto(&shou
);
486 /* Modify already present ones? Append some more? */
489 for(naplist
= NULL
;;){
490 snprintf(prefix
, sizeof prefix
, _("#%" PRIu32
" filename: "), attno
);
493 /* TODO If we would create .a_path_user in append() after any
494 * TODO expansion then we could avoid closing+rebuilding the temporary
495 * TODO file if the new user input matches the original value! */
496 if(aplist
->a_conv
== AC_TMPFILE
)
497 Fclose(aplist
->a_tmpf
);
498 shin
.s
= n_shexp_quote_cp(aplist
->a_path_user
, FAL0
);
500 shin
.s
= n_UNCONST(n_empty
);
503 if((shin
.s
= n_go_input_cp(gif
, prefix
, shin
.s
)) != NULL
){
504 enum n_shexp_state shs
;
509 shs
= n_shexp_parse_token((n_SHEXP_PARSE_TRUNC
|
510 n_SHEXP_PARSE_TRIM_SPACE
| n_SHEXP_PARSE_LOG
|
511 n_SHEXP_PARSE_IGNORE_EMPTY
),
513 if(!(shs
& n_SHEXP_STATE_STOP
))
514 n_err(_("# May be given one argument a time only: %s\n"),
515 n_shexp_quote_cp(s_save
, FAL0
));
516 if((shs
& (n_SHEXP_STATE_OUTPUT
| n_SHEXP_STATE_STOP
|
517 n_SHEXP_STATE_ERR_MASK
)
518 ) != (n_SHEXP_STATE_OUTPUT
| n_SHEXP_STATE_STOP
))
521 naplist
= n_attachment_append(naplist
, n_string_cp(shoup
), NULL
, &ap
);
523 if(n_psonce
& n_PSO_INTERACTIVE
)
524 a_attachment_yay(ap
);
530 aplist
= aplist
->a_flink
;
531 /* In non-interactive or batch mode an empty line ends processing */
532 if((n_psonce
& n_PSO_INTERACTIVE
) && !(n_poption
& n_PO_BATCH_FLAG
))
543 n_attachment_list_print(struct attachment
const *aplist
, FILE *fp
){
544 struct attachment
const *ap
;
551 for(attno
= 1, ap
= aplist
; ap
!= NULL
; ++rv
, ++attno
, ap
= ap
->a_flink
){
553 fprintf(fp
, "#%" PRIu32
". message/rfc822: %u\n", attno
, ap
->a_msgno
);
555 char const *incs
, *oucs
;
557 if(!(n_psonce
& n_PSO_REPRODUCIBLE
)){
558 incs
= ap
->a_input_charset
;
559 oucs
= ap
->a_charset
;
561 incs
= oucs
= n_reproducible_name
;
563 fprintf(fp
, "#%" PRIu32
": %s [%s -- %s",
564 attno
, n_shexp_quote_cp(ap
->a_name
, FAL0
),
565 n_shexp_quote_cp(ap
->a_path
, FAL0
),
566 (ap
->a_content_type
!= NULL
567 ? ap
->a_content_type
: _("unclassified content")));
569 if(ap
->a_conv
== AC_TMPFILE
)
570 /* I18N: input and output character set as given */
571 fprintf(fp
, _(", incs=%s -> oucs=%s (readily converted)"),
573 else if(ap
->a_conv
== AC_FIX_INCS
)
574 /* I18N: input character set as given, no conversion to apply */
575 fprintf(fp
, _(", incs=%s (no conversion)"), incs
);
576 else if(ap
->a_conv
== AC_DEFAULT
){
578 /* I18N: input character set as given, output iterates */
579 fprintf(fp
, _(", incs=%s -> oucs=*sendcharsets*"), incs
);
580 else if(ap
->a_content_type
== NULL
||
581 !ascncasecmp(ap
->a_content_type
, "text/", 5))
582 fprintf(fp
, _(", default character set handling"));