]>
diplodocus.org Git - nmh/blob - uip/mhstore.c
3 * mhstore.c -- store the contents of MIME messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.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 *cache_public
;
53 extern char *cache_private
;
58 extern char *parts
[NPARTS
+ 1];
59 extern char *types
[NTYPES
+ 1];
63 extern char *preferred_types
[];
64 extern char *preferred_subtypes
[];
65 extern int npreferred
;
67 #define quitser pipeser
71 CT
parse_mime (char *);
75 int type_ok (CT
, int);
76 void flush_errors (void);
80 void freects_done (int) NORETURN
;
85 static void pipeser (int);
89 main (int argc
, char **argv
)
91 /* verbosw defaults to 1 for backward compatibility. */
92 int msgnum
, *icachesw
, autosw
= 0, verbosw
= 1;
93 const char *clobbersw
= "always";
94 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
95 char *maildir
, buf
[100], **argp
;
98 struct msgs_array msgs
= { 0, 0, NULL
};
99 struct msgs
*mp
= NULL
;
102 int files_not_clobbered
;
105 if (nmh_init(argv
[0], 1)) { return 1; }
109 arguments
= getarguments (invo_name
, argc
, argv
, 1);
115 while ((cp
= *argp
++)) {
117 switch (smatch (++cp
, switches
)) {
119 ambigsw (cp
, switches
);
122 adios (NULL
, "-%s unknown", cp
);
125 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
127 print_help (buf
, switches
, 1);
130 print_version(invo_name
);
141 icachesw
= &rcachesw
;
144 icachesw
= &wcachesw
;
146 if (!(cp
= *argp
++) || *cp
== '-')
147 adios (NULL
, "missing argument to %s", argp
[-2]);
148 switch (*icachesw
= smatch (cp
, caches
)) {
150 ambigsw (cp
, caches
);
153 adios (NULL
, "%s unknown", cp
);
167 if (!(cp
= *argp
++) || *cp
== '-')
168 adios (NULL
, "missing argument to %s", argp
[-2]);
170 adios (NULL
, "too many parts (starting with %s), %d max",
176 if (!(cp
= *argp
++) || *cp
== '-')
177 adios (NULL
, "missing argument to %s", argp
[-2]);
179 adios (NULL
, "too many types (starting with %s), %d max",
185 if (!(cp
= *argp
++) || *cp
== '-')
186 adios (NULL
, "missing argument to %s", argp
[-2]);
187 if (npreferred
>= NPREFS
)
188 adios (NULL
, "too many preferred types (starting with %s), %d max",
190 preferred_types
[npreferred
] = cp
;
191 cp
= strchr(cp
, '/');
192 if (cp
) *cp
++ = '\0';
193 preferred_subtypes
[npreferred
++] = cp
;
197 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
198 adios (NULL
, "missing argument to %s", argp
[-2]);
199 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
203 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
204 adios (NULL
, "missing argument to %s", argp
[-2]);
205 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
215 if (!(cp
= *argp
++) || *cp
== '-')
216 adios (NULL
, "missing argument to %s", argp
[-2]);
224 if (*cp
== '+' || *cp
== '@') {
226 adios (NULL
, "only one folder at a time!");
228 folder
= pluspath (cp
);
230 app_msgarg(&msgs
, cp
);
233 /* null terminate the list of acceptable parts/types */
238 * Check if we've specified an additional profile
240 if ((cp
= getenv ("MHSTORE"))) {
241 if ((fp
= fopen (cp
, "r"))) {
242 readconfig ((struct node
**) 0, fp
, cp
, 0);
245 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
250 * Read the standard profile setup
252 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
253 readconfig ((struct node
**) 0, fp
, cp
, 0);
257 /* Check for public cache location */
258 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
261 /* Check for private cache location */
262 if (!(cache_private
= context_find (nmhprivcache
)))
263 cache_private
= ".cache";
264 cache_private
= getcpy (m_maildir (cache_private
));
267 * Cache the current directory before we do any chdirs()'s.
269 cwd
= add(pwd(), NULL
);
271 if (!context_find ("path"))
272 free (path ("./", TFOLDER
));
274 if (file
&& msgs
.size
)
275 adios (NULL
, "cannot specify msg and file at same time!");
278 * check if message is coming from file
281 if (!(cts
= (CT
*) mh_xcalloc ((size_t) 2, sizeof(*cts
))))
282 adios (NULL
, "out of memory");
285 if ((ct
= parse_mime (file
))) {
288 ct
->c_storage
= add (outfile
, NULL
);
293 * message(s) are coming from a folder
296 app_msgarg(&msgs
, "cur");
298 folder
= getfolder (1);
299 maildir
= m_maildir (folder
);
301 if (chdir (maildir
) == NOTOK
)
302 adios (maildir
, "unable to change directory to");
304 /* read folder and create message structure */
305 if (!(mp
= folder_read (folder
, 1)))
306 adios (NULL
, "unable to read folder %s", folder
);
308 /* check for empty folder */
310 adios (NULL
, "no messages in %s", folder
);
312 /* parse all the message ranges/sequences and set SELECTED */
313 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
314 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
316 seq_setprev (mp
); /* set the previous-sequence */
318 if (!(cts
= (CT
*) mh_xcalloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
319 adios (NULL
, "out of memory");
322 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
323 if (is_selected(mp
, msgnum
)) {
326 msgnam
= m_name (msgnum
);
327 if ((ct
= parse_mime (msgnam
))) {
330 ct
->c_storage
= add (outfile
, NULL
);
341 SIGNAL (SIGQUIT
, quitser
);
342 SIGNAL (SIGPIPE
, pipeser
);
345 * Get the associated umask for the relevant contents.
347 for (ctp
= cts
; *ctp
; ctp
++) {
351 if (type_ok (ct
, 1) && !ct
->c_umask
) {
352 if (stat (ct
->c_file
, &st
) != NOTOK
)
353 ct
->c_umask
= ~(st
.st_mode
& 0777);
355 ct
->c_umask
= ~m_gmprot();
360 * Store the message content
362 info
= mhstoreinfo_create (cts
, cwd
, clobbersw
, autosw
, verbosw
);
363 store_all_messages (info
);
364 files_not_clobbered
= mhstoreinfo_files_not_clobbered(info
);
365 mhstoreinfo_free(info
);
367 /* Now free all the structures for the content */
368 for (ctp
= cts
; *ctp
; ctp
++)
374 /* If reading from a folder, do some updating */
376 context_replace (pfolder
, folder
);/* update current folder */
377 seq_setcur (mp
, mp
->hghsel
); /* update current message */
378 seq_save (mp
); /* synchronize sequences */
379 context_save (); /* save the context file */
382 done (files_not_clobbered
);
392 fprintf (stderr
, "\n");