]>
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>
25 #ifdef HAVE_SYS_WAIT_H
26 # include <sys/wait.h>
29 static struct swit switches
[] = {
42 #define FILESW 6 /* interface from show */
47 { "type content", 0 },
49 { "rcache policy", 0 },
51 { "wcache policy", 0 },
58 * switches for debugging
68 extern char *tmp
; /* directory to place temp files */
73 extern char *cache_public
;
74 extern char *cache_private
;
78 extern char *cwd
; /* cache current working directory */
83 extern char *parts
[NPARTS
+ 1];
84 extern char *types
[NTYPES
+ 1];
90 #define quitser pipeser
93 CT
parse_mime (char *);
96 int part_ok (CT
, int);
97 int type_ok (CT
, int);
98 void set_endian (void);
99 void flush_errors (void);
102 void store_all_messages (CT
*);
105 void free_content (CT
);
107 void freects_done (int);
112 static RETSIGTYPE
pipeser (int);
116 main (int argc
, char **argv
)
118 int msgnum
, *icachesw
;
119 char *cp
, *file
= NULL
, *folder
= NULL
;
120 char *maildir
, buf
[100], **argp
;
122 struct msgs_array msgs
= { 0, 0, NULL
};
123 struct msgs
*mp
= NULL
;
130 setlocale(LC_ALL
, "");
132 invo_name
= r1bindex (argv
[0], '/');
134 /* read user profile/context */
137 arguments
= getarguments (invo_name
, argc
, argv
, 1);
143 while ((cp
= *argp
++)) {
145 switch (smatch (++cp
, switches
)) {
147 ambigsw (cp
, switches
);
150 adios (NULL
, "-%s unknown", cp
);
153 snprintf (buf
, sizeof(buf
), "%s [+folder] [msgs] [switches]",
155 print_help (buf
, switches
, 1);
158 print_version(invo_name
);
169 icachesw
= &rcachesw
;
172 icachesw
= &wcachesw
;
174 if (!(cp
= *argp
++) || *cp
== '-')
175 adios (NULL
, "missing argument to %s", argp
[-2]);
176 switch (*icachesw
= smatch (cp
, caches
)) {
178 ambigsw (cp
, caches
);
181 adios (NULL
, "%s unknown", cp
);
195 if (!(cp
= *argp
++) || *cp
== '-')
196 adios (NULL
, "missing argument to %s", argp
[-2]);
198 adios (NULL
, "too many parts (starting with %s), %d max",
204 if (!(cp
= *argp
++) || *cp
== '-')
205 adios (NULL
, "missing argument to %s", argp
[-2]);
207 adios (NULL
, "too many types (starting with %s), %d max",
213 if (!(cp
= *argp
++) || (*cp
== '-' && cp
[1]))
214 adios (NULL
, "missing argument to %s", argp
[-2]);
215 file
= *cp
== '-' ? cp
: path (cp
, TFILE
);
229 if (*cp
== '+' || *cp
== '@') {
231 adios (NULL
, "only one folder at a time!");
233 folder
= pluspath (cp
);
235 app_msgarg(&msgs
, cp
);
238 /* null terminate the list of acceptable parts/types */
245 * Check if we've specified an additional profile
247 if ((cp
= getenv ("MHSTORE"))) {
248 if ((fp
= fopen (cp
, "r"))) {
249 readconfig ((struct node
**) 0, fp
, cp
, 0);
252 admonish ("", "unable to read $MHSTORE profile (%s)", cp
);
257 * Read the standard profile setup
259 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
260 readconfig ((struct node
**) 0, fp
, cp
, 0);
264 /* Check for public cache location */
265 if ((cache_public
= context_find (nmhcache
)) && *cache_public
!= '/')
268 /* Check for private cache location */
269 if (!(cache_private
= context_find (nmhprivcache
)))
270 cache_private
= ".cache";
271 cache_private
= getcpy (m_maildir (cache_private
));
274 * Cache the current directory before we do any chdirs()'s.
276 cwd
= getcpy (pwd());
279 * Check for storage directory. If specified,
280 * then store temporary files there. Else we
281 * store them in standard nmh directory.
283 if ((cp
= context_find (nmhstorage
)) && *cp
)
284 tmp
= concat (cp
, "/", invo_name
, NULL
);
286 tmp
= add (m_maildir (invo_name
), NULL
);
288 if (!context_find ("path"))
289 free (path ("./", TFOLDER
));
291 if (file
&& msgs
.size
)
292 adios (NULL
, "cannot specify msg and file at same time!");
295 * check if message is coming from file
298 if (!(cts
= (CT
*) calloc ((size_t) 2, sizeof(*cts
))))
299 adios (NULL
, "out of memory");
302 if ((ct
= parse_mime (file
)));
306 * message(s) are coming from a folder
309 app_msgarg(&msgs
, "cur");
311 folder
= getfolder (1);
312 maildir
= m_maildir (folder
);
314 if (chdir (maildir
) == NOTOK
)
315 adios (maildir
, "unable to change directory to");
317 /* read folder and create message structure */
318 if (!(mp
= folder_read (folder
)))
319 adios (NULL
, "unable to read folder %s", folder
);
321 /* check for empty folder */
323 adios (NULL
, "no messages in %s", folder
);
325 /* parse all the message ranges/sequences and set SELECTED */
326 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
327 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
329 seq_setprev (mp
); /* set the previous-sequence */
331 if (!(cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof(*cts
))))
332 adios (NULL
, "out of memory");
335 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
336 if (is_selected(mp
, msgnum
)) {
339 msgnam
= m_name (msgnum
);
340 if ((ct
= parse_mime (msgnam
)))
350 SIGNAL (SIGQUIT
, quitser
);
351 SIGNAL (SIGPIPE
, pipeser
);
354 * Get the associated umask for the relevant contents.
356 for (ctp
= cts
; *ctp
; ctp
++) {
360 if (type_ok (ct
, 1) && !ct
->c_umask
) {
361 if (stat (ct
->c_file
, &st
) != NOTOK
)
362 ct
->c_umask
= ~(st
.st_mode
& 0777);
364 ct
->c_umask
= ~m_gmprot();
369 * Store the message content
371 store_all_messages (cts
);
373 /* Now free all the structures for the content */
374 for (ctp
= cts
; *ctp
; ctp
++)
380 /* If reading from a folder, do some updating */
382 context_replace (pfolder
, folder
);/* update current folder */
383 seq_setcur (mp
, mp
->hghsel
); /* update current message */
384 seq_save (mp
); /* synchronize sequences */
385 context_save (); /* save the context file */
399 fprintf (stderr
, "\n");