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