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