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