]>
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 #define MHSTORE_SWITCHES \
20 X("auto", 0, AUTOSW) \
21 X("noauto", 0, NAUTOSW) \
22 X("check", 0, CHECKSW) \
23 X("nocheck", 0, NCHECKSW) \
24 X("verbose", 0, VERBSW) \
25 X("noverbose", 0, NVERBSW) \
26 X("file file", 0, FILESW) /* interface from show */ \
27 X("outfile outfile", 0, OUTFILESW) \
28 X("part number", 0, PARTSW) \
29 X("type content", 0, TYPESW) \
30 X("prefer content", 0, PREFERSW) \
31 X("rcache policy", 0, RCACHESW) \
32 X("wcache policy", 0, WCACHESW) \
33 X("version", 0, VERSIONSW) \
34 X("help", 0, HELPSW) \
35 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
36 X("debug", -5, DEBUGSW) \
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(MHSTORE
);
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(MHSTORE
, switches
);
50 extern char *cache_public
;
51 extern char *cache_private
;
56 extern char *parts
[NPARTS
+ 1];
57 extern char *types
[NTYPES
+ 1];
61 extern char *preferred_types
[];
62 extern char *preferred_subtypes
[];
63 extern int npreferred
;
65 #define quitser pipeser
69 CT
parse_mime (char *);
73 int type_ok (CT
, int);
74 void flush_errors (void);
78 void freects_done (int) NORETURN
;
83 static void pipeser (int);
87 main (int argc
, char **argv
)
89 /* verbosw defaults to 1 for backward compatibility. */
90 int msgnum
, *icachesw
, autosw
= 0, verbosw
= 1;
91 const char *clobbersw
= "always";
92 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
93 char *maildir
, buf
[100], **argp
;
96 struct msgs_array msgs
= { 0, 0, NULL
};
97 struct msgs
*mp
= NULL
;
100 int files_not_clobbered
;
103 if (nmh_init(argv
[0], 1)) { return 1; }
107 arguments
= getarguments (invo_name
, argc
, argv
, 1);
113 while ((cp
= *argp
++)) {
115 switch (smatch (++cp
, switches
)) {
117 ambigsw (cp
, switches
);
120 adios (NULL
, "-%s unknown", cp
);
123 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
125 print_help (buf
, switches
, 1);
128 print_version(invo_name
);
139 icachesw
= &rcachesw
;
142 icachesw
= &wcachesw
;
144 if (!(cp
= *argp
++) || *cp
== '-')
145 adios (NULL
, "missing argument to %s", argp
[-2]);
146 switch (*icachesw
= smatch (cp
, caches
)) {
148 ambigsw (cp
, caches
);
151 adios (NULL
, "%s unknown", cp
);
165 if (!(cp
= *argp
++) || *cp
== '-')
166 adios (NULL
, "missing argument to %s", argp
[-2]);
168 adios (NULL
, "too many parts (starting with %s), %d max",
174 if (!(cp
= *argp
++) || *cp
== '-')
175 adios (NULL
, "missing argument to %s", argp
[-2]);
177 adios (NULL
, "too many types (starting with %s), %d max",
183 if (!(cp
= *argp
++) || *cp
== '-')
184 adios (NULL
, "missing argument to %s", argp
[-2]);
185 if (npreferred
>= NPREFS
)
186 adios (NULL
, "too many preferred types (starting with %s), %d max",
188 preferred_types
[npreferred
] = cp
;
189 cp
= strchr(cp
, '/');
190 if (cp
) *cp
++ = '\0';
191 preferred_subtypes
[npreferred
++] = cp
;
195 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
196 adios (NULL
, "missing argument to %s", argp
[-2]);
197 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
201 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
202 adios (NULL
, "missing argument to %s", argp
[-2]);
203 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
213 if (!(cp
= *argp
++) || *cp
== '-')
214 adios (NULL
, "missing argument to %s", argp
[-2]);
222 if (*cp
== '+' || *cp
== '@') {
224 adios (NULL
, "only one folder at a time!");
226 folder
= pluspath (cp
);
228 app_msgarg(&msgs
, cp
);
231 /* null terminate the list of acceptable parts/types */
236 * Check if we've specified an additional profile
238 if ((cp
= getenv ("MHSTORE"))) {
239 if ((fp
= fopen (cp
, "r"))) {
240 readconfig ((struct node
**) 0, fp
, cp
, 0);
243 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
248 * Read the standard profile setup
250 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
251 readconfig ((struct node
**) 0, fp
, cp
, 0);
255 /* Check for public cache location */
256 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
259 /* Check for private cache location */
260 if (!(cache_private
= context_find (nmhprivcache
)))
261 cache_private
= ".cache";
262 cache_private
= getcpy (m_maildir (cache_private
));
265 * Cache the current directory before we do any chdirs()'s.
267 cwd
= mh_xstrdup(pwd());
269 if (!context_find ("path"))
270 free (path ("./", TFOLDER
));
272 if (file
&& msgs
.size
)
273 adios (NULL
, "cannot specify msg and file at same time!");
276 * check if message is coming from file
279 cts
= mh_xcalloc(2, sizeof *cts
);
282 if ((ct
= parse_mime (file
))) {
285 ct
->c_storage
= mh_xstrdup(outfile
);
290 * message(s) are coming from a folder
293 app_msgarg(&msgs
, "cur");
295 folder
= getfolder (1);
296 maildir
= m_maildir (folder
);
298 if (chdir (maildir
) == NOTOK
)
299 adios (maildir
, "unable to change directory to");
301 /* read folder and create message structure */
302 if (!(mp
= folder_read (folder
, 1)))
303 adios (NULL
, "unable to read folder %s", folder
);
305 /* check for empty folder */
307 adios (NULL
, "no messages in %s", folder
);
309 /* parse all the message ranges/sequences and set SELECTED */
310 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
311 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
313 seq_setprev (mp
); /* set the previous-sequence */
315 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
318 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
319 if (is_selected(mp
, msgnum
)) {
322 msgnam
= m_name (msgnum
);
323 if ((ct
= parse_mime (msgnam
))) {
326 ct
->c_storage
= mh_xstrdup(outfile
);
337 SIGNAL (SIGQUIT
, quitser
);
338 SIGNAL (SIGPIPE
, pipeser
);
341 * Get the associated umask for the relevant contents.
343 for (ctp
= cts
; *ctp
; ctp
++) {
347 if (type_ok (ct
, 1) && !ct
->c_umask
) {
348 if (stat (ct
->c_file
, &st
) != NOTOK
)
349 ct
->c_umask
= ~(st
.st_mode
& 0777);
351 ct
->c_umask
= ~m_gmprot();
356 * Store the message content
358 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
359 store_all_messages (info
);
360 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
361 mhstoreinfo_free(info
);
363 /* Now free all the structures for the content */
364 for (ctp
= cts
; *ctp
; ctp
++)
370 /* If reading from a folder, do some updating */
372 context_replace (pfolder
, folder
);/* update current folder */
373 seq_setcur (mp
, mp
->hghsel
); /* update current message */
374 seq_save (mp
); /* synchronize sequences */
375 context_save (); /* save the context file */
378 done (files_not_clobbered
);
388 fprintf (stderr
, "\n");