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