]>
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
76 extern char *tmp
; /* directory to place temp files */
81 extern char *cache_public
;
82 extern char *cache_private
;
86 extern char *cwd
; /* cache current working directory */
91 extern char *parts
[NPARTS
+ 1];
92 extern char *types
[NTYPES
+ 1];
98 /* The list of top-level contents to display */
101 #define quitser pipeser
104 CT
parse_mime (char *);
107 int part_ok (CT
, int);
108 int type_ok (CT
, int);
109 void set_endian (void);
110 void flush_errors (void);
113 void store_all_messages (CT
*);
116 void free_content (CT
);
121 static RETSIGTYPE
pipeser (int);
125 main (int argc
, char **argv
)
127 int nummsgs
, maxmsgs
, msgnum
, *icachesw
;
128 char *cp
, *file
= NULL
, *folder
= NULL
;
129 char *maildir
, buf
[100], **argp
;
130 char **arguments
, **msgs
;
131 struct msgs
*mp
= NULL
;
136 setlocale(LC_ALL
, "");
138 invo_name
= r1bindex (argv
[0], '/');
140 /* read user profile/context */
143 arguments
= getarguments (invo_name
, argc
, argv
, 1);
147 * Allocate the initial space to record message
148 * names, ranges, and sequences.
152 if (!(msgs
= (char **) malloc ((size_t) (maxmsgs
* sizeof(*msgs
)))))
153 adios (NULL
, "unable to allocate storage");
158 while ((cp
= *argp
++)) {
160 switch (smatch (++cp
, switches
)) {
162 ambigsw (cp
, switches
);
165 adios (NULL
, "-%s unknown", cp
);
168 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
170 print_help (buf
, switches
, 1);
173 print_version(invo_name
);
184 icachesw
= &rcachesw
;
187 icachesw
= &wcachesw
;
189 if (!(cp
= *argp
++) || *cp
== '-')
190 adios (NULL
, "missing argument to %s", argp
[-2]);
191 switch (*icachesw
= smatch (cp
, caches
)) {
193 ambigsw (cp
, caches
);
196 adios (NULL
, "%s unknown", cp
);
210 if (!(cp
= *argp
++) || *cp
== '-')
211 adios (NULL
, "missing argument to %s", argp
[-2]);
213 adios (NULL
, "too many parts (starting with %s), %d max",
219 if (!(cp
= *argp
++) || *cp
== '-')
220 adios (NULL
, "missing argument to %s", argp
[-2]);
222 adios (NULL
, "too many types (starting with %s), %d max",
228 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
229 adios (NULL
, "missing argument to %s", argp
[-2]);
230 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
244 if (*cp
== '+' || *cp
== '@') {
246 adios (NULL
, "only one folder at a time!");
248 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
251 * Check if we need to allocate more space
252 * for message names/ranges/sequences.
254 if (nummsgs
>= maxmsgs
) {
256 if (!(msgs
= (char **) realloc (msgs
,
257 (size_t) (maxmsgs
* sizeof(*msgs
)))))
258 adios (NULL
, "unable to reallocate msgs storage");
260 msgs
[nummsgs
++] = cp
;
264 /* null terminate the list of acceptable parts/types */
271 * Check if we've specified an additional profile
273 if ((cp
= getenv ("MHSTORE"))) {
274 if ((fp
= fopen (cp
, "r"))) {
275 readconfig ((struct node
**) 0, fp
, cp
, 0);
278 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
283 * Read the standard profile setup
285 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
286 readconfig ((struct node
**) 0, fp
, cp
, 0);
290 /* Check for public cache location */
291 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
294 /* Check for private cache location */
295 if (!(cache_private
= context_find (nmhprivcache
)))
296 cache_private
= ".cache";
297 cache_private
= getcpy (m_maildir (cache_private
));
300 * Cache the current directory before we do any chdirs()'s.
302 cwd
= getcpy (pwd());
305 * Check for storage directory. If specified,
306 * then store temporary files there. Else we
307 * store them in standard nmh directory.
309 if ((cp
= context_find (nmhstorage
)) && *cp
)
310 tmp
= concat (cp
, "/", invo_name
, NULL
);
312 tmp
= add (m_maildir (invo_name
), NULL
);
314 if (!context_find ("path"))
315 free (path ("./", TFOLDER
));
318 adios (NULL
, "cannot specify msg and file at same time!");
321 * check if message is coming from file
324 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
325 adios (NULL
, "out of memory");
328 if ((ct
= parse_mime (file
)));
332 * message(s) are coming from a folder
335 msgs
[nummsgs
++] = "cur";
337 folder
= getfolder (1);
338 maildir
= m_maildir (folder
);
340 if (chdir (maildir
) == NOTOK
)
341 adios (maildir
, "unable to change directory to");
343 /* read folder and create message structure */
344 if (!(mp
= folder_read (folder
)))
345 adios (NULL
, "unable to read folder %s", folder
);
347 /* check for empty folder */
349 adios (NULL
, "no messages in %s", folder
);
351 /* parse all the message ranges/sequences and set SELECTED */
352 for (msgnum
= 0; msgnum
< nummsgs
; msgnum
++)
353 if (!m_convert (mp
, msgs
[msgnum
]))
355 seq_setprev (mp
); /* set the previous-sequence */
357 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
358 adios (NULL
, "out of memory");
361 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
362 if (is_selected(mp
, msgnum
)) {
365 msgnam
= m_name (msgnum
);
366 if ((ct
= parse_mime (msgnam
)))
376 SIGNAL (SIGQUIT
, quitser
);
377 SIGNAL (SIGPIPE
, pipeser
);
380 * Get the associated umask for the relevant contents.
382 for (ctp
= cts
; *ctp
; ctp
++) {
386 if (type_ok (ct
, 1) && !ct
->c_umask
) {
387 if (stat (ct
->c_file
, &st
) != NOTOK
)
388 ct
->c_umask
= ~(st
.st_mode
& 0777);
390 ct
->c_umask
= ~m_gmprot();
395 * Store the message content
397 store_all_messages (cts
);
399 /* Now free all the structures for the content */
400 for (ctp
= cts
; *ctp
; ctp
++)
406 /* If reading from a folder, do some updating */
408 context_replace (pfolder
, folder
);/* update current folder */
409 seq_setcur (mp
, mp
->hghsel
); /* update current message */
410 seq_save (mp
); /* synchronize sequences */
411 context_save (); /* save the context file */
424 fprintf (stderr
, "\n");
443 return 1; /* dead code to satisfy the compiler */