]> diplodocus.org Git - nmh/blob - uip/mhstore.c
6ea99f30ea1d32b1a6030a6ee89bc7cecd9df726
[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/m_name.h"
10 #include "sbr/m_gmprot.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/seq_setprev.h"
13 #include "sbr/seq_setcur.h"
14 #include "sbr/seq_save.h"
15 #include "sbr/smatch.h"
16 #include "sbr/m_convert.h"
17 #include "sbr/getfolder.h"
18 #include "sbr/folder_read.h"
19 #include "sbr/context_save.h"
20 #include "sbr/context_replace.h"
21 #include "sbr/context_find.h"
22 #include "sbr/readconfig.h"
23 #include "sbr/ambigsw.h"
24 #include "sbr/path.h"
25 #include "sbr/print_version.h"
26 #include "sbr/print_help.h"
27 #include "sbr/error.h"
28 #include <fcntl.h>
29 #include "h/signals.h"
30 #include "h/mts.h"
31 #include "h/tws.h"
32 #include "h/mime.h"
33 #include "h/mhparse.h"
34 #include "h/mhcachesbr.h"
35 #include "h/done.h"
36 #include "h/utils.h"
37 #include "mhmisc.h"
38 #include "sbr/m_maildir.h"
39 #include "mhfree.h"
40
41 #define MHSTORE_SWITCHES \
42 X("auto", 0, AUTOSW) \
43 X("noauto", 0, NAUTOSW) \
44 X("check", -5, CHECKSW) \
45 X("nocheck", -7, NCHECKSW) \
46 X("verbose", 0, VERBSW) \
47 X("noverbose", 0, NVERBSW) \
48 X("file file", 0, FILESW) /* interface from show */ \
49 X("outfile outfile", 0, OUTFILESW) \
50 X("part number", 0, PARTSW) \
51 X("type content", 0, TYPESW) \
52 X("prefer content", 0, PREFERSW) \
53 X("noprefer", 0, NPREFERSW) \
54 X("rcache policy", 0, RCACHESW) \
55 X("wcache policy", 0, WCACHESW) \
56 X("version", 0, VERSIONSW) \
57 X("help", 0, HELPSW) \
58 X("clobber always|auto|suffix|ask|never", 0, CLOBBERSW) \
59 X("debug", -5, DEBUGSW) \
60
61 #define X(sw, minchars, id) id,
62 DEFINE_SWITCH_ENUM(MHSTORE);
63 #undef X
64
65 #define X(sw, minchars, id) { sw, minchars, id },
66 DEFINE_SWITCH_ARRAY(MHSTORE, switches);
67 #undef X
68
69
70 #define quitser pipeser
71
72 /* mhparse.c */
73 int debugsw = 0;
74
75 /*
76 * static prototypes
77 */
78 static void pipeser (int);
79
80
81 int
82 main (int argc, char **argv)
83 {
84 int msgnum, *icachesw;
85 bool autosw = false;
86 /* verbosw defaults to 1 for backward compatibility. */
87 bool verbosw = true;
88 const char *clobbersw = "always";
89 char *cp, *file = NULL, *outfile = NULL, *folder = NULL;
90 char *maildir, buf[100], **argp;
91 char **arguments;
92 char *cwd;
93 struct msgs_array msgs = { 0, 0, NULL };
94 struct msgs *mp = NULL;
95 CT ct, *ctp;
96 FILE *fp;
97 int files_not_clobbered;
98 mhstoreinfo_t info;
99
100 if (nmh_init(argv[0], true, true)) { return 1; }
101
102 set_done(freects_done);
103
104 arguments = getarguments (invo_name, argc, argv, 1);
105 argp = arguments;
106
107 /*
108 * Parse arguments
109 */
110 while ((cp = *argp++)) {
111 if (*cp == '-') {
112 switch (smatch (++cp, switches)) {
113 case AMBIGSW:
114 ambigsw (cp, switches);
115 done (1);
116 case UNKWNSW:
117 die("-%s unknown", cp);
118
119 case HELPSW:
120 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
121 invo_name);
122 print_help (buf, switches, 1);
123 done (0);
124 case VERSIONSW:
125 print_version(invo_name);
126 done (0);
127
128 case AUTOSW:
129 autosw = true;
130 continue;
131 case NAUTOSW:
132 autosw = false;
133 continue;
134
135 case RCACHESW:
136 icachesw = &rcachesw;
137 goto do_cache;
138 case WCACHESW:
139 icachesw = &wcachesw;
140 do_cache:
141 if (!(cp = *argp++) || *cp == '-')
142 die("missing argument to %s", argp[-2]);
143 switch (*icachesw = smatch (cp, cache_policy)) {
144 case AMBIGSW:
145 ambigsw (cp, cache_policy);
146 done (1);
147 case UNKWNSW:
148 die("%s unknown", cp);
149 default:
150 break;
151 }
152 continue;
153
154 case CHECKSW:
155 case NCHECKSW:
156 /* Currently a NOP */
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 }