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