]>
diplodocus.org Git - nmh/blob - uip/mhstore.c
94b29bae214db9243a0861301a87992fbec5914e
2 * mhstore.c -- store the contents of MIME messages
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
11 #include <h/signals.h>
16 #include <h/mhparse.h>
17 #include <h/mhcachesbr.h>
20 #define MHSTORE_SWITCHES \
21 X("auto", 0, AUTOSW) \
22 X("noauto", 0, NAUTOSW) \
23 X("check", 0, CHECKSW) \
24 X("nocheck", 0, NCHECKSW) \
25 X("verbose", 0, VERBSW) \
26 X("noverbose", 0, NVERBSW) \
27 X("file file", 0, FILESW) /* interface from show */ \
28 X("outfile outfile", 0, OUTFILESW) \
29 X("part number", 0, PARTSW) \
30 X("type content", 0, TYPESW) \
31 X("prefer content", 0, PREFERSW) \
32 X("rcache policy", 0, RCACHESW) \
33 X("wcache policy", 0, WCACHESW) \
34 X("version", 0, VERSIONSW) \
35 X("help", 0, HELPSW) \
36 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
37 X("debug", -5, DEBUGSW) \
39 #define X(sw, minchars, id) id,
40 DEFINE_SWITCH_ENUM(MHSTORE
);
43 #define X(sw, minchars, id) { sw, minchars, id },
44 DEFINE_SWITCH_ARRAY(MHSTORE
, switches
);
51 extern char *cache_public
;
52 extern char *cache_private
;
57 extern char *parts
[NPARTS
+ 1];
58 extern char *types
[NTYPES
+ 1];
62 extern char *preferred_types
[];
63 extern char *preferred_subtypes
[];
64 extern int npreferred
;
66 #define quitser pipeser
70 CT
parse_mime (char *);
74 int type_ok (CT
, int);
75 void flush_errors (void);
79 void freects_done (int) NORETURN
;
84 static void pipeser (int);
88 main (int argc
, char **argv
)
90 /* verbosw defaults to 1 for backward compatibility. */
91 int msgnum
, *icachesw
, autosw
= 0, verbosw
= 1;
92 const char *clobbersw
= "always";
93 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
94 char *maildir
, buf
[100], **argp
;
97 struct msgs_array msgs
= { 0, 0, NULL
};
98 struct msgs
*mp
= NULL
;
101 int files_not_clobbered
;
104 if (nmh_init(argv
[0], 1)) { return 1; }
108 arguments
= getarguments (invo_name
, argc
, argv
, 1);
114 while ((cp
= *argp
++)) {
116 switch (smatch (++cp
, switches
)) {
118 ambigsw (cp
, switches
);
121 adios (NULL
, "-%s unknown", cp
);
124 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
126 print_help (buf
, switches
, 1);
129 print_version(invo_name
);
140 icachesw
= &rcachesw
;
143 icachesw
= &wcachesw
;
145 if (!(cp
= *argp
++) || *cp
== '-')
146 adios (NULL
, "missing argument to %s", argp
[-2]);
147 switch (*icachesw
= smatch (cp
, caches
)) {
149 ambigsw (cp
, caches
);
152 adios (NULL
, "%s unknown", cp
);
166 if (!(cp
= *argp
++) || *cp
== '-')
167 adios (NULL
, "missing argument to %s", argp
[-2]);
169 adios (NULL
, "too many parts (starting with %s), %d max",
175 if (!(cp
= *argp
++) || *cp
== '-')
176 adios (NULL
, "missing argument to %s", argp
[-2]);
178 adios (NULL
, "too many types (starting with %s), %d max",
184 if (!(cp
= *argp
++) || *cp
== '-')
185 adios (NULL
, "missing argument to %s", argp
[-2]);
186 if (npreferred
>= NPREFS
)
187 adios (NULL
, "too many preferred types (starting with %s), %d max",
189 preferred_types
[npreferred
] = cp
;
190 cp
= strchr(cp
, '/');
191 if (cp
) *cp
++ = '\0';
192 preferred_subtypes
[npreferred
++] = cp
;
196 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
197 adios (NULL
, "missing argument to %s", argp
[-2]);
198 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
202 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
203 adios (NULL
, "missing argument to %s", argp
[-2]);
204 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
214 if (!(cp
= *argp
++) || *cp
== '-')
215 adios (NULL
, "missing argument to %s", argp
[-2]);
223 if (*cp
== '+' || *cp
== '@') {
225 adios (NULL
, "only one folder at a time!");
227 folder
= pluspath (cp
);
229 app_msgarg(&msgs
, cp
);
232 /* null terminate the list of acceptable parts/types */
237 * Check if we've specified an additional profile
239 if ((cp
= getenv ("MHSTORE"))) {
240 if ((fp
= fopen (cp
, "r"))) {
241 readconfig ((struct node
**) 0, fp
, cp
, 0);
244 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
249 * Read the standard profile setup
251 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
252 readconfig ((struct node
**) 0, fp
, cp
, 0);
256 /* Check for public cache location */
257 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
260 /* Check for private cache location */
261 if (!(cache_private
= context_find (nmhprivcache
)))
262 cache_private
= ".cache";
263 cache_private
= getcpy (m_maildir (cache_private
));
266 * Cache the current directory before we do any chdirs()'s.
268 cwd
= mh_xstrdup(pwd());
270 if (!context_find ("path"))
271 free (path ("./", TFOLDER
));
273 if (file
&& msgs
.size
)
274 adios (NULL
, "cannot specify msg and file at same time!");
277 * check if message is coming from file
280 cts
= mh_xcalloc(2, sizeof *cts
);
283 if ((ct
= parse_mime (file
))) {
286 ct
->c_storage
= mh_xstrdup(outfile
);
291 * message(s) are coming from a folder
294 app_msgarg(&msgs
, "cur");
296 folder
= getfolder (1);
297 maildir
= m_maildir (folder
);
299 if (chdir (maildir
) == NOTOK
)
300 adios (maildir
, "unable to change directory to");
302 /* read folder and create message structure */
303 if (!(mp
= folder_read (folder
, 1)))
304 adios (NULL
, "unable to read folder %s", folder
);
306 /* check for empty folder */
308 adios (NULL
, "no messages in %s", folder
);
310 /* parse all the message ranges/sequences and set SELECTED */
311 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
312 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
314 seq_setprev (mp
); /* set the previous-sequence */
316 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
319 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
320 if (is_selected(mp
, msgnum
)) {
323 msgnam
= m_name (msgnum
);
324 if ((ct
= parse_mime (msgnam
))) {
327 ct
->c_storage
= mh_xstrdup(outfile
);
338 SIGNAL (SIGQUIT
, quitser
);
339 SIGNAL (SIGPIPE
, pipeser
);
342 * Get the associated umask for the relevant contents.
344 for (ctp
= cts
; *ctp
; ctp
++) {
348 if (type_ok (ct
, 1) && !ct
->c_umask
) {
349 if (stat (ct
->c_file
, &st
) != NOTOK
)
350 ct
->c_umask
= ~(st
.st_mode
& 0777);
352 ct
->c_umask
= ~m_gmprot();
357 * Store the message content
359 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
360 store_all_messages (info
);
361 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
362 mhstoreinfo_free(info
);
364 /* Now free all the structures for the content */
365 for (ctp
= cts
; *ctp
; ctp
++)
371 /* If reading from a folder, do some updating */
373 context_replace (pfolder
, folder
);/* update current folder */
374 seq_setcur (mp
, mp
->hghsel
); /* update current message */
375 seq_save (mp
); /* synchronize sequences */
376 context_save (); /* save the context file */
379 done (files_not_clobbered
);
389 fprintf (stderr
, "\n");