]>
diplodocus.org Git - nmh/blob - uip/mhstore.c
050310cfec82c97403ff570d38440d7188d1c210
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
;
54 extern char *cache_public
;
55 extern char *cache_private
;
59 extern char *cwd
; /* cache current working directory */
64 extern char *parts
[NPARTS
+ 1];
65 extern char *types
[NTYPES
+ 1];
71 #define quitser pipeser
74 CT
parse_mime (char *);
77 int part_ok (CT
, int);
78 int type_ok (CT
, int);
79 void flush_errors (void);
82 void store_all_messages (CT
*);
86 void freects_done (int) NORETURN
;
91 static void pipeser (int);
95 main (int argc
, char **argv
)
97 int msgnum
, *icachesw
;
98 char *cp
, *file
= NULL
, *outfile
= NULL
, *folder
= NULL
;
99 char *maildir
, buf
[100], **argp
;
101 struct msgs_array msgs
= { 0, 0, NULL
};
102 struct msgs
*mp
= NULL
;
109 setlocale(LC_ALL
, "");
111 invo_name
= r1bindex (argv
[0], '/');
113 /* read user profile/context */
116 arguments
= getarguments (invo_name
, argc
, argv
, 1);
122 while ((cp
= *argp
++)) {
124 switch (smatch (++cp
, switches
)) {
126 ambigsw (cp
, switches
);
129 adios (NULL
, "-%s unknown", cp
);
132 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
134 print_help (buf
, switches
, 1);
137 print_version(invo_name
);
148 icachesw
= &rcachesw
;
151 icachesw
= &wcachesw
;
153 if (!(cp
= *argp
++) || *cp
== '-')
154 adios (NULL
, "missing argument to %s", argp
[-2]);
155 switch (*icachesw
= smatch (cp
, caches
)) {
157 ambigsw (cp
, caches
);
160 adios (NULL
, "%s unknown", cp
);
174 if (!(cp
= *argp
++) || *cp
== '-')
175 adios (NULL
, "missing argument to %s", argp
[-2]);
177 adios (NULL
, "too many parts (starting with %s), %d max",
183 if (!(cp
= *argp
++) || *cp
== '-')
184 adios (NULL
, "missing argument to %s", argp
[-2]);
186 adios (NULL
, "too many types (starting with %s), %d max",
192 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
193 adios (NULL
, "missing argument to %s", argp
[-2]);
194 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
198 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
199 adios (NULL
, "missing argument to %s", argp
[-2]);
200 outfile
= *cp
== '-' ? cp
: path (cp
, TFILE
);
210 if (!(cp
= *argp
++) || *cp
== '-')
211 adios (NULL
, "missing argument to %s", argp
[-2]);
212 if (save_clobber_policy (cp
)) {
213 adios (NULL
, "invalid argument, %s, to %s", argp
[-1],
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
= getcpy (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 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
280 adios (NULL
, "out of memory");
283 if ((ct
= parse_mime (file
))) {
286 ct
->c_storage
= outfile
;
291 * message(s) are coming from a folder
294 app_msgarg(&msgs
, "cur");
296 folder
= getfolder (1);
297 maildir
= m_maildir (folder
);
299 if (chdir (maildir
) == NOTOK
)
300 adios (maildir
, "unable to change directory to");
302 /* read folder and create message structure */
303 if (!(mp
= folder_read (folder
, 1)))
304 adios (NULL
, "unable to read folder %s", folder
);
306 /* check for empty folder */
308 adios (NULL
, "no messages in %s", folder
);
310 /* parse all the message ranges/sequences and set SELECTED */
311 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
312 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
314 seq_setprev (mp
); /* set the previous-sequence */
316 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
317 adios (NULL
, "out of memory");
320 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
321 if (is_selected(mp
, msgnum
)) {
324 msgnam
= m_name (msgnum
);
325 if ((ct
= parse_mime (msgnam
))) {
328 ct
->c_storage
= add (outfile
, NULL
);
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");