]> diplodocus.org Git - nmh/blob - uip/mhstore.c
21bf0d18ca21c1c7ed810e3c440a85a24a77a51f
[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_gmprot.h"
10 #include "sbr/getarguments.h"
11 #include "sbr/seq_setprev.h"
12 #include "sbr/seq_setcur.h"
13 #include "sbr/seq_save.h"
14 #include "sbr/smatch.h"
15 #include "sbr/m_convert.h"
16 #include "sbr/getfolder.h"
17 #include "sbr/folder_read.h"
18 #include "sbr/context_save.h"
19 #include "sbr/context_replace.h"
20 #include "sbr/context_find.h"
21 #include "sbr/readconfig.h"
22 #include "sbr/ambigsw.h"
23 #include "sbr/path.h"
24 #include "sbr/print_version.h"
25 #include "sbr/print_help.h"
26 #include "sbr/error.h"
27 #include <fcntl.h>
28 #include "h/signals.h"
29 #include "h/md5.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", 0, CHECKSW) \
45 X("nocheck", 0, 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 checksw++;
156 continue;
157 case NCHECKSW:
158 checksw = 0;
159 continue;
160
161 case PARTSW:
162 if (!(cp = *argp++) || *cp == '-')
163 die("missing argument to %s", argp[-2]);
164 if (npart >= NPARTS)
165 die("too many parts (starting with %s), %d max",
166 cp, NPARTS);
167 parts[npart++] = cp;
168 continue;
169
170 case TYPESW:
171 if (!(cp = *argp++) || *cp == '-')
172 die("missing argument to %s", argp[-2]);
173 if (ntype >= NTYPES)
174 die("too many types (starting with %s), %d max",
175 cp, NTYPES);
176 types[ntype++] = cp;
177 continue;
178
179 case PREFERSW:
180 if (!(cp = *argp++) || *cp == '-')
181 die("missing argument to %s", argp[-2]);
182 if (npreferred >= NPREFS)
183 die("too many preferred types (starting with %s), %d max",
184 cp, NPREFS);
185 mime_preference[npreferred].type = cp;
186 cp = strchr(cp, '/');
187 if (cp) *cp++ = '\0';
188 mime_preference[npreferred++].subtype = cp;
189 continue;
190
191 case NPREFERSW:
192 npreferred = 0;
193 continue;
194
195 case FILESW:
196 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
197 die("missing argument to %s", argp[-2]);
198 file = *cp == '-' ? cp : path (cp, TFILE);
199 continue;
200
201 case OUTFILESW:
202 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
203 die("missing argument to %s", argp[-2]);
204 outfile = *cp == '-' ? cp : path (cp, TFILE);
205 continue;
206
207 case VERBSW:
208 verbosw = true;
209 continue;
210 case NVERBSW:
211 verbosw = false;
212 continue;
213 case CLOBBERSW:
214 if (!(cp = *argp++) || *cp == '-')
215 die("missing argument to %s", argp[-2]);
216 clobbersw = cp;
217 continue;
218 case DEBUGSW:
219 debugsw = 1;
220 continue;
221 }
222 }
223 if (*cp == '+' || *cp == '@') {
224 if (folder)
225 die("only one folder at a time!");
226 folder = pluspath (cp);
227 } else
228 app_msgarg(&msgs, cp);
229 }
230
231 /* null terminate the list of acceptable parts/types */
232 parts[npart] = NULL;
233 types[ntype] = NULL;
234
235 /*
236 * Check if we've specified an additional profile
237 */
238 if ((cp = getenv ("MHSTORE"))) {
239 if ((fp = fopen (cp, "r"))) {
240 readconfig(NULL, fp, cp, 0);
241 fclose (fp);
242 } else {
243 admonish ("", "unable to read $MHSTORE profile (%s)", cp);
244 }
245 }
246
247 /*
248 * Read the standard profile setup
249 */
250 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
251 readconfig(NULL, fp, cp, 0);
252 fclose (fp);
253 }
254
255 /* Check for public cache location */
256 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
257 cache_public = NULL;
258
259 /* Check for private cache location */
260 if (!(cache_private = context_find (nmhprivcache)))
261 cache_private = ".cache";
262 cache_private = mh_xstrdup(m_maildir(cache_private));
263
264 /*
265 * Cache the current directory before we do any chdirs()'s.
266 */
267 cwd = mh_xstrdup(pwd());
268
269 if (!context_find ("path"))
270 free (path ("./", TFOLDER));
271
272 if (file && msgs.size)
273 die("cannot specify msg and file at same time!");
274
275 /*
276 * check if message is coming from file
277 */
278 if (file) {
279 cts = mh_xcalloc(2, sizeof *cts);
280 ctp = cts;
281
282 if ((ct = parse_mime (file))) {
283 *ctp++ = ct;
284 if (outfile) {
285 ct->c_storage = mh_xstrdup(outfile);
286 }
287 }
288 } else {
289 /*
290 * message(s) are coming from a folder
291 */
292 if (!msgs.size)
293 app_msgarg(&msgs, "cur");
294 if (!folder)
295 folder = getfolder (1);
296 maildir = m_maildir (folder);
297
298 if (chdir (maildir) == NOTOK)
299 adios (maildir, "unable to change directory to");
300
301 /* read folder and create message structure */
302 if (!(mp = folder_read (folder, 1)))
303 die("unable to read folder %s", folder);
304
305 /* check for empty folder */
306 if (mp->nummsg == 0)
307 die("no messages in %s", folder);
308
309 /* parse all the message ranges/sequences and set SELECTED */
310 for (msgnum = 0; msgnum < msgs.size; msgnum++)
311 if (!m_convert (mp, msgs.msgs[msgnum]))
312 done (1);
313 seq_setprev (mp); /* set the previous-sequence */
314
315 cts = mh_xcalloc(mp->numsel + 1, sizeof *cts);
316 ctp = cts;
317
318 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
319 if (is_selected(mp, msgnum)) {
320 char *msgnam;
321
322 msgnam = m_name (msgnum);
323 if ((ct = parse_mime (msgnam))) {
324 *ctp++ = ct;
325 if (outfile) {
326 ct->c_storage = mh_xstrdup(outfile);
327 }
328 }
329 }
330 }
331 }
332
333 if (!*cts)
334 done (1);
335
336 userrs = true;
337 SIGNAL (SIGQUIT, quitser);
338 SIGNAL (SIGPIPE, pipeser);
339
340 /*
341 * Get the associated umask for the relevant contents.
342 */
343 for (ctp = cts; *ctp; ctp++) {
344 struct stat st;
345
346 ct = *ctp;
347 if (type_ok (ct, 1) && !ct->c_umask) {
348 if (stat (ct->c_file, &st) != NOTOK)
349 ct->c_umask = ~(st.st_mode & 0777);
350 else
351 ct->c_umask = ~m_gmprot();
352 }
353 }
354
355 /*
356 * Store the message content
357 */
358 info = mhstoreinfo_create (cts, cwd, clobbersw, autosw, verbosw);
359 store_all_messages (info);
360 files_not_clobbered = mhstoreinfo_files_not_clobbered(info);
361 mhstoreinfo_free(info);
362
363 /* Now free all the structures for the content */
364 for (ctp = cts; *ctp; ctp++)
365 free_content (*ctp);
366
367 free (cts);
368 cts = NULL;
369
370 /* If reading from a folder, do some updating */
371 if (mp) {
372 context_replace (pfolder, folder);/* update current folder */
373 seq_setcur (mp, mp->hghsel); /* update current message */
374 seq_save (mp); /* synchronize sequences */
375 context_save (); /* save the context file */
376 }
377
378 done (files_not_clobbered);
379 return 1;
380 }
381
382
383 static void
384 pipeser (int i)
385 {
386 if (i == SIGQUIT) {
387 fflush (stdout);
388 fprintf (stderr, "\n");
389 fflush (stderr);
390 }
391
392 done (1);
393 /* NOTREACHED */
394 }