]>
diplodocus.org Git - nmh/blob - uip/mhstore.c
7fa819aaf29ce761275ef356eef812b0fe1f481e
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>
20 #include "sbr/m_maildir.h"
23 #define MHSTORE_SWITCHES \
24 X("auto", 0, AUTOSW) \
25 X("noauto", 0, NAUTOSW) \
26 X("check", 0, CHECKSW) \
27 X("nocheck", 0, NCHECKSW) \
28 X("verbose", 0, VERBSW) \
29 X("noverbose", 0, NVERBSW) \
30 X("file file", 0, FILESW) /* interface from show */ \
31 X("outfile outfile", 0, OUTFILESW) \
32 X("part number", 0, PARTSW) \
33 X("type content", 0, TYPESW) \
34 X("prefer content", 0, PREFERSW) \
35 X("noprefer", 0, NPREFERSW) \
36 X("rcache policy", 0, RCACHESW) \
37 X("wcache policy", 0, WCACHESW) \
38 X("version", 0, VERSIONSW) \
39 X("help", 0, HELPSW) \
40 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
41 X("debug", -5, DEBUGSW) \
43 #define X(sw, minchars, id) id,
44 DEFINE_SWITCH_ENUM(MHSTORE
);
47 #define X(sw, minchars, id) { sw, minchars, id },
48 DEFINE_SWITCH_ARRAY(MHSTORE
, switches
);
52 #define quitser pipeser
60 static void pipeser (int);
64 main (int argc
, char **argv
)
66 /* verbosw defaults to 1 for backward compatibility. */
67 int msgnum
, *icachesw
, autosw
= 0, verbosw
= 1;
68 const char *clobbersw
= "always";
69 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
70 char *maildir
, buf
[100], **argp
;
73 struct msgs_array msgs
= { 0, 0, NULL
};
74 struct msgs
*mp
= NULL
;
77 int files_not_clobbered
;
80 if (nmh_init(argv
[0], 1)) { return 1; }
82 set_done(freects_done
);
84 arguments
= getarguments (invo_name
, argc
, argv
, 1);
90 while ((cp
= *argp
++)) {
92 switch (smatch (++cp
, switches
)) {
94 ambigsw (cp
, switches
);
97 adios (NULL
, "-%s unknown", cp
);
100 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
102 print_help (buf
, switches
, 1);
105 print_version(invo_name
);
116 icachesw
= &rcachesw
;
119 icachesw
= &wcachesw
;
121 if (!(cp
= *argp
++) || *cp
== '-')
122 adios (NULL
, "missing argument to %s", argp
[-2]);
123 switch (*icachesw
= smatch (cp
, cache_policy
)) {
125 ambigsw (cp
, cache_policy
);
128 adios (NULL
, "%s unknown", cp
);
142 if (!(cp
= *argp
++) || *cp
== '-')
143 adios (NULL
, "missing argument to %s", argp
[-2]);
145 adios (NULL
, "too many parts (starting with %s), %d max",
151 if (!(cp
= *argp
++) || *cp
== '-')
152 adios (NULL
, "missing argument to %s", argp
[-2]);
154 adios (NULL
, "too many types (starting with %s), %d max",
160 if (!(cp
= *argp
++) || *cp
== '-')
161 adios (NULL
, "missing argument to %s", argp
[-2]);
162 if (npreferred
>= NPREFS
)
163 adios (NULL
, "too many preferred types (starting with %s), %d max",
165 mime_preference
[npreferred
].type
= cp
;
166 cp
= strchr(cp
, '/');
167 if (cp
) *cp
++ = '\0';
168 mime_preference
[npreferred
++].subtype
= cp
;
176 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
177 adios (NULL
, "missing argument to %s", argp
[-2]);
178 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
182 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
183 adios (NULL
, "missing argument to %s", argp
[-2]);
184 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
194 if (!(cp
= *argp
++) || *cp
== '-')
195 adios (NULL
, "missing argument to %s", argp
[-2]);
203 if (*cp
== '+' || *cp
== '@') {
205 adios (NULL
, "only one folder at a time!");
206 folder
= pluspath (cp
);
208 app_msgarg(&msgs
, cp
);
211 /* null terminate the list of acceptable parts/types */
216 * Check if we've specified an additional profile
218 if ((cp
= getenv ("MHSTORE"))) {
219 if ((fp
= fopen (cp
, "r"))) {
220 readconfig(NULL
, fp
, cp
, 0);
223 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
228 * Read the standard profile setup
230 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
231 readconfig(NULL
, fp
, cp
, 0);
235 /* Check for public cache location */
236 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
239 /* Check for private cache location */
240 if (!(cache_private
= context_find (nmhprivcache
)))
241 cache_private
= ".cache";
242 cache_private
= getcpy (m_maildir (cache_private
));
245 * Cache the current directory before we do any chdirs()'s.
247 cwd
= mh_xstrdup(pwd());
249 if (!context_find ("path"))
250 free (path ("./", TFOLDER
));
252 if (file
&& msgs
.size
)
253 adios (NULL
, "cannot specify msg and file at same time!");
256 * check if message is coming from file
259 cts
= mh_xcalloc(2, sizeof *cts
);
262 if ((ct
= parse_mime (file
))) {
265 ct
->c_storage
= mh_xstrdup(outfile
);
270 * message(s) are coming from a folder
273 app_msgarg(&msgs
, "cur");
275 folder
= getfolder (1);
276 maildir
= m_maildir (folder
);
278 if (chdir (maildir
) == NOTOK
)
279 adios (maildir
, "unable to change directory to");
281 /* read folder and create message structure */
282 if (!(mp
= folder_read (folder
, 1)))
283 adios (NULL
, "unable to read folder %s", folder
);
285 /* check for empty folder */
287 adios (NULL
, "no messages in %s", folder
);
289 /* parse all the message ranges/sequences and set SELECTED */
290 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
291 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
293 seq_setprev (mp
); /* set the previous-sequence */
295 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
298 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
299 if (is_selected(mp
, msgnum
)) {
302 msgnam
= m_name (msgnum
);
303 if ((ct
= parse_mime (msgnam
))) {
306 ct
->c_storage
= mh_xstrdup(outfile
);
317 SIGNAL (SIGQUIT
, quitser
);
318 SIGNAL (SIGPIPE
, pipeser
);
321 * Get the associated umask for the relevant contents.
323 for (ctp
= cts
; *ctp
; ctp
++) {
327 if (type_ok (ct
, 1) && !ct
->c_umask
) {
328 if (stat (ct
->c_file
, &st
) != NOTOK
)
329 ct
->c_umask
= ~(st
.st_mode
& 0777);
331 ct
->c_umask
= ~m_gmprot();
336 * Store the message content
338 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
339 store_all_messages (info
);
340 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
341 mhstoreinfo_free(info
);
343 /* Now free all the structures for the content */
344 for (ctp
= cts
; *ctp
; ctp
++)
350 /* If reading from a folder, do some updating */
352 context_replace (pfolder
, folder
);/* update current folder */
353 seq_setcur (mp
, mp
->hghsel
); /* update current message */
354 seq_save (mp
); /* synchronize sequences */
355 context_save (); /* save the context file */
358 done (files_not_clobbered
);
368 fprintf (stderr
, "\n");