1 /* $NetBSD: mime_detach.c,v 1.4 2009/04/10 13:08:25 christos Exp $ */
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: mime_detach.c,v 1.4 2009/04/10 13:08:25 christos Exp $");
38 #endif /* not __lint__ */
53 #include "mime_child.h"
54 #include "mime_codecs.h"
55 #include "mime_detach.h"
67 mime_detach_control(void)
71 detach_ctl
.batch
= value(ENAME_MIME_DETACH_BATCH
) != NULL
;
72 detach_ctl
.ask
= detach_ctl
.batch
? 0 : 1;
73 detach_ctl
.overwrite
= 0;
75 cp
= value(ENAME_MIME_DETACH_OVERWRITE
);
76 if (cp
== NULL
|| strcasecmp(cp
, "no") == 0)
77 detach_ctl
.overwrite
= 0;
79 else if (*cp
== '\0' || strcasecmp(cp
, "yes") == 0)
80 detach_ctl
.overwrite
= 1;
82 else if (strcasecmp(cp
, "ask") == 0) {
83 detach_ctl
.overwrite
= 0;
87 (void)printf("invalid %s setting: %s",
88 ENAME_MIME_DETACH_OVERWRITE
, cp
);
95 detach_get_fname(char *prompt
, char *pathname
)
97 if (!detach_ctl
.batch
) {
100 fname
= my_gets(&elm
.filec
, prompt
, pathname
);
101 if (fname
== NULL
) /* ignore this attachment */
103 (void)strip_WSP(fname
);
104 fname
= skip_WSP(fname
);
105 if (*fname
== '\0') /* ignore this attachment */
107 pathname
= savestr(fname
); /* save this or it gets trashed */
109 else if (detach_ctl
.ask
)
110 (void)printf("%s%s\n", prompt
, pathname
);
121 detach_open_core(char *fname
, const char *partstr
)
126 flags
= (detach_ctl
.overwrite
? 0 : O_EXCL
) | O_CREAT
| O_TRUNC
| O_WRONLY
;
128 if ((fd
= open(fname
, flags
, 0600)) != -1 &&
129 Fdopen(fd
, "w") != NULL
)
130 return DETACH_OPEN_OK
;
132 if (detach_ctl
.ask
&& fd
== -1 && errno
== EEXIST
) {
135 (void)sasprintf(&p
, "%-7s overwrite: Always/Never/once/next/rename (ANonr)[n]? ",
137 p
= my_gets(&elm
.string
, p
, NULL
);
145 case 'A': detach_ctl
.overwrite
= 1;
146 detach_ctl
.batch
= 1;
150 if (Fopen(fname
, "w") != NULL
)
151 return DETACH_OPEN_OK
;
154 case 'N': detach_ctl
.overwrite
= 0;
155 detach_ctl
.batch
= 1;
158 case '\0': /* default */
165 case 'r': /* Rename */
166 return DETACH_RENAME
;
173 return DETACH_FAILED
;
177 detach_open_target(struct mime_info
*mip
)
185 * XXX: If partstr == NULL, we probably shouldn't be detaching
186 * anything, but let's be liberal and try to do something with
189 partstr
= mip
->mi_partstr
&& mip
->mi_partstr
[0] ? mip
->mi_partstr
: "0";
190 subtype
= mip
->mi_subtype
? mip
->mi_subtype
: "unknown";
193 * Get the suggested target pathname.
195 if (mip
->mi_filename
!= NULL
)
196 (void)sasprintf(&pathname
, "%s/%s", mip
->mi_detachdir
,
199 if (mip
->mi_detachall
== 0)
202 (void)sasprintf(&pathname
, "%s/msg-%s.part-%s.%s",
203 mip
->mi_detachdir
, mip
->mi_msgstr
,
210 (void)sasprintf(&prompt
, "%-7s filename: ", partstr
);
219 if ((fname
= detach_get_fname(prompt
, pathname
)) == NULL
)
222 * Make sure we don't have the name of something other
223 * than a normal file!
225 if (stat(fname
, &sb
) == 0 && !S_ISREG(sb
.st_mode
)) {
226 (void)printf("not a regular file: %s", fname
);
231 switch (detach_open_core(fname
, partstr
)) {
237 detach_ctl
.batch
= 0;
242 } while (!detach_ctl
.batch
);
248 * The main entry point for detaching.
251 mime_detach_parts(struct mime_info
*mip
)
256 if (mip
->mi_ignore_body
|| mip
->mp
->m_blines
== 0)
259 if ((dec
= mime_fio_decoder(mip
->mi_encoding
)) == NULL
&&
260 (dec
= mime_fio_decoder(MIME_TRANSFER_7BIT
)) == NULL
)
261 assert(/*CONSTCOND*/ 0); /* this should never get hit! */
263 if ((pathname
= detach_open_target(mip
)) == NULL
)
266 (void)printf("writing: %s\n", pathname
);
269 * XXX - should we do character set conversion here (done by
270 * run_decoder()), or just run dec()?
273 mime_run_function(dec
, pipe_end(mip
), NULL
);
275 run_decoder(mip
, dec
);
277 return pipe_end(mip
);
281 * Set the message part number to be used when constructing a filename
282 * for detaching unnamed parts in detach_open_target(). When
283 * threading, this is not a single number, hence the string value.
286 mime_detach_msgnum(struct mime_info
*mip
, const char *msgstr
)
288 for (/*EMPTY*/; mip
; mip
= mip
->mi_flink
)
289 mip
->mi_msgstr
= msgstr
;
292 #endif /* MIME_SUPPORT */