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