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