]> diplodocus.org Git - nmh/blob - uip/mhstore.c
We're not using the .Bu macro anymore.
[nmh] / uip / mhstore.c
1
2 /*
3 * mhstore.c -- store the contents of MIME messages
4 *
5 * $Id$
6 *
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.
10 */
11
12 #include <h/mh.h>
13 #include <fcntl.h>
14 #include <h/signals.h>
15 #include <h/md5.h>
16 #include <errno.h>
17 #include <signal.h>
18 #include <h/mts.h>
19 #include <h/tws.h>
20 #include <h/mime.h>
21 #include <h/mhparse.h>
22 #include <h/mhcachesbr.h>
23 #include <h/utils.h>
24
25 #ifdef HAVE_SYS_WAIT_H
26 # include <sys/wait.h>
27 #endif
28
29 static struct swit switches[] = {
30 #define AUTOSW 0
31 { "auto", 0 },
32 #define NAUTOSW 1
33 { "noauto", 0 },
34 #define CHECKSW 2
35 { "check", 0 },
36 #define NCHECKSW 3
37 { "nocheck", 0 },
38 #define VERBSW 4
39 { "verbose", 0 },
40 #define NVERBSW 5
41 { "noverbose", 0 },
42 #define FILESW 6 /* interface from show */
43 { "file file", 0 },
44 #define PARTSW 7
45 { "part number", 0 },
46 #define TYPESW 8
47 { "type content", 0 },
48 #define RCACHESW 9
49 { "rcache policy", 0 },
50 #define WCACHESW 10
51 { "wcache policy", 0 },
52 #define VERSIONSW 11
53 { "version", 0 },
54 #define HELPSW 12
55 { "help", 0 },
56
57 /*
58 * switches for debugging
59 */
60 #define DEBUGSW 13
61 { "debug", -5 },
62 { NULL, 0 }
63 };
64
65
66 /* mhparse.c */
67 extern int checksw;
68 extern char *tmp; /* directory to place temp files */
69
70 /* mhcachesbr.c */
71 extern int rcachesw;
72 extern int wcachesw;
73 extern char *cache_public;
74 extern char *cache_private;
75
76 /* mhstoresbr.c */
77 extern int autosw;
78 extern char *cwd; /* cache current working directory */
79
80 /* mhmisc.c */
81 extern int npart;
82 extern int ntype;
83 extern char *parts[NPARTS + 1];
84 extern char *types[NTYPES + 1];
85 extern int userrs;
86
87 int debugsw = 0;
88 int verbosw = 0;
89
90 /* The list of top-level contents to display */
91 CT *cts = NULL;
92
93 #define quitser pipeser
94
95 /* mhparse.c */
96 CT parse_mime (char *);
97
98 /* mhmisc.c */
99 int part_ok (CT, int);
100 int type_ok (CT, int);
101 void set_endian (void);
102 void flush_errors (void);
103
104 /* mhstoresbr.c */
105 void store_all_messages (CT *);
106
107 /* mhfree.c */
108 void free_content (CT);
109
110 /*
111 * static prototypes
112 */
113 static RETSIGTYPE pipeser (int);
114
115
116 int
117 main (int argc, char **argv)
118 {
119 int msgnum, *icachesw;
120 char *cp, *file = NULL, *folder = NULL;
121 char *maildir, buf[100], **argp;
122 char **arguments;
123 struct msgs_array msgs = { 0, 0, NULL };
124 struct msgs *mp = NULL;
125 CT ct, *ctp;
126 FILE *fp;
127
128 #ifdef LOCALE
129 setlocale(LC_ALL, "");
130 #endif
131 invo_name = r1bindex (argv[0], '/');
132
133 /* read user profile/context */
134 context_read();
135
136 arguments = getarguments (invo_name, argc, argv, 1);
137 argp = arguments;
138
139 /*
140 * Parse arguments
141 */
142 while ((cp = *argp++)) {
143 if (*cp == '-') {
144 switch (smatch (++cp, switches)) {
145 case AMBIGSW:
146 ambigsw (cp, switches);
147 done (1);
148 case UNKWNSW:
149 adios (NULL, "-%s unknown", cp);
150
151 case HELPSW:
152 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
153 invo_name);
154 print_help (buf, switches, 1);
155 done (1);
156 case VERSIONSW:
157 print_version(invo_name);
158 done (1);
159
160 case AUTOSW:
161 autosw++;
162 continue;
163 case NAUTOSW:
164 autosw = 0;
165 continue;
166
167 case RCACHESW:
168 icachesw = &rcachesw;
169 goto do_cache;
170 case WCACHESW:
171 icachesw = &wcachesw;
172 do_cache:
173 if (!(cp = *argp++) || *cp == '-')
174 adios (NULL, "missing argument to %s", argp[-2]);
175 switch (*icachesw = smatch (cp, caches)) {
176 case AMBIGSW:
177 ambigsw (cp, caches);
178 done (1);
179 case UNKWNSW:
180 adios (NULL, "%s unknown", cp);
181 default:
182 break;
183 }
184 continue;
185
186 case CHECKSW:
187 checksw++;
188 continue;
189 case NCHECKSW:
190 checksw = 0;
191 continue;
192
193 case PARTSW:
194 if (!(cp = *argp++) || *cp == '-')
195 adios (NULL, "missing argument to %s", argp[-2]);
196 if (npart >= NPARTS)
197 adios (NULL, "too many parts (starting with %s), %d max",
198 cp, NPARTS);
199 parts[npart++] = cp;
200 continue;
201
202 case TYPESW:
203 if (!(cp = *argp++) || *cp == '-')
204 adios (NULL, "missing argument to %s", argp[-2]);
205 if (ntype >= NTYPES)
206 adios (NULL, "too many types (starting with %s), %d max",
207 cp, NTYPES);
208 types[ntype++] = cp;
209 continue;
210
211 case FILESW:
212 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
213 adios (NULL, "missing argument to %s", argp[-2]);
214 file = *cp == '-' ? cp : path (cp, TFILE);
215 continue;
216
217 case VERBSW:
218 verbosw = 1;
219 continue;
220 case NVERBSW:
221 verbosw = 0;
222 continue;
223 case DEBUGSW:
224 debugsw = 1;
225 continue;
226 }
227 }
228 if (*cp == '+' || *cp == '@') {
229 if (folder)
230 adios (NULL, "only one folder at a time!");
231 else
232 folder = pluspath (cp);
233 } else
234 app_msgarg(&msgs, cp);
235 }
236
237 /* null terminate the list of acceptable parts/types */
238 parts[npart] = NULL;
239 types[ntype] = NULL;
240
241 set_endian ();
242
243 /*
244 * Check if we've specified an additional profile
245 */
246 if ((cp = getenv ("MHSTORE"))) {
247 if ((fp = fopen (cp, "r"))) {
248 readconfig ((struct node **) 0, fp, cp, 0);
249 fclose (fp);
250 } else {
251 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
252 }
253 }
254
255 /*
256 * Read the standard profile setup
257 */
258 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
259 readconfig ((struct node **) 0, fp, cp, 0);
260 fclose (fp);
261 }
262
263 /* Check for public cache location */
264 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
265 cache_public = NULL;
266
267 /* Check for private cache location */
268 if (!(cache_private = context_find (nmhprivcache)))
269 cache_private = ".cache";
270 cache_private = getcpy (m_maildir (cache_private));
271
272 /*
273 * Cache the current directory before we do any chdirs()'s.
274 */
275 cwd = getcpy (pwd());
276
277 /*
278 * Check for storage directory. If specified,
279 * then store temporary files there. Else we
280 * store them in standard nmh directory.
281 */
282 if ((cp = context_find (nmhstorage)) && *cp)
283 tmp = concat (cp, "/", invo_name, NULL);
284 else
285 tmp = add (m_maildir (invo_name), NULL);
286
287 if (!context_find ("path"))
288 free (path ("./", TFOLDER));
289
290 if (file && msgs.size)
291 adios (NULL, "cannot specify msg and file at same time!");
292
293 /*
294 * check if message is coming from file
295 */
296 if (file) {
297 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
298 adios (NULL, "out of memory");
299 ctp = cts;
300
301 if ((ct = parse_mime (file)));
302 *ctp++ = ct;
303 } else {
304 /*
305 * message(s) are coming from a folder
306 */
307 if (!msgs.size)
308 app_msgarg(&msgs, "cur");
309 if (!folder)
310 folder = getfolder (1);
311 maildir = m_maildir (folder);
312
313 if (chdir (maildir) == NOTOK)
314 adios (maildir, "unable to change directory to");
315
316 /* read folder and create message structure */
317 if (!(mp = folder_read (folder)))
318 adios (NULL, "unable to read folder %s", folder);
319
320 /* check for empty folder */
321 if (mp->nummsg == 0)
322 adios (NULL, "no messages in %s", folder);
323
324 /* parse all the message ranges/sequences and set SELECTED */
325 for (msgnum = 0; msgnum < msgs.size; msgnum++)
326 if (!m_convert (mp, msgs.msgs[msgnum]))
327 done (1);
328 seq_setprev (mp); /* set the previous-sequence */
329
330 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
331 adios (NULL, "out of memory");
332 ctp = cts;
333
334 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
335 if (is_selected(mp, msgnum)) {
336 char *msgnam;
337
338 msgnam = m_name (msgnum);
339 if ((ct = parse_mime (msgnam)))
340 *ctp++ = ct;
341 }
342 }
343 }
344
345 if (!*cts)
346 done (1);
347
348 userrs = 1;
349 SIGNAL (SIGQUIT, quitser);
350 SIGNAL (SIGPIPE, pipeser);
351
352 /*
353 * Get the associated umask for the relevant contents.
354 */
355 for (ctp = cts; *ctp; ctp++) {
356 struct stat st;
357
358 ct = *ctp;
359 if (type_ok (ct, 1) && !ct->c_umask) {
360 if (stat (ct->c_file, &st) != NOTOK)
361 ct->c_umask = ~(st.st_mode & 0777);
362 else
363 ct->c_umask = ~m_gmprot();
364 }
365 }
366
367 /*
368 * Store the message content
369 */
370 store_all_messages (cts);
371
372 /* Now free all the structures for the content */
373 for (ctp = cts; *ctp; ctp++)
374 free_content (*ctp);
375
376 free ((char *) cts);
377 cts = NULL;
378
379 /* If reading from a folder, do some updating */
380 if (mp) {
381 context_replace (pfolder, folder);/* update current folder */
382 seq_setcur (mp, mp->hghsel); /* update current message */
383 seq_save (mp); /* synchronize sequences */
384 context_save (); /* save the context file */
385 }
386
387 return done (0);
388 }
389
390
391 static RETSIGTYPE
392 pipeser (int i)
393 {
394 if (i == SIGQUIT) {
395 unlink ("core");
396 fflush (stdout);
397 fprintf (stderr, "\n");
398 fflush (stderr);
399 }
400
401 done (1);
402 /* NOTREACHED */
403 }
404
405
406 int
407 done (int status)
408 {
409 CT *ctp;
410
411 if ((ctp = cts))
412 for (; *ctp; ctp++)
413 free_content (*ctp);
414
415 exit (status);
416 return 1; /* dead code to satisfy the compiler */
417 }