]>
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("part number", 0, PARTSW) \
30 X("type content", 0, TYPESW) \
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
);
47 int save_clobber_policy (const char *);
48 extern int files_not_clobbered
;
51 extern char *tmp
; /* directory to place temp files */
56 extern char *cache_public
;
57 extern char *cache_private
;
61 extern char *cwd
; /* cache current working directory */
66 extern char *parts
[NPARTS
+ 1];
67 extern char *types
[NTYPES
+ 1];
73 #define quitser pipeser
76 CT
parse_mime (char *);
79 int part_ok (CT
, int);
80 int type_ok (CT
, int);
81 void flush_errors (void);
84 void store_all_messages (CT
*);
88 void freects_done (int) NORETURN
;
93 static void pipeser (int);
97 main (int argc
, char **argv
)
99 int msgnum
, *icachesw
;
100 char *cp
, *file
= NULL
, *folder
= NULL
;
101 char *maildir
, buf
[100], **argp
;
103 struct msgs_array msgs
= { 0, 0, NULL
};
104 struct msgs
*mp
= NULL
;
111 setlocale(LC_ALL
, "");
113 invo_name
= r1bindex (argv
[0], '/');
115 /* read user profile/context */
118 arguments
= getarguments (invo_name
, argc
, argv
, 1);
124 while ((cp
= *argp
++)) {
126 switch (smatch (++cp
, switches
)) {
128 ambigsw (cp
, switches
);
131 adios (NULL
, "-%s unknown", cp
);
134 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
136 print_help (buf
, switches
, 1);
139 print_version(invo_name
);
150 icachesw
= &rcachesw
;
153 icachesw
= &wcachesw
;
155 if (!(cp
= *argp
++) || *cp
== '-')
156 adios (NULL
, "missing argument to %s", argp
[-2]);
157 switch (*icachesw
= smatch (cp
, caches
)) {
159 ambigsw (cp
, caches
);
162 adios (NULL
, "%s unknown", cp
);
176 if (!(cp
= *argp
++) || *cp
== '-')
177 adios (NULL
, "missing argument to %s", argp
[-2]);
179 adios (NULL
, "too many parts (starting with %s), %d max",
185 if (!(cp
= *argp
++) || *cp
== '-')
186 adios (NULL
, "missing argument to %s", argp
[-2]);
188 adios (NULL
, "too many types (starting with %s), %d max",
194 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
195 adios (NULL
, "missing argument to %s", argp
[-2]);
196 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
206 if (!(cp
= *argp
++) || *cp
== '-')
207 adios (NULL
, "missing argument to %s", argp
[-2]);
208 if (save_clobber_policy (cp
)) {
209 adios (NULL
, "invalid argument, %s, to %s", argp
[-1],
218 if (*cp
== '+' || *cp
== '@') {
220 adios (NULL
, "only one folder at a time!");
222 folder
= pluspath (cp
);
224 app_msgarg(&msgs
, cp
);
227 /* null terminate the list of acceptable parts/types */
232 * Check if we've specified an additional profile
234 if ((cp
= getenv ("MHSTORE"))) {
235 if ((fp
= fopen (cp
, "r"))) {
236 readconfig ((struct node
**) 0, fp
, cp
, 0);
239 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
244 * Read the standard profile setup
246 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
247 readconfig ((struct node
**) 0, fp
, cp
, 0);
251 /* Check for public cache location */
252 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
255 /* Check for private cache location */
256 if (!(cache_private
= context_find (nmhprivcache
)))
257 cache_private
= ".cache";
258 cache_private
= getcpy (m_maildir (cache_private
));
261 * Cache the current directory before we do any chdirs()'s.
263 cwd
= getcpy (pwd());
266 * Check for storage directory. If specified,
267 * then store temporary files there. Else we
268 * store them in standard nmh directory.
270 if ((cp
= context_find (nmhstorage
)) && *cp
)
271 tmp
= concat (cp
, "/", invo_name
, NULL
);
273 tmp
= add (m_maildir (invo_name
), NULL
);
275 if (!context_find ("path"))
276 free (path ("./", TFOLDER
));
278 if (file
&& msgs
.size
)
279 adios (NULL
, "cannot specify msg and file at same time!");
282 * check if message is coming from file
285 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
286 adios (NULL
, "out of memory");
289 if ((ct
= parse_mime (file
)))
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
*) calloc ((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
)))
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 store_all_messages (cts
);
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");