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