]>
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>
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);
80 static void pipeser (int);
84 main (int argc
, char **argv
)
86 /* verbosw defaults to 1 for backward compatibility. */
87 int msgnum
, *icachesw
, autosw
= 0, verbosw
= 1;
88 const char *clobbersw
= "always";
89 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
90 char *maildir
, buf
[100], **argp
;
93 struct msgs_array msgs
= { 0, 0, NULL
};
94 struct msgs
*mp
= NULL
;
97 int files_not_clobbered
;
100 if (nmh_init(argv
[0], 1)) { return 1; }
104 arguments
= getarguments (invo_name
, argc
, argv
, 1);
110 while ((cp
= *argp
++)) {
112 switch (smatch (++cp
, switches
)) {
114 ambigsw (cp
, switches
);
117 adios (NULL
, "-%s unknown", cp
);
120 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
122 print_help (buf
, switches
, 1);
125 print_version(invo_name
);
136 icachesw
= &rcachesw
;
139 icachesw
= &wcachesw
;
141 if (!(cp
= *argp
++) || *cp
== '-')
142 adios (NULL
, "missing argument to %s", argp
[-2]);
143 switch (*icachesw
= smatch (cp
, caches
)) {
145 ambigsw (cp
, caches
);
148 adios (NULL
, "%s unknown", cp
);
162 if (!(cp
= *argp
++) || *cp
== '-')
163 adios (NULL
, "missing argument to %s", argp
[-2]);
165 adios (NULL
, "too many parts (starting with %s), %d max",
171 if (!(cp
= *argp
++) || *cp
== '-')
172 adios (NULL
, "missing argument to %s", argp
[-2]);
174 adios (NULL
, "too many types (starting with %s), %d max",
180 if (!(cp
= *argp
++) || *cp
== '-')
181 adios (NULL
, "missing argument to %s", argp
[-2]);
182 if (npreferred
>= NPREFS
)
183 adios (NULL
, "too many preferred types (starting with %s), %d max",
185 preferred_types
[npreferred
] = cp
;
186 cp
= strchr(cp
, '/');
187 if (cp
) *cp
++ = '\0';
188 preferred_subtypes
[npreferred
++] = cp
;
192 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
193 adios (NULL
, "missing argument to %s", argp
[-2]);
194 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
198 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
199 adios (NULL
, "missing argument to %s", argp
[-2]);
200 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
210 if (!(cp
= *argp
++) || *cp
== '-')
211 adios (NULL
, "missing argument to %s", argp
[-2]);
219 if (*cp
== '+' || *cp
== '@') {
221 adios (NULL
, "only one folder at a time!");
223 folder
= pluspath (cp
);
225 app_msgarg(&msgs
, cp
);
228 /* null terminate the list of acceptable parts/types */
233 * Check if we've specified an additional profile
235 if ((cp
= getenv ("MHSTORE"))) {
236 if ((fp
= fopen (cp
, "r"))) {
237 readconfig ((struct node
**) 0, fp
, cp
, 0);
240 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
245 * Read the standard profile setup
247 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
248 readconfig ((struct node
**) 0, fp
, cp
, 0);
252 /* Check for public cache location */
253 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
256 /* Check for private cache location */
257 if (!(cache_private
= context_find (nmhprivcache
)))
258 cache_private
= ".cache";
259 cache_private
= getcpy (m_maildir (cache_private
));
262 * Cache the current directory before we do any chdirs()'s.
264 cwd
= mh_xstrdup(pwd());
266 if (!context_find ("path"))
267 free (path ("./", TFOLDER
));
269 if (file
&& msgs
.size
)
270 adios (NULL
, "cannot specify msg and file at same time!");
273 * check if message is coming from file
276 cts
= mh_xcalloc(2, sizeof *cts
);
279 if ((ct
= parse_mime (file
))) {
282 ct
->c_storage
= mh_xstrdup(outfile
);
287 * message(s) are coming from a folder
290 app_msgarg(&msgs
, "cur");
292 folder
= getfolder (1);
293 maildir
= m_maildir (folder
);
295 if (chdir (maildir
) == NOTOK
)
296 adios (maildir
, "unable to change directory to");
298 /* read folder and create message structure */
299 if (!(mp
= folder_read (folder
, 1)))
300 adios (NULL
, "unable to read folder %s", folder
);
302 /* check for empty folder */
304 adios (NULL
, "no messages in %s", folder
);
306 /* parse all the message ranges/sequences and set SELECTED */
307 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
308 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
310 seq_setprev (mp
); /* set the previous-sequence */
312 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
315 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
316 if (is_selected(mp
, msgnum
)) {
319 msgnam
= m_name (msgnum
);
320 if ((ct
= parse_mime (msgnam
))) {
323 ct
->c_storage
= mh_xstrdup(outfile
);
334 SIGNAL (SIGQUIT
, quitser
);
335 SIGNAL (SIGPIPE
, pipeser
);
338 * Get the associated umask for the relevant contents.
340 for (ctp
= cts
; *ctp
; ctp
++) {
344 if (type_ok (ct
, 1) && !ct
->c_umask
) {
345 if (stat (ct
->c_file
, &st
) != NOTOK
)
346 ct
->c_umask
= ~(st
.st_mode
& 0777);
348 ct
->c_umask
= ~m_gmprot();
353 * Store the message content
355 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
356 store_all_messages (info
);
357 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
358 mhstoreinfo_free(info
);
360 /* Now free all the structures for the content */
361 for (ctp
= cts
; *ctp
; ctp
++)
367 /* If reading from a folder, do some updating */
369 context_replace (pfolder
, folder
);/* update current folder */
370 seq_setcur (mp
, mp
->hghsel
); /* update current message */
371 seq_save (mp
); /* synchronize sequences */
372 context_save (); /* save the context file */
375 done (files_not_clobbered
);
385 fprintf (stderr
, "\n");