]>
diplodocus.org Git - nmh/blob - uip/mhstore.c
1 /* mhstore.c -- store the contents of MIME messages
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
10 #include <h/signals.h>
15 #include <h/mhparse.h>
16 #include <h/mhcachesbr.h>
19 #include "sbr/m_maildir.h"
22 #define MHSTORE_SWITCHES \
23 X("auto", 0, AUTOSW) \
24 X("noauto", 0, NAUTOSW) \
25 X("check", 0, CHECKSW) \
26 X("nocheck", 0, NCHECKSW) \
27 X("verbose", 0, VERBSW) \
28 X("noverbose", 0, NVERBSW) \
29 X("file file", 0, FILESW) /* interface from show */ \
30 X("outfile outfile", 0, OUTFILESW) \
31 X("part number", 0, PARTSW) \
32 X("type content", 0, TYPESW) \
33 X("prefer content", 0, PREFERSW) \
34 X("noprefer", 0, NPREFERSW) \
35 X("rcache policy", 0, RCACHESW) \
36 X("wcache policy", 0, WCACHESW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
39 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
40 X("debug", -5, DEBUGSW) \
42 #define X(sw, minchars, id) id,
43 DEFINE_SWITCH_ENUM(MHSTORE
);
46 #define X(sw, minchars, id) { sw, minchars, id },
47 DEFINE_SWITCH_ARRAY(MHSTORE
, switches
);
51 #define quitser pipeser
59 static void pipeser (int);
63 main (int argc
, char **argv
)
65 /* verbosw defaults to 1 for backward compatibility. */
66 int msgnum
, *icachesw
, autosw
= 0, verbosw
= 1;
67 const char *clobbersw
= "always";
68 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
69 char *maildir
, buf
[100], **argp
;
72 struct msgs_array msgs
= { 0, 0, NULL
};
73 struct msgs
*mp
= NULL
;
76 int files_not_clobbered
;
79 if (nmh_init(argv
[0], 1)) { return 1; }
83 arguments
= getarguments (invo_name
, argc
, argv
, 1);
89 while ((cp
= *argp
++)) {
91 switch (smatch (++cp
, switches
)) {
93 ambigsw (cp
, switches
);
96 adios (NULL
, "-%s unknown", cp
);
99 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
101 print_help (buf
, switches
, 1);
104 print_version(invo_name
);
115 icachesw
= &rcachesw
;
118 icachesw
= &wcachesw
;
120 if (!(cp
= *argp
++) || *cp
== '-')
121 adios (NULL
, "missing argument to %s", argp
[-2]);
122 switch (*icachesw
= smatch (cp
, cache_policy
)) {
124 ambigsw (cp
, cache_policy
);
127 adios (NULL
, "%s unknown", cp
);
141 if (!(cp
= *argp
++) || *cp
== '-')
142 adios (NULL
, "missing argument to %s", argp
[-2]);
144 adios (NULL
, "too many parts (starting with %s), %d max",
150 if (!(cp
= *argp
++) || *cp
== '-')
151 adios (NULL
, "missing argument to %s", argp
[-2]);
153 adios (NULL
, "too many types (starting with %s), %d max",
159 if (!(cp
= *argp
++) || *cp
== '-')
160 adios (NULL
, "missing argument to %s", argp
[-2]);
161 if (npreferred
>= NPREFS
)
162 adios (NULL
, "too many preferred types (starting with %s), %d max",
164 mime_preference
[npreferred
].type
= cp
;
165 cp
= strchr(cp
, '/');
166 if (cp
) *cp
++ = '\0';
167 mime_preference
[npreferred
++].subtype
= cp
;
175 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
176 adios (NULL
, "missing argument to %s", argp
[-2]);
177 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
181 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
182 adios (NULL
, "missing argument to %s", argp
[-2]);
183 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
193 if (!(cp
= *argp
++) || *cp
== '-')
194 adios (NULL
, "missing argument to %s", argp
[-2]);
202 if (*cp
== '+' || *cp
== '@') {
204 adios (NULL
, "only one folder at a time!");
205 folder
= pluspath (cp
);
207 app_msgarg(&msgs
, cp
);
210 /* null terminate the list of acceptable parts/types */
215 * Check if we've specified an additional profile
217 if ((cp
= getenv ("MHSTORE"))) {
218 if ((fp
= fopen (cp
, "r"))) {
219 readconfig(NULL
, fp
, cp
, 0);
222 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
227 * Read the standard profile setup
229 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
230 readconfig(NULL
, fp
, cp
, 0);
234 /* Check for public cache location */
235 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
238 /* Check for private cache location */
239 if (!(cache_private
= context_find (nmhprivcache
)))
240 cache_private
= ".cache";
241 cache_private
= getcpy (m_maildir (cache_private
));
244 * Cache the current directory before we do any chdirs()'s.
246 cwd
= mh_xstrdup(pwd());
248 if (!context_find ("path"))
249 free (path ("./", TFOLDER
));
251 if (file
&& msgs
.size
)
252 adios (NULL
, "cannot specify msg and file at same time!");
255 * check if message is coming from file
258 cts
= mh_xcalloc(2, sizeof *cts
);
261 if ((ct
= parse_mime (file
))) {
264 ct
->c_storage
= mh_xstrdup(outfile
);
269 * message(s) are coming from a folder
272 app_msgarg(&msgs
, "cur");
274 folder
= getfolder (1);
275 maildir
= m_maildir (folder
);
277 if (chdir (maildir
) == NOTOK
)
278 adios (maildir
, "unable to change directory to");
280 /* read folder and create message structure */
281 if (!(mp
= folder_read (folder
, 1)))
282 adios (NULL
, "unable to read folder %s", folder
);
284 /* check for empty folder */
286 adios (NULL
, "no messages in %s", folder
);
288 /* parse all the message ranges/sequences and set SELECTED */
289 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
290 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
292 seq_setprev (mp
); /* set the previous-sequence */
294 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
297 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
298 if (is_selected(mp
, msgnum
)) {
301 msgnam
= m_name (msgnum
);
302 if ((ct
= parse_mime (msgnam
))) {
305 ct
->c_storage
= mh_xstrdup(outfile
);
316 SIGNAL (SIGQUIT
, quitser
);
317 SIGNAL (SIGPIPE
, pipeser
);
320 * Get the associated umask for the relevant contents.
322 for (ctp
= cts
; *ctp
; ctp
++) {
326 if (type_ok (ct
, 1) && !ct
->c_umask
) {
327 if (stat (ct
->c_file
, &st
) != NOTOK
)
328 ct
->c_umask
= ~(st
.st_mode
& 0777);
330 ct
->c_umask
= ~m_gmprot();
335 * Store the message content
337 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
338 store_all_messages (info
);
339 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
340 mhstoreinfo_free(info
);
342 /* Now free all the structures for the content */
343 for (ctp
= cts
; *ctp
; ctp
++)
349 /* If reading from a folder, do some updating */
351 context_replace (pfolder
, folder
);/* update current folder */
352 seq_setcur (mp
, mp
->hghsel
); /* update current message */
353 seq_save (mp
); /* synchronize sequences */
354 context_save (); /* save the context file */
357 done (files_not_clobbered
);
367 fprintf (stderr
, "\n");