]>
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>
18 #include "../sbr/m_maildir.h"
21 #define MHSTORE_SWITCHES \
22 X("auto", 0, AUTOSW) \
23 X("noauto", 0, NAUTOSW) \
24 X("check", 0, CHECKSW) \
25 X("nocheck", 0, NCHECKSW) \
26 X("verbose", 0, VERBSW) \
27 X("noverbose", 0, NVERBSW) \
28 X("file file", 0, FILESW) /* interface from show */ \
29 X("outfile outfile", 0, OUTFILESW) \
30 X("part number", 0, PARTSW) \
31 X("type content", 0, TYPESW) \
32 X("prefer content", 0, PREFERSW) \
33 X("rcache policy", 0, RCACHESW) \
34 X("wcache policy", 0, WCACHESW) \
35 X("version", 0, VERSIONSW) \
36 X("help", 0, HELPSW) \
37 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
38 X("debug", -5, DEBUGSW) \
40 #define X(sw, minchars, id) id,
41 DEFINE_SWITCH_ENUM(MHSTORE
);
44 #define X(sw, minchars, id) { sw, minchars, id },
45 DEFINE_SWITCH_ARRAY(MHSTORE
, switches
);
52 extern char *parts
[NPARTS
+ 1];
53 extern char *types
[NTYPES
+ 1];
57 extern char *preferred_types
[];
58 extern char *preferred_subtypes
[];
59 extern int npreferred
;
61 #define quitser pipeser
65 CT
parse_mime (char *);
69 int type_ok (CT
, int);
70 void flush_errors (void);
75 static void pipeser (int);
79 main (int argc
, char **argv
)
81 /* verbosw defaults to 1 for backward compatibility. */
82 int msgnum
, *icachesw
, autosw
= 0, verbosw
= 1;
83 const char *clobbersw
= "always";
84 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
85 char *maildir
, buf
[100], **argp
;
88 struct msgs_array msgs
= { 0, 0, NULL
};
89 struct msgs
*mp
= NULL
;
92 int files_not_clobbered
;
95 if (nmh_init(argv
[0], 1)) { return 1; }
99 arguments
= getarguments (invo_name
, argc
, argv
, 1);
105 while ((cp
= *argp
++)) {
107 switch (smatch (++cp
, switches
)) {
109 ambigsw (cp
, switches
);
112 adios (NULL
, "-%s unknown", cp
);
115 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
117 print_help (buf
, switches
, 1);
120 print_version(invo_name
);
131 icachesw
= &rcachesw
;
134 icachesw
= &wcachesw
;
136 if (!(cp
= *argp
++) || *cp
== '-')
137 adios (NULL
, "missing argument to %s", argp
[-2]);
138 switch (*icachesw
= smatch (cp
, cache_policy
)) {
140 ambigsw (cp
, cache_policy
);
143 adios (NULL
, "%s unknown", cp
);
157 if (!(cp
= *argp
++) || *cp
== '-')
158 adios (NULL
, "missing argument to %s", argp
[-2]);
160 adios (NULL
, "too many parts (starting with %s), %d max",
166 if (!(cp
= *argp
++) || *cp
== '-')
167 adios (NULL
, "missing argument to %s", argp
[-2]);
169 adios (NULL
, "too many types (starting with %s), %d max",
175 if (!(cp
= *argp
++) || *cp
== '-')
176 adios (NULL
, "missing argument to %s", argp
[-2]);
177 if (npreferred
>= NPREFS
)
178 adios (NULL
, "too many preferred types (starting with %s), %d max",
180 preferred_types
[npreferred
] = cp
;
181 cp
= strchr(cp
, '/');
182 if (cp
) *cp
++ = '\0';
183 preferred_subtypes
[npreferred
++] = cp
;
187 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
188 adios (NULL
, "missing argument to %s", argp
[-2]);
189 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
193 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
194 adios (NULL
, "missing argument to %s", argp
[-2]);
195 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
205 if (!(cp
= *argp
++) || *cp
== '-')
206 adios (NULL
, "missing argument to %s", argp
[-2]);
214 if (*cp
== '+' || *cp
== '@') {
216 adios (NULL
, "only one folder at a time!");
218 folder
= pluspath (cp
);
220 app_msgarg(&msgs
, cp
);
223 /* null terminate the list of acceptable parts/types */
228 * Check if we've specified an additional profile
230 if ((cp
= getenv ("MHSTORE"))) {
231 if ((fp
= fopen (cp
, "r"))) {
232 readconfig ((struct node
**) 0, fp
, cp
, 0);
235 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
240 * Read the standard profile setup
242 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
243 readconfig ((struct node
**) 0, fp
, cp
, 0);
247 /* Check for public cache location */
248 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
251 /* Check for private cache location */
252 if (!(cache_private
= context_find (nmhprivcache
)))
253 cache_private
= ".cache";
254 cache_private
= getcpy (m_maildir (cache_private
));
257 * Cache the current directory before we do any chdirs()'s.
259 cwd
= mh_xstrdup(pwd());
261 if (!context_find ("path"))
262 free (path ("./", TFOLDER
));
264 if (file
&& msgs
.size
)
265 adios (NULL
, "cannot specify msg and file at same time!");
268 * check if message is coming from file
271 cts
= mh_xcalloc(2, sizeof *cts
);
274 if ((ct
= parse_mime (file
))) {
277 ct
->c_storage
= mh_xstrdup(outfile
);
282 * message(s) are coming from a folder
285 app_msgarg(&msgs
, "cur");
287 folder
= getfolder (1);
288 maildir
= m_maildir (folder
);
290 if (chdir (maildir
) == NOTOK
)
291 adios (maildir
, "unable to change directory to");
293 /* read folder and create message structure */
294 if (!(mp
= folder_read (folder
, 1)))
295 adios (NULL
, "unable to read folder %s", folder
);
297 /* check for empty folder */
299 adios (NULL
, "no messages in %s", folder
);
301 /* parse all the message ranges/sequences and set SELECTED */
302 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
303 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
305 seq_setprev (mp
); /* set the previous-sequence */
307 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
310 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
311 if (is_selected(mp
, msgnum
)) {
314 msgnam
= m_name (msgnum
);
315 if ((ct
= parse_mime (msgnam
))) {
318 ct
->c_storage
= mh_xstrdup(outfile
);
329 SIGNAL (SIGQUIT
, quitser
);
330 SIGNAL (SIGPIPE
, pipeser
);
333 * Get the associated umask for the relevant contents.
335 for (ctp
= cts
; *ctp
; ctp
++) {
339 if (type_ok (ct
, 1) && !ct
->c_umask
) {
340 if (stat (ct
->c_file
, &st
) != NOTOK
)
341 ct
->c_umask
= ~(st
.st_mode
& 0777);
343 ct
->c_umask
= ~m_gmprot();
348 * Store the message content
350 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
351 store_all_messages (info
);
352 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
353 mhstoreinfo_free(info
);
355 /* Now free all the structures for the content */
356 for (ctp
= cts
; *ctp
; ctp
++)
362 /* If reading from a folder, do some updating */
364 context_replace (pfolder
, folder
);/* update current folder */
365 seq_setcur (mp
, mp
->hghsel
); /* update current message */
366 seq_save (mp
); /* synchronize sequences */
367 context_save (); /* save the context file */
370 done (files_not_clobbered
);
380 fprintf (stderr
, "\n");