]>
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("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
);
48 int save_clobber_policy (const char *);
49 extern int files_not_clobbered
;
52 extern char *tmp
; /* directory to place temp files */
57 extern char *cache_public
;
58 extern char *cache_private
;
62 extern char *cwd
; /* cache current working directory */
67 extern char *parts
[NPARTS
+ 1];
68 extern char *types
[NTYPES
+ 1];
74 #define quitser pipeser
77 CT
parse_mime (char *);
80 int part_ok (CT
, int);
81 int type_ok (CT
, int);
82 void flush_errors (void);
85 void store_all_messages (CT
*);
89 void freects_done (int) NORETURN
;
94 static void pipeser (int);
98 main (int argc
, char **argv
)
100 int msgnum
, *icachesw
;
101 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
102 char *maildir
, buf
[100], **argp
;
104 struct msgs_array msgs
= { 0, 0, NULL
};
105 struct msgs
*mp
= NULL
;
112 setlocale(LC_ALL
, "");
114 invo_name
= r1bindex (argv
[0], '/');
116 /* read user profile/context */
119 arguments
= getarguments (invo_name
, argc
, argv
, 1);
125 while ((cp
= *argp
++)) {
127 switch (smatch (++cp
, switches
)) {
129 ambigsw (cp
, switches
);
132 adios (NULL
, "-%s unknown", cp
);
135 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
137 print_help (buf
, switches
, 1);
140 print_version(invo_name
);
151 icachesw
= &rcachesw
;
154 icachesw
= &wcachesw
;
156 if (!(cp
= *argp
++) || *cp
== '-')
157 adios (NULL
, "missing argument to %s", argp
[-2]);
158 switch (*icachesw
= smatch (cp
, caches
)) {
160 ambigsw (cp
, caches
);
163 adios (NULL
, "%s unknown", cp
);
177 if (!(cp
= *argp
++) || *cp
== '-')
178 adios (NULL
, "missing argument to %s", argp
[-2]);
180 adios (NULL
, "too many parts (starting with %s), %d max",
186 if (!(cp
= *argp
++) || *cp
== '-')
187 adios (NULL
, "missing argument to %s", argp
[-2]);
189 adios (NULL
, "too many types (starting with %s), %d max",
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]);
215 if (save_clobber_policy (cp
)) {
216 adios (NULL
, "invalid argument, %s, to %s", argp
[-1],
225 if (*cp
== '+' || *cp
== '@') {
227 adios (NULL
, "only one folder at a time!");
229 folder
= pluspath (cp
);
231 app_msgarg(&msgs
, cp
);
234 /* null terminate the list of acceptable parts/types */
239 * Check if we've specified an additional profile
241 if ((cp
= getenv ("MHSTORE"))) {
242 if ((fp
= fopen (cp
, "r"))) {
243 readconfig ((struct node
**) 0, fp
, cp
, 0);
246 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
251 * Read the standard profile setup
253 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
254 readconfig ((struct node
**) 0, fp
, cp
, 0);
258 /* Check for public cache location */
259 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
262 /* Check for private cache location */
263 if (!(cache_private
= context_find (nmhprivcache
)))
264 cache_private
= ".cache";
265 cache_private
= getcpy (m_maildir (cache_private
));
268 * Cache the current directory before we do any chdirs()'s.
270 cwd
= getcpy (pwd());
273 * Check for storage directory. If specified,
274 * then store temporary files there. Else we
275 * store them in standard nmh directory.
277 if ((cp
= context_find (nmhstorage
)) && *cp
)
278 tmp
= concat (cp
, "/", invo_name
, NULL
);
280 tmp
= add (m_maildir (invo_name
), NULL
);
282 if (!context_find ("path"))
283 free (path ("./", TFOLDER
));
285 if (file
&& msgs
.size
)
286 adios (NULL
, "cannot specify msg and file at same time!");
289 * check if message is coming from file
292 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
293 adios (NULL
, "out of memory");
296 if ((ct
= parse_mime (file
))) {
299 ct
->c_storage
= outfile
;
304 * message(s) are coming from a folder
307 app_msgarg(&msgs
, "cur");
309 folder
= getfolder (1);
310 maildir
= m_maildir (folder
);
312 if (chdir (maildir
) == NOTOK
)
313 adios (maildir
, "unable to change directory to");
315 /* read folder and create message structure */
316 if (!(mp
= folder_read (folder
, 1)))
317 adios (NULL
, "unable to read folder %s", folder
);
319 /* check for empty folder */
321 adios (NULL
, "no messages in %s", folder
);
323 /* parse all the message ranges/sequences and set SELECTED */
324 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
325 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
327 seq_setprev (mp
); /* set the previous-sequence */
329 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
330 adios (NULL
, "out of memory");
333 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
334 if (is_selected(mp
, msgnum
)) {
337 msgnam
= m_name (msgnum
);
338 if ((ct
= parse_mime (msgnam
))) {
341 ct
->c_storage
= add (outfile
, NULL
);
352 SIGNAL (SIGQUIT
, quitser
);
353 SIGNAL (SIGPIPE
, pipeser
);
356 * Get the associated umask for the relevant contents.
358 for (ctp
= cts
; *ctp
; ctp
++) {
362 if (type_ok (ct
, 1) && !ct
->c_umask
) {
363 if (stat (ct
->c_file
, &st
) != NOTOK
)
364 ct
->c_umask
= ~(st
.st_mode
& 0777);
366 ct
->c_umask
= ~m_gmprot();
371 * Store the message content
373 store_all_messages (cts
);
375 /* Now free all the structures for the content */
376 for (ctp
= cts
; *ctp
; ctp
++)
382 /* If reading from a folder, do some updating */
384 context_replace (pfolder
, folder
);/* update current folder */
385 seq_setcur (mp
, mp
->hghsel
); /* update current message */
386 seq_save (mp
); /* synchronize sequences */
387 context_save (); /* save the context file */
390 done (files_not_clobbered
);
400 fprintf (stderr
, "\n");