]>
diplodocus.org Git - nmh/blob - uip/mhstore.c
3 * mhstore.c -- store the contents of MIME messages
10 #include <h/signals.h>
14 #include <zotnet/mts/mts.h>
15 #include <zotnet/tws/tws.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
20 #ifdef HAVE_SYS_WAIT_H
21 # include <sys/wait.h>
25 * We allocate space for message names (msgs array)
26 * this number of elements at a time.
31 static struct swit switches
[] = {
44 #define FILESW 6 /* interface from show */
49 { "type content", 0 },
51 { "rcache policy", 0 },
53 { "wcache policy", 0 },
60 * switches for debugging
72 extern char *tmp
; /* directory to place temp files */
77 extern char *cache_public
;
78 extern char *cache_private
;
82 extern char *cwd
; /* cache current working directory */
87 extern char *parts
[NPARTS
+ 1];
88 extern char *types
[NTYPES
+ 1];
94 /* The list of top-level contents to display */
97 #define quitser pipeser
100 CT
parse_mime (char *);
103 int part_ok (CT
, int);
104 int type_ok (CT
, int);
105 void set_endian (void);
106 void flush_errors (void);
109 void store_all_messages (CT
*);
112 void free_content (CT
);
117 static RETSIGTYPE
pipeser (int);
121 main (int argc
, char **argv
)
123 int nummsgs
, maxmsgs
, msgnum
, *icachesw
;
124 char *cp
, *file
= NULL
, *folder
= NULL
;
125 char *maildir
, buf
[100], **argp
;
126 char **arguments
, **msgs
;
127 struct msgs
*mp
= NULL
;
132 setlocale(LC_ALL
, "");
134 invo_name
= r1bindex (argv
[0], '/');
136 /* read user profile/context */
139 arguments
= getarguments (invo_name
, argc
, argv
, 1);
143 * Allocate the initial space to record message
144 * names, ranges, and sequences.
148 if (!(msgs
= (char **) malloc ((size_t) (maxmsgs
* sizeof(*msgs
)))))
149 adios (NULL
, "unable to allocate storage");
154 while ((cp
= *argp
++)) {
156 switch (smatch (++cp
, switches
)) {
158 ambigsw (cp
, switches
);
161 adios (NULL
, "-%s unknown", cp
);
164 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
166 print_help (buf
, switches
, 1);
169 print_version(invo_name
);
180 icachesw
= &rcachesw
;
183 icachesw
= &wcachesw
;
185 if (!(cp
= *argp
++) || *cp
== '-')
186 adios (NULL
, "missing argument to %s", argp
[-2]);
187 switch (*icachesw
= smatch (cp
, caches
)) {
189 ambigsw (cp
, caches
);
192 adios (NULL
, "%s unknown", cp
);
206 if (!(cp
= *argp
++) || *cp
== '-')
207 adios (NULL
, "missing argument to %s", argp
[-2]);
209 adios (NULL
, "too many parts (starting with %s), %d max",
215 if (!(cp
= *argp
++) || *cp
== '-')
216 adios (NULL
, "missing argument to %s", argp
[-2]);
218 adios (NULL
, "too many types (starting with %s), %d max",
224 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
225 adios (NULL
, "missing argument to %s", argp
[-2]);
226 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
240 if (*cp
== '+' || *cp
== '@') {
242 adios (NULL
, "only one folder at a time!");
244 folder
= path (cp
+ 1, *cp
== '+' ? TFOLDER
: TSUBCWF
);
247 * Check if we need to allocate more space
248 * for message names/ranges/sequences.
250 if (nummsgs
>= maxmsgs
) {
252 if (!(msgs
= (char **) realloc (msgs
,
253 (size_t) (maxmsgs
* sizeof(*msgs
)))))
254 adios (NULL
, "unable to reallocate msgs storage");
256 msgs
[nummsgs
++] = cp
;
260 /* null terminate the list of acceptable parts/types */
267 * Check if we've specified an additional profile
269 if ((cp
= getenv ("MHSTORE"))) {
270 if ((fp
= fopen (cp
, "r"))) {
271 readconfig ((struct node
**) 0, fp
, cp
, 0);
274 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
279 * Read the standard profile setup
281 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
282 readconfig ((struct node
**) 0, fp
, cp
, 0);
286 /* Check for public cache location */
287 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
290 /* Check for private cache location */
291 if (!(cache_private
= context_find (nmhprivcache
)))
292 cache_private
= ".cache";
293 cache_private
= getcpy (m_maildir (cache_private
));
296 * Cache the current directory before we do any chdirs()'s.
298 cwd
= getcpy (pwd());
301 * Check for storage directory. If specified,
302 * then store temporary files there. Else we
303 * store them in standard nmh directory.
305 if ((cp
= context_find (nmhstorage
)) && *cp
)
306 tmp
= concat (cp
, "/", invo_name
, NULL
);
308 tmp
= add (m_maildir (invo_name
), NULL
);
310 if (!context_find ("path"))
311 free (path ("./", TFOLDER
));
314 adios (NULL
, "cannot specify msg and file at same time!");
317 * check if message is coming from file
320 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
321 adios (NULL
, "out of memory");
324 if ((ct
= parse_mime (file
)));
328 * message(s) are coming from a folder
331 msgs
[nummsgs
++] = "cur";
333 folder
= getfolder (1);
334 maildir
= m_maildir (folder
);
336 if (chdir (maildir
) == NOTOK
)
337 adios (maildir
, "unable to change directory to");
339 /* read folder and create message structure */
340 if (!(mp
= folder_read (folder
)))
341 adios (NULL
, "unable to read folder %s", folder
);
343 /* check for empty folder */
345 adios (NULL
, "no messages in %s", folder
);
347 /* parse all the message ranges/sequences and set SELECTED */
348 for (msgnum
= 0; msgnum
< nummsgs
; msgnum
++)
349 if (!m_convert (mp
, msgs
[msgnum
]))
351 seq_setprev (mp
); /* set the previous-sequence */
353 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
354 adios (NULL
, "out of memory");
357 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
358 if (is_selected(mp
, msgnum
)) {
361 msgnam
= m_name (msgnum
);
362 if ((ct
= parse_mime (msgnam
)))
372 SIGNAL (SIGQUIT
, quitser
);
373 SIGNAL (SIGPIPE
, pipeser
);
376 * Get the associated umask for the relevant contents.
378 for (ctp
= cts
; *ctp
; ctp
++) {
382 if (type_ok (ct
, 1) && !ct
->c_umask
) {
383 if (stat (ct
->c_file
, &st
) != NOTOK
)
384 ct
->c_umask
= ~(st
.st_mode
& 0777);
386 ct
->c_umask
= ~m_gmprot();
391 * Store the message content
393 store_all_messages (cts
);
395 /* Now free all the structures for the content */
396 for (ctp
= cts
; *ctp
; ctp
++)
402 /* If reading from a folder, do some updating */
404 context_replace (pfolder
, folder
);/* update current folder */
405 seq_setcur (mp
, mp
->hghsel
); /* update current message */
406 seq_save (mp
); /* synchronize sequences */
407 context_save (); /* save the context file */
420 fprintf (stderr
, "\n");
439 return 1; /* dead code to satisfy the compiler */