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