]>
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>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
23 #define MHSTORE_SWITCHES \
24 X("auto", 0, AUTOSW) \
25 X("noauto", 0, NAUTOSW) \
26 X("check", 0, CHECKSW) \
27 X("nocheck", 0, NCHECKSW) \
28 X("verbose", 0, VERBSW) \
29 X("noverbose", 0, NVERBSW) \
30 X("file file", 0, FILESW) /* interface from show */ \
31 X("part number", 0, PARTSW) \
32 X("type content", 0, TYPESW) \
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
);
49 int save_clobber_policy (const char *);
50 extern int files_not_clobbered
;
53 extern char *tmp
; /* directory to place temp files */
58 extern char *cache_public
;
59 extern char *cache_private
;
63 extern char *cwd
; /* cache current working directory */
68 extern char *parts
[NPARTS
+ 1];
69 extern char *types
[NTYPES
+ 1];
75 #define quitser pipeser
78 CT
parse_mime (char *);
81 int part_ok (CT
, int);
82 int type_ok (CT
, int);
83 void flush_errors (void);
86 void store_all_messages (CT
*);
90 void freects_done (int) NORETURN
;
95 static void pipeser (int);
99 main (int argc
, char **argv
)
101 int msgnum
, *icachesw
;
102 char *cp
, *file
= NULL
, *folder
= NULL
;
103 char *maildir
, buf
[100], **argp
;
105 struct msgs_array msgs
= { 0, 0, NULL
};
106 struct msgs
*mp
= NULL
;
113 setlocale(LC_ALL
, "");
115 invo_name
= r1bindex (argv
[0], '/');
117 /* read user profile/context */
120 arguments
= getarguments (invo_name
, argc
, argv
, 1);
126 while ((cp
= *argp
++)) {
128 switch (smatch (++cp
, switches
)) {
130 ambigsw (cp
, switches
);
133 adios (NULL
, "-%s unknown", cp
);
136 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
138 print_help (buf
, switches
, 1);
141 print_version(invo_name
);
152 icachesw
= &rcachesw
;
155 icachesw
= &wcachesw
;
157 if (!(cp
= *argp
++) || *cp
== '-')
158 adios (NULL
, "missing argument to %s", argp
[-2]);
159 switch (*icachesw
= smatch (cp
, caches
)) {
161 ambigsw (cp
, caches
);
164 adios (NULL
, "%s unknown", cp
);
178 if (!(cp
= *argp
++) || *cp
== '-')
179 adios (NULL
, "missing argument to %s", argp
[-2]);
181 adios (NULL
, "too many parts (starting with %s), %d max",
187 if (!(cp
= *argp
++) || *cp
== '-')
188 adios (NULL
, "missing argument to %s", argp
[-2]);
190 adios (NULL
, "too many types (starting with %s), %d max",
196 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
197 adios (NULL
, "missing argument to %s", argp
[-2]);
198 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
208 if (!(cp
= *argp
++) || *cp
== '-')
209 adios (NULL
, "missing argument to %s", argp
[-2]);
210 if (save_clobber_policy (cp
)) {
211 adios (NULL
, "invalid argument, %s, to %s", argp
[-1],
220 if (*cp
== '+' || *cp
== '@') {
222 adios (NULL
, "only one folder at a time!");
224 folder
= pluspath (cp
);
226 app_msgarg(&msgs
, cp
);
229 /* null terminate the list of acceptable parts/types */
234 * Check if we've specified an additional profile
236 if ((cp
= getenv ("MHSTORE"))) {
237 if ((fp
= fopen (cp
, "r"))) {
238 readconfig ((struct node
**) 0, fp
, cp
, 0);
241 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
246 * Read the standard profile setup
248 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
249 readconfig ((struct node
**) 0, fp
, cp
, 0);
253 /* Check for public cache location */
254 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
257 /* Check for private cache location */
258 if (!(cache_private
= context_find (nmhprivcache
)))
259 cache_private
= ".cache";
260 cache_private
= getcpy (m_maildir (cache_private
));
263 * Cache the current directory before we do any chdirs()'s.
265 cwd
= getcpy (pwd());
268 * Check for storage directory. If specified,
269 * then store temporary files there. Else we
270 * store them in standard nmh directory.
272 if ((cp
= context_find (nmhstorage
)) && *cp
)
273 tmp
= concat (cp
, "/", invo_name
, NULL
);
275 tmp
= add (m_maildir (invo_name
), NULL
);
277 if (!context_find ("path"))
278 free (path ("./", TFOLDER
));
280 if (file
&& msgs
.size
)
281 adios (NULL
, "cannot specify msg and file at same time!");
284 * check if message is coming from file
287 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
288 adios (NULL
, "out of memory");
291 if ((ct
= parse_mime (file
)))
295 * message(s) are coming from a folder
298 app_msgarg(&msgs
, "cur");
300 folder
= getfolder (1);
301 maildir
= m_maildir (folder
);
303 if (chdir (maildir
) == NOTOK
)
304 adios (maildir
, "unable to change directory to");
306 /* read folder and create message structure */
307 if (!(mp
= folder_read (folder
)))
308 adios (NULL
, "unable to read folder %s", folder
);
310 /* check for empty folder */
312 adios (NULL
, "no messages in %s", folder
);
314 /* parse all the message ranges/sequences and set SELECTED */
315 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
316 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
318 seq_setprev (mp
); /* set the previous-sequence */
320 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
321 adios (NULL
, "out of memory");
324 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
325 if (is_selected(mp
, msgnum
)) {
328 msgnam
= m_name (msgnum
);
329 if ((ct
= parse_mime (msgnam
)))
339 SIGNAL (SIGQUIT
, quitser
);
340 SIGNAL (SIGPIPE
, pipeser
);
343 * Get the associated umask for the relevant contents.
345 for (ctp
= cts
; *ctp
; ctp
++) {
349 if (type_ok (ct
, 1) && !ct
->c_umask
) {
350 if (stat (ct
->c_file
, &st
) != NOTOK
)
351 ct
->c_umask
= ~(st
.st_mode
& 0777);
353 ct
->c_umask
= ~m_gmprot();
358 * Store the message content
360 store_all_messages (cts
);
362 /* Now free all the structures for the content */
363 for (ctp
= cts
; *ctp
; ctp
++)
369 /* If reading from a folder, do some updating */
371 context_replace (pfolder
, folder
);/* update current folder */
372 seq_setcur (mp
, mp
->hghsel
); /* update current message */
373 seq_save (mp
); /* synchronize sequences */
374 context_save (); /* save the context file */
377 done (files_not_clobbered
);
387 fprintf (stderr
, "\n");