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