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