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