]>
diplodocus.org Git - nmh/blob - uip/mhstore.c
3 * mhstore.c -- store the contents of MIME messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/signals.h>
21 #include <h/mhparse.h>
22 #include <h/mhcachesbr.h>
24 #ifdef HAVE_SYS_WAIT_H
25 # include <sys/wait.h>
29 * We allocate space for message names (msgs array)
30 * this number of elements at a time.
35 static struct swit switches
[] = {
48 #define FILESW 6 /* interface from show */
53 { "type content", 0 },
55 { "rcache policy", 0 },
57 { "wcache policy", 0 },
64 * switches for debugging
74 extern char *tmp
; /* directory to place temp files */
79 extern char *cache_public
;
80 extern char *cache_private
;
84 extern char *cwd
; /* cache current working directory */
89 extern char *parts
[NPARTS
+ 1];
90 extern char *types
[NTYPES
+ 1];
96 /* The list of top-level contents to display */
99 #define quitser pipeser
102 CT
parse_mime (char *);
105 int part_ok (CT
, int);
106 int type_ok (CT
, int);
107 void set_endian (void);
108 void flush_errors (void);
111 void store_all_messages (CT
*);
114 void free_content (CT
);
119 static RETSIGTYPE
pipeser (int);
123 main (int argc
, char **argv
)
125 int nummsgs
, maxmsgs
, msgnum
, *icachesw
;
126 char *cp
, *file
= NULL
, *folder
= NULL
;
127 char *maildir
, buf
[100], **argp
;
128 char **arguments
, **msgs
;
129 struct msgs
*mp
= NULL
;
134 setlocale(LC_ALL
, "");
136 invo_name
= r1bindex (argv
[0], '/');
138 /* read user profile/context */
141 arguments
= getarguments (invo_name
, argc
, argv
, 1);
145 * Allocate the initial space to record message
146 * names, ranges, and sequences.
150 if (!(msgs
= (char **) malloc ((size_t) (maxmsgs
* sizeof(*msgs
)))))
151 adios (NULL
, "unable to allocate storage");
156 while ((cp
= *argp
++)) {
158 switch (smatch (++cp
, switches
)) {
160 ambigsw (cp
, switches
);
163 adios (NULL
, "-%s unknown", cp
);
166 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
168 print_help (buf
, switches
, 1);
171 print_version(invo_name
);
182 icachesw
= &rcachesw
;
185 icachesw
= &wcachesw
;
187 if (!(cp
= *argp
++) || *cp
== '-')
188 adios (NULL
, "missing argument to %s", argp
[-2]);
189 switch (*icachesw
= smatch (cp
, caches
)) {
191 ambigsw (cp
, caches
);
194 adios (NULL
, "%s unknown", cp
);
208 if (!(cp
= *argp
++) || *cp
== '-')
209 adios (NULL
, "missing argument to %s", argp
[-2]);
211 adios (NULL
, "too many parts (starting with %s), %d max",
217 if (!(cp
= *argp
++) || *cp
== '-')
218 adios (NULL
, "missing argument to %s", argp
[-2]);
220 adios (NULL
, "too many types (starting with %s), %d max",
226 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
227 adios (NULL
, "missing argument to %s", argp
[-2]);
228 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
242 if (*cp
== '+' || *cp
== '@') {
244 adios (NULL
, "only one folder at a time!");
246 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
249 * Check if we need to allocate more space
250 * for message names/ranges/sequences.
252 if (nummsgs
>= maxmsgs
) {
254 if (!(msgs
= (char **) realloc (msgs
,
255 (size_t) (maxmsgs
* sizeof(*msgs
)))))
256 adios (NULL
, "unable to reallocate msgs storage");
258 msgs
[nummsgs
++] = cp
;
262 /* null terminate the list of acceptable parts/types */
269 * Check if we've specified an additional profile
271 if ((cp
= getenv ("MHSTORE"))) {
272 if ((fp
= fopen (cp
, "r"))) {
273 readconfig ((struct node
**) 0, fp
, cp
, 0);
276 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
281 * Read the standard profile setup
283 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
284 readconfig ((struct node
**) 0, fp
, cp
, 0);
288 /* Check for public cache location */
289 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
292 /* Check for private cache location */
293 if (!(cache_private
= context_find (nmhprivcache
)))
294 cache_private
= ".cache";
295 cache_private
= getcpy (m_maildir (cache_private
));
298 * Cache the current directory before we do any chdirs()'s.
300 cwd
= getcpy (pwd());
303 * Check for storage directory. If specified,
304 * then store temporary files there. Else we
305 * store them in standard nmh directory.
307 if ((cp
= context_find (nmhstorage
)) && *cp
)
308 tmp
= concat (cp
, "/", invo_name
, NULL
);
310 tmp
= add (m_maildir (invo_name
), NULL
);
312 if (!context_find ("path"))
313 free (path ("./", TFOLDER
));
316 adios (NULL
, "cannot specify msg and file at same time!");
319 * check if message is coming from file
322 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
323 adios (NULL
, "out of memory");
326 if ((ct
= parse_mime (file
)));
330 * message(s) are coming from a folder
333 msgs
[nummsgs
++] = "cur";
335 folder
= getfolder (1);
336 maildir
= m_maildir (folder
);
338 if (chdir (maildir
) == NOTOK
)
339 adios (maildir
, "unable to change directory to");
341 /* read folder and create message structure */
342 if (!(mp
= folder_read (folder
)))
343 adios (NULL
, "unable to read folder %s", folder
);
345 /* check for empty folder */
347 adios (NULL
, "no messages in %s", folder
);
349 /* parse all the message ranges/sequences and set SELECTED */
350 for (msgnum
= 0; msgnum
< nummsgs
; msgnum
++)
351 if (!m_convert (mp
, msgs
[msgnum
]))
353 seq_setprev (mp
); /* set the previous-sequence */
355 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
356 adios (NULL
, "out of memory");
359 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
360 if (is_selected(mp
, msgnum
)) {
363 msgnam
= m_name (msgnum
);
364 if ((ct
= parse_mime (msgnam
)))
374 SIGNAL (SIGQUIT
, quitser
);
375 SIGNAL (SIGPIPE
, pipeser
);
378 * Get the associated umask for the relevant contents.
380 for (ctp
= cts
; *ctp
; ctp
++) {
384 if (type_ok (ct
, 1) && !ct
->c_umask
) {
385 if (stat (ct
->c_file
, &st
) != NOTOK
)
386 ct
->c_umask
= ~(st
.st_mode
& 0777);
388 ct
->c_umask
= ~m_gmprot();
393 * Store the message content
395 store_all_messages (cts
);
397 /* Now free all the structures for the content */
398 for (ctp
= cts
; *ctp
; ctp
++)
404 /* If reading from a folder, do some updating */
406 context_replace (pfolder
, folder
);/* update current folder */
407 seq_setcur (mp
, mp
->hghsel
); /* update current message */
408 seq_save (mp
); /* synchronize sequences */
409 context_save (); /* save the context file */
422 fprintf (stderr
, "\n");
441 return 1; /* dead code to satisfy the compiler */